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