sodium-javascript/crypto_kx.js
Christian Bundy e597b14bae Remove unused argument from crypto_kx
Problem: randombytes_buf uses the size of the buffer as the number of
bytes to output, so we don't need to add an argument about the number of
bytes we want.

Solution: Remove unused argument and use buffer size assertion to be
sure that we're producing the correct number of random bytes.
2020-09-04 09:23:37 -07:00

35 lines
1.2 KiB
JavaScript

/* eslint-disable camelcase */
const { crypto_scalarmult_base } = require('./crypto_scalarmult')
const { crypto_generichash } = require('./crypto_generichash')
const { randombytes_buf } = require('./randombytes')
const assert = require('nanoassert')
const crypto_kx_SEEDBYTES = 32
const crypto_kx_PUBLICKEYBYTES = 32
const crypto_kx_SECRETKEYBYTES = 32
function crypto_kx_keypair (pk, sk) {
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)
return crypto_scalarmult_base(pk, sk)
}
function crypto_kx_seed_keypair (pk, sk, seed) {
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)
}
module.exports = {
crypto_kx_keypair,
crypto_kx_seed_keypair,
crypto_kx_SEEDBYTES,
crypto_kx_SECRETKEYBYTES,
crypto_kx_PUBLICKEYBYTES
}