pub struct SecretBox<S: Zeroize> { /* private fields */ }Expand description
A pure-software, stack-native hardened wrapper for sensitive values (no_std compatible).
SecretBox protects inline data by surrounding it with dynamically generated, randomized canaries
(verified via compiler-optimization-proof read_volatile checks) to immediately panic during stack
smashing buffer overflows.
It implements mathematically strict &mut self concurrency locks, forces zeroization on drop, and
mandates that data can only be touched inside strictly scoped .with_secret() closures to
eliminate accidental memory leaks. It strictly forbids Clone to prevent key scattering.
§Examples
use sec_mem::SecretBox;
// Wrap a basic 32-bit integer or a 256-bit array entirely on the stack
let mut secret = SecretBox::new([0x42u8; 32]);
// The data can only be accessed exclusively inside a closure
secret.with_secret_mut(|s| {
s[0] = 0x99;
});
// Any stack corruption that occurred is detected here, panicking instantly.Implementations§
Source§impl<S: Zeroize> SecretBox<S>
impl<S: Zeroize> SecretBox<S>
Sourcepub fn with_secret<F, R>(&mut self, f: F) -> R
pub fn with_secret<F, R>(&mut self, f: F) -> R
Safer alternative: Provide an exclusive closure-based interface.
Requires &mut self to mathematically prevent concurrent multi-threaded
aliasing or reference sharing.
Sourcepub fn with_secret_mut<F, R>(&mut self, f: F) -> R
pub fn with_secret_mut<F, R>(&mut self, f: F) -> R
Safer mutable alternative: Provide a closure-based interface.
Source§impl<S: Zeroize + Default> SecretBox<S>
impl<S: Zeroize + Default> SecretBox<S>
Sourcepub fn init_with_mut(ctr: impl FnOnce(&mut S)) -> Self
pub fn init_with_mut(ctr: impl FnOnce(&mut S)) -> Self
Create a secret value using a function that can initialize the value in-place.
Source§impl<S: Zeroize> SecretBox<S>
impl<S: Zeroize> SecretBox<S>
Sourcepub fn constant_time_eq(&mut self, other: &mut Self) -> Choicewhere
S: ConstantTimeEq,
pub fn constant_time_eq(&mut self, other: &mut Self) -> Choicewhere
S: ConstantTimeEq,
Performs a constant-time comparison of two SecretBoxes.
Requires &mut on both to enforce exclusive access and check canaries.