Struct cryptoxide::chacha20poly1305::ChaChaPoly1305
source · pub struct ChaChaPoly1305<const ROUNDS: usize> { /* private fields */ }
Expand description
A ChaCha20+Poly1305 Context
Implementations§
source§impl<const ROUNDS: usize> ChaChaPoly1305<ROUNDS>
impl<const ROUNDS: usize> ChaChaPoly1305<ROUNDS>
sourcepub fn new(key: &[u8], nonce: &[u8], aad: &[u8]) -> Self
pub fn new(key: &[u8], nonce: &[u8], aad: &[u8]) -> Self
Create a new ChaCha20Poly1305
- key needs to be 16 or 32 bytes
- nonce needs to be 8 or 12 bytes
sourcepub fn encrypt(&mut self, input: &[u8], output: &mut [u8], out_tag: &mut [u8])
pub fn encrypt(&mut self, input: &[u8], output: &mut [u8], out_tag: &mut [u8])
Encrypt input buffer to output buffer, and write an authenticated tag to out_tag.
Output buffer need to be the same size as the input buffer Out_tag mutable slice need to 16 bytes exactly.
Example: Encrypt a simple “hello world” message with chacha20poly1305 AEAD using a 64 bits nonce and a 128 bits keys, and arrange the output data in the format : ENCRYPTED_MSG | AEAD_TAG
use cryptoxide::chacha20poly1305::ChaCha20Poly1305;
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 aad : [u8; 0] = [];
let input : &[u8; 12] = b"hello world!";
let mut out : [u8; 12+16] = [0u8; 12+16];
let mut tag : [u8; 16] = [0u8; 16];
// create a new cipher
let mut cipher = ChaCha20Poly1305::new(&key, &nonce, &aad);
// encrypt the msg and append the tag at the end
cipher.encrypt(input, &mut out[0..12], &mut tag);
out[12..].copy_from_slice(&tag);
sourcepub fn decrypt(&mut self, input: &[u8], output: &mut [u8], tag: &[u8]) -> bool
pub fn decrypt(&mut self, input: &[u8], output: &mut [u8], tag: &[u8]) -> bool
Decrypt the input to the output buffer
if the calculated tag during decryption doesn’t match the tag in parameter, then the function return False
Example: Decrypt a simple message with chacha20poly1305 AEAD using a 64 bits nonce and a 128 bits keys where the first 12 bytes are the encrypted message and the tag is the last 16 bytes. if the cipher message has been tempered, a panic is raised (in the example):
use cryptoxide::chacha20poly1305::ChaCha20Poly1305;
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 aad : [u8; 0] = [];
let ae_msg : [u8; 12+16] = [98, 155, 81, 205, 163, 244, 162, 254, 57, 96, 183,
101, 167, 88, 238, 184, 17, 109, 89, 185, 72, 150,
97, 95, 149, 82, 179, 220];
let mut decrypt_msg : [u8; 12] = [0u8; 12];
// create a new cipher
let mut cipher = ChaCha20Poly1305::new(&key, &nonce, &aad);
// encrypt the msg and append the tag at the end
if !cipher.decrypt(&ae_msg[0..12], &mut decrypt_msg, &ae_msg[12..]) {
panic!("encrypted message has been tempered")
}
assert_eq!(&decrypt_msg, b"hello world!");
Trait Implementations§
source§impl<const ROUNDS: usize> Clone for ChaChaPoly1305<ROUNDS>
impl<const ROUNDS: usize> Clone for ChaChaPoly1305<ROUNDS>
source§fn clone(&self) -> ChaChaPoly1305<ROUNDS>
fn clone(&self) -> ChaChaPoly1305<ROUNDS>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more