Skip to content

Commit

Permalink
cache boxedresiduie for qinv
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Dec 2, 2023
1 parent a811be0 commit 102953d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/algorithms/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ pub fn rsa_decrypt<R: CryptoRngCore + ?Sized>(
let x = m1r.sub(&m2r);

// precomputed: qInv = (1/q) mod p
let qinv = BoxedResidue::new(&qinv, p_params.clone());

// h = qInv.(m1 - m2) mod p
let h = qinv.mul(&x).retrieve();
Expand Down
14 changes: 7 additions & 7 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub(crate) struct PrecomputedValues {
/// D mod (Q-1)
pub(crate) dq: BoxedUint,
/// Q^-1 mod P
pub(crate) qinv: BoxedUint,
pub(crate) qinv: BoxedResidue,

pub(crate) p_params: BoxedResidueParams,
pub(crate) q_params: BoxedResidueParams,
Expand All @@ -121,7 +121,6 @@ impl Zeroize for PrecomputedValues {
fn zeroize(&mut self) {
self.dp.zeroize();
self.dq.zeroize();
self.qinv.zeroize();
}
}

Expand Down Expand Up @@ -436,19 +435,20 @@ impl RsaPrivateKey {

// TODO: error handling

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

let x = NonZero::new(p.wrapping_sub(&BoxedUint::one())).unwrap();
let dp = d.rem_vartime(&x);
let x = NonZero::new(q.wrapping_sub(&BoxedUint::one())).unwrap();
let dq = d.rem_vartime(&x);
let qinv = q.inv_mod(p);
let qinv = BoxedResidue::new(q, p_params.clone());
let qinv = qinv.invert();
if qinv.is_none().into() {
return Err(Error::InvalidPrime);
}
let qinv = qinv.unwrap();

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

self.precomputed = Some(PrecomputedValues {
dp,
dq,
Expand Down Expand Up @@ -570,7 +570,7 @@ impl PrivateKeyPartsNew for RsaPrivateKey {
self.precomputed.as_ref().map(|p| &p.dq)
}

fn qinv(&self) -> Option<&BoxedUint> {
fn qinv(&self) -> Option<&BoxedResidue> {
self.precomputed.as_ref().map(|p| &p.qinv)
}

Expand Down
9 changes: 6 additions & 3 deletions src/traits/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
use alloc::vec::Vec;

use crypto_bigint::{modular::BoxedResidueParams, BoxedUint, NonZero};
use crypto_bigint::{
modular::{BoxedResidue, BoxedResidueParams},
BoxedUint, NonZero,
};
use num_bigint::{BigInt, BigUint, IntoBigInt};
use num_traits::FromPrimitive;
use zeroize::Zeroize;
Expand Down Expand Up @@ -98,7 +101,7 @@ impl<T: PrivateKeyPartsNew> PrivateKeyParts for T {
PrivateKeyPartsNew::dq(self).map(to_biguint)
}
fn qinv(&self) -> Option<BigInt> {
PrivateKeyPartsNew::qinv(self).and_then(|v| to_biguint(v).into_bigint())
PrivateKeyPartsNew::qinv(self).and_then(|v| to_biguint(&v.retrieve()).into_bigint())
}

fn crt_values(&self) -> Option<Vec<CrtValue>> {
Expand All @@ -121,7 +124,7 @@ pub trait PrivateKeyPartsNew: PublicKeyPartsNew {
fn dq(&self) -> Option<&BoxedUint>;

/// Returns the precomputed qinv value, Q^-1 mod P
fn qinv(&self) -> Option<&BoxedUint>;
fn qinv(&self) -> Option<&BoxedResidue>;

/// Returns an iterator over the CRT Values
fn crt_values(&self) -> Option<&[CrtValueNew]>;
Expand Down

0 comments on commit 102953d

Please sign in to comment.