From a82160d51b994c72c996911ee244a1339826a4e5 Mon Sep 17 00:00:00 2001 From: Christophe Diederichs <45171645+chm-diederichs@users.noreply.github.com> Date: Tue, 15 Sep 2020 17:27:39 +0200 Subject: [PATCH] crypto_verify return booleans & add crypto_verify_64 (#33) * crypto_verify return booleans * can now return crypto_verify result directly * remove redundant return values * unpackneg check returns boolean --- crypto_aead.js | 2 +- crypto_onetimeauth.js | 3 +-- crypto_secretbox.js | 4 +--- crypto_sign.js | 10 +++++----- crypto_verify.js | 11 ++++++++--- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/crypto_aead.js b/crypto_aead.js index 42f863f..a5be092 100644 --- a/crypto_aead.js +++ b/crypto_aead.js @@ -131,7 +131,7 @@ function crypto_aead_chacha20poly1305_ietf_decrypt_detached (m, nsec, c, mac, ad computed_mac.fill(0) slen.fill(0) - if (ret !== 0) { + if (!ret) { m.fill(0) throw new Error('could not verify data') } diff --git a/crypto_onetimeauth.js b/crypto_onetimeauth.js index 5d5cdbb..5fb0bc0 100644 --- a/crypto_onetimeauth.js +++ b/crypto_onetimeauth.js @@ -23,7 +23,6 @@ function crypto_onetimeauth (mac, msg, key) { var s = new Poly1305(key) s.update(msg, 0, msg.byteLength) s.finish(mac, 0) - return true } function crypto_onetimeauth_verify (mac, msg, key) { @@ -33,5 +32,5 @@ function crypto_onetimeauth_verify (mac, msg, key) { var tmp = new Uint8Array(16) crypto_onetimeauth(tmp, msg, key) - return crypto_verify_16(mac, 0, tmp, 0) === 0 + return crypto_verify_16(mac, 0, tmp, 0) } diff --git a/crypto_secretbox.js b/crypto_secretbox.js index 05fb264..63d66e8 100644 --- a/crypto_secretbox.js +++ b/crypto_secretbox.js @@ -37,7 +37,6 @@ function crypto_secretbox (c, m, n, k) { c.subarray(0, crypto_onetimeauth_KEYBYTES) ) c.fill(0, 0, crypto_secretbox_BOXZEROBYTES) - return 0 } function crypto_secretbox_open (m, c, n, k) { @@ -94,9 +93,8 @@ function crypto_secretbox_easy (o, msg, n, k) { const m = new Uint8Array(crypto_secretbox_ZEROBYTES + msg.byteLength) const c = new Uint8Array(m.byteLength) m.set(msg, crypto_secretbox_ZEROBYTES) - if (crypto_secretbox(c, m, n, k) === false) return false + crypto_secretbox(c, m, n, k) o.set(c.subarray(crypto_secretbox_BOXZEROBYTES)) - return true } function crypto_secretbox_open_easy (msg, box, n, k) { diff --git a/crypto_sign.js b/crypto_sign.js index 2e11f78..fa60c33 100644 --- a/crypto_sign.js +++ b/crypto_sign.js @@ -242,16 +242,16 @@ function unpackneg (r, p) { S(chk, r[0]) M(chk, chk, den) - if (neq25519(chk, num)) M(r[0], r[0], I) + if (!neq25519(chk, num)) M(r[0], r[0], I) S(chk, r[0]) M(chk, chk, den) - if (neq25519(chk, num)) return -1 + if (!neq25519(chk, num)) return false if (par25519(r[0]) === (p[31] >> 7)) Z(r[0], gf0, r[0]) M(r[3], r[0], r[1]) - return 0 + return true } /* eslint-disable no-unused-vars */ @@ -270,7 +270,7 @@ function crypto_sign_open (msg, sm, pk) { mlen = -1 if (n < 64) return false - if (unpackneg(q, pk)) return false + if (!unpackneg(q, pk)) return false for (i = 0; i < n; i++) m[i] = sm[i] for (i = 0; i < 32; i++) m[i + 32] = pk[i] @@ -283,7 +283,7 @@ function crypto_sign_open (msg, sm, pk) { pack(t, p) n -= 64 - if (crypto_verify_32(sm, 0, t, 0)) { + if (!crypto_verify_32(sm, 0, t, 0)) { for (i = 0; i < n; i++) m[i] = 0 return false // throw new Error('crypto_sign_open failed') diff --git a/crypto_verify.js b/crypto_verify.js index f23ff3d..afa666d 100644 --- a/crypto_verify.js +++ b/crypto_verify.js @@ -1,7 +1,8 @@ /* eslint-disable camelcase */ module.exports = { crypto_verify_16, - crypto_verify_32 + crypto_verify_32, + crypto_verify_64 } function vn (x, xi, y, yi, n) { @@ -16,9 +17,13 @@ Object.defineProperty(module.exports, 'vn', { }) function crypto_verify_16 (x, xi, y, yi) { - return vn(x, xi, y, yi, 16) + return vn(x, xi, y, yi, 16) === 0 } function crypto_verify_32 (x, xi, y, yi) { - return vn(x, xi, y, yi, 32) + return vn(x, xi, y, yi, 32) === 0 +} + +function crypto_verify_64 (x, xi, y, yi) { + return vn(x, xi, y, yi, 64) === 0 }