xref: /onnv-gate/usr/src/common/openssl/crypto/bn/bn_add.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/bn/bn_add.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include "cryptlib.h"
610Sstevel@tonic-gate #include "bn_lcl.h"
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /* r can == a or b */
BN_add(BIGNUM * r,const BIGNUM * a,const BIGNUM * b)640Sstevel@tonic-gate int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
650Sstevel@tonic-gate 	{
660Sstevel@tonic-gate 	const BIGNUM *tmp;
67*2139Sjp161948 	int a_neg = a->neg, ret;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	bn_check_top(a);
700Sstevel@tonic-gate 	bn_check_top(b);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	/*  a +  b	a+b
730Sstevel@tonic-gate 	 *  a + -b	a-b
740Sstevel@tonic-gate 	 * -a +  b	b-a
750Sstevel@tonic-gate 	 * -a + -b	-(a+b)
760Sstevel@tonic-gate 	 */
770Sstevel@tonic-gate 	if (a_neg ^ b->neg)
780Sstevel@tonic-gate 		{
790Sstevel@tonic-gate 		/* only one is negative */
800Sstevel@tonic-gate 		if (a_neg)
810Sstevel@tonic-gate 			{ tmp=a; a=b; b=tmp; }
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 		/* we are now a - b */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 		if (BN_ucmp(a,b) < 0)
860Sstevel@tonic-gate 			{
870Sstevel@tonic-gate 			if (!BN_usub(r,b,a)) return(0);
880Sstevel@tonic-gate 			r->neg=1;
890Sstevel@tonic-gate 			}
900Sstevel@tonic-gate 		else
910Sstevel@tonic-gate 			{
920Sstevel@tonic-gate 			if (!BN_usub(r,a,b)) return(0);
930Sstevel@tonic-gate 			r->neg=0;
940Sstevel@tonic-gate 			}
950Sstevel@tonic-gate 		return(1);
960Sstevel@tonic-gate 		}
970Sstevel@tonic-gate 
98*2139Sjp161948 	ret = BN_uadd(r,a,b);
99*2139Sjp161948 	r->neg = a_neg;
100*2139Sjp161948 	bn_check_top(r);
101*2139Sjp161948 	return ret;
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 
104*2139Sjp161948 /* unsigned add of b to a */
BN_uadd(BIGNUM * r,const BIGNUM * a,const BIGNUM * b)1050Sstevel@tonic-gate int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
1060Sstevel@tonic-gate 	{
107*2139Sjp161948 	int max,min,dif;
108*2139Sjp161948 	BN_ULONG *ap,*bp,*rp,carry,t1,t2;
1090Sstevel@tonic-gate 	const BIGNUM *tmp;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	bn_check_top(a);
1120Sstevel@tonic-gate 	bn_check_top(b);
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	if (a->top < b->top)
1150Sstevel@tonic-gate 		{ tmp=a; a=b; b=tmp; }
116*2139Sjp161948 	max = a->top;
117*2139Sjp161948 	min = b->top;
118*2139Sjp161948 	dif = max - min;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	if (bn_wexpand(r,max+1) == NULL)
121*2139Sjp161948 		return 0;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	r->top=max;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	ap=a->d;
1270Sstevel@tonic-gate 	bp=b->d;
1280Sstevel@tonic-gate 	rp=r->d;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	carry=bn_add_words(rp,ap,bp,min);
1310Sstevel@tonic-gate 	rp+=min;
1320Sstevel@tonic-gate 	ap+=min;
1330Sstevel@tonic-gate 	bp+=min;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	if (carry)
1360Sstevel@tonic-gate 		{
137*2139Sjp161948 		while (dif)
1380Sstevel@tonic-gate 			{
139*2139Sjp161948 			dif--;
140*2139Sjp161948 			t1 = *(ap++);
141*2139Sjp161948 			t2 = (t1+1) & BN_MASK2;
142*2139Sjp161948 			*(rp++) = t2;
143*2139Sjp161948 			if (t2)
1440Sstevel@tonic-gate 				{
1450Sstevel@tonic-gate 				carry=0;
1460Sstevel@tonic-gate 				break;
1470Sstevel@tonic-gate 				}
1480Sstevel@tonic-gate 			}
149*2139Sjp161948 		if (carry)
1500Sstevel@tonic-gate 			{
151*2139Sjp161948 			/* carry != 0 => dif == 0 */
152*2139Sjp161948 			*rp = 1;
1530Sstevel@tonic-gate 			r->top++;
1540Sstevel@tonic-gate 			}
1550Sstevel@tonic-gate 		}
156*2139Sjp161948 	if (dif && rp != ap)
157*2139Sjp161948 		while (dif--)
158*2139Sjp161948 			/* copy remaining words if ap != rp */
159*2139Sjp161948 			*(rp++) = *(ap++);
1600Sstevel@tonic-gate 	r->neg = 0;
161*2139Sjp161948 	bn_check_top(r);
162*2139Sjp161948 	return 1;
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate /* unsigned subtraction of b from a, a must be larger than b. */
BN_usub(BIGNUM * r,const BIGNUM * a,const BIGNUM * b)1660Sstevel@tonic-gate int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
1670Sstevel@tonic-gate 	{
168*2139Sjp161948 	int max,min,dif;
1690Sstevel@tonic-gate 	register BN_ULONG t1,t2,*ap,*bp,*rp;
1700Sstevel@tonic-gate 	int i,carry;
1710Sstevel@tonic-gate #if defined(IRIX_CC_BUG) && !defined(LINT)
1720Sstevel@tonic-gate 	int dummy;
1730Sstevel@tonic-gate #endif
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	bn_check_top(a);
1760Sstevel@tonic-gate 	bn_check_top(b);
1770Sstevel@tonic-gate 
178*2139Sjp161948 	max = a->top;
179*2139Sjp161948 	min = b->top;
180*2139Sjp161948 	dif = max - min;
181*2139Sjp161948 
182*2139Sjp161948 	if (dif < 0)	/* hmm... should not be happening */
1830Sstevel@tonic-gate 		{
1840Sstevel@tonic-gate 		BNerr(BN_F_BN_USUB,BN_R_ARG2_LT_ARG3);
1850Sstevel@tonic-gate 		return(0);
1860Sstevel@tonic-gate 		}
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if (bn_wexpand(r,max) == NULL) return(0);
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	ap=a->d;
1910Sstevel@tonic-gate 	bp=b->d;
1920Sstevel@tonic-gate 	rp=r->d;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate #if 1
1950Sstevel@tonic-gate 	carry=0;
196*2139Sjp161948 	for (i = min; i != 0; i--)
1970Sstevel@tonic-gate 		{
1980Sstevel@tonic-gate 		t1= *(ap++);
1990Sstevel@tonic-gate 		t2= *(bp++);
2000Sstevel@tonic-gate 		if (carry)
2010Sstevel@tonic-gate 			{
2020Sstevel@tonic-gate 			carry=(t1 <= t2);
2030Sstevel@tonic-gate 			t1=(t1-t2-1)&BN_MASK2;
2040Sstevel@tonic-gate 			}
2050Sstevel@tonic-gate 		else
2060Sstevel@tonic-gate 			{
2070Sstevel@tonic-gate 			carry=(t1 < t2);
2080Sstevel@tonic-gate 			t1=(t1-t2)&BN_MASK2;
2090Sstevel@tonic-gate 			}
2100Sstevel@tonic-gate #if defined(IRIX_CC_BUG) && !defined(LINT)
2110Sstevel@tonic-gate 		dummy=t1;
2120Sstevel@tonic-gate #endif
2130Sstevel@tonic-gate 		*(rp++)=t1&BN_MASK2;
2140Sstevel@tonic-gate 		}
2150Sstevel@tonic-gate #else
2160Sstevel@tonic-gate 	carry=bn_sub_words(rp,ap,bp,min);
2170Sstevel@tonic-gate 	ap+=min;
2180Sstevel@tonic-gate 	bp+=min;
2190Sstevel@tonic-gate 	rp+=min;
2200Sstevel@tonic-gate #endif
2210Sstevel@tonic-gate 	if (carry) /* subtracted */
2220Sstevel@tonic-gate 		{
223*2139Sjp161948 		if (!dif)
224*2139Sjp161948 			/* error: a < b */
225*2139Sjp161948 			return 0;
226*2139Sjp161948 		while (dif)
2270Sstevel@tonic-gate 			{
228*2139Sjp161948 			dif--;
229*2139Sjp161948 			t1 = *(ap++);
230*2139Sjp161948 			t2 = (t1-1)&BN_MASK2;
231*2139Sjp161948 			*(rp++) = t2;
232*2139Sjp161948 			if (t1)
233*2139Sjp161948 				break;
2340Sstevel@tonic-gate 			}
2350Sstevel@tonic-gate 		}
2360Sstevel@tonic-gate #if 0
2370Sstevel@tonic-gate 	memcpy(rp,ap,sizeof(*rp)*(max-i));
2380Sstevel@tonic-gate #else
2390Sstevel@tonic-gate 	if (rp != ap)
2400Sstevel@tonic-gate 		{
2410Sstevel@tonic-gate 		for (;;)
2420Sstevel@tonic-gate 			{
243*2139Sjp161948 			if (!dif--) break;
2440Sstevel@tonic-gate 			rp[0]=ap[0];
245*2139Sjp161948 			if (!dif--) break;
2460Sstevel@tonic-gate 			rp[1]=ap[1];
247*2139Sjp161948 			if (!dif--) break;
2480Sstevel@tonic-gate 			rp[2]=ap[2];
249*2139Sjp161948 			if (!dif--) break;
2500Sstevel@tonic-gate 			rp[3]=ap[3];
2510Sstevel@tonic-gate 			rp+=4;
2520Sstevel@tonic-gate 			ap+=4;
2530Sstevel@tonic-gate 			}
2540Sstevel@tonic-gate 		}
2550Sstevel@tonic-gate #endif
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	r->top=max;
2580Sstevel@tonic-gate 	r->neg=0;
259*2139Sjp161948 	bn_correct_top(r);
2600Sstevel@tonic-gate 	return(1);
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 
BN_sub(BIGNUM * r,const BIGNUM * a,const BIGNUM * b)2630Sstevel@tonic-gate int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
2640Sstevel@tonic-gate 	{
2650Sstevel@tonic-gate 	int max;
2660Sstevel@tonic-gate 	int add=0,neg=0;
2670Sstevel@tonic-gate 	const BIGNUM *tmp;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	bn_check_top(a);
2700Sstevel@tonic-gate 	bn_check_top(b);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	/*  a -  b	a-b
2730Sstevel@tonic-gate 	 *  a - -b	a+b
2740Sstevel@tonic-gate 	 * -a -  b	-(a+b)
2750Sstevel@tonic-gate 	 * -a - -b	b-a
2760Sstevel@tonic-gate 	 */
2770Sstevel@tonic-gate 	if (a->neg)
2780Sstevel@tonic-gate 		{
2790Sstevel@tonic-gate 		if (b->neg)
2800Sstevel@tonic-gate 			{ tmp=a; a=b; b=tmp; }
2810Sstevel@tonic-gate 		else
2820Sstevel@tonic-gate 			{ add=1; neg=1; }
2830Sstevel@tonic-gate 		}
2840Sstevel@tonic-gate 	else
2850Sstevel@tonic-gate 		{
2860Sstevel@tonic-gate 		if (b->neg) { add=1; neg=0; }
2870Sstevel@tonic-gate 		}
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	if (add)
2900Sstevel@tonic-gate 		{
2910Sstevel@tonic-gate 		if (!BN_uadd(r,a,b)) return(0);
2920Sstevel@tonic-gate 		r->neg=neg;
2930Sstevel@tonic-gate 		return(1);
2940Sstevel@tonic-gate 		}
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	/* We are actually doing a - b :-) */
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	max=(a->top > b->top)?a->top:b->top;
2990Sstevel@tonic-gate 	if (bn_wexpand(r,max) == NULL) return(0);
3000Sstevel@tonic-gate 	if (BN_ucmp(a,b) < 0)
3010Sstevel@tonic-gate 		{
3020Sstevel@tonic-gate 		if (!BN_usub(r,b,a)) return(0);
3030Sstevel@tonic-gate 		r->neg=1;
3040Sstevel@tonic-gate 		}
3050Sstevel@tonic-gate 	else
3060Sstevel@tonic-gate 		{
3070Sstevel@tonic-gate 		if (!BN_usub(r,a,b)) return(0);
3080Sstevel@tonic-gate 		r->neg=0;
3090Sstevel@tonic-gate 		}
310*2139Sjp161948 	bn_check_top(r);
3110Sstevel@tonic-gate 	return(1);
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 
314