Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/node): update aead-gcm-stream to 0.3 #25261

Merged
merged 1 commit into from
Aug 28, 2024
Merged
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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ext/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ path = "lib.rs"
sync_fs = ["deno_package_json/sync", "node_resolver/sync"]

[dependencies]
aead-gcm-stream = "0.1"
aead-gcm-stream = "0.3"
aes.workspace = true
async-trait.workspace = true
base64.workspace = true
Expand Down
36 changes: 24 additions & 12 deletions ext/node/ops/crypto/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,22 @@ impl Cipher {
"aes-192-ecb" => Aes192Ecb(Box::new(ecb::Encryptor::new(key.into()))),
"aes-256-ecb" => Aes256Ecb(Box::new(ecb::Encryptor::new(key.into()))),
"aes-128-gcm" => {
let mut cipher =
aead_gcm_stream::AesGcm::<aes::Aes128>::new(key.into());
cipher.init(iv.try_into()?);
if iv.len() != 12 {
return Err(type_error("IV length must be 12 bytes"));
}

let cipher =
aead_gcm_stream::AesGcm::<aes::Aes128>::new(key.into(), iv);

Aes128Gcm(Box::new(cipher))
}
"aes-256-gcm" => {
let mut cipher =
aead_gcm_stream::AesGcm::<aes::Aes256>::new(key.into());
cipher.init(iv.try_into()?);
if iv.len() != 12 {
return Err(type_error("IV length must be 12 bytes"));
}

let cipher =
aead_gcm_stream::AesGcm::<aes::Aes256>::new(key.into(), iv);

Aes256Gcm(Box::new(cipher))
}
Expand Down Expand Up @@ -320,16 +326,22 @@ impl Decipher {
"aes-192-ecb" => Aes192Ecb(Box::new(ecb::Decryptor::new(key.into()))),
"aes-256-ecb" => Aes256Ecb(Box::new(ecb::Decryptor::new(key.into()))),
"aes-128-gcm" => {
let mut decipher =
aead_gcm_stream::AesGcm::<aes::Aes128>::new(key.into());
decipher.init(iv.try_into()?);
if iv.len() != 12 {
return Err(type_error("IV length must be 12 bytes"));
}

let decipher =
aead_gcm_stream::AesGcm::<aes::Aes128>::new(key.into(), iv);

Aes128Gcm(Box::new(decipher))
}
"aes-256-gcm" => {
let mut decipher =
aead_gcm_stream::AesGcm::<aes::Aes256>::new(key.into());
decipher.init(iv.try_into()?);
if iv.len() != 12 {
return Err(type_error("IV length must be 12 bytes"));
}

let decipher =
aead_gcm_stream::AesGcm::<aes::Aes256>::new(key.into(), iv);

Aes256Gcm(Box::new(decipher))
}
Expand Down
18 changes: 18 additions & 0 deletions tests/unit_node/crypto/crypto_cipher_gcm_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,21 @@ for (
});
}
}

Deno.test({
name: "aes-128-gcm encrypt multiple",
fn() {
const key = Buffer.alloc(16);
const nonce = Buffer.alloc(12);

const gcm = crypto.createCipheriv("aes-128-gcm", key, nonce);

assertEquals(gcm.update("hello", "utf8", "hex"), "6bedb6a20f");
assertEquals(gcm.update("world", "utf8", "hex"), "c1cce09f4c");
gcm.final();
assertEquals(
gcm.getAuthTag().toString("hex"),
"bf6d20a38e0c828bea3de63b7ff1dfbd",
);
},
});