Skip to content

Commit 5e71ac2

Browse files
authored
Merge pull request #230 from libtom/math/miller-rabin
Fix number of Miller-Rabin rounds
2 parents 6f85293 + 26b5703 commit 5e71ac2

6 files changed

Lines changed: 29 additions & 9 deletions

File tree

src/headers/tomcrypt_math.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
typedef void rsa_key;
2525
#endif
2626

27+
#ifndef LTC_MILLER_RABIN_REPS
28+
/* Number of rounds of the Miller-Rabin test
29+
* "Reasonable values of reps are between 15 and 50." c.f. gmp doc of mpz_probab_prime_p() */
30+
#define LTC_MILLER_RABIN_REPS 35
31+
#endif
32+
2733
/** math descriptor */
2834
typedef struct {
2935
/** Name of the math provider */
@@ -345,7 +351,7 @@ typedef struct {
345351

346352
/** Primality testing
347353
@param a The integer to test
348-
@param b The number of tests that shall be executed
354+
@param b The number of Miller-Rabin tests that shall be executed
349355
@param c The destination of the result (FP_YES if prime)
350356
@return CRYPT_OK on success
351357
*/
@@ -472,13 +478,13 @@ typedef struct {
472478
int (*submod)(void *a, void *b, void *c, void *d);
473479

474480
/* ---- misc stuff ---- */
481+
475482
/** Make a pseudo-random mpi
476483
@param a The mpi to make random
477484
@param size The desired length
478485
@return CRYPT_OK on success
479486
*/
480487
int (*rand)(void *a, int size);
481-
482488
} ltc_math_descriptor;
483489

484490
extern ltc_math_descriptor ltc_mp;

src/math/gmp_desc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ static int isprime(void *a, int b, int *c)
446446
LTC_ARGCHK(a != NULL);
447447
LTC_ARGCHK(c != NULL);
448448
if (b == 0) {
449-
b = 8;
449+
b = LTC_MILLER_RABIN_REPS;
450450
} /* if */
451451
*c = mpz_probab_prime_p(a, b) > 0 ? LTC_MP_YES : LTC_MP_NO;
452452
return CRYPT_OK;

src/math/ltm_desc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ static int isprime(void *a, int b, int *c)
404404
LTC_ARGCHK(a != NULL);
405405
LTC_ARGCHK(c != NULL);
406406
if (b == 0) {
407-
b = 8;
407+
b = LTC_MILLER_RABIN_REPS;
408408
} /* if */
409409
err = mpi_to_ltc_error(mp_prime_is_prime(a, b, c));
410410
*c = (*c == MP_YES) ? LTC_MP_YES : LTC_MP_NO;

src/math/rand_prime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ int rand_prime(void *N, long len, prng_state *prng, int wprng)
6666
}
6767

6868
/* test */
69-
if ((err = mp_prime_is_prime(N, 8, &res)) != CRYPT_OK) {
69+
if ((err = mp_prime_is_prime(N, LTC_MILLER_RABIN_REPS, &res)) != CRYPT_OK) {
7070
XFREE(buf);
7171
return err;
7272
}

src/math/tfm_desc.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,10 @@ static int isprime(void *a, int b, int *c)
415415
{
416416
LTC_ARGCHK(a != NULL);
417417
LTC_ARGCHK(c != NULL);
418-
(void)b;
419-
*c = (fp_isprime(a) == FP_YES) ? LTC_MP_YES : LTC_MP_NO;
418+
if (b == 0) {
419+
b = LTC_MILLER_RABIN_REPS;
420+
} /* if */
421+
*c = (fp_isprime_ex(a, b) == FP_YES) ? LTC_MP_YES : LTC_MP_NO;
420422
return CRYPT_OK;
421423
}
422424

src/pk/dsa/dsa_make_key.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,23 @@ static int dsa_make_params(prng_state *prng, int wprng, int group_size, int modu
7575
L = modulus_size * 8;
7676
N = group_size * 8;
7777

78+
/* XXX-TODO no Lucas test */
79+
#ifdef LTC_MPI_HAS_LUCAS_TEST
7880
/* M-R tests (when followed by one Lucas test) according FIPS-186-4 - Appendix C.3 - table C.1 */
7981
mr_tests_p = (L <= 2048) ? 3 : 2;
8082
if (N <= 160) { mr_tests_q = 19; }
8183
else if (N <= 224) { mr_tests_q = 24; }
8284
else { mr_tests_q = 27; }
85+
#else
86+
/* M-R tests (without Lucas test) according FIPS-186-4 - Appendix C.3 - table C.1 */
87+
if (L <= 1024) { mr_tests_p = 40; }
88+
else if (L <= 2048) { mr_tests_p = 56; }
89+
else { mr_tests_p = 64; }
90+
91+
if (N <= 160) { mr_tests_q = 40; }
92+
else if (N <= 224) { mr_tests_q = 56; }
93+
else { mr_tests_q = 64; }
94+
#endif
8395

8496
if (N <= 256) {
8597
hash = register_hash(&sha256_desc);
@@ -122,7 +134,7 @@ static int dsa_make_params(prng_state *prng, int wprng, int group_size, int modu
122134
if ((err = mp_mod(U, t2N1, U)) != CRYPT_OK) { goto cleanup; }
123135
if ((err = mp_add(t2N1, U, q)) != CRYPT_OK) { goto cleanup; }
124136
if (!mp_isodd(q)) mp_add_d(q, 1, q);
125-
if ((err = mp_prime_is_prime(q, mr_tests_q, &res)) != CRYPT_OK) { goto cleanup; } /* XXX-TODO rounds are ignored; no Lucas test */
137+
if ((err = mp_prime_is_prime(q, mr_tests_q, &res)) != CRYPT_OK) { goto cleanup; }
126138
if (res == LTC_MP_YES) found_q = 1;
127139
}
128140

@@ -149,7 +161,7 @@ static int dsa_make_params(prng_state *prng, int wprng, int group_size, int modu
149161
if ((err = mp_sub(X, p, p)) != CRYPT_OK) { goto cleanup; }
150162
if (mp_cmp(p, t2L1) != LTC_MP_LT) {
151163
/* p >= 2^(L-1) */
152-
if ((err = mp_prime_is_prime(p, mr_tests_p, &res)) != CRYPT_OK) { goto cleanup; } /* XXX-TODO rounds are ignored; no Lucas test */
164+
if ((err = mp_prime_is_prime(p, mr_tests_p, &res)) != CRYPT_OK) { goto cleanup; }
153165
if (res == LTC_MP_YES) {
154166
found_p = 1;
155167
}

0 commit comments

Comments
 (0)