Skip to main content

SecretBox

Struct SecretBox 

Source
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>

Source

pub fn new(secret: S) -> Self

Create a secret value using an inline value.

Source

pub fn with_secret<F, R>(&mut self, f: F) -> R
where F: FnOnce(&S) -> R,

Safer alternative: Provide an exclusive closure-based interface. Requires &mut self to mathematically prevent concurrent multi-threaded aliasing or reference sharing.

Source

pub fn with_secret_mut<F, R>(&mut self, f: F) -> R
where F: FnOnce(&mut S) -> R,

Safer mutable alternative: Provide a closure-based interface.

Source§

impl<S: Zeroize + Default> SecretBox<S>

Source

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>

Source

pub fn constant_time_eq(&mut self, other: &mut Self) -> Choice
where S: ConstantTimeEq,

Performs a constant-time comparison of two SecretBoxes. Requires &mut on both to enforce exclusive access and check canaries.

Trait Implementations§

Source§

impl<S: Zeroize> Debug for SecretBox<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: Zeroize + Default> Default for SecretBox<S>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<S: Zeroize> Drop for SecretBox<S>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<S: Zeroize> From<S> for SecretBox<S>

Source§

fn from(source: S) -> Self

Converts to this type from the input type.
Source§

impl<S: Zeroize> Zeroize for SecretBox<S>

Source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.
Source§

impl<S: Zeroize> ZeroizeOnDrop for SecretBox<S>

Auto Trait Implementations§

§

impl<S> Freeze for SecretBox<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for SecretBox<S>
where S: RefUnwindSafe,

§

impl<S> Send for SecretBox<S>
where S: Send,

§

impl<S> Sync for SecretBox<S>
where S: Sync,

§

impl<S> Unpin for SecretBox<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for SecretBox<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for SecretBox<S>
where S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.