2
0

bn_local.h 25 KB

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