os/ossrv/ssl/libcrypto/src/crypto/bn/bn_add.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* crypto/bn/bn_add.c */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
sl@0
    59
#include <stdio.h>
sl@0
    60
#include "cryptlib.h"
sl@0
    61
#include "bn_lcl.h"
sl@0
    62
sl@0
    63
/* r can == a or b */
sl@0
    64
EXPORT_C int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
sl@0
    65
	{
sl@0
    66
	const BIGNUM *tmp;
sl@0
    67
	int a_neg = a->neg, ret;
sl@0
    68
sl@0
    69
	bn_check_top(a);
sl@0
    70
	bn_check_top(b);
sl@0
    71
sl@0
    72
	/*  a +  b	a+b
sl@0
    73
	 *  a + -b	a-b
sl@0
    74
	 * -a +  b	b-a
sl@0
    75
	 * -a + -b	-(a+b)
sl@0
    76
	 */
sl@0
    77
	if (a_neg ^ b->neg)
sl@0
    78
		{
sl@0
    79
		/* only one is negative */
sl@0
    80
		if (a_neg)
sl@0
    81
			{ tmp=a; a=b; b=tmp; }
sl@0
    82
sl@0
    83
		/* we are now a - b */
sl@0
    84
sl@0
    85
		if (BN_ucmp(a,b) < 0)
sl@0
    86
			{
sl@0
    87
			if (!BN_usub(r,b,a)) return(0);
sl@0
    88
			r->neg=1;
sl@0
    89
			}
sl@0
    90
		else
sl@0
    91
			{
sl@0
    92
			if (!BN_usub(r,a,b)) return(0);
sl@0
    93
			r->neg=0;
sl@0
    94
			}
sl@0
    95
		return(1);
sl@0
    96
		}
sl@0
    97
sl@0
    98
	ret = BN_uadd(r,a,b);
sl@0
    99
	r->neg = a_neg;
sl@0
   100
	bn_check_top(r);
sl@0
   101
	return ret;
sl@0
   102
	}
sl@0
   103
sl@0
   104
/* unsigned add of b to a */
sl@0
   105
EXPORT_C int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
sl@0
   106
	{
sl@0
   107
	int max,min,dif;
sl@0
   108
	BN_ULONG *ap,*bp,*rp,carry,t1,t2;
sl@0
   109
	const BIGNUM *tmp;
sl@0
   110
sl@0
   111
	bn_check_top(a);
sl@0
   112
	bn_check_top(b);
sl@0
   113
sl@0
   114
	if (a->top < b->top)
sl@0
   115
		{ tmp=a; a=b; b=tmp; }
sl@0
   116
	max = a->top;
sl@0
   117
	min = b->top;
sl@0
   118
	dif = max - min;
sl@0
   119
sl@0
   120
	if (bn_wexpand(r,max+1) == NULL)
sl@0
   121
		return 0;
sl@0
   122
sl@0
   123
	r->top=max;
sl@0
   124
sl@0
   125
sl@0
   126
	ap=a->d;
sl@0
   127
	bp=b->d;
sl@0
   128
	rp=r->d;
sl@0
   129
sl@0
   130
	carry=bn_add_words(rp,ap,bp,min);
sl@0
   131
	rp+=min;
sl@0
   132
	ap+=min;
sl@0
   133
	bp+=min;
sl@0
   134
sl@0
   135
	if (carry)
sl@0
   136
		{
sl@0
   137
		while (dif)
sl@0
   138
			{
sl@0
   139
			dif--;
sl@0
   140
			t1 = *(ap++);
sl@0
   141
			t2 = (t1+1) & BN_MASK2;
sl@0
   142
			*(rp++) = t2;
sl@0
   143
			if (t2)
sl@0
   144
				{
sl@0
   145
				carry=0;
sl@0
   146
				break;
sl@0
   147
				}
sl@0
   148
			}
sl@0
   149
		if (carry)
sl@0
   150
			{
sl@0
   151
			/* carry != 0 => dif == 0 */
sl@0
   152
			*rp = 1;
sl@0
   153
			r->top++;
sl@0
   154
			}
sl@0
   155
		}
sl@0
   156
	if (dif && rp != ap)
sl@0
   157
		while (dif--)
sl@0
   158
			/* copy remaining words if ap != rp */
sl@0
   159
			*(rp++) = *(ap++);
sl@0
   160
	r->neg = 0;
sl@0
   161
	bn_check_top(r);
sl@0
   162
	return 1;
sl@0
   163
	}
sl@0
   164
sl@0
   165
/* unsigned subtraction of b from a, a must be larger than b. */
sl@0
   166
EXPORT_C int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
sl@0
   167
	{
sl@0
   168
	int max,min,dif;
sl@0
   169
	register BN_ULONG t1,t2,*ap,*bp,*rp;
sl@0
   170
	int i,carry;
sl@0
   171
#if defined(IRIX_CC_BUG) && !defined(LINT)
sl@0
   172
	int dummy;
sl@0
   173
#endif
sl@0
   174
sl@0
   175
	bn_check_top(a);
sl@0
   176
	bn_check_top(b);
sl@0
   177
sl@0
   178
	max = a->top;
sl@0
   179
	min = b->top;
sl@0
   180
	dif = max - min;
sl@0
   181
sl@0
   182
	if (dif < 0)	/* hmm... should not be happening */
sl@0
   183
		{
sl@0
   184
		BNerr(BN_F_BN_USUB,BN_R_ARG2_LT_ARG3);
sl@0
   185
		return(0);
sl@0
   186
		}
sl@0
   187
sl@0
   188
	if (bn_wexpand(r,max) == NULL) return(0);
sl@0
   189
sl@0
   190
	ap=a->d;
sl@0
   191
	bp=b->d;
sl@0
   192
	rp=r->d;
sl@0
   193
sl@0
   194
#if 1
sl@0
   195
	carry=0;
sl@0
   196
	for (i = min; i != 0; i--)
sl@0
   197
		{
sl@0
   198
		t1= *(ap++);
sl@0
   199
		t2= *(bp++);
sl@0
   200
		if (carry)
sl@0
   201
			{
sl@0
   202
			carry=(t1 <= t2);
sl@0
   203
			t1=(t1-t2-1)&BN_MASK2;
sl@0
   204
			}
sl@0
   205
		else
sl@0
   206
			{
sl@0
   207
			carry=(t1 < t2);
sl@0
   208
			t1=(t1-t2)&BN_MASK2;
sl@0
   209
			}
sl@0
   210
#if defined(IRIX_CC_BUG) && !defined(LINT)
sl@0
   211
		dummy=t1;
sl@0
   212
#endif
sl@0
   213
		*(rp++)=t1&BN_MASK2;
sl@0
   214
		}
sl@0
   215
#else
sl@0
   216
	carry=bn_sub_words(rp,ap,bp,min);
sl@0
   217
	ap+=min;
sl@0
   218
	bp+=min;
sl@0
   219
	rp+=min;
sl@0
   220
#endif
sl@0
   221
	if (carry) /* subtracted */
sl@0
   222
		{
sl@0
   223
		if (!dif)
sl@0
   224
			/* error: a < b */
sl@0
   225
			return 0;
sl@0
   226
		while (dif)
sl@0
   227
			{
sl@0
   228
			dif--;
sl@0
   229
			t1 = *(ap++);
sl@0
   230
			t2 = (t1-1)&BN_MASK2;
sl@0
   231
			*(rp++) = t2;
sl@0
   232
			if (t1)
sl@0
   233
				break;
sl@0
   234
			}
sl@0
   235
		}
sl@0
   236
#if 0
sl@0
   237
	memcpy(rp,ap,sizeof(*rp)*(max-i));
sl@0
   238
#else
sl@0
   239
	if (rp != ap)
sl@0
   240
		{
sl@0
   241
		for (;;)
sl@0
   242
			{
sl@0
   243
			if (!dif--) break;
sl@0
   244
			rp[0]=ap[0];
sl@0
   245
			if (!dif--) break;
sl@0
   246
			rp[1]=ap[1];
sl@0
   247
			if (!dif--) break;
sl@0
   248
			rp[2]=ap[2];
sl@0
   249
			if (!dif--) break;
sl@0
   250
			rp[3]=ap[3];
sl@0
   251
			rp+=4;
sl@0
   252
			ap+=4;
sl@0
   253
			}
sl@0
   254
		}
sl@0
   255
#endif
sl@0
   256
sl@0
   257
	r->top=max;
sl@0
   258
	r->neg=0;
sl@0
   259
	bn_correct_top(r);
sl@0
   260
	return(1);
sl@0
   261
	}
sl@0
   262
sl@0
   263
EXPORT_C int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
sl@0
   264
	{
sl@0
   265
	int max;
sl@0
   266
	int add=0,neg=0;
sl@0
   267
	const BIGNUM *tmp;
sl@0
   268
sl@0
   269
	bn_check_top(a);
sl@0
   270
	bn_check_top(b);
sl@0
   271
sl@0
   272
	/*  a -  b	a-b
sl@0
   273
	 *  a - -b	a+b
sl@0
   274
	 * -a -  b	-(a+b)
sl@0
   275
	 * -a - -b	b-a
sl@0
   276
	 */
sl@0
   277
	if (a->neg)
sl@0
   278
		{
sl@0
   279
		if (b->neg)
sl@0
   280
			{ tmp=a; a=b; b=tmp; }
sl@0
   281
		else
sl@0
   282
			{ add=1; neg=1; }
sl@0
   283
		}
sl@0
   284
	else
sl@0
   285
		{
sl@0
   286
		if (b->neg) { add=1; neg=0; }
sl@0
   287
		}
sl@0
   288
sl@0
   289
	if (add)
sl@0
   290
		{
sl@0
   291
		if (!BN_uadd(r,a,b)) return(0);
sl@0
   292
		r->neg=neg;
sl@0
   293
		return(1);
sl@0
   294
		}
sl@0
   295
sl@0
   296
	/* We are actually doing a - b :-) */
sl@0
   297
sl@0
   298
	max=(a->top > b->top)?a->top:b->top;
sl@0
   299
	if (bn_wexpand(r,max) == NULL) return(0);
sl@0
   300
	if (BN_ucmp(a,b) < 0)
sl@0
   301
		{
sl@0
   302
		if (!BN_usub(r,b,a)) return(0);
sl@0
   303
		r->neg=1;
sl@0
   304
		}
sl@0
   305
	else
sl@0
   306
		{
sl@0
   307
		if (!BN_usub(r,a,b)) return(0);
sl@0
   308
		r->neg=0;
sl@0
   309
		}
sl@0
   310
	bn_check_top(r);
sl@0
   311
	return(1);
sl@0
   312
	}
sl@0
   313