Avoid segfault if ret==0.
[openssl.git] / crypto / bn / bn_gcd.c
index 1691877f31079edfce6344a58a5e2cb7a614fb4f..0248753f6dc9a0f1499fb19d092d0c7d5b37b640 100644 (file)
@@ -140,6 +140,7 @@ int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
        ret=1;
 err:
        BN_CTX_end(ctx);
+       bn_check_top(r);
        return(ret);
        }
 
@@ -194,6 +195,7 @@ static BIGNUM *euclid(BIGNUM *a, BIGNUM *b)
                {
                if (!BN_lshift(a,a,shifts)) goto err;
                }
+       bn_check_top(a);
        return(a);
 err:
        return(NULL);
@@ -244,11 +246,12 @@ BIGNUM *BN_mod_inverse(BIGNUM *in,
         *      sign*Y*a  ==  A   (mod |n|).
         */
 
-       if (BN_is_odd(n) && (BN_num_bits(n) <= 400))
+       if (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048)))
                {
                /* Binary inversion algorithm; requires odd modulus.
                 * This is faster than the general algorithm if the modulus
-                * is sufficiently small. */
+                * is sufficiently small (about 400 .. 500 bits on 32-bit
+                * sytems, but much more on 64-bit systems) */
                int shift;
                
                while (!BN_is_zero(B))
@@ -330,7 +333,7 @@ BIGNUM *BN_mod_inverse(BIGNUM *in,
                }
        else
                {
-               /* general inversion algorithm (less efficient than binary inversion) */
+               /* general inversion algorithm */
 
                while (!BN_is_zero(B))
                        {
@@ -485,5 +488,7 @@ BIGNUM *BN_mod_inverse(BIGNUM *in,
 err:
        if ((ret == NULL) && (in == NULL)) BN_free(R);
        BN_CTX_end(ctx);
+       if (ret)
+               bn_check_top(ret);
        return(ret);
        }