Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/bn/bn_local.h
Line
Count
Source
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
274M
#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) || defined(OPENSSL_SYS_WIN32) || defined(linux)
84
#define BN_DIV2W
85
#endif
86
87
/*
88
 * 64-bit processor with LP64 ABI
89
 */
90
#ifdef SIXTY_FOUR_BIT_LONG
91
#define BN_ULLONG unsigned long long
92
2.24G
#define BN_BITS4 32
93
9.35G
#define BN_MASK2 (0xffffffffffffffffL)
94
2.23G
#define BN_MASK2l (0xffffffffL)
95
1.39M
#define BN_MASK2h (0xffffffff00000000L)
96
#define BN_MASK2h1 (0xffffffff80000000L)
97
3.40M
#define BN_DEC_CONV (10000000000000000000UL)
98
3.19M
#define BN_DEC_NUM 19
99
3.05M
#define BN_DEC_FMT1 "%lu"
100
350k
#define BN_DEC_FMT2 "%019lu"
101
#endif
102
103
/*
104
 * 64-bit processor other than LP64 ABI
105
 */
106
#ifdef SIXTY_FOUR_BIT
107
#undef BN_LLONG
108
#undef BN_ULLONG
109
#define BN_BITS4 32
110
#define BN_MASK2 (0xffffffffffffffffLL)
111
#define BN_MASK2l (0xffffffffL)
112
#define BN_MASK2h (0xffffffff00000000LL)
113
#define BN_MASK2h1 (0xffffffff80000000LL)
114
#define BN_DEC_CONV (10000000000000000000ULL)
115
#define BN_DEC_NUM 19
116
#define BN_DEC_FMT1 "%llu"
117
#define BN_DEC_FMT2 "%019llu"
118
#endif
119
120
#ifdef THIRTY_TWO_BIT
121
#ifdef BN_LLONG
122
#if defined(_WIN32) && !defined(__GNUC__)
123
#define BN_ULLONG unsigned __int64
124
#else
125
#define BN_ULLONG unsigned long long
126
#endif
127
#endif
128
#define BN_BITS4 16
129
#define BN_MASK2 (0xffffffffL)
130
#define BN_MASK2l (0xffff)
131
#define BN_MASK2h1 (0xffff8000L)
132
#define BN_MASK2h (0xffff0000L)
133
#define BN_DEC_CONV (1000000000L)
134
#define BN_DEC_NUM 9
135
#define BN_DEC_FMT1 "%u"
136
#define BN_DEC_FMT2 "%09u"
137
#endif
138
139
/*-
140
 * Bignum consistency macros
141
 * There is one "API" macro, bn_fix_top(), for stripping leading zeroes from
142
 * bignum data after direct manipulations on the data. There is also an
143
 * "internal" macro, bn_check_top(), for verifying that there are no leading
144
 * zeroes. Unfortunately, some auditing is required due to the fact that
145
 * bn_fix_top() has become an overabused duct-tape because bignum data is
146
 * occasionally passed around in an inconsistent state. So the following
147
 * changes have been made to sort this out;
148
 * - bn_fix_top()s implementation has been moved to bn_correct_top()
149
 * - if BN_DEBUG isn't defined, bn_fix_top() maps to bn_correct_top(), and
150
 *   bn_check_top() is as before.
151
 * - if BN_DEBUG *is* defined;
152
 *   - bn_check_top() tries to pollute unused words even if the bignum 'top' is
153
 *     consistent. (ed: only if BN_RAND_DEBUG is defined)
154
 *   - bn_fix_top() maps to bn_check_top() rather than "fixing" anything.
155
 * The idea is to have debug builds flag up inconsistent bignums when they
156
 * occur. If that occurs in a bn_fix_top(), we examine the code in question; if
157
 * the use of bn_fix_top() was appropriate (ie. it follows directly after code
158
 * that manipulates the bignum) it is converted to bn_correct_top(), and if it
159
 * was not appropriate, we convert it permanently to bn_check_top() and track
160
 * down the cause of the bug. Eventually, no internal code should be using the
161
 * bn_fix_top() macro. External applications and libraries should try this with
162
 * their own code too, both in terms of building against the openssl headers
163
 * with BN_DEBUG defined *and* linking with a version of OpenSSL built with it
164
 * defined. This not only improves external code, it provides more test
165
 * coverage for openssl's own code.
166
 */
167
168
#ifdef BN_DEBUG
169
/*
170
 * The new BN_FLG_FIXED_TOP flag marks vectors that were not treated with
171
 * bn_correct_top, in other words such vectors are permitted to have zeros
172
 * in most significant limbs. Such vectors are used internally to achieve
173
 * execution time invariance for critical operations with private keys.
174
 * It's BN_DEBUG-only flag, because user application is not supposed to
175
 * observe it anyway. Moreover, optimizing compiler would actually remove
176
 * all operations manipulating the bit in question in non-BN_DEBUG build.
177
 */
178
#define BN_FLG_FIXED_TOP 0x10000
179
#ifdef BN_RAND_DEBUG
180
#define bn_pollute(a)                                                                       \
181
    do {                                                                                    \
182
        const BIGNUM *_bnum1 = (a);                                                         \
183
        if (_bnum1->top < _bnum1->dmax) {                                                   \
184
            unsigned char _tmp_char;                                                        \
185
            /* We cast away const without the compiler knowing, any                         \
186
             * *genuinely* constant variables that aren't mutable                           \
187
             * wouldn't be constructed with top!=dmax. */                                   \
188
            BN_ULONG *_not_const;                                                           \
189
            memcpy(&_not_const, &_bnum1->d, sizeof(_not_const));                            \
190
            (void)RAND_bytes(&_tmp_char, 1); /* Debug only - safe to ignore error return */ \
191
            memset(_not_const + _bnum1->top, _tmp_char,                                     \
192
                sizeof(*_not_const) * (_bnum1->dmax - _bnum1->top));                        \
193
        }                                                                                   \
194
    } while (0)
195
#else
196
#define bn_pollute(a)
197
#endif
198
#define bn_check_top(a)                                                                                                                   \
199
    do {                                                                                                                                  \
200
        const BIGNUM *_bnum2 = (a);                                                                                                       \
201
        if (_bnum2 != NULL) {                                                                                                             \
202
            int _top = _bnum2->top;                                                                                                       \
203
            (void)ossl_assert((_top == 0 && !_bnum2->neg) || (_top && ((_bnum2->flags & BN_FLG_FIXED_TOP) || _bnum2->d[_top - 1] != 0))); \
204
            bn_pollute(_bnum2);                                                                                                           \
205
        }                                                                                                                                 \
206
    } while (0)
207
208
#define bn_fix_top(a) bn_check_top(a)
209
210
#define bn_check_size(bn, bits) bn_wcheck_size(bn, ((bits + BN_BITS2 - 1)) / BN_BITS2)
211
#define bn_wcheck_size(bn, words)                                      \
212
    do {                                                               \
213
        const BIGNUM *_bnum2 = (bn);                                   \
214
        assert((words) <= (_bnum2)->dmax && (words) >= (_bnum2)->top); \
215
        /* avoid unused variable warning with NDEBUG */                \
216
        (void)(_bnum2);                                                \
217
    } while (0)
218
219
#else /* !BN_DEBUG */
220
221
3.05G
#define BN_FLG_FIXED_TOP 0
222
#define bn_pollute(a)
223
#define bn_check_top(a)
224
#define bn_fix_top(a) bn_correct_top(a)
225
#define bn_check_size(bn, bits)
226
#define bn_wcheck_size(bn, words)
227
228
#endif
229
230
BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
231
    BN_ULONG w);
232
BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
233
void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num);
234
BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
235
BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
236
    int num);
237
BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
238
    int num);
239
240
struct bignum_st {
241
    BN_ULONG *d; /* Pointer to an array of 'BN_BITS2' bit
242
                  * chunks. */
243
    int top; /* Index of last used d +1. */
244
    /* The next are internal book keeping for bn_expand. */
245
    int dmax; /* Size of the d array. */
246
    int neg; /* one if the number is negative */
247
    int flags;
248
};
249
250
/* Used for montgomery multiplication */
251
struct bn_mont_ctx_st {
252
    int ri; /* number of bits in R */
253
    BIGNUM RR; /* used to convert to montgomery form,
254
                  possibly zero-padded */
255
    BIGNUM N; /* The modulus */
256
    BIGNUM Ni; /* R*(1/R mod N) - N*Ni = 1 (Ni is only
257
                * stored for bignum algorithm) */
258
    BN_ULONG n0[2]; /* least significant word(s) of Ni; (type
259
                     * changed with 0.9.9, was "BN_ULONG n0;"
260
                     * before) */
261
    int flags;
262
};
263
264
/*
265
 * Used for reciprocal division/mod functions It cannot be shared between
266
 * threads
267
 */
268
struct bn_recp_ctx_st {
269
    BIGNUM N; /* the divisor */
270
    BIGNUM Nr; /* the reciprocal */
271
    int num_bits;
272
    int shift;
273
    int flags;
274
};
275
276
/* Used for slow "generation" functions. */
277
struct bn_gencb_st {
278
    unsigned int ver; /* To handle binary (in)compatibility */
279
    void *arg; /* callback-specific data */
280
    union {
281
        /* if (ver==1) - handles old style callbacks */
282
        void (*cb_1)(int, int, void *);
283
        /* if (ver==2) - new callback style */
284
        int (*cb_2)(int, int, BN_GENCB *);
285
    } cb;
286
};
287
288
/*-
289
 * BN_window_bits_for_exponent_size -- macro for sliding window mod_exp functions
290
 *
291
 *
292
 * For window size 'w' (w >= 2) and a random 'b' bits exponent,
293
 * the number of multiplications is a constant plus on average
294
 *
295
 *    2^(w-1) + (b-w)/(w+1);
296
 *
297
 * here  2^(w-1)  is for precomputing the table (we actually need
298
 * entries only for windows that have the lowest bit set), and
299
 * (b-w)/(w+1)  is an approximation for the expected number of
300
 * w-bit windows, not counting the first one.
301
 *
302
 * Thus we should use
303
 *
304
 *    w >= 6  if        b > 671
305
 *     w = 5  if  671 > b > 239
306
 *     w = 4  if  239 > b >  79
307
 *     w = 3  if   79 > b >  23
308
 *    w <= 2  if   23 > b
309
 *
310
 * (with draws in between).  Very small exponents are often selected
311
 * with low Hamming weight, so we use  w = 1  for b <= 23.
312
 */
313
#define BN_window_bits_for_exponent_size(b) \
314
325k
    ((b) > 671 ? 6 : (b) > 239 ? 5          \
315
319k
            : (b) > 79         ? 4          \
316
273k
            : (b) > 23         ? 3          \
317
130k
                               : 1)
318
319
/*
320
 * BN_mod_exp_mont_consttime is based on the assumption that the L1 data cache
321
 * line width of the target processor is at least the following value.
322
 */
323
203k
#define MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH (64)
324
101k
#define MOD_EXP_CTIME_MIN_CACHE_LINE_MASK (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - 1)
325
326
/*
327
 * Window sizes optimized for fixed window size modular exponentiation
328
 * algorithm (BN_mod_exp_mont_consttime). To achieve the security goals of
329
 * BN_mode_exp_mont_consttime, the maximum size of the window must not exceed
330
 * log_2(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH). Window size thresholds are
331
 * defined for cache line sizes of 32 and 64, cache line sizes where
332
 * log_2(32)=5 and log_2(64)=6 respectively. A window size of 7 should only be
333
 * used on processors that have a 128 byte or greater cache line size.
334
 */
335
#if MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 64
336
337
#define BN_window_bits_for_ctime_exponent_size(b) \
338
101k
    ((b) > 937 ? 6 : (b) > 306 ? 5                \
339
55.1k
            : (b) > 89         ? 4                \
340
48.1k
            : (b) > 22         ? 3                \
341
41.6k
                               : 1)
342
#define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (6)
343
344
#elif MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 32
345
346
#define BN_window_bits_for_ctime_exponent_size(b) \
347
    ((b) > 306 ? 5 : (b) > 89 ? 4                 \
348
            : (b) > 22        ? 3                 \
349
                              : 1)
350
#define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (5)
351
352
#endif
353
354
/* Pentium pro 16,16,16,32,64 */
355
/* Alpha       16,16,16,16.64 */
356
34.9M
#define BN_MULL_SIZE_NORMAL (16) /* 32 */
357
62.2M
#define BN_MUL_RECURSIVE_SIZE_NORMAL (16) /* 32 less than */
358
12.1M
#define BN_SQR_RECURSIVE_SIZE_NORMAL (16) /* 32 */
359
0
#define BN_MUL_LOW_RECURSIVE_SIZE_NORMAL (32) /* 32 */
360
#define BN_MONT_CTX_SET_SIZE_WORD (64) /* 32 */
361
362
#if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)
363
/*
364
 * BN_UMULT_HIGH section.
365
 * If the compiler doesn't support 2*N integer type, then you have to
366
 * replace every N*N multiplication with 4 (N/2)*(N/2) accompanied by some
367
 * shifts and additions which unavoidably results in severe performance
368
 * penalties. Of course provided that the hardware is capable of producing
369
 * 2*N result... That's when you normally start considering assembler
370
 * implementation. However! It should be pointed out that some CPUs (e.g.,
371
 * PowerPC, Alpha, and IA-64) provide *separate* instruction calculating
372
 * the upper half of the product placing the result into a general
373
 * purpose register. Now *if* the compiler supports inline assembler,
374
 * then it's not impossible to implement the "bignum" routines (and have
375
 * the compiler optimize 'em) exhibiting "native" performance in C. That's
376
 * what BN_UMULT_HIGH macro is about:-) Note that more recent compilers do
377
 * support 2*64 integer type, which is also used here.
378
 */
379
#if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__ == 16 && (defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG))
380
#define BN_UMULT_HIGH(a, b) (((uint128_t)(a) * (b)) >> 64)
381
#define BN_UMULT_LOHI(low, high, a, b) ({       \
382
        uint128_t ret=(uint128_t)(a)*(b);   \
383
        (high)=ret>>64; (low)=ret; })
384
#elif defined(__alpha) && (defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT))
385
#if defined(__DECC)
386
#include <c_asm.h>
387
#define BN_UMULT_HIGH(a, b) (BN_ULONG) asm("umulh %a0,%a1,%v0", (a), (b))
388
#elif defined(__GNUC__) && __GNUC__ >= 2
389
#define BN_UMULT_HIGH(a, b) ({     \
390
        register BN_ULONG ret;          \
391
        asm ("umulh     %1,%2,%0"       \
392
             : "=r"(ret)                \
393
             : "r"(a), "r"(b));         \
394
        ret; })
395
#endif /* compiler */
396
#elif defined(_ARCH_PPC64) && defined(SIXTY_FOUR_BIT_LONG)
397
#if defined(__GNUC__) && __GNUC__ >= 2
398
#define BN_UMULT_HIGH(a, b) ({     \
399
        register BN_ULONG ret;          \
400
        asm ("mulhdu    %0,%1,%2"       \
401
             : "=r"(ret)                \
402
             : "r"(a), "r"(b));         \
403
        ret; })
404
#endif /* compiler */
405
#elif (defined(__x86_64) || defined(__x86_64__)) && (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_RAND_DEBUG
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
    {                                       \
475
        BN_ULLONG t;                        \
476
        t = (BN_ULLONG)w * (a) + (r) + (c); \
477
        (r) = Lw(t);                        \
478
        (c) = Hw(t);                        \
479
    }
480
481
#define mul(r, a, w, c)               \
482
    {                                 \
483
        BN_ULLONG t;                  \
484
        t = (BN_ULLONG)w * (a) + (c); \
485
        (r) = Lw(t);                  \
486
        (c) = Hw(t);                  \
487
    }
488
489
#define sqr(r0, r1, a)            \
490
    {                             \
491
        BN_ULLONG t;              \
492
        t = (BN_ULLONG)(a) * (a); \
493
        (r0) = Lw(t);             \
494
        (r1) = Hw(t);             \
495
    }
496
497
#elif defined(BN_UMULT_LOHI)
498
#define mul_add(r, a, w, c)                 \
499
    {                                       \
500
        BN_ULONG high, low, ret, tmp = (a); \
501
        ret = (r);                          \
502
        BN_UMULT_LOHI(low, high, w, tmp);   \
503
        ret += (c);                         \
504
        (c) = (ret < (c));                  \
505
        (c) += high;                        \
506
        ret += low;                         \
507
        (c) += (ret < low);                 \
508
        (r) = ret;                          \
509
    }
510
511
#define mul(r, a, w, c)                    \
512
    {                                      \
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);                \
518
        (r) = ret;                         \
519
    }
520
521
#define sqr(r0, r1, a)                   \
522
    {                                    \
523
        BN_ULONG tmp = (a);              \
524
        BN_UMULT_LOHI(r0, r1, tmp, tmp); \
525
    }
526
527
#elif defined(BN_UMULT_HIGH)
528
#define mul_add(r, a, w, c)                 \
529
    {                                       \
530
        BN_ULONG high, low, ret, tmp = (a); \
531
        ret = (r);                          \
532
        high = BN_UMULT_HIGH(w, tmp);       \
533
        ret += (c);                         \
534
        low = (w) * tmp;                    \
535
        (c) = (ret < (c));                  \
536
        (c) += high;                        \
537
        ret += low;                         \
538
        (c) += (ret < low);                 \
539
        (r) = ret;                          \
540
    }
541
542
#define mul(r, a, w, c)                    \
543
    {                                      \
544
        BN_ULONG high, low, ret, ta = (a); \
545
        low = (w) * ta;                    \
546
        high = BN_UMULT_HIGH(w, ta);       \
547
        ret = low + (c);                   \
548
        (c) = high;                        \
549
        (c) += (ret < low);                \
550
        (r) = ret;                         \
551
    }
552
553
#define sqr(r0, r1, a)                  \
554
    {                                   \
555
        BN_ULONG tmp = (a);             \
556
        (r0) = tmp * tmp;               \
557
        (r1) = BN_UMULT_HIGH(tmp, tmp); \
558
    }
559
560
#else
561
/*************************************************************
562
 * No long long type
563
 */
564
565
886M
#define LBITS(a) ((a) & BN_MASK2l)
566
1.32G
#define HBITS(a) (((a) >> BN_BITS4) & BN_MASK2l)
567
886M
#define L2HBITS(a) (((a) << BN_BITS4) & BN_MASK2)
568
569
#define LLBITS(a) ((a) & BN_MASKl)
570
#define LHBITS(a) (((a) >> BN_BITS2) & BN_MASKl)
571
#define LL2HBITS(a) ((BN_ULLONG)((a) & BN_MASKl) << BN_BITS2)
572
573
#define mul64(l, h, bl, bh)                \
574
443M
    {                                      \
575
443M
        BN_ULONG m, m1, lt, ht;            \
576
443M
                                           \
577
443M
        lt = l;                            \
578
443M
        ht = h;                            \
579
443M
        m = (bh) * (lt);                   \
580
443M
        lt = (bl) * (lt);                  \
581
443M
        m1 = (bl) * (ht);                  \
582
443M
        ht = (bh) * (ht);                  \
583
443M
        m = (m + m1) & BN_MASK2;           \
584
443M
        ht += L2HBITS((BN_ULONG)(m < m1)); \
585
443M
        ht += HBITS(m);                    \
586
443M
        m1 = L2HBITS(m);                   \
587
443M
        lt = (lt + m1) & BN_MASK2;         \
588
443M
        ht += (lt < m1);                   \
589
443M
        (l) = lt;                          \
590
443M
        (h) = ht;                          \
591
443M
    }
592
593
#define sqr64(lo, ho, in)                        \
594
    {                                            \
595
        BN_ULONG l, h, m;                        \
596
                                                 \
597
        h = (in);                                \
598
        l = LBITS(h);                            \
599
        h = HBITS(h);                            \
600
        m = (l) * (h);                           \
601
        l *= l;                                  \
602
        h *= h;                                  \
603
        h += (m & BN_MASK2h1) >> (BN_BITS4 - 1); \
604
        m = (m & BN_MASK2l) << (BN_BITS4 + 1);   \
605
        l = (l + m) & BN_MASK2;                  \
606
        h += (l < m);                            \
607
        (lo) = l;                                \
608
        (ho) = h;                                \
609
    }
610
611
#define mul_add(r, a, bl, bh, c)  \
612
    {                             \
613
        BN_ULONG l, h;            \
614
                                  \
615
        h = (a);                  \
616
        l = LBITS(h);             \
617
        h = HBITS(h);             \
618
        mul64(l, h, (bl), (bh));  \
619
                                  \
620
        /* non-multiply part */   \
621
        l = (l + (c)) & BN_MASK2; \
622
        h += (l < (c));           \
623
        (c) = (r);                \
624
        l = (l + (c)) & BN_MASK2; \
625
        h += (l < (c));           \
626
        (c) = h & BN_MASK2;       \
627
        (r) = l;                  \
628
    }
629
630
#define mul(r, a, bl, bh, c)         \
631
    {                                \
632
        BN_ULONG l, h;               \
633
                                     \
634
        h = (a);                     \
635
        l = LBITS(h);                \
636
        h = HBITS(h);                \
637
        mul64(l, h, (bl), (bh));     \
638
                                     \
639
        /* non-multiply part */      \
640
        l += (c);                    \
641
        h += ((l & BN_MASK2) < (c)); \
642
        (c) = h & BN_MASK2;          \
643
        (r) = l & BN_MASK2;          \
644
    }
645
#endif /* !BN_LLONG */
646
647
void BN_RECP_CTX_init(BN_RECP_CTX *recp);
648
void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
649
650
void bn_init(BIGNUM *a);
651
void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb);
652
void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
653
void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
654
void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp);
655
void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a);
656
void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a);
657
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n);
658
int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl);
659
void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
660
    int dna, int dnb, BN_ULONG *t);
661
void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b,
662
    int n, int tna, int tnb, BN_ULONG *t);
663
void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t);
664
void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n);
665
void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
666
    BN_ULONG *t);
667
BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
668
    int cl, int dl);
669
int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
670
    const BN_ULONG *np, const BN_ULONG *n0, int num);
671
void bn_correct_top_consttime(BIGNUM *a);
672
BIGNUM *int_bn_mod_inverse(BIGNUM *in,
673
    const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,
674
    int *noinv);
675
676
static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)
677
3.13M
{
678
3.13M
    if (bits > (INT_MAX - BN_BITS2 + 1))
679
0
        return NULL;
680
681
3.13M
    if (((bits + BN_BITS2 - 1) / BN_BITS2) <= (a)->dmax)
682
1.75M
        return a;
683
684
1.38M
    return bn_expand2((a), (bits + BN_BITS2 - 1) / BN_BITS2);
685
3.13M
}
Unexecuted instantiation: bn_conv.c:bn_expand
bn_lib.c:bn_expand
Line
Count
Source
677
3.13M
{
678
3.13M
    if (bits > (INT_MAX - BN_BITS2 + 1))
679
0
        return NULL;
680
681
3.13M
    if (((bits + BN_BITS2 - 1) / BN_BITS2) <= (a)->dmax)
682
1.75M
        return a;
683
684
1.38M
    return bn_expand2((a), (bits + BN_BITS2 - 1) / BN_BITS2);
685
3.13M
}
Unexecuted instantiation: bn_mont.c:bn_expand
Unexecuted instantiation: bn_mul.c:bn_expand
Unexecuted instantiation: bn_shift.c:bn_expand
Unexecuted instantiation: bn_sqr.c:bn_expand
Unexecuted instantiation: bn_word.c:bn_expand
Unexecuted instantiation: x86_64-gcc.c:bn_expand
Unexecuted instantiation: bn_add.c:bn_expand
Unexecuted instantiation: bn_blind.c:bn_expand
Unexecuted instantiation: bn_ctx.c:bn_expand
Unexecuted instantiation: bn_div.c:bn_expand
Unexecuted instantiation: bn_exp.c:bn_expand
Unexecuted instantiation: bn_gcd.c:bn_expand
Unexecuted instantiation: bn_intern.c:bn_expand
Unexecuted instantiation: bn_mod.c:bn_expand
Unexecuted instantiation: bn_rand.c:bn_expand
Unexecuted instantiation: bn_recp.c:bn_expand
Unexecuted instantiation: rsaz_exp.c:bn_expand
Unexecuted instantiation: rsaz_exp_x2.c:bn_expand
Unexecuted instantiation: bn_dh.c:bn_expand
Unexecuted instantiation: bn_exp2.c:bn_expand
Unexecuted instantiation: bn_kron.c:bn_expand
Unexecuted instantiation: bn_nist.c:bn_expand
Unexecuted instantiation: bn_prime.c:bn_expand
Unexecuted instantiation: bn_print.c:bn_expand
Unexecuted instantiation: bn_rsa_fips186_4.c:bn_expand
Unexecuted instantiation: bn_sqrt.c:bn_expand
Unexecuted instantiation: bn_gf2m.c:bn_expand
Unexecuted instantiation: bn_srp.c:bn_expand
686
687
int ossl_bn_check_prime(const BIGNUM *w, int checks, BN_CTX *ctx,
688
    int do_trial_division, BN_GENCB *cb);
689
690
#endif