Change BN_mod_sqrt() so that it verifies that the input value is
[openssl.git] / crypto / bn / bn_add.c
index cd7d48d71e4a0b5c8048ce09b0af58392d427c92..6cba07e9f670ad83e4061357ea0a90ab135ad15c 100644 (file)
 #include "bn_lcl.h"
 
 /* r can == a or b */
-int BN_add(BIGNUM *r, BIGNUM *a, BIGNUM *b)
+int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
        {
-       BIGNUM *tmp;
+       const BIGNUM *tmp;
+       int a_neg = a->neg;
 
        bn_check_top(a);
        bn_check_top(b);
@@ -73,10 +74,10 @@ int BN_add(BIGNUM *r, BIGNUM *a, BIGNUM *b)
         * -a +  b      b-a
         * -a + -b      -(a+b)
         */
-       if (a->neg ^ b->neg)
+       if (a_neg ^ b->neg)
                {
                /* only one is negative */
-               if (a->neg)
+               if (a_neg)
                        { tmp=a; a=b; b=tmp; }
 
                /* we are now a - b */
@@ -94,22 +95,21 @@ int BN_add(BIGNUM *r, BIGNUM *a, BIGNUM *b)
                return(1);
                }
 
-       if (a->neg) /* both are neg */
+       if (!BN_uadd(r,a,b)) return(0);
+       if (a_neg) /* both are neg */
                r->neg=1;
        else
                r->neg=0;
-
-       if (!BN_uadd(r,a,b)) return(0);
        return(1);
        }
 
 /* unsigned add of b to a, r must be large enough */
-int BN_uadd(BIGNUM *r, BIGNUM *a, BIGNUM *b)
+int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
        {
        register int i;
        int max,min;
        BN_ULONG *ap,*bp,*rp,carry,t1;
-       BIGNUM *tmp;
+       const BIGNUM *tmp;
 
        bn_check_top(a);
        bn_check_top(b);
@@ -160,11 +160,12 @@ int BN_uadd(BIGNUM *r, BIGNUM *a, BIGNUM *b)
                        *(rp++)= *(ap++);
                }
        /* memcpy(rp,ap,sizeof(*ap)*(max-i));*/
+       r->neg = 0;
        return(1);
        }
 
 /* unsigned subtraction of b from a, a must be larger than b. */
-int BN_usub(BIGNUM *r, BIGNUM *a, BIGNUM *b)
+int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
        {
        int max,min;
        register BN_ULONG t1,t2,*ap,*bp,*rp;
@@ -251,15 +252,16 @@ int BN_usub(BIGNUM *r, BIGNUM *a, BIGNUM *b)
 #endif
 
        r->top=max;
+       r->neg=0;
        bn_fix_top(r);
        return(1);
        }
 
-int BN_sub(BIGNUM *r, BIGNUM *a, BIGNUM *b)
+int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
        {
        int max;
        int add=0,neg=0;
-       BIGNUM *tmp;
+       const BIGNUM *tmp;
 
        bn_check_top(a);
        bn_check_top(b);