aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / digest / src / dev / mac.rs
| 1 | /// Define MAC test |
| 2 | #[macro_export] |
| 3 | #[cfg(feature = "mac")] |
| 4 | #[cfg_attr(docsrs, doc(cfg(all(feature = "dev", feature = "mac"))))] |
| 5 | macro_rules! new_mac_test { |
| 6 | ($name:ident, $test_name:expr, $mac:ty $(,)?) => { |
| 7 | digest::new_mac_test!($name, $test_name, $mac, ""); |
| 8 | }; |
| 9 | ($name:ident, $test_name:expr, $mac:ty, trunc_left $(,)?) => { |
| 10 | digest::new_mac_test!($name, $test_name, $mac, "left"); |
| 11 | }; |
| 12 | ($name:ident, $test_name:expr, $mac:ty, trunc_right $(,)?) => { |
| 13 | digest::new_mac_test!($name, $test_name, $mac, "right"); |
| 14 | }; |
| 15 | ($name:ident, $test_name:expr, $mac:ty, $trunc:expr $(,)?) => { |
| 16 | #[test] |
| 17 | fn $name() { |
| 18 | use core::cmp::min; |
| 19 | use digest::dev::blobby::Blob3Iterator; |
| 20 | use digest::Mac; |
| 21 | |
| 22 | fn run_test(key: &[u8], input: &[u8], tag: &[u8]) -> Option<&'static str> { |
| 23 | let mac0 = <$mac as Mac>::new_from_slice(key).unwrap(); |
| 24 | |
| 25 | let mut mac = mac0.clone(); |
| 26 | mac.update(input); |
| 27 | let result = mac.finalize().into_bytes(); |
| 28 | let n = tag.len(); |
| 29 | let result_bytes = match $trunc { |
| 30 | "left" => &result[..n], |
| 31 | "right" => &result[result.len() - n..], |
| 32 | _ => &result[..], |
| 33 | }; |
| 34 | if result_bytes != tag { |
| 35 | return Some("whole message"); |
| 36 | } |
| 37 | |
| 38 | // test reading different chunk sizes |
| 39 | for chunk_size in 1..min(64, input.len()) { |
| 40 | let mut mac = mac0.clone(); |
| 41 | for chunk in input.chunks(chunk_size) { |
| 42 | mac.update(chunk); |
| 43 | } |
| 44 | let res = match $trunc { |
| 45 | "left" => mac.verify_truncated_left(tag), |
| 46 | "right" => mac.verify_truncated_right(tag), |
| 47 | _ => mac.verify_slice(tag), |
| 48 | }; |
| 49 | if res.is_err() { |
| 50 | return Some("chunked message"); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | None |
| 55 | } |
| 56 | |
| 57 | let data = include_bytes!(concat!("data/", $test_name, ".blb")); |
| 58 | |
| 59 | for (i, row) in Blob3Iterator::new(data).unwrap().enumerate() { |
| 60 | let [key, input, tag] = row.unwrap(); |
| 61 | if let Some(desc) = run_test(key, input, tag) { |
| 62 | panic!( |
| 63 | "\n\ |
| 64 | Failed test №{}: {}\n\ |
| 65 | key:\t{:?}\n\ |
| 66 | input:\t{:?}\n\ |
| 67 | tag:\t{:?}\n", |
| 68 | i, desc, key, input, tag, |
| 69 | ); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | }; |
| 74 | } |
| 75 | |
| 76 | /// Define resettable MAC test |
| 77 | #[macro_export] |
| 78 | #[cfg(feature = "mac")] |
| 79 | #[cfg_attr(docsrs, doc(cfg(all(feature = "dev", feature = "mac"))))] |
| 80 | macro_rules! new_resettable_mac_test { |
| 81 | ($name:ident, $test_name:expr, $mac:ty $(,)?) => { |
| 82 | digest::new_resettable_mac_test!($name, $test_name, $mac, ""); |
| 83 | }; |
| 84 | ($name:ident, $test_name:expr, $mac:ty, trunc_left $(,)?) => { |
| 85 | digest::new_resettable_mac_test!($name, $test_name, $mac, "left"); |
| 86 | }; |
| 87 | ($name:ident, $test_name:expr, $mac:ty, trunc_right $(,)?) => { |
| 88 | digest::new_resettable_mac_test!($name, $test_name, $mac, "right"); |
| 89 | }; |
| 90 | ($name:ident, $test_name:expr, $mac:ty, $trunc:expr $(,)?) => { |
| 91 | #[test] |
| 92 | fn $name() { |
| 93 | use core::cmp::min; |
| 94 | use digest::dev::blobby::Blob3Iterator; |
| 95 | use digest::Mac; |
| 96 | |
| 97 | fn run_test(key: &[u8], input: &[u8], tag: &[u8]) -> Option<&'static str> { |
| 98 | let mac0 = <$mac as Mac>::new_from_slice(key).unwrap(); |
| 99 | |
| 100 | let mut mac = mac0.clone(); |
| 101 | mac.update(input); |
| 102 | let result = mac.finalize_reset().into_bytes(); |
| 103 | let n = tag.len(); |
| 104 | let result_bytes = match $trunc { |
| 105 | "left" => &result[..n], |
| 106 | "right" => &result[result.len() - n..], |
| 107 | _ => &result[..], |
| 108 | }; |
| 109 | if result_bytes != tag { |
| 110 | return Some("whole message"); |
| 111 | } |
| 112 | |
| 113 | // test if reset worked correctly |
| 114 | mac.update(input); |
| 115 | let res = match $trunc { |
| 116 | "left" => mac.verify_truncated_left(tag), |
| 117 | "right" => mac.verify_truncated_right(tag), |
| 118 | _ => mac.verify_slice(tag), |
| 119 | }; |
| 120 | if res.is_err() { |
| 121 | return Some("after reset"); |
| 122 | } |
| 123 | |
| 124 | // test reading different chunk sizes |
| 125 | for chunk_size in 1..min(64, input.len()) { |
| 126 | let mut mac = mac0.clone(); |
| 127 | for chunk in input.chunks(chunk_size) { |
| 128 | mac.update(chunk); |
| 129 | } |
| 130 | let res = match $trunc { |
| 131 | "left" => mac.verify_truncated_left(tag), |
| 132 | "right" => mac.verify_truncated_right(tag), |
| 133 | _ => mac.verify_slice(tag), |
| 134 | }; |
| 135 | if res.is_err() { |
| 136 | return Some("chunked message"); |
| 137 | } |
| 138 | } |
| 139 | None |
| 140 | } |
| 141 | |
| 142 | let data = include_bytes!(concat!("data/", $test_name, ".blb")); |
| 143 | |
| 144 | for (i, row) in Blob3Iterator::new(data).unwrap().enumerate() { |
| 145 | let [key, input, tag] = row.unwrap(); |
| 146 | if let Some(desc) = run_test(key, input, tag) { |
| 147 | panic!( |
| 148 | "\n\ |
| 149 | Failed test №{}: {}\n\ |
| 150 | key:\t{:?}\n\ |
| 151 | input:\t{:?}\n\ |
| 152 | tag:\t{:?}\n", |
| 153 | i, desc, key, input, tag, |
| 154 | ); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | }; |
| 159 | } |