rust/pv_core: Implement Zeroize trait for u* and i* primtives

Use a macro for the trait implementations.
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
This commit is contained in:
Marc Hartmayer
2024-11-13 12:32:57 +00:00
committed by Steffen Eiden
parent 5a54722848
commit f1b94abefa

View File

@@ -12,6 +12,35 @@ pub trait Zeroize {
fn zeroize(&mut self);
}
macro_rules! integer_impl {
($typ:ty) => {
impl Zeroize for $typ {
/// Reliably overwrites the given number with zeros,
/// by performing a volatile write followed by a memory barrier
fn zeroize(&mut self) {
unsafe {
std::ptr::write_volatile(self as *mut $typ, <$typ>::default());
}
std::sync::atomic::compiler_fence(std::sync::atomic::Ordering::SeqCst);
}
}
};
}
integer_impl!(u8);
integer_impl!(u16);
integer_impl!(u32);
integer_impl!(u64);
integer_impl!(u128);
integer_impl!(usize);
integer_impl!(i8);
integer_impl!(i16);
integer_impl!(i32);
integer_impl!(i64);
integer_impl!(i128);
integer_impl!(isize);
// Automatically impl Zeroize for u8 arrays
impl<T: Default, const COUNT: usize> Zeroize for [T; COUNT] {
/// Reliably overwrites the given buffer with zeros,