@@ -272,19 +272,62 @@ impl RsaPrivateKey {
272
272
/// Default exponent for RSA keys.
273
273
const EXP : u64 = 65537 ;
274
274
275
- /// Generate a new Rsa key pair of the given bit size using the passed in `rng`.
276
- pub fn new < R : CryptoRng + ?Sized > ( rng : & mut R , bit_size : usize ) -> Result < RsaPrivateKey > {
277
- Self :: new_with_exp ( rng, bit_size, BoxedUint :: from ( Self :: EXP ) )
275
+ /// Minimum size of the modulus `n` in bits. Currently only applies to keygen.
276
+ const MIN_SIZE : u32 = 1024 ;
277
+
278
+ /// Generate a new RSA key pair with a modulus of the given bit size using the passed in `rng`.
279
+ ///
280
+ /// # Errors
281
+ /// - If `bit_size` is lower than the minimum 1024-bits.
282
+ pub fn new < R : CryptoRng + ?Sized > ( rng : & mut R , bit_size : usize ) -> Result < Self > {
283
+ Self :: new_with_exp ( rng, bit_size, Self :: EXP . into ( ) )
284
+ }
285
+
286
+ /// Generate a new RSA key pair of the given bit size.
287
+ ///
288
+ /// #⚠️Warning: Hazmat!
289
+ /// This version does not apply minimum key size checks, and as such may generate keys
290
+ /// which are insecure!
291
+ #[ cfg( feature = "hazmat" ) ]
292
+ pub fn new_unchecked < R : CryptoRng + ?Sized > ( rng : & mut R , bit_size : usize ) -> Result < Self > {
293
+ Self :: new_with_exp_unchecked ( rng, bit_size, Self :: EXP . into ( ) )
278
294
}
279
295
280
296
/// Generate a new RSA key pair of the given bit size and the public exponent
281
297
/// using the passed in `rng`.
282
298
///
283
- /// Unless you have specific needs, you should use `RsaPrivateKey::new` instead.
299
+ /// Unless you have specific needs, you should use [ `RsaPrivateKey::new`] instead.
284
300
pub fn new_with_exp < R : CryptoRng + ?Sized > (
285
301
rng : & mut R ,
286
302
bit_size : usize ,
287
303
exp : BoxedUint ,
304
+ ) -> Result < RsaPrivateKey > {
305
+ if bit_size < Self :: MIN_SIZE as usize {
306
+ return Err ( Error :: ModulusTooSmall ) ;
307
+ }
308
+
309
+ let components = generate_multi_prime_key_with_exp ( rng, 2 , bit_size, exp) ?;
310
+ RsaPrivateKey :: from_components (
311
+ components. n . get ( ) ,
312
+ components. e ,
313
+ components. d ,
314
+ components. primes ,
315
+ )
316
+ }
317
+
318
+ /// Generate a new RSA key pair of the given bit size and the public exponent
319
+ /// using the passed in `rng`.
320
+ ///
321
+ /// Unless you have specific needs, you should use [`RsaPrivateKey::new`] instead.
322
+ ///
323
+ /// #⚠️Warning: Hazmat!
324
+ /// This version does not apply minimum key size checks, and as such may generate keys
325
+ /// which are insecure!
326
+ #[ cfg( feature = "hazmat" ) ]
327
+ pub fn new_with_exp_unchecked < R : CryptoRng + ?Sized > (
328
+ rng : & mut R ,
329
+ bit_size : usize ,
330
+ exp : BoxedUint ,
288
331
) -> Result < RsaPrivateKey > {
289
332
let components = generate_multi_prime_key_with_exp ( rng, 2 , bit_size, exp) ?;
290
333
RsaPrivateKey :: from_components (
@@ -821,13 +864,13 @@ mod tests {
821
864
}
822
865
823
866
#[ test]
824
- #[ cfg( feature = "serde" ) ]
867
+ #[ cfg( all ( feature = "hazmat" , feature = " serde") ) ]
825
868
fn test_serde ( ) {
826
869
use rand_chacha:: { rand_core:: SeedableRng , ChaCha8Rng } ;
827
870
use serde_test:: { assert_tokens, Configure , Token } ;
828
871
829
872
let mut rng = ChaCha8Rng :: from_seed ( [ 42 ; 32 ] ) ;
830
- let priv_key = RsaPrivateKey :: new ( & mut rng, 64 ) . expect ( "failed to generate key" ) ;
873
+ let priv_key = RsaPrivateKey :: new_unchecked ( & mut rng, 64 ) . expect ( "failed to generate key" ) ;
831
874
832
875
let priv_tokens = [ Token :: Str ( concat ! (
833
876
"3056020100300d06092a864886f70d010101050004423040020100020900a" ,
0 commit comments