1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 04:52:26 -05:00

Compare commits

...

5 commits

Author SHA1 Message Date
Nathan Whitaker
a1a519a4f4
Merge 917e52c882 into e4a16e91fa 2025-01-20 11:03:12 +08:00
ryu
e4a16e91fa
docs(readme): update redirected links (#27726) 2025-01-20 03:01:25 +00:00
Yoshiya Hinosawa
917e52c882
Merge branch 'main' into aes-ctr 2025-01-13 18:25:18 +09:00
Nathan Whitaker
c09deecac1 fmt 2025-01-10 19:01:01 -08:00
Nathan Whitaker
a4a44e2ad0 implement aes-128-ctr and aes-256-ctr 2025-01-10 18:51:18 -08:00
8 changed files with 62 additions and 7 deletions

1
Cargo.lock generated
View file

@ -2032,6 +2032,7 @@ dependencies = [
"bytes", "bytes",
"cbc", "cbc",
"const-oid", "const-oid",
"ctr",
"data-encoding", "data-encoding",
"deno_core", "deno_core",
"deno_error", "deno_error",

View file

@ -122,6 +122,7 @@ cbc = { version = "=0.1.2", features = ["alloc"] }
chrono = { version = "0.4", default-features = false, features = ["std", "serde"] } chrono = { version = "0.4", default-features = false, features = ["std", "serde"] }
color-print = "0.3.5" color-print = "0.3.5"
console_static_text = "=0.8.1" console_static_text = "=0.8.1"
ctr = { version = "0.9.2", features = ["alloc"] }
dashmap = "5.5.3" dashmap = "5.5.3"
data-encoding = "2.3.3" data-encoding = "2.3.3"
data-url = "=0.3.1" data-url = "=0.3.1"

View file

@ -6,8 +6,8 @@
<img align="right" src="https://deno.land/logo.svg" height="150px" alt="the deno mascot dinosaur standing in the rain"> <img align="right" src="https://deno.land/logo.svg" height="150px" alt="the deno mascot dinosaur standing in the rain">
[Deno](https://www.deno.com) [Deno](https://deno.com)
([/ˈdiːnoʊ/](http://ipa-reader.xyz/?text=%CB%88di%CB%90no%CA%8A), pronounced ([/ˈdiːnoʊ/](https://ipa-reader.com/?text=%CB%88di%CB%90no%CA%8A), pronounced
`dee-no`) is a JavaScript, TypeScript, and WebAssembly runtime with secure `dee-no`) is a JavaScript, TypeScript, and WebAssembly runtime with secure
defaults and a great developer experience. It's built on [V8](https://v8.dev/), defaults and a great developer experience. It's built on [V8](https://v8.dev/),
[Rust](https://www.rust-lang.org/), and [Tokio](https://tokio.rs/). [Rust](https://www.rust-lang.org/), and [Tokio](https://tokio.rs/).

View file

@ -20,7 +20,7 @@ aes-kw = { version = "0.2.1", features = ["alloc"] }
base64.workspace = true base64.workspace = true
cbc.workspace = true cbc.workspace = true
const-oid = "0.9.0" const-oid = "0.9.0"
ctr = "0.9.1" ctr.workspace = true
curve25519-dalek = "4.1.3" curve25519-dalek = "4.1.3"
deno_core.workspace = true deno_core.workspace = true
deno_error.workspace = true deno_error.workspace = true

View file

@ -27,6 +27,7 @@ brotli.workspace = true
bytes.workspace = true bytes.workspace = true
cbc.workspace = true cbc.workspace = true
const-oid = "0.9.5" const-oid = "0.9.5"
ctr.workspace = true
data-encoding.workspace = true data-encoding.workspace = true
deno_core.workspace = true deno_core.workspace = true
deno_error.workspace = true deno_error.workspace = true

View file

@ -8,6 +8,7 @@ use aes::cipher::block_padding::Pkcs7;
use aes::cipher::BlockDecryptMut; use aes::cipher::BlockDecryptMut;
use aes::cipher::BlockEncryptMut; use aes::cipher::BlockEncryptMut;
use aes::cipher::KeyIvInit; use aes::cipher::KeyIvInit;
use aes::cipher::StreamCipher;
use deno_core::Resource; use deno_core::Resource;
use digest::generic_array::GenericArray; use digest::generic_array::GenericArray;
use digest::KeyInit; use digest::KeyInit;
@ -25,6 +26,8 @@ enum Cipher {
Aes128Gcm(Box<Aes128Gcm>), Aes128Gcm(Box<Aes128Gcm>),
Aes256Gcm(Box<Aes256Gcm>), Aes256Gcm(Box<Aes256Gcm>),
Aes256Cbc(Box<cbc::Encryptor<aes::Aes256>>), Aes256Cbc(Box<cbc::Encryptor<aes::Aes256>>),
Aes128Ctr(Box<ctr::Ctr128BE<aes::Aes128>>),
Aes256Ctr(Box<ctr::Ctr128BE<aes::Aes256>>),
// TODO(kt3k): add more algorithms Aes192Cbc, etc. // TODO(kt3k): add more algorithms Aes192Cbc, etc.
} }
@ -36,6 +39,8 @@ enum Decipher {
Aes128Gcm(Box<Aes128Gcm>), Aes128Gcm(Box<Aes128Gcm>),
Aes256Gcm(Box<Aes256Gcm>), Aes256Gcm(Box<Aes256Gcm>),
Aes256Cbc(Box<cbc::Decryptor<aes::Aes256>>), Aes256Cbc(Box<cbc::Decryptor<aes::Aes256>>),
Aes256Ctr(Box<ctr::Ctr128BE<aes::Aes256>>),
Aes128Ctr(Box<ctr::Ctr128BE<aes::Aes128>>),
// TODO(kt3k): add more algorithms Aes192Cbc, Aes128GCM, etc. // TODO(kt3k): add more algorithms Aes192Cbc, Aes128GCM, etc.
} }
@ -211,6 +216,24 @@ impl Cipher {
Aes256Cbc(Box::new(cbc::Encryptor::new(key.into(), iv.into()))) Aes256Cbc(Box::new(cbc::Encryptor::new(key.into(), iv.into())))
} }
"aes-256-ctr" => {
if key.len() != 32 {
return Err(CipherError::InvalidKeyLength);
}
if iv.len() != 16 {
return Err(CipherError::InvalidInitializationVector);
}
Aes256Ctr(Box::new(ctr::Ctr128BE::new(key.into(), iv.into())))
}
"aes-128-ctr" => {
if key.len() != 16 {
return Err(CipherError::InvalidKeyLength);
}
if iv.len() != 16 {
return Err(CipherError::InvalidInitializationVector);
}
Aes128Ctr(Box::new(ctr::Ctr128BE::new(key.into(), iv.into())))
}
_ => return Err(CipherError::UnknownCipher(algorithm_name.to_string())), _ => return Err(CipherError::UnknownCipher(algorithm_name.to_string())),
}) })
} }
@ -270,6 +293,12 @@ impl Cipher {
encryptor.encrypt_block_b2b_mut(input.into(), output.into()); encryptor.encrypt_block_b2b_mut(input.into(), output.into());
} }
} }
Aes256Ctr(encryptor) => {
encryptor.apply_keystream_b2b(input, output).unwrap();
}
Aes128Ctr(encryptor) => {
encryptor.apply_keystream_b2b(input, output).unwrap();
}
} }
} }
@ -350,6 +379,7 @@ impl Cipher {
); );
Ok(None) Ok(None)
} }
(Aes256Ctr(_) | Aes128Ctr(_), _) => Ok(None),
} }
} }
@ -405,6 +435,12 @@ impl Decipher {
"aes-128-ecb" => Aes128Ecb(Box::new(ecb::Decryptor::new(key.into()))), "aes-128-ecb" => Aes128Ecb(Box::new(ecb::Decryptor::new(key.into()))),
"aes-192-ecb" => Aes192Ecb(Box::new(ecb::Decryptor::new(key.into()))), "aes-192-ecb" => Aes192Ecb(Box::new(ecb::Decryptor::new(key.into()))),
"aes-256-ecb" => Aes256Ecb(Box::new(ecb::Decryptor::new(key.into()))), "aes-256-ecb" => Aes256Ecb(Box::new(ecb::Decryptor::new(key.into()))),
"aes-256-ctr" => {
Aes256Ctr(Box::new(ctr::Ctr128BE::new(key.into(), iv.into())))
}
"aes-128-ctr" => {
Aes128Ctr(Box::new(ctr::Ctr128BE::new(key.into(), iv.into())))
}
"aes-128-gcm" => { "aes-128-gcm" => {
let decipher = let decipher =
aead_gcm_stream::AesGcm::<aes::Aes128>::new(key.into(), iv); aead_gcm_stream::AesGcm::<aes::Aes128>::new(key.into(), iv);
@ -488,6 +524,12 @@ impl Decipher {
decryptor.decrypt_block_b2b_mut(input.into(), output.into()); decryptor.decrypt_block_b2b_mut(input.into(), output.into());
} }
} }
Aes256Ctr(decryptor) => {
decryptor.apply_keystream_b2b(input, output).unwrap();
}
Aes128Ctr(decryptor) => {
decryptor.apply_keystream_b2b(input, output).unwrap();
}
} }
} }
@ -593,6 +635,14 @@ impl Decipher {
); );
Ok(()) Ok(())
} }
(Aes256Ctr(mut decryptor), _) => {
decryptor.apply_keystream_b2b(input, output).unwrap();
Ok(())
}
(Aes128Ctr(mut decryptor), _) => {
decryptor.apply_keystream_b2b(input, output).unwrap();
Ok(())
}
} }
} }
} }

View file

@ -187,7 +187,8 @@ export class Cipheriv extends Transform implements Cipher {
this.#cache = new BlockModeCache(false); this.#cache = new BlockModeCache(false);
this.#context = op_node_create_cipheriv(cipher, toU8(key), toU8(iv)); this.#context = op_node_create_cipheriv(cipher, toU8(key), toU8(iv));
this.#needsBlockCache = this.#needsBlockCache =
!(cipher == "aes-128-gcm" || cipher == "aes-256-gcm"); !(cipher == "aes-128-gcm" || cipher == "aes-256-gcm" ||
cipher == "aes-128-ctr" || cipher == "aes-256-ctr");
if (this.#context == 0) { if (this.#context == 0) {
throw new TypeError("Unknown cipher"); throw new TypeError("Unknown cipher");
} }
@ -345,7 +346,8 @@ export class Decipheriv extends Transform implements Cipher {
this.#cache = new BlockModeCache(this.#autoPadding); this.#cache = new BlockModeCache(this.#autoPadding);
this.#context = op_node_create_decipheriv(cipher, toU8(key), toU8(iv)); this.#context = op_node_create_decipheriv(cipher, toU8(key), toU8(iv));
this.#needsBlockCache = this.#needsBlockCache =
!(cipher == "aes-128-gcm" || cipher == "aes-256-gcm"); !(cipher == "aes-128-gcm" || cipher == "aes-256-gcm" ||
cipher == "aes-128-ctr" || cipher == "aes-256-ctr");
if (this.#context == 0) { if (this.#context == 0) {
throw new TypeError("Unknown cipher"); throw new TypeError("Unknown cipher");
} }

View file

@ -48,8 +48,8 @@ const packages: Package[] = [{
const markdownText = `# Deno const markdownText = `# Deno
[Deno](https://www.deno.com) [Deno](https://deno.com)
([/ˈdiːnoʊ/](http://ipa-reader.xyz/?text=%CB%88di%CB%90no%CA%8A), pronounced ([/ˈdiːnoʊ/](https://ipa-reader.com/?text=%CB%88di%CB%90no%CA%8A), pronounced
\`dee-no\`) is a JavaScript, TypeScript, and WebAssembly runtime with secure \`dee-no\`) is a JavaScript, TypeScript, and WebAssembly runtime with secure
defaults and a great developer experience. It's built on [V8](https://v8.dev/), defaults and a great developer experience. It's built on [V8](https://v8.dev/),
[Rust](https://www.rust-lang.org/), and [Tokio](https://tokio.rs/). [Rust](https://www.rust-lang.org/), and [Tokio](https://tokio.rs/).