Trait crypto::ciphers::traits::Aead

source ·
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§

The size of the key required by this algorithm.

The size of the nonce required by this algorithm.

The size of the tag produced by this algorithm.

Required Associated Constants§

A human-friendly identifier of this algorithm.

Provided Associated Constants§

A const version of Aead::KeyLength.

A const version of Aead::NonceLength.

A const version of Aead::TagLength.

Required Methods§

Encrypt the given plaintext using key.

The output is written to the ciphertext/tag buffers.

Decrypt the given ciphertext using key and tag.

The output is written to the plaintext buffer.

Provided Methods§

Encrypt the given plaintext using key.

The output is written to ciphertext.

This is a version of Aead::encrypt with easier-to-use parameters.

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.

Generates a random nonce with the correct size for this algorithm.

Returns the size of the padding applied to the input buffer.

Note: This is not applicable to all implementations.

Implementors§