Skip to content

Commit 21b7c81

Browse files
store residue_params
1 parent bc92994 commit 21b7c81

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/algorithms/rsa.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ fn blind_new<R: CryptoRngCore, K: PublicKeyParts>(
330330
rng: &mut R,
331331
key: &K,
332332
c: &BoxedUint,
333+
n_params: &BoxedResidueParams,
333334
) -> (BoxedUint, BoxedUint) {
334335
let n = NonZero::new(to_uint(key.n().clone())).unwrap();
335336
let mut r: BoxedUint;
@@ -349,10 +350,9 @@ fn blind_new<R: CryptoRngCore, K: PublicKeyParts>(
349350
}
350351
}
351352

352-
let n_params = BoxedResidueParams::new(n.get()).unwrap();
353353
let e = to_uint(key.e().clone());
354354
let c = {
355-
let r = reduce(&r, n_params);
355+
let r = reduce(&r, n_params.clone());
356356
let rpowe = r.pow(&e).retrieve();
357357

358358
let c = c.wrapping_mul(&rpowe);
@@ -393,16 +393,20 @@ pub fn rsa_decrypt_new<R: CryptoRngCore + ?Sized>(
393393

394394
let mut ir = None;
395395

396+
let n_params = priv_key
397+
.residue_params()
398+
.cloned()
399+
.unwrap_or_else(|| BoxedResidueParams::new(n.clone().get()).unwrap());
400+
396401
let c = if let Some(ref mut rng) = rng {
397-
let (blinded, unblinder) = blind_new(rng, priv_key, &c);
402+
let (blinded, unblinder) = blind_new(rng, priv_key, &c, &n_params);
398403
ir = Some(unblinder);
399404
blinded
400405
} else {
401406
c
402407
};
403408

404409
// TODO: fast path with precalculated values;
405-
let n_params = BoxedResidueParams::new(n.clone().get()).unwrap();
406410
let c = reduce(&c, n_params);
407411
let m = c.pow(&d).retrieve();
408412

src/key.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ pub(crate) struct PrecomputedValues {
109109
/// differently in PKCS#1 and interoperability is sufficiently
110110
/// important that we mirror this.
111111
pub(crate) crt_values: Vec<CrtValueNew>,
112+
113+
pub(crate) residue_params: BoxedResidueParams,
112114
}
113115

114116
impl Zeroize for PrecomputedValues {
@@ -393,11 +395,16 @@ impl RsaPrivateKey {
393395
values
394396
};
395397

398+
// TODO: how to handle error?
399+
let residue_params =
400+
BoxedResidueParams::new(self.pubkey_components.n.clone().get()).unwrap();
401+
396402
self.precomputed = Some(PrecomputedValues {
397403
dp: to_uint(dp),
398404
dq: to_uint(dq),
399405
qinv,
400406
crt_values,
407+
residue_params,
401408
});
402409

403410
Ok(())
@@ -523,6 +530,10 @@ impl PrivateKeyPartsNew for RsaPrivateKey {
523530
None
524531
}
525532
}
533+
534+
fn residue_params(&self) -> Option<&BoxedResidueParams> {
535+
self.precomputed.as_ref().map(|p| &p.residue_params)
536+
}
526537
}
527538

528539
/// Check that the public key is well formed and has an exponent within acceptable bounds.

src/traits/keys.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use alloc::vec::Vec;
44

5-
use crypto_bigint::{BoxedUint, NonZero};
5+
use crypto_bigint::{modular::BoxedResidueParams, BoxedUint, NonZero};
66
use num_bigint::{BigInt, BigUint, IntoBigInt};
77
use zeroize::Zeroize;
88

@@ -126,6 +126,8 @@ pub trait PrivateKeyPartsNew: PublicKeyPartsNew {
126126

127127
/// Returns an iterator over the CRT Values
128128
fn crt_values(&self) -> Option<&[CrtValueNew]>;
129+
130+
fn residue_params(&self) -> Option<&BoxedResidueParams>;
129131
}
130132

131133
/// Contains the precomputed Chinese remainder theorem values.

0 commit comments

Comments
 (0)