-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsession_test.go
242 lines (202 loc) · 5.74 KB
/
session_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
package doubleratchet
import (
"fmt"
"github.com/stretchr/testify/require"
"testing"
)
func TestNew(t *testing.T) {
// Act.
var (
si, err = New(sk, bobPair)
s = si.(*session)
)
// Assert.
require.Nil(t, err)
require.NotEqual(t, Key{}, s.DHs.PrivateKey())
require.NotEqual(t, Key{}, s.DHs.PublicKey())
}
func TestNew_BadOption(t *testing.T) {
// Act.
_, err := New(sk, bobPair, WithMaxSkip(-10))
// Assert.
require.NotNil(t, err)
}
func TestNewWithRemoteKey(t *testing.T) {
// Act.
var (
si, err = NewWithRemoteKey(sk, bobPair.PublicKey())
s = si.(*session)
)
// Assert.
require.Nil(t, err)
require.Equal(t, bobPair.PublicKey(), s.DHr)
require.NotEqual(t, dhPair{}, s.DHs)
require.NotEqual(t, Key{}, s.RootCh.CK)
require.NotEqual(t, sk, s.RootCh.CK)
require.NotEqual(t, Key{}, s.SendCh.CK)
require.NotEqual(t, sk, s.SendCh.CK)
}
func TestNewWithRemoteKey_BadOption(t *testing.T) {
// Act.
_, err := NewWithRemoteKey(sk, bobPair.PublicKey(), WithMaxSkip(-10))
// Assert.
require.NotNil(t, err)
}
func TestSession_RatchetEncrypt_Basic(t *testing.T) {
// Arrange.
var (
si, err = NewWithRemoteKey(sk, bobPair.PublicKey())
s = si.(*session)
oldCKs = s.SendCh.CK
)
// Act.
m := si.RatchetEncrypt([]byte("1337"), nil)
// Assert.
require.Nil(t, err)
require.NotEqual(t, oldCKs, s.SendCh.CK)
require.EqualValues(t, 1, s.SendCh.N)
require.Equal(t, MessageHeader{
DH: s.DHs.PublicKey(),
N: 0,
PN: 0,
}, m.Header)
require.NotEmpty(t, m.Ciphertext)
}
func TestSession_RatchetDecrypt_CommunicationFailedWithNoPublicKey(t *testing.T) {
// Arrange.
var (
bob, _ = New(sk, bobPair)
alice, _ = New(sk, alicePair)
)
// Act.
var (
m = alice.RatchetEncrypt([]byte("something important"), nil)
_, err = bob.RatchetDecrypt(m, nil)
)
// Assert.
require.NotNil(t, err) // Invalid signature.
}
func TestSession_RatchetDecrypt_CommunicationAliceSends(t *testing.T) {
// Arrange.
var (
bob, _ = New(sk, bobPair)
alice, _ = NewWithRemoteKey(sk, bobPair.PublicKey())
)
for i := 0; i < 10; i++ {
pt := fmt.Sprintf("msg%d", i)
t.Run(pt, func(t *testing.T) {
h := SessionTestHelper{t, alice, bob}
h.AliceToBob(pt, []byte("alice associated data"))
})
}
}
func TestSession_RatchetDecrypt_CommunicationBobSends(t *testing.T) {
var (
bob, _ = New(sk, bobPair)
alice, _ = NewWithRemoteKey(sk, bobPair.PublicKey())
)
for i := 0; i < 10; i++ {
pt := fmt.Sprintf("msg%d", i)
t.Run(pt, func(t *testing.T) {
h := SessionTestHelper{t, alice, bob}
h.BobToAlice(pt, []byte("bob associated data"))
})
}
}
func TestSession_RatchetDecrypt_CommunicationPingPong(t *testing.T) {
// Arrange.
var (
bob, _ = New(sk, bobPair)
alice, _ = NewWithRemoteKey(sk, bobPair.PublicKey())
)
for i := 0; i < 10; i++ {
pt := fmt.Sprintf("msg%d", i)
t.Run(pt, func(t *testing.T) {
h := SessionTestHelper{t, alice, bob}
h.AliceToBob(pt+"alice", []byte("alice associated data"))
h.BobToAlice(pt+"bob", []byte("bob associated data"))
})
}
}
func TestSession_RatchetDecrypt_CommunicationSkippedMessages(t *testing.T) {
// Arrange.
var (
bobI, _ = New(sk, bobPair, WithMaxSkip(1))
bob = bobI.(*session)
aliceI, _ = NewWithRemoteKey(sk, bob.DHs.PublicKey(), WithMaxSkip(1))
alice = aliceI.(*session)
)
t.Run("skipped messages from alice", func(t *testing.T) {
// Arrange.
var (
m0 = alice.RatchetEncrypt([]byte("hi"), nil)
m1 = alice.RatchetEncrypt([]byte("bob"), nil)
m2 = alice.RatchetEncrypt([]byte("how are you?"), nil)
m3 = alice.RatchetEncrypt([]byte("still do cryptography?"), nil)
)
// Act and assert.
m1.Ciphertext[len(m1.Ciphertext)-1] ^= 10
_, err := bob.RatchetDecrypt(m1, nil) // Error: invalid signature.
require.NotNil(t, err)
require.EqualValues(t, 0, bob.MkSkipped.Count(bob.DHr))
m1.Ciphertext[len(m1.Ciphertext)-1] ^= 10
d, err := bob.RatchetDecrypt(m1, nil) // Decrypted and skipped.
require.Nil(t, err)
require.Equal(t, []byte("bob"), d)
require.EqualValues(t, 1, bob.MkSkipped.Count(bob.DHr))
_, err = bob.RatchetDecrypt(m3, nil) // Error: too many to skip.
require.NotNil(t, err)
d, err = bob.RatchetDecrypt(m2, nil) // Decrypted.
require.Nil(t, err)
require.Equal(t, []byte("how are you?"), d)
m3.Ciphertext[len(m3.Ciphertext)-1] ^= 10
_, err = bob.RatchetDecrypt(m3, nil) // Error: invalid signature.
require.NotNil(t, err)
m3.Ciphertext[len(m3.Ciphertext)-1] ^= 10
d, err = bob.RatchetDecrypt(m3, nil) // Decrypted.
require.Nil(t, err)
require.Equal(t, []byte("still do cryptography?"), d)
d, err = bob.RatchetDecrypt(m0, nil) // Decrypted.
require.Nil(t, err)
require.Equal(t, []byte("hi"), d)
})
}
func TestSession_SkippedKeysDeletion(t *testing.T) {
// Arrange.
var (
bob, _ = New(sk, bobPair, WithMaxKeep(2))
alice, _ = NewWithRemoteKey(sk, bobPair.PublicKey(), WithMaxKeep(2))
h = SessionTestHelper{t, alice, bob}
)
// Act.
m0 := alice.RatchetEncrypt([]byte("Hi"), nil)
h.AliceToBob("Bob!", nil) // Bob ratchet step 1.
h.BobToAlice("Alice?", nil) // Alice ratchet step 1.
h.AliceToBob("How are you?", nil) // Bob ratchet step 2.
// Assert.
_, err := bob.RatchetDecrypt(m0, nil)
require.NotNil(t, err)
}
type SessionTestHelper struct {
t *testing.T
alice Session
bob Session
}
func (h SessionTestHelper) AliceToBob(msg string, ad []byte) {
var (
msgByte = []byte(msg)
m = h.alice.RatchetEncrypt(msgByte, ad)
d, err = h.bob.RatchetDecrypt(m, ad)
)
require.Nil(h.t, err)
require.EqualValues(h.t, msgByte, d)
}
func (h SessionTestHelper) BobToAlice(msg string, ad []byte) {
var (
msgByte = []byte(msg)
m = h.bob.RatchetEncrypt(msgByte, ad)
d, err = h.alice.RatchetDecrypt(m, ad)
)
require.Nil(h.t, err)
require.EqualValues(h.t, msgByte, d)
}