Skip to main content

Crate grain_128aeadv2

Crate grain_128aeadv2 

Source
Expand description

§Quickstart

Basic usage (with default vec feature):

If you don’t want to use the module to generate the keys :

use grain_128aeadv2::{
    Grain128, Key, Nonce,
    aead::{KeyInit, AeadCore}
};

// PLEASE use a RANDOM key/nonce (don't copy-paste this...)
let key = [12, 33, 91, 88, 1, 0, 132, 11, 231, 28, 1, 3, 5, 1, 5, 1];
let nonce = [91, 88, 1, 0, 132, 11, 231, 1, 23, 32, 22, 33];
let cipher = Grain128::new(&key.into());

let (ciphertext, tag) = cipher.encrypt_aead(
    &nonce.into(),
    b"Some additionnal data",
    b"this is a secret message"
);

let plaintext = cipher.decrypt_aead(
    &nonce.into(),
    b"Some additionnal data",
    &ciphertext,
    &tag
).expect("Tag verification failed");

assert_eq!(&plaintext, b"this is a secret message"); 

With randomly sampled keys and nonces (requires getrandom feature):

use grain_128aeadv2::{Grain128, aead::{Aead, AeadCore, KeyInit}};

let key = Grain128::generate_key().expect("Unable to generate key");
let cipher = Grain128::new(&key);

// A nonce must be USED ONLY ONCE !
let nonce = Grain128::generate_nonce().expect("Unable to generate nonce");
let (ciphertext, tag) = cipher.encrypt_aead(
    &nonce,
    b"Some additionnal data",
    b"this is a secret message"
);

let plaintext = cipher.decrypt_aead(
    &nonce,
    b"Some additionnal data",
    &ciphertext,
    &tag
).expect("Tag verification failed");

assert_eq!(&plaintext, b"this is a secret message"); 

§In-place encryption (arrayvec or alloc)

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). Enabling the alloc feature of this crate will provide an impl of aead::Buffer for Vec.

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

use grain_128aeadv2::{
    Grain128, Key, Nonce,
    aead::{AeadCore, AeadInOut, KeyInit, arrayvec::ArrayVec}
};

let key = Grain128::generate_key().expect("Unable to generate key");
let cipher = Grain128::new(&key);

// A nonce must be USED ONLY ONCE !
let nonce = Grain128::generate_nonce().expect("Unable to generate nonce");
// Take care : 8 bytes overhead to store the tag
let mut buffer: ArrayVec<u8, 24> = ArrayVec::new();
buffer.try_extend_from_slice(b"a secret message").unwrap();

// Perform in place encryption inside 'buffer'
cipher.encrypt_in_place(&nonce, b"Some AD", &mut buffer).expect("Unable to encrypt");

// Perform in place decryption
cipher.decrypt_in_place(&nonce, b"Some AD", &mut buffer).expect("Tag verification failed");

assert_eq!(buffer.as_ref(), b"a secret message");
use grain_128aeadv2::{
    Grain128, Key, Nonce,
    aead::{AeadCore, AeadInOut, KeyInit, arrayvec::ArrayVec}
};

let key = Grain128::generate_key().expect("Unable to generate key");
let cipher = Grain128::new(&key);

// A nonce must be USED ONLY ONCE !
let nonce = Grain128::generate_nonce().expect("Unable to generate nonce");
// Take care : 8 bytes overhead to store the tag
let mut buffer: Vec<u8> = vec![];
buffer.extend_from_slice(b"a secret message");

// Perform in place encryption inside 'buffer'
cipher.encrypt_in_place(&nonce, b"Some AD", &mut buffer).expect("Unable to encrypt");

// Perform in place decryption
cipher.decrypt_in_place(&nonce, b"Some AD", &mut buffer).expect("Tag verification failed");

assert_eq!(&buffer, b"a secret message");

Re-exports§

pub use aead;
pub use zeroize;zeroize

Structs§

Array
Array is a newtype for an inner [T; N] array where N is determined by a generic ArraySize parameter, which is a marker trait for a numeric value determined by ZSTs that impl the typenum::Unsigned trait.
Error
Error type.
Grain128
Grain-128AEADv2 cipher.
InOutBuf
Custom slice type which references one immutable (input) slice and one mutable (output) slice of equal length. Input and output slices are either the same or do not overlap.

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.
Buffer
In-place encryption/decryption byte buffers.
KeyInit
Types which can be initialized from key.
KeySizeUser
Types which use key for initialization.

Type Aliases§

Key
Key used by KeySizeUser implementors.
Nonce
Nonce: single-use value for ensuring ciphertexts are unique
Tag
Tag: authentication code which ensures ciphertexts are authentic
U1
U8
U12
U16