2015-04-12 07:36:14 +00:00
|
|
|
package crypto_test
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-04-12 07:36:14 +00:00
|
|
|
"flag"
|
2015-03-14 18:53:51 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/restic/restic"
|
|
|
|
"github.com/restic/restic/chunker"
|
2015-04-12 07:36:14 +00:00
|
|
|
"github.com/restic/restic/crypto"
|
2015-04-09 19:15:48 +00:00
|
|
|
. "github.com/restic/restic/test"
|
2015-03-14 18:53:51 +00:00
|
|
|
)
|
|
|
|
|
2015-04-12 07:36:14 +00:00
|
|
|
var testLargeCrypto = flag.Bool("test.largecrypto", false, "also test crypto functions with large payloads")
|
|
|
|
|
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}
|
|
|
|
if *testLargeCrypto {
|
|
|
|
tests = append(tests, 7<<20+123)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, size := range tests {
|
|
|
|
data := make([]byte, size)
|
2015-04-12 07:36:14 +00:00
|
|
|
_, err := io.ReadFull(RandomReader(42, size), data)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2015-04-12 18:58:41 +00:00
|
|
|
ciphertext, err := crypto.Encrypt(k, restic.GetChunkBuf("TestEncryptDecrypt"), data)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-04-19 11:34:42 +00:00
|
|
|
Assert(t, len(ciphertext) == len(data)+crypto.Extension,
|
|
|
|
"ciphertext length does not match: want %d, got %d",
|
|
|
|
len(data)+crypto.Extension, len(ciphertext))
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2015-04-12 18:58:41 +00:00
|
|
|
plaintext, err := crypto.Decrypt(k, nil, ciphertext)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-04-19 11:34:42 +00:00
|
|
|
Assert(t, len(plaintext) == len(data),
|
|
|
|
"plaintext length does not match: want %d, got %d",
|
|
|
|
len(data), len(plaintext))
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
restic.FreeChunkBuf("TestEncryptDecrypt", ciphertext)
|
|
|
|
|
2015-04-09 19:15:48 +00:00
|
|
|
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)
|
|
|
|
f, err := os.Open("/dev/urandom")
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
_, err = io.ReadFull(f, data)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
ciphertext := make([]byte, size/2)
|
2015-04-12 18:58:41 +00:00
|
|
|
ciphertext, err = crypto.Encrypt(k, ciphertext, data)
|
2015-04-19 11:34:42 +00:00
|
|
|
// this must extend the slice
|
2015-04-12 18:58:41 +00:00
|
|
|
Assert(t, cap(ciphertext) > size/2,
|
|
|
|
"expected extended slice, but capacity is only %d bytes",
|
|
|
|
cap(ciphertext))
|
|
|
|
|
|
|
|
// check for the correct plaintext
|
|
|
|
plaintext, err := crypto.Decrypt(k, nil, ciphertext)
|
|
|
|
OK(t, err)
|
|
|
|
Assert(t, bytes.Equal(plaintext, data),
|
|
|
|
"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)
|
|
|
|
f, err := os.Open("/dev/urandom")
|
|
|
|
OK(t, err)
|
|
|
|
|
|
|
|
_, err = io.ReadFull(f, data)
|
|
|
|
OK(t, err)
|
|
|
|
|
2015-04-19 11:34:42 +00:00
|
|
|
ciphertext := make([]byte, 0, size+crypto.Extension)
|
2015-04-12 18:58:41 +00:00
|
|
|
|
2015-04-19 11:34:42 +00:00
|
|
|
ciphertext, err = crypto.Encrypt(k, ciphertext, data)
|
2015-04-12 18:58:41 +00:00
|
|
|
OK(t, err)
|
|
|
|
|
2015-04-19 11:34:42 +00:00
|
|
|
// use the same buffer for decryption
|
2015-04-12 18:58:41 +00:00
|
|
|
ciphertext, err = crypto.Decrypt(k, ciphertext, ciphertext)
|
|
|
|
OK(t, err)
|
|
|
|
Assert(t, bytes.Equal(ciphertext, data),
|
|
|
|
"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
|
|
|
|
c, err := crypto.Encrypt(k, nil, nil)
|
|
|
|
OK(t, err)
|
|
|
|
|
|
|
|
Assert(t, len(c) == crypto.Extension,
|
|
|
|
"wrong length returned for ciphertext, expected 0, got %d",
|
|
|
|
len(c))
|
|
|
|
|
|
|
|
// this should decrypt to an empty slice
|
|
|
|
p, err := crypto.Decrypt(k, nil, c)
|
|
|
|
OK(t, err)
|
|
|
|
Equals(t, []byte{}, p)
|
|
|
|
|
|
|
|
// test encryption for same slice, this should return an error
|
|
|
|
_, err = crypto.Encrypt(k, c, c)
|
|
|
|
Equals(t, crypto.ErrInvalidCiphertext, err)
|
|
|
|
}
|
|
|
|
|
2015-03-14 18:53:51 +00:00
|
|
|
func TestLargeEncrypt(t *testing.T) {
|
|
|
|
if !*testLargeCrypto {
|
|
|
|
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)
|
|
|
|
f, err := os.Open("/dev/urandom")
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
_, err = io.ReadFull(f, data)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2015-04-12 18:58:41 +00:00
|
|
|
ciphertext, err := crypto.Encrypt(k, make([]byte, size+crypto.Extension), data)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2015-04-12 18:58:41 +00:00
|
|
|
plaintext, err := crypto.Decrypt(k, []byte{}, ciphertext)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2015-04-09 19:15:48 +00:00
|
|
|
Equals(t, plaintext, data)
|
2015-03-14 18:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEncryptWriter(b *testing.B) {
|
|
|
|
size := 8 << 20 // 8MiB
|
2015-04-12 07:36:14 +00:00
|
|
|
rd := RandomReader(23, size)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2015-04-30 02:28:34 +00:00
|
|
|
k := crypto.NewRandomKey()
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.SetBytes(int64(size))
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
rd.Seek(0, 0)
|
2015-04-12 07:36:14 +00:00
|
|
|
wr := crypto.EncryptTo(k, ioutil.Discard)
|
2015-04-12 19:48:17 +00:00
|
|
|
n, err := io.Copy(wr, rd)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(b, err)
|
|
|
|
OK(b, wr.Close())
|
2015-04-12 19:48:17 +00:00
|
|
|
Assert(b, n == int64(size),
|
|
|
|
"not enough bytes writter: want %d, got %d", size, n)
|
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++ {
|
2015-04-12 07:36:14 +00:00
|
|
|
_, err := crypto.Encrypt(k, buf, data)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(b, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDecryptReader(b *testing.B) {
|
|
|
|
size := 8 << 20 // 8MiB
|
2015-04-12 07:36:14 +00:00
|
|
|
buf := Random(23, size)
|
2015-04-30 02:28:34 +00:00
|
|
|
k := crypto.NewRandomKey()
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2015-04-12 07:41:47 +00:00
|
|
|
ciphertext := make([]byte, len(buf)+crypto.Extension)
|
2015-04-12 07:36:14 +00:00
|
|
|
_, err := crypto.Encrypt(k, ciphertext, buf)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(b, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
rd := bytes.NewReader(ciphertext)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.SetBytes(int64(size))
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
rd.Seek(0, 0)
|
2015-04-12 07:36:14 +00:00
|
|
|
decRd, err := crypto.DecryptFrom(k, rd)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(b, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
_, err = io.Copy(ioutil.Discard, decRd)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(b, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEncryptDecryptReader(b *testing.B) {
|
2015-04-30 02:28:34 +00:00
|
|
|
k := crypto.NewRandomKey()
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
size := 8 << 20 // 8MiB
|
2015-04-12 07:36:14 +00:00
|
|
|
rd := RandomReader(23, size)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.SetBytes(int64(size))
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
rd.Seek(0, 0)
|
|
|
|
buf.Reset()
|
2015-04-12 07:36:14 +00:00
|
|
|
wr := crypto.EncryptTo(k, buf)
|
2015-03-14 18:53:51 +00:00
|
|
|
_, err := io.Copy(wr, rd)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(b, err)
|
|
|
|
OK(b, wr.Close())
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2015-04-12 07:36:14 +00:00
|
|
|
r, err := crypto.DecryptFrom(k, buf)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(b, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
_, err = io.Copy(ioutil.Discard, r)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(b, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
restic.PoolAlloc()
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
ciphertext := restic.GetChunkBuf("BenchmarkDecrypt")
|
|
|
|
defer restic.FreeChunkBuf("BenchmarkDecrypt", ciphertext)
|
|
|
|
plaintext := restic.GetChunkBuf("BenchmarkDecrypt")
|
|
|
|
defer restic.FreeChunkBuf("BenchmarkDecrypt", plaintext)
|
|
|
|
|
2015-04-12 18:58:41 +00:00
|
|
|
ciphertext, err := crypto.Encrypt(k, ciphertext, data)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(b, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.SetBytes(int64(size))
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-04-12 18:58:41 +00:00
|
|
|
plaintext, err = crypto.Decrypt(k, plaintext, ciphertext)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(b, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEncryptStreamWriter(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}
|
|
|
|
if *testLargeCrypto {
|
|
|
|
tests = append(tests, 7<<20+123)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, size := range tests {
|
|
|
|
data := make([]byte, size)
|
2015-04-12 07:36:14 +00:00
|
|
|
_, err := io.ReadFull(RandomReader(42, size), data)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
ciphertext := bytes.NewBuffer(nil)
|
2015-04-12 07:36:14 +00:00
|
|
|
wr := crypto.EncryptTo(k, ciphertext)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
_, err = io.Copy(wr, bytes.NewReader(data))
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
|
|
|
OK(t, wr.Close())
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2015-04-12 07:41:47 +00:00
|
|
|
l := len(data) + crypto.Extension
|
2015-04-09 19:15:48 +00:00
|
|
|
Assert(t, len(ciphertext.Bytes()) == l,
|
2015-03-14 18:53:51 +00:00
|
|
|
"wrong ciphertext length: expected %d, got %d",
|
|
|
|
l, len(ciphertext.Bytes()))
|
|
|
|
|
|
|
|
// decrypt with default function
|
2015-04-12 07:36:14 +00:00
|
|
|
plaintext, err := crypto.Decrypt(k, []byte{}, ciphertext.Bytes())
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
|
|
|
Assert(t, bytes.Equal(data, plaintext),
|
2015-03-14 18:53:51 +00:00
|
|
|
"wrong plaintext after decryption: expected %02x, got %02x",
|
|
|
|
data, plaintext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecryptStreamReader(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}
|
|
|
|
if *testLargeCrypto {
|
|
|
|
tests = append(tests, 7<<20+123)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, size := range tests {
|
|
|
|
data := make([]byte, size)
|
2015-04-12 07:36:14 +00:00
|
|
|
_, err := io.ReadFull(RandomReader(42, size), data)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2015-04-12 07:41:47 +00:00
|
|
|
ciphertext := make([]byte, size+crypto.Extension)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
// encrypt with default function
|
2015-04-12 18:58:41 +00:00
|
|
|
ciphertext, err = crypto.Encrypt(k, ciphertext, data)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-04-12 18:58:41 +00:00
|
|
|
Assert(t, len(ciphertext) == len(data)+crypto.Extension,
|
2015-03-14 18:53:51 +00:00
|
|
|
"wrong number of bytes returned after encryption: expected %d, got %d",
|
2015-04-12 18:58:41 +00:00
|
|
|
len(data)+crypto.Extension, len(ciphertext))
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2015-04-12 07:36:14 +00:00
|
|
|
rd, err := crypto.DecryptFrom(k, bytes.NewReader(ciphertext))
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
plaintext, err := ioutil.ReadAll(rd)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2015-04-09 19:15:48 +00:00
|
|
|
Assert(t, bytes.Equal(data, plaintext),
|
2015-03-14 18:53:51 +00:00
|
|
|
"wrong plaintext after decryption: expected %02x, got %02x",
|
|
|
|
data, plaintext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEncryptWriter(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}
|
|
|
|
if *testLargeCrypto {
|
|
|
|
tests = append(tests, 7<<20+123)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, size := range tests {
|
|
|
|
data := make([]byte, size)
|
2015-04-12 07:36:14 +00:00
|
|
|
_, err := io.ReadFull(RandomReader(42, size), data)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
2015-04-12 07:36:14 +00:00
|
|
|
wr := crypto.EncryptTo(k, buf)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
_, err = io.Copy(wr, bytes.NewReader(data))
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
|
|
|
OK(t, wr.Close())
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
ciphertext := buf.Bytes()
|
|
|
|
|
2015-04-12 07:41:47 +00:00
|
|
|
l := len(data) + crypto.Extension
|
2015-04-09 19:15:48 +00:00
|
|
|
Assert(t, len(ciphertext) == l,
|
2015-03-14 18:53:51 +00:00
|
|
|
"wrong ciphertext length: expected %d, got %d",
|
|
|
|
l, len(ciphertext))
|
|
|
|
|
|
|
|
// decrypt with default function
|
2015-04-12 07:36:14 +00:00
|
|
|
plaintext, err := crypto.Decrypt(k, []byte{}, ciphertext)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
|
|
|
Assert(t, bytes.Equal(data, plaintext),
|
2015-03-14 18:53:51 +00:00
|
|
|
"wrong plaintext after decryption: expected %02x, got %02x",
|
|
|
|
data, plaintext)
|
|
|
|
}
|
|
|
|
}
|