Commit d6d63fbf authored by Niels Möller's avatar Niels Möller

(bignum_next_prime): Fixed off-by-one error when

comparing input to the largest listed prime. General cleanup, as
prime_limit > 0 always. Use TMP_DECL and TMP_ALLOC.

Rev: src/nettle/rsa-keygen.c:1.6
parent 21ee1904
......@@ -35,6 +35,7 @@
#include "rsa.h"
#include "bignum.h"
#include "nettle-internal.h"
#ifndef DEBUG
# define DEBUG 0
......@@ -69,9 +70,10 @@ bignum_next_prime(mpz_t p, mpz_t n, int count,
void *progress_ctx, nettle_progress_func progress)
{
mpz_t tmp;
unsigned long *moduli = NULL;
TMP_DECL(moduli, unsigned long, NUMBER_OF_PRIMES);
unsigned long difference;
int prime_limit = NUMBER_OF_PRIMES;
unsigned prime_limit = NUMBER_OF_PRIMES;
/* First handle tiny numbers */
if (mpz_cmp_ui(n, 2) <= 0)
......@@ -87,22 +89,23 @@ bignum_next_prime(mpz_t p, mpz_t n, int count,
mpz_init(tmp);
if (prime_limit && (mpz_cmp_ui(p, primes[prime_limit]) <= 0) )
/* Use unly 3, 5 and 7 */
if (mpz_cmp_ui(p, primes[prime_limit-1]) <= 0)
/* Use only 3, 5 and 7 */
prime_limit = 3;
if (prime_limit)
{
/* Compute residues modulo small odd primes */
int i;
moduli = alloca(prime_limit * sizeof(*moduli));
TMP_ALLOC(moduli, prime_limit);
{
unsigned i;
for (i = 0; i < prime_limit; i++)
moduli[i] = mpz_fdiv_ui(p, primes[i]);
}
for (difference = 0; ; difference += 2)
{
int composite = 0;
unsigned i;
if (difference >= ULONG_MAX - 10)
{ /* Should not happen, at least not very often... */
mpz_add_ui(p, p, difference);
......@@ -110,11 +113,6 @@ bignum_next_prime(mpz_t p, mpz_t n, int count,
}
/* First check residues */
if (prime_limit)
{
int composite = 0;
int i;
for (i = 0; i < prime_limit; i++)
{
if (moduli[i] == 0)
......@@ -123,7 +121,6 @@ bignum_next_prime(mpz_t p, mpz_t n, int count,
}
if (composite)
continue;
}
mpz_add_ui(p, p, difference);
difference = 0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment