From 2d9e240fb6ae13e6139713f9bb8ccb51e5dc0bff Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 28 Mar 2025 13:42:46 +0000 Subject: [PATCH] [hashes] Use `fixed_time_eq` for `Hmac::eq` When someone is checking if an `Hmac` is equal to some other `Hmac`, its fairly common for them to be doing a constant-time-ness-sensitive operation. Thus, here we default to our existing `fixed_time_eq` method for `PartialEq` on `Hamc`, rather than the naive Rust slice comparison. While we should consider doing the same for all hash types, we do not yet do so here. --- hashes/src/hmac/mod.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/hashes/src/hmac/mod.rs b/hashes/src/hmac/mod.rs index 2d70253fc..d69c3ccc2 100644 --- a/hashes/src/hmac/mod.rs +++ b/hashes/src/hmac/mod.rs @@ -15,8 +15,9 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; use crate::{Hash, HashEngine}; /// A hash computed from a RFC 2104 HMAC. Parameterized by the underlying hash function. -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Copy, Clone, PartialOrd, Ord, Hash)] #[repr(transparent)] +#[allow(clippy::derived_hash_with_manual_eq)] pub struct Hmac(T); impl str::FromStr for Hmac { @@ -24,6 +25,14 @@ impl str::FromStr for Hmac { fn from_str(s: &str) -> Result { Ok(Hmac(str::FromStr::from_str(s)?)) } } +impl PartialEq for Hmac { + fn eq(&self, other: &Self) -> bool { + crate::cmp::fixed_time_eq(self.as_ref(), other.as_ref()) + } +} + +impl Eq for Hmac {} + /// Pair of underlying hash engines, used for the inner and outer hash of HMAC. #[derive(Debug, Clone)] pub struct HmacEngine {