2016-01-22 13:15:39 -06:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2021-10-29 06:20:07 -05:00
|
|
|
"bytes"
|
2016-01-22 13:15:39 -06:00
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/rand"
|
2016-01-25 15:15:29 -06:00
|
|
|
"crypto/sha256"
|
2021-10-29 06:20:07 -05:00
|
|
|
"encoding/base64"
|
2017-04-25 02:14:29 -05:00
|
|
|
"errors"
|
2021-05-25 10:35:54 -05:00
|
|
|
"fmt"
|
2016-01-22 13:15:39 -06:00
|
|
|
"io"
|
2020-06-29 07:08:32 -05:00
|
|
|
|
|
|
|
"golang.org/x/crypto/pbkdf2"
|
2016-01-22 13:15:39 -06:00
|
|
|
)
|
|
|
|
|
2021-10-29 06:20:07 -05:00
|
|
|
const (
|
|
|
|
saltLength = 8
|
|
|
|
aesCfb = "aes-cfb"
|
|
|
|
aesGcm = "aes-gcm"
|
|
|
|
encryptionAlgorithmDelimiter = '*'
|
|
|
|
)
|
2016-01-25 15:15:29 -06:00
|
|
|
|
2019-01-28 15:09:40 -06:00
|
|
|
// Decrypt decrypts a payload with a given secret.
|
2021-11-04 11:47:21 -05:00
|
|
|
// DEPRECATED. Do not use it.
|
|
|
|
// Use secrets.Service instead.
|
2021-10-07 09:33:50 -05:00
|
|
|
func Decrypt(payload []byte, secret string) ([]byte, error) {
|
2021-10-29 06:20:07 -05:00
|
|
|
alg, payload, err := deriveEncryptionAlgorithm(payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-25 10:35:54 -05:00
|
|
|
if len(payload) < saltLength {
|
|
|
|
return nil, fmt.Errorf("unable to compute salt")
|
|
|
|
}
|
2016-01-25 15:15:29 -06:00
|
|
|
salt := payload[:saltLength]
|
2019-10-23 03:40:12 -05:00
|
|
|
key, err := encryptionKeyToBytes(secret, string(salt))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-22 13:15:39 -06:00
|
|
|
|
|
|
|
block, err := aes.NewCipher(key)
|
|
|
|
if err != nil {
|
2017-04-25 02:14:29 -05:00
|
|
|
return nil, err
|
2016-01-22 13:15:39 -06:00
|
|
|
}
|
|
|
|
|
2021-10-29 06:20:07 -05:00
|
|
|
switch alg {
|
|
|
|
case aesGcm:
|
|
|
|
return decryptGCM(block, payload)
|
|
|
|
default:
|
|
|
|
return decryptCFB(block, payload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deriveEncryptionAlgorithm(payload []byte) (string, []byte, error) {
|
|
|
|
if len(payload) == 0 {
|
|
|
|
return "", nil, fmt.Errorf("unable to derive encryption algorithm")
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload[0] != encryptionAlgorithmDelimiter {
|
|
|
|
return aesCfb, payload, nil // backwards compatibility
|
|
|
|
}
|
|
|
|
|
|
|
|
payload = payload[1:]
|
|
|
|
algDelim := bytes.Index(payload, []byte{encryptionAlgorithmDelimiter})
|
|
|
|
if algDelim == -1 {
|
|
|
|
return aesCfb, payload, nil // backwards compatibility
|
|
|
|
}
|
|
|
|
|
|
|
|
algB64 := payload[:algDelim]
|
|
|
|
payload = payload[algDelim+1:]
|
|
|
|
|
|
|
|
alg := make([]byte, base64.RawStdEncoding.DecodedLen(len(algB64)))
|
|
|
|
|
|
|
|
_, err := base64.RawStdEncoding.Decode(alg, algB64)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(alg), payload, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decryptGCM(block cipher.Block, payload []byte) ([]byte, error) {
|
|
|
|
gcm, err := cipher.NewGCM(block)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nonce := payload[saltLength : saltLength+gcm.NonceSize()]
|
|
|
|
ciphertext := payload[saltLength+gcm.NonceSize():]
|
|
|
|
return gcm.Open(nil, nonce, ciphertext, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func decryptCFB(block cipher.Block, payload []byte) ([]byte, error) {
|
2016-01-22 13:15:39 -06:00
|
|
|
// The IV needs to be unique, but not secure. Therefore it's common to
|
|
|
|
// include it at the beginning of the ciphertext.
|
|
|
|
if len(payload) < aes.BlockSize {
|
2017-04-25 02:14:29 -05:00
|
|
|
return nil, errors.New("payload too short")
|
2016-01-22 13:15:39 -06:00
|
|
|
}
|
2021-10-29 06:20:07 -05:00
|
|
|
|
2016-01-25 15:15:29 -06:00
|
|
|
iv := payload[saltLength : saltLength+aes.BlockSize]
|
|
|
|
payload = payload[saltLength+aes.BlockSize:]
|
2017-09-08 03:19:04 -05:00
|
|
|
payloadDst := make([]byte, len(payload))
|
2016-01-22 13:15:39 -06:00
|
|
|
|
|
|
|
stream := cipher.NewCFBDecrypter(block, iv)
|
|
|
|
|
|
|
|
// XORKeyStream can work in-place if the two arguments are the same.
|
2017-09-08 03:19:04 -05:00
|
|
|
stream.XORKeyStream(payloadDst, payload)
|
|
|
|
return payloadDst, nil
|
2016-01-22 13:15:39 -06:00
|
|
|
}
|
|
|
|
|
2019-01-28 15:09:40 -06:00
|
|
|
// Encrypt encrypts a payload with a given secret.
|
2021-11-04 11:47:21 -05:00
|
|
|
// DEPRECATED. Do not use it.
|
|
|
|
// Use secrets.Service instead.
|
2021-10-07 09:33:50 -05:00
|
|
|
func Encrypt(payload []byte, secret string) ([]byte, error) {
|
2019-10-23 03:40:12 -05:00
|
|
|
salt, err := GetRandomString(saltLength)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-22 13:15:39 -06:00
|
|
|
|
2019-10-23 03:40:12 -05:00
|
|
|
key, err := encryptionKeyToBytes(secret, salt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-22 13:15:39 -06:00
|
|
|
block, err := aes.NewCipher(key)
|
|
|
|
if err != nil {
|
2017-04-25 02:14:29 -05:00
|
|
|
return nil, err
|
2016-01-22 13:15:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// The IV needs to be unique, but not secure. Therefore it's common to
|
|
|
|
// include it at the beginning of the ciphertext.
|
2016-01-25 15:15:29 -06:00
|
|
|
ciphertext := make([]byte, saltLength+aes.BlockSize+len(payload))
|
2020-09-22 09:22:19 -05:00
|
|
|
copy(ciphertext[:saltLength], salt)
|
2016-01-25 15:15:29 -06:00
|
|
|
iv := ciphertext[saltLength : saltLength+aes.BlockSize]
|
2016-01-22 13:15:39 -06:00
|
|
|
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
2017-04-25 02:14:29 -05:00
|
|
|
return nil, err
|
2016-01-22 13:15:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
stream := cipher.NewCFBEncrypter(block, iv)
|
2016-01-25 15:15:29 -06:00
|
|
|
stream.XORKeyStream(ciphertext[saltLength+aes.BlockSize:], payload)
|
2016-01-22 13:15:39 -06:00
|
|
|
|
2017-04-25 02:14:29 -05:00
|
|
|
return ciphertext, nil
|
2016-01-22 13:15:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Key needs to be 32bytes
|
2019-10-23 03:40:12 -05:00
|
|
|
func encryptionKeyToBytes(secret, salt string) ([]byte, error) {
|
2019-11-06 02:03:10 -06:00
|
|
|
return pbkdf2.Key([]byte(secret), []byte(salt), 10000, 32, sha256.New), nil
|
2016-01-22 13:15:39 -06:00
|
|
|
}
|