2020-06-24 12:08:55 +00:00
|
|
|
/* eslint-disable camelcase */
|
2020-06-24 14:11:56 +00:00
|
|
|
const assert = require('nanoassert')
|
2020-06-24 12:08:55 +00:00
|
|
|
const Poly1305 = require('./internal/poly1305')
|
2020-06-18 15:09:03 +00:00
|
|
|
const { crypto_verify_16 } = require('./crypto_verify')
|
|
|
|
|
2020-06-24 14:11:56 +00:00
|
|
|
const crypto_onetimeauth_BYTES = 16
|
|
|
|
const crypto_onetimeauth_KEYBYTES = 32
|
|
|
|
const crypto_onetimeauth_PRIMITIVE = 'poly1305'
|
|
|
|
|
2020-06-18 15:09:03 +00:00
|
|
|
module.exports = {
|
|
|
|
crypto_onetimeauth,
|
2020-06-24 14:11:56 +00:00
|
|
|
crypto_onetimeauth_verify,
|
|
|
|
crypto_onetimeauth_BYTES,
|
|
|
|
crypto_onetimeauth_KEYBYTES,
|
|
|
|
crypto_onetimeauth_PRIMITIVE
|
2020-06-18 15:09:03 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 14:11:56 +00:00
|
|
|
function crypto_onetimeauth (mac, msg, key) {
|
2020-06-24 14:14:52 +00:00
|
|
|
assert(mac.byteLength === crypto_onetimeauth_BYTES, "mac must be 'crypto_onetimeauth_BYTES' bytes")
|
2020-06-24 14:11:56 +00:00
|
|
|
assert(msg.byteLength != null, 'msg must be buffer')
|
2020-06-24 14:14:52 +00:00
|
|
|
assert(key.byteLength === crypto_onetimeauth_KEYBYTES, "key must be 'crypto_onetimeauth_KEYBYTES' bytes")
|
2020-06-24 14:11:56 +00:00
|
|
|
|
|
|
|
var s = new Poly1305(key)
|
|
|
|
s.update(msg, 0, msg.byteLength)
|
|
|
|
s.finish(mac, 0)
|
|
|
|
return true
|
2020-06-18 15:09:03 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 14:11:56 +00:00
|
|
|
function crypto_onetimeauth_verify (mac, msg, key) {
|
2020-06-24 14:14:52 +00:00
|
|
|
assert(mac.byteLength === crypto_onetimeauth_BYTES, "mac must be 'crypto_onetimeauth_BYTES' bytes")
|
2020-06-24 14:11:56 +00:00
|
|
|
assert(msg.byteLength != null, 'msg must be buffer')
|
2020-06-24 14:14:52 +00:00
|
|
|
assert(key.byteLength === crypto_onetimeauth_KEYBYTES, "key must be 'crypto_onetimeauth_KEYBYTES' bytes")
|
2020-06-24 14:11:56 +00:00
|
|
|
|
|
|
|
var tmp = new Uint8Array(16)
|
|
|
|
crypto_onetimeauth(tmp, msg, key)
|
|
|
|
return crypto_verify_16(mac, 0, tmp, 0) === 0
|
2020-06-18 15:09:03 +00:00
|
|
|
}
|