pub struct ChaChaPoly1305<const ROUNDS: usize> { /* private fields */ }
Expand description

A ChaCha20+Poly1305 Context

Implementations§

source§

impl<const ROUNDS: usize> ChaChaPoly1305<ROUNDS>

source

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
source

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

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>

source§

fn clone(&self) -> ChaChaPoly1305<ROUNDS>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<const ROUNDS: usize> RefUnwindSafe for ChaChaPoly1305<ROUNDS>

§

impl<const ROUNDS: usize> Send for ChaChaPoly1305<ROUNDS>

§

impl<const ROUNDS: usize> Sync for ChaChaPoly1305<ROUNDS>

§

impl<const ROUNDS: usize> Unpin for ChaChaPoly1305<ROUNDS>

§

impl<const ROUNDS: usize> UnwindSafe for ChaChaPoly1305<ROUNDS>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.