Skip to content

Commit

Permalink
Add Wycheproof HKDF test vectors (#49)
Browse files Browse the repository at this point in the history
Created from:
https://github.com/google/wycheproof/blob/2196000605e45d91097147c9c71f26b72af58003/testvectors/

files:
 - hkdf_sha1_test.json
 - hkdf_sha256_test.json
 - hkdf_sha384_test.json
 - hkdf_sha512_test.json
  • Loading branch information
daviddrysdale authored Feb 27, 2021
1 parent 8f51c58 commit 65f5009
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 1 deletion.
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>);

0 comments on commit 65f5009

Please sign in to comment.