Skip to content

Commit 07b2b0d

Browse files
committed
ascon-aead: migrate tests to blobby
1 parent a324b49 commit 07b2b0d

File tree

6 files changed

+68
-12490
lines changed

6 files changed

+68
-12490
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ascon-aead/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ zeroize = { version = "1.6", optional = true, default-features = false, features
2121
ascon = "0.4"
2222

2323
[dev-dependencies]
24-
hex-literal = "0.4"
25-
aead = { version = "0.6.0-rc.0", features = ["alloc"] }
24+
aead = { version = "0.6.0-rc.0", features = ["dev"] }
2625

2726
[features]
2827
default = ["alloc", "os_rng"]

ascon-aead/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//! Simple usage (allocating, no associated data):
1616
//!
1717
//! ```
18+
//! # #[cfg(feature = "alloc")] {
1819
//! use ascon_aead::{AsconAead128, Key, Nonce};
1920
//! use ascon_aead::aead::{Aead, KeyInit};
2021
//!
@@ -30,6 +31,7 @@
3031
//! .expect("decryption failure!"); // NOTE: handle this error to avoid panics!
3132
//!
3233
//! assert_eq!(&plaintext, b"plaintext message");
34+
//! # }
3335
//! ```
3436
//!
3537
//! With randomly sampled keys and nonces (requires `getrandom` feature):

ascon-aead/tests/data/kats.blb

40.5 KB
Binary file not shown.

ascon-aead/tests/kats.rs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#![cfg(feature = "alloc")] // TODO: remove after migration to the new `aead` crate
2+
use ascon_aead::{
3+
AsconAead128,
4+
aead::{Aead, KeyInit, Nonce, Payload, dev::blobby},
5+
};
6+
7+
fn run_pass_test<C: Aead>(
8+
cipher: &C,
9+
nonce: &Nonce<C>,
10+
aad: &[u8],
11+
pt: &[u8],
12+
ct: &[u8],
13+
) -> Result<(), &'static str> {
14+
let res = cipher
15+
.encrypt(nonce, Payload { aad, msg: pt })
16+
.map_err(|_| "encryption failure")?;
17+
if res != ct {
18+
return Err("encrypted data is different from target ciphertext");
19+
}
20+
21+
let res = cipher
22+
.decrypt(nonce, Payload { aad, msg: ct })
23+
.map_err(|_| "decryption failure")?;
24+
if res != pt {
25+
return Err("decrypted data is different from target plaintext");
26+
}
27+
28+
Ok(())
29+
}
30+
31+
#[macro_export]
32+
macro_rules! new_pass_test {
33+
($name:ident, $test_name:expr, $cipher:ty $(,)?) => {
34+
#[test]
35+
fn $name() {
36+
use blobby::Blob5Iterator;
37+
use $crate::KeyInit;
38+
39+
let data = include_bytes!(concat!("data/", $test_name, ".blb"));
40+
for (i, row) in Blob5Iterator::new(data).unwrap().enumerate() {
41+
let [key, nonce, aad, pt, ct] = row.unwrap();
42+
let key = key.try_into().expect("wrong key size");
43+
let nonce = nonce.try_into().expect("wrong nonce size");
44+
let cipher = <$cipher as KeyInit>::new(key);
45+
let res = run_pass_test(&cipher, nonce, aad, pt, ct);
46+
if let Err(reason) = res {
47+
panic!(
48+
"\n\
49+
Failed (pass) test #{i}\n\
50+
reason:\t{reason:?}\n\
51+
key:\t{key:?}\n\
52+
nonce:\t{nonce:?}\n\
53+
aad:\t{aad:?}\n\
54+
plaintext:\t{pt:?}\n\
55+
ciphertext:\t{ct:?}\n"
56+
);
57+
}
58+
}
59+
}
60+
};
61+
}
62+
63+
// Test vectors are taken from the reference Ascon implementation:
64+
// https://github.com/ascon/ascon-c/blob/fdfca408/crypto_aead/asconaead128/LWC_AEAD_KAT_128_128.txt
65+
new_pass_test!(ascon_aead_kats, "kats", AsconAead128);

0 commit comments

Comments
 (0)