Skip to main content

Module crypto_aead_chacha20poly1305_ietf

Module crypto_aead_chacha20poly1305_ietf 

Source
Expand description

§ChaCha20-Poly1305-IETF authenticated encryption

Implements libsodium’s crypto_aead_chacha20poly1305_ietf_* functions. This construction authenticates optional additional data, appends the authentication tag in combined mode, and uses 96-bit public nonces as specified by RFC 8439. This is not the legacy 64-bit-nonce construction.

§Classic API example

use dryoc::classic::crypto_aead_chacha20poly1305_ietf::*;
use dryoc::constants::{
    CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES, CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES,
};
use dryoc::types::*;

let key = crypto_aead_chacha20poly1305_ietf_keygen();
// This 96-bit nonce must be unique for every message encrypted with `key`.
let nonce = [0u8; CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES];
let message =
    b"Our doubts are traitors, and make us lose the good we oft might win, by fearing to attempt.";
let aad = b"metadata";

let mut ciphertext = vec![0u8; message.len() + CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES];
crypto_aead_chacha20poly1305_ietf_encrypt(&mut ciphertext, message, Some(aad), &nonce, &key)
    .expect("encrypt failed");

let mut decrypted = vec![0u8; message.len()];
crypto_aead_chacha20poly1305_ietf_decrypt(&mut decrypted, &ciphertext, Some(aad), &nonce, &key)
    .expect("decrypt failed");

assert_eq!(message, decrypted.as_slice());

Functions§

crypto_aead_chacha20poly1305_ietf_decrypt
Decrypts ciphertext with nonce, key, and optional associated data.
crypto_aead_chacha20poly1305_ietf_decrypt_detached
Detached version of crypto_aead_chacha20poly1305_ietf_decrypt.
crypto_aead_chacha20poly1305_ietf_decrypt_detached_inplace
In-place detached variant of crypto_aead_chacha20poly1305_ietf_decrypt_detached.
crypto_aead_chacha20poly1305_ietf_decrypt_inplace
Decrypts data in place after verifying the appended authentication tag.
crypto_aead_chacha20poly1305_ietf_encrypt
Encrypts message with nonce, key, and optional associated data.
crypto_aead_chacha20poly1305_ietf_encrypt_detached
Detached version of crypto_aead_chacha20poly1305_ietf_encrypt.
crypto_aead_chacha20poly1305_ietf_encrypt_detached_inplace
In-place detached variant of crypto_aead_chacha20poly1305_ietf_encrypt_detached.
crypto_aead_chacha20poly1305_ietf_encrypt_inplace
Encrypts data in place and appends the authentication tag.
crypto_aead_chacha20poly1305_ietf_keygen
Generates a random key using copy_randombytes.
crypto_aead_chacha20poly1305_ietf_keygen_inplace
In-place variant of crypto_aead_chacha20poly1305_ietf_keygen.

Type Aliases§

Key
Secret key for ChaCha20-Poly1305-IETF AEAD.
Mac
Authentication tag for ChaCha20-Poly1305-IETF AEAD.
Nonce
Public nonce for ChaCha20-Poly1305-IETF AEAD.