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 @@ ...@@ -35,6 +35,7 @@
#include "rsa.h" #include "rsa.h"
#include "bignum.h" #include "bignum.h"
#include "nettle-internal.h"
#ifndef DEBUG #ifndef DEBUG
# define DEBUG 0 # define DEBUG 0
...@@ -69,10 +70,11 @@ bignum_next_prime(mpz_t p, mpz_t n, int count, ...@@ -69,10 +70,11 @@ bignum_next_prime(mpz_t p, mpz_t n, int count,
void *progress_ctx, nettle_progress_func progress) void *progress_ctx, nettle_progress_func progress)
{ {
mpz_t tmp; mpz_t tmp;
unsigned long *moduli = NULL; TMP_DECL(moduli, unsigned long, NUMBER_OF_PRIMES);
unsigned long difference; unsigned long difference;
int prime_limit = NUMBER_OF_PRIMES; unsigned prime_limit = NUMBER_OF_PRIMES;
/* First handle tiny numbers */ /* First handle tiny numbers */
if (mpz_cmp_ui(n, 2) <= 0) if (mpz_cmp_ui(n, 2) <= 0)
{ {
...@@ -87,22 +89,23 @@ bignum_next_prime(mpz_t p, mpz_t n, int count, ...@@ -87,22 +89,23 @@ bignum_next_prime(mpz_t p, mpz_t n, int count,
mpz_init(tmp); mpz_init(tmp);
if (prime_limit && (mpz_cmp_ui(p, primes[prime_limit]) <= 0) ) if (mpz_cmp_ui(p, primes[prime_limit-1]) <= 0)
/* Use unly 3, 5 and 7 */ /* Use only 3, 5 and 7 */
prime_limit = 3; prime_limit = 3;
if (prime_limit) /* Compute residues modulo small odd primes */
{ TMP_ALLOC(moduli, prime_limit);
/* Compute residues modulo small odd primes */ {
int i; unsigned i;
for (i = 0; i < prime_limit; i++)
moduli = alloca(prime_limit * sizeof(*moduli)); moduli[i] = mpz_fdiv_ui(p, primes[i]);
for (i = 0; i < prime_limit; i++) }
moduli[i] = mpz_fdiv_ui(p, primes[i]);
}
for (difference = 0; ; difference += 2) for (difference = 0; ; difference += 2)
{ {
int composite = 0;
unsigned i;
if (difference >= ULONG_MAX - 10) if (difference >= ULONG_MAX - 10)
{ /* Should not happen, at least not very often... */ { /* Should not happen, at least not very often... */
mpz_add_ui(p, p, difference); mpz_add_ui(p, p, difference);
...@@ -110,20 +113,14 @@ bignum_next_prime(mpz_t p, mpz_t n, int count, ...@@ -110,20 +113,14 @@ bignum_next_prime(mpz_t p, mpz_t n, int count,
} }
/* First check residues */ /* First check residues */
if (prime_limit) for (i = 0; i < prime_limit; i++)
{ {
int composite = 0; if (moduli[i] == 0)
int i; composite = 1;
moduli[i] = (moduli[i] + 2) % primes[i];
for (i = 0; i < prime_limit; i++)
{
if (moduli[i] == 0)
composite = 1;
moduli[i] = (moduli[i] + 2) % primes[i];
}
if (composite)
continue;
} }
if (composite)
continue;
mpz_add_ui(p, p, difference); mpz_add_ui(p, p, difference);
difference = 0; 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