diff options
author | Yoshiya Hinosawa <stibium121@gmail.com> | 2023-03-14 15:59:23 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-14 15:59:23 +0900 |
commit | e80cc17dc43c26fe9dd1d1fb0dce80fc049cfffd (patch) | |
tree | c58f6679ede5df0e7d83e79e8c4bc7ee5e82c749 /ext/node/crypto/mod.rs | |
parent | 9aa20b3ba758765863a4c1055097fda399efcfc3 (diff) |
fix(ext/node): add crypto.createCipheriv (#18091)
Diffstat (limited to 'ext/node/crypto/mod.rs')
-rw-r--r-- | ext/node/crypto/mod.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/ext/node/crypto/mod.rs b/ext/node/crypto/mod.rs index f9a61b456..3e6af9b4b 100644 --- a/ext/node/crypto/mod.rs +++ b/ext/node/crypto/mod.rs @@ -15,6 +15,7 @@ use rsa::PublicKey; use rsa::RsaPrivateKey; use rsa::RsaPublicKey; +mod cipher; mod digest; #[op(fast)] @@ -153,3 +154,46 @@ pub fn op_node_public_encrypt( _ => Err(type_error("Unknown padding")), } } + +#[op(fast)] +pub fn op_node_create_cipheriv( + state: &mut OpState, + algorithm: &str, + key: &[u8], + iv: &[u8], +) -> u32 { + state.resource_table.add( + match cipher::CipherContext::new(algorithm, key, iv) { + Ok(context) => context, + Err(_) => return 0, + }, + ) +} + +#[op(fast)] +pub fn op_node_cipheriv_encrypt( + state: &mut OpState, + rid: u32, + input: &[u8], + output: &mut [u8], +) -> bool { + let context = match state.resource_table.get::<cipher::CipherContext>(rid) { + Ok(context) => context, + Err(_) => return false, + }; + context.encrypt(input, output); + true +} + +#[op] +pub fn op_node_cipheriv_final( + state: &mut OpState, + rid: u32, + input: &[u8], + output: &mut [u8], +) -> Result<(), AnyError> { + let context = state.resource_table.take::<cipher::CipherContext>(rid)?; + let context = Rc::try_unwrap(context) + .map_err(|_| type_error("Cipher context is already in use"))?; + context.r#final(input, output) +} |