Skip to content

Commit

Permalink
feat: use constant-time equality checking for DHKE (#232)
Browse files Browse the repository at this point in the history
This PR ensures that `DiffieHellmanSharedSecret` equality testing is
done in constant time.

Previously, this equality testing was offloaded to the underlying
`PublicKey` type. While this type supports the `ConstantTimeEq` trait,
it is not guaranteed that equality testing will use this in all
implementations.
  • Loading branch information
AaronFeickert authored Jul 15, 2024
1 parent bdf1d83 commit 2a1715a
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/dhke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use zeroize::{Zeroize, ZeroizeOnDrop};
use crate::keys::PublicKey;

/// The result of a Diffie-Hellman key exchange
#[derive(PartialEq, Eq, Zeroize, ZeroizeOnDrop)]
#[derive(Zeroize, ZeroizeOnDrop)]
pub struct DiffieHellmanSharedSecret<P>(P)
where P: PublicKey;

Expand Down Expand Up @@ -52,6 +52,16 @@ where P: PublicKey
}
}

impl<P> Eq for DiffieHellmanSharedSecret<P> where P: PublicKey {}

impl<P> PartialEq for DiffieHellmanSharedSecret<P>
where P: PublicKey
{
fn eq(&self, other: &Self) -> bool {
self.0.ct_eq(&other.0).into()
}
}

#[cfg(test)]
mod test {
use rand_core::OsRng;
Expand Down

0 comments on commit 2a1715a

Please sign in to comment.