xref: /onnv-gate/usr/src/common/openssl/crypto/bn/bn.h (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/bn/bn.h */
20Sstevel@tonic-gate /* Copyright (C) 1995-1997 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  */
58*2139Sjp161948 /* ====================================================================
59*2139Sjp161948  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60*2139Sjp161948  *
61*2139Sjp161948  * Portions of the attached software ("Contribution") are developed by
62*2139Sjp161948  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63*2139Sjp161948  *
64*2139Sjp161948  * The Contribution is licensed pursuant to the Eric Young open source
65*2139Sjp161948  * license provided above.
66*2139Sjp161948  *
67*2139Sjp161948  * The binary polynomial arithmetic software is originally written by
68*2139Sjp161948  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69*2139Sjp161948  *
70*2139Sjp161948  */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #ifndef HEADER_BN_H
730Sstevel@tonic-gate #define HEADER_BN_H
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #include <openssl/e_os2.h>
760Sstevel@tonic-gate #ifndef OPENSSL_NO_FP_API
770Sstevel@tonic-gate #include <stdio.h> /* FILE */
780Sstevel@tonic-gate #endif
79*2139Sjp161948 #include <openssl/ossl_typ.h>
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #ifdef  __cplusplus
820Sstevel@tonic-gate extern "C" {
830Sstevel@tonic-gate #endif
840Sstevel@tonic-gate 
85*2139Sjp161948 /* These preprocessor symbols control various aspects of the bignum headers and
86*2139Sjp161948  * library code. They're not defined by any "normal" configuration, as they are
87*2139Sjp161948  * intended for development and testing purposes. NB: defining all three can be
88*2139Sjp161948  * useful for debugging application code as well as openssl itself.
89*2139Sjp161948  *
90*2139Sjp161948  * BN_DEBUG - turn on various debugging alterations to the bignum code
91*2139Sjp161948  * BN_DEBUG_RAND - uses random poisoning of unused words to trip up
92*2139Sjp161948  * mismanagement of bignum internals. You must also define BN_DEBUG.
93*2139Sjp161948  */
94*2139Sjp161948 /* #define BN_DEBUG */
95*2139Sjp161948 /* #define BN_DEBUG_RAND */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate #define BN_MUL_COMBA
980Sstevel@tonic-gate #define BN_SQR_COMBA
990Sstevel@tonic-gate #define BN_RECURSION
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate /* This next option uses the C libraries (2 word)/(1 word) function.
1020Sstevel@tonic-gate  * If it is not defined, I use my C version (which is slower).
1030Sstevel@tonic-gate  * The reason for this flag is that when the particular C compiler
1040Sstevel@tonic-gate  * library routine is used, and the library is linked with a different
1050Sstevel@tonic-gate  * compiler, the library is missing.  This mostly happens when the
1060Sstevel@tonic-gate  * library is built with gcc and then linked using normal cc.  This would
1070Sstevel@tonic-gate  * be a common occurrence because gcc normally produces code that is
1080Sstevel@tonic-gate  * 2 times faster than system compilers for the big number stuff.
1090Sstevel@tonic-gate  * For machines with only one compiler (or shared libraries), this should
1100Sstevel@tonic-gate  * be on.  Again this in only really a problem on machines
1110Sstevel@tonic-gate  * using "long long's", are 32bit, and are not using my assembler code. */
1120Sstevel@tonic-gate #if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || \
1130Sstevel@tonic-gate     defined(OPENSSL_SYS_WIN32) || defined(linux)
1140Sstevel@tonic-gate # ifndef BN_DIV2W
1150Sstevel@tonic-gate #  define BN_DIV2W
1160Sstevel@tonic-gate # endif
1170Sstevel@tonic-gate #endif
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /* assuming long is 64bit - this is the DEC Alpha
1200Sstevel@tonic-gate  * unsigned long long is only 64 bits :-(, don't define
1210Sstevel@tonic-gate  * BN_LLONG for the DEC Alpha */
1220Sstevel@tonic-gate #ifdef SIXTY_FOUR_BIT_LONG
1230Sstevel@tonic-gate #define BN_ULLONG	unsigned long long
1240Sstevel@tonic-gate #define BN_ULONG	unsigned long
1250Sstevel@tonic-gate #define BN_LONG		long
1260Sstevel@tonic-gate #define BN_BITS		128
1270Sstevel@tonic-gate #define BN_BYTES	8
1280Sstevel@tonic-gate #define BN_BITS2	64
1290Sstevel@tonic-gate #define BN_BITS4	32
1300Sstevel@tonic-gate #define BN_MASK		(0xffffffffffffffffffffffffffffffffLL)
1310Sstevel@tonic-gate #define BN_MASK2	(0xffffffffffffffffL)
1320Sstevel@tonic-gate #define BN_MASK2l	(0xffffffffL)
1330Sstevel@tonic-gate #define BN_MASK2h	(0xffffffff00000000L)
1340Sstevel@tonic-gate #define BN_MASK2h1	(0xffffffff80000000L)
1350Sstevel@tonic-gate #define BN_TBIT		(0x8000000000000000L)
1360Sstevel@tonic-gate #define BN_DEC_CONV	(10000000000000000000UL)
1370Sstevel@tonic-gate #define BN_DEC_FMT1	"%lu"
1380Sstevel@tonic-gate #define BN_DEC_FMT2	"%019lu"
1390Sstevel@tonic-gate #define BN_DEC_NUM	19
1400Sstevel@tonic-gate #endif
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /* This is where the long long data type is 64 bits, but long is 32.
1430Sstevel@tonic-gate  * For machines where there are 64bit registers, this is the mode to use.
1440Sstevel@tonic-gate  * IRIX, on R4000 and above should use this mode, along with the relevant
1450Sstevel@tonic-gate  * assembler code :-).  Do NOT define BN_LLONG.
1460Sstevel@tonic-gate  */
1470Sstevel@tonic-gate #ifdef SIXTY_FOUR_BIT
1480Sstevel@tonic-gate #undef BN_LLONG
1490Sstevel@tonic-gate #undef BN_ULLONG
1500Sstevel@tonic-gate #define BN_ULONG	unsigned long long
1510Sstevel@tonic-gate #define BN_LONG		long long
1520Sstevel@tonic-gate #define BN_BITS		128
1530Sstevel@tonic-gate #define BN_BYTES	8
1540Sstevel@tonic-gate #define BN_BITS2	64
1550Sstevel@tonic-gate #define BN_BITS4	32
1560Sstevel@tonic-gate #define BN_MASK2	(0xffffffffffffffffLL)
1570Sstevel@tonic-gate #define BN_MASK2l	(0xffffffffL)
1580Sstevel@tonic-gate #define BN_MASK2h	(0xffffffff00000000LL)
1590Sstevel@tonic-gate #define BN_MASK2h1	(0xffffffff80000000LL)
1600Sstevel@tonic-gate #define BN_TBIT		(0x8000000000000000LL)
1610Sstevel@tonic-gate #define BN_DEC_CONV	(10000000000000000000ULL)
1620Sstevel@tonic-gate #define BN_DEC_FMT1	"%llu"
1630Sstevel@tonic-gate #define BN_DEC_FMT2	"%019llu"
1640Sstevel@tonic-gate #define BN_DEC_NUM	19
1650Sstevel@tonic-gate #endif
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate #ifdef THIRTY_TWO_BIT
168*2139Sjp161948 #ifdef BN_LLONG
169*2139Sjp161948 # if defined(OPENSSL_SYS_WIN32) && !defined(__GNUC__)
170*2139Sjp161948 #  define BN_ULLONG	unsigned __int64
171*2139Sjp161948 # else
172*2139Sjp161948 #  define BN_ULLONG	unsigned long long
173*2139Sjp161948 # endif
1740Sstevel@tonic-gate #endif
1750Sstevel@tonic-gate #define BN_ULONG	unsigned long
1760Sstevel@tonic-gate #define BN_LONG		long
1770Sstevel@tonic-gate #define BN_BITS		64
1780Sstevel@tonic-gate #define BN_BYTES	4
1790Sstevel@tonic-gate #define BN_BITS2	32
1800Sstevel@tonic-gate #define BN_BITS4	16
1810Sstevel@tonic-gate #ifdef OPENSSL_SYS_WIN32
1820Sstevel@tonic-gate /* VC++ doesn't like the LL suffix */
1830Sstevel@tonic-gate #define BN_MASK		(0xffffffffffffffffL)
1840Sstevel@tonic-gate #else
1850Sstevel@tonic-gate #define BN_MASK		(0xffffffffffffffffLL)
1860Sstevel@tonic-gate #endif
1870Sstevel@tonic-gate #define BN_MASK2	(0xffffffffL)
1880Sstevel@tonic-gate #define BN_MASK2l	(0xffff)
1890Sstevel@tonic-gate #define BN_MASK2h1	(0xffff8000L)
1900Sstevel@tonic-gate #define BN_MASK2h	(0xffff0000L)
1910Sstevel@tonic-gate #define BN_TBIT		(0x80000000L)
1920Sstevel@tonic-gate #define BN_DEC_CONV	(1000000000L)
1930Sstevel@tonic-gate #define BN_DEC_FMT1	"%lu"
1940Sstevel@tonic-gate #define BN_DEC_FMT2	"%09lu"
1950Sstevel@tonic-gate #define BN_DEC_NUM	9
1960Sstevel@tonic-gate #endif
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate #ifdef SIXTEEN_BIT
1990Sstevel@tonic-gate #ifndef BN_DIV2W
2000Sstevel@tonic-gate #define BN_DIV2W
2010Sstevel@tonic-gate #endif
2020Sstevel@tonic-gate #define BN_ULLONG	unsigned long
2030Sstevel@tonic-gate #define BN_ULONG	unsigned short
2040Sstevel@tonic-gate #define BN_LONG		short
2050Sstevel@tonic-gate #define BN_BITS		32
2060Sstevel@tonic-gate #define BN_BYTES	2
2070Sstevel@tonic-gate #define BN_BITS2	16
2080Sstevel@tonic-gate #define BN_BITS4	8
2090Sstevel@tonic-gate #define BN_MASK		(0xffffffff)
2100Sstevel@tonic-gate #define BN_MASK2	(0xffff)
2110Sstevel@tonic-gate #define BN_MASK2l	(0xff)
2120Sstevel@tonic-gate #define BN_MASK2h1	(0xff80)
2130Sstevel@tonic-gate #define BN_MASK2h	(0xff00)
2140Sstevel@tonic-gate #define BN_TBIT		(0x8000)
2150Sstevel@tonic-gate #define BN_DEC_CONV	(100000)
2160Sstevel@tonic-gate #define BN_DEC_FMT1	"%u"
2170Sstevel@tonic-gate #define BN_DEC_FMT2	"%05u"
2180Sstevel@tonic-gate #define BN_DEC_NUM	5
2190Sstevel@tonic-gate #endif
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate #ifdef EIGHT_BIT
2220Sstevel@tonic-gate #ifndef BN_DIV2W
2230Sstevel@tonic-gate #define BN_DIV2W
2240Sstevel@tonic-gate #endif
2250Sstevel@tonic-gate #define BN_ULLONG	unsigned short
2260Sstevel@tonic-gate #define BN_ULONG	unsigned char
2270Sstevel@tonic-gate #define BN_LONG		char
2280Sstevel@tonic-gate #define BN_BITS		16
2290Sstevel@tonic-gate #define BN_BYTES	1
2300Sstevel@tonic-gate #define BN_BITS2	8
2310Sstevel@tonic-gate #define BN_BITS4	4
2320Sstevel@tonic-gate #define BN_MASK		(0xffff)
2330Sstevel@tonic-gate #define BN_MASK2	(0xff)
2340Sstevel@tonic-gate #define BN_MASK2l	(0xf)
2350Sstevel@tonic-gate #define BN_MASK2h1	(0xf8)
2360Sstevel@tonic-gate #define BN_MASK2h	(0xf0)
2370Sstevel@tonic-gate #define BN_TBIT		(0x80)
2380Sstevel@tonic-gate #define BN_DEC_CONV	(100)
2390Sstevel@tonic-gate #define BN_DEC_FMT1	"%u"
2400Sstevel@tonic-gate #define BN_DEC_FMT2	"%02u"
2410Sstevel@tonic-gate #define BN_DEC_NUM	2
2420Sstevel@tonic-gate #endif
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate #define BN_DEFAULT_BITS	1280
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate #define BN_FLG_MALLOCED		0x01
2470Sstevel@tonic-gate #define BN_FLG_STATIC_DATA	0x02
248*2139Sjp161948 #define BN_FLG_EXP_CONSTTIME	0x04 /* avoid leaking exponent information through timings
249*2139Sjp161948                             	      * (BN_mod_exp_mont() will call BN_mod_exp_mont_consttime) */
250*2139Sjp161948 #ifndef OPENSSL_NO_DEPRECATED
2510Sstevel@tonic-gate #define BN_FLG_FREE		0x8000	/* used for debuging */
252*2139Sjp161948 #endif
2530Sstevel@tonic-gate #define BN_set_flags(b,n)	((b)->flags|=(n))
2540Sstevel@tonic-gate #define BN_get_flags(b,n)	((b)->flags&(n))
2550Sstevel@tonic-gate 
256*2139Sjp161948 /* get a clone of a BIGNUM with changed flags, for *temporary* use only
257*2139Sjp161948  * (the two BIGNUMs cannot not be used in parallel!) */
258*2139Sjp161948 #define BN_with_flags(dest,b,n)  ((dest)->d=(b)->d, \
259*2139Sjp161948                                   (dest)->top=(b)->top, \
260*2139Sjp161948                                   (dest)->dmax=(b)->dmax, \
261*2139Sjp161948                                   (dest)->neg=(b)->neg, \
262*2139Sjp161948                                   (dest)->flags=(((dest)->flags & BN_FLG_MALLOCED) \
263*2139Sjp161948                                                  |  ((b)->flags & ~BN_FLG_MALLOCED) \
264*2139Sjp161948                                                  |  BN_FLG_STATIC_DATA \
265*2139Sjp161948                                                  |  (n)))
266*2139Sjp161948 
267*2139Sjp161948 /* Already declared in ossl_typ.h */
268*2139Sjp161948 #if 0
269*2139Sjp161948 typedef struct bignum_st BIGNUM;
270*2139Sjp161948 /* Used for temp variables (declaration hidden in bn_lcl.h) */
271*2139Sjp161948 typedef struct bignum_ctx BN_CTX;
272*2139Sjp161948 typedef struct bn_blinding_st BN_BLINDING;
273*2139Sjp161948 typedef struct bn_mont_ctx_st BN_MONT_CTX;
274*2139Sjp161948 typedef struct bn_recp_ctx_st BN_RECP_CTX;
275*2139Sjp161948 typedef struct bn_gencb_st BN_GENCB;
276*2139Sjp161948 #endif
277*2139Sjp161948 
278*2139Sjp161948 struct bignum_st
2790Sstevel@tonic-gate 	{
2800Sstevel@tonic-gate 	BN_ULONG *d;	/* Pointer to an array of 'BN_BITS2' bit chunks. */
2810Sstevel@tonic-gate 	int top;	/* Index of last used d +1. */
2820Sstevel@tonic-gate 	/* The next are internal book keeping for bn_expand. */
2830Sstevel@tonic-gate 	int dmax;	/* Size of the d array. */
2840Sstevel@tonic-gate 	int neg;	/* one if the number is negative */
2850Sstevel@tonic-gate 	int flags;
286*2139Sjp161948 	};
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate /* Used for montgomery multiplication */
289*2139Sjp161948 struct bn_mont_ctx_st
2900Sstevel@tonic-gate 	{
2910Sstevel@tonic-gate 	int ri;        /* number of bits in R */
2920Sstevel@tonic-gate 	BIGNUM RR;     /* used to convert to montgomery form */
2930Sstevel@tonic-gate 	BIGNUM N;      /* The modulus */
2940Sstevel@tonic-gate 	BIGNUM Ni;     /* R*(1/R mod N) - N*Ni = 1
2950Sstevel@tonic-gate 	                * (Ni is only stored for bignum algorithm) */
2960Sstevel@tonic-gate 	BN_ULONG n0;   /* least significant word of Ni */
2970Sstevel@tonic-gate 	int flags;
298*2139Sjp161948 	};
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate /* Used for reciprocal division/mod functions
3010Sstevel@tonic-gate  * It cannot be shared between threads
3020Sstevel@tonic-gate  */
303*2139Sjp161948 struct bn_recp_ctx_st
3040Sstevel@tonic-gate 	{
3050Sstevel@tonic-gate 	BIGNUM N;	/* the divisor */
3060Sstevel@tonic-gate 	BIGNUM Nr;	/* the reciprocal */
3070Sstevel@tonic-gate 	int num_bits;
3080Sstevel@tonic-gate 	int shift;
3090Sstevel@tonic-gate 	int flags;
310*2139Sjp161948 	};
311*2139Sjp161948 
312*2139Sjp161948 /* Used for slow "generation" functions. */
313*2139Sjp161948 struct bn_gencb_st
314*2139Sjp161948 	{
315*2139Sjp161948 	unsigned int ver;	/* To handle binary (in)compatibility */
316*2139Sjp161948 	void *arg;		/* callback-specific data */
317*2139Sjp161948 	union
318*2139Sjp161948 		{
319*2139Sjp161948 		/* if(ver==1) - handles old style callbacks */
320*2139Sjp161948 		void (*cb_1)(int, int, void *);
321*2139Sjp161948 		/* if(ver==2) - new callback style */
322*2139Sjp161948 		int (*cb_2)(int, int, BN_GENCB *);
323*2139Sjp161948 		} cb;
324*2139Sjp161948 	};
325*2139Sjp161948 /* Wrapper function to make using BN_GENCB easier,  */
326*2139Sjp161948 int BN_GENCB_call(BN_GENCB *cb, int a, int b);
327*2139Sjp161948 /* Macro to populate a BN_GENCB structure with an "old"-style callback */
328*2139Sjp161948 #define BN_GENCB_set_old(gencb, callback, cb_arg) { \
329*2139Sjp161948 		BN_GENCB *tmp_gencb = (gencb); \
330*2139Sjp161948 		tmp_gencb->ver = 1; \
331*2139Sjp161948 		tmp_gencb->arg = (cb_arg); \
332*2139Sjp161948 		tmp_gencb->cb.cb_1 = (callback); }
333*2139Sjp161948 /* Macro to populate a BN_GENCB structure with a "new"-style callback */
334*2139Sjp161948 #define BN_GENCB_set(gencb, callback, cb_arg) { \
335*2139Sjp161948 		BN_GENCB *tmp_gencb = (gencb); \
336*2139Sjp161948 		tmp_gencb->ver = 2; \
337*2139Sjp161948 		tmp_gencb->arg = (cb_arg); \
338*2139Sjp161948 		tmp_gencb->cb.cb_2 = (callback); }
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate #define BN_prime_checks 0 /* default: select number of iterations
3410Sstevel@tonic-gate 			     based on the size of the number */
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate /* number of Miller-Rabin iterations for an error rate  of less than 2^-80
3440Sstevel@tonic-gate  * for random 'b'-bit input, b >= 100 (taken from table 4.4 in the Handbook
3450Sstevel@tonic-gate  * of Applied Cryptography [Menezes, van Oorschot, Vanstone; CRC Press 1996];
3460Sstevel@tonic-gate  * original paper: Damgaard, Landrock, Pomerance: Average case error estimates
3470Sstevel@tonic-gate  * for the strong probable prime test. -- Math. Comp. 61 (1993) 177-194) */
3480Sstevel@tonic-gate #define BN_prime_checks_for_size(b) ((b) >= 1300 ?  2 : \
3490Sstevel@tonic-gate                                 (b) >=  850 ?  3 : \
3500Sstevel@tonic-gate                                 (b) >=  650 ?  4 : \
3510Sstevel@tonic-gate                                 (b) >=  550 ?  5 : \
3520Sstevel@tonic-gate                                 (b) >=  450 ?  6 : \
3530Sstevel@tonic-gate                                 (b) >=  400 ?  7 : \
3540Sstevel@tonic-gate                                 (b) >=  350 ?  8 : \
3550Sstevel@tonic-gate                                 (b) >=  300 ?  9 : \
3560Sstevel@tonic-gate                                 (b) >=  250 ? 12 : \
3570Sstevel@tonic-gate                                 (b) >=  200 ? 15 : \
3580Sstevel@tonic-gate                                 (b) >=  150 ? 18 : \
3590Sstevel@tonic-gate                                 /* b >= 100 */ 27)
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate #define BN_num_bytes(a)	((BN_num_bits(a)+7)/8)
3620Sstevel@tonic-gate 
363*2139Sjp161948 /* Note that BN_abs_is_word didn't work reliably for w == 0 until 0.9.8 */
364*2139Sjp161948 #define BN_abs_is_word(a,w) ((((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w))) || \
365*2139Sjp161948 				(((w) == 0) && ((a)->top == 0)))
366*2139Sjp161948 #define BN_is_zero(a)       ((a)->top == 0)
3670Sstevel@tonic-gate #define BN_is_one(a)        (BN_abs_is_word((a),1) && !(a)->neg)
368*2139Sjp161948 #define BN_is_word(a,w)     (BN_abs_is_word((a),(w)) && (!(w) || !(a)->neg))
3690Sstevel@tonic-gate #define BN_is_odd(a)	    (((a)->top > 0) && ((a)->d[0] & 1))
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate #define BN_one(a)	(BN_set_word((a),1))
372*2139Sjp161948 #define BN_zero_ex(a) \
373*2139Sjp161948 	do { \
374*2139Sjp161948 		BIGNUM *_tmp_bn = (a); \
375*2139Sjp161948 		_tmp_bn->top = 0; \
376*2139Sjp161948 		_tmp_bn->neg = 0; \
377*2139Sjp161948 	} while(0)
378*2139Sjp161948 #ifdef OPENSSL_NO_DEPRECATED
379*2139Sjp161948 #define BN_zero(a)	BN_zero_ex(a)
380*2139Sjp161948 #else
3810Sstevel@tonic-gate #define BN_zero(a)	(BN_set_word((a),0))
382*2139Sjp161948 #endif
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate const BIGNUM *BN_value_one(void);
3850Sstevel@tonic-gate char *	BN_options(void);
3860Sstevel@tonic-gate BN_CTX *BN_CTX_new(void);
387*2139Sjp161948 #ifndef OPENSSL_NO_DEPRECATED
3880Sstevel@tonic-gate void	BN_CTX_init(BN_CTX *c);
389*2139Sjp161948 #endif
3900Sstevel@tonic-gate void	BN_CTX_free(BN_CTX *c);
3910Sstevel@tonic-gate void	BN_CTX_start(BN_CTX *ctx);
3920Sstevel@tonic-gate BIGNUM *BN_CTX_get(BN_CTX *ctx);
3930Sstevel@tonic-gate void	BN_CTX_end(BN_CTX *ctx);
3940Sstevel@tonic-gate int     BN_rand(BIGNUM *rnd, int bits, int top,int bottom);
3950Sstevel@tonic-gate int     BN_pseudo_rand(BIGNUM *rnd, int bits, int top,int bottom);
3960Sstevel@tonic-gate int	BN_rand_range(BIGNUM *rnd, BIGNUM *range);
3970Sstevel@tonic-gate int	BN_pseudo_rand_range(BIGNUM *rnd, BIGNUM *range);
3980Sstevel@tonic-gate int	BN_num_bits(const BIGNUM *a);
3990Sstevel@tonic-gate int	BN_num_bits_word(BN_ULONG);
4000Sstevel@tonic-gate BIGNUM *BN_new(void);
4010Sstevel@tonic-gate void	BN_init(BIGNUM *);
4020Sstevel@tonic-gate void	BN_clear_free(BIGNUM *a);
4030Sstevel@tonic-gate BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b);
4040Sstevel@tonic-gate void	BN_swap(BIGNUM *a, BIGNUM *b);
4050Sstevel@tonic-gate BIGNUM *BN_bin2bn(const unsigned char *s,int len,BIGNUM *ret);
4060Sstevel@tonic-gate int	BN_bn2bin(const BIGNUM *a, unsigned char *to);
4070Sstevel@tonic-gate BIGNUM *BN_mpi2bn(const unsigned char *s,int len,BIGNUM *ret);
4080Sstevel@tonic-gate int	BN_bn2mpi(const BIGNUM *a, unsigned char *to);
4090Sstevel@tonic-gate int	BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
4100Sstevel@tonic-gate int	BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
4110Sstevel@tonic-gate int	BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
4120Sstevel@tonic-gate int	BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
4130Sstevel@tonic-gate int	BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
4140Sstevel@tonic-gate int	BN_sqr(BIGNUM *r, const BIGNUM *a,BN_CTX *ctx);
415*2139Sjp161948 /* BN_set_negative(): sets sign of a bignum */
416*2139Sjp161948 void	BN_set_negative(BIGNUM *b, int n);
417*2139Sjp161948 /* BN_get_negative():  returns 1 if the bignum is < 0 and 0 otherwise */
418*2139Sjp161948 #define BN_is_negative(a) ((a)->neg != 0)
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate int	BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
4210Sstevel@tonic-gate 	BN_CTX *ctx);
4220Sstevel@tonic-gate #define BN_mod(rem,m,d,ctx) BN_div(NULL,(rem),(m),(d),(ctx))
4230Sstevel@tonic-gate int	BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx);
4240Sstevel@tonic-gate int	BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx);
4250Sstevel@tonic-gate int	BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m);
4260Sstevel@tonic-gate int	BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx);
4270Sstevel@tonic-gate int	BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m);
4280Sstevel@tonic-gate int	BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
4290Sstevel@tonic-gate 	const BIGNUM *m, BN_CTX *ctx);
4300Sstevel@tonic-gate int	BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
4310Sstevel@tonic-gate int	BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
4320Sstevel@tonic-gate int	BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m);
4330Sstevel@tonic-gate int	BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m, BN_CTX *ctx);
4340Sstevel@tonic-gate int	BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w);
4370Sstevel@tonic-gate BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w);
4380Sstevel@tonic-gate int	BN_mul_word(BIGNUM *a, BN_ULONG w);
4390Sstevel@tonic-gate int	BN_add_word(BIGNUM *a, BN_ULONG w);
4400Sstevel@tonic-gate int	BN_sub_word(BIGNUM *a, BN_ULONG w);
4410Sstevel@tonic-gate int	BN_set_word(BIGNUM *a, BN_ULONG w);
4420Sstevel@tonic-gate BN_ULONG BN_get_word(const BIGNUM *a);
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate int	BN_cmp(const BIGNUM *a, const BIGNUM *b);
4450Sstevel@tonic-gate void	BN_free(BIGNUM *a);
4460Sstevel@tonic-gate int	BN_is_bit_set(const BIGNUM *a, int n);
4470Sstevel@tonic-gate int	BN_lshift(BIGNUM *r, const BIGNUM *a, int n);
4480Sstevel@tonic-gate int	BN_lshift1(BIGNUM *r, const BIGNUM *a);
4490Sstevel@tonic-gate int	BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,BN_CTX *ctx);
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate int	BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
4520Sstevel@tonic-gate 	const BIGNUM *m,BN_CTX *ctx);
4530Sstevel@tonic-gate int	BN_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
4540Sstevel@tonic-gate 	const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
455*2139Sjp161948 int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
456*2139Sjp161948 	const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont);
4570Sstevel@tonic-gate int	BN_mod_exp_mont_word(BIGNUM *r, BN_ULONG a, const BIGNUM *p,
4580Sstevel@tonic-gate 	const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
4590Sstevel@tonic-gate int	BN_mod_exp2_mont(BIGNUM *r, const BIGNUM *a1, const BIGNUM *p1,
4600Sstevel@tonic-gate 	const BIGNUM *a2, const BIGNUM *p2,const BIGNUM *m,
4610Sstevel@tonic-gate 	BN_CTX *ctx,BN_MONT_CTX *m_ctx);
4620Sstevel@tonic-gate int	BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
4630Sstevel@tonic-gate 	const BIGNUM *m,BN_CTX *ctx);
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate int	BN_mask_bits(BIGNUM *a,int n);
4660Sstevel@tonic-gate #ifndef OPENSSL_NO_FP_API
4670Sstevel@tonic-gate int	BN_print_fp(FILE *fp, const BIGNUM *a);
4680Sstevel@tonic-gate #endif
4690Sstevel@tonic-gate #ifdef HEADER_BIO_H
4700Sstevel@tonic-gate int	BN_print(BIO *fp, const BIGNUM *a);
4710Sstevel@tonic-gate #else
4720Sstevel@tonic-gate int	BN_print(void *fp, const BIGNUM *a);
4730Sstevel@tonic-gate #endif
4740Sstevel@tonic-gate int	BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx);
4750Sstevel@tonic-gate int	BN_rshift(BIGNUM *r, const BIGNUM *a, int n);
4760Sstevel@tonic-gate int	BN_rshift1(BIGNUM *r, const BIGNUM *a);
4770Sstevel@tonic-gate void	BN_clear(BIGNUM *a);
4780Sstevel@tonic-gate BIGNUM *BN_dup(const BIGNUM *a);
4790Sstevel@tonic-gate int	BN_ucmp(const BIGNUM *a, const BIGNUM *b);
4800Sstevel@tonic-gate int	BN_set_bit(BIGNUM *a, int n);
4810Sstevel@tonic-gate int	BN_clear_bit(BIGNUM *a, int n);
4820Sstevel@tonic-gate char *	BN_bn2hex(const BIGNUM *a);
4830Sstevel@tonic-gate char *	BN_bn2dec(const BIGNUM *a);
4840Sstevel@tonic-gate int 	BN_hex2bn(BIGNUM **a, const char *str);
4850Sstevel@tonic-gate int 	BN_dec2bn(BIGNUM **a, const char *str);
4860Sstevel@tonic-gate int	BN_gcd(BIGNUM *r,const BIGNUM *a,const BIGNUM *b,BN_CTX *ctx);
4870Sstevel@tonic-gate int	BN_kronecker(const BIGNUM *a,const BIGNUM *b,BN_CTX *ctx); /* returns -2 for error */
4880Sstevel@tonic-gate BIGNUM *BN_mod_inverse(BIGNUM *ret,
4890Sstevel@tonic-gate 	const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx);
4900Sstevel@tonic-gate BIGNUM *BN_mod_sqrt(BIGNUM *ret,
4910Sstevel@tonic-gate 	const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx);
492*2139Sjp161948 
493*2139Sjp161948 /* Deprecated versions */
494*2139Sjp161948 #ifndef OPENSSL_NO_DEPRECATED
4950Sstevel@tonic-gate BIGNUM *BN_generate_prime(BIGNUM *ret,int bits,int safe,
4960Sstevel@tonic-gate 	const BIGNUM *add, const BIGNUM *rem,
4970Sstevel@tonic-gate 	void (*callback)(int,int,void *),void *cb_arg);
4980Sstevel@tonic-gate int	BN_is_prime(const BIGNUM *p,int nchecks,
4990Sstevel@tonic-gate 	void (*callback)(int,int,void *),
5000Sstevel@tonic-gate 	BN_CTX *ctx,void *cb_arg);
5010Sstevel@tonic-gate int	BN_is_prime_fasttest(const BIGNUM *p,int nchecks,
5020Sstevel@tonic-gate 	void (*callback)(int,int,void *),BN_CTX *ctx,void *cb_arg,
5030Sstevel@tonic-gate 	int do_trial_division);
504*2139Sjp161948 #endif /* !defined(OPENSSL_NO_DEPRECATED) */
505*2139Sjp161948 
506*2139Sjp161948 /* Newer versions */
507*2139Sjp161948 int	BN_generate_prime_ex(BIGNUM *ret,int bits,int safe, const BIGNUM *add,
508*2139Sjp161948 		const BIGNUM *rem, BN_GENCB *cb);
509*2139Sjp161948 int	BN_is_prime_ex(const BIGNUM *p,int nchecks, BN_CTX *ctx, BN_GENCB *cb);
510*2139Sjp161948 int	BN_is_prime_fasttest_ex(const BIGNUM *p,int nchecks, BN_CTX *ctx,
511*2139Sjp161948 		int do_trial_division, BN_GENCB *cb);
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate BN_MONT_CTX *BN_MONT_CTX_new(void );
5140Sstevel@tonic-gate void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
5150Sstevel@tonic-gate int BN_mod_mul_montgomery(BIGNUM *r,const BIGNUM *a,const BIGNUM *b,
5160Sstevel@tonic-gate 	BN_MONT_CTX *mont, BN_CTX *ctx);
5170Sstevel@tonic-gate #define BN_to_montgomery(r,a,mont,ctx)	BN_mod_mul_montgomery(\
5180Sstevel@tonic-gate 	(r),(a),&((mont)->RR),(mont),(ctx))
5190Sstevel@tonic-gate int BN_from_montgomery(BIGNUM *r,const BIGNUM *a,
5200Sstevel@tonic-gate 	BN_MONT_CTX *mont, BN_CTX *ctx);
5210Sstevel@tonic-gate void BN_MONT_CTX_free(BN_MONT_CTX *mont);
5220Sstevel@tonic-gate int BN_MONT_CTX_set(BN_MONT_CTX *mont,const BIGNUM *mod,BN_CTX *ctx);
5230Sstevel@tonic-gate BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to,BN_MONT_CTX *from);
524*2139Sjp161948 BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock,
525*2139Sjp161948 					const BIGNUM *mod, BN_CTX *ctx);
5260Sstevel@tonic-gate 
527*2139Sjp161948 /* BN_BLINDING flags */
528*2139Sjp161948 #define	BN_BLINDING_NO_UPDATE	0x00000001
529*2139Sjp161948 #define	BN_BLINDING_NO_RECREATE	0x00000002
530*2139Sjp161948 
531*2139Sjp161948 BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod);
5320Sstevel@tonic-gate void BN_BLINDING_free(BN_BLINDING *b);
5330Sstevel@tonic-gate int BN_BLINDING_update(BN_BLINDING *b,BN_CTX *ctx);
534*2139Sjp161948 int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
5350Sstevel@tonic-gate int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
536*2139Sjp161948 int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *);
537*2139Sjp161948 int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *);
538*2139Sjp161948 unsigned long BN_BLINDING_get_thread_id(const BN_BLINDING *);
539*2139Sjp161948 void BN_BLINDING_set_thread_id(BN_BLINDING *, unsigned long);
540*2139Sjp161948 unsigned long BN_BLINDING_get_flags(const BN_BLINDING *);
541*2139Sjp161948 void BN_BLINDING_set_flags(BN_BLINDING *, unsigned long);
542*2139Sjp161948 BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,
543*2139Sjp161948 	const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,
544*2139Sjp161948 	int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
545*2139Sjp161948 			  const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx),
546*2139Sjp161948 	BN_MONT_CTX *m_ctx);
5470Sstevel@tonic-gate 
548*2139Sjp161948 #ifndef OPENSSL_NO_DEPRECATED
5490Sstevel@tonic-gate void BN_set_params(int mul,int high,int low,int mont);
5500Sstevel@tonic-gate int BN_get_params(int which); /* 0, mul, 1 high, 2 low, 3 mont */
551*2139Sjp161948 #endif
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate void	BN_RECP_CTX_init(BN_RECP_CTX *recp);
5540Sstevel@tonic-gate BN_RECP_CTX *BN_RECP_CTX_new(void);
5550Sstevel@tonic-gate void	BN_RECP_CTX_free(BN_RECP_CTX *recp);
5560Sstevel@tonic-gate int	BN_RECP_CTX_set(BN_RECP_CTX *recp,const BIGNUM *rdiv,BN_CTX *ctx);
5570Sstevel@tonic-gate int	BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,
5580Sstevel@tonic-gate 	BN_RECP_CTX *recp,BN_CTX *ctx);
5590Sstevel@tonic-gate int	BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
5600Sstevel@tonic-gate 	const BIGNUM *m, BN_CTX *ctx);
5610Sstevel@tonic-gate int	BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
5620Sstevel@tonic-gate 	BN_RECP_CTX *recp, BN_CTX *ctx);
5630Sstevel@tonic-gate 
564*2139Sjp161948 /* Functions for arithmetic over binary polynomials represented by BIGNUMs.
565*2139Sjp161948  *
566*2139Sjp161948  * The BIGNUM::neg property of BIGNUMs representing binary polynomials is
567*2139Sjp161948  * ignored.
568*2139Sjp161948  *
569*2139Sjp161948  * Note that input arguments are not const so that their bit arrays can
570*2139Sjp161948  * be expanded to the appropriate size if needed.
571*2139Sjp161948  */
572*2139Sjp161948 
573*2139Sjp161948 int	BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); /*r = a + b*/
574*2139Sjp161948 #define BN_GF2m_sub(r, a, b) BN_GF2m_add(r, a, b)
575*2139Sjp161948 int	BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p); /*r=a mod p*/
576*2139Sjp161948 int	BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
577*2139Sjp161948 	const BIGNUM *p, BN_CTX *ctx); /* r = (a * b) mod p */
578*2139Sjp161948 int	BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
579*2139Sjp161948 	BN_CTX *ctx); /* r = (a * a) mod p */
580*2139Sjp161948 int	BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *b, const BIGNUM *p,
581*2139Sjp161948 	BN_CTX *ctx); /* r = (1 / b) mod p */
582*2139Sjp161948 int	BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
583*2139Sjp161948 	const BIGNUM *p, BN_CTX *ctx); /* r = (a / b) mod p */
584*2139Sjp161948 int	BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
585*2139Sjp161948 	const BIGNUM *p, BN_CTX *ctx); /* r = (a ^ b) mod p */
586*2139Sjp161948 int	BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
587*2139Sjp161948 	BN_CTX *ctx); /* r = sqrt(a) mod p */
588*2139Sjp161948 int	BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
589*2139Sjp161948 	BN_CTX *ctx); /* r^2 + r = a mod p */
590*2139Sjp161948 #define BN_GF2m_cmp(a, b) BN_ucmp((a), (b))
591*2139Sjp161948 /* Some functions allow for representation of the irreducible polynomials
592*2139Sjp161948  * as an unsigned int[], say p.  The irreducible f(t) is then of the form:
593*2139Sjp161948  *     t^p[0] + t^p[1] + ... + t^p[k]
594*2139Sjp161948  * where m = p[0] > p[1] > ... > p[k] = 0.
595*2139Sjp161948  */
596*2139Sjp161948 int	BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[]);
597*2139Sjp161948 	/* r = a mod p */
598*2139Sjp161948 int	BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
599*2139Sjp161948 	const unsigned int p[], BN_CTX *ctx); /* r = (a * b) mod p */
600*2139Sjp161948 int	BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[],
601*2139Sjp161948 	BN_CTX *ctx); /* r = (a * a) mod p */
602*2139Sjp161948 int	BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *b, const unsigned int p[],
603*2139Sjp161948 	BN_CTX *ctx); /* r = (1 / b) mod p */
604*2139Sjp161948 int	BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
605*2139Sjp161948 	const unsigned int p[], BN_CTX *ctx); /* r = (a / b) mod p */
606*2139Sjp161948 int	BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
607*2139Sjp161948 	const unsigned int p[], BN_CTX *ctx); /* r = (a ^ b) mod p */
608*2139Sjp161948 int	BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a,
609*2139Sjp161948 	const unsigned int p[], BN_CTX *ctx); /* r = sqrt(a) mod p */
610*2139Sjp161948 int	BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a,
611*2139Sjp161948 	const unsigned int p[], BN_CTX *ctx); /* r^2 + r = a mod p */
612*2139Sjp161948 int	BN_GF2m_poly2arr(const BIGNUM *a, unsigned int p[], int max);
613*2139Sjp161948 int	BN_GF2m_arr2poly(const unsigned int p[], BIGNUM *a);
614*2139Sjp161948 
615*2139Sjp161948 /* faster mod functions for the 'NIST primes'
616*2139Sjp161948  * 0 <= a < p^2 */
617*2139Sjp161948 int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
618*2139Sjp161948 int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
619*2139Sjp161948 int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
620*2139Sjp161948 int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
621*2139Sjp161948 int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
622*2139Sjp161948 
623*2139Sjp161948 const BIGNUM *BN_get0_nist_prime_192(void);
624*2139Sjp161948 const BIGNUM *BN_get0_nist_prime_224(void);
625*2139Sjp161948 const BIGNUM *BN_get0_nist_prime_256(void);
626*2139Sjp161948 const BIGNUM *BN_get0_nist_prime_384(void);
627*2139Sjp161948 const BIGNUM *BN_get0_nist_prime_521(void);
628*2139Sjp161948 
6290Sstevel@tonic-gate /* library internal functions */
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate #define bn_expand(a,bits) ((((((bits+BN_BITS2-1))/BN_BITS2)) <= (a)->dmax)?\
632*2139Sjp161948 	(a):bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2))
6330Sstevel@tonic-gate #define bn_wexpand(a,words) (((words) <= (a)->dmax)?(a):bn_expand2((a),(words)))
6340Sstevel@tonic-gate BIGNUM *bn_expand2(BIGNUM *a, int words);
635*2139Sjp161948 #ifndef OPENSSL_NO_DEPRECATED
636*2139Sjp161948 BIGNUM *bn_dup_expand(const BIGNUM *a, int words); /* unused */
637*2139Sjp161948 #endif
638*2139Sjp161948 
639*2139Sjp161948 /* Bignum consistency macros
640*2139Sjp161948  * There is one "API" macro, bn_fix_top(), for stripping leading zeroes from
641*2139Sjp161948  * bignum data after direct manipulations on the data. There is also an
642*2139Sjp161948  * "internal" macro, bn_check_top(), for verifying that there are no leading
643*2139Sjp161948  * zeroes. Unfortunately, some auditing is required due to the fact that
644*2139Sjp161948  * bn_fix_top() has become an overabused duct-tape because bignum data is
645*2139Sjp161948  * occasionally passed around in an inconsistent state. So the following
646*2139Sjp161948  * changes have been made to sort this out;
647*2139Sjp161948  * - bn_fix_top()s implementation has been moved to bn_correct_top()
648*2139Sjp161948  * - if BN_DEBUG isn't defined, bn_fix_top() maps to bn_correct_top(), and
649*2139Sjp161948  *   bn_check_top() is as before.
650*2139Sjp161948  * - if BN_DEBUG *is* defined;
651*2139Sjp161948  *   - bn_check_top() tries to pollute unused words even if the bignum 'top' is
652*2139Sjp161948  *     consistent. (ed: only if BN_DEBUG_RAND is defined)
653*2139Sjp161948  *   - bn_fix_top() maps to bn_check_top() rather than "fixing" anything.
654*2139Sjp161948  * The idea is to have debug builds flag up inconsistent bignums when they
655*2139Sjp161948  * occur. If that occurs in a bn_fix_top(), we examine the code in question; if
656*2139Sjp161948  * the use of bn_fix_top() was appropriate (ie. it follows directly after code
657*2139Sjp161948  * that manipulates the bignum) it is converted to bn_correct_top(), and if it
658*2139Sjp161948  * was not appropriate, we convert it permanently to bn_check_top() and track
659*2139Sjp161948  * down the cause of the bug. Eventually, no internal code should be using the
660*2139Sjp161948  * bn_fix_top() macro. External applications and libraries should try this with
661*2139Sjp161948  * their own code too, both in terms of building against the openssl headers
662*2139Sjp161948  * with BN_DEBUG defined *and* linking with a version of OpenSSL built with it
663*2139Sjp161948  * defined. This not only improves external code, it provides more test
664*2139Sjp161948  * coverage for openssl's own code.
665*2139Sjp161948  */
666*2139Sjp161948 
667*2139Sjp161948 #ifdef BN_DEBUG
668*2139Sjp161948 
669*2139Sjp161948 /* We only need assert() when debugging */
670*2139Sjp161948 #include <assert.h>
6710Sstevel@tonic-gate 
672*2139Sjp161948 #ifdef BN_DEBUG_RAND
673*2139Sjp161948 /* To avoid "make update" cvs wars due to BN_DEBUG, use some tricks */
674*2139Sjp161948 #ifndef RAND_pseudo_bytes
675*2139Sjp161948 int RAND_pseudo_bytes(unsigned char *buf,int num);
676*2139Sjp161948 #define BN_DEBUG_TRIX
677*2139Sjp161948 #endif
678*2139Sjp161948 #define bn_pollute(a) \
679*2139Sjp161948 	do { \
680*2139Sjp161948 		const BIGNUM *_bnum1 = (a); \
681*2139Sjp161948 		if(_bnum1->top < _bnum1->dmax) { \
682*2139Sjp161948 			unsigned char _tmp_char; \
683*2139Sjp161948 			/* We cast away const without the compiler knowing, any \
684*2139Sjp161948 			 * *genuinely* constant variables that aren't mutable \
685*2139Sjp161948 			 * wouldn't be constructed with top!=dmax. */ \
686*2139Sjp161948 			BN_ULONG *_not_const; \
687*2139Sjp161948 			memcpy(&_not_const, &_bnum1->d, sizeof(BN_ULONG*)); \
688*2139Sjp161948 			RAND_pseudo_bytes(&_tmp_char, 1); \
689*2139Sjp161948 			memset((unsigned char *)(_not_const + _bnum1->top), _tmp_char, \
690*2139Sjp161948 				(_bnum1->dmax - _bnum1->top) * sizeof(BN_ULONG)); \
691*2139Sjp161948 		} \
692*2139Sjp161948 	} while(0)
693*2139Sjp161948 #ifdef BN_DEBUG_TRIX
694*2139Sjp161948 #undef RAND_pseudo_bytes
695*2139Sjp161948 #endif
696*2139Sjp161948 #else
697*2139Sjp161948 #define bn_pollute(a)
698*2139Sjp161948 #endif
699*2139Sjp161948 #define bn_check_top(a) \
700*2139Sjp161948 	do { \
701*2139Sjp161948 		const BIGNUM *_bnum2 = (a); \
702*2139Sjp161948 		if (_bnum2 != NULL) { \
703*2139Sjp161948 			assert((_bnum2->top == 0) || \
704*2139Sjp161948 				(_bnum2->d[_bnum2->top - 1] != 0)); \
705*2139Sjp161948 			bn_pollute(_bnum2); \
706*2139Sjp161948 		} \
707*2139Sjp161948 	} while(0)
708*2139Sjp161948 
709*2139Sjp161948 #define bn_fix_top(a)		bn_check_top(a)
710*2139Sjp161948 
711*2139Sjp161948 #else /* !BN_DEBUG */
712*2139Sjp161948 
713*2139Sjp161948 #define bn_pollute(a)
714*2139Sjp161948 #define bn_check_top(a)
715*2139Sjp161948 #define bn_fix_top(a)		bn_correct_top(a)
716*2139Sjp161948 
717*2139Sjp161948 #endif
718*2139Sjp161948 
719*2139Sjp161948 #define bn_correct_top(a) \
7200Sstevel@tonic-gate         { \
7210Sstevel@tonic-gate         BN_ULONG *ftl; \
7220Sstevel@tonic-gate 	if ((a)->top > 0) \
7230Sstevel@tonic-gate 		{ \
7240Sstevel@tonic-gate 		for (ftl= &((a)->d[(a)->top-1]); (a)->top > 0; (a)->top--) \
7250Sstevel@tonic-gate 		if (*(ftl--)) break; \
7260Sstevel@tonic-gate 		} \
727*2139Sjp161948 	bn_pollute(a); \
7280Sstevel@tonic-gate 	}
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
7310Sstevel@tonic-gate BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
7320Sstevel@tonic-gate void     bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num);
7330Sstevel@tonic-gate BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
7340Sstevel@tonic-gate BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,int num);
7350Sstevel@tonic-gate BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,int num);
7360Sstevel@tonic-gate 
737*2139Sjp161948 /* Primes from RFC 2409 */
738*2139Sjp161948 BIGNUM *get_rfc2409_prime_768(BIGNUM *bn);
739*2139Sjp161948 BIGNUM *get_rfc2409_prime_1024(BIGNUM *bn);
740*2139Sjp161948 
741*2139Sjp161948 /* Primes from RFC 3526 */
742*2139Sjp161948 BIGNUM *get_rfc3526_prime_1536(BIGNUM *bn);
743*2139Sjp161948 BIGNUM *get_rfc3526_prime_2048(BIGNUM *bn);
744*2139Sjp161948 BIGNUM *get_rfc3526_prime_3072(BIGNUM *bn);
745*2139Sjp161948 BIGNUM *get_rfc3526_prime_4096(BIGNUM *bn);
746*2139Sjp161948 BIGNUM *get_rfc3526_prime_6144(BIGNUM *bn);
747*2139Sjp161948 BIGNUM *get_rfc3526_prime_8192(BIGNUM *bn);
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate int BN_bntest_rand(BIGNUM *rnd, int bits, int top,int bottom);
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate /* BEGIN ERROR CODES */
7520Sstevel@tonic-gate /* The following lines are auto generated by the script mkerr.pl. Any changes
7530Sstevel@tonic-gate  * made after this point may be overwritten when the script is next run.
7540Sstevel@tonic-gate  */
7550Sstevel@tonic-gate void ERR_load_BN_strings(void);
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate /* Error codes for the BN functions. */
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate /* Function codes. */
760*2139Sjp161948 #define BN_F_BNRAND					 127
761*2139Sjp161948 #define BN_F_BN_BLINDING_CONVERT_EX			 100
762*2139Sjp161948 #define BN_F_BN_BLINDING_CREATE_PARAM			 128
763*2139Sjp161948 #define BN_F_BN_BLINDING_INVERT_EX			 101
7640Sstevel@tonic-gate #define BN_F_BN_BLINDING_NEW				 102
7650Sstevel@tonic-gate #define BN_F_BN_BLINDING_UPDATE				 103
7660Sstevel@tonic-gate #define BN_F_BN_BN2DEC					 104
7670Sstevel@tonic-gate #define BN_F_BN_BN2HEX					 105
7680Sstevel@tonic-gate #define BN_F_BN_CTX_GET					 116
7690Sstevel@tonic-gate #define BN_F_BN_CTX_NEW					 106
770*2139Sjp161948 #define BN_F_BN_CTX_START				 129
7710Sstevel@tonic-gate #define BN_F_BN_DIV					 107
772*2139Sjp161948 #define BN_F_BN_DIV_RECP				 130
773*2139Sjp161948 #define BN_F_BN_EXP					 123
7740Sstevel@tonic-gate #define BN_F_BN_EXPAND2					 108
7750Sstevel@tonic-gate #define BN_F_BN_EXPAND_INTERNAL				 120
776*2139Sjp161948 #define BN_F_BN_GF2M_MOD				 131
777*2139Sjp161948 #define BN_F_BN_GF2M_MOD_EXP				 132
778*2139Sjp161948 #define BN_F_BN_GF2M_MOD_MUL				 133
779*2139Sjp161948 #define BN_F_BN_GF2M_MOD_SOLVE_QUAD			 134
780*2139Sjp161948 #define BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR			 135
781*2139Sjp161948 #define BN_F_BN_GF2M_MOD_SQR				 136
782*2139Sjp161948 #define BN_F_BN_GF2M_MOD_SQRT				 137
7830Sstevel@tonic-gate #define BN_F_BN_MOD_EXP2_MONT				 118
7840Sstevel@tonic-gate #define BN_F_BN_MOD_EXP_MONT				 109
785*2139Sjp161948 #define BN_F_BN_MOD_EXP_MONT_CONSTTIME			 124
7860Sstevel@tonic-gate #define BN_F_BN_MOD_EXP_MONT_WORD			 117
787*2139Sjp161948 #define BN_F_BN_MOD_EXP_RECP				 125
788*2139Sjp161948 #define BN_F_BN_MOD_EXP_SIMPLE				 126
7890Sstevel@tonic-gate #define BN_F_BN_MOD_INVERSE				 110
7900Sstevel@tonic-gate #define BN_F_BN_MOD_LSHIFT_QUICK			 119
7910Sstevel@tonic-gate #define BN_F_BN_MOD_MUL_RECIPROCAL			 111
7920Sstevel@tonic-gate #define BN_F_BN_MOD_SQRT				 121
7930Sstevel@tonic-gate #define BN_F_BN_MPI2BN					 112
7940Sstevel@tonic-gate #define BN_F_BN_NEW					 113
7950Sstevel@tonic-gate #define BN_F_BN_RAND					 114
7960Sstevel@tonic-gate #define BN_F_BN_RAND_RANGE				 122
7970Sstevel@tonic-gate #define BN_F_BN_USUB					 115
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate /* Reason codes. */
8000Sstevel@tonic-gate #define BN_R_ARG2_LT_ARG3				 100
8010Sstevel@tonic-gate #define BN_R_BAD_RECIPROCAL				 101
8020Sstevel@tonic-gate #define BN_R_BIGNUM_TOO_LONG				 114
8030Sstevel@tonic-gate #define BN_R_CALLED_WITH_EVEN_MODULUS			 102
8040Sstevel@tonic-gate #define BN_R_DIV_BY_ZERO				 103
8050Sstevel@tonic-gate #define BN_R_ENCODING_ERROR				 104
8060Sstevel@tonic-gate #define BN_R_EXPAND_ON_STATIC_BIGNUM_DATA		 105
8070Sstevel@tonic-gate #define BN_R_INPUT_NOT_REDUCED				 110
8080Sstevel@tonic-gate #define BN_R_INVALID_LENGTH				 106
8090Sstevel@tonic-gate #define BN_R_INVALID_RANGE				 115
8100Sstevel@tonic-gate #define BN_R_NOT_A_SQUARE				 111
8110Sstevel@tonic-gate #define BN_R_NOT_INITIALIZED				 107
8120Sstevel@tonic-gate #define BN_R_NO_INVERSE					 108
813*2139Sjp161948 #define BN_R_NO_SOLUTION				 116
8140Sstevel@tonic-gate #define BN_R_P_IS_NOT_PRIME				 112
8150Sstevel@tonic-gate #define BN_R_TOO_MANY_ITERATIONS			 113
8160Sstevel@tonic-gate #define BN_R_TOO_MANY_TEMPORARY_VARIABLES		 109
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate #ifdef  __cplusplus
8190Sstevel@tonic-gate }
8200Sstevel@tonic-gate #endif
8210Sstevel@tonic-gate #endif
822