Trait crypto::ciphers::traits::Aead [−][src]
pub trait Aead { type KeyLength: ArrayLength<u8>; type NonceLength: ArrayLength<u8>; type TagLength: ArrayLength<u8>; const NAME: &'static str; const KEY_LENGTH: usize; const NONCE_LENGTH: usize; const TAG_LENGTH: 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> { ... } }
A common interface for AEAD encryption algorithms.
Example using Aes256Gcm
:
use crypto::ciphers::{ aes::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);
Associated Types
type KeyLength: ArrayLength<u8>
[src]
The size of the key
required by this algorithm.
type NonceLength: ArrayLength<u8>
[src]
The size of the nonce
required by this algorithm.
type TagLength: ArrayLength<u8>
[src]
The size of the tag
produced by this algorithm.
Associated Constants
const NAME: &'static str
[src]
A human-friendly identifier of this algorithm.
const KEY_LENGTH: usize
[src]
A const version of Aead::KeyLength
.
const NONCE_LENGTH: usize
[src]
A const version of Aead::NonceLength
.
const TAG_LENGTH: usize
[src]
A const version of Aead::TagLength
.
Required methods
fn encrypt(
key: &Key<Self>,
nonce: &Nonce<Self>,
associated_data: &[u8],
plaintext: &[u8],
ciphertext: &mut [u8],
tag: &mut Tag<Self>
) -> Result<()>
[src]
key: &Key<Self>,
nonce: &Nonce<Self>,
associated_data: &[u8],
plaintext: &[u8],
ciphertext: &mut [u8],
tag: &mut Tag<Self>
) -> Result<()>
Encrypt the given plaintext
using key
.
The output is written to the ciphertext
/tag
buffers.
fn decrypt(
key: &Key<Self>,
nonce: &Nonce<Self>,
associated_data: &[u8],
plaintext: &mut [u8],
ciphertext: &[u8],
tag: &Tag<Self>
) -> Result<usize>
[src]
key: &Key<Self>,
nonce: &Nonce<Self>,
associated_data: &[u8],
plaintext: &mut [u8],
ciphertext: &[u8],
tag: &Tag<Self>
) -> Result<usize>
Decrypt the given ciphertext
using key
and tag
.
The output is written to the plaintext
buffer.
Provided methods
fn try_encrypt(
key: &[u8],
nonce: &[u8],
associated_data: &[u8],
plaintext: &[u8],
ciphertext: &mut [u8],
tag: &mut [u8]
) -> Result<()>
[src]
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.
fn try_decrypt(
key: &[u8],
nonce: &[u8],
associated_data: &[u8],
plaintext: &mut [u8],
ciphertext: &[u8],
tag: &[u8]
) -> Result<usize>
[src]
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.
fn random_nonce() -> Result<Nonce<Self>>
[src]
Generates a random nonce with the correct size for this algorithm.
fn padsize(_: &[u8]) -> Option<NonZeroUsize>
[src]
Returns the size of the padding applied to the input buffer.
Note: This is not applicable to all implementations.
Implementors
impl Aead for Aes256Gcm
[src]
impl Aead for Aes256Gcm
[src]aes
only.type KeyLength = U32
type NonceLength = U12
type TagLength = U16
const NAME: &'static str
[src]
fn encrypt(
key: &Key<Self>,
nonce: &Nonce<Self>,
associated_data: &[u8],
plaintext: &[u8],
ciphertext: &mut [u8],
tag: &mut Tag<Self>
) -> Result<()>
[src]
key: &Key<Self>,
nonce: &Nonce<Self>,
associated_data: &[u8],
plaintext: &[u8],
ciphertext: &mut [u8],
tag: &mut Tag<Self>
) -> Result<()>
Warning: type conversions on the tag type can be tricky. instead of &mut tag.try_into().unwrap()
use
(&mut tag).try_into().unwrap()
fn decrypt(
key: &Key<Self>,
nonce: &Nonce<Self>,
associated_data: &[u8],
plaintext: &mut [u8],
ciphertext: &[u8],
tag: &Tag<Self>
) -> Result<usize>
[src]
key: &Key<Self>,
nonce: &Nonce<Self>,
associated_data: &[u8],
plaintext: &mut [u8],
ciphertext: &[u8],
tag: &Tag<Self>
) -> Result<usize>
impl Aead for XChaCha20Poly1305
[src]
impl Aead for XChaCha20Poly1305
[src]chacha
only.type KeyLength = U32
type NonceLength = U24
type TagLength = U16
const NAME: &'static str
[src]
fn encrypt(
key: &Key<Self>,
nonce: &Nonce<Self>,
associated_data: &[u8],
plaintext: &[u8],
ciphertext: &mut [u8],
tag: &mut Tag<Self>
) -> Result<()>
[src]
key: &Key<Self>,
nonce: &Nonce<Self>,
associated_data: &[u8],
plaintext: &[u8],
ciphertext: &mut [u8],
tag: &mut Tag<Self>
) -> Result<()>
Warning: type conversions on the tag type can be tricky. instead of &mut tag.try_into().unwrap()
use
(&mut tag).try_into().unwrap()
fn decrypt(
key: &Key<Self>,
nonce: &Nonce<Self>,
associated_data: &[u8],
plaintext: &mut [u8],
ciphertext: &[u8],
tag: &Tag<Self>
) -> Result<usize>
[src]
key: &Key<Self>,
nonce: &Nonce<Self>,
associated_data: &[u8],
plaintext: &mut [u8],
ciphertext: &[u8],
tag: &Tag<Self>
) -> Result<usize>