UNPKG

1.31 kBJavaScriptView Raw
1/* eslint-disable camelcase */
2const assert = require('nanoassert')
3const Poly1305 = require('./internal/poly1305')
4const { crypto_verify_16 } = require('./crypto_verify')
5
6const crypto_onetimeauth_BYTES = 16
7const crypto_onetimeauth_KEYBYTES = 32
8const crypto_onetimeauth_PRIMITIVE = 'poly1305'
9
10module.exports = {
11 crypto_onetimeauth,
12 crypto_onetimeauth_verify,
13 crypto_onetimeauth_BYTES,
14 crypto_onetimeauth_KEYBYTES,
15 crypto_onetimeauth_PRIMITIVE
16}
17
18function crypto_onetimeauth (mac, msg, key) {
19 assert(mac.byteLength === crypto_onetimeauth_BYTES, "mac must be 'crypto_onetimeauth_BYTES' bytes")
20 assert(msg.byteLength != null, 'msg must be buffer')
21 assert(key.byteLength === crypto_onetimeauth_KEYBYTES, "key must be 'crypto_onetimeauth_KEYBYTES' bytes")
22
23 var s = new Poly1305(key)
24 s.update(msg, 0, msg.byteLength)
25 s.finish(mac, 0)
26 return true
27}
28
29function crypto_onetimeauth_verify (mac, msg, key) {
30 assert(mac.byteLength === crypto_onetimeauth_BYTES, "mac must be 'crypto_onetimeauth_BYTES' bytes")
31 assert(msg.byteLength != null, 'msg must be buffer')
32 assert(key.byteLength === crypto_onetimeauth_KEYBYTES, "key must be 'crypto_onetimeauth_KEYBYTES' bytes")
33
34 var tmp = new Uint8Array(16)
35 crypto_onetimeauth(tmp, msg, key)
36 return crypto_verify_16(mac, 0, tmp, 0) === 0
37}