Move agreement functional tests to tests/agreement_tests.rs.

Also remove ring::agreement::EphemeralKeyPair::from_test_vector().
This commit is contained in:
Brian Smith 2017-06-08 11:11:05 -10:00
parent 8dc0b9811c
commit f95cf64e57
5 changed files with 89 additions and 91 deletions

View File

@ -53,7 +53,6 @@ include = [
"src/digest/digest.rs",
"src/digest/sha1.rs",
"src/ec/ec.rs",
"src/ec/ecdh_tests.txt",
"src/ec/curve25519/curve25519.rs",
"src/ec/curve25519/ed25519.rs",
"src/ec/curve25519/ed25519_pkcs8_v2_template.der",
@ -230,6 +229,8 @@ include = [
"examples/checkdigest.rs",
"tests/aead_aes_128_gcm_tests.txt",
"tests/aead_aes_256_gcm_tests.txt",
"tests/agreement_tests.rs",
"tests/agreement_tests.txt",
"tests/digest_tests.rs",
"tests/digest_tests.txt",
"tests/ecdsa_from_pkcs8_tests.txt",

View File

@ -1,4 +1,4 @@
// Copyright 2015-2016 Brian Smith.
// 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
@ -118,18 +118,6 @@ impl<'a> EphemeralPrivateKey {
Ok(EphemeralPrivateKey { private_key, alg })
}
#[cfg(test)]
pub fn from_test_vector(alg: &'static Algorithm,
test_vector: untrusted::Input)
-> EphemeralPrivateKey {
EphemeralPrivateKey {
private_key:
ec::PrivateKey::from_bytes(&alg.i.curve, test_vector)
.unwrap(),
alg: alg,
}
}
/// The key exchange algorithm.
#[inline]
pub fn algorithm(&self) -> &'static Algorithm { self.alg }
@ -227,78 +215,3 @@ pub fn agree_ephemeral<F, R, E>(my_private_key: EphemeralPrivateKey,
// "Destroy" that doesn't meet the NSA requirement to "zeroize."
kdf(shared_key)
}
#[cfg(test)]
mod tests {
use {test, rand};
use untrusted;
use super::*;
#[test]
fn test_agreement_agree_ephemeral() {
let rng = rand::SystemRandom::new();
test::from_file("src/ec/ecdh_tests.txt", |section, test_case| {
assert_eq!(section, "");
let curve_name = test_case.consume_string("Curve");
let alg = alg_from_curve_name(&curve_name);
let peer_public = test_case.consume_bytes("PeerQ");
let peer_public = untrusted::Input::from(&peer_public);
match test_case.consume_optional_string("Error") {
None => {
let my_private = test_case.consume_bytes("D");
let my_public = test_case.consume_bytes("MyQ");
let output = test_case.consume_bytes("Output");
let private_key =
EphemeralPrivateKey::from_test_vector(
alg, untrusted::Input::from(&my_private));
let mut computed_public = [0u8; PUBLIC_KEY_MAX_LEN];
let computed_public =
&mut computed_public[..private_key.public_key_len()];
assert!(
private_key.compute_public_key(computed_public).is_ok());
assert_eq!(computed_public, &my_public[..]);
assert!(agree_ephemeral(private_key, alg, peer_public, (),
|key_material| {
assert_eq!(key_material, &output[..]);
Ok(())
}).is_ok());
},
Some(_) => {
// In the no-heap mode, some algorithms aren't supported so
// we have to skip those algorithms' test cases.
let dummy_private_key =
EphemeralPrivateKey::generate(alg, &rng)?;
fn kdf_not_called(_: &[u8]) -> Result<(), ()> {
panic!("The KDF was called during ECDH when the peer's \
public key is invalid.");
}
assert!(
agree_ephemeral(dummy_private_key, alg, peer_public,
(), kdf_not_called).is_err());
}
}
return Ok(());
});
}
fn alg_from_curve_name(curve_name: &str) -> &'static Algorithm {
if curve_name == "P-256" {
&ECDH_P256
} else if curve_name == "P-384" {
&ECDH_P384
} else if curve_name == "X25519" {
&X25519
} else {
panic!("Unsupported curve: {}", curve_name);
}
}
}

View File

@ -172,9 +172,9 @@ mod tests {
fn x25519_(private_key: &[u8], public_key: &[u8])
-> Result<std::vec::Vec<u8>, error::Unspecified> {
let rng = test::rand::FixedSliceRandom { bytes: private_key };
let private_key =
agreement::EphemeralPrivateKey::from_test_vector(
&agreement::X25519, untrusted::Input::from(private_key));
agreement::EphemeralPrivateKey::generate(&agreement::X25519, &rng)?;
let public_key = untrusted::Input::from(public_key);
agreement::agree_ephemeral(private_key, &agreement::X25519, public_key,
error::Unspecified, |agreed_value| {

84
tests/agreement_tests.rs Normal file
View File

@ -0,0 +1,84 @@
// 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;
extern crate untrusted;
use ring::{agreement, rand, test};
#[test]
fn agreement_agree_ephemeral() {
let rng = rand::SystemRandom::new();
test::from_file("tests/agreement_tests.txt", |section, test_case| {
assert_eq!(section, "");
let curve_name = test_case.consume_string("Curve");
let alg = alg_from_curve_name(&curve_name);
let peer_public = test_case.consume_bytes("PeerQ");
let peer_public = untrusted::Input::from(&peer_public);
match test_case.consume_optional_string("Error") {
None => {
let my_private = test_case.consume_bytes("D");
let rng = test::rand::FixedSliceRandom { bytes: &my_private };
let my_private =
agreement::EphemeralPrivateKey::generate(alg, &rng)?;
let my_public = test_case.consume_bytes("MyQ");
let output = test_case.consume_bytes("Output");
let mut computed_public = [0u8; agreement::PUBLIC_KEY_MAX_LEN];
let computed_public =
&mut computed_public[..my_private.public_key_len()];
assert!(my_private.compute_public_key(computed_public).is_ok());
assert_eq!(computed_public, &my_public[..]);
assert!(agreement::agree_ephemeral(my_private, alg, peer_public,
(), |key_material| {
assert_eq!(key_material, &output[..]);
Ok(())
}).is_ok());
},
Some(_) => {
// In the no-heap mode, some algorithms aren't supported so
// we have to skip those algorithms' test cases.
let dummy_private_key =
agreement::EphemeralPrivateKey::generate(alg, &rng)?;
fn kdf_not_called(_: &[u8]) -> Result<(), ()> {
panic!("The KDF was called during ECDH when the peer's \
public key is invalid.");
}
assert!(agreement::agree_ephemeral(dummy_private_key, alg,
peer_public, (),
kdf_not_called).is_err());
}
}
return Ok(());
});
}
fn alg_from_curve_name(curve_name: &str) -> &'static agreement::Algorithm {
if curve_name == "P-256" {
&agreement::ECDH_P256
} else if curve_name == "P-384" {
&agreement::ECDH_P384
} else if curve_name == "X25519" {
&agreement::X25519
} else {
panic!("Unsupported curve: {}", curve_name);
}
}