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>
impl<const ROUNDS: usize> Context<ROUNDS>
Sourcepub fn new(key: &[u8], nonce: &[u8; 12]) -> Self
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);
Sourcepub fn add_data(&mut self, aad: &[u8])
pub fn add_data(&mut self, aad: &[u8])
Add Authenticated Data to the MAC Context
This can be called multiple times
Sourcepub fn to_encryption(self) -> ContextEncryption<ROUNDS>
pub fn to_encryption(self) -> ContextEncryption<ROUNDS>
Finish authenticated part and move to the encryption phase
Sourcepub fn to_decryption(self) -> ContextDecryption<ROUNDS>
pub fn to_decryption(self) -> ContextDecryption<ROUNDS>
Finish authenticated part and move to the decryption phase
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more