|
1 | 1 | #![cfg(feature = "hmac")]
|
| 2 | +use belt_hash::BeltHash; |
2 | 3 | use hex_literal::hex;
|
3 |
| -use pbkdf2::pbkdf2_hmac_array as f; |
4 | 4 | use sha1::Sha1;
|
5 | 5 | use streebog::Streebog512;
|
6 | 6 |
|
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: |
8 | 23 | /// https://www.rfc-editor.org/rfc/rfc6070
|
9 | 24 | #[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"; |
41 | 36 | );
|
42 | 37 | }
|
43 | 38 |
|
44 | 39 | /// 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 |
46 | 41 | #[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, |
51 | 46 | "64770af7f748c3b1c9ac831dbcfd85c2"
|
52 | 47 | "6111b30a8a657ddc3056b80ca73e040d"
|
53 | 48 | "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, |
61 | 51 | "5a585bafdfbb6e8830d6d68aa3b43ac0"
|
62 | 52 | "0d2e4aebce01c9b31c2caed56f0236d4"
|
63 | 53 | "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, |
71 | 56 | "e52deb9a2d2aaff4e2ac9d47a41f34c2"
|
72 | 57 | "0376591c67807f0477e32549dc341bc7"
|
73 | 58 | "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, |
98 | 67 | "b2d8f1245fc4d29274802057e4b54e0a"
|
99 | 68 | "0753aa22fc53760b301cf008679e58fe"
|
100 | 69 | "4bee9addcae99ba2b0b20f431a9c5e50"
|
101 | 70 | "f395c89387d0945aedeca6eb4015dfc2"
|
102 | 71 | "bd2421ee9bb71183ba882ceebfef259f"
|
103 | 72 | "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, |
111 | 75 | "50df062885b69801a3c10248eb0a27ab"
|
112 | 76 | "6e522ffeb20c991c660f001475d73a4e"
|
113 | 77 | "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"; |
116 | 92 | );
|
117 | 93 | }
|
0 commit comments