From ca42862d52b15532fc26b04c8168b02f5e7ff351 Mon Sep 17 00:00:00 2001
From: Steffen Jaeckel <s@jaeckel.eu>
Date: Fri, 9 May 2014 23:00:50 +0200
Subject: [PATCH] math descriptor: add parameter "b" to isprime()

---
 src/headers/tomcrypt_math.h |  7 ++++---
 src/math/gmp_desc.c         |  9 ++++++---
 src/math/ltm_desc.c         | 11 +++++++----
 src/math/tfm_desc.c         |  7 ++++---
 4 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/src/headers/tomcrypt_math.h b/src/headers/tomcrypt_math.h
index bd07821..7e6e798 100644
--- a/src/headers/tomcrypt_math.h
+++ b/src/headers/tomcrypt_math.h
@@ -326,10 +326,11 @@ typedef struct {
 
    /** Primality testing
        @param a     The integer to test
-       @param b     The destination of the result (FP_YES if prime)
+       @param b     The number of tests that shall be executed
+       @param c     The destination of the result (FP_YES if prime)
        @return CRYPT_OK on success
    */
-   int (*isprime)(void *a, int *b);
+   int (*isprime)(void *a, int b, int *c);
 
 /* ----  (optional) ecc point math ---- */
 
@@ -516,7 +517,7 @@ extern const ltc_math_descriptor gmp_desc;
 #define mp_montgomery_free(a)        ltc_mp.montgomery_deinit(a)
 
 #define mp_exptmod(a,b,c,d)          ltc_mp.exptmod(a,b,c,d)
-#define mp_prime_is_prime(a, b, c)   ltc_mp.isprime(a, c)
+#define mp_prime_is_prime(a, b, c)   ltc_mp.isprime(a, b, c)
 
 #define mp_iszero(a)                 (mp_cmp_d(a, 0) == LTC_MP_EQ ? LTC_MP_YES : LTC_MP_NO)
 #define mp_isodd(a)                  (mp_get_digit_count(a) > 0 ? (mp_get_digit(a, 0) & 1 ? LTC_MP_YES : LTC_MP_NO) : LTC_MP_NO)
diff --git a/src/math/gmp_desc.c b/src/math/gmp_desc.c
index 9904f30..c6082d2 100644
--- a/src/math/gmp_desc.c
+++ b/src/math/gmp_desc.c
@@ -442,11 +442,14 @@ static int exptmod(void *a, void *b, void *c, void *d)
    return CRYPT_OK;
 }
 
-static int isprime(void *a, int *b)
+static int isprime(void *a, int b, int *c)
 {
    LTC_ARGCHK(a != NULL);
-   LTC_ARGCHK(b != NULL);
-   *b = mpz_probab_prime_p(a, 8) > 0 ? LTC_MP_YES : LTC_MP_NO;
+   LTC_ARGCHK(c != NULL);
+   if (b == 0) {
+       b = 8;
+   } /* 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 2760409..3c00aaf 100644
--- a/src/math/ltm_desc.c
+++ b/src/math/ltm_desc.c
@@ -400,13 +400,16 @@ static int exptmod(void *a, void *b, void *c, void *d)
    return mpi_to_ltc_error(mp_exptmod(a,b,c,d));
 }
 
-static int isprime(void *a, int *b)
+static int isprime(void *a, int b, int *c)
 {
    int err;
    LTC_ARGCHK(a != NULL);
-   LTC_ARGCHK(b != NULL);
-   err = mpi_to_ltc_error(mp_prime_is_prime(a, 8, b));
-   *b = (*b == MP_YES) ? LTC_MP_YES : LTC_MP_NO;
+   LTC_ARGCHK(c != NULL);
+   if (b == 0) {
+       b = 8;
+   } /* if */
+   err = mpi_to_ltc_error(mp_prime_is_prime(a, b, c));
+   *c = (*c == MP_YES) ? LTC_MP_YES : LTC_MP_NO;
    return err;
 }
 
diff --git a/src/math/tfm_desc.c b/src/math/tfm_desc.c
index f75408d..693b926 100644
--- a/src/math/tfm_desc.c
+++ b/src/math/tfm_desc.c
@@ -413,11 +413,12 @@ static int exptmod(void *a, void *b, void *c, void *d)
    return tfm_to_ltc_error(fp_exptmod(a,b,c,d));
 }
 
-static int isprime(void *a, int *b)
+static int isprime(void *a, int b, int *c)
 {
    LTC_ARGCHK(a != NULL);
-   LTC_ARGCHK(b != NULL);
-   *b = (fp_isprime(a) == FP_YES) ? LTC_MP_YES : LTC_MP_NO;
+   LTC_ARGCHK(c != NULL);
+   (void)b;
+   *c = (fp_isprime(a) == FP_YES) ? LTC_MP_YES : LTC_MP_NO;
    return CRYPT_OK;
 }