2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-04-11 14:41:50 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-10-18 12:10:29 +01:00
|
|
|
package encryption
|
2018-04-11 14:41:50 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.org/x/crypto/nacl/secretbox"
|
2018-10-19 14:38:13 +01:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/storj"
|
2018-04-11 14:41:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type secretboxEncrypter struct {
|
2018-04-16 01:48:19 +01:00
|
|
|
blockSize int
|
2018-10-19 14:38:13 +01:00
|
|
|
key *storj.Key
|
|
|
|
startingNonce *storj.Nonce
|
2018-04-11 14:41:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSecretboxEncrypter returns a Transformer that encrypts the data passing
|
|
|
|
// through with key.
|
2018-04-16 01:48:19 +01:00
|
|
|
//
|
|
|
|
// 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 NewSecretboxEncrypter(key *storj.Key, startingNonce *storj.Nonce, encryptedBlockSize int) (Transformer, error) {
|
2018-04-11 14:41:50 +01:00
|
|
|
if encryptedBlockSize <= secretbox.Overhead {
|
2018-10-23 15:05:15 +01:00
|
|
|
return nil, ErrInvalidConfig.New("encrypted block size %d too small", encryptedBlockSize)
|
2018-04-11 14:41:50 +01:00
|
|
|
}
|
2018-04-16 01:48:19 +01:00
|
|
|
return &secretboxEncrypter{
|
|
|
|
blockSize: encryptedBlockSize - secretbox.Overhead,
|
2018-10-19 14:38:13 +01:00
|
|
|
key: key,
|
|
|
|
startingNonce: startingNonce,
|
2018-04-16 01:48:19 +01:00
|
|
|
}, nil
|
2018-04-11 14:41:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *secretboxEncrypter) InBlockSize() int {
|
|
|
|
return s.blockSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *secretboxEncrypter) OutBlockSize() int {
|
|
|
|
return s.blockSize + secretbox.Overhead
|
|
|
|
}
|
|
|
|
|
2018-10-19 14:38:13 +01:00
|
|
|
func calcNonce(startingNonce *storj.Nonce, blockNum int64) (rv *storj.Nonce, err error) {
|
|
|
|
rv = new(storj.Nonce)
|
2018-04-16 01:48:19 +01:00
|
|
|
if copy(rv[:], (*startingNonce)[:]) != len(rv) {
|
|
|
|
return rv, Error.New("didn't copy memory?!")
|
|
|
|
}
|
|
|
|
_, err = incrementBytes(rv[:], blockNum)
|
|
|
|
return rv, err
|
2018-04-11 14:41:50 +01:00
|
|
|
}
|
|
|
|
|
2018-09-26 14:32:23 +01:00
|
|
|
func (s *secretboxEncrypter) Transform(out, in []byte, blockNum int64) ([]byte, error) {
|
2018-10-19 14:38:13 +01:00
|
|
|
nonce, err := calcNonce(s.startingNonce, blockNum)
|
2018-04-16 01:48:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 14:38:13 +01:00
|
|
|
return secretbox.Seal(out, in, nonce.Raw(), s.key.Raw()), nil
|
2018-04-11 14:41:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type secretboxDecrypter struct {
|
2018-04-16 01:48:19 +01:00
|
|
|
blockSize int
|
2018-10-19 14:38:13 +01:00
|
|
|
key *storj.Key
|
|
|
|
startingNonce *storj.Nonce
|
2018-04-11 14:41:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSecretboxDecrypter returns a Transformer that decrypts the data passing
|
2018-04-16 01:48:19 +01:00
|
|
|
// through with key. See the comments for NewSecretboxEncrypter about
|
|
|
|
// startingNonce.
|
2018-10-19 14:38:13 +01:00
|
|
|
func NewSecretboxDecrypter(key *storj.Key, startingNonce *storj.Nonce, encryptedBlockSize int) (Transformer, error) {
|
2018-04-11 14:41:50 +01:00
|
|
|
if encryptedBlockSize <= secretbox.Overhead {
|
2018-10-23 15:05:15 +01:00
|
|
|
return nil, ErrInvalidConfig.New("encrypted block size %d too small", encryptedBlockSize)
|
2018-04-11 14:41:50 +01:00
|
|
|
}
|
2018-04-16 01:48:19 +01:00
|
|
|
return &secretboxDecrypter{
|
|
|
|
blockSize: encryptedBlockSize - secretbox.Overhead,
|
2018-10-19 14:38:13 +01:00
|
|
|
key: key,
|
|
|
|
startingNonce: startingNonce,
|
2018-04-16 01:48:19 +01:00
|
|
|
}, nil
|
2018-04-11 14:41:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *secretboxDecrypter) InBlockSize() int {
|
|
|
|
return s.blockSize + secretbox.Overhead
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *secretboxDecrypter) OutBlockSize() int {
|
|
|
|
return s.blockSize
|
|
|
|
}
|
|
|
|
|
2018-09-26 14:32:23 +01:00
|
|
|
func (s *secretboxDecrypter) Transform(out, in []byte, blockNum int64) ([]byte, error) {
|
2018-10-19 14:38:13 +01:00
|
|
|
nonce, err := calcNonce(s.startingNonce, blockNum)
|
2018-04-16 01:48:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 14:38:13 +01:00
|
|
|
rv, success := secretbox.Open(out, in, nonce.Raw(), s.key.Raw())
|
2018-04-11 14:41:50 +01:00
|
|
|
if !success {
|
2018-10-23 15:05:15 +01:00
|
|
|
return nil, ErrDecryptFailed.New("")
|
2018-04-11 14:41:50 +01:00
|
|
|
}
|
|
|
|
return rv, nil
|
|
|
|
}
|
2018-09-26 14:32:23 +01:00
|
|
|
|
|
|
|
// EncryptSecretBox encrypts byte data with a key and nonce. The cipher data is returned
|
2018-10-19 14:38:13 +01:00
|
|
|
func EncryptSecretBox(data []byte, key *storj.Key, nonce *storj.Nonce) (cipherData []byte, err error) {
|
|
|
|
return secretbox.Seal(nil, data, nonce.Raw(), key.Raw()), nil
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptSecretBox decrypts byte data with a key and nonce. The plain data is returned
|
2018-10-19 14:38:13 +01:00
|
|
|
func DecryptSecretBox(cipherData []byte, key *storj.Key, nonce *storj.Nonce) (data []byte, err error) {
|
|
|
|
data, success := secretbox.Open(nil, cipherData, nonce.Raw(), key.Raw())
|
2018-09-26 14:32:23 +01:00
|
|
|
if !success {
|
2018-10-23 15:05:15 +01:00
|
|
|
return nil, ErrDecryptFailed.New("")
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|