Skip to main content

Crate libcrux_chacha20poly1305

Crate libcrux_chacha20poly1305 

Source
Expand description

This crate implements the ChaCha20Poly1305 AEAD algorithm, as well as the extended nonce variant XChaCha20Poly1305.

To encrypt something using ChaCha20Poly1305, use:

use libcrux_chacha20poly1305::*;
use libcrux_traits::aead::typed_owned::Aead as _;

let msg: &[U8; MSG_LEN] = b"squeamish ossifrage".classify_ref();
let mut ciphertext = [0u8; MSG_LEN];
let mut tag = Tag::from([0u8.classify(); TAG_LEN]);

let key = Key::from(key_bytes);
let nonce = Nonce::from([123u8.classify(); NONCE_LEN]);

key.encrypt(&mut ciphertext, &mut tag, &nonce, &[/* no aad */], msg)
    .expect("Encryption error");

// Ciphertext and tag contain encrypted data
assert_eq!(
    ciphertext,
    [ 181, 223,  66, 115, 105, 181,  98, 178,
      247, 139, 196, 238, 169, 225, 143,  94,
      174, 123, 232 ]
);
assert_eq!(
    tag.as_ref().declassify_ref(),
    &[ 155, 112, 155, 212, 133, 38, 145, 115,
        27, 221, 245, 237, 125, 28,  22, 101 ]
);

and to decrypt, do

use libcrux_chacha20poly1305::*;
use libcrux_traits::aead::typed_owned::Aead as _;

let mut plaintext = [0u8.classify(); MSG_LEN];
let mut tag = Tag::from(tag_bytes);

let key = Key::from(key_bytes);
let nonce = Nonce::from([123u8.classify(); NONCE_LEN]);

key.decrypt(&mut plaintext, &nonce, &[/* no aad */], &ciphertext, &tag)
    .expect("Encryption error");

assert_eq!(plaintext.declassify_ref(), b"squeamish ossifrage");

Modules§

xchacha20_poly1305
Non-standard XChacha implementation https://datatracker.ietf.org/doc/html/draft-arciszewski-xchacha-03

Structs§

ChaCha20Poly1305
The ChaCha20Poly1305 AEAD algorithm.

Enums§

AeadError
Describes the error conditions of the ChaCha20-Poly1305 AEAD.
MacError
Describes the error conditions of the Poly1305 MAC.

Constants§

KEY_LEN
The length of ChaCha20-Poly1305 keys.
NONCE_LEN
The length of ChaCha20-Poly1305 nonces.
TAG_LEN
The length of Poly1305 MAC tags.

Functions§

decrypt
The ChaCha20-Poly1305 AEAD decryption function. Writes the result of the decryption to ptxt, and returns the slice of appropriate length.
decrypt_detached
The ChaCha20-Poly1305 AEAD decryption function. Writes the result of the decryption to ptxt, and returns the slice of appropriate length.
encrypt
The ChaCha20-Poly1305 AEAD encryption function. Writes the concatenation of the ciphertext produced by ChaCha20 and the MAC tag into ctxt and returns the two pieces separately.
encrypt_detached
The ChaCha20-Poly1305 AEAD encryption function with detached tag.

Type Aliases§

Key
An owned ChaCha20Poly1305 key.
KeyRef
A reference to a ChaCha20Poly1305 key.
Nonce
An owned ChaCha20Poly1305 nonce.
NonceRef
A reference to a ChaCha20Poly1305 nonce.
Tag
An owned ChaCha20Poly1305 tag.
TagMut
A mutable reference to a ChaCha20Poly1305 tag.
TagRef
A reference to a ChaCha20Poly1305 tag.