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)
|
2015-05-04 22:14:07 +00:00
|
|
|
buf := make([]byte, size+crypto.Extension)
|
|
|
|
|
2017-06-19 19:12:53 +00:00
|
|
|
ciphertext, err := k.Encrypt(buf, data)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
|
|
|
rtest.Assert(t, len(ciphertext) == len(data)+crypto.Extension,
|
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
|
|
|
|
2016-09-03 11:34:04 +00:00
|
|
|
plaintext := make([]byte, len(ciphertext))
|
2017-06-19 19:12:53 +00:00
|
|
|
n, err := k.Decrypt(plaintext, ciphertext)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2016-09-03 11:34:04 +00:00
|
|
|
plaintext = plaintext[:n]
|
2017-10-02 13:06:39 +00:00
|
|
|
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
|
|
|
|
|
|
|
ciphertext := make([]byte, size/2)
|
2017-06-19 19:12:53 +00:00
|
|
|
ciphertext, err = k.Encrypt(ciphertext, data)
|
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-06-19 19:12:53 +00:00
|
|
|
n, err := k.Decrypt(plaintext, ciphertext)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2016-09-03 11:34:04 +00:00
|
|
|
plaintext = plaintext[:n]
|
2017-10-02 13:06:39 +00:00
|
|
|
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-06-19 19:12:53 +00:00
|
|
|
ciphertext, err = k.Encrypt(ciphertext, 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
|
|
|
// use the same buffer for decryption
|
2017-06-19 19:12:53 +00:00
|
|
|
n, err := k.Decrypt(ciphertext, ciphertext)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2016-09-03 11:34:04 +00:00
|
|
|
ciphertext = ciphertext[:n]
|
2017-10-02 13:06:39 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-04-19 12:06:55 +00:00
|
|
|
func TestCornerCases(t *testing.T) {
|
2015-04-30 02:28:34 +00:00
|
|
|
k := crypto.NewRandomKey()
|
2015-04-19 12:06:55 +00:00
|
|
|
|
|
|
|
// nil plaintext should encrypt to the empty string
|
|
|
|
// nil ciphertext should allocate a new slice for the ciphertext
|
2017-06-19 19:12:53 +00:00
|
|
|
c, err := k.Encrypt(nil, nil)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2015-04-19 12:06:55 +00:00
|
|
|
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.Assert(t, len(c) == crypto.Extension,
|
2015-04-19 12:06:55 +00:00
|
|
|
"wrong length returned for ciphertext, expected 0, got %d",
|
|
|
|
len(c))
|
|
|
|
|
2015-05-04 22:07:57 +00:00
|
|
|
// this should decrypt to nil
|
2017-06-19 19:12:53 +00:00
|
|
|
n, err := k.Decrypt(nil, c)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
|
|
|
rtest.Equals(t, 0, n)
|
2015-04-19 12:06:55 +00:00
|
|
|
|
|
|
|
// test encryption for same slice, this should return an error
|
2017-06-19 19:12:53 +00:00
|
|
|
_, err = k.Encrypt(c, c)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.Equals(t, crypto.ErrInvalidCiphertext, err)
|
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-06-19 19:12:53 +00:00
|
|
|
ciphertext, err := k.Encrypt(make([]byte, size+crypto.Extension), data)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2017-06-19 19:12:53 +00:00
|
|
|
plaintext, err := k.Decrypt([]byte{}, ciphertext)
|
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)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.SetBytes(int64(size))
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-06-19 19:12:53 +00:00
|
|
|
_, err := k.Encrypt(buf, data)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(b, err)
|
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
|
|
|
|
2015-05-04 22:14:07 +00:00
|
|
|
plaintext := make([]byte, size)
|
|
|
|
ciphertext := make([]byte, size+crypto.Extension)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2017-06-19 19:12:53 +00:00
|
|
|
ciphertext, err := k.Encrypt(ciphertext, data)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(b, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.SetBytes(int64(size))
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-06-19 19:12:53 +00:00
|
|
|
_, err = k.Decrypt(plaintext, ciphertext)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(b, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
}
|
|
|
|
}
|