sl@0: /* crypto/bn/bn_add.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include "bn_lcl.h" sl@0: sl@0: /* r can == a or b */ sl@0: EXPORT_C int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) sl@0: { sl@0: const BIGNUM *tmp; sl@0: int a_neg = a->neg, ret; sl@0: sl@0: bn_check_top(a); sl@0: bn_check_top(b); sl@0: sl@0: /* a + b a+b sl@0: * a + -b a-b sl@0: * -a + b b-a sl@0: * -a + -b -(a+b) sl@0: */ sl@0: if (a_neg ^ b->neg) sl@0: { sl@0: /* only one is negative */ sl@0: if (a_neg) sl@0: { tmp=a; a=b; b=tmp; } sl@0: sl@0: /* we are now a - b */ sl@0: sl@0: if (BN_ucmp(a,b) < 0) sl@0: { sl@0: if (!BN_usub(r,b,a)) return(0); sl@0: r->neg=1; sl@0: } sl@0: else sl@0: { sl@0: if (!BN_usub(r,a,b)) return(0); sl@0: r->neg=0; sl@0: } sl@0: return(1); sl@0: } sl@0: sl@0: ret = BN_uadd(r,a,b); sl@0: r->neg = a_neg; sl@0: bn_check_top(r); sl@0: return ret; sl@0: } sl@0: sl@0: /* unsigned add of b to a */ sl@0: EXPORT_C int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) sl@0: { sl@0: int max,min,dif; sl@0: BN_ULONG *ap,*bp,*rp,carry,t1,t2; sl@0: const BIGNUM *tmp; sl@0: sl@0: bn_check_top(a); sl@0: bn_check_top(b); sl@0: sl@0: if (a->top < b->top) sl@0: { tmp=a; a=b; b=tmp; } sl@0: max = a->top; sl@0: min = b->top; sl@0: dif = max - min; sl@0: sl@0: if (bn_wexpand(r,max+1) == NULL) sl@0: return 0; sl@0: sl@0: r->top=max; sl@0: sl@0: sl@0: ap=a->d; sl@0: bp=b->d; sl@0: rp=r->d; sl@0: sl@0: carry=bn_add_words(rp,ap,bp,min); sl@0: rp+=min; sl@0: ap+=min; sl@0: bp+=min; sl@0: sl@0: if (carry) sl@0: { sl@0: while (dif) sl@0: { sl@0: dif--; sl@0: t1 = *(ap++); sl@0: t2 = (t1+1) & BN_MASK2; sl@0: *(rp++) = t2; sl@0: if (t2) sl@0: { sl@0: carry=0; sl@0: break; sl@0: } sl@0: } sl@0: if (carry) sl@0: { sl@0: /* carry != 0 => dif == 0 */ sl@0: *rp = 1; sl@0: r->top++; sl@0: } sl@0: } sl@0: if (dif && rp != ap) sl@0: while (dif--) sl@0: /* copy remaining words if ap != rp */ sl@0: *(rp++) = *(ap++); sl@0: r->neg = 0; sl@0: bn_check_top(r); sl@0: return 1; sl@0: } sl@0: sl@0: /* unsigned subtraction of b from a, a must be larger than b. */ sl@0: EXPORT_C int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) sl@0: { sl@0: int max,min,dif; sl@0: register BN_ULONG t1,t2,*ap,*bp,*rp; sl@0: int i,carry; sl@0: #if defined(IRIX_CC_BUG) && !defined(LINT) sl@0: int dummy; sl@0: #endif sl@0: sl@0: bn_check_top(a); sl@0: bn_check_top(b); sl@0: sl@0: max = a->top; sl@0: min = b->top; sl@0: dif = max - min; sl@0: sl@0: if (dif < 0) /* hmm... should not be happening */ sl@0: { sl@0: BNerr(BN_F_BN_USUB,BN_R_ARG2_LT_ARG3); sl@0: return(0); sl@0: } sl@0: sl@0: if (bn_wexpand(r,max) == NULL) return(0); sl@0: sl@0: ap=a->d; sl@0: bp=b->d; sl@0: rp=r->d; sl@0: sl@0: #if 1 sl@0: carry=0; sl@0: for (i = min; i != 0; i--) sl@0: { sl@0: t1= *(ap++); sl@0: t2= *(bp++); sl@0: if (carry) sl@0: { sl@0: carry=(t1 <= t2); sl@0: t1=(t1-t2-1)&BN_MASK2; sl@0: } sl@0: else sl@0: { sl@0: carry=(t1 < t2); sl@0: t1=(t1-t2)&BN_MASK2; sl@0: } sl@0: #if defined(IRIX_CC_BUG) && !defined(LINT) sl@0: dummy=t1; sl@0: #endif sl@0: *(rp++)=t1&BN_MASK2; sl@0: } sl@0: #else sl@0: carry=bn_sub_words(rp,ap,bp,min); sl@0: ap+=min; sl@0: bp+=min; sl@0: rp+=min; sl@0: #endif sl@0: if (carry) /* subtracted */ sl@0: { sl@0: if (!dif) sl@0: /* error: a < b */ sl@0: return 0; sl@0: while (dif) sl@0: { sl@0: dif--; sl@0: t1 = *(ap++); sl@0: t2 = (t1-1)&BN_MASK2; sl@0: *(rp++) = t2; sl@0: if (t1) sl@0: break; sl@0: } sl@0: } sl@0: #if 0 sl@0: memcpy(rp,ap,sizeof(*rp)*(max-i)); sl@0: #else sl@0: if (rp != ap) sl@0: { sl@0: for (;;) sl@0: { sl@0: if (!dif--) break; sl@0: rp[0]=ap[0]; sl@0: if (!dif--) break; sl@0: rp[1]=ap[1]; sl@0: if (!dif--) break; sl@0: rp[2]=ap[2]; sl@0: if (!dif--) break; sl@0: rp[3]=ap[3]; sl@0: rp+=4; sl@0: ap+=4; sl@0: } sl@0: } sl@0: #endif sl@0: sl@0: r->top=max; sl@0: r->neg=0; sl@0: bn_correct_top(r); sl@0: return(1); sl@0: } sl@0: sl@0: EXPORT_C int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) sl@0: { sl@0: int max; sl@0: int add=0,neg=0; sl@0: const BIGNUM *tmp; sl@0: sl@0: bn_check_top(a); sl@0: bn_check_top(b); sl@0: sl@0: /* a - b a-b sl@0: * a - -b a+b sl@0: * -a - b -(a+b) sl@0: * -a - -b b-a sl@0: */ sl@0: if (a->neg) sl@0: { sl@0: if (b->neg) sl@0: { tmp=a; a=b; b=tmp; } sl@0: else sl@0: { add=1; neg=1; } sl@0: } sl@0: else sl@0: { sl@0: if (b->neg) { add=1; neg=0; } sl@0: } sl@0: sl@0: if (add) sl@0: { sl@0: if (!BN_uadd(r,a,b)) return(0); sl@0: r->neg=neg; sl@0: return(1); sl@0: } sl@0: sl@0: /* We are actually doing a - b :-) */ sl@0: sl@0: max=(a->top > b->top)?a->top:b->top; sl@0: if (bn_wexpand(r,max) == NULL) return(0); sl@0: if (BN_ucmp(a,b) < 0) sl@0: { sl@0: if (!BN_usub(r,b,a)) return(0); sl@0: r->neg=1; sl@0: } sl@0: else sl@0: { sl@0: if (!BN_usub(r,a,b)) return(0); sl@0: r->neg=0; sl@0: } sl@0: bn_check_top(r); sl@0: return(1); sl@0: } sl@0: