2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-09-26 14:32:23 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-10-18 12:10:29 +01:00
|
|
|
package encryption
|
2018-09-26 14:32:23 +01:00
|
|
|
|
|
|
|
import (
|
2018-10-25 21:28:16 +01:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha512"
|
|
|
|
|
2018-10-19 14:38:13 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-09-26 14:32:23 +01:00
|
|
|
)
|
|
|
|
|
2019-03-21 13:47:48 +00:00
|
|
|
const (
|
|
|
|
// AESGCMNonceSize is the size of an AES-GCM nonce
|
|
|
|
AESGCMNonceSize = 12
|
|
|
|
// unit32Size is the number of bytes in the uint32 type
|
|
|
|
uint32Size = 4
|
|
|
|
)
|
2018-09-26 14:32:23 +01:00
|
|
|
|
2018-10-19 14:38:13 +01:00
|
|
|
// AESGCMNonce represents the nonce used by the AES-GCM protocol
|
|
|
|
type AESGCMNonce [AESGCMNonceSize]byte
|
2018-09-26 14:32:23 +01:00
|
|
|
|
2018-10-19 14:38:13 +01:00
|
|
|
// ToAESGCMNonce returns the nonce as a AES-GCM nonce
|
|
|
|
func ToAESGCMNonce(nonce *storj.Nonce) *AESGCMNonce {
|
2018-09-26 14:32:23 +01:00
|
|
|
aes := new(AESGCMNonce)
|
2018-10-03 14:05:40 +01:00
|
|
|
copy((*aes)[:], nonce[:AESGCMNonceSize])
|
2018-09-26 14:32:23 +01:00
|
|
|
return aes
|
|
|
|
}
|
|
|
|
|
2018-10-19 14:38:13 +01:00
|
|
|
// Increment increments the nonce with the given amount
|
|
|
|
func Increment(nonce *storj.Nonce, amount int64) (truncated bool, err error) {
|
|
|
|
return incrementBytes(nonce[:], amount)
|
2018-10-18 15:21:08 +01:00
|
|
|
}
|
|
|
|
|
2018-10-19 14:38:13 +01:00
|
|
|
// Encrypt encrypts data with the given cipher, key and nonce
|
2019-07-03 19:07:44 +01:00
|
|
|
func Encrypt(data []byte, cipher storj.CipherSuite, key *storj.Key, nonce *storj.Nonce) (cipherData []byte, err error) {
|
2018-11-08 13:22:38 +00:00
|
|
|
// Don't encrypt empty slice
|
|
|
|
if len(data) == 0 {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
2018-09-26 14:32:23 +01:00
|
|
|
switch cipher {
|
2019-07-03 19:07:44 +01:00
|
|
|
case storj.EncNull:
|
2018-09-26 14:32:23 +01:00
|
|
|
return data, nil
|
2019-07-03 19:07:44 +01:00
|
|
|
case storj.EncAESGCM:
|
2018-10-19 14:38:13 +01:00
|
|
|
return EncryptAESGCM(data, key, ToAESGCMNonce(nonce))
|
2019-07-03 19:07:44 +01:00
|
|
|
case storj.EncSecretBox:
|
2018-09-26 14:32:23 +01:00
|
|
|
return EncryptSecretBox(data, key, nonce)
|
|
|
|
default:
|
2018-10-23 15:05:15 +01:00
|
|
|
return nil, ErrInvalidConfig.New("encryption type %d is not supported", cipher)
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 14:38:13 +01:00
|
|
|
// Decrypt decrypts cipherData with the given cipher, key and nonce
|
2019-07-03 19:07:44 +01:00
|
|
|
func Decrypt(cipherData []byte, cipher storj.CipherSuite, key *storj.Key, nonce *storj.Nonce) (data []byte, err error) {
|
2018-11-08 13:22:38 +00:00
|
|
|
// Don't decrypt empty slice
|
|
|
|
if len(cipherData) == 0 {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
2018-09-26 14:32:23 +01:00
|
|
|
switch cipher {
|
2019-07-03 19:07:44 +01:00
|
|
|
case storj.EncNull:
|
2018-09-26 14:32:23 +01:00
|
|
|
return cipherData, nil
|
2019-07-03 19:07:44 +01:00
|
|
|
case storj.EncAESGCM:
|
2018-10-19 14:38:13 +01:00
|
|
|
return DecryptAESGCM(cipherData, key, ToAESGCMNonce(nonce))
|
2019-07-03 19:07:44 +01:00
|
|
|
case storj.EncSecretBox:
|
2018-09-26 14:32:23 +01:00
|
|
|
return DecryptSecretBox(cipherData, key, nonce)
|
|
|
|
default:
|
2018-10-23 15:05:15 +01:00
|
|
|
return nil, ErrInvalidConfig.New("encryption type %d is not supported", cipher)
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 14:38:13 +01:00
|
|
|
// NewEncrypter creates a Transformer using the given cipher, key and nonce to encrypt data passing through it
|
2019-07-03 19:07:44 +01:00
|
|
|
func NewEncrypter(cipher storj.CipherSuite, key *storj.Key, startingNonce *storj.Nonce, encryptedBlockSize int) (Transformer, error) {
|
2018-09-26 14:32:23 +01:00
|
|
|
switch cipher {
|
2019-07-03 19:07:44 +01:00
|
|
|
case storj.EncNull:
|
2018-09-26 14:32:23 +01:00
|
|
|
return &NoopTransformer{}, nil
|
2019-07-03 19:07:44 +01:00
|
|
|
case storj.EncAESGCM:
|
2018-10-19 14:38:13 +01:00
|
|
|
return NewAESGCMEncrypter(key, ToAESGCMNonce(startingNonce), encryptedBlockSize)
|
2019-07-03 19:07:44 +01:00
|
|
|
case storj.EncSecretBox:
|
2018-10-19 14:38:13 +01:00
|
|
|
return NewSecretboxEncrypter(key, startingNonce, encryptedBlockSize)
|
2018-09-26 14:32:23 +01:00
|
|
|
default:
|
2018-10-23 15:05:15 +01:00
|
|
|
return nil, ErrInvalidConfig.New("encryption type %d is not supported", cipher)
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 14:38:13 +01:00
|
|
|
// NewDecrypter creates a Transformer using the given cipher, key and nonce to decrypt data passing through it
|
2019-07-03 19:07:44 +01:00
|
|
|
func NewDecrypter(cipher storj.CipherSuite, key *storj.Key, startingNonce *storj.Nonce, encryptedBlockSize int) (Transformer, error) {
|
2018-09-26 14:32:23 +01:00
|
|
|
switch cipher {
|
2019-07-03 19:07:44 +01:00
|
|
|
case storj.EncNull:
|
2018-09-26 14:32:23 +01:00
|
|
|
return &NoopTransformer{}, nil
|
2019-07-03 19:07:44 +01:00
|
|
|
case storj.EncAESGCM:
|
2018-10-19 14:38:13 +01:00
|
|
|
return NewAESGCMDecrypter(key, ToAESGCMNonce(startingNonce), encryptedBlockSize)
|
2019-07-03 19:07:44 +01:00
|
|
|
case storj.EncSecretBox:
|
2018-10-19 14:38:13 +01:00
|
|
|
return NewSecretboxDecrypter(key, startingNonce, encryptedBlockSize)
|
2018-09-26 14:32:23 +01:00
|
|
|
default:
|
2018-10-23 15:05:15 +01:00
|
|
|
return nil, ErrInvalidConfig.New("encryption type %d is not supported", cipher)
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-19 14:38:13 +01:00
|
|
|
|
|
|
|
// EncryptKey encrypts keyToEncrypt with the given cipher, key and nonce
|
2019-07-03 19:07:44 +01:00
|
|
|
func EncryptKey(keyToEncrypt *storj.Key, cipher storj.CipherSuite, key *storj.Key, nonce *storj.Nonce) (storj.EncryptedPrivateKey, error) {
|
2018-10-19 14:38:13 +01:00
|
|
|
return Encrypt(keyToEncrypt[:], cipher, key, nonce)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptKey decrypts keyToDecrypt with the given cipher, key and nonce
|
2019-07-03 19:07:44 +01:00
|
|
|
func DecryptKey(keyToDecrypt storj.EncryptedPrivateKey, cipher storj.CipherSuite, key *storj.Key, nonce *storj.Nonce) (*storj.Key, error) {
|
2018-10-19 14:38:13 +01:00
|
|
|
plainData, err := Decrypt(keyToDecrypt, cipher, key, nonce)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var decryptedKey storj.Key
|
|
|
|
copy(decryptedKey[:], plainData)
|
|
|
|
|
|
|
|
return &decryptedKey, nil
|
|
|
|
}
|
2018-10-25 21:28:16 +01:00
|
|
|
|
|
|
|
// DeriveKey derives new key from the given key and message using HMAC-SHA512
|
|
|
|
func DeriveKey(key *storj.Key, message string) (*storj.Key, error) {
|
|
|
|
mac := hmac.New(sha512.New, key[:])
|
|
|
|
_, err := mac.Write([]byte(message))
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
derived := new(storj.Key)
|
|
|
|
copy(derived[:], mac.Sum(nil))
|
|
|
|
|
|
|
|
return derived, nil
|
|
|
|
}
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
// CalcEncryptedSize calculates what would be the size of the cipher data after
|
|
|
|
// encrypting data with dataSize using a Transformer with the given encryption
|
2019-07-03 19:07:44 +01:00
|
|
|
// parameters.
|
|
|
|
func CalcEncryptedSize(dataSize int64, parameters storj.EncryptionParameters) (int64, error) {
|
|
|
|
transformer, err := NewEncrypter(parameters.CipherSuite, new(storj.Key), new(storj.Nonce), int(parameters.BlockSize))
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2019-03-21 13:47:48 +00:00
|
|
|
inBlockSize := int64(transformer.InBlockSize())
|
|
|
|
blocks := (dataSize + uint32Size + inBlockSize - 1) / inBlockSize
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
encryptedSize := blocks * int64(transformer.OutBlockSize())
|
|
|
|
|
|
|
|
return encryptedSize, nil
|
|
|
|
}
|