Coverage Report

Created: 2026-02-22 06:11

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