From 0d1beed2e3633d71d5e288e0382b85be361ec13d Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Thu, 8 Aug 2024 06:04:10 -0700 Subject: fix(ext/node): add `CipherIv.setAutoPadding()` (#24940) Co-Authored-By: Luca Casonato Fixes https://github.com/denoland/deno/issues/21804 Ref https://github.com/denoland/deno/issues/20924 --------- Signed-off-by: Divy Srivastava Co-authored-by: Luca Casonato --- ext/node/polyfills/internal/crypto/cipher.ts | 32 +++++++++++++++++++++------- 1 file changed, 24 insertions(+), 8 deletions(-) (limited to 'ext/node/polyfills') 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( -- cgit v1.2.3