Compare commits

..
2 Commits
Author SHA1 Message Date
Christophe Diederichs ce5ac41ecd 0.6.3 2020-09-15 17:39:54 +02:00
Christophe Diederichs a82160d51b 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
2020-09-15 17:27:39 +02:00
8 changed files with 16 additions and 91 deletions
+1 -1
View File
@@ -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')
}
-72
View File
@@ -1,72 +0,0 @@
/* eslint-disable camelcase */
const { crypto_verify_32, crypto_verify_64 } = require('./crypto_verify')
const Sha256 = require('sha256-universal')
const Sha512 = require('sha512-universal')
const assert = require('nanoassert')
const crypto_auth_hmacsha256_BYTES = 32
const crypto_auth_hmacsha256_KEYBYTES = 32
const crypto_auth_hmacsha512_BYTES = 64
const crypto_auth_hmacsha512_KEYBYTES = 32
const crypto_auth_hmacsha512256_BYTES = 32
const crypto_auth_hmacsha512256_KEYBYTES = 32
function crypto_auth_hmacsha256 (out, input, k) {
assert(out.byteLength === crypto_auth_hmacsha256_BYTES, "out should be 'crypto_auth_hmacsha256_BYTES' in length")
const hmac = Sha256.HMAC(key)
hmac.update(input)
return hmac.digest(out)
}
function crypto_auth_hmacsha256_verify (h, input, k) {
const correct = Sha256.HMAC(k).update(input).digest()
return crypto_verify_32(h, 0, correct, 0) | sodium_memcmp(correct, h, 32)
}
function crypto_auth_hmacsha512 (out, input, k) {
assert(out.byteLength === crypto_auth_hmacsha512_BYTES, "out should be 'crypto_auth_hmacsha512_BYTES' in length")
const hmac = Sha512.HMAC(key)
hmac.update(input)
return hmac.digest(out)
}
function crypto_auth_hmacsha512_verify (h, input, k) {
const correct = Sha512.HMAC(k).update(input).digest()
return crypto_verify_64(h, 0, correct, 0) | sodium_memcmp(correct, h, 64)
}
function crypto_auth_hmacsha512256 (out, input, k) {
assert(out.byteLength === crypto_auth_hmacsha512_BYTES, "out should be 'crypto_auth_hmacsha512256_BYTES' in length")
const out0 = Buffer.alloc(64)
const hmac = Sha512.HMAC(key)
hmac.update(input)
hmac.digest(out)
out.set(out0.subarray(0, 32))
}
function crypto_auth_hmacsha512256_verify (h, input, k) {
const correct = Sha512.HMAC(k).update(input).digest()
return crypto_verify_32(h, 0, correct, 0) | sodium_memcmp(correct, h, 32)
}
module.exports = {
crypto_auth_hmacsha256_BYTES,
crypto_auth_hmacsha256_KEYBYTES,
crypto_auth_hmacsha512_BYTES,
crypto_auth_hmacsha512_KEYBYTES,
crypto_auth_hmacsha512256_BYTES,
crypto_auth_hmacsha512256_KEYBYTES,
crypto_auth_hmacsha256,
crypto_auth_hmacsha256_verify,
crypto_auth_hmacsha512,
crypto_auth_hmacsha512_verify,
crypto_auth_hmacsha512256,
crypto_auth_hmacsha512256_verify
}
+1 -2
View File
@@ -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)
}
+1 -3
View File
@@ -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) {
+5 -5
View File
@@ -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')
+5 -4
View File
@@ -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,13 +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)
return vn(x, xi, y, yi, 64) === 0
}
-1
View File
@@ -12,7 +12,6 @@ forward(require('./randombytes'))
forward(require('./memory'))
forward(require('./helpers'))
forward(require('./crypto_verify'))
forward(require('./crypto_auth'))
forward(require('./crypto_box'))
forward(require('./crypto_generichash'))
forward(require('./crypto_hash'))
+3 -3
View File
@@ -1,14 +1,14 @@
{
"name": "sodium-javascript",
"version": "0.6.2",
"version": "0.6.3",
"description": "WIP - a pure javascript version of sodium-native",
"main": "index.js",
"dependencies": {
"blake2b": "^2.1.1",
"chacha20-universal": "^1.0.4",
"nanoassert": "^2.0.0",
"sha256-universal": "^1.1.0",
"sha512-universal": "^1.1.0",
"sha256-universal": "^1.0.1",
"sha512-universal": "^1.0.1",
"siphash24": "^1.0.1",
"xsalsa20": "^1.0.0"
},