@ocap/mcrypto 1.30.24 → 1.30.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/crypter/aes-legacy.d.mts +11 -0
- package/esm/crypter/aes-legacy.mjs +67 -15
- package/esm/crypter/aes.d.mts +14 -2
- package/esm/crypter/aes.mjs +69 -3
- package/esm/encode.mjs +1 -1
- package/esm/hasher/keccak.mjs +7 -2
- package/esm/hasher/sha2.d.mts +1 -1
- package/esm/hasher/sha2.mjs +26 -11
- package/esm/hasher/sha3.d.mts +1 -1
- package/esm/hasher/sha3.mjs +21 -6
- package/esm/index.d.mts +1 -1
- package/esm/signer/ed25519.d.mts +1 -1
- package/esm/signer/ed25519.mjs +143 -16
- package/esm/signer/ethereum.mjs +7 -3
- package/esm/signer/secp256k1.mjs +3 -2
- package/esm/webauthn.d.mts +68 -2
- package/esm/webauthn.mjs +64 -16
- package/lib/_vendor/@noble/curves/LICENSE +21 -0
- package/lib/_vendor/@noble/curves/abstract/curve.cjs +373 -0
- package/lib/_vendor/@noble/curves/abstract/modular.cjs +616 -0
- package/lib/_vendor/@noble/curves/abstract/weierstrass.cjs +1020 -0
- package/lib/_vendor/@noble/curves/nist.cjs +91 -0
- package/lib/_vendor/@noble/curves/secp256k1.cjs +73 -0
- package/lib/_vendor/@noble/curves/utils.cjs +463 -0
- package/lib/_vendor/@noble/ed25519/LICENSE +21 -0
- package/lib/_vendor/@noble/ed25519/index.cjs +512 -0
- package/lib/_vendor/@noble/hashes/LICENSE +21 -0
- package/lib/_vendor/@noble/hashes/_md.cjs +230 -0
- package/lib/_vendor/@noble/hashes/_u64.cjs +67 -0
- package/lib/_vendor/@noble/hashes/hmac.cjs +86 -0
- package/lib/_vendor/@noble/hashes/sha2.cjs +477 -0
- package/lib/_vendor/@noble/hashes/sha3.cjs +329 -0
- package/lib/_vendor/@noble/hashes/utils.cjs +410 -0
- package/lib/crypter/aes-legacy.cjs +68 -21
- package/lib/crypter/aes-legacy.d.cts +11 -0
- package/lib/crypter/aes.cjs +69 -2
- package/lib/crypter/aes.d.cts +14 -2
- package/lib/encode.cjs +1 -1
- package/lib/hasher/keccak.cjs +13 -8
- package/lib/hasher/sha2.cjs +32 -17
- package/lib/hasher/sha2.d.cts +1 -1
- package/lib/hasher/sha3.cjs +27 -12
- package/lib/hasher/sha3.d.cts +1 -1
- package/lib/index.d.cts +1 -1
- package/lib/signer/ed25519.cjs +146 -20
- package/lib/signer/ed25519.d.cts +1 -1
- package/lib/signer/ethereum.cjs +16 -12
- package/lib/signer/secp256k1.cjs +11 -10
- package/lib/webauthn.cjs +80 -31
- package/lib/webauthn.d.cts +68 -2
- package/package.json +6 -7
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
|
|
2
|
+
//#region ../../node_modules/@noble/hashes/utils.js
|
|
3
|
+
/**
|
|
4
|
+
* Checks if something is Uint8Array. Be careful: nodejs Buffer will return true.
|
|
5
|
+
* @param a - value to test
|
|
6
|
+
* @returns `true` when the value is a Uint8Array-compatible view.
|
|
7
|
+
* @example
|
|
8
|
+
* Check whether a value is a Uint8Array-compatible view.
|
|
9
|
+
* ```ts
|
|
10
|
+
* isBytes(new Uint8Array([1, 2, 3]));
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
function isBytes(a) {
|
|
14
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array" && "BYTES_PER_ELEMENT" in a && a.BYTES_PER_ELEMENT === 1;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Asserts something is a non-negative integer.
|
|
18
|
+
* @param n - number to validate
|
|
19
|
+
* @param title - label included in thrown errors
|
|
20
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
21
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
22
|
+
* @example
|
|
23
|
+
* Validate a non-negative integer option.
|
|
24
|
+
* ```ts
|
|
25
|
+
* anumber(32, 'length');
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
function anumber(n, title = "") {
|
|
29
|
+
if (typeof n !== "number") {
|
|
30
|
+
const prefix = title && `"${title}" `;
|
|
31
|
+
throw new TypeError(`${prefix}expected number, got ${typeof n}`);
|
|
32
|
+
}
|
|
33
|
+
if (!Number.isSafeInteger(n) || n < 0) {
|
|
34
|
+
const prefix = title && `"${title}" `;
|
|
35
|
+
throw new RangeError(`${prefix}expected integer >= 0, got ${n}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Asserts something is Uint8Array.
|
|
40
|
+
* @param value - value to validate
|
|
41
|
+
* @param length - optional exact length constraint
|
|
42
|
+
* @param title - label included in thrown errors
|
|
43
|
+
* @returns The validated byte array.
|
|
44
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
45
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
46
|
+
* @example
|
|
47
|
+
* Validate that a value is a byte array.
|
|
48
|
+
* ```ts
|
|
49
|
+
* abytes(new Uint8Array([1, 2, 3]));
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
function abytes(value, length, title = "") {
|
|
53
|
+
const bytes = isBytes(value);
|
|
54
|
+
const len = value?.length;
|
|
55
|
+
const needsLen = length !== void 0;
|
|
56
|
+
if (!bytes || needsLen && len !== length) {
|
|
57
|
+
const prefix = title && `"${title}" `;
|
|
58
|
+
const ofLen = needsLen ? ` of length ${length}` : "";
|
|
59
|
+
const got = bytes ? `length=${len}` : `type=${typeof value}`;
|
|
60
|
+
const message = prefix + "expected Uint8Array" + ofLen + ", got " + got;
|
|
61
|
+
if (!bytes) throw new TypeError(message);
|
|
62
|
+
throw new RangeError(message);
|
|
63
|
+
}
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Asserts something is a wrapped hash constructor.
|
|
68
|
+
* @param h - hash constructor to validate
|
|
69
|
+
* @throws On wrong argument types or invalid hash wrapper shape. {@link TypeError}
|
|
70
|
+
* @throws On invalid hash metadata ranges or values. {@link RangeError}
|
|
71
|
+
* @throws If the hash metadata allows empty outputs or block sizes. {@link Error}
|
|
72
|
+
* @example
|
|
73
|
+
* Validate a callable hash wrapper.
|
|
74
|
+
* ```ts
|
|
75
|
+
* import { ahash } from '@noble/hashes/utils.js';
|
|
76
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
77
|
+
* ahash(sha256);
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
function ahash(h) {
|
|
81
|
+
if (typeof h !== "function" || typeof h.create !== "function") throw new TypeError("Hash must wrapped by utils.createHasher");
|
|
82
|
+
anumber(h.outputLen);
|
|
83
|
+
anumber(h.blockLen);
|
|
84
|
+
if (h.outputLen < 1) throw new Error("\"outputLen\" must be >= 1");
|
|
85
|
+
if (h.blockLen < 1) throw new Error("\"blockLen\" must be >= 1");
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Asserts a hash instance has not been destroyed or finished.
|
|
89
|
+
* @param instance - hash instance to validate
|
|
90
|
+
* @param checkFinished - whether to reject finalized instances
|
|
91
|
+
* @throws If the hash instance has already been destroyed or finalized. {@link Error}
|
|
92
|
+
* @example
|
|
93
|
+
* Validate that a hash instance is still usable.
|
|
94
|
+
* ```ts
|
|
95
|
+
* import { aexists } from '@noble/hashes/utils.js';
|
|
96
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
97
|
+
* const hash = sha256.create();
|
|
98
|
+
* aexists(hash);
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
function aexists(instance, checkFinished = true) {
|
|
102
|
+
if (instance.destroyed) throw new Error("Hash instance has been destroyed");
|
|
103
|
+
if (checkFinished && instance.finished) throw new Error("Hash#digest() has already been called");
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Asserts output is a sufficiently-sized byte array.
|
|
107
|
+
* @param out - destination buffer
|
|
108
|
+
* @param instance - hash instance providing output length
|
|
109
|
+
* Oversized buffers are allowed; downstream code only promises to fill the first `outputLen` bytes.
|
|
110
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
111
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
112
|
+
* @example
|
|
113
|
+
* Validate a caller-provided digest buffer.
|
|
114
|
+
* ```ts
|
|
115
|
+
* import { aoutput } from '@noble/hashes/utils.js';
|
|
116
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
117
|
+
* const hash = sha256.create();
|
|
118
|
+
* aoutput(new Uint8Array(hash.outputLen), hash);
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
function aoutput(out, instance) {
|
|
122
|
+
abytes(out, void 0, "digestInto() output");
|
|
123
|
+
const min = instance.outputLen;
|
|
124
|
+
if (out.length < min) throw new RangeError("\"digestInto() output\" expected to be of length >=" + min);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Casts a typed array view to Uint32Array.
|
|
128
|
+
* `arr.byteOffset` must already be 4-byte aligned or the platform
|
|
129
|
+
* Uint32Array constructor will throw.
|
|
130
|
+
* @param arr - source typed array
|
|
131
|
+
* @returns Uint32Array view over the same buffer.
|
|
132
|
+
* @example
|
|
133
|
+
* Reinterpret a byte array as 32-bit words.
|
|
134
|
+
* ```ts
|
|
135
|
+
* u32(new Uint8Array(8));
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
function u32(arr) {
|
|
139
|
+
return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Zeroizes typed arrays in place. Warning: JS provides no guarantees.
|
|
143
|
+
* @param arrays - arrays to overwrite with zeros
|
|
144
|
+
* @example
|
|
145
|
+
* Zeroize sensitive buffers in place.
|
|
146
|
+
* ```ts
|
|
147
|
+
* clean(new Uint8Array([1, 2, 3]));
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
function clean(...arrays) {
|
|
151
|
+
for (let i = 0; i < arrays.length; i++) arrays[i].fill(0);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Creates a DataView for byte-level manipulation.
|
|
155
|
+
* @param arr - source typed array
|
|
156
|
+
* @returns DataView over the same buffer region.
|
|
157
|
+
* @example
|
|
158
|
+
* Create a DataView over an existing buffer.
|
|
159
|
+
* ```ts
|
|
160
|
+
* createView(new Uint8Array(4));
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
function createView(arr) {
|
|
164
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Rotate-right operation for uint32 values.
|
|
168
|
+
* @param word - source word
|
|
169
|
+
* @param shift - shift amount in bits
|
|
170
|
+
* @returns Rotated word.
|
|
171
|
+
* @example
|
|
172
|
+
* Rotate a 32-bit word to the right.
|
|
173
|
+
* ```ts
|
|
174
|
+
* rotr(0x12345678, 8);
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
function rotr(word, shift) {
|
|
178
|
+
return word << 32 - shift | word >>> shift;
|
|
179
|
+
}
|
|
180
|
+
/** Whether the current platform is little-endian. */
|
|
181
|
+
const isLE = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68)();
|
|
182
|
+
/**
|
|
183
|
+
* Byte-swap operation for uint32 values.
|
|
184
|
+
* @param word - source word
|
|
185
|
+
* @returns Word with reversed byte order.
|
|
186
|
+
* @example
|
|
187
|
+
* Reverse the byte order of a 32-bit word.
|
|
188
|
+
* ```ts
|
|
189
|
+
* byteSwap(0x11223344);
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
function byteSwap(word) {
|
|
193
|
+
return word << 24 & 4278190080 | word << 8 & 16711680 | word >>> 8 & 65280 | word >>> 24 & 255;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Byte-swaps every word of a Uint32Array in place.
|
|
197
|
+
* @param arr - array to mutate
|
|
198
|
+
* @returns The same array after mutation; callers pass live state arrays here.
|
|
199
|
+
* @example
|
|
200
|
+
* Reverse the byte order of every word in place.
|
|
201
|
+
* ```ts
|
|
202
|
+
* byteSwap32(new Uint32Array([0x11223344]));
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
function byteSwap32(arr) {
|
|
206
|
+
for (let i = 0; i < arr.length; i++) arr[i] = byteSwap(arr[i]);
|
|
207
|
+
return arr;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Conditionally byte-swaps a Uint32Array on big-endian platforms.
|
|
211
|
+
* @param u - array to normalize for host endianness
|
|
212
|
+
* @returns Original or byte-swapped array depending on platform endianness.
|
|
213
|
+
* On big-endian runtimes this mutates `u` in place via `byteSwap32(...)`.
|
|
214
|
+
* @example
|
|
215
|
+
* Normalize a word array for host endianness.
|
|
216
|
+
* ```ts
|
|
217
|
+
* swap32IfBE(new Uint32Array([0x11223344]));
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
const swap32IfBE = isLE ? (u) => u : byteSwap32;
|
|
221
|
+
const hasHexBuiltin = /* @__PURE__ */ (() => typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function")();
|
|
222
|
+
const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
223
|
+
/**
|
|
224
|
+
* Convert byte array to hex string.
|
|
225
|
+
* Uses the built-in function when available and assumes it matches the tested
|
|
226
|
+
* fallback semantics.
|
|
227
|
+
* @param bytes - bytes to encode
|
|
228
|
+
* @returns Lowercase hexadecimal string.
|
|
229
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
230
|
+
* @example
|
|
231
|
+
* Convert bytes to lowercase hexadecimal.
|
|
232
|
+
* ```ts
|
|
233
|
+
* bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])); // 'cafe0123'
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
236
|
+
function bytesToHex(bytes) {
|
|
237
|
+
abytes(bytes);
|
|
238
|
+
if (hasHexBuiltin) return bytes.toHex();
|
|
239
|
+
let hex = "";
|
|
240
|
+
for (let i = 0; i < bytes.length; i++) hex += hexes[bytes[i]];
|
|
241
|
+
return hex;
|
|
242
|
+
}
|
|
243
|
+
const asciis = {
|
|
244
|
+
_0: 48,
|
|
245
|
+
_9: 57,
|
|
246
|
+
A: 65,
|
|
247
|
+
F: 70,
|
|
248
|
+
a: 97,
|
|
249
|
+
f: 102
|
|
250
|
+
};
|
|
251
|
+
function asciiToBase16(ch) {
|
|
252
|
+
if (ch >= asciis._0 && ch <= asciis._9) return ch - asciis._0;
|
|
253
|
+
if (ch >= asciis.A && ch <= asciis.F) return ch - (asciis.A - 10);
|
|
254
|
+
if (ch >= asciis.a && ch <= asciis.f) return ch - (asciis.a - 10);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Convert hex string to byte array. Uses built-in function, when available.
|
|
258
|
+
* @param hex - hexadecimal string to decode
|
|
259
|
+
* @returns Decoded bytes.
|
|
260
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
261
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
262
|
+
* @example
|
|
263
|
+
* Decode lowercase hexadecimal into bytes.
|
|
264
|
+
* ```ts
|
|
265
|
+
* hexToBytes('cafe0123'); // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
268
|
+
function hexToBytes(hex) {
|
|
269
|
+
if (typeof hex !== "string") throw new TypeError("hex string expected, got " + typeof hex);
|
|
270
|
+
if (hasHexBuiltin) try {
|
|
271
|
+
return Uint8Array.fromHex(hex);
|
|
272
|
+
} catch (error) {
|
|
273
|
+
if (error instanceof SyntaxError) throw new RangeError(error.message);
|
|
274
|
+
throw error;
|
|
275
|
+
}
|
|
276
|
+
const hl = hex.length;
|
|
277
|
+
const al = hl / 2;
|
|
278
|
+
if (hl % 2) throw new RangeError("hex string expected, got unpadded hex of length " + hl);
|
|
279
|
+
const array = new Uint8Array(al);
|
|
280
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
281
|
+
const n1 = asciiToBase16(hex.charCodeAt(hi));
|
|
282
|
+
const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
|
283
|
+
if (n1 === void 0 || n2 === void 0) {
|
|
284
|
+
const char = hex[hi] + hex[hi + 1];
|
|
285
|
+
throw new RangeError("hex string expected, got non-hex character \"" + char + "\" at index " + hi);
|
|
286
|
+
}
|
|
287
|
+
array[ai] = n1 * 16 + n2;
|
|
288
|
+
}
|
|
289
|
+
return array;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Copies several Uint8Arrays into one.
|
|
293
|
+
* @param arrays - arrays to concatenate
|
|
294
|
+
* @returns Concatenated byte array.
|
|
295
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
296
|
+
* @example
|
|
297
|
+
* Concatenate multiple byte arrays.
|
|
298
|
+
* ```ts
|
|
299
|
+
* concatBytes(new Uint8Array([1]), new Uint8Array([2]));
|
|
300
|
+
* ```
|
|
301
|
+
*/
|
|
302
|
+
function concatBytes(...arrays) {
|
|
303
|
+
let sum = 0;
|
|
304
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
305
|
+
const a = arrays[i];
|
|
306
|
+
abytes(a);
|
|
307
|
+
sum += a.length;
|
|
308
|
+
}
|
|
309
|
+
const res = new Uint8Array(sum);
|
|
310
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
311
|
+
const a = arrays[i];
|
|
312
|
+
res.set(a, pad);
|
|
313
|
+
pad += a.length;
|
|
314
|
+
}
|
|
315
|
+
return res;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Creates a callable hash function from a stateful class constructor.
|
|
319
|
+
* @param hashCons - hash constructor or factory
|
|
320
|
+
* @param info - optional metadata such as DER OID
|
|
321
|
+
* @returns Frozen callable hash wrapper with `.create()`.
|
|
322
|
+
* Wrapper construction eagerly calls `hashCons(undefined)` once to read
|
|
323
|
+
* `outputLen` / `blockLen`, so constructor side effects happen at module
|
|
324
|
+
* init time.
|
|
325
|
+
* @example
|
|
326
|
+
* Wrap a stateful hash constructor into a callable helper.
|
|
327
|
+
* ```ts
|
|
328
|
+
* import { createHasher } from '@noble/hashes/utils.js';
|
|
329
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
330
|
+
* const wrapped = createHasher(sha256.create, { oid: sha256.oid });
|
|
331
|
+
* wrapped(new Uint8Array([1]));
|
|
332
|
+
* ```
|
|
333
|
+
*/
|
|
334
|
+
function createHasher(hashCons, info = {}) {
|
|
335
|
+
const hashC = (msg, opts) => hashCons(opts).update(msg).digest();
|
|
336
|
+
const tmp = hashCons(void 0);
|
|
337
|
+
hashC.outputLen = tmp.outputLen;
|
|
338
|
+
hashC.blockLen = tmp.blockLen;
|
|
339
|
+
hashC.canXOF = tmp.canXOF;
|
|
340
|
+
hashC.create = (opts) => hashCons(opts);
|
|
341
|
+
Object.assign(hashC, info);
|
|
342
|
+
return Object.freeze(hashC);
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Cryptographically secure PRNG backed by `crypto.getRandomValues`.
|
|
346
|
+
* @param bytesLength - number of random bytes to generate
|
|
347
|
+
* @returns Random bytes.
|
|
348
|
+
* The platform `getRandomValues()` implementation still defines any
|
|
349
|
+
* single-call length cap, and this helper rejects oversize requests
|
|
350
|
+
* with a stable library `RangeError` instead of host-specific errors.
|
|
351
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
352
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
353
|
+
* @throws If the current runtime does not provide `crypto.getRandomValues`. {@link Error}
|
|
354
|
+
* @example
|
|
355
|
+
* Generate a fresh random key or nonce.
|
|
356
|
+
* ```ts
|
|
357
|
+
* const key = randomBytes(16);
|
|
358
|
+
* ```
|
|
359
|
+
*/
|
|
360
|
+
function randomBytes(bytesLength = 32) {
|
|
361
|
+
anumber(bytesLength, "bytesLength");
|
|
362
|
+
const cr = typeof globalThis === "object" ? globalThis.crypto : null;
|
|
363
|
+
if (typeof cr?.getRandomValues !== "function") throw new Error("crypto.getRandomValues must be defined");
|
|
364
|
+
if (bytesLength > 65536) throw new RangeError(`"bytesLength" expected <= 65536, got ${bytesLength}`);
|
|
365
|
+
return cr.getRandomValues(new Uint8Array(bytesLength));
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Creates OID metadata for NIST hashes with prefix `06 09 60 86 48 01 65 03 04 02`.
|
|
369
|
+
* @param suffix - final OID byte for the selected hash.
|
|
370
|
+
* The helper accepts any byte even though only the documented NIST hash
|
|
371
|
+
* suffixes are meaningful downstream.
|
|
372
|
+
* @returns Object containing the DER-encoded OID.
|
|
373
|
+
* @example
|
|
374
|
+
* Build OID metadata for a NIST hash.
|
|
375
|
+
* ```ts
|
|
376
|
+
* oidNist(0x01);
|
|
377
|
+
* ```
|
|
378
|
+
*/
|
|
379
|
+
const oidNist = (suffix) => ({ oid: Uint8Array.from([
|
|
380
|
+
6,
|
|
381
|
+
9,
|
|
382
|
+
96,
|
|
383
|
+
134,
|
|
384
|
+
72,
|
|
385
|
+
1,
|
|
386
|
+
101,
|
|
387
|
+
3,
|
|
388
|
+
4,
|
|
389
|
+
2,
|
|
390
|
+
suffix
|
|
391
|
+
]) });
|
|
392
|
+
|
|
393
|
+
//#endregion
|
|
394
|
+
exports.abytes = abytes;
|
|
395
|
+
exports.aexists = aexists;
|
|
396
|
+
exports.ahash = ahash;
|
|
397
|
+
exports.anumber = anumber;
|
|
398
|
+
exports.aoutput = aoutput;
|
|
399
|
+
exports.bytesToHex = bytesToHex;
|
|
400
|
+
exports.clean = clean;
|
|
401
|
+
exports.concatBytes = concatBytes;
|
|
402
|
+
exports.createHasher = createHasher;
|
|
403
|
+
exports.createView = createView;
|
|
404
|
+
exports.hexToBytes = hexToBytes;
|
|
405
|
+
exports.isBytes = isBytes;
|
|
406
|
+
exports.oidNist = oidNist;
|
|
407
|
+
exports.randomBytes = randomBytes;
|
|
408
|
+
exports.rotr = rotr;
|
|
409
|
+
exports.swap32IfBE = swap32IfBE;
|
|
410
|
+
exports.u32 = u32;
|
|
@@ -1,34 +1,81 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
2
|
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
3
3
|
const require_protocols_crypter = require('../protocols/crypter.cjs');
|
|
4
|
-
let
|
|
5
|
-
|
|
6
|
-
let crypto_js_enc_base64 = require("crypto-js/enc-base64");
|
|
7
|
-
crypto_js_enc_base64 = require_rolldown_runtime.__toESM(crypto_js_enc_base64);
|
|
8
|
-
let crypto_js_enc_hex = require("crypto-js/enc-hex");
|
|
9
|
-
crypto_js_enc_hex = require_rolldown_runtime.__toESM(crypto_js_enc_hex);
|
|
10
|
-
let crypto_js_enc_latin1 = require("crypto-js/enc-latin1");
|
|
11
|
-
crypto_js_enc_latin1 = require_rolldown_runtime.__toESM(crypto_js_enc_latin1);
|
|
12
|
-
let crypto_js_enc_utf8 = require("crypto-js/enc-utf8");
|
|
13
|
-
crypto_js_enc_utf8 = require_rolldown_runtime.__toESM(crypto_js_enc_utf8);
|
|
14
|
-
let crypto_js_enc_utf16 = require("crypto-js/enc-utf16");
|
|
15
|
-
crypto_js_enc_utf16 = require_rolldown_runtime.__toESM(crypto_js_enc_utf16);
|
|
4
|
+
let node_crypto = require("node:crypto");
|
|
5
|
+
node_crypto = require_rolldown_runtime.__toESM(node_crypto);
|
|
16
6
|
|
|
17
7
|
//#region src/crypter/aes-legacy.ts
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
8
|
+
const OPENSSL_SALT_MAGIC = Buffer.from("Salted__", "latin1");
|
|
9
|
+
/**
|
|
10
|
+
* OpenSSL EVP_BytesToKey (MD5, 1 iteration) key + IV derivation for backward
|
|
11
|
+
* compatibility with historical `crypto-js` `Salted__` AES-256-CBC ciphertexts.
|
|
12
|
+
*/
|
|
13
|
+
function evpBytesToKey(password, salt) {
|
|
14
|
+
const blocks = [];
|
|
15
|
+
let prev = Buffer.alloc(0);
|
|
16
|
+
while (Buffer.concat(blocks).length < 48) {
|
|
17
|
+
const hash = node_crypto.default.createHash("md5");
|
|
18
|
+
if (prev.length > 0) hash.update(prev);
|
|
19
|
+
hash.update(password);
|
|
20
|
+
hash.update(salt);
|
|
21
|
+
prev = hash.digest();
|
|
22
|
+
blocks.push(prev);
|
|
23
|
+
}
|
|
24
|
+
const derived = Buffer.concat(blocks);
|
|
25
|
+
return {
|
|
26
|
+
key: derived.subarray(0, 32),
|
|
27
|
+
iv: derived.subarray(32, 48)
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function encodeOutput(buf, outputEncoding = "utf8") {
|
|
31
|
+
switch (outputEncoding) {
|
|
32
|
+
case "utf8": return buf.toString("utf8");
|
|
33
|
+
case "hex": return buf.toString("hex");
|
|
34
|
+
case "latin1": return buf.toString("latin1");
|
|
35
|
+
case "base64": return buf.toString("base64");
|
|
36
|
+
case "utf16": {
|
|
37
|
+
let str = "";
|
|
38
|
+
for (let i = 0; i < buf.length; i += 2) {
|
|
39
|
+
const hi = buf[i] ?? 0;
|
|
40
|
+
const lo = buf[i + 1] ?? 0;
|
|
41
|
+
str += String.fromCharCode(hi << 8 | lo);
|
|
42
|
+
}
|
|
43
|
+
return str;
|
|
44
|
+
}
|
|
45
|
+
default: throw new Error(`Unsupported output encoding: ${outputEncoding}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* @deprecated Legacy OpenSSL `Salted__` AES-256-CBC crypter kept only for backward compatibility
|
|
50
|
+
* with historical ciphertexts. Do not use for new encryption; use `@ocap/mcrypto/lib/crypter/aes` (AES-256-GCM).
|
|
51
|
+
*/
|
|
25
52
|
var AesCrypter = class extends require_protocols_crypter.default {
|
|
53
|
+
/**
|
|
54
|
+
* @deprecated Use `@ocap/mcrypto/lib/crypter/aes` (AES-256-GCM) for new data encryption.
|
|
55
|
+
*/
|
|
26
56
|
encrypt(message, secret) {
|
|
27
57
|
const text = typeof message === "string" ? message : JSON.stringify(message);
|
|
28
|
-
|
|
58
|
+
const salt = node_crypto.default.randomBytes(8);
|
|
59
|
+
const { key, iv } = evpBytesToKey(Buffer.from(secret, "utf8"), salt);
|
|
60
|
+
const cipher = node_crypto.default.createCipheriv("aes-256-cbc", key, iv);
|
|
61
|
+
const encrypted = Buffer.concat([cipher.update(Buffer.from(text, "utf8")), cipher.final()]);
|
|
62
|
+
return Buffer.concat([
|
|
63
|
+
OPENSSL_SALT_MAGIC,
|
|
64
|
+
salt,
|
|
65
|
+
encrypted
|
|
66
|
+
]).toString("base64");
|
|
29
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* @deprecated Legacy OpenSSL `Salted__` AES-256-CBC decryption.
|
|
70
|
+
*/
|
|
30
71
|
decrypt(cipher, secret, outputEncoding = "utf8") {
|
|
31
|
-
|
|
72
|
+
const raw = Buffer.from(cipher, "base64");
|
|
73
|
+
if (raw.length < 16 || !raw.subarray(0, 8).equals(OPENSSL_SALT_MAGIC)) return "";
|
|
74
|
+
const salt = raw.subarray(8, 16);
|
|
75
|
+
const ciphertext = raw.subarray(16);
|
|
76
|
+
const { key, iv } = evpBytesToKey(Buffer.from(secret, "utf8"), salt);
|
|
77
|
+
const decipher = node_crypto.default.createDecipheriv("aes-256-cbc", key, iv);
|
|
78
|
+
return encodeOutput(Buffer.concat([decipher.update(ciphertext), decipher.final()]), outputEncoding);
|
|
32
79
|
}
|
|
33
80
|
};
|
|
34
81
|
var aes_legacy_default = new AesCrypter();
|
|
@@ -1,8 +1,19 @@
|
|
|
1
1
|
import _default$1 from "../protocols/crypter.cjs";
|
|
2
2
|
|
|
3
3
|
//#region src/crypter/aes-legacy.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @deprecated Legacy OpenSSL `Salted__` AES-256-CBC crypter kept only for backward compatibility
|
|
7
|
+
* with historical ciphertexts. Do not use for new encryption; use `@ocap/mcrypto/lib/crypter/aes` (AES-256-GCM).
|
|
8
|
+
*/
|
|
4
9
|
declare class AesCrypter extends _default$1 {
|
|
10
|
+
/**
|
|
11
|
+
* @deprecated Use `@ocap/mcrypto/lib/crypter/aes` (AES-256-GCM) for new data encryption.
|
|
12
|
+
*/
|
|
5
13
|
encrypt(message: string | object, secret: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* @deprecated Legacy OpenSSL `Salted__` AES-256-CBC decryption.
|
|
16
|
+
*/
|
|
6
17
|
decrypt(cipher: string, secret: string, outputEncoding?: string): string;
|
|
7
18
|
}
|
|
8
19
|
declare const _default: AesCrypter;
|
package/lib/crypter/aes.cjs
CHANGED
|
@@ -8,8 +8,57 @@ let node_crypto = require("node:crypto");
|
|
|
8
8
|
node_crypto = require_rolldown_runtime.__toESM(node_crypto);
|
|
9
9
|
|
|
10
10
|
//#region src/crypter/aes.ts
|
|
11
|
+
/**
|
|
12
|
+
* Magic header for v2 AES-256-GCM authenticated ciphertexts ("GCM\x02").
|
|
13
|
+
* Binary format: [4B magic "GCM\x02"][16B salt][12B iv][ciphertext...][16B authTag]
|
|
14
|
+
*/
|
|
15
|
+
const GCM_MAGIC = Buffer.from([
|
|
16
|
+
71,
|
|
17
|
+
67,
|
|
18
|
+
77,
|
|
19
|
+
2
|
|
20
|
+
]);
|
|
21
|
+
const SALT_LENGTH = 16;
|
|
22
|
+
const IV_LENGTH = 12;
|
|
23
|
+
const AUTH_TAG_LENGTH = 16;
|
|
24
|
+
const MIN_GCM_ENVELOPE_LENGTH = GCM_MAGIC.length + SALT_LENGTH + IV_LENGTH + AUTH_TAG_LENGTH;
|
|
25
|
+
const HKDF_INFO = Buffer.from("ocap-aes-gcm-v2", "utf8");
|
|
26
|
+
function deriveGcmKey(secret, salt) {
|
|
27
|
+
const secretBuf = (0, _ocap_util.toBuffer)(secret);
|
|
28
|
+
const derived = node_crypto.default.hkdfSync("sha256", secretBuf, salt, HKDF_INFO, 32);
|
|
29
|
+
return new Uint8Array(derived);
|
|
30
|
+
}
|
|
31
|
+
function hasGcmMagic(data) {
|
|
32
|
+
if (data.length < MIN_GCM_ENVELOPE_LENGTH) return false;
|
|
33
|
+
return data[0] === GCM_MAGIC[0] && data[1] === GCM_MAGIC[1] && data[2] === GCM_MAGIC[2] && data[3] === GCM_MAGIC[3];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Authenticated AES-256-GCM Crypter with HKDF-SHA256 salted key derivation
|
|
37
|
+
* and backward-compatible read-only decryption for legacy AES-256-ECB payloads.
|
|
38
|
+
*/
|
|
11
39
|
var AesCrypter = class extends require_protocols_crypter.default {
|
|
12
40
|
encrypt(message, secret, encoding = "hex") {
|
|
41
|
+
const salt = node_crypto.default.randomBytes(SALT_LENGTH);
|
|
42
|
+
const iv = node_crypto.default.randomBytes(IV_LENGTH);
|
|
43
|
+
const key = deriveGcmKey(secret, salt);
|
|
44
|
+
const cipher = node_crypto.default.createCipheriv("aes-256-gcm", key, iv);
|
|
45
|
+
const plaintext = Uint8Array.from((0, _ocap_util.toBuffer)(message));
|
|
46
|
+
const encrypted = Buffer.concat([Uint8Array.from(cipher.update(plaintext)), Uint8Array.from(cipher.final())]);
|
|
47
|
+
const authTag = cipher.getAuthTag();
|
|
48
|
+
const envelope = Buffer.concat([
|
|
49
|
+
GCM_MAGIC,
|
|
50
|
+
salt,
|
|
51
|
+
iv,
|
|
52
|
+
encrypted,
|
|
53
|
+
authTag
|
|
54
|
+
]);
|
|
55
|
+
return require_encode.encode(Uint8Array.from(envelope), encoding);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* @deprecated Legacy deterministic AES-256-ECB encryption kept only for compatibility test vectors.
|
|
59
|
+
* Do not use for new data; use `encrypt()` (AES-256-GCM) instead.
|
|
60
|
+
*/
|
|
61
|
+
encryptLegacyEcb(message, secret, encoding = "hex") {
|
|
13
62
|
const keyBuffer = require_hasher_sha3.default.hash256(secret, 1, "buffer");
|
|
14
63
|
const key = Uint8Array.from(keyBuffer);
|
|
15
64
|
const cipher = node_crypto.default.createCipheriv("aes-256-ecb", key, "");
|
|
@@ -17,12 +66,29 @@ var AesCrypter = class extends require_protocols_crypter.default {
|
|
|
17
66
|
const output = cipher.update(Uint8Array.from((0, _ocap_util.toBuffer)(message)));
|
|
18
67
|
return require_encode.encode(Uint8Array.from(Buffer.concat([Uint8Array.from(output), Uint8Array.from(cipher.final())])), encoding);
|
|
19
68
|
}
|
|
20
|
-
decrypt(message, secret, encoding = "hex") {
|
|
69
|
+
decrypt(message, secret, encoding = "hex", options) {
|
|
70
|
+
const data = (0, _ocap_util.toBuffer)(message);
|
|
71
|
+
if (hasGcmMagic(data)) {
|
|
72
|
+
const saltStart = GCM_MAGIC.length;
|
|
73
|
+
const ivStart = saltStart + SALT_LENGTH;
|
|
74
|
+
const cipherStart = ivStart + IV_LENGTH;
|
|
75
|
+
const tagStart = data.length - AUTH_TAG_LENGTH;
|
|
76
|
+
const salt = data.subarray(saltStart, ivStart);
|
|
77
|
+
const iv = data.subarray(ivStart, cipherStart);
|
|
78
|
+
const encrypted = data.subarray(cipherStart, tagStart);
|
|
79
|
+
const authTag = data.subarray(tagStart);
|
|
80
|
+
const key$1 = deriveGcmKey(secret, salt);
|
|
81
|
+
const decipher$1 = node_crypto.default.createDecipheriv("aes-256-gcm", key$1, iv);
|
|
82
|
+
decipher$1.setAuthTag(authTag);
|
|
83
|
+
const output$1 = decipher$1.update(Uint8Array.from(encrypted));
|
|
84
|
+
return require_encode.encode(Uint8Array.from(Buffer.concat([Uint8Array.from(output$1), Uint8Array.from(decipher$1.final())])), encoding);
|
|
85
|
+
}
|
|
86
|
+
if (options?.allowLegacyEcb === false) throw new Error("Legacy unauthenticated AES-256-ECB ciphertext rejected");
|
|
21
87
|
const keyBuffer = require_hasher_sha3.default.hash256(secret, 1, "buffer");
|
|
22
88
|
const key = Uint8Array.from(keyBuffer);
|
|
23
89
|
const decipher = node_crypto.default.createDecipheriv("aes-256-ecb", key, "");
|
|
24
90
|
decipher.setAutoPadding(true);
|
|
25
|
-
const output = decipher.update(Uint8Array.from(
|
|
91
|
+
const output = decipher.update(Uint8Array.from(data));
|
|
26
92
|
return require_encode.encode(Uint8Array.from(Buffer.concat([Uint8Array.from(output), Uint8Array.from(decipher.final())])), encoding);
|
|
27
93
|
}
|
|
28
94
|
};
|
|
@@ -30,4 +96,5 @@ var aes_default = new AesCrypter();
|
|
|
30
96
|
|
|
31
97
|
//#endregion
|
|
32
98
|
exports.AesCrypter = AesCrypter;
|
|
99
|
+
exports.AesCrypterGCM = AesCrypter;
|
|
33
100
|
exports.default = aes_default;
|
package/lib/crypter/aes.d.cts
CHANGED
|
@@ -2,10 +2,22 @@ import _default$1 from "../protocols/crypter.cjs";
|
|
|
2
2
|
import { BytesType, EncodingType } from "@ocap/util";
|
|
3
3
|
|
|
4
4
|
//#region src/crypter/aes.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Authenticated AES-256-GCM Crypter with HKDF-SHA256 salted key derivation
|
|
8
|
+
* and backward-compatible read-only decryption for legacy AES-256-ECB payloads.
|
|
9
|
+
*/
|
|
5
10
|
declare class AesCrypter extends _default$1 {
|
|
6
11
|
encrypt(message: BytesType, secret: BytesType, encoding?: EncodingType): BytesType;
|
|
7
|
-
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated Legacy deterministic AES-256-ECB encryption kept only for compatibility test vectors.
|
|
14
|
+
* Do not use for new data; use `encrypt()` (AES-256-GCM) instead.
|
|
15
|
+
*/
|
|
16
|
+
encryptLegacyEcb(message: BytesType, secret: BytesType, encoding?: EncodingType): BytesType;
|
|
17
|
+
decrypt(message: BytesType, secret: BytesType, encoding?: EncodingType, options?: {
|
|
18
|
+
allowLegacyEcb?: boolean;
|
|
19
|
+
}): BytesType;
|
|
8
20
|
}
|
|
9
21
|
declare const _default: AesCrypter;
|
|
10
22
|
//#endregion
|
|
11
|
-
export { AesCrypter, _default as default };
|
|
23
|
+
export { AesCrypter, AesCrypter as AesCrypterGCM, _default as default };
|
package/lib/encode.cjs
CHANGED
|
@@ -3,7 +3,7 @@ let _ocap_util = require("@ocap/util");
|
|
|
3
3
|
|
|
4
4
|
//#region src/encode.ts
|
|
5
5
|
function encode(data, encoding = "hex") {
|
|
6
|
-
if (["hex", "base16"].includes(encoding)) return (0, _ocap_util.toHex)(data);
|
|
6
|
+
if (["hex", "base16"].includes(encoding)) return (0, _ocap_util.toHex)((0, _ocap_util.toUint8Array)(data));
|
|
7
7
|
if (encoding === "base58") return (0, _ocap_util.toBase58)(data);
|
|
8
8
|
if (encoding === "base64") return (0, _ocap_util.toBase64)(data);
|
|
9
9
|
if (encoding === "Uint8Array") return (0, _ocap_util.toUint8Array)(data);
|