diff --git a/crypto_box.js b/crypto_box.js index 1f2af82..2eb8d44 100644 --- a/crypto_box.js +++ b/crypto_box.js @@ -4,6 +4,7 @@ const { randombytes } = require('./randombytes') const { crypto_generichash_batch } = require('./crypto_generichash') const { crypto_secretbox_open_easy, crypto_secretbox_easy } = require('./crypto_secretbox') const xsalsa20 = require('xsalsa20') +const assert = require('nanoassert') var crypto_box_PUBLICKEYBYTES = 32, crypto_box_SECRETKEYBYTES = 32, @@ -39,13 +40,13 @@ function crypto_box_keypair(pk, sk) { } function crypto_box_seed_keypair(pk, sk, seed) { - check(pk, crypto_box_PUBLICKEYBYTES) - check(sk, crypto_box_SECRETKEYBYTES) - check(sk, crypto_box_SEEDBYTES) + 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") + assert(sk.byteLength === crypto_box_SEEDBYTES, "sk should be 'crypto_box_SEEDBYTES' bytes") - const hash = Buffer.alloc(64) + const hash = new Uint8Array(64) crypto_hash_sha512(hash, seed, 32) - hash.copy(sk, 0, 0, 32) + sk.set(hash.subarray(0, 32)) hash.fill(0) return crypto_scalarmult_base(pk, sk) diff --git a/crypto_hash.js b/crypto_hash.js index 73599ec..53c2c2b 100644 --- a/crypto_hash.js +++ b/crypto_hash.js @@ -1,19 +1,20 @@ const sha256 = require('sha256-wasm') const sha512 = require('sha512-wasm') +const assert = require('nanoassert') var crypto_hash_sha256_BYTES = 32 var crypto_hash_sha512_BYTES = 64 var crypto_hash_BYTES = crypto_hash_sha512_BYTES function crypto_hash_sha256 (out, m, n) { - check(out, crypto_hash_sha256_BYTES) + assert(out.byteLength === crypto_hash_sha256_BYTES, "out must be 'crypto_hash_sha256_BYTES' bytes long") sha256().update(m.subarray(0, n)).digest(out) return 0 } function crypto_hash_sha512 (out, m, n) { - check(out, crypto_hash_sha512_BYTES) + assert(out.byteLength === crypto_hash_sha512_BYTES, "out must be 'crypto_hash_sha512_BYTES' bytes long") sha512().update(m.subarray(0, n)).digest(out) return 0 @@ -30,7 +31,3 @@ module.exports = { crypto_hash_BYTES, crypto_hash_sha256_BYTES } - -function check (buf, len) { - if (!buf || (len && buf.length < len)) throw new Error('Argument must be a buffer' + (len ? ' of length ' + len : '')) -} diff --git a/crypto_kx.js b/crypto_kx.js index e77c6fb..3477c16 100644 --- a/crypto_kx.js +++ b/crypto_kx.js @@ -1,32 +1,29 @@ const { crypto_scalarmult_base } = require('./crypto_scalarmult') const { crypto_generichash } = require('./crypto_generichash') const { randombytes_buf } = require('./randombytes') +const assert = require('nanoassert') var crypto_kx_SEEDBYTES = 32 var crypto_kx_PUBLICKEYBYTES = 32 var crypto_kx_SECRETKEYBYTES = 32 function crypto_kx_keypair (pk, sk) { - check(pk, crypto_kx_PUBLICKEYBYTES) - check(sk, crypto_kx_SECRETKEYBYTES) + assert(pk.byteLength === crypto_kx_PUBLICKEYBYTES, "pk must be 'crypto_kx_PUBLICKEYBYTES' bytes") + assert(sk.byteLength === crypto_kx_SECRETKEYBYTES, "sk must be 'crypto_kx_SECRETKEYBYTES' bytes") randombytes_buf(sk, crypto_kx_SECRETKEYBYTES) return crypto_scalarmult_base(pk, sk) } function crypto_kx_seed_keypair (pk, sk, seed) { - check(pk, crypto_kx_PUBLICKEYBYTES) - check(sk, crypto_kx_SECRETKEYBYTES) - check(seed, crypto_kx_SEEDBYTES) + assert(pk.byteLength === crypto_kx_PUBLICKEYBYTES, "pk must be 'crypto_kx_PUBLICKEYBYTES' bytes") + assert(sk.byteLength === crypto_kx_SECRETKEYBYTES, "sk must be 'crypto_kx_SECRETKEYBYTES' bytes") + assert(seed.byteLength === crypto_kx_SEEDBYTES, "seed must be 'crypto_kx_SEEDBYTES' bytes") crypto_generichash(sk, seed) return crypto_scalarmult_base(pk, sk) } -function check (buf, len) { - if (!buf || (len && buf.length < len)) throw new Error('Argument must be a buffer' + (len ? ' of length ' + len : '')) -} - module.exports = { crypto_kx_keypair, crypto_kx_seed_keypair,