Module orion::hazardous::chacha20[][src]

IETF ChaCha20 as specified in the RFC 8439.

Parameters:

  • key: The secret key
  • nonce: The nonce value
  • initial_counter: The initial counter value. In most cases this is 0
  • ciphertext: The encrypted data
  • plaintext: The data to be encrypted
  • dst_out: Destination array that will hold the ciphertext/plaintext after encryption/decryption

See RFC for more information.

Exceptions:

An exception will be thrown if:

  • The length of the key is not 32 bytes
  • The length of the nonce is not 12 bytes
  • The length of dst_out is less than plaintext or ciphertext
  • plaintext or ciphertext are empty
  • plaintext or ciphertext are longer than (2^32)-2
  • The initial_counter is high enough to cause a potential overflow

Even though dst_out is allowed to be of greater length than plaintext, the ciphertext produced by ChaCha20 will always be of the same length as the plaintext.

Security:

It is critical for security that a given nonce is not re-used with a given key. Should this happen, the security of all data that has been encrypted with that given key is compromised.

Functions encrypt and decrypt do not provide any data integrity. If you need data integrity, you should be using a ChaCha20_Poly1305 construct instead. See RFC for more information.

Example:

use orion::hazardous::chacha20;
use orion::utilities::util;

let mut dst_out_pt = [0u8; 15];
let mut dst_out_ct = [0u8; 15];
let mut key = [0u8; 32];
let mut nonce = [0u8; 12];
let message = "Data to protect".as_bytes();

util::gen_rand_key(&mut key).unwrap();
util::gen_rand_key(&mut nonce).unwrap();


chacha20::encrypt(&key, &nonce, 0, message, &mut dst_out_ct);

chacha20::decrypt(&key, &nonce, 0, &dst_out_ct, &mut dst_out_pt);

assert_eq!(dst_out_pt, message);

Functions

decrypt

The ChaCha20 decryption function.

encrypt

The ChaCha20 encryption function.