Skip to content

Commit

Permalink
Merge pull request #352 from fortanix/mz/call-rsa_check_privkey-for-s…
Browse files Browse the repository at this point in the history
…anity-checking

Call `rsa_check_privkey` for sanity checking RSA private components
  • Loading branch information
mzohreva authored Feb 13, 2024
2 parents a438919 + f71a1cf commit 55eac05
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions mbedtls/src/pk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ Please use `private_from_ec_scalar_with_rng` instead."
let ctx = ret.inner.pk_ctx as *mut rsa_context;
rsa_import(ctx, to_ptr(n), to_ptr(p), to_ptr(q), to_ptr(d), to_ptr(e)).into_result()?;
rsa_complete(ctx).into_result()?;
rsa_check_privkey(ctx).into_result()?;
}
Ok(ret)
}
Expand Down Expand Up @@ -1657,6 +1658,21 @@ iy6KC991zzvaWY/Ys+q/84Afqa+0qJKQnPuy/7F5GkVdQA/lfbhi
assert_rsa_private_key_eq(&pk, &pk3);
}

#[test]
fn private_from_rsa_components_wrong_params() {
let pk = Pk::generate_rsa(&mut crate::test_support::rand::test_rng(), 2048, 0x10001).unwrap();
let components = RsaPrivateComponents::WithPrimes {
p: &pk.rsa_private_prime1().unwrap(),
q: &pk.rsa_private_prime2().unwrap(),
e: &pk.rsa_public_modulus().unwrap(), // incorrect
};
let err = match Pk::private_from_rsa_components(components) {
Ok(_) => panic!("expected an error, got a Pk"),
Err(e) => e,
};
assert_eq!(err, Error::RsaKeyCheckFailed);
}

#[test]
fn public_from_rsa_components_sanity() {
let mut pk = Pk::generate_rsa(&mut crate::test_support::rand::test_rng(), 2048, 0x10001).unwrap();
Expand Down

0 comments on commit 55eac05

Please sign in to comment.