Skip to content

Commit 98a86a2

Browse files
implement blinding (tests failing currently)
1 parent 21b7c81 commit 98a86a2

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

src/algorithms/rsa.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use alloc::borrow::Cow;
44
use alloc::vec::Vec;
55
use crypto_bigint::modular::BoxedResidueParams;
6-
use crypto_bigint::{BoxedUint, NonZero, Zero};
6+
use crypto_bigint::{BoxedUint, NonZero};
77
use num_bigint::{BigInt, BigUint, IntoBigInt, IntoBigUint, ModInverse, RandBigInt, ToBigInt};
88
use num_integer::{sqrt, Integer};
99
use num_traits::{FromPrimitive, One, Pow, Signed, Zero as _};
@@ -326,22 +326,23 @@ pub(crate) fn compute_private_exponent_carmicheal(
326326
}
327327
}
328328

329-
fn blind_new<R: CryptoRngCore, K: PublicKeyParts>(
329+
fn blind_new<R: CryptoRngCore, K: PublicKeyPartsNew>(
330330
rng: &mut R,
331331
key: &K,
332332
c: &BoxedUint,
333333
n_params: &BoxedResidueParams,
334334
) -> (BoxedUint, BoxedUint) {
335-
let n = NonZero::new(to_uint(key.n().clone())).unwrap();
336335
let mut r: BoxedUint;
337336
let mut ir: CtOption<BoxedUint>;
338337
let unblinder;
339338
loop {
340-
r = todo!(); // BoxedUint::random_mod(&mut rng, &n);
339+
// TODO: use constant time gen
340+
r = to_uint(rng.gen_biguint_below(&to_biguint(&key.n())));
341+
// TODO: correct mapping
341342
if r.is_zero().into() {
342343
r = BoxedUint::one();
343344
}
344-
ir = r.inv_mod(&n);
345+
ir = r.inv_mod(key.n());
345346

346347
// TODO: constant time?
347348
if let Some(ir) = ir.into() {
@@ -350,13 +351,12 @@ fn blind_new<R: CryptoRngCore, K: PublicKeyParts>(
350351
}
351352
}
352353

353-
let e = to_uint(key.e().clone());
354354
let c = {
355355
let r = reduce(&r, n_params.clone());
356-
let rpowe = r.pow(&e).retrieve();
356+
let mut rpowe = r.pow(key.e()).retrieve();
357357

358358
let c = c.wrapping_mul(&rpowe);
359-
let c = c.rem_vartime(&n);
359+
let c = c.rem_vartime(key.n());
360360

361361
rpowe.zeroize();
362362

@@ -367,9 +367,8 @@ fn blind_new<R: CryptoRngCore, K: PublicKeyParts>(
367367
}
368368

369369
fn unblind_new(key: &impl PublicKeyPartsNew, m: &BoxedUint, unblinder: &BoxedUint) -> BoxedUint {
370-
let n = key.n();
371370
let a = m.wrapping_mul(unblinder);
372-
a.rem_vartime(&n)
371+
a.rem_vartime(key.n())
373372
}
374373

375374
pub fn rsa_decrypt_new<R: CryptoRngCore + ?Sized>(

src/key.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use core::hash::{Hash, Hasher};
44
use crypto_bigint::modular::{BoxedResidue, BoxedResidueParams};
55
use crypto_bigint::{BoxedUint, Limb, NonZero};
66
use num_bigint::traits::ModInverse;
7-
use num_bigint::{BigInt, BigUint};
7+
use num_bigint::BigUint;
88
use num_integer::Integer;
99
use num_traits::{FromPrimitive, One, ToPrimitive};
1010
use rand_core::CryptoRngCore;
@@ -272,7 +272,7 @@ impl RsaPrivateKey {
272272
n: BigUint,
273273
e: BigUint,
274274
d: BigUint,
275-
mut primes: Vec<BigUint>,
275+
primes: Vec<BigUint>,
276276
) -> Result<RsaPrivateKey> {
277277
let mut should_validate = false;
278278
let mut primes: Vec<_> = primes.into_iter().map(to_uint).collect();

src/pkcs1v15.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,7 @@ mod tests {
389389
assert_ne!(input, ciphertext);
390390

391391
let blind: bool = rng.next_u32() < (1u32 << 31);
392-
// TODO:
393-
// let blinder = if blind { Some(&mut rng) } else { None };
394-
let blinder: Option<&mut ChaCha8Rng> = None;
392+
let blinder = if blind { Some(&mut rng) } else { None };
395393
let plaintext = decrypt_new(blinder, &priv_key, &ciphertext).unwrap();
396394
assert_eq!(input, plaintext);
397395
}

0 commit comments

Comments
 (0)