1 |
|
2 | const assert = require('nanoassert')
|
3 | const Poly1305 = require('./internal/poly1305')
|
4 | const { crypto_verify_16 } = require('./crypto_verify')
|
5 |
|
6 | const crypto_onetimeauth_BYTES = 16
|
7 | const crypto_onetimeauth_KEYBYTES = 32
|
8 | const crypto_onetimeauth_PRIMITIVE = 'poly1305'
|
9 |
|
10 | module.exports = {
|
11 | crypto_onetimeauth,
|
12 | crypto_onetimeauth_verify,
|
13 | crypto_onetimeauth_BYTES,
|
14 | crypto_onetimeauth_KEYBYTES,
|
15 | crypto_onetimeauth_PRIMITIVE
|
16 | }
|
17 |
|
18 | function 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 |
|
29 | function 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 | }
|