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