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§
Structs§
- Array
Arrayis a newtype for an inner[T; N]array whereNis determined by a genericArraySizeparameter, which is a marker trait for a numeric value determined by ZSTs that impl thetypenum::Unsignedtrait.- Error
- Error type.
- Grain128
- Grain-128AEADv2 cipher.
- InOut
Buf - 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§
- Aead
Core - Authenticated Encryption with Associated Data (AEAD) algorithm.
- Aead
InOut - 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.
- KeySize
User - Types which use key for initialization.