@@ -2,6 +2,7 @@ use alloc::vec::Vec;
2
2
use core:: cmp:: Ordering ;
3
3
use core:: fmt;
4
4
use core:: hash:: { Hash , Hasher } ;
5
+ use core:: ops:: Range ;
5
6
6
7
use crypto_bigint:: modular:: { BoxedMontyForm , BoxedMontyParams } ;
7
8
use crypto_bigint:: { BoxedUint , Integer , NonZero , Odd , Resize } ;
@@ -215,20 +216,30 @@ impl RsaPublicKey {
215
216
/// Maximum value of the public exponent `e`.
216
217
pub const MAX_PUB_EXPONENT : u64 = ( 1 << 33 ) - 1 ;
217
218
218
- /// Maximum size of the modulus `n` in bits.
219
+ /// Default minimum size of the modulus `n` in bits.
220
+ pub const MIN_SIZE : usize = 1024 ;
221
+
222
+ /// Default maximum size of the modulus `n` in bits.
219
223
pub const MAX_SIZE : usize = 4096 ;
220
224
221
225
/// Create a new public key from its components.
222
226
///
223
227
/// This function accepts public keys with a modulus size up to 4096-bits,
224
228
/// i.e. [`RsaPublicKey::MAX_SIZE`].
225
229
pub fn new ( n : BoxedUint , e : BoxedUint ) -> Result < Self > {
226
- Self :: new_with_max_size ( n, e, Self :: MAX_SIZE )
230
+ Self :: new_with_size_limits ( n, e, Self :: MIN_SIZE .. Self :: MAX_SIZE )
227
231
}
228
232
229
233
/// Create a new public key from its components.
230
- pub fn new_with_max_size ( n : BoxedUint , e : BoxedUint , max_size : usize ) -> Result < Self > {
231
- check_public_with_max_size ( & n, & e, max_size) ?;
234
+ ///
235
+ /// Accepts a third argument which specifies a range of allowed sizes from minimum to maximum
236
+ /// in bits, which by default is `1024..4096`.
237
+ pub fn new_with_size_limits (
238
+ n : BoxedUint ,
239
+ e : BoxedUint ,
240
+ size_range_bits : Range < usize > ,
241
+ ) -> Result < Self > {
242
+ check_public_with_size_range ( & n, & e, size_range_bits) ?;
232
243
233
244
let n_odd = Odd :: new ( n. clone ( ) )
234
245
. into_option ( )
@@ -244,7 +255,7 @@ impl RsaPublicKey {
244
255
///
245
256
/// This method is not recommended, and only intended for unusual use cases.
246
257
/// Most applications should use [`RsaPublicKey::new`] or
247
- /// [`RsaPublicKey::new_with_max_size `] instead.
258
+ /// [`RsaPublicKey::new_with_size_limits `] instead.
248
259
pub fn new_unchecked ( n : BoxedUint , e : BoxedUint ) -> Self {
249
260
let n_odd = Odd :: new ( n. clone ( ) ) . expect ( "n must be odd" ) ;
250
261
let n_params = BoxedMontyParams :: new ( n_odd) ;
@@ -613,16 +624,28 @@ impl PrivateKeyParts for RsaPrivateKey {
613
624
}
614
625
}
615
626
616
- /// Check that the public key is well formed and has an exponent within acceptable bounds.
627
+ /// Check that the public key is well- formed and has an exponent within acceptable bounds.
617
628
#[ inline]
618
629
pub fn check_public ( public_key : & impl PublicKeyParts ) -> Result < ( ) > {
619
- check_public_with_max_size ( public_key. n ( ) , public_key. e ( ) , RsaPublicKey :: MAX_SIZE )
630
+ check_public_with_size_range (
631
+ public_key. n ( ) ,
632
+ public_key. e ( ) ,
633
+ RsaPublicKey :: MIN_SIZE ..RsaPublicKey :: MAX_SIZE ,
634
+ )
620
635
}
621
636
622
- /// Check that the public key is well formed and has an exponent within acceptable bounds.
637
+ /// Check that the public key is well- formed and has an exponent within acceptable bounds.
623
638
#[ inline]
624
- fn check_public_with_max_size ( n : & BoxedUint , e : & BoxedUint , max_size : usize ) -> Result < ( ) > {
625
- if n. bits_precision ( ) as usize > max_size {
639
+ fn check_public_with_size_range (
640
+ n : & BoxedUint ,
641
+ e : & BoxedUint ,
642
+ size_range_bits : Range < usize > ,
643
+ ) -> Result < ( ) > {
644
+ if ( n. bits_precision ( ) as usize ) < size_range_bits. start {
645
+ return Err ( Error :: ModulusTooSmall ) ;
646
+ }
647
+
648
+ if ( n. bits_precision ( ) as usize ) > size_range_bits. end {
626
649
return Err ( Error :: ModulusTooLarge ) ;
627
650
}
628
651
0 commit comments