-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathcrypto_stream.js
212 lines (158 loc) · 6.74 KB
/
crypto_stream.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
const test = require('brittle')
const sodium = require('..')
test('crypto_stream', function (t) {
const buf = Buffer.alloc(50)
const nonce = random(sodium.crypto_stream_NONCEBYTES)
const key = random(sodium.crypto_stream_KEYBYTES)
sodium.crypto_stream(buf, nonce, key)
t.not(buf, Buffer.alloc(50), 'contains noise now')
const copy = Buffer.from(buf.toString('hex'), 'hex')
sodium.crypto_stream(buf, nonce, key)
t.alike(buf, copy, 'predictable from nonce, key')
})
test('crypto_stream_xor', function (t) {
const message = Buffer.from('Hello, World!')
const nonce = random(sodium.crypto_stream_NONCEBYTES)
const key = random(sodium.crypto_stream_KEYBYTES)
sodium.crypto_stream_xor(message, message, nonce, key)
t.not(message, Buffer.from('Hello, World!'), 'encrypted')
sodium.crypto_stream_xor(message, message, nonce, key)
t.alike(message, Buffer.from('Hello, World!'), 'decrypted')
})
test('crypto_stream_xor state', function (t) {
const message = Buffer.from('Hello, world!')
const nonce = random(sodium.crypto_stream_NONCEBYTES)
const key = random(sodium.crypto_stream_KEYBYTES)
const out = Buffer.alloc(message.length)
const state = Buffer.alloc(sodium.crypto_stream_xor_STATEBYTES)
sodium.crypto_stream_xor_init(state, nonce, key)
for (let i = 0; i < message.length; i++) {
sodium.crypto_stream_xor_update(state, out.slice(i, i + 1), message.slice(i, i + 1))
}
sodium.crypto_stream_xor_final(state)
sodium.crypto_stream_xor(out, out, nonce, key)
t.alike(out, message, 'decrypted')
})
test('crypto_stream_xor state with empty buffers', function (t) {
const message = Buffer.from('Hello, world!')
const nonce = random(sodium.crypto_stream_NONCEBYTES)
const key = random(sodium.crypto_stream_KEYBYTES)
const out = Buffer.alloc(message.length)
const state = Buffer.alloc(sodium.crypto_stream_xor_STATEBYTES)
sodium.crypto_stream_xor_init(state, nonce, key)
sodium.crypto_stream_xor_update(state, Buffer.alloc(0), Buffer.alloc(0))
for (let i = 0; i < message.length; i++) {
sodium.crypto_stream_xor_update(state, out.slice(i, i + 1), message.slice(i, i + 1))
sodium.crypto_stream_xor_update(state, Buffer.alloc(0), Buffer.alloc(0))
}
sodium.crypto_stream_xor_final(state)
sodium.crypto_stream_xor(out, out, nonce, key)
t.alike(out, message, 'decrypted')
})
test('crypto_stream_xor state long stream', function (t) {
const nonce = random(sodium.crypto_stream_NONCEBYTES)
const key = random(sodium.crypto_stream_KEYBYTES)
const encState = Buffer.alloc(sodium.crypto_stream_xor_STATEBYTES)
const decState = Buffer.alloc(sodium.crypto_stream_xor_STATEBYTES)
sodium.crypto_stream_xor_init(encState, nonce, key)
sodium.crypto_stream_xor_init(decState, nonce, key)
const plain = []
const encrypted = []
const decrypted = []
for (let i = 0; i < 1000; i++) {
const next = random(61)
plain.push(next)
const enc = Buffer.alloc(61)
sodium.crypto_stream_xor_update(encState, enc, next)
encrypted.push(enc)
const dec = Buffer.alloc(61)
sodium.crypto_stream_xor_update(decState, dec, enc)
decrypted.push(dec)
}
const enc2 = Buffer.alloc(1000 * 61)
sodium.crypto_stream_xor(enc2, Buffer.concat(plain), nonce, key)
t.alike(Buffer.concat(encrypted), enc2, 'same as encrypting all at once')
t.alike(Buffer.concat(decrypted), Buffer.concat(plain), 'decrypts')
})
test('crypto_stream_xor state long stream (random chunks)', function (t) {
const nonce = random(sodium.crypto_stream_NONCEBYTES)
const key = random(sodium.crypto_stream_KEYBYTES)
const encState = Buffer.alloc(sodium.crypto_stream_xor_STATEBYTES)
const decState = Buffer.alloc(sodium.crypto_stream_xor_STATEBYTES)
sodium.crypto_stream_xor_init(encState, nonce, key)
sodium.crypto_stream_xor_init(decState, nonce, key)
const plain = []
const encrypted = []
const decrypted = []
for (let i = 0; i < 10000; i++) {
const len = Math.floor(Math.random() * 256)
const next = random(len)
plain.push(next)
const enc = Buffer.alloc(len)
sodium.crypto_stream_xor_update(encState, enc, next)
encrypted.push(enc)
const dec = Buffer.alloc(len)
sodium.crypto_stream_xor_update(decState, dec, enc)
decrypted.push(dec)
}
const enc2 = Buffer.alloc(Buffer.concat(plain).length)
sodium.crypto_stream_xor(enc2, Buffer.concat(plain), nonce, key)
t.alike(Buffer.concat(encrypted), enc2, 'same as encrypting all at once')
t.alike(Buffer.concat(decrypted), Buffer.concat(plain), 'decrypts')
})
test('crypto_stream_xor state long stream (random chunks) with empty buffers', function (t) {
const nonce = random(sodium.crypto_stream_NONCEBYTES)
const key = random(sodium.crypto_stream_KEYBYTES)
const encState = Buffer.alloc(sodium.crypto_stream_xor_STATEBYTES)
const decState = Buffer.alloc(sodium.crypto_stream_xor_STATEBYTES)
sodium.crypto_stream_xor_init(encState, nonce, key)
sodium.crypto_stream_xor_init(decState, nonce, key)
const plain = []
const encrypted = []
const decrypted = []
for (let i = 0; i < 10000; i++) {
const len = Math.floor(Math.random() * 256)
const next = random(len)
plain.push(next)
sodium.crypto_stream_xor_update(encState, Buffer.alloc(0), Buffer.alloc(0))
const enc = Buffer.alloc(len)
sodium.crypto_stream_xor_update(encState, enc, next)
encrypted.push(enc)
const dec = Buffer.alloc(len)
sodium.crypto_stream_xor_update(decState, dec, enc)
decrypted.push(dec)
sodium.crypto_stream_xor_update(decState, Buffer.alloc(0), Buffer.alloc(0))
}
const enc2 = Buffer.alloc(Buffer.concat(plain).length)
sodium.crypto_stream_xor(enc2, Buffer.concat(plain), nonce, key)
t.alike(Buffer.concat(encrypted), enc2, 'same as encrypting all at once')
t.alike(Buffer.concat(decrypted), Buffer.concat(plain), 'decrypts')
})
test('crypto_stream_xor state after GC', function (t) {
const message = Buffer.from('Hello, world!')
let nonce = random(sodium.crypto_stream_NONCEBYTES)
let key = random(sodium.crypto_stream_KEYBYTES)
const out = Buffer.alloc(message.length)
const state = Buffer.alloc(sodium.crypto_stream_xor_STATEBYTES)
sodium.crypto_stream_xor_init(state, nonce, key)
const nonceCopy = Buffer.from(nonce.toString('hex'), 'hex')
const keyCopy = Buffer.from(key.toString('hex'), 'hex')
nonce = null
key = null
forceGC()
for (let i = 0; i < message.length; i++) {
sodium.crypto_stream_xor_update(state, out.slice(i, i + 1), message.slice(i, i + 1))
}
sodium.crypto_stream_xor_final(state)
sodium.crypto_stream_xor(out, out, nonceCopy, keyCopy)
t.alike(out, message, 'decrypted')
})
function random (n) {
const buf = Buffer.alloc(n)
sodium.randombytes_buf(buf)
return buf
}
function forceGC () {
require('v8').setFlagsFromString('--expose-gc')
require('vm').runInNewContext('gc')()
}