1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
package encryption
import (
cryptorand "crypto/rand"
"io"
"testing"
"github.com/docker/swarmkit/api"
"github.com/stretchr/testify/require"
)
// Using the same key to encrypt the same message, this encrypter produces two
// different ciphertexts because the underlying algorithm uses different IVs.
// Both of these can be decrypted into the same data though.
func TestFernet(t *testing.T) {
key := make([]byte, 32)
_, err := io.ReadFull(cryptorand.Reader, key)
require.NoError(t, err)
keyCopy := make([]byte, 32)
copy(key, keyCopy)
crypter1 := NewFernet(key)
crypter2 := NewFernet(keyCopy)
data := []byte("Hello again world")
er1, err := crypter1.Encrypt(data)
require.NoError(t, err)
er2, err := crypter2.Encrypt(data)
require.NoError(t, err)
require.NotEqual(t, er1.Data, er2.Data)
require.Empty(t, er1.Nonce)
require.Empty(t, er2.Nonce)
// it doesn't matter what the nonce is, it's ignored
_, err = io.ReadFull(cryptorand.Reader, er1.Nonce)
require.NoError(t, err)
// both crypters can decrypt the other's text
for i, decrypter := range []Decrypter{crypter1, crypter2} {
for j, record := range []*api.MaybeEncryptedRecord{er1, er2} {
result, err := decrypter.Decrypt(*record)
require.NoError(t, err, "error decrypting ciphertext produced by cryptor %d using cryptor %d", j+1, i+1)
require.Equal(t, data, result)
}
}
}
func TestFernetInvalidAlgorithm(t *testing.T) {
key := make([]byte, 32)
_, err := io.ReadFull(cryptorand.Reader, key)
require.NoError(t, err)
crypter := NewFernet(key)
er, err := crypter.Encrypt([]byte("Hello again world"))
require.NoError(t, err)
er.Algorithm = api.MaybeEncryptedRecord_NotEncrypted
_, err = crypter.Decrypt(*er)
require.Error(t, err)
require.Contains(t, err.Error(), "not a Fernet message")
}
func TestFernetCannotDecryptWithoutRightKey(t *testing.T) {
key := make([]byte, 32)
_, err := io.ReadFull(cryptorand.Reader, key)
require.NoError(t, err)
crypter := NewFernet(key)
er, err := crypter.Encrypt([]byte("Hello again world"))
require.NoError(t, err)
crypter = NewFernet([]byte{})
_, err = crypter.Decrypt(*er)
require.Error(t, err)
}
|