-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
403 lines (317 loc) · 15.4 KB
/
index.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/**
* @providesModule Sodium
* @flow
*/
import { NativeModules } from 'react-native'
const { Sodium } = NativeModules
// console.log(NativeModules)
const constants = Object.fromEntries(Object.entries(Sodium.getConstants()).map(
([k, v]) => {
if (k.slice(0, 42) == '_crypto_secretstream_xchacha20poly1305_TAG') {
return [k.slice(1), new Uint8Array([v])]
}
return [k, v]
}))
const SodiumAPI = {
...constants,
crypto_aead_xchacha20poly1305_ietf_keygen,
crypto_aead_xchacha20poly1305_ietf_encrypt,
crypto_aead_xchacha20poly1305_ietf_decrypt,
crypto_aead_chacha20poly1305_ietf_keygen,
crypto_aead_chacha20poly1305_ietf_encrypt,
crypto_aead_chacha20poly1305_ietf_decrypt,
crypto_core_ed25519_scalar_random,
crypto_core_ed25519_add,
crypto_core_ed25519_sub,
crypto_core_ed25519_from_uniform,
crypto_pwhash,
crypto_pwhash_async,
crypto_scalarmult,
crypto_scalarmult_base,
crypto_scalarmult_ed25519,
crypto_scalarmult_ed25519_noclamp,
crypto_scalarmult_ed25519_base,
crypto_scalarmult_ed25519_base_noclamp,
crypto_generichash_init,
crypto_generichash_update,
crypto_generichash_final,
crypto_generichash_batch,
crypto_generichash,
crypto_kdf_keygen,
crypto_kdf_derive_from_key,
crypto_kx_keypair,
crypto_sign_keypair,
crypto_sign_seed_keypair,
crypto_sign,
crypto_sign_open,
crypto_sign_detached,
crypto_sign_verify_detached,
crypto_sign_ed25519_sk_to_pk,
crypto_stream_xor,
crypto_secretstream_xchacha20poly1305_keygen,
crypto_secretstream_xchacha20poly1305_init_push,
crypto_secretstream_xchacha20poly1305_push,
crypto_secretstream_xchacha20poly1305_init_pull,
crypto_secretstream_xchacha20poly1305_pull,
crypto_secretbox_easy,
randombytes_buf,
sodium_pad,
sodium_unpad,
sodium_memcmp,
sodium_memzero,
sodium_free,
sodium_malloc
}
function crypto_secretbox_easy (...args) {
const res = new Uint8Array(Sodium.crypto_secretbox_easy(...Array.from(args, mapArgs)))
args[0].set(res)
}
function crypto_generichash (out, input, key) {
if (!key) key = new Uint8Array(0)
const state = new Uint8Array(384)
crypto_generichash_init(state, key, out.byteLength)
crypto_generichash_update(state, input)
crypto_generichash_final(state, out)
}
function crypto_generichash_batch (out, batch, key) {
if (!key) key = new Uint8Array(0)
const state = new Uint8Array(384)
crypto_generichash_init(state, key, out.byteLength)
for (let i = 0; i < batch.length; i++) {
crypto_generichash_update(state, batch[i])
}
crypto_generichash_final(state, out)
}
function randombytes_buf (buf) {
buf.set(new Uint8Array(Sodium.randombytes_buf(Array.from(buf))))
}
function crypto_aead_xchacha20poly1305_ietf_keygen (k) {
k.set(new Uint8Array(Sodium.crypto_aead_xchacha20poly1305_ietf_keygen(Array.from(k))))
}
function crypto_aead_xchacha20poly1305_ietf_encrypt (...args) {
const nativeResult = Sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_aead_xchacha20poly1305_ietf_encrypt execeution failed: ' + nativeResult + '.')
const res = new Uint8Array(nativeResult)
args[0].set(res)
return res.byteLength
}
function crypto_aead_xchacha20poly1305_ietf_decrypt (...args) {
const nativeResult = Sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_aead_xchacha20poly1305_ietf_decrypt execeution failed: ' + nativeResult + '.')
const res = new Uint8Array(nativeResult)
args[0].set(res)
return res.byteLength
}
function crypto_aead_chacha20poly1305_ietf_keygen (k) {
k.set(new Uint8Array(Sodium.crypto_aead_chacha20poly1305_ietf_keygen(Array.from(k))))
}
function crypto_aead_chacha20poly1305_ietf_encrypt (...args) {
const nativeResult = Sodium.crypto_aead_chacha20poly1305_ietf_encrypt(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_aead_chacha20poly1305_ietf_encrypt execeution failed: ' + nativeResult + '.')
const res = new Uint8Array(nativeResult)
args[0].set(res)
return res.byteLength
}
function crypto_aead_chacha20poly1305_ietf_decrypt (...args) {
const nativeResult = Sodium.crypto_aead_chacha20poly1305_ietf_decrypt(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_aead_chacha20poly1305_ietf_decrypt execeution failed: ' + nativeResult + '.')
const res = new Uint8Array(nativeResult)
args[0].set(res)
return res.byteLength
}
function crypto_core_ed25519_scalar_random (r) {
r.set(new Uint8Array(Sodium.crypto_core_ed25519_scalar_random(Array.from(r))))
}
function crypto_core_ed25519_add (...args) {
const nativeResult = Sodium.crypto_core_ed25519_add(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_core_ed25519_add execution faile: ' + nativeResult + 'd')
args[0].set(new Uint8Array(nativeResult))
}
function crypto_core_ed25519_sub (...args) {
const nativeResult = Sodium.crypto_core_ed25519_sub(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_core_ed25519_sub execution faile: ' + nativeResult + 'd')
args[0].set(new Uint8Array(nativeResult))
}
function crypto_core_ed25519_from_uniform (...args) {
const nativeResult = Sodium.crypto_core_ed25519_from_uniform(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_core_ed25519_from_uniform execution faile: ' + nativeResult + 'd')
args[0].set(new Uint8Array(nativeResult))
}
function crypto_pwhash (...args) {
const nativeResult = Sodium.crypto_pwhash(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_pwhash execution failed: ' + nativeResult + '.')
args[0].set(new Uint8Array(nativeResult))
}
async function crypto_pwhash_async (...args) {
const nativeResult = await Sodium.crypto_pwhash_async(...Array.from(args, mapArgs))
args[0].set(new Uint8Array(nativeResult))
}
function crypto_scalarmult (...args) {
const nativeResult = Sodium.crypto_scalarmult(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_scalarmult execution failed: ' + nativeResult + '.')
args[0].set(new Uint8Array(nativeResult))
}
function crypto_scalarmult_base (...args) {
const nativeResult = Sodium.crypto_scalarmult_base(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_scalarmult_base execution failed: ' + nativeResult + '.')
args[0].set(new Uint8Array(nativeResult))
}
function crypto_scalarmult_ed25519 (...args) {
const nativeResult = Sodium.crypto_scalarmult_ed25519(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_scalarmult_ed25519 execution failed: ' + nativeResult + '.')
args[0].set(new Uint8Array(nativeResult))
}
function crypto_scalarmult_ed25519_noclamp (...args) {
const nativeResult = Sodium.crypto_scalarmult_ed25519_noclamp(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_scalarmult_ed25519_noclamp execution failed: ' + nativeResult + '.')
args[0].set(new Uint8Array(nativeResult))
}
function crypto_scalarmult_ed25519_base (...args) {
const nativeResult = Sodium.crypto_scalarmult_ed25519_base(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_scalarmult_ed25519_base execution failed: ' + nativeResult + '.')
args[0].set(new Uint8Array(nativeResult))
}
function crypto_scalarmult_ed25519_base_noclamp (...args) {
const nativeResult = Sodium.crypto_scalarmult_ed25519_base_noclamp(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_scalarmult_ed25519_base_noclamp execution failed: ' + nativeResult + '.')
args[0].set(new Uint8Array(nativeResult))
}
function crypto_secretstream_xchacha20poly1305_keygen (...args) {
args[0].set(new Uint8Array(Sodium.crypto_secretstream_xchacha20poly1305_keygen(...Array.from(args, mapArgs))))
}
function crypto_secretstream_xchacha20poly1305_init_push (...args) {
const nativeResult = Sodium.crypto_secretstream_xchacha20poly1305_init_push(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_secretstream_xchacha20poly1305_init_push execution failed: ' + nativeResult + '.')
const resultBuf = new Uint8Array(nativeResult)
args[0].set(resultBuf.subarray(0, constants.crypto_secretstream_xchacha20poly1305_STATEBYTES))
args[1].set(resultBuf.subarray(constants.crypto_secretstream_xchacha20poly1305_STATEBYTES))
}
function crypto_secretstream_xchacha20poly1305_push (...args) {
const nativeResult = Sodium.crypto_secretstream_xchacha20poly1305_push(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_secretstream_xchacha20poly1305_push execution failed: ' + nativeResult + '.')
const resultBuf = new Uint8Array(nativeResult)
args[0].set(resultBuf.subarray(0, constants.crypto_secretstream_xchacha20poly1305_STATEBYTES))
args[1].set(resultBuf.subarray(constants.crypto_secretstream_xchacha20poly1305_STATEBYTES))
}
function crypto_secretstream_xchacha20poly1305_init_pull (...args) {
const nativeResult = Sodium.crypto_secretstream_xchacha20poly1305_init_pull(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_secretstream_xchacha20poly1305_init_pull execution failed: ' + nativeResult + '.')
args[0].set(new Uint8Array(nativeResult))
}
function crypto_secretstream_xchacha20poly1305_pull (...args) {
const nativeResult = Sodium.crypto_secretstream_xchacha20poly1305_pull(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_secretstream_xchacha20poly1305_pull execution failed: ' + nativeResult + '.')
const resultBuf = new Uint8Array(nativeResult)
args[0].set(resultBuf.subarray(0, constants.crypto_secretstream_xchacha20poly1305_STATEBYTES)) // state
args[2][0] = resultBuf[constants.crypto_secretstream_xchacha20poly1305_STATEBYTES] // tag
args[1].set(resultBuf.subarray(constants.crypto_secretstream_xchacha20poly1305_STATEBYTES + 1)) // message
}
function crypto_generichash_init (...args) {
const nativeResult = Sodium.crypto_generichash_init(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_generichash_init execution failed: ' + nativeResult + '.')
args[0].set(new Uint8Array(nativeResult))
}
function crypto_generichash_update (...args) {
const nativeResult = Sodium.crypto_generichash_update(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_generichash_update execution failed: ' + nativeResult + '.')
args[0].set(new Uint8Array(nativeResult))
}
function crypto_generichash_final (...args) {
const nativeResult = Sodium.crypto_generichash_final(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_generichash_final execution failed: ' + nativeResult + '.')
args[1].set(new Uint8Array(nativeResult))
}
function crypto_kdf_keygen (...args) {
args[0].set(new Uint8Array(Sodium.crypto_kdf_keygen(...Array.from(args, mapArgs))))
}
function crypto_kdf_derive_from_key (...args) {
const nativeResult = Sodium.crypto_kdf_derive_from_key(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_kdf_derive_from_key execution failed: ' + nativeResult + '.')
args[0].set(new Uint8Array(nativeResult))
}
function crypto_kx_keypair (...args) {
const nativeResult = Sodium.crypto_kx_keypair(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_kx_keypair execution failed: ' + nativeResult + '.')
const resultBuf = new Uint8Array(nativeResult)
args[0].set(resultBuf.subarray(0, constants.crypto_kx_PUBLICKEYBYTES))
args[1].set(resultBuf.subarray(constants.crypto_kx_PUBLICKEYBYTES))
}
function crypto_sign_keypair (...args) {
const nativeResult = Sodium.crypto_sign_keypair(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_sign_keypair execution failed: ' + nativeResult + '.')
const resultBuf = new Uint8Array(nativeResult)
args[0].set(resultBuf.subarray(0, constants.crypto_kx_PUBLICKEYBYTES))
args[1].set(resultBuf.subarray(constants.crypto_kx_PUBLICKEYBYTES))
}
function crypto_sign_seed_keypair (...args) {
const nativeResult = Sodium.crypto_sign_seed_keypair(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_sign_seed_keypair execution failed: ' + nativeResult + '.')
const resultBuf = new Uint8Array(nativeResult)
args[0].set(resultBuf.subarray(0, constants.crypto_kx_PUBLICKEYBYTES))
args[1].set(resultBuf.subarray(constants.crypto_kx_PUBLICKEYBYTES))
}
function crypto_sign (...args) {
const nativeResult = Sodium.crypto_sign(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_sign execution failed: ' + nativeResult + '.')
args[0].set(new Uint8Array(nativeResult))
}
function crypto_sign_open (...args) {
const nativeResult = Sodium.crypto_sign_open(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_sign_open execution failed: ' + nativeResult + '.')
args[0].set(new Uint8Array(nativeResult))
}
function crypto_sign_detached (...args) {
const nativeResult = Sodium.crypto_sign_detached(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_sign_detached execution failed: ' + nativeResult + '.')
args[0].set(new Uint8Array(nativeResult))
}
function crypto_sign_verify_detached (...args) {
const nativeResult = Sodium.crypto_sign_verify_detached(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_sign_verify_detached execution failed: ' + nativeResult + '.')
return true
}
function crypto_sign_ed25519_sk_to_pk (...args) {
const nativeResult = Sodium.crypto_sign_ed25519_sk_to_pk(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_sign_ed25519_sk_to_pk execution failed: ' + nativeResult + '.')
args[0].set(new Uint8Array(nativeResult))
}
function crypto_stream_xor (...args) {
const nativeResult = Sodium.crypto_stream_xor(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('crypto_stream_xor execution failed: ' + nativeResult + '.')
args[0].set(new Uint8Array(nativeResult))
}
function sodium_pad (...args) {
const nativeResult = Sodium.sodium_pad(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('sodium_pad execution failed: ' + nativeResult + '.')
args[0].set(new Uint8Array(nativeResult))
return nativeResult.length
}
function sodium_unpad (...args) {
const nativeResult = Sodium.sodium_unpad(...Array.from(args, mapArgs))
if (typeof nativeResult === 'string') throw new Error('sodium_unpad execution failed: ' + nativeResult + '.')
return nativeResult
}
function sodium_memcmp (a, b) {
return vn(a, 0, b, 0, a.byteLength) === 0 && a.byteLength === b.byteLength
}
function sodium_malloc (n) {
return new Uint8Array(n)
}
function sodium_free (n) {
sodium_memzero(n)
}
function sodium_memzero (arr) {
arr.fill(0)
}
module.exports = SodiumAPI
function mapArgs (arg) {
if (arg == null) return new Array(0)
if (typeof arg === 'number') return arg
return Array.from(arg)
}
// constant time compare
function vn (x, xi, y, yi, n) {
let d = 0
for (let i = 0; i < n; i++) d |= x[xi + i] ^ y[yi + i]
return (1 & ((d - 1) >>> 8)) - 1
}