-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto_test.go
More file actions
113 lines (101 loc) · 2.62 KB
/
Copy pathcrypto_test.go
File metadata and controls
113 lines (101 loc) · 2.62 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package cryptlite
import (
"bytes"
"testing"
)
func TestEncryptDecryptRoundtrip(t *testing.T) {
key, err := generateKey()
if err != nil {
t.Fatal(err)
}
plaintext := []byte(`{"hello":"world"}`)
aad := []byte("table/col/row1")
ct, nonce, err := encrypt(key, plaintext, aad)
if err != nil {
t.Fatal(err)
}
got, err := decrypt(key, ct, nonce, aad)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, plaintext) {
t.Fatalf("roundtrip mismatch: got %q want %q", got, plaintext)
}
}
func TestUniqueNoncePerEncryption(t *testing.T) {
key, _ := generateKey()
plaintext := []byte("same payload")
aad := []byte("ctx")
_, n1, _ := encrypt(key, plaintext, aad)
_, n2, _ := encrypt(key, plaintext, aad)
if bytes.Equal(n1, n2) {
t.Fatal("nonces must not repeat")
}
}
func TestDecryptFailsWrongKey(t *testing.T) {
key1, _ := generateKey()
key2, _ := generateKey()
ct, nonce, _ := encrypt(key1, []byte("secret"), nil)
if _, err := decrypt(key2, ct, nonce, nil); err == nil {
t.Fatal("expected decryption failure with wrong key")
}
}
func TestDecryptFailsWrongAAD(t *testing.T) {
key, _ := generateKey()
ct, nonce, _ := encrypt(key, []byte("secret"), []byte("ctx-a"))
if _, err := decrypt(key, ct, nonce, []byte("ctx-b")); err == nil {
t.Fatal("expected decryption failure with wrong AAD")
}
}
func TestDecryptFailsCorruptedCiphertext(t *testing.T) {
key, _ := generateKey()
ct, nonce, _ := encrypt(key, []byte("secret"), nil)
ct[0] ^= 0xFF
if _, err := decrypt(key, ct, nonce, nil); err == nil {
t.Fatal("expected decryption failure with corrupted ciphertext")
}
}
func TestKeyVersionPreserved(t *testing.T) {
blob := &EncryptedBlob{KeyVersion: 42}
if blob.KeyVersion != 42 {
t.Fatal("key version not preserved")
}
}
func TestEmptyPlaintext(t *testing.T) {
key, _ := generateKey()
ct, nonce, err := encrypt(key, []byte{}, nil)
if err != nil {
t.Fatal(err)
}
got, err := decrypt(key, ct, nonce, nil)
if err != nil {
t.Fatal(err)
}
if len(got) != 0 {
t.Fatalf("expected empty plaintext, got %q", got)
}
}
func TestLargeJSONBlob(t *testing.T) {
key, _ := generateKey()
large := make([]byte, 1<<20) // 1 MiB
for i := range large {
large[i] = byte(i % 256)
}
ct, nonce, err := encrypt(key, large, []byte("big"))
if err != nil {
t.Fatal(err)
}
got, err := decrypt(key, ct, nonce, []byte("big"))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, large) {
t.Fatal("large blob roundtrip mismatch")
}
}
func TestDecryptFailsEmptyNonce(t *testing.T) {
key, _ := generateKey()
if _, err := decrypt(key, []byte("ct"), []byte{}, nil); err == nil {
t.Fatal("expected error for wrong-size nonce")
}
}