Coverage Report

Created: 2025-11-07 06:36

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