xref: /dflybsd-src/crypto/libressl/include/openssl/bn.h (revision 961e30ea7dc61d1112b778ea4981eac68129fb86)
1*de0e0e4dSAntonio Huete Jimenez /* $OpenBSD: bn.h,v 1.55 2022/07/12 14:42:48 kn Exp $ */
2f5b1c8a1SJohn Marino /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3f5b1c8a1SJohn Marino  * All rights reserved.
4f5b1c8a1SJohn Marino  *
5f5b1c8a1SJohn Marino  * This package is an SSL implementation written
6f5b1c8a1SJohn Marino  * by Eric Young (eay@cryptsoft.com).
7f5b1c8a1SJohn Marino  * The implementation was written so as to conform with Netscapes SSL.
8f5b1c8a1SJohn Marino  *
9f5b1c8a1SJohn Marino  * This library is free for commercial and non-commercial use as long as
10f5b1c8a1SJohn Marino  * the following conditions are aheared to.  The following conditions
11f5b1c8a1SJohn Marino  * apply to all code found in this distribution, be it the RC4, RSA,
12f5b1c8a1SJohn Marino  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13f5b1c8a1SJohn Marino  * included with this distribution is covered by the same copyright terms
14f5b1c8a1SJohn Marino  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15f5b1c8a1SJohn Marino  *
16f5b1c8a1SJohn Marino  * Copyright remains Eric Young's, and as such any Copyright notices in
17f5b1c8a1SJohn Marino  * the code are not to be removed.
18f5b1c8a1SJohn Marino  * If this package is used in a product, Eric Young should be given attribution
19f5b1c8a1SJohn Marino  * as the author of the parts of the library used.
20f5b1c8a1SJohn Marino  * This can be in the form of a textual message at program startup or
21f5b1c8a1SJohn Marino  * in documentation (online or textual) provided with the package.
22f5b1c8a1SJohn Marino  *
23f5b1c8a1SJohn Marino  * Redistribution and use in source and binary forms, with or without
24f5b1c8a1SJohn Marino  * modification, are permitted provided that the following conditions
25f5b1c8a1SJohn Marino  * are met:
26f5b1c8a1SJohn Marino  * 1. Redistributions of source code must retain the copyright
27f5b1c8a1SJohn Marino  *    notice, this list of conditions and the following disclaimer.
28f5b1c8a1SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
29f5b1c8a1SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
30f5b1c8a1SJohn Marino  *    documentation and/or other materials provided with the distribution.
31f5b1c8a1SJohn Marino  * 3. All advertising materials mentioning features or use of this software
32f5b1c8a1SJohn Marino  *    must display the following acknowledgement:
33f5b1c8a1SJohn Marino  *    "This product includes cryptographic software written by
34f5b1c8a1SJohn Marino  *     Eric Young (eay@cryptsoft.com)"
35f5b1c8a1SJohn Marino  *    The word 'cryptographic' can be left out if the rouines from the library
36f5b1c8a1SJohn Marino  *    being used are not cryptographic related :-).
37f5b1c8a1SJohn Marino  * 4. If you include any Windows specific code (or a derivative thereof) from
38f5b1c8a1SJohn Marino  *    the apps directory (application code) you must include an acknowledgement:
39f5b1c8a1SJohn Marino  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40f5b1c8a1SJohn Marino  *
41f5b1c8a1SJohn Marino  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42f5b1c8a1SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43f5b1c8a1SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44f5b1c8a1SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45f5b1c8a1SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46f5b1c8a1SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47f5b1c8a1SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48f5b1c8a1SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49f5b1c8a1SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50f5b1c8a1SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51f5b1c8a1SJohn Marino  * SUCH DAMAGE.
52f5b1c8a1SJohn Marino  *
53f5b1c8a1SJohn Marino  * The licence and distribution terms for any publically available version or
54f5b1c8a1SJohn Marino  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55f5b1c8a1SJohn Marino  * copied and put under another distribution licence
56f5b1c8a1SJohn Marino  * [including the GNU Public Licence.]
57f5b1c8a1SJohn Marino  */
58f5b1c8a1SJohn Marino /* ====================================================================
59f5b1c8a1SJohn Marino  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
60f5b1c8a1SJohn Marino  *
61f5b1c8a1SJohn Marino  * Redistribution and use in source and binary forms, with or without
62f5b1c8a1SJohn Marino  * modification, are permitted provided that the following conditions
63f5b1c8a1SJohn Marino  * are met:
64f5b1c8a1SJohn Marino  *
65f5b1c8a1SJohn Marino  * 1. Redistributions of source code must retain the above copyright
66f5b1c8a1SJohn Marino  *    notice, this list of conditions and the following disclaimer.
67f5b1c8a1SJohn Marino  *
68f5b1c8a1SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
69f5b1c8a1SJohn Marino  *    notice, this list of conditions and the following disclaimer in
70f5b1c8a1SJohn Marino  *    the documentation and/or other materials provided with the
71f5b1c8a1SJohn Marino  *    distribution.
72f5b1c8a1SJohn Marino  *
73f5b1c8a1SJohn Marino  * 3. All advertising materials mentioning features or use of this
74f5b1c8a1SJohn Marino  *    software must display the following acknowledgment:
75f5b1c8a1SJohn Marino  *    "This product includes software developed by the OpenSSL Project
76f5b1c8a1SJohn Marino  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77f5b1c8a1SJohn Marino  *
78f5b1c8a1SJohn Marino  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79f5b1c8a1SJohn Marino  *    endorse or promote products derived from this software without
80f5b1c8a1SJohn Marino  *    prior written permission. For written permission, please contact
81f5b1c8a1SJohn Marino  *    openssl-core@openssl.org.
82f5b1c8a1SJohn Marino  *
83f5b1c8a1SJohn Marino  * 5. Products derived from this software may not be called "OpenSSL"
84f5b1c8a1SJohn Marino  *    nor may "OpenSSL" appear in their names without prior written
85f5b1c8a1SJohn Marino  *    permission of the OpenSSL Project.
86f5b1c8a1SJohn Marino  *
87f5b1c8a1SJohn Marino  * 6. Redistributions of any form whatsoever must retain the following
88f5b1c8a1SJohn Marino  *    acknowledgment:
89f5b1c8a1SJohn Marino  *    "This product includes software developed by the OpenSSL Project
90f5b1c8a1SJohn Marino  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91f5b1c8a1SJohn Marino  *
92f5b1c8a1SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93f5b1c8a1SJohn Marino  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94f5b1c8a1SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95f5b1c8a1SJohn Marino  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96f5b1c8a1SJohn Marino  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97f5b1c8a1SJohn Marino  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98f5b1c8a1SJohn Marino  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99f5b1c8a1SJohn Marino  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100f5b1c8a1SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101f5b1c8a1SJohn Marino  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102f5b1c8a1SJohn Marino  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103f5b1c8a1SJohn Marino  * OF THE POSSIBILITY OF SUCH DAMAGE.
104f5b1c8a1SJohn Marino  * ====================================================================
105f5b1c8a1SJohn Marino  *
106f5b1c8a1SJohn Marino  * This product includes cryptographic software written by Eric Young
107f5b1c8a1SJohn Marino  * (eay@cryptsoft.com).  This product includes software written by Tim
108f5b1c8a1SJohn Marino  * Hudson (tjh@cryptsoft.com).
109f5b1c8a1SJohn Marino  *
110f5b1c8a1SJohn Marino  */
111f5b1c8a1SJohn Marino /* ====================================================================
112f5b1c8a1SJohn Marino  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113f5b1c8a1SJohn Marino  *
114f5b1c8a1SJohn Marino  * Portions of the attached software ("Contribution") are developed by
115f5b1c8a1SJohn Marino  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
116f5b1c8a1SJohn Marino  *
117f5b1c8a1SJohn Marino  * The Contribution is licensed pursuant to the Eric Young open source
118f5b1c8a1SJohn Marino  * license provided above.
119f5b1c8a1SJohn Marino  *
120f5b1c8a1SJohn Marino  * The binary polynomial arithmetic software is originally written by
121f5b1c8a1SJohn Marino  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
122f5b1c8a1SJohn Marino  *
123f5b1c8a1SJohn Marino  */
124f5b1c8a1SJohn Marino 
125f5b1c8a1SJohn Marino #ifndef HEADER_BN_H
126f5b1c8a1SJohn Marino #define HEADER_BN_H
127f5b1c8a1SJohn Marino 
128f5b1c8a1SJohn Marino #include <stdio.h>
129f5b1c8a1SJohn Marino #include <stdlib.h>
130f5b1c8a1SJohn Marino 
131f5b1c8a1SJohn Marino #include <openssl/opensslconf.h>
132f5b1c8a1SJohn Marino 
133f5b1c8a1SJohn Marino #include <openssl/ossl_typ.h>
134f5b1c8a1SJohn Marino #include <openssl/crypto.h>
135f5b1c8a1SJohn Marino #include <openssl/bio.h>
136f5b1c8a1SJohn Marino 
137f5b1c8a1SJohn Marino #ifdef  __cplusplus
138f5b1c8a1SJohn Marino extern "C" {
139f5b1c8a1SJohn Marino #endif
140f5b1c8a1SJohn Marino 
141f5b1c8a1SJohn Marino /* These preprocessor symbols control various aspects of the bignum headers and
142f5b1c8a1SJohn Marino  * library code. They're not defined by any "normal" configuration, as they are
143f5b1c8a1SJohn Marino  * intended for development and testing purposes. NB: defining all three can be
144f5b1c8a1SJohn Marino  * useful for debugging application code as well as openssl itself.
145f5b1c8a1SJohn Marino  *
146f5b1c8a1SJohn Marino  * BN_DEBUG - turn on various debugging alterations to the bignum code
147f5b1c8a1SJohn Marino  * BN_DEBUG_RAND - uses random poisoning of unused words to trip up
148f5b1c8a1SJohn Marino  * mismanagement of bignum internals. You must also define BN_DEBUG.
149f5b1c8a1SJohn Marino  */
150f5b1c8a1SJohn Marino /* #define BN_DEBUG */
151f5b1c8a1SJohn Marino /* #define BN_DEBUG_RAND */
152f5b1c8a1SJohn Marino 
153f5b1c8a1SJohn Marino #ifndef OPENSSL_SMALL_FOOTPRINT
154f5b1c8a1SJohn Marino #define BN_MUL_COMBA
155f5b1c8a1SJohn Marino #define BN_SQR_COMBA
156f5b1c8a1SJohn Marino #define BN_RECURSION
157f5b1c8a1SJohn Marino #endif
158f5b1c8a1SJohn Marino 
159f5b1c8a1SJohn Marino /* This next option uses the C libraries (2 word)/(1 word) function.
160f5b1c8a1SJohn Marino  * If it is not defined, I use my C version (which is slower).
161f5b1c8a1SJohn Marino  * The reason for this flag is that when the particular C compiler
162f5b1c8a1SJohn Marino  * library routine is used, and the library is linked with a different
163f5b1c8a1SJohn Marino  * compiler, the library is missing.  This mostly happens when the
164f5b1c8a1SJohn Marino  * library is built with gcc and then linked using normal cc.  This would
165f5b1c8a1SJohn Marino  * be a common occurrence because gcc normally produces code that is
166f5b1c8a1SJohn Marino  * 2 times faster than system compilers for the big number stuff.
167f5b1c8a1SJohn Marino  * For machines with only one compiler (or shared libraries), this should
168f5b1c8a1SJohn Marino  * be on.  Again this in only really a problem on machines
169f5b1c8a1SJohn Marino  * using "long long's", are 32bit, and are not using my assembler code. */
170f5b1c8a1SJohn Marino /* #define BN_DIV2W */
171f5b1c8a1SJohn Marino 
172f5b1c8a1SJohn Marino #ifdef _LP64
173f5b1c8a1SJohn Marino #undef	BN_LLONG
174f5b1c8a1SJohn Marino #define BN_ULONG	unsigned long
175f5b1c8a1SJohn Marino #define BN_LONG		long
176f5b1c8a1SJohn Marino #define BN_BITS		128
177f5b1c8a1SJohn Marino #define BN_BYTES	8
178f5b1c8a1SJohn Marino #define BN_BITS2	64
179f5b1c8a1SJohn Marino #define BN_BITS4	32
180f5b1c8a1SJohn Marino #define BN_MASK2	(0xffffffffffffffffL)
181f5b1c8a1SJohn Marino #define BN_MASK2l	(0xffffffffL)
182f5b1c8a1SJohn Marino #define BN_MASK2h	(0xffffffff00000000L)
183f5b1c8a1SJohn Marino #define BN_MASK2h1	(0xffffffff80000000L)
184f5b1c8a1SJohn Marino #define BN_TBIT		(0x8000000000000000L)
185f5b1c8a1SJohn Marino #define BN_DEC_CONV	(10000000000000000000UL)
186f5b1c8a1SJohn Marino #define BN_DEC_FMT1	"%lu"
187f5b1c8a1SJohn Marino #define BN_DEC_FMT2	"%019lu"
188f5b1c8a1SJohn Marino #define BN_DEC_NUM	19
189f5b1c8a1SJohn Marino #define BN_HEX_FMT1	"%lX"
190f5b1c8a1SJohn Marino #define BN_HEX_FMT2	"%016lX"
191f5b1c8a1SJohn Marino #else
192f5b1c8a1SJohn Marino #define BN_ULLONG	unsigned long long
193f5b1c8a1SJohn Marino #define	BN_LLONG
194f5b1c8a1SJohn Marino #define BN_ULONG	unsigned int
195f5b1c8a1SJohn Marino #define BN_LONG		int
196f5b1c8a1SJohn Marino #define BN_BITS		64
197f5b1c8a1SJohn Marino #define BN_BYTES	4
198f5b1c8a1SJohn Marino #define BN_BITS2	32
199f5b1c8a1SJohn Marino #define BN_BITS4	16
200f5b1c8a1SJohn Marino #define BN_MASK		(0xffffffffffffffffLL)
201f5b1c8a1SJohn Marino #define BN_MASK2	(0xffffffffL)
202f5b1c8a1SJohn Marino #define BN_MASK2l	(0xffff)
203f5b1c8a1SJohn Marino #define BN_MASK2h1	(0xffff8000L)
204f5b1c8a1SJohn Marino #define BN_MASK2h	(0xffff0000L)
205f5b1c8a1SJohn Marino #define BN_TBIT		(0x80000000L)
206f5b1c8a1SJohn Marino #define BN_DEC_CONV	(1000000000L)
207f5b1c8a1SJohn Marino #define BN_DEC_FMT1	"%u"
208f5b1c8a1SJohn Marino #define BN_DEC_FMT2	"%09u"
209f5b1c8a1SJohn Marino #define BN_DEC_NUM	9
210f5b1c8a1SJohn Marino #define BN_HEX_FMT1	"%X"
211f5b1c8a1SJohn Marino #define BN_HEX_FMT2	"%08X"
212f5b1c8a1SJohn Marino #endif
213f5b1c8a1SJohn Marino 
214f5b1c8a1SJohn Marino #define BN_FLG_MALLOCED		0x01
215f5b1c8a1SJohn Marino #define BN_FLG_STATIC_DATA	0x02
216f5b1c8a1SJohn Marino #define BN_FLG_CONSTTIME	0x04 /* avoid leaking exponent information through timing,
217f5b1c8a1SJohn Marino                                       * BN_mod_exp_mont() will call BN_mod_exp_mont_consttime,
218f5b1c8a1SJohn Marino                                       * BN_div() will call BN_div_no_branch,
219f5b1c8a1SJohn Marino                                       * BN_mod_inverse() will call BN_mod_inverse_no_branch.
220f5b1c8a1SJohn Marino                                       */
221f5b1c8a1SJohn Marino 
222f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_DEPRECATED
223f5b1c8a1SJohn Marino #define BN_FLG_EXP_CONSTTIME BN_FLG_CONSTTIME /* deprecated name for the flag */
224f5b1c8a1SJohn Marino                                       /* avoid leaking exponent information through timings
225f5b1c8a1SJohn Marino                                       * (BN_mod_exp_mont() will call BN_mod_exp_mont_consttime) */
226f5b1c8a1SJohn Marino #endif
227f5b1c8a1SJohn Marino 
228f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_DEPRECATED
229*de0e0e4dSAntonio Huete Jimenez #define BN_FLG_FREE		0x8000	/* used for debugging */
230f5b1c8a1SJohn Marino #endif
231*de0e0e4dSAntonio Huete Jimenez void BN_set_flags(BIGNUM *b, int n);
232*de0e0e4dSAntonio Huete Jimenez int BN_get_flags(const BIGNUM *b, int n);
233*de0e0e4dSAntonio Huete Jimenez void BN_with_flags(BIGNUM *dest, const BIGNUM *src, int flags);
234f5b1c8a1SJohn Marino 
235*de0e0e4dSAntonio Huete Jimenez /* Values for |top| in BN_rand() */
236*de0e0e4dSAntonio Huete Jimenez #define BN_RAND_TOP_ANY    -1
237*de0e0e4dSAntonio Huete Jimenez #define BN_RAND_TOP_ONE     0
238*de0e0e4dSAntonio Huete Jimenez #define BN_RAND_TOP_TWO     1
239f5b1c8a1SJohn Marino 
240*de0e0e4dSAntonio Huete Jimenez /* Values for |bottom| in BN_rand() */
241*de0e0e4dSAntonio Huete Jimenez #define BN_RAND_BOTTOM_ANY  0
242*de0e0e4dSAntonio Huete Jimenez #define BN_RAND_BOTTOM_ODD  1
24372c33676SMaxim Ag 
24472c33676SMaxim Ag BN_GENCB *BN_GENCB_new(void);
24572c33676SMaxim Ag void BN_GENCB_free(BN_GENCB *cb);
24672c33676SMaxim Ag 
247f5b1c8a1SJohn Marino /* Wrapper function to make using BN_GENCB easier,  */
248f5b1c8a1SJohn Marino int BN_GENCB_call(BN_GENCB *cb, int a, int b);
249*de0e0e4dSAntonio Huete Jimenez 
250*de0e0e4dSAntonio Huete Jimenez /* Populate a BN_GENCB structure with an "old"-style callback */
251*de0e0e4dSAntonio Huete Jimenez void BN_GENCB_set_old(BN_GENCB *gencb, void (*callback)(int, int, void *),
252*de0e0e4dSAntonio Huete Jimenez     void *cb_arg);
253*de0e0e4dSAntonio Huete Jimenez 
254*de0e0e4dSAntonio Huete Jimenez /* Populate a BN_GENCB structure with a "new"-style callback */
255*de0e0e4dSAntonio Huete Jimenez void BN_GENCB_set(BN_GENCB *gencb, int (*callback)(int, int, BN_GENCB *),
256*de0e0e4dSAntonio Huete Jimenez     void *cb_arg);
257*de0e0e4dSAntonio Huete Jimenez 
258*de0e0e4dSAntonio Huete Jimenez void *BN_GENCB_get_arg(BN_GENCB *cb);
259f5b1c8a1SJohn Marino 
260f5b1c8a1SJohn Marino #define BN_prime_checks 0 /* default: select number of iterations
261f5b1c8a1SJohn Marino 			     based on the size of the number */
262f5b1c8a1SJohn Marino 
263cca6fc52SDaniel Fojt /*
264cca6fc52SDaniel Fojt  * BN_prime_checks_for_size() returns the number of Miller-Rabin
265cca6fc52SDaniel Fojt  * iterations that will be done for checking that a random number
266cca6fc52SDaniel Fojt  * is probably prime.  The error rate for accepting a composite
267cca6fc52SDaniel Fojt  * number as prime depends on the size of the prime |b|.  The error
268cca6fc52SDaniel Fojt  * rates used are for calculating an RSA key with 2 primes, and so
269cca6fc52SDaniel Fojt  * the level is what you would expect for a key of double the size
270cca6fc52SDaniel Fojt  * of the prime.
271cca6fc52SDaniel Fojt  *
272cca6fc52SDaniel Fojt  * This table is generated using the algorithm of FIPS PUB 186-4
273cca6fc52SDaniel Fojt  * Digital Signature Standard (DSS), section F.1, page 117.
274cca6fc52SDaniel Fojt  * (https://dx.doi.org/10.6028/NIST.FIPS.186-4)
275cca6fc52SDaniel Fojt  *
276cca6fc52SDaniel Fojt  * The following magma script was used to generate the output:
277cca6fc52SDaniel Fojt  * securitybits:=125;
278cca6fc52SDaniel Fojt  * k:=1024;
279cca6fc52SDaniel Fojt  * for t:=1 to 65 do
280cca6fc52SDaniel Fojt  *   for M:=3 to Floor(2*Sqrt(k-1)-1) do
281cca6fc52SDaniel Fojt  *     S:=0;
282cca6fc52SDaniel Fojt  *     // Sum over m
283cca6fc52SDaniel Fojt  *     for m:=3 to M do
284cca6fc52SDaniel Fojt  *       s:=0;
285cca6fc52SDaniel Fojt  *       // Sum over j
286cca6fc52SDaniel Fojt  *       for j:=2 to m do
287cca6fc52SDaniel Fojt  *         s+:=(RealField(32)!2)^-(j+(k-1)/j);
288cca6fc52SDaniel Fojt  *       end for;
289cca6fc52SDaniel Fojt  *       S+:=2^(m-(m-1)*t)*s;
290cca6fc52SDaniel Fojt  *     end for;
291cca6fc52SDaniel Fojt  *     A:=2^(k-2-M*t);
292cca6fc52SDaniel Fojt  *     B:=8*(Pi(RealField(32))^2-6)/3*2^(k-2)*S;
293cca6fc52SDaniel Fojt  *     pkt:=2.00743*Log(2)*k*2^-k*(A+B);
294cca6fc52SDaniel Fojt  *     seclevel:=Floor(-Log(2,pkt));
295cca6fc52SDaniel Fojt  *     if seclevel ge securitybits then
296cca6fc52SDaniel Fojt  *       printf "k: %5o, security: %o bits  (t: %o, M: %o)\n",k,seclevel,t,M;
297cca6fc52SDaniel Fojt  *       break;
298cca6fc52SDaniel Fojt  *     end if;
299cca6fc52SDaniel Fojt  *   end for;
300cca6fc52SDaniel Fojt  *   if seclevel ge securitybits then break; end if;
301cca6fc52SDaniel Fojt  * end for;
302cca6fc52SDaniel Fojt  *
303cca6fc52SDaniel Fojt  * It can be run online at:
304cca6fc52SDaniel Fojt  * http://magma.maths.usyd.edu.au/calc
305cca6fc52SDaniel Fojt  *
306cca6fc52SDaniel Fojt  * And will output:
307cca6fc52SDaniel Fojt  * k:  1024, security: 129 bits  (t: 6, M: 23)
308cca6fc52SDaniel Fojt  *
309cca6fc52SDaniel Fojt  * k is the number of bits of the prime, securitybits is the level
310cca6fc52SDaniel Fojt  * we want to reach.
311cca6fc52SDaniel Fojt  *
312cca6fc52SDaniel Fojt  * prime length | RSA key size | # MR tests | security level
313cca6fc52SDaniel Fojt  * -------------+--------------|------------+---------------
314cca6fc52SDaniel Fojt  *  (b) >= 6394 |     >= 12788 |          3 |        256 bit
315cca6fc52SDaniel Fojt  *  (b) >= 3747 |     >=  7494 |          3 |        192 bit
316cca6fc52SDaniel Fojt  *  (b) >= 1345 |     >=  2690 |          4 |        128 bit
317cca6fc52SDaniel Fojt  *  (b) >= 1080 |     >=  2160 |          5 |        128 bit
318cca6fc52SDaniel Fojt  *  (b) >=  852 |     >=  1704 |          5 |        112 bit
319cca6fc52SDaniel Fojt  *  (b) >=  476 |     >=   952 |          5 |         80 bit
320cca6fc52SDaniel Fojt  *  (b) >=  400 |     >=   800 |          6 |         80 bit
321cca6fc52SDaniel Fojt  *  (b) >=  347 |     >=   694 |          7 |         80 bit
322cca6fc52SDaniel Fojt  *  (b) >=  308 |     >=   616 |          8 |         80 bit
323cca6fc52SDaniel Fojt  *  (b) >=   55 |     >=   110 |         27 |         64 bit
324cca6fc52SDaniel Fojt  *  (b) >=    6 |     >=    12 |         34 |         64 bit
325cca6fc52SDaniel Fojt  */
326cca6fc52SDaniel Fojt 
327cca6fc52SDaniel Fojt #define BN_prime_checks_for_size(b) ((b) >= 3747 ?  3 : \
328cca6fc52SDaniel Fojt 				(b) >=  1345 ?  4 : \
329cca6fc52SDaniel Fojt 				(b) >=  476 ?  5 : \
330cca6fc52SDaniel Fojt 				(b) >=  400 ?  6 : \
331cca6fc52SDaniel Fojt 				(b) >=  347 ?  7 : \
332cca6fc52SDaniel Fojt 				(b) >=  308 ?  8 : \
333cca6fc52SDaniel Fojt 				(b) >=  55  ? 27 : \
334cca6fc52SDaniel Fojt 				/* b >= 6 */ 34)
335f5b1c8a1SJohn Marino 
336f5b1c8a1SJohn Marino #define BN_num_bytes(a)	((BN_num_bits(a)+7)/8)
337f5b1c8a1SJohn Marino 
338*de0e0e4dSAntonio Huete Jimenez int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w);
339*de0e0e4dSAntonio Huete Jimenez int BN_is_zero(const BIGNUM *a);
340*de0e0e4dSAntonio Huete Jimenez int BN_is_one(const BIGNUM *a);
341*de0e0e4dSAntonio Huete Jimenez int BN_is_word(const BIGNUM *a, const BN_ULONG w);
342*de0e0e4dSAntonio Huete Jimenez int BN_is_odd(const BIGNUM *a);
343f5b1c8a1SJohn Marino 
344*de0e0e4dSAntonio Huete Jimenez #define BN_one(a)	BN_set_word((a), 1)
345*de0e0e4dSAntonio Huete Jimenez 
346*de0e0e4dSAntonio Huete Jimenez void BN_zero_ex(BIGNUM *a);
347f5b1c8a1SJohn Marino 
348f5b1c8a1SJohn Marino #ifdef OPENSSL_NO_DEPRECATED
349f5b1c8a1SJohn Marino #define BN_zero(a)	BN_zero_ex(a)
350f5b1c8a1SJohn Marino #else
351f5b1c8a1SJohn Marino #define BN_zero(a)	(BN_set_word((a),0))
352f5b1c8a1SJohn Marino #endif
353f5b1c8a1SJohn Marino 
354f5b1c8a1SJohn Marino const BIGNUM *BN_value_one(void);
355f5b1c8a1SJohn Marino char *	BN_options(void);
356f5b1c8a1SJohn Marino BN_CTX *BN_CTX_new(void);
357f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_DEPRECATED
358f5b1c8a1SJohn Marino void	BN_CTX_init(BN_CTX *c);
359f5b1c8a1SJohn Marino #endif
360f5b1c8a1SJohn Marino void	BN_CTX_free(BN_CTX *c);
361f5b1c8a1SJohn Marino void	BN_CTX_start(BN_CTX *ctx);
362f5b1c8a1SJohn Marino BIGNUM *BN_CTX_get(BN_CTX *ctx);
363f5b1c8a1SJohn Marino void	BN_CTX_end(BN_CTX *ctx);
364f5b1c8a1SJohn Marino int     BN_rand(BIGNUM *rnd, int bits, int top, int bottom);
365f5b1c8a1SJohn Marino int     BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom);
366f5b1c8a1SJohn Marino int	BN_rand_range(BIGNUM *rnd, const BIGNUM *range);
367f5b1c8a1SJohn Marino int	BN_pseudo_rand_range(BIGNUM *rnd, const BIGNUM *range);
368f5b1c8a1SJohn Marino int	BN_num_bits(const BIGNUM *a);
369f5b1c8a1SJohn Marino int	BN_num_bits_word(BN_ULONG);
370f5b1c8a1SJohn Marino BIGNUM *BN_new(void);
371f5b1c8a1SJohn Marino void	BN_init(BIGNUM *);
372f5b1c8a1SJohn Marino void	BN_clear_free(BIGNUM *a);
373f5b1c8a1SJohn Marino BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b);
374f5b1c8a1SJohn Marino void	BN_swap(BIGNUM *a, BIGNUM *b);
375f5b1c8a1SJohn Marino BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret);
376f5b1c8a1SJohn Marino int	BN_bn2bin(const BIGNUM *a, unsigned char *to);
377*de0e0e4dSAntonio Huete Jimenez int	BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen);
378*de0e0e4dSAntonio Huete Jimenez BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret);
379*de0e0e4dSAntonio Huete Jimenez int	BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen);
380f5b1c8a1SJohn Marino BIGNUM *BN_mpi2bn(const unsigned char *s, int len, BIGNUM *ret);
381f5b1c8a1SJohn Marino int	BN_bn2mpi(const BIGNUM *a, unsigned char *to);
382f5b1c8a1SJohn Marino int	BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
383f5b1c8a1SJohn Marino int	BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
384f5b1c8a1SJohn Marino int	BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
385f5b1c8a1SJohn Marino int	BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
386f5b1c8a1SJohn Marino int	BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
387f5b1c8a1SJohn Marino int	BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx);
388f5b1c8a1SJohn Marino /** BN_set_negative sets sign of a BIGNUM
389f5b1c8a1SJohn Marino  * \param  b  pointer to the BIGNUM object
390f5b1c8a1SJohn Marino  * \param  n  0 if the BIGNUM b should be positive and a value != 0 otherwise
391f5b1c8a1SJohn Marino  */
392f5b1c8a1SJohn Marino void	BN_set_negative(BIGNUM *b, int n);
393*de0e0e4dSAntonio Huete Jimenez 
394*de0e0e4dSAntonio Huete Jimenez int BN_is_negative(const BIGNUM *b);
395f5b1c8a1SJohn Marino 
39672c33676SMaxim Ag #ifndef LIBRESSL_INTERNAL
397f5b1c8a1SJohn Marino int	BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
398f5b1c8a1SJohn Marino     BN_CTX *ctx);
399f5b1c8a1SJohn Marino #define BN_mod(rem,m,d,ctx) BN_div(NULL,(rem),(m),(d),(ctx))
40072c33676SMaxim Ag #endif
401f5b1c8a1SJohn Marino int	BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx);
402f5b1c8a1SJohn Marino int	BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx);
403f5b1c8a1SJohn Marino int	BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m);
404f5b1c8a1SJohn Marino int	BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx);
405f5b1c8a1SJohn Marino int	BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m);
406f5b1c8a1SJohn Marino int	BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
407f5b1c8a1SJohn Marino     const BIGNUM *m, BN_CTX *ctx);
408f5b1c8a1SJohn Marino int	BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
409f5b1c8a1SJohn Marino int	BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
410f5b1c8a1SJohn Marino int	BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m);
411f5b1c8a1SJohn Marino int	BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m, BN_CTX *ctx);
412f5b1c8a1SJohn Marino int	BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m);
413f5b1c8a1SJohn Marino 
414f5b1c8a1SJohn Marino BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w);
415f5b1c8a1SJohn Marino BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w);
416f5b1c8a1SJohn Marino int	BN_mul_word(BIGNUM *a, BN_ULONG w);
417f5b1c8a1SJohn Marino int	BN_add_word(BIGNUM *a, BN_ULONG w);
418f5b1c8a1SJohn Marino int	BN_sub_word(BIGNUM *a, BN_ULONG w);
419f5b1c8a1SJohn Marino int	BN_set_word(BIGNUM *a, BN_ULONG w);
420f5b1c8a1SJohn Marino BN_ULONG BN_get_word(const BIGNUM *a);
421f5b1c8a1SJohn Marino 
422f5b1c8a1SJohn Marino int	BN_cmp(const BIGNUM *a, const BIGNUM *b);
423f5b1c8a1SJohn Marino void	BN_free(BIGNUM *a);
424f5b1c8a1SJohn Marino int	BN_is_bit_set(const BIGNUM *a, int n);
425f5b1c8a1SJohn Marino int	BN_lshift(BIGNUM *r, const BIGNUM *a, int n);
426f5b1c8a1SJohn Marino int	BN_lshift1(BIGNUM *r, const BIGNUM *a);
427f5b1c8a1SJohn Marino int	BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
428f5b1c8a1SJohn Marino 
42972c33676SMaxim Ag #ifndef LIBRESSL_INTERNAL
430f5b1c8a1SJohn Marino int	BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
431f5b1c8a1SJohn Marino     const BIGNUM *m, BN_CTX *ctx);
432f5b1c8a1SJohn Marino int	BN_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
433f5b1c8a1SJohn Marino     const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
43472c33676SMaxim Ag #endif
435f5b1c8a1SJohn Marino int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
436f5b1c8a1SJohn Marino     const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont);
437f5b1c8a1SJohn Marino int	BN_mod_exp_mont_word(BIGNUM *r, BN_ULONG a, const BIGNUM *p,
438f5b1c8a1SJohn Marino     const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
439f5b1c8a1SJohn Marino int	BN_mod_exp2_mont(BIGNUM *r, const BIGNUM *a1, const BIGNUM *p1,
440f5b1c8a1SJohn Marino     const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m,
441f5b1c8a1SJohn Marino     BN_CTX *ctx, BN_MONT_CTX *m_ctx);
442f5b1c8a1SJohn Marino int	BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
443f5b1c8a1SJohn Marino     const BIGNUM *m, BN_CTX *ctx);
444f5b1c8a1SJohn Marino 
445f5b1c8a1SJohn Marino int	BN_mask_bits(BIGNUM *a, int n);
446f5b1c8a1SJohn Marino int	BN_print_fp(FILE *fp, const BIGNUM *a);
447f5b1c8a1SJohn Marino int	BN_print(BIO *fp, const BIGNUM *a);
448f5b1c8a1SJohn Marino int	BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx);
449f5b1c8a1SJohn Marino int	BN_rshift(BIGNUM *r, const BIGNUM *a, int n);
450f5b1c8a1SJohn Marino int	BN_rshift1(BIGNUM *r, const BIGNUM *a);
451f5b1c8a1SJohn Marino void	BN_clear(BIGNUM *a);
452f5b1c8a1SJohn Marino BIGNUM *BN_dup(const BIGNUM *a);
453f5b1c8a1SJohn Marino int	BN_ucmp(const BIGNUM *a, const BIGNUM *b);
454f5b1c8a1SJohn Marino int	BN_set_bit(BIGNUM *a, int n);
455f5b1c8a1SJohn Marino int	BN_clear_bit(BIGNUM *a, int n);
456f5b1c8a1SJohn Marino char *	BN_bn2hex(const BIGNUM *a);
457f5b1c8a1SJohn Marino char *	BN_bn2dec(const BIGNUM *a);
458f5b1c8a1SJohn Marino int 	BN_hex2bn(BIGNUM **a, const char *str);
459f5b1c8a1SJohn Marino int 	BN_dec2bn(BIGNUM **a, const char *str);
460f5b1c8a1SJohn Marino int	BN_asc2bn(BIGNUM **a, const char *str);
46172c33676SMaxim Ag #ifndef LIBRESSL_INTERNAL
462f5b1c8a1SJohn Marino int	BN_gcd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
46372c33676SMaxim Ag #endif
464f5b1c8a1SJohn Marino int	BN_kronecker(const BIGNUM *a,const BIGNUM *b,BN_CTX *ctx); /* returns -2 for error */
46572c33676SMaxim Ag #ifndef LIBRESSL_INTERNAL
466f5b1c8a1SJohn Marino BIGNUM *BN_mod_inverse(BIGNUM *ret,
467f5b1c8a1SJohn Marino     const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx);
46872c33676SMaxim Ag #endif
469f5b1c8a1SJohn Marino BIGNUM *BN_mod_sqrt(BIGNUM *ret,
470f5b1c8a1SJohn Marino     const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx);
471f5b1c8a1SJohn Marino 
472f5b1c8a1SJohn Marino void	BN_consttime_swap(BN_ULONG swap, BIGNUM *a, BIGNUM *b, int nwords);
473f5b1c8a1SJohn Marino 
474*de0e0e4dSAntonio Huete Jimenez int	BN_security_bits(int L, int N);
475*de0e0e4dSAntonio Huete Jimenez 
476f5b1c8a1SJohn Marino /* Deprecated versions */
477f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_DEPRECATED
478f5b1c8a1SJohn Marino BIGNUM *BN_generate_prime(BIGNUM *ret, int bits, int safe,
479f5b1c8a1SJohn Marino     const BIGNUM *add, const BIGNUM *rem,
480f5b1c8a1SJohn Marino     void (*callback)(int, int, void *), void *cb_arg);
481f5b1c8a1SJohn Marino int	BN_is_prime(const BIGNUM *p, int nchecks,
482f5b1c8a1SJohn Marino     void (*callback)(int, int, void *),
483f5b1c8a1SJohn Marino     BN_CTX *ctx, void *cb_arg);
484f5b1c8a1SJohn Marino int	BN_is_prime_fasttest(const BIGNUM *p, int nchecks,
485f5b1c8a1SJohn Marino     void (*callback)(int, int, void *), BN_CTX *ctx, void *cb_arg,
486f5b1c8a1SJohn Marino     int do_trial_division);
487f5b1c8a1SJohn Marino #endif /* !defined(OPENSSL_NO_DEPRECATED) */
488f5b1c8a1SJohn Marino 
489f5b1c8a1SJohn Marino /* Newer versions */
490f5b1c8a1SJohn Marino int	BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, const BIGNUM *add,
491f5b1c8a1SJohn Marino     const BIGNUM *rem, BN_GENCB *cb);
492f5b1c8a1SJohn Marino int	BN_is_prime_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, BN_GENCB *cb);
493f5b1c8a1SJohn Marino int	BN_is_prime_fasttest_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx,
494f5b1c8a1SJohn Marino     int do_trial_division, BN_GENCB *cb);
495f5b1c8a1SJohn Marino 
496f5b1c8a1SJohn Marino int BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx);
497f5b1c8a1SJohn Marino 
498f5b1c8a1SJohn Marino int BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
499f5b1c8a1SJohn Marino     const BIGNUM *Xp, const BIGNUM *Xp1, const BIGNUM *Xp2,
500f5b1c8a1SJohn Marino     const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb);
501f5b1c8a1SJohn Marino int BN_X931_generate_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
502f5b1c8a1SJohn Marino     BIGNUM *Xp1, BIGNUM *Xp2,
503f5b1c8a1SJohn Marino     const BIGNUM *Xp,
504f5b1c8a1SJohn Marino     const BIGNUM *e, BN_CTX *ctx,
505f5b1c8a1SJohn Marino     BN_GENCB *cb);
506f5b1c8a1SJohn Marino 
507f5b1c8a1SJohn Marino BN_MONT_CTX *BN_MONT_CTX_new(void );
508f5b1c8a1SJohn Marino void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
509f5b1c8a1SJohn Marino int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
510f5b1c8a1SJohn Marino     BN_MONT_CTX *mont, BN_CTX *ctx);
511*de0e0e4dSAntonio Huete Jimenez int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
512*de0e0e4dSAntonio Huete Jimenez     BN_CTX *ctx);
513f5b1c8a1SJohn Marino int BN_from_montgomery(BIGNUM *r, const BIGNUM *a,
514f5b1c8a1SJohn Marino     BN_MONT_CTX *mont, BN_CTX *ctx);
515f5b1c8a1SJohn Marino void BN_MONT_CTX_free(BN_MONT_CTX *mont);
516f5b1c8a1SJohn Marino int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx);
517f5b1c8a1SJohn Marino BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from);
518f5b1c8a1SJohn Marino BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock,
519f5b1c8a1SJohn Marino     const BIGNUM *mod, BN_CTX *ctx);
520f5b1c8a1SJohn Marino 
521f5b1c8a1SJohn Marino /* BN_BLINDING flags */
522f5b1c8a1SJohn Marino #define	BN_BLINDING_NO_UPDATE	0x00000001
523f5b1c8a1SJohn Marino #define	BN_BLINDING_NO_RECREATE	0x00000002
524f5b1c8a1SJohn Marino 
525f5b1c8a1SJohn Marino BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod);
526f5b1c8a1SJohn Marino void BN_BLINDING_free(BN_BLINDING *b);
527f5b1c8a1SJohn Marino int BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx);
528f5b1c8a1SJohn Marino int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
529f5b1c8a1SJohn Marino int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
530f5b1c8a1SJohn Marino int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *);
531f5b1c8a1SJohn Marino int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *);
532f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_DEPRECATED
533f5b1c8a1SJohn Marino unsigned long BN_BLINDING_get_thread_id(const BN_BLINDING *);
534f5b1c8a1SJohn Marino void BN_BLINDING_set_thread_id(BN_BLINDING *, unsigned long);
535f5b1c8a1SJohn Marino #endif
536f5b1c8a1SJohn Marino CRYPTO_THREADID *BN_BLINDING_thread_id(BN_BLINDING *);
537f5b1c8a1SJohn Marino unsigned long BN_BLINDING_get_flags(const BN_BLINDING *);
538f5b1c8a1SJohn Marino void BN_BLINDING_set_flags(BN_BLINDING *, unsigned long);
539f5b1c8a1SJohn Marino BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,
540f5b1c8a1SJohn Marino     const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,
541f5b1c8a1SJohn Marino     int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
542f5b1c8a1SJohn Marino     const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx),
543f5b1c8a1SJohn Marino     BN_MONT_CTX *m_ctx);
544f5b1c8a1SJohn Marino 
545f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_DEPRECATED
546f5b1c8a1SJohn Marino void BN_set_params(int mul, int high, int low, int mont);
547f5b1c8a1SJohn Marino int BN_get_params(int which); /* 0, mul, 1 high, 2 low, 3 mont */
548f5b1c8a1SJohn Marino #endif
549f5b1c8a1SJohn Marino 
550f5b1c8a1SJohn Marino void	BN_RECP_CTX_init(BN_RECP_CTX *recp);
551f5b1c8a1SJohn Marino BN_RECP_CTX *BN_RECP_CTX_new(void);
552f5b1c8a1SJohn Marino void	BN_RECP_CTX_free(BN_RECP_CTX *recp);
553f5b1c8a1SJohn Marino int	BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *rdiv, BN_CTX *ctx);
554f5b1c8a1SJohn Marino int	BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,
555f5b1c8a1SJohn Marino     BN_RECP_CTX *recp, BN_CTX *ctx);
556f5b1c8a1SJohn Marino int	BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
557f5b1c8a1SJohn Marino     const BIGNUM *m, BN_CTX *ctx);
558f5b1c8a1SJohn Marino int	BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
559f5b1c8a1SJohn Marino     BN_RECP_CTX *recp, BN_CTX *ctx);
560f5b1c8a1SJohn Marino 
561f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_EC2M
562f5b1c8a1SJohn Marino 
563f5b1c8a1SJohn Marino /* Functions for arithmetic over binary polynomials represented by BIGNUMs.
564f5b1c8a1SJohn Marino  *
565f5b1c8a1SJohn Marino  * The BIGNUM::neg property of BIGNUMs representing binary polynomials is
566f5b1c8a1SJohn Marino  * ignored.
567f5b1c8a1SJohn Marino  *
568f5b1c8a1SJohn Marino  * Note that input arguments are not const so that their bit arrays can
569f5b1c8a1SJohn Marino  * be expanded to the appropriate size if needed.
570f5b1c8a1SJohn Marino  */
571f5b1c8a1SJohn Marino 
572f5b1c8a1SJohn Marino int	BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); /*r = a + b*/
573f5b1c8a1SJohn Marino #define BN_GF2m_sub(r, a, b) BN_GF2m_add(r, a, b)
574f5b1c8a1SJohn Marino int	BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p); /*r=a mod p*/
575f5b1c8a1SJohn Marino int
576f5b1c8a1SJohn Marino BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
577f5b1c8a1SJohn Marino 	const BIGNUM *p, BN_CTX *ctx); /* r = (a * b) mod p */
578f5b1c8a1SJohn Marino int
579f5b1c8a1SJohn Marino BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
580f5b1c8a1SJohn Marino 	BN_CTX *ctx); /* r = (a * a) mod p */
581f5b1c8a1SJohn Marino int
582f5b1c8a1SJohn Marino BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *b, const BIGNUM *p,
583f5b1c8a1SJohn Marino 	BN_CTX *ctx); /* r = (1 / b) mod p */
584f5b1c8a1SJohn Marino int
585f5b1c8a1SJohn Marino BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
586f5b1c8a1SJohn Marino 	const BIGNUM *p, BN_CTX *ctx); /* r = (a / b) mod p */
587f5b1c8a1SJohn Marino int
588f5b1c8a1SJohn Marino BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
589f5b1c8a1SJohn Marino 	const BIGNUM *p, BN_CTX *ctx); /* r = (a ^ b) mod p */
590f5b1c8a1SJohn Marino int
591f5b1c8a1SJohn Marino BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
592f5b1c8a1SJohn Marino 	BN_CTX *ctx); /* r = sqrt(a) mod p */
593f5b1c8a1SJohn Marino int	BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
594f5b1c8a1SJohn Marino 	BN_CTX *ctx); /* r^2 + r = a mod p */
595f5b1c8a1SJohn Marino #define BN_GF2m_cmp(a, b) BN_ucmp((a), (b))
596f5b1c8a1SJohn Marino /* Some functions allow for representation of the irreducible polynomials
597f5b1c8a1SJohn Marino  * as an unsigned int[], say p.  The irreducible f(t) is then of the form:
598f5b1c8a1SJohn Marino  *     t^p[0] + t^p[1] + ... + t^p[k]
599f5b1c8a1SJohn Marino  * where m = p[0] > p[1] > ... > p[k] = 0.
600f5b1c8a1SJohn Marino  */
601f5b1c8a1SJohn Marino int	BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[]);
602f5b1c8a1SJohn Marino /* r = a mod p */
603f5b1c8a1SJohn Marino int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
604f5b1c8a1SJohn Marino 	const int p[], BN_CTX *ctx); /* r = (a * b) mod p */
605f5b1c8a1SJohn Marino int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[],
606f5b1c8a1SJohn Marino 	BN_CTX *ctx); /* r = (a * a) mod p */
607f5b1c8a1SJohn Marino int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *b, const int p[],
608f5b1c8a1SJohn Marino 	BN_CTX *ctx); /* r = (1 / b) mod p */
609f5b1c8a1SJohn Marino int	BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
610f5b1c8a1SJohn Marino 	const int p[], BN_CTX *ctx); /* r = (a / b) mod p */
611f5b1c8a1SJohn Marino int	BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
612f5b1c8a1SJohn Marino 	const int p[], BN_CTX *ctx); /* r = (a ^ b) mod p */
613f5b1c8a1SJohn Marino int	BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a,
614f5b1c8a1SJohn Marino 	const int p[], BN_CTX *ctx); /* r = sqrt(a) mod p */
615f5b1c8a1SJohn Marino int	BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a,
616f5b1c8a1SJohn Marino 	const int p[], BN_CTX *ctx); /* r^2 + r = a mod p */
617f5b1c8a1SJohn Marino int	BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max);
618f5b1c8a1SJohn Marino int	BN_GF2m_arr2poly(const int p[], BIGNUM *a);
619f5b1c8a1SJohn Marino 
620f5b1c8a1SJohn Marino #endif
621f5b1c8a1SJohn Marino 
622f5b1c8a1SJohn Marino /* faster mod functions for the 'NIST primes'
623f5b1c8a1SJohn Marino  * 0 <= a < p^2 */
624f5b1c8a1SJohn Marino int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
625f5b1c8a1SJohn Marino int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
626f5b1c8a1SJohn Marino int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
627f5b1c8a1SJohn Marino int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
628f5b1c8a1SJohn Marino int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
629f5b1c8a1SJohn Marino 
630f5b1c8a1SJohn Marino const BIGNUM *BN_get0_nist_prime_192(void);
631f5b1c8a1SJohn Marino const BIGNUM *BN_get0_nist_prime_224(void);
632f5b1c8a1SJohn Marino const BIGNUM *BN_get0_nist_prime_256(void);
633f5b1c8a1SJohn Marino const BIGNUM *BN_get0_nist_prime_384(void);
634f5b1c8a1SJohn Marino const BIGNUM *BN_get0_nist_prime_521(void);
635f5b1c8a1SJohn Marino 
636f5b1c8a1SJohn Marino /* Primes from RFC 2409 */
637f5b1c8a1SJohn Marino BIGNUM *get_rfc2409_prime_768(BIGNUM *bn);
638f5b1c8a1SJohn Marino BIGNUM *get_rfc2409_prime_1024(BIGNUM *bn);
63972c33676SMaxim Ag BIGNUM *BN_get_rfc2409_prime_768(BIGNUM *bn);
64072c33676SMaxim Ag BIGNUM *BN_get_rfc2409_prime_1024(BIGNUM *bn);
641f5b1c8a1SJohn Marino 
642f5b1c8a1SJohn Marino /* Primes from RFC 3526 */
643f5b1c8a1SJohn Marino BIGNUM *get_rfc3526_prime_1536(BIGNUM *bn);
644f5b1c8a1SJohn Marino BIGNUM *get_rfc3526_prime_2048(BIGNUM *bn);
645f5b1c8a1SJohn Marino BIGNUM *get_rfc3526_prime_3072(BIGNUM *bn);
646f5b1c8a1SJohn Marino BIGNUM *get_rfc3526_prime_4096(BIGNUM *bn);
647f5b1c8a1SJohn Marino BIGNUM *get_rfc3526_prime_6144(BIGNUM *bn);
648f5b1c8a1SJohn Marino BIGNUM *get_rfc3526_prime_8192(BIGNUM *bn);
64972c33676SMaxim Ag BIGNUM *BN_get_rfc3526_prime_1536(BIGNUM *bn);
65072c33676SMaxim Ag BIGNUM *BN_get_rfc3526_prime_2048(BIGNUM *bn);
65172c33676SMaxim Ag BIGNUM *BN_get_rfc3526_prime_3072(BIGNUM *bn);
65272c33676SMaxim Ag BIGNUM *BN_get_rfc3526_prime_4096(BIGNUM *bn);
65372c33676SMaxim Ag BIGNUM *BN_get_rfc3526_prime_6144(BIGNUM *bn);
65472c33676SMaxim Ag BIGNUM *BN_get_rfc3526_prime_8192(BIGNUM *bn);
655f5b1c8a1SJohn Marino 
656f5b1c8a1SJohn Marino void ERR_load_BN_strings(void);
657f5b1c8a1SJohn Marino 
658f5b1c8a1SJohn Marino /* Error codes for the BN functions. */
659f5b1c8a1SJohn Marino 
660f5b1c8a1SJohn Marino /* Function codes. */
661f5b1c8a1SJohn Marino #define BN_F_BNRAND					 127
662f5b1c8a1SJohn Marino #define BN_F_BN_BLINDING_CONVERT_EX			 100
663f5b1c8a1SJohn Marino #define BN_F_BN_BLINDING_CREATE_PARAM			 128
664f5b1c8a1SJohn Marino #define BN_F_BN_BLINDING_INVERT_EX			 101
665f5b1c8a1SJohn Marino #define BN_F_BN_BLINDING_NEW				 102
666f5b1c8a1SJohn Marino #define BN_F_BN_BLINDING_UPDATE				 103
667f5b1c8a1SJohn Marino #define BN_F_BN_BN2DEC					 104
668f5b1c8a1SJohn Marino #define BN_F_BN_BN2HEX					 105
669f5b1c8a1SJohn Marino #define BN_F_BN_CTX_GET					 116
670f5b1c8a1SJohn Marino #define BN_F_BN_CTX_NEW					 106
671f5b1c8a1SJohn Marino #define BN_F_BN_CTX_START				 129
672f5b1c8a1SJohn Marino #define BN_F_BN_DIV					 107
673f5b1c8a1SJohn Marino #define BN_F_BN_DIV_NO_BRANCH				 138
674f5b1c8a1SJohn Marino #define BN_F_BN_DIV_RECP				 130
675f5b1c8a1SJohn Marino #define BN_F_BN_EXP					 123
676f5b1c8a1SJohn Marino #define BN_F_BN_EXPAND2					 108
677f5b1c8a1SJohn Marino #define BN_F_BN_GENERATE_PRIME_EX			 140
678f5b1c8a1SJohn Marino #define BN_F_BN_EXPAND_INTERNAL				 120
679f5b1c8a1SJohn Marino #define BN_F_BN_GF2M_MOD				 131
680f5b1c8a1SJohn Marino #define BN_F_BN_GF2M_MOD_EXP				 132
681f5b1c8a1SJohn Marino #define BN_F_BN_GF2M_MOD_MUL				 133
682f5b1c8a1SJohn Marino #define BN_F_BN_GF2M_MOD_SOLVE_QUAD			 134
683f5b1c8a1SJohn Marino #define BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR			 135
684f5b1c8a1SJohn Marino #define BN_F_BN_GF2M_MOD_SQR				 136
685f5b1c8a1SJohn Marino #define BN_F_BN_GF2M_MOD_SQRT				 137
686f5b1c8a1SJohn Marino #define BN_F_BN_MOD_EXP2_MONT				 118
687f5b1c8a1SJohn Marino #define BN_F_BN_MOD_EXP_MONT				 109
688f5b1c8a1SJohn Marino #define BN_F_BN_MOD_EXP_MONT_CONSTTIME			 124
689f5b1c8a1SJohn Marino #define BN_F_BN_MOD_EXP_MONT_WORD			 117
690f5b1c8a1SJohn Marino #define BN_F_BN_MOD_EXP_RECP				 125
691f5b1c8a1SJohn Marino #define BN_F_BN_MOD_EXP_SIMPLE				 126
692f5b1c8a1SJohn Marino #define BN_F_BN_MOD_INVERSE				 110
693f5b1c8a1SJohn Marino #define BN_F_BN_MOD_INVERSE_NO_BRANCH			 139
694f5b1c8a1SJohn Marino #define BN_F_BN_MOD_LSHIFT_QUICK			 119
695f5b1c8a1SJohn Marino #define BN_F_BN_MOD_MUL_RECIPROCAL			 111
696f5b1c8a1SJohn Marino #define BN_F_BN_MOD_SQRT				 121
697f5b1c8a1SJohn Marino #define BN_F_BN_MPI2BN					 112
698f5b1c8a1SJohn Marino #define BN_F_BN_NEW					 113
699f5b1c8a1SJohn Marino #define BN_F_BN_RAND					 114
700f5b1c8a1SJohn Marino #define BN_F_BN_RAND_RANGE				 122
701f5b1c8a1SJohn Marino #define BN_F_BN_USUB					 115
702f5b1c8a1SJohn Marino 
703f5b1c8a1SJohn Marino /* Reason codes. */
704f5b1c8a1SJohn Marino #define BN_R_ARG2_LT_ARG3				 100
705f5b1c8a1SJohn Marino #define BN_R_BAD_RECIPROCAL				 101
706f5b1c8a1SJohn Marino #define BN_R_BIGNUM_TOO_LONG				 114
707f5b1c8a1SJohn Marino #define BN_R_BITS_TOO_SMALL				 117
708f5b1c8a1SJohn Marino #define BN_R_CALLED_WITH_EVEN_MODULUS			 102
709f5b1c8a1SJohn Marino #define BN_R_DIV_BY_ZERO				 103
710f5b1c8a1SJohn Marino #define BN_R_ENCODING_ERROR				 104
711f5b1c8a1SJohn Marino #define BN_R_EXPAND_ON_STATIC_BIGNUM_DATA		 105
712f5b1c8a1SJohn Marino #define BN_R_INPUT_NOT_REDUCED				 110
713f5b1c8a1SJohn Marino #define BN_R_INVALID_LENGTH				 106
714f5b1c8a1SJohn Marino #define BN_R_INVALID_RANGE				 115
715f5b1c8a1SJohn Marino #define BN_R_NOT_A_SQUARE				 111
716f5b1c8a1SJohn Marino #define BN_R_NOT_INITIALIZED				 107
717f5b1c8a1SJohn Marino #define BN_R_NO_INVERSE					 108
718f5b1c8a1SJohn Marino #define BN_R_NO_SOLUTION				 116
719f5b1c8a1SJohn Marino #define BN_R_P_IS_NOT_PRIME				 112
720f5b1c8a1SJohn Marino #define BN_R_TOO_MANY_ITERATIONS			 113
721f5b1c8a1SJohn Marino #define BN_R_TOO_MANY_TEMPORARY_VARIABLES		 109
722f5b1c8a1SJohn Marino 
723f5b1c8a1SJohn Marino #ifdef  __cplusplus
724f5b1c8a1SJohn Marino }
725f5b1c8a1SJohn Marino #endif
726f5b1c8a1SJohn Marino #endif
727