Skip to main content

SecretPool

Struct SecretPool 

Source
pub struct SecretPool<const N: usize, const SLOTS: usize> { /* private fields */ }
Expand description

Fixed-slot arena for many same-size secrets inside one locked mapping.

SecretPool<N, SLOTS> amortizes platform memory-locking overhead when an application needs many fixed-size secrets at once. Instead of using one locked page-backed mapping per secret, the pool creates one private locked mapping large enough for SLOTS slots of N bytes and hands out lifetime-bound SecretPoolSlot handles.

Slots borrow the pool, so Rust prevents the pool from being dropped while a slot is still live. Dropping a slot volatile-clears exactly that slot and returns it to the pool. Dropping the pool volatile-clears the full mapping before unlocking and releasing it.

Implementations§

Source§

impl<const N: usize, const SLOTS: usize> SecretPool<N, SLOTS>

Source

pub fn new() -> Result<Self, MemoryLockError>

Create a locked pool with SLOTS fixed-size slots of N bytes.

This performs one platform mapping and one platform lock operation for the whole arena. The requested mapping length is rounded to the platform page granule, so the pool also clears padding bytes on drop.

Source

pub const fn slot_size(&self) -> usize

Number of bytes in each slot.

Source

pub const fn capacity_slots(&self) -> usize

Number of slots in the pool.

Source

pub const fn is_empty(&self) -> bool

Returns true when the pool cannot hold any secret bytes.

Source

pub const fn locked_len(&self) -> usize

Rounded platform mapping length locked by this pool.

Source

pub fn available_slots(&self) -> usize

Count slots that are currently available.

This is a point-in-time observation. Other threads may allocate or release slots immediately after this method returns.

Source

pub fn allocate(&self) -> Option<SecretPoolSlot<'_, N, SLOTS>>

Allocate one unused slot from the pool.

Returns None when every slot is currently allocated. The returned slot starts zeroed if it has never been used before, or freshly zeroed from the previous slot drop.

With random-canary, operating-system CSPRNG failure is also reported as None. Use SecretPool::try_allocate when the caller needs to distinguish pool exhaustion from random-canary setup failure.

Source

pub fn try_allocate( &self, ) -> Result<Option<SecretPoolSlot<'_, N, SLOTS>>, MemoryLockError>

Allocate one unused slot from the pool and report random-canary setup errors explicitly.

This is equivalent to SecretPool::allocate unless the random-canary feature is enabled. With random-canary, operating system CSPRNG failure is returned as MemoryLockError instead of panicking.

Source

pub fn allocate_from_slice( &self, source: &[u8], ) -> Result<Option<SecretPoolSlot<'_, N, SLOTS>>, LengthError>

Allocate a slot and copy bytes from a same-length slice.

Returns Ok(None) when the pool is full. A length mismatch returns an error without allocating a slot.

Source

pub fn allocate_from_array( &self, bytes: [u8; N], ) -> Option<SecretPoolSlot<'_, N, SLOTS>>

Allocate a slot, copy an owned array into it, then clear the input array with the crate’s volatile wipe backend.

Source

pub fn allocate_from_fn( &self, make_byte: impl FnMut(usize) -> u8, ) -> Option<SecretPoolSlot<'_, N, SLOTS>>

Allocate a slot and generate each byte directly inside it.

Source

pub fn try_allocate_from_fn<E>( &self, make_byte: impl FnMut(usize) -> Result<u8, E>, ) -> Result<Option<SecretPoolSlot<'_, N, SLOTS>>, E>

Allocate a slot and fallibly generate each byte directly inside it.

If generation fails, the partially initialized slot is volatile-cleared and returned to the pool before the error is returned.

Source

pub fn secure_clear(&mut self)

Clear the full locked mapping and mark every slot available.

This requires &mut self, so Rust prevents it while any live slot handle still borrows the pool.

Source

pub fn secure_clear_and_flush(&mut self)

Trait Implementations§

Source§

impl<const N: usize, const SLOTS: usize> Debug for SecretPool<N, SLOTS>

Source§

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

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

impl<const N: usize, const SLOTS: usize> Drop for SecretPool<N, SLOTS>

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<const N: usize, const SLOTS: usize> SecureSanitize for SecretPool<N, SLOTS>

Source§

fn secure_sanitize(&mut self)

Clear the sensitive bytes owned by this value.
Source§

impl<const N: usize, const SLOTS: usize> Send for SecretPool<N, SLOTS>

Source§

impl<const N: usize, const SLOTS: usize> Sync for SecretPool<N, SLOTS>

Auto Trait Implementations§

§

impl<const N: usize, const SLOTS: usize> !Freeze for SecretPool<N, SLOTS>

§

impl<const N: usize, const SLOTS: usize> RefUnwindSafe for SecretPool<N, SLOTS>

§

impl<const N: usize, const SLOTS: usize> Unpin for SecretPool<N, SLOTS>

§

impl<const N: usize, const SLOTS: usize> UnsafeUnpin for SecretPool<N, SLOTS>

§

impl<const N: usize, const SLOTS: usize> UnwindSafe for SecretPool<N, SLOTS>

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, 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.