pub struct Context<const ROUNDS: usize> { /* private fields */ }
Expand description

Chacha20Poly1305 Incremental Context for Authenticated Data (AAD)

The initial context set the key and nonce, and the authenticated data (if any), then it needs to converted either to a ContextEncryption or ContextDecryption using the Context::to_encryption or Context::to_decryption methods (respectively).

use cryptoxide::chacha20poly1305::Context;

let key : [u8; 16] = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
let nonce : [u8; 8] = [1,2,3,4,5,6,7,8];
let mut context = Context::<20>::new(&key, &nonce);

// Add incrementally 2 slices of data
context.add_data(b"authenticated");
context.add_data(b"data");

let mut encrypted_input = [0u8; 10+16];
let mut context = context.to_encryption();

// Encrypt incrementally 2 slices and append the encrypted data to the output buffer
context.encrypt(b"hello", &mut encrypted_input[0..5]);
context.encrypt(b"world", &mut encrypted_input[5..10]);

// Finalize the context, and append the tag to the end of the output buffer
let tag = context.finalize();
encrypted_input[10..26].copy_from_slice(&tag.0);

Implementations§

source§

impl<const ROUNDS: usize> Context<ROUNDS>

source

pub fn new(key: &[u8], nonce: &[u8]) -> Self

Create a new context given the key and nonce.

use cryptoxide::chacha20poly1305::Context;

let key : [u8; 16] = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
let nonce : [u8; 8] = [1,2,3,4,5,6,7,8];
let context = Context::<20>::new(&key, &nonce);
source

pub fn add_data(&mut self, aad: &[u8])

Add Authenticated Data to the MAC Context

This can be called multiple times

source

pub fn to_encryption(self) -> ContextEncryption<ROUNDS>

Finish authenticated part and move to the encryption phase

source

pub fn to_decryption(self) -> ContextDecryption<ROUNDS>

Finish authenticated part and move to the decryption phase

Trait Implementations§

source§

impl<const ROUNDS: usize> Clone for Context<ROUNDS>

source§

fn clone(&self) -> Context<ROUNDS>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<const ROUNDS: usize> RefUnwindSafe for Context<ROUNDS>

§

impl<const ROUNDS: usize> Send for Context<ROUNDS>

§

impl<const ROUNDS: usize> Sync for Context<ROUNDS>

§

impl<const ROUNDS: usize> Unpin for Context<ROUNDS>

§

impl<const ROUNDS: usize> UnwindSafe for Context<ROUNDS>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.