@volter/tabnode 0.5.49 → 0.5.51
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/dist/__sw__.js +17 -6
- package/dist/assets/{runtime-worker-C1IV4qnr.js → runtime-worker-Cu2mr9G9.js} +1076 -67
- package/dist/index.mjs +1083 -81
- package/dist/{port-bridge-CixAESwA.js → port-bridge-Cergvxli.js} +22 -18
- package/dist/port-bridge.d.ts +5 -0
- package/dist/port-bridge.mjs +1 -1
- package/dist/shims/crypto-cipher.d.ts +203 -0
- package/dist/shims/crypto.d.ts +465 -1
- package/package.json +2 -1
- package/src/port-bridge.ts +21 -15
- package/src/shims/crypto-cipher.ts +354 -0
- package/src/shims/crypto.ts +27 -2
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node's `createCipheriv` and `createDecipheriv` for AES in GCM, CBC, CTR and
|
|
3
|
+
* ECB modes. A guest calls them synchronously and WebCrypto is async-only, so
|
|
4
|
+
* the modes are built here over one synchronous AES block (`@noble/ciphers`,
|
|
5
|
+
* pinned exactly: its single-block helpers are the library's unsafe surface)
|
|
6
|
+
* and GCM's GHASH. Every mode streams as OpenSSL does: `update` answers the
|
|
7
|
+
* bytes it can, `final` the rest, so a caller that reads `update` alone for a
|
|
8
|
+
* stream mode gets its ciphertext (jose's GCM encrypt: `const ciphertext =
|
|
9
|
+
* cipher.update(plaintext); cipher.final();`, which is how NextAuth seals its
|
|
10
|
+
* session cookie).
|
|
11
|
+
*
|
|
12
|
+
* Not here: the ciphers are not `stream.Transform`s (only `update`/`final`),
|
|
13
|
+
* and no mode but these four, nor ChaCha20-Poly1305, is offered.
|
|
14
|
+
*/
|
|
15
|
+
import { unsafe } from '@noble/ciphers/aes.js';
|
|
16
|
+
import { GHASH } from '@noble/ciphers/_polyval.js';
|
|
17
|
+
|
|
18
|
+
type Mode = 'gcm' | 'cbc' | 'ctr' | 'ecb';
|
|
19
|
+
type Bytes = Uint8Array;
|
|
20
|
+
|
|
21
|
+
export interface CipherDeps {
|
|
22
|
+
/** The guest's Buffer.from over bytes. */
|
|
23
|
+
toBuffer(bytes: Bytes): Uint8Array;
|
|
24
|
+
/** Bytes of a string (in its encoding) or a view, as the rest of crypto reads them. */
|
|
25
|
+
bytesOf(value: unknown, encoding?: string): Bytes;
|
|
26
|
+
/** A secret KeyObject's bytes, or undefined when the value is not a KeyObject. */
|
|
27
|
+
secretKeyBytes(value: unknown): Bytes | undefined;
|
|
28
|
+
/** Bytes in an output encoding, as `digest` answers them. */
|
|
29
|
+
encode(bytes: Bytes, encoding?: string): unknown;
|
|
30
|
+
error(code: string, message: string): Error;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const CIPHERS = /^aes-(128|192|256)-(gcm|cbc|ctr|ecb)$/u;
|
|
34
|
+
|
|
35
|
+
/** The ciphers `createCipheriv` accepts, as `getCiphers` lists them. */
|
|
36
|
+
export function cipherNames(): string[] {
|
|
37
|
+
const names: string[] = [];
|
|
38
|
+
for (const bits of [128, 192, 256]) for (const mode of ['cbc', 'ctr', 'ecb', 'gcm']) names.push(`aes-${bits}-${mode}`);
|
|
39
|
+
return names;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const GCM_TAG_LENGTHS = new Set([4, 8, 12, 13, 14, 15, 16]);
|
|
43
|
+
|
|
44
|
+
export function createCipherClasses(deps: CipherDeps) {
|
|
45
|
+
const { error } = deps;
|
|
46
|
+
|
|
47
|
+
function parse(algorithm: unknown): { keyLength: number; mode: Mode } {
|
|
48
|
+
if (typeof algorithm !== 'string') throw Object.assign(new TypeError('The "cipher" argument must be of type string.'), { code: 'ERR_INVALID_ARG_TYPE' });
|
|
49
|
+
const match = CIPHERS.exec(algorithm.toLowerCase());
|
|
50
|
+
if (!match) throw error('ERR_CRYPTO_UNKNOWN_CIPHER', 'Unknown cipher');
|
|
51
|
+
return { keyLength: Number(match[1]) / 8, mode: match[2] as Mode };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function keyOf(key: unknown, keyLength: number): Bytes {
|
|
55
|
+
const bytes = deps.secretKeyBytes(key) ?? deps.bytesOf(key);
|
|
56
|
+
if (bytes.length !== keyLength) throw Object.assign(new RangeError('Invalid key length'), { code: 'ERR_CRYPTO_INVALID_KEYLEN' });
|
|
57
|
+
return new Uint8Array(bytes);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function ivOf(iv: unknown, mode: Mode): Bytes | undefined {
|
|
61
|
+
if (mode === 'ecb') {
|
|
62
|
+
if (iv === null || iv === undefined) return undefined;
|
|
63
|
+
const bytes = deps.bytesOf(iv);
|
|
64
|
+
if (bytes.length !== 0) throw Object.assign(new TypeError('Invalid initialization vector'), { code: 'ERR_CRYPTO_INVALID_IV' });
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
if (iv === null || iv === undefined) throw Object.assign(new TypeError('Invalid initialization vector'), { code: 'ERR_CRYPTO_INVALID_IV' });
|
|
68
|
+
const bytes = new Uint8Array(deps.bytesOf(iv));
|
|
69
|
+
if (mode === 'gcm' ? bytes.length === 0 : bytes.length !== 16) throw Object.assign(new TypeError('Invalid initialization vector'), { code: 'ERR_CRYPTO_INVALID_IV' });
|
|
70
|
+
return bytes;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** One AES block, encrypted in place in a fresh aligned array. */
|
|
74
|
+
function encryptBlock(xk: Uint32Array, block: Bytes): Bytes {
|
|
75
|
+
const out = new Uint8Array(16);
|
|
76
|
+
out.set(block);
|
|
77
|
+
return unsafe.encryptBlock(xk, out) as Bytes;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function decryptBlock(xk: Uint32Array, block: Bytes): Bytes {
|
|
81
|
+
const out = new Uint8Array(16);
|
|
82
|
+
out.set(block);
|
|
83
|
+
return unsafe.decryptBlock(xk, out) as Bytes;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function concat(a: Bytes, b: Bytes): Bytes {
|
|
87
|
+
if (a.length === 0) return b;
|
|
88
|
+
if (b.length === 0) return a;
|
|
89
|
+
const out = new Uint8Array(a.length + b.length);
|
|
90
|
+
out.set(a);
|
|
91
|
+
out.set(b, a.length);
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** A 128-bit big-endian counter, or its low 32 bits alone (GCM's inc32). */
|
|
96
|
+
function increment(counter: Bytes, from: number): void {
|
|
97
|
+
for (let i = 15; i >= from; i--) {
|
|
98
|
+
counter[i] = (counter[i]! + 1) & 0xff;
|
|
99
|
+
if (counter[i] !== 0) return;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// The subclasses' doors to the tag, which the base keeps private (as KeyObject's keyInfoOf reads its key).
|
|
104
|
+
let authTagOf: (cipher: CipherBase) => Bytes;
|
|
105
|
+
let expectTagOf: (cipher: CipherBase, tag: unknown, encoding?: string) => void;
|
|
106
|
+
|
|
107
|
+
class CipherBase {
|
|
108
|
+
static {
|
|
109
|
+
authTagOf = (cipher) => cipher.#getTag();
|
|
110
|
+
expectTagOf = (cipher, tag, encoding) => cipher.#setTag(tag, encoding);
|
|
111
|
+
}
|
|
112
|
+
readonly #decrypt: boolean;
|
|
113
|
+
readonly #mode: Mode;
|
|
114
|
+
readonly #xk: Uint32Array;
|
|
115
|
+
readonly #xkDec: Uint32Array | undefined;
|
|
116
|
+
#padding = true;
|
|
117
|
+
#finished = false;
|
|
118
|
+
#started = false;
|
|
119
|
+
/** CBC/ECB: bytes not yet a whole block (and, decrypting with padding, the last whole block). */
|
|
120
|
+
#pending: Bytes = new Uint8Array(0);
|
|
121
|
+
/** CBC's chaining block. */
|
|
122
|
+
#chain: Bytes | undefined;
|
|
123
|
+
/** CTR/GCM: the next counter block and what is left of the current keystream block. */
|
|
124
|
+
#counter: Bytes | undefined;
|
|
125
|
+
#keystream: Bytes = new Uint8Array(0);
|
|
126
|
+
/** GCM. */
|
|
127
|
+
#ghash: GHASH | undefined;
|
|
128
|
+
#tagMask: Bytes | undefined;
|
|
129
|
+
#ghashPending: Bytes = new Uint8Array(0);
|
|
130
|
+
#aadLength = 0;
|
|
131
|
+
#dataLength = 0;
|
|
132
|
+
#authTagLength: number | undefined;
|
|
133
|
+
#authTag: Bytes | undefined;
|
|
134
|
+
|
|
135
|
+
constructor(decrypt: boolean, algorithm: unknown, key: unknown, iv: unknown, options?: { authTagLength?: number }) {
|
|
136
|
+
const { keyLength, mode } = parse(algorithm);
|
|
137
|
+
this.#decrypt = decrypt;
|
|
138
|
+
this.#mode = mode;
|
|
139
|
+
const keyBytes = keyOf(key, keyLength);
|
|
140
|
+
const ivBytes = ivOf(iv, mode);
|
|
141
|
+
this.#xk = unsafe.expandKeyLE(keyBytes) as Uint32Array;
|
|
142
|
+
this.#xkDec = decrypt && (mode === 'cbc' || mode === 'ecb') ? unsafe.expandKeyDecLE(keyBytes) as Uint32Array : undefined;
|
|
143
|
+
const tagLength = options?.authTagLength;
|
|
144
|
+
if (tagLength !== undefined) {
|
|
145
|
+
if (mode !== 'gcm') throw Object.assign(new TypeError(`Invalid authentication tag length: ${tagLength}`), { code: 'ERR_CRYPTO_INVALID_AUTH_TAG' });
|
|
146
|
+
if (!GCM_TAG_LENGTHS.has(tagLength)) throw Object.assign(new TypeError(`Invalid authentication tag length: ${tagLength}`), { code: 'ERR_CRYPTO_INVALID_AUTH_TAG' });
|
|
147
|
+
this.#authTagLength = tagLength;
|
|
148
|
+
}
|
|
149
|
+
if (mode === 'cbc') this.#chain = ivBytes;
|
|
150
|
+
if (mode === 'ctr') this.#counter = new Uint8Array(ivBytes!);
|
|
151
|
+
if (mode === 'gcm') {
|
|
152
|
+
// NIST SP 800-38D: H = E_K(0), J0 from a 96-bit IV or GHASH of any other, the tag mask E_K(J0), and the
|
|
153
|
+
// payload counted from inc32(J0).
|
|
154
|
+
const h = encryptBlock(this.#xk, new Uint8Array(16));
|
|
155
|
+
let j0: Bytes;
|
|
156
|
+
if (ivBytes!.length === 12) {
|
|
157
|
+
j0 = new Uint8Array(16);
|
|
158
|
+
j0.set(ivBytes!);
|
|
159
|
+
j0[15] = 1;
|
|
160
|
+
} else {
|
|
161
|
+
const lengths = new Uint8Array(16);
|
|
162
|
+
new DataView(lengths.buffer).setBigUint64(8, BigInt(ivBytes!.length * 8), false);
|
|
163
|
+
const g = new GHASH(h);
|
|
164
|
+
g.update(ivBytes!).update(lengths);
|
|
165
|
+
j0 = g.digest() as Bytes;
|
|
166
|
+
}
|
|
167
|
+
this.#tagMask = encryptBlock(this.#xk, j0);
|
|
168
|
+
this.#counter = new Uint8Array(j0);
|
|
169
|
+
increment(this.#counter, 12);
|
|
170
|
+
this.#ghash = new GHASH(h);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
setAutoPadding(autoPadding = true): this {
|
|
175
|
+
if (this.#finished) throw error('ERR_CRYPTO_INVALID_STATE', 'Invalid state for operation setAutoPadding');
|
|
176
|
+
this.#padding = Boolean(autoPadding);
|
|
177
|
+
return this;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
setAAD(buffer: unknown, _options?: { plaintextLength?: number }): this {
|
|
181
|
+
if (this.#mode !== 'gcm' || this.#started || this.#finished) throw error('ERR_CRYPTO_INVALID_STATE', 'Invalid state for operation setAAD');
|
|
182
|
+
const aad = deps.bytesOf(buffer);
|
|
183
|
+
// GHASH pads each update to a block; AAD is padded once, as GCM pads it, whatever its length.
|
|
184
|
+
this.#ghash!.update(aad);
|
|
185
|
+
this.#aadLength += aad.length;
|
|
186
|
+
return this;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
#getTag(): Bytes {
|
|
190
|
+
if (this.#mode !== 'gcm' || this.#decrypt || !this.#finished || !this.#authTag) throw error('ERR_CRYPTO_INVALID_STATE', 'Invalid state for operation getAuthTag');
|
|
191
|
+
return this.#authTag;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
#setTag(tag: unknown, encoding?: string): void {
|
|
195
|
+
if (this.#mode !== 'gcm' || !this.#decrypt || this.#finished) throw error('ERR_CRYPTO_INVALID_STATE', 'Invalid state for operation setAuthTag');
|
|
196
|
+
const bytes = new Uint8Array(typeof tag === 'string' ? deps.bytesOf(tag, encoding) : deps.bytesOf(tag));
|
|
197
|
+
const allowed = this.#authTagLength === undefined ? GCM_TAG_LENGTHS.has(bytes.length) : bytes.length === this.#authTagLength;
|
|
198
|
+
if (!allowed) throw Object.assign(new TypeError(`Invalid authentication tag length: ${bytes.length}`), { code: 'ERR_CRYPTO_INVALID_AUTH_TAG' });
|
|
199
|
+
this.#authTag = bytes;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
update(data: unknown, inputEncoding?: string, outputEncoding?: string): unknown {
|
|
203
|
+
if (this.#finished) throw error('ERR_CRYPTO_INVALID_STATE', 'Invalid state for operation update');
|
|
204
|
+
this.#started = true;
|
|
205
|
+
const input = typeof data === 'string' ? deps.bytesOf(data, inputEncoding) : deps.bytesOf(data);
|
|
206
|
+
return deps.encode(this.#process(new Uint8Array(input)), outputEncoding);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
final(outputEncoding?: string): unknown {
|
|
210
|
+
if (this.#finished) throw error('ERR_CRYPTO_INVALID_STATE', 'Unsupported state');
|
|
211
|
+
this.#finished = true;
|
|
212
|
+
return deps.encode(this.#finish(), outputEncoding);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
#process(input: Bytes): Bytes {
|
|
216
|
+
switch (this.#mode) {
|
|
217
|
+
case 'ctr': return this.#stream(input);
|
|
218
|
+
case 'gcm': {
|
|
219
|
+
if (this.#decrypt) this.#absorb(input);
|
|
220
|
+
const out = this.#stream(input);
|
|
221
|
+
if (!this.#decrypt) this.#absorb(out);
|
|
222
|
+
this.#dataLength += input.length;
|
|
223
|
+
return out;
|
|
224
|
+
}
|
|
225
|
+
default: return this.#blocks(input);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** XOR with the counter's keystream, carrying a partial block across calls. */
|
|
230
|
+
#stream(input: Bytes): Bytes {
|
|
231
|
+
const out = new Uint8Array(input.length);
|
|
232
|
+
let at = 0;
|
|
233
|
+
while (at < input.length) {
|
|
234
|
+
if (this.#keystream.length === 0) {
|
|
235
|
+
this.#keystream = encryptBlock(this.#xk, this.#counter!);
|
|
236
|
+
increment(this.#counter!, this.#mode === 'gcm' ? 12 : 0);
|
|
237
|
+
}
|
|
238
|
+
const take = Math.min(this.#keystream.length, input.length - at);
|
|
239
|
+
for (let i = 0; i < take; i++) out[at + i] = input[at + i]! ^ this.#keystream[i]!;
|
|
240
|
+
this.#keystream = this.#keystream.subarray(take);
|
|
241
|
+
at += take;
|
|
242
|
+
}
|
|
243
|
+
return out;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** GHASH over the ciphertext, in whole blocks: GHASH pads each update, so a partial block waits for the next. */
|
|
247
|
+
#absorb(ciphertext: Bytes): void {
|
|
248
|
+
const all = concat(this.#ghashPending, ciphertext);
|
|
249
|
+
const whole = all.length - (all.length % 16);
|
|
250
|
+
if (whole > 0) this.#ghash!.update(all.subarray(0, whole));
|
|
251
|
+
this.#ghashPending = all.slice(whole);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** CBC and ECB: every whole block now; decrypting with padding, the last whole block waits for `final`. */
|
|
255
|
+
#blocks(input: Bytes): Bytes {
|
|
256
|
+
const all = concat(this.#pending, input);
|
|
257
|
+
let whole = all.length - (all.length % 16);
|
|
258
|
+
if (this.#decrypt && this.#padding && whole === all.length && whole > 0) whole -= 16;
|
|
259
|
+
const out = new Uint8Array(whole);
|
|
260
|
+
for (let at = 0; at < whole; at += 16) out.set(this.#block(all.subarray(at, at + 16)), at);
|
|
261
|
+
this.#pending = all.slice(whole);
|
|
262
|
+
return out;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
#block(block: Bytes): Bytes {
|
|
266
|
+
if (this.#mode === 'ecb') return this.#decrypt ? decryptBlock(this.#xkDec!, block) : encryptBlock(this.#xk, block);
|
|
267
|
+
if (this.#decrypt) {
|
|
268
|
+
const plain = decryptBlock(this.#xkDec!, block);
|
|
269
|
+
for (let i = 0; i < 16; i++) plain[i] = plain[i]! ^ this.#chain![i]!;
|
|
270
|
+
this.#chain = new Uint8Array(block);
|
|
271
|
+
return plain;
|
|
272
|
+
}
|
|
273
|
+
const mixed = new Uint8Array(16);
|
|
274
|
+
for (let i = 0; i < 16; i++) mixed[i] = block[i]! ^ this.#chain![i]!;
|
|
275
|
+
const cipher = encryptBlock(this.#xk, mixed);
|
|
276
|
+
this.#chain = cipher;
|
|
277
|
+
return cipher;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
#finish(): Bytes {
|
|
281
|
+
if (this.#mode === 'ctr') return new Uint8Array(0);
|
|
282
|
+
if (this.#mode === 'gcm') return this.#finishGcm();
|
|
283
|
+
const rest = this.#pending;
|
|
284
|
+
this.#pending = new Uint8Array(0);
|
|
285
|
+
if (!this.#decrypt) {
|
|
286
|
+
if (!this.#padding) {
|
|
287
|
+
if (rest.length !== 0) throw error('ERR_OSSL_WRONG_FINAL_BLOCK_LENGTH', 'error:1C80006B:Provider routines::wrong final block length');
|
|
288
|
+
return rest;
|
|
289
|
+
}
|
|
290
|
+
const pad = 16 - rest.length;
|
|
291
|
+
const block = new Uint8Array(16);
|
|
292
|
+
block.set(rest);
|
|
293
|
+
block.fill(pad, rest.length);
|
|
294
|
+
return this.#block(block);
|
|
295
|
+
}
|
|
296
|
+
if (!this.#padding) {
|
|
297
|
+
if (rest.length !== 0) throw error('ERR_OSSL_WRONG_FINAL_BLOCK_LENGTH', 'error:1C80006B:Provider routines::wrong final block length');
|
|
298
|
+
return rest;
|
|
299
|
+
}
|
|
300
|
+
if (rest.length !== 16) throw error('ERR_OSSL_WRONG_FINAL_BLOCK_LENGTH', 'error:1C80006B:Provider routines::wrong final block length');
|
|
301
|
+
const plain = this.#block(rest);
|
|
302
|
+
const pad = plain[15]!;
|
|
303
|
+
let valid = pad >= 1 && pad <= 16;
|
|
304
|
+
for (let i = 16 - pad; valid && i < 16; i++) valid = plain[i] === pad;
|
|
305
|
+
if (!valid) throw error('ERR_OSSL_BAD_DECRYPT', 'error:1C800064:Provider routines::bad decrypt');
|
|
306
|
+
return plain.slice(0, 16 - pad);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
#finishGcm(): Bytes {
|
|
310
|
+
if (this.#ghashPending.length) this.#ghash!.update(this.#ghashPending);
|
|
311
|
+
const lengths = new Uint8Array(16);
|
|
312
|
+
const view = new DataView(lengths.buffer);
|
|
313
|
+
view.setBigUint64(0, BigInt(this.#aadLength * 8), false);
|
|
314
|
+
view.setBigUint64(8, BigInt(this.#dataLength * 8), false);
|
|
315
|
+
const full = this.#ghash!.update(lengths).digest() as Bytes;
|
|
316
|
+
for (let i = 0; i < 16; i++) full[i] = full[i]! ^ this.#tagMask![i]!;
|
|
317
|
+
if (!this.#decrypt) {
|
|
318
|
+
this.#authTag = full.slice(0, this.#authTagLength ?? 16);
|
|
319
|
+
return new Uint8Array(0);
|
|
320
|
+
}
|
|
321
|
+
const expected = this.#authTag;
|
|
322
|
+
let same = expected !== undefined;
|
|
323
|
+
if (expected) for (let i = 0; i < expected.length; i++) same = same && full[i] === expected[i];
|
|
324
|
+
if (!same) throw new Error('Unsupported state or unable to authenticate data');
|
|
325
|
+
return new Uint8Array(0);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
class Cipheriv extends CipherBase {
|
|
330
|
+
constructor(algorithm: unknown, key: unknown, iv: unknown, options?: { authTagLength?: number }) {
|
|
331
|
+
super(false, algorithm, key, iv, options);
|
|
332
|
+
}
|
|
333
|
+
getAuthTag(): Uint8Array {
|
|
334
|
+
return deps.toBuffer(authTagOf(this));
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
class Decipheriv extends CipherBase {
|
|
339
|
+
constructor(algorithm: unknown, key: unknown, iv: unknown, options?: { authTagLength?: number }) {
|
|
340
|
+
super(true, algorithm, key, iv, options);
|
|
341
|
+
}
|
|
342
|
+
setAuthTag(tag: unknown, encoding?: string): this {
|
|
343
|
+
expectTagOf(this, tag, encoding);
|
|
344
|
+
return this;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return {
|
|
349
|
+
Cipheriv,
|
|
350
|
+
Decipheriv,
|
|
351
|
+
createCipheriv: (algorithm: unknown, key: unknown, iv: unknown, options?: { authTagLength?: number }) => new Cipheriv(algorithm, key, iv, options),
|
|
352
|
+
createDecipheriv: (algorithm: unknown, key: unknown, iv: unknown, options?: { authTagLength?: number }) => new Decipheriv(algorithm, key, iv, options),
|
|
353
|
+
};
|
|
354
|
+
}
|
package/src/shims/crypto.ts
CHANGED
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
validateNumber,
|
|
22
22
|
} from '../node-internals';
|
|
23
23
|
import { cryptoConstants as constants } from './crypto-constants';
|
|
24
|
+
import { cipherNames, createCipherClasses } from './crypto-cipher';
|
|
24
25
|
export { constants };
|
|
25
26
|
|
|
26
27
|
interface DigestState {
|
|
@@ -911,7 +912,8 @@ function timingSafeEqual(a: Buffer | Uint8Array, b: Buffer | Uint8Array): boolea
|
|
|
911
912
|
}
|
|
912
913
|
|
|
913
914
|
function getCiphers(): string[] {
|
|
914
|
-
|
|
915
|
+
// What createCipheriv accepts, no more, as getHashes is createHash's.
|
|
916
|
+
return cipherNames();
|
|
915
917
|
}
|
|
916
918
|
|
|
917
919
|
function getHashes(): string[] {
|
|
@@ -1133,11 +1135,34 @@ async function importKey(
|
|
|
1133
1135
|
throw new Error(`Unsupported key format: ${keyInfo.format}`);
|
|
1134
1136
|
}
|
|
1135
1137
|
|
|
1138
|
+
// ============================================================================
|
|
1139
|
+
// Ciphers
|
|
1140
|
+
// ============================================================================
|
|
1141
|
+
|
|
1142
|
+
const { Cipheriv, Decipheriv, createCipheriv, createDecipheriv } = createCipherClasses({
|
|
1143
|
+
toBuffer: (bytes) => Buffer.from(bytes),
|
|
1144
|
+
bytesOf: (value, encoding) => cryptoBytes(value, encoding, true),
|
|
1145
|
+
secretKeyBytes: (value) => {
|
|
1146
|
+
if (!(value instanceof KeyObject)) return undefined;
|
|
1147
|
+
const info = keyInfoOf(value);
|
|
1148
|
+
if (info.type !== 'secret' || !(info.keyData instanceof Uint8Array)) {
|
|
1149
|
+
throw Object.assign(new TypeError(`Invalid key object type ${info.type}, expected secret.`), { code: 'ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE' });
|
|
1150
|
+
}
|
|
1151
|
+
return info.keyData;
|
|
1152
|
+
},
|
|
1153
|
+
encode: (bytes, encoding) => digestResult(bytes, encoding),
|
|
1154
|
+
error: cryptoError,
|
|
1155
|
+
});
|
|
1156
|
+
|
|
1136
1157
|
// ============================================================================
|
|
1137
1158
|
// Exports
|
|
1138
1159
|
// ============================================================================
|
|
1139
1160
|
|
|
1140
1161
|
return {
|
|
1162
|
+
Cipheriv,
|
|
1163
|
+
Decipheriv,
|
|
1164
|
+
createCipheriv,
|
|
1165
|
+
createDecipheriv,
|
|
1141
1166
|
unsupported,
|
|
1142
1167
|
randomBytes,
|
|
1143
1168
|
randomFill,
|
|
@@ -1167,6 +1192,6 @@ return {
|
|
|
1167
1192
|
|
|
1168
1193
|
}
|
|
1169
1194
|
const cryptoModule = createCryptoModule();
|
|
1170
|
-
export const { randomBytes, randomFillSync, randomFill, randomUUID, randomInt, getRandomValues, unsupported, createHash, createHmac, hash, pbkdf2Sync, pbkdf2, sign, verify, createSign, createVerify, KeyObject, createSecretKey, createPublicKey, createPrivateKey, generateKeyPair, timingSafeEqual, getCiphers, getHashes } = cryptoModule;
|
|
1195
|
+
export const { Cipheriv, Decipheriv, createCipheriv, createDecipheriv, randomBytes, randomFillSync, randomFill, randomUUID, randomInt, getRandomValues, unsupported, createHash, createHmac, hash, pbkdf2Sync, pbkdf2, sign, verify, createSign, createVerify, KeyObject, createSecretKey, createPublicKey, createPrivateKey, generateKeyPair, timingSafeEqual, getCiphers, getHashes } = cryptoModule;
|
|
1171
1196
|
export type KeyObject = InstanceType<typeof KeyObject>;
|
|
1172
1197
|
export default cryptoModule;
|