@arcblock/did-noise 0.0.0-stage → 1.30.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +13 -0
- package/esm/chacha20poly1305.d.mts +18 -0
- package/esm/chacha20poly1305.mjs +152 -0
- package/esm/chain-node-mesh.d.mts +79 -0
- package/esm/chain-node-mesh.mjs +187 -0
- package/esm/http-transport.d.mts +72 -0
- package/esm/http-transport.mjs +216 -0
- package/esm/index.d.mts +6 -0
- package/esm/index.mjs +7 -0
- package/esm/mesh-tunnel.d.mts +165 -0
- package/esm/mesh-tunnel.mjs +350 -0
- package/esm/noise-session.d.mts +157 -0
- package/esm/noise-session.mjs +491 -0
- package/lib/_vendor/@noble/curves/LICENSE +21 -0
- package/lib/_vendor/@noble/curves/abstract/curve.cjs +338 -0
- package/lib/_vendor/@noble/curves/abstract/edwards.cjs +542 -0
- package/lib/_vendor/@noble/curves/abstract/hash-to-curve.cjs +72 -0
- package/lib/_vendor/@noble/curves/abstract/modular.cjs +557 -0
- package/lib/_vendor/@noble/curves/abstract/montgomery.cjs +149 -0
- package/lib/_vendor/@noble/curves/ed25519.cjs +302 -0
- package/lib/_vendor/@noble/curves/utils.cjs +436 -0
- package/lib/_vendor/@noble/hashes/LICENSE +21 -0
- package/lib/_vendor/@noble/hashes/_blake.cjs +300 -0
- package/lib/_vendor/@noble/hashes/_md.cjs +194 -0
- package/lib/_vendor/@noble/hashes/_u64.cjs +60 -0
- package/lib/_vendor/@noble/hashes/blake2.cjs +291 -0
- package/lib/_vendor/@noble/hashes/hkdf.cjs +104 -0
- package/lib/_vendor/@noble/hashes/hmac.cjs +86 -0
- package/lib/_vendor/@noble/hashes/sha2.cjs +417 -0
- package/lib/_vendor/@noble/hashes/utils.cjs +422 -0
- package/lib/chacha20poly1305.cjs +153 -0
- package/lib/chacha20poly1305.d.cts +18 -0
- package/lib/chain-node-mesh.cjs +189 -0
- package/lib/chain-node-mesh.d.cts +79 -0
- package/lib/http-transport.cjs +219 -0
- package/lib/http-transport.d.cts +72 -0
- package/lib/index.cjs +31 -0
- package/lib/index.d.cts +6 -0
- package/lib/mesh-tunnel.cjs +354 -0
- package/lib/mesh-tunnel.d.cts +165 -0
- package/lib/noise-session.cjs +503 -0
- package/lib/noise-session.d.cts +157 -0
- package/package.json +80 -4
- package/README.md +0 -3
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
const require_utils = require('./utils.cjs');
|
|
2
|
+
const require__md = require('./_md.cjs');
|
|
3
|
+
const require__u64 = require('./_u64.cjs');
|
|
4
|
+
const require__blake = require('./_blake.cjs');
|
|
5
|
+
|
|
6
|
+
//#region ../../node_modules/@noble/hashes/blake2.js
|
|
7
|
+
/**
|
|
8
|
+
* blake2b (64-bit) & blake2s (8 to 32-bit) hash functions.
|
|
9
|
+
* b could have been faster, but there is no fast u64 in js, so s is 1.5x faster.
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
function checkBlake2Opts(outputLen, opts = {}, keyLen, saltLen, persLen) {
|
|
13
|
+
require_utils.anumber(keyLen);
|
|
14
|
+
if (outputLen <= 0 || outputLen > keyLen) throw new Error("outputLen bigger than keyLen");
|
|
15
|
+
const { key, salt, personalization } = opts;
|
|
16
|
+
if (key !== void 0 && (key.length < 1 || key.length > keyLen)) throw new Error("\"key\" expected to be undefined or of length=1.." + keyLen);
|
|
17
|
+
if (salt !== void 0) require_utils.abytes(salt, saltLen, "salt");
|
|
18
|
+
if (personalization !== void 0) require_utils.abytes(personalization, persLen, "personalization");
|
|
19
|
+
}
|
|
20
|
+
/** Internal base class for BLAKE2. */
|
|
21
|
+
var _BLAKE2 = class {
|
|
22
|
+
buffer;
|
|
23
|
+
buffer32;
|
|
24
|
+
finished = false;
|
|
25
|
+
destroyed = false;
|
|
26
|
+
length = 0;
|
|
27
|
+
pos = 0;
|
|
28
|
+
blockLen;
|
|
29
|
+
outputLen;
|
|
30
|
+
canXOF = false;
|
|
31
|
+
constructor(blockLen, outputLen) {
|
|
32
|
+
require_utils.anumber(blockLen);
|
|
33
|
+
require_utils.anumber(outputLen);
|
|
34
|
+
this.blockLen = blockLen;
|
|
35
|
+
this.outputLen = outputLen;
|
|
36
|
+
this.buffer = new Uint8Array(blockLen);
|
|
37
|
+
this.buffer32 = require_utils.u32(this.buffer);
|
|
38
|
+
}
|
|
39
|
+
update(data) {
|
|
40
|
+
require_utils.aexists(this);
|
|
41
|
+
require_utils.abytes(data);
|
|
42
|
+
const { blockLen, buffer, buffer32 } = this;
|
|
43
|
+
const len = data.length;
|
|
44
|
+
const offset = data.byteOffset;
|
|
45
|
+
const buf = data.buffer;
|
|
46
|
+
for (let pos = 0; pos < len;) {
|
|
47
|
+
if (this.pos === blockLen) {
|
|
48
|
+
require_utils.swap32IfBE(buffer32);
|
|
49
|
+
this.compress(buffer32, 0, false);
|
|
50
|
+
require_utils.swap32IfBE(buffer32);
|
|
51
|
+
this.pos = 0;
|
|
52
|
+
}
|
|
53
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
54
|
+
const dataOffset = offset + pos;
|
|
55
|
+
if (take === blockLen && !(dataOffset % 4) && pos + take < len) {
|
|
56
|
+
const data32 = new Uint32Array(buf, dataOffset, Math.floor((len - pos) / 4));
|
|
57
|
+
require_utils.swap32IfBE(data32);
|
|
58
|
+
for (let pos32 = 0; pos + blockLen < len; pos32 += buffer32.length, pos += blockLen) {
|
|
59
|
+
this.length += blockLen;
|
|
60
|
+
this.compress(data32, pos32, false);
|
|
61
|
+
}
|
|
62
|
+
require_utils.swap32IfBE(data32);
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
66
|
+
this.pos += take;
|
|
67
|
+
this.length += take;
|
|
68
|
+
pos += take;
|
|
69
|
+
}
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
digestInto(out) {
|
|
73
|
+
require_utils.aexists(this);
|
|
74
|
+
require_utils.aoutput(out, this);
|
|
75
|
+
const { pos, buffer32 } = this;
|
|
76
|
+
this.finished = true;
|
|
77
|
+
require_utils.clean(this.buffer.subarray(pos));
|
|
78
|
+
require_utils.swap32IfBE(buffer32);
|
|
79
|
+
this.compress(buffer32, 0, true);
|
|
80
|
+
require_utils.swap32IfBE(buffer32);
|
|
81
|
+
if (out.byteOffset & 3) throw new RangeError("\"digestInto() output\" expected 4-byte aligned byteOffset, got " + out.byteOffset);
|
|
82
|
+
const state = this.get();
|
|
83
|
+
const out32 = require_utils.u32(out);
|
|
84
|
+
const full = Math.floor(this.outputLen / 4);
|
|
85
|
+
for (let i = 0; i < full; i++) out32[i] = require_utils.swap8IfBE(state[i]);
|
|
86
|
+
const tail = this.outputLen % 4;
|
|
87
|
+
if (!tail) return;
|
|
88
|
+
const off = full * 4;
|
|
89
|
+
const word = state[full];
|
|
90
|
+
for (let i = 0; i < tail; i++) out[off + i] = word >>> 8 * i;
|
|
91
|
+
}
|
|
92
|
+
digest() {
|
|
93
|
+
const { buffer, outputLen } = this;
|
|
94
|
+
this.digestInto(buffer);
|
|
95
|
+
const res = buffer.slice(0, outputLen);
|
|
96
|
+
this.destroy();
|
|
97
|
+
return res;
|
|
98
|
+
}
|
|
99
|
+
_cloneInto(to) {
|
|
100
|
+
const { buffer, length, finished, destroyed, outputLen, pos } = this;
|
|
101
|
+
to ||= new this.constructor({ dkLen: outputLen });
|
|
102
|
+
to.set(...this.get());
|
|
103
|
+
to.buffer.set(buffer);
|
|
104
|
+
to.destroyed = destroyed;
|
|
105
|
+
to.finished = finished;
|
|
106
|
+
to.length = length;
|
|
107
|
+
to.pos = pos;
|
|
108
|
+
to.outputLen = outputLen;
|
|
109
|
+
return to;
|
|
110
|
+
}
|
|
111
|
+
clone() {
|
|
112
|
+
return this._cloneInto();
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* BLAKE2-compress core method.
|
|
117
|
+
* Runs only the round function over a caller-supplied local vector; callers initialize `v0..v15`
|
|
118
|
+
* and apply the final `h[i] ^= v[i] ^ v[i + 8]` fold themselves.
|
|
119
|
+
* @param s - flattened sigma schedule bytes
|
|
120
|
+
* @param offset - starting word offset inside `msg`, not a byte offset
|
|
121
|
+
* @param msg - message words
|
|
122
|
+
* @param rounds - round count to execute
|
|
123
|
+
* @param v0 - state word 0
|
|
124
|
+
* @param v1 - state word 1
|
|
125
|
+
* @param v2 - state word 2
|
|
126
|
+
* @param v3 - state word 3
|
|
127
|
+
* @param v4 - state word 4
|
|
128
|
+
* @param v5 - state word 5
|
|
129
|
+
* @param v6 - state word 6
|
|
130
|
+
* @param v7 - state word 7
|
|
131
|
+
* @param v8 - state word 8
|
|
132
|
+
* @param v9 - state word 9
|
|
133
|
+
* @param v10 - state word 10
|
|
134
|
+
* @param v11 - state word 11
|
|
135
|
+
* @param v12 - state word 12
|
|
136
|
+
* @param v13 - state word 13
|
|
137
|
+
* @param v14 - state word 14
|
|
138
|
+
* @param v15 - state word 15
|
|
139
|
+
* @returns Updated compression state words.
|
|
140
|
+
* @example
|
|
141
|
+
* Run the BLAKE2 compression core on zeroed state and message words.
|
|
142
|
+
* ```ts
|
|
143
|
+
* import { compress } from '@noble/hashes/blake2.js';
|
|
144
|
+
* const state = compress(
|
|
145
|
+
* new Uint8Array(16),
|
|
146
|
+
* 0,
|
|
147
|
+
* new Uint32Array(16),
|
|
148
|
+
* 1,
|
|
149
|
+
* 0, 0, 0, 0, 0, 0, 0, 0,
|
|
150
|
+
* 0, 0, 0, 0, 0, 0, 0, 0
|
|
151
|
+
* );
|
|
152
|
+
* state.v0;
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
function compress(s, offset, msg, rounds, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) {
|
|
156
|
+
let j = 0;
|
|
157
|
+
for (let i = 0; i < rounds; i++) {
|
|
158
|
+
({a: v0, b: v4, c: v8, d: v12} = require__blake.G1s(v0, v4, v8, v12, msg[offset + s[j++]]));
|
|
159
|
+
({a: v0, b: v4, c: v8, d: v12} = require__blake.G2s(v0, v4, v8, v12, msg[offset + s[j++]]));
|
|
160
|
+
({a: v1, b: v5, c: v9, d: v13} = require__blake.G1s(v1, v5, v9, v13, msg[offset + s[j++]]));
|
|
161
|
+
({a: v1, b: v5, c: v9, d: v13} = require__blake.G2s(v1, v5, v9, v13, msg[offset + s[j++]]));
|
|
162
|
+
({a: v2, b: v6, c: v10, d: v14} = require__blake.G1s(v2, v6, v10, v14, msg[offset + s[j++]]));
|
|
163
|
+
({a: v2, b: v6, c: v10, d: v14} = require__blake.G2s(v2, v6, v10, v14, msg[offset + s[j++]]));
|
|
164
|
+
({a: v3, b: v7, c: v11, d: v15} = require__blake.G1s(v3, v7, v11, v15, msg[offset + s[j++]]));
|
|
165
|
+
({a: v3, b: v7, c: v11, d: v15} = require__blake.G2s(v3, v7, v11, v15, msg[offset + s[j++]]));
|
|
166
|
+
({a: v0, b: v5, c: v10, d: v15} = require__blake.G1s(v0, v5, v10, v15, msg[offset + s[j++]]));
|
|
167
|
+
({a: v0, b: v5, c: v10, d: v15} = require__blake.G2s(v0, v5, v10, v15, msg[offset + s[j++]]));
|
|
168
|
+
({a: v1, b: v6, c: v11, d: v12} = require__blake.G1s(v1, v6, v11, v12, msg[offset + s[j++]]));
|
|
169
|
+
({a: v1, b: v6, c: v11, d: v12} = require__blake.G2s(v1, v6, v11, v12, msg[offset + s[j++]]));
|
|
170
|
+
({a: v2, b: v7, c: v8, d: v13} = require__blake.G1s(v2, v7, v8, v13, msg[offset + s[j++]]));
|
|
171
|
+
({a: v2, b: v7, c: v8, d: v13} = require__blake.G2s(v2, v7, v8, v13, msg[offset + s[j++]]));
|
|
172
|
+
({a: v3, b: v4, c: v9, d: v14} = require__blake.G1s(v3, v4, v9, v14, msg[offset + s[j++]]));
|
|
173
|
+
({a: v3, b: v4, c: v9, d: v14} = require__blake.G2s(v3, v4, v9, v14, msg[offset + s[j++]]));
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
v0,
|
|
177
|
+
v1,
|
|
178
|
+
v2,
|
|
179
|
+
v3,
|
|
180
|
+
v4,
|
|
181
|
+
v5,
|
|
182
|
+
v6,
|
|
183
|
+
v7,
|
|
184
|
+
v8,
|
|
185
|
+
v9,
|
|
186
|
+
v10,
|
|
187
|
+
v11,
|
|
188
|
+
v12,
|
|
189
|
+
v13,
|
|
190
|
+
v14,
|
|
191
|
+
v15
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
const B2S_IV = /* @__PURE__ */ require__md.SHA256_IV.slice();
|
|
195
|
+
/** Internal blake2s hash class. */
|
|
196
|
+
var _BLAKE2s = class extends _BLAKE2 {
|
|
197
|
+
v0 = B2S_IV[0] | 0;
|
|
198
|
+
v1 = B2S_IV[1] | 0;
|
|
199
|
+
v2 = B2S_IV[2] | 0;
|
|
200
|
+
v3 = B2S_IV[3] | 0;
|
|
201
|
+
v4 = B2S_IV[4] | 0;
|
|
202
|
+
v5 = B2S_IV[5] | 0;
|
|
203
|
+
v6 = B2S_IV[6] | 0;
|
|
204
|
+
v7 = B2S_IV[7] | 0;
|
|
205
|
+
constructor(opts = {}) {
|
|
206
|
+
const olen = opts.dkLen === void 0 ? 32 : opts.dkLen;
|
|
207
|
+
super(64, olen);
|
|
208
|
+
checkBlake2Opts(olen, opts, 32, 8, 8);
|
|
209
|
+
let { key, personalization, salt } = opts;
|
|
210
|
+
let keyLength = 0;
|
|
211
|
+
if (key !== void 0) {
|
|
212
|
+
require_utils.abytes(key, void 0, "key");
|
|
213
|
+
keyLength = key.length;
|
|
214
|
+
}
|
|
215
|
+
this.v0 ^= this.outputLen | keyLength << 8 | 16842752;
|
|
216
|
+
if (salt !== void 0) {
|
|
217
|
+
require_utils.abytes(salt, void 0, "salt");
|
|
218
|
+
const slt = require_utils.u32(salt);
|
|
219
|
+
this.v4 ^= require_utils.swap8IfBE(slt[0]);
|
|
220
|
+
this.v5 ^= require_utils.swap8IfBE(slt[1]);
|
|
221
|
+
}
|
|
222
|
+
if (personalization !== void 0) {
|
|
223
|
+
require_utils.abytes(personalization, void 0, "personalization");
|
|
224
|
+
const pers = require_utils.u32(personalization);
|
|
225
|
+
this.v6 ^= require_utils.swap8IfBE(pers[0]);
|
|
226
|
+
this.v7 ^= require_utils.swap8IfBE(pers[1]);
|
|
227
|
+
}
|
|
228
|
+
if (key !== void 0) {
|
|
229
|
+
const tmp = new Uint8Array(this.blockLen);
|
|
230
|
+
tmp.set(key);
|
|
231
|
+
this.update(tmp);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
get() {
|
|
235
|
+
const { v0, v1, v2, v3, v4, v5, v6, v7 } = this;
|
|
236
|
+
return [
|
|
237
|
+
v0,
|
|
238
|
+
v1,
|
|
239
|
+
v2,
|
|
240
|
+
v3,
|
|
241
|
+
v4,
|
|
242
|
+
v5,
|
|
243
|
+
v6,
|
|
244
|
+
v7
|
|
245
|
+
];
|
|
246
|
+
}
|
|
247
|
+
set(v0, v1, v2, v3, v4, v5, v6, v7) {
|
|
248
|
+
this.v0 = v0 | 0;
|
|
249
|
+
this.v1 = v1 | 0;
|
|
250
|
+
this.v2 = v2 | 0;
|
|
251
|
+
this.v3 = v3 | 0;
|
|
252
|
+
this.v4 = v4 | 0;
|
|
253
|
+
this.v5 = v5 | 0;
|
|
254
|
+
this.v6 = v6 | 0;
|
|
255
|
+
this.v7 = v7 | 0;
|
|
256
|
+
}
|
|
257
|
+
compress(msg, offset, isLast) {
|
|
258
|
+
const { h, l } = require__u64.fromBig(BigInt(this.length));
|
|
259
|
+
const { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 } = compress(require__blake.BSIGMA, offset, msg, 10, this.v0, this.v1, this.v2, this.v3, this.v4, this.v5, this.v6, this.v7, B2S_IV[0], B2S_IV[1], B2S_IV[2], B2S_IV[3], l ^ B2S_IV[4], h ^ B2S_IV[5], isLast ? ~B2S_IV[6] : B2S_IV[6], B2S_IV[7]);
|
|
260
|
+
this.v0 ^= v0 ^ v8;
|
|
261
|
+
this.v1 ^= v1 ^ v9;
|
|
262
|
+
this.v2 ^= v2 ^ v10;
|
|
263
|
+
this.v3 ^= v3 ^ v11;
|
|
264
|
+
this.v4 ^= v4 ^ v12;
|
|
265
|
+
this.v5 ^= v5 ^ v13;
|
|
266
|
+
this.v6 ^= v6 ^ v14;
|
|
267
|
+
this.v7 ^= v7 ^ v15;
|
|
268
|
+
}
|
|
269
|
+
destroy() {
|
|
270
|
+
this.destroyed = true;
|
|
271
|
+
require_utils.clean(this.buffer32);
|
|
272
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
/**
|
|
276
|
+
* Blake2s hash function. Focuses on 8-bit to 32-bit platforms. 1.5x faster than blake2b in JS.
|
|
277
|
+
* @param msg - message that would be hashed
|
|
278
|
+
* @param opts - Optional output, MAC, salt, and personalization settings.
|
|
279
|
+
* `dkLen` must be 1..32 bytes; `salt` and `personalization`, if present,
|
|
280
|
+
* must be 8 bytes each. See {@link Blake2Opts}.
|
|
281
|
+
* @returns Digest bytes.
|
|
282
|
+
* @example
|
|
283
|
+
* Hash a message with Blake2s.
|
|
284
|
+
* ```ts
|
|
285
|
+
* blake2s(new Uint8Array([97, 98, 99]));
|
|
286
|
+
* ```
|
|
287
|
+
*/
|
|
288
|
+
const blake2s = /* @__PURE__ */ require_utils.createHasher((opts) => new _BLAKE2s(opts));
|
|
289
|
+
|
|
290
|
+
//#endregion
|
|
291
|
+
exports.blake2s = blake2s;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
const require_utils = require('./utils.cjs');
|
|
2
|
+
const require_hmac = require('./hmac.cjs');
|
|
3
|
+
|
|
4
|
+
//#region ../../node_modules/@noble/hashes/hkdf.js
|
|
5
|
+
/**
|
|
6
|
+
* HKDF (RFC 5869): extract + expand in one step.
|
|
7
|
+
* See {@link https://soatok.blog/2021/11/17/understanding-hkdf/}.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* HKDF-extract from spec. Less important part. `HKDF-Extract(IKM, salt) -> PRK`
|
|
12
|
+
* Arguments position differs from spec (IKM is first one, since it is not optional)
|
|
13
|
+
* Local validation only checks `hash`; `ikm` / `salt` byte validation is delegated to `hmac()`.
|
|
14
|
+
* @param hash - hash function that would be used (e.g. sha256)
|
|
15
|
+
* @param ikm - input keying material, the initial key
|
|
16
|
+
* @param salt - optional salt value (a non-secret random value)
|
|
17
|
+
* @returns Pseudorandom key derived from input keying material.
|
|
18
|
+
* @example
|
|
19
|
+
* Run the HKDF extract step.
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { extract } from '@noble/hashes/hkdf.js';
|
|
22
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
23
|
+
* extract(sha256, new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6]));
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
function extract(hash, ikm, salt) {
|
|
27
|
+
require_utils.ahash(hash);
|
|
28
|
+
if (salt === void 0) salt = new Uint8Array(hash.outputLen);
|
|
29
|
+
return require_hmac.hmac(hash, salt, ikm);
|
|
30
|
+
}
|
|
31
|
+
const HKDF_COUNTER = /* @__PURE__ */ Uint8Array.of(0);
|
|
32
|
+
const EMPTY_BUFFER = /* @__PURE__ */ Uint8Array.of();
|
|
33
|
+
/**
|
|
34
|
+
* HKDF-expand from the spec. The most important part. `HKDF-Expand(PRK, info, L) -> OKM`
|
|
35
|
+
* @param hash - hash function that would be used (e.g. sha256)
|
|
36
|
+
* @param prk - a pseudorandom key of at least HashLen octets
|
|
37
|
+
* (usually, the output from the extract step)
|
|
38
|
+
* @param info - optional context and application specific information (can be a zero-length string)
|
|
39
|
+
* @param length - length of output keying material in bytes.
|
|
40
|
+
* RFC 5869 §2.3 allows `0..255*HashLen`, so `0` returns an empty OKM.
|
|
41
|
+
* @returns Output keying material with the requested length.
|
|
42
|
+
* @throws If the requested output length exceeds the HKDF limit
|
|
43
|
+
* for the selected hash. {@link Error}
|
|
44
|
+
* @example
|
|
45
|
+
* Run the HKDF expand step.
|
|
46
|
+
* ```ts
|
|
47
|
+
* import { expand } from '@noble/hashes/hkdf.js';
|
|
48
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
49
|
+
* expand(sha256, new Uint8Array(32), new Uint8Array([1, 2, 3]), 16);
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
function expand(hash, prk, info, length = 32) {
|
|
53
|
+
require_utils.ahash(hash);
|
|
54
|
+
require_utils.anumber(length, "length");
|
|
55
|
+
require_utils.abytes(prk, void 0, "prk");
|
|
56
|
+
const olen = hash.outputLen;
|
|
57
|
+
if (prk.length < olen) throw new Error("\"prk\" must be at least HashLen octets");
|
|
58
|
+
if (length > 255 * olen) throw new Error("Length must be <= 255*HashLen");
|
|
59
|
+
const blocks = Math.ceil(length / olen);
|
|
60
|
+
if (info === void 0) info = EMPTY_BUFFER;
|
|
61
|
+
else require_utils.abytes(info, void 0, "info");
|
|
62
|
+
const okm = new Uint8Array(blocks * olen);
|
|
63
|
+
const HMAC = require_hmac.hmac.create(hash, prk);
|
|
64
|
+
const HMACTmp = HMAC._cloneInto();
|
|
65
|
+
const T = new Uint8Array(HMAC.outputLen);
|
|
66
|
+
for (let counter = 0; counter < blocks; counter++) {
|
|
67
|
+
HKDF_COUNTER[0] = counter + 1;
|
|
68
|
+
HMACTmp.update(counter === 0 ? EMPTY_BUFFER : T).update(info).update(HKDF_COUNTER).digestInto(T);
|
|
69
|
+
okm.set(T, olen * counter);
|
|
70
|
+
HMAC._cloneInto(HMACTmp);
|
|
71
|
+
}
|
|
72
|
+
HMAC.destroy();
|
|
73
|
+
HMACTmp.destroy();
|
|
74
|
+
require_utils.clean(T, HKDF_COUNTER);
|
|
75
|
+
return okm.slice(0, length);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* HKDF (RFC 5869): derive keys from an initial input.
|
|
79
|
+
* Combines hkdf_extract + hkdf_expand in one step
|
|
80
|
+
* @param hash - hash function that would be used (e.g. sha256)
|
|
81
|
+
* @param ikm - input keying material, the initial key
|
|
82
|
+
* @param salt - optional salt value (a non-secret random value)
|
|
83
|
+
* @param info - optional context and application specific information bytes
|
|
84
|
+
* @param length - length of output keying material in bytes.
|
|
85
|
+
* RFC 5869 §2.3 allows `0..255*HashLen`, so `0` returns an empty OKM.
|
|
86
|
+
* @returns Output keying material derived from the input key.
|
|
87
|
+
* @throws If the requested output length exceeds the HKDF limit
|
|
88
|
+
* for the selected hash. {@link Error}
|
|
89
|
+
* @example
|
|
90
|
+
* HKDF (RFC 5869): derive keys from an initial input.
|
|
91
|
+
* ```ts
|
|
92
|
+
* import { hkdf } from '@noble/hashes/hkdf.js';
|
|
93
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
94
|
+
* import { randomBytes, utf8ToBytes } from '@noble/hashes/utils.js';
|
|
95
|
+
* const inputKey = randomBytes(32);
|
|
96
|
+
* const salt = randomBytes(32);
|
|
97
|
+
* const info = utf8ToBytes('application-key');
|
|
98
|
+
* const okm = hkdf(sha256, inputKey, salt, info, 32);
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
const hkdf = (hash, ikm, salt, info, length) => expand(hash, extract(hash, ikm, salt), info, length);
|
|
102
|
+
|
|
103
|
+
//#endregion
|
|
104
|
+
exports.hkdf = hkdf;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const require_utils = require('./utils.cjs');
|
|
2
|
+
|
|
3
|
+
//#region ../../node_modules/@noble/hashes/hmac.js
|
|
4
|
+
/**
|
|
5
|
+
* HMAC: RFC2104 message authentication code.
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Internal class for HMAC.
|
|
10
|
+
* Accepts any byte key, although RFC 2104 §3 recommends keys at least
|
|
11
|
+
* `HashLen` bytes long.
|
|
12
|
+
*/
|
|
13
|
+
var _HMAC = class {
|
|
14
|
+
oHash;
|
|
15
|
+
iHash;
|
|
16
|
+
blockLen;
|
|
17
|
+
outputLen;
|
|
18
|
+
canXOF = false;
|
|
19
|
+
finished = false;
|
|
20
|
+
destroyed = false;
|
|
21
|
+
constructor(hash, key) {
|
|
22
|
+
require_utils.ahash(hash);
|
|
23
|
+
require_utils.abytes(key, void 0, "key");
|
|
24
|
+
this.iHash = hash.create();
|
|
25
|
+
if (typeof this.iHash.update !== "function") throw new Error("Expected instance of class which extends utils.Hash");
|
|
26
|
+
this.blockLen = this.iHash.blockLen;
|
|
27
|
+
this.outputLen = this.iHash.outputLen;
|
|
28
|
+
const blockLen = this.blockLen;
|
|
29
|
+
const pad = new Uint8Array(blockLen);
|
|
30
|
+
pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
|
|
31
|
+
for (let i = 0; i < pad.length; i++) pad[i] ^= 54;
|
|
32
|
+
this.iHash.update(pad);
|
|
33
|
+
this.oHash = hash.create();
|
|
34
|
+
for (let i = 0; i < pad.length; i++) pad[i] ^= 106;
|
|
35
|
+
this.oHash.update(pad);
|
|
36
|
+
require_utils.clean(pad);
|
|
37
|
+
}
|
|
38
|
+
update(buf) {
|
|
39
|
+
require_utils.aexists(this);
|
|
40
|
+
this.iHash.update(buf);
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
digestInto(out) {
|
|
44
|
+
require_utils.aexists(this);
|
|
45
|
+
require_utils.aoutput(out, this);
|
|
46
|
+
this.finished = true;
|
|
47
|
+
const buf = out.subarray(0, this.outputLen);
|
|
48
|
+
this.iHash.digestInto(buf);
|
|
49
|
+
this.oHash.update(buf);
|
|
50
|
+
this.oHash.digestInto(buf);
|
|
51
|
+
this.destroy();
|
|
52
|
+
}
|
|
53
|
+
digest() {
|
|
54
|
+
const out = new Uint8Array(this.oHash.outputLen);
|
|
55
|
+
this.digestInto(out);
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
_cloneInto(to) {
|
|
59
|
+
to ||= Object.create(Object.getPrototypeOf(this), {});
|
|
60
|
+
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
|
61
|
+
to = to;
|
|
62
|
+
to.finished = finished;
|
|
63
|
+
to.destroyed = destroyed;
|
|
64
|
+
to.blockLen = blockLen;
|
|
65
|
+
to.outputLen = outputLen;
|
|
66
|
+
to.oHash = oHash._cloneInto(to.oHash);
|
|
67
|
+
to.iHash = iHash._cloneInto(to.iHash);
|
|
68
|
+
return to;
|
|
69
|
+
}
|
|
70
|
+
clone() {
|
|
71
|
+
return this._cloneInto();
|
|
72
|
+
}
|
|
73
|
+
destroy() {
|
|
74
|
+
this.destroyed = true;
|
|
75
|
+
this.oHash.destroy();
|
|
76
|
+
this.iHash.destroy();
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
const hmac = /* @__PURE__ */ (() => {
|
|
80
|
+
const hmac_ = ((hash, key, message) => new _HMAC(hash, key).update(message).digest());
|
|
81
|
+
hmac_.create = (hash, key) => new _HMAC(hash, key);
|
|
82
|
+
return hmac_;
|
|
83
|
+
})();
|
|
84
|
+
|
|
85
|
+
//#endregion
|
|
86
|
+
exports.hmac = hmac;
|