-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds RsaPrivateKey::from_primes
and RsaPrivateKey::from_p_q
methods
#386
Conversation
649f3ee
to
dd51b36
Compare
RsaPrivateKey::from_primes
methodRsaPrivateKey::from_primes
and RsaPrivateKey::from_p_q
methods
src/algorithms/rsa.rs
Outdated
/// Compute the private exponent from its primes (p and q) and public exponent | ||
pub(crate) fn compute_private_exponent(primes: &[BigUint], exp: &BigUint) -> Result<BigUint> { | ||
if primes.len() < 2 { | ||
return Err(Error::InvalidPrime); | ||
} | ||
|
||
let mut totient = BigUint::one(); | ||
|
||
for prime in primes { | ||
totient *= prime - BigUint::one(); | ||
} | ||
|
||
if let Some(d) = exp.mod_inverse(totient) { | ||
Ok(d.to_biguint().unwrap()) | ||
} else { | ||
// `exp` evenly divides `totient` | ||
Err(Error::InvalidPrime) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This somewhat duplicates https://github.com/RustCrypto/RSA/blob/c7b0eae/src/algorithms/generate.rs#L93-L113
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried something to reuse those bits. Let me know what you think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or make generate reuse those bits, I guess.
Couple nits but otherwise this looks fine to me |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not much of a rust reviewer, but this does look fine to me!
dd51b36
to
4543edd
Compare
This is used on Yubico HSM for import/export under wrap as well as when importing a key unsealed.
4543edd
to
7badaa3
Compare
This is used on Yubico HSM for import/export under wrap as well as when importing a key unsealed.