Skip to content

Commit 2b486f9

Browse files
authored
fix: add missing check_public validation in OAEP decrypt
1 parent ca31a4a commit 2b486f9

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/oaep.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ where
270270
D: Digest + FixedOutputReset,
271271
MGD: Digest + FixedOutputReset,
272272
{
273+
key::check_public(priv_key)?;
274+
273275
if ciphertext.len() != priv_key.size() {
274276
return Err(Error::Decryption);
275277
}
@@ -609,4 +611,47 @@ mod tests {
609611
"decrypt should have failed on hash verification"
610612
);
611613
}
614+
615+
#[test]
616+
#[cfg(feature = "hazmat")]
617+
fn test_decrypt_oaep_rejects_invalid_key() {
618+
use crate::algorithms::generate::generate_multi_prime_key_with_exp;
619+
use crate::errors::Error;
620+
621+
let mut rng = ChaCha8Rng::from_seed([42; 32]);
622+
623+
// Create a key with exponent larger than MAX_PUB_EXPONENT (2^33 - 1)
624+
// using the hazmat API that skips exponent size validation.
625+
let large_e = BoxedUint::from((1u64 << 34) + 1);
626+
let components =
627+
generate_multi_prime_key_with_exp(&mut rng, 2, 1024, large_e.clone()).unwrap();
628+
let priv_key = RsaPrivateKey::from_components_with_large_exponent(
629+
components.n.get(),
630+
components.e,
631+
components.d,
632+
components.primes,
633+
)
634+
.unwrap();
635+
636+
let dummy_ciphertext = vec![0u8; priv_key.size()];
637+
638+
// Decryption via PaddingScheme (uses oaep::decrypt) must reject
639+
// with PublicExponentTooLarge specifically — not a generic decryption error.
640+
let result = priv_key.decrypt(Oaep::<Sha256>::new(), &dummy_ciphertext);
641+
assert_eq!(
642+
result.unwrap_err(),
643+
Error::PublicExponentTooLarge,
644+
"decrypt via PaddingScheme should reject a key with oversized exponent"
645+
);
646+
647+
// Decryption via DecryptingKey (uses oaep::decrypt_digest) must also reject
648+
// with the same specific error.
649+
let decrypting_key = DecryptingKey::<Sha256>::new(priv_key);
650+
let result = decrypting_key.decrypt(&dummy_ciphertext);
651+
assert_eq!(
652+
result.unwrap_err(),
653+
Error::PublicExponentTooLarge,
654+
"decrypt via DecryptingKey should reject a key with oversized exponent"
655+
);
656+
}
612657
}

0 commit comments

Comments
 (0)