@@ -251,7 +251,6 @@ impl RsaPrivateKey {
251
251
d : BigUint ,
252
252
mut primes : Vec < BigUint > ,
253
253
) -> Result < RsaPrivateKey > {
254
- let mut should_validate = false ;
255
254
if primes. len ( ) < 2 {
256
255
if !primes. is_empty ( ) {
257
256
return Err ( Error :: NprimesTooSmall ) ;
@@ -261,7 +260,6 @@ impl RsaPrivateKey {
261
260
let ( p, q) = recover_primes ( & n, & e, & d) ?;
262
261
primes. push ( p) ;
263
262
primes. push ( q) ;
264
- should_validate = true ;
265
263
}
266
264
267
265
let mut k = RsaPrivateKey {
@@ -271,10 +269,8 @@ impl RsaPrivateKey {
271
269
precomputed : None ,
272
270
} ;
273
271
274
- // Validate the key if we had to recover the primes.
275
- if should_validate {
276
- k. validate ( ) ?;
277
- }
272
+ // Alaways validate the key, to ensure precompute can't fail
273
+ k. validate ( ) ?;
278
274
279
275
// precompute when possible, ignore error otherwise.
280
276
let _ = k. precompute ( ) ;
@@ -717,13 +713,13 @@ mod tests {
717
713
Base64 :: decode_vec( "CUWC+hRWOT421kwRllgVjy6FYv6jQUcgDNHeAiYZnf5HjS9iK2ki7v8G5dL/0f+Yf+NhE/4q8w4m8go51hACrVpP1p8GJDjiT09+RsOzITsHwl+ceEKoe56ZW6iDHBLlrNw5/MtcYhKpjNU9KJ2udm5J/c9iislcjgckrZG2IB8ADgXHMEByZ5DgaMl4AKZ1Gx8/q6KftTvmOT5rNTMLi76VN5KWQcDWK/DqXiOiZHM7Nr4dX4me3XeRgABJyNR8Fqxj3N1+HrYLe/zs7LOaK0++F9Ul3tLelhrhsvLxei3oCZkF9A/foD3on3luYA+1cRcxWpSY3h2J4/22+yo4+Q==" ) . unwrap( ) ,
718
714
] ;
719
715
720
- RsaPrivateKey :: from_components (
716
+ let res = RsaPrivateKey :: from_components (
721
717
BigUint :: from_bytes_be ( & n) ,
722
718
BigUint :: from_bytes_be ( & e) ,
723
719
BigUint :: from_bytes_be ( & d) ,
724
720
primes. iter ( ) . map ( |p| BigUint :: from_bytes_be ( p) ) . collect ( ) ,
725
- )
726
- . unwrap ( ) ;
721
+ ) ;
722
+ assert_eq ! ( res , Err ( Error :: InvalidModulus ) ) ;
727
723
}
728
724
729
725
#[ test]
0 commit comments