Skip to content

Commit

Permalink
try to figure out more about the key gen failure
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Dec 4, 2018
1 parent d3e5d89 commit 469beef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/algorithms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn generate_multi_prime_key<R: Rng>(
continue 'next;
}

let exp = BigUint::from_u64(EXP).unwrap();
let exp = BigUint::from_u64(EXP).expect("invalid static exponent");
if let Some(d) = exp.mod_inverse(totient) {
n_final = n;
d_final = d;
Expand All @@ -106,7 +106,7 @@ pub fn generate_multi_prime_key<R: Rng>(

Ok(RSAPrivateKey::from_components(
n_final,
BigUint::from_u64(EXP).unwrap(),
BigUint::from_u64(EXP).expect("invalid static exponent"),
d_final,
primes,
))
Expand Down
27 changes: 16 additions & 11 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,20 +497,22 @@ mod tests {
}

fn test_key_basics(private_key: RSAPrivateKey) {
private_key.validate().expect("failed to validate");
private_key.validate().expect("invalid private key");

assert!(
private_key.d() < private_key.n(),
"private exponent too large"
);

let pub_key: RSAPublicKey = private_key.clone().into();
let m = BigUint::from_u64(42).unwrap();
let m = BigUint::from_u64(42).expect("invalid 42");
let c = encrypt(&pub_key, &m);
let m2 = decrypt::<ThreadRng>(None, &private_key, &c).unwrap();
let m2 = decrypt::<ThreadRng>(None, &private_key, &c)
.expect("unable to decrypt without blinding");
assert_eq!(m, m2);
let mut rng = thread_rng();
let m3 = decrypt(Some(&mut rng), &private_key, &c).unwrap();
let m3 =
decrypt(Some(&mut rng), &private_key, &c).expect("unable to decrypt with blinding");
assert_eq!(m, m3);
}

Expand All @@ -519,14 +521,17 @@ mod tests {
#[test]
fn $name() {
let mut rng = thread_rng();
let private_key = if $multi == 2 {
RSAPrivateKey::new(&mut rng, $size).unwrap()
} else {
generate_multi_prime_key(&mut rng, $multi, $size).unwrap()
};
assert_eq!(private_key.n().bits(), $size);

test_key_basics(private_key);
for _ in 0..10 {
let private_key = if $multi == 2 {
RSAPrivateKey::new(&mut rng, $size).expect("failed to generate key")
} else {
generate_multi_prime_key(&mut rng, $multi, $size).unwrap()
};
assert_eq!(private_key.n().bits(), $size);

test_key_basics(private_key);
}
}
};
}
Expand Down

0 comments on commit 469beef

Please sign in to comment.