1pub use blobby;
3
4#[macro_export]
6#[cfg_attr(docsrs, doc(cfg(feature = "dev")))]
7macro_rules! new_test {
8 ($name:ident, $test_name:expr, $cipher:ty $(,)?) => {
9 #[test]
10 fn $name() {
11 use aead::{
12 dev::blobby::Blob6Iterator,
13 generic_array::{typenum::Unsigned, GenericArray},
14 Aead, KeyInit, Payload,
15 };
16
17 fn run_test(
18 key: &[u8],
19 nonce: &[u8],
20 aad: &[u8],
21 pt: &[u8],
22 ct: &[u8],
23 pass: bool,
24 ) -> Result<(), &'static str> {
25 let key = key.try_into().map_err(|_| "wrong key size")?;
26 let cipher = <$cipher>::new(key);
27 let nonce = nonce.try_into().map_err(|_| "wrong nonce size")?;
28
29 if !pass {
30 let res = cipher.decrypt(nonce, Payload { aad: aad, msg: ct });
31 if res.is_ok() {
32 return Err("decryption must return error");
33 }
34 return Ok(());
35 }
36
37 let res = cipher
38 .encrypt(nonce, Payload { aad: aad, msg: pt })
39 .map_err(|_| "encryption failure")?;
40 if res != ct {
41 return Err("encrypted data is different from target ciphertext");
42 }
43 let res = cipher
44 .decrypt(nonce, Payload { aad: aad, msg: ct })
45 .map_err(|_| "decryption failure")?;
46 if res != pt {
47 return Err("decrypted data is different from target plaintext");
48 }
49 Ok(())
50 }
51
52 let data = include_bytes!(concat!("data/", $test_name, ".blb"));
53 for (i, row) in Blob6Iterator::new(data).unwrap().enumerate() {
54 let [key, nonce, aad, pt, ct, status] = row.unwrap();
55 let pass = match status[0] {
56 0 => false,
57 1 => true,
58 _ => panic!("invalid value for pass flag"),
59 };
60 if let Err(reason) = run_test(key, nonce, aad, pt, ct, pass) {
61 panic!(
62 "\n\
63 Failed test №{}\n\
64 reason: \t{:?}\n\
65 key:\t{:?}\n\
66 nonce:\t{:?}\n\
67 aad:\t{:?}\n\
68 plaintext:\t{:?}\n\
69 ciphertext:\t{:?}\n\
70 pass:\t{}\n\
71 ",
72 i, reason, key, nonce, aad, pt, ct, pass,
73 );
74 }
75 }
76 }
77 };
78}