xref: /onnv-gate/usr/src/common/openssl/crypto/bn/bn_lib.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/bn/bn_lib.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 #ifndef BN_DEBUG
600Sstevel@tonic-gate # undef NDEBUG /* avoid conflicting definitions */
610Sstevel@tonic-gate # define NDEBUG
620Sstevel@tonic-gate #endif
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #include <assert.h>
650Sstevel@tonic-gate #include <limits.h>
660Sstevel@tonic-gate #include <stdio.h>
670Sstevel@tonic-gate #include "cryptlib.h"
680Sstevel@tonic-gate #include "bn_lcl.h"
690Sstevel@tonic-gate 
700Sstevel@tonic-gate const char *BN_version="Big Number" OPENSSL_VERSION_PTEXT;
710Sstevel@tonic-gate 
72*2139Sjp161948 /* This stuff appears to be completely unused, so is deprecated */
73*2139Sjp161948 #ifndef OPENSSL_NO_DEPRECATED
740Sstevel@tonic-gate /* For a 32 bit machine
750Sstevel@tonic-gate  * 2 -   4 ==  128
760Sstevel@tonic-gate  * 3 -   8 ==  256
770Sstevel@tonic-gate  * 4 -  16 ==  512
780Sstevel@tonic-gate  * 5 -  32 == 1024
790Sstevel@tonic-gate  * 6 -  64 == 2048
800Sstevel@tonic-gate  * 7 - 128 == 4096
810Sstevel@tonic-gate  * 8 - 256 == 8192
820Sstevel@tonic-gate  */
830Sstevel@tonic-gate static int bn_limit_bits=0;
840Sstevel@tonic-gate static int bn_limit_num=8;        /* (1<<bn_limit_bits) */
850Sstevel@tonic-gate static int bn_limit_bits_low=0;
860Sstevel@tonic-gate static int bn_limit_num_low=8;    /* (1<<bn_limit_bits_low) */
870Sstevel@tonic-gate static int bn_limit_bits_high=0;
880Sstevel@tonic-gate static int bn_limit_num_high=8;   /* (1<<bn_limit_bits_high) */
890Sstevel@tonic-gate static int bn_limit_bits_mont=0;
900Sstevel@tonic-gate static int bn_limit_num_mont=8;   /* (1<<bn_limit_bits_mont) */
910Sstevel@tonic-gate 
BN_set_params(int mult,int high,int low,int mont)920Sstevel@tonic-gate void BN_set_params(int mult, int high, int low, int mont)
930Sstevel@tonic-gate 	{
940Sstevel@tonic-gate 	if (mult >= 0)
950Sstevel@tonic-gate 		{
96*2139Sjp161948 		if (mult > (int)(sizeof(int)*8)-1)
970Sstevel@tonic-gate 			mult=sizeof(int)*8-1;
980Sstevel@tonic-gate 		bn_limit_bits=mult;
990Sstevel@tonic-gate 		bn_limit_num=1<<mult;
1000Sstevel@tonic-gate 		}
1010Sstevel@tonic-gate 	if (high >= 0)
1020Sstevel@tonic-gate 		{
103*2139Sjp161948 		if (high > (int)(sizeof(int)*8)-1)
1040Sstevel@tonic-gate 			high=sizeof(int)*8-1;
1050Sstevel@tonic-gate 		bn_limit_bits_high=high;
1060Sstevel@tonic-gate 		bn_limit_num_high=1<<high;
1070Sstevel@tonic-gate 		}
1080Sstevel@tonic-gate 	if (low >= 0)
1090Sstevel@tonic-gate 		{
110*2139Sjp161948 		if (low > (int)(sizeof(int)*8)-1)
1110Sstevel@tonic-gate 			low=sizeof(int)*8-1;
1120Sstevel@tonic-gate 		bn_limit_bits_low=low;
1130Sstevel@tonic-gate 		bn_limit_num_low=1<<low;
1140Sstevel@tonic-gate 		}
1150Sstevel@tonic-gate 	if (mont >= 0)
1160Sstevel@tonic-gate 		{
117*2139Sjp161948 		if (mont > (int)(sizeof(int)*8)-1)
1180Sstevel@tonic-gate 			mont=sizeof(int)*8-1;
1190Sstevel@tonic-gate 		bn_limit_bits_mont=mont;
1200Sstevel@tonic-gate 		bn_limit_num_mont=1<<mont;
1210Sstevel@tonic-gate 		}
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 
BN_get_params(int which)1240Sstevel@tonic-gate int BN_get_params(int which)
1250Sstevel@tonic-gate 	{
1260Sstevel@tonic-gate 	if      (which == 0) return(bn_limit_bits);
1270Sstevel@tonic-gate 	else if (which == 1) return(bn_limit_bits_high);
1280Sstevel@tonic-gate 	else if (which == 2) return(bn_limit_bits_low);
1290Sstevel@tonic-gate 	else if (which == 3) return(bn_limit_bits_mont);
1300Sstevel@tonic-gate 	else return(0);
1310Sstevel@tonic-gate 	}
132*2139Sjp161948 #endif
1330Sstevel@tonic-gate 
BN_value_one(void)1340Sstevel@tonic-gate const BIGNUM *BN_value_one(void)
1350Sstevel@tonic-gate 	{
1360Sstevel@tonic-gate 	static BN_ULONG data_one=1L;
137*2139Sjp161948 	static BIGNUM const_one={&data_one,1,1,0,BN_FLG_STATIC_DATA};
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	return(&const_one);
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 
BN_options(void)1420Sstevel@tonic-gate char *BN_options(void)
1430Sstevel@tonic-gate 	{
1440Sstevel@tonic-gate 	static int init=0;
1450Sstevel@tonic-gate 	static char data[16];
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if (!init)
1480Sstevel@tonic-gate 		{
1490Sstevel@tonic-gate 		init++;
1500Sstevel@tonic-gate #ifdef BN_LLONG
1510Sstevel@tonic-gate 		BIO_snprintf(data,sizeof data,"bn(%d,%d)",
1520Sstevel@tonic-gate 			     (int)sizeof(BN_ULLONG)*8,(int)sizeof(BN_ULONG)*8);
1530Sstevel@tonic-gate #else
1540Sstevel@tonic-gate 		BIO_snprintf(data,sizeof data,"bn(%d,%d)",
1550Sstevel@tonic-gate 			     (int)sizeof(BN_ULONG)*8,(int)sizeof(BN_ULONG)*8);
1560Sstevel@tonic-gate #endif
1570Sstevel@tonic-gate 		}
1580Sstevel@tonic-gate 	return(data);
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 
BN_num_bits_word(BN_ULONG l)1610Sstevel@tonic-gate int BN_num_bits_word(BN_ULONG l)
1620Sstevel@tonic-gate 	{
1630Sstevel@tonic-gate 	static const char bits[256]={
1640Sstevel@tonic-gate 		0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,
1650Sstevel@tonic-gate 		5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
1660Sstevel@tonic-gate 		6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
1670Sstevel@tonic-gate 		6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
1680Sstevel@tonic-gate 		7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
1690Sstevel@tonic-gate 		7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
1700Sstevel@tonic-gate 		7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
1710Sstevel@tonic-gate 		7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
1720Sstevel@tonic-gate 		8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
1730Sstevel@tonic-gate 		8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
1740Sstevel@tonic-gate 		8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
1750Sstevel@tonic-gate 		8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
1760Sstevel@tonic-gate 		8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
1770Sstevel@tonic-gate 		8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
1780Sstevel@tonic-gate 		8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
1790Sstevel@tonic-gate 		8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
1800Sstevel@tonic-gate 		};
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate #if defined(SIXTY_FOUR_BIT_LONG)
1830Sstevel@tonic-gate 	if (l & 0xffffffff00000000L)
1840Sstevel@tonic-gate 		{
1850Sstevel@tonic-gate 		if (l & 0xffff000000000000L)
1860Sstevel@tonic-gate 			{
1870Sstevel@tonic-gate 			if (l & 0xff00000000000000L)
1880Sstevel@tonic-gate 				{
1890Sstevel@tonic-gate 				return(bits[(int)(l>>56)]+56);
1900Sstevel@tonic-gate 				}
1910Sstevel@tonic-gate 			else	return(bits[(int)(l>>48)]+48);
1920Sstevel@tonic-gate 			}
1930Sstevel@tonic-gate 		else
1940Sstevel@tonic-gate 			{
1950Sstevel@tonic-gate 			if (l & 0x0000ff0000000000L)
1960Sstevel@tonic-gate 				{
1970Sstevel@tonic-gate 				return(bits[(int)(l>>40)]+40);
1980Sstevel@tonic-gate 				}
1990Sstevel@tonic-gate 			else	return(bits[(int)(l>>32)]+32);
2000Sstevel@tonic-gate 			}
2010Sstevel@tonic-gate 		}
2020Sstevel@tonic-gate 	else
2030Sstevel@tonic-gate #else
2040Sstevel@tonic-gate #ifdef SIXTY_FOUR_BIT
2050Sstevel@tonic-gate 	if (l & 0xffffffff00000000LL)
2060Sstevel@tonic-gate 		{
2070Sstevel@tonic-gate 		if (l & 0xffff000000000000LL)
2080Sstevel@tonic-gate 			{
2090Sstevel@tonic-gate 			if (l & 0xff00000000000000LL)
2100Sstevel@tonic-gate 				{
2110Sstevel@tonic-gate 				return(bits[(int)(l>>56)]+56);
2120Sstevel@tonic-gate 				}
2130Sstevel@tonic-gate 			else	return(bits[(int)(l>>48)]+48);
2140Sstevel@tonic-gate 			}
2150Sstevel@tonic-gate 		else
2160Sstevel@tonic-gate 			{
2170Sstevel@tonic-gate 			if (l & 0x0000ff0000000000LL)
2180Sstevel@tonic-gate 				{
2190Sstevel@tonic-gate 				return(bits[(int)(l>>40)]+40);
2200Sstevel@tonic-gate 				}
2210Sstevel@tonic-gate 			else	return(bits[(int)(l>>32)]+32);
2220Sstevel@tonic-gate 			}
2230Sstevel@tonic-gate 		}
2240Sstevel@tonic-gate 	else
2250Sstevel@tonic-gate #endif
2260Sstevel@tonic-gate #endif
2270Sstevel@tonic-gate 		{
2280Sstevel@tonic-gate #if defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
2290Sstevel@tonic-gate 		if (l & 0xffff0000L)
2300Sstevel@tonic-gate 			{
2310Sstevel@tonic-gate 			if (l & 0xff000000L)
2320Sstevel@tonic-gate 				return(bits[(int)(l>>24L)]+24);
2330Sstevel@tonic-gate 			else	return(bits[(int)(l>>16L)]+16);
2340Sstevel@tonic-gate 			}
2350Sstevel@tonic-gate 		else
2360Sstevel@tonic-gate #endif
2370Sstevel@tonic-gate 			{
2380Sstevel@tonic-gate #if defined(SIXTEEN_BIT) || defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
2390Sstevel@tonic-gate 			if (l & 0xff00L)
2400Sstevel@tonic-gate 				return(bits[(int)(l>>8)]+8);
2410Sstevel@tonic-gate 			else
2420Sstevel@tonic-gate #endif
2430Sstevel@tonic-gate 				return(bits[(int)(l   )]  );
2440Sstevel@tonic-gate 			}
2450Sstevel@tonic-gate 		}
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
BN_num_bits(const BIGNUM * a)2480Sstevel@tonic-gate int BN_num_bits(const BIGNUM *a)
2490Sstevel@tonic-gate 	{
250*2139Sjp161948 	int i = a->top - 1;
2510Sstevel@tonic-gate 	bn_check_top(a);
2520Sstevel@tonic-gate 
253*2139Sjp161948 	if (BN_is_zero(a)) return 0;
254*2139Sjp161948 	return ((i*BN_BITS2) + BN_num_bits_word(a->d[i]));
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 
BN_clear_free(BIGNUM * a)2570Sstevel@tonic-gate void BN_clear_free(BIGNUM *a)
2580Sstevel@tonic-gate 	{
2590Sstevel@tonic-gate 	int i;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	if (a == NULL) return;
262*2139Sjp161948 	bn_check_top(a);
2630Sstevel@tonic-gate 	if (a->d != NULL)
2640Sstevel@tonic-gate 		{
2650Sstevel@tonic-gate 		OPENSSL_cleanse(a->d,a->dmax*sizeof(a->d[0]));
2660Sstevel@tonic-gate 		if (!(BN_get_flags(a,BN_FLG_STATIC_DATA)))
2670Sstevel@tonic-gate 			OPENSSL_free(a->d);
2680Sstevel@tonic-gate 		}
2690Sstevel@tonic-gate 	i=BN_get_flags(a,BN_FLG_MALLOCED);
2700Sstevel@tonic-gate 	OPENSSL_cleanse(a,sizeof(BIGNUM));
2710Sstevel@tonic-gate 	if (i)
2720Sstevel@tonic-gate 		OPENSSL_free(a);
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 
BN_free(BIGNUM * a)2750Sstevel@tonic-gate void BN_free(BIGNUM *a)
2760Sstevel@tonic-gate 	{
2770Sstevel@tonic-gate 	if (a == NULL) return;
278*2139Sjp161948 	bn_check_top(a);
2790Sstevel@tonic-gate 	if ((a->d != NULL) && !(BN_get_flags(a,BN_FLG_STATIC_DATA)))
2800Sstevel@tonic-gate 		OPENSSL_free(a->d);
2810Sstevel@tonic-gate 	if (a->flags & BN_FLG_MALLOCED)
2820Sstevel@tonic-gate 		OPENSSL_free(a);
283*2139Sjp161948 	else
284*2139Sjp161948 		{
285*2139Sjp161948 #ifndef OPENSSL_NO_DEPRECATED
286*2139Sjp161948 		a->flags|=BN_FLG_FREE;
287*2139Sjp161948 #endif
288*2139Sjp161948 		a->d = NULL;
289*2139Sjp161948 		}
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 
BN_init(BIGNUM * a)2920Sstevel@tonic-gate void BN_init(BIGNUM *a)
2930Sstevel@tonic-gate 	{
2940Sstevel@tonic-gate 	memset(a,0,sizeof(BIGNUM));
295*2139Sjp161948 	bn_check_top(a);
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
BN_new(void)2980Sstevel@tonic-gate BIGNUM *BN_new(void)
2990Sstevel@tonic-gate 	{
3000Sstevel@tonic-gate 	BIGNUM *ret;
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	if ((ret=(BIGNUM *)OPENSSL_malloc(sizeof(BIGNUM))) == NULL)
3030Sstevel@tonic-gate 		{
3040Sstevel@tonic-gate 		BNerr(BN_F_BN_NEW,ERR_R_MALLOC_FAILURE);
3050Sstevel@tonic-gate 		return(NULL);
3060Sstevel@tonic-gate 		}
3070Sstevel@tonic-gate 	ret->flags=BN_FLG_MALLOCED;
3080Sstevel@tonic-gate 	ret->top=0;
3090Sstevel@tonic-gate 	ret->neg=0;
3100Sstevel@tonic-gate 	ret->dmax=0;
3110Sstevel@tonic-gate 	ret->d=NULL;
312*2139Sjp161948 	bn_check_top(ret);
3130Sstevel@tonic-gate 	return(ret);
3140Sstevel@tonic-gate 	}
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate /* This is used both by bn_expand2() and bn_dup_expand() */
3170Sstevel@tonic-gate /* The caller MUST check that words > b->dmax before calling this */
bn_expand_internal(const BIGNUM * b,int words)3180Sstevel@tonic-gate static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
3190Sstevel@tonic-gate 	{
3200Sstevel@tonic-gate 	BN_ULONG *A,*a = NULL;
3210Sstevel@tonic-gate 	const BN_ULONG *B;
3220Sstevel@tonic-gate 	int i;
3230Sstevel@tonic-gate 
324*2139Sjp161948 	bn_check_top(b);
325*2139Sjp161948 
3260Sstevel@tonic-gate 	if (words > (INT_MAX/(4*BN_BITS2)))
3270Sstevel@tonic-gate 		{
3280Sstevel@tonic-gate 		BNerr(BN_F_BN_EXPAND_INTERNAL,BN_R_BIGNUM_TOO_LONG);
3290Sstevel@tonic-gate 		return NULL;
3300Sstevel@tonic-gate 		}
3310Sstevel@tonic-gate 	if (BN_get_flags(b,BN_FLG_STATIC_DATA))
3320Sstevel@tonic-gate 		{
3330Sstevel@tonic-gate 		BNerr(BN_F_BN_EXPAND_INTERNAL,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
3340Sstevel@tonic-gate 		return(NULL);
3350Sstevel@tonic-gate 		}
336*2139Sjp161948 	a=A=(BN_ULONG *)OPENSSL_malloc(sizeof(BN_ULONG)*words);
3370Sstevel@tonic-gate 	if (A == NULL)
3380Sstevel@tonic-gate 		{
3390Sstevel@tonic-gate 		BNerr(BN_F_BN_EXPAND_INTERNAL,ERR_R_MALLOC_FAILURE);
3400Sstevel@tonic-gate 		return(NULL);
3410Sstevel@tonic-gate 		}
3420Sstevel@tonic-gate #if 1
3430Sstevel@tonic-gate 	B=b->d;
3440Sstevel@tonic-gate 	/* Check if the previous number needs to be copied */
3450Sstevel@tonic-gate 	if (B != NULL)
3460Sstevel@tonic-gate 		{
3470Sstevel@tonic-gate 		for (i=b->top>>2; i>0; i--,A+=4,B+=4)
3480Sstevel@tonic-gate 			{
3490Sstevel@tonic-gate 			/*
3500Sstevel@tonic-gate 			 * The fact that the loop is unrolled
3510Sstevel@tonic-gate 			 * 4-wise is a tribute to Intel. It's
3520Sstevel@tonic-gate 			 * the one that doesn't have enough
3530Sstevel@tonic-gate 			 * registers to accomodate more data.
3540Sstevel@tonic-gate 			 * I'd unroll it 8-wise otherwise:-)
3550Sstevel@tonic-gate 			 *
3560Sstevel@tonic-gate 			 *		<appro@fy.chalmers.se>
3570Sstevel@tonic-gate 			 */
3580Sstevel@tonic-gate 			BN_ULONG a0,a1,a2,a3;
3590Sstevel@tonic-gate 			a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];
3600Sstevel@tonic-gate 			A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;
3610Sstevel@tonic-gate 			}
3620Sstevel@tonic-gate 		switch (b->top&3)
3630Sstevel@tonic-gate 			{
3640Sstevel@tonic-gate 		case 3:	A[2]=B[2];
3650Sstevel@tonic-gate 		case 2:	A[1]=B[1];
3660Sstevel@tonic-gate 		case 1:	A[0]=B[0];
3670Sstevel@tonic-gate 		case 0: /* workaround for ultrix cc: without 'case 0', the optimizer does
3680Sstevel@tonic-gate 		         * the switch table by doing a=top&3; a--; goto jump_table[a];
3690Sstevel@tonic-gate 		         * which fails for top== 0 */
3700Sstevel@tonic-gate 			;
3710Sstevel@tonic-gate 			}
3720Sstevel@tonic-gate 		}
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate #else
375*2139Sjp161948 	memset(A,0,sizeof(BN_ULONG)*words);
3760Sstevel@tonic-gate 	memcpy(A,b->d,sizeof(b->d[0])*b->top);
3770Sstevel@tonic-gate #endif
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	return(a);
3800Sstevel@tonic-gate 	}
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate /* This is an internal function that can be used instead of bn_expand2()
3830Sstevel@tonic-gate  * when there is a need to copy BIGNUMs instead of only expanding the
3840Sstevel@tonic-gate  * data part, while still expanding them.
3850Sstevel@tonic-gate  * Especially useful when needing to expand BIGNUMs that are declared
3860Sstevel@tonic-gate  * 'const' and should therefore not be changed.
3870Sstevel@tonic-gate  * The reason to use this instead of a BN_dup() followed by a bn_expand2()
3880Sstevel@tonic-gate  * is memory allocation overhead.  A BN_dup() followed by a bn_expand2()
3890Sstevel@tonic-gate  * will allocate new memory for the BIGNUM data twice, and free it once,
3900Sstevel@tonic-gate  * while bn_dup_expand() makes sure allocation is made only once.
3910Sstevel@tonic-gate  */
3920Sstevel@tonic-gate 
393*2139Sjp161948 #ifndef OPENSSL_NO_DEPRECATED
bn_dup_expand(const BIGNUM * b,int words)3940Sstevel@tonic-gate BIGNUM *bn_dup_expand(const BIGNUM *b, int words)
3950Sstevel@tonic-gate 	{
3960Sstevel@tonic-gate 	BIGNUM *r = NULL;
3970Sstevel@tonic-gate 
398*2139Sjp161948 	bn_check_top(b);
399*2139Sjp161948 
4000Sstevel@tonic-gate 	/* This function does not work if
4010Sstevel@tonic-gate 	 *      words <= b->dmax && top < words
4020Sstevel@tonic-gate 	 * because BN_dup() does not preserve 'dmax'!
4030Sstevel@tonic-gate 	 * (But bn_dup_expand() is not used anywhere yet.)
4040Sstevel@tonic-gate 	 */
405*2139Sjp161948 
4060Sstevel@tonic-gate 	if (words > b->dmax)
4070Sstevel@tonic-gate 		{
4080Sstevel@tonic-gate 		BN_ULONG *a = bn_expand_internal(b, words);
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 		if (a)
4110Sstevel@tonic-gate 			{
4120Sstevel@tonic-gate 			r = BN_new();
4130Sstevel@tonic-gate 			if (r)
4140Sstevel@tonic-gate 				{
4150Sstevel@tonic-gate 				r->top = b->top;
4160Sstevel@tonic-gate 				r->dmax = words;
4170Sstevel@tonic-gate 				r->neg = b->neg;
4180Sstevel@tonic-gate 				r->d = a;
4190Sstevel@tonic-gate 				}
4200Sstevel@tonic-gate 			else
4210Sstevel@tonic-gate 				{
4220Sstevel@tonic-gate 				/* r == NULL, BN_new failure */
4230Sstevel@tonic-gate 				OPENSSL_free(a);
4240Sstevel@tonic-gate 				}
4250Sstevel@tonic-gate 			}
4260Sstevel@tonic-gate 		/* If a == NULL, there was an error in allocation in
4270Sstevel@tonic-gate 		   bn_expand_internal(), and NULL should be returned */
4280Sstevel@tonic-gate 		}
4290Sstevel@tonic-gate 	else
4300Sstevel@tonic-gate 		{
4310Sstevel@tonic-gate 		r = BN_dup(b);
4320Sstevel@tonic-gate 		}
4330Sstevel@tonic-gate 
434*2139Sjp161948 	bn_check_top(r);
4350Sstevel@tonic-gate 	return r;
4360Sstevel@tonic-gate 	}
437*2139Sjp161948 #endif
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate /* This is an internal function that should not be used in applications.
440*2139Sjp161948  * It ensures that 'b' has enough room for a 'words' word number
441*2139Sjp161948  * and initialises any unused part of b->d with leading zeros.
4420Sstevel@tonic-gate  * It is mostly used by the various BIGNUM routines. If there is an error,
4430Sstevel@tonic-gate  * NULL is returned. If not, 'b' is returned. */
4440Sstevel@tonic-gate 
bn_expand2(BIGNUM * b,int words)4450Sstevel@tonic-gate BIGNUM *bn_expand2(BIGNUM *b, int words)
4460Sstevel@tonic-gate 	{
447*2139Sjp161948 	bn_check_top(b);
448*2139Sjp161948 
4490Sstevel@tonic-gate 	if (words > b->dmax)
4500Sstevel@tonic-gate 		{
4510Sstevel@tonic-gate 		BN_ULONG *a = bn_expand_internal(b, words);
452*2139Sjp161948 		if(!a) return NULL;
453*2139Sjp161948 		if(b->d) OPENSSL_free(b->d);
454*2139Sjp161948 		b->d=a;
455*2139Sjp161948 		b->dmax=words;
456*2139Sjp161948 		}
4570Sstevel@tonic-gate 
458*2139Sjp161948 /* None of this should be necessary because of what b->top means! */
459*2139Sjp161948 #if 0
460*2139Sjp161948 	/* NB: bn_wexpand() calls this only if the BIGNUM really has to grow */
461*2139Sjp161948 	if (b->top < b->dmax)
462*2139Sjp161948 		{
463*2139Sjp161948 		int i;
464*2139Sjp161948 		BN_ULONG *A = &(b->d[b->top]);
465*2139Sjp161948 		for (i=(b->dmax - b->top)>>3; i>0; i--,A+=8)
4660Sstevel@tonic-gate 			{
467*2139Sjp161948 			A[0]=0; A[1]=0; A[2]=0; A[3]=0;
468*2139Sjp161948 			A[4]=0; A[5]=0; A[6]=0; A[7]=0;
4690Sstevel@tonic-gate 			}
470*2139Sjp161948 		for (i=(b->dmax - b->top)&7; i>0; i--,A++)
471*2139Sjp161948 			A[0]=0;
472*2139Sjp161948 		assert(A == &(b->d[b->dmax]));
4730Sstevel@tonic-gate 		}
474*2139Sjp161948 #endif
475*2139Sjp161948 	bn_check_top(b);
4760Sstevel@tonic-gate 	return b;
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
BN_dup(const BIGNUM * a)4790Sstevel@tonic-gate BIGNUM *BN_dup(const BIGNUM *a)
4800Sstevel@tonic-gate 	{
481*2139Sjp161948 	BIGNUM *t;
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	if (a == NULL) return NULL;
4840Sstevel@tonic-gate 	bn_check_top(a);
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	t = BN_new();
487*2139Sjp161948 	if (t == NULL) return NULL;
488*2139Sjp161948 	if(!BN_copy(t, a))
489*2139Sjp161948 		{
4900Sstevel@tonic-gate 		BN_free(t);
491*2139Sjp161948 		return NULL;
492*2139Sjp161948 		}
493*2139Sjp161948 	bn_check_top(t);
494*2139Sjp161948 	return t;
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate 
BN_copy(BIGNUM * a,const BIGNUM * b)4970Sstevel@tonic-gate BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
4980Sstevel@tonic-gate 	{
4990Sstevel@tonic-gate 	int i;
5000Sstevel@tonic-gate 	BN_ULONG *A;
5010Sstevel@tonic-gate 	const BN_ULONG *B;
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	bn_check_top(b);
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	if (a == b) return(a);
5060Sstevel@tonic-gate 	if (bn_wexpand(a,b->top) == NULL) return(NULL);
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate #if 1
5090Sstevel@tonic-gate 	A=a->d;
5100Sstevel@tonic-gate 	B=b->d;
5110Sstevel@tonic-gate 	for (i=b->top>>2; i>0; i--,A+=4,B+=4)
5120Sstevel@tonic-gate 		{
5130Sstevel@tonic-gate 		BN_ULONG a0,a1,a2,a3;
5140Sstevel@tonic-gate 		a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];
5150Sstevel@tonic-gate 		A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;
5160Sstevel@tonic-gate 		}
5170Sstevel@tonic-gate 	switch (b->top&3)
5180Sstevel@tonic-gate 		{
5190Sstevel@tonic-gate 		case 3: A[2]=B[2];
5200Sstevel@tonic-gate 		case 2: A[1]=B[1];
5210Sstevel@tonic-gate 		case 1: A[0]=B[0];
5220Sstevel@tonic-gate 		case 0: ; /* ultrix cc workaround, see comments in bn_expand_internal */
5230Sstevel@tonic-gate 		}
5240Sstevel@tonic-gate #else
5250Sstevel@tonic-gate 	memcpy(a->d,b->d,sizeof(b->d[0])*b->top);
5260Sstevel@tonic-gate #endif
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	a->top=b->top;
5290Sstevel@tonic-gate 	a->neg=b->neg;
530*2139Sjp161948 	bn_check_top(a);
5310Sstevel@tonic-gate 	return(a);
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 
BN_swap(BIGNUM * a,BIGNUM * b)5340Sstevel@tonic-gate void BN_swap(BIGNUM *a, BIGNUM *b)
5350Sstevel@tonic-gate 	{
5360Sstevel@tonic-gate 	int flags_old_a, flags_old_b;
5370Sstevel@tonic-gate 	BN_ULONG *tmp_d;
5380Sstevel@tonic-gate 	int tmp_top, tmp_dmax, tmp_neg;
5390Sstevel@tonic-gate 
540*2139Sjp161948 	bn_check_top(a);
541*2139Sjp161948 	bn_check_top(b);
542*2139Sjp161948 
5430Sstevel@tonic-gate 	flags_old_a = a->flags;
5440Sstevel@tonic-gate 	flags_old_b = b->flags;
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	tmp_d = a->d;
5470Sstevel@tonic-gate 	tmp_top = a->top;
5480Sstevel@tonic-gate 	tmp_dmax = a->dmax;
5490Sstevel@tonic-gate 	tmp_neg = a->neg;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	a->d = b->d;
5520Sstevel@tonic-gate 	a->top = b->top;
5530Sstevel@tonic-gate 	a->dmax = b->dmax;
5540Sstevel@tonic-gate 	a->neg = b->neg;
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	b->d = tmp_d;
5570Sstevel@tonic-gate 	b->top = tmp_top;
5580Sstevel@tonic-gate 	b->dmax = tmp_dmax;
5590Sstevel@tonic-gate 	b->neg = tmp_neg;
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	a->flags = (flags_old_a & BN_FLG_MALLOCED) | (flags_old_b & BN_FLG_STATIC_DATA);
5620Sstevel@tonic-gate 	b->flags = (flags_old_b & BN_FLG_MALLOCED) | (flags_old_a & BN_FLG_STATIC_DATA);
563*2139Sjp161948 	bn_check_top(a);
564*2139Sjp161948 	bn_check_top(b);
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 
BN_clear(BIGNUM * a)5670Sstevel@tonic-gate void BN_clear(BIGNUM *a)
5680Sstevel@tonic-gate 	{
569*2139Sjp161948 	bn_check_top(a);
5700Sstevel@tonic-gate 	if (a->d != NULL)
5710Sstevel@tonic-gate 		memset(a->d,0,a->dmax*sizeof(a->d[0]));
5720Sstevel@tonic-gate 	a->top=0;
5730Sstevel@tonic-gate 	a->neg=0;
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 
BN_get_word(const BIGNUM * a)5760Sstevel@tonic-gate BN_ULONG BN_get_word(const BIGNUM *a)
5770Sstevel@tonic-gate 	{
578*2139Sjp161948 	if (a->top > 1)
579*2139Sjp161948 		return BN_MASK2;
580*2139Sjp161948 	else if (a->top == 1)
581*2139Sjp161948 		return a->d[0];
582*2139Sjp161948 	/* a->top == 0 */
583*2139Sjp161948 	return 0;
5840Sstevel@tonic-gate 	}
5850Sstevel@tonic-gate 
BN_set_word(BIGNUM * a,BN_ULONG w)5860Sstevel@tonic-gate int BN_set_word(BIGNUM *a, BN_ULONG w)
5870Sstevel@tonic-gate 	{
588*2139Sjp161948 	bn_check_top(a);
589*2139Sjp161948 	if (bn_expand(a,(int)sizeof(BN_ULONG)*8) == NULL) return(0);
590*2139Sjp161948 	a->neg = 0;
591*2139Sjp161948 	a->d[0] = w;
592*2139Sjp161948 	a->top = (w ? 1 : 0);
593*2139Sjp161948 	bn_check_top(a);
5940Sstevel@tonic-gate 	return(1);
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 
BN_bin2bn(const unsigned char * s,int len,BIGNUM * ret)5970Sstevel@tonic-gate BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
5980Sstevel@tonic-gate 	{
5990Sstevel@tonic-gate 	unsigned int i,m;
6000Sstevel@tonic-gate 	unsigned int n;
6010Sstevel@tonic-gate 	BN_ULONG l;
602*2139Sjp161948 	BIGNUM  *bn = NULL;
6030Sstevel@tonic-gate 
604*2139Sjp161948 	if (ret == NULL)
605*2139Sjp161948 		ret = bn = BN_new();
6060Sstevel@tonic-gate 	if (ret == NULL) return(NULL);
607*2139Sjp161948 	bn_check_top(ret);
6080Sstevel@tonic-gate 	l=0;
6090Sstevel@tonic-gate 	n=len;
6100Sstevel@tonic-gate 	if (n == 0)
6110Sstevel@tonic-gate 		{
6120Sstevel@tonic-gate 		ret->top=0;
6130Sstevel@tonic-gate 		return(ret);
6140Sstevel@tonic-gate 		}
6150Sstevel@tonic-gate 	i=((n-1)/BN_BYTES)+1;
6160Sstevel@tonic-gate 	m=((n-1)%(BN_BYTES));
617*2139Sjp161948 	if (bn_wexpand(ret, (int)i) == NULL)
618*2139Sjp161948 		{
619*2139Sjp161948 		if (bn) BN_free(bn);
620*2139Sjp161948 		return NULL;
621*2139Sjp161948 		}
6220Sstevel@tonic-gate 	ret->top=i;
6230Sstevel@tonic-gate 	ret->neg=0;
624*2139Sjp161948 	while (n--)
6250Sstevel@tonic-gate 		{
6260Sstevel@tonic-gate 		l=(l<<8L)| *(s++);
6270Sstevel@tonic-gate 		if (m-- == 0)
6280Sstevel@tonic-gate 			{
6290Sstevel@tonic-gate 			ret->d[--i]=l;
6300Sstevel@tonic-gate 			l=0;
6310Sstevel@tonic-gate 			m=BN_BYTES-1;
6320Sstevel@tonic-gate 			}
6330Sstevel@tonic-gate 		}
6340Sstevel@tonic-gate 	/* need to call this due to clear byte at top if avoiding
6350Sstevel@tonic-gate 	 * having the top bit set (-ve number) */
636*2139Sjp161948 	bn_correct_top(ret);
6370Sstevel@tonic-gate 	return(ret);
6380Sstevel@tonic-gate 	}
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate /* ignore negative */
BN_bn2bin(const BIGNUM * a,unsigned char * to)6410Sstevel@tonic-gate int BN_bn2bin(const BIGNUM *a, unsigned char *to)
6420Sstevel@tonic-gate 	{
6430Sstevel@tonic-gate 	int n,i;
6440Sstevel@tonic-gate 	BN_ULONG l;
6450Sstevel@tonic-gate 
646*2139Sjp161948 	bn_check_top(a);
6470Sstevel@tonic-gate 	n=i=BN_num_bytes(a);
648*2139Sjp161948 	while (i--)
6490Sstevel@tonic-gate 		{
6500Sstevel@tonic-gate 		l=a->d[i/BN_BYTES];
6510Sstevel@tonic-gate 		*(to++)=(unsigned char)(l>>(8*(i%BN_BYTES)))&0xff;
6520Sstevel@tonic-gate 		}
6530Sstevel@tonic-gate 	return(n);
6540Sstevel@tonic-gate 	}
6550Sstevel@tonic-gate 
BN_ucmp(const BIGNUM * a,const BIGNUM * b)6560Sstevel@tonic-gate int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
6570Sstevel@tonic-gate 	{
6580Sstevel@tonic-gate 	int i;
6590Sstevel@tonic-gate 	BN_ULONG t1,t2,*ap,*bp;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	bn_check_top(a);
6620Sstevel@tonic-gate 	bn_check_top(b);
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	i=a->top-b->top;
6650Sstevel@tonic-gate 	if (i != 0) return(i);
6660Sstevel@tonic-gate 	ap=a->d;
6670Sstevel@tonic-gate 	bp=b->d;
6680Sstevel@tonic-gate 	for (i=a->top-1; i>=0; i--)
6690Sstevel@tonic-gate 		{
6700Sstevel@tonic-gate 		t1= ap[i];
6710Sstevel@tonic-gate 		t2= bp[i];
6720Sstevel@tonic-gate 		if (t1 != t2)
673*2139Sjp161948 			return((t1 > t2) ? 1 : -1);
6740Sstevel@tonic-gate 		}
6750Sstevel@tonic-gate 	return(0);
6760Sstevel@tonic-gate 	}
6770Sstevel@tonic-gate 
BN_cmp(const BIGNUM * a,const BIGNUM * b)6780Sstevel@tonic-gate int BN_cmp(const BIGNUM *a, const BIGNUM *b)
6790Sstevel@tonic-gate 	{
6800Sstevel@tonic-gate 	int i;
6810Sstevel@tonic-gate 	int gt,lt;
6820Sstevel@tonic-gate 	BN_ULONG t1,t2;
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	if ((a == NULL) || (b == NULL))
6850Sstevel@tonic-gate 		{
6860Sstevel@tonic-gate 		if (a != NULL)
6870Sstevel@tonic-gate 			return(-1);
6880Sstevel@tonic-gate 		else if (b != NULL)
6890Sstevel@tonic-gate 			return(1);
6900Sstevel@tonic-gate 		else
6910Sstevel@tonic-gate 			return(0);
6920Sstevel@tonic-gate 		}
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	bn_check_top(a);
6950Sstevel@tonic-gate 	bn_check_top(b);
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	if (a->neg != b->neg)
6980Sstevel@tonic-gate 		{
6990Sstevel@tonic-gate 		if (a->neg)
7000Sstevel@tonic-gate 			return(-1);
7010Sstevel@tonic-gate 		else	return(1);
7020Sstevel@tonic-gate 		}
7030Sstevel@tonic-gate 	if (a->neg == 0)
7040Sstevel@tonic-gate 		{ gt=1; lt= -1; }
7050Sstevel@tonic-gate 	else	{ gt= -1; lt=1; }
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	if (a->top > b->top) return(gt);
7080Sstevel@tonic-gate 	if (a->top < b->top) return(lt);
7090Sstevel@tonic-gate 	for (i=a->top-1; i>=0; i--)
7100Sstevel@tonic-gate 		{
7110Sstevel@tonic-gate 		t1=a->d[i];
7120Sstevel@tonic-gate 		t2=b->d[i];
7130Sstevel@tonic-gate 		if (t1 > t2) return(gt);
7140Sstevel@tonic-gate 		if (t1 < t2) return(lt);
7150Sstevel@tonic-gate 		}
7160Sstevel@tonic-gate 	return(0);
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 
BN_set_bit(BIGNUM * a,int n)7190Sstevel@tonic-gate int BN_set_bit(BIGNUM *a, int n)
7200Sstevel@tonic-gate 	{
7210Sstevel@tonic-gate 	int i,j,k;
7220Sstevel@tonic-gate 
723*2139Sjp161948 	if (n < 0)
724*2139Sjp161948 		return 0;
725*2139Sjp161948 
7260Sstevel@tonic-gate 	i=n/BN_BITS2;
7270Sstevel@tonic-gate 	j=n%BN_BITS2;
7280Sstevel@tonic-gate 	if (a->top <= i)
7290Sstevel@tonic-gate 		{
7300Sstevel@tonic-gate 		if (bn_wexpand(a,i+1) == NULL) return(0);
7310Sstevel@tonic-gate 		for(k=a->top; k<i+1; k++)
7320Sstevel@tonic-gate 			a->d[k]=0;
7330Sstevel@tonic-gate 		a->top=i+1;
7340Sstevel@tonic-gate 		}
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	a->d[i]|=(((BN_ULONG)1)<<j);
737*2139Sjp161948 	bn_check_top(a);
7380Sstevel@tonic-gate 	return(1);
7390Sstevel@tonic-gate 	}
7400Sstevel@tonic-gate 
BN_clear_bit(BIGNUM * a,int n)7410Sstevel@tonic-gate int BN_clear_bit(BIGNUM *a, int n)
7420Sstevel@tonic-gate 	{
7430Sstevel@tonic-gate 	int i,j;
7440Sstevel@tonic-gate 
745*2139Sjp161948 	bn_check_top(a);
746*2139Sjp161948 	if (n < 0) return 0;
747*2139Sjp161948 
7480Sstevel@tonic-gate 	i=n/BN_BITS2;
7490Sstevel@tonic-gate 	j=n%BN_BITS2;
7500Sstevel@tonic-gate 	if (a->top <= i) return(0);
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	a->d[i]&=(~(((BN_ULONG)1)<<j));
753*2139Sjp161948 	bn_correct_top(a);
7540Sstevel@tonic-gate 	return(1);
7550Sstevel@tonic-gate 	}
7560Sstevel@tonic-gate 
BN_is_bit_set(const BIGNUM * a,int n)7570Sstevel@tonic-gate int BN_is_bit_set(const BIGNUM *a, int n)
7580Sstevel@tonic-gate 	{
7590Sstevel@tonic-gate 	int i,j;
7600Sstevel@tonic-gate 
761*2139Sjp161948 	bn_check_top(a);
762*2139Sjp161948 	if (n < 0) return 0;
7630Sstevel@tonic-gate 	i=n/BN_BITS2;
7640Sstevel@tonic-gate 	j=n%BN_BITS2;
765*2139Sjp161948 	if (a->top <= i) return 0;
7660Sstevel@tonic-gate 	return((a->d[i]&(((BN_ULONG)1)<<j))?1:0);
7670Sstevel@tonic-gate 	}
7680Sstevel@tonic-gate 
BN_mask_bits(BIGNUM * a,int n)7690Sstevel@tonic-gate int BN_mask_bits(BIGNUM *a, int n)
7700Sstevel@tonic-gate 	{
7710Sstevel@tonic-gate 	int b,w;
7720Sstevel@tonic-gate 
773*2139Sjp161948 	bn_check_top(a);
774*2139Sjp161948 	if (n < 0) return 0;
775*2139Sjp161948 
7760Sstevel@tonic-gate 	w=n/BN_BITS2;
7770Sstevel@tonic-gate 	b=n%BN_BITS2;
778*2139Sjp161948 	if (w >= a->top) return 0;
7790Sstevel@tonic-gate 	if (b == 0)
7800Sstevel@tonic-gate 		a->top=w;
7810Sstevel@tonic-gate 	else
7820Sstevel@tonic-gate 		{
7830Sstevel@tonic-gate 		a->top=w+1;
7840Sstevel@tonic-gate 		a->d[w]&= ~(BN_MASK2<<b);
7850Sstevel@tonic-gate 		}
786*2139Sjp161948 	bn_correct_top(a);
7870Sstevel@tonic-gate 	return(1);
7880Sstevel@tonic-gate 	}
7890Sstevel@tonic-gate 
BN_set_negative(BIGNUM * a,int b)790*2139Sjp161948 void BN_set_negative(BIGNUM *a, int b)
791*2139Sjp161948 	{
792*2139Sjp161948 	if (b && !BN_is_zero(a))
793*2139Sjp161948 		a->neg = 1;
794*2139Sjp161948 	else
795*2139Sjp161948 		a->neg = 0;
796*2139Sjp161948 	}
797*2139Sjp161948 
bn_cmp_words(const BN_ULONG * a,const BN_ULONG * b,int n)7980Sstevel@tonic-gate int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
7990Sstevel@tonic-gate 	{
8000Sstevel@tonic-gate 	int i;
8010Sstevel@tonic-gate 	BN_ULONG aa,bb;
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	aa=a[n-1];
8040Sstevel@tonic-gate 	bb=b[n-1];
8050Sstevel@tonic-gate 	if (aa != bb) return((aa > bb)?1:-1);
8060Sstevel@tonic-gate 	for (i=n-2; i>=0; i--)
8070Sstevel@tonic-gate 		{
8080Sstevel@tonic-gate 		aa=a[i];
8090Sstevel@tonic-gate 		bb=b[i];
8100Sstevel@tonic-gate 		if (aa != bb) return((aa > bb)?1:-1);
8110Sstevel@tonic-gate 		}
8120Sstevel@tonic-gate 	return(0);
8130Sstevel@tonic-gate 	}
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate /* Here follows a specialised variants of bn_cmp_words().  It has the
8160Sstevel@tonic-gate    property of performing the operation on arrays of different sizes.
8170Sstevel@tonic-gate    The sizes of those arrays is expressed through cl, which is the
8180Sstevel@tonic-gate    common length ( basicall, min(len(a),len(b)) ), and dl, which is the
8190Sstevel@tonic-gate    delta between the two lengths, calculated as len(a)-len(b).
8200Sstevel@tonic-gate    All lengths are the number of BN_ULONGs...  */
8210Sstevel@tonic-gate 
bn_cmp_part_words(const BN_ULONG * a,const BN_ULONG * b,int cl,int dl)8220Sstevel@tonic-gate int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b,
8230Sstevel@tonic-gate 	int cl, int dl)
8240Sstevel@tonic-gate 	{
8250Sstevel@tonic-gate 	int n,i;
8260Sstevel@tonic-gate 	n = cl-1;
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	if (dl < 0)
8290Sstevel@tonic-gate 		{
8300Sstevel@tonic-gate 		for (i=dl; i<0; i++)
8310Sstevel@tonic-gate 			{
8320Sstevel@tonic-gate 			if (b[n-i] != 0)
8330Sstevel@tonic-gate 				return -1; /* a < b */
8340Sstevel@tonic-gate 			}
8350Sstevel@tonic-gate 		}
8360Sstevel@tonic-gate 	if (dl > 0)
8370Sstevel@tonic-gate 		{
8380Sstevel@tonic-gate 		for (i=dl; i>0; i--)
8390Sstevel@tonic-gate 			{
8400Sstevel@tonic-gate 			if (a[n+i] != 0)
8410Sstevel@tonic-gate 				return 1; /* a > b */
8420Sstevel@tonic-gate 			}
8430Sstevel@tonic-gate 		}
8440Sstevel@tonic-gate 	return bn_cmp_words(a,b,cl);
8450Sstevel@tonic-gate 	}
846