Skip to main content

GuardedMemory

Struct GuardedMemory 

Source
pub struct GuardedMemory { /* private fields */ }
Expand description

A guarded memory region that zeroizes on drop.

For enclave environments where sensitive data must be:

  1. Zeroized when no longer needed
  2. Tracked for lifetime management
  3. Protected from accidental copies
  4. Locked in RAM (when mlock feature is enabled)

Uses Box<[u8]> internally (not Vec<u8>) to guarantee the backing pointer is never invalidated by reallocation — critical for mlock safety.

§Example

use chains_sdk::security::GuardedMemory;

let mut guard = GuardedMemory::new(32);
guard.as_mut()[..4].copy_from_slice(&[0xDE, 0xAD, 0xBE, 0xEF]);
// memory is automatically zeroized (and munlocked) when `guard` is dropped

Implementations§

Source§

impl GuardedMemory

Source

pub fn new(size: usize) -> Self

Allocate a new guarded memory region of size bytes (zeroed).

When the mlock feature is enabled, the memory is locked into RAM to prevent the OS from swapping it to disk.

Source

pub fn from_vec(data: Vec<u8>) -> Self

Create from existing data (takes ownership, original is NOT zeroized).

Prefer this over copying into a new() buffer when you already have the data in a Vec<u8>.

Source§

impl GuardedMemory

Source

pub fn len(&self) -> usize

Length of the guarded region in bytes.

Source

pub fn is_empty(&self) -> bool

Whether the guarded region is empty.

Trait Implementations§

Source§

impl AsMut<[u8]> for GuardedMemory

Source§

fn as_mut(&mut self) -> &mut [u8]

Mutable access to the guarded bytes.

Source§

impl AsRef<[u8]> for GuardedMemory

Source§

fn as_ref(&self) -> &[u8]

Immutable access to the guarded bytes.

Source§

impl Debug for GuardedMemory

Source§

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

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

impl Drop for GuardedMemory

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

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> ToHex for T
where T: AsRef<[u8]>,

Source§

fn encode_hex<U>(&self) -> U
where U: FromIterator<char>,

Encode the hex strict representing self into the result. Lower case letters are used (e.g. f9b4ca)
Source§

fn encode_hex_upper<U>(&self) -> U
where U: FromIterator<char>,

Encode the hex strict representing self into the result. Upper case letters are used (e.g. F9B4CA)
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.