forked from bcoin-org/bcrypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmac.js
138 lines (103 loc) · 2.69 KB
/
kmac.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*!
* kmac.js - KMAC implementation for bcrypto
* Copyright (c) 2018-2019, Christopher Jeffrey (MIT License).
* https://github.com/bcoin-org/bcrypto
*
* Resources:
* https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-185.pdf
* https://github.com/XKCP/XKCP/blob/8f447eb/lib/high/Keccak/SP800-185/SP800-185.inc
* https://github.com/XKCP/XKCP/blob/8f447eb/lib/high/Keccak/SP800-185/SP800-185.c
* https://github.com/XKCP/XKCP/blob/8f447eb/tests/UnitTests/testSP800-185.c
* https://github.com/emn178/js-sha3/blob/master/src/sha3.js
*/
'use strict';
const assert = require('./internal/assert');
const CSHAKE = require('./cshake');
const HMAC = require('./internal/hmac');
/*
* Constants
*/
const PREFIX = Buffer.from('KMAC', 'binary');
const EMPTY = Buffer.alloc(0);
/*
* KMAC
*/
class KMAC extends CSHAKE {
constructor() {
super();
}
init(bits, key, pers) {
if (key == null)
key = EMPTY;
assert(Buffer.isBuffer(key));
super.init(bits, PREFIX, pers);
super.bytePad([key], this.rate / 8);
return this;
}
final(len) {
if (len == null) {
const size = this.rate / 8;
len = 100 - size / 2;
}
super.rightEncode(len * 8);
return super.final(len);
}
static hash() {
return new KMAC();
}
static hmac(bits, key, pers, len) {
if (bits == null)
bits = 256;
assert((bits >>> 0) === bits);
assert(bits === 128 || bits === 256);
const rate = 1600 - bits * 2;
return new HMAC(KMAC, rate / 8, [bits, key, pers], [len]);
}
static digest(data, bits, key, pers, len) {
return KMAC.ctx.init(bits, key, pers).update(data).final(len);
}
static root(left, right, bits, key, pers, len) {
if (bits == null)
bits = 256;
if (len == null)
len = 0;
if (len === 0) {
assert((bits >>> 0) === bits);
len = bits >>> 3;
}
assert((len >>> 0) === len);
assert(Buffer.isBuffer(left) && left.length === len);
assert(Buffer.isBuffer(right) && right.length === len);
const {ctx} = KMAC;
ctx.init(bits, key, pers);
ctx.update(left);
ctx.update(right);
return ctx.final(len);
}
static multi(x, y, z, bits, key, pers, len) {
const {ctx} = KMAC;
ctx.init(bits, key, pers);
ctx.update(x);
ctx.update(y);
if (z)
ctx.update(z);
return ctx.final(len);
}
static mac(data, salt, bits, key, pers, len) {
return KMAC.hmac(bits, key, pers, len).init(salt).update(data).final();
}
}
/*
* Static
*/
KMAC.native = CSHAKE.native;
KMAC.id = 'KMAC256';
KMAC.size = 32;
KMAC.bits = 256;
KMAC.blockSize = 136;
KMAC.zero = Buffer.alloc(32, 0x00);
KMAC.ctx = new KMAC();
/*
* Expose
*/
module.exports = KMAC;