ts-tweetnacl 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,527 @@
1
+ TweetNaCl.js
2
+ ============
3
+
4
+ Port of [TweetNaCl](http://tweetnacl.cr.yp.to) / [NaCl](http://nacl.cr.yp.to/)
5
+ to JavaScript for modern browsers and Node.js. Public domain.
6
+
7
+ Demo: <https://dchest.github.io/tweetnacl-js/>
8
+
9
+ Documentation
10
+ =============
11
+
12
+ * [Overview](#overview)
13
+ * [Audits](#audits)
14
+ * [Security Considerations](#security-considerations)
15
+ * [Installation](#installation)
16
+ * [Examples](#examples)
17
+ * [Usage](#usage)
18
+ * [Public-key authenticated encryption (box)](#public-key-authenticated-encryption-box)
19
+ * [Secret-key authenticated encryption (secretbox)](#secret-key-authenticated-encryption-secretbox)
20
+ * [Scalar multiplication](#scalar-multiplication)
21
+ * [Signatures](#signatures)
22
+ * [Hashing](#hashing)
23
+ * [Random bytes generation](#random-bytes-generation)
24
+ * [Constant-time comparison](#constant-time-comparison)
25
+ * [System requirements](#system-requirements)
26
+ * [Development and testing](#development-and-testing)
27
+ * [Benchmarks](#benchmarks)
28
+ * [Contributors](#contributors)
29
+ * [Who uses it](#who-uses-it)
30
+
31
+
32
+ Overview
33
+ --------
34
+
35
+ The primary goal of this project is to produce a translation of TweetNaCl to
36
+ JavaScript which is as close as possible to the original C implementation, plus
37
+ a thin layer of idiomatic high-level API on top of it.
38
+
39
+ There are two versions, you can use either of them:
40
+
41
+ * `nacl.js` is the port of TweetNaCl with minimum differences from the
42
+ original + high-level API.
43
+
44
+ * `nacl-fast.js` is like `nacl.js`, but with some functions replaced with
45
+ faster versions. (Used by default when importing NPM package.)
46
+
47
+
48
+ WebCrypto API
49
+ -------------
50
+
51
+ Note that [WebCrypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) supports most of the primitives implemented by TweetNaCl.js (`X25519` in `nacl.scalarMult`, `Ed25519` in `nacl.sign.detached`, and `SHA-512` in `nacl.hash`), except for `XSalsa20-Poly1305` authenticated encryption, so you should use it if possible.
52
+
53
+ Audits
54
+ ------
55
+
56
+ TweetNaCl.js has been audited by [Cure53](https://cure53.de/) in January-February
57
+ 2017 (audit was sponsored by [Deletype](https://deletype.com)):
58
+
59
+ > The overall outcome of this audit signals a particularly positive assessment
60
+ > for TweetNaCl-js, as the testing team was unable to find any security
61
+ > problems in the library.
62
+
63
+ [Read full audit report](https://cure53.de/tweetnacl.pdf)
64
+
65
+ While the audit didn't find any bugs, there has been [1 bug](https://github.com/dchest/tweetnacl-js/issues/187) discovered and fixed after the audit.
66
+
67
+
68
+ Security Considerations
69
+ -----------------------
70
+
71
+ It is important to note that TweetNaCl.js is a low-level library
72
+ that doesn't provide complete security protocols. When designing
73
+ protocols, you should carefully consider various properties of
74
+ underlying primitives.
75
+
76
+ ### No secret key commitment
77
+
78
+ While XSalsa20-Poly1305, as used in `nacl.secretbox` and `nacl.box`,
79
+ meets the standard notions of privacy and authenticity for a secret-key
80
+ authenticated-encryption scheme using nonces, it is *not key-committing*,
81
+ which means that it is possible to find a ciphertext which decrypts to
82
+ valid plaintexts under two different keys. This may lead to vulnerabilities
83
+ if encrypted messages are used in a context where key commitment is expected.
84
+
85
+ ### Signature malleability
86
+
87
+ While Ed25519 as originally defined and implemented in `nacl.sign`
88
+ meets the standard notion of unforgeability for a public-key
89
+ signature scheme under chosen-message attacks, it is *malleable*:
90
+ given a signed message, it is possible, without knowing the secret key,
91
+ to create a different signature for the same message that will verify
92
+ under the same public key. This may lead to vulnerabilities if
93
+ signatures are used in a context where malleability is not expected.
94
+
95
+ ### Hash length-extension attacks
96
+
97
+ The SHA-512 hash function, as implemented by `nacl.hash`, is *not
98
+ resistant* to length-extension attacks.
99
+
100
+ ### Side-channel attacks
101
+
102
+ While TweetNaCl.js uses algorithmic constant-time operations,
103
+ it is impossible to guarantee that they are physically constant time
104
+ given JavaScript runtimes, JIT compilers, and other factors.
105
+ It is also impossible to guarantee that secret data is physically
106
+ removed from memory during cleanup due to copying garbage
107
+ collectors and optimizing compilers.
108
+
109
+
110
+ Installation
111
+ ------------
112
+
113
+ You can install TweetNaCl.js via a package manager:
114
+
115
+ [Yarn](https://yarnpkg.com/):
116
+
117
+ $ yarn add tweetnacl
118
+
119
+ [NPM](https://www.npmjs.org/):
120
+
121
+ $ npm install tweetnacl
122
+
123
+ or [download source code](https://github.com/dchest/tweetnacl-js/releases).
124
+
125
+
126
+ Examples
127
+ --------
128
+ You can find usage examples in our [wiki](https://github.com/dchest/tweetnacl-js/wiki/Examples).
129
+
130
+
131
+ Usage
132
+ -----
133
+
134
+ All API functions accept and return bytes as `Uint8Array`s. If you need to
135
+ encode or decode strings, use functions from
136
+ <https://github.com/dchest/tweetnacl-util-js> or one of the more robust codec
137
+ packages.
138
+
139
+ In Node.js v4 and later `Buffer` objects are backed by `Uint8Array`s, so you
140
+ can freely pass them to TweetNaCl.js functions as arguments. The returned
141
+ objects are still `Uint8Array`s, so if you need `Buffer`s, you'll have to
142
+ convert them manually; make sure to convert using copying: `Buffer.from(array)`
143
+ (or `new Buffer(array)` in Node.js v4 or earlier), instead of sharing:
144
+ `Buffer.from(array.buffer)` (or `new Buffer(array.buffer)` Node 4 or earlier),
145
+ because some functions return subarrays of their buffers.
146
+
147
+
148
+ ### Public-key authenticated encryption (box)
149
+
150
+ Implements *x25519-xsalsa20-poly1305*.
151
+
152
+ #### nacl.box.keyPair()
153
+
154
+ Generates a new random key pair for box and returns it as an object with
155
+ `publicKey` and `secretKey` members:
156
+
157
+ {
158
+ publicKey: ..., // Uint8Array with 32-byte public key
159
+ secretKey: ... // Uint8Array with 32-byte secret key
160
+ }
161
+
162
+
163
+ #### nacl.box.keyPair.fromSecretKey(secretKey)
164
+
165
+ Returns a key pair for box with public key corresponding to the given secret
166
+ key.
167
+
168
+ #### nacl.box(message, nonce, theirPublicKey, mySecretKey)
169
+
170
+ Encrypts and authenticates message using peer's public key, our secret key, and
171
+ the given nonce, which must be unique for each distinct message for a key pair.
172
+
173
+ Returns an encrypted and authenticated message, which is
174
+ `nacl.box.overheadLength` longer than the original message.
175
+
176
+ #### nacl.box.open(box, nonce, theirPublicKey, mySecretKey)
177
+
178
+ Authenticates and decrypts the given box with peer's public key, our secret
179
+ key, and the given nonce.
180
+
181
+ Returns the original message, or `null` if authentication fails.
182
+
183
+ #### nacl.box.before(theirPublicKey, mySecretKey)
184
+
185
+ Returns a precomputed shared key which can be used in `nacl.box.after` and
186
+ `nacl.box.open.after`.
187
+
188
+ #### nacl.box.after(message, nonce, sharedKey)
189
+
190
+ Same as `nacl.box`, but uses a shared key precomputed with `nacl.box.before`.
191
+
192
+ #### nacl.box.open.after(box, nonce, sharedKey)
193
+
194
+ Same as `nacl.box.open`, but uses a shared key precomputed with `nacl.box.before`.
195
+
196
+ #### Constants
197
+
198
+ ##### nacl.box.publicKeyLength = 32
199
+
200
+ Length of public key in bytes.
201
+
202
+ ##### nacl.box.secretKeyLength = 32
203
+
204
+ Length of secret key in bytes.
205
+
206
+ ##### nacl.box.sharedKeyLength = 32
207
+
208
+ Length of precomputed shared key in bytes.
209
+
210
+ ##### nacl.box.nonceLength = 24
211
+
212
+ Length of nonce in bytes.
213
+
214
+ ##### nacl.box.overheadLength = 16
215
+
216
+ Length of overhead added to box compared to original message.
217
+
218
+
219
+ ### Secret-key authenticated encryption (secretbox)
220
+
221
+ Implements *xsalsa20-poly1305*.
222
+
223
+ #### nacl.secretbox(message, nonce, key)
224
+
225
+ Encrypts and authenticates message using the key and the nonce. The nonce must
226
+ be unique for each distinct message for this key.
227
+
228
+ Returns an encrypted and authenticated message, which is
229
+ `nacl.secretbox.overheadLength` longer than the original message.
230
+
231
+ #### nacl.secretbox.open(box, nonce, key)
232
+
233
+ Authenticates and decrypts the given secret box using the key and the nonce.
234
+
235
+ Returns the original message, or `null` if authentication fails.
236
+
237
+ #### Constants
238
+
239
+ ##### nacl.secretbox.keyLength = 32
240
+
241
+ Length of key in bytes.
242
+
243
+ ##### nacl.secretbox.nonceLength = 24
244
+
245
+ Length of nonce in bytes.
246
+
247
+ ##### nacl.secretbox.overheadLength = 16
248
+
249
+ Length of overhead added to secret box compared to original message.
250
+
251
+
252
+ ### Scalar multiplication
253
+
254
+ Implements *x25519*.
255
+
256
+ #### nacl.scalarMult(n, p)
257
+
258
+ Multiplies an integer `n` by a group element `p` and returns the resulting
259
+ group element.
260
+
261
+ #### nacl.scalarMult.base(n)
262
+
263
+ Multiplies an integer `n` by a standard group element and returns the resulting
264
+ group element.
265
+
266
+ #### Constants
267
+
268
+ ##### nacl.scalarMult.scalarLength = 32
269
+
270
+ Length of scalar in bytes.
271
+
272
+ ##### nacl.scalarMult.groupElementLength = 32
273
+
274
+ Length of group element in bytes.
275
+
276
+
277
+ ### Signatures
278
+
279
+ Implements [ed25519](http://ed25519.cr.yp.to).
280
+
281
+ #### nacl.sign.keyPair()
282
+
283
+ Generates new random key pair for signing and returns it as an object with
284
+ `publicKey` and `secretKey` members:
285
+
286
+ {
287
+ publicKey: ..., // Uint8Array with 32-byte public key
288
+ secretKey: ... // Uint8Array with 64-byte secret key
289
+ }
290
+
291
+ #### nacl.sign.keyPair.fromSecretKey(secretKey)
292
+
293
+ Returns a signing key pair with public key corresponding to the given
294
+ 64-byte secret key. The secret key must have been generated by
295
+ `nacl.sign.keyPair` or `nacl.sign.keyPair.fromSeed`.
296
+
297
+ #### nacl.sign.keyPair.fromSeed(seed)
298
+
299
+ Returns a new signing key pair generated deterministically from a 32-byte seed.
300
+ The seed must contain enough entropy to be secure. This method is not
301
+ recommended for general use: instead, use `nacl.sign.keyPair` to generate a new
302
+ key pair from a random seed.
303
+
304
+ #### nacl.sign(message, secretKey)
305
+
306
+ Signs the message using the secret key and returns a signed message.
307
+
308
+ #### nacl.sign.open(signedMessage, publicKey)
309
+
310
+ Verifies the signed message and returns the message without signature.
311
+
312
+ Returns `null` if verification failed.
313
+
314
+ #### nacl.sign.detached(message, secretKey)
315
+
316
+ Signs the message using the secret key and returns a signature.
317
+
318
+ #### nacl.sign.detached.verify(message, signature, publicKey)
319
+
320
+ Verifies the signature for the message and returns `true` if verification
321
+ succeeded or `false` if it failed.
322
+
323
+ #### Constants
324
+
325
+ ##### nacl.sign.publicKeyLength = 32
326
+
327
+ Length of signing public key in bytes.
328
+
329
+ ##### nacl.sign.secretKeyLength = 64
330
+
331
+ Length of signing secret key in bytes.
332
+
333
+ ##### nacl.sign.seedLength = 32
334
+
335
+ Length of seed for `nacl.sign.keyPair.fromSeed` in bytes.
336
+
337
+ ##### nacl.sign.signatureLength = 64
338
+
339
+ Length of signature in bytes.
340
+
341
+
342
+ ### Hashing
343
+
344
+ Implements *SHA-512*.
345
+
346
+ #### nacl.hash(message)
347
+
348
+ Returns SHA-512 hash of the message.
349
+
350
+ #### Constants
351
+
352
+ ##### nacl.hash.hashLength = 64
353
+
354
+ Length of hash in bytes.
355
+
356
+
357
+ ### Random bytes generation
358
+
359
+ #### nacl.randomBytes(length)
360
+
361
+ Returns a `Uint8Array` of the given length containing random bytes of
362
+ cryptographic quality.
363
+
364
+ **Implementation note**
365
+
366
+ TweetNaCl.js uses the following methods to generate random bytes,
367
+ depending on the platform it runs on:
368
+
369
+ * `window.crypto.getRandomValues` (WebCrypto standard)
370
+ * `window.msCrypto.getRandomValues` (Internet Explorer 11)
371
+ * `crypto.randomBytes` (Node.js)
372
+
373
+ If the platform doesn't provide a suitable PRNG, the following functions,
374
+ which require random numbers, will throw exception:
375
+
376
+ * `nacl.randomBytes`
377
+ * `nacl.box.keyPair`
378
+ * `nacl.sign.keyPair`
379
+
380
+ Other functions are deterministic and will continue working.
381
+
382
+ If a platform you are targeting doesn't implement secure random number
383
+ generator, but you somehow have a cryptographically-strong source of entropy
384
+ (not `Math.random`!), and you know what you are doing, you can plug it into
385
+ TweetNaCl.js like this:
386
+
387
+ nacl.setPRNG(function(x, n) {
388
+ // ... copy n random bytes into x ...
389
+ });
390
+
391
+ Note that `nacl.setPRNG` *completely replaces* internal random byte generator
392
+ with the one provided.
393
+
394
+
395
+ ### Constant-time comparison
396
+
397
+ #### nacl.verify(x, y)
398
+
399
+ Compares `x` and `y` in constant time and returns `true` if their lengths are
400
+ non-zero and equal, and their contents are equal.
401
+
402
+ Returns `false` if either of the arguments has zero length, or arguments have
403
+ different lengths, or their contents differ.
404
+
405
+
406
+ System requirements
407
+ -------------------
408
+
409
+ TweetNaCl.js supports modern browsers that have a cryptographically secure
410
+ pseudorandom number generator and typed arrays, including the latest versions
411
+ of:
412
+
413
+ * Chrome
414
+ * Firefox
415
+ * Safari (Mac, iOS)
416
+ * Internet Explorer 11
417
+
418
+ Other systems:
419
+
420
+ * Node.js
421
+
422
+
423
+ Development and testing
424
+ ------------------------
425
+
426
+ Install NPM modules needed for development:
427
+
428
+ $ npm install
429
+
430
+ To build minified versions:
431
+
432
+ $ npm run build
433
+
434
+ Tests use minified version, so make sure to rebuild it every time you change
435
+ `nacl.js` or `nacl-fast.js`.
436
+
437
+ ### Testing
438
+
439
+ To run tests in Node.js:
440
+
441
+ $ npm run test-node
442
+
443
+ By default all tests described here work on `nacl.min.js`. To test other
444
+ versions, set environment variable `NACL_SRC` to the file name you want to test.
445
+ For example, the following command will test fast minified version:
446
+
447
+ $ NACL_SRC=nacl-fast.min.js npm run test-node
448
+
449
+ To run full suite of tests in Node.js, including comparing outputs of
450
+ JavaScript port to outputs of the original C version:
451
+
452
+ $ npm run test-node-all
453
+
454
+ To prepare tests for browsers:
455
+
456
+ $ npm run build-test-browser
457
+
458
+ and then open `test/browser/test.html` (or `test/browser/test-fast.html`) to
459
+ run them.
460
+
461
+ To run tests in both Node and Electron:
462
+
463
+ $ npm test
464
+
465
+ ### Benchmarking
466
+
467
+ To run benchmarks in Node.js:
468
+
469
+ $ npm run bench
470
+ $ NACL_SRC=nacl-fast.min.js npm run bench
471
+
472
+ To run benchmarks in a browser, open `test/benchmark/bench.html` (or
473
+ `test/benchmark/bench-fast.html`).
474
+
475
+
476
+ Benchmarks
477
+ ----------
478
+
479
+ For reference, here are benchmarks from MacBook Pro (Retina, 13-inch, Mid 2014)
480
+ laptop with 2.6 GHz Intel Core i5 CPU (Intel) in Chrome 53/OS X, Xiaomi Redmi
481
+ Note 3 smartphone with 1.8 GHz Qualcomm Snapdragon 650 64-bit CPU (ARM) in
482
+ Chrome 52/Android, and MacBook Air 2020 with Apple M1 SOC (M1) in Chromium 102/macOS.
483
+
484
+ | | nacl.js Intel | nacl-fast.js Intel | nacl.js ARM | nacl-fast.js ARM | nacl-fast.js M1 |
485
+ | ------------- |:-------------:|:-------------------:|:-------------:|:-----------------:|:-----------------:|
486
+ | salsa20 | 1.3 MB/s | 128 MB/s | 0.4 MB/s | 43 MB/s | 268 MB/s |
487
+ | poly1305 | 13 MB/s | 171 MB/s | 4 MB/s | 52 MB/s | 248 MB/s |
488
+ | hash | 4 MB/s | 34 MB/s | 0.9 MB/s | 12 MB/s | 76 MB/s |
489
+ | secretbox 1K | 1113 op/s | 57583 op/s | 334 op/s | 14227 op/s | 54546 op/s |
490
+ | box 1K | 145 op/s | 718 op/s | 37 op/s | 368 op/s | 1836 op/s |
491
+ | scalarMult | 171 op/s | 733 op/s | 56 op/s | 380 op/s | 1882 op/s |
492
+ | sign | 77 op/s | 200 op/s | 20 op/s | 61 op/s | 592 op/s |
493
+ | sign.open | 39 op/s | 102 op/s | 11 op/s | 31 op/s | 300 op/s |
494
+
495
+ (You can run benchmarks on your devices by clicking on the links at the bottom
496
+ of the [home page](https://tweetnacl.js.org)).
497
+
498
+ In short, with *nacl-fast.js* and 1024-byte messages you can expect to encrypt and
499
+ authenticate more than 57000 messages per second on a typical laptop or more than
500
+ 14000 messages per second on a $170 smartphone, sign about 500 and verify 300
501
+ messages per second on a laptop or 60 and 30 messages per second on a smartphone,
502
+ per CPU core (with Web Workers you can do these operations in parallel),
503
+ which is good enough for most applications.
504
+
505
+
506
+ Contributors
507
+ ------------
508
+
509
+ See AUTHORS.md file.
510
+
511
+
512
+ Third-party libraries based on TweetNaCl.js
513
+ -------------------------------------------
514
+
515
+ * [chloride](https://github.com/dominictarr/chloride) - unified API for various NaCl modules
516
+ * [forward-secrecy](https://github.com/alax/forward-secrecy) — Axolotl ratchet implementation
517
+ * [nacl-stream](https://github.com/dchest/nacl-stream-js) - streaming encryption
518
+ * [ristretto255-js](https://github.com/calibra/ristretto255-js) — implementation of the [ristretto255 group](https://ristretto.group/)
519
+ * [tweetnacl-auth-js](https://github.com/dchest/tweetnacl-auth-js) — implementation of [`crypto_auth`](http://nacl.cr.yp.to/auth.html)
520
+ * [tweetnacl-js-sealed-box](https://github.com/TogaTech/tweetnacl-js-sealed-box) — fork that adds [`sealed boxes`](https://download.libsodium.org/doc/public-key_cryptography/sealed_boxes.html)
521
+ * [ed2curve](https://github.com/dchest/ed2curve-js) — convert Ed25519 signing key pair to X25519 boxes key pair
522
+
523
+
524
+ Who uses it
525
+ -----------
526
+
527
+ Some notable users of TweetNaCl.js are listed on the [associated wiki page](https://github.com/dchest/tweetnacl-js/wiki/Who-uses-TweetNaCl.js).