VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.1/crypto/bn/bn_local.h@ 94096

最後變更 在這個檔案從94096是 94082,由 vboxsync 提交於 3 年 前

libs/openssl-3.0.1: started applying and adjusting our OpenSSL changes to 3.0.1. bugref:10128

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette