Skip to content

Truly independent web browser

LibCrypto: Compare WebCrypto MACs with subtle

Shannon Booth80dffac73dcc
diff --git a/Cargo.lock b/Cargo.lock
index 321c25c439c..af333c4d5e2 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1871,6 +1871,7 @@ name = "libcrypto_rust"
version = "0.1.0"
dependencies = [
"ak",
+ "subtle",
]
[[package]]
diff --git a/Libraries/LibCrypto/Rust/Cargo.toml b/Libraries/LibCrypto/Rust/Cargo.toml
index 24cc856c11f..93184888f54 100644
--- a/Libraries/LibCrypto/Rust/Cargo.toml
+++ b/Libraries/LibCrypto/Rust/Cargo.toml
@@ -5,6 +5,7 @@ edition = "2024"
[dependencies]
ak = { path = "../../../AK/Rust" }
+subtle = "2.6"
[lints]
workspace = true
diff --git a/Libraries/LibCrypto/Rust/src/ffi.rs b/Libraries/LibCrypto/Rust/src/ffi.rs
index 38a37cf6650..3c1534522a4 100644
--- a/Libraries/LibCrypto/Rust/src/ffi.rs
+++ b/Libraries/LibCrypto/Rust/src/ffi.rs
@@ -88,7 +88,6 @@ unsafe extern "C" {
state: *mut c_void,
receive: ReceiveBytes,
) -> ();
- pub(super) fn crypto_timing_safe_compare(left: Span<'_, u8>, right: Span<'_, u8>) -> bool;
pub(super) fn crypto_kmac_default_key_length(kind: KmacKind) -> u32;
pub(super) fn crypto_kmac_validate_parameters(output_length: u32, customization: OptionalBytes<'_>) -> bool;
pub(super) fn crypto_kmac_sign(
diff --git a/Libraries/LibCrypto/Rust/src/lib.rs b/Libraries/LibCrypto/Rust/src/lib.rs
index b4597a4ebd1..af32faae3d2 100644
--- a/Libraries/LibCrypto/Rust/src/lib.rs
+++ b/Libraries/LibCrypto/Rust/src/lib.rs
@@ -12,6 +12,7 @@
use core::ffi::c_void;
use ak::{Span, SpanMut, Utf16String};
+use subtle::ConstantTimeEq;
mod ffi;
mod key;
@@ -250,8 +251,7 @@ pub fn hmac_sign(kind: ShaHashKind, key: &[u8], message: &[u8]) -> Vec<u8> {
}
pub fn timing_safe_compare(left: &[u8], right: &[u8]) -> bool {
- // SAFETY: The input is borrowed for the call.
- unsafe { ffi::crypto_timing_safe_compare(Span::new(left), Span::new(right)) }
+ left.ct_eq(right).into()
}
pub fn kmac_default_key_length(kind: KmacKind) -> u32 {
diff --git a/Libraries/LibCrypto/RustBindings.cpp b/Libraries/LibCrypto/RustBindings.cpp
index 4a5b902fc52..081108dcca9 100644
--- a/Libraries/LibCrypto/RustBindings.cpp
+++ b/Libraries/LibCrypto/RustBindings.cpp
@@ -823,7 +823,6 @@ bool crypto_okp_export_der(OkpCurve curve, bool is_public, ReadonlyBytes key, vo
extern "C" {
void crypto_hmac_sign(::Crypto::Hash::HashKind, ReadonlyBytes, ReadonlyBytes, void*, ReceiveEncodedBytes);
-bool crypto_timing_safe_compare(ReadonlyBytes, ReadonlyBytes);
}
static_assert(::Crypto::Hash::SHA1::block_size() == 64);
@@ -838,12 +837,6 @@ void crypto_hmac_sign(::Crypto::Hash::HashKind hash_kind, ReadonlyBytes key, Rea
receive(state, mac);
}
-bool crypto_timing_safe_compare(ReadonlyBytes left, ReadonlyBytes right)
-{
- VERIFY(left.size() == right.size());
- return timing_safe_compare(left.data(), right.data(), left.size());
-}
-
extern "C" {
bool crypto_mldsa_sign(::Crypto::PK::MLDSASize, KeyHandle const*, ReadonlyBytes, ReadonlyBytes, void*, ReceiveEncodedBytes);
bool crypto_mldsa_verify(::Crypto::PK::MLDSASize, KeyHandle const*, ReadonlyBytes, ReadonlyBytes, ReadonlyBytes, bool*);