pub struct ZeroizingGuard<T>{ /* private fields */ }Expand description
RAII guard for owned values that automatically zeroizes on drop.
ZeroizingGuard wraps an owned value T in a Box and ensures that it is zeroized
when the guard is dropped. This is useful for returning sensitive data from
functions while guaranteeing automatic cleanup.
§Design
- Wraps
Box<T>(owns the value on the heap, avoiding stack copies) - Takes
&mut Tin constructor and swaps withT::default(), zeroizing the source - Implements
DerefandDerefMutfor convenient access - Zeroizes
inneron drop - Contains
ZeroizeOnDropSentinelto verify zeroization happened
§Usage
use redoubt_zero_core::{ZeroizingGuard, ZeroizationProbe, FastZeroizable};
fn create_sensitive_data() -> ZeroizingGuard<u64> {
let mut value = 12345u64;
ZeroizingGuard::from_mut(&mut value)
}
{
let guard = create_sensitive_data();
assert_eq!(*guard, 12345);
} // guard drops here → value is zeroized§Panics
The guard panics on drop if the wrapped value’s ZeroizeOnDropSentinel was not
marked as zeroized. This ensures zeroization invariants are enforced.
Implementations§
Source§impl<T> ZeroizingGuard<T>
impl<T> ZeroizingGuard<T>
Sourcepub fn from_mut(value: &mut T) -> Self
pub fn from_mut(value: &mut T) -> Self
Creates a new guard by swapping the value from the source and zeroizing it.
The source location is swapped with T::default() and then zeroized,
ensuring no copies of the sensitive data remain on the stack.
The value is stored in a Box on the heap.
§Example
use redoubt_zero_core::{ZeroizingGuard, ZeroizationProbe};
let mut value = 42u32;
let guard = ZeroizingGuard::from_mut(&mut value);
assert_eq!(*guard, 42);
assert!(value.is_zeroized()); // source is zeroizedSourcepub fn from_default() -> Self
pub fn from_default() -> Self
Creates a new guard with the default value of T.
This is a convenience method equivalent to:
let mut value = T::default();
ZeroizingGuard::from_mut(&mut value)§Example
use redoubt_zero_core::{ZeroizingGuard, ZeroizationProbe};
let guard: ZeroizingGuard<u64> = ZeroizingGuard::from_default();
assert!(guard.is_zeroized());Trait Implementations§
Source§impl<T> AssertZeroizeOnDrop for ZeroizingGuard<T>
impl<T> AssertZeroizeOnDrop for ZeroizingGuard<T>
Source§fn clone_sentinel(&self) -> ZeroizeOnDropSentinel
fn clone_sentinel(&self) -> ZeroizeOnDropSentinel
ZeroizeOnDropSentinel for verification. Read moreSource§fn assert_zeroize_on_drop(self)
fn assert_zeroize_on_drop(self)
Source§impl<T> Debug for ZeroizingGuard<T>
impl<T> Debug for ZeroizingGuard<T>
Source§impl<T> Deref for ZeroizingGuard<T>
impl<T> Deref for ZeroizingGuard<T>
Source§impl<T> DerefMut for ZeroizingGuard<T>
impl<T> DerefMut for ZeroizingGuard<T>
Source§impl<T> Drop for ZeroizingGuard<T>
impl<T> Drop for ZeroizingGuard<T>
Source§impl<T> FastZeroizable for ZeroizingGuard<T>
impl<T> FastZeroizable for ZeroizingGuard<T>
Source§fn fast_zeroize(&mut self)
fn fast_zeroize(&mut self)
Source§impl<T> ZeroizationProbe for ZeroizingGuard<T>
impl<T> ZeroizationProbe for ZeroizingGuard<T>
Source§fn is_zeroized(&self) -> bool
fn is_zeroized(&self) -> bool
true if the value is zeroized (all bytes are 0). Read more