2015-04-12 07:36:14 +00:00
|
|
|
package crypto_test
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-08-16 21:09:39 +00:00
|
|
|
"crypto/rand"
|
2015-03-14 18:53:51 +00:00
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/crypto"
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest "github.com/restic/restic/internal/test"
|
2016-08-21 09:33:53 +00:00
|
|
|
|
|
|
|
"github.com/restic/chunker"
|
2015-03-14 18:53:51 +00:00
|
|
|
)
|
|
|
|
|
2015-06-28 11:15:35 +00:00
|
|
|
const testLargeCrypto = false
|
2015-04-12 07:36:14 +00:00
|
|
|
|
2015-03-14 18:53:51 +00:00
|
|
|
func TestEncryptDecrypt(t *testing.T) {
|
2015-04-30 02:28:34 +00:00
|
|
|
k := crypto.NewRandomKey()
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
tests := []int{5, 23, 2<<18 + 23, 1 << 20}
|
2015-06-28 11:15:35 +00:00
|
|
|
if testLargeCrypto {
|
2015-03-14 18:53:51 +00:00
|
|
|
tests = append(tests, 7<<20+123)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, size := range tests {
|
2017-10-02 13:06:39 +00:00
|
|
|
data := rtest.Random(42, size)
|
2017-10-28 09:24:15 +00:00
|
|
|
buf := make([]byte, 0, size+crypto.Extension)
|
2015-05-04 22:14:07 +00:00
|
|
|
|
2017-10-28 09:24:15 +00:00
|
|
|
nonce := crypto.NewRandomNonce()
|
|
|
|
ciphertext := k.Seal(buf[:0], nonce, data, nil)
|
|
|
|
rtest.Assert(t, len(ciphertext) == len(data)+k.Overhead(),
|
2015-04-19 11:34:42 +00:00
|
|
|
"ciphertext length does not match: want %d, got %d",
|
|
|
|
len(data)+crypto.Extension, len(ciphertext))
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2017-10-28 09:24:15 +00:00
|
|
|
plaintext := make([]byte, 0, len(ciphertext))
|
|
|
|
plaintext, err := k.Open(plaintext[:0], nonce, ciphertext, nil)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
|
|
|
rtest.Assert(t, len(plaintext) == len(data),
|
2015-04-19 11:34:42 +00:00
|
|
|
"plaintext length does not match: want %d, got %d",
|
|
|
|
len(data), len(plaintext))
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.Equals(t, plaintext, data)
|
2015-03-14 18:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSmallBuffer(t *testing.T) {
|
2015-04-30 02:28:34 +00:00
|
|
|
k := crypto.NewRandomKey()
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
size := 600
|
|
|
|
data := make([]byte, size)
|
2015-08-16 21:09:39 +00:00
|
|
|
_, err := io.ReadFull(rand.Reader, data)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2017-10-28 09:24:15 +00:00
|
|
|
ciphertext := make([]byte, 0, size/2)
|
|
|
|
nonce := crypto.NewRandomNonce()
|
|
|
|
ciphertext = k.Seal(ciphertext[:0], nonce, data, nil)
|
2015-04-19 11:34:42 +00:00
|
|
|
// this must extend the slice
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.Assert(t, cap(ciphertext) > size/2,
|
2015-04-12 18:58:41 +00:00
|
|
|
"expected extended slice, but capacity is only %d bytes",
|
|
|
|
cap(ciphertext))
|
|
|
|
|
|
|
|
// check for the correct plaintext
|
2016-09-03 11:34:04 +00:00
|
|
|
plaintext := make([]byte, len(ciphertext))
|
2017-10-28 09:24:15 +00:00
|
|
|
plaintext, err = k.Open(plaintext[:0], nonce, ciphertext, nil)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
|
|
|
rtest.Assert(t, bytes.Equal(plaintext, data),
|
2015-04-12 18:58:41 +00:00
|
|
|
"wrong plaintext returned")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSameBuffer(t *testing.T) {
|
2015-04-30 02:28:34 +00:00
|
|
|
k := crypto.NewRandomKey()
|
2015-04-12 18:58:41 +00:00
|
|
|
|
|
|
|
size := 600
|
|
|
|
data := make([]byte, size)
|
2015-08-16 21:09:39 +00:00
|
|
|
_, err := io.ReadFull(rand.Reader, data)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2015-04-12 18:58:41 +00:00
|
|
|
|
2015-04-19 11:34:42 +00:00
|
|
|
ciphertext := make([]byte, 0, size+crypto.Extension)
|
2015-04-12 18:58:41 +00:00
|
|
|
|
2017-10-28 09:24:15 +00:00
|
|
|
nonce := crypto.NewRandomNonce()
|
|
|
|
ciphertext = k.Seal(ciphertext, nonce, data, nil)
|
2015-04-12 18:58:41 +00:00
|
|
|
|
2015-04-19 11:34:42 +00:00
|
|
|
// use the same buffer for decryption
|
2017-10-28 09:24:15 +00:00
|
|
|
ciphertext, err = k.Open(ciphertext[:0], nonce, ciphertext, nil)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
|
|
|
rtest.Assert(t, bytes.Equal(ciphertext, data),
|
2015-04-12 18:58:41 +00:00
|
|
|
"wrong plaintext returned")
|
2015-03-14 18:53:51 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 09:24:15 +00:00
|
|
|
func encrypt(t testing.TB, k *crypto.Key, data, ciphertext, nonce []byte) []byte {
|
|
|
|
prefixlen := len(ciphertext)
|
|
|
|
ciphertext = k.Seal(ciphertext, nonce, data, nil)
|
|
|
|
if len(ciphertext) != len(data)+k.Overhead()+prefixlen {
|
|
|
|
t.Fatalf("destination slice has wrong length, want %d, got %d",
|
|
|
|
len(data)+k.Overhead(), len(ciphertext))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ciphertext
|
|
|
|
}
|
|
|
|
|
|
|
|
func decryptNewSliceAndCompare(t testing.TB, k *crypto.Key, data, ciphertext, nonce []byte) {
|
|
|
|
plaintext := make([]byte, 0, len(ciphertext))
|
|
|
|
decryptAndCompare(t, k, data, ciphertext, nonce, plaintext)
|
|
|
|
}
|
|
|
|
|
|
|
|
func decryptAndCompare(t testing.TB, k *crypto.Key, data, ciphertext, nonce, dst []byte) {
|
|
|
|
prefix := make([]byte, len(dst))
|
|
|
|
copy(prefix, dst)
|
|
|
|
|
|
|
|
plaintext, err := k.Open(dst, nonce, ciphertext, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to decrypt ciphertext: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data)+len(prefix) != len(plaintext) {
|
|
|
|
t.Fatalf("wrong plaintext returned, want %d bytes, got %d", len(data)+len(prefix), len(plaintext))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(plaintext[:len(prefix)], prefix) {
|
|
|
|
t.Fatal("prefix is wrong")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(plaintext[len(prefix):], data) {
|
|
|
|
t.Fatal("wrong plaintext returned")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-01 08:46:05 +00:00
|
|
|
func TestAppendOpen(t *testing.T) {
|
2015-04-30 02:28:34 +00:00
|
|
|
k := crypto.NewRandomKey()
|
2017-10-28 09:24:15 +00:00
|
|
|
nonce := crypto.NewRandomNonce()
|
2015-04-19 12:06:55 +00:00
|
|
|
|
2017-10-28 09:24:15 +00:00
|
|
|
data := make([]byte, 600)
|
|
|
|
_, err := io.ReadFull(rand.Reader, data)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2017-10-28 09:24:15 +00:00
|
|
|
ciphertext := encrypt(t, k, data, nil, nonce)
|
|
|
|
|
|
|
|
// we need to test several different cases:
|
|
|
|
// * destination slice is nil
|
|
|
|
// * destination slice is empty and has enough capacity
|
|
|
|
// * destination slice is empty and does not have enough capacity
|
|
|
|
// * destination slice contains data and has enough capacity
|
|
|
|
// * destination slice contains data and does not have enough capacity
|
|
|
|
|
|
|
|
// destination slice is nil
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
var plaintext []byte
|
|
|
|
decryptAndCompare(t, k, data, ciphertext, nonce, plaintext)
|
|
|
|
})
|
|
|
|
|
|
|
|
// destination slice is empty and has enough capacity
|
|
|
|
t.Run("empty-large", func(t *testing.T) {
|
|
|
|
plaintext := make([]byte, 0, len(data)+100)
|
|
|
|
decryptAndCompare(t, k, data, ciphertext, nonce, plaintext)
|
|
|
|
})
|
|
|
|
|
|
|
|
// destination slice is empty and does not have enough capacity
|
|
|
|
t.Run("empty-small", func(t *testing.T) {
|
|
|
|
plaintext := make([]byte, 0, len(data)/2)
|
|
|
|
decryptAndCompare(t, k, data, ciphertext, nonce, plaintext)
|
|
|
|
})
|
|
|
|
|
|
|
|
// destination slice contains data and has enough capacity
|
|
|
|
t.Run("prefix-large", func(t *testing.T) {
|
|
|
|
plaintext := make([]byte, 0, len(data)+100)
|
|
|
|
plaintext = append(plaintext, []byte("foobar")...)
|
|
|
|
decryptAndCompare(t, k, data, ciphertext, nonce, plaintext)
|
|
|
|
})
|
|
|
|
|
|
|
|
// destination slice contains data and does not have enough capacity
|
|
|
|
t.Run("prefix-small", func(t *testing.T) {
|
|
|
|
plaintext := make([]byte, 0, len(data)/2)
|
|
|
|
plaintext = append(plaintext, []byte("foobar")...)
|
|
|
|
decryptAndCompare(t, k, data, ciphertext, nonce, plaintext)
|
|
|
|
})
|
|
|
|
}
|
2015-04-19 12:06:55 +00:00
|
|
|
|
2017-11-01 08:46:05 +00:00
|
|
|
func TestAppendSeal(t *testing.T) {
|
2017-10-28 09:24:15 +00:00
|
|
|
k := crypto.NewRandomKey()
|
2015-04-19 12:06:55 +00:00
|
|
|
|
2017-10-28 09:24:15 +00:00
|
|
|
data := make([]byte, 600)
|
|
|
|
_, err := io.ReadFull(rand.Reader, data)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2015-04-19 12:06:55 +00:00
|
|
|
|
2017-10-28 09:24:15 +00:00
|
|
|
// we need to test several different cases:
|
|
|
|
// * destination slice is nil
|
|
|
|
// * destination slice is empty and has enough capacity
|
|
|
|
// * destination slice is empty and does not have enough capacity
|
|
|
|
// * destination slice contains data and has enough capacity
|
|
|
|
// * destination slice contains data and does not have enough capacity
|
|
|
|
|
|
|
|
// destination slice is nil
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
nonce := crypto.NewRandomNonce()
|
|
|
|
var ciphertext []byte
|
|
|
|
|
|
|
|
ciphertext = encrypt(t, k, data, ciphertext, nonce)
|
|
|
|
decryptNewSliceAndCompare(t, k, data, ciphertext, nonce)
|
|
|
|
})
|
|
|
|
|
|
|
|
// destination slice is empty and has enough capacity
|
|
|
|
t.Run("empty-large", func(t *testing.T) {
|
|
|
|
nonce := crypto.NewRandomNonce()
|
|
|
|
ciphertext := make([]byte, 0, len(data)+100)
|
|
|
|
|
|
|
|
ciphertext = encrypt(t, k, data, ciphertext, nonce)
|
|
|
|
decryptNewSliceAndCompare(t, k, data, ciphertext, nonce)
|
|
|
|
})
|
|
|
|
|
|
|
|
// destination slice is empty and does not have enough capacity
|
|
|
|
t.Run("empty-small", func(t *testing.T) {
|
|
|
|
nonce := crypto.NewRandomNonce()
|
|
|
|
ciphertext := make([]byte, 0, len(data)/2)
|
|
|
|
|
|
|
|
ciphertext = encrypt(t, k, data, ciphertext, nonce)
|
|
|
|
decryptNewSliceAndCompare(t, k, data, ciphertext, nonce)
|
|
|
|
})
|
|
|
|
|
|
|
|
// destination slice contains data and has enough capacity
|
|
|
|
t.Run("prefix-large", func(t *testing.T) {
|
|
|
|
nonce := crypto.NewRandomNonce()
|
|
|
|
ciphertext := make([]byte, 0, len(data)+100)
|
|
|
|
ciphertext = append(ciphertext, []byte("foobar")...)
|
|
|
|
|
|
|
|
ciphertext = encrypt(t, k, data, ciphertext, nonce)
|
|
|
|
if string(ciphertext[:6]) != "foobar" {
|
|
|
|
t.Errorf("prefix is missing")
|
|
|
|
}
|
|
|
|
decryptNewSliceAndCompare(t, k, data, ciphertext[6:], nonce)
|
|
|
|
})
|
|
|
|
|
|
|
|
// destination slice contains data and does not have enough capacity
|
|
|
|
t.Run("prefix-small", func(t *testing.T) {
|
|
|
|
nonce := crypto.NewRandomNonce()
|
|
|
|
ciphertext := make([]byte, 0, len(data)/2)
|
|
|
|
ciphertext = append(ciphertext, []byte("foobar")...)
|
|
|
|
|
|
|
|
ciphertext = encrypt(t, k, data, ciphertext, nonce)
|
|
|
|
if string(ciphertext[:6]) != "foobar" {
|
|
|
|
t.Errorf("prefix is missing")
|
|
|
|
}
|
|
|
|
decryptNewSliceAndCompare(t, k, data, ciphertext[6:], nonce)
|
|
|
|
})
|
2015-04-19 12:06:55 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 18:53:51 +00:00
|
|
|
func TestLargeEncrypt(t *testing.T) {
|
2015-06-28 11:15:35 +00:00
|
|
|
if !testLargeCrypto {
|
2015-03-14 18:53:51 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
2015-04-30 02:28:34 +00:00
|
|
|
k := crypto.NewRandomKey()
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
for _, size := range []int{chunker.MaxSize, chunker.MaxSize + 1, chunker.MaxSize + 1<<20} {
|
|
|
|
data := make([]byte, size)
|
2015-08-16 21:09:39 +00:00
|
|
|
_, err := io.ReadFull(rand.Reader, data)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2017-10-28 09:24:15 +00:00
|
|
|
nonce := crypto.NewRandomNonce()
|
|
|
|
ciphertext := k.Seal(make([]byte, size+k.Overhead()), nonce, data, nil)
|
|
|
|
plaintext, err := k.Open([]byte{}, nonce, ciphertext, nil)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.Equals(t, plaintext, data)
|
2015-03-14 18:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEncrypt(b *testing.B) {
|
|
|
|
size := 8 << 20 // 8MiB
|
|
|
|
data := make([]byte, size)
|
|
|
|
|
2015-04-30 02:28:34 +00:00
|
|
|
k := crypto.NewRandomKey()
|
2015-04-12 07:41:47 +00:00
|
|
|
buf := make([]byte, len(data)+crypto.Extension)
|
2017-10-28 09:24:15 +00:00
|
|
|
nonce := crypto.NewRandomNonce()
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.SetBytes(int64(size))
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-10-28 09:24:15 +00:00
|
|
|
_ = k.Seal(buf, nonce, data, nil)
|
2015-03-14 18:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDecrypt(b *testing.B) {
|
|
|
|
size := 8 << 20 // 8MiB
|
|
|
|
data := make([]byte, size)
|
|
|
|
|
2015-04-30 02:28:34 +00:00
|
|
|
k := crypto.NewRandomKey()
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2017-10-28 09:24:15 +00:00
|
|
|
plaintext := make([]byte, 0, size)
|
|
|
|
ciphertext := make([]byte, 0, size+crypto.Extension)
|
|
|
|
nonce := crypto.NewRandomNonce()
|
|
|
|
ciphertext = k.Seal(ciphertext, nonce, data, nil)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2017-10-28 09:24:15 +00:00
|
|
|
var err error
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.SetBytes(int64(size))
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-10-28 09:24:15 +00:00
|
|
|
_, err = k.Open(plaintext, nonce, ciphertext, nil)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(b, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
}
|
|
|
|
}
|