Upgrade onetimeauth to sodium-native api

This commit is contained in:
Emil Bay 2020-06-24 16:11:56 +02:00
parent d8d59d0443
commit 80dd633012

View File

@ -1,21 +1,37 @@
/* eslint-disable camelcase */ /* eslint-disable camelcase */
const assert = require('nanoassert')
const Poly1305 = require('./internal/poly1305') const Poly1305 = require('./internal/poly1305')
const { crypto_verify_16 } = require('./crypto_verify') const { crypto_verify_16 } = require('./crypto_verify')
const crypto_onetimeauth_BYTES = 16
const crypto_onetimeauth_KEYBYTES = 32
const crypto_onetimeauth_PRIMITIVE = 'poly1305'
module.exports = { module.exports = {
crypto_onetimeauth, crypto_onetimeauth,
crypto_onetimeauth_verify crypto_onetimeauth_verify,
crypto_onetimeauth_BYTES,
crypto_onetimeauth_KEYBYTES,
crypto_onetimeauth_PRIMITIVE
} }
function crypto_onetimeauth (out, outpos, m, mpos, n, k) { function crypto_onetimeauth (mac, msg, key) {
var s = new Poly1305(k) assert(mac.byteLength === crypto_onetimeauth_BYTES, 'mac must be \'crypto_onetimeauth_BYTES\' bytes')
s.update(m, mpos, n) assert(msg.byteLength != null, 'msg must be buffer')
s.finish(out, outpos) assert(key.byteLength === crypto_onetimeauth_KEYBYTES, 'key must be \'crypto_onetimeauth_KEYBYTES\' bytes')
return 0
var s = new Poly1305(key)
s.update(msg, 0, msg.byteLength)
s.finish(mac, 0)
return true
} }
function crypto_onetimeauth_verify (h, hpos, m, mpos, n, k) { function crypto_onetimeauth_verify (mac, msg, key) {
var x = new Uint8Array(16) assert(mac.byteLength === crypto_onetimeauth_BYTES, 'mac must be \'crypto_onetimeauth_BYTES\' bytes')
crypto_onetimeauth(x, 0, m, mpos, n, k) assert(msg.byteLength != null, 'msg must be buffer')
return crypto_verify_16(h, hpos, x, 0) assert(key.byteLength === crypto_onetimeauth_KEYBYTES, 'key must be \'crypto_onetimeauth_KEYBYTES\' bytes')
var tmp = new Uint8Array(16)
crypto_onetimeauth(tmp, msg, key)
return crypto_verify_16(mac, 0, tmp, 0) === 0
} }