Struct Context

Source
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; 12] = [1,2,3,4,5,6,7,8,9,10,11,12];
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; 12]) -> 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; 12] = [1,2,3,4,5,6,7,8,9,10,11,12];
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 duplicate 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> Freeze for Context<ROUNDS>

§

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 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

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 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.