Skip to content

Adds RsaPrivateKey::from_primes and RsaPrivateKey::from_p_q methods#386

Merged
tarcieri merged 1 commit intoRustCrypto:masterfrom
baloo:baloo/private/from-primes
Nov 28, 2023
Merged

Adds RsaPrivateKey::from_primes and RsaPrivateKey::from_p_q methods#386
tarcieri merged 1 commit intoRustCrypto:masterfrom
baloo:baloo/private/from-primes

Conversation

@baloo
Copy link
Copy Markdown
Member

@baloo baloo commented Nov 21, 2023

This is used on Yubico HSM for import/export under wrap as well as when importing a key unsealed.

@baloo baloo marked this pull request as draft November 21, 2023 20:33
@baloo baloo force-pushed the baloo/private/from-primes branch from 649f3ee to dd51b36 Compare November 21, 2023 23:19
@baloo baloo marked this pull request as ready for review November 21, 2023 23:20
@baloo baloo changed the title Adds a RsaPrivateKey::from_primes method Adds RsaPrivateKey::from_primes and RsaPrivateKey::from_p_q methods Nov 21, 2023
baloo added a commit to baloo/yubihsm.rs that referenced this pull request Nov 21, 2023
baloo added a commit to baloo/yubihsm.rs that referenced this pull request Nov 22, 2023
Comment thread src/algorithms/rsa.rs Outdated
Comment thread src/algorithms/rsa.rs
Comment on lines +259 to +278
/// 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)
}
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

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.

Copy link
Copy Markdown
Member Author

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.

@tarcieri
Copy link
Copy Markdown
Member

Couple nits but otherwise this looks fine to me

Copy link
Copy Markdown

@ilebedev-arista ilebedev-arista left a 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!

Comment thread src/key.rs
@baloo baloo force-pushed the baloo/private/from-primes branch from dd51b36 to 4543edd Compare November 22, 2023 18:39
This is used on Yubico HSM for import/export under wrap as well as when
importing a key unsealed.
@baloo baloo force-pushed the baloo/private/from-primes branch from 4543edd to 7badaa3 Compare November 22, 2023 18:49
@tarcieri tarcieri merged commit 63409e5 into RustCrypto:master Nov 28, 2023
@baloo baloo deleted the baloo/private/from-primes branch November 28, 2023 03:22
baloo added a commit to baloo/yubihsm.rs that referenced this pull request Nov 28, 2023
@baloo baloo mentioned this pull request Nov 28, 2023
baloo added a commit to baloo/yubihsm.rs that referenced this pull request Nov 28, 2023
tony-iqlusion pushed a commit to iqlusioninc/yubihsm.rs that referenced this pull request Nov 28, 2023
takumi-earth pushed a commit to earthlings-dev/RSA that referenced this pull request Jan 27, 2026
…ds (RustCrypto#386)

This is used on Yubico HSM for import/export under wrap as well as when
importing a key unsealed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants