Skip to content

Commit 4fd795c

Browse files
committed
Support for cryptographic operations with larger keys
Currently, this crate allows instantiation of public keys larger than 4096 bit (via `RsaPublicKey::new_with_max_size`), but doing cryptographic operations with such public keys fails in `key::check_public`, which always checks the modulus size against the constant `RsaPublicKey::MAX_SIZE`. I think it would be nice to cap both public and private key sizes to 4096 bit by default, but to allow opt-in creation of larger keys (complete with working cryptographic operations).
1 parent d0405d2 commit 4fd795c

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/key.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl RsaPublicKey {
228228

229229
/// Create a new public key from its components.
230230
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)?;
231+
check_public_with_max_size(&n, &e, Some(max_size))?;
232232

233233
let n_odd = Odd::new(n.clone())
234234
.into_option()
@@ -666,14 +666,19 @@ impl PrivateKeyParts for RsaPrivateKey {
666666
/// Check that the public key is well formed and has an exponent within acceptable bounds.
667667
#[inline]
668668
pub fn check_public(public_key: &impl PublicKeyParts) -> Result<()> {
669-
check_public_with_max_size(public_key.n(), public_key.e(), RsaPublicKey::MAX_SIZE)
669+
check_public_with_max_size(public_key.n(), public_key.e(), None)
670670
}
671671

672672
/// Check that the public key is well formed and has an exponent within acceptable bounds.
673673
#[inline]
674-
fn check_public_with_max_size(n: &BoxedUint, e: &BoxedUint, max_size: usize) -> Result<()> {
675-
if n.bits_vartime() as usize > max_size {
676-
return Err(Error::ModulusTooLarge);
674+
fn check_public_with_max_size(
675+
n: &BoxedUint, e: &BoxedUint,
676+
max_size: Option<usize>,
677+
) -> Result<()> {
678+
if let Some(max_size) = max_size {
679+
if n.bits_vartime() as usize > max_size {
680+
return Err(Error::ModulusTooLarge);
681+
}
677682
}
678683

679684
if e >= n || n.is_even().into() || n.is_zero().into() {

0 commit comments

Comments
 (0)