|
1 | | -use blobby::Blob4Iterator; |
2 | 1 | use hkdf::{GenericHkdf, HmacImpl}; |
3 | 2 | use hmac::{Hmac, SimpleHmac}; |
4 | 3 |
|
5 | | -fn test<H: HmacImpl>(data: &[u8]) { |
6 | | - for (i, row) in Blob4Iterator::new(data).unwrap().enumerate() { |
7 | | - let [ikm, salt, info, okm] = row.unwrap(); |
| 4 | +#[derive(Copy, Clone, Debug)] |
| 5 | +struct TestVector { |
| 6 | + ikm: &'static [u8], |
| 7 | + salt: &'static [u8], |
| 8 | + info: &'static [u8], |
| 9 | + okm: &'static [u8], |
| 10 | +} |
8 | 11 |
|
9 | | - let prk = GenericHkdf::<H>::new(Some(salt), ikm); |
10 | | - let mut got_okm = vec![0; okm.len()]; |
| 12 | +fn test<H: HmacImpl>(test_vectors: &[TestVector]) { |
| 13 | + let mut buf = [0u8; 1 << 14]; |
| 14 | + for (i, tv) in test_vectors.iter().enumerate() { |
| 15 | + let prk = GenericHkdf::<H>::new(Some(tv.salt), tv.ikm); |
| 16 | + let okm_dst = &mut buf[..tv.okm.len()]; |
11 | 17 |
|
12 | 18 | let mut err = None; |
13 | | - if prk.expand(info, &mut got_okm).is_err() { |
| 19 | + if prk.expand(tv.info, okm_dst).is_err() { |
14 | 20 | err = Some("prk expand"); |
15 | 21 | } |
16 | | - if got_okm != okm { |
| 22 | + if okm_dst != tv.okm { |
17 | 23 | err = Some("mismatch in okm"); |
18 | 24 | } |
19 | 25 |
|
20 | 26 | if let Some(err_desc) = err { |
21 | | - panic!( |
22 | | - "\n\ |
23 | | - Failed test №{i}: {err_desc}\n\ |
24 | | - ikm:\t{ikm:?}\n\ |
25 | | - salt:\t{salt:?}\n\ |
26 | | - info:\t{info:?}\n\ |
27 | | - okm:\t{okm:?}\n" |
28 | | - ); |
| 27 | + panic!("Failed test #{i}: {err_desc}\nTest vector:\t{tv:#?}"); |
29 | 28 | } |
30 | 29 | } |
31 | 30 | } |
32 | 31 |
|
33 | 32 | macro_rules! new_test { |
34 | | - ($name:ident, $test_name:expr, $hash:ty) => { |
| 33 | + ($name:ident, $hash:ty) => { |
35 | 34 | #[test] |
36 | 35 | fn $name() { |
37 | | - let data = include_bytes!(concat!("data/", $test_name, ".blb")); |
38 | | - test::<Hmac<$hash>>(data); |
39 | | - test::<SimpleHmac<$hash>>(data); |
| 36 | + blobby::parse_into_structs!( |
| 37 | + include_bytes!(concat!("data/", stringify!($name), ".blb")); |
| 38 | + static TEST_VECTORS: &[TestVector { ikm, salt, info, okm }]; |
| 39 | + ); |
| 40 | + |
| 41 | + test::<Hmac<$hash>>(TEST_VECTORS); |
| 42 | + test::<SimpleHmac<$hash>>(TEST_VECTORS); |
40 | 43 | } |
41 | 44 | }; |
42 | 45 | } |
43 | 46 |
|
44 | | -new_test!(wycheproof_sha1, "wycheproof-sha1", sha1::Sha1); |
45 | | -new_test!(wycheproof_sha256, "wycheproof-sha256", sha2::Sha256); |
46 | | -new_test!(wycheproof_sha384, "wycheproof-sha384", sha2::Sha384); |
47 | | -new_test!(wycheproof_sha512, "wycheproof-sha512", sha2::Sha512); |
| 47 | +new_test!(wycheproof_sha1, sha1::Sha1); |
| 48 | +new_test!(wycheproof_sha256, sha2::Sha256); |
| 49 | +new_test!(wycheproof_sha384, sha2::Sha384); |
| 50 | +new_test!(wycheproof_sha512, sha2::Sha512); |
0 commit comments