@@ -272,19 +272,62 @@ impl RsaPrivateKey {
272272 /// Default exponent for RSA keys.
273273 const EXP : u64 = 65537 ;
274274
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 ( ) )
278294 }
279295
280296 /// Generate a new RSA key pair of the given bit size and the public exponent
281297 /// using the passed in `rng`.
282298 ///
283- /// Unless you have specific needs, you should use `RsaPrivateKey::new` instead.
299+ /// Unless you have specific needs, you should use [ `RsaPrivateKey::new`] instead.
284300 pub fn new_with_exp < R : CryptoRng + ?Sized > (
285301 rng : & mut R ,
286302 bit_size : usize ,
287303 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 ,
288331 ) -> Result < RsaPrivateKey > {
289332 let components = generate_multi_prime_key_with_exp ( rng, 2 , bit_size, exp) ?;
290333 RsaPrivateKey :: from_components (
@@ -821,13 +864,13 @@ mod tests {
821864 }
822865
823866 #[ test]
824- #[ cfg( feature = "serde" ) ]
867+ #[ cfg( all ( feature = "hazmat" , feature = " serde") ) ]
825868 fn test_serde ( ) {
826869 use rand_chacha:: { rand_core:: SeedableRng , ChaCha8Rng } ;
827870 use serde_test:: { assert_tokens, Configure , Token } ;
828871
829872 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" ) ;
831874
832875 let priv_tokens = [ Token :: Str ( concat ! (
833876 "3056020100300d06092a864886f70d010101050004423040020100020900a" ,
0 commit comments