-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcrypto_secretstream.js
226 lines (168 loc) · 9.91 KB
/
crypto_secretstream.js
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
/* eslint-disable camelcase */
const test = require('tape')
function getRandomInt (min, max) {
min = Math.ceil(min)
max = Math.floor(max)
return Math.floor(Math.random() * (max - min) + min) // The maximum is exclusive and the minimum is inclusive
}
module.exports = function (sodium) {
const NONCE_OFFSET = sodium.crypto_secretstream_xchacha20poly1305_KEYBYTES
const PAD_OFFSET = NONCE_OFFSET + 12
test('secretstream', (t) => {
const state = new Uint8Array(sodium.crypto_secretstream_xchacha20poly1305_STATEBYTES)
const statesave = new Uint8Array(sodium.crypto_secretstream_xchacha20poly1305_STATEBYTES)
const state_copy = new Uint8Array(sodium.crypto_secretstream_xchacha20poly1305_STATEBYTES)
const header = new Uint8Array(sodium.crypto_secretstream_xchacha20poly1305_HEADERBYTES)
const tag = new Uint8Array(1)
const ad_len = getRandomInt(0, 100)
const m1_len = getRandomInt(0, 1000)
const m2_len = getRandomInt(0, 1000)
const m3_len = getRandomInt(0, 1000)
const c1 = new Uint8Array(m1_len + sodium.crypto_secretstream_xchacha20poly1305_ABYTES)
const c2 = new Uint8Array(m2_len + sodium.crypto_secretstream_xchacha20poly1305_ABYTES)
const c3 = new Uint8Array(m3_len + sodium.crypto_secretstream_xchacha20poly1305_ABYTES)
const csave = new Uint8Array((m1_len | m2_len | m3_len) + sodium.crypto_secretstream_xchacha20poly1305_ABYTES)
const ad = new Uint8Array(ad_len)
const m1 = new Uint8Array(m1_len)
const m2 = new Uint8Array(m2_len)
const m3 = new Uint8Array(m3_len)
sodium.randombytes_buf(ad)
sodium.randombytes_buf(m1)
sodium.randombytes_buf(m2)
sodium.randombytes_buf(m3)
const m1_ = new Uint8Array(m1)
const m2_ = new Uint8Array(m2)
const m3_ = new Uint8Array(m3)
const k = new Uint8Array(sodium.crypto_secretstream_xchacha20poly1305_KEYBYTES)
sodium.crypto_secretstream_xchacha20poly1305_keygen(k)
/* push */
let ret = sodium.crypto_secretstream_xchacha20poly1305_init_push(state, header, k)
// t.equal(ret, 'init_push failed')
ret = sodium.crypto_secretstream_xchacha20poly1305_push(state, c1, m1, null, sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE) // how can ad be null here?
t.equal(ret, m1_len + sodium.crypto_secretstream_xchacha20poly1305_ABYTES)
ret = sodium.crypto_secretstream_xchacha20poly1305_push(state, c2, m2, ad, sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE)
t.equal(ret, m2_len + sodium.crypto_secretstream_xchacha20poly1305_ABYTES)
ret = sodium.crypto_secretstream_xchacha20poly1305_push(state, c3, m3, ad, sodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL)
t.ok(ret, 'third push failed')
/* pull */
sodium.crypto_secretstream_xchacha20poly1305_init_pull(state, header, k)
ret = sodium.crypto_secretstream_xchacha20poly1305_pull(state, m1, tag, c1, null)
t.equal(ret, m1_len, 'first pull passed')
t.equal(tag[0], sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE[0], 'tag pull failed')
t.same(m1, m1_, 'failed m1 memcmp')
ret = sodium.crypto_secretstream_xchacha20poly1305_pull(state, m2, tag, c2, ad)
t.equal(ret, m2_len, 'second pull failed')
t.equal(tag[0], sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE[0], 'second tag pull failed')
t.same(m2, m2_, 'failed m2 memcmp')
if (ad_len > 0) {
t.throws(() => { sodium.crypto_secretstream_xchacha20poly1305_pull(state, m3, tag, c3) })
}
ret = sodium.crypto_secretstream_xchacha20poly1305_pull(state, m3, tag, c3, ad)
t.equal(ret, m3_len, 'failed fourth pull')
t.equal(tag[0], sodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL[0], 'failed final tag pull')
t.same(m3, m3_, 'failed m3 memcmp')
/* previous with FINAL tag */
t.throws(() => {
sodium.crypto_secretstream_xchacha20poly1305_pull(state, m3, tag, c3, ad)
})
/* previous without a tag */
t.throws(() => {
sodium.crypto_secretstream_xchacha20poly1305_pull(state, m2, tag, c2, null)
})
/* short ciphertext */
t.throws(() => {
sodium.crypto_secretstream_xchacha20poly1305_pull(state, m2, tag,
c2.subarray(0, getRandomInt(0, sodium.crypto_secretstream_xchacha20poly1305_ABYTES)))
})
t.throws(() => {
sodium.crypto_secretstream_xchacha20poly1305_pull(state, m2, tag, c2, null)
})
/* empty ciphertext */
t.throws(() => {
sodium.crypto_secretstream_xchacha20poly1305_pull(state, m2, tag,
c2.subarray(0, sodium.crypto_secretstream_xchacha20poly1305_ABYTES))
})
/* without explicit rekeying */
ret = sodium.crypto_secretstream_xchacha20poly1305_init_push(state, header, k)
ret = sodium.crypto_secretstream_xchacha20poly1305_push(state, c1, m1, null, sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE)
t.equal(ret, m1_len + sodium.crypto_secretstream_xchacha20poly1305_ABYTES)
ret = sodium.crypto_secretstream_xchacha20poly1305_push(state, c2, m2, null, sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE)
t.equal(ret, m2_len + sodium.crypto_secretstream_xchacha20poly1305_ABYTES)
ret = sodium.crypto_secretstream_xchacha20poly1305_init_pull(state, header, k)
ret = sodium.crypto_secretstream_xchacha20poly1305_pull(state, m1, tag, c1, null)
t.equal(ret, m1_len)
ret = sodium.crypto_secretstream_xchacha20poly1305_pull(state, m2, tag, c2, null)
t.equal(ret, m2_len)
/* with explicit rekeying */
ret = sodium.crypto_secretstream_xchacha20poly1305_init_push(state, header, k)
ret = sodium.crypto_secretstream_xchacha20poly1305_push(state, c1, m1, null, sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE)
t.equal(ret, m1_len + sodium.crypto_secretstream_xchacha20poly1305_ABYTES)
sodium.crypto_secretstream_xchacha20poly1305_rekey(state)
ret = sodium.crypto_secretstream_xchacha20poly1305_push(state, c2, m2, null, sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE)
t.equal(ret, m2_len + sodium.crypto_secretstream_xchacha20poly1305_ABYTES)
ret = sodium.crypto_secretstream_xchacha20poly1305_init_pull(state, header, k)
ret = sodium.crypto_secretstream_xchacha20poly1305_pull(state, m1, tag, c1, null)
t.equal(ret, m1_len)
t.throws(() => {
sodium.crypto_secretstream_xchacha20poly1305_pull(state, m2, tag, c2, null)
})
sodium.crypto_secretstream_xchacha20poly1305_rekey(state)
ret = sodium.crypto_secretstream_xchacha20poly1305_pull(state, m2, tag, c2, null)
t.equal(ret, m2_len)
/* with explicit rekeying using TAG_REKEY */
sodium.crypto_secretstream_xchacha20poly1305_init_push(state, header, k)
statesave.set(state)
ret = sodium.crypto_secretstream_xchacha20poly1305_push(state, c1, m1, null, sodium.crypto_secretstream_xchacha20poly1305_TAG_REKEY)
t.equal(ret, m1_len + sodium.crypto_secretstream_xchacha20poly1305_ABYTES)
ret = sodium.crypto_secretstream_xchacha20poly1305_push(state, c2, m2, null, sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE)
t.equal(ret, m2_len + sodium.crypto_secretstream_xchacha20poly1305_ABYTES)
csave.subarray(0, m2_len + sodium.crypto_secretstream_xchacha20poly1305_ABYTES).set(c2.subarray(0, m2_len + sodium.crypto_secretstream_xchacha20poly1305_ABYTES))
sodium.crypto_secretstream_xchacha20poly1305_init_pull(state, header, k)
ret = sodium.crypto_secretstream_xchacha20poly1305_pull(state, m1, tag, c1, null)
t.equal(ret, m1_len)
t.equal(tag[0], sodium.crypto_secretstream_xchacha20poly1305_TAG_REKEY[0])
ret = sodium.crypto_secretstream_xchacha20poly1305_pull(state, m2, tag, c2, null)
t.equal(ret, m2_len)
t.equal(tag[0], sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE[0])
state.set(statesave)
ret = sodium.crypto_secretstream_xchacha20poly1305_push(state, c1, m1, null, sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE)
t.equal(ret, m1_len + sodium.crypto_secretstream_xchacha20poly1305_ABYTES)
ret = sodium.crypto_secretstream_xchacha20poly1305_push(state, c2, m2, null, sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE)
t.equal(ret, m2_len + sodium.crypto_secretstream_xchacha20poly1305_ABYTES)
t.notSame(
csave.subarray(0, m2_len + sodium.crypto_secretstream_xchacha20poly1305_ABYTES),
c2.subarray(0, m2_len + sodium.crypto_secretstream_xchacha20poly1305_ABYTES)
)
/* New stream */
sodium.crypto_secretstream_xchacha20poly1305_init_push(state, header, k)
ret = sodium.crypto_secretstream_xchacha20poly1305_push(state, c1, m1, null,
sodium.crypto_secretstream_xchacha20poly1305_TAG_PUSH)
t.equal(ret, m1_len + sodium.crypto_secretstream_xchacha20poly1305_ABYTES)
/* Force a counter overflow, check that the key has been updated
* even though the tag was not changed to REKEY */
const nonce = state.subarray(NONCE_OFFSET, PAD_OFFSET)
for (let i = 0; i < 4; i++) {
nonce[i] = 0xff
}
state_copy.set(state)
ret = sodium.crypto_secretstream_xchacha20poly1305_push(state, c2, m2, ad, sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE)
t.equal(ret, m2_len + sodium.crypto_secretstream_xchacha20poly1305_ABYTES)
t.notSame(state_copy.subarray(0, NONCE_OFFSET), state.subarray(0, NONCE_OFFSET))
t.notSame(state_copy.subarray(NONCE_OFFSET, PAD_OFFSET), state.subarray(NONCE_OFFSET, PAD_OFFSET))
t.equal(state.subarray(NONCE_OFFSET, PAD_OFFSET)[0], 1)
t.ok(sodium.sodium_is_zero(state.subarray(NONCE_OFFSET + 1, NONCE_OFFSET + 4)))
sodium.crypto_secretstream_xchacha20poly1305_init_pull(state, header, k)
ret = sodium.crypto_secretstream_xchacha20poly1305_pull(state, m1, tag, c1, null)
t.equal(ret, m1_len)
t.equal(tag[0], sodium.crypto_secretstream_xchacha20poly1305_TAG_PUSH[0])
t.same(m1, m1_)
for (let i = 0; i < 4; i++) {
nonce[i] = 0xff
}
ret = sodium.crypto_secretstream_xchacha20poly1305_pull(state, m2, tag, c2, ad)
t.equal(ret, m2_len)
t.equal(tag[0], sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE[0])
t.same(m2, m2_)
t.end()
})
}