Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ae2df3305e | ||
|
|
6c584231e6 | ||
|
|
426ca77d47 | ||
|
|
25c97802f4 | ||
|
|
87ff2a56bd | ||
|
|
7ef6e52870 | ||
|
|
0227f45c6b | ||
|
|
ca32540960 | ||
|
|
51093efbc5 | ||
|
|
f79aed4eee | ||
|
|
3eb1d64460 | ||
|
|
7febc97986 | ||
|
|
66494ec3b5 | ||
|
|
35f38a6c10 | ||
|
|
125384f26a | ||
|
|
d432fe295d | ||
|
|
97200f90da | ||
|
|
656d6d251e | ||
|
|
61b6e6916a | ||
|
|
ab004d8022 |
@@ -1 +1,2 @@
|
||||
node_modules
|
||||
package-lock.json
|
||||
|
||||
@@ -33,7 +33,7 @@ console.log('Plaintext:', plainText.toString())
|
||||
## API
|
||||
|
||||
See [sodium-native](https://github.com/sodium-friends/sodium-native).
|
||||
This is a work in progress so all functions are not implemented yet.
|
||||
This is a work in progress so not all functions are implemented yet.
|
||||
|
||||
This module is organised into individual submodules which can be required
|
||||
independently for smaller bundles in the browser. To leverage automatic
|
||||
|
||||
+2
-2
@@ -30,7 +30,7 @@ function crypto_aead_chacha20poly1305_ietf_encrypt (c, m, ad, nsec, npub, k) {
|
||||
}
|
||||
|
||||
function crypto_aead_chacha20poly1305_ietf_encrypt_detached (c, mac, m, ad, nsec, npub, k) {
|
||||
if (ad === null) return crypto_aead_chacha20poly1305_ietf_encrypt(c, mac, m, new Uint8Array(0), nsec, npub, k)
|
||||
if (ad === null) return crypto_aead_chacha20poly1305_ietf_encrypt_detached(c, mac, m, new Uint8Array(0), nsec, npub, k)
|
||||
|
||||
assert(c.byteLength === m.byteLength, 'ciphertext should be same length than message')
|
||||
assert(npub.byteLength === crypto_aead_chacha20poly1305_ietf_NPUBBYTES,
|
||||
@@ -91,7 +91,7 @@ function crypto_aead_chacha20poly1305_ietf_decrypt (m, nsec, c, ad, npub, k) {
|
||||
}
|
||||
|
||||
function crypto_aead_chacha20poly1305_ietf_decrypt_detached (m, nsec, c, mac, ad, npub, k) {
|
||||
if (ad === null) return crypto_aead_chacha20poly1305_ietf_decrypt(m, nsec, c, mac, new Uint8Array(0), npub, k)
|
||||
if (ad === null) return crypto_aead_chacha20poly1305_ietf_decrypt_detached(m, nsec, c, mac, new Uint8Array(0), npub, k)
|
||||
|
||||
assert(c.byteLength === m.byteLength, 'message should be same length than ciphertext')
|
||||
assert(npub.byteLength === crypto_aead_chacha20poly1305_ietf_NPUBBYTES,
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
/* eslint-disable camelcase */
|
||||
const { crypto_verify_32 } = require('./crypto_verify')
|
||||
const Sha512 = require('sha512-universal')
|
||||
const assert = require('nanoassert')
|
||||
|
||||
const crypto_auth_BYTES = 32
|
||||
const crypto_auth_KEYBYTES = 32
|
||||
|
||||
function crypto_auth (out, input, k) {
|
||||
assert(out.byteLength === crypto_auth_BYTES, "out should be 'crypto_auth_BYTES' in length")
|
||||
assert(k.byteLength === crypto_auth_KEYBYTES, "key should be 'crypto_auth_KEYBYTES' in length")
|
||||
|
||||
const out0 = new Uint8Array(64)
|
||||
const hmac = Sha512.HMAC(k)
|
||||
hmac.update(input)
|
||||
hmac.digest(out0)
|
||||
|
||||
out.set(out0.subarray(0, 32))
|
||||
}
|
||||
|
||||
function crypto_auth_verify (h, input, k) {
|
||||
assert(h.byteLength === crypto_auth_BYTES, "h should be 'crypto_auth_BYTES' in length")
|
||||
assert(k.byteLength === crypto_auth_KEYBYTES, "key should be 'crypto_auth_KEYBYTES' in length")
|
||||
|
||||
const correct = Sha512.HMAC(k).update(input).digest()
|
||||
|
||||
return crypto_verify_32(h, 0, correct, 0)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
crypto_auth_BYTES,
|
||||
crypto_auth_KEYBYTES,
|
||||
crypto_auth,
|
||||
crypto_auth_verify
|
||||
}
|
||||
+99
-3
@@ -3,7 +3,13 @@ const { crypto_hash_sha512 } = require('./crypto_hash')
|
||||
const { crypto_scalarmult, crypto_scalarmult_base } = require('./crypto_scalarmult')
|
||||
const { randombytes } = require('./randombytes')
|
||||
const { crypto_generichash_batch } = require('./crypto_generichash')
|
||||
const { crypto_secretbox_open_easy, crypto_secretbox_easy } = require('./crypto_secretbox')
|
||||
const { crypto_stream_xsalsa20_MESSAGEBYTES_MAX } = require('./crypto_stream')
|
||||
const {
|
||||
crypto_secretbox_open_easy,
|
||||
crypto_secretbox_easy,
|
||||
crypto_secretbox_detached,
|
||||
crypto_secretbox_open_detached
|
||||
} = require('./crypto_secretbox')
|
||||
const xsalsa20 = require('xsalsa20')
|
||||
const assert = require('nanoassert')
|
||||
|
||||
@@ -15,8 +21,17 @@ const crypto_box_BOXZEROBYTES = 16
|
||||
const crypto_box_SEALBYTES = 48
|
||||
const crypto_box_SEEDBYTES = 32
|
||||
const crypto_box_BEFORENMBYTES = 32
|
||||
const crypto_box_MACBYTES = 16
|
||||
|
||||
const crypto_box_curve25519xsalsa20poly1305_MACBYTES = 16
|
||||
|
||||
const crypto_box_MESSAGEBYTES_MAX =
|
||||
crypto_stream_xsalsa20_MESSAGEBYTES_MAX -
|
||||
crypto_box_curve25519xsalsa20poly1305_MACBYTES
|
||||
|
||||
module.exports = {
|
||||
crypto_box_easy,
|
||||
crypto_box_open_easy,
|
||||
crypto_box_keypair,
|
||||
crypto_box_seed_keypair,
|
||||
crypto_box_seal,
|
||||
@@ -28,7 +43,8 @@ module.exports = {
|
||||
crypto_box_BOXZEROBYTES,
|
||||
crypto_box_SEALBYTES,
|
||||
crypto_box_SEEDBYTES,
|
||||
crypto_box_BEFORENMBYTES
|
||||
crypto_box_BEFORENMBYTES,
|
||||
crypto_box_MACBYTES
|
||||
}
|
||||
|
||||
function crypto_box_keypair (pk, sk) {
|
||||
@@ -37,7 +53,6 @@ function crypto_box_keypair (pk, sk) {
|
||||
randombytes(sk, 32)
|
||||
return crypto_scalarmult_base(pk, sk)
|
||||
}
|
||||
|
||||
function crypto_box_seed_keypair (pk, sk, seed) {
|
||||
assert(pk.byteLength === crypto_box_PUBLICKEYBYTES, "pk should be 'crypto_box_PUBLICKEYBYTES' bytes")
|
||||
assert(sk.byteLength === crypto_box_SECRETKEYBYTES, "sk should be 'crypto_box_SECRETKEYBYTES' bytes")
|
||||
@@ -95,6 +110,87 @@ function crypto_box_seal_open (m, c, pk, sk) {
|
||||
return crypto_secretbox_open_easy(m, c.subarray(epk.length), n, k)
|
||||
}
|
||||
|
||||
function crypto_box_beforenm (k, pk, sk) {
|
||||
const zero = new Uint8Array(16)
|
||||
const s = new Uint8Array(32)
|
||||
|
||||
assert(crypto_scalarmult(s, sk, pk) === 0)
|
||||
|
||||
xsalsa20.core_hsalsa20(k, zero, s, xsalsa20.SIGMA)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
function crypto_box_detached_afternm (c, mac, m, n, k) {
|
||||
return crypto_secretbox_detached(c, mac, m, n, k)
|
||||
}
|
||||
|
||||
function crypto_box_detached (c, mac, m, n, pk, sk) {
|
||||
check(mac, crypto_box_MACBYTES)
|
||||
check(n, crypto_box_NONCEBYTES)
|
||||
check(pk, crypto_box_PUBLICKEYBYTES)
|
||||
check(sk, crypto_box_SECRETKEYBYTES)
|
||||
|
||||
const k = new Uint8Array(crypto_box_BEFORENMBYTES)
|
||||
|
||||
assert(crypto_box_beforenm(k, pk, sk))
|
||||
|
||||
const ret = crypto_box_detached_afternm(c, mac, m, n, k)
|
||||
cleanup(k)
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
function crypto_box_easy (c, m, n, pk, sk) {
|
||||
assert(
|
||||
c.length >= m.length + crypto_box_MACBYTES,
|
||||
"c should be at least 'm.length + crypto_box_MACBYTES' bytes"
|
||||
)
|
||||
assert(
|
||||
m.length <= crypto_box_MESSAGEBYTES_MAX,
|
||||
"m should be at most 'crypto_box_MESSAGEBYTES_MAX' bytes"
|
||||
)
|
||||
|
||||
return crypto_box_detached(
|
||||
c.subarray(crypto_box_MACBYTES, m.length + crypto_box_MACBYTES),
|
||||
c.subarray(0, crypto_box_MACBYTES),
|
||||
m,
|
||||
n,
|
||||
pk,
|
||||
sk
|
||||
)
|
||||
}
|
||||
|
||||
function crypto_box_open_detached_afternm (m, c, mac, n, k) {
|
||||
return crypto_secretbox_open_detached(m, c, mac, n, k)
|
||||
}
|
||||
|
||||
function crypto_box_open_detached (m, c, mac, n, pk, sk) {
|
||||
const k = new Uint8Array(crypto_box_BEFORENMBYTES)
|
||||
assert(crypto_box_beforenm(k, pk, sk))
|
||||
|
||||
const ret = crypto_box_open_detached_afternm(m, c, mac, n, k)
|
||||
cleanup(k)
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
function crypto_box_open_easy (m, c, n, pk, sk) {
|
||||
assert(
|
||||
c.length >= m.length + crypto_box_MACBYTES,
|
||||
"c should be at least 'm.length + crypto_box_MACBYTES' bytes"
|
||||
)
|
||||
|
||||
return crypto_box_open_detached(
|
||||
m,
|
||||
c.subarray(crypto_box_MACBYTES, m.length + crypto_box_MACBYTES),
|
||||
c.subarray(0, crypto_box_MACBYTES),
|
||||
n,
|
||||
pk,
|
||||
sk
|
||||
)
|
||||
}
|
||||
|
||||
function check (buf, len) {
|
||||
if (!buf || (len && buf.length < len)) throw new Error('Argument must be a buffer' + (len ? ' of length ' + len : ''))
|
||||
}
|
||||
|
||||
+4
-4
@@ -68,8 +68,8 @@ function crypto_secretbox_detached (o, mac, msg, n, k) {
|
||||
|
||||
const tmp = new Uint8Array(msg.byteLength + mac.byteLength)
|
||||
crypto_secretbox_easy(tmp, msg, n, k)
|
||||
o.set(tmp.subarray(0, msg.byteLength))
|
||||
mac.set(tmp.subarray(msg.byteLength))
|
||||
mac.set(tmp.subarray(0, mac.byteLength))
|
||||
o.set(tmp.subarray(mac.byteLength))
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -80,8 +80,8 @@ function crypto_secretbox_open_detached (msg, o, mac, n, k) {
|
||||
assert(k.byteLength === crypto_secretbox_KEYBYTES, "k must be 'crypto_secretbox_KEYBYTES' bytes")
|
||||
|
||||
const tmp = new Uint8Array(o.byteLength + mac.byteLength)
|
||||
tmp.set(o)
|
||||
tmp.set(mac, msg.byteLength)
|
||||
tmp.set(mac)
|
||||
tmp.set(o, mac.byteLength)
|
||||
return crypto_secretbox_open_easy(msg, tmp, n, k)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,271 @@
|
||||
/* eslint-disable camelcase */
|
||||
const assert = require('nanoassert')
|
||||
const { randombytes_buf } = require('./randombytes')
|
||||
const {
|
||||
crypto_stream_chacha20_ietf,
|
||||
crypto_stream_chacha20_ietf_xor,
|
||||
crypto_stream_chacha20_ietf_xor_ic,
|
||||
crypto_stream_chacha20_ietf_KEYBYTES
|
||||
} = require('./crypto_stream_chacha20')
|
||||
const { crypto_core_hchacha20, crypto_core_hchacha20_INPUTBYTES } = require('./internal/hchacha20')
|
||||
const Poly1305 = require('./internal/poly1305')
|
||||
const { sodium_increment, sodium_is_zero, sodium_memcmp } = require('./helpers')
|
||||
|
||||
const crypto_onetimeauth_poly1305_BYTES = 16
|
||||
const crypto_secretstream_xchacha20poly1305_COUNTERBYTES = 4
|
||||
const crypto_secretstream_xchacha20poly1305_INONCEBYTES = 8
|
||||
const crypto_aead_xchacha20poly1305_ietf_KEYBYTES = 32
|
||||
const crypto_secretstream_xchacha20poly1305_KEYBYTES = crypto_aead_xchacha20poly1305_ietf_KEYBYTES
|
||||
const crypto_aead_xchacha20poly1305_ietf_NPUBBYTES = 24
|
||||
const crypto_secretstream_xchacha20poly1305_HEADERBYTES = crypto_aead_xchacha20poly1305_ietf_NPUBBYTES
|
||||
const crypto_aead_xchacha20poly1305_ietf_ABYTES = 16
|
||||
const crypto_secretstream_xchacha20poly1305_ABYTES = 1 + crypto_aead_xchacha20poly1305_ietf_ABYTES
|
||||
const crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX = Number.MAX_SAFE_INTEGER
|
||||
const crypto_aead_chacha20poly1305_ietf_MESSAGEBYTES_MAX = Number.MAX_SAFE_INTEGER
|
||||
const crypto_secretstream_xchacha20poly1305_TAGBYTES = 1
|
||||
const crypto_secretstream_xchacha20poly1305_TAG_MESSAGE = new Uint8Array([0])
|
||||
const crypto_secretstream_xchacha20poly1305_TAG_PUSH = new Uint8Array([1])
|
||||
const crypto_secretstream_xchacha20poly1305_TAG_REKEY = new Uint8Array([2])
|
||||
const crypto_secretstream_xchacha20poly1305_TAG_FINAL = new Uint8Array([crypto_secretstream_xchacha20poly1305_TAG_PUSH | crypto_secretstream_xchacha20poly1305_TAG_REKEY])
|
||||
const crypto_secretstream_xchacha20poly1305_STATEBYTES = crypto_secretstream_xchacha20poly1305_KEYBYTES +
|
||||
crypto_secretstream_xchacha20poly1305_INONCEBYTES + crypto_secretstream_xchacha20poly1305_COUNTERBYTES + 8
|
||||
|
||||
const KEY_OFFSET = 0
|
||||
const NONCE_OFFSET = crypto_secretstream_xchacha20poly1305_KEYBYTES
|
||||
const PAD_OFFSET = NONCE_OFFSET + crypto_secretstream_xchacha20poly1305_INONCEBYTES + crypto_secretstream_xchacha20poly1305_COUNTERBYTES
|
||||
|
||||
const _pad0 = new Uint8Array(16)
|
||||
|
||||
function STORE64_LE (dest, int) {
|
||||
let mul = 1
|
||||
let i = 0
|
||||
dest[0] = int & 0xFF
|
||||
while (++i < 8 && (mul *= 0x100)) {
|
||||
dest[i] = (int / mul) & 0xFF
|
||||
}
|
||||
}
|
||||
|
||||
function crypto_secretstream_xchacha20poly1305_counter_reset (state) {
|
||||
assert(state.byteLength === crypto_secretstream_xchacha20poly1305_STATEBYTES,
|
||||
'state is should be crypto_secretstream_xchacha20poly1305_STATEBYTES long')
|
||||
|
||||
const nonce = state.subarray(NONCE_OFFSET, PAD_OFFSET)
|
||||
for (let i = 0; i < crypto_secretstream_xchacha20poly1305_COUNTERBYTES; i++) {
|
||||
nonce[i] = 0
|
||||
}
|
||||
nonce[0] = 1
|
||||
}
|
||||
|
||||
function crypto_secretstream_xchacha20poly1305_keygen (k) {
|
||||
assert(k.length === crypto_secretstream_xchacha20poly1305_KEYBYTES)
|
||||
randombytes_buf(k)
|
||||
}
|
||||
|
||||
function crypto_secretstream_xchacha20poly1305_init_push (state, out, key) {
|
||||
assert(state.byteLength === crypto_secretstream_xchacha20poly1305_STATEBYTES,
|
||||
'state is should be crypto_secretstream_xchacha20poly1305_STATEBYTES long')
|
||||
assert(out instanceof Uint8Array && out.length === crypto_secretstream_xchacha20poly1305_HEADERBYTES, 'out not byte array of length crypto_secretstream_xchacha20poly1305_HEADERBYTES')
|
||||
assert(key instanceof Uint8Array && key.length === crypto_secretstream_xchacha20poly1305_KEYBYTES, 'key not byte array of length crypto_secretstream_xchacha20poly1305_KEYBYTES')
|
||||
|
||||
const k = state.subarray(KEY_OFFSET, NONCE_OFFSET)
|
||||
const nonce = state.subarray(NONCE_OFFSET, PAD_OFFSET)
|
||||
const pad = state.subarray(PAD_OFFSET)
|
||||
|
||||
randombytes_buf(out, crypto_secretstream_xchacha20poly1305_HEADERBYTES)
|
||||
crypto_core_hchacha20(k, out, key, null)
|
||||
crypto_secretstream_xchacha20poly1305_counter_reset(state)
|
||||
for (let i = 0; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) {
|
||||
nonce[i + crypto_secretstream_xchacha20poly1305_COUNTERBYTES] = out[i + crypto_core_hchacha20_INPUTBYTES]
|
||||
}
|
||||
pad.fill(0)
|
||||
}
|
||||
|
||||
function crypto_secretstream_xchacha20poly1305_init_pull (state, _in, key) {
|
||||
assert(state.byteLength === crypto_secretstream_xchacha20poly1305_STATEBYTES,
|
||||
'state is should be crypto_secretstream_xchacha20poly1305_STATEBYTES long')
|
||||
assert(_in instanceof Uint8Array && _in.length === crypto_secretstream_xchacha20poly1305_HEADERBYTES,
|
||||
'_in not byte array of length crypto_secretstream_xchacha20poly1305_HEADERBYTES')
|
||||
assert(key instanceof Uint8Array && key.length === crypto_secretstream_xchacha20poly1305_KEYBYTES,
|
||||
'key not byte array of length crypto_secretstream_xchacha20poly1305_KEYBYTES')
|
||||
|
||||
const k = state.subarray(KEY_OFFSET, NONCE_OFFSET)
|
||||
const nonce = state.subarray(NONCE_OFFSET, PAD_OFFSET)
|
||||
const pad = state.subarray(PAD_OFFSET)
|
||||
|
||||
crypto_core_hchacha20(k, _in, key, null)
|
||||
crypto_secretstream_xchacha20poly1305_counter_reset(state)
|
||||
|
||||
for (let i = 0; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) {
|
||||
nonce[i + crypto_secretstream_xchacha20poly1305_COUNTERBYTES] = _in[i + crypto_core_hchacha20_INPUTBYTES]
|
||||
}
|
||||
pad.fill(0)
|
||||
}
|
||||
|
||||
function crypto_secretstream_xchacha20poly1305_rekey (state) {
|
||||
assert(state.byteLength === crypto_secretstream_xchacha20poly1305_STATEBYTES,
|
||||
'state is should be crypto_secretstream_xchacha20poly1305_STATEBYTES long')
|
||||
|
||||
const k = state.subarray(KEY_OFFSET, NONCE_OFFSET)
|
||||
const nonce = state.subarray(NONCE_OFFSET, PAD_OFFSET)
|
||||
|
||||
const new_key_and_inonce = new Uint8Array(
|
||||
crypto_stream_chacha20_ietf_KEYBYTES + crypto_secretstream_xchacha20poly1305_INONCEBYTES)
|
||||
let i
|
||||
for (i = 0; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) {
|
||||
new_key_and_inonce[i] = k[i]
|
||||
}
|
||||
for (i = 0; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) {
|
||||
new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i] =
|
||||
nonce[crypto_secretstream_xchacha20poly1305_COUNTERBYTES + i]
|
||||
}
|
||||
crypto_stream_chacha20_ietf_xor(new_key_and_inonce, new_key_and_inonce, nonce, k)
|
||||
for (i = 0; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) {
|
||||
k[i] = new_key_and_inonce[i]
|
||||
}
|
||||
for (i = 0; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) {
|
||||
nonce[crypto_secretstream_xchacha20poly1305_COUNTERBYTES + i] =
|
||||
new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i]
|
||||
}
|
||||
crypto_secretstream_xchacha20poly1305_counter_reset(state)
|
||||
}
|
||||
|
||||
function crypto_secretstream_xchacha20poly1305_push (state, out, m, ad, tag) {
|
||||
assert(state.byteLength === crypto_secretstream_xchacha20poly1305_STATEBYTES,
|
||||
'state is should be crypto_secretstream_xchacha20poly1305_STATEBYTES long')
|
||||
if (!ad) ad = new Uint8Array(0)
|
||||
|
||||
const k = state.subarray(KEY_OFFSET, NONCE_OFFSET)
|
||||
const nonce = state.subarray(NONCE_OFFSET, PAD_OFFSET)
|
||||
|
||||
const block = new Uint8Array(64)
|
||||
const slen = new Uint8Array(8)
|
||||
|
||||
assert(crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX <=
|
||||
crypto_aead_chacha20poly1305_ietf_MESSAGEBYTES_MAX)
|
||||
|
||||
crypto_stream_chacha20_ietf(block, nonce, k)
|
||||
const poly = new Poly1305(block)
|
||||
block.fill(0)
|
||||
|
||||
poly.update(ad, 0, ad.byteLength)
|
||||
poly.update(_pad0, 0, (0x10 - ad.byteLength) & 0xf)
|
||||
|
||||
block[0] = tag[0]
|
||||
crypto_stream_chacha20_ietf_xor_ic(block, block, nonce, 1, k)
|
||||
|
||||
poly.update(block, 0, block.byteLength)
|
||||
out[0] = block[0]
|
||||
|
||||
const c = out.subarray(1, out.byteLength)
|
||||
crypto_stream_chacha20_ietf_xor_ic(c, m, nonce, 2, k)
|
||||
poly.update(c, 0, m.byteLength)
|
||||
poly.update(_pad0, 0, (0x10 - block.byteLength + m.byteLength) & 0xf)
|
||||
|
||||
STORE64_LE(slen, ad.byteLength)
|
||||
poly.update(slen, 0, slen.byteLength)
|
||||
STORE64_LE(slen, block.byteLength + m.byteLength)
|
||||
poly.update(slen, 0, slen.byteLength)
|
||||
|
||||
const mac = out.subarray(1 + m.byteLength, out.byteLength)
|
||||
poly.finish(mac, 0)
|
||||
|
||||
assert(crypto_onetimeauth_poly1305_BYTES >=
|
||||
crypto_secretstream_xchacha20poly1305_INONCEBYTES)
|
||||
xor_buf(nonce.subarray(crypto_secretstream_xchacha20poly1305_COUNTERBYTES, nonce.length),
|
||||
mac, crypto_secretstream_xchacha20poly1305_INONCEBYTES)
|
||||
sodium_increment(nonce)
|
||||
|
||||
if ((tag[0] & crypto_secretstream_xchacha20poly1305_TAG_REKEY) !== 0 ||
|
||||
sodium_is_zero(nonce.subarray(0, crypto_secretstream_xchacha20poly1305_COUNTERBYTES))) {
|
||||
crypto_secretstream_xchacha20poly1305_rekey(state)
|
||||
}
|
||||
|
||||
return crypto_secretstream_xchacha20poly1305_ABYTES + m.byteLength
|
||||
}
|
||||
|
||||
function crypto_secretstream_xchacha20poly1305_pull (state, m, tag, _in, ad) {
|
||||
assert(state.byteLength === crypto_secretstream_xchacha20poly1305_STATEBYTES,
|
||||
'state is should be crypto_secretstream_xchacha20poly1305_STATEBYTES long')
|
||||
if (!ad) ad = new Uint8Array(0)
|
||||
|
||||
const k = state.subarray(KEY_OFFSET, NONCE_OFFSET)
|
||||
const nonce = state.subarray(NONCE_OFFSET, PAD_OFFSET)
|
||||
|
||||
const block = new Uint8Array(64)
|
||||
const slen = new Uint8Array(8)
|
||||
const mac = new Uint8Array(crypto_onetimeauth_poly1305_BYTES)
|
||||
|
||||
assert(_in.byteLength >= crypto_secretstream_xchacha20poly1305_ABYTES,
|
||||
'ciphertext is too short.')
|
||||
|
||||
const mlen = _in.byteLength - crypto_secretstream_xchacha20poly1305_ABYTES
|
||||
crypto_stream_chacha20_ietf(block, nonce, k)
|
||||
const poly = new Poly1305(block)
|
||||
block.fill(0) // sodium_memzero(block, sizeof block);
|
||||
|
||||
poly.update(ad, 0, ad.byteLength)
|
||||
poly.update(_pad0, 0, (0x10 - ad.byteLength) & 0xf)
|
||||
|
||||
block.fill(0) // memset(block, 0, sizeof block);
|
||||
block[0] = _in[0]
|
||||
crypto_stream_chacha20_ietf_xor_ic(block, block, nonce, 1, k)
|
||||
|
||||
tag[0] = block[0]
|
||||
block[0] = _in[0]
|
||||
poly.update(block, 0, block.byteLength)
|
||||
|
||||
const c = _in.subarray(1, _in.length)
|
||||
poly.update(c, 0, mlen)
|
||||
|
||||
poly.update(_pad0, 0, (0x10 - block.byteLength + mlen) & 0xf)
|
||||
|
||||
STORE64_LE(slen, ad.byteLength)
|
||||
poly.update(slen, 0, slen.byteLength)
|
||||
STORE64_LE(slen, block.byteLength + m.byteLength)
|
||||
poly.update(slen, 0, slen.byteLength)
|
||||
|
||||
poly.finish(mac, 0)
|
||||
const stored_mac = _in.subarray(1 + mlen, _in.length)
|
||||
|
||||
if (!sodium_memcmp(mac, stored_mac)) {
|
||||
mac.fill(0)
|
||||
throw new Error('MAC could not be verified.')
|
||||
}
|
||||
|
||||
crypto_stream_chacha20_ietf_xor_ic(m, c.subarray(0, m.length), nonce, 2, k)
|
||||
xor_buf(nonce.subarray(crypto_secretstream_xchacha20poly1305_COUNTERBYTES, nonce.length),
|
||||
mac, crypto_secretstream_xchacha20poly1305_INONCEBYTES)
|
||||
sodium_increment(nonce)
|
||||
|
||||
if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) !== 0 ||
|
||||
sodium_is_zero(nonce.subarray(0, crypto_secretstream_xchacha20poly1305_COUNTERBYTES))) {
|
||||
crypto_secretstream_xchacha20poly1305_rekey(state)
|
||||
}
|
||||
|
||||
return mlen
|
||||
}
|
||||
|
||||
function xor_buf (out, _in, n) {
|
||||
for (let i = 0; i < n; i++) {
|
||||
out[i] ^= _in[i]
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
crypto_secretstream_xchacha20poly1305_keygen,
|
||||
crypto_secretstream_xchacha20poly1305_init_push,
|
||||
crypto_secretstream_xchacha20poly1305_init_pull,
|
||||
crypto_secretstream_xchacha20poly1305_rekey,
|
||||
crypto_secretstream_xchacha20poly1305_push,
|
||||
crypto_secretstream_xchacha20poly1305_pull,
|
||||
crypto_secretstream_xchacha20poly1305_STATEBYTES,
|
||||
crypto_secretstream_xchacha20poly1305_ABYTES,
|
||||
crypto_secretstream_xchacha20poly1305_HEADERBYTES,
|
||||
crypto_secretstream_xchacha20poly1305_KEYBYTES,
|
||||
crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX,
|
||||
crypto_secretstream_xchacha20poly1305_TAGBYTES,
|
||||
crypto_secretstream_xchacha20poly1305_TAG_MESSAGE,
|
||||
crypto_secretstream_xchacha20poly1305_TAG_PUSH,
|
||||
crypto_secretstream_xchacha20poly1305_TAG_REKEY,
|
||||
crypto_secretstream_xchacha20poly1305_TAG_FINAL
|
||||
}
|
||||
+159
-12
@@ -8,11 +8,19 @@ const {
|
||||
inv25519, unpack25519
|
||||
} = require('./internal/ed25519')
|
||||
const { randombytes } = require('./randombytes')
|
||||
const { crypto_scalarmult_BYTES } = require('./crypto_scalarmult.js')
|
||||
const { crypto_hash_sha512_BYTES } = require('./crypto_hash.js')
|
||||
const assert = require('nanoassert')
|
||||
|
||||
const crypto_sign_BYTES = 64
|
||||
const crypto_sign_PUBLICKEYBYTES = 32
|
||||
const crypto_sign_SECRETKEYBYTES = 64
|
||||
const crypto_sign_SEEDBYTES = 32
|
||||
const crypto_sign_ed25519_PUBLICKEYBYTES = 32
|
||||
const crypto_sign_ed25519_SECRETKEYBYTES = 64
|
||||
const crypto_sign_ed25519_SEEDBYTES = 32
|
||||
const crypto_sign_ed25519_BYTES = 64
|
||||
|
||||
const crypto_sign_BYTES = crypto_sign_ed25519_BYTES
|
||||
const crypto_sign_PUBLICKEYBYTES = crypto_sign_ed25519_PUBLICKEYBYTES
|
||||
const crypto_sign_SECRETKEYBYTES = crypto_sign_ed25519_SECRETKEYBYTES
|
||||
const crypto_sign_SEEDBYTES = crypto_sign_ed25519_SEEDBYTES
|
||||
|
||||
module.exports = {
|
||||
crypto_sign_keypair,
|
||||
@@ -24,7 +32,16 @@ module.exports = {
|
||||
crypto_sign_BYTES,
|
||||
crypto_sign_PUBLICKEYBYTES,
|
||||
crypto_sign_SECRETKEYBYTES,
|
||||
crypto_sign_SEEDBYTES
|
||||
crypto_sign_SEEDBYTES,
|
||||
crypto_sign_ed25519_PUBLICKEYBYTES,
|
||||
crypto_sign_ed25519_SECRETKEYBYTES,
|
||||
crypto_sign_ed25519_SEEDBYTES,
|
||||
crypto_sign_ed25519_BYTES,
|
||||
crypto_sign_ed25519_pk_to_curve25519,
|
||||
crypto_sign_ed25519_sk_to_curve25519,
|
||||
crypto_sign_ed25519_sk_to_pk,
|
||||
unpackneg,
|
||||
pack
|
||||
}
|
||||
|
||||
function set25519 (r, a) {
|
||||
@@ -85,6 +102,8 @@ function pack (r, p) {
|
||||
}
|
||||
|
||||
function scalarmult (p, q, s) {
|
||||
// don't mutate q
|
||||
var h = [gf(q[0]), gf(q[1]), gf(q[2]), gf(q[3])]
|
||||
var b, i
|
||||
set25519(p[0], gf0)
|
||||
set25519(p[1], gf1)
|
||||
@@ -92,10 +111,10 @@ function scalarmult (p, q, s) {
|
||||
set25519(p[3], gf0)
|
||||
for (i = 255; i >= 0; --i) {
|
||||
b = (s[(i / 8) | 0] >> (i & 7)) & 1
|
||||
cswap(p, q, b)
|
||||
add(q, p)
|
||||
cswap(p, h, b)
|
||||
add(h, p)
|
||||
add(p, p)
|
||||
cswap(p, q, b)
|
||||
cswap(p, h, b)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,7 +145,6 @@ function crypto_sign_keypair (pk, sk, seeded) {
|
||||
pack(pk, p)
|
||||
|
||||
for (i = 0; i < 32; i++) sk[i + 32] = pk[i]
|
||||
return 0
|
||||
}
|
||||
|
||||
function crypto_sign_seed_keypair (pk, sk, seed) {
|
||||
@@ -248,7 +266,9 @@ function unpackneg (r, p) {
|
||||
M(chk, chk, den)
|
||||
if (!neq25519(chk, num)) return false
|
||||
|
||||
if (par25519(r[0]) === (p[31] >> 7)) Z(r[0], gf0, r[0])
|
||||
if (par25519(r[0]) === (p[31] >> 7)) {
|
||||
Z(r[0], gf(), r[0])
|
||||
}
|
||||
|
||||
M(r[3], r[0], r[1])
|
||||
return true
|
||||
@@ -317,6 +337,133 @@ function neq25519 (a, b) {
|
||||
return crypto_verify_32(c, 0, d, 0)
|
||||
}
|
||||
|
||||
function check (buf, len) {
|
||||
if (!buf || (len && buf.length < len)) throw new Error('Argument must be a buffer' + (len ? ' of length ' + len : ''))
|
||||
function ed25519_mul_l (p, q) {
|
||||
scalarmult(p, q, L)
|
||||
}
|
||||
|
||||
function ed25519_is_on_main_subgroup (p) {
|
||||
var pl = [gf(), gf(), gf(), gf()]
|
||||
|
||||
ed25519_mul_l(pl, p)
|
||||
|
||||
var zero = 0
|
||||
for (let i = 0; i < 16; i++) {
|
||||
zero |= (pl[0][i] & 0xffff)
|
||||
}
|
||||
|
||||
return zero === 0
|
||||
}
|
||||
|
||||
function crypto_sign_ed25519_pk_to_curve25519 (x25519_pk, ed25519_pk) {
|
||||
check(x25519_pk, crypto_sign_PUBLICKEYBYTES)
|
||||
check(ed25519_pk, crypto_sign_ed25519_PUBLICKEYBYTES)
|
||||
|
||||
var a = [gf(), gf(), gf(), gf()]
|
||||
var x = gf([1])
|
||||
var one_minus_y = gf([1])
|
||||
|
||||
assert(
|
||||
isSmallOrder(ed25519_pk) &&
|
||||
unpackneg(a, ed25519_pk) &&
|
||||
ed25519_is_on_main_subgroup(a), 'Cannot convert key: bad point')
|
||||
|
||||
for (let i = 0; i < a.length; i++) {
|
||||
pack25519(x25519_pk, a[i])
|
||||
}
|
||||
|
||||
Z(one_minus_y, one_minus_y, a[1])
|
||||
A(x, x, a[1])
|
||||
inv25519(one_minus_y, one_minus_y)
|
||||
M(x, x, one_minus_y)
|
||||
pack25519(x25519_pk, x)
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
function isSmallOrder (s) {
|
||||
Uint8Array.from([])
|
||||
|
||||
var bad_points = [
|
||||
// 0 (order 4)
|
||||
Uint8Array.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
|
||||
|
||||
// 1 (order 1)
|
||||
Uint8Array.from([0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
|
||||
|
||||
// 2707385501144840649318225287225658788936804267575313519463743609750303402022(order 8)
|
||||
Uint8Array.from([0x26, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0, 0x45, 0xc3,
|
||||
0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0, 0xd5, 0xdf, 0xac, 0x05, 0xd3,
|
||||
0xc6, 0x33, 0x39, 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x05]),
|
||||
|
||||
// 55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8)
|
||||
Uint8Array.from([0xc7, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f, 0xba, 0x3c,
|
||||
0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f, 0x2a, 0x20, 0x53, 0xfa, 0x2c,
|
||||
0x39, 0xcc, 0xc6, 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0x7a]),
|
||||
|
||||
// p-1 (order 2)
|
||||
Uint8Array.from([0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]),
|
||||
|
||||
// p (=0 order 4)
|
||||
Uint8Array.from([0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]),
|
||||
|
||||
// p + 1 (=1 order 1)
|
||||
Uint8Array.from([0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f])
|
||||
]
|
||||
|
||||
var c = new Uint8Array(7)
|
||||
var j
|
||||
|
||||
check(bad_points, 7)
|
||||
for (let i = 0; i < bad_points.length; i++) {
|
||||
for (j = 0; j < 31; j++) {
|
||||
c[i] |= s[j] ^ bad_points[i][j]
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < bad_points.length; i++) {
|
||||
c[i] |= (s[j] & 0x7f) ^ bad_points[i][j]
|
||||
}
|
||||
|
||||
var k = 0
|
||||
for (let i = 0; i < bad_points.length; i++) {
|
||||
k |= (c[i] - 1)
|
||||
}
|
||||
|
||||
return ((k >> 8) & 1) === 0
|
||||
}
|
||||
|
||||
function crypto_sign_ed25519_sk_to_pk (pk, sk) {
|
||||
check(pk, crypto_sign_ed25519_PUBLICKEYBYTES)
|
||||
pk.set(sk.subarray(crypto_sign_ed25519_SEEDBYTES))
|
||||
return pk
|
||||
}
|
||||
|
||||
function crypto_sign_ed25519_sk_to_curve25519 (curveSk, edSk) {
|
||||
assert(curveSk && curveSk.byteLength === crypto_scalarmult_BYTES, "curveSk must be 'crypto_sign_SECRETKEYBYTES' long")
|
||||
assert(edSk && edSk.byteLength === crypto_sign_ed25519_SECRETKEYBYTES, "edSk must be 'crypto_sign_ed25519_SECRETKEYBYTES' long")
|
||||
|
||||
var h = new Uint8Array(crypto_hash_sha512_BYTES)
|
||||
crypto_hash(h, edSk, 32)
|
||||
|
||||
h[0] &= 248
|
||||
h[31] &= 127
|
||||
h[31] |= 64
|
||||
|
||||
curveSk.set(h.subarray(0, crypto_scalarmult_BYTES))
|
||||
h.fill(0)
|
||||
return curveSk
|
||||
}
|
||||
|
||||
function check (buf, len, arg = 'Argument') {
|
||||
if (!buf || (len && buf.length < len)) throw new Error(arg + ' must be a buffer' + (len ? ' of length ' + len : ''))
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ if (new Uint16Array([1])[0] !== 1) throw new Error('Big endian architecture is n
|
||||
exports.crypto_stream_KEYBYTES = 32
|
||||
exports.crypto_stream_NONCEBYTES = 24
|
||||
exports.crypto_stream_PRIMITIVE = 'xsalsa20'
|
||||
exports.crypto_stream_xsalsa20_MESSAGEBYTES_MAX = Number.MAX_SAFE_INTEGER
|
||||
|
||||
exports.crypto_stream = function (c, nonce, key) {
|
||||
c.fill(0)
|
||||
|
||||
@@ -12,6 +12,7 @@ forward(require('./randombytes'))
|
||||
forward(require('./memory'))
|
||||
forward(require('./helpers'))
|
||||
forward(require('./crypto_verify'))
|
||||
forward(require('./crypto_auth'))
|
||||
forward(require('./crypto_box'))
|
||||
forward(require('./crypto_generichash'))
|
||||
forward(require('./crypto_hash'))
|
||||
@@ -22,6 +23,7 @@ forward(require('./crypto_aead'))
|
||||
forward(require('./crypto_onetimeauth'))
|
||||
forward(require('./crypto_scalarmult'))
|
||||
forward(require('./crypto_secretbox'))
|
||||
forward(require('./crypto_secretstream'))
|
||||
forward(require('./crypto_shorthash'))
|
||||
forward(require('./crypto_sign'))
|
||||
forward(require('./crypto_stream'))
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
/* eslint-disable camelcase */
|
||||
const { sodium_malloc } = require('../memory')
|
||||
const assert = require('nanoassert')
|
||||
|
||||
if (new Uint16Array([1])[0] !== 1) throw new Error('Big endian architecture is not supported.')
|
||||
|
||||
const crypto_core_hchacha20_OUTPUTBYTES = 32
|
||||
const crypto_core_hchacha20_INPUTBYTES = 16
|
||||
const crypto_core_hchacha20_KEYBYTES = 32
|
||||
const crypto_core_hchacha20_CONSTBYTES = 16
|
||||
|
||||
function ROTL32 (x, b) {
|
||||
x &= 0xFFFFFFFF
|
||||
b &= 0xFFFFFFFF
|
||||
return (x << b) | (x >>> (32 - b))
|
||||
}
|
||||
|
||||
function LOAD32_LE (src, offset) {
|
||||
assert(src instanceof Uint8Array, 'src not byte array')
|
||||
let w = src[offset]
|
||||
w |= src[offset + 1] << 8
|
||||
w |= src[offset + 2] << 16
|
||||
w |= src[offset + 3] << 24
|
||||
return w
|
||||
}
|
||||
|
||||
function STORE32_LE (dest, int, offset) {
|
||||
assert(dest instanceof Uint8Array, 'dest not byte array')
|
||||
var mul = 1
|
||||
var i = 0
|
||||
dest[offset] = int & 0xFF // grab bottom byte
|
||||
while (++i < 4 && (mul *= 0x100)) {
|
||||
dest[offset + i] = (int / mul) & 0xFF
|
||||
}
|
||||
}
|
||||
|
||||
function QUARTERROUND (l, A, B, C, D) {
|
||||
l[A] += l[B]
|
||||
l[D] = ROTL32(l[D] ^ l[A], 16)
|
||||
l[C] += l[D]
|
||||
l[B] = ROTL32(l[B] ^ l[C], 12)
|
||||
l[A] += l[B]
|
||||
l[D] = ROTL32(l[D] ^ l[A], 8)
|
||||
l[C] += l[D]
|
||||
l[B] = ROTL32(l[B] ^ l[C], 7)
|
||||
}
|
||||
|
||||
function crypto_core_hchacha20 (out, _in, k, c) {
|
||||
assert(out instanceof Uint8Array && out.length === 32, 'out is not an array of 32 bytes')
|
||||
assert(k instanceof Uint8Array && k.length === 32, 'k is not an array of 32 bytes')
|
||||
assert(c === null || (c instanceof Uint8Array && c.length === 16), 'c is not null or an array of 16 bytes')
|
||||
|
||||
let i = 0
|
||||
const x = new Uint32Array(16)
|
||||
if (!c) {
|
||||
x[0] = 0x61707865
|
||||
x[1] = 0x3320646E
|
||||
x[2] = 0x79622D32
|
||||
x[3] = 0x6B206574
|
||||
} else {
|
||||
x[0] = LOAD32_LE(c, 0)
|
||||
x[1] = LOAD32_LE(c, 4)
|
||||
x[2] = LOAD32_LE(c, 8)
|
||||
x[3] = LOAD32_LE(c, 12)
|
||||
}
|
||||
x[4] = LOAD32_LE(k, 0)
|
||||
x[5] = LOAD32_LE(k, 4)
|
||||
x[6] = LOAD32_LE(k, 8)
|
||||
x[7] = LOAD32_LE(k, 12)
|
||||
x[8] = LOAD32_LE(k, 16)
|
||||
x[9] = LOAD32_LE(k, 20)
|
||||
x[10] = LOAD32_LE(k, 24)
|
||||
x[11] = LOAD32_LE(k, 28)
|
||||
x[12] = LOAD32_LE(_in, 0)
|
||||
x[13] = LOAD32_LE(_in, 4)
|
||||
x[14] = LOAD32_LE(_in, 8)
|
||||
x[15] = LOAD32_LE(_in, 12)
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
QUARTERROUND(x, 0, 4, 8, 12)
|
||||
QUARTERROUND(x, 1, 5, 9, 13)
|
||||
QUARTERROUND(x, 2, 6, 10, 14)
|
||||
QUARTERROUND(x, 3, 7, 11, 15)
|
||||
QUARTERROUND(x, 0, 5, 10, 15)
|
||||
QUARTERROUND(x, 1, 6, 11, 12)
|
||||
QUARTERROUND(x, 2, 7, 8, 13)
|
||||
QUARTERROUND(x, 3, 4, 9, 14)
|
||||
}
|
||||
|
||||
STORE32_LE(out, x[0], 0)
|
||||
STORE32_LE(out, x[1], 4)
|
||||
STORE32_LE(out, x[2], 8)
|
||||
STORE32_LE(out, x[3], 12)
|
||||
STORE32_LE(out, x[12], 16)
|
||||
STORE32_LE(out, x[13], 20)
|
||||
STORE32_LE(out, x[14], 24)
|
||||
STORE32_LE(out, x[15], 28)
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
function crypto_core_hchacha20_outputbytes () {
|
||||
return crypto_core_hchacha20_OUTPUTBYTES
|
||||
}
|
||||
|
||||
function crypto_core_hchacha20_inputbytes () {
|
||||
return crypto_core_hchacha20_INPUTBYTES
|
||||
}
|
||||
|
||||
function crypto_core_hchacha20_keybytes () {
|
||||
return crypto_core_hchacha20_KEYBYTES
|
||||
}
|
||||
|
||||
function crypto_core_hchacha20_constbytes () {
|
||||
return crypto_core_hchacha20_CONSTBYTES
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
crypto_core_hchacha20_INPUTBYTES,
|
||||
LOAD32_LE,
|
||||
STORE32_LE,
|
||||
QUARTERROUND,
|
||||
crypto_core_hchacha20,
|
||||
crypto_core_hchacha20_outputbytes,
|
||||
crypto_core_hchacha20_inputbytes,
|
||||
crypto_core_hchacha20_keybytes,
|
||||
crypto_core_hchacha20_constbytes
|
||||
}
|
||||
@@ -1,21 +1,28 @@
|
||||
/* eslint-disable camelcase */
|
||||
var MessageChannel = global.MessageChannel
|
||||
if (MessageChannel == null) ({ MessageChannel } = require('worker' + '_threads'))
|
||||
|
||||
function sodium_malloc (n) {
|
||||
return new Uint8Array(n)
|
||||
}
|
||||
|
||||
const sink = new MessageChannel()
|
||||
function sodium_free (n) {
|
||||
sodium_memzero(n)
|
||||
sink.port1.postMessage(n.buffer, [n.buffer])
|
||||
loadSink().port1.postMessage(n.buffer, [n.buffer])
|
||||
}
|
||||
|
||||
function sodium_memzero (arr) {
|
||||
arr.fill(0)
|
||||
}
|
||||
|
||||
var sink
|
||||
|
||||
function loadSink () {
|
||||
if (sink) return sink
|
||||
var MessageChannel = globalThis.MessageChannel
|
||||
if (MessageChannel == null) ({ MessageChannel } = require('worker' + '_threads'))
|
||||
sink = new MessageChannel()
|
||||
return sink
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
sodium_malloc,
|
||||
sodium_free,
|
||||
|
||||
+7
-9
@@ -1,21 +1,21 @@
|
||||
{
|
||||
"name": "sodium-javascript",
|
||||
"version": "0.6.3",
|
||||
"version": "0.8.0",
|
||||
"description": "WIP - a pure javascript version of sodium-native",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"blake2b": "^2.1.1",
|
||||
"chacha20-universal": "^1.0.4",
|
||||
"nanoassert": "^2.0.0",
|
||||
"sha256-universal": "^1.0.1",
|
||||
"sha512-universal": "^1.0.1",
|
||||
"sha256-universal": "^1.1.0",
|
||||
"sha512-universal": "^1.1.0",
|
||||
"siphash24": "^1.0.1",
|
||||
"xsalsa20": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"browserify": "^16.5.1",
|
||||
"sodium-test": "^0.9.0",
|
||||
"standard": "^14.3.4",
|
||||
"sodium-test": "^0.10.0",
|
||||
"standard": "^15.0.1",
|
||||
"tape-run": "^7.0.0"
|
||||
},
|
||||
"standard": {
|
||||
@@ -24,10 +24,8 @@
|
||||
]
|
||||
},
|
||||
"browser": {
|
||||
"crypto": false
|
||||
},
|
||||
"react-native": {
|
||||
"crypto": "crypto"
|
||||
"crypto": false,
|
||||
"worker_threads": false
|
||||
},
|
||||
"scripts": {
|
||||
"pretest": "standard",
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@ var assert = require('nanoassert')
|
||||
|
||||
var randombytes = (function () {
|
||||
var QUOTA = 65536 // limit for QuotaExceededException
|
||||
var crypto = global.crypto || global.msCrypto
|
||||
var crypto = globalThis.crypto || globalThis.msCrypto
|
||||
|
||||
function browserBytes (out, n) {
|
||||
for (let i = 0; i < n; i += QUOTA) {
|
||||
|
||||
Reference in New Issue
Block a user