mirror of
https://github.com/ibm-s390-linux/s390-tools.git
synced 2026-08-05 02:14:52 +00:00
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:
committed by
Steffen Eiden
parent
5a54722848
commit
f1b94abefa
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user