bn_lcl.h 24 KB

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