Skip to content
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

Make RsaKeyData::new fallible #517

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/piv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,11 +764,12 @@ pub struct RsaKeyData {

#[cfg(feature = "untested")]
impl RsaKeyData {
/// Generates a new RSA key data set from two randomly generated, secret, primes.
/// Generates a new RSA key data set from two (randomly generated) secret primes.
///
/// Panics if `secret_p` or `secret_q` are invalid primes.
#[allow(clippy::unwrap_used)] // TODO(tarcieri): make fallible and handle errors
pub fn new(secret_p: &[u8], secret_q: &[u8]) -> Self {
/// # Returns
/// - `Ok(key_data)` if `secret_p` and `secret_q` are valid primes.
/// - `Err(Error::AlgorithmError)` if `secret_p`/`secret_q` are invalid primes.
pub fn new(secret_p: &[u8], secret_q: &[u8]) -> Result<Self> {
let p = BigUint::from_bytes_be(secret_p);
let q = BigUint::from_bytes_be(secret_q);

Expand All @@ -779,27 +780,27 @@ impl RsaKeyData {
p_t.lcm(&q_t)
};

let exp = BigUint::from_u64(KEYDATA_RSA_EXP).unwrap();
let exp = BigUint::from_u64(KEYDATA_RSA_EXP).ok_or(Error::AlgorithmError)?;

let d = exp.mod_inverse(&totient).unwrap();
let d = d.to_biguint().unwrap();
let d = exp.mod_inverse(&totient).ok_or(Error::AlgorithmError)?;
let d = d.to_biguint().ok_or(Error::AlgorithmError)?;

// We calculate the optimization values ahead of time, instead of making the user
// do so.

let dp = &d % (&p - BigUint::one());
let dq = &d % (&q - BigUint::one());

let qinv = q.clone().mod_inverse(&p).unwrap();
let qinv = q.clone().mod_inverse(&p).ok_or(Error::AlgorithmError)?;
let (_, qinv) = qinv.to_bytes_be();

RsaKeyData {
Ok(RsaKeyData {
p: Zeroizing::new(p.to_bytes_be()),
q: Zeroizing::new(q.to_bytes_be()),
dp: Zeroizing::new(dp.to_bytes_be()),
dq: Zeroizing::new(dq.to_bytes_be()),
qinv: Zeroizing::new(qinv),
}
})
}

fn total_len(&self) -> usize {
Expand Down