Fork of TweetNaCl (ed25519), but using blake2b instead of sha512 (like raiblocks)
- Overview
- Audits
- Installation
- Usage
- System requirements
- Development and testing
- Benchmarks
- Contributors
- Who uses it
The primary goal of this project is to produce a translation of TweetNaCl using the blake2b hash algorithm instead of sha512 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 likenacl.js
, but with some functions replaced with faster versions. (Used by default when importing NPM package.)
TweetNaCl.js (parent project) 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. It has to be noted that this is an exceptionally rare result of a source code audit for any project and must be seen as a true testament to a development proceeding with security at its core.
To reiterate, the TweetNaCl-js project, the source code was found to be bug-free at this point.
[...]
In sum, the testing team is happy to recommend the TweetNaCl-js project as likely one of the safer and more secure cryptographic tools among its competition.
You can install TweetNaCl-blake2b.js via a package manager:
Yarn:
$ yarn add tweetnacl-blake2b
NPM:
$ npm install tweetnacl-blake2b
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-blake2b.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: new Buffer(array)
,
instead of sharing: new Buffer(array.buffer)
, 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-blake2b.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-blake2b.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-blake2b.js supports modern browsers that have a cryptographically secure pseudorandom number generator and typed arrays, including the latest versions of:
- Chrome
- Firefox
- Safari (Mac, iOS)
- Internet Explorer 11
Other systems:
- Node.js
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
.
See AUTHORS.md file.