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

Fix private_from_rsa_components_sanity test #351

Merged
merged 1 commit into from
Feb 13, 2024
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: 16 additions & 5 deletions mbedtls/src/pk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1626,24 +1626,35 @@ iy6KC991zzvaWY/Ys+q/84Afqa+0qJKQnPuy/7F5GkVdQA/lfbhi
}
}

fn assert_rsa_private_key_eq(pk1: &Pk, pk2: &Pk) {
assert_eq!(pk1.rsa_public_modulus().unwrap(), pk2.rsa_public_modulus().unwrap()); // n
assert_eq!(pk1.rsa_public_exponent().unwrap(), pk2.rsa_public_exponent().unwrap()); // e
assert_eq!(pk1.rsa_private_exponent().unwrap(), pk2.rsa_private_exponent().unwrap()); // d
let p1 = pk1.rsa_private_prime1().unwrap();
let p2 = pk2.rsa_private_prime1().unwrap();
let q1 = pk1.rsa_private_prime2().unwrap();
let q2 = pk2.rsa_private_prime2().unwrap();
assert!(((p1 == p2) && (q1 == q2)) || ((p1 == q2) && (q1 == p2)));
}

#[test]
fn private_from_rsa_components_sanity() {
let mut pk = Pk::generate_rsa(&mut crate::test_support::rand::test_deterministic_rng(), 2048, 0x10001).unwrap();
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: &Mpi::new(pk.rsa_public_exponent().unwrap() as _).unwrap(),
};
let mut pk2 = Pk::private_from_rsa_components(components).unwrap();
assert_eq!(pk.write_private_der_vec().unwrap(), pk2.write_private_der_vec().unwrap());
let pk2 = Pk::private_from_rsa_components(components).unwrap();
assert_rsa_private_key_eq(&pk, &pk2);

let components = RsaPrivateComponents::WithPrivateExponent {
n: &pk.rsa_public_modulus().unwrap(),
d: &pk.rsa_private_exponent().unwrap(),
e: &Mpi::new(pk.rsa_public_exponent().unwrap() as _).unwrap(),
};
let mut pk3 = Pk::private_from_rsa_components(components).unwrap();
assert_eq!(pk.write_private_der_vec().unwrap(), pk3.write_private_der_vec().unwrap());
let pk3 = Pk::private_from_rsa_components(components).unwrap();
assert_rsa_private_key_eq(&pk, &pk3);
}

#[test]
Expand Down
Loading