From 102953da274c135124f27b5b68a96271786b0f8b Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Sat, 2 Dec 2023 13:21:15 +0100 Subject: [PATCH] cache boxedresiduie for qinv --- src/algorithms/rsa.rs | 1 - src/key.rs | 14 +++++++------- src/traits/keys.rs | 9 ++++++--- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/algorithms/rsa.rs b/src/algorithms/rsa.rs index f626338e..77cf681a 100644 --- a/src/algorithms/rsa.rs +++ b/src/algorithms/rsa.rs @@ -95,7 +95,6 @@ pub fn rsa_decrypt( 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(); diff --git a/src/key.rs b/src/key.rs index d75309a5..3f1ee820 100644 --- a/src/key.rs +++ b/src/key.rs @@ -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, @@ -121,7 +121,6 @@ impl Zeroize for PrecomputedValues { fn zeroize(&mut self) { self.dp.zeroize(); self.dq.zeroize(); - self.qinv.zeroize(); } } @@ -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, @@ -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) } diff --git a/src/traits/keys.rs b/src/traits/keys.rs index 7d4afbce..671d0647 100644 --- a/src/traits/keys.rs +++ b/src/traits/keys.rs @@ -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; @@ -98,7 +101,7 @@ impl PrivateKeyParts for T { PrivateKeyPartsNew::dq(self).map(to_biguint) } fn qinv(&self) -> Option { - 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> { @@ -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]>;