secmem-alloc License: MIT OR Apache-2.0 secmem-alloc on crates.io Source Code Repository

secmem-alloc is a crate designed allocate private/secret memory. It is intended to be used for storing cryptographic secrets in memory. This crate provides custom allocators using various techniques to improve secrecy of the memory, most notably zeroization on deallocation.

Examples

For example, we read in a secret password from standard-in, which we want to zeroize on drop (deallocation). Note that this code does leave the password visible on the prompt; it is only to give an idea of how to use this crate.

```rust

![feature(allocator_api)]

// requires nightly_allocator_api crate feature to be enabled and a nightly compiler use secmemalloc::allocatorapi::Allocator; use secmemalloc::zeroizingalloc::ZeroizeAlloc; use std::alloc::Global;

fn read_password(buf: &mut Vec) { // query password from the user and put it in buf }

fn main() { println!("Please enter your password: "); let mut stdin = std::io::stdin(); let allocator = ZeroizeAlloc::new(Global); let mut password = Vec::newin(allocator); readpassword(&mut password);

// use `password` however you like
// you can even grow and shrink the vector `password` and if it needs to be reallocated, the
// old allocation is immediately zeroized

// password is automatically zeroized on drop (deallocation)

} ```

As a second example assume you have a cryptographic secret key of 256 bytes, which should be zeroized on drop. In addition, we don’t want the key to be written to swap.

``rust // requires no crate features and works on stable // if you enable thenightlyallocatorapi` crate feature, the following line is necessary

![feature(allocator_api)]

use secmemalloc::allocatorapi::Allocator; use secmemalloc::boxed::Box; use secmemalloc::sec_alloc::SecStackSinglePageAlloc;

fn getsecretkey(buf: &mut Box<[u8; 256], A>) { // fill buf with the bytes of the secret key }

fn main() { let allocator: SecStackSinglePageAlloc = SecStackSinglePageAlloc::new().expect("could not create allocator"); let mut key = Box::newin([0u8; 256], &allocator); getsecretkey(&mut key);

// use `key` however you like
// `key` will not be written to swap except possibly on hibernation

// `key` is automatically zeroized on drop (deallocation)

} ```

Cargo features

TODOs

Changelog

See CHANGELOG.md.

Documentation

The API documentation of secmem-alloc is available at https://docs.rs/secmem-alloc/*/secmem_alloc/.