Turn BN_prime_checks into a macro.
[openssl.git] / crypto / bn / bn_prime.c
index 28610766d0cde49a4b851bf7937b601c0c55db84..57305c7273b5b271665a0d9d29e0453b2849d1d8 100644 (file)
@@ -73,15 +73,16 @@ static int witness(BIGNUM *a, BIGNUM *n, BN_CTX *ctx,BN_CTX *ctx2,
 static int probable_prime(BIGNUM *rnd, int bits);
 static int probable_prime_dh(BIGNUM *rnd, int bits,
        BIGNUM *add, BIGNUM *rem, BN_CTX *ctx);
 static int probable_prime(BIGNUM *rnd, int bits);
 static int probable_prime_dh(BIGNUM *rnd, int bits,
        BIGNUM *add, BIGNUM *rem, BN_CTX *ctx);
-static int probable_prime_dh_strong(BIGNUM *rnd, int bits,
+static int probable_prime_dh_safe(BIGNUM *rnd, int bits,
        BIGNUM *add, BIGNUM *rem, BN_CTX *ctx);
        BIGNUM *add, BIGNUM *rem, BN_CTX *ctx);
-BIGNUM *BN_generate_prime(BIGNUM *ret, int bits, int strong, BIGNUM *add,
-            BIGNUM *rem, void (*callback)(int,int,char *), char *cb_arg)
+BIGNUM *BN_generate_prime(BIGNUM *ret, int bits, int safe, BIGNUM *add,
+            BIGNUM *rem, void (*callback)(int,int,void *), void *cb_arg)
        {
        BIGNUM *rnd=NULL;
        BIGNUM t;
        int i,j,c1=0;
        BN_CTX *ctx;
        {
        BIGNUM *rnd=NULL;
        BIGNUM t;
        int i,j,c1=0;
        BN_CTX *ctx;
+       int checks = BN_prime_checks(bits);
 
        ctx=BN_CTX_new();
        if (ctx == NULL) goto err;
 
        ctx=BN_CTX_new();
        if (ctx == NULL) goto err;
@@ -100,9 +101,9 @@ loop:
                }
        else
                {
                }
        else
                {
-               if (strong)
+               if (safe)
                        {
                        {
-                       if (!probable_prime_dh_strong(rnd,bits,add,rem,ctx))
+                       if (!probable_prime_dh_safe(rnd,bits,add,rem,ctx))
                                 goto err;
                        }
                else
                                 goto err;
                        }
                else
@@ -114,21 +115,21 @@ loop:
        /* if (BN_mod_word(rnd,(BN_ULONG)3) == 1) goto loop; */
        if (callback != NULL) callback(0,c1++,cb_arg);
 
        /* if (BN_mod_word(rnd,(BN_ULONG)3) == 1) goto loop; */
        if (callback != NULL) callback(0,c1++,cb_arg);
 
-       if (!strong)
+       if (!safe)
                {
                {
-               i=BN_is_prime(rnd,BN_prime_checks,callback,ctx,cb_arg);
+               i=BN_is_prime(rnd,checks,callback,ctx,cb_arg);
                if (i == -1) goto err;
                if (i == 0) goto loop;
                }
        else
                {
                if (i == -1) goto err;
                if (i == 0) goto loop;
                }
        else
                {
-               /* for a strong prime generation,
+               /* for "safe prime" generation,
                 * check that (p-1)/2 is prime.
                 * Since a prime is odd, We just
                 * need to divide by 2 */
                if (!BN_rshift1(&t,rnd)) goto err;
 
                 * check that (p-1)/2 is prime.
                 * Since a prime is odd, We just
                 * need to divide by 2 */
                if (!BN_rshift1(&t,rnd)) goto err;
 
-               for (i=0; i<BN_prime_checks; i++)
+               for (i=0; i<checks; i++)
                        {
                        j=BN_is_prime(rnd,1,callback,ctx,cb_arg);
                        if (j == -1) goto err;
                        {
                        j=BN_is_prime(rnd,1,callback,ctx,cb_arg);
                        if (j == -1) goto err;
@@ -139,7 +140,7 @@ loop:
                        if (j == 0) goto loop;
 
                        if (callback != NULL) callback(2,c1-1,cb_arg);
                        if (j == 0) goto loop;
 
                        if (callback != NULL) callback(2,c1-1,cb_arg);
-                       /* We have a strong prime test pass */
+                       /* We have a safe prime test pass */
                        }
                }
        /* we have a prime :-) */
                        }
                }
        /* we have a prime :-) */
@@ -151,8 +152,8 @@ err:
        return(ret);
        }
 
        return(ret);
        }
 
-int BN_is_prime(BIGNUM *a, int checks, void (*callback)(int,int,char *),
-            BN_CTX *ctx_passed, char *cb_arg)
+int BN_is_prime(BIGNUM *a, int checks, void (*callback)(int,int,void *),
+            BN_CTX *ctx_passed, void *cb_arg)
        {
        int i,j,c2=0,ret= -1;
        BIGNUM *check;
        {
        int i,j,c2=0,ret= -1;
        BIGNUM *check;
@@ -319,7 +320,7 @@ static int probable_prime_dh(BIGNUM *rnd, int bits, BIGNUM *add, BIGNUM *rem,
        loop: for (i=1; i<NUMPRIMES; i++)
                {
                /* check that rnd is a prime */
        loop: for (i=1; i<NUMPRIMES; i++)
                {
                /* check that rnd is a prime */
-               if (BN_mod_word(rnd,(BN_LONG)primes[i]) <= 1)
+               if (BN_mod_word(rnd,(BN_ULONG)primes[i]) <= 1)
                        {
                        if (!BN_add(rnd,rnd,add)) goto err;
                        goto loop;
                        {
                        if (!BN_add(rnd,rnd,add)) goto err;
                        goto loop;
@@ -331,7 +332,7 @@ err:
        return(ret);
        }
 
        return(ret);
        }
 
-static int probable_prime_dh_strong(BIGNUM *p, int bits, BIGNUM *padd,
+static int probable_prime_dh_safe(BIGNUM *p, int bits, BIGNUM *padd,
             BIGNUM *rem, BN_CTX *ctx)
        {
        int i,ret=0;
             BIGNUM *rem, BN_CTX *ctx)
        {
        int i,ret=0;
@@ -366,8 +367,8 @@ static int probable_prime_dh_strong(BIGNUM *p, int bits, BIGNUM *padd,
                /* check that p and q are prime */
                /* check that for p and q
                 * gcd(p-1,primes) == 1 (except for 2) */
                /* check that p and q are prime */
                /* check that for p and q
                 * gcd(p-1,primes) == 1 (except for 2) */
-               if (    (BN_mod_word(p,(BN_LONG)primes[i]) == 0) ||
-                       (BN_mod_word(q,(BN_LONG)primes[i]) == 0))
+               if (    (BN_mod_word(p,(BN_ULONG)primes[i]) == 0) ||
+                       (BN_mod_word(q,(BN_ULONG)primes[i]) == 0))
                        {
                        if (!BN_add(p,p,padd)) goto err;
                        if (!BN_add(q,q,qadd)) goto err;
                        {
                        if (!BN_add(p,p,padd)) goto err;
                        if (!BN_add(q,q,qadd)) goto err;