2020-06-16 12:18:32 +00:00
|
|
|
const { crypto_hash_sha512 } = require('./crypto_hash')
|
2020-06-16 14:02:42 +00:00
|
|
|
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 xsalsa20 = require('xsalsa20')
|
2020-06-16 12:18:32 +00:00
|
|
|
|
2020-06-16 14:02:42 +00:00
|
|
|
var crypto_box_PUBLICKEYBYTES = 32,
|
|
|
|
crypto_box_SECRETKEYBYTES = 32,
|
|
|
|
crypto_box_BEFORENMBYTES = 32,
|
|
|
|
crypto_box_NONCEBYTES = 24,
|
|
|
|
crypto_box_ZEROBYTES = 32,
|
|
|
|
crypto_box_BOXZEROBYTES = 16,
|
|
|
|
crypto_box_SEALBYTES = 48,
|
|
|
|
crypto_box_SEEDBYTES = 32,
|
|
|
|
crypto_box_BEFORENMBYTES = 32
|
|
|
|
|
2020-05-04 19:12:44 +00:00
|
|
|
module.exports = {
|
|
|
|
crypto_box_keypair,
|
2020-06-16 14:02:42 +00:00
|
|
|
crypto_box_seed_keypair,
|
2020-05-04 19:12:44 +00:00
|
|
|
crypto_box_seal,
|
|
|
|
crypto_box_seal_open,
|
|
|
|
crypto_box_PUBLICKEYBYTES,
|
|
|
|
crypto_box_SECRETKEYBYTES,
|
|
|
|
crypto_box_BEFORENMBYTES,
|
|
|
|
crypto_box_NONCEBYTES,
|
|
|
|
crypto_box_ZEROBYTES,
|
|
|
|
crypto_box_BOXZEROBYTES,
|
|
|
|
crypto_box_SEALBYTES,
|
2020-06-16 14:02:42 +00:00
|
|
|
crypto_box_SEEDBYTES,
|
2020-05-04 19:12:44 +00:00
|
|
|
crypto_box_BEFORENMBYTES
|
|
|
|
}
|
|
|
|
|
|
|
|
function crypto_box_keypair(pk, sk) {
|
|
|
|
check(pk, crypto_box_PUBLICKEYBYTES)
|
|
|
|
check(sk, crypto_box_SECRETKEYBYTES)
|
|
|
|
randombytes(sk, 32)
|
|
|
|
return crypto_scalarmult_base(pk, sk)
|
|
|
|
}
|
|
|
|
|
2020-06-16 12:18:32 +00:00
|
|
|
function crypto_box_seed_keypair(pk, sk, seed) {
|
|
|
|
check(pk, crypto_box_PUBLICKEYBYTES)
|
|
|
|
check(sk, crypto_box_SECRETKEYBYTES)
|
|
|
|
check(sk, crypto_box_SEEDBYTES)
|
|
|
|
|
|
|
|
const hash = Buffer.alloc(64)
|
|
|
|
crypto_hash_sha512(hash, seed, 32)
|
2020-06-16 14:02:42 +00:00
|
|
|
hash.copy(sk, 0, 0, 32)
|
2020-06-16 14:57:58 +00:00
|
|
|
hash.fill(0)
|
2020-06-16 12:18:32 +00:00
|
|
|
|
|
|
|
return crypto_scalarmult_base(pk, sk)
|
|
|
|
}
|
|
|
|
|
2020-05-04 19:12:44 +00:00
|
|
|
function crypto_box_seal(c, m, pk) {
|
|
|
|
check(c, crypto_box_SEALBYTES + m.length)
|
|
|
|
check(pk, crypto_box_PUBLICKEYBYTES)
|
|
|
|
|
|
|
|
var epk = c.subarray(0, crypto_box_PUBLICKEYBYTES)
|
|
|
|
var esk = new Uint8Array(crypto_box_SECRETKEYBYTES)
|
|
|
|
crypto_box_keypair(epk, esk)
|
|
|
|
|
|
|
|
var n = new Uint8Array(crypto_box_NONCEBYTES)
|
2020-06-16 14:02:42 +00:00
|
|
|
crypto_generichash_batch(n, [ epk, pk ])
|
2020-05-04 19:12:44 +00:00
|
|
|
|
|
|
|
var s = new Uint8Array(crypto_box_PUBLICKEYBYTES)
|
|
|
|
crypto_scalarmult(s, esk, pk)
|
|
|
|
|
|
|
|
var k = new Uint8Array(crypto_box_BEFORENMBYTES)
|
|
|
|
var zero = new Uint8Array(16)
|
|
|
|
xsalsa20.core_hsalsa20(k, zero, s, xsalsa20.SIGMA)
|
|
|
|
|
|
|
|
crypto_secretbox_easy(c.subarray(epk.length), m, n, k)
|
|
|
|
|
|
|
|
cleanup(esk)
|
|
|
|
}
|
|
|
|
|
|
|
|
function crypto_box_seal_open(m, c, pk, sk) {
|
|
|
|
check(c, crypto_box_SEALBYTES)
|
|
|
|
check(m, c.length - crypto_box_SEALBYTES)
|
|
|
|
check(pk, crypto_box_PUBLICKEYBYTES)
|
|
|
|
check(sk, crypto_box_SECRETKEYBYTES)
|
|
|
|
|
|
|
|
var epk = c.subarray(0, crypto_box_PUBLICKEYBYTES)
|
|
|
|
|
|
|
|
var n = new Uint8Array(crypto_box_NONCEBYTES)
|
2020-06-16 14:02:42 +00:00
|
|
|
crypto_generichash_batch(n, [ epk, pk ])
|
2020-05-04 19:12:44 +00:00
|
|
|
|
|
|
|
var s = new Uint8Array(crypto_box_PUBLICKEYBYTES)
|
|
|
|
crypto_scalarmult(s, sk, epk)
|
|
|
|
|
|
|
|
var k = new Uint8Array(crypto_box_BEFORENMBYTES)
|
|
|
|
var zero = new Uint8Array(16)
|
|
|
|
xsalsa20.core_hsalsa20(k, zero, s, xsalsa20.SIGMA)
|
|
|
|
|
|
|
|
return crypto_secretbox_open_easy(m, c.subarray(epk.length), n, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
function check (buf, len) {
|
|
|
|
if (!buf || (len && buf.length < len)) throw new Error('Argument must be a buffer' + (len ? ' of length ' + len : ''))
|
|
|
|
}
|
|
|
|
|
2020-06-16 14:02:42 +00:00
|
|
|
function cleanup(arr) {
|
|
|
|
for (var i = 0; i < arr.length; i++) arr[i] = 0;
|
|
|
|
}
|