Add improvements from initial review

This commit is contained in:
Christian Bundy 2020-09-01 14:01:58 -07:00
parent f36deb3334
commit 3b75fe02e3
2 changed files with 41 additions and 30 deletions

View File

@ -121,13 +121,11 @@ function crypto_box_beforenm (k, pk, sk) {
const zero = new Uint8Array(16)
const s = new Uint8Array(32)
if (crypto_scalarmult(s, sk, pk) !== 0) {
return -1
}
assert(crypto_scalarmult(s, sk, pk) === 0)
xsalsa20.core_hsalsa20(k, zero, s, xsalsa20.SIGMA)
return 0
return true
}
// int
@ -166,13 +164,12 @@ function crypto_box_detached (c, mac, m, n, pk, sk) {
const k = new Uint8Array(crypto_box_BEFORENMBYTES)
if (crypto_box_beforenm(k, pk, sk) !== 0) {
return -1
}
assert(crypto_box_beforenm(k, pk, sk))
const ret = crypto_box_detached_afternm(c, mac, m, n, k)
cleanup(k)
return crypto_box_detached_afternm(c, mac, m, n, k)
return ret
}
// int
@ -187,12 +184,17 @@ function crypto_box_detached (c, mac, m, n, pk, sk) {
// pk, sk);
// }
function crypto_box_easy (c, m, n, pk, sk) {
assert(c.length === m.length + crypto_box_MACBYTES, "c should be a buffer of length 'm.length + crypto_box_MACBYTES'")
console.log({ c, m, crypto_box_MACBYTES })
console.log('yep')
assert(c.length >= m.length + crypto_box_MACBYTES, "c should be at least 'm.length + crypto_box_MACBYTES'")
assert(c.byteLength <= crypto_box_MESSAGEBYTES_MAX, "m should not be more than 'crypto_box_MESSAGEBYTES_MAX' bytes")
return crypto_box_detached(c.subarray(crypto_box_MACBYTES), c.subarray(0, crypto_box_MACBYTES), m, n, pk, sk)
return crypto_box_detached(
c.subarray(crypto_box_MACBYTES, m.length + crypto_box_MACBYTES),
c.subarray(0, crypto_box_MACBYTES),
m,
n,
pk,
sk
)
}
// int
// crypto_box_open_detached_afternm(unsigned char *m, const unsigned char *c,
@ -232,13 +234,19 @@ function crypto_box_open_detached (m, c, mac, n, pk, sk) {
const k = Uint8Array(crypto_box_BEFORENMBYTES)
if (crypto_box_beforenm(k, pk, sk) !== 0) {
return -1
}
assert(crypto_box_beforenm(k, pk, sk))
const ret = crypto_box_open_detached_afternm(
m,
c,
mac,
n,
k
)
cleanup(k)
return crypto_box_open_detached_afternm(m, c, mac, n, k)
return ret
}
// int
@ -258,10 +266,15 @@ function crypto_box_open_easy (m, c, n, pk, sk) {
check(pk, crypto_box_PUBLICKEYBYTES)
check(sk, crypto_box_SECRETKEYBYTES)
if (c.length < crypto_box_MACBYTES) {
return -1
}
return crypto_box_open_detached(m, c.subarray(crypto_box_MACBYTES), n, pk, sk)
assert(c.length < crypto_box_MACBYTES)
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) {

View File

@ -69,10 +69,9 @@ const m = new Uint8Array([
])
// static unsigned char c[147 + crypto_box_MACBYTES];
// TODO: Is this supposed to diverge?! The original doesn't seem to match the crypto_box_easy docs.
const c = new Uint8Array(147)
const c = new Uint8Array(147 + crypto_box_MACBYTES)
//
//
// int
// main(void)
// {
@ -80,7 +79,7 @@ const c = new Uint8Array(147)
// int ret;
let i
let ret
//
//
// ret = crypto_box_easy(c, m, 131, nonce, bobpk, alicesk);
// assert(ret == 0);
ret = crypto_box_easy(c, m, nonce, bobpk, alicesk)
@ -89,22 +88,21 @@ ret = crypto_box_easy(c, m, nonce, bobpk, alicesk)
// printf(",0x%02x", (unsigned int) c[i]);
// }
for (i = 0; i < 131 + crypto_box_MACBYTES; ++i) {
const hex = c[i].toString(16).padStart(2, '0')
process.stdout.write(`,0x${hex}`)
process.stdout.write(`${hex} `)
}
// printf("\n");
process.stdout.write('\n')
//
//
// /* Null message */
//
//
// ret = crypto_box_easy(c, guard_page, 0, nonce, bobpk, alicesk);
// assert(ret == 0);
// for (i = 0; i < 1 + crypto_box_MACBYTES; ++i) {
// printf(",0x%02x", (unsigned int) c[i]);
// }
// printf("\n");
//
//
// ret =
// crypto_box_open_easy(c, c, crypto_box_MACBYTES, nonce, bobpk, alicesk);
// assert(ret == 0);
@ -115,7 +113,7 @@ process.stdout.write('\n')
// c[randombytes_uniform(crypto_box_MACBYTES)]++;
// ret = crypto_box_open_easy(c, c, crypto_box_MACBYTES, nonce, bobpk, alicesk);
// assert(ret == -1);
//
//
// return 0;
// }