Skip to content

Commit

Permalink
cache p and q params
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Nov 30, 2023
1 parent 3f1751e commit b795c22
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/algorithms/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,19 @@ pub fn rsa_decrypt<R: CryptoRngCore + ?Sized>(
let dp = priv_key.dp().unwrap();
let dq = priv_key.dq().unwrap();
let qinv = priv_key.qinv().unwrap();
let p_params = priv_key.p_params().unwrap();
let q_params = priv_key.q_params().unwrap();

let p = &priv_key.primes()[0];
let q = &priv_key.primes()[1];

// TODO: store
let p_params = BoxedResidueParams::new(p.clone()).unwrap();
let q_params = BoxedResidueParams::new(q.clone()).unwrap();

// precomputed: dP = (1/e) mod (p-1) = d mod (p-1)
// precomputed: dQ = (1/e) mod (q-1) = d mod (q-1)

// m1 = c^dP mod p
let m1 = pow_mod_params(&c, &dp, p_params.clone());
// m2 = c^dQ mod q
let m2 = pow_mod_params(&c, &dq, q_params);
let m2 = pow_mod_params(&c, &dq, q_params.clone());

// precomputed: qInv = (1/q) mod p

Expand Down
16 changes: 16 additions & 0 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ pub(crate) struct PrecomputedValues {
pub(crate) qinv: BoxedUint,

pub(crate) residue_params: BoxedResidueParams,

pub(crate) p_params: BoxedResidueParams,
pub(crate) q_params: BoxedResidueParams,
}

impl Zeroize for PrecomputedValues {
Expand Down Expand Up @@ -404,11 +407,16 @@ impl RsaPrivateKey {
let residue_params =
BoxedResidueParams::new(self.pubkey_components.n.clone().get()).unwrap();

let p_params = BoxedResidueParams::new(p.clone()).unwrap();
let q_params = BoxedResidueParams::new(q.clone()).unwrap();

self.precomputed = Some(PrecomputedValues {
dp,
dq,
qinv,
residue_params,
p_params,
q_params,
});

Ok(())
Expand Down Expand Up @@ -535,6 +543,14 @@ impl PrivateKeyPartsNew for RsaPrivateKey {
fn residue_params(&self) -> Option<&BoxedResidueParams> {
self.precomputed.as_ref().map(|p| &p.residue_params)
}

fn p_params(&self) -> Option<&BoxedResidueParams> {
self.precomputed.as_ref().map(|p| &p.p_params)
}

fn q_params(&self) -> Option<&BoxedResidueParams> {
self.precomputed.as_ref().map(|p| &p.q_params)
}
}

/// Check that the public key is well formed and has an exponent within acceptable bounds.
Expand Down
4 changes: 4 additions & 0 deletions src/traits/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ pub trait PrivateKeyPartsNew: PublicKeyPartsNew {
fn crt_values(&self) -> Option<&[CrtValueNew]>;

fn residue_params(&self) -> Option<&BoxedResidueParams>;

fn p_params(&self) -> Option<&BoxedResidueParams>;

fn q_params(&self) -> Option<&BoxedResidueParams>;
}

/// Contains the precomputed Chinese remainder theorem values.
Expand Down

0 comments on commit b795c22

Please sign in to comment.