1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 00:29:09 -05:00

refactor: rewrite ext/node/crypto to op2 macro (#20675)

This commit is contained in:
Bartek Iwańczuk 2023-09-26 14:07:04 +02:00 committed by GitHub
parent 8fcea5966c
commit 3c0d6de155
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 49 deletions

View file

@ -46,6 +46,7 @@ pub fn op_node_check_prime(num: serde_v8::BigInt, checks: usize) -> bool {
primes::is_probably_prime(&num, checks) primes::is_probably_prime(&num, checks)
} }
// TODO(bartlomieju): blocked on `op2` crashing on `ArrayBufferView`
#[op] #[op]
pub fn op_node_check_prime_bytes( pub fn op_node_check_prime_bytes(
bytes: &[u8], bytes: &[u8],
@ -64,6 +65,7 @@ pub async fn op_node_check_prime_async(
Ok(spawn_blocking(move || primes::is_probably_prime(&num, checks)).await?) Ok(spawn_blocking(move || primes::is_probably_prime(&num, checks)).await?)
} }
// TODO(bartlomieju): blocked on `op2` supporting returning a future
#[op] #[op]
pub fn op_node_check_prime_bytes_async( pub fn op_node_check_prime_bytes_async(
bytes: &[u8], bytes: &[u8],
@ -504,13 +506,14 @@ pub fn op_node_pbkdf2(
pbkdf2_sync(&password, &salt, iterations, digest, derived_key).is_ok() pbkdf2_sync(&password, &salt, iterations, digest, derived_key).is_ok()
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_node_pbkdf2_async( pub async fn op_node_pbkdf2_async(
password: StringOrBuffer, #[serde] password: StringOrBuffer,
salt: StringOrBuffer, #[serde] salt: StringOrBuffer,
iterations: u32, #[smi] iterations: u32,
digest: String, #[string] digest: String,
keylen: usize, #[number] keylen: usize,
) -> Result<ToJsBuffer, AnyError> { ) -> Result<ToJsBuffer, AnyError> {
spawn_blocking(move || { spawn_blocking(move || {
let mut derived_key = vec![0; keylen]; let mut derived_key = vec![0; keylen];
@ -578,13 +581,14 @@ pub fn op_node_hkdf(
hkdf_sync(hash, ikm, salt, info, okm) hkdf_sync(hash, ikm, salt, info, okm)
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_node_hkdf_async( pub async fn op_node_hkdf_async(
hash: String, #[string] hash: String,
ikm: JsBuffer, #[buffer] ikm: JsBuffer,
salt: JsBuffer, #[buffer] salt: JsBuffer,
info: JsBuffer, #[buffer] info: JsBuffer,
okm_len: usize, #[number] okm_len: usize,
) -> Result<ToJsBuffer, AnyError> { ) -> Result<ToJsBuffer, AnyError> {
spawn_blocking(move || { spawn_blocking(move || {
let mut okm = vec![0u8; okm_len]; let mut okm = vec![0u8; okm_len];
@ -616,18 +620,20 @@ fn generate_rsa(
Ok((private_key_der.into(), public_key_der.into())) Ok((private_key_der.into(), public_key_der.into()))
} }
#[op] #[op2]
#[serde]
pub fn op_node_generate_rsa( pub fn op_node_generate_rsa(
modulus_length: usize, #[number] modulus_length: usize,
public_exponent: usize, #[number] public_exponent: usize,
) -> Result<(ToJsBuffer, ToJsBuffer), AnyError> { ) -> Result<(ToJsBuffer, ToJsBuffer), AnyError> {
generate_rsa(modulus_length, public_exponent) generate_rsa(modulus_length, public_exponent)
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_node_generate_rsa_async( pub async fn op_node_generate_rsa_async(
modulus_length: usize, #[number] modulus_length: usize,
public_exponent: usize, #[number] public_exponent: usize,
) -> Result<(ToJsBuffer, ToJsBuffer), AnyError> { ) -> Result<(ToJsBuffer, ToJsBuffer), AnyError> {
spawn_blocking(move || generate_rsa(modulus_length, public_exponent)).await? spawn_blocking(move || generate_rsa(modulus_length, public_exponent)).await?
} }
@ -670,18 +676,20 @@ fn dsa_generate(
)) ))
} }
#[op] #[op2]
#[serde]
pub fn op_node_dsa_generate( pub fn op_node_dsa_generate(
modulus_length: usize, #[number] modulus_length: usize,
divisor_length: usize, #[number] divisor_length: usize,
) -> Result<(ToJsBuffer, ToJsBuffer), AnyError> { ) -> Result<(ToJsBuffer, ToJsBuffer), AnyError> {
dsa_generate(modulus_length, divisor_length) dsa_generate(modulus_length, divisor_length)
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_node_dsa_generate_async( pub async fn op_node_dsa_generate_async(
modulus_length: usize, #[number] modulus_length: usize,
divisor_length: usize, #[number] divisor_length: usize,
) -> Result<(ToJsBuffer, ToJsBuffer), AnyError> { ) -> Result<(ToJsBuffer, ToJsBuffer), AnyError> {
spawn_blocking(move || dsa_generate(modulus_length, divisor_length)).await? spawn_blocking(move || dsa_generate(modulus_length, divisor_length)).await?
} }
@ -843,21 +851,23 @@ fn dh_generate(
)) ))
} }
#[op] #[op2]
#[serde]
pub fn op_node_dh_generate( pub fn op_node_dh_generate(
prime: Option<&[u8]>, #[serde] prime: Option<&[u8]>,
prime_len: usize, #[number] prime_len: usize,
generator: usize, #[number] generator: usize,
) -> Result<(ToJsBuffer, ToJsBuffer), AnyError> { ) -> Result<(ToJsBuffer, ToJsBuffer), AnyError> {
dh_generate(prime, prime_len, generator) dh_generate(prime, prime_len, generator)
} }
// TODO(lev): This duplication should be avoided. // TODO(lev): This duplication should be avoided.
#[op] #[op2]
#[serde]
pub fn op_node_dh_generate2( pub fn op_node_dh_generate2(
prime: JsBuffer, #[buffer] prime: JsBuffer,
prime_len: usize, #[number] prime_len: usize,
generator: usize, #[number] generator: usize,
) -> Result<(ToJsBuffer, ToJsBuffer), AnyError> { ) -> Result<(ToJsBuffer, ToJsBuffer), AnyError> {
dh_generate(Some(prime).as_deref(), prime_len, generator) dh_generate(Some(prime).as_deref(), prime_len, generator)
} }
@ -877,11 +887,12 @@ pub fn op_node_dh_compute_secret(
Ok(shared_secret.to_bytes_be().into()) Ok(shared_secret.to_bytes_be().into())
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_node_dh_generate_async( pub async fn op_node_dh_generate_async(
prime: Option<JsBuffer>, #[buffer] prime: Option<JsBuffer>,
prime_len: usize, #[number] prime_len: usize,
generator: usize, #[number] generator: usize,
) -> Result<(ToJsBuffer, ToJsBuffer), AnyError> { ) -> Result<(ToJsBuffer, ToJsBuffer), AnyError> {
spawn_blocking(move || dh_generate(prime.as_deref(), prime_len, generator)) spawn_blocking(move || dh_generate(prime.as_deref(), prime_len, generator))
.await? .await?
@ -954,16 +965,15 @@ pub fn op_node_scrypt_sync(
) )
} }
#[op2(async)] #[op]
#[serde]
pub async fn op_node_scrypt_async( pub async fn op_node_scrypt_async(
#[serde] password: StringOrBuffer, password: StringOrBuffer,
#[serde] salt: StringOrBuffer, salt: StringOrBuffer,
#[smi] keylen: u32, keylen: u32,
#[smi] cost: u32, cost: u32,
#[smi] block_size: u32, block_size: u32,
#[smi] parallelization: u32, parallelization: u32,
#[smi] maxmem: u32, maxmem: u32,
) -> Result<ToJsBuffer, AnyError> { ) -> Result<ToJsBuffer, AnyError> {
spawn_blocking(move || { spawn_blocking(move || {
let mut output_buffer = vec![0u8; keylen as usize]; let mut output_buffer = vec![0u8; keylen as usize];
@ -1153,14 +1163,16 @@ fn gen_prime(size: usize) -> ToJsBuffer {
primes::Prime::generate(size).0.to_bytes_be().into() primes::Prime::generate(size).0.to_bytes_be().into()
} }
#[op] #[op2]
pub fn op_node_gen_prime(size: usize) -> ToJsBuffer { #[serde]
pub fn op_node_gen_prime(#[number] size: usize) -> ToJsBuffer {
gen_prime(size) gen_prime(size)
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_node_gen_prime_async( pub async fn op_node_gen_prime_async(
size: usize, #[number] size: usize,
) -> Result<ToJsBuffer, AnyError> { ) -> Result<ToJsBuffer, AnyError> {
Ok(spawn_blocking(move || gen_prime(size)).await?) Ok(spawn_blocking(move || gen_prime(size)).await?)
} }

View file

@ -318,6 +318,7 @@ pub fn op_zlib_write_async(
}) })
} }
// TODO(bartlomieju): op2 can't seem to handle clippy ignore here
#[op] #[op]
pub fn op_zlib_write( pub fn op_zlib_write(
state: &mut OpState, state: &mut OpState,
@ -344,6 +345,7 @@ pub fn op_zlib_write(
Ok(zlib.err) Ok(zlib.err)
} }
// TODO(bartlomieju): op2 can't seem to handle clippy ignore here
#[op] #[op]
pub fn op_zlib_init( pub fn op_zlib_init(
state: &mut OpState, state: &mut OpState,