bn_lcl.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. # include <assert.h>
  135. # ifdef BN_DEBUG_RAND
  136. # define bn_pollute(a) \
  137. do { \
  138. const BIGNUM *_bnum1 = (a); \
  139. if (_bnum1->top < _bnum1->dmax) { \
  140. unsigned char _tmp_char; \
  141. /* We cast away const without the compiler knowing, any \
  142. * *genuinely* constant variables that aren't mutable \
  143. * wouldn't be constructed with top!=dmax. */ \
  144. BN_ULONG *_not_const; \
  145. memcpy(&_not_const, &_bnum1->d, sizeof(_not_const)); \
  146. RAND_bytes(&_tmp_char, 1); /* Debug only - safe to ignore error return */\
  147. memset(_not_const + _bnum1->top, _tmp_char, \
  148. sizeof(*_not_const) * (_bnum1->dmax - _bnum1->top)); \
  149. } \
  150. } while(0)
  151. # else
  152. # define bn_pollute(a)
  153. # endif
  154. # define bn_check_top(a) \
  155. do { \
  156. const BIGNUM *_bnum2 = (a); \
  157. if (_bnum2 != NULL) { \
  158. assert(((_bnum2->top == 0) && !_bnum2->neg) || \
  159. (_bnum2->top && (_bnum2->d[_bnum2->top - 1] != 0))); \
  160. bn_pollute(_bnum2); \
  161. } \
  162. } while(0)
  163. # define bn_fix_top(a) bn_check_top(a)
  164. # define bn_check_size(bn, bits) bn_wcheck_size(bn, ((bits+BN_BITS2-1))/BN_BITS2)
  165. # define bn_wcheck_size(bn, words) \
  166. do { \
  167. const BIGNUM *_bnum2 = (bn); \
  168. assert((words) <= (_bnum2)->dmax && \
  169. (words) >= (_bnum2)->top); \
  170. /* avoid unused variable warning with NDEBUG */ \
  171. (void)(_bnum2); \
  172. } while(0)
  173. # else /* !BN_DEBUG */
  174. # define bn_pollute(a)
  175. # define bn_check_top(a)
  176. # define bn_fix_top(a) bn_correct_top(a)
  177. # define bn_check_size(bn, bits)
  178. # define bn_wcheck_size(bn, words)
  179. # endif
  180. BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
  181. BN_ULONG w);
  182. BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
  183. void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num);
  184. BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
  185. BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  186. int num);
  187. BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  188. int num);
  189. struct bignum_st {
  190. BN_ULONG *d; /* Pointer to an array of 'BN_BITS2' bit
  191. * chunks. */
  192. int top; /* Index of last used d +1. */
  193. /* The next are internal book keeping for bn_expand. */
  194. int dmax; /* Size of the d array. */
  195. int neg; /* one if the number is negative */
  196. int flags;
  197. };
  198. /* Used for montgomery multiplication */
  199. struct bn_mont_ctx_st {
  200. int ri; /* number of bits in R */
  201. BIGNUM RR; /* used to convert to montgomery form */
  202. BIGNUM N; /* The modulus */
  203. BIGNUM Ni; /* R*(1/R mod N) - N*Ni = 1 (Ni is only
  204. * stored for bignum algorithm) */
  205. BN_ULONG n0[2]; /* least significant word(s) of Ni; (type
  206. * changed with 0.9.9, was "BN_ULONG n0;"
  207. * before) */
  208. int flags;
  209. };
  210. /*
  211. * Used for reciprocal division/mod functions It cannot be shared between
  212. * threads
  213. */
  214. struct bn_recp_ctx_st {
  215. BIGNUM N; /* the divisor */
  216. BIGNUM Nr; /* the reciprocal */
  217. int num_bits;
  218. int shift;
  219. int flags;
  220. };
  221. /* Used for slow "generation" functions. */
  222. struct bn_gencb_st {
  223. unsigned int ver; /* To handle binary (in)compatibility */
  224. void *arg; /* callback-specific data */
  225. union {
  226. /* if (ver==1) - handles old style callbacks */
  227. void (*cb_1) (int, int, void *);
  228. /* if (ver==2) - new callback style */
  229. int (*cb_2) (int, int, BN_GENCB *);
  230. } cb;
  231. };
  232. /*-
  233. * BN_window_bits_for_exponent_size -- macro for sliding window mod_exp functions
  234. *
  235. *
  236. * For window size 'w' (w >= 2) and a random 'b' bits exponent,
  237. * the number of multiplications is a constant plus on average
  238. *
  239. * 2^(w-1) + (b-w)/(w+1);
  240. *
  241. * here 2^(w-1) is for precomputing the table (we actually need
  242. * entries only for windows that have the lowest bit set), and
  243. * (b-w)/(w+1) is an approximation for the expected number of
  244. * w-bit windows, not counting the first one.
  245. *
  246. * Thus we should use
  247. *
  248. * w >= 6 if b > 671
  249. * w = 5 if 671 > b > 239
  250. * w = 4 if 239 > b > 79
  251. * w = 3 if 79 > b > 23
  252. * w <= 2 if 23 > b
  253. *
  254. * (with draws in between). Very small exponents are often selected
  255. * with low Hamming weight, so we use w = 1 for b <= 23.
  256. */
  257. # define BN_window_bits_for_exponent_size(b) \
  258. ((b) > 671 ? 6 : \
  259. (b) > 239 ? 5 : \
  260. (b) > 79 ? 4 : \
  261. (b) > 23 ? 3 : 1)
  262. /*
  263. * BN_mod_exp_mont_conttime is based on the assumption that the L1 data cache
  264. * line width of the target processor is at least the following value.
  265. */
  266. # define MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH ( 64 )
  267. # define MOD_EXP_CTIME_MIN_CACHE_LINE_MASK (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - 1)
  268. /*
  269. * Window sizes optimized for fixed window size modular exponentiation
  270. * algorithm (BN_mod_exp_mont_consttime). To achieve the security goals of
  271. * BN_mode_exp_mont_consttime, the maximum size of the window must not exceed
  272. * log_2(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH). Window size thresholds are
  273. * defined for cache line sizes of 32 and 64, cache line sizes where
  274. * log_2(32)=5 and log_2(64)=6 respectively. A window size of 7 should only be
  275. * used on processors that have a 128 byte or greater cache line size.
  276. */
  277. # if MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 64
  278. # define BN_window_bits_for_ctime_exponent_size(b) \
  279. ((b) > 937 ? 6 : \
  280. (b) > 306 ? 5 : \
  281. (b) > 89 ? 4 : \
  282. (b) > 22 ? 3 : 1)
  283. # define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (6)
  284. # elif MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 32
  285. # define BN_window_bits_for_ctime_exponent_size(b) \
  286. ((b) > 306 ? 5 : \
  287. (b) > 89 ? 4 : \
  288. (b) > 22 ? 3 : 1)
  289. # define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (5)
  290. # endif
  291. /* Pentium pro 16,16,16,32,64 */
  292. /* Alpha 16,16,16,16.64 */
  293. # define BN_MULL_SIZE_NORMAL (16)/* 32 */
  294. # define BN_MUL_RECURSIVE_SIZE_NORMAL (16)/* 32 less than */
  295. # define BN_SQR_RECURSIVE_SIZE_NORMAL (16)/* 32 */
  296. # define BN_MUL_LOW_RECURSIVE_SIZE_NORMAL (32)/* 32 */
  297. # define BN_MONT_CTX_SET_SIZE_WORD (64)/* 32 */
  298. /*
  299. * 2011-02-22 SMS. In various places, a size_t variable or a type cast to
  300. * size_t was used to perform integer-only operations on pointers. This
  301. * failed on VMS with 64-bit pointers (CC /POINTER_SIZE = 64) because size_t
  302. * is still only 32 bits. What's needed in these cases is an integer type
  303. * with the same size as a pointer, which size_t is not certain to be. The
  304. * only fix here is VMS-specific.
  305. */
  306. # if defined(OPENSSL_SYS_VMS)
  307. # if __INITIAL_POINTER_SIZE == 64
  308. # define PTR_SIZE_INT long long
  309. # else /* __INITIAL_POINTER_SIZE == 64 */
  310. # define PTR_SIZE_INT int
  311. # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
  312. # elif !defined(PTR_SIZE_INT) /* defined(OPENSSL_SYS_VMS) */
  313. # define PTR_SIZE_INT size_t
  314. # endif /* defined(OPENSSL_SYS_VMS) [else] */
  315. # if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)
  316. /*
  317. * BN_UMULT_HIGH section.
  318. * If the compiler doesn't support 2*N integer type, then you have to
  319. * replace every N*N multiplication with 4 (N/2)*(N/2) accompanied by some
  320. * shifts and additions which unavoidably results in severe performance
  321. * penalties. Of course provided that the hardware is capable of producing
  322. * 2*N result... That's when you normally start considering assembler
  323. * implementation. However! It should be pointed out that some CPUs (e.g.,
  324. * PowerPC, Alpha, and IA-64) provide *separate* instruction calculating
  325. * the upper half of the product placing the result into a general
  326. * purpose register. Now *if* the compiler supports inline assembler,
  327. * then it's not impossible to implement the "bignum" routines (and have
  328. * the compiler optimize 'em) exhibiting "native" performance in C. That's
  329. * what BN_UMULT_HIGH macro is about:-) Note that more recent compilers do
  330. * support 2*64 integer type, which is also used here.
  331. */
  332. # if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__==16 && \
  333. (defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG))
  334. # define BN_UMULT_HIGH(a,b) (((__uint128_t)(a)*(b))>>64)
  335. # define BN_UMULT_LOHI(low,high,a,b) ({ \
  336. __uint128_t ret=(__uint128_t)(a)*(b); \
  337. (high)=ret>>64; (low)=ret; })
  338. # elif 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_PPC64) && 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. # define BN_UMULT_HIGH(a,b) ({ \
  387. register BN_ULONG ret; \
  388. asm ("dmultu %1,%2" \
  389. : "=h"(ret) \
  390. : "r"(a), "r"(b) : "l"); \
  391. ret; })
  392. # define BN_UMULT_LOHI(low,high,a,b) \
  393. asm ("dmultu %2,%3" \
  394. : "=l"(low),"=h"(high) \
  395. : "r"(a), "r"(b));
  396. # endif
  397. # elif defined(__aarch64__) && defined(SIXTY_FOUR_BIT_LONG)
  398. # if defined(__GNUC__) && __GNUC__>=2
  399. # define BN_UMULT_HIGH(a,b) ({ \
  400. register BN_ULONG ret; \
  401. asm ("umulh %0,%1,%2" \
  402. : "=r"(ret) \
  403. : "r"(a), "r"(b)); \
  404. ret; })
  405. # endif
  406. # endif /* cpu */
  407. # endif /* OPENSSL_NO_ASM */
  408. # ifdef BN_DEBUG_RAND
  409. # define bn_clear_top2max(a) \
  410. { \
  411. int ind = (a)->dmax - (a)->top; \
  412. BN_ULONG *ftl = &(a)->d[(a)->top-1]; \
  413. for (; ind != 0; ind--) \
  414. *(++ftl) = 0x0; \
  415. }
  416. # else
  417. # define bn_clear_top2max(a)
  418. # endif
  419. # ifdef BN_LLONG
  420. /*******************************************************************
  421. * Using the long long type, has to be twice as wide as BN_ULONG...
  422. */
  423. # define Lw(t) (((BN_ULONG)(t))&BN_MASK2)
  424. # define Hw(t) (((BN_ULONG)((t)>>BN_BITS2))&BN_MASK2)
  425. # define mul_add(r,a,w,c) { \
  426. BN_ULLONG t; \
  427. t=(BN_ULLONG)w * (a) + (r) + (c); \
  428. (r)= Lw(t); \
  429. (c)= Hw(t); \
  430. }
  431. # define mul(r,a,w,c) { \
  432. BN_ULLONG t; \
  433. t=(BN_ULLONG)w * (a) + (c); \
  434. (r)= Lw(t); \
  435. (c)= Hw(t); \
  436. }
  437. # define sqr(r0,r1,a) { \
  438. BN_ULLONG t; \
  439. t=(BN_ULLONG)(a)*(a); \
  440. (r0)=Lw(t); \
  441. (r1)=Hw(t); \
  442. }
  443. # elif defined(BN_UMULT_LOHI)
  444. # define mul_add(r,a,w,c) { \
  445. BN_ULONG high,low,ret,tmp=(a); \
  446. ret = (r); \
  447. BN_UMULT_LOHI(low,high,w,tmp); \
  448. ret += (c); \
  449. (c) = (ret<(c))?1:0; \
  450. (c) += high; \
  451. ret += low; \
  452. (c) += (ret<low)?1:0; \
  453. (r) = ret; \
  454. }
  455. # define mul(r,a,w,c) { \
  456. BN_ULONG high,low,ret,ta=(a); \
  457. BN_UMULT_LOHI(low,high,w,ta); \
  458. ret = low + (c); \
  459. (c) = high; \
  460. (c) += (ret<low)?1:0; \
  461. (r) = ret; \
  462. }
  463. # define sqr(r0,r1,a) { \
  464. BN_ULONG tmp=(a); \
  465. BN_UMULT_LOHI(r0,r1,tmp,tmp); \
  466. }
  467. # elif defined(BN_UMULT_HIGH)
  468. # define mul_add(r,a,w,c) { \
  469. BN_ULONG high,low,ret,tmp=(a); \
  470. ret = (r); \
  471. high= BN_UMULT_HIGH(w,tmp); \
  472. ret += (c); \
  473. low = (w) * tmp; \
  474. (c) = (ret<(c))?1:0; \
  475. (c) += high; \
  476. ret += low; \
  477. (c) += (ret<low)?1:0; \
  478. (r) = ret; \
  479. }
  480. # define mul(r,a,w,c) { \
  481. BN_ULONG high,low,ret,ta=(a); \
  482. low = (w) * ta; \
  483. high= BN_UMULT_HIGH(w,ta); \
  484. ret = low + (c); \
  485. (c) = high; \
  486. (c) += (ret<low)?1:0; \
  487. (r) = ret; \
  488. }
  489. # define sqr(r0,r1,a) { \
  490. BN_ULONG tmp=(a); \
  491. (r0) = tmp * tmp; \
  492. (r1) = BN_UMULT_HIGH(tmp,tmp); \
  493. }
  494. # else
  495. /*************************************************************
  496. * No long long type
  497. */
  498. # define LBITS(a) ((a)&BN_MASK2l)
  499. # define HBITS(a) (((a)>>BN_BITS4)&BN_MASK2l)
  500. # define L2HBITS(a) (((a)<<BN_BITS4)&BN_MASK2)
  501. # define LLBITS(a) ((a)&BN_MASKl)
  502. # define LHBITS(a) (((a)>>BN_BITS2)&BN_MASKl)
  503. # define LL2HBITS(a) ((BN_ULLONG)((a)&BN_MASKl)<<BN_BITS2)
  504. # define mul64(l,h,bl,bh) \
  505. { \
  506. BN_ULONG m,m1,lt,ht; \
  507. \
  508. lt=l; \
  509. ht=h; \
  510. m =(bh)*(lt); \
  511. lt=(bl)*(lt); \
  512. m1=(bl)*(ht); \
  513. ht =(bh)*(ht); \
  514. m=(m+m1)&BN_MASK2; if (m < m1) ht+=L2HBITS((BN_ULONG)1); \
  515. ht+=HBITS(m); \
  516. m1=L2HBITS(m); \
  517. lt=(lt+m1)&BN_MASK2; if (lt < m1) ht++; \
  518. (l)=lt; \
  519. (h)=ht; \
  520. }
  521. # define sqr64(lo,ho,in) \
  522. { \
  523. BN_ULONG l,h,m; \
  524. \
  525. h=(in); \
  526. l=LBITS(h); \
  527. h=HBITS(h); \
  528. m =(l)*(h); \
  529. l*=l; \
  530. h*=h; \
  531. h+=(m&BN_MASK2h1)>>(BN_BITS4-1); \
  532. m =(m&BN_MASK2l)<<(BN_BITS4+1); \
  533. l=(l+m)&BN_MASK2; if (l < m) h++; \
  534. (lo)=l; \
  535. (ho)=h; \
  536. }
  537. # define mul_add(r,a,bl,bh,c) { \
  538. BN_ULONG l,h; \
  539. \
  540. h= (a); \
  541. l=LBITS(h); \
  542. h=HBITS(h); \
  543. mul64(l,h,(bl),(bh)); \
  544. \
  545. /* non-multiply part */ \
  546. l=(l+(c))&BN_MASK2; if (l < (c)) h++; \
  547. (c)=(r); \
  548. l=(l+(c))&BN_MASK2; if (l < (c)) h++; \
  549. (c)=h&BN_MASK2; \
  550. (r)=l; \
  551. }
  552. # define mul(r,a,bl,bh,c) { \
  553. BN_ULONG l,h; \
  554. \
  555. h= (a); \
  556. l=LBITS(h); \
  557. h=HBITS(h); \
  558. mul64(l,h,(bl),(bh)); \
  559. \
  560. /* non-multiply part */ \
  561. l+=(c); if ((l&BN_MASK2) < (c)) h++; \
  562. (c)=h&BN_MASK2; \
  563. (r)=l&BN_MASK2; \
  564. }
  565. # endif /* !BN_LLONG */
  566. void BN_RECP_CTX_init(BN_RECP_CTX *recp);
  567. void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
  568. void bn_init(BIGNUM *a);
  569. void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb);
  570. void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
  571. void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
  572. void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp);
  573. void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a);
  574. void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a);
  575. int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n);
  576. int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl);
  577. void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
  578. int dna, int dnb, BN_ULONG *t);
  579. void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b,
  580. int n, int tna, int tnb, BN_ULONG *t);
  581. void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t);
  582. void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n);
  583. void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
  584. BN_ULONG *t);
  585. BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  586. int cl, int dl);
  587. int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  588. const BN_ULONG *np, const BN_ULONG *n0, int num);
  589. BIGNUM *int_bn_mod_inverse(BIGNUM *in,
  590. const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,
  591. int *noinv);
  592. int bn_probable_prime_dh(BIGNUM *rnd, int bits,
  593. const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx);
  594. static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)
  595. {
  596. if (bits > (INT_MAX - BN_BITS2 + 1))
  597. return NULL;
  598. if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)
  599. return a;
  600. return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);
  601. }
  602. #ifdef __cplusplus
  603. }
  604. #endif
  605. #endif