VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.0/crypto/bn/bn_local.h@ 99507

最後變更 在這個檔案從99507是 99366,由 vboxsync 提交於 2 年 前

openssl-3.1.0: Applied and adjusted our OpenSSL changes to 3.0.7. bugref:10418

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

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