-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpace_test.go
318 lines (279 loc) · 7.88 KB
/
cpace_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright 2020 Google LLC
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package cpace_test
import (
"bytes"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"fmt"
"io"
"strings"
"testing"
"filippo.io/cpace"
"github.com/gtank/ristretto255"
"golang.org/x/crypto/hkdf"
)
func Example() {
password := "password"
c := cpace.NewContextInfo("192.0.2.1:12345", "192.0.2.2:42", nil)
msgA, s, err := cpace.Start(password, c)
if err != nil {
panic(err)
}
msgB, keyB, err := cpace.Exchange(password, c, msgA)
if err != nil {
panic(err)
}
keyA, err := s.Finish(msgB)
if err != nil {
panic(err)
}
fmt.Println("keyA == keyB:", bytes.Equal(keyA, keyB))
// Output: keyA == keyB: true
}
func TestTranscript(t *testing.T) {
// Don't try this at home.
defer func(original io.Reader) { rand.Reader = original }(rand.Reader)
rand.Reader = hkdf.Expand(sha256.New, []byte("INSECURE"), nil)
password := "password"
c := cpace.NewContextInfo("a", "b", []byte("ad"))
tx := sha256.New()
msgA, s, err := cpace.Start(password, c)
if err != nil {
t.Fatal(err)
}
t.Logf("msgA: %x\n", msgA)
tx.Write(msgA)
msgB, key, err := cpace.Exchange(password, c, msgA)
if err != nil {
t.Fatal(err)
}
t.Logf("msgB: %x\n", msgB)
tx.Write(msgB)
t.Logf("key: %x\n", key)
tx.Write(key)
if keyA, err := s.Finish(msgB); err != nil {
t.Fatal(err)
} else if !bytes.Equal(key, keyA) {
t.Error("keys were not equal")
}
expected := "NpHB1PcNLBGo9idbTXys5aRkuAlV+FQAshGfsJoxs3g"
if h := base64.RawStdEncoding.EncodeToString(tx.Sum(nil)); h != expected {
t.Errorf("transcript hash changed: got %q, expected %q", h, expected)
}
}
func BenchmarkStart(b *testing.B) {
password := "password"
c := cpace.NewContextInfo("192.0.2.1:12345", "192.0.2.2:42", []byte("ad"))
for i := 0; i < b.N; i++ {
msgA, s, err := cpace.Start(password, c)
if len(msgA) != 16+32 || s == nil || err != nil {
panic(err)
}
}
}
func BenchmarkExchange(b *testing.B) {
password := "password"
c := cpace.NewContextInfo("192.0.2.1:12345", "192.0.2.2:42", []byte("ad"))
msgA, _, err := cpace.Start(password, c)
if err != nil {
panic(err)
}
for i := 0; i < b.N; i++ {
msgB, key, err := cpace.Exchange(password, c, msgA)
if len(msgB) != 32 || len(key) != 32 || err != nil {
panic(err)
}
}
}
func BenchmarkFinish(b *testing.B) {
password := "password"
c := cpace.NewContextInfo("192.0.2.1:12345", "192.0.2.2:42", []byte("ad"))
msgA, s, err := cpace.Start(password, c)
if err != nil {
panic(err)
}
msgB, _, err := cpace.Exchange(password, c, msgA)
if err != nil {
panic(err)
}
for i := 0; i < b.N; i++ {
key, err := s.Finish(msgB)
if len(key) != 32 || err != nil {
panic(err)
}
}
}
func TestLargeContextValues(t *testing.T) {
password := "password"
validC := cpace.NewContextInfo(strings.Repeat("a", 1<<16-1), "b", nil)
badC := cpace.NewContextInfo(strings.Repeat("a", 1<<16), "b", nil)
msgA, _, err := cpace.Start(password, validC)
if err != nil {
t.Fatal(err)
}
if _, _, err := cpace.Exchange(password, validC, msgA); err != nil {
t.Fatal(err)
}
if _, _, err := cpace.Start(password, badC); err == nil {
t.Error("expected error for long context value")
}
if _, _, err := cpace.Exchange(password, badC, msgA); err == nil {
t.Error("expected error for long context value")
}
}
func TestBrokenMessages(t *testing.T) {
password := "password"
c := cpace.NewContextInfo("192.0.2.1:12345", "192.0.2.2:42", nil)
msgA, s, err := cpace.Start(password, c)
if err != nil {
t.Fatal(err)
}
if _, key, err := cpace.Exchange(password, c, msgA[:len(msgA)-1]); err == nil {
t.Error("expected error for short msgA")
} else if key != nil {
t.Error("on error, key was not nil")
}
msgA[len(msgA)-1] ^= 0xff
if _, key, err := cpace.Exchange(password, c, msgA[:len(msgA)-1]); err == nil {
t.Error("expected error for modified msgA")
} else if key != nil {
t.Error("on error, key was not nil")
}
msgA[len(msgA)-1] ^= 0xff
msgB, _, err := cpace.Exchange(password, c, msgA)
if err != nil {
t.Fatal(err)
}
if key, err := s.Finish(msgB[:len(msgB)-1]); err == nil {
t.Error("expected error for short msgB")
} else if key != nil {
t.Error("on error, key was not nil")
}
msgB[len(msgB)-1] ^= 0xff
if key, err := s.Finish(msgB[:len(msgB)-1]); err == nil {
t.Error("expected error for modified msgB")
} else if key != nil {
t.Error("on error, key was not nil")
}
msgB[len(msgB)-1] ^= 0xff
}
func TestResults(t *testing.T) {
tests := []struct {
Name string
PasswordA, PasswordB string
ContextA, ContextB *cpace.ContextInfo
Equal bool
}{
{
Name: "valid, without ad", Equal: true,
PasswordA: "p", PasswordB: "p",
ContextA: cpace.NewContextInfo("a", "b", nil),
ContextB: cpace.NewContextInfo("a", "b", nil),
},
{
Name: "valid, with ad", Equal: true,
PasswordA: "p", PasswordB: "p",
ContextA: cpace.NewContextInfo("a", "b", []byte("x")),
ContextB: cpace.NewContextInfo("a", "b", []byte("x")),
},
{
Name: "valid, equal identities", Equal: true,
PasswordA: "p", PasswordB: "p",
ContextA: cpace.NewContextInfo("a", "a", nil),
ContextB: cpace.NewContextInfo("a", "a", nil),
},
{
Name: "different passwords", Equal: false,
PasswordA: "p", PasswordB: "P",
ContextA: cpace.NewContextInfo("a", "b", nil),
ContextB: cpace.NewContextInfo("a", "b", nil),
},
{
Name: "different identity a", Equal: false,
PasswordA: "p", PasswordB: "p",
ContextA: cpace.NewContextInfo("a", "b", nil),
ContextB: cpace.NewContextInfo("x", "b", nil),
},
{
Name: "different identity b", Equal: false,
PasswordA: "p", PasswordB: "p",
ContextA: cpace.NewContextInfo("a", "b", nil),
ContextB: cpace.NewContextInfo("a", "x", nil),
},
{
Name: "different ad", Equal: false,
PasswordA: "p", PasswordB: "p",
ContextA: cpace.NewContextInfo("a", "b", []byte("foo")),
ContextB: cpace.NewContextInfo("a", "b", []byte("bar")),
},
{
Name: "swapped identities", Equal: false,
PasswordA: "p", PasswordB: "p",
ContextA: cpace.NewContextInfo("a", "b", nil),
ContextB: cpace.NewContextInfo("b", "a", nil),
},
{
Name: "missing ad", Equal: false,
PasswordA: "p", PasswordB: "p",
ContextA: cpace.NewContextInfo("a", "b", []byte("x")),
ContextB: cpace.NewContextInfo("a", "b", nil),
},
{
Name: "identity concatenation", Equal: false,
PasswordA: "p", PasswordB: "p",
ContextA: cpace.NewContextInfo("ax", "b", nil),
ContextB: cpace.NewContextInfo("a", "xb", nil),
},
{
Name: "empty password", Equal: false,
PasswordA: "p", PasswordB: "",
ContextA: cpace.NewContextInfo("a", "b", nil),
ContextB: cpace.NewContextInfo("a", "b", nil),
},
}
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
msgA, s, err := cpace.Start(tt.PasswordA, tt.ContextA)
if err != nil {
t.Fatal(err)
}
msgB, keyB, err := cpace.Exchange(tt.PasswordB, tt.ContextB, msgA)
if err != nil {
t.Fatal(err)
}
keyA, err := s.Finish(msgB)
if err != nil {
t.Fatal(err)
}
if len(keyA) != 32 {
t.Errorf("keyA length is %v, expected %v", len(keyA), 32)
}
if len(keyB) != 32 {
t.Errorf("keyB length is %v, expected %v", len(keyB), 32)
}
if eq := bytes.Equal(keyA, keyB); eq != tt.Equal {
t.Errorf("keyA == keyB is %v, expected %v", eq, tt.Equal)
}
})
}
}
func TestIdentity(t *testing.T) {
password := "password"
c := cpace.NewContextInfo("192.0.2.1:12345", "192.0.2.2:42", nil)
msgA, s, err := cpace.Start(password, c)
if err != nil {
t.Fatal(err)
}
identity := ristretto255.NewElement().Zero()
if _, _, err := cpace.Exchange(password, c, identity.Encode(msgA[:16])); err == nil {
t.Error("expected error for identity value")
}
if _, err := s.Finish(identity.Encode(nil)); err == nil {
t.Error("expected error for identity value")
}
}