Skip to main content

Crate mc_oblivious_aes_gcm

Crate mc_oblivious_aes_gcm 

Source
Expand description

§MobileCoin: Oblivious AES-GCM

WARNING: You should use the aes-gcm crate, not this one. This crate is a patch/fork of the execellent RustCrypto crate to support a very, very niche use-case for MobileCoin, and as such it’s maintenance and security are necessarily going to lag behind that of RustCrypto’s crate.

§Usage

Simple usage (allocating, no associated data):

// NOTE: requires the `getrandom` feature is enabled

use mc_oblivious_aes_gcm::{
    aead::{Aead, AeadCore, Generate, Key, KeyInit},
    Aes256Gcm, Nonce, // Or `Aes128Gcm`
};

let key = Key::<Aes256Gcm>::generate();
let cipher = Aes256Gcm::new(&key);

let nonce = Nonce::generate(); // MUST be unique per message
let ciphertext = cipher.encrypt(&nonce, b"plaintext message".as_ref())?;

let plaintext = cipher.decrypt(&nonce, ciphertext.as_ref())?;
assert_eq!(&plaintext, b"plaintext message");

§In-place Usage (eliminates alloc requirement)

This crate has an optional alloc feature which can be disabled in e.g. microcontroller environments that don’t have a heap.

The AeadInOut::encrypt_in_place and AeadInOut::decrypt_in_place methods accept any type that impls the aead::Buffer trait which contains the plaintext for encryption or ciphertext for decryption.

Enabling the arrayvec feature of this crate will provide an impl of aead::Buffer for arrayvec::ArrayVec (re-exported from the aead crate as aead::arrayvec::ArrayVec), and enabling the bytes feature of this crate will provide an impl of aead::Buffer for bytes::BytesMut (re-exported from the aead crate as aead::bytes::BytesMut).

It can then be passed as the buffer parameter to the in-place encrypt and decrypt methods:

// NOTE: requires the `arrayvec` and `getrandom` features are enabled

use mc_oblivious_aes_gcm::{
    aead::{AeadCore, AeadInOut, Generate, Key, KeyInit, arrayvec::ArrayVec},
    Aes256Gcm, Nonce, // Or `Aes128Gcm`
};

let key = Key::<Aes256Gcm>::generate();
let cipher = Aes256Gcm::new(&key);

let nonce = Nonce::generate(); // MUST be unique per message
let mut buffer: ArrayVec<u8, 128> = ArrayVec::new(); // Note: buffer needs 16-bytes overhead for auth tag
buffer.try_extend_from_slice(b"plaintext message").unwrap();

// Encrypt `buffer` in-place, replacing the plaintext contents with ciphertext
cipher.encrypt_in_place(&nonce, b"", &mut buffer)?;

// `buffer` now contains the message ciphertext
assert_ne!(buffer.as_ref(), b"plaintext message");

// Decrypt `buffer` in-place, replacing its ciphertext context with the original plaintext
cipher.decrypt_in_place(&nonce, b"", &mut buffer)?;
assert_eq!(buffer.as_ref(), b"plaintext message");

Re-exports§

pub use self;
pub use aes;aes

Structs§

AesGcm
AES-GCM: generic over an underlying AES implementation and nonce size.
CtDecryptResultalloc and zeroize
A new-type wrapper around choice with the #[must_use] annotation that Result has. This wraps the value Choice::from(true) when decryption succeeded, and Choice::from(false) otherwise.
Error
Error type.

Constants§

A_MAX
Maximum length of associated data in bytes.
P_MAX
Maximum length of plaintext in bytes.

Traits§

AeadCore
Authenticated Encryption with Associated Data (AEAD) algorithm.
AeadInOut
In-place and inout AEAD trait which handles the authentication tag as a return value/separate parameter.
CtAeadDecryptalloc and zeroize
API for Aead in-place decryption which is constant-time with respect to the mac check failing
KeyInit
Types which can be initialized from a key.
KeySizeUser
Types which use key for initialization.
TagSize
Trait implemented for valid tag sizes, i.e. [U12][consts::U12], [U13][consts::U13], [U14][consts::U14], [U15][consts::U15] and [U16][consts::U16]. When the crate feature hazmat is enabled, [U4][consts::U4] and [U8][consts::U8] are also supported.

Type Aliases§

Aes128Gcmaes
AES-GCM with a 128-bit key and 96-bit nonce.
Aes256Gcmaes
AES-GCM with a 256-bit key and 96-bit nonce.
Key
Key used by KeySizeUser implementors.
Nonce
AES-GCM nonces.
Tag
AES-GCM tags.