bn_local.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /*
  2. * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. #ifndef OSSL_CRYPTO_BN_LOCAL_H
  10. # define OSSL_CRYPTO_BN_LOCAL_H
  11. /*
  12. * The EDK2 build doesn't use bn_conf.h; it sets THIRTY_TWO_BIT or
  13. * SIXTY_FOUR_BIT in its own environment since it doesn't re-run our
  14. * Configure script and needs to support both 32-bit and 64-bit.
  15. */
  16. # include <openssl/opensslconf.h>
  17. # if !defined(OPENSSL_SYS_UEFI)
  18. # include "crypto/bn_conf.h"
  19. # endif
  20. # include "crypto/bn.h"
  21. # include "internal/cryptlib.h"
  22. # include "internal/numbers.h"
  23. /*
  24. * These preprocessor symbols control various aspects of the bignum headers
  25. * and library code. They're not defined by any "normal" configuration, as
  26. * they are intended for development and testing purposes. NB: defining
  27. * them can be useful for debugging application code as well as openssl
  28. * itself. BN_DEBUG - turn on various debugging alterations to the bignum
  29. * code BN_RAND_DEBUG - uses random poisoning of unused words to trip up
  30. * mismanagement of bignum internals. Enable BN_RAND_DEBUG is known to
  31. * break some of the OpenSSL tests.
  32. */
  33. # if defined(BN_RAND_DEBUG) && !defined(BN_DEBUG)
  34. # define BN_DEBUG
  35. # endif
  36. # if defined(BN_RAND_DEBUG)
  37. # include <openssl/rand.h>
  38. # endif
  39. /*
  40. * This should limit the stack usage due to alloca to about 4K.
  41. * BN_SOFT_LIMIT is a soft limit equivalent to 2*OPENSSL_RSA_MAX_MODULUS_BITS.
  42. * Beyond that size bn_mul_mont is no longer used, and the constant time
  43. * assembler code is disabled, due to the blatant alloca and bn_mul_mont usage.
  44. * Note that bn_mul_mont does an alloca that is hidden away in assembly.
  45. * It is not recommended to do computations with numbers exceeding this limit,
  46. * since the result will be highly version dependent:
  47. * While the current OpenSSL version will use non-optimized, but safe code,
  48. * previous versions will use optimized code, that may crash due to unexpected
  49. * stack overflow, and future versions may very well turn this into a hard
  50. * limit.
  51. * Note however, that it is possible to override the size limit using
  52. * "./config -DBN_SOFT_LIMIT=<limit>" if necessary, and the O/S specific
  53. * stack limit is known and taken into consideration.
  54. */
  55. # ifndef BN_SOFT_LIMIT
  56. # define BN_SOFT_LIMIT (4096 / BN_BYTES)
  57. # endif
  58. # ifndef OPENSSL_SMALL_FOOTPRINT
  59. # define BN_MUL_COMBA
  60. # define BN_SQR_COMBA
  61. # define BN_RECURSION
  62. # endif
  63. /*
  64. * This next option uses the C libraries (2 word)/(1 word) function. If it is
  65. * not defined, I use my C version (which is slower). The reason for this
  66. * flag is that when the particular C compiler library routine is used, and
  67. * the library is linked with a different compiler, the library is missing.
  68. * This mostly happens when the library is built with gcc and then linked
  69. * using normal cc. This would be a common occurrence because gcc normally
  70. * produces code that is 2 times faster than system compilers for the big
  71. * number stuff. For machines with only one compiler (or shared libraries),
  72. * this should be on. Again this in only really a problem on machines using
  73. * "long long's", are 32bit, and are not using my assembler code.
  74. */
  75. # if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || \
  76. defined(OPENSSL_SYS_WIN32) || defined(linux)
  77. # define BN_DIV2W
  78. # endif
  79. /*
  80. * 64-bit processor with LP64 ABI
  81. */
  82. # ifdef SIXTY_FOUR_BIT_LONG
  83. # define BN_ULLONG unsigned long long
  84. # define BN_BITS4 32
  85. # define BN_MASK2 (0xffffffffffffffffL)
  86. # define BN_MASK2l (0xffffffffL)
  87. # define BN_MASK2h (0xffffffff00000000L)
  88. # define BN_MASK2h1 (0xffffffff80000000L)
  89. # define BN_DEC_CONV (10000000000000000000UL)
  90. # define BN_DEC_NUM 19
  91. # define BN_DEC_FMT1 "%lu"
  92. # define BN_DEC_FMT2 "%019lu"
  93. # endif
  94. /*
  95. * 64-bit processor other than LP64 ABI
  96. */
  97. # ifdef SIXTY_FOUR_BIT
  98. # undef BN_LLONG
  99. # undef BN_ULLONG
  100. # define BN_BITS4 32
  101. # define BN_MASK2 (0xffffffffffffffffLL)
  102. # define BN_MASK2l (0xffffffffL)
  103. # define BN_MASK2h (0xffffffff00000000LL)
  104. # define BN_MASK2h1 (0xffffffff80000000LL)
  105. # define BN_DEC_CONV (10000000000000000000ULL)
  106. # define BN_DEC_NUM 19
  107. # define BN_DEC_FMT1 "%llu"
  108. # define BN_DEC_FMT2 "%019llu"
  109. # endif
  110. # ifdef THIRTY_TWO_BIT
  111. # ifdef BN_LLONG
  112. # if defined(_WIN32) && !defined(__GNUC__)
  113. # define BN_ULLONG unsigned __int64
  114. # else
  115. # define BN_ULLONG unsigned long long
  116. # endif
  117. # endif
  118. # define BN_BITS4 16
  119. # define BN_MASK2 (0xffffffffL)
  120. # define BN_MASK2l (0xffff)
  121. # define BN_MASK2h1 (0xffff8000L)
  122. # define BN_MASK2h (0xffff0000L)
  123. # define BN_DEC_CONV (1000000000L)
  124. # define BN_DEC_NUM 9
  125. # define BN_DEC_FMT1 "%u"
  126. # define BN_DEC_FMT2 "%09u"
  127. # endif
  128. /*-
  129. * Bignum consistency macros
  130. * There is one "API" macro, bn_fix_top(), for stripping leading zeroes from
  131. * bignum data after direct manipulations on the data. There is also an
  132. * "internal" macro, bn_check_top(), for verifying that there are no leading
  133. * zeroes. Unfortunately, some auditing is required due to the fact that
  134. * bn_fix_top() has become an overabused duct-tape because bignum data is
  135. * occasionally passed around in an inconsistent state. So the following
  136. * changes have been made to sort this out;
  137. * - bn_fix_top()s implementation has been moved to bn_correct_top()
  138. * - if BN_DEBUG isn't defined, bn_fix_top() maps to bn_correct_top(), and
  139. * bn_check_top() is as before.
  140. * - if BN_DEBUG *is* defined;
  141. * - bn_check_top() tries to pollute unused words even if the bignum 'top' is
  142. * consistent. (ed: only if BN_RAND_DEBUG is defined)
  143. * - bn_fix_top() maps to bn_check_top() rather than "fixing" anything.
  144. * The idea is to have debug builds flag up inconsistent bignums when they
  145. * occur. If that occurs in a bn_fix_top(), we examine the code in question; if
  146. * the use of bn_fix_top() was appropriate (ie. it follows directly after code
  147. * that manipulates the bignum) it is converted to bn_correct_top(), and if it
  148. * was not appropriate, we convert it permanently to bn_check_top() and track
  149. * down the cause of the bug. Eventually, no internal code should be using the
  150. * bn_fix_top() macro. External applications and libraries should try this with
  151. * their own code too, both in terms of building against the openssl headers
  152. * with BN_DEBUG defined *and* linking with a version of OpenSSL built with it
  153. * defined. This not only improves external code, it provides more test
  154. * coverage for openssl's own code.
  155. */
  156. # ifdef BN_DEBUG
  157. /*
  158. * The new BN_FLG_FIXED_TOP flag marks vectors that were not treated with
  159. * bn_correct_top, in other words such vectors are permitted to have zeros
  160. * in most significant limbs. Such vectors are used internally to achieve
  161. * execution time invariance for critical operations with private keys.
  162. * It's BN_DEBUG-only flag, because user application is not supposed to
  163. * observe it anyway. Moreover, optimizing compiler would actually remove
  164. * all operations manipulating the bit in question in non-BN_DEBUG build.
  165. */
  166. # define BN_FLG_FIXED_TOP 0x10000
  167. # ifdef BN_RAND_DEBUG
  168. # define bn_pollute(a) \
  169. do { \
  170. const BIGNUM *_bnum1 = (a); \
  171. if (_bnum1->top < _bnum1->dmax) { \
  172. unsigned char _tmp_char; \
  173. /* We cast away const without the compiler knowing, any \
  174. * *genuinely* constant variables that aren't mutable \
  175. * wouldn't be constructed with top!=dmax. */ \
  176. BN_ULONG *_not_const; \
  177. memcpy(&_not_const, &_bnum1->d, sizeof(_not_const)); \
  178. (void)RAND_bytes(&_tmp_char, 1); /* Debug only - safe to ignore error return */\
  179. memset(_not_const + _bnum1->top, _tmp_char, \
  180. sizeof(*_not_const) * (_bnum1->dmax - _bnum1->top)); \
  181. } \
  182. } while(0)
  183. # else
  184. # define bn_pollute(a)
  185. # endif
  186. # define bn_check_top(a) \
  187. do { \
  188. const BIGNUM *_bnum2 = (a); \
  189. if (_bnum2 != NULL) { \
  190. int _top = _bnum2->top; \
  191. (void)ossl_assert((_top == 0 && !_bnum2->neg) || \
  192. (_top && ((_bnum2->flags & BN_FLG_FIXED_TOP) \
  193. || _bnum2->d[_top - 1] != 0))); \
  194. bn_pollute(_bnum2); \
  195. } \
  196. } while(0)
  197. # define bn_fix_top(a) bn_check_top(a)
  198. # define bn_check_size(bn, bits) bn_wcheck_size(bn, ((bits+BN_BITS2-1))/BN_BITS2)
  199. # define bn_wcheck_size(bn, words) \
  200. do { \
  201. const BIGNUM *_bnum2 = (bn); \
  202. assert((words) <= (_bnum2)->dmax && \
  203. (words) >= (_bnum2)->top); \
  204. /* avoid unused variable warning with NDEBUG */ \
  205. (void)(_bnum2); \
  206. } while(0)
  207. # else /* !BN_DEBUG */
  208. # define BN_FLG_FIXED_TOP 0
  209. # define bn_pollute(a)
  210. # define bn_check_top(a)
  211. # define bn_fix_top(a) bn_correct_top(a)
  212. # define bn_check_size(bn, bits)
  213. # define bn_wcheck_size(bn, words)
  214. # endif
  215. BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
  216. BN_ULONG w);
  217. BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
  218. void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num);
  219. BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
  220. BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  221. int num);
  222. BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  223. int num);
  224. struct bignum_st {
  225. BN_ULONG *d; /*
  226. * Pointer to an array of 'BN_BITS2' bit
  227. * chunks. These chunks are organised in
  228. * a least significant chunk first order.
  229. */
  230. int top; /* Index of last used d +1. */
  231. /* The next are internal book keeping for bn_expand. */
  232. int dmax; /* Size of the d array. */
  233. int neg; /* one if the number is negative */
  234. int flags;
  235. };
  236. /* Used for montgomery multiplication */
  237. struct bn_mont_ctx_st {
  238. int ri; /* number of bits in R */
  239. BIGNUM RR; /* used to convert to montgomery form,
  240. possibly zero-padded */
  241. BIGNUM N; /* The modulus */
  242. BIGNUM Ni; /* R*(1/R mod N) - N*Ni = 1 (Ni is only
  243. * stored for bignum algorithm) */
  244. BN_ULONG n0[2]; /* least significant word(s) of Ni; (type
  245. * changed with 0.9.9, was "BN_ULONG n0;"
  246. * before) */
  247. int flags;
  248. };
  249. /*
  250. * Used for reciprocal division/mod functions It cannot be shared between
  251. * threads
  252. */
  253. struct bn_recp_ctx_st {
  254. BIGNUM N; /* the divisor */
  255. BIGNUM Nr; /* the reciprocal */
  256. int num_bits;
  257. int shift;
  258. int flags;
  259. };
  260. /* Used for slow "generation" functions. */
  261. struct bn_gencb_st {
  262. unsigned int ver; /* To handle binary (in)compatibility */
  263. void *arg; /* callback-specific data */
  264. union {
  265. /* if (ver==1) - handles old style callbacks */
  266. void (*cb_1) (int, int, void *);
  267. /* if (ver==2) - new callback style */
  268. int (*cb_2) (int, int, BN_GENCB *);
  269. } cb;
  270. };
  271. /*-
  272. * BN_window_bits_for_exponent_size -- macro for sliding window mod_exp functions
  273. *
  274. *
  275. * For window size 'w' (w >= 2) and a random 'b' bits exponent,
  276. * the number of multiplications is a constant plus on average
  277. *
  278. * 2^(w-1) + (b-w)/(w+1);
  279. *
  280. * here 2^(w-1) is for precomputing the table (we actually need
  281. * entries only for windows that have the lowest bit set), and
  282. * (b-w)/(w+1) is an approximation for the expected number of
  283. * w-bit windows, not counting the first one.
  284. *
  285. * Thus we should use
  286. *
  287. * w >= 6 if b > 671
  288. * w = 5 if 671 > b > 239
  289. * w = 4 if 239 > b > 79
  290. * w = 3 if 79 > b > 23
  291. * w <= 2 if 23 > b
  292. *
  293. * (with draws in between). Very small exponents are often selected
  294. * with low Hamming weight, so we use w = 1 for b <= 23.
  295. */
  296. # define BN_window_bits_for_exponent_size(b) \
  297. ((b) > 671 ? 6 : \
  298. (b) > 239 ? 5 : \
  299. (b) > 79 ? 4 : \
  300. (b) > 23 ? 3 : 1)
  301. /*
  302. * BN_mod_exp_mont_consttime is based on the assumption that the L1 data cache
  303. * line width of the target processor is at least the following value.
  304. */
  305. # define MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH ( 64 )
  306. # define MOD_EXP_CTIME_MIN_CACHE_LINE_MASK (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - 1)
  307. /*
  308. * Window sizes optimized for fixed window size modular exponentiation
  309. * algorithm (BN_mod_exp_mont_consttime). To achieve the security goals of
  310. * BN_mode_exp_mont_consttime, the maximum size of the window must not exceed
  311. * log_2(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH). Window size thresholds are
  312. * defined for cache line sizes of 32 and 64, cache line sizes where
  313. * log_2(32)=5 and log_2(64)=6 respectively. A window size of 7 should only be
  314. * used on processors that have a 128 byte or greater cache line size.
  315. */
  316. # if MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 64
  317. # define BN_window_bits_for_ctime_exponent_size(b) \
  318. ((b) > 937 ? 6 : \
  319. (b) > 306 ? 5 : \
  320. (b) > 89 ? 4 : \
  321. (b) > 22 ? 3 : 1)
  322. # define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (6)
  323. # elif MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 32
  324. # define BN_window_bits_for_ctime_exponent_size(b) \
  325. ((b) > 306 ? 5 : \
  326. (b) > 89 ? 4 : \
  327. (b) > 22 ? 3 : 1)
  328. # define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (5)
  329. # endif
  330. /* Pentium pro 16,16,16,32,64 */
  331. /* Alpha 16,16,16,16.64 */
  332. # define BN_MULL_SIZE_NORMAL (16)/* 32 */
  333. # define BN_MUL_RECURSIVE_SIZE_NORMAL (16)/* 32 less than */
  334. # define BN_SQR_RECURSIVE_SIZE_NORMAL (16)/* 32 */
  335. # define BN_MUL_LOW_RECURSIVE_SIZE_NORMAL (32)/* 32 */
  336. # define BN_MONT_CTX_SET_SIZE_WORD (64)/* 32 */
  337. # if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)
  338. /*
  339. * BN_UMULT_HIGH section.
  340. * If the compiler doesn't support 2*N integer type, then you have to
  341. * replace every N*N multiplication with 4 (N/2)*(N/2) accompanied by some
  342. * shifts and additions which unavoidably results in severe performance
  343. * penalties. Of course provided that the hardware is capable of producing
  344. * 2*N result... That's when you normally start considering assembler
  345. * implementation. However! It should be pointed out that some CPUs (e.g.,
  346. * PowerPC, Alpha, and IA-64) provide *separate* instruction calculating
  347. * the upper half of the product placing the result into a general
  348. * purpose register. Now *if* the compiler supports inline assembler,
  349. * then it's not impossible to implement the "bignum" routines (and have
  350. * the compiler optimize 'em) exhibiting "native" performance in C. That's
  351. * what BN_UMULT_HIGH macro is about:-) Note that more recent compilers do
  352. * support 2*64 integer type, which is also used here.
  353. */
  354. # if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__==16 && \
  355. (defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG))
  356. # define BN_UMULT_HIGH(a,b) (((uint128_t)(a)*(b))>>64)
  357. # define BN_UMULT_LOHI(low,high,a,b) ({ \
  358. uint128_t ret=(uint128_t)(a)*(b); \
  359. (high)=ret>>64; (low)=ret; })
  360. # elif defined(__alpha) && (defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT))
  361. # if defined(__DECC)
  362. # include <c_asm.h>
  363. # define BN_UMULT_HIGH(a,b) (BN_ULONG)asm("umulh %a0,%a1,%v0",(a),(b))
  364. # elif defined(__GNUC__) && __GNUC__>=2
  365. # define BN_UMULT_HIGH(a,b) ({ \
  366. register BN_ULONG ret; \
  367. asm ("umulh %1,%2,%0" \
  368. : "=r"(ret) \
  369. : "r"(a), "r"(b)); \
  370. ret; })
  371. # endif /* compiler */
  372. # elif defined(_ARCH_PPC64) && defined(SIXTY_FOUR_BIT_LONG)
  373. # if defined(__GNUC__) && __GNUC__>=2
  374. # define BN_UMULT_HIGH(a,b) ({ \
  375. register BN_ULONG ret; \
  376. asm ("mulhdu %0,%1,%2" \
  377. : "=r"(ret) \
  378. : "r"(a), "r"(b)); \
  379. ret; })
  380. # endif /* compiler */
  381. # elif (defined(__x86_64) || defined(__x86_64__)) && \
  382. (defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT))
  383. # if defined(__GNUC__) && __GNUC__>=2
  384. # define BN_UMULT_HIGH(a,b) ({ \
  385. register BN_ULONG ret,discard; \
  386. asm ("mulq %3" \
  387. : "=a"(discard),"=d"(ret) \
  388. : "a"(a), "g"(b) \
  389. : "cc"); \
  390. ret; })
  391. # define BN_UMULT_LOHI(low,high,a,b) \
  392. asm ("mulq %3" \
  393. : "=a"(low),"=d"(high) \
  394. : "a"(a),"g"(b) \
  395. : "cc");
  396. # endif
  397. # elif (defined(_M_AMD64) || defined(_M_X64)) && defined(SIXTY_FOUR_BIT)
  398. # if defined(_MSC_VER) && _MSC_VER>=1400
  399. unsigned __int64 __umulh(unsigned __int64 a, unsigned __int64 b);
  400. unsigned __int64 _umul128(unsigned __int64 a, unsigned __int64 b,
  401. unsigned __int64 *h);
  402. # pragma intrinsic(__umulh,_umul128)
  403. # define BN_UMULT_HIGH(a,b) __umulh((a),(b))
  404. # define BN_UMULT_LOHI(low,high,a,b) ((low)=_umul128((a),(b),&(high)))
  405. # endif
  406. # elif defined(__mips) && (defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG))
  407. # if defined(__GNUC__) && __GNUC__>=2
  408. # define BN_UMULT_HIGH(a,b) ({ \
  409. register BN_ULONG ret; \
  410. asm ("dmultu %1,%2" \
  411. : "=h"(ret) \
  412. : "r"(a), "r"(b) : "l"); \
  413. ret; })
  414. # define BN_UMULT_LOHI(low,high,a,b) \
  415. asm ("dmultu %2,%3" \
  416. : "=l"(low),"=h"(high) \
  417. : "r"(a), "r"(b));
  418. # endif
  419. # elif defined(__aarch64__) && defined(SIXTY_FOUR_BIT_LONG)
  420. # if defined(__GNUC__) && __GNUC__>=2
  421. # define BN_UMULT_HIGH(a,b) ({ \
  422. register BN_ULONG ret; \
  423. asm ("umulh %0,%1,%2" \
  424. : "=r"(ret) \
  425. : "r"(a), "r"(b)); \
  426. ret; })
  427. # endif
  428. # endif /* cpu */
  429. # endif /* OPENSSL_NO_ASM */
  430. # ifdef BN_RAND_DEBUG
  431. # define bn_clear_top2max(a) \
  432. { \
  433. int ind = (a)->dmax - (a)->top; \
  434. BN_ULONG *ftl = &(a)->d[(a)->top-1]; \
  435. for (; ind != 0; ind--) \
  436. *(++ftl) = 0x0; \
  437. }
  438. # else
  439. # define bn_clear_top2max(a)
  440. # endif
  441. # ifdef BN_LLONG
  442. /*******************************************************************
  443. * Using the long long type, has to be twice as wide as BN_ULONG...
  444. */
  445. # define Lw(t) (((BN_ULONG)(t))&BN_MASK2)
  446. # define Hw(t) (((BN_ULONG)((t)>>BN_BITS2))&BN_MASK2)
  447. # define mul_add(r,a,w,c) { \
  448. BN_ULLONG t; \
  449. t=(BN_ULLONG)w * (a) + (r) + (c); \
  450. (r)= Lw(t); \
  451. (c)= Hw(t); \
  452. }
  453. # define mul(r,a,w,c) { \
  454. BN_ULLONG t; \
  455. t=(BN_ULLONG)w * (a) + (c); \
  456. (r)= Lw(t); \
  457. (c)= Hw(t); \
  458. }
  459. # define sqr(r0,r1,a) { \
  460. BN_ULLONG t; \
  461. t=(BN_ULLONG)(a)*(a); \
  462. (r0)=Lw(t); \
  463. (r1)=Hw(t); \
  464. }
  465. # elif defined(BN_UMULT_LOHI)
  466. # define mul_add(r,a,w,c) { \
  467. BN_ULONG high,low,ret,tmp=(a); \
  468. ret = (r); \
  469. BN_UMULT_LOHI(low,high,w,tmp); \
  470. ret += (c); \
  471. (c) = (ret<(c)); \
  472. (c) += high; \
  473. ret += low; \
  474. (c) += (ret<low); \
  475. (r) = ret; \
  476. }
  477. # define mul(r,a,w,c) { \
  478. BN_ULONG high,low,ret,ta=(a); \
  479. BN_UMULT_LOHI(low,high,w,ta); \
  480. ret = low + (c); \
  481. (c) = high; \
  482. (c) += (ret<low); \
  483. (r) = ret; \
  484. }
  485. # define sqr(r0,r1,a) { \
  486. BN_ULONG tmp=(a); \
  487. BN_UMULT_LOHI(r0,r1,tmp,tmp); \
  488. }
  489. # elif defined(BN_UMULT_HIGH)
  490. # define mul_add(r,a,w,c) { \
  491. BN_ULONG high,low,ret,tmp=(a); \
  492. ret = (r); \
  493. high= BN_UMULT_HIGH(w,tmp); \
  494. ret += (c); \
  495. low = (w) * tmp; \
  496. (c) = (ret<(c)); \
  497. (c) += high; \
  498. ret += low; \
  499. (c) += (ret<low); \
  500. (r) = ret; \
  501. }
  502. # define mul(r,a,w,c) { \
  503. BN_ULONG high,low,ret,ta=(a); \
  504. low = (w) * ta; \
  505. high= BN_UMULT_HIGH(w,ta); \
  506. ret = low + (c); \
  507. (c) = high; \
  508. (c) += (ret<low); \
  509. (r) = ret; \
  510. }
  511. # define sqr(r0,r1,a) { \
  512. BN_ULONG tmp=(a); \
  513. (r0) = tmp * tmp; \
  514. (r1) = BN_UMULT_HIGH(tmp,tmp); \
  515. }
  516. # else
  517. /*************************************************************
  518. * No long long type
  519. */
  520. # define LBITS(a) ((a)&BN_MASK2l)
  521. # define HBITS(a) (((a)>>BN_BITS4)&BN_MASK2l)
  522. # define L2HBITS(a) (((a)<<BN_BITS4)&BN_MASK2)
  523. # define LLBITS(a) ((a)&BN_MASKl)
  524. # define LHBITS(a) (((a)>>BN_BITS2)&BN_MASKl)
  525. # define LL2HBITS(a) ((BN_ULLONG)((a)&BN_MASKl)<<BN_BITS2)
  526. # define mul64(l,h,bl,bh) \
  527. { \
  528. BN_ULONG m,m1,lt,ht; \
  529. \
  530. lt=l; \
  531. ht=h; \
  532. m =(bh)*(lt); \
  533. lt=(bl)*(lt); \
  534. m1=(bl)*(ht); \
  535. ht =(bh)*(ht); \
  536. m=(m+m1)&BN_MASK2; ht += L2HBITS((BN_ULONG)(m < m1)); \
  537. ht+=HBITS(m); \
  538. m1=L2HBITS(m); \
  539. lt=(lt+m1)&BN_MASK2; ht += (lt < m1); \
  540. (l)=lt; \
  541. (h)=ht; \
  542. }
  543. # define sqr64(lo,ho,in) \
  544. { \
  545. BN_ULONG l,h,m; \
  546. \
  547. h=(in); \
  548. l=LBITS(h); \
  549. h=HBITS(h); \
  550. m =(l)*(h); \
  551. l*=l; \
  552. h*=h; \
  553. h+=(m&BN_MASK2h1)>>(BN_BITS4-1); \
  554. m =(m&BN_MASK2l)<<(BN_BITS4+1); \
  555. l=(l+m)&BN_MASK2; h += (l < m); \
  556. (lo)=l; \
  557. (ho)=h; \
  558. }
  559. # define mul_add(r,a,bl,bh,c) { \
  560. BN_ULONG l,h; \
  561. \
  562. h= (a); \
  563. l=LBITS(h); \
  564. h=HBITS(h); \
  565. mul64(l,h,(bl),(bh)); \
  566. \
  567. /* non-multiply part */ \
  568. l=(l+(c))&BN_MASK2; h += (l < (c)); \
  569. (c)=(r); \
  570. l=(l+(c))&BN_MASK2; h += (l < (c)); \
  571. (c)=h&BN_MASK2; \
  572. (r)=l; \
  573. }
  574. # define mul(r,a,bl,bh,c) { \
  575. BN_ULONG l,h; \
  576. \
  577. h= (a); \
  578. l=LBITS(h); \
  579. h=HBITS(h); \
  580. mul64(l,h,(bl),(bh)); \
  581. \
  582. /* non-multiply part */ \
  583. l+=(c); h += ((l&BN_MASK2) < (c)); \
  584. (c)=h&BN_MASK2; \
  585. (r)=l&BN_MASK2; \
  586. }
  587. # endif /* !BN_LLONG */
  588. void BN_RECP_CTX_init(BN_RECP_CTX *recp);
  589. void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
  590. void bn_init(BIGNUM *a);
  591. void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb);
  592. void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
  593. void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
  594. void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp);
  595. void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a);
  596. void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a);
  597. int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n);
  598. int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl);
  599. void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
  600. int dna, int dnb, BN_ULONG *t);
  601. void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b,
  602. int n, int tna, int tnb, BN_ULONG *t);
  603. void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t);
  604. void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n);
  605. void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
  606. BN_ULONG *t);
  607. BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  608. int cl, int dl);
  609. int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  610. const BN_ULONG *np, const BN_ULONG *n0, int num);
  611. void bn_correct_top_consttime(BIGNUM *a);
  612. BIGNUM *int_bn_mod_inverse(BIGNUM *in,
  613. const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,
  614. int *noinv);
  615. static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)
  616. {
  617. if (bits > (INT_MAX - BN_BITS2 + 1))
  618. return NULL;
  619. if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)
  620. return a;
  621. return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);
  622. }
  623. int ossl_bn_check_prime(const BIGNUM *w, int checks, BN_CTX *ctx,
  624. int do_trial_division, BN_GENCB *cb);
  625. #endif