Path: blob/main/crypto/openssl/crypto/bn/bn_local.h
34875 views
/*1* Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#ifndef OSSL_CRYPTO_BN_LOCAL_H10# define OSSL_CRYPTO_BN_LOCAL_H1112/*13* The EDK2 build doesn't use bn_conf.h; it sets THIRTY_TWO_BIT or14* SIXTY_FOUR_BIT in its own environment since it doesn't re-run our15* Configure script and needs to support both 32-bit and 64-bit.16*/17# include <openssl/opensslconf.h>1819# if !defined(OPENSSL_SYS_UEFI)20# include "crypto/bn_conf.h"21# endif2223# include "crypto/bn.h"24# include "internal/cryptlib.h"25# include "internal/numbers.h"2627/*28* These preprocessor symbols control various aspects of the bignum headers29* and library code. They're not defined by any "normal" configuration, as30* they are intended for development and testing purposes. NB: defining31* them can be useful for debugging application code as well as openssl32* itself. BN_DEBUG - turn on various debugging alterations to the bignum33* code BN_RAND_DEBUG - uses random poisoning of unused words to trip up34* mismanagement of bignum internals. Enable BN_RAND_DEBUG is known to35* break some of the OpenSSL tests.36*/37# if defined(BN_RAND_DEBUG) && !defined(BN_DEBUG)38# define BN_DEBUG39# endif40# if defined(BN_RAND_DEBUG)41# include <openssl/rand.h>42# endif4344/*45* This should limit the stack usage due to alloca to about 4K.46* BN_SOFT_LIMIT is a soft limit equivalent to 2*OPENSSL_RSA_MAX_MODULUS_BITS.47* Beyond that size bn_mul_mont is no longer used, and the constant time48* assembler code is disabled, due to the blatant alloca and bn_mul_mont usage.49* Note that bn_mul_mont does an alloca that is hidden away in assembly.50* It is not recommended to do computations with numbers exceeding this limit,51* since the result will be highly version dependent:52* While the current OpenSSL version will use non-optimized, but safe code,53* previous versions will use optimized code, that may crash due to unexpected54* stack overflow, and future versions may very well turn this into a hard55* limit.56* Note however, that it is possible to override the size limit using57* "./config -DBN_SOFT_LIMIT=<limit>" if necessary, and the O/S specific58* stack limit is known and taken into consideration.59*/60# ifndef BN_SOFT_LIMIT61# define BN_SOFT_LIMIT (4096 / BN_BYTES)62# endif6364# ifndef OPENSSL_SMALL_FOOTPRINT65# define BN_MUL_COMBA66# define BN_SQR_COMBA67# define BN_RECURSION68# endif6970/*71* This next option uses the C libraries (2 word)/(1 word) function. If it is72* not defined, I use my C version (which is slower). The reason for this73* flag is that when the particular C compiler library routine is used, and74* the library is linked with a different compiler, the library is missing.75* This mostly happens when the library is built with gcc and then linked76* using normal cc. This would be a common occurrence because gcc normally77* produces code that is 2 times faster than system compilers for the big78* number stuff. For machines with only one compiler (or shared libraries),79* this should be on. Again this in only really a problem on machines using80* "long long's", are 32bit, and are not using my assembler code.81*/82# if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || \83defined(OPENSSL_SYS_WIN32) || defined(linux)84# define BN_DIV2W85# endif8687/*88* 64-bit processor with LP64 ABI89*/90# ifdef SIXTY_FOUR_BIT_LONG91# define BN_ULLONG unsigned long long92# define BN_BITS4 3293# define BN_MASK2 (0xffffffffffffffffL)94# define BN_MASK2l (0xffffffffL)95# define BN_MASK2h (0xffffffff00000000L)96# define BN_MASK2h1 (0xffffffff80000000L)97# define BN_DEC_CONV (10000000000000000000UL)98# define BN_DEC_NUM 1999# define BN_DEC_FMT1 "%lu"100# define BN_DEC_FMT2 "%019lu"101# endif102103/*104* 64-bit processor other than LP64 ABI105*/106# ifdef SIXTY_FOUR_BIT107# undef BN_LLONG108# undef BN_ULLONG109# define BN_BITS4 32110# define BN_MASK2 (0xffffffffffffffffLL)111# define BN_MASK2l (0xffffffffL)112# define BN_MASK2h (0xffffffff00000000LL)113# define BN_MASK2h1 (0xffffffff80000000LL)114# define BN_DEC_CONV (10000000000000000000ULL)115# define BN_DEC_NUM 19116# define BN_DEC_FMT1 "%llu"117# define BN_DEC_FMT2 "%019llu"118# endif119120# ifdef THIRTY_TWO_BIT121# ifdef BN_LLONG122# if defined(_WIN32) && !defined(__GNUC__)123# define BN_ULLONG unsigned __int64124# else125# define BN_ULLONG unsigned long long126# endif127# endif128# define BN_BITS4 16129# define BN_MASK2 (0xffffffffL)130# define BN_MASK2l (0xffff)131# define BN_MASK2h1 (0xffff8000L)132# define BN_MASK2h (0xffff0000L)133# define BN_DEC_CONV (1000000000L)134# define BN_DEC_NUM 9135# define BN_DEC_FMT1 "%u"136# define BN_DEC_FMT2 "%09u"137# endif138139140/*-141* Bignum consistency macros142* There is one "API" macro, bn_fix_top(), for stripping leading zeroes from143* bignum data after direct manipulations on the data. There is also an144* "internal" macro, bn_check_top(), for verifying that there are no leading145* zeroes. Unfortunately, some auditing is required due to the fact that146* bn_fix_top() has become an overabused duct-tape because bignum data is147* occasionally passed around in an inconsistent state. So the following148* changes have been made to sort this out;149* - bn_fix_top()s implementation has been moved to bn_correct_top()150* - if BN_DEBUG isn't defined, bn_fix_top() maps to bn_correct_top(), and151* bn_check_top() is as before.152* - if BN_DEBUG *is* defined;153* - bn_check_top() tries to pollute unused words even if the bignum 'top' is154* consistent. (ed: only if BN_RAND_DEBUG is defined)155* - bn_fix_top() maps to bn_check_top() rather than "fixing" anything.156* The idea is to have debug builds flag up inconsistent bignums when they157* occur. If that occurs in a bn_fix_top(), we examine the code in question; if158* the use of bn_fix_top() was appropriate (ie. it follows directly after code159* that manipulates the bignum) it is converted to bn_correct_top(), and if it160* was not appropriate, we convert it permanently to bn_check_top() and track161* down the cause of the bug. Eventually, no internal code should be using the162* bn_fix_top() macro. External applications and libraries should try this with163* their own code too, both in terms of building against the openssl headers164* with BN_DEBUG defined *and* linking with a version of OpenSSL built with it165* defined. This not only improves external code, it provides more test166* coverage for openssl's own code.167*/168169# ifdef BN_DEBUG170/*171* The new BN_FLG_FIXED_TOP flag marks vectors that were not treated with172* bn_correct_top, in other words such vectors are permitted to have zeros173* in most significant limbs. Such vectors are used internally to achieve174* execution time invariance for critical operations with private keys.175* It's BN_DEBUG-only flag, because user application is not supposed to176* observe it anyway. Moreover, optimizing compiler would actually remove177* all operations manipulating the bit in question in non-BN_DEBUG build.178*/179# define BN_FLG_FIXED_TOP 0x10000180# ifdef BN_RAND_DEBUG181# define bn_pollute(a) \182do { \183const BIGNUM *_bnum1 = (a); \184if (_bnum1->top < _bnum1->dmax) { \185unsigned char _tmp_char; \186/* We cast away const without the compiler knowing, any \187* *genuinely* constant variables that aren't mutable \188* wouldn't be constructed with top!=dmax. */ \189BN_ULONG *_not_const; \190memcpy(&_not_const, &_bnum1->d, sizeof(_not_const)); \191(void)RAND_bytes(&_tmp_char, 1); /* Debug only - safe to ignore error return */\192memset(_not_const + _bnum1->top, _tmp_char, \193sizeof(*_not_const) * (_bnum1->dmax - _bnum1->top)); \194} \195} while(0)196# else197# define bn_pollute(a)198# endif199# define bn_check_top(a) \200do { \201const BIGNUM *_bnum2 = (a); \202if (_bnum2 != NULL) { \203int _top = _bnum2->top; \204(void)ossl_assert((_top == 0 && !_bnum2->neg) || \205(_top && ((_bnum2->flags & BN_FLG_FIXED_TOP) \206|| _bnum2->d[_top - 1] != 0))); \207bn_pollute(_bnum2); \208} \209} while(0)210211# define bn_fix_top(a) bn_check_top(a)212213# define bn_check_size(bn, bits) bn_wcheck_size(bn, ((bits+BN_BITS2-1))/BN_BITS2)214# define bn_wcheck_size(bn, words) \215do { \216const BIGNUM *_bnum2 = (bn); \217assert((words) <= (_bnum2)->dmax && \218(words) >= (_bnum2)->top); \219/* avoid unused variable warning with NDEBUG */ \220(void)(_bnum2); \221} while(0)222223# else /* !BN_DEBUG */224225# define BN_FLG_FIXED_TOP 0226# define bn_pollute(a)227# define bn_check_top(a)228# define bn_fix_top(a) bn_correct_top(a)229# define bn_check_size(bn, bits)230# define bn_wcheck_size(bn, words)231232# endif233234BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,235BN_ULONG w);236BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);237void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num);238BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);239BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,240int num);241BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,242int num);243244struct bignum_st {245BN_ULONG *d; /*246* Pointer to an array of 'BN_BITS2' bit247* chunks. These chunks are organised in248* a least significant chunk first order.249*/250int top; /* Index of last used d +1. */251/* The next are internal book keeping for bn_expand. */252int dmax; /* Size of the d array. */253int neg; /* one if the number is negative */254int flags;255};256257/* Used for montgomery multiplication */258struct bn_mont_ctx_st {259int ri; /* number of bits in R */260BIGNUM RR; /* used to convert to montgomery form,261possibly zero-padded */262BIGNUM N; /* The modulus */263BIGNUM Ni; /* R*(1/R mod N) - N*Ni = 1 (Ni is only264* stored for bignum algorithm) */265BN_ULONG n0[2]; /* least significant word(s) of Ni; (type266* changed with 0.9.9, was "BN_ULONG n0;"267* before) */268int flags;269};270271/*272* Used for reciprocal division/mod functions It cannot be shared between273* threads274*/275struct bn_recp_ctx_st {276BIGNUM N; /* the divisor */277BIGNUM Nr; /* the reciprocal */278int num_bits;279int shift;280int flags;281};282283/* Used for slow "generation" functions. */284struct bn_gencb_st {285unsigned int ver; /* To handle binary (in)compatibility */286void *arg; /* callback-specific data */287union {288/* if (ver==1) - handles old style callbacks */289void (*cb_1) (int, int, void *);290/* if (ver==2) - new callback style */291int (*cb_2) (int, int, BN_GENCB *);292} cb;293};294295/*-296* BN_window_bits_for_exponent_size -- macro for sliding window mod_exp functions297*298*299* For window size 'w' (w >= 2) and a random 'b' bits exponent,300* the number of multiplications is a constant plus on average301*302* 2^(w-1) + (b-w)/(w+1);303*304* here 2^(w-1) is for precomputing the table (we actually need305* entries only for windows that have the lowest bit set), and306* (b-w)/(w+1) is an approximation for the expected number of307* w-bit windows, not counting the first one.308*309* Thus we should use310*311* w >= 6 if b > 671312* w = 5 if 671 > b > 239313* w = 4 if 239 > b > 79314* w = 3 if 79 > b > 23315* w <= 2 if 23 > b316*317* (with draws in between). Very small exponents are often selected318* with low Hamming weight, so we use w = 1 for b <= 23.319*/320# define BN_window_bits_for_exponent_size(b) \321((b) > 671 ? 6 : \322(b) > 239 ? 5 : \323(b) > 79 ? 4 : \324(b) > 23 ? 3 : 1)325326/*327* BN_mod_exp_mont_consttime is based on the assumption that the L1 data cache328* line width of the target processor is at least the following value.329*/330# define MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH ( 64 )331# define MOD_EXP_CTIME_MIN_CACHE_LINE_MASK (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - 1)332333/*334* Window sizes optimized for fixed window size modular exponentiation335* algorithm (BN_mod_exp_mont_consttime). To achieve the security goals of336* BN_mode_exp_mont_consttime, the maximum size of the window must not exceed337* log_2(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH). Window size thresholds are338* defined for cache line sizes of 32 and 64, cache line sizes where339* log_2(32)=5 and log_2(64)=6 respectively. A window size of 7 should only be340* used on processors that have a 128 byte or greater cache line size.341*/342# if MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 64343344# define BN_window_bits_for_ctime_exponent_size(b) \345((b) > 937 ? 6 : \346(b) > 306 ? 5 : \347(b) > 89 ? 4 : \348(b) > 22 ? 3 : 1)349# define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (6)350351# elif MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 32352353# define BN_window_bits_for_ctime_exponent_size(b) \354((b) > 306 ? 5 : \355(b) > 89 ? 4 : \356(b) > 22 ? 3 : 1)357# define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (5)358359# endif360361/* Pentium pro 16,16,16,32,64 */362/* Alpha 16,16,16,16.64 */363# define BN_MULL_SIZE_NORMAL (16)/* 32 */364# define BN_MUL_RECURSIVE_SIZE_NORMAL (16)/* 32 less than */365# define BN_SQR_RECURSIVE_SIZE_NORMAL (16)/* 32 */366# define BN_MUL_LOW_RECURSIVE_SIZE_NORMAL (32)/* 32 */367# define BN_MONT_CTX_SET_SIZE_WORD (64)/* 32 */368369# if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)370/*371* BN_UMULT_HIGH section.372* If the compiler doesn't support 2*N integer type, then you have to373* replace every N*N multiplication with 4 (N/2)*(N/2) accompanied by some374* shifts and additions which unavoidably results in severe performance375* penalties. Of course provided that the hardware is capable of producing376* 2*N result... That's when you normally start considering assembler377* implementation. However! It should be pointed out that some CPUs (e.g.,378* PowerPC, Alpha, and IA-64) provide *separate* instruction calculating379* the upper half of the product placing the result into a general380* purpose register. Now *if* the compiler supports inline assembler,381* then it's not impossible to implement the "bignum" routines (and have382* the compiler optimize 'em) exhibiting "native" performance in C. That's383* what BN_UMULT_HIGH macro is about:-) Note that more recent compilers do384* support 2*64 integer type, which is also used here.385*/386# if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__==16 && \387(defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG))388# define BN_UMULT_HIGH(a,b) (((uint128_t)(a)*(b))>>64)389# define BN_UMULT_LOHI(low,high,a,b) ({ \390uint128_t ret=(uint128_t)(a)*(b); \391(high)=ret>>64; (low)=ret; })392# elif defined(__alpha) && (defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT))393# if defined(__DECC)394# include <c_asm.h>395# define BN_UMULT_HIGH(a,b) (BN_ULONG)asm("umulh %a0,%a1,%v0",(a),(b))396# elif defined(__GNUC__) && __GNUC__>=2397# define BN_UMULT_HIGH(a,b) ({ \398register BN_ULONG ret; \399asm ("umulh %1,%2,%0" \400: "=r"(ret) \401: "r"(a), "r"(b)); \402ret; })403# endif /* compiler */404# elif defined(_ARCH_PPC64) && defined(SIXTY_FOUR_BIT_LONG)405# if defined(__GNUC__) && __GNUC__>=2406# define BN_UMULT_HIGH(a,b) ({ \407register BN_ULONG ret; \408asm ("mulhdu %0,%1,%2" \409: "=r"(ret) \410: "r"(a), "r"(b)); \411ret; })412# endif /* compiler */413# elif (defined(__x86_64) || defined(__x86_64__)) && \414(defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT))415# if defined(__GNUC__) && __GNUC__>=2416# define BN_UMULT_HIGH(a,b) ({ \417register BN_ULONG ret,discard; \418asm ("mulq %3" \419: "=a"(discard),"=d"(ret) \420: "a"(a), "g"(b) \421: "cc"); \422ret; })423# define BN_UMULT_LOHI(low,high,a,b) \424asm ("mulq %3" \425: "=a"(low),"=d"(high) \426: "a"(a),"g"(b) \427: "cc");428# endif429# elif (defined(_M_AMD64) || defined(_M_X64)) && defined(SIXTY_FOUR_BIT)430# if defined(_MSC_VER) && _MSC_VER>=1400431unsigned __int64 __umulh(unsigned __int64 a, unsigned __int64 b);432unsigned __int64 _umul128(unsigned __int64 a, unsigned __int64 b,433unsigned __int64 *h);434# pragma intrinsic(__umulh,_umul128)435# define BN_UMULT_HIGH(a,b) __umulh((a),(b))436# define BN_UMULT_LOHI(low,high,a,b) ((low)=_umul128((a),(b),&(high)))437# endif438# elif defined(__mips) && (defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG))439# if defined(__GNUC__) && __GNUC__>=2440# define BN_UMULT_HIGH(a,b) ({ \441register BN_ULONG ret; \442asm ("dmultu %1,%2" \443: "=h"(ret) \444: "r"(a), "r"(b) : "l"); \445ret; })446# define BN_UMULT_LOHI(low,high,a,b) \447asm ("dmultu %2,%3" \448: "=l"(low),"=h"(high) \449: "r"(a), "r"(b));450# endif451# elif defined(__aarch64__) && defined(SIXTY_FOUR_BIT_LONG)452# if defined(__GNUC__) && __GNUC__>=2453# define BN_UMULT_HIGH(a,b) ({ \454register BN_ULONG ret; \455asm ("umulh %0,%1,%2" \456: "=r"(ret) \457: "r"(a), "r"(b)); \458ret; })459# endif460# endif /* cpu */461# endif /* OPENSSL_NO_ASM */462463# ifdef BN_RAND_DEBUG464# define bn_clear_top2max(a) \465{ \466int ind = (a)->dmax - (a)->top; \467BN_ULONG *ftl = &(a)->d[(a)->top-1]; \468for (; ind != 0; ind--) \469*(++ftl) = 0x0; \470}471# else472# define bn_clear_top2max(a)473# endif474475# ifdef BN_LLONG476/*******************************************************************477* Using the long long type, has to be twice as wide as BN_ULONG...478*/479# define Lw(t) (((BN_ULONG)(t))&BN_MASK2)480# define Hw(t) (((BN_ULONG)((t)>>BN_BITS2))&BN_MASK2)481482# define mul_add(r,a,w,c) { \483BN_ULLONG t; \484t=(BN_ULLONG)w * (a) + (r) + (c); \485(r)= Lw(t); \486(c)= Hw(t); \487}488489# define mul(r,a,w,c) { \490BN_ULLONG t; \491t=(BN_ULLONG)w * (a) + (c); \492(r)= Lw(t); \493(c)= Hw(t); \494}495496# define sqr(r0,r1,a) { \497BN_ULLONG t; \498t=(BN_ULLONG)(a)*(a); \499(r0)=Lw(t); \500(r1)=Hw(t); \501}502503# elif defined(BN_UMULT_LOHI)504# define mul_add(r,a,w,c) { \505BN_ULONG high,low,ret,tmp=(a); \506ret = (r); \507BN_UMULT_LOHI(low,high,w,tmp); \508ret += (c); \509(c) = (ret<(c)); \510(c) += high; \511ret += low; \512(c) += (ret<low); \513(r) = ret; \514}515516# define mul(r,a,w,c) { \517BN_ULONG high,low,ret,ta=(a); \518BN_UMULT_LOHI(low,high,w,ta); \519ret = low + (c); \520(c) = high; \521(c) += (ret<low); \522(r) = ret; \523}524525# define sqr(r0,r1,a) { \526BN_ULONG tmp=(a); \527BN_UMULT_LOHI(r0,r1,tmp,tmp); \528}529530# elif defined(BN_UMULT_HIGH)531# define mul_add(r,a,w,c) { \532BN_ULONG high,low,ret,tmp=(a); \533ret = (r); \534high= BN_UMULT_HIGH(w,tmp); \535ret += (c); \536low = (w) * tmp; \537(c) = (ret<(c)); \538(c) += high; \539ret += low; \540(c) += (ret<low); \541(r) = ret; \542}543544# define mul(r,a,w,c) { \545BN_ULONG high,low,ret,ta=(a); \546low = (w) * ta; \547high= BN_UMULT_HIGH(w,ta); \548ret = low + (c); \549(c) = high; \550(c) += (ret<low); \551(r) = ret; \552}553554# define sqr(r0,r1,a) { \555BN_ULONG tmp=(a); \556(r0) = tmp * tmp; \557(r1) = BN_UMULT_HIGH(tmp,tmp); \558}559560# else561/*************************************************************562* No long long type563*/564565# define LBITS(a) ((a)&BN_MASK2l)566# define HBITS(a) (((a)>>BN_BITS4)&BN_MASK2l)567# define L2HBITS(a) (((a)<<BN_BITS4)&BN_MASK2)568569# define LLBITS(a) ((a)&BN_MASKl)570# define LHBITS(a) (((a)>>BN_BITS2)&BN_MASKl)571# define LL2HBITS(a) ((BN_ULLONG)((a)&BN_MASKl)<<BN_BITS2)572573# define mul64(l,h,bl,bh) \574{ \575BN_ULONG m,m1,lt,ht; \576\577lt=l; \578ht=h; \579m =(bh)*(lt); \580lt=(bl)*(lt); \581m1=(bl)*(ht); \582ht =(bh)*(ht); \583m=(m+m1)&BN_MASK2; ht += L2HBITS((BN_ULONG)(m < m1)); \584ht+=HBITS(m); \585m1=L2HBITS(m); \586lt=(lt+m1)&BN_MASK2; ht += (lt < m1); \587(l)=lt; \588(h)=ht; \589}590591# define sqr64(lo,ho,in) \592{ \593BN_ULONG l,h,m; \594\595h=(in); \596l=LBITS(h); \597h=HBITS(h); \598m =(l)*(h); \599l*=l; \600h*=h; \601h+=(m&BN_MASK2h1)>>(BN_BITS4-1); \602m =(m&BN_MASK2l)<<(BN_BITS4+1); \603l=(l+m)&BN_MASK2; h += (l < m); \604(lo)=l; \605(ho)=h; \606}607608# define mul_add(r,a,bl,bh,c) { \609BN_ULONG l,h; \610\611h= (a); \612l=LBITS(h); \613h=HBITS(h); \614mul64(l,h,(bl),(bh)); \615\616/* non-multiply part */ \617l=(l+(c))&BN_MASK2; h += (l < (c)); \618(c)=(r); \619l=(l+(c))&BN_MASK2; h += (l < (c)); \620(c)=h&BN_MASK2; \621(r)=l; \622}623624# define mul(r,a,bl,bh,c) { \625BN_ULONG l,h; \626\627h= (a); \628l=LBITS(h); \629h=HBITS(h); \630mul64(l,h,(bl),(bh)); \631\632/* non-multiply part */ \633l+=(c); h += ((l&BN_MASK2) < (c)); \634(c)=h&BN_MASK2; \635(r)=l&BN_MASK2; \636}637# endif /* !BN_LLONG */638639void BN_RECP_CTX_init(BN_RECP_CTX *recp);640void BN_MONT_CTX_init(BN_MONT_CTX *ctx);641642void bn_init(BIGNUM *a);643void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb);644void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);645void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);646void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp);647void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a);648void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a);649int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n);650int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl);651void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,652int dna, int dnb, BN_ULONG *t);653void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b,654int n, int tna, int tnb, BN_ULONG *t);655void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t);656void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n);657void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,658BN_ULONG *t);659BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,660int cl, int dl);661int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,662const BN_ULONG *np, const BN_ULONG *n0, int num);663void bn_correct_top_consttime(BIGNUM *a);664BIGNUM *int_bn_mod_inverse(BIGNUM *in,665const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,666int *noinv);667668static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)669{670if (bits > (INT_MAX - BN_BITS2 + 1))671return NULL;672673if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)674return a;675676return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);677}678679int ossl_bn_check_prime(const BIGNUM *w, int checks, BN_CTX *ctx,680int do_trial_division, BN_GENCB *cb);681682#endif683684685