anon_buffer

Macro anon_buffer 

Source
macro_rules! anon_buffer {
    ($size:expr$(;)?) => { ... };
}
Expand description

Create a fixed-size hardened anonymous buffer.

As per the rationale presented in the mem module, it is often necessary to take extra measures to ensure that sensitive data stored in memory is not at risk of being revealed. This macro is like the “little sibling” of the hardened_buffer macro: It creates an anonymous fixed size array-like buffer with memory allocated using Sodium’s secure memory utilities, which is therefore suitable for storing secret values. This macro is most useful for storing sensitive intermediary values, where creating a whole new type with hardened_buffer seems redundant.

anon_buffer!(Size) will return an array-like type of length Size backed by hardened memory. The new type will implement the following traits:

The new type will also implement the methods zero (which sets all the bytes in the buffer to zero in such a way that the compiler will not optimize away the operation), and try_clone (which attempts to clone the struct). The struct constant LENGTH will be set to $size.

§Examples

use alkali::mem::{anon_buffer, eq};

let mut buffer_a = anon_buffer!(32).unwrap();
buffer_a.copy_from_slice(b"Copy this data into that buffer.");
println!("{:x?}", &buffer_a[..]);

let mut buffer_b = anon_buffer!(16).unwrap();
buffer_b.copy_from_slice(b"Copy this data i");

assert!(eq(&buffer_a[..16], &buffer_b[..]).unwrap());