Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Wycheproof HKDF test vectors #49

Merged
merged 1 commit into from
Feb 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions hkdf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Binary file added hkdf/tests/data/wycheproof-sha1.blb
Binary file not shown.
Binary file added hkdf/tests/data/wycheproof-sha256.blb
Binary file not shown.
Binary file added hkdf/tests/data/wycheproof-sha384.blb
Binary file not shown.
Binary file added hkdf/tests/data/wycheproof-sha512.blb
Binary file not shown.
47 changes: 46 additions & 1 deletion hkdf/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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::<Sha1>);
new_test!(wycheproof_sha256, "wycheproof-sha256", Hkdf::<Sha256>);
new_test!(wycheproof_sha384, "wycheproof-sha384", Hkdf::<Sha384>);
new_test!(wycheproof_sha512, "wycheproof-sha512", Hkdf::<Sha512>);