From 7453eddae9ce742eb336cd63ed8d29a24949eef0 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Mon, 12 Jun 2017 13:45:07 +0200 Subject: [PATCH] improve ltc_math_descriptor a bit * introduce LTC_MILLER_RABIN_REPS which is used as default value in the isprime() implementations --- src/headers/tomcrypt_math.h | 10 ++++++++-- src/math/gmp_desc.c | 2 +- src/math/ltm_desc.c | 2 +- src/math/rand_prime.c | 2 +- src/math/tfm_desc.c | 6 ++++-- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/headers/tomcrypt_math.h b/src/headers/tomcrypt_math.h index 0be2245..4332e5b 100644 --- a/src/headers/tomcrypt_math.h +++ b/src/headers/tomcrypt_math.h @@ -24,6 +24,12 @@ typedef void rsa_key; #endif +#ifndef LTC_MILLER_RABIN_REPS + /* Number of rounds of the Miller-Rabin test + * "Reasonable values of reps are between 15 and 50." c.f. gmp doc of mpz_probab_prime_p() */ + #define LTC_MILLER_RABIN_REPS 35 +#endif + /** math descriptor */ typedef struct { /** Name of the math provider */ @@ -345,7 +351,7 @@ typedef struct { /** Primality testing @param a The integer to test - @param b The number of tests that shall be executed + @param b The number of Miller-Rabin tests that shall be executed @param c The destination of the result (FP_YES if prime) @return CRYPT_OK on success */ @@ -472,13 +478,13 @@ typedef struct { int (*submod)(void *a, void *b, void *c, void *d); /* ---- misc stuff ---- */ + /** Make a pseudo-random mpi @param a The mpi to make random @param size The desired length @return CRYPT_OK on success */ int (*rand)(void *a, int size); - } ltc_math_descriptor; extern ltc_math_descriptor ltc_mp; diff --git a/src/math/gmp_desc.c b/src/math/gmp_desc.c index 6997279..d80d87f 100644 --- a/src/math/gmp_desc.c +++ b/src/math/gmp_desc.c @@ -446,7 +446,7 @@ static int isprime(void *a, int b, int *c) LTC_ARGCHK(a != NULL); LTC_ARGCHK(c != NULL); if (b == 0) { - b = 8; + b = LTC_MILLER_RABIN_REPS; } /* if */ *c = mpz_probab_prime_p(a, b) > 0 ? LTC_MP_YES : LTC_MP_NO; return CRYPT_OK; diff --git a/src/math/ltm_desc.c b/src/math/ltm_desc.c index aa5f88a..3e2a0c9 100644 --- a/src/math/ltm_desc.c +++ b/src/math/ltm_desc.c @@ -404,7 +404,7 @@ static int isprime(void *a, int b, int *c) LTC_ARGCHK(a != NULL); LTC_ARGCHK(c != NULL); if (b == 0) { - b = 8; + b = LTC_MILLER_RABIN_REPS; } /* if */ err = mpi_to_ltc_error(mp_prime_is_prime(a, b, c)); *c = (*c == MP_YES) ? LTC_MP_YES : LTC_MP_NO; diff --git a/src/math/rand_prime.c b/src/math/rand_prime.c index 8213cdb..4dd5764 100644 --- a/src/math/rand_prime.c +++ b/src/math/rand_prime.c @@ -66,7 +66,7 @@ int rand_prime(void *N, long len, prng_state *prng, int wprng) } /* test */ - if ((err = mp_prime_is_prime(N, 8, &res)) != CRYPT_OK) { + if ((err = mp_prime_is_prime(N, LTC_MILLER_RABIN_REPS, &res)) != CRYPT_OK) { XFREE(buf); return err; } diff --git a/src/math/tfm_desc.c b/src/math/tfm_desc.c index 855083c..66d0ddb 100644 --- a/src/math/tfm_desc.c +++ b/src/math/tfm_desc.c @@ -415,8 +415,10 @@ static int isprime(void *a, int b, int *c) { LTC_ARGCHK(a != NULL); LTC_ARGCHK(c != NULL); - (void)b; - *c = (fp_isprime(a) == FP_YES) ? LTC_MP_YES : LTC_MP_NO; + if (b == 0) { + b = LTC_MILLER_RABIN_REPS; + } /* if */ + *c = (fp_isprime_ex(a, b) == FP_YES) ? LTC_MP_YES : LTC_MP_NO; return CRYPT_OK; }