2018-05-15 16:11:03 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-10-18 12:10:29 +01:00
|
|
|
package encryption
|
2018-05-15 16:11:03 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
2018-09-26 14:32:23 +01:00
|
|
|
|
2018-10-19 14:38:13 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-05-15 16:11:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type aesgcmEncrypter struct {
|
|
|
|
blockSize int
|
2018-10-19 14:38:13 +01:00
|
|
|
key *storj.Key
|
|
|
|
startingNonce *AESGCMNonce
|
2018-05-15 16:11:03 +01:00
|
|
|
overhead int
|
|
|
|
aesgcm cipher.AEAD
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAESGCMEncrypter returns a Transformer that encrypts the data passing
|
|
|
|
// through with key.
|
|
|
|
//
|
|
|
|
// startingNonce is treated as a big-endian encoded unsigned
|
|
|
|
// integer, and as blocks pass through, their block number and the starting
|
|
|
|
// nonce is added together to come up with that block's nonce. Encrypting
|
|
|
|
// different data with the same key and the same nonce is a huge security
|
|
|
|
// issue. It's safe to always encode new data with a random key and random
|
|
|
|
// startingNonce. The monotonically-increasing nonce (that rolls over) is to
|
|
|
|
// protect against data reordering.
|
|
|
|
//
|
|
|
|
// When in doubt, generate a new key from crypto/rand and a startingNonce
|
|
|
|
// from crypto/rand as often as possible.
|
2018-10-19 14:38:13 +01:00
|
|
|
func NewAESGCMEncrypter(key *storj.Key, startingNonce *AESGCMNonce, encryptedBlockSize int) (Transformer, error) {
|
|
|
|
block, err := aes.NewCipher(key[:])
|
2018-05-15 16:11:03 +01:00
|
|
|
if err != nil {
|
2018-10-23 15:05:15 +01:00
|
|
|
return nil, Error.Wrap(err)
|
2018-05-15 16:11:03 +01:00
|
|
|
}
|
|
|
|
aesgcmEncrypt, err := cipher.NewGCM(block)
|
|
|
|
if err != nil {
|
2018-10-23 15:05:15 +01:00
|
|
|
return nil, Error.Wrap(err)
|
2018-05-15 16:11:03 +01:00
|
|
|
}
|
|
|
|
if encryptedBlockSize <= aesgcmEncrypt.Overhead() {
|
2018-10-23 15:05:15 +01:00
|
|
|
return nil, ErrInvalidConfig.New("encrypted block size %d too small", encryptedBlockSize)
|
2018-05-15 16:11:03 +01:00
|
|
|
}
|
|
|
|
return &aesgcmEncrypter{
|
|
|
|
blockSize: encryptedBlockSize - aesgcmEncrypt.Overhead(),
|
2018-10-19 14:38:13 +01:00
|
|
|
key: key,
|
|
|
|
startingNonce: startingNonce,
|
2018-05-15 16:11:03 +01:00
|
|
|
overhead: aesgcmEncrypt.Overhead(),
|
|
|
|
aesgcm: aesgcmEncrypt,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *aesgcmEncrypter) InBlockSize() int {
|
|
|
|
return s.blockSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *aesgcmEncrypter) OutBlockSize() int {
|
|
|
|
return s.blockSize + s.overhead
|
|
|
|
}
|
|
|
|
|
2018-09-26 14:32:23 +01:00
|
|
|
func calcGCMNonce(startingNonce *AESGCMNonce, blockNum int64) (rv [12]byte, err error) {
|
2018-05-15 16:11:03 +01:00
|
|
|
if copy(rv[:], (*startingNonce)[:]) != len(rv) {
|
|
|
|
return rv, Error.New("didn't copy memory?!")
|
|
|
|
}
|
|
|
|
_, err = incrementBytes(rv[:], blockNum)
|
|
|
|
return rv, err
|
|
|
|
}
|
|
|
|
|
2018-09-26 14:32:23 +01:00
|
|
|
func (s *aesgcmEncrypter) Transform(out, in []byte, blockNum int64) ([]byte, error) {
|
2018-10-19 14:38:13 +01:00
|
|
|
nonce, err := calcGCMNonce(s.startingNonce, blockNum)
|
2018-05-15 16:11:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-23 15:05:15 +01:00
|
|
|
cipherData := s.aesgcm.Seal(out, nonce[:], in, nil)
|
|
|
|
return cipherData, nil
|
2018-05-15 16:11:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type aesgcmDecrypter struct {
|
|
|
|
blockSize int
|
2018-10-19 14:38:13 +01:00
|
|
|
key *storj.Key
|
|
|
|
startingNonce *AESGCMNonce
|
2018-05-15 16:11:03 +01:00
|
|
|
overhead int
|
|
|
|
aesgcm cipher.AEAD
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAESGCMDecrypter returns a Transformer that decrypts the data passing
|
|
|
|
// through with key. See the comments for NewAESGCMEncrypter about
|
|
|
|
// startingNonce.
|
2018-10-19 14:38:13 +01:00
|
|
|
func NewAESGCMDecrypter(key *storj.Key, startingNonce *AESGCMNonce, encryptedBlockSize int) (Transformer, error) {
|
2018-10-03 14:05:40 +01:00
|
|
|
block, err := aes.NewCipher(key[:])
|
2018-05-15 16:11:03 +01:00
|
|
|
if err != nil {
|
2018-10-23 15:05:15 +01:00
|
|
|
return nil, Error.Wrap(err)
|
2018-05-15 16:11:03 +01:00
|
|
|
}
|
|
|
|
aesgcmDecrypt, err := cipher.NewGCM(block)
|
|
|
|
if err != nil {
|
2018-10-23 15:05:15 +01:00
|
|
|
return nil, Error.Wrap(err)
|
2018-05-15 16:11:03 +01:00
|
|
|
}
|
|
|
|
if encryptedBlockSize <= aesgcmDecrypt.Overhead() {
|
2018-10-23 15:05:15 +01:00
|
|
|
return nil, ErrInvalidConfig.New("encrypted block size %d too small", encryptedBlockSize)
|
2018-05-15 16:11:03 +01:00
|
|
|
}
|
|
|
|
return &aesgcmDecrypter{
|
|
|
|
blockSize: encryptedBlockSize - aesgcmDecrypt.Overhead(),
|
2018-10-19 14:38:13 +01:00
|
|
|
key: key,
|
|
|
|
startingNonce: startingNonce,
|
2018-05-15 16:11:03 +01:00
|
|
|
overhead: aesgcmDecrypt.Overhead(),
|
|
|
|
aesgcm: aesgcmDecrypt,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
func (s *aesgcmDecrypter) InBlockSize() int {
|
|
|
|
return s.blockSize + s.overhead
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *aesgcmDecrypter) OutBlockSize() int {
|
|
|
|
return s.blockSize
|
|
|
|
}
|
|
|
|
|
2018-09-26 14:32:23 +01:00
|
|
|
func (s *aesgcmDecrypter) Transform(out, in []byte, blockNum int64) ([]byte, error) {
|
2018-10-19 14:38:13 +01:00
|
|
|
nonce, err := calcGCMNonce(s.startingNonce, blockNum)
|
2018-05-15 16:11:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-23 15:05:15 +01:00
|
|
|
plainData, err := s.aesgcm.Open(out, nonce[:], in, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrDecryptFailed.Wrap(err)
|
|
|
|
}
|
|
|
|
return plainData, nil
|
2018-05-15 16:11:03 +01:00
|
|
|
}
|
2018-09-26 14:32:23 +01:00
|
|
|
|
|
|
|
// EncryptAESGCM encrypts byte data with a key and nonce. The cipher data is returned
|
2018-10-19 14:38:13 +01:00
|
|
|
func EncryptAESGCM(data []byte, key *storj.Key, nonce *AESGCMNonce) (cipherData []byte, err error) {
|
2018-10-03 14:05:40 +01:00
|
|
|
block, err := aes.NewCipher(key[:])
|
2018-09-26 14:32:23 +01:00
|
|
|
if err != nil {
|
2018-10-23 15:05:15 +01:00
|
|
|
return []byte{}, Error.Wrap(err)
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
|
|
|
aesgcm, err := cipher.NewGCM(block)
|
|
|
|
if err != nil {
|
2018-10-23 15:05:15 +01:00
|
|
|
return []byte{}, Error.Wrap(err)
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
2018-10-03 14:05:40 +01:00
|
|
|
cipherData = aesgcm.Seal(nil, nonce[:], data, nil)
|
2018-09-26 14:32:23 +01:00
|
|
|
return cipherData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptAESGCM decrypts byte data with a key and nonce. The plain data is returned
|
2018-10-19 14:38:13 +01:00
|
|
|
func DecryptAESGCM(cipherData []byte, key *storj.Key, nonce *AESGCMNonce) (data []byte, err error) {
|
2018-09-26 14:32:23 +01:00
|
|
|
if len(cipherData) == 0 {
|
2018-10-23 15:05:15 +01:00
|
|
|
return []byte{}, Error.New("empty cipher data")
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
2018-10-03 14:05:40 +01:00
|
|
|
block, err := aes.NewCipher(key[:])
|
2018-09-26 14:32:23 +01:00
|
|
|
if err != nil {
|
2018-10-23 15:05:15 +01:00
|
|
|
return []byte{}, Error.Wrap(err)
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
|
|
|
aesgcm, err := cipher.NewGCM(block)
|
|
|
|
if err != nil {
|
2018-10-23 15:05:15 +01:00
|
|
|
return []byte{}, Error.Wrap(err)
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
2018-10-23 15:05:15 +01:00
|
|
|
plainData, err := aesgcm.Open(nil, nonce[:], cipherData, nil)
|
2018-09-26 14:32:23 +01:00
|
|
|
if err != nil {
|
2018-10-23 15:05:15 +01:00
|
|
|
return []byte{}, ErrDecryptFailed.Wrap(err)
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
2018-10-23 15:05:15 +01:00
|
|
|
return plainData, nil
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|