@@ -764,7 +764,8 @@ fn x25519_diffie_hellman<'a>(
764
764
#[ cfg( test) ]
765
765
mod tests {
766
766
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 ,
768
769
} ;
769
770
use crate :: encoding:: {
770
771
AsBigEndian , AsDer , Curve25519SeedBin , EcPrivateKeyBin , EcPrivateKeyRfc5915Der ,
@@ -830,6 +831,36 @@ mod tests {
830
831
}
831
832
}
832
833
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
+
833
864
#[ test]
834
865
fn test_agreement_ecdh_p256 ( ) {
835
866
let alg = & agreement:: ECDH_P256 ;
0 commit comments