change checks to assertions

This commit is contained in:
Christophe Diederichs 2020-06-17 17:27:28 +02:00
parent d626fb2ca5
commit fdfc09a157
3 changed files with 15 additions and 20 deletions

View File

@ -4,6 +4,7 @@ const { randombytes } = require('./randombytes')
const { crypto_generichash_batch } = require('./crypto_generichash') const { crypto_generichash_batch } = require('./crypto_generichash')
const { crypto_secretbox_open_easy, crypto_secretbox_easy } = require('./crypto_secretbox') const { crypto_secretbox_open_easy, crypto_secretbox_easy } = require('./crypto_secretbox')
const xsalsa20 = require('xsalsa20') const xsalsa20 = require('xsalsa20')
const assert = require('nanoassert')
var crypto_box_PUBLICKEYBYTES = 32, var crypto_box_PUBLICKEYBYTES = 32,
crypto_box_SECRETKEYBYTES = 32, crypto_box_SECRETKEYBYTES = 32,
@ -39,13 +40,13 @@ function crypto_box_keypair(pk, sk) {
} }
function crypto_box_seed_keypair(pk, sk, seed) { function crypto_box_seed_keypair(pk, sk, seed) {
check(pk, crypto_box_PUBLICKEYBYTES) assert(pk.byteLength === crypto_box_PUBLICKEYBYTES, "pk should be 'crypto_box_PUBLICKEYBYTES' bytes")
check(sk, crypto_box_SECRETKEYBYTES) assert(sk.byteLength === crypto_box_SECRETKEYBYTES, "sk should be 'crypto_box_SECRETKEYBYTES' bytes")
check(sk, crypto_box_SEEDBYTES) 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) crypto_hash_sha512(hash, seed, 32)
hash.copy(sk, 0, 0, 32) sk.set(hash.subarray(0, 32))
hash.fill(0) hash.fill(0)
return crypto_scalarmult_base(pk, sk) return crypto_scalarmult_base(pk, sk)

View File

@ -1,19 +1,20 @@
const sha256 = require('sha256-wasm') const sha256 = require('sha256-wasm')
const sha512 = require('sha512-wasm') const sha512 = require('sha512-wasm')
const assert = require('nanoassert')
var crypto_hash_sha256_BYTES = 32 var crypto_hash_sha256_BYTES = 32
var crypto_hash_sha512_BYTES = 64 var crypto_hash_sha512_BYTES = 64
var crypto_hash_BYTES = crypto_hash_sha512_BYTES var crypto_hash_BYTES = crypto_hash_sha512_BYTES
function crypto_hash_sha256 (out, m, n) { 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) sha256().update(m.subarray(0, n)).digest(out)
return 0 return 0
} }
function crypto_hash_sha512 (out, m, n) { 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) sha512().update(m.subarray(0, n)).digest(out)
return 0 return 0
@ -30,7 +31,3 @@ module.exports = {
crypto_hash_BYTES, crypto_hash_BYTES,
crypto_hash_sha256_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 : ''))
}

View File

@ -1,32 +1,29 @@
const { crypto_scalarmult_base } = require('./crypto_scalarmult') const { crypto_scalarmult_base } = require('./crypto_scalarmult')
const { crypto_generichash } = require('./crypto_generichash') const { crypto_generichash } = require('./crypto_generichash')
const { randombytes_buf } = require('./randombytes') const { randombytes_buf } = require('./randombytes')
const assert = require('nanoassert')
var crypto_kx_SEEDBYTES = 32 var crypto_kx_SEEDBYTES = 32
var crypto_kx_PUBLICKEYBYTES = 32 var crypto_kx_PUBLICKEYBYTES = 32
var crypto_kx_SECRETKEYBYTES = 32 var crypto_kx_SECRETKEYBYTES = 32
function crypto_kx_keypair (pk, sk) { function crypto_kx_keypair (pk, sk) {
check(pk, crypto_kx_PUBLICKEYBYTES) assert(pk.byteLength === crypto_kx_PUBLICKEYBYTES, "pk must be 'crypto_kx_PUBLICKEYBYTES' bytes")
check(sk, crypto_kx_SECRETKEYBYTES) assert(sk.byteLength === crypto_kx_SECRETKEYBYTES, "sk must be 'crypto_kx_SECRETKEYBYTES' bytes")
randombytes_buf(sk, crypto_kx_SECRETKEYBYTES) randombytes_buf(sk, crypto_kx_SECRETKEYBYTES)
return crypto_scalarmult_base(pk, sk) return crypto_scalarmult_base(pk, sk)
} }
function crypto_kx_seed_keypair (pk, sk, seed) { function crypto_kx_seed_keypair (pk, sk, seed) {
check(pk, crypto_kx_PUBLICKEYBYTES) assert(pk.byteLength === crypto_kx_PUBLICKEYBYTES, "pk must be 'crypto_kx_PUBLICKEYBYTES' bytes")
check(sk, crypto_kx_SECRETKEYBYTES) assert(sk.byteLength === crypto_kx_SECRETKEYBYTES, "sk must be 'crypto_kx_SECRETKEYBYTES' bytes")
check(seed, crypto_kx_SEEDBYTES) assert(seed.byteLength === crypto_kx_SEEDBYTES, "seed must be 'crypto_kx_SEEDBYTES' bytes")
crypto_generichash(sk, seed) crypto_generichash(sk, seed)
return crypto_scalarmult_base(pk, sk) 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 = { module.exports = {
crypto_kx_keypair, crypto_kx_keypair,
crypto_kx_seed_keypair, crypto_kx_seed_keypair,