/
sodium.js
326 lines (249 loc) · 9.11 KB
/
sodium.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
/**
* # Main module file
*
* @module sodium
* @name node-sodium
* @author Pedro Paixao
* @date 11/9/13
*
*/
/* jslint node: true */
'use strict';
// Base
var binding = require('../build/Release/sodium');
var toBuffer = require('./toBuffer');
// Public Key
var Box = require('./box');
var Sign = require('./sign');
// Symmetric Key
var SecretBox = require('./secretbox');
var Auth = require('./auth');
var OneTimeAuth = require('./onetime-auth');
var Stream = require('./stream');
// Elliptic Curve Diffie-Hellman using Curve25519
var ECDH = require('./ecdh');
// Nonces
var BoxNonce = require('./nonces/box-nonce');
var SecretBoxNonce = require('./nonces/secretbox-nonce');
var StreamNonce = require('./nonces/stream-nonce');
// Keys
var AuthKey = require('./keys/auth-key');
var BoxKey = require('./keys/box-key');
var OneTimeKey = require('./keys/onetime-key');
var SecretBoxKey = require('./keys/secretbox-key');
var SignKey = require('./keys/sign-key');
var StreamKey = require('./keys/stream-key');
var DHKey = require('./keys/dh-key');
/**
* # API
* Export all low level lib sodium functions directly
* for developers that are used to lib sodium C interface
*/
module.exports.api = binding;
/** `libsodium` version */
module.exports.version = binding.version;
module.exports.versionMinor = binding.versionMinor;
module.exports.versionMajor = binding.versionMajor;
/** Utilities */
module.exports.Utils = {
memzero: binding.memzero,
memcmp: binding.memcmp,
verify16: binding.crypto_verify_16,
verify32: binding.crypto_verify_32,
toBuffer: toBuffer,
};
module.exports.Utils.to_hex = function (args) {
var ret = "";
for ( var i = 0; i < args.length; i++ )
ret += (args[i] < 16 ? "0" : "") + args[i].toString(16);
return ret; //.toUpperCase();
};
module.exports.Utils.from_hex = function (str) {
if (typeof str == 'string') {
var ret = new Uint8Array(Math.floor(str.length / 2));
var i = 0;
str.replace(/(..)/g, function(str) { ret[i++] = parseInt(str, 16);});
return ret;
}
};
/** Hash functions */
module.exports.Hash = {
/** Default message hash */
hash: binding.crypto_hash,
/** SHA 256 */
sha256: binding.crypto_hash_sha256,
/** SHA 512 */
sha512: binding.crypto_hash_sha512,
/** Size of hash buffer in bytes */
bytes: binding.crypto_hash_BYTES,
/** Size of hash block */
blockBytes: binding.crypto_hash_BLOCKBYTES,
/** Default primitive */
primitive: binding.crypto_hash_PRIMITIVE
};
/** Random Functions */
module.exports.Random = {
/** Fill buffer with random bytest */
buffer : binding.randombytes_buf,
/** Initialize OS dependent random device */
stir : binding.randombytes_stir,
/** Close the random device */
close : binding.randombytes_close,
/** Return a random 32-bit unsigned value */
rand : binding.randombytes_random,
/** Return a value between 0 and upper_bound using a uniform distribution */
uniform : binding.randombytes_uniform
};
// Public Key
module.exports.Box = Box;
module.exports.Sign = Sign;
// Symmetric Key
module.exports.Auth = Auth;
module.exports.SecretBox = SecretBox;
module.exports.Stream = Stream;
module.exports.OneTimeAuth = OneTimeAuth;
// Nonces
module.exports.Nonces = {
Box: BoxNonce,
SecretBox: SecretBoxNonce,
Stream: StreamNonce
};
// Symmetric Keys
module.exports.Key = {
SecretBox: SecretBoxKey,
Auth: AuthKey,
OneTimeAuth: OneTimeKey,
Stream: StreamKey,
// Public/Secret Key Pairs
Box: BoxKey,
Sign: SignKey,
ECDH: DHKey
};
// Elliptic Curve Diffie-Hellman with Curve25519
module.exports.ECDH = ECDH;
/**
* Lib Sodium Constants
*
* the base library defines several important constant that you should use to
* check the size of buffers, nonces, keys, etc.
*
* All constants represent the size of the buffer or zone of a buffer in bytes
*/
module.exports.Const = {};
/** ScalarMult related constants */
module.exports.Const.ECDH = {
/** Size of scalar buffers */
scalarBytes: binding.crypto_scalarmult_SCALARBYTES,
/** Size of scalar buffers */
bytes: binding.crypto_scalarmult_BYTES,
/** Size of the public and secret keys */
keyBytes: binding.crypto_scalarmult_BYTES,
/** String name of the default crypto primitive used in scalarmult operations */
primitive: binding.crypto_scalarmult_PRIMITIVE
};
/** ScalarMult related constants */
module.exports.Const.ScalarMult = {
/** Size of scalar buffers */
scalarBytes: binding.crypto_scalarmult_SCALARBYTES,
/** Size of the scalarmult keys and points */
bytes: binding.crypto_scalarmult_BYTES,
/** String name of the default crypto primitive used in scalarmult operations */
primitive: binding.crypto_scalarmult_PRIMITIVE
};
/** Hash related constants */
module.exports.Const.Hash = {
/** Size of hash buffer in bytes */
bytes: binding.crypto_hash_BYTES,
/** Size of hash block */
blockBytes: binding.crypto_hash_BLOCKBYTES,
/** Default primitive */
primitive: binding.crypto_hash_PRIMITIVE
};
/** Box related constant sizes in bytes */
module.exports.Const.Box = {
/** Box Nonce buffer size in bytes */
nonceBytes : binding.crypto_box_NONCEBYTES,
/** Box Public Key buffer size in bytes */
publicKeyBytes : binding.crypto_box_PUBLICKEYBYTES,
/** Box Public Key buffer size in bytes */
secretKeyBytes : binding.crypto_box_SECRETKEYBYTES,
/**
* Messages passed to low level API should be padded with zeroBytes at the beginning.
* This implementation automatically pads the message, so no need to do it on your own
*/
zeroBytes : binding.crypto_box_ZEROBYTES,
/**
* Encrypted messages are padded with zeroBoxSize bytes of zeros. If the padding is not
* there the message will not decrypt successfully.
*/
boxZeroBytes : binding.crypto_box_BOXZEROBYTES,
/**
* Padding used in beforenm method. Like zeroBytes this implementation automatically
* pads the message.
*
* @see Const.Box.zeroBytes
*/
beforenmBytes : binding.crypto_box_BEFORENMBYTES,
/** String name of the default crypto primitive used in box operations */
primitive: binding.crypto_box_PRIMITIVE
};
/** Authentication Constants */
module.exports.Const.Auth = {
/** Size of the authentication token */
bytes: binding.crypto_auth_BYTES,
/** Size of the secret key used to generate the authentication token */
keyBytes: binding.crypto_auth_KEYBYTES,
/** String name of the default crypto primitive used in auth operations */
primitive: binding.crypto_auth_PRIMITIVE
};
/** One Time Authentication Constants */
module.exports.Const.OneTimeAuth = {
/** Size of the authentication token */
bytes: binding.crypto_onetimeauth_BYTES,
/** Size of the secret key used to generate the authentication token */
keyBytes: binding.crypto_onetimeauth_KEYBYTES,
/** String name of the default crypto primitive used in onetimeauth operations */
primitive: binding.crypto_onetimeauth_PRIMITIVE
};
/** SecretBox Symmetric Key Crypto Constants */
module.exports.Const.SecretBox = {
/** SecretBox padding of cipher text buffer */
boxZeroBytes: binding.crypto_secretbox_BOXZEROBYTES,
/** Size of the secret key used to encrypt/decrypt messages */
keyBytes: binding.crypto_secretbox_KEYBYTES,
/** Size of the Nonce used in encryption/decryption of messages */
nonceBytes: binding.crypto_secretbox_NONCEBYTES,
/** Passing of message. This implementation does message padding automatically */
zeroBytes: binding.crypto_secretbox_ZEROBYTES,
/** String name of the default crypto primitive used in secretbox operations */
primitive: binding.crypto_secretbox_PRIMITIVE
};
/** Digital message signature constants */
module.exports.Const.Sign = {
/** Size of the generated message signature */
bytes: binding.crypto_sign_BYTES,
/** Size of the public key used to verify signatures */
publicKeyBytes: binding.crypto_sign_PUBLICKEYBYTES,
/** Size of the secret key used to sign a message */
secretKeyBytes: binding.crypto_sign_SECRETKEYBYTES,
/** String name of the default crypto primitive used in sign operations */
primitive: binding.crypto_sign_PRIMITIVE
};
/** Symmetric Encryption Constants */
module.exports.Const.Stream = {
/** Size of secret key used to encrypt/decrypt messages */
keyBytes : binding.crypto_stream_KEYBYTES,
/** Size of nonce used to encrypt/decrypt messages */
nonceBytes : binding.crypto_stream_NONCEBYTES,
/** String name of the default crypto primitive used in stream operations */
primitive: binding.crypto_stream_PRIMITIVE
};
/** Short hash related constants */
module.exports.Const.ShortHash = {
/** Size of short hash buffer in bytes*/
bytes: binding.crypto_shorthash_BYTES,
/** Size of short hash Key buffer in bytes */
keyBytes: binding.crypto_shorthash_KEYBYTES,
/** String name of primitive used to calculate short hash */
primitive: binding.crypto_shorthash_PRIMITIVE
};