Struct cryptoxide::chacha20poly1305::ChaCha20Poly1305[][src]

pub struct ChaCha20Poly1305 { /* fields omitted */ }
Expand description

A ChaCha20+Poly1305 Context

Implementations

Create a new ChaCha20Poly1305

  • key needs to be 16 or 32 bytes
  • nonce needs to be 8 or 12 bytes

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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.