Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_crypto/tests/mac.rs

4.2 KiB, 2 runs

created by r1870400018:76923, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! The pure-Rust MACs and key derivations against published vectors: HMAC-SHA256 (RFC 4231),
2//! HKDF-SHA256 (RFC 5869) and PBKDF2-HMAC-SHA256 (RFC 7914 §11 and the widely used RFC
3//! 6070-style set for SHA-256). The 16,777,216-round vector is ignored by default; run it with
4//! `cargo test --release -p oxedyne_fe2o3_crypto --test mac -- --ignored`.
5
6use oxedyne_fe2o3_core::prelude::*;
7use oxedyne_fe2o3_crypto::mac::{
8 hkdf_sha256,
9 hmac_sha256,
10 pbkdf2_hmac_sha256,
11};
12
13
14fn unhex(s: &str) -> Outcome<Vec<u8>> {
15 let s: String = s.chars().filter(|c| !c.is_whitespace()).collect();
16 let mut out = Vec::with_capacity(s.len() / 2);
17 for i in (0..s.len()).step_by(2) {
18 out.push(res!(u8::from_str_radix(&s[i..i + 2], 16), Decode));
19 }
20 Ok(out)
21}
22
23fn check(pass: &[u8], salt: &[u8], rounds: u32, want: &str) -> Outcome<()> {
24 let want = res!(unhex(want));
25 let mut got = vec![0u8; want.len()];
26 res!(pbkdf2_hmac_sha256(pass, salt, rounds, &mut got));
27 if got != want {
28 return Err(err!(
29 "PBKDF2-HMAC-SHA256({:?}, {:?}, {}) gave {:02x?}, expected {:02x?}.",
30 String::from_utf8_lossy(pass), String::from_utf8_lossy(salt), rounds, got, want;
31 Test, Mismatch));
32 }
33 Ok(())
34}
35
36/// RFC 4231 cases 2 and 6, the second with a key longer than a block.
37#[test]
38fn test_hmac_sha256_rfc4231_00() -> Outcome<()> {
39 req!(hmac_sha256(b"Jefe", b"what do ya want for nothing?").to_vec(), res!(unhex(
40 "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843")));
41 req!(hmac_sha256(&[0xaa; 131], b"Test Using Larger Than Block-Size Key - Hash Key First")
42 .to_vec(), res!(unhex(
43 "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54")));
44 Ok(())
45}
46
47/// RFC 5869 test case 1.
48#[test]
49fn test_hkdf_sha256_rfc5869_00() -> Outcome<()> {
50 let salt: Vec<u8> = (0x00..=0x0c).collect();
51 let info: Vec<u8> = (0xf0..=0xf9).collect();
52 let mut okm = [0u8; 42];
53 res!(hkdf_sha256(&[0x0b; 22], &salt, &info, &mut okm));
54 req!(okm.to_vec(), res!(unhex(
55 "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865")));
56 Ok(())
57}
58
59#[test]
60fn test_pbkdf2_rfc7914_one_round_two_blocks_00() -> Outcome<()> {
61 check(b"passwd", b"salt", 1,
62 "55ac046e56e3089fec1691c22544b605f94185216dde0465e68b9d57c20dacbc
63 49ca9cccf179b645991664b39d77ef317c71b845b1e30bd509112041d3a19783")
64}
65
66#[test]
67fn test_pbkdf2_rfc7914_eighty_thousand_rounds_00() -> Outcome<()> {
68 check(b"Password", b"NaCl", 80_000,
69 "4ddcd8f60b98be21830cee5ef22701f9641a4418d04c0414aeff08876b34ab56
70 a1d425a1225833549adb841b51c9b3176a272bdebba1d078478f62b397f33c8d")
71}
72
73#[test]
74fn test_pbkdf2_sha256_counts_00() -> Outcome<()> {
75 res!(check(b"password", b"salt", 1,
76 "120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b"));
77 res!(check(b"password", b"salt", 2,
78 "ae4d0c95af6b46d32d0adff928f06dd02a303f8ef3c251dfd6e2d85a95474c43"));
79 check(b"password", b"salt", 4_096,
80 "c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134a")
81}
82
83/// Output that ends part way through a block, a password longer than the 32-byte digest, and
84/// embedded zero bytes.
85#[test]
86fn test_pbkdf2_sha256_partial_block_and_zero_bytes_00() -> Outcome<()> {
87 res!(check(b"passwordPASSWORDpassword", b"saltSALTsaltSALTsaltSALTsaltSALTsalt", 4_096,
88 "348c89dbcbd32b2f32d814b8116e84cf2b17347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9"));
89 check(b"pass\0word", b"sa\0lt", 4_096, "89b69d0516f829893c696226650a8687")
90}
91
92#[test]
93#[ignore = "16,777,216 rounds: run with --release --ignored"]
94fn test_pbkdf2_sha256_sixteen_million_rounds_00() -> Outcome<()> {
95 check(b"password", b"salt", 16_777_216,
96 "cf81c66fe8cfc04d1f31ecb65dab4089f7f179e89b3b0bcb17ad10e3ac6eba46")
97}
98
99#[test]
100fn test_pbkdf2_refuses_zero_rounds_00() {
101 let mut out = [0u8; 32];
102 assert!(pbkdf2_hmac_sha256(b"p", b"s", 0, &mut out).is_err());
103}
104
105#[test]
106fn test_pbkdf2_empty_output_is_nothing_to_do_00() -> Outcome<()> {
107 let mut out: [u8; 0] = [];
108 res!(pbkdf2_hmac_sha256(b"p", b"s", 1, &mut out));
109 assert!(pbkdf2_hmac_sha256(b"p", b"s", 0, &mut out).is_err(), "zero rounds still refused");
110 Ok(())
111}