xref: /onnv-gate/usr/src/common/openssl/crypto/bn/bn_lib.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* crypto/bn/bn_lib.c */
2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * This package is an SSL implementation written
6*0Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
7*0Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
10*0Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
11*0Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
12*0Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13*0Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
14*0Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15*0Sstevel@tonic-gate  *
16*0Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
17*0Sstevel@tonic-gate  * the code are not to be removed.
18*0Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
19*0Sstevel@tonic-gate  * as the author of the parts of the library used.
20*0Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
21*0Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
22*0Sstevel@tonic-gate  *
23*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
24*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
25*0Sstevel@tonic-gate  * are met:
26*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
27*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
28*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
29*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
30*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
31*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
32*0Sstevel@tonic-gate  *    must display the following acknowledgement:
33*0Sstevel@tonic-gate  *    "This product includes cryptographic software written by
34*0Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
35*0Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
36*0Sstevel@tonic-gate  *    being used are not cryptographic related :-).
37*0Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
38*0Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
39*0Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*0Sstevel@tonic-gate  * SUCH DAMAGE.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
54*0Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55*0Sstevel@tonic-gate  * copied and put under another distribution licence
56*0Sstevel@tonic-gate  * [including the GNU Public Licence.]
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #ifndef BN_DEBUG
60*0Sstevel@tonic-gate # undef NDEBUG /* avoid conflicting definitions */
61*0Sstevel@tonic-gate # define NDEBUG
62*0Sstevel@tonic-gate #endif
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate #include <assert.h>
65*0Sstevel@tonic-gate #include <limits.h>
66*0Sstevel@tonic-gate #include <stdio.h>
67*0Sstevel@tonic-gate #include "cryptlib.h"
68*0Sstevel@tonic-gate #include "bn_lcl.h"
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate const char *BN_version="Big Number" OPENSSL_VERSION_PTEXT;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate /* For a 32 bit machine
73*0Sstevel@tonic-gate  * 2 -   4 ==  128
74*0Sstevel@tonic-gate  * 3 -   8 ==  256
75*0Sstevel@tonic-gate  * 4 -  16 ==  512
76*0Sstevel@tonic-gate  * 5 -  32 == 1024
77*0Sstevel@tonic-gate  * 6 -  64 == 2048
78*0Sstevel@tonic-gate  * 7 - 128 == 4096
79*0Sstevel@tonic-gate  * 8 - 256 == 8192
80*0Sstevel@tonic-gate  */
81*0Sstevel@tonic-gate static int bn_limit_bits=0;
82*0Sstevel@tonic-gate static int bn_limit_num=8;        /* (1<<bn_limit_bits) */
83*0Sstevel@tonic-gate static int bn_limit_bits_low=0;
84*0Sstevel@tonic-gate static int bn_limit_num_low=8;    /* (1<<bn_limit_bits_low) */
85*0Sstevel@tonic-gate static int bn_limit_bits_high=0;
86*0Sstevel@tonic-gate static int bn_limit_num_high=8;   /* (1<<bn_limit_bits_high) */
87*0Sstevel@tonic-gate static int bn_limit_bits_mont=0;
88*0Sstevel@tonic-gate static int bn_limit_num_mont=8;   /* (1<<bn_limit_bits_mont) */
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate void BN_set_params(int mult, int high, int low, int mont)
91*0Sstevel@tonic-gate 	{
92*0Sstevel@tonic-gate 	if (mult >= 0)
93*0Sstevel@tonic-gate 		{
94*0Sstevel@tonic-gate 		if (mult > (sizeof(int)*8)-1)
95*0Sstevel@tonic-gate 			mult=sizeof(int)*8-1;
96*0Sstevel@tonic-gate 		bn_limit_bits=mult;
97*0Sstevel@tonic-gate 		bn_limit_num=1<<mult;
98*0Sstevel@tonic-gate 		}
99*0Sstevel@tonic-gate 	if (high >= 0)
100*0Sstevel@tonic-gate 		{
101*0Sstevel@tonic-gate 		if (high > (sizeof(int)*8)-1)
102*0Sstevel@tonic-gate 			high=sizeof(int)*8-1;
103*0Sstevel@tonic-gate 		bn_limit_bits_high=high;
104*0Sstevel@tonic-gate 		bn_limit_num_high=1<<high;
105*0Sstevel@tonic-gate 		}
106*0Sstevel@tonic-gate 	if (low >= 0)
107*0Sstevel@tonic-gate 		{
108*0Sstevel@tonic-gate 		if (low > (sizeof(int)*8)-1)
109*0Sstevel@tonic-gate 			low=sizeof(int)*8-1;
110*0Sstevel@tonic-gate 		bn_limit_bits_low=low;
111*0Sstevel@tonic-gate 		bn_limit_num_low=1<<low;
112*0Sstevel@tonic-gate 		}
113*0Sstevel@tonic-gate 	if (mont >= 0)
114*0Sstevel@tonic-gate 		{
115*0Sstevel@tonic-gate 		if (mont > (sizeof(int)*8)-1)
116*0Sstevel@tonic-gate 			mont=sizeof(int)*8-1;
117*0Sstevel@tonic-gate 		bn_limit_bits_mont=mont;
118*0Sstevel@tonic-gate 		bn_limit_num_mont=1<<mont;
119*0Sstevel@tonic-gate 		}
120*0Sstevel@tonic-gate 	}
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate int BN_get_params(int which)
123*0Sstevel@tonic-gate 	{
124*0Sstevel@tonic-gate 	if      (which == 0) return(bn_limit_bits);
125*0Sstevel@tonic-gate 	else if (which == 1) return(bn_limit_bits_high);
126*0Sstevel@tonic-gate 	else if (which == 2) return(bn_limit_bits_low);
127*0Sstevel@tonic-gate 	else if (which == 3) return(bn_limit_bits_mont);
128*0Sstevel@tonic-gate 	else return(0);
129*0Sstevel@tonic-gate 	}
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate const BIGNUM *BN_value_one(void)
132*0Sstevel@tonic-gate 	{
133*0Sstevel@tonic-gate 	static BN_ULONG data_one=1L;
134*0Sstevel@tonic-gate 	static BIGNUM const_one={&data_one,1,1,0};
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	return(&const_one);
137*0Sstevel@tonic-gate 	}
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate char *BN_options(void)
140*0Sstevel@tonic-gate 	{
141*0Sstevel@tonic-gate 	static int init=0;
142*0Sstevel@tonic-gate 	static char data[16];
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	if (!init)
145*0Sstevel@tonic-gate 		{
146*0Sstevel@tonic-gate 		init++;
147*0Sstevel@tonic-gate #ifdef BN_LLONG
148*0Sstevel@tonic-gate 		BIO_snprintf(data,sizeof data,"bn(%d,%d)",
149*0Sstevel@tonic-gate 			     (int)sizeof(BN_ULLONG)*8,(int)sizeof(BN_ULONG)*8);
150*0Sstevel@tonic-gate #else
151*0Sstevel@tonic-gate 		BIO_snprintf(data,sizeof data,"bn(%d,%d)",
152*0Sstevel@tonic-gate 			     (int)sizeof(BN_ULONG)*8,(int)sizeof(BN_ULONG)*8);
153*0Sstevel@tonic-gate #endif
154*0Sstevel@tonic-gate 		}
155*0Sstevel@tonic-gate 	return(data);
156*0Sstevel@tonic-gate 	}
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate int BN_num_bits_word(BN_ULONG l)
159*0Sstevel@tonic-gate 	{
160*0Sstevel@tonic-gate 	static const char bits[256]={
161*0Sstevel@tonic-gate 		0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,
162*0Sstevel@tonic-gate 		5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
163*0Sstevel@tonic-gate 		6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
164*0Sstevel@tonic-gate 		6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
165*0Sstevel@tonic-gate 		7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
166*0Sstevel@tonic-gate 		7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
167*0Sstevel@tonic-gate 		7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
168*0Sstevel@tonic-gate 		7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
169*0Sstevel@tonic-gate 		8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
170*0Sstevel@tonic-gate 		8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
171*0Sstevel@tonic-gate 		8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
172*0Sstevel@tonic-gate 		8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
173*0Sstevel@tonic-gate 		8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
174*0Sstevel@tonic-gate 		8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
175*0Sstevel@tonic-gate 		8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
176*0Sstevel@tonic-gate 		8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
177*0Sstevel@tonic-gate 		};
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate #if defined(SIXTY_FOUR_BIT_LONG)
180*0Sstevel@tonic-gate 	if (l & 0xffffffff00000000L)
181*0Sstevel@tonic-gate 		{
182*0Sstevel@tonic-gate 		if (l & 0xffff000000000000L)
183*0Sstevel@tonic-gate 			{
184*0Sstevel@tonic-gate 			if (l & 0xff00000000000000L)
185*0Sstevel@tonic-gate 				{
186*0Sstevel@tonic-gate 				return(bits[(int)(l>>56)]+56);
187*0Sstevel@tonic-gate 				}
188*0Sstevel@tonic-gate 			else	return(bits[(int)(l>>48)]+48);
189*0Sstevel@tonic-gate 			}
190*0Sstevel@tonic-gate 		else
191*0Sstevel@tonic-gate 			{
192*0Sstevel@tonic-gate 			if (l & 0x0000ff0000000000L)
193*0Sstevel@tonic-gate 				{
194*0Sstevel@tonic-gate 				return(bits[(int)(l>>40)]+40);
195*0Sstevel@tonic-gate 				}
196*0Sstevel@tonic-gate 			else	return(bits[(int)(l>>32)]+32);
197*0Sstevel@tonic-gate 			}
198*0Sstevel@tonic-gate 		}
199*0Sstevel@tonic-gate 	else
200*0Sstevel@tonic-gate #else
201*0Sstevel@tonic-gate #ifdef SIXTY_FOUR_BIT
202*0Sstevel@tonic-gate 	if (l & 0xffffffff00000000LL)
203*0Sstevel@tonic-gate 		{
204*0Sstevel@tonic-gate 		if (l & 0xffff000000000000LL)
205*0Sstevel@tonic-gate 			{
206*0Sstevel@tonic-gate 			if (l & 0xff00000000000000LL)
207*0Sstevel@tonic-gate 				{
208*0Sstevel@tonic-gate 				return(bits[(int)(l>>56)]+56);
209*0Sstevel@tonic-gate 				}
210*0Sstevel@tonic-gate 			else	return(bits[(int)(l>>48)]+48);
211*0Sstevel@tonic-gate 			}
212*0Sstevel@tonic-gate 		else
213*0Sstevel@tonic-gate 			{
214*0Sstevel@tonic-gate 			if (l & 0x0000ff0000000000LL)
215*0Sstevel@tonic-gate 				{
216*0Sstevel@tonic-gate 				return(bits[(int)(l>>40)]+40);
217*0Sstevel@tonic-gate 				}
218*0Sstevel@tonic-gate 			else	return(bits[(int)(l>>32)]+32);
219*0Sstevel@tonic-gate 			}
220*0Sstevel@tonic-gate 		}
221*0Sstevel@tonic-gate 	else
222*0Sstevel@tonic-gate #endif
223*0Sstevel@tonic-gate #endif
224*0Sstevel@tonic-gate 		{
225*0Sstevel@tonic-gate #if defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
226*0Sstevel@tonic-gate 		if (l & 0xffff0000L)
227*0Sstevel@tonic-gate 			{
228*0Sstevel@tonic-gate 			if (l & 0xff000000L)
229*0Sstevel@tonic-gate 				return(bits[(int)(l>>24L)]+24);
230*0Sstevel@tonic-gate 			else	return(bits[(int)(l>>16L)]+16);
231*0Sstevel@tonic-gate 			}
232*0Sstevel@tonic-gate 		else
233*0Sstevel@tonic-gate #endif
234*0Sstevel@tonic-gate 			{
235*0Sstevel@tonic-gate #if defined(SIXTEEN_BIT) || defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
236*0Sstevel@tonic-gate 			if (l & 0xff00L)
237*0Sstevel@tonic-gate 				return(bits[(int)(l>>8)]+8);
238*0Sstevel@tonic-gate 			else
239*0Sstevel@tonic-gate #endif
240*0Sstevel@tonic-gate 				return(bits[(int)(l   )]  );
241*0Sstevel@tonic-gate 			}
242*0Sstevel@tonic-gate 		}
243*0Sstevel@tonic-gate 	}
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate int BN_num_bits(const BIGNUM *a)
246*0Sstevel@tonic-gate 	{
247*0Sstevel@tonic-gate 	BN_ULONG l;
248*0Sstevel@tonic-gate 	int i;
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	bn_check_top(a);
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	if (a->top == 0) return(0);
253*0Sstevel@tonic-gate 	l=a->d[a->top-1];
254*0Sstevel@tonic-gate 	assert(l != 0);
255*0Sstevel@tonic-gate 	i=(a->top-1)*BN_BITS2;
256*0Sstevel@tonic-gate 	return(i+BN_num_bits_word(l));
257*0Sstevel@tonic-gate 	}
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate void BN_clear_free(BIGNUM *a)
260*0Sstevel@tonic-gate 	{
261*0Sstevel@tonic-gate 	int i;
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 	if (a == NULL) return;
264*0Sstevel@tonic-gate 	if (a->d != NULL)
265*0Sstevel@tonic-gate 		{
266*0Sstevel@tonic-gate 		OPENSSL_cleanse(a->d,a->dmax*sizeof(a->d[0]));
267*0Sstevel@tonic-gate 		if (!(BN_get_flags(a,BN_FLG_STATIC_DATA)))
268*0Sstevel@tonic-gate 			OPENSSL_free(a->d);
269*0Sstevel@tonic-gate 		}
270*0Sstevel@tonic-gate 	i=BN_get_flags(a,BN_FLG_MALLOCED);
271*0Sstevel@tonic-gate 	OPENSSL_cleanse(a,sizeof(BIGNUM));
272*0Sstevel@tonic-gate 	if (i)
273*0Sstevel@tonic-gate 		OPENSSL_free(a);
274*0Sstevel@tonic-gate 	}
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate void BN_free(BIGNUM *a)
277*0Sstevel@tonic-gate 	{
278*0Sstevel@tonic-gate 	if (a == NULL) return;
279*0Sstevel@tonic-gate 	if ((a->d != NULL) && !(BN_get_flags(a,BN_FLG_STATIC_DATA)))
280*0Sstevel@tonic-gate 		OPENSSL_free(a->d);
281*0Sstevel@tonic-gate 	a->flags|=BN_FLG_FREE; /* REMOVE? */
282*0Sstevel@tonic-gate 	if (a->flags & BN_FLG_MALLOCED)
283*0Sstevel@tonic-gate 		OPENSSL_free(a);
284*0Sstevel@tonic-gate 	}
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate void BN_init(BIGNUM *a)
287*0Sstevel@tonic-gate 	{
288*0Sstevel@tonic-gate 	memset(a,0,sizeof(BIGNUM));
289*0Sstevel@tonic-gate 	}
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate BIGNUM *BN_new(void)
292*0Sstevel@tonic-gate 	{
293*0Sstevel@tonic-gate 	BIGNUM *ret;
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 	if ((ret=(BIGNUM *)OPENSSL_malloc(sizeof(BIGNUM))) == NULL)
296*0Sstevel@tonic-gate 		{
297*0Sstevel@tonic-gate 		BNerr(BN_F_BN_NEW,ERR_R_MALLOC_FAILURE);
298*0Sstevel@tonic-gate 		return(NULL);
299*0Sstevel@tonic-gate 		}
300*0Sstevel@tonic-gate 	ret->flags=BN_FLG_MALLOCED;
301*0Sstevel@tonic-gate 	ret->top=0;
302*0Sstevel@tonic-gate 	ret->neg=0;
303*0Sstevel@tonic-gate 	ret->dmax=0;
304*0Sstevel@tonic-gate 	ret->d=NULL;
305*0Sstevel@tonic-gate 	return(ret);
306*0Sstevel@tonic-gate 	}
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate /* This is used both by bn_expand2() and bn_dup_expand() */
309*0Sstevel@tonic-gate /* The caller MUST check that words > b->dmax before calling this */
310*0Sstevel@tonic-gate static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
311*0Sstevel@tonic-gate 	{
312*0Sstevel@tonic-gate 	BN_ULONG *A,*a = NULL;
313*0Sstevel@tonic-gate 	const BN_ULONG *B;
314*0Sstevel@tonic-gate 	int i;
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	if (words > (INT_MAX/(4*BN_BITS2)))
317*0Sstevel@tonic-gate 		{
318*0Sstevel@tonic-gate 		BNerr(BN_F_BN_EXPAND_INTERNAL,BN_R_BIGNUM_TOO_LONG);
319*0Sstevel@tonic-gate 		return NULL;
320*0Sstevel@tonic-gate 		}
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 	bn_check_top(b);
323*0Sstevel@tonic-gate 	if (BN_get_flags(b,BN_FLG_STATIC_DATA))
324*0Sstevel@tonic-gate 		{
325*0Sstevel@tonic-gate 		BNerr(BN_F_BN_EXPAND_INTERNAL,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
326*0Sstevel@tonic-gate 		return(NULL);
327*0Sstevel@tonic-gate 		}
328*0Sstevel@tonic-gate 	a=A=(BN_ULONG *)OPENSSL_malloc(sizeof(BN_ULONG)*(words+1));
329*0Sstevel@tonic-gate 	if (A == NULL)
330*0Sstevel@tonic-gate 		{
331*0Sstevel@tonic-gate 		BNerr(BN_F_BN_EXPAND_INTERNAL,ERR_R_MALLOC_FAILURE);
332*0Sstevel@tonic-gate 		return(NULL);
333*0Sstevel@tonic-gate 		}
334*0Sstevel@tonic-gate #if 1
335*0Sstevel@tonic-gate 	B=b->d;
336*0Sstevel@tonic-gate 	/* Check if the previous number needs to be copied */
337*0Sstevel@tonic-gate 	if (B != NULL)
338*0Sstevel@tonic-gate 		{
339*0Sstevel@tonic-gate 		for (i=b->top>>2; i>0; i--,A+=4,B+=4)
340*0Sstevel@tonic-gate 			{
341*0Sstevel@tonic-gate 			/*
342*0Sstevel@tonic-gate 			 * The fact that the loop is unrolled
343*0Sstevel@tonic-gate 			 * 4-wise is a tribute to Intel. It's
344*0Sstevel@tonic-gate 			 * the one that doesn't have enough
345*0Sstevel@tonic-gate 			 * registers to accomodate more data.
346*0Sstevel@tonic-gate 			 * I'd unroll it 8-wise otherwise:-)
347*0Sstevel@tonic-gate 			 *
348*0Sstevel@tonic-gate 			 *		<appro@fy.chalmers.se>
349*0Sstevel@tonic-gate 			 */
350*0Sstevel@tonic-gate 			BN_ULONG a0,a1,a2,a3;
351*0Sstevel@tonic-gate 			a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];
352*0Sstevel@tonic-gate 			A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;
353*0Sstevel@tonic-gate 			}
354*0Sstevel@tonic-gate 		switch (b->top&3)
355*0Sstevel@tonic-gate 			{
356*0Sstevel@tonic-gate 		case 3:	A[2]=B[2];
357*0Sstevel@tonic-gate 		case 2:	A[1]=B[1];
358*0Sstevel@tonic-gate 		case 1:	A[0]=B[0];
359*0Sstevel@tonic-gate 		case 0: /* workaround for ultrix cc: without 'case 0', the optimizer does
360*0Sstevel@tonic-gate 		         * the switch table by doing a=top&3; a--; goto jump_table[a];
361*0Sstevel@tonic-gate 		         * which fails for top== 0 */
362*0Sstevel@tonic-gate 			;
363*0Sstevel@tonic-gate 			}
364*0Sstevel@tonic-gate 		}
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate 	/* Now need to zero any data between b->top and b->max */
367*0Sstevel@tonic-gate 	/* XXX Why? */
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 	A= &(a[b->top]);
370*0Sstevel@tonic-gate 	for (i=(words - b->top)>>3; i>0; i--,A+=8)
371*0Sstevel@tonic-gate 		{
372*0Sstevel@tonic-gate 		A[0]=0; A[1]=0; A[2]=0; A[3]=0;
373*0Sstevel@tonic-gate 		A[4]=0; A[5]=0; A[6]=0; A[7]=0;
374*0Sstevel@tonic-gate 		}
375*0Sstevel@tonic-gate 	for (i=(words - b->top)&7; i>0; i--,A++)
376*0Sstevel@tonic-gate 		A[0]=0;
377*0Sstevel@tonic-gate #else
378*0Sstevel@tonic-gate 	memset(A,0,sizeof(BN_ULONG)*(words+1));
379*0Sstevel@tonic-gate 	memcpy(A,b->d,sizeof(b->d[0])*b->top);
380*0Sstevel@tonic-gate #endif
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 	return(a);
383*0Sstevel@tonic-gate 	}
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate /* This is an internal function that can be used instead of bn_expand2()
386*0Sstevel@tonic-gate  * when there is a need to copy BIGNUMs instead of only expanding the
387*0Sstevel@tonic-gate  * data part, while still expanding them.
388*0Sstevel@tonic-gate  * Especially useful when needing to expand BIGNUMs that are declared
389*0Sstevel@tonic-gate  * 'const' and should therefore not be changed.
390*0Sstevel@tonic-gate  * The reason to use this instead of a BN_dup() followed by a bn_expand2()
391*0Sstevel@tonic-gate  * is memory allocation overhead.  A BN_dup() followed by a bn_expand2()
392*0Sstevel@tonic-gate  * will allocate new memory for the BIGNUM data twice, and free it once,
393*0Sstevel@tonic-gate  * while bn_dup_expand() makes sure allocation is made only once.
394*0Sstevel@tonic-gate  */
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate BIGNUM *bn_dup_expand(const BIGNUM *b, int words)
397*0Sstevel@tonic-gate 	{
398*0Sstevel@tonic-gate 	BIGNUM *r = NULL;
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	/* This function does not work if
401*0Sstevel@tonic-gate 	 *      words <= b->dmax && top < words
402*0Sstevel@tonic-gate 	 * because BN_dup() does not preserve 'dmax'!
403*0Sstevel@tonic-gate 	 * (But bn_dup_expand() is not used anywhere yet.)
404*0Sstevel@tonic-gate 	 */
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate 	if (words > b->dmax)
407*0Sstevel@tonic-gate 		{
408*0Sstevel@tonic-gate 		BN_ULONG *a = bn_expand_internal(b, words);
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate 		if (a)
411*0Sstevel@tonic-gate 			{
412*0Sstevel@tonic-gate 			r = BN_new();
413*0Sstevel@tonic-gate 			if (r)
414*0Sstevel@tonic-gate 				{
415*0Sstevel@tonic-gate 				r->top = b->top;
416*0Sstevel@tonic-gate 				r->dmax = words;
417*0Sstevel@tonic-gate 				r->neg = b->neg;
418*0Sstevel@tonic-gate 				r->d = a;
419*0Sstevel@tonic-gate 				}
420*0Sstevel@tonic-gate 			else
421*0Sstevel@tonic-gate 				{
422*0Sstevel@tonic-gate 				/* r == NULL, BN_new failure */
423*0Sstevel@tonic-gate 				OPENSSL_free(a);
424*0Sstevel@tonic-gate 				}
425*0Sstevel@tonic-gate 			}
426*0Sstevel@tonic-gate 		/* If a == NULL, there was an error in allocation in
427*0Sstevel@tonic-gate 		   bn_expand_internal(), and NULL should be returned */
428*0Sstevel@tonic-gate 		}
429*0Sstevel@tonic-gate 	else
430*0Sstevel@tonic-gate 		{
431*0Sstevel@tonic-gate 		r = BN_dup(b);
432*0Sstevel@tonic-gate 		}
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate 	return r;
435*0Sstevel@tonic-gate 	}
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate /* This is an internal function that should not be used in applications.
438*0Sstevel@tonic-gate  * It ensures that 'b' has enough room for a 'words' word number number.
439*0Sstevel@tonic-gate  * It is mostly used by the various BIGNUM routines. If there is an error,
440*0Sstevel@tonic-gate  * NULL is returned. If not, 'b' is returned. */
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate BIGNUM *bn_expand2(BIGNUM *b, int words)
443*0Sstevel@tonic-gate 	{
444*0Sstevel@tonic-gate 	if (words > b->dmax)
445*0Sstevel@tonic-gate 		{
446*0Sstevel@tonic-gate 		BN_ULONG *a = bn_expand_internal(b, words);
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 		if (a)
449*0Sstevel@tonic-gate 			{
450*0Sstevel@tonic-gate 			if (b->d)
451*0Sstevel@tonic-gate 				OPENSSL_free(b->d);
452*0Sstevel@tonic-gate 			b->d=a;
453*0Sstevel@tonic-gate 			b->dmax=words;
454*0Sstevel@tonic-gate 			}
455*0Sstevel@tonic-gate 		else
456*0Sstevel@tonic-gate 			b = NULL;
457*0Sstevel@tonic-gate 		}
458*0Sstevel@tonic-gate 	return b;
459*0Sstevel@tonic-gate 	}
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate BIGNUM *BN_dup(const BIGNUM *a)
462*0Sstevel@tonic-gate 	{
463*0Sstevel@tonic-gate 	BIGNUM *r, *t;
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 	if (a == NULL) return NULL;
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate 	bn_check_top(a);
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate 	t = BN_new();
470*0Sstevel@tonic-gate 	if (t == NULL) return(NULL);
471*0Sstevel@tonic-gate 	r = BN_copy(t, a);
472*0Sstevel@tonic-gate 	/* now  r == t || r == NULL */
473*0Sstevel@tonic-gate 	if (r == NULL)
474*0Sstevel@tonic-gate 		BN_free(t);
475*0Sstevel@tonic-gate 	return r;
476*0Sstevel@tonic-gate 	}
477*0Sstevel@tonic-gate 
478*0Sstevel@tonic-gate BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
479*0Sstevel@tonic-gate 	{
480*0Sstevel@tonic-gate 	int i;
481*0Sstevel@tonic-gate 	BN_ULONG *A;
482*0Sstevel@tonic-gate 	const BN_ULONG *B;
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate 	bn_check_top(b);
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate 	if (a == b) return(a);
487*0Sstevel@tonic-gate 	if (bn_wexpand(a,b->top) == NULL) return(NULL);
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate #if 1
490*0Sstevel@tonic-gate 	A=a->d;
491*0Sstevel@tonic-gate 	B=b->d;
492*0Sstevel@tonic-gate 	for (i=b->top>>2; i>0; i--,A+=4,B+=4)
493*0Sstevel@tonic-gate 		{
494*0Sstevel@tonic-gate 		BN_ULONG a0,a1,a2,a3;
495*0Sstevel@tonic-gate 		a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];
496*0Sstevel@tonic-gate 		A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;
497*0Sstevel@tonic-gate 		}
498*0Sstevel@tonic-gate 	switch (b->top&3)
499*0Sstevel@tonic-gate 		{
500*0Sstevel@tonic-gate 		case 3: A[2]=B[2];
501*0Sstevel@tonic-gate 		case 2: A[1]=B[1];
502*0Sstevel@tonic-gate 		case 1: A[0]=B[0];
503*0Sstevel@tonic-gate 		case 0: ; /* ultrix cc workaround, see comments in bn_expand_internal */
504*0Sstevel@tonic-gate 		}
505*0Sstevel@tonic-gate #else
506*0Sstevel@tonic-gate 	memcpy(a->d,b->d,sizeof(b->d[0])*b->top);
507*0Sstevel@tonic-gate #endif
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate /*	memset(&(a->d[b->top]),0,sizeof(a->d[0])*(a->max-b->top));*/
510*0Sstevel@tonic-gate 	a->top=b->top;
511*0Sstevel@tonic-gate 	if ((a->top == 0) && (a->d != NULL))
512*0Sstevel@tonic-gate 		a->d[0]=0;
513*0Sstevel@tonic-gate 	a->neg=b->neg;
514*0Sstevel@tonic-gate 	return(a);
515*0Sstevel@tonic-gate 	}
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate void BN_swap(BIGNUM *a, BIGNUM *b)
518*0Sstevel@tonic-gate 	{
519*0Sstevel@tonic-gate 	int flags_old_a, flags_old_b;
520*0Sstevel@tonic-gate 	BN_ULONG *tmp_d;
521*0Sstevel@tonic-gate 	int tmp_top, tmp_dmax, tmp_neg;
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate 	flags_old_a = a->flags;
524*0Sstevel@tonic-gate 	flags_old_b = b->flags;
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate 	tmp_d = a->d;
527*0Sstevel@tonic-gate 	tmp_top = a->top;
528*0Sstevel@tonic-gate 	tmp_dmax = a->dmax;
529*0Sstevel@tonic-gate 	tmp_neg = a->neg;
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 	a->d = b->d;
532*0Sstevel@tonic-gate 	a->top = b->top;
533*0Sstevel@tonic-gate 	a->dmax = b->dmax;
534*0Sstevel@tonic-gate 	a->neg = b->neg;
535*0Sstevel@tonic-gate 
536*0Sstevel@tonic-gate 	b->d = tmp_d;
537*0Sstevel@tonic-gate 	b->top = tmp_top;
538*0Sstevel@tonic-gate 	b->dmax = tmp_dmax;
539*0Sstevel@tonic-gate 	b->neg = tmp_neg;
540*0Sstevel@tonic-gate 
541*0Sstevel@tonic-gate 	a->flags = (flags_old_a & BN_FLG_MALLOCED) | (flags_old_b & BN_FLG_STATIC_DATA);
542*0Sstevel@tonic-gate 	b->flags = (flags_old_b & BN_FLG_MALLOCED) | (flags_old_a & BN_FLG_STATIC_DATA);
543*0Sstevel@tonic-gate 	}
544*0Sstevel@tonic-gate 
545*0Sstevel@tonic-gate 
546*0Sstevel@tonic-gate void BN_clear(BIGNUM *a)
547*0Sstevel@tonic-gate 	{
548*0Sstevel@tonic-gate 	if (a->d != NULL)
549*0Sstevel@tonic-gate 		memset(a->d,0,a->dmax*sizeof(a->d[0]));
550*0Sstevel@tonic-gate 	a->top=0;
551*0Sstevel@tonic-gate 	a->neg=0;
552*0Sstevel@tonic-gate 	}
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate BN_ULONG BN_get_word(const BIGNUM *a)
555*0Sstevel@tonic-gate 	{
556*0Sstevel@tonic-gate 	int i,n;
557*0Sstevel@tonic-gate 	BN_ULONG ret=0;
558*0Sstevel@tonic-gate 
559*0Sstevel@tonic-gate 	n=BN_num_bytes(a);
560*0Sstevel@tonic-gate 	if (n > sizeof(BN_ULONG))
561*0Sstevel@tonic-gate 		return(BN_MASK2);
562*0Sstevel@tonic-gate 	for (i=a->top-1; i>=0; i--)
563*0Sstevel@tonic-gate 		{
564*0Sstevel@tonic-gate #ifndef SIXTY_FOUR_BIT /* the data item > unsigned long */
565*0Sstevel@tonic-gate 		ret<<=BN_BITS4; /* stops the compiler complaining */
566*0Sstevel@tonic-gate 		ret<<=BN_BITS4;
567*0Sstevel@tonic-gate #else
568*0Sstevel@tonic-gate 		ret=0;
569*0Sstevel@tonic-gate #endif
570*0Sstevel@tonic-gate 		ret|=a->d[i];
571*0Sstevel@tonic-gate 		}
572*0Sstevel@tonic-gate 	return(ret);
573*0Sstevel@tonic-gate 	}
574*0Sstevel@tonic-gate 
575*0Sstevel@tonic-gate int BN_set_word(BIGNUM *a, BN_ULONG w)
576*0Sstevel@tonic-gate 	{
577*0Sstevel@tonic-gate 	int i,n;
578*0Sstevel@tonic-gate 	if (bn_expand(a,sizeof(BN_ULONG)*8) == NULL) return(0);
579*0Sstevel@tonic-gate 
580*0Sstevel@tonic-gate 	n=sizeof(BN_ULONG)/BN_BYTES;
581*0Sstevel@tonic-gate 	a->neg=0;
582*0Sstevel@tonic-gate 	a->top=0;
583*0Sstevel@tonic-gate 	a->d[0]=(BN_ULONG)w&BN_MASK2;
584*0Sstevel@tonic-gate 	if (a->d[0] != 0) a->top=1;
585*0Sstevel@tonic-gate 	for (i=1; i<n; i++)
586*0Sstevel@tonic-gate 		{
587*0Sstevel@tonic-gate 		/* the following is done instead of
588*0Sstevel@tonic-gate 		 * w>>=BN_BITS2 so compilers don't complain
589*0Sstevel@tonic-gate 		 * on builds where sizeof(long) == BN_TYPES */
590*0Sstevel@tonic-gate #ifndef SIXTY_FOUR_BIT /* the data item > unsigned long */
591*0Sstevel@tonic-gate 		w>>=BN_BITS4;
592*0Sstevel@tonic-gate 		w>>=BN_BITS4;
593*0Sstevel@tonic-gate #else
594*0Sstevel@tonic-gate 		w=0;
595*0Sstevel@tonic-gate #endif
596*0Sstevel@tonic-gate 		a->d[i]=(BN_ULONG)w&BN_MASK2;
597*0Sstevel@tonic-gate 		if (a->d[i] != 0) a->top=i+1;
598*0Sstevel@tonic-gate 		}
599*0Sstevel@tonic-gate 	return(1);
600*0Sstevel@tonic-gate 	}
601*0Sstevel@tonic-gate 
602*0Sstevel@tonic-gate BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
603*0Sstevel@tonic-gate 	{
604*0Sstevel@tonic-gate 	unsigned int i,m;
605*0Sstevel@tonic-gate 	unsigned int n;
606*0Sstevel@tonic-gate 	BN_ULONG l;
607*0Sstevel@tonic-gate 
608*0Sstevel@tonic-gate 	if (ret == NULL) ret=BN_new();
609*0Sstevel@tonic-gate 	if (ret == NULL) return(NULL);
610*0Sstevel@tonic-gate 	l=0;
611*0Sstevel@tonic-gate 	n=len;
612*0Sstevel@tonic-gate 	if (n == 0)
613*0Sstevel@tonic-gate 		{
614*0Sstevel@tonic-gate 		ret->top=0;
615*0Sstevel@tonic-gate 		return(ret);
616*0Sstevel@tonic-gate 		}
617*0Sstevel@tonic-gate 	if (bn_expand(ret,(int)(n+2)*8) == NULL)
618*0Sstevel@tonic-gate 		return(NULL);
619*0Sstevel@tonic-gate 	i=((n-1)/BN_BYTES)+1;
620*0Sstevel@tonic-gate 	m=((n-1)%(BN_BYTES));
621*0Sstevel@tonic-gate 	ret->top=i;
622*0Sstevel@tonic-gate 	ret->neg=0;
623*0Sstevel@tonic-gate 	while (n-- > 0)
624*0Sstevel@tonic-gate 		{
625*0Sstevel@tonic-gate 		l=(l<<8L)| *(s++);
626*0Sstevel@tonic-gate 		if (m-- == 0)
627*0Sstevel@tonic-gate 			{
628*0Sstevel@tonic-gate 			ret->d[--i]=l;
629*0Sstevel@tonic-gate 			l=0;
630*0Sstevel@tonic-gate 			m=BN_BYTES-1;
631*0Sstevel@tonic-gate 			}
632*0Sstevel@tonic-gate 		}
633*0Sstevel@tonic-gate 	/* need to call this due to clear byte at top if avoiding
634*0Sstevel@tonic-gate 	 * having the top bit set (-ve number) */
635*0Sstevel@tonic-gate 	bn_fix_top(ret);
636*0Sstevel@tonic-gate 	return(ret);
637*0Sstevel@tonic-gate 	}
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate /* ignore negative */
640*0Sstevel@tonic-gate int BN_bn2bin(const BIGNUM *a, unsigned char *to)
641*0Sstevel@tonic-gate 	{
642*0Sstevel@tonic-gate 	int n,i;
643*0Sstevel@tonic-gate 	BN_ULONG l;
644*0Sstevel@tonic-gate 
645*0Sstevel@tonic-gate 	n=i=BN_num_bytes(a);
646*0Sstevel@tonic-gate 	while (i-- > 0)
647*0Sstevel@tonic-gate 		{
648*0Sstevel@tonic-gate 		l=a->d[i/BN_BYTES];
649*0Sstevel@tonic-gate 		*(to++)=(unsigned char)(l>>(8*(i%BN_BYTES)))&0xff;
650*0Sstevel@tonic-gate 		}
651*0Sstevel@tonic-gate 	return(n);
652*0Sstevel@tonic-gate 	}
653*0Sstevel@tonic-gate 
654*0Sstevel@tonic-gate int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
655*0Sstevel@tonic-gate 	{
656*0Sstevel@tonic-gate 	int i;
657*0Sstevel@tonic-gate 	BN_ULONG t1,t2,*ap,*bp;
658*0Sstevel@tonic-gate 
659*0Sstevel@tonic-gate 	bn_check_top(a);
660*0Sstevel@tonic-gate 	bn_check_top(b);
661*0Sstevel@tonic-gate 
662*0Sstevel@tonic-gate 	i=a->top-b->top;
663*0Sstevel@tonic-gate 	if (i != 0) return(i);
664*0Sstevel@tonic-gate 	ap=a->d;
665*0Sstevel@tonic-gate 	bp=b->d;
666*0Sstevel@tonic-gate 	for (i=a->top-1; i>=0; i--)
667*0Sstevel@tonic-gate 		{
668*0Sstevel@tonic-gate 		t1= ap[i];
669*0Sstevel@tonic-gate 		t2= bp[i];
670*0Sstevel@tonic-gate 		if (t1 != t2)
671*0Sstevel@tonic-gate 			return(t1 > t2?1:-1);
672*0Sstevel@tonic-gate 		}
673*0Sstevel@tonic-gate 	return(0);
674*0Sstevel@tonic-gate 	}
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate int BN_cmp(const BIGNUM *a, const BIGNUM *b)
677*0Sstevel@tonic-gate 	{
678*0Sstevel@tonic-gate 	int i;
679*0Sstevel@tonic-gate 	int gt,lt;
680*0Sstevel@tonic-gate 	BN_ULONG t1,t2;
681*0Sstevel@tonic-gate 
682*0Sstevel@tonic-gate 	if ((a == NULL) || (b == NULL))
683*0Sstevel@tonic-gate 		{
684*0Sstevel@tonic-gate 		if (a != NULL)
685*0Sstevel@tonic-gate 			return(-1);
686*0Sstevel@tonic-gate 		else if (b != NULL)
687*0Sstevel@tonic-gate 			return(1);
688*0Sstevel@tonic-gate 		else
689*0Sstevel@tonic-gate 			return(0);
690*0Sstevel@tonic-gate 		}
691*0Sstevel@tonic-gate 
692*0Sstevel@tonic-gate 	bn_check_top(a);
693*0Sstevel@tonic-gate 	bn_check_top(b);
694*0Sstevel@tonic-gate 
695*0Sstevel@tonic-gate 	if (a->neg != b->neg)
696*0Sstevel@tonic-gate 		{
697*0Sstevel@tonic-gate 		if (a->neg)
698*0Sstevel@tonic-gate 			return(-1);
699*0Sstevel@tonic-gate 		else	return(1);
700*0Sstevel@tonic-gate 		}
701*0Sstevel@tonic-gate 	if (a->neg == 0)
702*0Sstevel@tonic-gate 		{ gt=1; lt= -1; }
703*0Sstevel@tonic-gate 	else	{ gt= -1; lt=1; }
704*0Sstevel@tonic-gate 
705*0Sstevel@tonic-gate 	if (a->top > b->top) return(gt);
706*0Sstevel@tonic-gate 	if (a->top < b->top) return(lt);
707*0Sstevel@tonic-gate 	for (i=a->top-1; i>=0; i--)
708*0Sstevel@tonic-gate 		{
709*0Sstevel@tonic-gate 		t1=a->d[i];
710*0Sstevel@tonic-gate 		t2=b->d[i];
711*0Sstevel@tonic-gate 		if (t1 > t2) return(gt);
712*0Sstevel@tonic-gate 		if (t1 < t2) return(lt);
713*0Sstevel@tonic-gate 		}
714*0Sstevel@tonic-gate 	return(0);
715*0Sstevel@tonic-gate 	}
716*0Sstevel@tonic-gate 
717*0Sstevel@tonic-gate int BN_set_bit(BIGNUM *a, int n)
718*0Sstevel@tonic-gate 	{
719*0Sstevel@tonic-gate 	int i,j,k;
720*0Sstevel@tonic-gate 
721*0Sstevel@tonic-gate 	i=n/BN_BITS2;
722*0Sstevel@tonic-gate 	j=n%BN_BITS2;
723*0Sstevel@tonic-gate 	if (a->top <= i)
724*0Sstevel@tonic-gate 		{
725*0Sstevel@tonic-gate 		if (bn_wexpand(a,i+1) == NULL) return(0);
726*0Sstevel@tonic-gate 		for(k=a->top; k<i+1; k++)
727*0Sstevel@tonic-gate 			a->d[k]=0;
728*0Sstevel@tonic-gate 		a->top=i+1;
729*0Sstevel@tonic-gate 		}
730*0Sstevel@tonic-gate 
731*0Sstevel@tonic-gate 	a->d[i]|=(((BN_ULONG)1)<<j);
732*0Sstevel@tonic-gate 	return(1);
733*0Sstevel@tonic-gate 	}
734*0Sstevel@tonic-gate 
735*0Sstevel@tonic-gate int BN_clear_bit(BIGNUM *a, int n)
736*0Sstevel@tonic-gate 	{
737*0Sstevel@tonic-gate 	int i,j;
738*0Sstevel@tonic-gate 
739*0Sstevel@tonic-gate 	i=n/BN_BITS2;
740*0Sstevel@tonic-gate 	j=n%BN_BITS2;
741*0Sstevel@tonic-gate 	if (a->top <= i) return(0);
742*0Sstevel@tonic-gate 
743*0Sstevel@tonic-gate 	a->d[i]&=(~(((BN_ULONG)1)<<j));
744*0Sstevel@tonic-gate 	bn_fix_top(a);
745*0Sstevel@tonic-gate 	return(1);
746*0Sstevel@tonic-gate 	}
747*0Sstevel@tonic-gate 
748*0Sstevel@tonic-gate int BN_is_bit_set(const BIGNUM *a, int n)
749*0Sstevel@tonic-gate 	{
750*0Sstevel@tonic-gate 	int i,j;
751*0Sstevel@tonic-gate 
752*0Sstevel@tonic-gate 	if (n < 0) return(0);
753*0Sstevel@tonic-gate 	i=n/BN_BITS2;
754*0Sstevel@tonic-gate 	j=n%BN_BITS2;
755*0Sstevel@tonic-gate 	if (a->top <= i) return(0);
756*0Sstevel@tonic-gate 	return((a->d[i]&(((BN_ULONG)1)<<j))?1:0);
757*0Sstevel@tonic-gate 	}
758*0Sstevel@tonic-gate 
759*0Sstevel@tonic-gate int BN_mask_bits(BIGNUM *a, int n)
760*0Sstevel@tonic-gate 	{
761*0Sstevel@tonic-gate 	int b,w;
762*0Sstevel@tonic-gate 
763*0Sstevel@tonic-gate 	w=n/BN_BITS2;
764*0Sstevel@tonic-gate 	b=n%BN_BITS2;
765*0Sstevel@tonic-gate 	if (w >= a->top) return(0);
766*0Sstevel@tonic-gate 	if (b == 0)
767*0Sstevel@tonic-gate 		a->top=w;
768*0Sstevel@tonic-gate 	else
769*0Sstevel@tonic-gate 		{
770*0Sstevel@tonic-gate 		a->top=w+1;
771*0Sstevel@tonic-gate 		a->d[w]&= ~(BN_MASK2<<b);
772*0Sstevel@tonic-gate 		}
773*0Sstevel@tonic-gate 	bn_fix_top(a);
774*0Sstevel@tonic-gate 	return(1);
775*0Sstevel@tonic-gate 	}
776*0Sstevel@tonic-gate 
777*0Sstevel@tonic-gate int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
778*0Sstevel@tonic-gate 	{
779*0Sstevel@tonic-gate 	int i;
780*0Sstevel@tonic-gate 	BN_ULONG aa,bb;
781*0Sstevel@tonic-gate 
782*0Sstevel@tonic-gate 	aa=a[n-1];
783*0Sstevel@tonic-gate 	bb=b[n-1];
784*0Sstevel@tonic-gate 	if (aa != bb) return((aa > bb)?1:-1);
785*0Sstevel@tonic-gate 	for (i=n-2; i>=0; i--)
786*0Sstevel@tonic-gate 		{
787*0Sstevel@tonic-gate 		aa=a[i];
788*0Sstevel@tonic-gate 		bb=b[i];
789*0Sstevel@tonic-gate 		if (aa != bb) return((aa > bb)?1:-1);
790*0Sstevel@tonic-gate 		}
791*0Sstevel@tonic-gate 	return(0);
792*0Sstevel@tonic-gate 	}
793*0Sstevel@tonic-gate 
794*0Sstevel@tonic-gate /* Here follows a specialised variants of bn_cmp_words().  It has the
795*0Sstevel@tonic-gate    property of performing the operation on arrays of different sizes.
796*0Sstevel@tonic-gate    The sizes of those arrays is expressed through cl, which is the
797*0Sstevel@tonic-gate    common length ( basicall, min(len(a),len(b)) ), and dl, which is the
798*0Sstevel@tonic-gate    delta between the two lengths, calculated as len(a)-len(b).
799*0Sstevel@tonic-gate    All lengths are the number of BN_ULONGs...  */
800*0Sstevel@tonic-gate 
801*0Sstevel@tonic-gate int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b,
802*0Sstevel@tonic-gate 	int cl, int dl)
803*0Sstevel@tonic-gate 	{
804*0Sstevel@tonic-gate 	int n,i;
805*0Sstevel@tonic-gate 	n = cl-1;
806*0Sstevel@tonic-gate 
807*0Sstevel@tonic-gate 	if (dl < 0)
808*0Sstevel@tonic-gate 		{
809*0Sstevel@tonic-gate 		for (i=dl; i<0; i++)
810*0Sstevel@tonic-gate 			{
811*0Sstevel@tonic-gate 			if (b[n-i] != 0)
812*0Sstevel@tonic-gate 				return -1; /* a < b */
813*0Sstevel@tonic-gate 			}
814*0Sstevel@tonic-gate 		}
815*0Sstevel@tonic-gate 	if (dl > 0)
816*0Sstevel@tonic-gate 		{
817*0Sstevel@tonic-gate 		for (i=dl; i>0; i--)
818*0Sstevel@tonic-gate 			{
819*0Sstevel@tonic-gate 			if (a[n+i] != 0)
820*0Sstevel@tonic-gate 				return 1; /* a > b */
821*0Sstevel@tonic-gate 			}
822*0Sstevel@tonic-gate 		}
823*0Sstevel@tonic-gate 	return bn_cmp_words(a,b,cl);
824*0Sstevel@tonic-gate 	}
825