diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2024-03-21 14:11:54 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-21 14:11:54 +0530 |
commit | 1f60b8af97b15cb8e33f68c44f602cf69d79bd7a (patch) | |
tree | b7ab11749fc4e20a9b4589ba2b97bd64991bb9f0 /ext/node/ops | |
parent | 210f2911ce3f498524c0354e8f60d62e3dbc39ed (diff) |
fix(ext/node): ECDH.publicKey() point encoding (#23013)
Diffstat (limited to 'ext/node/ops')
-rw-r--r-- | ext/node/ops/crypto/mod.rs | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/ext/node/ops/crypto/mod.rs b/ext/node/ops/crypto/mod.rs index 7ea96c031..ed1b7fc75 100644 --- a/ext/node/ops/crypto/mod.rs +++ b/ext/node/ops/crypto/mod.rs @@ -1047,6 +1047,75 @@ pub async fn op_node_scrypt_async( .await? } +#[op2] +#[buffer] +pub fn op_node_ecdh_encode_pubkey( + #[string] curve: &str, + #[buffer] pubkey: &[u8], + compress: bool, +) -> Result<Vec<u8>, AnyError> { + use elliptic_curve::sec1::FromEncodedPoint; + + match curve { + "secp256k1" => { + let pubkey = + elliptic_curve::PublicKey::<k256::Secp256k1>::from_encoded_point( + &elliptic_curve::sec1::EncodedPoint::<k256::Secp256k1>::from_bytes( + pubkey, + )?, + ); + // CtOption does not expose its variants. + if pubkey.is_none().into() { + return Err(type_error("Invalid public key")); + } + + let pubkey = pubkey.unwrap(); + + Ok(pubkey.to_encoded_point(compress).as_ref().to_vec()) + } + "prime256v1" | "secp256r1" => { + let pubkey = elliptic_curve::PublicKey::<NistP256>::from_encoded_point( + &elliptic_curve::sec1::EncodedPoint::<NistP256>::from_bytes(pubkey)?, + ); + // CtOption does not expose its variants. + if pubkey.is_none().into() { + return Err(type_error("Invalid public key")); + } + + let pubkey = pubkey.unwrap(); + + Ok(pubkey.to_encoded_point(compress).as_ref().to_vec()) + } + "secp384r1" => { + let pubkey = elliptic_curve::PublicKey::<NistP384>::from_encoded_point( + &elliptic_curve::sec1::EncodedPoint::<NistP384>::from_bytes(pubkey)?, + ); + // CtOption does not expose its variants. + if pubkey.is_none().into() { + return Err(type_error("Invalid public key")); + } + + let pubkey = pubkey.unwrap(); + + Ok(pubkey.to_encoded_point(compress).as_ref().to_vec()) + } + "secp224r1" => { + let pubkey = elliptic_curve::PublicKey::<NistP224>::from_encoded_point( + &elliptic_curve::sec1::EncodedPoint::<NistP224>::from_bytes(pubkey)?, + ); + // CtOption does not expose its variants. + if pubkey.is_none().into() { + return Err(type_error("Invalid public key")); + } + + let pubkey = pubkey.unwrap(); + + Ok(pubkey.to_encoded_point(compress).as_ref().to_vec()) + } + &_ => Err(type_error("Unsupported curve")), + } +} + #[op2(fast)] pub fn op_node_ecdh_generate_keys( #[string] curve: &str, |