Skip to content
Snippets Groups Projects
Commit e4492103 authored by Niels Möller's avatar Niels Möller
Browse files

(bignum_random_prime): Deleted function.

(rsa_generate_keypair): Use new nettle_random_prime. Generate
secret factors p and q with the two most significant bits set.

Rev: nettle/rsa-keygen.c:1.8
parent 5855faca
No related branches found
No related tags found
No related merge requests found
...@@ -42,27 +42,6 @@ ...@@ -42,27 +42,6 @@
#endif #endif
/* Returns a random prime of size BITS */
static void
bignum_random_prime(mpz_t x, unsigned bits,
void *random_ctx, nettle_random_func random,
void *progress_ctx, nettle_progress_func progress)
{
assert(bits);
for (;;)
{
nettle_mpz_random_size(x, random_ctx, random, bits);
mpz_setbit(x, bits - 1);
/* Miller-rabin count of 25 is probably much overkill. */
nettle_next_prime(x, x, 25, 10000, progress_ctx, progress);
if (mpz_sizeinbase(x, 2) == bits)
break;
}
}
int int
rsa_generate_keypair(struct rsa_public_key *pub, rsa_generate_keypair(struct rsa_public_key *pub,
struct rsa_private_key *key, struct rsa_private_key *key,
...@@ -110,9 +89,10 @@ rsa_generate_keypair(struct rsa_public_key *pub, ...@@ -110,9 +89,10 @@ rsa_generate_keypair(struct rsa_public_key *pub,
/* Generate p, such that gcd(p-1, e) = 1 */ /* Generate p, such that gcd(p-1, e) = 1 */
for (;;) for (;;)
{ {
bignum_random_prime(key->p, (n_size+1)/2, nettle_random_prime(key->p, (n_size+1)/2, 1,
random_ctx, random, random_ctx, random,
progress_ctx, progress); progress_ctx, progress);
mpz_sub_ui(p1, key->p, 1); mpz_sub_ui(p1, key->p, 1);
/* If e was given, we must chose p such that p-1 has no factors in /* If e was given, we must chose p such that p-1 has no factors in
...@@ -133,7 +113,7 @@ rsa_generate_keypair(struct rsa_public_key *pub, ...@@ -133,7 +113,7 @@ rsa_generate_keypair(struct rsa_public_key *pub,
/* Generate q, such that gcd(q-1, e) = 1 */ /* Generate q, such that gcd(q-1, e) = 1 */
for (;;) for (;;)
{ {
bignum_random_prime(key->q, n_size/2, nettle_random_prime(key->q, n_size/2, 1,
random_ctx, random, random_ctx, random,
progress_ctx, progress); progress_ctx, progress);
...@@ -157,26 +137,9 @@ rsa_generate_keypair(struct rsa_public_key *pub, ...@@ -157,26 +137,9 @@ rsa_generate_keypair(struct rsa_public_key *pub,
/* Now we have the primes. Is the product of the right size? */ /* Now we have the primes. Is the product of the right size? */
mpz_mul(pub->n, key->p, key->q); mpz_mul(pub->n, key->p, key->q);
if (mpz_sizeinbase(pub->n, 2) != n_size) assert (mpz_sizeinbase(pub->n, 2) == n_size);
/* We might get an n of size n_size-1. Then just try again. */
{
#if DEBUG
fprintf(stderr,
"\nWanted size: %d, p-size: %d, q-size: %d, n-size: %d\n",
n_size,
mpz_sizeinbase(key->p,2),
mpz_sizeinbase(key->q,2),
mpz_sizeinbase(pub->n,2));
#endif
if (progress)
{
progress(progress_ctx, 'b');
progress(progress_ctx, '\n');
}
continue;
}
if (progress) if (progress)
progress(progress_ctx, '\n'); progress(progress_ctx, '\n');
...@@ -232,7 +195,7 @@ rsa_generate_keypair(struct rsa_public_key *pub, ...@@ -232,7 +195,7 @@ rsa_generate_keypair(struct rsa_public_key *pub,
/* c was computed earlier */ /* c was computed earlier */
pub->size = key->size = (mpz_sizeinbase(pub->n, 2) + 7) / 8; pub->size = key->size = (n_size + 7) / 8;
assert(pub->size >= RSA_MINIMUM_N_OCTETS); assert(pub->size >= RSA_MINIMUM_N_OCTETS);
mpz_clear(p1); mpz_clear(q1); mpz_clear(phi); mpz_clear(tmp); mpz_clear(p1); mpz_clear(q1); mpz_clear(phi); mpz_clear(tmp);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment