use core::sync::atomic::{Ordering, compiler_fence};
pub(crate) fn secure_wipe_u32(xs: &mut [u32]) {
for x in xs {
unsafe { core::ptr::write_volatile(x, 0) };
}
compiler_fence(Ordering::SeqCst);
}
pub(crate) fn secure_wipe_bytes(xs: &mut [u8]) {
for x in xs {
unsafe { core::ptr::write_volatile(x, 0) };
}
compiler_fence(Ordering::SeqCst);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wipe_zeroes_every_element() {
let mut words = [0xDEADBEEFu32; 37];
secure_wipe_u32(&mut words);
assert!(words.iter().all(|&w| w == 0));
let mut bytes = [0xA5u8; 64];
secure_wipe_bytes(&mut bytes);
assert!(bytes.iter().all(|&b| b == 0));
}
#[test]
fn wipe_tolerates_empty_slices() {
secure_wipe_u32(&mut []);
secure_wipe_bytes(&mut []);
}
}