-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextend.js
157 lines (130 loc) · 4.05 KB
/
extend.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
const Heap = require('./lib/heap')
const loadSodiumWasm = require('./lib/load-sodium-wasm')
module.exports = (sodiumJS) => {
const heap = new Heap()
const sodiumWasm = loadSodiumWasm(sodiumJS, heap)
sodiumJS.crypto_aead_chacha20poly1305_ietf_encrypt = function (out, m, ad, nsec, nonce, key) {
const cipherLength = m.length + sodiumJS.crypto_aead_chacha20poly1305_ietf_ABYTES
const _out = heap.alloc(cipherLength)
sodiumWasm.crypto_aead_chacha20poly1305_ietf_encrypt(
_out,
null,
heap.set(m),
m.length,
ad ? heap.set(ad) : null,
ad ? ad.length : null,
nsec ? heap.set(nsec) : null,
heap.set(nonce),
heap.set(key)
)
heap.copy(out, _out, cipherLength)
heap.release()
return cipherLength
}
sodiumJS.crypto_aead_chacha20poly1305_ietf_decrypt = function (out, nsec, c, ad, nonce, key) {
const messageLength = c.length - sodiumJS.crypto_aead_chacha20poly1305_ietf_ABYTES
const _out = heap.alloc(messageLength)
sodiumWasm.crypto_aead_chacha20poly1305_ietf_decrypt(
_out,
null,
nsec ? heap.set(nsec) : null,
heap.set(c),
c.length,
ad ? heap.set(ad) : null,
ad ? ad.length : null,
heap.set(nonce),
heap.set(key)
)
heap.copy(out, _out, messageLength)
heap.release()
return messageLength
}
sodiumJS.crypto_aead_xchacha20poly1305_ietf_encrypt = function (out, m, ad, nsec, nonce, key) {
const cipherLength = m.length + sodiumJS.crypto_aead_xchacha20poly1305_ietf_ABYTES
const _out = heap.alloc(cipherLength)
sodiumWasm.crypto_aead_xchacha20poly1305_ietf_encrypt(
_out,
null,
heap.set(m),
m.length,
ad ? heap.set(ad) : null,
ad ? ad.length : null,
nsec ? heap.set(nsec) : null,
heap.set(nonce),
heap.set(key)
)
heap.copy(out, _out, cipherLength)
heap.release()
return cipherLength
}
sodiumJS.crypto_aead_xchacha20poly1305_ietf_decrypt = function (out, nsec, c, ad, nonce, key) {
const messageLength = c.length - sodiumJS.crypto_aead_xchacha20poly1305_ietf_ABYTES
const _out = heap.alloc(messageLength)
sodiumWasm.crypto_aead_xchacha20poly1305_ietf_decrypt(
_out,
null,
nsec ? heap.set(nsec) : null,
heap.set(c),
c.length,
ad ? heap.set(ad) : null,
ad ? ad.length : null,
heap.set(nonce),
heap.set(key)
)
heap.copy(out, _out, messageLength)
heap.release()
return messageLength
}
sodiumJS.crypto_kx_keypair = function (pk, sk) {
const _pk = heap.alloc(pk.length)
const _sk = heap.alloc(pk.length)
sodiumWasm.crypto_kx_keypair(
_pk,
_sk
)
heap.copy(pk, _pk, pk.length)
heap.copy(sk, _sk, sk.length)
heap.release()
}
sodiumJS.crypto_kx_seed_keypair = function (pk, sk, seed) {
const _pk = heap.alloc(pk.length)
const _sk = heap.alloc(pk.length)
sodiumWasm.crypto_kx_seed_keypair(
_pk,
_sk,
heap.set(seed)
)
heap.copy(pk, _pk, pk.length)
heap.copy(sk, _sk, sk.length)
heap.release()
}
sodiumJS.crypto_kx_client_session_keys = function (clientRx, clientTx, clientPk, clientSk, serverPk) {
const _clientRx = heap.alloc(clientRx.length)
const _clientTx = heap.alloc(clientTx.length)
sodiumWasm.crypto_kx_client_session_keys(
_clientRx,
_clientTx,
heap.set(clientPk),
heap.set(clientSk),
heap.set(serverPk)
)
heap.copy(clientRx, _clientRx, clientRx.length)
heap.copy(clientTx, _clientTx, clientTx.length)
heap.release()
}
sodiumJS.crypto_kx_server_session_keys = function (serverRx, serverTx, serverPk, serverSk, clientPk) {
const _serverRx = heap.alloc(serverRx.length)
const _serverTx = heap.alloc(serverTx.length)
sodiumWasm.crypto_kx_server_session_keys(
_serverRx,
_serverTx,
heap.set(serverPk),
heap.set(serverSk),
heap.set(clientPk)
)
heap.copy(serverRx, _serverRx, serverRx.length)
heap.copy(serverTx, _serverTx, serverTx.length)
heap.release()
}
return sodiumJS
}