2020-08-10 17:31:21 +01:00
|
|
|
package mucp
|
2019-11-25 14:58:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
2020-02-24 17:15:20 +03:00
|
|
|
|
2020-08-10 17:31:21 +01:00
|
|
|
"github.com/micro/go-micro/v3/tunnel"
|
2020-02-24 17:15:20 +03:00
|
|
|
"github.com/oxtoacart/bpool"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// the local buffer pool
|
|
|
|
// gcmStandardNonceSize from crypto/cipher/gcm.go is 12 bytes
|
|
|
|
// 100 - is max size of pool
|
|
|
|
noncePool = bpool.NewBytePool(100, 12)
|
2019-11-25 14:58:12 +00:00
|
|
|
)
|
|
|
|
|
2019-11-25 18:56:00 +00:00
|
|
|
// hash hahes the data into 32 bytes key and returns it
|
|
|
|
// hash uses sha256 underneath to hash the supplied key
|
2020-03-09 20:10:08 +03:00
|
|
|
func hash(key []byte) []byte {
|
|
|
|
sum := sha256.Sum256(key)
|
|
|
|
return sum[:]
|
2019-11-25 18:56:00 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 15:34:41 +00:00
|
|
|
// Encrypt encrypts data and returns the encrypted data
|
2020-03-11 20:55:39 +03:00
|
|
|
func Encrypt(gcm cipher.AEAD, data []byte) ([]byte, error) {
|
|
|
|
var err error
|
2019-11-25 14:58:12 +00:00
|
|
|
|
2020-02-24 17:15:20 +03:00
|
|
|
// get new byte array the size of the nonce from pool
|
2019-11-25 14:58:12 +00:00
|
|
|
// NOTE: we might use smaller nonce size in the future
|
2020-02-24 17:15:20 +03:00
|
|
|
nonce := noncePool.Get()
|
|
|
|
if _, err = rand.Read(nonce); err != nil {
|
2019-11-25 14:58:12 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-24 17:15:20 +03:00
|
|
|
defer noncePool.Put(nonce)
|
2019-11-25 14:58:12 +00:00
|
|
|
|
|
|
|
// NOTE: we prepend the nonce to the payload
|
|
|
|
// we need to do this as we need the same nonce
|
|
|
|
// to decrypt the payload when receiving it
|
|
|
|
return gcm.Seal(nonce, nonce, data, nil), nil
|
|
|
|
}
|
|
|
|
|
2019-11-25 15:34:41 +00:00
|
|
|
// Decrypt decrypts the payload and returns the decrypted data
|
2020-03-11 20:55:39 +03:00
|
|
|
func newCipher(key []byte) (cipher.AEAD, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// generate a new AES cipher using our 32 byte key for decrypting the message
|
2019-11-25 14:58:12 +00:00
|
|
|
c, err := aes.NewCipher(hash(key))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-11 20:55:39 +03:00
|
|
|
// gcm or Galois/Counter Mode, is a mode of operation
|
|
|
|
// for symmetric key cryptographic block ciphers
|
|
|
|
// - https://en.wikipedia.org/wiki/Galois/Counter_Mode
|
2019-11-25 14:58:12 +00:00
|
|
|
gcm, err := cipher.NewGCM(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-11 20:55:39 +03:00
|
|
|
return gcm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Decrypt(gcm cipher.AEAD, data []byte) ([]byte, error) {
|
|
|
|
var err error
|
|
|
|
|
2019-11-25 14:58:12 +00:00
|
|
|
nonceSize := gcm.NonceSize()
|
2019-12-12 17:10:32 +00:00
|
|
|
|
|
|
|
if len(data) < nonceSize {
|
2020-08-10 17:31:21 +01:00
|
|
|
return nil, tunnel.ErrDecryptingData
|
2019-12-12 17:10:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 14:58:12 +00:00
|
|
|
// NOTE: we need to parse out nonce from the payload
|
|
|
|
// we prepend the nonce to every encrypted payload
|
|
|
|
nonce, ciphertext := data[:nonceSize], data[nonceSize:]
|
2020-03-09 20:10:08 +03:00
|
|
|
ciphertext, err = gcm.Open(ciphertext[:0], nonce, ciphertext, nil)
|
2019-11-25 14:58:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:10:08 +03:00
|
|
|
return ciphertext, nil
|
2019-11-25 14:58:12 +00:00
|
|
|
}
|