Fix RSA bug that I did not generate random primes for p and q

This commit is contained in:
2022-10-27 18:57:34 -04:00
parent 8a87bafe02
commit 093697ec2c
3 changed files with 58 additions and 28 deletions

View File

@@ -16,7 +16,8 @@ pub fn challenge41() -> Option<()> {
public_key.n.rand_range(&mut s).ok()?;
// C' = ((S**E mod N) C) mod N
let mut c2 = BigNum::new().ok()?;
c2.mod_exp(&s, &public_key.e, &public_key.n, &mut ctx).ok()?;
c2.mod_exp(&s, &public_key.e, &public_key.n, &mut ctx)
.ok()?;
let c2 = &(&c2 * &c) % &public_key.n;
let p2 = rsa::rsa_decrypt(&c2, &private_key).ok()?;
@@ -29,3 +30,15 @@ pub fn challenge41() -> Option<()> {
println!("[okay] Challenge 41: implement unpadded message recovery oracle");
Some(())
}
pub fn challenge42() -> Option<()> {
let (public_key, private_key) = rsa::rsa_gen_keys().ok()?;
let i = BigNum::from_u32(1337).ok()?;
let c = rsa::rsa_encrypt(&i, &public_key).ok()?;
let m = rsa::rsa_decrypt(&c, &private_key).ok()?;
assert_eq!(i, m, "rsa is broken");
println!("[xxxx] Challenge 42: Bleichenbacher's e=3 RSA Attack");
None
}