Path: blob/main/crypto/openssl/crypto/bn/bn_local.h
108036 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) || defined(OPENSSL_SYS_WIN32) || defined(linux)83#define BN_DIV2W84#endif8586/*87* 64-bit processor with LP64 ABI88*/89#ifdef SIXTY_FOUR_BIT_LONG90#define BN_ULLONG unsigned long long91#define BN_BITS4 3292#define BN_MASK2 (0xffffffffffffffffL)93#define BN_MASK2l (0xffffffffL)94#define BN_MASK2h (0xffffffff00000000L)95#define BN_MASK2h1 (0xffffffff80000000L)96#define BN_DEC_CONV (10000000000000000000UL)97#define BN_DEC_NUM 1998#define BN_DEC_FMT1 "%lu"99#define BN_DEC_FMT2 "%019lu"100#endif101102/*103* 64-bit processor other than LP64 ABI104*/105#ifdef SIXTY_FOUR_BIT106#undef BN_LLONG107#undef BN_ULLONG108#define BN_BITS4 32109#define BN_MASK2 (0xffffffffffffffffLL)110#define BN_MASK2l (0xffffffffL)111#define BN_MASK2h (0xffffffff00000000LL)112#define BN_MASK2h1 (0xffffffff80000000LL)113#define BN_DEC_CONV (10000000000000000000ULL)114#define BN_DEC_NUM 19115#define BN_DEC_FMT1 "%llu"116#define BN_DEC_FMT2 "%019llu"117#endif118119#ifdef THIRTY_TWO_BIT120#ifdef BN_LLONG121#if defined(_WIN32) && !defined(__GNUC__)122#define BN_ULLONG unsigned __int64123#else124#define BN_ULLONG unsigned long long125#endif126#endif127#define BN_BITS4 16128#define BN_MASK2 (0xffffffffL)129#define BN_MASK2l (0xffff)130#define BN_MASK2h1 (0xffff8000L)131#define BN_MASK2h (0xffff0000L)132#define BN_DEC_CONV (1000000000L)133#define BN_DEC_NUM 9134#define BN_DEC_FMT1 "%u"135#define BN_DEC_FMT2 "%09u"136#endif137138/*-139* Bignum consistency macros140* There is one "API" macro, bn_fix_top(), for stripping leading zeroes from141* bignum data after direct manipulations on the data. There is also an142* "internal" macro, bn_check_top(), for verifying that there are no leading143* zeroes. Unfortunately, some auditing is required due to the fact that144* bn_fix_top() has become an overabused duct-tape because bignum data is145* occasionally passed around in an inconsistent state. So the following146* changes have been made to sort this out;147* - bn_fix_top()s implementation has been moved to bn_correct_top()148* - if BN_DEBUG isn't defined, bn_fix_top() maps to bn_correct_top(), and149* bn_check_top() is as before.150* - if BN_DEBUG *is* defined;151* - bn_check_top() tries to pollute unused words even if the bignum 'top' is152* consistent. (ed: only if BN_RAND_DEBUG is defined)153* - bn_fix_top() maps to bn_check_top() rather than "fixing" anything.154* The idea is to have debug builds flag up inconsistent bignums when they155* occur. If that occurs in a bn_fix_top(), we examine the code in question; if156* the use of bn_fix_top() was appropriate (ie. it follows directly after code157* that manipulates the bignum) it is converted to bn_correct_top(), and if it158* was not appropriate, we convert it permanently to bn_check_top() and track159* down the cause of the bug. Eventually, no internal code should be using the160* bn_fix_top() macro. External applications and libraries should try this with161* their own code too, both in terms of building against the openssl headers162* with BN_DEBUG defined *and* linking with a version of OpenSSL built with it163* defined. This not only improves external code, it provides more test164* coverage for openssl's own code.165*/166167#ifdef BN_DEBUG168/*169* The new BN_FLG_FIXED_TOP flag marks vectors that were not treated with170* bn_correct_top, in other words such vectors are permitted to have zeros171* in most significant limbs. Such vectors are used internally to achieve172* execution time invariance for critical operations with private keys.173* It's BN_DEBUG-only flag, because user application is not supposed to174* observe it anyway. Moreover, optimizing compiler would actually remove175* all operations manipulating the bit in question in non-BN_DEBUG build.176*/177#define BN_FLG_FIXED_TOP 0x10000178#ifdef BN_RAND_DEBUG179#define bn_pollute(a) \180do { \181const BIGNUM *_bnum1 = (a); \182if (_bnum1->top < _bnum1->dmax) { \183unsigned char _tmp_char; \184/* We cast away const without the compiler knowing, any \185* *genuinely* constant variables that aren't mutable \186* wouldn't be constructed with top!=dmax. */ \187BN_ULONG *_not_const; \188memcpy(&_not_const, &_bnum1->d, sizeof(_not_const)); \189(void)RAND_bytes(&_tmp_char, 1); /* Debug only - safe to ignore error return */ \190memset(_not_const + _bnum1->top, _tmp_char, \191sizeof(*_not_const) * (_bnum1->dmax - _bnum1->top)); \192} \193} while (0)194#else195#define bn_pollute(a)196#endif197#define bn_check_top(a) \198do { \199const BIGNUM *_bnum2 = (a); \200if (_bnum2 != NULL) { \201int _top = _bnum2->top; \202(void)ossl_assert((_top == 0 && !_bnum2->neg) || (_top && ((_bnum2->flags & BN_FLG_FIXED_TOP) || _bnum2->d[_top - 1] != 0))); \203bn_pollute(_bnum2); \204} \205} while (0)206207#define bn_fix_top(a) bn_check_top(a)208209#define bn_check_size(bn, bits) bn_wcheck_size(bn, ((bits + BN_BITS2 - 1)) / BN_BITS2)210#define bn_wcheck_size(bn, words) \211do { \212const BIGNUM *_bnum2 = (bn); \213assert((words) <= (_bnum2)->dmax && (words) >= (_bnum2)->top); \214/* avoid unused variable warning with NDEBUG */ \215(void)(_bnum2); \216} while (0)217218#else /* !BN_DEBUG */219220#define BN_FLG_FIXED_TOP 0221#define bn_pollute(a)222#define bn_check_top(a)223#define bn_fix_top(a) bn_correct_top(a)224#define bn_check_size(bn, bits)225#define bn_wcheck_size(bn, words)226227#endif228229BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,230BN_ULONG w);231BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);232void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num);233BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);234BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,235int num);236BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,237int num);238239struct bignum_st {240BN_ULONG *d; /*241* Pointer to an array of 'BN_BITS2' bit242* chunks. These chunks are organised in243* a least significant chunk first order.244*/245int top; /* Index of last used d +1. */246/* The next are internal book keeping for bn_expand. */247int dmax; /* Size of the d array. */248int neg; /* one if the number is negative */249int flags;250};251252/* Used for montgomery multiplication */253struct bn_mont_ctx_st {254int ri; /* number of bits in R */255BIGNUM RR; /* used to convert to montgomery form,256possibly zero-padded */257BIGNUM N; /* The modulus */258BIGNUM Ni; /* R*(1/R mod N) - N*Ni = 1 (Ni is only259* stored for bignum algorithm) */260BN_ULONG n0[2]; /* least significant word(s) of Ni; (type261* changed with 0.9.9, was "BN_ULONG n0;"262* before) */263int flags;264};265266/*267* Used for reciprocal division/mod functions It cannot be shared between268* threads269*/270struct bn_recp_ctx_st {271BIGNUM N; /* the divisor */272BIGNUM Nr; /* the reciprocal */273int num_bits;274int shift;275int flags;276};277278/* Used for slow "generation" functions. */279struct bn_gencb_st {280unsigned int ver; /* To handle binary (in)compatibility */281void *arg; /* callback-specific data */282union {283/* if (ver==1) - handles old style callbacks */284void (*cb_1)(int, int, void *);285/* if (ver==2) - new callback style */286int (*cb_2)(int, int, BN_GENCB *);287} cb;288};289290/*-291* BN_window_bits_for_exponent_size -- macro for sliding window mod_exp functions292*293*294* For window size 'w' (w >= 2) and a random 'b' bits exponent,295* the number of multiplications is a constant plus on average296*297* 2^(w-1) + (b-w)/(w+1);298*299* here 2^(w-1) is for precomputing the table (we actually need300* entries only for windows that have the lowest bit set), and301* (b-w)/(w+1) is an approximation for the expected number of302* w-bit windows, not counting the first one.303*304* Thus we should use305*306* w >= 6 if b > 671307* w = 5 if 671 > b > 239308* w = 4 if 239 > b > 79309* w = 3 if 79 > b > 23310* w <= 2 if 23 > b311*312* (with draws in between). Very small exponents are often selected313* with low Hamming weight, so we use w = 1 for b <= 23.314*/315#define BN_window_bits_for_exponent_size(b) \316((b) > 671 ? 6 : (b) > 239 ? 5 \317: (b) > 79 ? 4 \318: (b) > 23 ? 3 \319: 1)320321/*322* BN_mod_exp_mont_consttime is based on the assumption that the L1 data cache323* line width of the target processor is at least the following value.324*/325#define MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH (64)326#define MOD_EXP_CTIME_MIN_CACHE_LINE_MASK (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - 1)327328/*329* Window sizes optimized for fixed window size modular exponentiation330* algorithm (BN_mod_exp_mont_consttime). To achieve the security goals of331* BN_mode_exp_mont_consttime, the maximum size of the window must not exceed332* log_2(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH). Window size thresholds are333* defined for cache line sizes of 32 and 64, cache line sizes where334* log_2(32)=5 and log_2(64)=6 respectively. A window size of 7 should only be335* used on processors that have a 128 byte or greater cache line size.336*/337#if MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 64338339#define BN_window_bits_for_ctime_exponent_size(b) \340((b) > 937 ? 6 : (b) > 306 ? 5 \341: (b) > 89 ? 4 \342: (b) > 22 ? 3 \343: 1)344#define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (6)345346#elif MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 32347348#define BN_window_bits_for_ctime_exponent_size(b) \349((b) > 306 ? 5 : (b) > 89 ? 4 \350: (b) > 22 ? 3 \351: 1)352#define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (5)353354#endif355356/* Pentium pro 16,16,16,32,64 */357/* Alpha 16,16,16,16.64 */358#define BN_MULL_SIZE_NORMAL (16) /* 32 */359#define BN_MUL_RECURSIVE_SIZE_NORMAL (16) /* 32 less than */360#define BN_SQR_RECURSIVE_SIZE_NORMAL (16) /* 32 */361#define BN_MUL_LOW_RECURSIVE_SIZE_NORMAL (32) /* 32 */362#define BN_MONT_CTX_SET_SIZE_WORD (64) /* 32 */363364#if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)365/*366* BN_UMULT_HIGH section.367* If the compiler doesn't support 2*N integer type, then you have to368* replace every N*N multiplication with 4 (N/2)*(N/2) accompanied by some369* shifts and additions which unavoidably results in severe performance370* penalties. Of course provided that the hardware is capable of producing371* 2*N result... That's when you normally start considering assembler372* implementation. However! It should be pointed out that some CPUs (e.g.,373* PowerPC, Alpha, and IA-64) provide *separate* instruction calculating374* the upper half of the product placing the result into a general375* purpose register. Now *if* the compiler supports inline assembler,376* then it's not impossible to implement the "bignum" routines (and have377* the compiler optimize 'em) exhibiting "native" performance in C. That's378* what BN_UMULT_HIGH macro is about:-) Note that more recent compilers do379* support 2*64 integer type, which is also used here.380*/381#if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__ == 16 && (defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG))382#define BN_UMULT_HIGH(a, b) (((uint128_t)(a) * (b)) >> 64)383#define BN_UMULT_LOHI(low, high, a, b) ({ \384uint128_t ret=(uint128_t)(a)*(b); \385(high)=ret>>64; (low)=ret; })386#elif defined(__alpha) && (defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT))387#if defined(__DECC)388#include <c_asm.h>389#define BN_UMULT_HIGH(a, b) (BN_ULONG) asm("umulh %a0,%a1,%v0", (a), (b))390#elif defined(__GNUC__) && __GNUC__ >= 2391#define BN_UMULT_HIGH(a, b) ({ \392register BN_ULONG ret; \393asm ("umulh %1,%2,%0" \394: "=r"(ret) \395: "r"(a), "r"(b)); \396ret; })397#endif /* compiler */398#elif defined(_ARCH_PPC64) && defined(SIXTY_FOUR_BIT_LONG)399#if defined(__GNUC__) && __GNUC__ >= 2400#define BN_UMULT_HIGH(a, b) ({ \401register BN_ULONG ret; \402asm ("mulhdu %0,%1,%2" \403: "=r"(ret) \404: "r"(a), "r"(b)); \405ret; })406#endif /* compiler */407#elif (defined(__x86_64) || defined(__x86_64__)) && (defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT))408#if defined(__GNUC__) && __GNUC__ >= 2409#define BN_UMULT_HIGH(a, b) ({ \410register BN_ULONG ret,discard; \411asm ("mulq %3" \412: "=a"(discard),"=d"(ret) \413: "a"(a), "g"(b) \414: "cc"); \415ret; })416#define BN_UMULT_LOHI(low, high, a, b) \417asm("mulq %3" \418: "=a"(low), "=d"(high) \419: "a"(a), "g"(b) \420: "cc");421#endif422#elif (defined(_M_AMD64) || defined(_M_X64)) && defined(SIXTY_FOUR_BIT)423#if defined(_MSC_VER) && _MSC_VER >= 1400424unsigned __int64 __umulh(unsigned __int64 a, unsigned __int64 b);425unsigned __int64 _umul128(unsigned __int64 a, unsigned __int64 b,426unsigned __int64 *h);427#pragma intrinsic(__umulh, _umul128)428#define BN_UMULT_HIGH(a, b) __umulh((a), (b))429#define BN_UMULT_LOHI(low, high, a, b) ((low) = _umul128((a), (b), &(high)))430#endif431#elif defined(__mips) && (defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG))432#if defined(__GNUC__) && __GNUC__ >= 2433#define BN_UMULT_HIGH(a, b) ({ \434register BN_ULONG ret; \435asm ("dmultu %1,%2" \436: "=h"(ret) \437: "r"(a), "r"(b) : "l"); \438ret; })439#define BN_UMULT_LOHI(low, high, a, b) \440asm("dmultu %2,%3" \441: "=l"(low), "=h"(high) \442: "r"(a), "r"(b));443#endif444#elif defined(__aarch64__) && defined(SIXTY_FOUR_BIT_LONG)445#if defined(__GNUC__) && __GNUC__ >= 2446#define BN_UMULT_HIGH(a, b) ({ \447register BN_ULONG ret; \448asm ("umulh %0,%1,%2" \449: "=r"(ret) \450: "r"(a), "r"(b)); \451ret; })452#endif453#endif /* cpu */454#endif /* OPENSSL_NO_ASM */455456#ifdef BN_RAND_DEBUG457#define bn_clear_top2max(a) \458{ \459int ind = (a)->dmax - (a)->top; \460BN_ULONG *ftl = &(a)->d[(a)->top - 1]; \461for (; ind != 0; ind--) \462*(++ftl) = 0x0; \463}464#else465#define bn_clear_top2max(a)466#endif467468#ifdef BN_LLONG469/*******************************************************************470* Using the long long type, has to be twice as wide as BN_ULONG...471*/472#define Lw(t) (((BN_ULONG)(t)) & BN_MASK2)473#define Hw(t) (((BN_ULONG)((t) >> BN_BITS2)) & BN_MASK2)474475#define mul_add(r, a, w, c) \476{ \477BN_ULLONG t; \478t = (BN_ULLONG)w * (a) + (r) + (c); \479(r) = Lw(t); \480(c) = Hw(t); \481}482483#define mul(r, a, w, c) \484{ \485BN_ULLONG t; \486t = (BN_ULLONG)w * (a) + (c); \487(r) = Lw(t); \488(c) = Hw(t); \489}490491#define sqr(r0, r1, a) \492{ \493BN_ULLONG t; \494t = (BN_ULLONG)(a) * (a); \495(r0) = Lw(t); \496(r1) = Hw(t); \497}498499#elif defined(BN_UMULT_LOHI)500#define mul_add(r, a, w, c) \501{ \502BN_ULONG high, low, ret, tmp = (a); \503ret = (r); \504BN_UMULT_LOHI(low, high, w, tmp); \505ret += (c); \506(c) = (ret < (c)); \507(c) += high; \508ret += low; \509(c) += (ret < low); \510(r) = ret; \511}512513#define mul(r, a, w, c) \514{ \515BN_ULONG high, low, ret, ta = (a); \516BN_UMULT_LOHI(low, high, w, ta); \517ret = low + (c); \518(c) = high; \519(c) += (ret < low); \520(r) = ret; \521}522523#define sqr(r0, r1, a) \524{ \525BN_ULONG tmp = (a); \526BN_UMULT_LOHI(r0, r1, tmp, tmp); \527}528529#elif defined(BN_UMULT_HIGH)530#define mul_add(r, a, w, c) \531{ \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) \545{ \546BN_ULONG high, low, ret, ta = (a); \547low = (w) * ta; \548high = BN_UMULT_HIGH(w, ta); \549ret = low + (c); \550(c) = high; \551(c) += (ret < low); \552(r) = ret; \553}554555#define sqr(r0, r1, a) \556{ \557BN_ULONG tmp = (a); \558(r0) = tmp * tmp; \559(r1) = BN_UMULT_HIGH(tmp, tmp); \560}561562#else563/*************************************************************564* No long long type565*/566567#define LBITS(a) ((a) & BN_MASK2l)568#define HBITS(a) (((a) >> BN_BITS4) & BN_MASK2l)569#define L2HBITS(a) (((a) << BN_BITS4) & BN_MASK2)570571#define LLBITS(a) ((a) & BN_MASKl)572#define LHBITS(a) (((a) >> BN_BITS2) & BN_MASKl)573#define LL2HBITS(a) ((BN_ULLONG)((a) & BN_MASKl) << BN_BITS2)574575#define mul64(l, h, bl, bh) \576{ \577BN_ULONG m, m1, lt, ht; \578\579lt = l; \580ht = h; \581m = (bh) * (lt); \582lt = (bl) * (lt); \583m1 = (bl) * (ht); \584ht = (bh) * (ht); \585m = (m + m1) & BN_MASK2; \586ht += L2HBITS((BN_ULONG)(m < m1)); \587ht += HBITS(m); \588m1 = L2HBITS(m); \589lt = (lt + m1) & BN_MASK2; \590ht += (lt < m1); \591(l) = lt; \592(h) = ht; \593}594595#define sqr64(lo, ho, in) \596{ \597BN_ULONG l, h, m; \598\599h = (in); \600l = LBITS(h); \601h = HBITS(h); \602m = (l) * (h); \603l *= l; \604h *= h; \605h += (m & BN_MASK2h1) >> (BN_BITS4 - 1); \606m = (m & BN_MASK2l) << (BN_BITS4 + 1); \607l = (l + m) & BN_MASK2; \608h += (l < m); \609(lo) = l; \610(ho) = h; \611}612613#define mul_add(r, a, bl, bh, c) \614{ \615BN_ULONG l, h; \616\617h = (a); \618l = LBITS(h); \619h = HBITS(h); \620mul64(l, h, (bl), (bh)); \621\622/* non-multiply part */ \623l = (l + (c)) & BN_MASK2; \624h += (l < (c)); \625(c) = (r); \626l = (l + (c)) & BN_MASK2; \627h += (l < (c)); \628(c) = h & BN_MASK2; \629(r) = l; \630}631632#define mul(r, a, bl, bh, c) \633{ \634BN_ULONG l, h; \635\636h = (a); \637l = LBITS(h); \638h = HBITS(h); \639mul64(l, h, (bl), (bh)); \640\641/* non-multiply part */ \642l += (c); \643h += ((l & BN_MASK2) < (c)); \644(c) = h & BN_MASK2; \645(r) = l & BN_MASK2; \646}647#endif /* !BN_LLONG */648649void BN_RECP_CTX_init(BN_RECP_CTX *recp);650void BN_MONT_CTX_init(BN_MONT_CTX *ctx);651652void bn_init(BIGNUM *a);653void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb);654void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);655void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);656void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp);657void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a);658void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a);659int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n);660int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl);661void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,662int dna, int dnb, BN_ULONG *t);663void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b,664int n, int tna, int tnb, BN_ULONG *t);665void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t);666void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n);667void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,668BN_ULONG *t);669BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,670int cl, int dl);671int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,672const BN_ULONG *np, const BN_ULONG *n0, int num);673void bn_correct_top_consttime(BIGNUM *a);674BIGNUM *int_bn_mod_inverse(BIGNUM *in,675const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,676int *noinv);677678static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)679{680if (bits > (INT_MAX - BN_BITS2 + 1))681return NULL;682683if (((bits + BN_BITS2 - 1) / BN_BITS2) <= (a)->dmax)684return a;685686return bn_expand2((a), (bits + BN_BITS2 - 1) / BN_BITS2);687}688689int ossl_bn_check_prime(const BIGNUM *w, int checks, BN_CTX *ctx,690int do_trial_division, BN_GENCB *cb);691692#endif693694695