summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/internal
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2024-08-08 06:04:10 -0700
committerGitHub <noreply@github.com>2024-08-08 18:34:10 +0530
commit0d1beed2e3633d71d5e288e0382b85be361ec13d (patch)
treeed02258b19695bff1ab3ccaeafd78786406bb832 /ext/node/polyfills/internal
parent2f6da40bd609ebda8f30d748427d325d80e58274 (diff)
fix(ext/node): add `CipherIv.setAutoPadding()` (#24940)
Co-Authored-By: Luca Casonato <hello@lcas.dev> Fixes https://github.com/denoland/deno/issues/21804 Ref https://github.com/denoland/deno/issues/20924 --------- Signed-off-by: Divy Srivastava <dj.srivastava23@gmail.com> Co-authored-by: Luca Casonato <hello@lcas.dev>
Diffstat (limited to 'ext/node/polyfills/internal')
-rw-r--r--ext/node/polyfills/internal/crypto/cipher.ts32
1 files changed, 24 insertions, 8 deletions
diff --git a/ext/node/polyfills/internal/crypto/cipher.ts b/ext/node/polyfills/internal/crypto/cipher.ts
index d83d4fa8f..0a0a1ca06 100644
--- a/ext/node/polyfills/internal/crypto/cipher.ts
+++ b/ext/node/polyfills/internal/crypto/cipher.ts
@@ -17,6 +17,7 @@ import {
op_node_decipheriv_decrypt,
op_node_decipheriv_final,
op_node_decipheriv_set_aad,
+ op_node_decipheriv_take,
op_node_private_decrypt,
op_node_private_encrypt,
op_node_public_encrypt,
@@ -163,6 +164,8 @@ export class Cipheriv extends Transform implements Cipher {
#authTag?: Buffer;
+ #autoPadding = true;
+
constructor(
cipher: string,
key: CipherKey,
@@ -191,8 +194,13 @@ export class Cipheriv extends Transform implements Cipher {
final(encoding: string = getDefaultEncoding()): Buffer | string {
const buf = new Buffer(16);
+
+ if (!this.#autoPadding && this.#cache.cache.byteLength != 16) {
+ throw new Error("Invalid final block size");
+ }
const maybeTag = op_node_cipheriv_final(
this.#context,
+ this.#autoPadding,
this.#cache.cache,
buf,
);
@@ -217,8 +225,8 @@ export class Cipheriv extends Transform implements Cipher {
return this;
}
- setAutoPadding(_autoPadding?: boolean): this {
- notImplemented("crypto.Cipheriv.prototype.setAutoPadding");
+ setAutoPadding(autoPadding?: boolean): this {
+ this.#autoPadding = !!autoPadding;
return this;
}
@@ -299,6 +307,8 @@ export class Decipheriv extends Transform implements Cipher {
/** DecipherContext resource id */
#context: number;
+ #autoPadding = true;
+
/** ciphertext data cache */
#cache: BlockModeCache;
@@ -333,18 +343,23 @@ export class Decipheriv extends Transform implements Cipher {
}
final(encoding: string = getDefaultEncoding()): Buffer | string {
+ if (!this.#needsBlockCache || this.#cache.cache.byteLength === 0) {
+ op_node_decipheriv_take(this.#context);
+ return encoding === "buffer" ? Buffer.from([]) : "";
+ }
+ if (this.#cache.cache.byteLength != 16) {
+ throw new Error("Invalid final block size");
+ }
+
let buf = new Buffer(16);
op_node_decipheriv_final(
this.#context,
+ this.#autoPadding,
this.#cache.cache,
buf,
this.#authTag || NO_TAG,
);
- if (!this.#needsBlockCache) {
- return encoding === "buffer" ? Buffer.from([]) : "";
- }
-
buf = buf.subarray(0, 16 - buf.at(-1)); // Padded in Pkcs7 mode
return encoding === "buffer" ? buf : buf.toString(encoding);
}
@@ -364,8 +379,9 @@ export class Decipheriv extends Transform implements Cipher {
return this;
}
- setAutoPadding(_autoPadding?: boolean): this {
- notImplemented("crypto.Decipheriv.prototype.setAutoPadding");
+ setAutoPadding(autoPadding?: boolean): this {
+ this.#autoPadding = Boolean(autoPadding);
+ return this;
}
update(