Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 35 additions & 20 deletions ext/node/ops/crypto/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,10 +758,13 @@ impl Decipher {
Ok(())
}
(Aes128Cbc(mut decryptor), false) => {
decryptor.decrypt_block_b2b_mut(
GenericArray::from_slice(input),
GenericArray::from_mut_slice(output),
);
if !input.is_empty() {
assert_block_len!(input.len(), 16);
decryptor.decrypt_block_b2b_mut(
GenericArray::from_slice(input),
GenericArray::from_mut_slice(output),
);
}
Ok(())
}
(Aes128Ecb(decryptor), true) => {
Expand All @@ -772,10 +775,13 @@ impl Decipher {
Ok(())
}
(Aes128Ecb(mut decryptor), false) => {
decryptor.decrypt_block_b2b_mut(
GenericArray::from_slice(input),
GenericArray::from_mut_slice(output),
);
if !input.is_empty() {
assert_block_len!(input.len(), 16);
decryptor.decrypt_block_b2b_mut(
GenericArray::from_slice(input),
GenericArray::from_mut_slice(output),
);
}
Ok(())
}
(Aes192Ecb(decryptor), true) => {
Expand All @@ -786,10 +792,13 @@ impl Decipher {
Ok(())
}
(Aes192Ecb(mut decryptor), false) => {
decryptor.decrypt_block_b2b_mut(
GenericArray::from_slice(input),
GenericArray::from_mut_slice(output),
);
if !input.is_empty() {
assert_block_len!(input.len(), 16);
decryptor.decrypt_block_b2b_mut(
GenericArray::from_slice(input),
GenericArray::from_mut_slice(output),
);
}
Ok(())
}
(Aes256Ecb(decryptor), true) => {
Expand All @@ -800,10 +809,13 @@ impl Decipher {
Ok(())
}
(Aes256Ecb(mut decryptor), false) => {
decryptor.decrypt_block_b2b_mut(
GenericArray::from_slice(input),
GenericArray::from_mut_slice(output),
);
if !input.is_empty() {
assert_block_len!(input.len(), 16);
decryptor.decrypt_block_b2b_mut(
GenericArray::from_slice(input),
GenericArray::from_mut_slice(output),
);
}
Ok(())
}
(Aes128Gcm(decipher, auth_tag_length), true) => {
Expand Down Expand Up @@ -848,10 +860,13 @@ impl Decipher {
Ok(())
}
(Aes256Cbc(mut decryptor), false) => {
decryptor.decrypt_block_b2b_mut(
GenericArray::from_slice(input),
GenericArray::from_mut_slice(output),
);
if !input.is_empty() {
assert_block_len!(input.len(), 16);
decryptor.decrypt_block_b2b_mut(
GenericArray::from_slice(input),
GenericArray::from_mut_slice(output),
);
}
Ok(())
}
(Aes256Ctr(mut decryptor), _) => {
Expand Down
160 changes: 160 additions & 0 deletions tests/unit_node/crypto/crypto_cipher_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,3 +654,163 @@ Deno.test({
);
},
});

// Regression test for https://github.com/denoland/deno/issues/28703
Deno.test({
name: "createDecipheriv - setAutoPadding(false) with empty final input",
fn() {
// Test aes-256-ecb (the original issue)
{
const decipher = crypto.createDecipheriv(
"aes-256-ecb",
Buffer.alloc(32),
"",
);
decipher.setAutoPadding(false);
decipher.end(Buffer.alloc(16));
}

// Test aes-128-ecb
{
const decipher = crypto.createDecipheriv(
"aes-128-ecb",
Buffer.alloc(16),
"",
);
decipher.setAutoPadding(false);
decipher.end(Buffer.alloc(16));
}

// Test aes-192-ecb
{
const decipher = crypto.createDecipheriv(
"aes-192-ecb",
Buffer.alloc(24),
"",
);
decipher.setAutoPadding(false);
decipher.end(Buffer.alloc(16));
}

// Test aes-128-cbc
{
const decipher = crypto.createDecipheriv(
"aes-128-cbc",
Buffer.alloc(16),
Buffer.alloc(16),
);
decipher.setAutoPadding(false);
decipher.end(Buffer.alloc(16));
}

// Test aes-256-cbc
{
const decipher = crypto.createDecipheriv(
"aes-256-cbc",
Buffer.alloc(32),
Buffer.alloc(16),
);
decipher.setAutoPadding(false);
decipher.end(Buffer.alloc(16));
}
},
});

Deno.test({
name:
"createDecipheriv - setAutoPadding(false) with invalid block length should error",
fn() {
// Invalid block length (10 bytes instead of 16) should throw an error, not panic
// Test all affected cipher modes

// aes-256-ecb
{
const decipher = crypto.createDecipheriv(
"aes-256-ecb",
Buffer.alloc(32),
"",
);
decipher.setAutoPadding(false);
decipher.update(Buffer.alloc(10));
assertThrows(
() => {
decipher.final();
},
RangeError,
"wrong final block length",
);
}

// aes-128-ecb
{
const decipher = crypto.createDecipheriv(
"aes-128-ecb",
Buffer.alloc(16),
"",
);
decipher.setAutoPadding(false);
decipher.update(Buffer.alloc(10));
assertThrows(
() => {
decipher.final();
},
RangeError,
"wrong final block length",
);
}

// aes-192-ecb
{
const decipher = crypto.createDecipheriv(
"aes-192-ecb",
Buffer.alloc(24),
"",
);
decipher.setAutoPadding(false);
decipher.update(Buffer.alloc(10));
assertThrows(
() => {
decipher.final();
},
RangeError,
"wrong final block length",
);
}

// aes-128-cbc
{
const decipher = crypto.createDecipheriv(
"aes-128-cbc",
Buffer.alloc(16),
Buffer.alloc(16),
);
decipher.setAutoPadding(false);
decipher.update(Buffer.alloc(10));
assertThrows(
() => {
decipher.final();
},
RangeError,
"wrong final block length",
);
}

// aes-256-cbc
{
const decipher = crypto.createDecipheriv(
"aes-256-cbc",
Buffer.alloc(32),
Buffer.alloc(16),
);
decipher.setAutoPadding(false);
decipher.update(Buffer.alloc(10));
assertThrows(
() => {
decipher.final();
},
RangeError,
"wrong final block length",
);
}
},
});