Skip to content

Commit 7cf3c6c

Browse files
committed
Implement constant time comparison for SecretKey
The current implementation of `PartialEq` leaks data because it is not constant time. Attempt to make the `PartialEq` implementation constant time.
1 parent 19039d9 commit 7cf3c6c

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/key.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ pub struct SecretKey([u8; constants::SECRET_KEY_SIZE]);
6161
impl_display_secret!(SecretKey);
6262

6363
impl PartialEq for SecretKey {
64+
/// This implementation is designed to be constant time to help prevent side channel attacks.
6465
#[inline]
6566
fn eq(&self, other: &Self) -> bool {
66-
self[..] == other[..]
67+
let accum = self.0.iter().zip(&other.0)
68+
.fold(0, |accum, (a, b)| accum | a ^ b);
69+
unsafe { core::ptr::read_volatile(&accum) == 0 }
6770
}
6871
}
6972

0 commit comments

Comments
 (0)