Skip to content

Commit

Permalink
some encoding fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Jul 6, 2024
1 parent a2d4998 commit cf3548b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
10 changes: 5 additions & 5 deletions src/algorithms/rsa.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Generic RSA implementation
use alloc::borrow::Cow;
use crypto_bigint::modular::{BoxedMontyForm, BoxedMontyParams};
use crypto_bigint::{BoxedUint, Gcd, InvMod, NonZero, Odd, RandomMod, Wrapping};
use crypto_bigint::{BoxedUint, Gcd, NonZero, Odd, RandomMod, Wrapping};
use rand_core::CryptoRngCore;
use zeroize::Zeroize;

Expand Down Expand Up @@ -323,9 +322,10 @@ pub(crate) fn compute_private_exponent_carmicheal(
let p1 = p - &BoxedUint::one();
let q1 = q - &BoxedUint::one();

let lcm = p1; // TODO: p1.lcm(&q1);
let lcm = Odd::new(lcm).unwrap();
if let Some(d) = BoxedUint::from(exp).inv_odd_mod(&lcm).into() {
// LCM inlined
let gcd = p1.gcd(&q1).unwrap();
let lcm = p1 / NonZero::new(gcd).unwrap() * &q1;
if let Some(d) = BoxedUint::from(exp).inv_mod(&lcm).into() {
Ok(d)
} else {
// `exp` evenly divides `lcm`
Expand Down
20 changes: 11 additions & 9 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,27 @@ impl TryFrom<pkcs8::PrivateKeyInfo<'_>> for RsaPrivateKey {
let key_malformed = pkcs8::Error::KeyMalformed;

let bits =
u32::try_from(pkcs1_key.modulus.as_bytes().len()).map_err(|_| key_malformed)? / 8;
u32::try_from(pkcs1_key.modulus.as_bytes().len()).map_err(|_| key_malformed)? * 8;

let n = BoxedUint::from_be_slice(pkcs1_key.modulus.as_bytes(), bits)
.map_err(|_| key_malformed)?;
let n = Option::from(Odd::new(n)).ok_or_else(|| key_malformed)?;
let e = u64::from_be_bytes(
pkcs1_key
.public_exponent
.as_bytes()
.try_into()
.map_err(|_| key_malformed)?,
);

// exponent potentially needs padding
let mut e_slice = [0u8; 8];
let raw_e_slice = pkcs1_key.public_exponent.as_bytes();
e_slice[8 - raw_e_slice.len()..].copy_from_slice(raw_e_slice);
let e = u64::from_be_bytes(e_slice);
let d = BoxedUint::from_be_slice(pkcs1_key.private_exponent.as_bytes(), bits)
.map_err(|_| key_malformed)?;

let prime1 = BoxedUint::from_be_slice(pkcs1_key.prime1.as_bytes(), bits)
.map_err(|_| key_malformed)?;
let prime2 = BoxedUint::from_be_slice(pkcs1_key.prime2.as_bytes(), bits)
.map_err(|_| key_malformed)?;
let primes = vec![prime1, prime2];
RsaPrivateKey::from_components(n, e, d, primes).map_err(|_| pkcs8::Error::KeyMalformed)

RsaPrivateKey::from_components(n, e, d, primes).map_err(|_| key_malformed)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloc::vec::Vec;
use core::cmp::Ordering;
use core::hash::{Hash, Hasher};
use crypto_bigint::modular::{BoxedMontyForm, BoxedMontyParams};
use crypto_bigint::{BoxedUint, Integer, InvMod, NonZero, Odd};
use crypto_bigint::{BoxedUint, Integer, NonZero, Odd};
use rand_core::CryptoRngCore;
use zeroize::{Zeroize, ZeroizeOnDrop};
#[cfg(feature = "serde")]
Expand Down

0 comments on commit cf3548b

Please sign in to comment.