Gathering detailed insights and metrics for tweetnacl
Gathering detailed insights and metrics for tweetnacl
Gathering detailed insights and metrics for tweetnacl
Gathering detailed insights and metrics for tweetnacl
Port of TweetNaCl cryptographic library to JavaScript
npm install tweetnacl
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,786 Stars
412 Commits
295 Forks
44 Watching
8 Branches
18 Contributors
Updated on 26 Nov 2024
JavaScript (98.28%)
C (1.51%)
Go (0.13%)
HTML (0.06%)
Makefile (0.02%)
Cumulative downloads
Total Downloads
Last day
-9.4%
4,398,588
Compared to previous day
Last week
3.1%
27,160,369
Compared to previous week
Last month
22.5%
105,021,366
Compared to previous month
Last year
-5.2%
1,015,861,258
Compared to previous year
7
Port of TweetNaCl / NaCl to JavaScript for modern browsers and Node.js. Public domain.
Demo: https://dchest.github.io/tweetnacl-js/
The primary goal of this project is to produce a translation of TweetNaCl to JavaScript which is as close as possible to the original C implementation, plus a thin layer of idiomatic high-level API on top of it.
There are two versions, you can use either of them:
nacl.js
is the port of TweetNaCl with minimum differences from the
original + high-level API.
nacl-fast.js
is like nacl.js
, but with some functions replaced with
faster versions. (Used by default when importing NPM package.)
TweetNaCl.js has been audited by Cure53 in January-February 2017 (audit was sponsored by Deletype):
The overall outcome of this audit signals a particularly positive assessment for TweetNaCl-js, as the testing team was unable to find any security problems in the library.
While the audit didn't find any bugs, there has been 1 bug discovered and fixed after the audit.
It is important to note that TweetNaCl.js is a low-level library that doesn't provide complete security protocols. When designing protocols, you should carefully consider various properties of underlying primitives.
While XSalsa20-Poly1305, as used in nacl.secretbox
and nacl.box
,
meets the standard notions of privacy and authenticity for a secret-key
authenticated-encryption scheme using nonces, it is not key-committing,
which means that it is possible to find a ciphertext which decrypts to
valid plaintexts under two different keys. This may lead to vulnerabilities
if encrypted messages are used in a context where key commitment is expected.
While Ed25519 as originally defined and implemented in nacl.sign
meets the standard notion of unforgeability for a public-key
signature scheme under chosen-message attacks, it is malleable:
given a signed message, it is possible, without knowing the secret key,
to create a different signature for the same message that will verify
under the same public key. This may lead to vulnerabilities if
signatures are used in a context where malleability is not expected.
The SHA-512 hash function, as implemented by nacl.hash
, is not
resistant to length-extension attacks.
While TweetNaCl.js uses algorithmic constant-time operations, it is impossible to guarantee that they are physically constant time given JavaScript runtimes, JIT compilers, and other factors. It is also impossible to guarantee that secret data is physically removed from memory during cleanup due to copying garbage collectors and optimizing compilers.
You can install TweetNaCl.js via a package manager:
Yarn:
$ yarn add tweetnacl
NPM:
$ npm install tweetnacl
You can find usage examples in our wiki.
All API functions accept and return bytes as Uint8Array
s. If you need to
encode or decode strings, use functions from
https://github.com/dchest/tweetnacl-util-js or one of the more robust codec
packages.
In Node.js v4 and later Buffer
objects are backed by Uint8Array
s, so you
can freely pass them to TweetNaCl.js functions as arguments. The returned
objects are still Uint8Array
s, so if you need Buffer
s, you'll have to
convert them manually; make sure to convert using copying: Buffer.from(array)
(or new Buffer(array)
in Node.js v4 or earlier), instead of sharing:
Buffer.from(array.buffer)
(or new Buffer(array.buffer)
Node 4 or earlier),
because some functions return subarrays of their buffers.
Implements x25519-xsalsa20-poly1305.
Generates a new random key pair for box and returns it as an object with
publicKey
and secretKey
members:
{
publicKey: ..., // Uint8Array with 32-byte public key
secretKey: ... // Uint8Array with 32-byte secret key
}
Returns a key pair for box with public key corresponding to the given secret key.
Encrypts and authenticates message using peer's public key, our secret key, and the given nonce, which must be unique for each distinct message for a key pair.
Returns an encrypted and authenticated message, which is
nacl.box.overheadLength
longer than the original message.
Authenticates and decrypts the given box with peer's public key, our secret key, and the given nonce.
Returns the original message, or null
if authentication fails.
Returns a precomputed shared key which can be used in nacl.box.after
and
nacl.box.open.after
.
Same as nacl.box
, but uses a shared key precomputed with nacl.box.before
.
Same as nacl.box.open
, but uses a shared key precomputed with nacl.box.before
.
Length of public key in bytes.
Length of secret key in bytes.
Length of precomputed shared key in bytes.
Length of nonce in bytes.
Length of overhead added to box compared to original message.
Implements xsalsa20-poly1305.
Encrypts and authenticates message using the key and the nonce. The nonce must be unique for each distinct message for this key.
Returns an encrypted and authenticated message, which is
nacl.secretbox.overheadLength
longer than the original message.
Authenticates and decrypts the given secret box using the key and the nonce.
Returns the original message, or null
if authentication fails.
Length of key in bytes.
Length of nonce in bytes.
Length of overhead added to secret box compared to original message.
Implements x25519.
Multiplies an integer n
by a group element p
and returns the resulting
group element.
Multiplies an integer n
by a standard group element and returns the resulting
group element.
Length of scalar in bytes.
Length of group element in bytes.
Implements ed25519.
Generates new random key pair for signing and returns it as an object with
publicKey
and secretKey
members:
{
publicKey: ..., // Uint8Array with 32-byte public key
secretKey: ... // Uint8Array with 64-byte secret key
}
Returns a signing key pair with public key corresponding to the given
64-byte secret key. The secret key must have been generated by
nacl.sign.keyPair
or nacl.sign.keyPair.fromSeed
.
Returns a new signing key pair generated deterministically from a 32-byte seed.
The seed must contain enough entropy to be secure. This method is not
recommended for general use: instead, use nacl.sign.keyPair
to generate a new
key pair from a random seed.
Signs the message using the secret key and returns a signed message.
Verifies the signed message and returns the message without signature.
Returns null
if verification failed.
Signs the message using the secret key and returns a signature.
Verifies the signature for the message and returns true
if verification
succeeded or false
if it failed.
Length of signing public key in bytes.
Length of signing secret key in bytes.
Length of seed for nacl.sign.keyPair.fromSeed
in bytes.
Length of signature in bytes.
Implements SHA-512.
Returns SHA-512 hash of the message.
Length of hash in bytes.
Returns a Uint8Array
of the given length containing random bytes of
cryptographic quality.
Implementation note
TweetNaCl.js uses the following methods to generate random bytes, depending on the platform it runs on:
window.crypto.getRandomValues
(WebCrypto standard)window.msCrypto.getRandomValues
(Internet Explorer 11)crypto.randomBytes
(Node.js)If the platform doesn't provide a suitable PRNG, the following functions, which require random numbers, will throw exception:
nacl.randomBytes
nacl.box.keyPair
nacl.sign.keyPair
Other functions are deterministic and will continue working.
If a platform you are targeting doesn't implement secure random number
generator, but you somehow have a cryptographically-strong source of entropy
(not Math.random
!), and you know what you are doing, you can plug it into
TweetNaCl.js like this:
nacl.setPRNG(function(x, n) {
// ... copy n random bytes into x ...
});
Note that nacl.setPRNG
completely replaces internal random byte generator
with the one provided.
Compares x
and y
in constant time and returns true
if their lengths are
non-zero and equal, and their contents are equal.
Returns false
if either of the arguments has zero length, or arguments have
different lengths, or their contents differ.
TweetNaCl.js supports modern browsers that have a cryptographically secure pseudorandom number generator and typed arrays, including the latest versions of:
Other systems:
Install NPM modules needed for development:
$ npm install
To build minified versions:
$ npm run build
Tests use minified version, so make sure to rebuild it every time you change
nacl.js
or nacl-fast.js
.
To run tests in Node.js:
$ npm run test-node
By default all tests described here work on nacl.min.js
. To test other
versions, set environment variable NACL_SRC
to the file name you want to test.
For example, the following command will test fast minified version:
$ NACL_SRC=nacl-fast.min.js npm run test-node
To run full suite of tests in Node.js, including comparing outputs of JavaScript port to outputs of the original C version:
$ npm run test-node-all
To prepare tests for browsers:
$ npm run build-test-browser
and then open test/browser/test.html
(or test/browser/test-fast.html
) to
run them.
To run tests in both Node and Electron:
$ npm test
To run benchmarks in Node.js:
$ npm run bench
$ NACL_SRC=nacl-fast.min.js npm run bench
To run benchmarks in a browser, open test/benchmark/bench.html
(or
test/benchmark/bench-fast.html
).
For reference, here are benchmarks from MacBook Pro (Retina, 13-inch, Mid 2014) laptop with 2.6 GHz Intel Core i5 CPU (Intel) in Chrome 53/OS X, Xiaomi Redmi Note 3 smartphone with 1.8 GHz Qualcomm Snapdragon 650 64-bit CPU (ARM) in Chrome 52/Android, and MacBook Air 2020 with Apple M1 SOC (M1) in Chromium 102/macOS.
nacl.js Intel | nacl-fast.js Intel | nacl.js ARM | nacl-fast.js ARM | nacl-fast.js M1 | |
---|---|---|---|---|---|
salsa20 | 1.3 MB/s | 128 MB/s | 0.4 MB/s | 43 MB/s | 268 MB/s |
poly1305 | 13 MB/s | 171 MB/s | 4 MB/s | 52 MB/s | 248 MB/s |
hash | 4 MB/s | 34 MB/s | 0.9 MB/s | 12 MB/s | 76 MB/s |
secretbox 1K | 1113 op/s | 57583 op/s | 334 op/s | 14227 op/s | 54546 op/s |
box 1K | 145 op/s | 718 op/s | 37 op/s | 368 op/s | 1836 op/s |
scalarMult | 171 op/s | 733 op/s | 56 op/s | 380 op/s | 1882 op/s |
sign | 77 op/s | 200 op/s | 20 op/s | 61 op/s | 592 op/s |
sign.open | 39 op/s | 102 op/s | 11 op/s | 31 op/s | 300 op/s |
(You can run benchmarks on your devices by clicking on the links at the bottom of the home page).
In short, with nacl-fast.js and 1024-byte messages you can expect to encrypt and authenticate more than 57000 messages per second on a typical laptop or more than 14000 messages per second on a $170 smartphone, sign about 500 and verify 300 messages per second on a laptop or 60 and 30 messages per second on a smartphone, per CPU core (with Web Workers you can do these operations in parallel), which is good enough for most applications.
See AUTHORS.md file.
crypto_auth
sealed boxes
Some notable users of TweetNaCl.js are listed on the associated wiki page.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 6/25 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
24 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More