Skip to content

Commit 4960ee4

Browse files
committed
Add test w/ invalid inputs
1 parent 7c68274 commit 4960ee4

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

aws-lc-rs/src/agreement.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,8 @@ fn x25519_diffie_hellman<'a>(
764764
#[cfg(test)]
765765
mod tests {
766766
use crate::agreement::{
767-
PrivateKey, PublicKey, UnparsedPublicKey, ECDH_P256, ECDH_P384, ECDH_P521, X25519,
767+
agree, Algorithm, PrivateKey, PublicKey, UnparsedPublicKey, ECDH_P256, ECDH_P384,
768+
ECDH_P521, X25519,
768769
};
769770
use crate::encoding::{
770771
AsBigEndian, AsDer, Curve25519SeedBin, EcPrivateKeyBin, EcPrivateKeyRfc5915Der,
@@ -830,6 +831,36 @@ mod tests {
830831
}
831832
}
832833

834+
#[test]
835+
fn test_agreement_invalid_keys() {
836+
fn test_with_key(alg: &'static Algorithm, my_private_key: &PrivateKey, test_key: &[u8]) {
837+
assert!(PrivateKey::from_private_key(alg, test_key).is_err());
838+
assert!(PrivateKey::from_private_key_der(alg, test_key).is_err());
839+
assert!(agree(
840+
my_private_key,
841+
&UnparsedPublicKey::new(alg, test_key),
842+
(),
843+
|_| Ok(())
844+
)
845+
.is_err());
846+
}
847+
848+
let alg_variants: [&'static Algorithm; 4] = [&X25519, &ECDH_P256, &ECDH_P384, &ECDH_P521];
849+
850+
for alg in alg_variants {
851+
let my_private_key = PrivateKey::generate(alg).unwrap();
852+
853+
let empty_key = [];
854+
test_with_key(alg, &my_private_key, &empty_key);
855+
856+
let wrong_size_key: [u8; 31] = [
857+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
858+
23, 24, 25, 26, 27, 28, 29, 30,
859+
];
860+
test_with_key(alg, &my_private_key, &wrong_size_key);
861+
}
862+
}
863+
833864
#[test]
834865
fn test_agreement_ecdh_p256() {
835866
let alg = &agreement::ECDH_P256;

0 commit comments

Comments
 (0)