CryptBox

Struct CryptBox 

Source
pub struct CryptBox<T> { /* private fields */ }
Expand description

An “encrypted” box type, which encrypts the inner data

In particular:

  • a CryptBox<T> owns three entire memory pages (12 KiB on modern systems, BEWARE!)
  • the inner data is encrypted using Ascon128
  • the pages are protected using mprotect by the flag Prot::NoAccess
  • the memory will not be paged to the disk because of mlock

After creating a CryptBox<T> it is encrypted and can be decrypted to yield a PlainBox<T> instance. PlainBox<T> implements Deref and other traits which allow for deref coercion, but it does not implement any associated functions. To encrypt the underlying data again use CryptBox::encrypt. This approach eliminates unnecessary imports.

Both CryptBox<T> and PlainBox<T> implement Drop, which clears the memory they were using and sets it to zero, erasing all leftover data.

§Note

This is not an allocator, thus storing types like Vec<T> or String does not make any sense (the CryptBox<T> will just store the fat pointer part of such structs).

§Examples

Accessing data:

use insectbox::CryptBox;

let cb = CryptBox::new(b"I'm in a CryptBox :)".to_owned());
// at this point we can only decrypt such a box
let cb = cb.decrypt();
// because of deref coercion we can extract the data and do stuff with it
println!("{:?}", cb.as_ref());
// after finishing we can encrypt the data again
let cb = CryptBox::encrypt(cb);
// it will be dropped now, if this was the intended end of lifetime for cb
// we could have left it as a PlainBox<T>

Constructing from a constructor:

use insectbox::CryptBox;

fn make_arr<const N: usize>() -> [u8; N] {
    [0x69; N]
}

// will panic if size_of::<T>() + 16 > PAGE_SIZE
// AVOID large numbers
let cb = CryptBox::construct(make_arr::<32>);
let cb = cb.decrypt();

assert_eq!(&[0x69; 32], cb.as_slice());

Implementations§

Source§

impl<T> CryptBox<T>

Source

pub fn new(val: T) -> Self

Make a new CryptBox<T> by moving t into the memory owned by the box.

§Panics

This function panics if size_of::<T>() + 16 > PAGE_SIZE or the encryption fails, or if the memory allocation failed.

Source

pub fn construct<F: Fn() -> T>(f: F) -> Self

Make a new CryptBox<T> by calling f and assigning the result to the inner pointer.

§Panics

This function panics if size_of::<T>() + 16 > PAGE_SIZE or the encryption fails, or if the memory allocation failed.

Source

pub fn try_new(val: T) -> Option<CryptBox<T>>

Make a new CryptBox<T> by moving t into the memory owned by the box. Returns None on failure.

Source

pub fn try_construct<F: Fn() -> T>(f: F) -> Option<CryptBox<T>>

Make a new CryptBox<T> by calling f and assigning the result to the inner pointer. Returns None on failure.

Source

pub fn decrypt(self) -> PlainBox<T>

Consumes self and returns an instance of PlainBox<T> which can be used to access the inner data.

§Panics

This will panic if there was a decryption failure. The allocation will be freed, unless it was the underlying memory which caused the failure.

Source

pub fn encrypt(s: PlainBox<T>) -> CryptBox<T>

Takes ownership of a PlainBox<T> and encrypts it again as a CryptBox<T>.

Trait Implementations§

Source§

impl<T: Debug> Debug for CryptBox<T>

Source§

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

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

impl<T> Drop for CryptBox<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for CryptBox<T>

§

impl<T> RefUnwindSafe for CryptBox<T>
where T: RefUnwindSafe,

§

impl<T> !Send for CryptBox<T>

§

impl<T> !Sync for CryptBox<T>

§

impl<T> Unpin for CryptBox<T>
where T: Unpin,

§

impl<T> UnwindSafe for CryptBox<T>
where T: 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V