Skip to content

Commit 784fe6b

Browse files
authored
Bump crypto-bigint to v0.7.0-rc.28 (#663)
This includes changes to the `Mul` impls to make them behave like `core`, so in `rsa` the previous usages have been changed to `concatenating_mul`. Likewise, `square` was deprecated and replaced with `concatenating_square`. See RustCrypto/crypto-bigint#1208
1 parent 6df1997 commit 784fe6b

4 files changed

Lines changed: 28 additions & 19 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ exclude = ["marvin_toolkit/", "thirdparty/"]
1515

1616
[dependencies]
1717
const-oid = { version = "0.10", default-features = false }
18-
crypto-bigint = { version = "0.7.0-rc.27", default-features = false, features = ["zeroize", "alloc"] }
18+
crypto-bigint = { version = "0.7.0-rc.28", default-features = false, features = ["zeroize", "alloc"] }
1919
crypto-primes = { version = "0.7.0-pre.9", default-features = false }
2020
digest = { version = "0.11.0-rc.11", default-features = false, features = ["alloc", "oid"] }
2121
rand_core = { version = "0.10", default-features = false }

src/algorithms/rsa.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
33
use core::cmp::Ordering;
44

5-
use crypto_bigint::modular::{BoxedMontyForm, BoxedMontyParams};
6-
use crypto_bigint::{BoxedUint, Gcd, NonZero, Odd, RandomMod, Resize};
5+
use crypto_bigint::{
6+
modular::{BoxedMontyForm, BoxedMontyParams},
7+
BoxedUint, ConcatenatingMul, ConcatenatingSquare, Gcd, NonZero, Odd, RandomMod, Resize,
8+
};
79
use rand_core::TryCryptoRng;
810
use zeroize::Zeroize;
911

@@ -117,7 +119,8 @@ pub fn rsa_decrypt<R: TryCryptoRng + ?Sized>(
117119

118120
// m = m2 + h.q
119121
let m2 = m2.try_resize(n.bits_precision()).ok_or(Error::Internal)?;
120-
let hq = (h * q)
122+
let hq = h
123+
.concatenating_mul(&q)
121124
.try_resize(n.bits_precision())
122125
.ok_or(Error::Internal)?;
123126
m2.wrapping_add(&hq)
@@ -275,14 +278,14 @@ pub fn recover_primes(
275278
let d = d.resize_unchecked(d.bits_precision());
276279
let n = n.resize_unchecked(bits);
277280

278-
let a1 = d * e - &one;
281+
let a1 = d.concatenating_mul(&e) - &one;
279282
let a2 = (n.as_ref() - &one).gcd(&a1);
280-
let a = a1 * a2;
283+
let a = a1.concatenating_mul(&a2);
281284
let n = n.resize_unchecked(a.bits_precision());
282285

283286
// 2. Let m = floor(a /n) and r = a – m n, so that a = m n + r and 0 ≤ r < n.
284287
let m = &a / &n;
285-
let r = a - &m * n.as_ref();
288+
let r = a - m.concatenating_mul(&*n);
286289
let n = n.get();
287290

288291
// 3. Let b = ( (n – r)/(m + 1) ) + 1; if b is not an integer or b^2 ≤ 4n, then output an error indicator,
@@ -294,8 +297,8 @@ pub fn recover_primes(
294297
let b = ((&n - &r) / NonZero::new(&m + &one).expect("adding one")) + one;
295298

296299
let four = BoxedUint::from(4u32);
297-
let four_n = &n * four;
298-
let b_squared = b.square();
300+
let four_n = n.concatenating_mul(&four);
301+
let b_squared = b.concatenating_square();
299302

300303
if b_squared <= four_n {
301304
return Err(Error::InvalidArguments);
@@ -306,7 +309,7 @@ pub fn recover_primes(
306309
// then output an error indicator, and exit without further processing.
307310
let y = b_squared_minus_four_n.floor_sqrt();
308311

309-
let y_squared = y.square();
312+
let y_squared = y.concatenating_square();
310313
let sqrt_is_whole_number = y_squared == b_squared_minus_four_n;
311314
if !sqrt_is_whole_number {
312315
return Err(Error::InvalidArguments);
@@ -327,7 +330,7 @@ pub(crate) fn compute_modulus(primes: &[BoxedUint]) -> Odd<BoxedUint> {
327330
let mut primes = primes.iter();
328331
let mut out = primes.next().expect("must at least be one prime").clone();
329332
for p in primes {
330-
out *= p;
333+
out = out.concatenating_mul(&p);
331334
}
332335
Odd::new(out).expect("modulus must be odd")
333336
}
@@ -346,7 +349,7 @@ pub(crate) fn compute_private_exponent_euler_totient(
346349
let mut totient = BoxedUint::one_with_precision(bits);
347350

348351
for prime in primes {
349-
totient *= prime - &BoxedUint::one();
352+
totient = totient.concatenating_mul(&(prime - &BoxedUint::one()));
350353
}
351354
let exp = exp.resize_unchecked(totient.bits_precision());
352355

@@ -379,7 +382,7 @@ pub(crate) fn compute_private_exponent_carmicheal(
379382

380383
// LCM inlined
381384
let gcd = p1.gcd(&q1);
382-
let lcm = p1 / NonZero::new(gcd).expect("gcd is non zero") * &q1;
385+
let lcm = (p1 / NonZero::new(gcd).expect("gcd is non zero")).concatenating_mul(&q1);
383386
let exp = exp.resize_unchecked(lcm.bits_precision());
384387
if let Some(d) = exp.invert_mod(&NonZero::new(lcm).expect("non zero")).into() {
385388
Ok(d)

src/key.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ use core::cmp::Ordering;
33
use core::fmt;
44
use core::hash::{Hash, Hasher};
55

6-
use crypto_bigint::modular::{BoxedMontyForm, BoxedMontyParams};
7-
use crypto_bigint::{BoxedUint, Integer, NonZero, Odd, Resize};
6+
use crypto_bigint::{
7+
modular::{BoxedMontyForm, BoxedMontyParams},
8+
BoxedUint, ConcatenatingMul, Integer, NonZero, Odd, Resize,
9+
};
810
use rand_core::CryptoRng;
911
use zeroize::{Zeroize, ZeroizeOnDrop};
1012
#[cfg(feature = "serde")]
@@ -379,7 +381,11 @@ impl RsaPrivateKey {
379381
// Check that the product of primes matches the modulus.
380382
// This also ensures that `bit_precision` of each prime is <= that of the modulus,
381383
// and `bit_precision` of their product is >= that of the modulus.
382-
if primes.iter().fold(BoxedUint::one(), |acc, p| acc * p) != n_c.as_ref() {
384+
if primes
385+
.iter()
386+
.fold(BoxedUint::one(), |acc, p| acc.concatenating_mul(&p))
387+
!= n_c.as_ref()
388+
{
383389
return Err(Error::InvalidModulus);
384390
}
385391
}
@@ -766,7 +772,7 @@ fn validate_private_key_parts(key: &RsaPrivateKey) -> Result<()> {
766772
// inverse. Therefore e is coprime to lcm(p-1,q-1,r-1,...) =
767773
// exponent(ℤ/nℤ). It also implies that a^de ≡ a mod p as a^(p-1) ≡ 1
768774
// mod p. Thus a^de ≡ a mod n for all a coprime to n, as required.
769-
let de = key.d.mul(&key.pubkey_components.e);
775+
let de = key.d.concatenating_mul(&key.pubkey_components.e);
770776

771777
for prime in &key.primes {
772778
let x = NonZero::new(prime.wrapping_sub(BoxedUint::one())).unwrap();

0 commit comments

Comments
 (0)