pub trait Aead {
type KeyLength: ArrayLength<u8>;
type NonceLength: ArrayLength<u8>;
type TagLength: ArrayLength<u8>;
const NAME: &'static str;
const KEY_LENGTH: usize = <Self::KeyLength as Unsigned>::USIZE;
const NONCE_LENGTH: usize = <Self::NonceLength as Unsigned>::USIZE;
const TAG_LENGTH: usize = <Self::TagLength as Unsigned>::USIZE;
fn encrypt(
key: &Key<Self>,
nonce: &Nonce<Self>,
associated_data: &[u8],
plaintext: &[u8],
ciphertext: &mut [u8],
tag: &mut Tag<Self>
) -> Result<()>;
fn decrypt(
key: &Key<Self>,
nonce: &Nonce<Self>,
associated_data: &[u8],
plaintext: &mut [u8],
ciphertext: &[u8],
tag: &Tag<Self>
) -> Result<usize>;
fn try_encrypt(
key: &[u8],
nonce: &[u8],
associated_data: &[u8],
plaintext: &[u8],
ciphertext: &mut [u8],
tag: &mut [u8]
) -> Result<()> { ... }
fn try_decrypt(
key: &[u8],
nonce: &[u8],
associated_data: &[u8],
plaintext: &mut [u8],
ciphertext: &[u8],
tag: &[u8]
) -> Result<usize> { ... }
fn random_nonce() -> Result<Nonce<Self>> { ... }
fn padsize(_: &[u8]) -> Option<NonZeroUsize> { ... }
}
Expand description
A common interface for AEAD encryption algorithms.
Example using [Aes256Gcm
][crate::ciphers::aes::Aes256Gcm
]:
use crypto::ciphers::{
aes_gcm::Aes256Gcm,
traits::{Aead, Key, Nonce, Tag},
};
let plaintext: &[u8] = b"crypto.rs";
let associated_data: &[u8] = b"stronghodl";
let mut encrypted: Vec<u8> = vec![0; plaintext.len()];
let mut decrypted: Vec<u8> = vec![0; encrypted.len()];
let mut tag: Vec<u8> = vec![0; Aes256Gcm::TAG_LENGTH];
let key: Key<Aes256Gcm> = Default::default();
let nonce: Nonce<Aes256Gcm> = Aes256Gcm::random_nonce()?;
Aes256Gcm::try_encrypt(&key, &nonce, associated_data, plaintext, &mut encrypted, &mut tag)?;
Aes256Gcm::try_decrypt(&key, &nonce, associated_data, &mut decrypted, &encrypted, &tag)?;
assert_eq!(decrypted, plaintext);
Required Associated Types§
sourcetype KeyLength: ArrayLength<u8>
type KeyLength: ArrayLength<u8>
The size of the key
required by this algorithm.
sourcetype NonceLength: ArrayLength<u8>
type NonceLength: ArrayLength<u8>
The size of the nonce
required by this algorithm.
sourcetype TagLength: ArrayLength<u8>
type TagLength: ArrayLength<u8>
The size of the tag
produced by this algorithm.
Required Associated Constants§
Provided Associated Constants§
sourceconst KEY_LENGTH: usize = <Self::KeyLength as Unsigned>::USIZE
const KEY_LENGTH: usize = <Self::KeyLength as Unsigned>::USIZE
A const version of Aead::KeyLength
.
sourceconst NONCE_LENGTH: usize = <Self::NonceLength as Unsigned>::USIZE
const NONCE_LENGTH: usize = <Self::NonceLength as Unsigned>::USIZE
A const version of Aead::NonceLength
.
sourceconst TAG_LENGTH: usize = <Self::TagLength as Unsigned>::USIZE
const TAG_LENGTH: usize = <Self::TagLength as Unsigned>::USIZE
A const version of Aead::TagLength
.
Required Methods§
Provided Methods§
sourcefn try_encrypt(
key: &[u8],
nonce: &[u8],
associated_data: &[u8],
plaintext: &[u8],
ciphertext: &mut [u8],
tag: &mut [u8]
) -> Result<()>
fn try_encrypt(
key: &[u8],
nonce: &[u8],
associated_data: &[u8],
plaintext: &[u8],
ciphertext: &mut [u8],
tag: &mut [u8]
) -> Result<()>
Encrypt the given plaintext
using key
.
The output is written to ciphertext
.
This is a version of Aead::encrypt
with easier-to-use parameters.
sourcefn try_decrypt(
key: &[u8],
nonce: &[u8],
associated_data: &[u8],
plaintext: &mut [u8],
ciphertext: &[u8],
tag: &[u8]
) -> Result<usize>
fn try_decrypt(
key: &[u8],
nonce: &[u8],
associated_data: &[u8],
plaintext: &mut [u8],
ciphertext: &[u8],
tag: &[u8]
) -> Result<usize>
Decrypt the given ciphertext
using key
and tag
.
The output is written to the plaintext
buffer.
This is a version of Aead::decrypt
with easier-to-use parameters.
sourcefn random_nonce() -> Result<Nonce<Self>>
fn random_nonce() -> Result<Nonce<Self>>
Generates a random nonce with the correct size for this algorithm.
sourcefn padsize(_: &[u8]) -> Option<NonZeroUsize>
fn padsize(_: &[u8]) -> Option<NonZeroUsize>
Returns the size of the padding applied to the input buffer.
Note: This is not applicable to all implementations.