@didcid/cipher 0.1.3 → 0.2.1

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.
@@ -0,0 +1,1652 @@
1
+ 'use strict';
2
+
3
+ const crypto = typeof globalThis === 'object' && 'crypto' in globalThis ? globalThis.crypto : undefined;
4
+
5
+ /**
6
+ * Utilities for hex, bytes, CSPRNG.
7
+ * @module
8
+ */
9
+ /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
10
+ // We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
11
+ // node.js versions earlier than v19 don't declare it in global scope.
12
+ // For node.js, package.json#exports field mapping rewrites import
13
+ // from `crypto` to `cryptoNode`, which imports native module.
14
+ // Makes the utils un-importable in browsers without a bundler.
15
+ // Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
16
+ /** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
17
+ function isBytes$2(a) {
18
+ return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
19
+ }
20
+ /** Asserts something is positive integer. */
21
+ function anumber(n) {
22
+ if (!Number.isSafeInteger(n) || n < 0)
23
+ throw new Error('positive integer expected, got ' + n);
24
+ }
25
+ /** Asserts something is Uint8Array. */
26
+ function abytes(b, ...lengths) {
27
+ if (!isBytes$2(b))
28
+ throw new Error('Uint8Array expected');
29
+ if (lengths.length > 0 && !lengths.includes(b.length))
30
+ throw new Error('Uint8Array expected of length ' + lengths + ', got length=' + b.length);
31
+ }
32
+ /** Asserts something is hash */
33
+ function ahash(h) {
34
+ if (typeof h !== 'function' || typeof h.create !== 'function')
35
+ throw new Error('Hash should be wrapped by utils.createHasher');
36
+ anumber(h.outputLen);
37
+ anumber(h.blockLen);
38
+ }
39
+ /** Asserts a hash instance has not been destroyed / finished */
40
+ function aexists(instance, checkFinished = true) {
41
+ if (instance.destroyed)
42
+ throw new Error('Hash instance has been destroyed');
43
+ if (checkFinished && instance.finished)
44
+ throw new Error('Hash#digest() has already been called');
45
+ }
46
+ /** Asserts output is properly-sized byte array */
47
+ function aoutput(out, instance) {
48
+ abytes(out);
49
+ const min = instance.outputLen;
50
+ if (out.length < min) {
51
+ throw new Error('digestInto() expects output buffer of length at least ' + min);
52
+ }
53
+ }
54
+ /** Zeroize a byte array. Warning: JS provides no guarantees. */
55
+ function clean(...arrays) {
56
+ for (let i = 0; i < arrays.length; i++) {
57
+ arrays[i].fill(0);
58
+ }
59
+ }
60
+ /** Create DataView of an array for easy byte-level manipulation. */
61
+ function createView$1(arr) {
62
+ return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
63
+ }
64
+ /** The rotate right (circular right shift) operation for uint32 */
65
+ function rotr(word, shift) {
66
+ return (word << (32 - shift)) | (word >>> shift);
67
+ }
68
+ /**
69
+ * There is no setImmediate in browser and setTimeout is slow.
70
+ * Call of async fn will return Promise, which will be fullfiled only on
71
+ * next scheduler queue processing step and this is exactly what we need.
72
+ */
73
+ const nextTick = async () => { };
74
+ /** Returns control to thread each 'tick' ms to avoid blocking. */
75
+ async function asyncLoop(iters, tick, cb) {
76
+ let ts = Date.now();
77
+ for (let i = 0; i < iters; i++) {
78
+ cb(i);
79
+ // Date.now() is not monotonic, so in case if clock goes backwards we return return control too
80
+ const diff = Date.now() - ts;
81
+ if (diff >= 0 && diff < tick)
82
+ continue;
83
+ await nextTick();
84
+ ts += diff;
85
+ }
86
+ }
87
+ /**
88
+ * Converts string to bytes using UTF8 encoding.
89
+ * @example utf8ToBytes('abc') // Uint8Array.from([97, 98, 99])
90
+ */
91
+ function utf8ToBytes$1(str) {
92
+ if (typeof str !== 'string')
93
+ throw new Error('string expected');
94
+ return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
95
+ }
96
+ /**
97
+ * Normalizes (non-hex) string or Uint8Array to Uint8Array.
98
+ * Warning: when Uint8Array is passed, it would NOT get copied.
99
+ * Keep in mind for future mutable operations.
100
+ */
101
+ function toBytes$1(data) {
102
+ if (typeof data === 'string')
103
+ data = utf8ToBytes$1(data);
104
+ abytes(data);
105
+ return data;
106
+ }
107
+ /**
108
+ * Helper for KDFs: consumes uint8array or string.
109
+ * When string is passed, does utf8 decoding, using TextDecoder.
110
+ */
111
+ function kdfInputToBytes(data) {
112
+ if (typeof data === 'string')
113
+ data = utf8ToBytes$1(data);
114
+ abytes(data);
115
+ return data;
116
+ }
117
+ function checkOpts$1(defaults, opts) {
118
+ if (opts !== undefined && {}.toString.call(opts) !== '[object Object]')
119
+ throw new Error('options should be object or undefined');
120
+ const merged = Object.assign(defaults, opts);
121
+ return merged;
122
+ }
123
+ /** For runtime check if class implements interface */
124
+ class Hash {
125
+ }
126
+ /** Wraps hash function, creating an interface on top of it */
127
+ function createHasher(hashCons) {
128
+ const hashC = (msg) => hashCons().update(toBytes$1(msg)).digest();
129
+ const tmp = hashCons();
130
+ hashC.outputLen = tmp.outputLen;
131
+ hashC.blockLen = tmp.blockLen;
132
+ hashC.create = () => hashCons();
133
+ return hashC;
134
+ }
135
+ /** Cryptographically secure PRNG. Uses internal OS-level `crypto.getRandomValues`. */
136
+ function randomBytes(bytesLength = 32) {
137
+ if (crypto && typeof crypto.getRandomValues === 'function') {
138
+ return crypto.getRandomValues(new Uint8Array(bytesLength));
139
+ }
140
+ // Legacy Node.js compatibility
141
+ if (crypto && typeof crypto.randomBytes === 'function') {
142
+ return Uint8Array.from(crypto.randomBytes(bytesLength));
143
+ }
144
+ throw new Error('crypto.getRandomValues must be defined');
145
+ }
146
+
147
+ /**
148
+ * HMAC: RFC2104 message authentication code.
149
+ * @module
150
+ */
151
+ class HMAC extends Hash {
152
+ constructor(hash, _key) {
153
+ super();
154
+ this.finished = false;
155
+ this.destroyed = false;
156
+ ahash(hash);
157
+ const key = toBytes$1(_key);
158
+ this.iHash = hash.create();
159
+ if (typeof this.iHash.update !== 'function')
160
+ throw new Error('Expected instance of class which extends utils.Hash');
161
+ this.blockLen = this.iHash.blockLen;
162
+ this.outputLen = this.iHash.outputLen;
163
+ const blockLen = this.blockLen;
164
+ const pad = new Uint8Array(blockLen);
165
+ // blockLen can be bigger than outputLen
166
+ pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
167
+ for (let i = 0; i < pad.length; i++)
168
+ pad[i] ^= 0x36;
169
+ this.iHash.update(pad);
170
+ // By doing update (processing of first block) of outer hash here we can re-use it between multiple calls via clone
171
+ this.oHash = hash.create();
172
+ // Undo internal XOR && apply outer XOR
173
+ for (let i = 0; i < pad.length; i++)
174
+ pad[i] ^= 0x36 ^ 0x5c;
175
+ this.oHash.update(pad);
176
+ clean(pad);
177
+ }
178
+ update(buf) {
179
+ aexists(this);
180
+ this.iHash.update(buf);
181
+ return this;
182
+ }
183
+ digestInto(out) {
184
+ aexists(this);
185
+ abytes(out, this.outputLen);
186
+ this.finished = true;
187
+ this.iHash.digestInto(out);
188
+ this.oHash.update(out);
189
+ this.oHash.digestInto(out);
190
+ this.destroy();
191
+ }
192
+ digest() {
193
+ const out = new Uint8Array(this.oHash.outputLen);
194
+ this.digestInto(out);
195
+ return out;
196
+ }
197
+ _cloneInto(to) {
198
+ // Create new instance without calling constructor since key already in state and we don't know it.
199
+ to || (to = Object.create(Object.getPrototypeOf(this), {}));
200
+ const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
201
+ to = to;
202
+ to.finished = finished;
203
+ to.destroyed = destroyed;
204
+ to.blockLen = blockLen;
205
+ to.outputLen = outputLen;
206
+ to.oHash = oHash._cloneInto(to.oHash);
207
+ to.iHash = iHash._cloneInto(to.iHash);
208
+ return to;
209
+ }
210
+ clone() {
211
+ return this._cloneInto();
212
+ }
213
+ destroy() {
214
+ this.destroyed = true;
215
+ this.oHash.destroy();
216
+ this.iHash.destroy();
217
+ }
218
+ }
219
+ /**
220
+ * HMAC: RFC2104 message authentication code.
221
+ * @param hash - function that would be used e.g. sha256
222
+ * @param key - message key
223
+ * @param message - message data
224
+ * @example
225
+ * import { hmac } from '@noble/hashes/hmac';
226
+ * import { sha256 } from '@noble/hashes/sha2';
227
+ * const mac1 = hmac(sha256, 'key', 'message');
228
+ */
229
+ const hmac = (hash, key, message) => new HMAC(hash, key).update(message).digest();
230
+ hmac.create = (hash, key) => new HMAC(hash, key);
231
+
232
+ /**
233
+ * Internal Merkle-Damgard hash utils.
234
+ * @module
235
+ */
236
+ /** Polyfill for Safari 14. https://caniuse.com/mdn-javascript_builtins_dataview_setbiguint64 */
237
+ function setBigUint64$1(view, byteOffset, value, isLE) {
238
+ if (typeof view.setBigUint64 === 'function')
239
+ return view.setBigUint64(byteOffset, value, isLE);
240
+ const _32n = BigInt(32);
241
+ const _u32_max = BigInt(0xffffffff);
242
+ const wh = Number((value >> _32n) & _u32_max);
243
+ const wl = Number(value & _u32_max);
244
+ const h = isLE ? 4 : 0;
245
+ const l = isLE ? 0 : 4;
246
+ view.setUint32(byteOffset + h, wh, isLE);
247
+ view.setUint32(byteOffset + l, wl, isLE);
248
+ }
249
+ /** Choice: a ? b : c */
250
+ function Chi(a, b, c) {
251
+ return (a & b) ^ (~a & c);
252
+ }
253
+ /** Majority function, true if any two inputs is true. */
254
+ function Maj(a, b, c) {
255
+ return (a & b) ^ (a & c) ^ (b & c);
256
+ }
257
+ /**
258
+ * Merkle-Damgard hash construction base class.
259
+ * Could be used to create MD5, RIPEMD, SHA1, SHA2.
260
+ */
261
+ class HashMD extends Hash {
262
+ constructor(blockLen, outputLen, padOffset, isLE) {
263
+ super();
264
+ this.finished = false;
265
+ this.length = 0;
266
+ this.pos = 0;
267
+ this.destroyed = false;
268
+ this.blockLen = blockLen;
269
+ this.outputLen = outputLen;
270
+ this.padOffset = padOffset;
271
+ this.isLE = isLE;
272
+ this.buffer = new Uint8Array(blockLen);
273
+ this.view = createView$1(this.buffer);
274
+ }
275
+ update(data) {
276
+ aexists(this);
277
+ data = toBytes$1(data);
278
+ abytes(data);
279
+ const { view, buffer, blockLen } = this;
280
+ const len = data.length;
281
+ for (let pos = 0; pos < len;) {
282
+ const take = Math.min(blockLen - this.pos, len - pos);
283
+ // Fast path: we have at least one block in input, cast it to view and process
284
+ if (take === blockLen) {
285
+ const dataView = createView$1(data);
286
+ for (; blockLen <= len - pos; pos += blockLen)
287
+ this.process(dataView, pos);
288
+ continue;
289
+ }
290
+ buffer.set(data.subarray(pos, pos + take), this.pos);
291
+ this.pos += take;
292
+ pos += take;
293
+ if (this.pos === blockLen) {
294
+ this.process(view, 0);
295
+ this.pos = 0;
296
+ }
297
+ }
298
+ this.length += data.length;
299
+ this.roundClean();
300
+ return this;
301
+ }
302
+ digestInto(out) {
303
+ aexists(this);
304
+ aoutput(out, this);
305
+ this.finished = true;
306
+ // Padding
307
+ // We can avoid allocation of buffer for padding completely if it
308
+ // was previously not allocated here. But it won't change performance.
309
+ const { buffer, view, blockLen, isLE } = this;
310
+ let { pos } = this;
311
+ // append the bit '1' to the message
312
+ buffer[pos++] = 0b10000000;
313
+ clean(this.buffer.subarray(pos));
314
+ // we have less than padOffset left in buffer, so we cannot put length in
315
+ // current block, need process it and pad again
316
+ if (this.padOffset > blockLen - pos) {
317
+ this.process(view, 0);
318
+ pos = 0;
319
+ }
320
+ // Pad until full block byte with zeros
321
+ for (let i = pos; i < blockLen; i++)
322
+ buffer[i] = 0;
323
+ // Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that
324
+ // You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.
325
+ // So we just write lowest 64 bits of that value.
326
+ setBigUint64$1(view, blockLen - 8, BigInt(this.length * 8), isLE);
327
+ this.process(view, 0);
328
+ const oview = createView$1(out);
329
+ const len = this.outputLen;
330
+ // NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT
331
+ if (len % 4)
332
+ throw new Error('_sha2: outputLen should be aligned to 32bit');
333
+ const outLen = len / 4;
334
+ const state = this.get();
335
+ if (outLen > state.length)
336
+ throw new Error('_sha2: outputLen bigger than state');
337
+ for (let i = 0; i < outLen; i++)
338
+ oview.setUint32(4 * i, state[i], isLE);
339
+ }
340
+ digest() {
341
+ const { buffer, outputLen } = this;
342
+ this.digestInto(buffer);
343
+ const res = buffer.slice(0, outputLen);
344
+ this.destroy();
345
+ return res;
346
+ }
347
+ _cloneInto(to) {
348
+ to || (to = new this.constructor());
349
+ to.set(...this.get());
350
+ const { blockLen, buffer, length, finished, destroyed, pos } = this;
351
+ to.destroyed = destroyed;
352
+ to.finished = finished;
353
+ to.length = length;
354
+ to.pos = pos;
355
+ if (length % blockLen)
356
+ to.buffer.set(buffer);
357
+ return to;
358
+ }
359
+ clone() {
360
+ return this._cloneInto();
361
+ }
362
+ }
363
+ /**
364
+ * Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
365
+ * Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
366
+ */
367
+ /** Initial SHA256 state. Bits 0..32 of frac part of sqrt of primes 2..19 */
368
+ const SHA256_IV = /* @__PURE__ */ Uint32Array.from([
369
+ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
370
+ ]);
371
+ /** Initial SHA512 state. Bits 0..64 of frac part of sqrt of primes 2..19 */
372
+ const SHA512_IV = /* @__PURE__ */ Uint32Array.from([
373
+ 0x6a09e667, 0xf3bcc908, 0xbb67ae85, 0x84caa73b, 0x3c6ef372, 0xfe94f82b, 0xa54ff53a, 0x5f1d36f1,
374
+ 0x510e527f, 0xade682d1, 0x9b05688c, 0x2b3e6c1f, 0x1f83d9ab, 0xfb41bd6b, 0x5be0cd19, 0x137e2179,
375
+ ]);
376
+
377
+ /**
378
+ * Internal helpers for u64. BigUint64Array is too slow as per 2025, so we implement it using Uint32Array.
379
+ * @todo re-check https://issues.chromium.org/issues/42212588
380
+ * @module
381
+ */
382
+ const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
383
+ const _32n = /* @__PURE__ */ BigInt(32);
384
+ function fromBig(n, le = false) {
385
+ if (le)
386
+ return { h: Number(n & U32_MASK64), l: Number((n >> _32n) & U32_MASK64) };
387
+ return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
388
+ }
389
+ function split(lst, le = false) {
390
+ const len = lst.length;
391
+ let Ah = new Uint32Array(len);
392
+ let Al = new Uint32Array(len);
393
+ for (let i = 0; i < len; i++) {
394
+ const { h, l } = fromBig(lst[i], le);
395
+ [Ah[i], Al[i]] = [h, l];
396
+ }
397
+ return [Ah, Al];
398
+ }
399
+ // for Shift in [0, 32)
400
+ const shrSH = (h, _l, s) => h >>> s;
401
+ const shrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
402
+ // Right rotate for Shift in [1, 32)
403
+ const rotrSH = (h, l, s) => (h >>> s) | (l << (32 - s));
404
+ const rotrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
405
+ // Right rotate for Shift in (32, 64), NOTE: 32 is special case.
406
+ const rotrBH = (h, l, s) => (h << (64 - s)) | (l >>> (s - 32));
407
+ const rotrBL = (h, l, s) => (h >>> (s - 32)) | (l << (64 - s));
408
+ // JS uses 32-bit signed integers for bitwise operations which means we cannot
409
+ // simple take carry out of low bit sum by shift, we need to use division.
410
+ function add(Ah, Al, Bh, Bl) {
411
+ const l = (Al >>> 0) + (Bl >>> 0);
412
+ return { h: (Ah + Bh + ((l / 2 ** 32) | 0)) | 0, l: l | 0 };
413
+ }
414
+ // Addition with more than 2 elements
415
+ const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
416
+ const add3H = (low, Ah, Bh, Ch) => (Ah + Bh + Ch + ((low / 2 ** 32) | 0)) | 0;
417
+ const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
418
+ const add4H = (low, Ah, Bh, Ch, Dh) => (Ah + Bh + Ch + Dh + ((low / 2 ** 32) | 0)) | 0;
419
+ const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
420
+ const add5H = (low, Ah, Bh, Ch, Dh, Eh) => (Ah + Bh + Ch + Dh + Eh + ((low / 2 ** 32) | 0)) | 0;
421
+
422
+ /**
423
+ * SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
424
+ * SHA256 is the fastest hash implementable in JS, even faster than Blake3.
425
+ * Check out [RFC 4634](https://datatracker.ietf.org/doc/html/rfc4634) and
426
+ * [FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
427
+ * @module
428
+ */
429
+ /**
430
+ * Round constants:
431
+ * First 32 bits of fractional parts of the cube roots of the first 64 primes 2..311)
432
+ */
433
+ // prettier-ignore
434
+ const SHA256_K = /* @__PURE__ */ Uint32Array.from([
435
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
436
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
437
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
438
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
439
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
440
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
441
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
442
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
443
+ ]);
444
+ /** Reusable temporary buffer. "W" comes straight from spec. */
445
+ const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
446
+ class SHA256 extends HashMD {
447
+ constructor(outputLen = 32) {
448
+ super(64, outputLen, 8, false);
449
+ // We cannot use array here since array allows indexing by variable
450
+ // which means optimizer/compiler cannot use registers.
451
+ this.A = SHA256_IV[0] | 0;
452
+ this.B = SHA256_IV[1] | 0;
453
+ this.C = SHA256_IV[2] | 0;
454
+ this.D = SHA256_IV[3] | 0;
455
+ this.E = SHA256_IV[4] | 0;
456
+ this.F = SHA256_IV[5] | 0;
457
+ this.G = SHA256_IV[6] | 0;
458
+ this.H = SHA256_IV[7] | 0;
459
+ }
460
+ get() {
461
+ const { A, B, C, D, E, F, G, H } = this;
462
+ return [A, B, C, D, E, F, G, H];
463
+ }
464
+ // prettier-ignore
465
+ set(A, B, C, D, E, F, G, H) {
466
+ this.A = A | 0;
467
+ this.B = B | 0;
468
+ this.C = C | 0;
469
+ this.D = D | 0;
470
+ this.E = E | 0;
471
+ this.F = F | 0;
472
+ this.G = G | 0;
473
+ this.H = H | 0;
474
+ }
475
+ process(view, offset) {
476
+ // Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
477
+ for (let i = 0; i < 16; i++, offset += 4)
478
+ SHA256_W[i] = view.getUint32(offset, false);
479
+ for (let i = 16; i < 64; i++) {
480
+ const W15 = SHA256_W[i - 15];
481
+ const W2 = SHA256_W[i - 2];
482
+ const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
483
+ const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
484
+ SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
485
+ }
486
+ // Compression function main loop, 64 rounds
487
+ let { A, B, C, D, E, F, G, H } = this;
488
+ for (let i = 0; i < 64; i++) {
489
+ const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
490
+ const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
491
+ const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
492
+ const T2 = (sigma0 + Maj(A, B, C)) | 0;
493
+ H = G;
494
+ G = F;
495
+ F = E;
496
+ E = (D + T1) | 0;
497
+ D = C;
498
+ C = B;
499
+ B = A;
500
+ A = (T1 + T2) | 0;
501
+ }
502
+ // Add the compressed chunk to the current hash value
503
+ A = (A + this.A) | 0;
504
+ B = (B + this.B) | 0;
505
+ C = (C + this.C) | 0;
506
+ D = (D + this.D) | 0;
507
+ E = (E + this.E) | 0;
508
+ F = (F + this.F) | 0;
509
+ G = (G + this.G) | 0;
510
+ H = (H + this.H) | 0;
511
+ this.set(A, B, C, D, E, F, G, H);
512
+ }
513
+ roundClean() {
514
+ clean(SHA256_W);
515
+ }
516
+ destroy() {
517
+ this.set(0, 0, 0, 0, 0, 0, 0, 0);
518
+ clean(this.buffer);
519
+ }
520
+ }
521
+ // SHA2-512 is slower than sha256 in js because u64 operations are slow.
522
+ // Round contants
523
+ // First 32 bits of the fractional parts of the cube roots of the first 80 primes 2..409
524
+ // prettier-ignore
525
+ const K512 = /* @__PURE__ */ (() => split([
526
+ '0x428a2f98d728ae22', '0x7137449123ef65cd', '0xb5c0fbcfec4d3b2f', '0xe9b5dba58189dbbc',
527
+ '0x3956c25bf348b538', '0x59f111f1b605d019', '0x923f82a4af194f9b', '0xab1c5ed5da6d8118',
528
+ '0xd807aa98a3030242', '0x12835b0145706fbe', '0x243185be4ee4b28c', '0x550c7dc3d5ffb4e2',
529
+ '0x72be5d74f27b896f', '0x80deb1fe3b1696b1', '0x9bdc06a725c71235', '0xc19bf174cf692694',
530
+ '0xe49b69c19ef14ad2', '0xefbe4786384f25e3', '0x0fc19dc68b8cd5b5', '0x240ca1cc77ac9c65',
531
+ '0x2de92c6f592b0275', '0x4a7484aa6ea6e483', '0x5cb0a9dcbd41fbd4', '0x76f988da831153b5',
532
+ '0x983e5152ee66dfab', '0xa831c66d2db43210', '0xb00327c898fb213f', '0xbf597fc7beef0ee4',
533
+ '0xc6e00bf33da88fc2', '0xd5a79147930aa725', '0x06ca6351e003826f', '0x142929670a0e6e70',
534
+ '0x27b70a8546d22ffc', '0x2e1b21385c26c926', '0x4d2c6dfc5ac42aed', '0x53380d139d95b3df',
535
+ '0x650a73548baf63de', '0x766a0abb3c77b2a8', '0x81c2c92e47edaee6', '0x92722c851482353b',
536
+ '0xa2bfe8a14cf10364', '0xa81a664bbc423001', '0xc24b8b70d0f89791', '0xc76c51a30654be30',
537
+ '0xd192e819d6ef5218', '0xd69906245565a910', '0xf40e35855771202a', '0x106aa07032bbd1b8',
538
+ '0x19a4c116b8d2d0c8', '0x1e376c085141ab53', '0x2748774cdf8eeb99', '0x34b0bcb5e19b48a8',
539
+ '0x391c0cb3c5c95a63', '0x4ed8aa4ae3418acb', '0x5b9cca4f7763e373', '0x682e6ff3d6b2b8a3',
540
+ '0x748f82ee5defb2fc', '0x78a5636f43172f60', '0x84c87814a1f0ab72', '0x8cc702081a6439ec',
541
+ '0x90befffa23631e28', '0xa4506cebde82bde9', '0xbef9a3f7b2c67915', '0xc67178f2e372532b',
542
+ '0xca273eceea26619c', '0xd186b8c721c0c207', '0xeada7dd6cde0eb1e', '0xf57d4f7fee6ed178',
543
+ '0x06f067aa72176fba', '0x0a637dc5a2c898a6', '0x113f9804bef90dae', '0x1b710b35131c471b',
544
+ '0x28db77f523047d84', '0x32caab7b40c72493', '0x3c9ebe0a15c9bebc', '0x431d67c49c100d4c',
545
+ '0x4cc5d4becb3e42b6', '0x597f299cfc657e2a', '0x5fcb6fab3ad6faec', '0x6c44198c4a475817'
546
+ ].map(n => BigInt(n))))();
547
+ const SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
548
+ const SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
549
+ // Reusable temporary buffers
550
+ const SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
551
+ const SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
552
+ class SHA512 extends HashMD {
553
+ constructor(outputLen = 64) {
554
+ super(128, outputLen, 16, false);
555
+ // We cannot use array here since array allows indexing by variable
556
+ // which means optimizer/compiler cannot use registers.
557
+ // h -- high 32 bits, l -- low 32 bits
558
+ this.Ah = SHA512_IV[0] | 0;
559
+ this.Al = SHA512_IV[1] | 0;
560
+ this.Bh = SHA512_IV[2] | 0;
561
+ this.Bl = SHA512_IV[3] | 0;
562
+ this.Ch = SHA512_IV[4] | 0;
563
+ this.Cl = SHA512_IV[5] | 0;
564
+ this.Dh = SHA512_IV[6] | 0;
565
+ this.Dl = SHA512_IV[7] | 0;
566
+ this.Eh = SHA512_IV[8] | 0;
567
+ this.El = SHA512_IV[9] | 0;
568
+ this.Fh = SHA512_IV[10] | 0;
569
+ this.Fl = SHA512_IV[11] | 0;
570
+ this.Gh = SHA512_IV[12] | 0;
571
+ this.Gl = SHA512_IV[13] | 0;
572
+ this.Hh = SHA512_IV[14] | 0;
573
+ this.Hl = SHA512_IV[15] | 0;
574
+ }
575
+ // prettier-ignore
576
+ get() {
577
+ const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
578
+ return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
579
+ }
580
+ // prettier-ignore
581
+ set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
582
+ this.Ah = Ah | 0;
583
+ this.Al = Al | 0;
584
+ this.Bh = Bh | 0;
585
+ this.Bl = Bl | 0;
586
+ this.Ch = Ch | 0;
587
+ this.Cl = Cl | 0;
588
+ this.Dh = Dh | 0;
589
+ this.Dl = Dl | 0;
590
+ this.Eh = Eh | 0;
591
+ this.El = El | 0;
592
+ this.Fh = Fh | 0;
593
+ this.Fl = Fl | 0;
594
+ this.Gh = Gh | 0;
595
+ this.Gl = Gl | 0;
596
+ this.Hh = Hh | 0;
597
+ this.Hl = Hl | 0;
598
+ }
599
+ process(view, offset) {
600
+ // Extend the first 16 words into the remaining 64 words w[16..79] of the message schedule array
601
+ for (let i = 0; i < 16; i++, offset += 4) {
602
+ SHA512_W_H[i] = view.getUint32(offset);
603
+ SHA512_W_L[i] = view.getUint32((offset += 4));
604
+ }
605
+ for (let i = 16; i < 80; i++) {
606
+ // s0 := (w[i-15] rightrotate 1) xor (w[i-15] rightrotate 8) xor (w[i-15] rightshift 7)
607
+ const W15h = SHA512_W_H[i - 15] | 0;
608
+ const W15l = SHA512_W_L[i - 15] | 0;
609
+ const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
610
+ const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
611
+ // s1 := (w[i-2] rightrotate 19) xor (w[i-2] rightrotate 61) xor (w[i-2] rightshift 6)
612
+ const W2h = SHA512_W_H[i - 2] | 0;
613
+ const W2l = SHA512_W_L[i - 2] | 0;
614
+ const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
615
+ const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
616
+ // SHA256_W[i] = s0 + s1 + SHA256_W[i - 7] + SHA256_W[i - 16];
617
+ const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
618
+ const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
619
+ SHA512_W_H[i] = SUMh | 0;
620
+ SHA512_W_L[i] = SUMl | 0;
621
+ }
622
+ let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
623
+ // Compression function main loop, 80 rounds
624
+ for (let i = 0; i < 80; i++) {
625
+ // S1 := (e rightrotate 14) xor (e rightrotate 18) xor (e rightrotate 41)
626
+ const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
627
+ const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
628
+ //const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
629
+ const CHIh = (Eh & Fh) ^ (~Eh & Gh);
630
+ const CHIl = (El & Fl) ^ (~El & Gl);
631
+ // T1 = H + sigma1 + Chi(E, F, G) + SHA512_K[i] + SHA512_W[i]
632
+ // prettier-ignore
633
+ const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
634
+ const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
635
+ const T1l = T1ll | 0;
636
+ // S0 := (a rightrotate 28) xor (a rightrotate 34) xor (a rightrotate 39)
637
+ const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
638
+ const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
639
+ const MAJh = (Ah & Bh) ^ (Ah & Ch) ^ (Bh & Ch);
640
+ const MAJl = (Al & Bl) ^ (Al & Cl) ^ (Bl & Cl);
641
+ Hh = Gh | 0;
642
+ Hl = Gl | 0;
643
+ Gh = Fh | 0;
644
+ Gl = Fl | 0;
645
+ Fh = Eh | 0;
646
+ Fl = El | 0;
647
+ ({ h: Eh, l: El } = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
648
+ Dh = Ch | 0;
649
+ Dl = Cl | 0;
650
+ Ch = Bh | 0;
651
+ Cl = Bl | 0;
652
+ Bh = Ah | 0;
653
+ Bl = Al | 0;
654
+ const All = add3L(T1l, sigma0l, MAJl);
655
+ Ah = add3H(All, T1h, sigma0h, MAJh);
656
+ Al = All | 0;
657
+ }
658
+ // Add the compressed chunk to the current hash value
659
+ ({ h: Ah, l: Al } = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
660
+ ({ h: Bh, l: Bl } = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
661
+ ({ h: Ch, l: Cl } = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
662
+ ({ h: Dh, l: Dl } = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
663
+ ({ h: Eh, l: El } = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
664
+ ({ h: Fh, l: Fl } = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
665
+ ({ h: Gh, l: Gl } = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
666
+ ({ h: Hh, l: Hl } = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
667
+ this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
668
+ }
669
+ roundClean() {
670
+ clean(SHA512_W_H, SHA512_W_L);
671
+ }
672
+ destroy() {
673
+ clean(this.buffer);
674
+ this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
675
+ }
676
+ }
677
+ /**
678
+ * SHA2-256 hash function from RFC 4634.
679
+ *
680
+ * It is the fastest JS hash, even faster than Blake3.
681
+ * To break sha256 using birthday attack, attackers need to try 2^128 hashes.
682
+ * BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
683
+ */
684
+ const sha256 = /* @__PURE__ */ createHasher(() => new SHA256());
685
+ /** SHA2-512 hash function from RFC 4634. */
686
+ const sha512 = /* @__PURE__ */ createHasher(() => new SHA512());
687
+
688
+ /*! noble-ciphers - MIT License (c) 2023 Paul Miller (paulmillr.com) */
689
+ // Cast array to different type
690
+ const u8 = (arr) => new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
691
+ const u32 = (arr) => new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
692
+ function isBytes$1(a) {
693
+ return (a instanceof Uint8Array ||
694
+ (a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array'));
695
+ }
696
+ // Cast array to view
697
+ const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
698
+ // big-endian hardware is rare. Just in case someone still decides to run ciphers:
699
+ // early-throw an error because we don't support BE yet.
700
+ const isLE = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
701
+ if (!isLE)
702
+ throw new Error('Non little-endian hardware is not supported');
703
+ /**
704
+ * @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
705
+ */
706
+ function utf8ToBytes(str) {
707
+ if (typeof str !== 'string')
708
+ throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
709
+ return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
710
+ }
711
+ function bytesToUtf8(bytes) {
712
+ return new TextDecoder().decode(bytes);
713
+ }
714
+ /**
715
+ * Normalizes (non-hex) string or Uint8Array to Uint8Array.
716
+ * Warning: when Uint8Array is passed, it would NOT get copied.
717
+ * Keep in mind for future mutable operations.
718
+ */
719
+ function toBytes(data) {
720
+ if (typeof data === 'string')
721
+ data = utf8ToBytes(data);
722
+ else if (isBytes$1(data))
723
+ data = data.slice();
724
+ else
725
+ throw new Error(`expected Uint8Array, got ${typeof data}`);
726
+ return data;
727
+ }
728
+ /**
729
+ * Copies several Uint8Arrays into one.
730
+ */
731
+ function concatBytes(...arrays) {
732
+ let sum = 0;
733
+ for (let i = 0; i < arrays.length; i++) {
734
+ const a = arrays[i];
735
+ if (!isBytes$1(a))
736
+ throw new Error('Uint8Array expected');
737
+ sum += a.length;
738
+ }
739
+ const res = new Uint8Array(sum);
740
+ for (let i = 0, pad = 0; i < arrays.length; i++) {
741
+ const a = arrays[i];
742
+ res.set(a, pad);
743
+ pad += a.length;
744
+ }
745
+ return res;
746
+ }
747
+ // Check if object doens't have custom constructor (like Uint8Array/Array)
748
+ const isPlainObject = (obj) => Object.prototype.toString.call(obj) === '[object Object]' && obj.constructor === Object;
749
+ function checkOpts(defaults, opts) {
750
+ if (opts !== undefined && (typeof opts !== 'object' || !isPlainObject(opts)))
751
+ throw new Error('options must be object or undefined');
752
+ const merged = Object.assign(defaults, opts);
753
+ return merged;
754
+ }
755
+ function ensureBytes(b, len) {
756
+ if (!isBytes$1(b))
757
+ throw new Error('Uint8Array expected');
758
+ if (typeof len === 'number')
759
+ if (b.length !== len)
760
+ throw new Error(`Uint8Array length ${len} expected`);
761
+ }
762
+ // Compares 2 u8a-s in kinda constant time
763
+ function equalBytes(a, b) {
764
+ if (a.length !== b.length)
765
+ return false;
766
+ let diff = 0;
767
+ for (let i = 0; i < a.length; i++)
768
+ diff |= a[i] ^ b[i];
769
+ return diff === 0;
770
+ }
771
+ const wrapCipher = (params, c) => {
772
+ Object.assign(c, params);
773
+ return c;
774
+ };
775
+ // Polyfill for Safari 14
776
+ function setBigUint64(view, byteOffset, value, isLE) {
777
+ if (typeof view.setBigUint64 === 'function')
778
+ return view.setBigUint64(byteOffset, value, isLE);
779
+ const _32n = BigInt(32);
780
+ const _u32_max = BigInt(0xffffffff);
781
+ const wh = Number((value >> _32n) & _u32_max);
782
+ const wl = Number(value & _u32_max);
783
+ const h = isLE ? 4 : 0;
784
+ const l = isLE ? 0 : 4;
785
+ view.setUint32(byteOffset + h, wh, isLE);
786
+ view.setUint32(byteOffset + l, wl, isLE);
787
+ }
788
+
789
+ function number(n) {
790
+ if (!Number.isSafeInteger(n) || n < 0)
791
+ throw new Error(`wrong positive integer: ${n}`);
792
+ }
793
+ function bool(b) {
794
+ if (typeof b !== 'boolean')
795
+ throw new Error(`boolean expected, not ${b}`);
796
+ }
797
+ // TODO: merge with utils
798
+ function isBytes(a) {
799
+ return (a != null &&
800
+ typeof a === 'object' &&
801
+ (a instanceof Uint8Array || a.constructor.name === 'Uint8Array'));
802
+ }
803
+ function bytes(b, ...lengths) {
804
+ if (!isBytes(b))
805
+ throw new Error('Uint8Array expected');
806
+ if (lengths.length > 0 && !lengths.includes(b.length))
807
+ throw new Error(`Uint8Array expected of length ${lengths}, not of length=${b.length}`);
808
+ }
809
+ function exists(instance, checkFinished = true) {
810
+ if (instance.destroyed)
811
+ throw new Error('Hash instance has been destroyed');
812
+ if (checkFinished && instance.finished)
813
+ throw new Error('Hash#digest() has already been called');
814
+ }
815
+ function output(out, instance) {
816
+ bytes(out);
817
+ const min = instance.outputLen;
818
+ if (out.length < min) {
819
+ throw new Error(`digestInto() expects output buffer of length at least ${min}`);
820
+ }
821
+ }
822
+
823
+ // GHash from AES-GCM and its little-endian "mirror image" Polyval from AES-SIV.
824
+ // Implemented in terms of GHash with conversion function for keys
825
+ // GCM GHASH from NIST SP800-38d, SIV from RFC 8452.
826
+ // https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
827
+ // GHASH modulo: x^128 + x^7 + x^2 + x + 1
828
+ // POLYVAL modulo: x^128 + x^127 + x^126 + x^121 + 1
829
+ const BLOCK_SIZE$1 = 16;
830
+ // TODO: rewrite
831
+ // temporary padding buffer
832
+ const ZEROS16 = /* @__PURE__ */ new Uint8Array(16);
833
+ const ZEROS32 = u32(ZEROS16);
834
+ const POLY$1 = 0xe1; // v = 2*v % POLY
835
+ // v = 2*v % POLY
836
+ // NOTE: because x + x = 0 (add/sub is same), mul2(x) != x+x
837
+ // We can multiply any number using montgomery ladder and this function (works as double, add is simple xor)
838
+ const mul2$1 = (s0, s1, s2, s3) => {
839
+ const hiBit = s3 & 1;
840
+ return {
841
+ s3: (s2 << 31) | (s3 >>> 1),
842
+ s2: (s1 << 31) | (s2 >>> 1),
843
+ s1: (s0 << 31) | (s1 >>> 1),
844
+ s0: (s0 >>> 1) ^ ((POLY$1 << 24) & -(hiBit & 1)), // reduce % poly
845
+ };
846
+ };
847
+ const swapLE = (n) => (((n >>> 0) & 0xff) << 24) |
848
+ (((n >>> 8) & 0xff) << 16) |
849
+ (((n >>> 16) & 0xff) << 8) |
850
+ ((n >>> 24) & 0xff) |
851
+ 0;
852
+ /**
853
+ * `mulX_POLYVAL(ByteReverse(H))` from spec
854
+ * @param k mutated in place
855
+ */
856
+ function _toGHASHKey(k) {
857
+ k.reverse();
858
+ const hiBit = k[15] & 1;
859
+ // k >>= 1
860
+ let carry = 0;
861
+ for (let i = 0; i < k.length; i++) {
862
+ const t = k[i];
863
+ k[i] = (t >>> 1) | carry;
864
+ carry = (t & 1) << 7;
865
+ }
866
+ k[0] ^= -hiBit & 0xe1; // if (hiBit) n ^= 0xe1000000000000000000000000000000;
867
+ return k;
868
+ }
869
+ const estimateWindow = (bytes) => {
870
+ if (bytes > 64 * 1024)
871
+ return 8;
872
+ if (bytes > 1024)
873
+ return 4;
874
+ return 2;
875
+ };
876
+ class GHASH {
877
+ // We select bits per window adaptively based on expectedLength
878
+ constructor(key, expectedLength) {
879
+ this.blockLen = BLOCK_SIZE$1;
880
+ this.outputLen = BLOCK_SIZE$1;
881
+ this.s0 = 0;
882
+ this.s1 = 0;
883
+ this.s2 = 0;
884
+ this.s3 = 0;
885
+ this.finished = false;
886
+ key = toBytes(key);
887
+ ensureBytes(key, 16);
888
+ const kView = createView(key);
889
+ let k0 = kView.getUint32(0, false);
890
+ let k1 = kView.getUint32(4, false);
891
+ let k2 = kView.getUint32(8, false);
892
+ let k3 = kView.getUint32(12, false);
893
+ // generate table of doubled keys (half of montgomery ladder)
894
+ const doubles = [];
895
+ for (let i = 0; i < 128; i++) {
896
+ doubles.push({ s0: swapLE(k0), s1: swapLE(k1), s2: swapLE(k2), s3: swapLE(k3) });
897
+ ({ s0: k0, s1: k1, s2: k2, s3: k3 } = mul2$1(k0, k1, k2, k3));
898
+ }
899
+ const W = estimateWindow(expectedLength || 1024);
900
+ if (![1, 2, 4, 8].includes(W))
901
+ throw new Error(`ghash: wrong window size=${W}, should be 2, 4 or 8`);
902
+ this.W = W;
903
+ const bits = 128; // always 128 bits;
904
+ const windows = bits / W;
905
+ const windowSize = (this.windowSize = 2 ** W);
906
+ const items = [];
907
+ // Create precompute table for window of W bits
908
+ for (let w = 0; w < windows; w++) {
909
+ // truth table: 00, 01, 10, 11
910
+ for (let byte = 0; byte < windowSize; byte++) {
911
+ // prettier-ignore
912
+ let s0 = 0, s1 = 0, s2 = 0, s3 = 0;
913
+ for (let j = 0; j < W; j++) {
914
+ const bit = (byte >>> (W - j - 1)) & 1;
915
+ if (!bit)
916
+ continue;
917
+ const { s0: d0, s1: d1, s2: d2, s3: d3 } = doubles[W * w + j];
918
+ (s0 ^= d0), (s1 ^= d1), (s2 ^= d2), (s3 ^= d3);
919
+ }
920
+ items.push({ s0, s1, s2, s3 });
921
+ }
922
+ }
923
+ this.t = items;
924
+ }
925
+ _updateBlock(s0, s1, s2, s3) {
926
+ (s0 ^= this.s0), (s1 ^= this.s1), (s2 ^= this.s2), (s3 ^= this.s3);
927
+ const { W, t, windowSize } = this;
928
+ // prettier-ignore
929
+ let o0 = 0, o1 = 0, o2 = 0, o3 = 0;
930
+ const mask = (1 << W) - 1; // 2**W will kill performance.
931
+ let w = 0;
932
+ for (const num of [s0, s1, s2, s3]) {
933
+ for (let bytePos = 0; bytePos < 4; bytePos++) {
934
+ const byte = (num >>> (8 * bytePos)) & 0xff;
935
+ for (let bitPos = 8 / W - 1; bitPos >= 0; bitPos--) {
936
+ const bit = (byte >>> (W * bitPos)) & mask;
937
+ const { s0: e0, s1: e1, s2: e2, s3: e3 } = t[w * windowSize + bit];
938
+ (o0 ^= e0), (o1 ^= e1), (o2 ^= e2), (o3 ^= e3);
939
+ w += 1;
940
+ }
941
+ }
942
+ }
943
+ this.s0 = o0;
944
+ this.s1 = o1;
945
+ this.s2 = o2;
946
+ this.s3 = o3;
947
+ }
948
+ update(data) {
949
+ data = toBytes(data);
950
+ exists(this);
951
+ const b32 = u32(data);
952
+ const blocks = Math.floor(data.length / BLOCK_SIZE$1);
953
+ const left = data.length % BLOCK_SIZE$1;
954
+ for (let i = 0; i < blocks; i++) {
955
+ this._updateBlock(b32[i * 4 + 0], b32[i * 4 + 1], b32[i * 4 + 2], b32[i * 4 + 3]);
956
+ }
957
+ if (left) {
958
+ ZEROS16.set(data.subarray(blocks * BLOCK_SIZE$1));
959
+ this._updateBlock(ZEROS32[0], ZEROS32[1], ZEROS32[2], ZEROS32[3]);
960
+ ZEROS32.fill(0); // clean tmp buffer
961
+ }
962
+ return this;
963
+ }
964
+ destroy() {
965
+ const { t } = this;
966
+ // clean precompute table
967
+ for (const elm of t) {
968
+ (elm.s0 = 0), (elm.s1 = 0), (elm.s2 = 0), (elm.s3 = 0);
969
+ }
970
+ }
971
+ digestInto(out) {
972
+ exists(this);
973
+ output(out, this);
974
+ this.finished = true;
975
+ const { s0, s1, s2, s3 } = this;
976
+ const o32 = u32(out);
977
+ o32[0] = s0;
978
+ o32[1] = s1;
979
+ o32[2] = s2;
980
+ o32[3] = s3;
981
+ return out;
982
+ }
983
+ digest() {
984
+ const res = new Uint8Array(BLOCK_SIZE$1);
985
+ this.digestInto(res);
986
+ this.destroy();
987
+ return res;
988
+ }
989
+ }
990
+ class Polyval extends GHASH {
991
+ constructor(key, expectedLength) {
992
+ key = toBytes(key);
993
+ const ghKey = _toGHASHKey(key.slice());
994
+ super(ghKey, expectedLength);
995
+ ghKey.fill(0);
996
+ }
997
+ update(data) {
998
+ data = toBytes(data);
999
+ exists(this);
1000
+ const b32 = u32(data);
1001
+ const left = data.length % BLOCK_SIZE$1;
1002
+ const blocks = Math.floor(data.length / BLOCK_SIZE$1);
1003
+ for (let i = 0; i < blocks; i++) {
1004
+ this._updateBlock(swapLE(b32[i * 4 + 3]), swapLE(b32[i * 4 + 2]), swapLE(b32[i * 4 + 1]), swapLE(b32[i * 4 + 0]));
1005
+ }
1006
+ if (left) {
1007
+ ZEROS16.set(data.subarray(blocks * BLOCK_SIZE$1));
1008
+ this._updateBlock(swapLE(ZEROS32[3]), swapLE(ZEROS32[2]), swapLE(ZEROS32[1]), swapLE(ZEROS32[0]));
1009
+ ZEROS32.fill(0); // clean tmp buffer
1010
+ }
1011
+ return this;
1012
+ }
1013
+ digestInto(out) {
1014
+ exists(this);
1015
+ output(out, this);
1016
+ this.finished = true;
1017
+ // tmp ugly hack
1018
+ const { s0, s1, s2, s3 } = this;
1019
+ const o32 = u32(out);
1020
+ o32[0] = s0;
1021
+ o32[1] = s1;
1022
+ o32[2] = s2;
1023
+ o32[3] = s3;
1024
+ return out.reverse();
1025
+ }
1026
+ }
1027
+ function wrapConstructorWithKey(hashCons) {
1028
+ const hashC = (msg, key) => hashCons(key, msg.length).update(toBytes(msg)).digest();
1029
+ const tmp = hashCons(new Uint8Array(16), 0);
1030
+ hashC.outputLen = tmp.outputLen;
1031
+ hashC.blockLen = tmp.blockLen;
1032
+ hashC.create = (key, expectedLength) => hashCons(key, expectedLength);
1033
+ return hashC;
1034
+ }
1035
+ const ghash = wrapConstructorWithKey((key, expectedLength) => new GHASH(key, expectedLength));
1036
+ const polyval = wrapConstructorWithKey((key, expectedLength) => new Polyval(key, expectedLength));
1037
+
1038
+ // AES (Advanced Encryption Standard) aka Rijndael block cipher.
1039
+ //
1040
+ // Data is split into 128-bit blocks. Encrypted in 10/12/14 rounds (128/192/256bit). Every round:
1041
+ // 1. **S-box**, table substitution
1042
+ // 2. **Shift rows**, cyclic shift left of all rows of data array
1043
+ // 3. **Mix columns**, multiplying every column by fixed polynomial
1044
+ // 4. **Add round key**, round_key xor i-th column of array
1045
+ //
1046
+ // Resources:
1047
+ // - FIPS-197 https://csrc.nist.gov/files/pubs/fips/197/final/docs/fips-197.pdf
1048
+ // - Original proposal: https://csrc.nist.gov/csrc/media/projects/cryptographic-standards-and-guidelines/documents/aes-development/rijndael-ammended.pdf
1049
+ const BLOCK_SIZE = 16;
1050
+ const BLOCK_SIZE32 = 4;
1051
+ const EMPTY_BLOCK = new Uint8Array(BLOCK_SIZE);
1052
+ const POLY = 0x11b; // 1 + x + x**3 + x**4 + x**8
1053
+ // TODO: remove multiplication, binary ops only
1054
+ function mul2(n) {
1055
+ return (n << 1) ^ (POLY & -(n >> 7));
1056
+ }
1057
+ function mul(a, b) {
1058
+ let res = 0;
1059
+ for (; b > 0; b >>= 1) {
1060
+ // Montgomery ladder
1061
+ res ^= a & -(b & 1); // if (b&1) res ^=a (but const-time).
1062
+ a = mul2(a); // a = 2*a
1063
+ }
1064
+ return res;
1065
+ }
1066
+ // AES S-box is generated using finite field inversion,
1067
+ // an affine transform, and xor of a constant 0x63.
1068
+ const _sbox = /* @__PURE__ */ (() => {
1069
+ let t = new Uint8Array(256);
1070
+ for (let i = 0, x = 1; i < 256; i++, x ^= mul2(x))
1071
+ t[i] = x;
1072
+ const sbox = new Uint8Array(256);
1073
+ sbox[0] = 0x63; // first elm
1074
+ for (let i = 0; i < 255; i++) {
1075
+ let x = t[255 - i];
1076
+ x |= x << 8;
1077
+ sbox[t[i]] = (x ^ (x >> 4) ^ (x >> 5) ^ (x >> 6) ^ (x >> 7) ^ 0x63) & 0xff;
1078
+ }
1079
+ return sbox;
1080
+ })();
1081
+ // Inverted S-box
1082
+ const _inv_sbox = /* @__PURE__ */ _sbox.map((_, j) => _sbox.indexOf(j));
1083
+ // Rotate u32 by 8
1084
+ const rotr32_8 = (n) => (n << 24) | (n >>> 8);
1085
+ const rotl32_8 = (n) => (n << 8) | (n >>> 24);
1086
+ // T-table is optimization suggested in 5.2 of original proposal (missed from FIPS-197). Changes:
1087
+ // - LE instead of BE
1088
+ // - bigger tables: T0 and T1 are merged into T01 table and T2 & T3 into T23;
1089
+ // so index is u16, instead of u8. This speeds up things, unexpectedly
1090
+ function genTtable(sbox, fn) {
1091
+ if (sbox.length !== 256)
1092
+ throw new Error('Wrong sbox length');
1093
+ const T0 = new Uint32Array(256).map((_, j) => fn(sbox[j]));
1094
+ const T1 = T0.map(rotl32_8);
1095
+ const T2 = T1.map(rotl32_8);
1096
+ const T3 = T2.map(rotl32_8);
1097
+ const T01 = new Uint32Array(256 * 256);
1098
+ const T23 = new Uint32Array(256 * 256);
1099
+ const sbox2 = new Uint16Array(256 * 256);
1100
+ for (let i = 0; i < 256; i++) {
1101
+ for (let j = 0; j < 256; j++) {
1102
+ const idx = i * 256 + j;
1103
+ T01[idx] = T0[i] ^ T1[j];
1104
+ T23[idx] = T2[i] ^ T3[j];
1105
+ sbox2[idx] = (sbox[i] << 8) | sbox[j];
1106
+ }
1107
+ }
1108
+ return { sbox, sbox2, T0, T1, T2, T3, T01, T23 };
1109
+ }
1110
+ const TABLE_ENC = /* @__PURE__ */ genTtable(_sbox, (s) => (mul(s, 3) << 24) | (s << 16) | (s << 8) | mul(s, 2));
1111
+ const TABLE_DEC = /* @__PURE__ */ genTtable(_inv_sbox, (s) => (mul(s, 11) << 24) | (mul(s, 13) << 16) | (mul(s, 9) << 8) | mul(s, 14));
1112
+ const POWX = /* @__PURE__ */ (() => {
1113
+ const p = new Uint8Array(16);
1114
+ for (let i = 0, x = 1; i < 16; i++, x = mul2(x))
1115
+ p[i] = x;
1116
+ return p;
1117
+ })();
1118
+ function expandKeyLE(key) {
1119
+ ensureBytes(key);
1120
+ const len = key.length;
1121
+ if (![16, 24, 32].includes(len))
1122
+ throw new Error(`aes: wrong key size: should be 16, 24 or 32, got: ${len}`);
1123
+ const { sbox2 } = TABLE_ENC;
1124
+ const k32 = u32(key);
1125
+ const Nk = k32.length;
1126
+ const subByte = (n) => applySbox(sbox2, n, n, n, n);
1127
+ const xk = new Uint32Array(len + 28); // expanded key
1128
+ xk.set(k32);
1129
+ // 4.3.1 Key expansion
1130
+ for (let i = Nk; i < xk.length; i++) {
1131
+ let t = xk[i - 1];
1132
+ if (i % Nk === 0)
1133
+ t = subByte(rotr32_8(t)) ^ POWX[i / Nk - 1];
1134
+ else if (Nk > 6 && i % Nk === 4)
1135
+ t = subByte(t);
1136
+ xk[i] = xk[i - Nk] ^ t;
1137
+ }
1138
+ return xk;
1139
+ }
1140
+ function expandKeyDecLE(key) {
1141
+ const encKey = expandKeyLE(key);
1142
+ const xk = encKey.slice();
1143
+ const Nk = encKey.length;
1144
+ const { sbox2 } = TABLE_ENC;
1145
+ const { T0, T1, T2, T3 } = TABLE_DEC;
1146
+ // Inverse key by chunks of 4 (rounds)
1147
+ for (let i = 0; i < Nk; i += 4) {
1148
+ for (let j = 0; j < 4; j++)
1149
+ xk[i + j] = encKey[Nk - i - 4 + j];
1150
+ }
1151
+ encKey.fill(0);
1152
+ // apply InvMixColumn except first & last round
1153
+ for (let i = 4; i < Nk - 4; i++) {
1154
+ const x = xk[i];
1155
+ const w = applySbox(sbox2, x, x, x, x);
1156
+ xk[i] = T0[w & 0xff] ^ T1[(w >>> 8) & 0xff] ^ T2[(w >>> 16) & 0xff] ^ T3[w >>> 24];
1157
+ }
1158
+ return xk;
1159
+ }
1160
+ // Apply tables
1161
+ function apply0123(T01, T23, s0, s1, s2, s3) {
1162
+ return (T01[((s0 << 8) & 0xff00) | ((s1 >>> 8) & 0xff)] ^
1163
+ T23[((s2 >>> 8) & 0xff00) | ((s3 >>> 24) & 0xff)]);
1164
+ }
1165
+ function applySbox(sbox2, s0, s1, s2, s3) {
1166
+ return (sbox2[(s0 & 0xff) | (s1 & 0xff00)] |
1167
+ (sbox2[((s2 >>> 16) & 0xff) | ((s3 >>> 16) & 0xff00)] << 16));
1168
+ }
1169
+ function encrypt(xk, s0, s1, s2, s3) {
1170
+ const { sbox2, T01, T23 } = TABLE_ENC;
1171
+ let k = 0;
1172
+ (s0 ^= xk[k++]), (s1 ^= xk[k++]), (s2 ^= xk[k++]), (s3 ^= xk[k++]);
1173
+ const rounds = xk.length / 4 - 2;
1174
+ for (let i = 0; i < rounds; i++) {
1175
+ const t0 = xk[k++] ^ apply0123(T01, T23, s0, s1, s2, s3);
1176
+ const t1 = xk[k++] ^ apply0123(T01, T23, s1, s2, s3, s0);
1177
+ const t2 = xk[k++] ^ apply0123(T01, T23, s2, s3, s0, s1);
1178
+ const t3 = xk[k++] ^ apply0123(T01, T23, s3, s0, s1, s2);
1179
+ (s0 = t0), (s1 = t1), (s2 = t2), (s3 = t3);
1180
+ }
1181
+ // last round (without mixcolumns, so using SBOX2 table)
1182
+ const t0 = xk[k++] ^ applySbox(sbox2, s0, s1, s2, s3);
1183
+ const t1 = xk[k++] ^ applySbox(sbox2, s1, s2, s3, s0);
1184
+ const t2 = xk[k++] ^ applySbox(sbox2, s2, s3, s0, s1);
1185
+ const t3 = xk[k++] ^ applySbox(sbox2, s3, s0, s1, s2);
1186
+ return { s0: t0, s1: t1, s2: t2, s3: t3 };
1187
+ }
1188
+ function decrypt(xk, s0, s1, s2, s3) {
1189
+ const { sbox2, T01, T23 } = TABLE_DEC;
1190
+ let k = 0;
1191
+ (s0 ^= xk[k++]), (s1 ^= xk[k++]), (s2 ^= xk[k++]), (s3 ^= xk[k++]);
1192
+ const rounds = xk.length / 4 - 2;
1193
+ for (let i = 0; i < rounds; i++) {
1194
+ const t0 = xk[k++] ^ apply0123(T01, T23, s0, s3, s2, s1);
1195
+ const t1 = xk[k++] ^ apply0123(T01, T23, s1, s0, s3, s2);
1196
+ const t2 = xk[k++] ^ apply0123(T01, T23, s2, s1, s0, s3);
1197
+ const t3 = xk[k++] ^ apply0123(T01, T23, s3, s2, s1, s0);
1198
+ (s0 = t0), (s1 = t1), (s2 = t2), (s3 = t3);
1199
+ }
1200
+ // Last round
1201
+ const t0 = xk[k++] ^ applySbox(sbox2, s0, s3, s2, s1);
1202
+ const t1 = xk[k++] ^ applySbox(sbox2, s1, s0, s3, s2);
1203
+ const t2 = xk[k++] ^ applySbox(sbox2, s2, s1, s0, s3);
1204
+ const t3 = xk[k++] ^ applySbox(sbox2, s3, s2, s1, s0);
1205
+ return { s0: t0, s1: t1, s2: t2, s3: t3 };
1206
+ }
1207
+ function getDst(len, dst) {
1208
+ if (!dst)
1209
+ return new Uint8Array(len);
1210
+ ensureBytes(dst);
1211
+ if (dst.length < len)
1212
+ throw new Error(`aes: wrong destination length, expected at least ${len}, got: ${dst.length}`);
1213
+ return dst;
1214
+ }
1215
+ // TODO: investigate merging with ctr32
1216
+ function ctrCounter(xk, nonce, src, dst) {
1217
+ ensureBytes(nonce, BLOCK_SIZE);
1218
+ ensureBytes(src);
1219
+ const srcLen = src.length;
1220
+ dst = getDst(srcLen, dst);
1221
+ const ctr = nonce;
1222
+ const c32 = u32(ctr);
1223
+ // Fill block (empty, ctr=0)
1224
+ let { s0, s1, s2, s3 } = encrypt(xk, c32[0], c32[1], c32[2], c32[3]);
1225
+ const src32 = u32(src);
1226
+ const dst32 = u32(dst);
1227
+ // process blocks
1228
+ for (let i = 0; i + 4 <= src32.length; i += 4) {
1229
+ dst32[i + 0] = src32[i + 0] ^ s0;
1230
+ dst32[i + 1] = src32[i + 1] ^ s1;
1231
+ dst32[i + 2] = src32[i + 2] ^ s2;
1232
+ dst32[i + 3] = src32[i + 3] ^ s3;
1233
+ // Full 128 bit counter with wrap around
1234
+ let carry = 1;
1235
+ for (let i = ctr.length - 1; i >= 0; i--) {
1236
+ carry = (carry + (ctr[i] & 0xff)) | 0;
1237
+ ctr[i] = carry & 0xff;
1238
+ carry >>>= 8;
1239
+ }
1240
+ ({ s0, s1, s2, s3 } = encrypt(xk, c32[0], c32[1], c32[2], c32[3]));
1241
+ }
1242
+ // leftovers (less than block)
1243
+ // It's possible to handle > u32 fast, but is it worth it?
1244
+ const start = BLOCK_SIZE * Math.floor(src32.length / BLOCK_SIZE32);
1245
+ if (start < srcLen) {
1246
+ const b32 = new Uint32Array([s0, s1, s2, s3]);
1247
+ const buf = u8(b32);
1248
+ for (let i = start, pos = 0; i < srcLen; i++, pos++)
1249
+ dst[i] = src[i] ^ buf[pos];
1250
+ }
1251
+ return dst;
1252
+ }
1253
+ // AES CTR with overflowing 32 bit counter
1254
+ // It's possible to do 32le significantly simpler (and probably faster) by using u32.
1255
+ // But, we need both, and perf bottleneck is in ghash anyway.
1256
+ function ctr32(xk, isLE, nonce, src, dst) {
1257
+ ensureBytes(nonce, BLOCK_SIZE);
1258
+ ensureBytes(src);
1259
+ dst = getDst(src.length, dst);
1260
+ const ctr = nonce; // write new value to nonce, so it can be re-used
1261
+ const c32 = u32(ctr);
1262
+ const view = createView(ctr);
1263
+ const src32 = u32(src);
1264
+ const dst32 = u32(dst);
1265
+ const ctrPos = isLE ? 0 : 12;
1266
+ const srcLen = src.length;
1267
+ // Fill block (empty, ctr=0)
1268
+ let ctrNum = view.getUint32(ctrPos, isLE); // read current counter value
1269
+ let { s0, s1, s2, s3 } = encrypt(xk, c32[0], c32[1], c32[2], c32[3]);
1270
+ // process blocks
1271
+ for (let i = 0; i + 4 <= src32.length; i += 4) {
1272
+ dst32[i + 0] = src32[i + 0] ^ s0;
1273
+ dst32[i + 1] = src32[i + 1] ^ s1;
1274
+ dst32[i + 2] = src32[i + 2] ^ s2;
1275
+ dst32[i + 3] = src32[i + 3] ^ s3;
1276
+ ctrNum = (ctrNum + 1) >>> 0; // u32 wrap
1277
+ view.setUint32(ctrPos, ctrNum, isLE);
1278
+ ({ s0, s1, s2, s3 } = encrypt(xk, c32[0], c32[1], c32[2], c32[3]));
1279
+ }
1280
+ // leftovers (less than a block)
1281
+ const start = BLOCK_SIZE * Math.floor(src32.length / BLOCK_SIZE32);
1282
+ if (start < srcLen) {
1283
+ const b32 = new Uint32Array([s0, s1, s2, s3]);
1284
+ const buf = u8(b32);
1285
+ for (let i = start, pos = 0; i < srcLen; i++, pos++)
1286
+ dst[i] = src[i] ^ buf[pos];
1287
+ }
1288
+ return dst;
1289
+ }
1290
+ /**
1291
+ * CTR: counter mode. Creates stream cipher.
1292
+ * Requires good IV. Parallelizable. OK, but no MAC.
1293
+ */
1294
+ wrapCipher({ blockSize: 16, nonceLength: 16 }, function ctr(key, nonce) {
1295
+ ensureBytes(key);
1296
+ ensureBytes(nonce, BLOCK_SIZE);
1297
+ function processCtr(buf, dst) {
1298
+ const xk = expandKeyLE(key);
1299
+ const n = nonce.slice();
1300
+ const out = ctrCounter(xk, n, buf, dst);
1301
+ xk.fill(0);
1302
+ n.fill(0);
1303
+ return out;
1304
+ }
1305
+ return {
1306
+ encrypt: (plaintext, dst) => processCtr(plaintext, dst),
1307
+ decrypt: (ciphertext, dst) => processCtr(ciphertext, dst),
1308
+ };
1309
+ });
1310
+ function validateBlockDecrypt(data) {
1311
+ ensureBytes(data);
1312
+ if (data.length % BLOCK_SIZE !== 0) {
1313
+ throw new Error(`aes/(cbc-ecb).decrypt ciphertext should consist of blocks with size ${BLOCK_SIZE}`);
1314
+ }
1315
+ }
1316
+ function validateBlockEncrypt(plaintext, pcks5, dst) {
1317
+ let outLen = plaintext.length;
1318
+ const remaining = outLen % BLOCK_SIZE;
1319
+ if (!pcks5 && remaining !== 0)
1320
+ throw new Error('aec/(cbc-ecb): unpadded plaintext with disabled padding');
1321
+ const b = u32(plaintext);
1322
+ if (pcks5) {
1323
+ let left = BLOCK_SIZE - remaining;
1324
+ if (!left)
1325
+ left = BLOCK_SIZE; // if no bytes left, create empty padding block
1326
+ outLen = outLen + left;
1327
+ }
1328
+ const out = getDst(outLen, dst);
1329
+ const o = u32(out);
1330
+ return { b, o, out };
1331
+ }
1332
+ function validatePCKS(data, pcks5) {
1333
+ if (!pcks5)
1334
+ return data;
1335
+ const len = data.length;
1336
+ if (!len)
1337
+ throw new Error(`aes/pcks5: empty ciphertext not allowed`);
1338
+ const lastByte = data[len - 1];
1339
+ if (lastByte <= 0 || lastByte > 16)
1340
+ throw new Error(`aes/pcks5: wrong padding byte: ${lastByte}`);
1341
+ const out = data.subarray(0, -lastByte);
1342
+ for (let i = 0; i < lastByte; i++)
1343
+ if (data[len - i - 1] !== lastByte)
1344
+ throw new Error(`aes/pcks5: wrong padding`);
1345
+ return out;
1346
+ }
1347
+ function padPCKS(left) {
1348
+ const tmp = new Uint8Array(16);
1349
+ const tmp32 = u32(tmp);
1350
+ tmp.set(left);
1351
+ const paddingByte = BLOCK_SIZE - left.length;
1352
+ for (let i = BLOCK_SIZE - paddingByte; i < BLOCK_SIZE; i++)
1353
+ tmp[i] = paddingByte;
1354
+ return tmp32;
1355
+ }
1356
+ /**
1357
+ * ECB: Electronic CodeBook. Simple deterministic replacement.
1358
+ * Dangerous: always map x to y. See [AES Penguin](https://words.filippo.io/the-ecb-penguin/).
1359
+ */
1360
+ wrapCipher({ blockSize: 16 }, function ecb(key, opts = {}) {
1361
+ ensureBytes(key);
1362
+ const pcks5 = !opts.disablePadding;
1363
+ return {
1364
+ encrypt: (plaintext, dst) => {
1365
+ ensureBytes(plaintext);
1366
+ const { b, o, out: _out } = validateBlockEncrypt(plaintext, pcks5, dst);
1367
+ const xk = expandKeyLE(key);
1368
+ let i = 0;
1369
+ for (; i + 4 <= b.length;) {
1370
+ const { s0, s1, s2, s3 } = encrypt(xk, b[i + 0], b[i + 1], b[i + 2], b[i + 3]);
1371
+ (o[i++] = s0), (o[i++] = s1), (o[i++] = s2), (o[i++] = s3);
1372
+ }
1373
+ if (pcks5) {
1374
+ const tmp32 = padPCKS(plaintext.subarray(i * 4));
1375
+ const { s0, s1, s2, s3 } = encrypt(xk, tmp32[0], tmp32[1], tmp32[2], tmp32[3]);
1376
+ (o[i++] = s0), (o[i++] = s1), (o[i++] = s2), (o[i++] = s3);
1377
+ }
1378
+ xk.fill(0);
1379
+ return _out;
1380
+ },
1381
+ decrypt: (ciphertext, dst) => {
1382
+ validateBlockDecrypt(ciphertext);
1383
+ const xk = expandKeyDecLE(key);
1384
+ const out = getDst(ciphertext.length, dst);
1385
+ const b = u32(ciphertext);
1386
+ const o = u32(out);
1387
+ for (let i = 0; i + 4 <= b.length;) {
1388
+ const { s0, s1, s2, s3 } = decrypt(xk, b[i + 0], b[i + 1], b[i + 2], b[i + 3]);
1389
+ (o[i++] = s0), (o[i++] = s1), (o[i++] = s2), (o[i++] = s3);
1390
+ }
1391
+ xk.fill(0);
1392
+ return validatePCKS(out, pcks5);
1393
+ },
1394
+ };
1395
+ });
1396
+ /**
1397
+ * CBC: Cipher-Block-Chaining. Key is previous round’s block.
1398
+ * Fragile: needs proper padding. Unauthenticated: needs MAC.
1399
+ */
1400
+ wrapCipher({ blockSize: 16, nonceLength: 16 }, function cbc(key, iv, opts = {}) {
1401
+ ensureBytes(key);
1402
+ ensureBytes(iv, 16);
1403
+ const pcks5 = !opts.disablePadding;
1404
+ return {
1405
+ encrypt: (plaintext, dst) => {
1406
+ const xk = expandKeyLE(key);
1407
+ const { b, o, out: _out } = validateBlockEncrypt(plaintext, pcks5, dst);
1408
+ const n32 = u32(iv);
1409
+ // prettier-ignore
1410
+ let s0 = n32[0], s1 = n32[1], s2 = n32[2], s3 = n32[3];
1411
+ let i = 0;
1412
+ for (; i + 4 <= b.length;) {
1413
+ (s0 ^= b[i + 0]), (s1 ^= b[i + 1]), (s2 ^= b[i + 2]), (s3 ^= b[i + 3]);
1414
+ ({ s0, s1, s2, s3 } = encrypt(xk, s0, s1, s2, s3));
1415
+ (o[i++] = s0), (o[i++] = s1), (o[i++] = s2), (o[i++] = s3);
1416
+ }
1417
+ if (pcks5) {
1418
+ const tmp32 = padPCKS(plaintext.subarray(i * 4));
1419
+ (s0 ^= tmp32[0]), (s1 ^= tmp32[1]), (s2 ^= tmp32[2]), (s3 ^= tmp32[3]);
1420
+ ({ s0, s1, s2, s3 } = encrypt(xk, s0, s1, s2, s3));
1421
+ (o[i++] = s0), (o[i++] = s1), (o[i++] = s2), (o[i++] = s3);
1422
+ }
1423
+ xk.fill(0);
1424
+ return _out;
1425
+ },
1426
+ decrypt: (ciphertext, dst) => {
1427
+ validateBlockDecrypt(ciphertext);
1428
+ const xk = expandKeyDecLE(key);
1429
+ const n32 = u32(iv);
1430
+ const out = getDst(ciphertext.length, dst);
1431
+ const b = u32(ciphertext);
1432
+ const o = u32(out);
1433
+ // prettier-ignore
1434
+ let s0 = n32[0], s1 = n32[1], s2 = n32[2], s3 = n32[3];
1435
+ for (let i = 0; i + 4 <= b.length;) {
1436
+ // prettier-ignore
1437
+ const ps0 = s0, ps1 = s1, ps2 = s2, ps3 = s3;
1438
+ (s0 = b[i + 0]), (s1 = b[i + 1]), (s2 = b[i + 2]), (s3 = b[i + 3]);
1439
+ const { s0: o0, s1: o1, s2: o2, s3: o3 } = decrypt(xk, s0, s1, s2, s3);
1440
+ (o[i++] = o0 ^ ps0), (o[i++] = o1 ^ ps1), (o[i++] = o2 ^ ps2), (o[i++] = o3 ^ ps3);
1441
+ }
1442
+ xk.fill(0);
1443
+ return validatePCKS(out, pcks5);
1444
+ },
1445
+ };
1446
+ });
1447
+ // TODO: merge with chacha, however gcm has bitLen while chacha has byteLen
1448
+ function computeTag(fn, isLE, key, data, AAD) {
1449
+ const h = fn.create(key, data.length + (AAD?.length || 0));
1450
+ if (AAD)
1451
+ h.update(AAD);
1452
+ h.update(data);
1453
+ const num = new Uint8Array(16);
1454
+ const view = createView(num);
1455
+ if (AAD)
1456
+ setBigUint64(view, 0, BigInt(AAD.length * 8), isLE);
1457
+ setBigUint64(view, 8, BigInt(data.length * 8), isLE);
1458
+ h.update(num);
1459
+ return h.digest();
1460
+ }
1461
+ /**
1462
+ * GCM: Galois/Counter Mode.
1463
+ * Good, modern version of CTR, parallel, with MAC.
1464
+ * Be careful: MACs can be forged.
1465
+ */
1466
+ const gcm = wrapCipher({ blockSize: 16, nonceLength: 12, tagLength: 16 }, function gcm(key, nonce, AAD) {
1467
+ ensureBytes(nonce);
1468
+ // Nonce can be pretty much anything (even 1 byte). But smaller nonces less secure.
1469
+ if (nonce.length === 0)
1470
+ throw new Error('aes/gcm: empty nonce');
1471
+ const tagLength = 16;
1472
+ function _computeTag(authKey, tagMask, data) {
1473
+ const tag = computeTag(ghash, false, authKey, data, AAD);
1474
+ for (let i = 0; i < tagMask.length; i++)
1475
+ tag[i] ^= tagMask[i];
1476
+ return tag;
1477
+ }
1478
+ function deriveKeys() {
1479
+ const xk = expandKeyLE(key);
1480
+ const authKey = EMPTY_BLOCK.slice();
1481
+ const counter = EMPTY_BLOCK.slice();
1482
+ ctr32(xk, false, counter, counter, authKey);
1483
+ if (nonce.length === 12) {
1484
+ counter.set(nonce);
1485
+ }
1486
+ else {
1487
+ // Spec (NIST 800-38d) supports variable size nonce.
1488
+ // Not supported for now, but can be useful.
1489
+ const nonceLen = EMPTY_BLOCK.slice();
1490
+ const view = createView(nonceLen);
1491
+ setBigUint64(view, 8, BigInt(nonce.length * 8), false);
1492
+ // ghash(nonce || u64be(0) || u64be(nonceLen*8))
1493
+ ghash.create(authKey).update(nonce).update(nonceLen).digestInto(counter);
1494
+ }
1495
+ const tagMask = ctr32(xk, false, counter, EMPTY_BLOCK);
1496
+ return { xk, authKey, counter, tagMask };
1497
+ }
1498
+ return {
1499
+ encrypt: (plaintext) => {
1500
+ ensureBytes(plaintext);
1501
+ const { xk, authKey, counter, tagMask } = deriveKeys();
1502
+ const out = new Uint8Array(plaintext.length + tagLength);
1503
+ ctr32(xk, false, counter, plaintext, out);
1504
+ const tag = _computeTag(authKey, tagMask, out.subarray(0, out.length - tagLength));
1505
+ out.set(tag, plaintext.length);
1506
+ xk.fill(0);
1507
+ return out;
1508
+ },
1509
+ decrypt: (ciphertext) => {
1510
+ ensureBytes(ciphertext);
1511
+ if (ciphertext.length < tagLength)
1512
+ throw new Error(`aes/gcm: ciphertext less than tagLen (${tagLength})`);
1513
+ const { xk, authKey, counter, tagMask } = deriveKeys();
1514
+ const data = ciphertext.subarray(0, -tagLength);
1515
+ const passedTag = ciphertext.subarray(-tagLength);
1516
+ const tag = _computeTag(authKey, tagMask, data);
1517
+ if (!equalBytes(tag, passedTag))
1518
+ throw new Error('aes/gcm: invalid ghash tag');
1519
+ const out = ctr32(xk, false, counter, data);
1520
+ authKey.fill(0);
1521
+ tagMask.fill(0);
1522
+ xk.fill(0);
1523
+ return out;
1524
+ },
1525
+ };
1526
+ });
1527
+ const limit = (name, min, max) => (value) => {
1528
+ if (!Number.isSafeInteger(value) || min > value || value > max)
1529
+ throw new Error(`${name}: invalid value=${value}, must be [${min}..${max}]`);
1530
+ };
1531
+ /**
1532
+ * AES-GCM-SIV: classic AES-GCM with nonce-misuse resistance.
1533
+ * Guarantees that, when a nonce is repeated, the only security loss is that identical
1534
+ * plaintexts will produce identical ciphertexts.
1535
+ * RFC 8452, https://datatracker.ietf.org/doc/html/rfc8452
1536
+ */
1537
+ wrapCipher({ blockSize: 16, nonceLength: 12, tagLength: 16 }, function siv(key, nonce, AAD) {
1538
+ const tagLength = 16;
1539
+ // From RFC 8452: Section 6
1540
+ const AAD_LIMIT = limit('AAD', 0, 2 ** 36);
1541
+ const PLAIN_LIMIT = limit('plaintext', 0, 2 ** 36);
1542
+ const NONCE_LIMIT = limit('nonce', 12, 12);
1543
+ const CIPHER_LIMIT = limit('ciphertext', 16, 2 ** 36 + 16);
1544
+ ensureBytes(nonce);
1545
+ NONCE_LIMIT(nonce.length);
1546
+ if (AAD) {
1547
+ ensureBytes(AAD);
1548
+ AAD_LIMIT(AAD.length);
1549
+ }
1550
+ function deriveKeys() {
1551
+ const len = key.length;
1552
+ if (len !== 16 && len !== 24 && len !== 32)
1553
+ throw new Error(`key length must be 16, 24 or 32 bytes, got: ${len} bytes`);
1554
+ const xk = expandKeyLE(key);
1555
+ const encKey = new Uint8Array(len);
1556
+ const authKey = new Uint8Array(16);
1557
+ const n32 = u32(nonce);
1558
+ // prettier-ignore
1559
+ let s0 = 0, s1 = n32[0], s2 = n32[1], s3 = n32[2];
1560
+ let counter = 0;
1561
+ for (const derivedKey of [authKey, encKey].map(u32)) {
1562
+ const d32 = u32(derivedKey);
1563
+ for (let i = 0; i < d32.length; i += 2) {
1564
+ // aes(u32le(0) || nonce)[:8] || aes(u32le(1) || nonce)[:8] ...
1565
+ const { s0: o0, s1: o1 } = encrypt(xk, s0, s1, s2, s3);
1566
+ d32[i + 0] = o0;
1567
+ d32[i + 1] = o1;
1568
+ s0 = ++counter; // increment counter inside state
1569
+ }
1570
+ }
1571
+ xk.fill(0);
1572
+ return { authKey, encKey: expandKeyLE(encKey) };
1573
+ }
1574
+ function _computeTag(encKey, authKey, data) {
1575
+ const tag = computeTag(polyval, true, authKey, data, AAD);
1576
+ // Compute the expected tag by XORing S_s and the nonce, clearing the
1577
+ // most significant bit of the last byte and encrypting with the
1578
+ // message-encryption key.
1579
+ for (let i = 0; i < 12; i++)
1580
+ tag[i] ^= nonce[i];
1581
+ tag[15] &= 0x7f; // Clear the highest bit
1582
+ // encrypt tag as block
1583
+ const t32 = u32(tag);
1584
+ // prettier-ignore
1585
+ let s0 = t32[0], s1 = t32[1], s2 = t32[2], s3 = t32[3];
1586
+ ({ s0, s1, s2, s3 } = encrypt(encKey, s0, s1, s2, s3));
1587
+ (t32[0] = s0), (t32[1] = s1), (t32[2] = s2), (t32[3] = s3);
1588
+ return tag;
1589
+ }
1590
+ // actual decrypt/encrypt of message.
1591
+ function processSiv(encKey, tag, input) {
1592
+ let block = tag.slice();
1593
+ block[15] |= 0x80; // Force highest bit
1594
+ return ctr32(encKey, true, block, input);
1595
+ }
1596
+ return {
1597
+ encrypt: (plaintext) => {
1598
+ ensureBytes(plaintext);
1599
+ PLAIN_LIMIT(plaintext.length);
1600
+ const { encKey, authKey } = deriveKeys();
1601
+ const tag = _computeTag(encKey, authKey, plaintext);
1602
+ const out = new Uint8Array(plaintext.length + tagLength);
1603
+ out.set(tag, plaintext.length);
1604
+ out.set(processSiv(encKey, tag, plaintext));
1605
+ encKey.fill(0);
1606
+ authKey.fill(0);
1607
+ return out;
1608
+ },
1609
+ decrypt: (ciphertext) => {
1610
+ ensureBytes(ciphertext);
1611
+ CIPHER_LIMIT(ciphertext.length);
1612
+ const tag = ciphertext.subarray(-tagLength);
1613
+ const { encKey, authKey } = deriveKeys();
1614
+ const plaintext = processSiv(encKey, tag, ciphertext.subarray(0, -tagLength));
1615
+ const expectedTag = _computeTag(encKey, authKey, plaintext);
1616
+ encKey.fill(0);
1617
+ authKey.fill(0);
1618
+ if (!equalBytes(tag, expectedTag))
1619
+ throw new Error('invalid polyval tag');
1620
+ return plaintext;
1621
+ },
1622
+ };
1623
+ });
1624
+
1625
+ exports.ahash = ahash;
1626
+ exports.anumber = anumber;
1627
+ exports.asyncLoop = asyncLoop;
1628
+ exports.bool = bool;
1629
+ exports.bytes = bytes;
1630
+ exports.bytesToUtf8 = bytesToUtf8;
1631
+ exports.checkOpts = checkOpts;
1632
+ exports.checkOpts$1 = checkOpts$1;
1633
+ exports.clean = clean;
1634
+ exports.concatBytes = concatBytes;
1635
+ exports.createView = createView;
1636
+ exports.createView$1 = createView$1;
1637
+ exports.ensureBytes = ensureBytes;
1638
+ exports.equalBytes = equalBytes;
1639
+ exports.exists = exists;
1640
+ exports.gcm = gcm;
1641
+ exports.hmac = hmac;
1642
+ exports.kdfInputToBytes = kdfInputToBytes;
1643
+ exports.number = number;
1644
+ exports.output = output;
1645
+ exports.randomBytes = randomBytes;
1646
+ exports.setBigUint64 = setBigUint64;
1647
+ exports.sha256 = sha256;
1648
+ exports.sha512 = sha512;
1649
+ exports.toBytes = toBytes;
1650
+ exports.u32 = u32;
1651
+ exports.utf8ToBytes = utf8ToBytes;
1652
+ exports.wrapCipher = wrapCipher;