2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #ifndef OSSL_CRYPTO_BN_LOCAL_H
11 # define OSSL_CRYPTO_BN_LOCAL_H
14 * The EDK2 build doesn't use bn_conf.h; it sets THIRTY_TWO_BIT or
15 * SIXTY_FOUR_BIT in its own environment since it doesn't re-run our
16 * Configure script and needs to support both 32-bit and 64-bit.
18 # include <openssl/opensslconf.h>
20 # if !defined(OPENSSL_SYS_UEFI)
21 # include "crypto/bn_conf.h"
24 # include "crypto/bn.h"
25 # include "internal/cryptlib.h"
26 # include "internal/numbers.h"
29 * These preprocessor symbols control various aspects of the bignum headers
30 * and library code. They're not defined by any "normal" configuration, as
31 * they are intended for development and testing purposes. NB: defining
32 * them can be useful for debugging application code as well as openssl
33 * itself. BN_DEBUG - turn on various debugging alterations to the bignum
34 * code BN_RAND_DEBUG - uses random poisoning of unused words to trip up
35 * mismanagement of bignum internals. Enable BN_RAND_DEBUG is known to
36 * break some of the OpenSSL tests.
38 # if defined(BN_RAND_DEBUG) && !defined(BN_DEBUG)
41 # if defined(BN_RAND_DEBUG)
42 # include <openssl/rand.h>
46 * This should limit the stack usage due to alloca to about 4K.
47 * BN_SOFT_LIMIT is a soft limit equivalent to 2*OPENSSL_RSA_MAX_MODULUS_BITS.
48 * Beyond that size bn_mul_mont is no longer used, and the constant time
49 * assembler code is disabled, due to the blatant alloca and bn_mul_mont usage.
50 * Note that bn_mul_mont does an alloca that is hidden away in assembly.
51 * It is not recommended to do computations with numbers exceeding this limit,
52 * since the result will be highly version dependent:
53 * While the current OpenSSL version will use non-optimized, but safe code,
54 * previous versions will use optimized code, that may crash due to unexpected
55 * stack overflow, and future versions may very well turn this into a hard
57 * Note however, that it is possible to override the size limit using
58 * "./config -DBN_SOFT_LIMIT=<limit>" if necessary, and the O/S specific
59 * stack limit is known and taken into consideration.
61 # ifndef BN_SOFT_LIMIT
62 # define BN_SOFT_LIMIT (4096 / BN_BYTES)
65 # ifndef OPENSSL_SMALL_FOOTPRINT
72 * This next option uses the C libraries (2 word)/(1 word) function. If it is
73 * not defined, I use my C version (which is slower). The reason for this
74 * flag is that when the particular C compiler library routine is used, and
75 * the library is linked with a different compiler, the library is missing.
76 * This mostly happens when the library is built with gcc and then linked
77 * using normal cc. This would be a common occurrence because gcc normally
78 * produces code that is 2 times faster than system compilers for the big
79 * number stuff. For machines with only one compiler (or shared libraries),
80 * this should be on. Again this in only really a problem on machines using
81 * "long long's", are 32bit, and are not using my assembler code.
83 # if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || \
84 defined(OPENSSL_SYS_WIN32) || defined(linux)
89 * 64-bit processor with LP64 ABI
91 # ifdef SIXTY_FOUR_BIT_LONG
92 # define BN_ULLONG unsigned long long
94 # define BN_MASK2 (0xffffffffffffffffL)
95 # define BN_MASK2l (0xffffffffL)
96 # define BN_MASK2h (0xffffffff00000000L)
97 # define BN_MASK2h1 (0xffffffff80000000L)
98 # define BN_DEC_CONV (10000000000000000000UL)
99 # define BN_DEC_NUM 19
100 # define BN_DEC_FMT1 "%lu"
101 # define BN_DEC_FMT2 "%019lu"
105 * 64-bit processor other than LP64 ABI
107 # ifdef SIXTY_FOUR_BIT
111 # define BN_MASK2 (0xffffffffffffffffLL)
112 # define BN_MASK2l (0xffffffffL)
113 # define BN_MASK2h (0xffffffff00000000LL)
114 # define BN_MASK2h1 (0xffffffff80000000LL)
115 # define BN_DEC_CONV (10000000000000000000ULL)
116 # define BN_DEC_NUM 19
117 # define BN_DEC_FMT1 "%llu"
118 # define BN_DEC_FMT2 "%019llu"
121 # ifdef THIRTY_TWO_BIT
123 # if defined(_WIN32) && !defined(__GNUC__)
124 # define BN_ULLONG unsigned __int64
126 # define BN_ULLONG unsigned long long
130 # define BN_MASK2 (0xffffffffL)
131 # define BN_MASK2l (0xffff)
132 # define BN_MASK2h1 (0xffff8000L)
133 # define BN_MASK2h (0xffff0000L)
134 # define BN_DEC_CONV (1000000000L)
135 # define BN_DEC_NUM 9
136 # define BN_DEC_FMT1 "%u"
137 # define BN_DEC_FMT2 "%09u"
142 * Bignum consistency macros
143 * There is one "API" macro, bn_fix_top(), for stripping leading zeroes from
144 * bignum data after direct manipulations on the data. There is also an
145 * "internal" macro, bn_check_top(), for verifying that there are no leading
146 * zeroes. Unfortunately, some auditing is required due to the fact that
147 * bn_fix_top() has become an overabused duct-tape because bignum data is
148 * occasionally passed around in an inconsistent state. So the following
149 * changes have been made to sort this out;
150 * - bn_fix_top()s implementation has been moved to bn_correct_top()
151 * - if BN_DEBUG isn't defined, bn_fix_top() maps to bn_correct_top(), and
152 * bn_check_top() is as before.
153 * - if BN_DEBUG *is* defined;
154 * - bn_check_top() tries to pollute unused words even if the bignum 'top' is
155 * consistent. (ed: only if BN_RAND_DEBUG is defined)
156 * - bn_fix_top() maps to bn_check_top() rather than "fixing" anything.
157 * The idea is to have debug builds flag up inconsistent bignums when they
158 * occur. If that occurs in a bn_fix_top(), we examine the code in question; if
159 * the use of bn_fix_top() was appropriate (ie. it follows directly after code
160 * that manipulates the bignum) it is converted to bn_correct_top(), and if it
161 * was not appropriate, we convert it permanently to bn_check_top() and track
162 * down the cause of the bug. Eventually, no internal code should be using the
163 * bn_fix_top() macro. External applications and libraries should try this with
164 * their own code too, both in terms of building against the openssl headers
165 * with BN_DEBUG defined *and* linking with a version of OpenSSL built with it
166 * defined. This not only improves external code, it provides more test
167 * coverage for openssl's own code.
172 * The new BN_FLG_FIXED_TOP flag marks vectors that were not treated with
173 * bn_correct_top, in other words such vectors are permitted to have zeros
174 * in most significant limbs. Such vectors are used internally to achieve
175 * execution time invariance for critical operations with private keys.
176 * It's BN_DEBUG-only flag, because user application is not supposed to
177 * observe it anyway. Moreover, optimizing compiler would actually remove
178 * all operations manipulating the bit in question in non-BN_DEBUG build.
180 # define BN_FLG_FIXED_TOP 0x10000
181 # ifdef BN_RAND_DEBUG
182 # define bn_pollute(a) \
184 const BIGNUM *_bnum1 = (a); \
185 if (_bnum1->top < _bnum1->dmax) { \
186 unsigned char _tmp_char; \
187 /* We cast away const without the compiler knowing, any \
188 * *genuinely* constant variables that aren't mutable \
189 * wouldn't be constructed with top!=dmax. */ \
190 BN_ULONG *_not_const; \
191 memcpy(&_not_const, &_bnum1->d, sizeof(_not_const)); \
192 (void)RAND_bytes(&_tmp_char, 1); /* Debug only - safe to ignore error return */\
193 memset(_not_const + _bnum1->top, _tmp_char, \
194 sizeof(*_not_const) * (_bnum1->dmax - _bnum1->top)); \
198 # define bn_pollute(a)
200 # define bn_check_top(a) \
202 const BIGNUM *_bnum2 = (a); \
203 if (_bnum2 != NULL) { \
204 int _top = _bnum2->top; \
205 (void)ossl_assert((_top == 0 && !_bnum2->neg) || \
206 (_top && ((_bnum2->flags & BN_FLG_FIXED_TOP) \
207 || _bnum2->d[_top - 1] != 0))); \
208 bn_pollute(_bnum2); \
212 # define bn_fix_top(a) bn_check_top(a)
214 # define bn_check_size(bn, bits) bn_wcheck_size(bn, ((bits+BN_BITS2-1))/BN_BITS2)
215 # define bn_wcheck_size(bn, words) \
217 const BIGNUM *_bnum2 = (bn); \
218 assert((words) <= (_bnum2)->dmax && \
219 (words) >= (_bnum2)->top); \
220 /* avoid unused variable warning with NDEBUG */ \
224 # else /* !BN_DEBUG */
226 # define BN_FLG_FIXED_TOP 0
227 # define bn_pollute(a)
228 # define bn_check_top(a)
229 # define bn_fix_top(a) bn_correct_top(a)
230 # define bn_check_size(bn, bits)
231 # define bn_wcheck_size(bn, words)
235 BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
237 BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
238 void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num);
239 BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
240 BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
242 BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
247 * Pointer to an array of 'BN_BITS2' bit
248 * chunks. These chunks are organised in
249 * a least significant chunk first order.
251 int top; /* Index of last used d +1. */
252 /* The next are internal book keeping for bn_expand. */
253 int dmax; /* Size of the d array. */
254 int neg; /* one if the number is negative */
258 /* Used for montgomery multiplication */
259 struct bn_mont_ctx_st {
260 int ri; /* number of bits in R */
261 BIGNUM RR; /* used to convert to montgomery form,
262 possibly zero-padded */
263 BIGNUM N; /* The modulus */
264 BIGNUM Ni; /* R*(1/R mod N) - N*Ni = 1 (Ni is only
265 * stored for bignum algorithm) */
266 BN_ULONG n0[2]; /* least significant word(s) of Ni; (type
267 * changed with 0.9.9, was "BN_ULONG n0;"
273 * Used for reciprocal division/mod functions It cannot be shared between
276 struct bn_recp_ctx_st {
277 BIGNUM N; /* the divisor */
278 BIGNUM Nr; /* the reciprocal */
284 /* Used for slow "generation" functions. */
286 unsigned int ver; /* To handle binary (in)compatibility */
287 void *arg; /* callback-specific data */
289 /* if (ver==1) - handles old style callbacks */
290 void (*cb_1) (int, int, void *);
291 /* if (ver==2) - new callback style */
292 int (*cb_2) (int, int, BN_GENCB *);
296 struct bn_blinding_st {
300 BIGNUM *mod; /* just a reference */
301 CRYPTO_THREAD_ID tid;
305 int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
306 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
311 * BN_window_bits_for_exponent_size -- macro for sliding window mod_exp functions
314 * For window size 'w' (w >= 2) and a random 'b' bits exponent,
315 * the number of multiplications is a constant plus on average
317 * 2^(w-1) + (b-w)/(w+1);
319 * here 2^(w-1) is for precomputing the table (we actually need
320 * entries only for windows that have the lowest bit set), and
321 * (b-w)/(w+1) is an approximation for the expected number of
322 * w-bit windows, not counting the first one.
327 * w = 5 if 671 > b > 239
328 * w = 4 if 239 > b > 79
329 * w = 3 if 79 > b > 23
332 * (with draws in between). Very small exponents are often selected
333 * with low Hamming weight, so we use w = 1 for b <= 23.
335 # define BN_window_bits_for_exponent_size(b) \
342 * BN_mod_exp_mont_consttime is based on the assumption that the L1 data cache
343 * line width of the target processor is at least the following value.
345 # define MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH ( 64 )
346 # define MOD_EXP_CTIME_MIN_CACHE_LINE_MASK (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - 1)
349 * Window sizes optimized for fixed window size modular exponentiation
350 * algorithm (BN_mod_exp_mont_consttime). To achieve the security goals of
351 * BN_mode_exp_mont_consttime, the maximum size of the window must not exceed
352 * log_2(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH). Window size thresholds are
353 * defined for cache line sizes of 32 and 64, cache line sizes where
354 * log_2(32)=5 and log_2(64)=6 respectively. A window size of 7 should only be
355 * used on processors that have a 128 byte or greater cache line size.
357 # if MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 64
359 # define BN_window_bits_for_ctime_exponent_size(b) \
364 # define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (6)
366 # elif MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 32
368 # define BN_window_bits_for_ctime_exponent_size(b) \
372 # define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (5)
376 /* Pentium pro 16,16,16,32,64 */
377 /* Alpha 16,16,16,16.64 */
378 # define BN_MULL_SIZE_NORMAL (16)/* 32 */
379 # define BN_MUL_RECURSIVE_SIZE_NORMAL (16)/* 32 less than */
380 # define BN_SQR_RECURSIVE_SIZE_NORMAL (16)/* 32 */
381 # define BN_MUL_LOW_RECURSIVE_SIZE_NORMAL (32)/* 32 */
382 # define BN_MONT_CTX_SET_SIZE_WORD (64)/* 32 */
385 * 2011-02-22 SMS. In various places, a size_t variable or a type cast to
386 * size_t was used to perform integer-only operations on pointers. This
387 * failed on VMS with 64-bit pointers (CC /POINTER_SIZE = 64) because size_t
388 * is still only 32 bits. What's needed in these cases is an integer type
389 * with the same size as a pointer, which size_t is not certain to be. The
390 * only fix here is VMS-specific.
392 # if defined(OPENSSL_SYS_VMS)
393 # if __INITIAL_POINTER_SIZE == 64
394 # define PTR_SIZE_INT long long
395 # else /* __INITIAL_POINTER_SIZE == 64 */
396 # define PTR_SIZE_INT int
397 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
398 # elif !defined(PTR_SIZE_INT) /* defined(OPENSSL_SYS_VMS) */
399 # define PTR_SIZE_INT size_t
400 # endif /* defined(OPENSSL_SYS_VMS) [else] */
402 # if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)
404 * BN_UMULT_HIGH section.
405 * If the compiler doesn't support 2*N integer type, then you have to
406 * replace every N*N multiplication with 4 (N/2)*(N/2) accompanied by some
407 * shifts and additions which unavoidably results in severe performance
408 * penalties. Of course provided that the hardware is capable of producing
409 * 2*N result... That's when you normally start considering assembler
410 * implementation. However! It should be pointed out that some CPUs (e.g.,
411 * PowerPC, Alpha, and IA-64) provide *separate* instruction calculating
412 * the upper half of the product placing the result into a general
413 * purpose register. Now *if* the compiler supports inline assembler,
414 * then it's not impossible to implement the "bignum" routines (and have
415 * the compiler optimize 'em) exhibiting "native" performance in C. That's
416 * what BN_UMULT_HIGH macro is about:-) Note that more recent compilers do
417 * support 2*64 integer type, which is also used here.
419 # if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__==16 && \
420 (defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG))
421 # define BN_UMULT_HIGH(a,b) (((uint128_t)(a)*(b))>>64)
422 # define BN_UMULT_LOHI(low,high,a,b) ({ \
423 uint128_t ret=(uint128_t)(a)*(b); \
424 (high)=ret>>64; (low)=ret; })
425 # elif defined(__alpha) && (defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT))
428 # define BN_UMULT_HIGH(a,b) (BN_ULONG)asm("umulh %a0,%a1,%v0",(a),(b))
429 # elif defined(__GNUC__) && __GNUC__>=2
430 # define BN_UMULT_HIGH(a,b) ({ \
431 register BN_ULONG ret; \
432 asm ("umulh %1,%2,%0" \
436 # endif /* compiler */
437 # elif defined(_ARCH_PPC64) && defined(SIXTY_FOUR_BIT_LONG)
438 # if defined(__GNUC__) && __GNUC__>=2
439 # define BN_UMULT_HIGH(a,b) ({ \
440 register BN_ULONG ret; \
441 asm ("mulhdu %0,%1,%2" \
445 # endif /* compiler */
446 # elif (defined(__x86_64) || defined(__x86_64__)) && \
447 (defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT))
448 # if defined(__GNUC__) && __GNUC__>=2
449 # define BN_UMULT_HIGH(a,b) ({ \
450 register BN_ULONG ret,discard; \
452 : "=a"(discard),"=d"(ret) \
456 # define BN_UMULT_LOHI(low,high,a,b) \
458 : "=a"(low),"=d"(high) \
462 # elif (defined(_M_AMD64) || defined(_M_X64)) && defined(SIXTY_FOUR_BIT)
463 # if defined(_MSC_VER) && _MSC_VER>=1400
464 unsigned __int64 __umulh(unsigned __int64 a, unsigned __int64 b);
465 unsigned __int64 _umul128(unsigned __int64 a, unsigned __int64 b,
466 unsigned __int64 *h);
467 # pragma intrinsic(__umulh,_umul128)
468 # define BN_UMULT_HIGH(a,b) __umulh((a),(b))
469 # define BN_UMULT_LOHI(low,high,a,b) ((low)=_umul128((a),(b),&(high)))
471 # elif defined(__mips) && (defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG))
472 # if defined(__GNUC__) && __GNUC__>=2
473 # define BN_UMULT_HIGH(a,b) ({ \
474 register BN_ULONG ret; \
475 asm ("dmultu %1,%2" \
477 : "r"(a), "r"(b) : "l"); \
479 # define BN_UMULT_LOHI(low,high,a,b) \
480 asm ("dmultu %2,%3" \
481 : "=l"(low),"=h"(high) \
484 # elif defined(__aarch64__) && defined(SIXTY_FOUR_BIT_LONG)
485 # if defined(__GNUC__) && __GNUC__>=2
486 # define BN_UMULT_HIGH(a,b) ({ \
487 register BN_ULONG ret; \
488 asm ("umulh %0,%1,%2" \
494 # endif /* OPENSSL_NO_ASM */
496 # ifdef BN_RAND_DEBUG
497 # define bn_clear_top2max(a) \
499 int ind = (a)->dmax - (a)->top; \
500 BN_ULONG *ftl = &(a)->d[(a)->top-1]; \
501 for (; ind != 0; ind--) \
505 # define bn_clear_top2max(a)
509 /*******************************************************************
510 * Using the long long type, has to be twice as wide as BN_ULONG...
512 # define Lw(t) (((BN_ULONG)(t))&BN_MASK2)
513 # define Hw(t) (((BN_ULONG)((t)>>BN_BITS2))&BN_MASK2)
515 # define mul_add(r,a,w,c) { \
517 t=(BN_ULLONG)w * (a) + (r) + (c); \
522 # define mul(r,a,w,c) { \
524 t=(BN_ULLONG)w * (a) + (c); \
529 # define sqr(r0,r1,a) { \
531 t=(BN_ULLONG)(a)*(a); \
536 # elif defined(BN_UMULT_LOHI)
537 # define mul_add(r,a,w,c) { \
538 BN_ULONG high,low,ret,tmp=(a); \
540 BN_UMULT_LOHI(low,high,w,tmp); \
542 (c) = (ret<(c))?1:0; \
545 (c) += (ret<low)?1:0; \
549 # define mul(r,a,w,c) { \
550 BN_ULONG high,low,ret,ta=(a); \
551 BN_UMULT_LOHI(low,high,w,ta); \
554 (c) += (ret<low)?1:0; \
558 # define sqr(r0,r1,a) { \
560 BN_UMULT_LOHI(r0,r1,tmp,tmp); \
563 # elif defined(BN_UMULT_HIGH)
564 # define mul_add(r,a,w,c) { \
565 BN_ULONG high,low,ret,tmp=(a); \
567 high= BN_UMULT_HIGH(w,tmp); \
570 (c) = (ret<(c))?1:0; \
573 (c) += (ret<low)?1:0; \
577 # define mul(r,a,w,c) { \
578 BN_ULONG high,low,ret,ta=(a); \
580 high= BN_UMULT_HIGH(w,ta); \
583 (c) += (ret<low)?1:0; \
587 # define sqr(r0,r1,a) { \
590 (r1) = BN_UMULT_HIGH(tmp,tmp); \
594 /*************************************************************
598 # define LBITS(a) ((a)&BN_MASK2l)
599 # define HBITS(a) (((a)>>BN_BITS4)&BN_MASK2l)
600 # define L2HBITS(a) (((a)<<BN_BITS4)&BN_MASK2)
602 # define LLBITS(a) ((a)&BN_MASKl)
603 # define LHBITS(a) (((a)>>BN_BITS2)&BN_MASKl)
604 # define LL2HBITS(a) ((BN_ULLONG)((a)&BN_MASKl)<<BN_BITS2)
606 # define mul64(l,h,bl,bh) \
608 BN_ULONG m,m1,lt,ht; \
616 m=(m+m1)&BN_MASK2; if (m < m1) ht+=L2HBITS((BN_ULONG)1); \
619 lt=(lt+m1)&BN_MASK2; if (lt < m1) ht++; \
624 # define sqr64(lo,ho,in) \
634 h+=(m&BN_MASK2h1)>>(BN_BITS4-1); \
635 m =(m&BN_MASK2l)<<(BN_BITS4+1); \
636 l=(l+m)&BN_MASK2; if (l < m) h++; \
641 # define mul_add(r,a,bl,bh,c) { \
647 mul64(l,h,(bl),(bh)); \
649 /* non-multiply part */ \
650 l=(l+(c))&BN_MASK2; if (l < (c)) h++; \
652 l=(l+(c))&BN_MASK2; if (l < (c)) h++; \
657 # define mul(r,a,bl,bh,c) { \
663 mul64(l,h,(bl),(bh)); \
665 /* non-multiply part */ \
666 l+=(c); if ((l&BN_MASK2) < (c)) h++; \
670 # endif /* !BN_LLONG */
672 void BN_RECP_CTX_init(BN_RECP_CTX *recp);
673 void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
675 void bn_init(BIGNUM *a);
676 void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb);
677 void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
678 void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
679 void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp);
680 void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a);
681 void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a);
682 int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n);
683 int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl);
684 void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
685 int dna, int dnb, BN_ULONG *t);
686 void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b,
687 int n, int tna, int tnb, BN_ULONG *t);
688 void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t);
689 void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n);
690 void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
692 BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
694 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
695 const BN_ULONG *np, const BN_ULONG *n0, int num);
697 BIGNUM *int_bn_mod_inverse(BIGNUM *in,
698 const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,
701 static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)
703 if (bits > (INT_MAX - BN_BITS2 + 1))
706 if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)
709 return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);
712 int ossl_bn_check_prime(const BIGNUM *w, int checks, BN_CTX *ctx,
713 int do_trial_division, BN_GENCB *cb);