micro509 0.17.0 → 0.17.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.
- package/CHANGELOG.md +46 -1
- package/README.md +3 -0
- package/dist/kdf/argon2-arithmetic.js +15 -0
- package/dist/kdf/argon2-arithmetic.js.map +1 -0
- package/dist/kdf/argon2-core.js +486 -0
- package/dist/kdf/argon2-core.js.map +1 -0
- package/dist/kdf/argon2-memory.js +20 -0
- package/dist/kdf/argon2-memory.js.map +1 -0
- package/dist/kdf/argon2-portable.js +18 -0
- package/dist/kdf/argon2-portable.js.map +1 -0
- package/dist/kdf/argon2.d.ts +15 -9
- package/dist/kdf/argon2.js +32 -13
- package/dist/kdf/argon2.js.map +1 -1
- package/package.json +8 -14
package/CHANGELOG.md
CHANGED
|
@@ -19,6 +19,50 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
19
19
|
|
|
20
20
|
## [Unreleased]
|
|
21
21
|
|
|
22
|
+
## [0.17.1] - 2026-10-26
|
|
23
|
+
|
|
24
|
+
Derive Argon2 keys across supported runtimes with the TypeScript implementation,
|
|
25
|
+
including versions `0x10` and `0x13`. The implementation is checked against RFC
|
|
26
|
+
vectors, libargon2, Node crypto and the reference WASM oracle.
|
|
27
|
+
|
|
28
|
+
Repeated derivations reuse wiped working memory, and repeated installs reuse
|
|
29
|
+
verified compiler artifacts. The key-derivation guide demonstrates encryption,
|
|
30
|
+
saved parameters, message recovery and tamper rejection.
|
|
31
|
+
|
|
32
|
+
### Added
|
|
33
|
+
|
|
34
|
+
- Reproduce Argon2 performance comparisons with `bun run bench:argon2`, including
|
|
35
|
+
TypeScript, the WASM oracle, native WebCrypto, and Node crypto. Reports include
|
|
36
|
+
checked outputs, latency samples, throughput, event-loop delay, and baseline
|
|
37
|
+
and sampled peak RSS from a separate process for each backend and workload.
|
|
38
|
+
- Derive Argon2d, Argon2i, and Argon2id keys with versions `0x10` and `0x13`
|
|
39
|
+
across Bun, Node.js, Deno, browsers, and Cloudflare Workers. The complete
|
|
40
|
+
Argon2 and BLAKE2b implementation is TypeScript. `micro509/kdf` selects
|
|
41
|
+
between native WebCrypto and TypeScript automatically; `backend: 'native'`
|
|
42
|
+
and `backend: 'portable'` select one explicitly. Resource limits apply before
|
|
43
|
+
allocation, and owned working memory is cleared after derivation. The
|
|
44
|
+
C/WASM implementation serves as a test oracle for the TypeScript port.
|
|
45
|
+
[#203](https://github.com/kjanat/micro509/issues/203)
|
|
46
|
+
|
|
47
|
+
### Changed
|
|
48
|
+
|
|
49
|
+
- Reuse wiped TypeScript Argon2 matrices through a bounded pool of weak references,
|
|
50
|
+
reducing temporary memory growth across repeated derivations. Concurrent calls
|
|
51
|
+
own separate matrices.
|
|
52
|
+
- Sample benchmark RSS directly to avoid inherited parent-process peaks. Harden
|
|
53
|
+
compiler-cache environment writes and hash metadata and contents through one
|
|
54
|
+
open file handle.
|
|
55
|
+
|
|
56
|
+
- Cache TypeScript compiler bridge artifacts across installs and CI jobs, validating
|
|
57
|
+
build inputs and outputs before reuse. Rename the shared CI action to `setup`.
|
|
58
|
+
|
|
59
|
+
- The key-derivation guide demonstrates encryption, a serialized JSON record,
|
|
60
|
+
message recovery and rejection of modified ciphertext. Saved binary fields
|
|
61
|
+
use base64url for compact, readable output. The docs build checks examples
|
|
62
|
+
against the package declarations and runs the TypeScript Argon2 implementation.
|
|
63
|
+
- Install Tombi in shared CI setup and resolve it through PATH for formatting.
|
|
64
|
+
Simplify the autofix workflow.
|
|
65
|
+
|
|
22
66
|
## [0.17.0] - 2026-10-25
|
|
23
67
|
|
|
24
68
|
Work with OpenSSH keys, derive encryption keys with Argon2, and sign and verify
|
|
@@ -2028,7 +2072,8 @@ Initial prerelease. API may change before 1.0.
|
|
|
2028
2072
|
- Zero runtime dependencies, WebCrypto-native, tree-shakeable subpath exports;
|
|
2029
2073
|
runs on Node, Bun, Deno, browsers, and Cloudflare Workers.
|
|
2030
2074
|
|
|
2031
|
-
[Unreleased]: https://github.com/kjanat/micro509/compare/v0.17.
|
|
2075
|
+
[Unreleased]: https://github.com/kjanat/micro509/compare/v0.17.1...HEAD
|
|
2076
|
+
[0.17.1]: https://github.com/kjanat/micro509/compare/v0.17.0...v0.17.1
|
|
2032
2077
|
[0.17.0]: https://github.com/kjanat/micro509/compare/v0.16.0...v0.17.0
|
|
2033
2078
|
[0.16.0]: https://github.com/kjanat/micro509/compare/v0.15.0...v0.16.0
|
|
2034
2079
|
[0.15.0]: https://github.com/kjanat/micro509/compare/v0.14.0...v0.15.0
|
package/README.md
CHANGED
|
@@ -29,6 +29,9 @@ with keys and PKCS containers using WebCrypto.
|
|
|
29
29
|
Zero dependencies. Tree-shakeable subpath entrypoints. Pure WebCrypto. Runs in
|
|
30
30
|
Node, Bun, Deno, browsers, and Cloudflare Workers.
|
|
31
31
|
|
|
32
|
+
`micro509/kdf` derives Argon2d, Argon2i, and Argon2id keys across supported runtimes,
|
|
33
|
+
including versions `0x10` and `0x13`.
|
|
34
|
+
|
|
32
35
|
> **Prerelease** — API may change before 1.0.
|
|
33
36
|
|
|
34
37
|
## Install
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// src/kdf/argon2-arithmetic.ts
|
|
2
|
+
/**
|
|
3
|
+
* Exact high word of an unsigned 32×32 product; `low` is Math.imul(a, b).
|
|
4
|
+
*
|
|
5
|
+
* Subtracting the exact low word leaves a multiple of 2^32 with at most 2048
|
|
6
|
+
* units of floating-point error. Scaling by 2^32 reduces that error below
|
|
7
|
+
* 2^-20, so adding 0.5 and truncating recovers the exact integer high word.
|
|
8
|
+
* BlaMka already computes the low word and passes it here for reuse.
|
|
9
|
+
*/
|
|
10
|
+
function multiplyHigh(e, t, n) {
|
|
11
|
+
return (e * t - (n >>> 0)) / 4294967296 + .5 >>> 0;
|
|
12
|
+
}
|
|
13
|
+
export { multiplyHigh };
|
|
14
|
+
|
|
15
|
+
//# sourceMappingURL=argon2-arithmetic.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"argon2-arithmetic.js","names":["a","b","low"],"sources":["../../src/kdf/argon2-arithmetic.ts"],"sourcesContent":["/**\n * Exact high word of an unsigned 32×32 product; `low` is Math.imul(a, b).\n *\n * Subtracting the exact low word leaves a multiple of 2^32 with at most 2048\n * units of floating-point error. Scaling by 2^32 reduces that error below\n * 2^-20, so adding 0.5 and truncating recovers the exact integer high word.\n * BlaMka already computes the low word and passes it here for reuse.\n */\nexport function multiplyHigh(a: number, b: number, low: number): number {\n\treturn ((a * b - (low >>> 0)) / 0x1_0000_0000 + 0.5) >>> 0;\n}\n"],"mappings":";;;;;;;;;AAQA,SAAgB,aAAa,GAAW,GAAW,GAAqB;CACvE,QAASA,IAAIC,KAAKC,MAAQ,MAAM,aAAgB,OAAS;AAC1D"}
|
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
import { multiplyHigh as e } from "./argon2-arithmetic.js";
|
|
2
|
+
import { createMatrixPool as t } from "./argon2-memory.js";
|
|
3
|
+
// src/kdf/argon2-core.ts
|
|
4
|
+
const n = t(), r = 18446744073709551615n, i = [
|
|
5
|
+
7640891576956012808n,
|
|
6
|
+
13503953896175478587n,
|
|
7
|
+
4354685564936845355n,
|
|
8
|
+
11912009170470909681n,
|
|
9
|
+
5840696475078001361n,
|
|
10
|
+
11170449401992604703n,
|
|
11
|
+
2270897969802886507n,
|
|
12
|
+
6620516959819538809n
|
|
13
|
+
], a = [
|
|
14
|
+
[
|
|
15
|
+
0,
|
|
16
|
+
1,
|
|
17
|
+
2,
|
|
18
|
+
3,
|
|
19
|
+
4,
|
|
20
|
+
5,
|
|
21
|
+
6,
|
|
22
|
+
7,
|
|
23
|
+
8,
|
|
24
|
+
9,
|
|
25
|
+
10,
|
|
26
|
+
11,
|
|
27
|
+
12,
|
|
28
|
+
13,
|
|
29
|
+
14,
|
|
30
|
+
15
|
|
31
|
+
],
|
|
32
|
+
[
|
|
33
|
+
14,
|
|
34
|
+
10,
|
|
35
|
+
4,
|
|
36
|
+
8,
|
|
37
|
+
9,
|
|
38
|
+
15,
|
|
39
|
+
13,
|
|
40
|
+
6,
|
|
41
|
+
1,
|
|
42
|
+
12,
|
|
43
|
+
0,
|
|
44
|
+
2,
|
|
45
|
+
11,
|
|
46
|
+
7,
|
|
47
|
+
5,
|
|
48
|
+
3
|
|
49
|
+
],
|
|
50
|
+
[
|
|
51
|
+
11,
|
|
52
|
+
8,
|
|
53
|
+
12,
|
|
54
|
+
0,
|
|
55
|
+
5,
|
|
56
|
+
2,
|
|
57
|
+
15,
|
|
58
|
+
13,
|
|
59
|
+
10,
|
|
60
|
+
14,
|
|
61
|
+
3,
|
|
62
|
+
6,
|
|
63
|
+
7,
|
|
64
|
+
1,
|
|
65
|
+
9,
|
|
66
|
+
4
|
|
67
|
+
],
|
|
68
|
+
[
|
|
69
|
+
7,
|
|
70
|
+
9,
|
|
71
|
+
3,
|
|
72
|
+
1,
|
|
73
|
+
13,
|
|
74
|
+
12,
|
|
75
|
+
11,
|
|
76
|
+
14,
|
|
77
|
+
2,
|
|
78
|
+
6,
|
|
79
|
+
5,
|
|
80
|
+
10,
|
|
81
|
+
4,
|
|
82
|
+
0,
|
|
83
|
+
15,
|
|
84
|
+
8
|
|
85
|
+
],
|
|
86
|
+
[
|
|
87
|
+
9,
|
|
88
|
+
0,
|
|
89
|
+
5,
|
|
90
|
+
7,
|
|
91
|
+
2,
|
|
92
|
+
4,
|
|
93
|
+
10,
|
|
94
|
+
15,
|
|
95
|
+
14,
|
|
96
|
+
1,
|
|
97
|
+
11,
|
|
98
|
+
12,
|
|
99
|
+
6,
|
|
100
|
+
8,
|
|
101
|
+
3,
|
|
102
|
+
13
|
|
103
|
+
],
|
|
104
|
+
[
|
|
105
|
+
2,
|
|
106
|
+
12,
|
|
107
|
+
6,
|
|
108
|
+
10,
|
|
109
|
+
0,
|
|
110
|
+
11,
|
|
111
|
+
8,
|
|
112
|
+
3,
|
|
113
|
+
4,
|
|
114
|
+
13,
|
|
115
|
+
7,
|
|
116
|
+
5,
|
|
117
|
+
15,
|
|
118
|
+
14,
|
|
119
|
+
1,
|
|
120
|
+
9
|
|
121
|
+
],
|
|
122
|
+
[
|
|
123
|
+
12,
|
|
124
|
+
5,
|
|
125
|
+
1,
|
|
126
|
+
15,
|
|
127
|
+
14,
|
|
128
|
+
13,
|
|
129
|
+
4,
|
|
130
|
+
10,
|
|
131
|
+
0,
|
|
132
|
+
7,
|
|
133
|
+
6,
|
|
134
|
+
3,
|
|
135
|
+
9,
|
|
136
|
+
2,
|
|
137
|
+
8,
|
|
138
|
+
11
|
|
139
|
+
],
|
|
140
|
+
[
|
|
141
|
+
13,
|
|
142
|
+
11,
|
|
143
|
+
7,
|
|
144
|
+
14,
|
|
145
|
+
12,
|
|
146
|
+
1,
|
|
147
|
+
3,
|
|
148
|
+
9,
|
|
149
|
+
5,
|
|
150
|
+
0,
|
|
151
|
+
15,
|
|
152
|
+
4,
|
|
153
|
+
8,
|
|
154
|
+
6,
|
|
155
|
+
2,
|
|
156
|
+
10
|
|
157
|
+
],
|
|
158
|
+
[
|
|
159
|
+
6,
|
|
160
|
+
15,
|
|
161
|
+
14,
|
|
162
|
+
9,
|
|
163
|
+
11,
|
|
164
|
+
3,
|
|
165
|
+
0,
|
|
166
|
+
8,
|
|
167
|
+
12,
|
|
168
|
+
2,
|
|
169
|
+
13,
|
|
170
|
+
7,
|
|
171
|
+
1,
|
|
172
|
+
4,
|
|
173
|
+
10,
|
|
174
|
+
5
|
|
175
|
+
],
|
|
176
|
+
[
|
|
177
|
+
10,
|
|
178
|
+
2,
|
|
179
|
+
8,
|
|
180
|
+
4,
|
|
181
|
+
7,
|
|
182
|
+
6,
|
|
183
|
+
1,
|
|
184
|
+
5,
|
|
185
|
+
15,
|
|
186
|
+
11,
|
|
187
|
+
9,
|
|
188
|
+
14,
|
|
189
|
+
3,
|
|
190
|
+
12,
|
|
191
|
+
13,
|
|
192
|
+
0
|
|
193
|
+
],
|
|
194
|
+
[
|
|
195
|
+
0,
|
|
196
|
+
1,
|
|
197
|
+
2,
|
|
198
|
+
3,
|
|
199
|
+
4,
|
|
200
|
+
5,
|
|
201
|
+
6,
|
|
202
|
+
7,
|
|
203
|
+
8,
|
|
204
|
+
9,
|
|
205
|
+
10,
|
|
206
|
+
11,
|
|
207
|
+
12,
|
|
208
|
+
13,
|
|
209
|
+
14,
|
|
210
|
+
15
|
|
211
|
+
],
|
|
212
|
+
[
|
|
213
|
+
14,
|
|
214
|
+
10,
|
|
215
|
+
4,
|
|
216
|
+
8,
|
|
217
|
+
9,
|
|
218
|
+
15,
|
|
219
|
+
13,
|
|
220
|
+
6,
|
|
221
|
+
1,
|
|
222
|
+
12,
|
|
223
|
+
0,
|
|
224
|
+
2,
|
|
225
|
+
11,
|
|
226
|
+
7,
|
|
227
|
+
5,
|
|
228
|
+
3
|
|
229
|
+
]
|
|
230
|
+
], o = [
|
|
231
|
+
[
|
|
232
|
+
0,
|
|
233
|
+
4,
|
|
234
|
+
8,
|
|
235
|
+
12
|
|
236
|
+
],
|
|
237
|
+
[
|
|
238
|
+
1,
|
|
239
|
+
5,
|
|
240
|
+
9,
|
|
241
|
+
13
|
|
242
|
+
],
|
|
243
|
+
[
|
|
244
|
+
2,
|
|
245
|
+
6,
|
|
246
|
+
10,
|
|
247
|
+
14
|
|
248
|
+
],
|
|
249
|
+
[
|
|
250
|
+
3,
|
|
251
|
+
7,
|
|
252
|
+
11,
|
|
253
|
+
15
|
|
254
|
+
],
|
|
255
|
+
[
|
|
256
|
+
0,
|
|
257
|
+
5,
|
|
258
|
+
10,
|
|
259
|
+
15
|
|
260
|
+
],
|
|
261
|
+
[
|
|
262
|
+
1,
|
|
263
|
+
6,
|
|
264
|
+
11,
|
|
265
|
+
12
|
|
266
|
+
],
|
|
267
|
+
[
|
|
268
|
+
2,
|
|
269
|
+
7,
|
|
270
|
+
8,
|
|
271
|
+
13
|
|
272
|
+
],
|
|
273
|
+
[
|
|
274
|
+
3,
|
|
275
|
+
4,
|
|
276
|
+
9,
|
|
277
|
+
14
|
|
278
|
+
]
|
|
279
|
+
];
|
|
280
|
+
function read32(e, t) {
|
|
281
|
+
return ((e[t] ?? 0) | (e[t + 1] ?? 0) << 8 | (e[t + 2] ?? 0) << 16 | (e[t + 3] ?? 0) << 24) >>> 0;
|
|
282
|
+
}
|
|
283
|
+
function write32(e, t, n) {
|
|
284
|
+
e[t] = n & 255, e[t + 1] = n >>> 8 & 255, e[t + 2] = n >>> 16 & 255, e[t + 3] = n >>> 24;
|
|
285
|
+
}
|
|
286
|
+
function read64(e, t) {
|
|
287
|
+
let n = 0n;
|
|
288
|
+
for (let r = 7; r >= 0; --r) n = n << 8n | BigInt(e[t + r] ?? 0);
|
|
289
|
+
return n;
|
|
290
|
+
}
|
|
291
|
+
function concat(...e) {
|
|
292
|
+
let t = new Uint8Array(e.reduce((e, t) => e + t.length, 0)), n = 0;
|
|
293
|
+
for (let r of e) t.set(r, n), n += r.length;
|
|
294
|
+
return t;
|
|
295
|
+
}
|
|
296
|
+
function blakeMix(e, t, n, i, a, o, s) {
|
|
297
|
+
e[t] = (e[t] ?? 0n) + (e[n] ?? 0n) + o & r, e[a] = rotateRight((e[a] ?? 0n) ^ (e[t] ?? 0n), 32), e[i] = (e[i] ?? 0n) + (e[a] ?? 0n) & r, e[n] = rotateRight((e[n] ?? 0n) ^ (e[i] ?? 0n), 24), e[t] = (e[t] ?? 0n) + (e[n] ?? 0n) + s & r, e[a] = rotateRight((e[a] ?? 0n) ^ (e[t] ?? 0n), 16), e[i] = (e[i] ?? 0n) + (e[a] ?? 0n) & r, e[n] = rotateRight((e[n] ?? 0n) ^ (e[i] ?? 0n), 63);
|
|
298
|
+
}
|
|
299
|
+
function rotateRight(e, t) {
|
|
300
|
+
let n = BigInt(t);
|
|
301
|
+
return (e >> n | e << 64n - n) & r;
|
|
302
|
+
}
|
|
303
|
+
function blakeRound(e, t, n) {
|
|
304
|
+
for (let [r, [i, a, s, c]] of o.entries()) blakeMix(e, i, a, s, c, t[n[r * 2] ?? 0] ?? 0n, t[n[r * 2 + 1] ?? 0] ?? 0n);
|
|
305
|
+
}
|
|
306
|
+
function blake2bCompress(e, t, n, o) {
|
|
307
|
+
let s = Array.from({ length: 16 }, (e, n) => read64(t, n * 8)), c = [...e, ...i];
|
|
308
|
+
try {
|
|
309
|
+
c[12] = (c[12] ?? 0n) ^ n & r, c[13] = (c[13] ?? 0n) ^ n >> 64n, o && (c[14] = (c[14] ?? 0n) ^ r);
|
|
310
|
+
for (let e of a) blakeRound(c, s, e);
|
|
311
|
+
for (let t = 0; t < 8; t += 1) e[t] = (e[t] ?? 0n) ^ (c[t] ?? 0n) ^ (c[t + 8] ?? 0n);
|
|
312
|
+
} finally {
|
|
313
|
+
s.fill(0n), c.fill(0n);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
function blake2b(e, t) {
|
|
317
|
+
let n = [...i], r = /* @__PURE__ */ new Uint8Array(128);
|
|
318
|
+
try {
|
|
319
|
+
n[0] = (n[0] ?? 0n) ^ (16842752n | BigInt(t));
|
|
320
|
+
let i = 0;
|
|
321
|
+
for (; e.length - i > 128;) blake2bCompress(n, e.subarray(i, i + 128), BigInt(i + 128), !1), i += 128;
|
|
322
|
+
r.set(e.subarray(i)), blake2bCompress(n, r, BigInt(e.length), !0);
|
|
323
|
+
let a = new Uint8Array(t);
|
|
324
|
+
for (let e = 0; e < t; e += 1) a[e] = Number((n[Math.floor(e / 8)] ?? 0n) >> BigInt(e % 8 * 8) & 255n);
|
|
325
|
+
return a;
|
|
326
|
+
} finally {
|
|
327
|
+
n.fill(0n), r.fill(0);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
function blakeLong(e, t) {
|
|
331
|
+
let n = concat(Uint8Array.from([
|
|
332
|
+
t & 255,
|
|
333
|
+
t >>> 8 & 255,
|
|
334
|
+
t >>> 16 & 255,
|
|
335
|
+
t >>> 24
|
|
336
|
+
]), e), r = /* @__PURE__ */ new Uint8Array();
|
|
337
|
+
try {
|
|
338
|
+
if (t <= 64) return blake2b(n, t);
|
|
339
|
+
r = blake2b(n, 64);
|
|
340
|
+
let e = new Uint8Array(t), i = 0;
|
|
341
|
+
for (; t - i > 64;) if (e.set(r.subarray(0, 32), i), i += 32, t - i > 64) {
|
|
342
|
+
let e = blake2b(r, 64);
|
|
343
|
+
r.fill(0), r = e;
|
|
344
|
+
}
|
|
345
|
+
let a = blake2b(r, t - i);
|
|
346
|
+
try {
|
|
347
|
+
return e.set(a, i), e;
|
|
348
|
+
} finally {
|
|
349
|
+
a.fill(0);
|
|
350
|
+
}
|
|
351
|
+
} finally {
|
|
352
|
+
n.fill(0), r.fill(0);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
function initialHash(e, t) {
|
|
356
|
+
let n = e.password, r = e.salt, i = e.secret ?? /* @__PURE__ */ new Uint8Array(), a = e.associatedData ?? /* @__PURE__ */ new Uint8Array(), o = /* @__PURE__ */ new Uint8Array(28), s = [
|
|
357
|
+
e.parallelism,
|
|
358
|
+
e.outputBytes,
|
|
359
|
+
e.memoryKiB,
|
|
360
|
+
e.passes,
|
|
361
|
+
e.version,
|
|
362
|
+
t,
|
|
363
|
+
n.length
|
|
364
|
+
], c = 0;
|
|
365
|
+
for (let e of s) write32(o, c, e), c += 4;
|
|
366
|
+
let l = /* @__PURE__ */ new Uint8Array(12);
|
|
367
|
+
write32(l, 0, r.length), write32(l, 4, i.length), write32(l, 8, a.length);
|
|
368
|
+
let u = concat(o.subarray(0, 28), n, l.subarray(0, 4), r, l.subarray(4, 8), i, l.subarray(8, 12), a);
|
|
369
|
+
try {
|
|
370
|
+
return blake2b(u, 64);
|
|
371
|
+
} finally {
|
|
372
|
+
u.fill(0);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
function argonMix(t, n, r, i, a) {
|
|
376
|
+
let o = t[n] ?? 0, s = (t[n + 1] ?? 0) | 0, c = t[r] ?? 0, l = (t[r + 1] ?? 0) | 0, u = t[i] ?? 0, d = (t[i + 1] ?? 0) | 0, f = t[a] ?? 0, p = (t[a + 1] ?? 0) | 0, m, h, g, _;
|
|
377
|
+
m = Math.imul(o, c), h = o + c + (m << 1 >>> 0), s = s + l + (e(o, c, m) << 1 | m >>> 31) + (h / 4294967296 | 0) | 0, o = h >>> 0, g = f ^ o, _ = p ^ s, f = _ >>> 0, p = g | 0, m = Math.imul(u, f), h = u + f + (m << 1 >>> 0), d = d + p + (e(u, f, m) << 1 | m >>> 31) + (h / 4294967296 | 0) | 0, u = h >>> 0, g = c ^ u, _ = l ^ d, c = (g >>> 24 | _ << 8) >>> 0, l = _ >>> 24 | g << 8, m = Math.imul(o, c), h = o + c + (m << 1 >>> 0), s = s + l + (e(o, c, m) << 1 | m >>> 31) + (h / 4294967296 | 0) | 0, o = h >>> 0, g = f ^ o, _ = p ^ s, f = (g >>> 16 | _ << 16) >>> 0, p = _ >>> 16 | g << 16, m = Math.imul(u, f), h = u + f + (m << 1 >>> 0), d = d + p + (e(u, f, m) << 1 | m >>> 31) + (h / 4294967296 | 0) | 0, u = h >>> 0, g = c ^ u, _ = l ^ d, c = (g << 1 | _ >>> 31) >>> 0, l = _ << 1 | g >>> 31, t[n] = o, t[n + 1] = s, t[r] = c, t[r + 1] = l, t[i] = u, t[i + 1] = d, t[a] = f, t[a + 1] = p;
|
|
378
|
+
}
|
|
379
|
+
function argonRound(e, t, n, r) {
|
|
380
|
+
argonMix(e, t, t + 2 * n, t + 4 * n, t + 6 * n), argonMix(e, t + r, t + 2 * n + r, t + 4 * n + r, t + 6 * n + r), argonMix(e, t + n, t + 3 * n, t + 5 * n, t + 7 * n), argonMix(e, t + n + r, t + 3 * n + r, t + 5 * n + r, t + 7 * n + r), argonMix(e, t, t + 2 * n + r, t + 5 * n, t + 7 * n + r), argonMix(e, t + r, t + 3 * n, t + 5 * n + r, t + 6 * n), argonMix(e, t + n, t + 3 * n + r, t + 4 * n, t + 6 * n + r), argonMix(e, t + n + r, t + 2 * n, t + 4 * n + r, t + 7 * n);
|
|
381
|
+
}
|
|
382
|
+
function permuteBlock(e) {
|
|
383
|
+
for (let t = 0; t < 256; t += 32) argonRound(e, t, 4, 2);
|
|
384
|
+
for (let t = 0; t < 32; t += 4) argonRound(e, t, 32, 2);
|
|
385
|
+
}
|
|
386
|
+
function fillBlock(e, t, n, r, i, a, o) {
|
|
387
|
+
for (let r = 0; r < 256; r += 1) {
|
|
388
|
+
let i = (e[t + r] ?? 0) ^ (e[n + r] ?? 0);
|
|
389
|
+
a[r] = i, o[r] = i;
|
|
390
|
+
}
|
|
391
|
+
if (permuteBlock(o), i) for (let t = 0; t < 256; t += 1) e[r + t] = (a[t] ?? 0) ^ (o[t] ?? 0) ^ (e[r + t] ?? 0);
|
|
392
|
+
else for (let t = 0; t < 256; t += 1) e[r + t] = (a[t] ?? 0) ^ (o[t] ?? 0);
|
|
393
|
+
}
|
|
394
|
+
function indexAlpha(t, n, r, i, a, o, s) {
|
|
395
|
+
let c;
|
|
396
|
+
c = t === 0 ? n === 0 ? r - 1 : n * a + (i ? r - 1 : r === 0 ? -1 : 0) : o - a + (i ? r - 1 : r === 0 ? -1 : 0);
|
|
397
|
+
let l = e(s, s, Math.imul(s, s)), u = c - 1 - e(c, l, Math.imul(c, l));
|
|
398
|
+
return ((t === 0 || n === 3 ? 0 : (n + 1) * a) + u) % o;
|
|
399
|
+
}
|
|
400
|
+
function dataIndependent(e, t, n) {
|
|
401
|
+
return e === 1 || e === 2 && t === 0 && n < 2;
|
|
402
|
+
}
|
|
403
|
+
function compressBlocks(e, t, n, r, i) {
|
|
404
|
+
for (let n = 0; n < 256; n += 1) {
|
|
405
|
+
let a = (e[n] ?? 0) ^ (t[n] ?? 0);
|
|
406
|
+
r[n] = a, i[n] = a;
|
|
407
|
+
}
|
|
408
|
+
permuteBlock(i);
|
|
409
|
+
for (let e = 0; e < 256; e += 1) n[e] = (r[e] ?? 0) ^ (i[e] ?? 0);
|
|
410
|
+
}
|
|
411
|
+
function generateAddressBlock(e, t, n, r, i, a) {
|
|
412
|
+
e[12] = (e[12] ?? 0) + 1 >>> 0, compressBlocks(n, e, a, r, i), compressBlocks(n, a, t, r, i);
|
|
413
|
+
}
|
|
414
|
+
function toWords(e) {
|
|
415
|
+
let t = /* @__PURE__ */ new Uint32Array(256);
|
|
416
|
+
for (let n = 0; n < 256; n += 1) t[n] = read32(e, n * 4);
|
|
417
|
+
return t;
|
|
418
|
+
}
|
|
419
|
+
function fillIndexedBlock(e, t, n, r, i) {
|
|
420
|
+
let { pass: a, slice: o, lane: s } = n, { memory: c, laneLength: l, segmentLength: u, original: d, permuted: f } = t, p = s * l + o * u + r, m = (p % l === 0 ? p + l - 1 : p - 1) * 256, h = i === void 0 ? m : r % 128 * 2, g = i ?? c, _ = g[h] ?? 0, v = (g[h + 1] ?? 0) % e.parallelism;
|
|
421
|
+
a === 0 && o === 0 && (v = s);
|
|
422
|
+
let y = indexAlpha(a, o, r, v === s, u, l, _), b = p * 256;
|
|
423
|
+
fillBlock(c, m, (v * l + y) * 256, b, e.version !== 16 && a !== 0, d, f);
|
|
424
|
+
}
|
|
425
|
+
async function fillSegment(e, t, n, r) {
|
|
426
|
+
let { pass: i, slice: a, lane: o } = r, { memoryBlocks: s, segmentLength: c, original: l, permuted: u, zero: d, addressTemporary: f } = n, p = dataIndependent(t, i, a), m = /* @__PURE__ */ new Uint32Array(256), h = /* @__PURE__ */ new Uint32Array(256);
|
|
427
|
+
try {
|
|
428
|
+
p && (m[0] = i, m[2] = o, m[4] = a, m[6] = s, m[8] = e.passes, m[10] = t);
|
|
429
|
+
let g = i === 0 && a === 0 ? 2 : 0;
|
|
430
|
+
p && g !== 0 && generateAddressBlock(m, h, d, l, u, f);
|
|
431
|
+
for (let t = g; t < c; t += 512) {
|
|
432
|
+
let i = new Promise((e) => setTimeout(e, 0)), a = Math.min(t + 512, c);
|
|
433
|
+
for (let i = t; i < a; i += 1) p && i % 128 == 0 && generateAddressBlock(m, h, d, l, u, f), fillIndexedBlock(e, n, r, i, p ? h : void 0);
|
|
434
|
+
await i;
|
|
435
|
+
}
|
|
436
|
+
} finally {
|
|
437
|
+
m.fill(0), h.fill(0), f.fill(0);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
async function deriveArgon2TypeScript(e) {
|
|
441
|
+
let t = {
|
|
442
|
+
argon2d: 0,
|
|
443
|
+
argon2i: 1,
|
|
444
|
+
argon2id: 2
|
|
445
|
+
}[e.algorithm], r = Math.floor(e.memoryKiB / (4 * e.parallelism)) * 4 * e.parallelism, i = r / e.parallelism, a = i / 4, o = /* @__PURE__ */ new Uint32Array(), s = /* @__PURE__ */ new Uint8Array(), c = /* @__PURE__ */ new Uint32Array(), l = /* @__PURE__ */ new Uint32Array(), u = /* @__PURE__ */ new Uint32Array(), d = /* @__PURE__ */ new Uint32Array(), f = /* @__PURE__ */ new Uint8Array(), p = /* @__PURE__ */ new Uint32Array();
|
|
446
|
+
try {
|
|
447
|
+
o = n.take(r * 256), s = initialHash(e, t), c = /* @__PURE__ */ new Uint32Array(256), l = /* @__PURE__ */ new Uint32Array(256), u = /* @__PURE__ */ new Uint32Array(256), d = /* @__PURE__ */ new Uint32Array(256);
|
|
448
|
+
for (let t = 0; t < e.parallelism; t += 1) for (let e = 0; e < 2; e += 1) {
|
|
449
|
+
let n = /* @__PURE__ */ new Uint8Array(8);
|
|
450
|
+
write32(n, 0, e), write32(n, 4, t);
|
|
451
|
+
let r = concat(s, n), a = /* @__PURE__ */ new Uint8Array(), c = /* @__PURE__ */ new Uint32Array();
|
|
452
|
+
try {
|
|
453
|
+
a = blakeLong(r, 1024), c = toWords(a), o.set(c, (t * i + e) * 256);
|
|
454
|
+
} finally {
|
|
455
|
+
r.fill(0), a.fill(0), c.fill(0);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
let m = {
|
|
459
|
+
memory: o,
|
|
460
|
+
memoryBlocks: r,
|
|
461
|
+
laneLength: i,
|
|
462
|
+
segmentLength: a,
|
|
463
|
+
original: c,
|
|
464
|
+
permuted: l,
|
|
465
|
+
zero: u,
|
|
466
|
+
addressTemporary: d
|
|
467
|
+
};
|
|
468
|
+
for (let n = 0; n < e.passes; n += 1) for (let r = 0; r < 4; r += 1) for (let i = 0; i < e.parallelism; i += 1) await fillSegment(e, t, m, {
|
|
469
|
+
pass: n,
|
|
470
|
+
slice: r,
|
|
471
|
+
lane: i
|
|
472
|
+
});
|
|
473
|
+
f = /* @__PURE__ */ new Uint8Array(1024), p = toWords(f);
|
|
474
|
+
for (let t = 0; t < e.parallelism; t += 1) {
|
|
475
|
+
let e = (t * i + i - 1) * 256;
|
|
476
|
+
for (let t = 0; t < 256; t += 1) p[t] = (p[t] ?? 0) ^ (o[e + t] ?? 0);
|
|
477
|
+
}
|
|
478
|
+
for (let e = 0; e < 256; e += 1) write32(f, e * 4, p[e] ?? 0);
|
|
479
|
+
return blakeLong(f, e.outputBytes);
|
|
480
|
+
} finally {
|
|
481
|
+
s.fill(0), c.fill(0), l.fill(0), u.fill(0), d.fill(0), f.fill(0), p.fill(0), n.release(o);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
export { deriveArgon2TypeScript };
|
|
485
|
+
|
|
486
|
+
//# sourceMappingURL=argon2-core.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"argon2-core.js","names":["matrices","createMatrixPool","MASK_64","IV","SIGMA","ROUND_INDEXES","bytes","offset","value","index","parts","output","length","part","a","v","b","x","d","c","y","shift","bits","words","sigma","block","h","count","last","finalBlock","outputBytes","input","seed","next","password","salt","secret","associatedData","fields","values","type","tail","message","aLow","aHigh","bLow","bHigh","cLow","cHigh","dLow","dHigh","product","sum","low","high","multiplyHigh","stride","pair","memory","previousOffset","referenceOffset","permuted","withXor","destinationOffset","original","areaSize","pass","slice","segmentLength","sameLane","laneLength","squareHigh","pseudoRandom","relative","previous","reference","inputBlock","zero","addressTemporary","addressBlock","position","workspace","currentBlock","lane","pseudoOffset","pseudo","pseudoLow","referenceLane","referenceIndex","destinationBlock","independent","memoryBlocks","startingIndex","start","pause","resolve","end","h0","finalWords","suffix","lastOffset"],"sources":["../../src/kdf/argon2-core.ts"],"sourcesContent":["/**\n * Argon2 core: RFC 9106 sections 3.2–3.6, with version 0x10 compatibility.\n * BLAKE2b uses 64-bit BigInts; the memory matrix uses little-endian pairs of\n * 32-bit words. Each derivation owns its state and clears mutable work buffers.\n */\nimport type { Argon2Input } from '#micro509/kdf/argon2';\nimport { multiplyHigh } from '#micro509/kdf/argon2-arithmetic';\nimport { createMatrixPool } from '#micro509/kdf/argon2-memory';\n\nconst matrices = createMatrixPool();\n\nconst MASK_64 = 0xffff_ffff_ffff_ffffn;\nconst IV = [\n\t0x6a09e667f3bcc908n,\n\t0xbb67ae8584caa73bn,\n\t0x3c6ef372fe94f82bn,\n\t0xa54ff53a5f1d36f1n,\n\t0x510e527fade682d1n,\n\t0x9b05688c2b3e6c1fn,\n\t0x1f83d9abfb41bd6bn,\n\t0x5be0cd19137e2179n,\n];\nconst SIGMA = [\n\t[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],\n\t[14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3],\n\t[11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4],\n\t[7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8],\n\t[9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13],\n\t[2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9],\n\t[12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11],\n\t[13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10],\n\t[6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5],\n\t[10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0],\n\t[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],\n\t[14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3],\n];\n// Column and diagonal quarter-round indexes shared by BLAKE2b and BlaMka.\nconst ROUND_INDEXES = [\n\t[0, 4, 8, 12],\n\t[1, 5, 9, 13],\n\t[2, 6, 10, 14],\n\t[3, 7, 11, 15],\n\t[0, 5, 10, 15],\n\t[1, 6, 11, 12],\n\t[2, 7, 8, 13],\n\t[3, 4, 9, 14],\n] as const;\ntype MutableWords = { length: number; [index: number]: bigint };\n\nfunction read32(bytes: Uint8Array, offset: number): number {\n\treturn (\n\t\t((bytes[offset] ?? 0) |\n\t\t\t((bytes[offset + 1] ?? 0) << 8) |\n\t\t\t((bytes[offset + 2] ?? 0) << 16) |\n\t\t\t((bytes[offset + 3] ?? 0) << 24)) >>>\n\t\t0\n\t);\n}\n\nfunction write32(bytes: Uint8Array, offset: number, value: number): void {\n\tbytes[offset] = value & 0xff;\n\tbytes[offset + 1] = (value >>> 8) & 0xff;\n\tbytes[offset + 2] = (value >>> 16) & 0xff;\n\tbytes[offset + 3] = value >>> 24;\n}\n\nfunction read64(bytes: Uint8Array, offset: number): bigint {\n\tlet value = 0n;\n\tfor (let index = 7; index >= 0; index -= 1) {\n\t\tvalue = (value << 8n) | BigInt(bytes[offset + index] ?? 0);\n\t}\n\treturn value;\n}\n\nfunction concat(...parts: Uint8Array[]): Uint8Array {\n\tconst output = new Uint8Array(parts.reduce((length, part) => length + part.length, 0));\n\tlet offset = 0;\n\tfor (const part of parts) {\n\t\toutput.set(part, offset);\n\t\toffset += part.length;\n\t}\n\treturn output;\n}\n\nfunction blakeMix(\n\tv: MutableWords,\n\ta: number,\n\tb: number,\n\tc: number,\n\td: number,\n\tx: bigint,\n\ty: bigint,\n): void {\n\tv[a] = ((v[a] ?? 0n) + (v[b] ?? 0n) + x) & MASK_64;\n\tv[d] = rotateRight((v[d] ?? 0n) ^ (v[a] ?? 0n), 32);\n\tv[c] = ((v[c] ?? 0n) + (v[d] ?? 0n)) & MASK_64;\n\tv[b] = rotateRight((v[b] ?? 0n) ^ (v[c] ?? 0n), 24);\n\tv[a] = ((v[a] ?? 0n) + (v[b] ?? 0n) + y) & MASK_64;\n\tv[d] = rotateRight((v[d] ?? 0n) ^ (v[a] ?? 0n), 16);\n\tv[c] = ((v[c] ?? 0n) + (v[d] ?? 0n)) & MASK_64;\n\tv[b] = rotateRight((v[b] ?? 0n) ^ (v[c] ?? 0n), 63);\n}\n\nfunction rotateRight(value: bigint, bits: number): bigint {\n\tconst shift = BigInt(bits);\n\treturn ((value >> shift) | (value << (64n - shift))) & MASK_64;\n}\n\nfunction blakeRound(v: MutableWords, words: bigint[], sigma: number[]): void {\n\tfor (const [index, [a, b, c, d]] of ROUND_INDEXES.entries()) {\n\t\tblakeMix(\n\t\t\tv,\n\t\t\ta,\n\t\t\tb,\n\t\t\tc,\n\t\t\td,\n\t\t\twords[sigma[index * 2] ?? 0] ?? 0n,\n\t\t\twords[sigma[index * 2 + 1] ?? 0] ?? 0n,\n\t\t);\n\t}\n}\n\nfunction blake2bCompress(h: bigint[], block: Uint8Array, count: bigint, last: boolean): void {\n\tconst words = Array.from({ length: 16 }, (_, index) => read64(block, index * 8));\n\tconst v = [...h, ...IV];\n\ttry {\n\t\tv[12] = (v[12] ?? 0n) ^ (count & MASK_64);\n\t\tv[13] = (v[13] ?? 0n) ^ (count >> 64n);\n\t\tif (last) v[14] = (v[14] ?? 0n) ^ MASK_64;\n\t\tfor (const sigma of SIGMA) blakeRound(v, words, sigma);\n\t\tfor (let index = 0; index < 8; index += 1) {\n\t\t\th[index] = (h[index] ?? 0n) ^ (v[index] ?? 0n) ^ (v[index + 8] ?? 0n);\n\t\t}\n\t} finally {\n\t\twords.fill(0n);\n\t\tv.fill(0n);\n\t}\n}\n\nfunction blake2b(input: Uint8Array, outputBytes: number): Uint8Array {\n\tconst h = [...IV];\n\tconst finalBlock = new Uint8Array(128);\n\ttry {\n\t\th[0] = (h[0] ?? 0n) ^ (0x01010000n | BigInt(outputBytes));\n\t\tlet offset = 0;\n\t\twhile (input.length - offset > 128) {\n\t\t\tblake2bCompress(h, input.subarray(offset, offset + 128), BigInt(offset + 128), false);\n\t\t\toffset += 128;\n\t\t}\n\t\tfinalBlock.set(input.subarray(offset));\n\t\tblake2bCompress(h, finalBlock, BigInt(input.length), true);\n\t\tconst output = new Uint8Array(outputBytes);\n\t\tfor (let index = 0; index < outputBytes; index += 1) {\n\t\t\toutput[index] = Number(((h[Math.floor(index / 8)] ?? 0n) >> BigInt((index % 8) * 8)) & 0xffn);\n\t\t}\n\t\treturn output;\n\t} finally {\n\t\th.fill(0n);\n\t\tfinalBlock.fill(0);\n\t}\n}\n\nfunction blakeLong(input: Uint8Array, outputBytes: number): Uint8Array {\n\tconst seed = concat(\n\t\tUint8Array.from([\n\t\t\toutputBytes & 0xff,\n\t\t\t(outputBytes >>> 8) & 0xff,\n\t\t\t(outputBytes >>> 16) & 0xff,\n\t\t\toutputBytes >>> 24,\n\t\t]),\n\t\tinput,\n\t);\n\tlet value: Uint8Array<ArrayBufferLike> = new Uint8Array(0);\n\ttry {\n\t\tif (outputBytes <= 64) return blake2b(seed, outputBytes);\n\t\tvalue = blake2b(seed, 64);\n\t\tconst output = new Uint8Array(outputBytes);\n\t\tlet offset = 0;\n\t\twhile (outputBytes - offset > 64) {\n\t\t\toutput.set(value.subarray(0, 32), offset);\n\t\t\toffset += 32;\n\t\t\tif (outputBytes - offset > 64) {\n\t\t\t\tconst next = blake2b(value, 64);\n\t\t\t\tvalue.fill(0);\n\t\t\t\tvalue = next;\n\t\t\t}\n\t\t}\n\t\tconst last = blake2b(value, outputBytes - offset);\n\t\ttry {\n\t\t\toutput.set(last, offset);\n\t\t\treturn output;\n\t\t} finally {\n\t\t\tlast.fill(0);\n\t\t}\n\t} finally {\n\t\tseed.fill(0);\n\t\tvalue.fill(0);\n\t}\n}\n\nfunction initialHash(input: Argon2Input, type: number): Uint8Array {\n\tconst password = input.password;\n\tconst salt = input.salt;\n\tconst secret = input.secret ?? new Uint8Array();\n\tconst associatedData = input.associatedData ?? new Uint8Array();\n\tconst fields = new Uint8Array(28);\n\tconst values = [\n\t\tinput.parallelism,\n\t\tinput.outputBytes,\n\t\tinput.memoryKiB,\n\t\tinput.passes,\n\t\tinput.version,\n\t\ttype,\n\t\tpassword.length,\n\t];\n\tlet offset = 0;\n\tfor (const value of values) {\n\t\twrite32(fields, offset, value);\n\t\toffset += 4;\n\t}\n\tconst tail = new Uint8Array(12);\n\twrite32(tail, 0, salt.length);\n\twrite32(tail, 4, secret.length);\n\twrite32(tail, 8, associatedData.length);\n\tconst message = concat(\n\t\tfields.subarray(0, 28),\n\t\tpassword,\n\t\ttail.subarray(0, 4),\n\t\tsalt,\n\t\ttail.subarray(4, 8),\n\t\tsecret,\n\t\ttail.subarray(8, 12),\n\t\tassociatedData,\n\t);\n\ttry {\n\t\treturn blake2b(message, 64);\n\t} finally {\n\t\tmessage.fill(0);\n\t}\n}\n\n// Keep the four 64-bit registers local throughout the quarter round. The\n// scratch block is read once and written once, instead of at each add/rotate.\n// Low words stay unsigned for carry calculation; high words stay signed so\n// the JIT can use 32-bit addition. Storing into Uint32Array preserves the bits.\nfunction argonMix(words: Uint32Array, a: number, b: number, c: number, d: number): void {\n\tlet aLow = words[a] ?? 0;\n\tlet aHigh = (words[a + 1] ?? 0) | 0;\n\tlet bLow = words[b] ?? 0;\n\tlet bHigh = (words[b + 1] ?? 0) | 0;\n\tlet cLow = words[c] ?? 0;\n\tlet cHigh = (words[c + 1] ?? 0) | 0;\n\tlet dLow = words[d] ?? 0;\n\tlet dHigh = (words[d + 1] ?? 0) | 0;\n\tlet product: number;\n\tlet sum: number;\n\tlet low: number;\n\tlet high: number;\n\n\tproduct = Math.imul(aLow, bLow);\n\tsum = aLow + bLow + ((product << 1) >>> 0);\n\taHigh =\n\t\t(aHigh +\n\t\t\tbHigh +\n\t\t\t((multiplyHigh(aLow, bLow, product) << 1) | (product >>> 31)) +\n\t\t\t((sum / 0x1_0000_0000) | 0)) |\n\t\t0;\n\taLow = sum >>> 0;\n\tlow = dLow ^ aLow;\n\thigh = dHigh ^ aHigh;\n\tdLow = high >>> 0;\n\tdHigh = low | 0;\n\n\tproduct = Math.imul(cLow, dLow);\n\tsum = cLow + dLow + ((product << 1) >>> 0);\n\tcHigh =\n\t\t(cHigh +\n\t\t\tdHigh +\n\t\t\t((multiplyHigh(cLow, dLow, product) << 1) | (product >>> 31)) +\n\t\t\t((sum / 0x1_0000_0000) | 0)) |\n\t\t0;\n\tcLow = sum >>> 0;\n\tlow = bLow ^ cLow;\n\thigh = bHigh ^ cHigh;\n\tbLow = ((low >>> 24) | (high << 8)) >>> 0;\n\tbHigh = (high >>> 24) | (low << 8) | 0;\n\n\tproduct = Math.imul(aLow, bLow);\n\tsum = aLow + bLow + ((product << 1) >>> 0);\n\taHigh =\n\t\t(aHigh +\n\t\t\tbHigh +\n\t\t\t((multiplyHigh(aLow, bLow, product) << 1) | (product >>> 31)) +\n\t\t\t((sum / 0x1_0000_0000) | 0)) |\n\t\t0;\n\taLow = sum >>> 0;\n\tlow = dLow ^ aLow;\n\thigh = dHigh ^ aHigh;\n\tdLow = ((low >>> 16) | (high << 16)) >>> 0;\n\tdHigh = (high >>> 16) | (low << 16) | 0;\n\n\tproduct = Math.imul(cLow, dLow);\n\tsum = cLow + dLow + ((product << 1) >>> 0);\n\tcHigh =\n\t\t(cHigh +\n\t\t\tdHigh +\n\t\t\t((multiplyHigh(cLow, dLow, product) << 1) | (product >>> 31)) +\n\t\t\t((sum / 0x1_0000_0000) | 0)) |\n\t\t0;\n\tcLow = sum >>> 0;\n\tlow = bLow ^ cLow;\n\thigh = bHigh ^ cHigh;\n\tbLow = ((low << 1) | (high >>> 31)) >>> 0;\n\tbHigh = (high << 1) | (low >>> 31) | 0;\n\twords[a] = aLow;\n\twords[a + 1] = aHigh;\n\twords[b] = bLow;\n\twords[b + 1] = bHigh;\n\twords[c] = cLow;\n\twords[c + 1] = cHigh;\n\twords[d] = dLow;\n\twords[d + 1] = dHigh;\n}\n\n// Each round uses fixed word strides: contiguous rows, then paired columns.\n// Spell out the eight quarter rounds to avoid schedule-array loads in the hot loop.\nfunction argonRound(words: Uint32Array, offset: number, stride: number, pair: number): void {\n\targonMix(words, offset, offset + 2 * stride, offset + 4 * stride, offset + 6 * stride);\n\targonMix(\n\t\twords,\n\t\toffset + pair,\n\t\toffset + 2 * stride + pair,\n\t\toffset + 4 * stride + pair,\n\t\toffset + 6 * stride + pair,\n\t);\n\targonMix(words, offset + stride, offset + 3 * stride, offset + 5 * stride, offset + 7 * stride);\n\targonMix(\n\t\twords,\n\t\toffset + stride + pair,\n\t\toffset + 3 * stride + pair,\n\t\toffset + 5 * stride + pair,\n\t\toffset + 7 * stride + pair,\n\t);\n\targonMix(\n\t\twords,\n\t\toffset,\n\t\toffset + 2 * stride + pair,\n\t\toffset + 5 * stride,\n\t\toffset + 7 * stride + pair,\n\t);\n\targonMix(\n\t\twords,\n\t\toffset + pair,\n\t\toffset + 3 * stride,\n\t\toffset + 5 * stride + pair,\n\t\toffset + 6 * stride,\n\t);\n\targonMix(\n\t\twords,\n\t\toffset + stride,\n\t\toffset + 3 * stride + pair,\n\t\toffset + 4 * stride,\n\t\toffset + 6 * stride + pair,\n\t);\n\targonMix(\n\t\twords,\n\t\toffset + stride + pair,\n\t\toffset + 2 * stride,\n\t\toffset + 4 * stride + pair,\n\t\toffset + 7 * stride,\n\t);\n}\n\nfunction permuteBlock(words: Uint32Array): void {\n\tfor (let offset = 0; offset < 256; offset += 32) argonRound(words, offset, 4, 2);\n\tfor (let offset = 0; offset < 32; offset += 4) argonRound(words, offset, 32, 2);\n}\n\nfunction fillBlock(\n\tmemory: Uint32Array,\n\tpreviousOffset: number,\n\treferenceOffset: number,\n\tdestinationOffset: number,\n\twithXor: boolean,\n\toriginal: Uint32Array,\n\tpermuted: Uint32Array,\n): void {\n\tfor (let index = 0; index < 256; index += 1) {\n\t\tconst value = (memory[previousOffset + index] ?? 0) ^ (memory[referenceOffset + index] ?? 0);\n\t\toriginal[index] = value;\n\t\tpermuted[index] = value;\n\t}\n\tpermuteBlock(permuted);\n\tif (withXor) {\n\t\tfor (let index = 0; index < 256; index += 1) {\n\t\t\tmemory[destinationOffset + index] =\n\t\t\t\t(original[index] ?? 0) ^ (permuted[index] ?? 0) ^ (memory[destinationOffset + index] ?? 0);\n\t\t}\n\t} else {\n\t\tfor (let index = 0; index < 256; index += 1) {\n\t\t\tmemory[destinationOffset + index] = (original[index] ?? 0) ^ (permuted[index] ?? 0);\n\t\t}\n\t}\n}\n\nfunction indexAlpha(\n\tpass: number,\n\tslice: number,\n\tindex: number,\n\tsameLane: boolean,\n\tsegmentLength: number,\n\tlaneLength: number,\n\tpseudoRandom: number,\n): number {\n\tlet areaSize: number;\n\tif (pass === 0) {\n\t\tareaSize =\n\t\t\tslice === 0\n\t\t\t\t? index - 1\n\t\t\t\t: slice * segmentLength + (sameLane ? index - 1 : index === 0 ? -1 : 0);\n\t} else {\n\t\tareaSize = laneLength - segmentLength + (sameLane ? index - 1 : index === 0 ? -1 : 0);\n\t}\n\tconst squareHigh = multiplyHigh(\n\t\tpseudoRandom,\n\t\tpseudoRandom,\n\t\tMath.imul(pseudoRandom, pseudoRandom),\n\t);\n\tconst relative =\n\t\tareaSize - 1 - multiplyHigh(areaSize, squareHigh, Math.imul(areaSize, squareHigh));\n\tconst start = pass === 0 ? 0 : slice === 3 ? 0 : (slice + 1) * segmentLength;\n\treturn (start + relative) % laneLength;\n}\n\nfunction dataIndependent(type: number, pass: number, slice: number): boolean {\n\treturn type === 1 || (type === 2 && pass === 0 && slice < 2);\n}\n\nfunction compressBlocks(\n\tprevious: Uint32Array,\n\treference: Uint32Array,\n\toutput: Uint32Array,\n\toriginal: Uint32Array,\n\tpermuted: Uint32Array,\n): void {\n\tfor (let index = 0; index < 256; index += 1) {\n\t\tconst value = (previous[index] ?? 0) ^ (reference[index] ?? 0);\n\t\toriginal[index] = value;\n\t\tpermuted[index] = value;\n\t}\n\tpermuteBlock(permuted);\n\tfor (let index = 0; index < 256; index += 1) {\n\t\toutput[index] = (original[index] ?? 0) ^ (permuted[index] ?? 0);\n\t}\n}\n\nfunction generateAddressBlock(\n\tinputBlock: Uint32Array,\n\taddressBlock: Uint32Array,\n\tzero: Uint32Array,\n\toriginal: Uint32Array,\n\tpermuted: Uint32Array,\n\taddressTemporary: Uint32Array,\n): void {\n\tinputBlock[12] = ((inputBlock[12] ?? 0) + 1) >>> 0;\n\tcompressBlocks(zero, inputBlock, addressTemporary, original, permuted);\n\tcompressBlocks(zero, addressTemporary, addressBlock, original, permuted);\n}\n\nfunction toWords(bytes: Uint8Array): Uint32Array {\n\tconst words = new Uint32Array(256);\n\tfor (let index = 0; index < 256; index += 1) words[index] = read32(bytes, index * 4);\n\treturn words;\n}\n\ninterface Workspace {\n\treadonly memory: Uint32Array;\n\treadonly memoryBlocks: number;\n\treadonly laneLength: number;\n\treadonly segmentLength: number;\n\treadonly original: Uint32Array;\n\treadonly permuted: Uint32Array;\n\treadonly zero: Uint32Array;\n\treadonly addressTemporary: Uint32Array;\n}\n\nfunction fillIndexedBlock(\n\tinput: Argon2Input,\n\tworkspace: Workspace,\n\tposition: { pass: number; slice: number; lane: number },\n\tindex: number,\n\taddressBlock: Uint32Array | undefined,\n): void {\n\tconst { pass, slice, lane } = position;\n\tconst { memory, laneLength, segmentLength, original, permuted } = workspace;\n\tconst currentBlock = lane * laneLength + slice * segmentLength + index;\n\tconst previousBlock =\n\t\tcurrentBlock % laneLength === 0 ? currentBlock + laneLength - 1 : currentBlock - 1;\n\tconst previousOffset = previousBlock * 256;\n\tconst pseudoOffset = addressBlock === undefined ? previousOffset : (index % 128) * 2;\n\tconst pseudo = addressBlock ?? memory;\n\tconst pseudoLow = pseudo[pseudoOffset] ?? 0;\n\tconst pseudoHigh = pseudo[pseudoOffset + 1] ?? 0;\n\tlet referenceLane = pseudoHigh % input.parallelism;\n\tif (pass === 0 && slice === 0) referenceLane = lane;\n\tconst referenceIndex = indexAlpha(\n\t\tpass,\n\t\tslice,\n\t\tindex,\n\t\treferenceLane === lane,\n\t\tsegmentLength,\n\t\tlaneLength,\n\t\tpseudoLow,\n\t);\n\tconst destinationBlock = currentBlock * 256;\n\tfillBlock(\n\t\tmemory,\n\t\tpreviousOffset,\n\t\t(referenceLane * laneLength + referenceIndex) * 256,\n\t\tdestinationBlock,\n\t\tinput.version !== 0x10 && pass !== 0,\n\t\toriginal,\n\t\tpermuted,\n\t);\n}\n\nasync function fillSegment(\n\tinput: Argon2Input,\n\ttype: number,\n\tworkspace: Workspace,\n\tposition: { pass: number; slice: number; lane: number },\n): Promise<void> {\n\tconst { pass, slice, lane } = position;\n\tconst { memoryBlocks, segmentLength, original, permuted, zero, addressTemporary } = workspace;\n\tconst independent = dataIndependent(type, pass, slice);\n\tconst inputBlock = new Uint32Array(256);\n\tconst addressBlock = new Uint32Array(256);\n\ttry {\n\t\tif (independent) {\n\t\t\tinputBlock[0] = pass;\n\t\t\tinputBlock[2] = lane;\n\t\t\tinputBlock[4] = slice;\n\t\t\tinputBlock[6] = memoryBlocks;\n\t\t\tinputBlock[8] = input.passes;\n\t\t\tinputBlock[10] = type;\n\t\t}\n\t\tconst startingIndex = pass === 0 && slice === 0 ? 2 : 0;\n\t\tif (independent && startingIndex !== 0) {\n\t\t\tgenerateAddressBlock(inputBlock, addressBlock, zero, original, permuted, addressTemporary);\n\t\t}\n\t\tfor (let start = startingIndex; start < segmentLength; start += 512) {\n\t\t\t// Start the timer before computing so its delay overlaps useful work.\n\t\t\t// Awaiting it still yields to the event loop after at most 512 blocks.\n\t\t\tconst pause = new Promise<void>((resolve) => setTimeout(resolve, 0));\n\t\t\tconst end = Math.min(start + 512, segmentLength);\n\t\t\tfor (let index = start; index < end; index += 1) {\n\t\t\t\tif (independent && index % 128 === 0) {\n\t\t\t\t\tgenerateAddressBlock(\n\t\t\t\t\t\tinputBlock,\n\t\t\t\t\t\taddressBlock,\n\t\t\t\t\t\tzero,\n\t\t\t\t\t\toriginal,\n\t\t\t\t\t\tpermuted,\n\t\t\t\t\t\taddressTemporary,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tfillIndexedBlock(input, workspace, position, index, independent ? addressBlock : undefined);\n\t\t\t}\n\t\t\tawait pause;\n\t\t}\n\t} finally {\n\t\tinputBlock.fill(0);\n\t\taddressBlock.fill(0);\n\t\taddressTemporary.fill(0);\n\t}\n}\n\nexport async function deriveArgon2TypeScript(input: Argon2Input): Promise<Uint8Array> {\n\tconst type = { argon2d: 0, argon2i: 1, argon2id: 2 }[input.algorithm];\n\tconst memoryBlocks =\n\t\tMath.floor(input.memoryKiB / (4 * input.parallelism)) * 4 * input.parallelism;\n\tconst laneLength = memoryBlocks / input.parallelism;\n\tconst segmentLength = laneLength / 4;\n\tlet memory = new Uint32Array(0);\n\tlet h0: Uint8Array<ArrayBufferLike> = new Uint8Array(0);\n\tlet original = new Uint32Array(0);\n\tlet permuted = new Uint32Array(0);\n\tlet zero = new Uint32Array(0);\n\tlet addressTemporary = new Uint32Array(0);\n\tlet finalBlock: Uint8Array<ArrayBufferLike> = new Uint8Array(0);\n\tlet finalWords: Uint32Array<ArrayBufferLike> = new Uint32Array(0);\n\ttry {\n\t\tmemory = matrices.take(memoryBlocks * 256);\n\t\th0 = initialHash(input, type);\n\t\toriginal = new Uint32Array(256);\n\t\tpermuted = new Uint32Array(256);\n\t\tzero = new Uint32Array(256);\n\t\taddressTemporary = new Uint32Array(256);\n\t\tfor (let lane = 0; lane < input.parallelism; lane += 1) {\n\t\t\tfor (let block = 0; block < 2; block += 1) {\n\t\t\t\tconst suffix = new Uint8Array(8);\n\t\t\t\twrite32(suffix, 0, block);\n\t\t\t\twrite32(suffix, 4, lane);\n\t\t\t\tconst seed = concat(h0, suffix);\n\t\t\t\tlet bytes: Uint8Array<ArrayBufferLike> = new Uint8Array(0);\n\t\t\t\tlet words: Uint32Array<ArrayBufferLike> = new Uint32Array(0);\n\t\t\t\ttry {\n\t\t\t\t\tbytes = blakeLong(seed, 1024);\n\t\t\t\t\twords = toWords(bytes);\n\t\t\t\t\tmemory.set(words, (lane * laneLength + block) * 256);\n\t\t\t\t} finally {\n\t\t\t\t\tseed.fill(0);\n\t\t\t\t\tbytes.fill(0);\n\t\t\t\t\twords.fill(0);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tconst workspace: Workspace = {\n\t\t\tmemory,\n\t\t\tmemoryBlocks,\n\t\t\tlaneLength,\n\t\t\tsegmentLength,\n\t\t\toriginal,\n\t\t\tpermuted,\n\t\t\tzero,\n\t\t\taddressTemporary,\n\t\t};\n\t\tfor (let pass = 0; pass < input.passes; pass += 1) {\n\t\t\tfor (let slice = 0; slice < 4; slice += 1) {\n\t\t\t\tfor (let lane = 0; lane < input.parallelism; lane += 1) {\n\t\t\t\t\tawait fillSegment(input, type, workspace, { pass, slice, lane });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfinalBlock = new Uint8Array(1024);\n\t\tfinalWords = toWords(finalBlock);\n\t\tfor (let lane = 0; lane < input.parallelism; lane += 1) {\n\t\t\tconst lastOffset = (lane * laneLength + laneLength - 1) * 256;\n\t\t\tfor (let index = 0; index < 256; index += 1) {\n\t\t\t\tfinalWords[index] = (finalWords[index] ?? 0) ^ (memory[lastOffset + index] ?? 0);\n\t\t\t}\n\t\t}\n\t\tfor (let index = 0; index < 256; index += 1)\n\t\t\twrite32(finalBlock, index * 4, finalWords[index] ?? 0);\n\t\treturn blakeLong(finalBlock, input.outputBytes);\n\t} finally {\n\t\th0.fill(0);\n\t\toriginal.fill(0);\n\t\tpermuted.fill(0);\n\t\tzero.fill(0);\n\t\taddressTemporary.fill(0);\n\t\tfinalBlock.fill(0);\n\t\tfinalWords.fill(0);\n\t\tmatrices.release(memory);\n\t}\n}\n"],"mappings":";;;AASA,MAAMA,IA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,KAAK,KAAS,GACzC,EAAWA,MAAUyF,EAAWzF,MAAU,MAAMiD,EAAO0C,IAAa3F,MAAU;EAEhF;EACA,KAAK,IAAIA,IAAQ,GAAGA,IAAQ,KAAK,KAAS,GACzC,QAAQoB,GAAYpB,IAAQ,GAAGyF,EAAWzF,MAAU,CAAC;EACtD,OAAO,UAAUoB,GAAYE,EAAM,WAAW;CAC/C,UAAU;EAQT,AAPAkE,EAAG,KAAK,CAAC,GACTjC,EAAS,KAAK,CAAC,GACfH,EAAS,KAAK,CAAC,GACfgB,EAAK,KAAK,CAAC,GACXC,EAAiB,KAAK,CAAC,GACvBjD,EAAW,KAAK,CAAC,GACjBqE,EAAW,KAAK,CAAC,GACjBlG,EAAS,QAAQ0D,CAAM;CACxB;AACD"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/kdf/argon2-memory.ts
|
|
2
|
+
/** Reuse wiped matrices without preventing garbage collection of idle storage. */
|
|
3
|
+
function createMatrixPool() {
|
|
4
|
+
let e = [];
|
|
5
|
+
return {
|
|
6
|
+
take(t) {
|
|
7
|
+
for (let n = e.length - 1; n >= 0; --n) {
|
|
8
|
+
let r = e[n]?.deref();
|
|
9
|
+
if ((r === void 0 || r.length === t) && (e.splice(n, 1), r !== void 0)) return r;
|
|
10
|
+
}
|
|
11
|
+
return new Uint32Array(t);
|
|
12
|
+
},
|
|
13
|
+
release(t) {
|
|
14
|
+
t.fill(0), t.length !== 0 && (e.length === 4 && e.shift(), e.push(new WeakRef(t)));
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export { createMatrixPool };
|
|
19
|
+
|
|
20
|
+
//# sourceMappingURL=argon2-memory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"argon2-memory.js","names":["idle","index","matrix","words"],"sources":["../../src/kdf/argon2-memory.ts"],"sourcesContent":["interface MatrixPool {\n\treadonly take: (words: number) => Uint32Array<ArrayBuffer>;\n\treadonly release: (matrix: Uint32Array<ArrayBuffer>) => void;\n}\n\n/** Reuse wiped matrices without preventing garbage collection of idle storage. */\nexport function createMatrixPool(): MatrixPool {\n\tconst idle: WeakRef<Uint32Array<ArrayBuffer>>[] = [];\n\treturn {\n\t\ttake(words) {\n\t\t\tfor (let index = idle.length - 1; index >= 0; index -= 1) {\n\t\t\t\tconst matrix = idle[index]?.deref();\n\t\t\t\tif (matrix === undefined || matrix.length === words) {\n\t\t\t\t\tidle.splice(index, 1);\n\t\t\t\t\tif (matrix !== undefined) return matrix;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn new Uint32Array(words);\n\t\t},\n\t\trelease(matrix) {\n\t\t\tmatrix.fill(0);\n\t\t\tif (matrix.length === 0) return;\n\t\t\tif (idle.length === 4) idle.shift();\n\t\t\tidle.push(new WeakRef(matrix));\n\t\t},\n\t};\n}\n"],"mappings":";;AAMA,SAAgB,mBAA+B;CAC9C,IAAMA,IAA4C,CAAC;CACnD,OAAO;EACN,KAAK,GAAO;GACX,KAAK,IAAIC,IAAQD,EAAK,SAAS,GAAGC,KAAS,GAAG,KAAY;IACzD,IAAMC,IAASF,EAAKC,EAAM,EAAE,MAAM;IAClC,KAAIC,MAAW,KAAA,KAAaA,EAAO,WAAWC,OAC7CH,EAAK,OAAOC,GAAO,CAAC,GAChBC,MAAW,KAAA,IAAW,OAAOA;GAEnC;GACA,OAAO,IAAI,YAAYC,CAAK;EAC7B;EACA,QAAQ,GAAQ;GAEf,AADAD,EAAO,KAAK,CAAC,GACTA,EAAO,WAAW,MAClBF,EAAK,WAAW,KAAGA,EAAK,MAAM,GAClCA,EAAK,KAAK,IAAI,QAAQE,CAAM,CAAC;EAC9B;CACD;AACD"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { deriveArgon2TypeScript as e } from "./argon2-core.js";
|
|
2
|
+
// src/kdf/argon2-portable.ts
|
|
3
|
+
/** Internal TypeScript backend; the public boundary validates inputs and limits. */
|
|
4
|
+
async function portableArgon2(t) {
|
|
5
|
+
return t.memoryKiB * 1024 + t.outputBytes + t.password.byteLength + t.salt.byteLength + (t.secret?.byteLength ?? 0) + (t.associatedData?.byteLength ?? 0) >= 2147483648 ? {
|
|
6
|
+
ok: !1,
|
|
7
|
+
error: {
|
|
8
|
+
code: "unsupported",
|
|
9
|
+
message: "Request exceeds portable Argon2 address space"
|
|
10
|
+
}
|
|
11
|
+
} : {
|
|
12
|
+
ok: !0,
|
|
13
|
+
value: await e(t)
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export { portableArgon2 };
|
|
17
|
+
|
|
18
|
+
//# sourceMappingURL=argon2-portable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"argon2-portable.js","names":["input","deriveArgon2TypeScript"],"sources":["../../src/kdf/argon2-portable.ts"],"sourcesContent":["import type { Argon2Input, Argon2Result } from '#micro509/kdf/argon2';\nimport { deriveArgon2TypeScript } from '#micro509/kdf/argon2-core';\n\n/** Internal TypeScript backend; the public boundary validates inputs and limits. */\nexport async function portableArgon2(input: Argon2Input): Promise<Argon2Result> {\n\tconst bytes =\n\t\tinput.memoryKiB * 1024 +\n\t\tinput.outputBytes +\n\t\tinput.password.byteLength +\n\t\tinput.salt.byteLength +\n\t\t(input.secret?.byteLength ?? 0) +\n\t\t(input.associatedData?.byteLength ?? 0);\n\tif (bytes >= 0x80000000) {\n\t\treturn {\n\t\t\tok: false,\n\t\t\terror: { code: 'unsupported', message: 'Request exceeds portable Argon2 address space' },\n\t\t};\n\t}\n\treturn { ok: true, value: await deriveArgon2TypeScript(input) };\n}\n"],"mappings":";;;AAIA,eAAsB,eAAe,GAA2C;CAc/E,OAZCA,EAAM,YAAY,OAClBA,EAAM,cACNA,EAAM,SAAS,aACfA,EAAM,KAAK,cACVA,EAAM,QAAQ,cAAc,MAC5BA,EAAM,gBAAgB,cAAc,MACzB,aACL;EACN,IAAI;EACJ,OAAO;GAAE,MAAM;GAAe,SAAS;EAAgD;CACxF,IAEM;EAAE,IAAI;EAAM,OAAO,MAAMC,EAAuBD,CAAK;CAAE;AAC/D"}
|
package/dist/kdf/argon2.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Micro509Error, Result } from "../result/result.js";
|
|
2
|
-
/**
|
|
3
|
-
export type Argon2Algorithm = "
|
|
2
|
+
/** RFC 9106 Argon2 variants. Use Argon2id for new password-based applications. */
|
|
3
|
+
export type Argon2Algorithm = "argon2d" | "argon2i" | "argon2id";
|
|
4
4
|
/** Argon2 versions used by KDBX. The native backend supports only 0x13. */
|
|
5
5
|
export type Argon2Version = 0x10 | 0x13;
|
|
6
6
|
/** Exact derivation inputs. Password encoding and format preprocessing belong to the caller. */
|
|
@@ -11,7 +11,7 @@ export interface Argon2Input {
|
|
|
11
11
|
readonly version: Argon2Version;
|
|
12
12
|
/** Password or format-preprocessed input bytes. */
|
|
13
13
|
readonly password: Uint8Array;
|
|
14
|
-
/** Salt bytes.
|
|
14
|
+
/** Salt bytes. RFC 9106 recommends 16 bytes; the native backend requires at least eight. */
|
|
15
15
|
readonly salt: Uint8Array;
|
|
16
16
|
/** Total memory in kibibytes, at least eight times parallelism. */
|
|
17
17
|
readonly memoryKiB: number;
|
|
@@ -57,21 +57,27 @@ export type Argon2Backend = (input: Argon2Input) => Promise<Argon2Result>;
|
|
|
57
57
|
export interface Argon2Options {
|
|
58
58
|
/** Per-call ceilings. Applications must separately bound concurrent calls. */
|
|
59
59
|
readonly limits?: Argon2Limits;
|
|
60
|
-
/**
|
|
61
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Omit to prefer native WebCrypto, then the TypeScript implementation on `unsupported` only.
|
|
62
|
+
* Select `native` or `portable` to require that implementation, or supply a
|
|
63
|
+
* trusted callback. Explicit selections never fall back to another backend.
|
|
64
|
+
*/
|
|
65
|
+
readonly backend?: Argon2Backend | "native" | "portable";
|
|
62
66
|
}
|
|
63
67
|
/**
|
|
64
68
|
* Derives raw Argon2 bytes after validating parameters and resource ceilings.
|
|
65
69
|
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
+
* Native WebCrypto is preferred for version 0x13. An unsupported request uses
|
|
71
|
+
* the TypeScript implementation for the exact variant and version, including
|
|
72
|
+
* 0x10. Other native failures are returned without retrying. Explicit
|
|
73
|
+
* backends never fall back. No algorithm, version, or parameter is substituted.
|
|
70
74
|
*
|
|
71
75
|
* Caller buffers are copied synchronously before the first await and never
|
|
72
76
|
* modified. Owned temporary arrays are zeroed on completion. Native key storage
|
|
73
77
|
* and runtime copies cannot be explicitly erased by JavaScript. The returned
|
|
74
78
|
* output is caller-owned. Calls cannot cancel native work once dispatched.
|
|
79
|
+
* TypeScript work yields while filling memory blocks. Its private memory matrix
|
|
80
|
+
* is erased on completion or failure.
|
|
75
81
|
* Limits apply per call, not to concurrent invocations.
|
|
76
82
|
*/
|
|
77
83
|
export declare function deriveArgon2(input: Argon2Input, options?: Argon2Options): Promise<Argon2Result>;
|
package/dist/kdf/argon2.js
CHANGED
|
@@ -12,7 +12,7 @@ function uintInRange(e, t, n) {
|
|
|
12
12
|
return Number.isSafeInteger(e) && e >= t && e <= n;
|
|
13
13
|
}
|
|
14
14
|
function validate(e, t) {
|
|
15
|
-
if (e.algorithm !== "argon2id" && e.algorithm !== "argon2d") return failure("unsupported", "Unsupported Argon2 variant");
|
|
15
|
+
if (e.algorithm !== "argon2id" && e.algorithm !== "argon2d" && e.algorithm !== "argon2i") return failure("unsupported", "Unsupported Argon2 variant");
|
|
16
16
|
if (e.version !== 16 && e.version !== 19) return failure("unsupported", "Unsupported Argon2 version");
|
|
17
17
|
if (!uintInRange(e.parallelism, 1, 16777215) || !uintInRange(e.memoryKiB, 8 * e.parallelism, 4294967295) || !uintInRange(e.passes, 1, 4294967295) || !uintInRange(e.outputBytes, 4, 4294967295)) return failure("invalid_parameters", "Invalid Argon2 numeric parameters");
|
|
18
18
|
let n = [
|
|
@@ -82,16 +82,25 @@ async function nativeArgon2(e) {
|
|
|
82
82
|
case "argon2d":
|
|
83
83
|
n = "Argon2d";
|
|
84
84
|
break;
|
|
85
|
+
case "argon2i":
|
|
86
|
+
n = "Argon2i";
|
|
87
|
+
break;
|
|
85
88
|
default: return failure("unsupported", "Unsupported Argon2 variant");
|
|
86
89
|
}
|
|
87
90
|
try {
|
|
88
|
-
let r
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
91
|
+
let r;
|
|
92
|
+
try {
|
|
93
|
+
r = await Reflect.apply(t.importKey, t, [
|
|
94
|
+
"raw-secret",
|
|
95
|
+
e.password,
|
|
96
|
+
n,
|
|
97
|
+
!1,
|
|
98
|
+
["deriveBits"]
|
|
99
|
+
]);
|
|
100
|
+
} catch (e) {
|
|
101
|
+
if (e instanceof TypeError) return failure("unsupported", "This runtime does not support the raw-secret key format");
|
|
102
|
+
throw e;
|
|
103
|
+
}
|
|
95
104
|
if (typeof CryptoKey > "u" || !(r instanceof CryptoKey)) return failure("derivation_failed", "Native Argon2 returned an invalid key");
|
|
96
105
|
let i = {
|
|
97
106
|
name: n,
|
|
@@ -116,18 +125,28 @@ async function nativeArgon2(e) {
|
|
|
116
125
|
return e instanceof Error && e.name === "NotSupportedError" ? failure("unsupported", "This runtime does not support native Argon2") : failure("derivation_failed", "Native Argon2 derivation failed");
|
|
117
126
|
}
|
|
118
127
|
}
|
|
128
|
+
async function builtinArgon2(e, t) {
|
|
129
|
+
if (t !== "portable") {
|
|
130
|
+
let n = await nativeArgon2(e);
|
|
131
|
+
if (t === "native" || n.ok || n.error.code !== "unsupported") return n;
|
|
132
|
+
}
|
|
133
|
+
let { portableArgon2: n } = await import("./argon2-portable.js");
|
|
134
|
+
return n(e);
|
|
135
|
+
}
|
|
119
136
|
/**
|
|
120
137
|
* Derives raw Argon2 bytes after validating parameters and resource ceilings.
|
|
121
138
|
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
139
|
+
* Native WebCrypto is preferred for version 0x13. An unsupported request uses
|
|
140
|
+
* the TypeScript implementation for the exact variant and version, including
|
|
141
|
+
* 0x10. Other native failures are returned without retrying. Explicit
|
|
142
|
+
* backends never fall back. No algorithm, version, or parameter is substituted.
|
|
126
143
|
*
|
|
127
144
|
* Caller buffers are copied synchronously before the first await and never
|
|
128
145
|
* modified. Owned temporary arrays are zeroed on completion. Native key storage
|
|
129
146
|
* and runtime copies cannot be explicitly erased by JavaScript. The returned
|
|
130
147
|
* output is caller-owned. Calls cannot cancel native work once dispatched.
|
|
148
|
+
* TypeScript work yields while filling memory blocks. Its private memory matrix
|
|
149
|
+
* is erased on completion or failure.
|
|
131
150
|
* Limits apply per call, not to concurrent invocations.
|
|
132
151
|
*/
|
|
133
152
|
async function deriveArgon2(e, t = {}) {
|
|
@@ -142,7 +161,7 @@ async function deriveArgon2(e, t = {}) {
|
|
|
142
161
|
if (r !== void 0) return r;
|
|
143
162
|
let i = copyInput(n);
|
|
144
163
|
try {
|
|
145
|
-
let e = await
|
|
164
|
+
let e = typeof t.backend == "function" ? await t.backend(i) : await builtinArgon2(i, t.backend);
|
|
146
165
|
if (!e.ok) {
|
|
147
166
|
let t = e.error.code;
|
|
148
167
|
return failure(t === "unsupported" || t === "limit_exceeded" || t === "invalid_parameters" ? t : "derivation_failed", "Argon2 backend could not derive the requested key");
|
package/dist/kdf/argon2.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"argon2.js","names":["value","min","max","input","inputs","bytes","bounds","limits","sum","maximum","actual","secret","associatedData","subtle","name","key","params","output","error","snapshot","invalid","options","owned","result","code"],"sources":["../../src/kdf/argon2.ts"],"sourcesContent":["/**\n * Argon2 adapter and resource-policy boundary. The default backend uses native\n * WebCrypto, with no algorithm fallback or portable implementation bundled.\n *\n * @module\n */\n\nimport type { Micro509Error, Result } from '#micro509/result/result';\n\n/** Argon2id for new uses, or Argon2d for compatibility with existing formats. */\nexport type Argon2Algorithm = 'argon2id' | 'argon2d';\n\n/** Argon2 versions used by KDBX. The native backend supports only 0x13. */\nexport type Argon2Version = 0x10 | 0x13;\n\n/** Exact derivation inputs. Password encoding and format preprocessing belong to the caller. */\nexport interface Argon2Input {\n\t/** Select Argon2id for new applications. */\n\treadonly algorithm: Argon2Algorithm;\n\t/** Exact version. No version substitution is performed. */\n\treadonly version: Argon2Version;\n\t/** Password or format-preprocessed input bytes. */\n\treadonly password: Uint8Array;\n\t/** Salt bytes. The native backend requires at least eight bytes. */\n\treadonly salt: Uint8Array;\n\t/** Total memory in kibibytes, at least eight times parallelism. */\n\treadonly memoryKiB: number;\n\t/** Number of full passes through memory. */\n\treadonly passes: number;\n\t/** Number of lanes. */\n\treadonly parallelism: number;\n\t/** Requested output length in bytes, at least four. */\n\treadonly outputBytes: number;\n\t/** Optional secret input, separate from the password. */\n\treadonly secret?: Uint8Array;\n\t/** Optional associated data included in the derivation. */\n\treadonly associatedData?: Uint8Array;\n}\n\n/** Per-invocation ceilings, checked before copying inputs or calling the backend. */\nexport interface Argon2Limits {\n\t/** Memory ceiling in KiB. Default: 262144 (256 MiB). */\n\treadonly maxMemoryKiB?: number;\n\t/** Pass ceiling. Default: 16. */\n\treadonly maxPasses?: number;\n\t/** Lane ceiling. Default: 16. */\n\treadonly maxParallelism?: number;\n\t/** Output-byte ceiling. Default: 1024. */\n\treadonly maxOutputBytes?: number;\n\t/** Sum of password, salt, secret, and associated-data lengths. Default: 1048576. */\n\treadonly maxInputBytes?: number;\n}\n\n/** Stable failures without password, salt, or backend exception content. */\nexport type Argon2ErrorCode =\n\t| 'invalid_parameters'\n\t| 'limit_exceeded'\n\t| 'unsupported'\n\t| 'derivation_failed';\n\n/** Derived bytes or a typed error. Successful output belongs to the caller. */\nexport type Argon2Result = Result<Uint8Array, Micro509Error<Argon2ErrorCode>>;\n\n/**\n * Trusted, explicitly supplied implementation for the exact requested algorithm\n * and version. Called only after parameter and resource checks. Input arrays are\n * private copies, wiped when the promise settles. A backend that transfers an input\n * buffer must erase it in the receiving worker. Transfer ownership of output\n * bytes to the caller: the wrapper copies and wipes that array before returning.\n * Report unsupported requests explicitly, without fallback. Execution behavior\n * and cancellation of a custom backend are its provider's responsibility.\n */\nexport type Argon2Backend = (input: Argon2Input) => Promise<Argon2Result>;\n\n/** Resource policy and optional external implementation for {@linkcode deriveArgon2}. */\nexport interface Argon2Options {\n\t/** Per-call ceilings. Applications must separately bound concurrent calls. */\n\treadonly limits?: Argon2Limits;\n\t/** Explicit trusted backend. Omit to use native WebCrypto version 0x13. */\n\treadonly backend?: Argon2Backend;\n}\n\nfunction failure(code: Argon2ErrorCode, message: string): Argon2Result {\n\treturn { ok: false, error: { code, message } };\n}\n\nfunction uintInRange(value: number, min: number, max: number): boolean {\n\treturn Number.isSafeInteger(value) && value >= min && value <= max;\n}\n\nfunction validate(input: Argon2Input, limits: Argon2Limits): Argon2Result | undefined {\n\tif (input.algorithm !== 'argon2id' && input.algorithm !== 'argon2d') {\n\t\treturn failure('unsupported', 'Unsupported Argon2 variant');\n\t}\n\tif (input.version !== 0x10 && input.version !== 0x13) {\n\t\treturn failure('unsupported', 'Unsupported Argon2 version');\n\t}\n\t// RFC 9106 section 3.1. Memory is passed exactly, including values not\n\t// divisible by 4*p: rounding the input would change the initial hash.\n\tif (\n\t\t!uintInRange(input.parallelism, 1, 0xffffff) ||\n\t\t!uintInRange(input.memoryKiB, 8 * input.parallelism, 0xffffffff) ||\n\t\t!uintInRange(input.passes, 1, 0xffffffff) ||\n\t\t!uintInRange(input.outputBytes, 4, 0xffffffff)\n\t) {\n\t\treturn failure('invalid_parameters', 'Invalid Argon2 numeric parameters');\n\t}\n\tconst inputs = [input.password, input.salt, input.secret, input.associatedData];\n\tif (\n\t\t!(input.password instanceof Uint8Array) ||\n\t\t!(input.salt instanceof Uint8Array) ||\n\t\tinputs.some((bytes) => bytes !== undefined && !(bytes instanceof Uint8Array))\n\t) {\n\t\treturn failure('invalid_parameters', 'Argon2 inputs must be Uint8Array values');\n\t}\n\ttry {\n\t\t// A detached or out-of-bounds view reports zero length. Validate its\n\t\t// backing store before copying any of the caller's secret material.\n\t\tfor (const bytes of inputs) {\n\t\t\tif (bytes !== undefined) Uint8Array.prototype.values.call(bytes);\n\t\t}\n\t} catch {\n\t\treturn failure('invalid_parameters', 'Argon2 input buffer is unavailable');\n\t}\n\tconst bounds = [\n\t\t[input.memoryKiB, limits.maxMemoryKiB ?? 262144],\n\t\t[input.passes, limits.maxPasses ?? 16],\n\t\t[input.parallelism, limits.maxParallelism ?? 16],\n\t\t[input.outputBytes, limits.maxOutputBytes ?? 1024],\n\t\t[\n\t\t\tinputs.reduce((sum, bytes) => sum + (bytes?.byteLength ?? 0), 0),\n\t\t\tlimits.maxInputBytes ?? 1048576,\n\t\t],\n\t] as const;\n\tif (bounds.some(([, maximum]) => !uintInRange(maximum, 1, 0xffffffff))) {\n\t\treturn failure('invalid_parameters', 'Argon2 resource limits must be positive uint32 values');\n\t}\n\tif (bounds.some(([actual, maximum]) => actual > maximum)) {\n\t\treturn failure('limit_exceeded', 'Argon2 request exceeds resource limits');\n\t}\n\treturn undefined;\n}\n\nfunction snapshotInput(input: Argon2Input): Argon2Input {\n\t// Read declared properties once, including inherited and non-enumerable ones.\n\t// Byte arrays remain references until validation has enforced resource bounds.\n\tconst {\n\t\talgorithm,\n\t\tversion,\n\t\tpassword,\n\t\tsalt,\n\t\tmemoryKiB,\n\t\tpasses,\n\t\tparallelism,\n\t\toutputBytes,\n\t\tsecret,\n\t\tassociatedData,\n\t} = input;\n\treturn {\n\t\talgorithm,\n\t\tversion,\n\t\tpassword,\n\t\tsalt,\n\t\tmemoryKiB,\n\t\tpasses,\n\t\tparallelism,\n\t\toutputBytes,\n\t\t...(secret === undefined ? {} : { secret }),\n\t\t...(associatedData === undefined ? {} : { associatedData }),\n\t};\n}\n\nfunction copyInput(input: Argon2Input): Argon2Input {\n\treturn {\n\t\talgorithm: input.algorithm,\n\t\tversion: input.version,\n\t\tmemoryKiB: input.memoryKiB,\n\t\tpasses: input.passes,\n\t\tparallelism: input.parallelism,\n\t\toutputBytes: input.outputBytes,\n\t\tpassword: new Uint8Array(input.password),\n\t\tsalt: new Uint8Array(input.salt),\n\t\t...(input.secret === undefined ? {} : { secret: new Uint8Array(input.secret) }),\n\t\t...(input.associatedData === undefined\n\t\t\t? {}\n\t\t\t: { associatedData: new Uint8Array(input.associatedData) }),\n\t};\n}\n\nfunction eraseAccessible(bytes: Uint8Array | undefined): void {\n\t// Transfer detaches the sender's view. Its bytes can only be erased by the\n\t// receiving worker, and cleanup must still proceed through the other inputs.\n\tif (bytes !== undefined && bytes.byteLength > 0) Uint8Array.prototype.fill.call(bytes, 0);\n}\n\nasync function nativeArgon2(input: Argon2Input): Promise<Argon2Result> {\n\tif (input.version !== 0x13) {\n\t\treturn failure('unsupported', 'Native Argon2 supports version 0x13 only');\n\t}\n\tif (input.salt.byteLength < 8) {\n\t\treturn failure('unsupported', 'Native Argon2 requires a salt of at least eight bytes');\n\t}\n\tif (input.outputBytes > Math.floor(0xffffffff / 8)) {\n\t\treturn failure('unsupported', 'Requested output exceeds the native WebCrypto bit-length range');\n\t}\n\tif (typeof globalThis.crypto?.subtle === 'undefined') {\n\t\treturn failure('unsupported', 'Native WebCrypto is unavailable');\n\t}\n\tconst subtle = globalThis.crypto.subtle;\n\tlet name: 'Argon2id' | 'Argon2d';\n\tswitch (input.algorithm) {\n\t\tcase 'argon2id':\n\t\t\tname = 'Argon2id';\n\t\t\tbreak;\n\t\tcase 'argon2d':\n\t\t\tname = 'Argon2d';\n\t\t\tbreak;\n\t\tdefault:\n\t\t\treturn failure('unsupported', 'Unsupported Argon2 variant');\n\t}\n\ttry {\n\t\t// raw-secret is a runtime WebCrypto extension not present in every DOM\n\t\t// declaration. The result is narrowed rather than asserted or globally\n\t\t// augmenting consumers' SubtleCrypto definitions.\n\t\tconst key: unknown = await Reflect.apply(subtle.importKey, subtle, [\n\t\t\t'raw-secret',\n\t\t\tinput.password,\n\t\t\tname,\n\t\t\tfalse,\n\t\t\t['deriveBits'],\n\t\t]);\n\t\tif (typeof CryptoKey === 'undefined' || !(key instanceof CryptoKey)) {\n\t\t\treturn failure('derivation_failed', 'Native Argon2 returned an invalid key');\n\t\t}\n\t\tconst params = {\n\t\t\tname,\n\t\t\tversion: input.version,\n\t\t\tmemory: input.memoryKiB,\n\t\t\tpasses: input.passes,\n\t\t\tparallelism: input.parallelism,\n\t\t\tnonce: new Uint8Array(input.salt),\n\t\t\t...(input.secret === undefined ? {} : { secretValue: new Uint8Array(input.secret) }),\n\t\t\t...(input.associatedData === undefined\n\t\t\t\t? {}\n\t\t\t\t: { associatedData: new Uint8Array(input.associatedData) }),\n\t\t};\n\t\ttry {\n\t\t\tconst output = await subtle.deriveBits(params, key, input.outputBytes * 8);\n\t\t\treturn { ok: true, value: new Uint8Array(output) };\n\t\t} finally {\n\t\t\tparams.nonce.fill(0);\n\t\t\tparams.secretValue?.fill(0);\n\t\t\tparams.associatedData?.fill(0);\n\t\t}\n\t} catch (error) {\n\t\treturn error instanceof Error && error.name === 'NotSupportedError'\n\t\t\t? failure('unsupported', 'This runtime does not support native Argon2')\n\t\t\t: failure('derivation_failed', 'Native Argon2 derivation failed');\n\t}\n}\n\n/**\n * Derives raw Argon2 bytes after validating parameters and resource ceilings.\n *\n * The default native WebCrypto adapter implements version 0x13. Runtime support\n * is detected by the import/derive operations, without an extra trial derivation.\n * Supply an explicit backend for other environments or version 0x10. There is\n * no fallback between variants, versions, or implementations.\n *\n * Caller buffers are copied synchronously before the first await and never\n * modified. Owned temporary arrays are zeroed on completion. Native key storage\n * and runtime copies cannot be explicitly erased by JavaScript. The returned\n * output is caller-owned. Calls cannot cancel native work once dispatched.\n * Limits apply per call, not to concurrent invocations.\n */\nexport async function deriveArgon2(\n\tinput: Argon2Input,\n\toptions: Argon2Options = {},\n): Promise<Argon2Result> {\n\tif (input === null || typeof input !== 'object') {\n\t\treturn failure('invalid_parameters', 'Argon2 input must be an object');\n\t}\n\tlet snapshot: Argon2Input;\n\ttry {\n\t\tsnapshot = snapshotInput(input);\n\t} catch {\n\t\treturn failure('invalid_parameters', 'Argon2 input properties could not be read');\n\t}\n\tconst invalid = validate(snapshot, options.limits ?? {});\n\tif (invalid !== undefined) return invalid;\n\tconst owned = copyInput(snapshot);\n\ttry {\n\t\tconst result = await (options.backend ?? nativeArgon2)(owned);\n\t\tif (!result.ok) {\n\t\t\t// A custom provider's diagnostic may contain secrets. Return only the\n\t\t\t// recognized code and a fixed message at this public boundary.\n\t\t\tconst code = result.error.code;\n\t\t\treturn failure(\n\t\t\t\tcode === 'unsupported' || code === 'limit_exceeded' || code === 'invalid_parameters'\n\t\t\t\t\t? code\n\t\t\t\t\t: 'derivation_failed',\n\t\t\t\t'Argon2 backend could not derive the requested key',\n\t\t\t);\n\t\t}\n\t\tif (!(result.value instanceof Uint8Array)) {\n\t\t\treturn failure('derivation_failed', 'Argon2 backend returned invalid output');\n\t\t}\n\t\ttry {\n\t\t\treturn result.value.byteLength === owned.outputBytes\n\t\t\t\t? { ok: true, value: new Uint8Array(result.value) }\n\t\t\t\t: failure('derivation_failed', 'Argon2 backend returned an incorrect output length');\n\t\t} finally {\n\t\t\teraseAccessible(result.value);\n\t\t}\n\t} catch {\n\t\treturn failure('derivation_failed', 'Argon2 backend failed');\n\t} finally {\n\t\teraseAccessible(owned.password);\n\t\teraseAccessible(owned.salt);\n\t\teraseAccessible(owned.secret);\n\t\teraseAccessible(owned.associatedData);\n\t}\n}\n"],"mappings":";AAkFA,SAAS,QAAQ,GAAuB,GAA+B;CACtE,OAAO;EAAE,IAAI;EAAO,OAAO;GAAE;GAAM;EAAQ;CAAE;AAC9C;AAEA,SAAS,YAAY,GAAe,GAAa,GAAsB;CACtE,OAAO,OAAO,cAAcA,CAAK,KAAKA,KAASC,KAAOD,KAASE;AAChE;AAEA,SAAS,SAAS,GAAoB,GAAgD;CACrF,IAAIC,EAAM,cAAc,cAAcA,EAAM,c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AA2C;CACjF;CACA,IAAMiB,IAAU,SAASD,GAAUE,EAAQ,UAAU,CAAC,CAAC;CACvD,IAAID,MAAY,KAAA,GAAW,OAAOA;CAClC,IAAME,IAAQ,UAAUH,CAAQ;CAChC,IAAI;EACH,IAAMI,IAAS,OAAOF,EAAQ,WAAW,aAAA,CAAcC,CAAK;EAC5D,IAAI,CAACC,EAAO,IAAI;GAGf,IAAMC,IAAOD,EAAO,MAAM;GAC1B,OAAO,QACNC,MAAS,iBAAiBA,MAAS,oBAAoBA,MAAS,uBAC7DA,IACA,qBACH,mDACD;EACD;EACA,IAAI,EAAED,EAAO,iBAAiB,aAC7B,OAAO,QAAQ,qBAAqB,wCAAwC;EAE7E,IAAI;GACH,OAAOA,EAAO,MAAM,eAAeD,EAAM,cACtC;IAAE,IAAI;IAAM,OAAO,IAAI,WAAWC,EAAO,KAAK;GAAE,IAChD,QAAQ,qBAAqB,oDAAoD;EACrF,UAAU;GACT,gBAAgBA,EAAO,KAAK;EAC7B;CACD,QAAQ;EACP,OAAO,QAAQ,qBAAqB,uBAAuB;CAC5D,UAAU;EAIT,AAHA,gBAAgBD,EAAM,QAAQ,GAC9B,gBAAgBA,EAAM,IAAI,GAC1B,gBAAgBA,EAAM,MAAM,GAC5B,gBAAgBA,EAAM,cAAc;CACrC;AACD"}
|
|
1
|
+
{"version":3,"file":"argon2.js","names":["value","min","max","input","inputs","bytes","bounds","limits","sum","maximum","actual","secret","associatedData","subtle","name","key","error","params","output","backend","result","portableArgon2","snapshot","invalid","options","owned","code"],"sources":["../../src/kdf/argon2.ts"],"sourcesContent":["/**\n * Argon2 derivation and resource-policy boundary. The default backend prefers\n * native WebCrypto and uses the TypeScript implementation when unsupported.\n *\n * @module\n */\n\nimport type { Micro509Error, Result } from '#micro509/result/result';\n\n/** RFC 9106 Argon2 variants. Use Argon2id for new password-based applications. */\nexport type Argon2Algorithm = 'argon2d' | 'argon2i' | 'argon2id';\n\n/** Argon2 versions used by KDBX. The native backend supports only 0x13. */\nexport type Argon2Version = 0x10 | 0x13;\n\n/** Exact derivation inputs. Password encoding and format preprocessing belong to the caller. */\nexport interface Argon2Input {\n\t/** Select Argon2id for new applications. */\n\treadonly algorithm: Argon2Algorithm;\n\t/** Exact version. No version substitution is performed. */\n\treadonly version: Argon2Version;\n\t/** Password or format-preprocessed input bytes. */\n\treadonly password: Uint8Array;\n\t/** Salt bytes. RFC 9106 recommends 16 bytes; the native backend requires at least eight. */\n\treadonly salt: Uint8Array;\n\t/** Total memory in kibibytes, at least eight times parallelism. */\n\treadonly memoryKiB: number;\n\t/** Number of full passes through memory. */\n\treadonly passes: number;\n\t/** Number of lanes. */\n\treadonly parallelism: number;\n\t/** Requested output length in bytes, at least four. */\n\treadonly outputBytes: number;\n\t/** Optional secret input, separate from the password. */\n\treadonly secret?: Uint8Array;\n\t/** Optional associated data included in the derivation. */\n\treadonly associatedData?: Uint8Array;\n}\n\n/** Per-invocation ceilings, checked before copying inputs or calling the backend. */\nexport interface Argon2Limits {\n\t/** Memory ceiling in KiB. Default: 262144 (256 MiB). */\n\treadonly maxMemoryKiB?: number;\n\t/** Pass ceiling. Default: 16. */\n\treadonly maxPasses?: number;\n\t/** Lane ceiling. Default: 16. */\n\treadonly maxParallelism?: number;\n\t/** Output-byte ceiling. Default: 1024. */\n\treadonly maxOutputBytes?: number;\n\t/** Sum of password, salt, secret, and associated-data lengths. Default: 1048576. */\n\treadonly maxInputBytes?: number;\n}\n\n/** Stable failures without password, salt, or backend exception content. */\nexport type Argon2ErrorCode =\n\t| 'invalid_parameters'\n\t| 'limit_exceeded'\n\t| 'unsupported'\n\t| 'derivation_failed';\n\n/** Derived bytes or a typed error. Successful output belongs to the caller. */\nexport type Argon2Result = Result<Uint8Array, Micro509Error<Argon2ErrorCode>>;\n\n/**\n * Trusted, explicitly supplied implementation for the exact requested algorithm\n * and version. Called only after parameter and resource checks. Input arrays are\n * private copies, wiped when the promise settles. A backend that transfers an input\n * buffer must erase it in the receiving worker. Transfer ownership of output\n * bytes to the caller: the wrapper copies and wipes that array before returning.\n * Report unsupported requests explicitly, without fallback. Execution behavior\n * and cancellation of a custom backend are its provider's responsibility.\n */\nexport type Argon2Backend = (input: Argon2Input) => Promise<Argon2Result>;\n\n/** Resource policy and optional external implementation for {@linkcode deriveArgon2}. */\nexport interface Argon2Options {\n\t/** Per-call ceilings. Applications must separately bound concurrent calls. */\n\treadonly limits?: Argon2Limits;\n\t/**\n\t * Omit to prefer native WebCrypto, then the TypeScript implementation on `unsupported` only.\n\t * Select `native` or `portable` to require that implementation, or supply a\n\t * trusted callback. Explicit selections never fall back to another backend.\n\t */\n\treadonly backend?: Argon2Backend | 'native' | 'portable';\n}\n\nfunction failure(code: Argon2ErrorCode, message: string): Argon2Result {\n\treturn { ok: false, error: { code, message } };\n}\n\nfunction uintInRange(value: number, min: number, max: number): boolean {\n\treturn Number.isSafeInteger(value) && value >= min && value <= max;\n}\n\nfunction validate(input: Argon2Input, limits: Argon2Limits): Argon2Result | undefined {\n\tif (\n\t\tinput.algorithm !== 'argon2id' &&\n\t\tinput.algorithm !== 'argon2d' &&\n\t\tinput.algorithm !== 'argon2i'\n\t) {\n\t\treturn failure('unsupported', 'Unsupported Argon2 variant');\n\t}\n\tif (input.version !== 0x10 && input.version !== 0x13) {\n\t\treturn failure('unsupported', 'Unsupported Argon2 version');\n\t}\n\t// RFC 9106 section 3.1. Memory is passed exactly, including values not\n\t// divisible by 4*p: rounding the input would change the initial hash.\n\tif (\n\t\t!uintInRange(input.parallelism, 1, 0xffffff) ||\n\t\t!uintInRange(input.memoryKiB, 8 * input.parallelism, 0xffffffff) ||\n\t\t!uintInRange(input.passes, 1, 0xffffffff) ||\n\t\t!uintInRange(input.outputBytes, 4, 0xffffffff)\n\t) {\n\t\treturn failure('invalid_parameters', 'Invalid Argon2 numeric parameters');\n\t}\n\tconst inputs = [input.password, input.salt, input.secret, input.associatedData];\n\tif (\n\t\t!(input.password instanceof Uint8Array) ||\n\t\t!(input.salt instanceof Uint8Array) ||\n\t\tinputs.some((bytes) => bytes !== undefined && !(bytes instanceof Uint8Array))\n\t) {\n\t\treturn failure('invalid_parameters', 'Argon2 inputs must be Uint8Array values');\n\t}\n\ttry {\n\t\t// A detached or out-of-bounds view reports zero length. Validate its\n\t\t// backing store before copying any of the caller's secret material.\n\t\tfor (const bytes of inputs) {\n\t\t\tif (bytes !== undefined) Uint8Array.prototype.values.call(bytes);\n\t\t}\n\t} catch {\n\t\treturn failure('invalid_parameters', 'Argon2 input buffer is unavailable');\n\t}\n\tconst bounds = [\n\t\t[input.memoryKiB, limits.maxMemoryKiB ?? 262144],\n\t\t[input.passes, limits.maxPasses ?? 16],\n\t\t[input.parallelism, limits.maxParallelism ?? 16],\n\t\t[input.outputBytes, limits.maxOutputBytes ?? 1024],\n\t\t[\n\t\t\tinputs.reduce((sum, bytes) => sum + (bytes?.byteLength ?? 0), 0),\n\t\t\tlimits.maxInputBytes ?? 1048576,\n\t\t],\n\t] as const;\n\tif (bounds.some(([, maximum]) => !uintInRange(maximum, 1, 0xffffffff))) {\n\t\treturn failure('invalid_parameters', 'Argon2 resource limits must be positive uint32 values');\n\t}\n\tif (bounds.some(([actual, maximum]) => actual > maximum)) {\n\t\treturn failure('limit_exceeded', 'Argon2 request exceeds resource limits');\n\t}\n\treturn undefined;\n}\n\nfunction snapshotInput(input: Argon2Input): Argon2Input {\n\t// Read declared properties once, including inherited and non-enumerable ones.\n\t// Byte arrays remain references until validation has enforced resource bounds.\n\tconst {\n\t\talgorithm,\n\t\tversion,\n\t\tpassword,\n\t\tsalt,\n\t\tmemoryKiB,\n\t\tpasses,\n\t\tparallelism,\n\t\toutputBytes,\n\t\tsecret,\n\t\tassociatedData,\n\t} = input;\n\treturn {\n\t\talgorithm,\n\t\tversion,\n\t\tpassword,\n\t\tsalt,\n\t\tmemoryKiB,\n\t\tpasses,\n\t\tparallelism,\n\t\toutputBytes,\n\t\t...(secret === undefined ? {} : { secret }),\n\t\t...(associatedData === undefined ? {} : { associatedData }),\n\t};\n}\n\nfunction copyInput(input: Argon2Input): Argon2Input {\n\treturn {\n\t\talgorithm: input.algorithm,\n\t\tversion: input.version,\n\t\tmemoryKiB: input.memoryKiB,\n\t\tpasses: input.passes,\n\t\tparallelism: input.parallelism,\n\t\toutputBytes: input.outputBytes,\n\t\tpassword: new Uint8Array(input.password),\n\t\tsalt: new Uint8Array(input.salt),\n\t\t...(input.secret === undefined ? {} : { secret: new Uint8Array(input.secret) }),\n\t\t...(input.associatedData === undefined\n\t\t\t? {}\n\t\t\t: { associatedData: new Uint8Array(input.associatedData) }),\n\t};\n}\n\nfunction eraseAccessible(bytes: Uint8Array | undefined): void {\n\t// Transfer detaches the sender's view. Its bytes can only be erased by the\n\t// receiving worker, and cleanup must still proceed through the other inputs.\n\tif (bytes !== undefined && bytes.byteLength > 0) Uint8Array.prototype.fill.call(bytes, 0);\n}\n\nasync function nativeArgon2(input: Argon2Input): Promise<Argon2Result> {\n\tif (input.version !== 0x13) {\n\t\treturn failure('unsupported', 'Native Argon2 supports version 0x13 only');\n\t}\n\tif (input.salt.byteLength < 8) {\n\t\treturn failure('unsupported', 'Native Argon2 requires a salt of at least eight bytes');\n\t}\n\tif (input.outputBytes > Math.floor(0xffffffff / 8)) {\n\t\treturn failure('unsupported', 'Requested output exceeds the native WebCrypto bit-length range');\n\t}\n\tif (typeof globalThis.crypto?.subtle === 'undefined') {\n\t\treturn failure('unsupported', 'Native WebCrypto is unavailable');\n\t}\n\tconst subtle = globalThis.crypto.subtle;\n\tlet name: 'Argon2d' | 'Argon2i' | 'Argon2id';\n\tswitch (input.algorithm) {\n\t\tcase 'argon2id':\n\t\t\tname = 'Argon2id';\n\t\t\tbreak;\n\t\tcase 'argon2d':\n\t\t\tname = 'Argon2d';\n\t\t\tbreak;\n\t\tcase 'argon2i':\n\t\t\tname = 'Argon2i';\n\t\t\tbreak;\n\t\tdefault:\n\t\t\treturn failure('unsupported', 'Unsupported Argon2 variant');\n\t}\n\ttry {\n\t\t// raw-secret is a runtime WebCrypto extension not present in every DOM\n\t\t// declaration. The result is narrowed rather than asserted or globally\n\t\t// augmenting consumers' SubtleCrypto definitions.\n\t\tlet key: unknown;\n\t\ttry {\n\t\t\tkey = await Reflect.apply(subtle.importKey, subtle, [\n\t\t\t\t'raw-secret',\n\t\t\t\tinput.password,\n\t\t\t\tname,\n\t\t\t\tfalse,\n\t\t\t\t['deriveBits'],\n\t\t\t]);\n\t\t} catch (error) {\n\t\t\t// Older WebCrypto KeyFormat enums reject raw-secret with TypeError.\n\t\t\t// Keep this classification local to import; deriveBits failures stay failures.\n\t\t\tif (error instanceof TypeError) {\n\t\t\t\treturn failure('unsupported', 'This runtime does not support the raw-secret key format');\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\t\tif (typeof CryptoKey === 'undefined' || !(key instanceof CryptoKey)) {\n\t\t\treturn failure('derivation_failed', 'Native Argon2 returned an invalid key');\n\t\t}\n\t\tconst params = {\n\t\t\tname,\n\t\t\tversion: input.version,\n\t\t\tmemory: input.memoryKiB,\n\t\t\tpasses: input.passes,\n\t\t\tparallelism: input.parallelism,\n\t\t\tnonce: new Uint8Array(input.salt),\n\t\t\t...(input.secret === undefined ? {} : { secretValue: new Uint8Array(input.secret) }),\n\t\t\t...(input.associatedData === undefined\n\t\t\t\t? {}\n\t\t\t\t: { associatedData: new Uint8Array(input.associatedData) }),\n\t\t};\n\t\ttry {\n\t\t\tconst output = await subtle.deriveBits(params, key, input.outputBytes * 8);\n\t\t\treturn { ok: true, value: new Uint8Array(output) };\n\t\t} finally {\n\t\t\tparams.nonce.fill(0);\n\t\t\tparams.secretValue?.fill(0);\n\t\t\tparams.associatedData?.fill(0);\n\t\t}\n\t} catch (error) {\n\t\treturn error instanceof Error && error.name === 'NotSupportedError'\n\t\t\t? failure('unsupported', 'This runtime does not support native Argon2')\n\t\t\t: failure('derivation_failed', 'Native Argon2 derivation failed');\n\t}\n}\n\nasync function builtinArgon2(\n\tinput: Argon2Input,\n\tbackend: 'native' | 'portable' | undefined,\n): Promise<Argon2Result> {\n\tif (backend !== 'portable') {\n\t\tconst result = await nativeArgon2(input);\n\t\tif (backend === 'native' || result.ok || result.error.code !== 'unsupported') return result;\n\t}\n\tconst { portableArgon2 } = await import('#micro509/kdf/argon2-portable');\n\treturn portableArgon2(input);\n}\n\n/**\n * Derives raw Argon2 bytes after validating parameters and resource ceilings.\n *\n * Native WebCrypto is preferred for version 0x13. An unsupported request uses\n * the TypeScript implementation for the exact variant and version, including\n * 0x10. Other native failures are returned without retrying. Explicit\n * backends never fall back. No algorithm, version, or parameter is substituted.\n *\n * Caller buffers are copied synchronously before the first await and never\n * modified. Owned temporary arrays are zeroed on completion. Native key storage\n * and runtime copies cannot be explicitly erased by JavaScript. The returned\n * output is caller-owned. Calls cannot cancel native work once dispatched.\n * TypeScript work yields while filling memory blocks. Its private memory matrix\n * is erased on completion or failure.\n * Limits apply per call, not to concurrent invocations.\n */\nexport async function deriveArgon2(\n\tinput: Argon2Input,\n\toptions: Argon2Options = {},\n): Promise<Argon2Result> {\n\tif (input === null || typeof input !== 'object') {\n\t\treturn failure('invalid_parameters', 'Argon2 input must be an object');\n\t}\n\tlet snapshot: Argon2Input;\n\ttry {\n\t\tsnapshot = snapshotInput(input);\n\t} catch {\n\t\treturn failure('invalid_parameters', 'Argon2 input properties could not be read');\n\t}\n\tconst invalid = validate(snapshot, options.limits ?? {});\n\tif (invalid !== undefined) return invalid;\n\tconst owned = copyInput(snapshot);\n\ttry {\n\t\tconst result =\n\t\t\ttypeof options.backend === 'function'\n\t\t\t\t? await options.backend(owned)\n\t\t\t\t: await builtinArgon2(owned, options.backend);\n\t\tif (!result.ok) {\n\t\t\t// A custom provider's diagnostic may contain secrets. Return only the\n\t\t\t// recognized code and a fixed message at this public boundary.\n\t\t\tconst code = result.error.code;\n\t\t\treturn failure(\n\t\t\t\tcode === 'unsupported' || code === 'limit_exceeded' || code === 'invalid_parameters'\n\t\t\t\t\t? code\n\t\t\t\t\t: 'derivation_failed',\n\t\t\t\t'Argon2 backend could not derive the requested key',\n\t\t\t);\n\t\t}\n\t\tif (!(result.value instanceof Uint8Array)) {\n\t\t\treturn failure('derivation_failed', 'Argon2 backend returned invalid output');\n\t\t}\n\t\ttry {\n\t\t\treturn result.value.byteLength === owned.outputBytes\n\t\t\t\t? { ok: true, value: new Uint8Array(result.value) }\n\t\t\t\t: failure('derivation_failed', 'Argon2 backend returned an incorrect output length');\n\t\t} finally {\n\t\t\teraseAccessible(result.value);\n\t\t}\n\t} catch {\n\t\treturn failure('derivation_failed', 'Argon2 backend failed');\n\t} finally {\n\t\teraseAccessible(owned.password);\n\t\teraseAccessible(owned.salt);\n\t\teraseAccessible(owned.secret);\n\t\teraseAccessible(owned.associatedData);\n\t}\n}\n"],"mappings":";AAsFA,SAAS,QAAQ,GAAuB,GAA+B;CACtE,OAAO;EAAE,IAAI;EAAO,OAAO;GAAE;GAAM;EAAQ;CAAE;AAC9C;AAEA,SAAS,YAAY,GAAe,GAAa,GAAsB;CACtE,OAAO,OAAO,cAAcA,CAAK,KAAKA,KAASC,KAAOD,KAASE;AAChE;AAEA,SAAS,SAAS,GAAoB,GAAgD;CACrF,IACCC,EAAM,cAAc,cACpBA,EAAM,cAAc,aACpBA,EAAM,cAAc,WAEpB,OAAO,QAAQ,eAAe,4BAA4B;CAE3D,IAAIA,EAAM,YAAY,MAAQA,EAAM,YAAY,IAC/C,OAAO,QAAQ,eAAe,4BAA4B;CAI3D,IACC,CAAC,YAAYA,EAAM,aAAa,GAAG,QAAQ,KAC3C,CAAC,YAAYA,EAAM,WAAW,IAAIA,EAAM,aAAa,UAAU,KAC/D,CAAC,YAAYA,EAAM,QAAQ,GAAG,UAAU,KACxC,CAAC,YAAYA,EAAM,aAAa,GAAG,UAAU,GAE7C,OAAO,QAAQ,sBAAsB,mCAAmC;CAEzE,IAAMC,IAAS;EAACD,EAAM;EAAUA,EAAM;EAAMA,EAAM;EAAQA,EAAM;CAAc;CAC9E,IACC,EAAEA,EAAM,oBAA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AAA,GAAW,OAAOA;CAClC,IAAME,IAAQ,UAAUH,CAAQ;CAChC,IAAI;EACH,IAAMF,IACL,OAAOI,EAAQ,WAAY,aACxB,MAAMA,EAAQ,QAAQC,CAAK,IAC3B,MAAM,cAAcA,GAAOD,EAAQ,OAAO;EAC9C,IAAI,CAACJ,EAAO,IAAI;GAGf,IAAMM,IAAON,EAAO,MAAM;GAC1B,OAAO,QACNM,MAAS,iBAAiBA,MAAS,oBAAoBA,MAAS,uBAC7DA,IACA,qBACH,mDACD;EACD;EACA,IAAI,EAAEN,EAAO,iBAAiB,aAC7B,OAAO,QAAQ,qBAAqB,wCAAwC;EAE7E,IAAI;GACH,OAAOA,EAAO,MAAM,eAAeK,EAAM,cACtC;IAAE,IAAI;IAAM,OAAO,IAAI,WAAWL,EAAO,KAAK;GAAE,IAChD,QAAQ,qBAAqB,oDAAoD;EACrF,UAAU;GACT,gBAAgBA,EAAO,KAAK;EAC7B;CACD,QAAQ;EACP,OAAO,QAAQ,qBAAqB,uBAAuB;CAC5D,UAAU;EAIT,AAHA,gBAAgBK,EAAM,QAAQ,GAC9B,gBAAgBA,EAAM,IAAI,GAC1B,gBAAgBA,EAAM,MAAM,GAC5B,gBAAgBA,EAAM,cAAc;CACrC;AACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "micro509",
|
|
3
|
-
"version": "0.17.
|
|
3
|
+
"version": "0.17.1",
|
|
4
4
|
"description": "The zero-dependency TypeScript PKI toolkit for real certificate workflows.",
|
|
5
5
|
"keywords": ["x509", "certificate", "csr", "pem", "der", "jwk", "importmapify"],
|
|
6
6
|
"homepage": "https://micro509.kjanat.dev",
|
|
@@ -88,7 +88,9 @@
|
|
|
88
88
|
"workspaces": ["packages/*", "site/.vitepress", "tsc/*"],
|
|
89
89
|
"scripts": {
|
|
90
90
|
"bd": "tsdown -F micro509",
|
|
91
|
+
"bench:argon2": "node bench/argon2/benchmark.ts",
|
|
91
92
|
"build": "tsdown -F micro509",
|
|
93
|
+
"build:argon2": "run @micro509/tsdown-plugin-argon2:build",
|
|
92
94
|
"build:tsc": "tsdown -F @kjanat/tsc-bridge -F @kjanat/tsc-compat -F @kjanat/tsc-protocol -F @kjanat/tsc-vue-mapper -F @kjanat/tsc-wasm",
|
|
93
95
|
"build:watch": "tsdown --watch",
|
|
94
96
|
"check": "run -pK check:biome check:deno typecheck",
|
|
@@ -111,7 +113,7 @@
|
|
|
111
113
|
"lint:fix": "biome check --fix",
|
|
112
114
|
"ms": "bun scripts/fetch-spec.bun.ts ms",
|
|
113
115
|
"playwright:chromium": "npx playwright install --with-deps chromium",
|
|
114
|
-
"prepare": "
|
|
116
|
+
"prepare": "run -s tsc/prepare.ts deno:importsmap deno:cache",
|
|
115
117
|
"prepublishOnly": "[ -n \"${GITHUB_ACTIONS:-}\" ] || { printf '%s\\n' 'manual npm publish blocked; use release workflow for provenance' >&2; exit 1; }",
|
|
116
118
|
"publish:jsr": "[ -n \"${GITHUB_ACTIONS:-}\" ] || { printf '%s\\n' 'manual JSR publish blocked; use release workflow' >&2; exit 1; }; BIN=\"${BIN:-bun}\"; $BIN x jsr publish",
|
|
117
119
|
"publish:pkgprnew": "BIN=\"${BIN:-bun}\"; $BIN x pkg-pr-new publish --packageManager=bun --bun --template './examples/vite' --template './examples/tsc-compat' . ./tsc/protocol ./tsc/bridge ./tsc/wasm ./tsc/compat ./tsc/vue-mapper",
|
|
@@ -128,8 +130,10 @@
|
|
|
128
130
|
"spec": "bun scripts/spec/main.ts",
|
|
129
131
|
"test": "BIN=\"${BIN:-bun}\"; AGENT=\"${AGENT:-1}\" $BIN test --concurrent",
|
|
130
132
|
"test:35433": "PR=\"35433\"; (command -v \"bun-${PR}\" >/dev/null || bunx bun-pr \"${PR}\") && BIN=\"bun-${PR}\" bun --silent run test",
|
|
133
|
+
"test:argon2:oracle": "run -s build:argon2 && bun test test/argon2-oracle.test.ts && node --test test/native/argon2-module.test.mjs test/native/argon2-memory.node.mjs",
|
|
131
134
|
"test:coverage": "run -q test --coverage",
|
|
132
|
-
"test:coverage:native": "node --experimental-test-coverage --test-coverage-include=src/kdf
|
|
135
|
+
"test:coverage:native": "mkdir -p coverage && node --experimental-test-coverage --test-coverage-include='src/kdf/*.ts' --test-reporter=spec --test-reporter-destination=stdout --test-reporter=lcov --test-reporter-destination=coverage/native.lcov --test test/native/argon2.node.mjs",
|
|
136
|
+
"test:coverage:portable": "mkdir -p coverage && node --experimental-test-coverage --test-coverage-include='src/kdf/*.ts' --test-reporter=spec --test-reporter-destination=stdout --test-reporter=lcov --test-reporter-destination=coverage/portable.lcov --test test/native/argon2-availability.node.mjs",
|
|
133
137
|
"test:differential": "BIN=\"${BIN:-bun}\"; $BIN test test/differential.test.ts test/differential-fuzz.test.ts",
|
|
134
138
|
"test:pkits": "BIN=\"${BIN:-bun}\"; $BIN test test/pkits.test.ts",
|
|
135
139
|
"test:watch": "BIN=\"${BIN:-bun}\"; $BIN run test --watch",
|
|
@@ -148,8 +152,6 @@
|
|
|
148
152
|
"@dprint/biome": "catalog:dprint",
|
|
149
153
|
"@dprint/formatter": "catalog:dprint",
|
|
150
154
|
"@dprint/json": "catalog:dprint",
|
|
151
|
-
"@dprint/markdown": "catalog:dprint",
|
|
152
|
-
"@dprint/typescript": "catalog:dprint",
|
|
153
155
|
"@gorhill/ubo-core": "0.1.30",
|
|
154
156
|
"@kjanat/tsc-bridge": "workspace:*",
|
|
155
157
|
"@micro509/doc-render": "workspace:*",
|
|
@@ -160,9 +162,6 @@
|
|
|
160
162
|
"biome": "catalog:",
|
|
161
163
|
"deno": "catalog:deno",
|
|
162
164
|
"dprint": "catalog:",
|
|
163
|
-
"dprint-plugin-malva": "catalog:dprint",
|
|
164
|
-
"dprint-plugin-markup": "catalog:dprint",
|
|
165
|
-
"dprint-plugin-yaml": "catalog:dprint",
|
|
166
165
|
"dreamcli": "npm:@kjanat/dreamcli@^5.0.0",
|
|
167
166
|
"importmapify": "catalog:deno",
|
|
168
167
|
"jose": "^6.2.13",
|
|
@@ -238,12 +237,7 @@
|
|
|
238
237
|
"dprint": {
|
|
239
238
|
"@dprint/biome": "^0.15.0",
|
|
240
239
|
"@dprint/formatter": "^0.6.0",
|
|
241
|
-
"@dprint/json": "^0.25.0"
|
|
242
|
-
"@dprint/markdown": "^0.28.0",
|
|
243
|
-
"@dprint/typescript": "^0.96.1",
|
|
244
|
-
"dprint-plugin-malva": "^0.16.0",
|
|
245
|
-
"dprint-plugin-markup": "^0.27.5",
|
|
246
|
-
"dprint-plugin-yaml": "^0.6.0"
|
|
240
|
+
"@dprint/json": "^0.25.0"
|
|
247
241
|
},
|
|
248
242
|
"site": {
|
|
249
243
|
"markdown-it-task-lists": "^2.1.1",
|