Move PBKDF2 functional tests to tests/.
This commit is contained in:
parent
eb73404257
commit
a04bdddf4d
@ -100,7 +100,6 @@ include = [
|
|||||||
"src/init.rs",
|
"src/init.rs",
|
||||||
"src/lib.rs",
|
"src/lib.rs",
|
||||||
"src/pbkdf2.rs",
|
"src/pbkdf2.rs",
|
||||||
"src/pbkdf2_tests.txt",
|
|
||||||
"src/pkcs8.rs",
|
"src/pkcs8.rs",
|
||||||
"src/poly1305.rs",
|
"src/poly1305.rs",
|
||||||
"src/poly1305_test.txt",
|
"src/poly1305_test.txt",
|
||||||
@ -245,6 +244,8 @@ include = [
|
|||||||
"tests/hkdf_tests.txt",
|
"tests/hkdf_tests.txt",
|
||||||
"tests/hmac_test.rs",
|
"tests/hmac_test.rs",
|
||||||
"tests/hmac_tests.txt",
|
"tests/hmac_tests.txt",
|
||||||
|
"tests/pbkdf2_tests.rs",
|
||||||
|
"tests/pbkdf2_tests.txt",
|
||||||
"tests/rsa_from_pkcs8_tests.txt",
|
"tests/rsa_from_pkcs8_tests.txt",
|
||||||
"tests/rsa_pkcs1_sign_tests.txt",
|
"tests/rsa_pkcs1_sign_tests.txt",
|
||||||
"tests/rsa_pkcs1_verify_tests.txt",
|
"tests/rsa_pkcs1_verify_tests.txt",
|
||||||
|
@ -262,69 +262,3 @@ pub static HMAC_SHA256: PRF = PRF { digest_alg: &digest::SHA256 };
|
|||||||
|
|
||||||
/// HMAC-SHA512.
|
/// HMAC-SHA512.
|
||||||
pub static HMAC_SHA512: PRF = PRF { digest_alg: &digest::SHA512 };
|
pub static HMAC_SHA512: PRF = PRF { digest_alg: &digest::SHA512 };
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use {error, pbkdf2, test};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
pub fn pkbdf2_tests() {
|
|
||||||
test::from_file("src/pbkdf2_tests.txt", |section, test_case| {
|
|
||||||
assert_eq!(section, "");
|
|
||||||
let prf_digest_alg = &test_case.consume_string("Hash");
|
|
||||||
let prf = match prf_digest_alg.as_ref() {
|
|
||||||
"SHA256" => &pbkdf2::HMAC_SHA256,
|
|
||||||
"SHA512" => &pbkdf2::HMAC_SHA512,
|
|
||||||
_ => panic!("Unexpected digest algorithm in PBKDF2 test"),
|
|
||||||
};
|
|
||||||
let iterations = test_case.consume_usize("c");
|
|
||||||
let secret = test_case.consume_bytes("P");
|
|
||||||
let salt = test_case.consume_bytes("S");
|
|
||||||
let dk = test_case.consume_bytes("DK");
|
|
||||||
let verify_expected_result = test_case.consume_string("Verify");
|
|
||||||
let verify_expected_result =
|
|
||||||
match verify_expected_result.as_str() {
|
|
||||||
"OK" => Ok(()),
|
|
||||||
"Err" => Err(error::Unspecified),
|
|
||||||
_ => panic!("Unsupported value of \"Verify\""),
|
|
||||||
};
|
|
||||||
|
|
||||||
{
|
|
||||||
let mut out = vec![0u8; dk.len()];
|
|
||||||
pbkdf2::derive(prf, iterations as u32, &salt, &secret,
|
|
||||||
&mut out);
|
|
||||||
assert_eq!(dk == out,
|
|
||||||
verify_expected_result.is_ok() || dk.is_empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(pbkdf2::verify(prf, iterations as u32, &salt, &secret,
|
|
||||||
&dk),
|
|
||||||
verify_expected_result);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[should_panic]
|
|
||||||
pub fn pkbdf2_zero_iterations() {
|
|
||||||
let prf = &pbkdf2::HMAC_SHA256;
|
|
||||||
let secret = "ZeroIterationsTest".as_bytes();
|
|
||||||
let iterations: u32 = 0;
|
|
||||||
let salt = "salt".as_bytes();
|
|
||||||
let mut out = vec![0u8; 2];
|
|
||||||
pbkdf2::derive(prf, iterations, &salt, &secret, &mut out);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
// Control for pkbdf2_zero_iterations
|
|
||||||
pub fn pkbdf2_one_iteration() {
|
|
||||||
let prf = &pbkdf2::HMAC_SHA256;
|
|
||||||
let secret = "ZeroIterationsTest".as_bytes();
|
|
||||||
let iterations: u32 = 1;
|
|
||||||
let salt = "salt".as_bytes();
|
|
||||||
let mut out = vec![0u8; 2];
|
|
||||||
pbkdf2::derive(prf, iterations, &salt, &secret, &mut out);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
77
tests/pbkdf2_tests.rs
Normal file
77
tests/pbkdf2_tests.rs
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
// Copyright 2015-2017 Brian Smith.
|
||||||
|
//
|
||||||
|
// Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
// purpose with or without fee is hereby granted, provided that the above
|
||||||
|
// copyright notice and this permission notice appear in all copies.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
|
||||||
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||||
|
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||||
|
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||||
|
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
|
extern crate ring;
|
||||||
|
|
||||||
|
use ring::{error, pbkdf2, test};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn pbkdf2_tests() {
|
||||||
|
test::from_file("tests/pbkdf2_tests.txt", |section, test_case| {
|
||||||
|
assert_eq!(section, "");
|
||||||
|
let prf_digest_alg = &test_case.consume_string("Hash");
|
||||||
|
let prf = match prf_digest_alg.as_ref() {
|
||||||
|
"SHA256" => &pbkdf2::HMAC_SHA256,
|
||||||
|
"SHA512" => &pbkdf2::HMAC_SHA512,
|
||||||
|
_ => panic!("Unexpected digest algorithm in PBKDF2 test"),
|
||||||
|
};
|
||||||
|
let iterations = test_case.consume_usize("c");
|
||||||
|
let secret = test_case.consume_bytes("P");
|
||||||
|
let salt = test_case.consume_bytes("S");
|
||||||
|
let dk = test_case.consume_bytes("DK");
|
||||||
|
let verify_expected_result = test_case.consume_string("Verify");
|
||||||
|
let verify_expected_result =
|
||||||
|
match verify_expected_result.as_str() {
|
||||||
|
"OK" => Ok(()),
|
||||||
|
"Err" => Err(error::Unspecified),
|
||||||
|
_ => panic!("Unsupported value of \"Verify\""),
|
||||||
|
};
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut out = vec![0u8; dk.len()];
|
||||||
|
pbkdf2::derive(prf, iterations as u32, &salt, &secret,
|
||||||
|
&mut out);
|
||||||
|
assert_eq!(dk == out,
|
||||||
|
verify_expected_result.is_ok() || dk.is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!(pbkdf2::verify(prf, iterations as u32, &salt, &secret,
|
||||||
|
&dk),
|
||||||
|
verify_expected_result);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
pub fn pbkdf2_zero_iterations() {
|
||||||
|
let prf = &pbkdf2::HMAC_SHA256;
|
||||||
|
let secret = "ZeroIterationsTest".as_bytes();
|
||||||
|
let iterations: u32 = 0;
|
||||||
|
let salt = "salt".as_bytes();
|
||||||
|
let mut out = vec![0u8; 2];
|
||||||
|
pbkdf2::derive(prf, iterations, &salt, &secret, &mut out);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Control for pkbdf2_zero_iterations
|
||||||
|
#[test]
|
||||||
|
pub fn pbkdf2_one_iteration() {
|
||||||
|
let prf = &pbkdf2::HMAC_SHA256;
|
||||||
|
let secret = "ZeroIterationsTest".as_bytes();
|
||||||
|
let iterations: u32 = 1;
|
||||||
|
let salt = "salt".as_bytes();
|
||||||
|
let mut out = vec![0u8; 2];
|
||||||
|
pbkdf2::derive(prf, iterations, &salt, &secret, &mut out);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user