Refine logic in bn_mont.c and eliminate redundant BN_CTX pulls.
[openssl.git] / crypto / bn / bn_prime.c
index 43eb9e6dfd0bf5eba8220db8217e04043c403f2d..d03403a600d8ca7a2cd7c7ef3d06dde708a78f52 100644 (file)
@@ -142,6 +142,8 @@ int BN_GENCB_call(BN_GENCB *cb, int a, int b)
                {
        case 1:
                /* Deprecated-style callbacks */
+               if(!cb->cb.cb_1)
+                       return 1;
                cb->cb.cb_1(a, b, cb->arg);
                return 1;
        case 2:
@@ -157,7 +159,7 @@ int BN_GENCB_call(BN_GENCB *cb, int a, int b)
 int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
        const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)
        {
-       BIGNUM t;
+       BIGNUM *t;
        int found=0;
        int i,j,c1=0;
        BN_CTX *ctx;
@@ -165,7 +167,9 @@ int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
 
        ctx=BN_CTX_new();
        if (ctx == NULL) goto err;
-       BN_init(&t);
+       BN_CTX_start(ctx);
+       t = BN_CTX_get(ctx);
+       if(!t) goto err;
 loop: 
        /* make a random number and set the top and bottom bits */
        if (add == NULL)
@@ -202,7 +206,7 @@ loop:
                 * check that (p-1)/2 is prime.
                 * Since a prime is odd, We just
                 * need to divide by 2 */
-               if (!BN_rshift1(&t,ret)) goto err;
+               if (!BN_rshift1(t,ret)) goto err;
 
                for (i=0; i<checks; i++)
                        {
@@ -210,7 +214,7 @@ loop:
                        if (j == -1) goto err;
                        if (j == 0) goto loop;
 
-                       j=BN_is_prime_fasttest_ex(&t,1,ctx,0,cb);
+                       j=BN_is_prime_fasttest_ex(t,1,ctx,0,cb);
                        if (j == -1) goto err;
                        if (j == 0) goto loop;
 
@@ -222,8 +226,12 @@ loop:
        /* we have a prime :-) */
        found = 1;
 err:
-       BN_free(&t);
-       if (ctx != NULL) BN_CTX_free(ctx);
+       if (ctx != NULL)
+               {
+               BN_CTX_end(ctx);
+               BN_CTX_free(ctx);
+               }
+       bn_check_top(ret);
        return found;
        }
 
@@ -361,6 +369,7 @@ static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
                }
        /* If we get here, 'w' is the (a-1)/2-th power of the original 'w',
         * and it is neither -1 nor +1 -- so 'a' cannot be prime */
+       bn_check_top(w);
        return 1;
        }
 
@@ -392,6 +401,7 @@ again:
                        }
                }
        if (!BN_add_word(rnd,delta)) return(0);
+       bn_check_top(rnd);
        return(1);
        }
 
@@ -429,6 +439,7 @@ static int probable_prime_dh(BIGNUM *rnd, int bits,
        ret=1;
 err:
        BN_CTX_end(ctx);
+       bn_check_top(rnd);
        return(ret);
        }
 
@@ -480,5 +491,6 @@ static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
        ret=1;
 err:
        BN_CTX_end(ctx);
+       bn_check_top(p);
        return(ret);
        }