@decryption/hashes 0.2.0

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.
Files changed (60) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +75 -0
  3. package/_blake.d.ts +20 -0
  4. package/_blake.js +56 -0
  5. package/_md.d.ts +98 -0
  6. package/_md.js +207 -0
  7. package/_u64.d.ts +62 -0
  8. package/_u64.js +106 -0
  9. package/argon2.d.ts +111 -0
  10. package/argon2.js +525 -0
  11. package/blake1.d.ts +155 -0
  12. package/blake1.js +599 -0
  13. package/blake2.d.ts +183 -0
  14. package/blake2.js +540 -0
  15. package/blake3.d.ts +67 -0
  16. package/blake3.js +287 -0
  17. package/eskdf.d.ts +91 -0
  18. package/eskdf.js +223 -0
  19. package/esm/_blake.js +51 -0
  20. package/esm/_md.js +201 -0
  21. package/esm/_u64.js +83 -0
  22. package/esm/argon2.js +516 -0
  23. package/esm/blake1.js +559 -0
  24. package/esm/blake2.js +500 -0
  25. package/esm/blake3.js +283 -0
  26. package/esm/eskdf.js +217 -0
  27. package/esm/hkdf.js +119 -0
  28. package/esm/hmac.js +92 -0
  29. package/esm/index.js +34 -0
  30. package/esm/legacy.js +326 -0
  31. package/esm/pbkdf2.js +134 -0
  32. package/esm/scrypt.js +256 -0
  33. package/esm/sha2.js +457 -0
  34. package/esm/sha3-addons.js +782 -0
  35. package/esm/sha3.js +430 -0
  36. package/esm/utils.js +577 -0
  37. package/esm/webcrypto.js +163 -0
  38. package/hkdf.d.ts +63 -0
  39. package/hkdf.js +125 -0
  40. package/hmac.d.ts +46 -0
  41. package/hmac.js +96 -0
  42. package/index.d.ts +0 -0
  43. package/index.js +34 -0
  44. package/legacy.d.ts +101 -0
  45. package/legacy.js +332 -0
  46. package/package.json +47 -0
  47. package/pbkdf2.d.ts +55 -0
  48. package/pbkdf2.js +138 -0
  49. package/scrypt.d.ts +64 -0
  50. package/scrypt.js +260 -0
  51. package/sha2.d.ts +248 -0
  52. package/sha2.js +499 -0
  53. package/sha3-addons.d.ts +460 -0
  54. package/sha3-addons.js +791 -0
  55. package/sha3.d.ts +198 -0
  56. package/sha3.js +435 -0
  57. package/utils.d.ts +518 -0
  58. package/utils.js +606 -0
  59. package/webcrypto.d.ts +119 -0
  60. package/webcrypto.js +168 -0
package/esm/scrypt.js ADDED
@@ -0,0 +1,256 @@
1
+ /**
2
+ * RFC 7914 Scrypt KDF. Can be used to create a key from password and salt.
3
+ * @module
4
+ */
5
+ import { pbkdf2 } from './pbkdf2';
6
+ import { sha256 } from './sha2';
7
+ // prettier-ignore
8
+ import { anumber, asyncLoop, checkOpts, clean, rotl, swap32IfBE, u32 } from './utils';
9
+ // The main Scrypt loop: uses Salsa extensively.
10
+ // Six versions of the function were tried, this is the fastest one.
11
+ // RFC 7914 §3 / §4 step 2 applies Salsa20/8 to one 16-word (64-byte) block
12
+ // after xor'ing two such blocks.
13
+ // The local `y*` snapshot keeps the xor input stable even when `out` aliases `prev` or `input`.
14
+ // prettier-ignore
15
+ function XorAndSalsa(prev, pi, input, ii, out, oi) {
16
+ // Based on https://cr.yp.to/salsa20.html and RFC 7914's Salsa20/8 core.
17
+ // Xor blocks
18
+ let y00 = prev[pi++] ^ input[ii++], y01 = prev[pi++] ^ input[ii++];
19
+ let y02 = prev[pi++] ^ input[ii++], y03 = prev[pi++] ^ input[ii++];
20
+ let y04 = prev[pi++] ^ input[ii++], y05 = prev[pi++] ^ input[ii++];
21
+ let y06 = prev[pi++] ^ input[ii++], y07 = prev[pi++] ^ input[ii++];
22
+ let y08 = prev[pi++] ^ input[ii++], y09 = prev[pi++] ^ input[ii++];
23
+ let y10 = prev[pi++] ^ input[ii++], y11 = prev[pi++] ^ input[ii++];
24
+ let y12 = prev[pi++] ^ input[ii++], y13 = prev[pi++] ^ input[ii++];
25
+ let y14 = prev[pi++] ^ input[ii++], y15 = prev[pi++] ^ input[ii++];
26
+ // Save state to temporary variables (salsa)
27
+ let x00 = y00, x01 = y01, x02 = y02, x03 = y03, x04 = y04, x05 = y05, x06 = y06, x07 = y07, x08 = y08, x09 = y09, x10 = y10, x11 = y11, x12 = y12, x13 = y13, x14 = y14, x15 = y15;
28
+ // Main loop (salsa)
29
+ for (let i = 0; i < 8; i += 2) {
30
+ x04 ^= rotl(x00 + x12 | 0, 7);
31
+ x08 ^= rotl(x04 + x00 | 0, 9);
32
+ x12 ^= rotl(x08 + x04 | 0, 13);
33
+ x00 ^= rotl(x12 + x08 | 0, 18);
34
+ x09 ^= rotl(x05 + x01 | 0, 7);
35
+ x13 ^= rotl(x09 + x05 | 0, 9);
36
+ x01 ^= rotl(x13 + x09 | 0, 13);
37
+ x05 ^= rotl(x01 + x13 | 0, 18);
38
+ x14 ^= rotl(x10 + x06 | 0, 7);
39
+ x02 ^= rotl(x14 + x10 | 0, 9);
40
+ x06 ^= rotl(x02 + x14 | 0, 13);
41
+ x10 ^= rotl(x06 + x02 | 0, 18);
42
+ x03 ^= rotl(x15 + x11 | 0, 7);
43
+ x07 ^= rotl(x03 + x15 | 0, 9);
44
+ x11 ^= rotl(x07 + x03 | 0, 13);
45
+ x15 ^= rotl(x11 + x07 | 0, 18);
46
+ x01 ^= rotl(x00 + x03 | 0, 7);
47
+ x02 ^= rotl(x01 + x00 | 0, 9);
48
+ x03 ^= rotl(x02 + x01 | 0, 13);
49
+ x00 ^= rotl(x03 + x02 | 0, 18);
50
+ x06 ^= rotl(x05 + x04 | 0, 7);
51
+ x07 ^= rotl(x06 + x05 | 0, 9);
52
+ x04 ^= rotl(x07 + x06 | 0, 13);
53
+ x05 ^= rotl(x04 + x07 | 0, 18);
54
+ x11 ^= rotl(x10 + x09 | 0, 7);
55
+ x08 ^= rotl(x11 + x10 | 0, 9);
56
+ x09 ^= rotl(x08 + x11 | 0, 13);
57
+ x10 ^= rotl(x09 + x08 | 0, 18);
58
+ x12 ^= rotl(x15 + x14 | 0, 7);
59
+ x13 ^= rotl(x12 + x15 | 0, 9);
60
+ x14 ^= rotl(x13 + x12 | 0, 13);
61
+ x15 ^= rotl(x14 + x13 | 0, 18);
62
+ }
63
+ // Write output (salsa)
64
+ out[oi++] = (y00 + x00) | 0;
65
+ out[oi++] = (y01 + x01) | 0;
66
+ out[oi++] = (y02 + x02) | 0;
67
+ out[oi++] = (y03 + x03) | 0;
68
+ out[oi++] = (y04 + x04) | 0;
69
+ out[oi++] = (y05 + x05) | 0;
70
+ out[oi++] = (y06 + x06) | 0;
71
+ out[oi++] = (y07 + x07) | 0;
72
+ out[oi++] = (y08 + x08) | 0;
73
+ out[oi++] = (y09 + x09) | 0;
74
+ out[oi++] = (y10 + x10) | 0;
75
+ out[oi++] = (y11 + x11) | 0;
76
+ out[oi++] = (y12 + x12) | 0;
77
+ out[oi++] = (y13 + x13) | 0;
78
+ out[oi++] = (y14 + x14) | 0;
79
+ out[oi++] = (y15 + x15) | 0;
80
+ }
81
+ function BlockMix(input, ii, out, oi, r) {
82
+ // The block B is `r` 128-byte chunks, i.e. `2r` 16-word (64-byte) Salsa blocks.
83
+ let head = oi + 0;
84
+ let tail = oi + 16 * r;
85
+ for (let i = 0; i < 16; i++)
86
+ out[tail + i] = input[ii + (2 * r - 1) * 16 + i]; // X ← B[2r−1]
87
+ for (let i = 0; i < r; i++, head += 16, ii += 16) {
88
+ // RFC 7914 §4 step 3 outputs `Y[0], Y[2], ...` first, then `Y[1], Y[3], ...`;
89
+ // `head` and `tail` lay out those even/odd halves in place.
90
+ XorAndSalsa(out, tail, input, ii, out, head); // head[i] = Salsa(blockIn[2*i] ^ tail[i-1])
91
+ if (i > 0)
92
+ tail += 16; // First iteration overwrites tmp value in tail
93
+ // tail[i] = Salsa(blockIn[2*i+1] ^ head[i])
94
+ XorAndSalsa(out, head, input, (ii += 16), out, tail);
95
+ }
96
+ }
97
+ // Common prologue and epilogue for sync/async functions
98
+ function scryptInit(password, salt, _opts) {
99
+ // Maxmem - 1GB+1KB by default
100
+ const opts = checkOpts({
101
+ dkLen: 32,
102
+ asyncTick: 10,
103
+ maxmem: 1024 ** 3 + 1024,
104
+ }, _opts);
105
+ const { N, r, p, dkLen, asyncTick, maxmem, onProgress } = opts;
106
+ anumber(N, 'N');
107
+ anumber(r, 'r');
108
+ anumber(p, 'p');
109
+ anumber(dkLen, 'dkLen');
110
+ anumber(asyncTick, 'asyncTick');
111
+ anumber(maxmem, 'maxmem');
112
+ if (onProgress !== undefined && typeof onProgress !== 'function')
113
+ throw new Error('progressCb must be a function');
114
+ const blockSize = 128 * r;
115
+ const blockSize32 = blockSize / 4;
116
+ // Max N is 2^32 (Integrify is 32-bit).
117
+ // Real limit can be 2^22: some JS engines limit Uint8Array to 4GB.
118
+ // Spec check `N >= 2^(blockSize / 8)` is not done for compat with popular libs,
119
+ // which used incorrect r: 1, p: 8. Also, the check seems to be a spec error:
120
+ // https://www.rfc-editor.org/errata_search.php?rfc=7914
121
+ const pow32 = Math.pow(2, 32);
122
+ if (N <= 1 || (N & (N - 1)) !== 0 || N > pow32)
123
+ throw new Error('"N" expected a power of 2, and 2^1 <= N <= 2^32');
124
+ if (p < 1 || p > ((pow32 - 1) * 32) / blockSize)
125
+ throw new Error('"p" expected integer 1..((2^32 - 1) * 32) / (128 * r)');
126
+ // RFC 7914 §2 defines `dkLen` as a positive integer.
127
+ if (dkLen < 1 || dkLen > (pow32 - 1) * 32)
128
+ throw new Error('"dkLen" expected integer 1..(2^32 - 1) * 32');
129
+ // Include the shared `tmp` scratch block so `maxmem` matches noble's actual temporary allocation.
130
+ // Node requires more headroom here, so this accounting is intentionally noble-specific.
131
+ const memUsed = blockSize * (N + p + 1);
132
+ if (memUsed > maxmem)
133
+ throw new Error('"maxmem" limit was hit: memUsed(128*r*(N+p+1))=' + memUsed + ', maxmem=' + maxmem);
134
+ // [B0...Bp−1] ← PBKDF2HMAC-SHA256(Passphrase, Salt, 1, blockSize*ParallelizationFactor)
135
+ // Since it has only one iteration there is no reason to use async variant
136
+ const B = pbkdf2(sha256, password, salt, { c: 1, dkLen: blockSize * p });
137
+ const B32 = u32(B);
138
+ // Re-used between parallel iterations. Array(iterations) of B
139
+ const V = u32(new Uint8Array(blockSize * N));
140
+ const tmp = u32(new Uint8Array(blockSize));
141
+ let blockMixCb = () => { };
142
+ if (onProgress) {
143
+ const totalBlockMix = 2 * N * p;
144
+ // Invoke callback if progress changes from 10.01 to 10.02
145
+ // Allows to draw smooth progress bar on up to 8K screen
146
+ const callbackPer = Math.max(Math.floor(totalBlockMix / 10000), 1);
147
+ let blockMixCnt = 0;
148
+ blockMixCb = () => {
149
+ blockMixCnt++;
150
+ if (onProgress && (!(blockMixCnt % callbackPer) || blockMixCnt === totalBlockMix))
151
+ onProgress(blockMixCnt / totalBlockMix);
152
+ };
153
+ }
154
+ return { N, r, p, dkLen, blockSize32, V, B32, B, tmp, blockMixCb, asyncTick };
155
+ }
156
+ function scryptOutput(password, dkLen, B, V, tmp) {
157
+ // Shared final PBKDF2-and-cleanup step: keep the derived key, wipe the scrypt workspace.
158
+ const res = pbkdf2(sha256, password, B, { c: 1, dkLen });
159
+ clean(B, V, tmp);
160
+ return res;
161
+ }
162
+ /**
163
+ * Scrypt KDF from RFC 7914. See {@link ScryptOpts}.
164
+ * @param password - password or key material to derive from;
165
+ * JS string inputs are UTF-8 encoded first
166
+ * @param salt - unique salt bytes or string; JS string inputs are UTF-8 encoded first
167
+ * @param opts - Scrypt cost and memory parameters. `dkLen`, if provided,
168
+ * must be `>= 1` per RFC 7914 §2. See {@link ScryptOpts}.
169
+ * @returns Derived key bytes.
170
+ * @throws If the Scrypt cost, memory, or callback options are invalid. {@link Error}
171
+ * @example
172
+ * Derive a key with scrypt.
173
+ * ```ts
174
+ * scrypt('password', 'salt', { N: 2**18, r: 8, p: 1, dkLen: 32 });
175
+ * ```
176
+ */
177
+ export function scrypt(password, salt, opts) {
178
+ const { N, r, p, dkLen, blockSize32, V, B32, B, tmp, blockMixCb } = scryptInit(password, salt, opts);
179
+ swap32IfBE(B32);
180
+ for (let pi = 0; pi < p; pi++) {
181
+ const Pi = blockSize32 * pi;
182
+ for (let i = 0; i < blockSize32; i++)
183
+ V[i] = B32[Pi + i]; // V[0] = B[i]
184
+ for (let i = 0, pos = 0; i < N - 1; i++) {
185
+ BlockMix(V, pos, V, (pos += blockSize32), r); // V[i] = BlockMix(V[i-1]);
186
+ blockMixCb();
187
+ }
188
+ BlockMix(V, (N - 1) * blockSize32, B32, Pi, r); // Process last element
189
+ blockMixCb();
190
+ for (let i = 0; i < N; i++) {
191
+ // First u32 of the last 64-byte block (u32 is LE)
192
+ // RFC 7914 Integerify(X) uses the whole last 64-byte block, but mod N
193
+ // only depends on the low word here because N is a power of two and
194
+ // this implementation caps N at 2^32.
195
+ // & (N - 1) is % N as N is a power of 2, N & (N - 1) = 0 is checked
196
+ // above; >>> 0 for unsigned, input fits in u32.
197
+ const j = (B32[Pi + blockSize32 - 16] & (N - 1)) >>> 0; // j = Integrify(X) % iterations
198
+ // tmp = B ^ V[j]
199
+ for (let k = 0; k < blockSize32; k++)
200
+ tmp[k] = B32[Pi + k] ^ V[j * blockSize32 + k];
201
+ BlockMix(tmp, 0, B32, Pi, r); // B = BlockMix(B ^ V[j])
202
+ blockMixCb();
203
+ }
204
+ }
205
+ swap32IfBE(B32);
206
+ return scryptOutput(password, dkLen, B, V, tmp);
207
+ }
208
+ /**
209
+ * Scrypt KDF from RFC 7914. Async version. See {@link ScryptOpts}.
210
+ * @param password - password or key material to derive from;
211
+ * JS string inputs are UTF-8 encoded first
212
+ * @param salt - unique salt bytes or string; JS string inputs are UTF-8 encoded first
213
+ * @param opts - Scrypt cost and memory parameters. `dkLen`, if provided,
214
+ * must be `>= 1` per RFC 7914 §2. `asyncTick` is only a local
215
+ * scheduler-yield control for this JS wrapper, not part of RFC 7914.
216
+ * See {@link ScryptOpts}.
217
+ * @returns Promise resolving to derived key bytes.
218
+ * @throws If the Scrypt cost, memory, or callback options are invalid. {@link Error}
219
+ * @example
220
+ * Derive a key with scrypt asynchronously.
221
+ * ```ts
222
+ * await scryptAsync('password', 'salt', { N: 2**18, r: 8, p: 1, dkLen: 32 });
223
+ * ```
224
+ */
225
+ export async function scryptAsync(password, salt, opts) {
226
+ const { N, r, p, dkLen, blockSize32, V, B32, B, tmp, blockMixCb, asyncTick } = scryptInit(password, salt, opts);
227
+ swap32IfBE(B32);
228
+ for (let pi = 0; pi < p; pi++) {
229
+ const Pi = blockSize32 * pi;
230
+ for (let i = 0; i < blockSize32; i++)
231
+ V[i] = B32[Pi + i]; // V[0] = B[i]
232
+ let pos = 0;
233
+ await asyncLoop(N - 1, asyncTick, () => {
234
+ BlockMix(V, pos, V, (pos += blockSize32), r); // V[i] = BlockMix(V[i-1]);
235
+ blockMixCb();
236
+ });
237
+ BlockMix(V, (N - 1) * blockSize32, B32, Pi, r); // Process last element
238
+ blockMixCb();
239
+ await asyncLoop(N, asyncTick, () => {
240
+ // First u32 of the last 64-byte block (u32 is LE)
241
+ // RFC 7914 Integerify(X) uses the whole last 64-byte block, but mod N
242
+ // only depends on the low word here because N is a power of two and
243
+ // this implementation caps N at 2^32.
244
+ // & (N - 1) is % N as N is a power of 2, N & (N - 1) = 0 is checked
245
+ // above; >>> 0 for unsigned, input fits in u32.
246
+ const j = (B32[Pi + blockSize32 - 16] & (N - 1)) >>> 0; // j = Integrify(X) % iterations
247
+ // tmp = B ^ V[j]
248
+ for (let k = 0; k < blockSize32; k++)
249
+ tmp[k] = B32[Pi + k] ^ V[j * blockSize32 + k];
250
+ BlockMix(tmp, 0, B32, Pi, r); // B = BlockMix(B ^ V[j])
251
+ blockMixCb();
252
+ });
253
+ }
254
+ swap32IfBE(B32);
255
+ return scryptOutput(password, dkLen, B, V, tmp);
256
+ }