diff --git a/Cargo.lock b/Cargo.lock index 1f06362..ed4952e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,12 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dfdb4953a096c551ce9ace855a604d702e6e62d77fac690575ae347571717f5" +[[package]] +name = "blobby" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc52553543ecb104069b0ff9e0fcc5c739ad16202935528a112d974e8f1a4ee8" + [[package]] name = "block-buffer" version = "0.9.0" @@ -123,6 +129,7 @@ name = "hkdf" version = "0.10.0" dependencies = [ "bencher", + "blobby", "crypto-tests", "digest 0.9.0", "hex", diff --git a/hkdf/Cargo.toml b/hkdf/Cargo.toml index a025733..292bf0c 100644 --- a/hkdf/Cargo.toml +++ b/hkdf/Cargo.toml @@ -22,6 +22,7 @@ digest = "0.9" hmac = "0.10" [dev-dependencies] +blobby = "0.3" crypto-tests = "0.5.*" hex = "0.4" sha-1 = "0.9" diff --git a/hkdf/tests/data/wycheproof-sha1.blb b/hkdf/tests/data/wycheproof-sha1.blb new file mode 100644 index 0000000..cb7dd3c Binary files /dev/null and b/hkdf/tests/data/wycheproof-sha1.blb differ diff --git a/hkdf/tests/data/wycheproof-sha256.blb b/hkdf/tests/data/wycheproof-sha256.blb new file mode 100644 index 0000000..6213609 Binary files /dev/null and b/hkdf/tests/data/wycheproof-sha256.blb differ diff --git a/hkdf/tests/data/wycheproof-sha384.blb b/hkdf/tests/data/wycheproof-sha384.blb new file mode 100644 index 0000000..2323055 Binary files /dev/null and b/hkdf/tests/data/wycheproof-sha384.blb differ diff --git a/hkdf/tests/data/wycheproof-sha512.blb b/hkdf/tests/data/wycheproof-sha512.blb new file mode 100644 index 0000000..7a75318 Binary files /dev/null and b/hkdf/tests/data/wycheproof-sha512.blb differ diff --git a/hkdf/tests/tests.rs b/hkdf/tests/tests.rs index 6b18e0d..00a18c1 100644 --- a/hkdf/tests/tests.rs +++ b/hkdf/tests/tests.rs @@ -4,7 +4,7 @@ use hex; use hkdf::{Hkdf, HkdfExtract}; use sha1::Sha1; -use sha2::Sha256; +use sha2::{Sha256, Sha384, Sha512}; struct Test<'a> { ikm: &'a str, @@ -329,3 +329,48 @@ fn test_extract_streaming() { num_concatted += 1; } } + +/// Define test +macro_rules! new_test { + ($name:ident, $test_name:expr, $hkdf:ty) => { + #[test] + fn $name() { + use blobby::Blob4Iterator; + + fn run_test(ikm: &[u8], salt: &[u8], info: &[u8], okm: &[u8]) -> Option<&'static str> { + let prk = <$hkdf>::new(Some(salt), ikm); + let mut got_okm = vec![0; okm.len()]; + + if prk.expand(info, &mut got_okm).is_err() { + return Some("prk expand"); + } + if got_okm != okm { + return Some("mismatch in okm"); + } + None + } + + let data = include_bytes!(concat!("data/", $test_name, ".blb")); + + for (i, row) in Blob4Iterator::new(data).unwrap().enumerate() { + let [ikm, salt, info, okm] = row.unwrap(); + if let Some(desc) = run_test(ikm, salt, info, okm) { + panic!( + "\n\ + Failed test №{}: {}\n\ + ikm:\t{:?}\n\ + salt:\t{:?}\n\ + info:\t{:?}\n\ + okm:\t{:?}\n", + i, desc, ikm, salt, info, okm + ); + } + } + } + }; +} + +new_test!(wycheproof_sha1, "wycheproof-sha1", Hkdf::); +new_test!(wycheproof_sha256, "wycheproof-sha256", Hkdf::); +new_test!(wycheproof_sha384, "wycheproof-sha384", Hkdf::); +new_test!(wycheproof_sha512, "wycheproof-sha512", Hkdf::);