Skip to main content

ChaCha20Blake3

Struct ChaCha20Blake3 

Source
pub struct ChaCha20Blake3 { /* private fields */ }
Expand description

ChaCha20-BLAKE3 AEAD (encrypt-then-MAC).

The master key and nonce are fed through a BLAKE3-based KDF to derive a ChaCha20 encryption key, a ChaCha20 nonce, and an authentication key. The plaintext is encrypted with ChaCha20 and then MACed with BLAKE3(keyed, aad || len(aad) || ciphertext || len(ciphertext)).

§Parameters

  • Key: 256 bits (32 bytes)
  • Nonce: 256 bits (32 bytes)
  • Tag: 256 bits (32 bytes)

§Panics

encrypt_in_place and decrypt_in_place panic if the nonce is not exactly 32 bytes.

§Example

use crypto::{Aead, chacha::ChaCha20Blake3};

let key = [0xab; 32];
let nonce = [0xcd; 32];
let aad = b"associated data";
let plaintext = b"hello world";

let cipher = ChaCha20Blake3::new(&key);

let mut buf = plaintext.to_vec();
let tag = cipher.encrypt_in_place(&mut buf, &nonce, aad);

// buf now holds the ciphertext; tag is the 32-byte authentication tag.

cipher.decrypt_in_place(&mut buf, &nonce, aad, tag.as_ref())
    .expect("decryption failed");
assert_eq!(&buf, plaintext);

Implementations§

Source§

impl ChaCha20Blake3

Source

pub fn new(key: &[u8; 32]) -> Self

Trait Implementations§

Source§

impl Aead for ChaCha20Blake3

Source§

const TAG_SIZE: usize = 32

Source§

const NONCE_SIZE: usize = 32

Source§

fn encrypt_in_place(&self, in_out: &mut [u8], nonce: &[u8], aad: &[u8]) -> Hash

Source§

fn decrypt_in_place( &self, in_out: &mut [u8], nonce: &[u8], aad: &[u8], tag: &[u8], ) -> Result<(), AeadError>

Source§

fn encrypt(&self, plaintext: &[u8], nonce: &[u8], aad: &[u8]) -> Vec<u8>

Source§

fn decrypt( &self, ciphertext: &[u8], nonce: &[u8], aad: &[u8], ) -> Result<Vec<u8>, AeadError>

Source§

impl Drop for ChaCha20Blake3

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Zeroize for ChaCha20Blake3

Source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Zeroize for T
where T: Zeroize,