This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ratchet_test.go
343 lines (293 loc) · 9.43 KB
/
ratchet_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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package ratchet
import (
"crypto/rand"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func pairedRatchet(t *testing.T) (aRatchet, bRatchet *Ratchet) {
var err error
aRatchet, err = InitRatchet(rand.Reader)
require.NoError(t, err)
bRatchet, err = InitRatchet(rand.Reader)
require.NoError(t, err)
// create the key exchange blobs
akx, err := aRatchet.CreateKeyExchange()
require.NoError(t, err)
bkx, err := bRatchet.CreateKeyExchange()
require.NoError(t, err)
// do the actual key exchange
err = aRatchet.ProcessKeyExchange(bkx)
require.NoError(t, err)
err = bRatchet.ProcessKeyExchange(akx)
require.NoError(t, err)
return
}
func Test_KeyExchange(t *testing.T) {
a, b := pairedRatchet(t)
msg := []byte("test message")
encrypted, err := a.Encrypt(nil, msg)
require.NoError(t, err)
result, err := b.Decrypt(encrypted)
require.NoError(t, err)
require.Equal(t, msg, result)
DestroyRatchet(a)
DestroyRatchet(b)
}
func Test_RealKeyExchange(t *testing.T) {
// create two new ratchets
a, err := InitRatchet(rand.Reader)
require.NoError(t, err)
b, err := InitRatchet(rand.Reader)
require.NoError(t, err)
// create the key exchange blobs
akx, err := a.CreateKeyExchange()
require.NoError(t, err)
bkx, err := b.CreateKeyExchange()
require.NoError(t, err)
// do the actual key exchange
err = a.ProcessKeyExchange(bkx)
require.NoError(t, err)
err = b.ProcessKeyExchange(akx)
require.NoError(t, err)
// try to encrypt and decrypt a message
msg := []byte("test message")
encrypted, err := a.Encrypt(nil, msg)
require.NoError(t, err)
result, err := b.Decrypt(encrypted)
require.NoError(t, err)
require.Equal(t, msg, result)
msg2 := []byte(`This essay might seem to focus on the ethical weight of
each scientist’s personal, professional choices. But I am actually more concerned
about how we, as cryptographers and computer scientists, act in aggregate. Our
collective behavior embodies values—and the institutions we create do, too.`)
encrypted, err = a.Encrypt(nil, msg2)
require.NoError(t, err)
result, err = b.Decrypt(encrypted)
require.NoError(t, err)
require.Equal(t, msg2, result)
_, err = b.CreateKeyExchange()
require.Error(t, ErrHandshakeAlreadyComplete, err)
err = a.ProcessKeyExchange(bkx)
require.Error(t, ErrHandshakeAlreadyComplete, err)
DestroyRatchet(a)
DestroyRatchet(b)
err = a.ProcessKeyExchange(bkx)
require.Error(t, ErrHandshakeAlreadyComplete, err)
_, err = b.CreateKeyExchange()
require.Error(t, ErrHandshakeAlreadyComplete, err)
}
func Test_Serialization0(t *testing.T) {
// create two new ratchets
a, err := InitRatchet(rand.Reader)
require.NoError(t, err)
_, err = a.Save()
require.NoError(t, err)
}
func Test_Serialization1(t *testing.T) {
a, b := pairedRatchet(t)
// 1
msg := []byte("test message number one is a short one")
encrypted, err := a.Encrypt(nil, msg)
require.NoError(t, err)
result, err := b.Decrypt(encrypted)
require.NoError(t, err)
require.Equal(t, msg, result)
serialized, err := a.Save()
require.NoError(t, err)
r, err := NewRatchetFromBytes(rand.Reader, serialized)
require.NoError(t, err)
// 2
msg2 := []byte(`The word privacy, its meaning abstract and debated, its connotations often
negative, is not a winning word. Privacy is for medical records, toileting, and
sex — not for democracy or freedom. The word anonymity is even worse: modern
political parlance has painted this as nearly a flavor of terrorism. Security is
more winning a word and, in fact, I spoke of secure messaging instead of private
messaging or anonymous messaging because I think it better captures what I
want conveyed: that a communication whose endpoints are manifest is not at all
secure. A person needs to feel insecure if using such a channel.`)
encrypted, err = r.Encrypt(nil, msg2)
require.NoError(t, err)
result, err = b.Decrypt(encrypted)
require.NoError(t, err)
require.Equal(t, msg2, result)
// 3
msg3 := []byte(`But even the word security doesn’t support a good framing of our problem:
we should try to speak of thwarting mass surveillance more than enhancing
privacy, anonymity, or security. As discussed before, we know instinctively
that ubiquitous surveillance is incompatible with freedom, democracy, and
human rights. 189 This makes surveillance a thing against which one can fight.
The surveillance camera and data center make visual our emerging dystopia,
while privacy, anonymity, and security are so abstract as to nearly defy visual
representation.`)
encrypted, err = r.Encrypt(nil, msg3)
require.NoError(t, err)
result, err = b.Decrypt(encrypted)
require.NoError(t, err)
require.Equal(t, msg3, result)
DestroyRatchet(a)
DestroyRatchet(b)
DestroyRatchet(r)
}
type scriptAction struct {
// object is one of sendA, sendB or sendDelayed. The first two options
// cause a message to be sent from one party to the other. The latter
// causes a previously delayed message, identified by id, to be
// delivered.
object int
// result is one of deliver, drop or delay. If delay, then the message
// is stored using the value in id. This value can be repeated later
// with a sendDelayed.
result int
id int
}
const (
sendA = iota
sendB
sendDelayed
deliver
drop
delay
)
func reinitRatchet(t *testing.T, r *Ratchet) *Ratchet {
state, err := r.Save()
require.NoError(t, err)
DestroyRatchet(r)
newR, err := NewRatchetFromBytes(rand.Reader, state)
require.NoError(t, err)
return newR
}
func testScript(t *testing.T, script []scriptAction) {
type delayedMessage struct {
msg []byte
encrypted []byte
fromA bool
}
delayedMessages := make(map[int]delayedMessage)
a, b := pairedRatchet(t)
for i, action := range script {
switch action.object {
case sendA, sendB:
sender, receiver := a, b
if action.object == sendB {
sender, receiver = receiver, sender
}
var msg [20]byte
rand.Reader.Read(msg[:])
encrypted, err := sender.Encrypt(nil, msg[:])
require.NoError(t, err)
switch action.result {
case deliver:
result, err := receiver.Decrypt(encrypted)
require.NoError(t, err, fmt.Sprintf("#%d: receiver returned error: %s", i, err))
require.Equal(t, msg[:], result, fmt.Sprintf("#%d: bad message: got %x, not %x", i, result, msg[:]))
case delay:
_, ok := delayedMessages[action.id]
require.False(t, ok, fmt.Sprintf("#%d: already have delayed message with id %d", i, action.id))
delayedMessages[action.id] = delayedMessage{msg[:], encrypted, sender == a}
case drop:
}
case sendDelayed:
delayed, ok := delayedMessages[action.id]
require.True(t, ok, fmt.Sprintf("#%d: no such delayed message id: %d", i, action.id))
receiver := a
if delayed.fromA {
receiver = b
}
result, err := receiver.Decrypt(delayed.encrypted)
require.NoError(t, err, fmt.Sprintf("#%d: receiver returned error: %s", i, err))
require.Equal(t, delayed.msg, result, fmt.Sprintf("#%d: bad message: got %x, not %x", i, result, delayed.msg))
}
a = reinitRatchet(t, a)
b = reinitRatchet(t, b)
}
}
func Test_RatchetBackAndForth(t *testing.T) {
testScript(t, []scriptAction{
{sendA, deliver, -1},
{sendB, deliver, -1},
{sendA, deliver, -1},
{sendB, deliver, -1},
{sendA, deliver, -1},
{sendB, deliver, -1},
})
}
func Test_RatchetReordering(t *testing.T) {
script := []scriptAction{}
script = append(script, scriptAction{sendA, deliver, -1})
for i := 0; i < MaxMissingMessages; i++ {
script = append(script, scriptAction{sendA, delay, i})
}
for i := MaxMissingMessages; i >= 0; i-- {
script = append(script, scriptAction{sendA, deliver, i})
}
testScript(t, script)
}
func Test_RatchetReorderAfterDHRatchet(t *testing.T) {
testScript(t, []scriptAction{
{sendA, deliver, -1},
{sendA, delay, 0},
{sendB, deliver, -1},
{sendA, deliver, -1},
{sendB, deliver, -1},
{sendDelayed, deliver, 0},
})
}
func Test_RatchetDroppedMessages(t *testing.T) {
testScript(t, []scriptAction{
{sendA, drop, -1},
{sendA, drop, -1},
{sendA, drop, -1},
{sendA, drop, -1},
{sendA, deliver, -1},
{sendB, deliver, -1},
})
}
func Test_serialize_savedkeys(t *testing.T) {
a, b := pairedRatchet(t)
msg := []byte("test message")
encrypted1, err := a.Encrypt(nil, msg)
require.NoError(t, err)
encrypted2, err := a.Encrypt(nil, msg)
require.NoError(t, err)
encrypted3, err := a.Encrypt(nil, msg)
require.NoError(t, err)
result, err := b.Decrypt(encrypted2)
require.NoError(t, err)
require.Equal(t, msg, result)
serialized, err := a.Save()
require.NoError(t, err)
serialized2, err := b.Save()
require.NoError(t, err)
_, err = NewRatchetFromBytes(rand.Reader, serialized)
require.NoError(t, err)
l, err := NewRatchetFromBytes(rand.Reader, serialized2)
require.NoError(t, err)
result, err = l.Decrypt(encrypted3)
require.NoError(t, err)
result, err = l.Decrypt(encrypted1)
require.NoError(t, err)
}
func Test_RatchetDuplicateMessage(t *testing.T) {
a, b := pairedRatchet(t)
msg1 := []byte("test message 1")
msg2 := []byte("test message 2")
msg3 := []byte("test message 3")
encrypted1, err := a.Encrypt(nil, msg1)
require.NoError(t, err)
encrypted2, err := a.Encrypt(nil, msg2)
require.NoError(t, err)
encrypted3, err := a.Encrypt(nil, msg3)
require.NoError(t, err)
result, err := b.Decrypt(encrypted2)
require.NoError(t, err)
require.Equal(t, msg2, result)
result, err = b.Decrypt(encrypted2)
require.Error(t, ErrDuplicateOrDelayed)
result, err = b.Decrypt(encrypted1)
require.NoError(t, err)
require.Equal(t, msg1, result)
result, err = b.Decrypt(encrypted3)
require.NoError(t, err)
require.Equal(t, msg3, result)
}