storj/pkg/eestream/secretbox_test.go

47 lines
994 B
Go
Raw Normal View History

2018-04-11 14:41:50 +01:00
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package eestream
import (
"bytes"
"crypto/rand"
"io/ioutil"
"testing"
)
func randData(amount int) []byte {
buf := make([]byte, amount)
_, err := rand.Read(buf)
if err != nil {
panic(err)
}
return buf
}
func TestSecretbox(t *testing.T) {
Stream encryption (#302) * begin adding encryption for remote pieces * begin adding decryption * add encryption key as arg to Put and Get * move encryption/decryption to object store * Add encryption key to object store constructor * Add the erasure scheme to object store constructor * Ensure decrypter is initialized with the stripe size used by encrypter * Revert "Ensure decrypter is initialized with the stripe size used by encrypter" This reverts commit 07272333f461606edfb43ad106cc152f37a3bd46. * Revert "Add the erasure scheme to object store constructor" This reverts commit ea5e793b536159d993b96e3db69a37c1656a193c. * move encryption to stream store * move decryption stuff to stream store * revert changes in object store * add encryptedBlockSize and close rangers on error during Get * calculate padding sizes correctly * encryptedBlockSize -> encryptionBlockSize * pass encryption key and block size into stream store * remove encryption key and block size from object store constructor * move encrypter/decrypter initialization * remove unnecessary cast * Fix padding issue * Fix linter * add todos * use random encryption key for data encryption. Store an encrypted copy of this key in segment metadata * use different encryption key for each segment * encrypt data in one step if it is small enough * refactor and move encryption stuff * fix errors related to nil slices passed to copy * fix encrypter vs. decrypter bug * put encryption stuff in eestream * get captplanet test to pass * fix linting errors * add types for encryption keys/nonces and clean up * fix tests * more review changes * add Cipher type for encryption stuff * fix rs_test * Simplify type casting of key and nonce * Init starting nonce to the segment index * don't copy derived key * remove default encryption key; force user to explicitly set it * move getSegmentPath to streams package * dont require user to specify encryption key for captplanet * rename GenericKey and GenericNonce to Key and Nonce * review changes * fix linting error * Download uses the encryption type from metadata * Store enc block size in metadata and use it for download
2018-09-26 14:32:23 +01:00
var key Key
copy(key[:], randData(KeySize))
var firstNonce Nonce
copy(firstNonce[:], randData(NonceSize))
encrypter, err := NewSecretboxEncrypter(&key, &firstNonce, 4*1024)
2018-04-11 14:41:50 +01:00
if err != nil {
t.Fatal(err)
}
data := randData(encrypter.InBlockSize() * 10)
encrypted := TransformReader(
ioutil.NopCloser(bytes.NewReader(data)), encrypter, 0)
decrypter, err := NewSecretboxDecrypter(&key, &firstNonce, 4*1024)
2018-04-11 14:41:50 +01:00
if err != nil {
t.Fatal(err)
}
decrypted := TransformReader(encrypted, decrypter, 0)
data2, err := ioutil.ReadAll(decrypted)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, data2) {
t.Fatalf("encryption/decryption failed")
}
}