bn_gf2m.c: optimized BN_GF2m_mod_inv delivers sometimes 2x of ECDSA sign.
[oweals/openssl.git] / crypto / bn / bn_div.c
index 68ff960aef551bd3b2e56d80646e41034518b2dc..2c5a4b13cfb1cdc506290ba1262f5e5a8efec6e9 100644 (file)
@@ -56,6 +56,8 @@
  * [including the GNU Public Licence.]
  */
 
+#define OPENSSL_FIPSAPI
+
 #include <stdio.h>
 #include <openssl/bn.h>
 #include "cryptlib.h"
@@ -102,7 +104,7 @@ int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
        /* The next 2 are needed so we can do a dv->d[0]|=1 later
         * since BN_lshift1 will only work once there is a value :-) */
        BN_zero(dv);
-       bn_wexpand(dv,1);
+       if(bn_wexpand(dv,1) == NULL) goto end;
        dv->top=1;
 
        if (!BN_lshift(D,D,nm-nd)) goto end;
@@ -155,6 +157,7 @@ int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
     * Same story here, but it's 128-bit by 64-bit division. Wow!
     *                                  <appro@fy.chalmers.se>
     */
+#  undef bn_div_words
 #  define bn_div_words(n0,n1,d0)               \
        ({  asm volatile (                      \
                "divq   %4"                     \
@@ -181,12 +184,11 @@ static int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,
 int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
           BN_CTX *ctx)
        {
-       int norm_shift,i;
-       size_t loop;
+       int norm_shift,i,loop;
        BIGNUM *tmp,wnum,*snum,*sdiv,*res;
        BN_ULONG *resp,*wnump;
        BN_ULONG d0,d1;
-       size_t num_n,div_n;
+       int num_n,div_n;
 
        /* Invalid zero-padding would have particularly bad consequences
         * in the case of 'num', so don't just rely on bn_check_top() for this one
@@ -230,7 +232,8 @@ int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
        if (dv == NULL)
                res=BN_CTX_get(ctx);
        else    res=dv;
-       if (sdiv == NULL || res == NULL) goto err;
+       if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)
+               goto err;
 
        /* First we normalise the numbers */
        norm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);
@@ -266,7 +269,7 @@ int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
        resp= &(res->d[loop-1]);
 
        /* space for temp */
-       if (!bn_wexpand(tmp, div_n+1)) goto err;
+       if (!bn_wexpand(tmp,(div_n+1))) goto err;
 
        if (BN_ucmp(&wnum,sdiv) >= 0)
                {
@@ -430,7 +433,7 @@ static int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,
        BIGNUM *tmp,wnum,*snum,*sdiv,*res;
        BN_ULONG *resp,*wnump;
        BN_ULONG d0,d1;
-       size_t num_n,div_n;
+       int num_n,div_n;
 
        bn_check_top(dv);
        bn_check_top(rm);
@@ -499,12 +502,12 @@ static int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,
 
        /* Setup to 'res' */
        res->neg= (num->neg^divisor->neg);
-       if (!bn_wexpand(res,loop+1U)) goto err;
+       if (!bn_wexpand(res,(loop+1))) goto err;
        res->top=loop-1;
        resp= &(res->d[loop-1]);
 
        /* space for temp */
-       if (!bn_wexpand(tmp,div_n+1U)) goto err;
+       if (!bn_wexpand(tmp,(div_n+1))) goto err;
 
        /* if res->top == 0 then clear the neg value otherwise decrease
         * the resp pointer */