1 // Copyright 2015-2017 Brian Smith.
2 //
3 // Permission to use, copy, modify, and/or distribute this software for any
4 // purpose with or without fee is hereby granted, provided that the above
5 // copyright notice and this permission notice appear in all copies.
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
8 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
10 // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15 #![forbid(
16 anonymous_parameters,
17 box_pointers,
18 missing_copy_implementations,
19 missing_debug_implementations,
20 missing_docs,
21 trivial_casts,
22 trivial_numeric_casts,
23 unsafe_code,
24 unstable_features,
25 unused_extern_crates,
26 unused_import_braces,
27 unused_qualifications,
28 unused_results,
29 variant_size_differences,
30 warnings
31 )]
32
33 use core::num::NonZeroU32;
34 use ring::{digest, error, pbkdf2, test, test_file};
35
36 #[cfg(target_arch = "wasm32")]
37 use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure};
38
39 #[cfg(target_arch = "wasm32")]
40 wasm_bindgen_test_configure!(run_in_browser);
41
42 /// Test vectors from BoringSSL, Go, and other sources.
43 #[test]
44 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
pbkdf2_tests()45 pub fn pbkdf2_tests() {
46 test::run(test_file!("pbkdf2_tests.txt"), |section, test_case| {
47 assert_eq!(section, "");
48 let algorithm = {
49 let digest_alg = test_case.consume_digest_alg("Hash").unwrap();
50 if digest_alg == &digest::SHA1_FOR_LEGACY_USE_ONLY {
51 pbkdf2::PBKDF2_HMAC_SHA1
52 } else if digest_alg == &digest::SHA256 {
53 pbkdf2::PBKDF2_HMAC_SHA256
54 } else if digest_alg == &digest::SHA384 {
55 pbkdf2::PBKDF2_HMAC_SHA384
56 } else if digest_alg == &digest::SHA512 {
57 pbkdf2::PBKDF2_HMAC_SHA512
58 } else {
59 unreachable!()
60 }
61 };
62 let iterations = test_case.consume_usize("c");
63 let iterations = NonZeroU32::new(iterations as u32).unwrap();
64 let secret = test_case.consume_bytes("P");
65 let salt = test_case.consume_bytes("S");
66 let dk = test_case.consume_bytes("DK");
67 let verify_expected_result = test_case.consume_string("Verify");
68 let verify_expected_result = match verify_expected_result.as_str() {
69 "OK" => Ok(()),
70 "Err" => Err(error::Unspecified),
71 _ => panic!("Unsupported value of \"Verify\""),
72 };
73
74 {
75 let mut out = vec![0u8; dk.len()];
76 pbkdf2::derive(algorithm, iterations, &salt, &secret, &mut out);
77 assert_eq!(dk == out, verify_expected_result.is_ok() || dk.is_empty());
78 }
79
80 #[cfg(any(not(target_arch = "wasm32"), feature = "wasm32_c"))]
81 assert_eq!(
82 pbkdf2::verify(algorithm, iterations, &salt, &secret, &dk),
83 verify_expected_result
84 );
85
86 Ok(())
87 });
88 }
89