subtle.mx raw

   1  package subtle
   2  
   3  // The Web Crypto surface, implemented on the stdlib's own primitives. Every
   4  // one of these operations is arithmetic, not a host capability: the same
   5  // crypto/aes, crypto/hmac, crypto/pbkdf2 and crypto/sha* code runs on the
   6  // native and the wasm target, and only randomness has a target-specific
   7  // source (crypto/rand provides it). A per-target stub file meant the native
   8  // build could not do any of it, which is why nothing that touched a cipher
   9  // could be tested without a browser.
  10  
  11  import (
  12  	"crypto/aes"
  13  	"crypto/aes256gcm"
  14  	"crypto/cipher"
  15  	"crypto/hmac"
  16  	"crypto/pbkdf2"
  17  	"crypto/rand"
  18  	"crypto/sha256"
  19  	"crypto/sha512"
  20  	"vendor/golang.org/x/crypto/argon2"
  21  )
  22  
  23  func RandomBytes(dst []byte) {
  24  	rand.Read(dst)
  25  }
  26  
  27  func AESCBCEncrypt(key, iv, plaintext []byte) (buf []byte) {
  28  	block, err := aes.NewCipher(key)
  29  	if err != nil {
  30  		return nil
  31  	}
  32  	padded := pkcs7Pad(plaintext, aes.BlockSize)
  33  	ct := []byte{:len(padded)}
  34  	cipher.NewCBCEncrypter(block, iv).CryptBlocks(ct, padded)
  35  	return ct
  36  }
  37  
  38  func AESCBCDecrypt(key, iv, ciphertext []byte) (buf []byte) {
  39  	block, err := aes.NewCipher(key)
  40  	if err != nil {
  41  		return nil
  42  	}
  43  	pt := []byte{:len(ciphertext)}
  44  	cipher.NewCBCDecrypter(block, iv).CryptBlocks(pt, ciphertext)
  45  	return pkcs7Unpad(pt, aes.BlockSize)
  46  }
  47  
  48  func AESGCMEncrypt(key, iv, plaintext []byte) (buf []byte) {
  49  	return aes256gcm.Seal(key, iv, plaintext, nil)
  50  }
  51  
  52  func AESGCMDecrypt(key, iv, ciphertext []byte) (buf []byte) {
  53  	pt, ok := aes256gcm.Open(key, iv, ciphertext, nil)
  54  	if !ok {
  55  		return nil
  56  	}
  57  	return pt
  58  }
  59  
  60  func PBKDF2DeriveKey(password string, salt []byte, iterations int32) (buf []byte) {
  61  	dk, _ := pbkdf2.Key(sha256.New, []byte(password), salt, iterations, 32)
  62  	return dk
  63  }
  64  
  65  func Argon2idDeriveKey(password string, salt []byte, t, m, p, dkLen int32) (buf []byte) {
  66  	return argon2.IDKey([]byte(password), salt, uint32(t), uint32(m), uint8(p), uint32(dkLen))
  67  }
  68  
  69  func SHA256Hex(data []byte) (s string) {
  70  	return sha256.SumHex(data)
  71  }
  72  
  73  func HMACSHA512(key, data []byte) (buf []byte) {
  74  	mac := hmac.New(sha512.New, key)
  75  	mac.Write(data)
  76  	return mac.Sum(nil)
  77  }
  78  
  79  func PBKDF2SHA512(password string, salt []byte, iterations, dkLen int32) (buf []byte) {
  80  	dk, _ := pbkdf2.Key(sha512.New, []byte(password), salt, iterations, dkLen)
  81  	return dk
  82  }
  83  
  84  func pkcs7Pad(data []byte, blockSize int32) (buf []byte) {
  85  	pad := blockSize - len(data)%blockSize
  86  	out := []byte{:0:len(data) + pad}
  87  	out = out | data
  88  	for i := 0; i < pad; i++ {
  89  		out = push(out, byte(pad))
  90  	}
  91  	return out
  92  }
  93  
  94  func pkcs7Unpad(data []byte, blockSize int32) (buf []byte) {
  95  	if len(data) == 0 {
  96  		return nil
  97  	}
  98  	pad := int32(data[len(data)-1])
  99  	if pad > blockSize || pad == 0 || pad > len(data) {
 100  		return nil
 101  	}
 102  	for i := len(data) - pad; i < len(data); i++ {
 103  		if int32(data[i]) != pad {
 104  			return nil
 105  		}
 106  	}
 107  	return data[:len(data)-pad]
 108  }
 109