forked from cloudflare/circl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aead_test.go
186 lines (154 loc) · 4.08 KB
/
aead_test.go
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package hpke
import (
"bytes"
"crypto/rand"
"fmt"
"testing"
"github.com/cloudflare/circl/internal/test"
)
func TestAeadExporter(t *testing.T) {
suite := Suite{kdfID: KDF_HKDF_SHA256, aeadID: AEAD_AES128GCM}
exporter := &encdecContext{suite: suite}
maxLength := uint(255 * suite.kdfID.ExtractSize())
err := test.CheckPanic(func() {
exporter.Export([]byte("exporter"), maxLength+1)
})
test.CheckNoErr(t, err, "exporter max size")
}
func setupAeadTest() (*sealContext, *openContext, error) {
suite := Suite{aeadID: AEAD_AES128GCM}
key := make([]byte, suite.aeadID.KeySize())
if n, err := rand.Read(key); err != nil {
return nil, nil, err
} else if n != len(key) {
return nil, nil, fmt.Errorf("unexpected key size: got %d; want %d", n, len(key))
}
aead, err := suite.aeadID.New(key)
if err != nil {
return nil, nil, err
}
Nn := suite.aeadID.NonceSize()
baseNonce := make([]byte, Nn)
if n, err := rand.Read(baseNonce); err != nil {
return nil, nil, err
} else if n != len(baseNonce) {
return nil, nil, fmt.Errorf("unexpected base nonce size: got %d; want %d", n, len(baseNonce))
}
sealer := &sealContext{
&encdecContext{
suite, nil, nil, nil, nil, nil, baseNonce, make([]byte, Nn), aead, make([]byte, Nn),
},
}
opener := &openContext{
&encdecContext{
suite, nil, nil, nil, nil, nil, baseNonce, make([]byte, Nn), aead, make([]byte, Nn),
},
}
return sealer, opener, nil
}
func TestAeadNonceUpdate(t *testing.T) {
sealer, opener, err := setupAeadTest()
test.CheckNoErr(t, err, "setup failed")
pt := []byte("plaintext")
aad := []byte("aad")
numAttempts := 2
var prevCt []byte
for i := 0; i < numAttempts; i++ {
ct, err := sealer.Seal(pt, aad)
if err != nil {
t.Fatalf("encryption failed: %s", err)
}
if prevCt != nil && bytes.Equal(ct, prevCt) {
t.Error("ciphertext matches the previous (nonce not updated)")
}
_, err = opener.Open(ct, aad)
if err != nil {
t.Errorf("decryption failed: %s", err)
}
prevCt = ct
}
}
func TestOpenPhaseMismatch(t *testing.T) {
sealer, opener, err := setupAeadTest()
test.CheckNoErr(t, err, "setup failed")
pt := []byte("plaintext")
aad := []byte("aad")
ct, err := sealer.Seal(pt, aad)
if err != nil {
t.Fatalf("encryption failed: %s", err)
}
recovered, err := opener.Open(ct, aad)
if err != nil {
t.Fatalf("decryption failed: %s", err)
}
if !bytes.Equal(pt, recovered) {
t.Fatal("Plaintext mismatch")
}
_, err = opener.Open(ct, aad)
if err == nil {
t.Fatal("decryption succeeded when it should have failed")
}
}
func TestSealPhaseMismatch(t *testing.T) {
sealer, opener, err := setupAeadTest()
test.CheckNoErr(t, err, "setup failed")
pt := []byte("plaintext")
aad := []byte("aad")
_, err = sealer.Seal(pt, aad)
if err != nil {
t.Fatalf("encryption failed: %s", err)
}
ct, err := sealer.Seal(pt, aad)
if err != nil {
t.Fatalf("encryption failed: %s", err)
}
_, err = opener.Open(ct, aad)
if err == nil {
t.Fatal("decryption succeeded when it should have failed")
}
}
func TestAeadSeqOverflow(t *testing.T) {
sealer, opener, err := setupAeadTest()
test.CheckNoErr(t, err, "setup failed")
Nn := len(sealer.baseNonce)
pt := []byte("plaintext")
aad := []byte("aad")
// Sets sequence number to 256 before its max value = 0xFF...FF.
for i := 0; i < Nn; i++ {
sealer.sequenceNumber[i] = 0xFF
opener.sequenceNumber[i] = 0xFF
}
sealer.sequenceNumber[Nn-1] = 0x00
opener.sequenceNumber[Nn-1] = 0x00
numAttempts := 260
wantCorrect := 2 * 255
wantIncorrect := 2*numAttempts - wantCorrect
gotCorrect := 0
gotIncorrect := 0
for i := 0; i < numAttempts; i++ {
ct, err := sealer.Seal(pt, aad)
switch {
case ct != nil && err == nil:
gotCorrect++
case ct == nil && err != nil:
gotIncorrect++
default:
t.FailNow()
}
pt2, err := opener.Open(ct, aad)
switch {
case pt2 != nil && err == nil:
gotCorrect++
case pt2 == nil && err != nil:
gotIncorrect++
default:
t.FailNow()
}
}
if gotCorrect != wantCorrect {
test.ReportError(t, gotCorrect, wantCorrect)
}
if gotIncorrect != wantIncorrect {
test.ReportError(t, gotIncorrect, wantIncorrect)
}
}