|
1 | 1 | //! Development-related functionality |
| 2 | +use crate::{ |
| 3 | + Aead, AeadInOut, Nonce, Payload, Tag, TagPosition, array::typenum::Unsigned, inout::InOutBuf, |
| 4 | +}; |
2 | 5 | pub use blobby; |
3 | 6 |
|
| 7 | +/// Run AEAD test for the provided passing test vector |
| 8 | +pub fn run_pass_test<C: AeadInOut>( |
| 9 | + cipher: &C, |
| 10 | + nonce: &Nonce<C>, |
| 11 | + aad: &[u8], |
| 12 | + pt: &[u8], |
| 13 | + ct: &[u8], |
| 14 | +) -> Result<(), &'static str> { |
| 15 | + let res = cipher |
| 16 | + .encrypt(nonce, Payload { aad, msg: pt }) |
| 17 | + .map_err(|_| "encryption failure")?; |
| 18 | + if res != ct { |
| 19 | + return Err("encrypted data is different from target ciphertext"); |
| 20 | + } |
| 21 | + |
| 22 | + let res = cipher |
| 23 | + .decrypt(nonce, Payload { aad, msg: ct }) |
| 24 | + .map_err(|_| "decryption failure")?; |
| 25 | + if res != pt { |
| 26 | + return Err("decrypted data is different from target plaintext"); |
| 27 | + } |
| 28 | + |
| 29 | + let (ct, tag) = match C::TAG_POSITION { |
| 30 | + TagPosition::Prefix => { |
| 31 | + let (tag, ct) = ct.split_at(C::TagSize::USIZE); |
| 32 | + (ct, tag) |
| 33 | + } |
| 34 | + TagPosition::Postfix => ct.split_at(pt.len()), |
| 35 | + }; |
| 36 | + let tag: &Tag<C> = tag.try_into().expect("tag has correct length"); |
| 37 | + |
| 38 | + // Fill output buffer with "garbage" to test that its data does not get read during encryption |
| 39 | + let mut buf: alloc::vec::Vec<u8> = (0..pt.len()).map(|i| i as u8).collect(); |
| 40 | + let inout_buf = InOutBuf::new(pt, &mut buf).expect("pt and buf have the same length"); |
| 41 | + |
| 42 | + let calc_tag = cipher |
| 43 | + .encrypt_inout_detached(nonce, aad, inout_buf) |
| 44 | + .map_err(|_| "encrypt_inout_detached: encryption failure")?; |
| 45 | + if tag != &calc_tag { |
| 46 | + return Err("encrypt_inout_detached: tag mismatch"); |
| 47 | + } |
| 48 | + if ct != buf { |
| 49 | + return Err("encrypt_inout_detached: ciphertext mismatch"); |
| 50 | + } |
| 51 | + |
| 52 | + // Fill output buffer with "garbage" |
| 53 | + buf.iter_mut().enumerate().for_each(|(i, v)| *v = i as u8); |
| 54 | + |
| 55 | + let inout_buf = InOutBuf::new(ct, &mut buf).expect("ct and buf have the same length"); |
| 56 | + cipher |
| 57 | + .decrypt_inout_detached(nonce, aad, inout_buf, tag) |
| 58 | + .map_err(|_| "decrypt_inout_detached: decryption failure")?; |
| 59 | + if pt != buf { |
| 60 | + return Err("decrypt_inout_detached: plaintext mismatch"); |
| 61 | + } |
| 62 | + |
| 63 | + Ok(()) |
| 64 | +} |
| 65 | + |
| 66 | +/// Run AEAD test for the provided failing test vector |
| 67 | +pub fn run_fail_test<C: AeadInOut>( |
| 68 | + cipher: &C, |
| 69 | + nonce: &Nonce<C>, |
| 70 | + aad: &[u8], |
| 71 | + ct: &[u8], |
| 72 | +) -> Result<(), &'static str> { |
| 73 | + let res = cipher.decrypt(nonce, Payload { aad, msg: ct }); |
| 74 | + if res.is_ok() { |
| 75 | + Err("decryption must return error") |
| 76 | + } else { |
| 77 | + Ok(()) |
| 78 | + } |
| 79 | +} |
| 80 | + |
4 | 81 | /// Define AEAD test |
5 | 82 | #[macro_export] |
6 | 83 | macro_rules! new_test { |
7 | 84 | ($name:ident, $test_name:expr, $cipher:ty $(,)?) => { |
8 | 85 | #[test] |
9 | 86 | fn $name() { |
10 | | - use aead::{ |
11 | | - Aead, KeyInit, Payload, |
12 | | - array::{Array, typenum::Unsigned}, |
13 | | - dev::blobby::Blob6Iterator, |
14 | | - }; |
15 | | - |
16 | | - fn run_test( |
17 | | - key: &[u8], |
18 | | - nonce: &[u8], |
19 | | - aad: &[u8], |
20 | | - pt: &[u8], |
21 | | - ct: &[u8], |
22 | | - pass: bool, |
23 | | - ) -> Result<(), &'static str> { |
24 | | - let key = key.try_into().map_err(|_| "wrong key size")?; |
25 | | - let cipher = <$cipher>::new(key); |
26 | | - let nonce = nonce.try_into().map_err(|_| "wrong nonce size")?; |
27 | | - |
28 | | - if !pass { |
29 | | - let res = cipher.decrypt(nonce, Payload { aad: aad, msg: ct }); |
30 | | - if res.is_ok() { |
31 | | - return Err("decryption must return error"); |
32 | | - } |
33 | | - return Ok(()); |
34 | | - } |
35 | | - |
36 | | - let res = cipher |
37 | | - .encrypt(nonce, Payload { aad: aad, msg: pt }) |
38 | | - .map_err(|_| "encryption failure")?; |
39 | | - if res != ct { |
40 | | - return Err("encrypted data is different from target ciphertext"); |
41 | | - } |
42 | | - let res = cipher |
43 | | - .decrypt(nonce, Payload { aad: aad, msg: ct }) |
44 | | - .map_err(|_| "decryption failure")?; |
45 | | - if res != pt { |
46 | | - return Err("decrypted data is different from target plaintext"); |
47 | | - } |
48 | | - Ok(()) |
49 | | - } |
| 87 | + use $crate::KeyInit; |
| 88 | + use $crate::dev::blobby::Blob6Iterator; |
50 | 89 |
|
51 | 90 | let data = include_bytes!(concat!("data/", $test_name, ".blb")); |
52 | 91 | for (i, row) in Blob6Iterator::new(data).unwrap().enumerate() { |
53 | 92 | let [key, nonce, aad, pt, ct, status] = row.unwrap(); |
54 | | - let pass = match status[0] { |
55 | | - 0 => false, |
56 | | - 1 => true, |
| 93 | + let key = key.try_into().expect("wrong key size"); |
| 94 | + let nonce = nonce.try_into().expect("wrong nonce size"); |
| 95 | + let cipher = <$cipher as KeyInit>::new(key); |
| 96 | + |
| 97 | + let res = match status { |
| 98 | + [0] => $crate::dev::run_fail_test(&cipher, nonce, aad, ct), |
| 99 | + [1] => $crate::dev::run_pass_test(&cipher, nonce, aad, pt, ct), |
57 | 100 | _ => panic!("invalid value for pass flag"), |
58 | 101 | }; |
59 | | - if let Err(reason) = run_test(key, nonce, aad, pt, ct, pass) { |
| 102 | + let mut pass = status[0] == 1; |
| 103 | + if let Err(reason) = res { |
60 | 104 | panic!( |
61 | 105 | "\n\ |
62 | | - Failed test №{}\n\ |
63 | | - reason: \t{:?}\n\ |
64 | | - key:\t{:?}\n\ |
65 | | - nonce:\t{:?}\n\ |
66 | | - aad:\t{:?}\n\ |
67 | | - plaintext:\t{:?}\n\ |
68 | | - ciphertext:\t{:?}\n\ |
69 | | - pass:\t{}\n\ |
70 | | - ", |
71 | | - i, reason, key, nonce, aad, pt, ct, pass, |
| 106 | + Failed test #{i}\n\ |
| 107 | + reason:\t{reason:?}\n\ |
| 108 | + key:\t{key:?}\n\ |
| 109 | + nonce:\t{nonce:?}\n\ |
| 110 | + aad:\t{aad:?}\n\ |
| 111 | + plaintext:\t{pt:?}\n\ |
| 112 | + ciphertext:\t{ct:?}\n\ |
| 113 | + pass:\t{pass}\n" |
72 | 114 | ); |
73 | 115 | } |
74 | 116 | } |
|
0 commit comments