Skip to content

Commit c1f00a3

Browse files
authored
Support for cryptographic operations with larger keys (#594)
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 85f03b5 commit c1f00a3

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/key.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl RsaPublicKey {
184184
/// Create a new public key from its components.
185185
pub fn new_with_max_size(n: BigUint, e: BigUint, max_size: usize) -> Result<Self> {
186186
let k = Self { n, e };
187-
check_public_with_max_size(&k, max_size)?;
187+
check_public_with_max_size(&k, Some(max_size))?;
188188
Ok(k)
189189
}
190190

@@ -269,7 +269,7 @@ impl RsaPrivateKey {
269269
precomputed: None,
270270
};
271271

272-
// Alaways validate the key, to ensure precompute can't fail
272+
// Always validate the key, to ensure precompute can't fail
273273
k.validate()?;
274274

275275
// precompute when possible, ignore error otherwise.
@@ -493,14 +493,19 @@ impl PrivateKeyParts for RsaPrivateKey {
493493
/// Check that the public key is well formed and has an exponent within acceptable bounds.
494494
#[inline]
495495
pub fn check_public(public_key: &impl PublicKeyParts) -> Result<()> {
496-
check_public_with_max_size(public_key, RsaPublicKey::MAX_SIZE)
496+
check_public_with_max_size(public_key, None)
497497
}
498498

499499
/// Check that the public key is well formed and has an exponent within acceptable bounds.
500500
#[inline]
501-
fn check_public_with_max_size(public_key: &impl PublicKeyParts, max_size: usize) -> Result<()> {
502-
if public_key.n().bits() > max_size {
503-
return Err(Error::ModulusTooLarge);
501+
fn check_public_with_max_size(
502+
public_key: &impl PublicKeyParts,
503+
max_size: Option<usize>,
504+
) -> Result<()> {
505+
if let Some(max_size) = max_size {
506+
if public_key.n().bits() > max_size {
507+
return Err(Error::ModulusTooLarge);
508+
}
504509
}
505510

506511
let e = public_key

0 commit comments

Comments
 (0)