RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / include / bcmcrypto / bn.h
blob6e01d8934dd544f87f27f79691a90c7f04d96ee0
1 /*
2 * bn.h: Big Number header file.
4 * Code copied from openssl distribution and
5 * Modified just enough so that compiles and runs standalone
7 * Copyright (C) 2012, Broadcom Corporation. All Rights Reserved.
8 *
9 * Permission to use, copy, modify, and/or distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
16 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
18 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
19 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * $Id: bn.h 241182 2011-02-17 21:50:03Z $
23 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
24 * All rights reserved.
26 * This package is an SSL implementation written
27 * by Eric Young (eay@cryptsoft.com).
28 * The implementation was written so as to conform with Netscapes SSL.
30 * This library is free for commercial and non-commercial use as long as
31 * the following conditions are aheared to. The following conditions
32 * apply to all code found in this distribution, be it the RC4, RSA,
33 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
34 * included with this distribution is covered by the same copyright terms
35 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
37 * Copyright remains Eric Young's, and as such any Copyright notices in
38 * the code are not to be removed.
39 * If this package is used in a product, Eric Young should be given attribution
40 * as the author of the parts of the library used.
41 * This can be in the form of a textual message at program startup or
42 * in documentation (online or textual) provided with the package.
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * "This product includes cryptographic software written by
55 * Eric Young (eay@cryptsoft.com)"
56 * The word 'cryptographic' can be left out if the rouines from the library
57 * being used are not cryptographic related :-).
58 * 4. If you include any Windows specific code (or a derivative thereof) from
59 * the apps directory (application code) you must include an acknowledgement:
60 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
62 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * SUCH DAMAGE.
74 * The licence and distribution terms for any publically available version or
75 * derivative of this code cannot be changed. i.e. this code cannot simply be
76 * copied and put under another distribution licence
77 * [including the GNU Public Licence.]
80 #ifndef HEADER_BN_H
81 #define HEADER_BN_H
83 #define THIRTY_TWO_BIT
84 #define OPENSSL_NO_ASM
86 #ifndef OPENSSL_NO_FP_API
87 #include <stdio.h> /* FILE */
88 #endif
90 #ifdef __cplusplus
91 extern "C" {
92 #endif
94 #ifdef OPENSSL_SYS_VMS
95 #undef BN_LLONG /* experimental, so far... */
96 #endif
98 #define BN_MUL_COMBA
99 #define BN_SQR_COMBA
100 #define BN_RECURSION
102 /* This next option uses the C libraries (2 word)/(1 word) function.
103 * If it is not defined, I use my C version (which is slower).
104 * The reason for this flag is that when the particular C compiler
105 * library routine is used, and the library is linked with a different
106 * compiler, the library is missing. This mostly happens when the
107 * library is built with gcc and then linked using normal cc. This would
108 * be a common occurrence because gcc normally produces code that is
109 * 2 times faster than system compilers for the big number stuff.
110 * For machines with only one compiler (or shared libraries), this should
111 * be on. Again this in only really a problem on machines
112 * using "long long's", are 32bit, and are not using my assembler code.
114 #if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || \
115 defined(OPENSSL_SYS_WIN32) || defined(linux)
116 # ifndef BN_DIV2W
117 # define BN_DIV2W
118 # endif
119 #endif /* OPENSSL_SYS_MSDOS || OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32 || linux */
121 /* assuming long is 64bit - this is the DEC Alpha
122 * unsigned long long is only 64 bits :-(, don't define
123 * BN_LLONG for the DEC Alpha
125 #ifdef SIXTY_FOUR_BIT_LONG
126 #define BN_ULLONG unsigned long long
127 #define BN_ULONG unsigned long
128 #define BN_LONG long
129 #define BN_BITS 128
130 #define BN_BYTES 8
131 #define BN_BITS2 64
132 #define BN_BITS4 32
133 #define BN_MASK (0xffffffffffffffffffffffffffffffffLL)
134 #define BN_MASK2 (0xffffffffffffffffL)
135 #define BN_MASK2l (0xffffffffL)
136 #define BN_MASK2h (0xffffffff00000000L)
137 #define BN_MASK2h1 (0xffffffff80000000L)
138 #define BN_TBIT (0x8000000000000000L)
139 #define BN_DEC_CONV (10000000000000000000UL)
140 #define BN_DEC_FMT1 "%lu"
141 #define BN_DEC_FMT2 "%019lu"
142 #define BN_DEC_NUM 19
143 #endif /* SIXTY_FOUR_BIT_LONG */
145 /* This is where the long long data type is 64 bits, but long is 32.
146 * For machines where there are 64bit registers, this is the mode to use.
147 * IRIX, on R4000 and above should use this mode, along with the relevant
148 * assembler code :-). Do NOT define BN_LLONG.
150 #ifdef SIXTY_FOUR_BIT
151 #undef BN_LLONG
152 #undef BN_ULLONG
153 #define BN_ULONG unsigned long long
154 #define BN_LONG long long
155 #define BN_BITS 128
156 #define BN_BYTES 8
157 #define BN_BITS2 64
158 #define BN_BITS4 32
159 #define BN_MASK2 (0xffffffffffffffffLL)
160 #define BN_MASK2l (0xffffffffL)
161 #define BN_MASK2h (0xffffffff00000000LL)
162 #define BN_MASK2h1 (0xffffffff80000000LL)
163 #define BN_TBIT (0x8000000000000000LL)
164 #define BN_DEC_CONV (10000000000000000000ULL)
165 #define BN_DEC_FMT1 "%llu"
166 #define BN_DEC_FMT2 "%019llu"
167 #define BN_DEC_NUM 19
168 #endif /* SIXTY_FOUR_BIT */
170 #ifdef THIRTY_TWO_BIT
171 #if defined(OPENSSL_SYS_WIN32) && !defined(__GNUC__)
172 #define BN_ULLONG unsigned _int64
173 #else
174 #define BN_ULLONG unsigned long long
175 #endif
176 #define BN_ULONG unsigned long
177 #define BN_LONG long
178 #define BN_BITS 64
179 #define BN_BYTES 4
180 #define BN_BITS2 32
181 #define BN_BITS4 16
182 #ifdef OPENSSL_SYS_WIN32
183 /* VC++ doesn't like the LL suffix */
184 #define BN_MASK (0xffffffffffffffffL)
185 #else
186 #define BN_MASK (0xffffffffffffffffLL)
187 #endif
188 #define BN_MASK2 (0xffffffffL)
189 #define BN_MASK2l (0xffff)
190 #define BN_MASK2h1 (0xffff8000L)
191 #define BN_MASK2h (0xffff0000L)
192 #define BN_TBIT (0x80000000L)
193 #define BN_DEC_CONV (1000000000L)
194 #define BN_DEC_FMT1 "%lu"
195 #define BN_DEC_FMT2 "%09lu"
196 #define BN_DEC_NUM 9
197 #endif /* THIRTY_TWO_BIT */
199 #ifdef SIXTEEN_BIT
200 #ifndef BN_DIV2W
201 #define BN_DIV2W
202 #endif
203 #define BN_ULLONG unsigned long
204 #define BN_ULONG unsigned short
205 #define BN_LONG short
206 #define BN_BITS 32
207 #define BN_BYTES 2
208 #define BN_BITS2 16
209 #define BN_BITS4 8
210 #define BN_MASK (0xffffffff)
211 #define BN_MASK2 (0xffff)
212 #define BN_MASK2l (0xff)
213 #define BN_MASK2h1 (0xff80)
214 #define BN_MASK2h (0xff00)
215 #define BN_TBIT (0x8000)
216 #define BN_DEC_CONV (100000)
217 #define BN_DEC_FMT1 "%u"
218 #define BN_DEC_FMT2 "%05u"
219 #define BN_DEC_NUM 5
220 #endif /* SIXTEEN_BIT */
222 #ifdef EIGHT_BIT
223 #ifndef BN_DIV2W
224 #define BN_DIV2W
225 #endif
226 #define BN_ULLONG unsigned short
227 #define BN_ULONG unsigned char
228 #define BN_LONG char
229 #define BN_BITS 16
230 #define BN_BYTES 1
231 #define BN_BITS2 8
232 #define BN_BITS4 4
233 #define BN_MASK (0xffff)
234 #define BN_MASK2 (0xff)
235 #define BN_MASK2l (0xf)
236 #define BN_MASK2h1 (0xf8)
237 #define BN_MASK2h (0xf0)
238 #define BN_TBIT (0x80)
239 #define BN_DEC_CONV (100)
240 #define BN_DEC_FMT1 "%u"
241 #define BN_DEC_FMT2 "%02u"
242 #define BN_DEC_NUM 2
243 #endif /* EIGHT_BIT */
245 #define BN_DEFAULT_BITS 1280
247 #ifdef BIGNUM
248 #undef BIGNUM
249 #endif
251 #define BN_FLG_MALLOCED 0x01
252 #define BN_FLG_STATIC_DATA 0x02
253 #define BN_FLG_FREE 0x8000 /* used for debuging */
254 #define BN_set_flags(b, n) ((b)->flags |= (n))
255 #define BN_get_flags(b, n) ((b)->flags & (n))
257 typedef void (*bn_rand_fn_t)(unsigned char *, int);
259 typedef struct bignum_st
261 BN_ULONG *d; /* Pointer to an array of 'BN_BITS2' bit chunks. */
262 int top; /* Index of last used d +1. */
263 /* The next are internal book keeping for bn_expand. */
264 int dmax; /* Size of the d array. */
265 int neg; /* one if the number is negative */
266 int flags;
267 } BIGNUM;
269 /* Used for temp variables (declaration hidden in bn_lcl.h) */
270 typedef struct bignum_ctx BN_CTX;
272 typedef struct bn_blinding_st
274 int init;
275 BIGNUM *A;
276 BIGNUM *Ai;
277 BIGNUM *mod; /* just a reference */
278 unsigned long thread_id; /* added in OpenSSL 0.9.6j and 0.9.7b;
279 * used only by crypto/rsa/rsa_eay.c, rsa_lib.c
281 } BN_BLINDING;
283 /* Used for montgomery multiplication */
284 typedef struct bn_mont_ctx_st
286 int ri; /* number of bits in R */
287 BIGNUM RR; /* used to convert to montgomery form */
288 BIGNUM N; /* The modulus */
289 BIGNUM Ni; /* R*(1/R mod N) - N*Ni = 1
290 * (Ni is only stored for bignum algorithm)
292 BN_ULONG n0; /* least significant word of Ni */
293 int flags;
294 } BN_MONT_CTX;
296 /* Used for reciprocal division/mod functions
297 * It cannot be shared between threads
299 typedef struct bn_recp_ctx_st
301 BIGNUM N; /* the divisor */
302 BIGNUM Nr; /* the reciprocal */
303 int num_bits;
304 int shift;
305 int flags;
306 } BN_RECP_CTX;
308 /* default: select number of iterations
309 * based on the size of the number
311 #define BN_prime_checks 0
313 /* number of Miller-Rabin iterations for an error rate of less than 2^-80
314 * for random 'b'-bit input, b >= 100 (taken from table 4.4 in the Handbook
315 * of Applied Cryptography [Menezes, van Oorschot, Vanstone; CRC Press 1996];
316 * original paper: Damgaard, Landrock, Pomerance: Average case error estimates
317 * for the strong probable prime test. -- Math. Comp. 61 (1993) 177-194)
319 #define BN_prime_checks_for_size(b) ((b) >= 1300 ? 2 : \
320 (b) >= 850 ? 3 : \
321 (b) >= 650 ? 4 : \
322 (b) >= 550 ? 5 : \
323 (b) >= 450 ? 6 : \
324 (b) >= 400 ? 7 : \
325 (b) >= 350 ? 8 : \
326 (b) >= 300 ? 9 : \
327 (b) >= 250 ? 12 : \
328 (b) >= 200 ? 15 : \
329 (b) >= 150 ? 18 : \
330 /* b >= 100 */ 27)
332 #define BN_num_bytes(a) ((BN_num_bits(a)+7)/8)
334 /* Note that BN_abs_is_word does not work reliably for w == 0 */
335 #define BN_abs_is_word(a, w) (((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w)))
336 #define BN_is_zero(a) (((a)->top == 0) || BN_abs_is_word(a, 0))
337 #define BN_is_one(a) (BN_abs_is_word((a), 1) && !(a)->neg)
338 #define BN_is_word(a, w) ((w) ? BN_abs_is_word((a), (w)) && !(a)->neg : BN_is_zero((a)))
339 #define BN_is_odd(a) (((a)->top > 0) && ((a)->d[0] & 1))
341 #define BN_one(a) (BN_set_word((a), 1))
342 #define BN_zero(a) (BN_set_word((a), 0))
344 /* #define BN_ascii2bn(a) BN_hex2bn(a) */
345 /* #define BN_bn2ascii(a) BN_bn2hex(a) */
347 void BN_register_RAND(bn_rand_fn_t);
348 const BIGNUM *BN_value_one(void);
349 char * BN_options(void);
350 BN_CTX *BN_CTX_new(void);
351 void BN_CTX_init(BN_CTX *c);
352 void BN_CTX_free(BN_CTX *c);
353 void BN_CTX_start(BN_CTX *ctx);
354 BIGNUM *BN_CTX_get(BN_CTX *ctx);
355 void BN_CTX_end(BN_CTX *ctx);
356 int BN_rand(BIGNUM *rnd, int bits, int top, int bottom);
357 int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom);
358 int BN_rand_range(BIGNUM *rnd, BIGNUM *range);
359 int BN_pseudo_rand_range(BIGNUM *rnd, BIGNUM *range);
360 int BN_num_bits(const BIGNUM *a);
361 int BN_num_bits_word(BN_ULONG);
362 BIGNUM *BN_new(void);
363 void BN_init(BIGNUM *);
364 void BN_clear_free(BIGNUM *a);
365 BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b);
366 void BN_swap(BIGNUM *a, BIGNUM *b);
367 BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret);
368 int BN_bn2bin(const BIGNUM *a, unsigned char *to);
369 BIGNUM *BN_mpi2bn(const unsigned char *s, int len, BIGNUM *ret);
370 int BN_bn2mpi(const BIGNUM *a, unsigned char *to);
371 int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
372 int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
373 int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
374 int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
375 int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
376 int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx);
378 int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
379 BN_CTX *ctx);
380 #define BN_mod(rem, m, d, ctx) BN_div(NULL, (rem), (m), (d), (ctx))
381 int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx);
382 int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx);
383 int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m);
384 int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx);
385 int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m);
386 int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
387 const BIGNUM *m, BN_CTX *ctx);
388 int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
389 int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
390 int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m);
391 int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m, BN_CTX *ctx);
392 int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m);
394 BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w);
395 BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w);
396 int BN_mul_word(BIGNUM *a, BN_ULONG w);
397 int BN_add_word(BIGNUM *a, BN_ULONG w);
398 int BN_sub_word(BIGNUM *a, BN_ULONG w);
399 int BN_set_word(BIGNUM *a, BN_ULONG w);
400 BN_ULONG BN_get_word(const BIGNUM *a);
402 int BN_cmp(const BIGNUM *a, const BIGNUM *b);
403 void BN_free(BIGNUM *a);
404 int BN_is_bit_set(const BIGNUM *a, int n);
405 int BN_lshift(BIGNUM *r, const BIGNUM *a, int n);
406 int BN_lshift1(BIGNUM *r, const BIGNUM *a);
407 int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
409 int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx);
410 int BN_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
411 BN_CTX *ctx, BN_MONT_CTX *m_ctx);
412 int BN_mod_exp_mont_word(BIGNUM *r, BN_ULONG a, const BIGNUM *p, const BIGNUM *m,
413 BN_CTX *ctx, BN_MONT_CTX *m_ctx);
414 int BN_mod_exp2_mont(BIGNUM *r, const BIGNUM *a1, const BIGNUM *p1, const BIGNUM *a2,
415 const BIGNUM *p2, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
416 int BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
417 BN_CTX *ctx);
419 int BN_mask_bits(BIGNUM *a, int n);
420 #ifndef OPENSSL_NO_FP_API
421 int BN_print_fp(FILE *fp, const BIGNUM *a);
422 #endif
423 #ifdef HEADER_BIO_H
424 int BN_print(BIO *fp, const BIGNUM *a);
425 #else
426 int BN_print(void *fp, const BIGNUM *a);
427 #endif
428 int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx);
429 int BN_rshift(BIGNUM *r, const BIGNUM *a, int n);
430 int BN_rshift1(BIGNUM *r, const BIGNUM *a);
431 void BN_clear(BIGNUM *a);
432 BIGNUM *BN_dup(const BIGNUM *a);
433 int BN_ucmp(const BIGNUM *a, const BIGNUM *b);
434 int BN_set_bit(BIGNUM *a, int n);
435 int BN_clear_bit(BIGNUM *a, int n);
436 char * BN_bn2hex(const BIGNUM *a);
437 char * BN_bn2dec(const BIGNUM *a);
438 int BN_hex2bn(BIGNUM **a, const char *str);
439 int BN_dec2bn(BIGNUM **a, const char *str);
440 int BN_gcd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
441 int BN_kronecker(const BIGNUM *a,const BIGNUM *b,BN_CTX *ctx); /* returns -2 for error */
442 BIGNUM *BN_mod_inverse(BIGNUM *ret, const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx);
443 BIGNUM *BN_mod_sqrt(BIGNUM *ret, const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx);
444 BIGNUM *BN_generate_prime(BIGNUM *ret, int bits, int safe, const BIGNUM *add, const BIGNUM *rem,
445 void (*callback)(int, int, void *), void *cb_arg);
446 int BN_is_prime(const BIGNUM *p, int nchecks, void (*callback)(int, int, void *),
447 BN_CTX *ctx, void *cb_arg);
448 int BN_is_prime_fasttest(const BIGNUM *p, int nchecks, void (*callback)(int, int, void *),
449 BN_CTX *ctx, void *cb_arg, int do_trial_division);
451 BN_MONT_CTX *BN_MONT_CTX_new(void);
452 void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
453 int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
454 BN_MONT_CTX *mont, BN_CTX *ctx);
455 #define BN_to_montgomery(r, a, mont, ctx) \
456 BN_mod_mul_montgomery((r), (a), &((mont)->RR), (mont), (ctx))
457 int BN_from_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont, BN_CTX *ctx);
458 void BN_MONT_CTX_free(BN_MONT_CTX *mont);
459 int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx);
460 BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from);
462 BN_BLINDING *BN_BLINDING_new(BIGNUM *A, BIGNUM *Ai, BIGNUM *mod);
463 void BN_BLINDING_free(BN_BLINDING *b);
464 int BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx);
465 int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *r, BN_CTX *ctx);
466 int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
468 void BN_set_params(int mul, int high, int low, int mont);
469 int BN_get_params(int which); /* 0, mul, 1 high, 2 low, 3 mont */
471 void BN_RECP_CTX_init(BN_RECP_CTX *recp);
472 BN_RECP_CTX *BN_RECP_CTX_new(void);
473 void BN_RECP_CTX_free(BN_RECP_CTX *recp);
474 int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *rdiv, BN_CTX *ctx);
475 int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,
476 BN_RECP_CTX *recp, BN_CTX *ctx);
477 int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
478 const BIGNUM *m, BN_CTX *ctx);
479 int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
480 BN_RECP_CTX *recp, BN_CTX *ctx);
482 /* library internal functions */
484 #define bn_expand(a, bits) ((((((bits + BN_BITS2 - 1)) / BN_BITS2)) <= \
485 (a)->dmax) ? (a) : bn_expand2((a), (bits) / BN_BITS2 + 1))
486 #define bn_wexpand(a, words) (((words) <= (a)->dmax) ? (a) : bn_expand2((a), (words)))
487 BIGNUM *bn_expand2(BIGNUM *a, int words);
488 BIGNUM *bn_dup_expand(const BIGNUM *a, int words);
490 #define bn_fix_top(a) \
492 BN_ULONG *ftl; \
493 if ((a)->top > 0) { \
494 for (ftl = &((a)->d[(a)->top-1]); (a)->top > 0; (a)->top--) \
495 if (*(ftl--)) break; \
499 BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
500 BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
501 void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num);
502 BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
503 BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, int num);
504 BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, int num);
506 #ifdef BN_RECURSION
507 void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, BN_ULONG *t);
508 void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int tn, int n, BN_ULONG *t);
509 #endif
511 #ifdef BN_DEBUG
512 void bn_dump1(FILE *o, const char *a, const BN_ULONG *b, int n);
513 # define bn_dump(a, n) bn_dump1(stderr, #a, a, n);
514 #else
515 # define bn_dump(a, b)
516 #endif
518 int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom);
520 /* BEGIN ERROR CODES */
521 /* The following lines are auto generated by the script mkerr.pl. Any changes
522 * made after this point may be overwritten when the script is next run.
524 void ERR_load_BN_strings(void);
526 /* Error codes for the BN functions. */
528 /* Function codes. */
529 #define BN_F_BN_BLINDING_CONVERT 100
530 #define BN_F_BN_BLINDING_INVERT 101
531 #define BN_F_BN_BLINDING_NEW 102
532 #define BN_F_BN_BLINDING_UPDATE 103
533 #define BN_F_BN_BN2DEC 104
534 #define BN_F_BN_BN2HEX 105
535 #define BN_F_BN_CTX_GET 116
536 #define BN_F_BN_CTX_NEW 106
537 #define BN_F_BN_DIV 107
538 #define BN_F_BN_EXPAND2 108
539 #define BN_F_BN_EXPAND_INTERNAL 120
540 #define BN_F_BN_MOD_EXP2_MONT 118
541 #define BN_F_BN_MOD_EXP_MONT 109
542 #define BN_F_BN_MOD_EXP_MONT_WORD 117
543 #define BN_F_BN_MOD_INVERSE 110
544 #define BN_F_BN_MOD_LSHIFT_QUICK 119
545 #define BN_F_BN_MOD_MUL_RECIPROCAL 111
546 #define BN_F_BN_MOD_SQRT 121
547 #define BN_F_BN_MPI2BN 112
548 #define BN_F_BN_NEW 113
549 #define BN_F_BN_RAND 114
550 #define BN_F_BN_RAND_RANGE 122
551 #define BN_F_BN_USUB 115
553 /* Reason codes. */
554 #define BN_R_ARG2_LT_ARG3 100
555 #define BN_R_BAD_RECIPROCAL 101
556 #define BN_R_BIGNUM_TOO_LONG 114
557 #define BN_R_CALLED_WITH_EVEN_MODULUS 102
558 #define BN_R_DIV_BY_ZERO 103
559 #define BN_R_ENCODING_ERROR 104
560 #define BN_R_EXPAND_ON_STATIC_BIGNUM_DATA 105
561 #define BN_R_INPUT_NOT_REDUCED 110
562 #define BN_R_INVALID_LENGTH 106
563 #define BN_R_INVALID_RANGE 115
564 #define BN_R_NOT_A_SQUARE 111
565 #define BN_R_NOT_INITIALIZED 107
566 #define BN_R_NO_INVERSE 108
567 #define BN_R_P_IS_NOT_PRIME 112
568 #define BN_R_TOO_MANY_ITERATIONS 113
569 #define BN_R_TOO_MANY_TEMPORARY_VARIABLES 109
571 #ifdef __cplusplus
573 #endif
575 #endif /* HEADER_BN_H */