xref: /onnv-gate/usr/src/common/openssl/crypto/bn/bn_word.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/bn/bn_word.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 
BN_mod_word(const BIGNUM * a,BN_ULONG w)630Sstevel@tonic-gate BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w)
640Sstevel@tonic-gate 	{
650Sstevel@tonic-gate #ifndef BN_LLONG
660Sstevel@tonic-gate 	BN_ULONG ret=0;
670Sstevel@tonic-gate #else
680Sstevel@tonic-gate 	BN_ULLONG ret=0;
690Sstevel@tonic-gate #endif
700Sstevel@tonic-gate 	int i;
710Sstevel@tonic-gate 
72*2139Sjp161948 	if (w == 0)
73*2139Sjp161948 		return (BN_ULONG)-1;
74*2139Sjp161948 
75*2139Sjp161948 	bn_check_top(a);
760Sstevel@tonic-gate 	w&=BN_MASK2;
770Sstevel@tonic-gate 	for (i=a->top-1; i>=0; i--)
780Sstevel@tonic-gate 		{
790Sstevel@tonic-gate #ifndef BN_LLONG
800Sstevel@tonic-gate 		ret=((ret<<BN_BITS4)|((a->d[i]>>BN_BITS4)&BN_MASK2l))%w;
810Sstevel@tonic-gate 		ret=((ret<<BN_BITS4)|(a->d[i]&BN_MASK2l))%w;
820Sstevel@tonic-gate #else
830Sstevel@tonic-gate 		ret=(BN_ULLONG)(((ret<<(BN_ULLONG)BN_BITS2)|a->d[i])%
840Sstevel@tonic-gate 			(BN_ULLONG)w);
850Sstevel@tonic-gate #endif
860Sstevel@tonic-gate 		}
870Sstevel@tonic-gate 	return((BN_ULONG)ret);
880Sstevel@tonic-gate 	}
890Sstevel@tonic-gate 
BN_div_word(BIGNUM * a,BN_ULONG w)900Sstevel@tonic-gate BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w)
910Sstevel@tonic-gate 	{
92*2139Sjp161948 	BN_ULONG ret = 0;
93*2139Sjp161948 	int i, j;
94*2139Sjp161948 
95*2139Sjp161948 	bn_check_top(a);
96*2139Sjp161948 	w &= BN_MASK2;
970Sstevel@tonic-gate 
98*2139Sjp161948 	if (!w)
99*2139Sjp161948 		/* actually this an error (division by zero) */
100*2139Sjp161948 		return (BN_ULONG)-1;
101*2139Sjp161948 	if (a->top == 0)
102*2139Sjp161948 		return 0;
103*2139Sjp161948 
104*2139Sjp161948 	/* normalize input (so bn_div_words doesn't complain) */
105*2139Sjp161948 	j = BN_BITS2 - BN_num_bits_word(w);
106*2139Sjp161948 	w <<= j;
107*2139Sjp161948 	if (!BN_lshift(a, a, j))
108*2139Sjp161948 		return (BN_ULONG)-1;
109*2139Sjp161948 
1100Sstevel@tonic-gate 	for (i=a->top-1; i>=0; i--)
1110Sstevel@tonic-gate 		{
1120Sstevel@tonic-gate 		BN_ULONG l,d;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 		l=a->d[i];
1150Sstevel@tonic-gate 		d=bn_div_words(ret,l,w);
1160Sstevel@tonic-gate 		ret=(l-((d*w)&BN_MASK2))&BN_MASK2;
1170Sstevel@tonic-gate 		a->d[i]=d;
1180Sstevel@tonic-gate 		}
1190Sstevel@tonic-gate 	if ((a->top > 0) && (a->d[a->top-1] == 0))
1200Sstevel@tonic-gate 		a->top--;
121*2139Sjp161948 	ret >>= j;
122*2139Sjp161948 	bn_check_top(a);
1230Sstevel@tonic-gate 	return(ret);
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 
BN_add_word(BIGNUM * a,BN_ULONG w)1260Sstevel@tonic-gate int BN_add_word(BIGNUM *a, BN_ULONG w)
1270Sstevel@tonic-gate 	{
1280Sstevel@tonic-gate 	BN_ULONG l;
1290Sstevel@tonic-gate 	int i;
1300Sstevel@tonic-gate 
131*2139Sjp161948 	bn_check_top(a);
132*2139Sjp161948 	w &= BN_MASK2;
133*2139Sjp161948 
134*2139Sjp161948 	/* degenerate case: w is zero */
135*2139Sjp161948 	if (!w) return 1;
136*2139Sjp161948 	/* degenerate case: a is zero */
137*2139Sjp161948 	if(BN_is_zero(a)) return BN_set_word(a, w);
138*2139Sjp161948 	/* handle 'a' when negative */
1390Sstevel@tonic-gate 	if (a->neg)
1400Sstevel@tonic-gate 		{
1410Sstevel@tonic-gate 		a->neg=0;
1420Sstevel@tonic-gate 		i=BN_sub_word(a,w);
1430Sstevel@tonic-gate 		if (!BN_is_zero(a))
1440Sstevel@tonic-gate 			a->neg=!(a->neg);
1450Sstevel@tonic-gate 		return(i);
1460Sstevel@tonic-gate 		}
147*2139Sjp161948 	/* Only expand (and risk failing) if it's possibly necessary */
148*2139Sjp161948 	if (((BN_ULONG)(a->d[a->top - 1] + 1) == 0) &&
149*2139Sjp161948 			(bn_wexpand(a,a->top+1) == NULL))
150*2139Sjp161948 		return(0);
1510Sstevel@tonic-gate 	i=0;
1520Sstevel@tonic-gate 	for (;;)
1530Sstevel@tonic-gate 		{
1540Sstevel@tonic-gate 		if (i >= a->top)
1550Sstevel@tonic-gate 			l=w;
1560Sstevel@tonic-gate 		else
157*2139Sjp161948 			l=(a->d[i]+w)&BN_MASK2;
1580Sstevel@tonic-gate 		a->d[i]=l;
1590Sstevel@tonic-gate 		if (w > l)
1600Sstevel@tonic-gate 			w=1;
1610Sstevel@tonic-gate 		else
1620Sstevel@tonic-gate 			break;
1630Sstevel@tonic-gate 		i++;
1640Sstevel@tonic-gate 		}
1650Sstevel@tonic-gate 	if (i >= a->top)
1660Sstevel@tonic-gate 		a->top++;
167*2139Sjp161948 	bn_check_top(a);
1680Sstevel@tonic-gate 	return(1);
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 
BN_sub_word(BIGNUM * a,BN_ULONG w)1710Sstevel@tonic-gate int BN_sub_word(BIGNUM *a, BN_ULONG w)
1720Sstevel@tonic-gate 	{
1730Sstevel@tonic-gate 	int i;
1740Sstevel@tonic-gate 
175*2139Sjp161948 	bn_check_top(a);
176*2139Sjp161948 	w &= BN_MASK2;
177*2139Sjp161948 
178*2139Sjp161948 	/* degenerate case: w is zero */
179*2139Sjp161948 	if (!w) return 1;
180*2139Sjp161948 	/* degenerate case: a is zero */
181*2139Sjp161948 	if(BN_is_zero(a))
182*2139Sjp161948 		{
183*2139Sjp161948 		i = BN_set_word(a,w);
184*2139Sjp161948 		if (i != 0)
185*2139Sjp161948 			BN_set_negative(a, 1);
186*2139Sjp161948 		return i;
187*2139Sjp161948 		}
188*2139Sjp161948 	/* handle 'a' when negative */
189*2139Sjp161948 	if (a->neg)
1900Sstevel@tonic-gate 		{
1910Sstevel@tonic-gate 		a->neg=0;
1920Sstevel@tonic-gate 		i=BN_add_word(a,w);
1930Sstevel@tonic-gate 		a->neg=1;
1940Sstevel@tonic-gate 		return(i);
1950Sstevel@tonic-gate 		}
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if ((a->top == 1) && (a->d[0] < w))
1980Sstevel@tonic-gate 		{
1990Sstevel@tonic-gate 		a->d[0]=w-a->d[0];
2000Sstevel@tonic-gate 		a->neg=1;
2010Sstevel@tonic-gate 		return(1);
2020Sstevel@tonic-gate 		}
2030Sstevel@tonic-gate 	i=0;
2040Sstevel@tonic-gate 	for (;;)
2050Sstevel@tonic-gate 		{
2060Sstevel@tonic-gate 		if (a->d[i] >= w)
2070Sstevel@tonic-gate 			{
2080Sstevel@tonic-gate 			a->d[i]-=w;
2090Sstevel@tonic-gate 			break;
2100Sstevel@tonic-gate 			}
2110Sstevel@tonic-gate 		else
2120Sstevel@tonic-gate 			{
2130Sstevel@tonic-gate 			a->d[i]=(a->d[i]-w)&BN_MASK2;
2140Sstevel@tonic-gate 			i++;
2150Sstevel@tonic-gate 			w=1;
2160Sstevel@tonic-gate 			}
2170Sstevel@tonic-gate 		}
2180Sstevel@tonic-gate 	if ((a->d[i] == 0) && (i == (a->top-1)))
2190Sstevel@tonic-gate 		a->top--;
220*2139Sjp161948 	bn_check_top(a);
2210Sstevel@tonic-gate 	return(1);
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate 
BN_mul_word(BIGNUM * a,BN_ULONG w)2240Sstevel@tonic-gate int BN_mul_word(BIGNUM *a, BN_ULONG w)
2250Sstevel@tonic-gate 	{
2260Sstevel@tonic-gate 	BN_ULONG ll;
2270Sstevel@tonic-gate 
228*2139Sjp161948 	bn_check_top(a);
2290Sstevel@tonic-gate 	w&=BN_MASK2;
2300Sstevel@tonic-gate 	if (a->top)
2310Sstevel@tonic-gate 		{
2320Sstevel@tonic-gate 		if (w == 0)
2330Sstevel@tonic-gate 			BN_zero(a);
2340Sstevel@tonic-gate 		else
2350Sstevel@tonic-gate 			{
2360Sstevel@tonic-gate 			ll=bn_mul_words(a->d,a->d,a->top,w);
2370Sstevel@tonic-gate 			if (ll)
2380Sstevel@tonic-gate 				{
2390Sstevel@tonic-gate 				if (bn_wexpand(a,a->top+1) == NULL) return(0);
2400Sstevel@tonic-gate 				a->d[a->top++]=ll;
2410Sstevel@tonic-gate 				}
2420Sstevel@tonic-gate 			}
2430Sstevel@tonic-gate 		}
244*2139Sjp161948 	bn_check_top(a);
2450Sstevel@tonic-gate 	return(1);
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
248