@@ -4,6 +4,7 @@ use std::{
44 str:: FromStr ,
55} ;
66
7+ use subtle:: ConstantTimeEq ;
78use zeroize:: Zeroize ;
89
910use crate :: secure_utils:: memlock;
@@ -17,7 +18,7 @@ use crate::secure_utils::memlock;
1718/// - Automatic `madvise(MADV_NOCORE/MADV_DONTDUMP)` to protect against leaking into core dumps (FreeBSD, DragonflyBSD, Linux)
1819///
1920/// Comparisons using the `PartialEq` implementation are undefined behavior (and most likely wrong) if `T` has any padding bytes.
20- #[ derive( Eq , PartialEq , PartialOrd , Ord , Hash ) ]
21+ #[ derive( Eq , PartialOrd , Ord , Hash ) ]
2122pub struct SecureArray < T , const LENGTH : usize >
2223where
2324 T : Copy + Zeroize ,
@@ -56,6 +57,29 @@ impl<T: Copy + Zeroize, const LENGTH: usize> Clone for SecureArray<T, LENGTH> {
5657 }
5758}
5859
60+ impl < T , const LENGTH : usize > PartialEq for SecureArray < T , LENGTH >
61+ where
62+ T : Copy + Zeroize ,
63+ {
64+ fn eq ( & self , other : & Self ) -> bool {
65+ let self_bytes = unsafe {
66+ std:: slice:: from_raw_parts (
67+ self . content . as_ptr ( ) as * const T as * const u8 ,
68+ LENGTH * std:: mem:: size_of :: < T > ( ) ,
69+ )
70+ } ;
71+
72+ let other_bytes = unsafe {
73+ std:: slice:: from_raw_parts (
74+ other. content . as_ptr ( ) as * const T as * const u8 ,
75+ LENGTH * std:: mem:: size_of :: < T > ( ) ,
76+ )
77+ } ;
78+
79+ self_bytes. ct_eq ( other_bytes) . into ( )
80+ }
81+ }
82+
5983// Creation
6084impl < T , const LENGTH : usize > From < [ T ; LENGTH ] > for SecureArray < T , LENGTH >
6185where
0 commit comments