bn_local.h 25 KB

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