Skip to content

Commit 61b66c7

Browse files
authored
pbkdf2: use helper macro to simplify tests (#617)
Additionally, adds a test vector for `belt-hash`.
1 parent 4ac0ef5 commit 61b66c7

File tree

3 files changed

+89
-95
lines changed

3 files changed

+89
-95
lines changed

Cargo.lock

Lines changed: 27 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pbkdf2/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ hex-literal = "1"
2929
sha1 = "0.11.0-rc.0"
3030
sha2 = "0.11.0-rc.0"
3131
streebog = "0.11.0-rc.0"
32+
belt-hash = "0.2.0-rc.0"
3233

3334
[features]
3435
default = ["hmac"]

pbkdf2/tests/mod.rs

Lines changed: 61 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,93 @@
11
#![cfg(feature = "hmac")]
2+
use belt_hash::BeltHash;
23
use hex_literal::hex;
3-
use pbkdf2::pbkdf2_hmac_array as f;
44
use sha1::Sha1;
55
use streebog::Streebog512;
66

7-
/// Tests from RFC 6070:
7+
macro_rules! test {
8+
(
9+
$hash:ty;
10+
$($password:expr, $salt:expr, $rounds:expr, $($expected_hash:literal)*;)*
11+
) => {
12+
$({
13+
const EXPECTED_HASH: &[u8] = &hex_literal::hex!($($expected_hash)*);
14+
const N: usize = EXPECTED_HASH.len();
15+
16+
let hash = pbkdf2::pbkdf2_hmac_array::<$hash, N>($password, $salt, $rounds);
17+
assert_eq!(hash[..], EXPECTED_HASH[..]);
18+
})*
19+
};
20+
}
21+
22+
/// Test vectors from RFC 6070:
823
/// https://www.rfc-editor.org/rfc/rfc6070
924
#[test]
10-
fn rfc6070() {
11-
assert_eq!(
12-
f::<Sha1, 20>(b"password", b"salt", 1),
13-
hex!("0c60c80f961f0e71f3a9b524af6012062fe037a6"),
14-
);
15-
assert_eq!(
16-
f::<Sha1, 20>(b"password", b"salt", 2),
17-
hex!("ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957"),
18-
);
19-
assert_eq!(
20-
f::<Sha1, 20>(b"password", b"salt", 4096),
21-
hex!("4b007901b765489abead49d926f721d065a429c1"),
22-
);
23-
// this test passes, but takes a long time to execute
24-
/*
25-
assert_eq!(
26-
f::<Sha1, 20>(b"password", b"salt", 16777216),
27-
hex!("eefe3d61cd4da4e4e9945b3d6ba2158c2634e984"),
28-
);
29-
*/
30-
assert_eq!(
31-
f::<Sha1, 25>(
32-
b"passwordPASSWORDpassword",
33-
b"saltSALTsaltSALTsaltSALTsaltSALTsalt",
34-
4096
35-
),
36-
hex!("3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038"),
37-
);
38-
assert_eq!(
39-
f::<Sha1, 16>(b"pass\0word", b"sa\0lt", 4096),
40-
hex!("56fa6aa75548099dcc37d7f03425e0c3"),
25+
fn pbkdf2_rfc6070() {
26+
test!(
27+
Sha1;
28+
b"password", b"salt", 1, "0c60c80f961f0e71f3a9b524af6012062fe037a6";
29+
b"password", b"salt", 2, "ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957";
30+
b"password", b"salt", 4096, "4b007901b765489abead49d926f721d065a429c1";
31+
// this test passes, but takes a long time to execute
32+
// b"password", b"salt", 16777216, "eefe3d61cd4da4e4e9945b3d6ba2158c2634e984";
33+
b"passwordPASSWORDpassword", b"saltSALTsaltSALTsaltSALTsaltSALTsalt", 4096,
34+
"3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038";
35+
b"pass\0word", b"sa\0lt", 4096, "56fa6aa75548099dcc37d7f03425e0c3";
4136
);
4237
}
4338

4439
/// Test vectors from R 50.1.111-2016:
45-
/// https://tc26.ru/standard/rs/Р 50.1.111-2016.pdf
40+
/// https://tc26.ru/standard/rs/Р%2050.1.111-2016.pdf
4641
#[test]
47-
fn gost() {
48-
assert_eq!(
49-
f::<Streebog512, 64>(b"password", b"salt", 1),
50-
hex!(
42+
fn pbkdf2_streebog() {
43+
test!(
44+
Streebog512;
45+
b"password", b"salt", 1,
5146
"64770af7f748c3b1c9ac831dbcfd85c2"
5247
"6111b30a8a657ddc3056b80ca73e040d"
5348
"2854fd36811f6d825cc4ab66ec0a68a4"
54-
"90a9e5cf5156b3a2b7eecddbf9a16b47"
55-
),
56-
);
57-
58-
assert_eq!(
59-
f::<Streebog512, 64>(b"password", b"salt", 2),
60-
hex!(
49+
"90a9e5cf5156b3a2b7eecddbf9a16b47";
50+
b"password", b"salt", 2,
6151
"5a585bafdfbb6e8830d6d68aa3b43ac0"
6252
"0d2e4aebce01c9b31c2caed56f0236d4"
6353
"d34b2b8fbd2c4e89d54d46f50e47d45b"
64-
"bac301571743119e8d3c42ba66d348de"
65-
),
66-
);
67-
68-
assert_eq!(
69-
f::<Streebog512, 64>(b"password", b"salt", 4096),
70-
hex!(
54+
"bac301571743119e8d3c42ba66d348de";
55+
b"password", b"salt", 4096,
7156
"e52deb9a2d2aaff4e2ac9d47a41f34c2"
7257
"0376591c67807f0477e32549dc341bc7"
7358
"867c09841b6d58e29d0347c996301d55"
74-
"df0d34e47cf68f4e3c2cdaf1d9ab86c3"
75-
),
76-
);
77-
78-
// this test passes, but takes a long time to execute
79-
/*
80-
assert_eq!(
81-
f::<Streebog512, 64>(b"password", b"salt", 16777216),
82-
hex!(
83-
"49e4843bba76e300afe24c4d23dc7392"
84-
"def12f2c0e244172367cd70a8982ac36"
85-
"1adb601c7e2a314e8cb7b1e9df840e36"
86-
"ab5615be5d742b6cf203fb55fdc48071"
87-
),
88-
);
89-
*/
90-
91-
assert_eq!(
92-
f::<Streebog512, 100>(
93-
b"passwordPASSWORDpassword",
94-
b"saltSALTsaltSALTsaltSALTsaltSALTsalt",
95-
4096,
96-
),
97-
hex!(
59+
"df0d34e47cf68f4e3c2cdaf1d9ab86c3";
60+
// this test passes, but takes a long time to execute
61+
// b"password", b"salt", 16777216,
62+
// "49e4843bba76e300afe24c4d23dc7392"
63+
// "def12f2c0e244172367cd70a8982ac36"
64+
// "1adb601c7e2a314e8cb7b1e9df840e36"
65+
// "ab5615be5d742b6cf203fb55fdc48071";
66+
b"passwordPASSWORDpassword", b"saltSALTsaltSALTsaltSALTsaltSALTsalt", 4096,
9867
"b2d8f1245fc4d29274802057e4b54e0a"
9968
"0753aa22fc53760b301cf008679e58fe"
10069
"4bee9addcae99ba2b0b20f431a9c5e50"
10170
"f395c89387d0945aedeca6eb4015dfc2"
10271
"bd2421ee9bb71183ba882ceebfef259f"
10372
"33f9e27dc6178cb89dc37428cf9cc52a"
104-
"2baa2d3a"
105-
),
106-
);
107-
108-
assert_eq!(
109-
f::<Streebog512, 64>(b"pass\0word", b"sa\0lt", 4096),
110-
hex!(
73+
"2baa2d3a";
74+
b"pass\0word", b"sa\0lt", 4096,
11175
"50df062885b69801a3c10248eb0a27ab"
11276
"6e522ffeb20c991c660f001475d73a4e"
11377
"167f782c18e97e92976d9c1d970831ea"
114-
"78ccb879f67068cdac1910740844e830"
115-
),
78+
"78ccb879f67068cdac1910740844e830";
79+
);
80+
}
81+
82+
/// Test vector from STB 4.101.45-2013 (page 33):
83+
/// https://apmi.bsu.by/assets/files/std/bign-spec294.pdf
84+
#[test]
85+
fn pbkdf2_belt() {
86+
test!(
87+
BeltHash;
88+
&hex!("42313934 42414338 30413038 46353342"),
89+
&hex!("BE329713 43FC9A48"),
90+
10_000,
91+
"3D331BBB B1FBBB40 E4BF22F6 CB9A689E F13A77DC 09ECF932 91BFE424 39A72E7D";
11692
);
11793
}

0 commit comments

Comments
 (0)