Skip to content

Commit

Permalink
tests: Refactor HMAC test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
brycx committed Jan 25, 2020
1 parent f279777 commit 9e2817f
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 190 deletions.
2 changes: 1 addition & 1 deletion tests/kdf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ pub fn hkdf_test_runner(
} else {
assert!(verify(expected_okm, salt, ikm, Some(&info), &mut okm_out).is_err());
}
}
}
67 changes: 25 additions & 42 deletions tests/mac/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,41 @@ pub mod wycheproof_hmac_sha512;
extern crate orion;

use self::{
orion::hazardous::mac::{hmac, poly1305},
poly1305::{OneTimeKey, Tag},
orion::hazardous::hash::sha512::SHA512_OUTSIZE,
orion::hazardous::mac::{hmac, poly1305},
poly1305::{OneTimeKey, Tag},
};

fn hmac_test_runner(secret_key: &[u8], data: &[u8], expected: &[u8], trunc: Option<usize>) {
let key = hmac::SecretKey::from_slice(secret_key).unwrap();
let mut mac = hmac::Hmac::new(&key);
mac.update(data).unwrap();

let res = mac.finalize().unwrap();
let len = match trunc {
Some(length) => length,
None => 64,
};

let one_shot = hmac::Hmac::hmac(&key, data).unwrap();

assert_eq!(
res.unprotected_as_bytes()[..len].as_ref(),
expected[..len].as_ref()
);
assert_eq!(
one_shot.unprotected_as_bytes()[..len].as_ref(),
expected[..len].as_ref()
);
}

fn wycheproof_hmac_test_runner(
fn hmac_test_runner(
expected: &[u8],
secret_key: &[u8],
data: &[u8],
expected: &[u8],
len_bits: usize,
result: bool,
len_bytes: Option<usize>,
valid_result: bool,
) {
let key = hmac::SecretKey::from_slice(secret_key).unwrap();
let mut ctx = hmac::Hmac::new(&key);
ctx.update(data).unwrap();
let actual = ctx.finalize().unwrap();
let len = match len_bytes {
Some(length) => length,
None => SHA512_OUTSIZE,
};

let len = len_bits / 8;
let key = hmac::SecretKey::from_slice(secret_key).unwrap();

if result {
if len == 64 {
let expected_tag = hmac::Tag::from_slice(expected).unwrap();
assert!(hmac::Hmac::verify(&expected_tag, &key, data).is_ok());
// Only use verify() on SHA512_OUTSIZE length tags since this is
// the amount that Tag requires.
if len == SHA512_OUTSIZE {
let expected_tag = hmac::Tag::from_slice(expected).unwrap();
let res = hmac::Hmac::verify(&expected_tag, &key, data);
if valid_result {
assert!(res.is_ok());
} else {
assert_eq!(expected, actual.unprotected_as_bytes()[..len].as_ref());
assert!(res.is_err());
}
} else {
if len == 64 {
let expected_tag = hmac::Tag::from_slice(expected).unwrap();
assert!(hmac::Hmac::verify(&expected_tag, &key, data).is_err());
let mut ctx = hmac::Hmac::new(&key);
ctx.update(data).unwrap();
let actual = ctx.finalize().unwrap();
if valid_result {
assert_eq!(expected, actual.unprotected_as_bytes()[..len].as_ref());
} else {
assert_ne!(expected, actual.unprotected_as_bytes()[..len].as_ref());
}
Expand Down
17 changes: 9 additions & 8 deletions tests/mac/nist_cavp_hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,12 +758,13 @@ fn test_nist_cavp() {
["f78343071f61ee7d9f791bd53132e6d557928bcfe4b214bebf6f3592e46374c7ab148c3c4d6a1443a4675cf4321298c865b440631947b6b05f2c2a337d1cbb9b3661de974b4604eb41cc77c3659e85470e47e16f22a34619db935d59cbf5e1101ed401c020db069eff1035e9d1bff77bd8b3379e05ac0c20bc0e98aad7d7304dedd3bc5ed4136184649b5e0f7e5b", "d63b50b54e1536e35d5f3c6e29f1e49a78ca43fa22b31232c71f0300bd56517e4cd29ba11ee9f206f1ad31ee8f118c87004d6c6dfe837b70a9a2fa987c8b5b6680720c5dbf8791c1fcd6d59fa16cc20df9bc0fb39f41598a376476e45b9f06add8e34af01b373a9ce6a3d189484cacb6cbe0d3d5ef34d709d72c1dee43dc79da", "086f674d778db491e73b6fbc5126233c6b6e1f066963356d49ea386d9c0868ad25bf6edad0371cde87cea94a18c6dba47535dfce2e40d2246ab17980495d656c", "64"],

];
for test_case in test_vectors.iter() {
hmac_test_runner(
&decode(test_case[0]).unwrap(),
&decode(test_case[1]).unwrap(),
&decode(test_case[2]).unwrap(),
Some(test_case[3].parse::<usize>().unwrap()),
);
}
for test_case in test_vectors.iter() {
hmac_test_runner(
&decode(test_case[2]).unwrap(),
&decode(test_case[0]).unwrap(),
&decode(test_case[1]).unwrap(),
Some(test_case[3].parse::<usize>().unwrap()),
true,
);
}
}
259 changes: 124 additions & 135 deletions tests/mac/rfc_hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,139 +2,128 @@
#[cfg(test)]
mod rfc4231 {

extern crate hex;

use self::hex::decode;
use crate::mac::hmac_test_runner;

#[test]
fn test_case_1() {
let secret_key = decode("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b").unwrap();
let data = "Hi There".as_bytes().to_vec();
let expected_hmac_512 = decode(
"87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cde\
daa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854",
)
.unwrap();

hmac_test_runner(&secret_key, &data, &expected_hmac_512, None);
}

#[test]
fn test_case_2() {
let secret_key = "Jefe".as_bytes().to_vec();
let data = "what do ya want for nothing?".as_bytes().to_vec();

let expected_hmac_512 = decode(
"164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea250554\
9758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737",
)
.unwrap();

hmac_test_runner(&secret_key, &data, &expected_hmac_512, None);
}

#[test]
fn test_case_3() {
let secret_key = decode("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").unwrap();
let data = decode(
"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd\
dddddddddddddddddddddddddddddddddddd",
)
.unwrap();

let expected_hmac_512 = decode(
"fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39\
bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb",
)
.unwrap();

hmac_test_runner(&secret_key, &data, &expected_hmac_512, None);
}

#[test]
fn test_case_4() {
let secret_key = decode("0102030405060708090a0b0c0d0e0f10111213141516171819").unwrap();
let data = decode(
"cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd\
cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd",
)
.unwrap();

let expected_hmac_512 = decode(
"b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3db\
a91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd",
)
.unwrap();

hmac_test_runner(&secret_key, &data, &expected_hmac_512, None);
}

#[test]
fn test_case_5() {
let secret_key = decode("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c").unwrap();
let data = decode("546573742057697468205472756e636174696f6e").unwrap();

let expected_hmac_512 = decode("415fad6271580a531d4179bc891d87a6").unwrap();

hmac_test_runner(
&secret_key,
&data,
&expected_hmac_512,
Some(expected_hmac_512.len()),
);
}

#[test]
fn test_case_6() {
let secret_key = decode(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaa",
)
.unwrap();
let data = decode(
"54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a\
65204b6579202d2048617368204b6579204669727374",
)
.unwrap();

let expected_hmac_512 = decode(
"80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f352\
6b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598",
)
.unwrap();

hmac_test_runner(&secret_key, &data, &expected_hmac_512, None);
}

#[test]
fn test_case_7() {
let secret_key = decode(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaa",
)
.unwrap();
let data = decode(
"5468697320697320612074657374207573696e672061206c6172676572207468\
616e20626c6f636b2d73697a65206b657920616e642061206c61726765722074\
68616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565\
647320746f20626520686173686564206265666f7265206265696e6720757365\
642062792074686520484d414320616c676f726974686d2e",
)
.unwrap();

let expected_hmac_512 = decode(
"e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944\
b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58",
)
.unwrap();

hmac_test_runner(&secret_key, &data, &expected_hmac_512, None);
}
extern crate hex;

use self::hex::decode;
use crate::mac::hmac_test_runner;

#[test]
fn test_case_1() {
let secret_key = decode("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b").unwrap();
let data = "Hi There".as_bytes().to_vec();
let expected = decode(
"87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cde\
daa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854",
)
.unwrap();

hmac_test_runner(&expected, &secret_key, &data, None, true);
}

#[test]
fn test_case_2() {
let secret_key = "Jefe".as_bytes().to_vec();
let data = "what do ya want for nothing?".as_bytes().to_vec();
let expected = decode(
"164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea250554\
9758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737",
)
.unwrap();

hmac_test_runner(&expected, &secret_key, &data, None, true);
}

#[test]
fn test_case_3() {
let secret_key = decode("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").unwrap();
let data = decode(
"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd\
dddddddddddddddddddddddddddddddddddd",
)
.unwrap();
let expected = decode(
"fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39\
bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb",
)
.unwrap();

hmac_test_runner(&expected, &secret_key, &data, None, true);
}

#[test]
fn test_case_4() {
let secret_key = decode("0102030405060708090a0b0c0d0e0f10111213141516171819").unwrap();
let data = decode(
"cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd\
cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd",
)
.unwrap();
let expected = decode(
"b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3db\
a91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd",
)
.unwrap();

hmac_test_runner(&expected, &secret_key, &data, None, true);
}

#[test]
fn test_case_5() {
let secret_key = decode("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c").unwrap();
let data = decode("546573742057697468205472756e636174696f6e").unwrap();
let expected = decode("415fad6271580a531d4179bc891d87a6").unwrap();

hmac_test_runner(&expected, &secret_key, &data, Some(expected.len()), true);
}

#[test]
fn test_case_6() {
let secret_key = decode(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaa",
)
.unwrap();
let data = decode(
"54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a\
65204b6579202d2048617368204b6579204669727374",
)
.unwrap();
let expected = decode(
"80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f352\
6b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598",
)
.unwrap();

hmac_test_runner(&expected, &secret_key, &data, None, true);
}

#[test]
fn test_case_7() {
let secret_key = decode(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaa",
)
.unwrap();
let data = decode(
"5468697320697320612074657374207573696e672061206c6172676572207468\
616e20626c6f636b2d73697a65206b657920616e642061206c61726765722074\
68616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565\
647320746f20626520686173686564206265666f7265206265696e6720757365\
642062792074686520484d414320616c676f726974686d2e",
)
.unwrap();
let expected = decode(
"e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944\
b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58",
)
.unwrap();

hmac_test_runner(&expected, &secret_key, &data, None, true);
}
}
Loading

0 comments on commit 9e2817f

Please sign in to comment.