Skip to content

Commit bb4c345

Browse files
authored
fix: some methods in node:crypto cause panic (#567)
1 parent bd73b45 commit bb4c345

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/node/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ tokio.workspace = true
5757
url.workspace = true
5858
winapi.workspace = true
5959

60-
aead-gcm-stream = "0.3"
60+
aead-gcm-stream = "0.4"
6161
blake2 = "0.10.6"
6262
const-oid = "0.9.5"
6363
digest = { version = "0.10.5", features = ["core-api", "std"] }

ext/node/ops/crypto/cipher.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use aes::cipher::block_padding::Pkcs7;
44
use aes::cipher::BlockDecryptMut;
55
use aes::cipher::BlockEncryptMut;
66
use aes::cipher::KeyIvInit;
7+
use aes::cipher::KeySizeUser;
78
use deno_core::error::type_error;
89
use deno_core::error::AnyError;
910
use deno_core::Resource;
@@ -126,7 +127,7 @@ impl Cipher {
126127
"aes-192-ecb" => Aes192Ecb(Box::new(ecb::Encryptor::new(key.into()))),
127128
"aes-256-ecb" => Aes256Ecb(Box::new(ecb::Encryptor::new(key.into()))),
128129
"aes-128-gcm" => {
129-
if iv.len() != 12 {
130+
if key.len() != aes::Aes128::key_size() {
130131
return Err(type_error("IV length must be 12 bytes"));
131132
}
132133

@@ -135,7 +136,7 @@ impl Cipher {
135136
Aes128Gcm(Box::new(cipher))
136137
}
137138
"aes-256-gcm" => {
138-
if iv.len() != 12 {
139+
if key.len() != aes::Aes256::key_size() {
139140
return Err(type_error("IV length must be 12 bytes"));
140141
}
141142

@@ -302,7 +303,7 @@ impl Decipher {
302303
"aes-192-ecb" => Aes192Ecb(Box::new(ecb::Decryptor::new(key.into()))),
303304
"aes-256-ecb" => Aes256Ecb(Box::new(ecb::Decryptor::new(key.into()))),
304305
"aes-128-gcm" => {
305-
if iv.len() != 12 {
306+
if key.len() != aes::Aes128::key_size() {
306307
return Err(type_error("IV length must be 12 bytes"));
307308
}
308309

@@ -311,7 +312,7 @@ impl Decipher {
311312
Aes128Gcm(Box::new(decipher))
312313
}
313314
"aes-256-gcm" => {
314-
if iv.len() != 12 {
315+
if key.len() != aes::Aes256::key_size() {
315316
return Err(type_error("IV length must be 12 bytes"));
316317
}
317318

ext/node/polyfills/internal/crypto/cipher.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ export function privateEncrypt(
433433
const padding = privateKey.padding || 1;
434434

435435
buffer = getArrayBufferOrView(buffer, "buffer");
436-
return op_node_private_encrypt(data, buffer, padding);
436+
return Buffer.from(op_node_private_encrypt(data, buffer, padding));
437437
}
438438

439439
export function privateDecrypt(
@@ -444,7 +444,7 @@ export function privateDecrypt(
444444
const padding = privateKey.padding || 1;
445445

446446
buffer = getArrayBufferOrView(buffer, "buffer");
447-
return op_node_private_decrypt(data, buffer, padding);
447+
return Buffer.from(op_node_private_decrypt(data, buffer, padding));
448448
}
449449

450450
export function publicEncrypt(
@@ -455,7 +455,7 @@ export function publicEncrypt(
455455
const padding = publicKey.padding || 1;
456456

457457
buffer = getArrayBufferOrView(buffer, "buffer");
458-
return op_node_public_encrypt(data, buffer, padding);
458+
return Buffer.from(op_node_public_encrypt(data, buffer, padding));
459459
}
460460

461461
export function prepareKey(key) {

0 commit comments

Comments
 (0)