Skip to main content

ChaCha20Poly1305

Struct ChaCha20Poly1305 

Source
pub struct ChaCha20Poly1305;
Expand description

ChaCha20-Poly1305 AEAD per RFC 8439.

Stateless tag struct – the per-message state lives in the ChaCha20 / Poly1305 instances. Exposed as a unit struct so the API matches the AES-GCM Gcm struct in cipher::modes.

Implementations§

Source§

impl ChaCha20Poly1305

Source

pub fn encrypt( key: &[u8; 32], nonce: &[u8; 12], aad: &[u8], plaintext: &[u8], ) -> (Vec<u8>, [u8; 16])

Encrypt and authenticate a message.

Returns (ciphertext, tag) where ciphertext.len() == plaintext.len() and tag is exactly 16 bytes. Both must be transmitted to the receiver alongside the nonce and AAD.

Source

pub fn decrypt( key: &[u8; 32], nonce: &[u8; 12], aad: &[u8], ciphertext: &[u8], tag: &[u8; 16], ) -> Option<Vec<u8>>

Decrypt and verify a ciphertext.

Returns Some(plaintext) only if tag is the correct MAC for (aad, ciphertext) under (key, nonce). The tag is compared in constant time – the function execution time does not leak which byte of the tag was wrong.

Returns None if the tag does not verify. Callers MUST NOT use the returned plaintext if None is returned, and in particular must not log it, hash it, or branch on its contents – the only correct response to a bad tag is to abort the protocol.

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.