Struct cryptoxide::chacha20poly1305::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; 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>
impl<const ROUNDS: usize> Context<ROUNDS>
sourcepub fn new(key: &[u8], nonce: &[u8]) -> Self
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);
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