Truly independent web browser
LibCrypto+LibWeb: Give ECDSA signatures to LibCrypto as r || s
Shannon Boothc4dc904f880b
diff --git a/Libraries/LibCrypto/Rust/src/ec.rs b/Libraries/LibCrypto/Rust/src/ec.rsindex b089e82cfe3..e6f857769d9 100644--- a/Libraries/LibCrypto/Rust/src/ec.rs+++ b/Libraries/LibCrypto/Rust/src/ec.rs@@ -259,18 +259,17 @@ pub fn ec_generate_key_pair(curve: EcCurve) -> Result<KeyPair, CryptoError> {})}-pub fn ec_sign(key: &KeyHandle, digest: &[u8]) -> Result<(Vec<u8>, Vec<u8>), CryptoError> {+pub fn ec_sign(key: &KeyHandle, digest: &[u8]) -> Result<Vec<u8>, CryptoError> {let private_key = private_key(key);openssl::ecdsa_sign(&private_key.key, digest, private_key.curve.scalar_size())}-pub fn ec_verify(key: &KeyHandle, digest: &[u8], r: &[u8], s: &[u8]) -> Result<bool, CryptoError> {+pub fn ec_verify(key: &KeyHandle, digest: &[u8], signature: &[u8]) -> Result<bool, CryptoError> {let public_key = public_key(key);- let scalar_size = public_key.curve.scalar_size();- if r.len() != scalar_size || s.len() != scalar_size {+ if signature.len() != public_key.curve.scalar_size() * 2 {return Ok(false);}- openssl::ecdsa_verify(&public_key.key, digest, r, s)+ openssl::ecdsa_verify(&public_key.key, digest, signature)}pub fn ec_public_key_scalar_size(key: &KeyHandle) -> usize {diff --git a/Libraries/LibCrypto/Rust/src/openssl.rs b/Libraries/LibCrypto/Rust/src/openssl.rsindex 5bbdc836225..1e44128620f 100644--- a/Libraries/LibCrypto/Rust/src/openssl.rs+++ b/Libraries/LibCrypto/Rust/src/openssl.rs@@ -899,8 +899,8 @@ pub(crate) fn ec_public_point(nid: c_int, scalar: &[u8]) -> Result<Option<Vec<u8Ok(Some(encoded))}-/// The ECDSA signature (r, s) of `digest` by `key`, each of `scalar_size` bytes.-pub(crate) fn ecdsa_sign(key: &Pkey, digest: &[u8], scalar_size: usize) -> Result<(Vec<u8>, Vec<u8>), CryptoError> {+/// The ECDSA signature of `digest` by `key`: r and then s, each of `scalar_size` bytes.+pub(crate) fn ecdsa_sign(key: &Pkey, digest: &[u8], scalar_size: usize) -> Result<Vec<u8>, CryptoError> {let encoded = sign_digest(key, digest)?;let mut cursor = encoded.as_ptr();// SAFETY: The cursor points into `encoded`, of the length given; a non-null result is owned.@@ -910,19 +910,20 @@ pub(crate) fn ecdsa_sign(key: &Pkey, digest: &[u8], scalar_size: usize) -> Resullet (mut r, mut s) = (ptr::null(), ptr::null());// SAFETY: The signature is live, and owns r and s.unsafe { sys::ECDSA_SIG_get0(signature.as_ptr(), &raw mut r, &raw mut s) };- let padded = |number: *const sys::BIGNUM| -> Result<Vec<u8>, CryptoError> {- let mut bytes = vec![0; scalar_size];+ let mut bytes = vec![0; scalar_size * 2];+ let (r_bytes, s_bytes) = bytes.split_at_mut(scalar_size);+ for (number, number_bytes) in [(r, r_bytes), (s, s_bytes)] {// SAFETY: The number is live while the signature is, and the buffer holds `scalar_size` bytes.- if unsafe { sys::BN_bn2binpad(number, bytes.as_mut_ptr(), int_length(scalar_size)?) } < 0 {+ if unsafe { sys::BN_bn2binpad(number, number_bytes.as_mut_ptr(), int_length(scalar_size)?) } < 0 {return failed();}- Ok(bytes)- };- Ok((padded(r)?, padded(s)?))+ }+ Ok(bytes)}-/// Verifies the ECDSA signature (r, s) of `digest` with `key`.-pub(crate) fn ecdsa_verify(key: &Pkey, digest: &[u8], r: &[u8], s: &[u8]) -> Result<bool, CryptoError> {+/// Verifies the ECDSA signature of `digest` with `key`, which is r and then s, each of half its length.+pub(crate) fn ecdsa_verify(key: &Pkey, digest: &[u8], signature: &[u8]) -> Result<bool, CryptoError> {+ let (r, s) = signature.split_at(signature.len() / 2);// SAFETY: Making a signature takes no arguments.let signature = EcdsaSignature::new(unsafe { sys::ECDSA_SIG_new() })?;let r = BigNum::from_bytes(r, false)?;diff --git a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rsindex 5ac80612600..7e645e0ea06 100644--- a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs@@ -7900,7 +7900,7 @@ impl AlgorithmMethods for Ecdsa {}// https://w3c.github.io/webcrypto/#ecdsa-operations-sign- fn sign(&self, cx: &CallContext, _: &AlgorithmParams, key: Ref<CryptoKey>, message: &Bytes) -> WebIdlResult<Bytes> {+ fn sign(&self, _: &CallContext, _: &AlgorithmParams, key: Ref<CryptoKey>, message: &Bytes) -> WebIdlResult<Bytes> {let EcParameters::SignOrVerify(normalized_algorithm) = &self.parameters else {unreachable!("the operation uses EC SignOrVerify parameters")};@@ -7932,54 +7932,24 @@ impl AlgorithmMethods for Ecdsa {unreachable!("the key has an EC algorithm")};- let result;-// 6. If the namedCurve attribute of the [[algorithm]] internal slot of key is "P-256", "P-384" or "P-521":- if let Some(curve) = ec_curve_from_name(named_curve) {- let coord_size = match curve {- EcCurve::P256 => 32,- EcCurve::P384 => 48,- EcCurve::P521 => 66,- };-+ let result = if ec_curve_from_name(named_curve).is_some() {// 1. Perform the ECDSA signing process, as specified in [RFC6090], Section 5.4.2, with M as the message,// using params as the EC domain parameters, and with d as the private key.// 2. Let r and s be the pair of integers resulting from performing the ECDSA signing process.- let signature = libcrypto_rust::ec_sign(d, m.as_slice());- let (r_bytes, s_bytes) = match signature {- Ok(signature) => signature,- Err(message) => {- return Err(Exception::Dom(- "OperationError",- Utf16String::from_utf8(message.message()),- ));- }- };-// 3. Let result be an empty byte sequence.- let mut result_bytes = Vec::new();- if result_bytes.try_reserve_exact(coord_size * 2).is_err() {- return cx- .vm()- .throw_completion(ErrorKind::InternalError, ErrorType::OutOfMemory, &[])- .map_err(Into::into);- }- result_bytes.resize(coord_size * 2, 0);-// 4. Let n be the smallest integer such that n * 8 is greater than the logarithm to base 2 of the order of the base point of the elliptic curve identified by params.// 5. Convert r to a byte sequence of length n and append it to result.- assert!(r_bytes.as_slice().len() <= coord_size);- result_bytes[..r_bytes.as_slice().len()].copy_from_slice(r_bytes.as_slice());-// 6. Convert s to a byte sequence of length n and append it to result.- assert!(s_bytes.as_slice().len() <= coord_size);- result_bytes[coord_size..coord_size + s_bytes.as_slice().len()].copy_from_slice(s_bytes.as_slice());- result = Bytes::new(result_bytes);+ // NB: LibCrypto gives the signature as result.+ libcrypto_rust::ec_sign(d, m.as_slice())+ .map(Bytes::new)+ .map_err(operation_error)?} else {// Otherwise, the namedCurve attribute of the [[algorithm]] internal slot of key is a value specified in an applicable specification:// Perform the ECDSA signature steps specified in that specification, passing in M, params and d and resulting in result.return Err(dom_error("DataError", b"Invalid algorithm"));- }+ };// 7. Return result.Ok(result)@@ -8041,23 +8011,13 @@ impl AlgorithmMethods for Ecdsa {let result = if ec_curve_from_name(named_curve).is_some() {// 1. Let n be the smallest integer such that n * 8 is greater than the logarithm to base 2 of the order of the// base point of the elliptic curve identified by params.- let n = libcrypto_rust::ec_public_key_scalar_size(q);-// 2. If signature does not have a length of n * 2 bytes, then return false.- if signature.as_slice().len() != n * 2 {- return Ok(false);- }-// 3. Let r be the result of converting the first n bytes of signature to an integer.- let r = &signature.as_slice()[..n];-// 4. Let s be the result of converting the last n bytes of signature to an integer.- let s = &signature.as_slice()[n..];-// 5. Perform the ECDSA verifying process, as specified in [RFC6090], Section 5.4.3, with M as the received// message, (r, s) as the signature and using params as the EC domain parameters, and Q as the public key.- let verification = libcrypto_rust::ec_verify(q, m.as_slice(), r, s);- verification.map_err(operation_error)?+ // NB: LibCrypto checks the length of the signature and reads r and s from it.+ libcrypto_rust::ec_verify(q, m.as_slice(), signature.as_slice()).map_err(operation_error)?} else {// Otherwise, the namedCurve attribute of the [[algorithm]] internal slot of key is a value specified in an applicable specification:// Perform the ECDSA verification steps specified in that specification passing in M, signature, params and Q and resulting in an indication of whether or not the purported signature is valid.