2
0

bn_lcl.h 25 KB

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