2019-05-24 18:06:48 +02:00
|
|
|
package encryption
|
2015-06-23 13:23:39 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2020-05-10 02:01:51 +02:00
|
|
|
// Cipher provides methods to encrypt and decrypt
|
|
|
|
type Cipher interface {
|
|
|
|
Encrypt(value []byte) ([]byte, error)
|
|
|
|
Decrypt(ciphertext []byte) ([]byte, error)
|
|
|
|
EncryptInto(s *string) error
|
|
|
|
DecryptInto(s *string) error
|
2015-06-23 13:23:39 +02:00
|
|
|
}
|
|
|
|
|
2020-05-10 02:34:32 +02:00
|
|
|
type DefaultCipher struct {}
|
|
|
|
|
|
|
|
// Encrypt is a dummy method for CommonCipher.EncryptInto support
|
|
|
|
func (c *DefaultCipher) Encrypt(value []byte) ([]byte, error) { return value, nil }
|
|
|
|
|
|
|
|
// Decrypt is a dummy method for CommonCipher.DecryptInto support
|
|
|
|
func (c *DefaultCipher) Decrypt(ciphertext []byte) ([]byte, error) { return ciphertext, nil }
|
|
|
|
|
|
|
|
// EncryptInto encrypts the value and stores it back in the string pointer
|
|
|
|
func (c *DefaultCipher) EncryptInto(s *string) error {
|
|
|
|
return into(c.Encrypt, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptInto decrypts the value and stores it back in the string pointer
|
|
|
|
func (c *DefaultCipher) DecryptInto(s *string) error {
|
|
|
|
return into(c.Decrypt, s)
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:01:51 +02:00
|
|
|
type Base64Cipher struct {
|
2020-05-10 02:34:32 +02:00
|
|
|
DefaultCipher
|
2020-05-10 02:01:51 +02:00
|
|
|
Cipher Cipher
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:34:32 +02:00
|
|
|
// NewBase64Cipher returns a new AES Cipher for encrypting cookie values
|
|
|
|
// and wrapping them in Base64 -- Supports Legacy encryption scheme
|
2020-05-10 18:44:04 +02:00
|
|
|
func NewBase64Cipher(initCipher func([]byte) (Cipher, error), secret []byte) (Cipher, error) {
|
|
|
|
c, err := initCipher(secret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-10 02:01:51 +02:00
|
|
|
return &Base64Cipher{Cipher: c}, nil
|
2015-06-23 13:23:39 +02:00
|
|
|
}
|
|
|
|
|
2020-05-11 21:27:27 +02:00
|
|
|
// Encrypt encrypts a value with the embedded Cipher & Base64 encodes it
|
2020-05-10 02:01:51 +02:00
|
|
|
func (c *Base64Cipher) Encrypt(value []byte) ([]byte, error) {
|
|
|
|
encrypted, err := c.Cipher.Encrypt([]byte(value))
|
2020-05-04 20:21:25 +02:00
|
|
|
if err != nil {
|
2020-05-10 02:01:51 +02:00
|
|
|
return nil, err
|
2015-06-23 13:23:39 +02:00
|
|
|
}
|
|
|
|
|
2020-05-10 02:01:51 +02:00
|
|
|
return []byte(base64.StdEncoding.EncodeToString(encrypted)), nil
|
2015-06-23 13:23:39 +02:00
|
|
|
}
|
|
|
|
|
2020-05-11 21:27:27 +02:00
|
|
|
// Decrypt Base64 decodes a value & decrypts it with the embedded Cipher
|
2020-05-10 02:01:51 +02:00
|
|
|
func (c *Base64Cipher) Decrypt(ciphertext []byte) ([]byte, error) {
|
|
|
|
encrypted, err := base64.StdEncoding.DecodeString(string(ciphertext))
|
2015-06-23 13:23:39 +02:00
|
|
|
if err != nil {
|
2020-05-10 18:44:04 +02:00
|
|
|
return nil, fmt.Errorf("failed to base64 decode value %s", err)
|
2015-06-23 13:23:39 +02:00
|
|
|
}
|
|
|
|
|
2020-05-10 02:01:51 +02:00
|
|
|
return c.Cipher.Decrypt(encrypted)
|
|
|
|
}
|
|
|
|
|
|
|
|
type CFBCipher struct {
|
2020-05-10 02:34:32 +02:00
|
|
|
DefaultCipher
|
2020-05-10 02:01:51 +02:00
|
|
|
cipher.Block
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCFBCipher returns a new AES CFB Cipher
|
2020-05-10 02:34:32 +02:00
|
|
|
func NewCFBCipher(secret []byte) (Cipher, error) {
|
2020-05-10 02:01:51 +02:00
|
|
|
c, err := aes.NewCipher(secret)
|
2020-05-04 20:21:25 +02:00
|
|
|
if err != nil {
|
2020-05-10 02:01:51 +02:00
|
|
|
return nil, err
|
2020-05-04 20:21:25 +02:00
|
|
|
}
|
2020-05-10 02:01:51 +02:00
|
|
|
return &CFBCipher{Block: c}, err
|
2020-05-04 20:21:25 +02:00
|
|
|
}
|
|
|
|
|
2020-05-10 02:01:51 +02:00
|
|
|
// Encrypt with AES CFB
|
|
|
|
func (c *CFBCipher) Encrypt(value []byte) ([]byte, error) {
|
2020-05-04 20:21:25 +02:00
|
|
|
ciphertext := make([]byte, aes.BlockSize+len(value))
|
|
|
|
iv := ciphertext[:aes.BlockSize]
|
|
|
|
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create initialization vector %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stream := cipher.NewCFBEncrypter(c.Block, iv)
|
|
|
|
stream.XORKeyStream(ciphertext[aes.BlockSize:], value)
|
|
|
|
return ciphertext, nil
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:34:32 +02:00
|
|
|
// Decrypt an AES CFB ciphertext
|
2020-05-10 02:01:51 +02:00
|
|
|
func (c *CFBCipher) Decrypt(ciphertext []byte) ([]byte, error) {
|
2020-05-04 20:21:25 +02:00
|
|
|
if len(ciphertext) < aes.BlockSize {
|
2020-05-10 18:44:04 +02:00
|
|
|
return nil, fmt.Errorf("encrypted value should be at least %d bytes, but is only %d bytes", aes.BlockSize, len(ciphertext))
|
2015-06-23 13:23:39 +02:00
|
|
|
}
|
|
|
|
|
2020-05-11 21:27:27 +02:00
|
|
|
iv, ciphertext := ciphertext[:aes.BlockSize], ciphertext[aes.BlockSize:]
|
|
|
|
plaintext := make([]byte, len(ciphertext))
|
2015-06-23 13:23:39 +02:00
|
|
|
stream := cipher.NewCFBDecrypter(c.Block, iv)
|
2020-05-11 21:27:27 +02:00
|
|
|
stream.XORKeyStream(plaintext, ciphertext)
|
2015-06-23 13:23:39 +02:00
|
|
|
|
2020-05-11 21:27:27 +02:00
|
|
|
return plaintext, nil
|
2015-06-23 13:23:39 +02:00
|
|
|
}
|
2020-05-30 09:53:38 +02:00
|
|
|
|
2020-05-10 02:34:32 +02:00
|
|
|
type GCMCipher struct {
|
|
|
|
DefaultCipher
|
|
|
|
cipher.Block
|
2020-05-30 09:53:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-10 02:34:32 +02:00
|
|
|
// NewGCMCipher returns a new AES GCM Cipher
|
|
|
|
func NewGCMCipher(secret []byte) (Cipher, error) {
|
|
|
|
c, err := aes.NewCipher(secret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &GCMCipher{Block: c}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypt with AES GCM on raw bytes
|
|
|
|
func (c *GCMCipher) Encrypt(value []byte) ([]byte, error) {
|
|
|
|
gcm, err := cipher.NewGCM(c.Block)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nonce := make([]byte, gcm.NonceSize())
|
|
|
|
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-11 21:27:27 +02:00
|
|
|
// Using nonce as Seal's dst argument results in it being the first
|
|
|
|
// chunk of bytes in the ciphertext. Decrypt retrieves the nonce/IV from this.
|
2020-05-10 02:34:32 +02:00
|
|
|
ciphertext := gcm.Seal(nonce, nonce, value, nil)
|
|
|
|
return ciphertext, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrypt an AES GCM ciphertext
|
|
|
|
func (c *GCMCipher) Decrypt(ciphertext []byte) ([]byte, error) {
|
|
|
|
gcm, err := cipher.NewGCM(c.Block)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nonceSize := gcm.NonceSize()
|
|
|
|
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
|
|
|
|
|
|
|
|
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return plaintext, nil
|
2020-05-30 09:53:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// codecFunc is a function that takes a string and encodes/decodes it
|
2020-05-10 02:01:51 +02:00
|
|
|
type codecFunc func([]byte) ([]byte, error)
|
|
|
|
|
2020-05-30 09:53:38 +02:00
|
|
|
func into(f codecFunc, s *string) error {
|
|
|
|
// Do not encrypt/decrypt nil or empty strings
|
|
|
|
if s == nil || *s == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:01:51 +02:00
|
|
|
d, err := f([]byte(*s))
|
2020-05-30 09:53:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-10 02:01:51 +02:00
|
|
|
*s = string(d)
|
2020-05-30 09:53:38 +02:00
|
|
|
return nil
|
|
|
|
}
|