Coverage Report

Created: 2025-12-10 06:24

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