Finish Challenge 64 RSA parity oracle

This commit is contained in:
2023-01-19 19:57:14 -05:00
parent 365bde182d
commit c6c6167112
6 changed files with 104 additions and 35 deletions

View File

@@ -1,3 +1,4 @@
use crate::utils::bnclone;
use num_bigint::BigUint;
use num_bigint::RandBigInt;
use openssl::bn::BigNum;
@@ -62,8 +63,6 @@ pub fn rsa_gen_keys() -> Result<(RsaPublicKey, RsaPrivateKey), ErrorStack> {
let mut n = BigNum::new()?;
n.checked_mul(&p, &q, &mut ctx)?;
// This is stupid but I couldn't figure out how to clone a bignum so we do this.
let mut n2 = BigNum::new()?;
n2.checked_mul(&p, &q, &mut ctx)?;
// Let et be (p-1)*(q-1) (the "totient"). You need this value only for keygen.
let mut et = BigNum::new()?;
@@ -80,7 +79,7 @@ pub fn rsa_gen_keys() -> Result<(RsaPublicKey, RsaPrivateKey), ErrorStack> {
};
// Your public key is [e, n]. Your private key is [d, n].
return Ok((RsaPublicKey { e, n }, RsaPrivateKey { d, n: n2 }));
return Ok((RsaPublicKey { e, n: bnclone(&n) }, RsaPrivateKey { d, n }));
}
}
@@ -120,12 +119,8 @@ pub fn invmod(a: &BigNum, n: &BigNum) -> Result<BigNum, ErrorStack> {
Ok((r1, u1, v1))
}
// No, couldn't think of a worse way to do that.
let a_cloned = BigNum::from_hex_str(&a.to_hex_str()?)?;
let n_cloned = BigNum::from_hex_str(&n.to_hex_str()?)?;
// if v1 == 0 there is no mod_inverse
let (_, u1, _v1) = extended_gcd(a_cloned, n_cloned)?;
let (_, u1, _v1) = extended_gcd(bnclone(&a), bnclone(&n))?;
let r_manual = &(&(&u1 % n) + n) % n;
let mut ctx = BigNumContext::new()?;