Skip to content

Commit

Permalink
update aead benches to use libcrux-chacha20poly1305 standalone crate
Browse files Browse the repository at this point in the history
  • Loading branch information
keks committed Feb 11, 2025
1 parent f91f5cd commit 6afaf9e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 14 deletions.
1 change: 1 addition & 0 deletions benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rand = { version = "0.8" }

[dev-dependencies]
libcrux = { path = "../", features = ["rand", "tests"] }
libcrux-chacha20poly1305 = { path = "../chacha20poly1305" }
libcrux-kem = { path = "../libcrux-kem", features = ["tests"] }
libcrux-ml-kem = { path = "../libcrux-ml-kem" }
rand_core = { version = "0.6" }
Expand Down
44 changes: 30 additions & 14 deletions benchmarks/benches/aead.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
use chacha20poly1305::{AeadCore, AeadInPlace, KeyInit};
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion, Throughput};
use libcrux::{aead::*, digest, drbg};
use libcrux::{digest, drbg};

use libcrux_chacha20poly1305::*;

use benchmarks::util::*;
use rand_core::OsRng;
use ring::aead::UnboundKey;

fn randbuf<const LEN: usize>(drbg: &mut drbg::Drbg) -> Result<[u8; LEN], drbg::Error> {
let mut buf = [0; LEN];
drbg.generate(&mut buf).map(|_| buf)
}

// Comparing libcrux performance for different payload sizes and other implementations.
fn comparisons_encrypt(c: &mut Criterion) {
const PAYLOAD_SIZES: [usize; 1] = [1024 * 1024 * 10];
Expand All @@ -22,14 +29,15 @@ fn comparisons_encrypt(c: &mut Criterion) {
|b, payload_size| {
b.iter_batched(
|| {
let key = Key::generate(Algorithm::Chacha20Poly1305, &mut drbg);
let nonce = Iv::generate(&mut drbg);
let data = randombytes(*payload_size);
let key = randbuf(&mut drbg).unwrap();
let nonce = randbuf(&mut drbg).unwrap();
let ptxt = randombytes(*payload_size);
let ctxt = vec![0; *payload_size];
let aad = randombytes(1_000);
(data, nonce, aad, key)
(ptxt, ctxt, nonce, aad, key)
},
|(mut data, nonce, aad, key)| {
let _tag = encrypt(&key, &mut data, nonce, &aad);
|(ptxt, mut ctxt, nonce, aad, key)| {
let _tag = encrypt(&key, &ptxt, &mut ctxt, &aad, &nonce);
},
BatchSize::SmallInput,
)
Expand Down Expand Up @@ -129,16 +137,24 @@ fn comparisons_decrypt(c: &mut Criterion) {
|b, payload_size| {
b.iter_batched(
|| {
let key = Key::generate(Algorithm::Chacha20Poly1305, &mut drbg);
let nonce_enc = Iv::generate(&mut drbg);
let nonce = Iv(nonce_enc.0);
let mut data = randombytes(*payload_size);
let key = randbuf(&mut drbg).unwrap();
let nonce_enc = randbuf(&mut drbg).unwrap();
let nonce = nonce_enc;
let ptxt = randombytes(*payload_size);
let mut ctxt = vec![0; *payload_size + TAG_LEN];
let aad = randombytes(1_000);

let tag = encrypt(&key, &mut data, nonce_enc, &aad).unwrap();
(key, nonce, data, tag, aad)
let ctxt_len = ctxt.len();

let (ctxt_got, _tag) =
encrypt(&key, &ptxt, &mut ctxt, &aad, &nonce).unwrap();
assert_eq!(ctxt_len, ctxt_got.len());

(key, nonce, ptxt, ctxt, aad)
},
|(key, nonce, mut ptxt, ctxt, aad)| {
decrypt(&key, &mut ptxt, &ctxt, &aad, &nonce).unwrap();
},
|(key, nonce, mut data, tag, aad)| decrypt(&key, &mut data, nonce, &aad, &tag),
BatchSize::SmallInput,
)
},
Expand Down
44 changes: 44 additions & 0 deletions chacha20poly1305/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![no_std]

extern crate alloc;

/// The length of ChaCha20-Poly1305 keys.
pub const KEY_LEN: usize = 32;

Expand All @@ -10,6 +12,7 @@ pub const TAG_LEN: usize = 16;
pub const NONCE_LEN: usize = 12;

/// Describes the error conditions of the ChaCha20-Poly1305 AEAD.
#[derive(Debug)]
pub enum AeadError {
/// Indicates that the plaintext argument is too large for the library to handle.
PlaintextTooLarge,
Expand All @@ -26,7 +29,35 @@ pub enum AeadError {
InvalidCiphertext,
}

impl alloc::fmt::Display for AeadError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let msg = match self {
AeadError::PlaintextTooLarge => {
"The plaintext argument is too large for the library to handle"
}
AeadError::CiphertextTooLarge => {
"The ciphertext argument is too large for the library to handle"
}
AeadError::AadTooLarge => {
"The associated data argument is too large for the library to handle"
}
AeadError::CiphertextTooShort => {
"The provided destination ciphertext does not fit the ciphertext and tag"
}
AeadError::PlaintextTooShort => {
"The provided destination plaintext is too short to fit the decrypted plaintext"
}
AeadError::InvalidCiphertext => {
"The ciphertext is not a valid encryption under the given key and nonce."
}
};

f.write_str(msg)
}
}

/// Describes the error conditions of the Poly1305 MAC.
#[derive(Debug)]
pub enum MacError {
/// Indicates that the message argument is too large for the library to handle.
MessageTooLarge,
Expand All @@ -35,6 +66,19 @@ pub enum MacError {
InvalidMacTag,
}

impl alloc::fmt::Display for MacError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let msg = match self {
MacError::MessageTooLarge => {
"The message argument is too large for the library to handle"
}
MacError::InvalidMacTag => "The MAC tag is invalid for that key and message",
};

f.write_str(msg)
}
}

mod hacl {
pub(crate) use libcrux_poly1305::hacl::mac_poly1305;

Expand Down

0 comments on commit 6afaf9e

Please sign in to comment.