Expand description

ChaCha20Poly1305 is an authenticated symmetric stream cipher based on chacha20 and poly1305.

the specification of chacha20poly1305 is available at RFC8439 and it follows general principle related to AEAD.

This module provides 2 interfaces:

The incremental interfaces should be used when you are streaming data or that you need more control over the memory usage, as the one-shot interface expects one single call with slices parameter.

Examples

Encrypting using the one-shot interface:

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);

Encrypting using the incremental interfaces:

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);

Structs

A ChaCha20+Poly1305 Context
Chacha20Poly1305 Incremental Context for Authenticated Data (AAD)
ChaCha20Poly1305 Incremental Context for decryption
ChaCha20Poly1305 Incremental Context for encryption
ChaCha20Poly1305 Authenticated Tag (128 bits)

Enums

Whether or not, the decryption was succesful related to the expected tag

Type Definitions