Skip to content

Commit

Permalink
Merge branch 'main' into wysiwys/update-ecdh-benches
Browse files Browse the repository at this point in the history
  • Loading branch information
wysiwys authored Feb 12, 2025
2 parents 27d24b6 + 22aafcc commit 4c6be95
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 15 deletions.
3 changes: 2 additions & 1 deletion 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-ecdh = { path = "../libcrux-ecdh" }
libcrux-kem = { path = "../libcrux-kem", features = ["tests"] }
libcrux-ml-kem = { path = "../libcrux-ml-kem" }
Expand Down Expand Up @@ -71,7 +72,7 @@ name = "p256"
harness = false

[[bench]]
name = "aead"
name = "chacha20poly1305"
harness = false

[[bench]]
Expand Down
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 @@ -127,18 +135,27 @@ fn comparisons_decrypt(c: &mut Criterion) {
BenchmarkId::new("libcrux", fmt(*payload_size)),
payload_size,
|b, payload_size| {
let payload_size = *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_got, tag) =
encrypt(&key, &ptxt, &mut ctxt, &aad, &nonce).unwrap();
assert_eq!(payload_size, ctxt_got.len());
assert_eq!(TAG_LEN, tag.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
42 changes: 42 additions & 0 deletions chacha20poly1305/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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 +27,35 @@ pub enum AeadError {
InvalidCiphertext,
}

impl core::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 +64,19 @@ pub enum MacError {
InvalidMacTag,
}

impl core::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 4c6be95

Please sign in to comment.