Struct SecureArray

Source
pub struct SecureArray<T, const LENGTH: usize>
where T: Zeroize,
{ /* private fields */ }
Expand description

A fixed-size array allocated in a secure memory region.

SecureArray provides the same core security guarantees as the other types in this crate, including zeroization on drop and optional memory locking/encryption when

It is ideal for secrets of a known, fixed length.

§Program Termination

Direct indexing (e.g., array[0]) on a locked array will cause the operating system to terminate the process with an access violation error. Always use the provided scope methods (unlocked_scope, unlocked_mut_scope) for safe access.

§Examples

use secure_types::SecureArray;

let key_data = [1u8; 32];
let secure_key: SecureArray<u8, 32> = SecureArray::new(key_data).unwrap();

secure_key.unlocked_scope(|unlocked_slice| {
    assert_eq!(unlocked_slice.len(), 32);
    assert_eq!(unlocked_slice[0], 1);
});

Implementations§

Source§

impl<T, const LENGTH: usize> SecureArray<T, LENGTH>
where T: Zeroize,

Source

pub fn empty() -> Result<Self, Error>

Creates an empty (but allocated) SecureArray. The memory is allocated but not initialized, and it’s the caller’s responsibility to fill it.

Source

pub fn new(content: [T; LENGTH]) -> Result<Self, Error>

Creates a new SecureArray from a given array.

Source

pub fn len(&self) -> usize

Source

pub fn as_ptr(&self) -> *const T

Source

pub fn as_mut_ptr(&mut self) -> *mut u8

Source

pub fn unlocked_scope<F, R>(&self, f: F) -> R
where F: FnOnce(&[T]) -> R,

Provides scoped, immutable access to the array’s data as a slice.

Source

pub fn unlocked_mut_scope<F, R>(&mut self, f: F) -> R
where F: FnOnce(&mut [T]) -> R,

Provides scoped, mutable access to the array’s data as a mutable slice.

Source

pub fn erase(&mut self)

Securely erases the contents of the array by zeroizing the memory.

Trait Implementations§

Source§

impl<T: Clone + Zeroize, const LENGTH: usize> Clone for SecureArray<T, LENGTH>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Zeroize, const LENGTH: usize> Drop for SecureArray<T, LENGTH>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Zeroize, const LENGTH: usize> Index<usize> for SecureArray<T, LENGTH>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T: Zeroize, const LENGTH: usize> IndexMut<usize> for SecureArray<T, LENGTH>

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T, const LENGTH: usize> TryFrom<[T; LENGTH]> for SecureArray<T, LENGTH>
where T: Zeroize,

Source§

type Error = Error

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

fn try_from(s: [T; LENGTH]) -> Result<Self, Error>

Performs the conversion.
Source§

impl<T: Zeroize + Send, const LENGTH: usize> Send for SecureArray<T, LENGTH>

Source§

impl<T: Zeroize + Send + Sync, const LENGTH: usize> Sync for SecureArray<T, LENGTH>

Auto Trait Implementations§

§

impl<T, const LENGTH: usize> Freeze for SecureArray<T, LENGTH>

§

impl<T, const LENGTH: usize> RefUnwindSafe for SecureArray<T, LENGTH>
where T: RefUnwindSafe,

§

impl<T, const LENGTH: usize> Unpin for SecureArray<T, LENGTH>
where T: Unpin,

§

impl<T, const LENGTH: usize> UnwindSafe for SecureArray<T, LENGTH>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.