sodium-javascript/crypto_stream_chacha20.js
Christophe Diederichs b44f83f0a8
Split library into modules (#20)
* crypto_stream: signature change needed to modularise

* move ed25519 arithmetic to separate module

* module: poly1305

* module: crypto_scalarmult

* module: crypto_hash

* module: crypto_sign

* module: crypto_secretbox

* move verify functions to crypto_verify module

* leftover crypto_stream functions

* module: crypto_onetimeauth

* module: crypto_box

* tidy up

* require ed25519.js

* update: crypto_hash

* add chacha20; align API with PR#21

* update sha512 to wasm module

* fix bugs in crypto_sign

* be standard

* add: crypto_box_seed_keypair; alias crypto_kx methods to crypto_box

* scalarmult: import curve methods; be standard

* correction: crypto_kx is not actually an alias of crypto_box

* export _9 constant field element

* add: crypto_box_seed_keypair

* removed duplicate module.exports declaraion

* declare constants about exports

* rename memzero -> sodium-memzero

* update sodium_memzero function to arr.fill(0)

* tidy: remove legacy functions

* added: crypto_aead_chacha20poly1305_ietf methods

* listen to linter

* add assertions

* chacha: readUint32Le generalised for uint8array; aead: standard fix

* add null check on ad param

* added: sodium_memcmp

* export sodium_memcmp

* export crypto_verify module

* sodium_memcmp returns boolean

* added: sodium_is_zero

* catch syntax error

* throw if crypto_aead cannot validate, fix typo in crypto_verify

* move chacha20 alg to external module

* use Uint8Arrays instead of buffers

* change checks to assertions

* bump to chacha 1.0.3 - remove Buffer dependency

* reduce code branching, align return values with sodium-native

* add sha-wasm deps to package.json

* standard fixes

* bump chacha20 to 1.0.4: remove Buffer dep

* move crypto_hash_sha256 to module to uncouple wasm dependencies

* add endian check: all other modules require members of this set

* correct filename: crypto_hash_sha256

* export constant: crypto_hash_sha512_BYTES
2020-06-18 17:09:03 +02:00

85 lines
2.9 KiB
JavaScript

const assert = require('nanoassert')
const Chacha20 = require('chacha20-universal')
if (new Uint16Array([1])[0] !== 1) throw new Error('Big endian architecture is not supported.')
exports.crypto_stream_chacha20_KEYBYTES = 32
exports.crypto_stream_chacha20_NONCEBYTES = 8
exports.crypto_stream_chacha20_MESSAGEBYTES_MAX = Number.MAX_SAFE_INTEGER
exports.crypto_stream_chacha20_ietf_KEYBYTES = 32
exports.crypto_stream_chacha20_ietf_NONCEBYTES = 12
exports.crypto_stream_chacha20_ietf_MESSAGEBYTES_MAX = 2 ** 32
exports.crypto_stream_chacha20 = function (c, n, k) {
c.fill(0)
exports.crypto_stream_chacha20_xor(c, c, n, k)
}
exports.crypto_stream_chacha20_xor = function (c, m, n, k) {
assert(n.byteLength === exports.crypto_stream_chacha20_NONCEBYTES,
'n should be crypto_stream_chacha20_NONCEBYTES')
assert(k.byteLength === exports.crypto_stream_chacha20_KEYBYTES,
'k should be crypto_stream_chacha20_KEYBYTES')
const xor = new Chacha20(n, k)
xor.update(c, m)
xor.final()
}
exports.crypto_stream_chacha20_xor_ic = function (c, m, n, ic, k) {
assert(n.byteLength === exports.crypto_stream_chacha20_NONCEBYTES,
'n should be crypto_stream_chacha20_NONCEBYTES')
assert(k.byteLength === exports.crypto_stream_chacha20_KEYBYTES,
'k should be crypto_stream_chacha20_KEYBYTES')
const xor = new Chacha20(n, k, ic)
xor.update(c, m)
xor.final()
}
exports.crypto_stream_chacha20_xor_instance = function (n, k) {
assert(n.byteLength === exports.crypto_stream_chacha20_NONCEBYTES,
'n should be crypto_stream_chacha20_NONCEBYTES')
assert(k.byteLength === exports.crypto_stream_chacha20_KEYBYTES,
'k should be crypto_stream_chacha20_KEYBYTES')
return new Chacha20(n, k)
}
exports.crypto_stream_chacha20_ietf = function (c, n, k) {
c.fill(0)
exports.crypto_stream_chacha20_ietf_xor(c, c, n, k)
}
exports.crypto_stream_chacha20_ietf_xor = function (c, m, n, k) {
assert(n.byteLength === exports.crypto_stream_chacha20_ietf_NONCEBYTES,
'n should be crypto_stream_chacha20_ietf_NONCEBYTES')
assert(k.byteLength === exports.crypto_stream_chacha20_ietf_KEYBYTES,
'k should be crypto_stream_chacha20_ietf_KEYBYTES')
const xor = new Chacha20(n, k)
xor.update(c, m)
xor.final()
}
exports.crypto_stream_chacha20_ietf_xor_ic = function (c, m, n, ic, k) {
assert(n.byteLength === exports.crypto_stream_chacha20_ietf_NONCEBYTES,
'n should be crypto_stream_chacha20_ietf_NONCEBYTES')
assert(k.byteLength === exports.crypto_stream_chacha20_ietf_KEYBYTES,
'k should be crypto_stream_chacha20_ietf_KEYBYTES')
const xor = new Chacha20(n, k, ic)
xor.update(c, m)
xor.final()
}
exports.crypto_stream_chacha20_ietf_xor_instance = function (n, k) {
assert(n.byteLength === exports.crypto_stream_chacha20_ietf_NONCEBYTES,
'n should be crypto_stream_chacha20_ietf_NONCEBYTES')
assert(k.byteLength === exports.crypto_stream_chacha20_ietf_KEYBYTES,
'k should be crypto_stream_chacha20_ietf_KEYBYTES')
return new Chacha20(n, k)
}