Coverage Report

Created: 2025-10-28 06:56

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
712k
#  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
 * The new BN_FLG_FIXED_TOP flag marks vectors that were not treated with
163
 * bn_correct_top, in other words such vectors are permitted to have zeros
164
 * in most significant limbs. Such vectors are used internally to achieve
165
 * execution time invariance for critical operations with private keys.
166
 * It's BN_DEBUG-only flag, because user application is not supposed to
167
 * observe it anyway. Moreover, optimizing compiler would actually remove
168
 * all operations manipulating the bit in question in non-BN_DEBUG build.
169
 */
170
#  define BN_FLG_FIXED_TOP 0x10000
171
#  ifdef BN_RAND_DEBUG
172
#   define bn_pollute(a) \
173
        do { \
174
            const BIGNUM *_bnum1 = (a); \
175
            if (_bnum1->top < _bnum1->dmax) { \
176
                unsigned char _tmp_char; \
177
                /* We cast away const without the compiler knowing, any \
178
                 * *genuinely* constant variables that aren't mutable \
179
                 * wouldn't be constructed with top!=dmax. */ \
180
                BN_ULONG *_not_const; \
181
                memcpy(&_not_const, &_bnum1->d, sizeof(_not_const)); \
182
                (void)RAND_bytes(&_tmp_char, 1); /* Debug only - safe to ignore error return */\
183
                memset(_not_const + _bnum1->top, _tmp_char, \
184
                       sizeof(*_not_const) * (_bnum1->dmax - _bnum1->top)); \
185
            } \
186
        } while(0)
187
#  else
188
#   define bn_pollute(a)
189
#  endif
190
#  define bn_check_top(a) \
191
        do { \
192
                const BIGNUM *_bnum2 = (a); \
193
                if (_bnum2 != NULL) { \
194
                        int _top = _bnum2->top; \
195
                        (void)ossl_assert((_top == 0 && !_bnum2->neg) || \
196
                                  (_top && ((_bnum2->flags & BN_FLG_FIXED_TOP) \
197
                                            || _bnum2->d[_top - 1] != 0))); \
198
                        bn_pollute(_bnum2); \
199
                } \
200
        } while(0)
201
202
#  define bn_fix_top(a)           bn_check_top(a)
203
204
#  define bn_check_size(bn, bits) bn_wcheck_size(bn, ((bits+BN_BITS2-1))/BN_BITS2)
205
#  define bn_wcheck_size(bn, words) \
206
        do { \
207
                const BIGNUM *_bnum2 = (bn); \
208
                assert((words) <= (_bnum2)->dmax && \
209
                       (words) >= (_bnum2)->top); \
210
                /* avoid unused variable warning with NDEBUG */ \
211
                (void)(_bnum2); \
212
        } while(0)
213
214
# else                          /* !BN_DEBUG */
215
216
99.5k
#  define BN_FLG_FIXED_TOP 0
217
#  define bn_pollute(a)
218
#  define bn_check_top(a)
219
#  define bn_fix_top(a)           bn_correct_top(a)
220
#  define bn_check_size(bn, bits)
221
#  define bn_wcheck_size(bn, words)
222
223
# endif
224
225
BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
226
                          BN_ULONG w);
227
BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
228
void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num);
229
BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
230
BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
231
                      int num);
232
BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
233
                      int num);
234
235
struct bignum_st {
236
    BN_ULONG *d;                /*
237
                                 * Pointer to an array of 'BN_BITS2' bit
238
                                 * chunks. These chunks are organised in
239
                                 * a least significant chunk first order.
240
                                 */
241
    int top;                    /* Index of last used d +1. */
242
    /* The next are internal book keeping for bn_expand. */
243
    int dmax;                   /* Size of the d array. */
244
    int neg;                    /* one if the number is negative */
245
    int flags;
246
};
247
248
/* Used for montgomery multiplication */
249
struct bn_mont_ctx_st {
250
    BIGNUM RR;                  /* used to convert to montgomery form,
251
                                   possibly zero-padded */
252
    BIGNUM N;                   /* The modulus */
253
    BIGNUM Ni;                  /* R*(1/R mod N) - N*Ni = 1 (Ni is only
254
                                 * stored for bignum algorithm) */
255
    BN_ULONG n0[2];             /* least significant word(s) of Ni; (type
256
                                 * changed with 0.9.9, was "BN_ULONG n0;"
257
                                 * before) */
258
    int ri;                     /* number of bits in R */
259
    int flags;
260
};
261
262
/*
263
 * Used for reciprocal division/mod functions It cannot be shared between
264
 * threads
265
 */
266
struct bn_recp_ctx_st {
267
    BIGNUM N;                   /* the divisor */
268
    BIGNUM Nr;                  /* the reciprocal */
269
    int num_bits;
270
    int shift;
271
    int flags;
272
};
273
274
/* Used for slow "generation" functions. */
275
struct bn_gencb_st {
276
    unsigned int ver;           /* To handle binary (in)compatibility */
277
    void *arg;                  /* callback-specific data */
278
    union {
279
        /* if (ver==1) - handles old style callbacks */
280
        void (*cb_1) (int, int, void *);
281
        /* if (ver==2) - new callback style */
282
        int (*cb_2) (int, int, BN_GENCB *);
283
    } cb;
284
};
285
286
/*-
287
 * BN_window_bits_for_exponent_size -- macro for sliding window mod_exp functions
288
 *
289
 *
290
 * For window size 'w' (w >= 2) and a random 'b' bits exponent,
291
 * the number of multiplications is a constant plus on average
292
 *
293
 *    2^(w-1) + (b-w)/(w+1);
294
 *
295
 * here  2^(w-1)  is for precomputing the table (we actually need
296
 * entries only for windows that have the lowest bit set), and
297
 * (b-w)/(w+1)  is an approximation for the expected number of
298
 * w-bit windows, not counting the first one.
299
 *
300
 * Thus we should use
301
 *
302
 *    w >= 6  if        b > 671
303
 *     w = 5  if  671 > b > 239
304
 *     w = 4  if  239 > b >  79
305
 *     w = 3  if   79 > b >  23
306
 *    w <= 2  if   23 > b
307
 *
308
 * (with draws in between).  Very small exponents are often selected
309
 * with low Hamming weight, so we use  w = 1  for b <= 23.
310
 */
311
# define BN_window_bits_for_exponent_size(b) \
312
0
                ((b) > 671 ? 6 : \
313
0
                 (b) > 239 ? 5 : \
314
0
                 (b) >  79 ? 4 : \
315
0
                 (b) >  23 ? 3 : 1)
316
317
/*
318
 * BN_mod_exp_mont_consttime is based on the assumption that the L1 data cache
319
 * line width of the target processor is at least the following value.
320
 */
321
0
# define MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH      ( 64 )
322
0
# define MOD_EXP_CTIME_MIN_CACHE_LINE_MASK       (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - 1)
323
324
/*
325
 * Window sizes optimized for fixed window size modular exponentiation
326
 * algorithm (BN_mod_exp_mont_consttime). To achieve the security goals of
327
 * BN_mode_exp_mont_consttime, the maximum size of the window must not exceed
328
 * log_2(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH). Window size thresholds are
329
 * defined for cache line sizes of 32 and 64, cache line sizes where
330
 * log_2(32)=5 and log_2(64)=6 respectively. A window size of 7 should only be
331
 * used on processors that have a 128 byte or greater cache line size.
332
 */
333
# if MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 64
334
335
#  define BN_window_bits_for_ctime_exponent_size(b) \
336
0
                ((b) > 937 ? 6 : \
337
0
                 (b) > 306 ? 5 : \
338
0
                 (b) >  89 ? 4 : \
339
0
                 (b) >  22 ? 3 : 1)
340
#  define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE    (6)
341
342
# elif MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 32
343
344
#  define BN_window_bits_for_ctime_exponent_size(b) \
345
                ((b) > 306 ? 5 : \
346
                 (b) >  89 ? 4 : \
347
                 (b) >  22 ? 3 : 1)
348
#  define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE    (5)
349
350
# endif
351
352
/* Pentium pro 16,16,16,32,64 */
353
/* Alpha       16,16,16,16.64 */
354
0
# define BN_MULL_SIZE_NORMAL                     (16)/* 32 */
355
0
# define BN_MUL_RECURSIVE_SIZE_NORMAL            (16)/* 32 less than */
356
0
# define BN_SQR_RECURSIVE_SIZE_NORMAL            (16)/* 32 */
357
0
# define BN_MUL_LOW_RECURSIVE_SIZE_NORMAL        (32)/* 32 */
358
# define BN_MONT_CTX_SET_SIZE_WORD               (64)/* 32 */
359
360
# if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)
361
/*
362
 * BN_UMULT_HIGH section.
363
 * If the compiler doesn't support 2*N integer type, then you have to
364
 * replace every N*N multiplication with 4 (N/2)*(N/2) accompanied by some
365
 * shifts and additions which unavoidably results in severe performance
366
 * penalties. Of course provided that the hardware is capable of producing
367
 * 2*N result... That's when you normally start considering assembler
368
 * implementation. However! It should be pointed out that some CPUs (e.g.,
369
 * PowerPC, Alpha, and IA-64) provide *separate* instruction calculating
370
 * the upper half of the product placing the result into a general
371
 * purpose register. Now *if* the compiler supports inline assembler,
372
 * then it's not impossible to implement the "bignum" routines (and have
373
 * the compiler optimize 'em) exhibiting "native" performance in C. That's
374
 * what BN_UMULT_HIGH macro is about:-) Note that more recent compilers do
375
 * support 2*64 integer type, which is also used here.
376
 */
377
#  if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__==16 && \
378
      (defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG))
379
#   define BN_UMULT_HIGH(a,b)          (((uint128_t)(a)*(b))>>64)
380
0
#   define BN_UMULT_LOHI(low,high,a,b) ({       \
381
0
        uint128_t ret=(uint128_t)(a)*(b);   \
382
0
        (high)=ret>>64; (low)=ret;      })
383
#  elif defined(__alpha) && (defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT))
384
#   if defined(__DECC)
385
#    include <c_asm.h>
386
#    define BN_UMULT_HIGH(a,b)   (BN_ULONG)asm("umulh %a0,%a1,%v0",(a),(b))
387
#   elif defined(__GNUC__) && __GNUC__>=2
388
#    define BN_UMULT_HIGH(a,b)   ({     \
389
        register BN_ULONG ret;          \
390
        asm ("umulh     %1,%2,%0"       \
391
             : "=r"(ret)                \
392
             : "r"(a), "r"(b));         \
393
        ret;                      })
394
#   endif                       /* compiler */
395
#  elif defined(_ARCH_PPC64) && defined(SIXTY_FOUR_BIT_LONG)
396
#   if defined(__GNUC__) && __GNUC__>=2
397
#    define BN_UMULT_HIGH(a,b)   ({     \
398
        register BN_ULONG ret;          \
399
        asm ("mulhdu    %0,%1,%2"       \
400
             : "=r"(ret)                \
401
             : "r"(a), "r"(b));         \
402
        ret;                      })
403
#   endif                       /* compiler */
404
#  elif (defined(__x86_64) || defined(__x86_64__)) && \
405
       (defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT))
406
#   if defined(__GNUC__) && __GNUC__>=2
407
#    define BN_UMULT_HIGH(a,b)   ({     \
408
        register BN_ULONG ret,discard;  \
409
        asm ("mulq      %3"             \
410
             : "=a"(discard),"=d"(ret)  \
411
             : "a"(a), "g"(b)           \
412
             : "cc");                   \
413
        ret;                      })
414
#    define BN_UMULT_LOHI(low,high,a,b) \
415
        asm ("mulq      %3"             \
416
                : "=a"(low),"=d"(high)  \
417
                : "a"(a),"g"(b)         \
418
                : "cc");
419
#   endif
420
#  elif (defined(_M_AMD64) || defined(_M_X64)) && defined(SIXTY_FOUR_BIT)
421
#   if defined(_MSC_VER) && _MSC_VER>=1400
422
unsigned __int64 __umulh(unsigned __int64 a, unsigned __int64 b);
423
unsigned __int64 _umul128(unsigned __int64 a, unsigned __int64 b,
424
                          unsigned __int64 *h);
425
#    pragma intrinsic(__umulh,_umul128)
426
#    define BN_UMULT_HIGH(a,b)           __umulh((a),(b))
427
#    define BN_UMULT_LOHI(low,high,a,b)  ((low)=_umul128((a),(b),&(high)))
428
#   endif
429
#  elif defined(__mips) && (defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG))
430
#   if defined(__GNUC__) && __GNUC__>=2
431
#    define BN_UMULT_HIGH(a,b) ({       \
432
        register BN_ULONG ret;          \
433
        asm ("dmultu    %1,%2"          \
434
             : "=h"(ret)                \
435
             : "r"(a), "r"(b) : "l");   \
436
        ret;                    })
437
#    define BN_UMULT_LOHI(low,high,a,b) \
438
        asm ("dmultu    %2,%3"          \
439
             : "=l"(low),"=h"(high)     \
440
             : "r"(a), "r"(b));
441
#   endif
442
#  elif defined(__aarch64__) && defined(SIXTY_FOUR_BIT_LONG)
443
#   if defined(__GNUC__) && __GNUC__>=2
444
#    define BN_UMULT_HIGH(a,b)   ({     \
445
        register BN_ULONG ret;          \
446
        asm ("umulh     %0,%1,%2"       \
447
             : "=r"(ret)                \
448
             : "r"(a), "r"(b));         \
449
        ret;                      })
450
#   endif
451
#  endif                        /* cpu */
452
# endif                         /* OPENSSL_NO_ASM */
453
454
# ifdef BN_RAND_DEBUG
455
#  define bn_clear_top2max(a) \
456
        { \
457
        int      ind = (a)->dmax - (a)->top; \
458
        BN_ULONG *ftl = &(a)->d[(a)->top-1]; \
459
        for (; ind != 0; ind--) \
460
                *(++ftl) = 0x0; \
461
        }
462
# else
463
#  define bn_clear_top2max(a)
464
# endif
465
466
# ifdef BN_LLONG
467
/*******************************************************************
468
 * Using the long long type, has to be twice as wide as BN_ULONG...
469
 */
470
#  define Lw(t)    (((BN_ULONG)(t))&BN_MASK2)
471
#  define Hw(t)    (((BN_ULONG)((t)>>BN_BITS2))&BN_MASK2)
472
473
#  define mul_add(r,a,w,c) { \
474
        BN_ULLONG t; \
475
        t=(BN_ULLONG)w * (a) + (r) + (c); \
476
        (r)= Lw(t); \
477
        (c)= Hw(t); \
478
        }
479
480
#  define mul(r,a,w,c) { \
481
        BN_ULLONG t; \
482
        t=(BN_ULLONG)w * (a) + (c); \
483
        (r)= Lw(t); \
484
        (c)= Hw(t); \
485
        }
486
487
#  define sqr(r0,r1,a) { \
488
        BN_ULLONG t; \
489
        t=(BN_ULLONG)(a)*(a); \
490
        (r0)=Lw(t); \
491
        (r1)=Hw(t); \
492
        }
493
494
# elif defined(BN_UMULT_LOHI)
495
#  define mul_add(r,a,w,c) {              \
496
        BN_ULONG high,low,ret,tmp=(a);  \
497
        ret =  (r);                     \
498
        BN_UMULT_LOHI(low,high,w,tmp);  \
499
        ret += (c);                     \
500
        (c) =  (ret<(c));               \
501
        (c) += high;                    \
502
        ret += low;                     \
503
        (c) += (ret<low);               \
504
        (r) =  ret;                     \
505
        }
506
507
#  define mul(r,a,w,c)    {               \
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
        BN_ULONG tmp=(a);               \
518
        BN_UMULT_LOHI(r0,r1,tmp,tmp);   \
519
        }
520
521
# elif defined(BN_UMULT_HIGH)
522
#  define mul_add(r,a,w,c) {              \
523
        BN_ULONG high,low,ret,tmp=(a);  \
524
        ret =  (r);                     \
525
        high=  BN_UMULT_HIGH(w,tmp);    \
526
        ret += (c);                     \
527
        low =  (w) * tmp;               \
528
        (c) =  (ret<(c));               \
529
        (c) += high;                    \
530
        ret += low;                     \
531
        (c) += (ret<low);               \
532
        (r) =  ret;                     \
533
        }
534
535
#  define mul(r,a,w,c)    {               \
536
        BN_ULONG high,low,ret,ta=(a);   \
537
        low =  (w) * ta;                \
538
        high=  BN_UMULT_HIGH(w,ta);     \
539
        ret =  low + (c);               \
540
        (c) =  high;                    \
541
        (c) += (ret<low);               \
542
        (r) =  ret;                     \
543
        }
544
545
#  define sqr(r0,r1,a)    {               \
546
        BN_ULONG tmp=(a);               \
547
        (r0) = tmp * tmp;               \
548
        (r1) = BN_UMULT_HIGH(tmp,tmp);  \
549
        }
550
551
# else
552
/*************************************************************
553
 * No long long type
554
 */
555
556
#  define LBITS(a)        ((a)&BN_MASK2l)
557
#  define HBITS(a)        (((a)>>BN_BITS4)&BN_MASK2l)
558
#  define L2HBITS(a)      (((a)<<BN_BITS4)&BN_MASK2)
559
560
#  define LLBITS(a)       ((a)&BN_MASKl)
561
#  define LHBITS(a)       (((a)>>BN_BITS2)&BN_MASKl)
562
#  define LL2HBITS(a)     ((BN_ULLONG)((a)&BN_MASKl)<<BN_BITS2)
563
564
#  define mul64(l,h,bl,bh) \
565
        { \
566
        BN_ULONG m,m1,lt,ht; \
567
 \
568
        lt=l; \
569
        ht=h; \
570
        m =(bh)*(lt); \
571
        lt=(bl)*(lt); \
572
        m1=(bl)*(ht); \
573
        ht =(bh)*(ht); \
574
        m=(m+m1)&BN_MASK2; ht += L2HBITS((BN_ULONG)(m < m1)); \
575
        ht+=HBITS(m); \
576
        m1=L2HBITS(m); \
577
        lt=(lt+m1)&BN_MASK2; ht += (lt < m1); \
578
        (l)=lt; \
579
        (h)=ht; \
580
        }
581
582
#  define sqr64(lo,ho,in) \
583
        { \
584
        BN_ULONG l,h,m; \
585
 \
586
        h=(in); \
587
        l=LBITS(h); \
588
        h=HBITS(h); \
589
        m =(l)*(h); \
590
        l*=l; \
591
        h*=h; \
592
        h+=(m&BN_MASK2h1)>>(BN_BITS4-1); \
593
        m =(m&BN_MASK2l)<<(BN_BITS4+1); \
594
        l=(l+m)&BN_MASK2; h += (l < m); \
595
        (lo)=l; \
596
        (ho)=h; \
597
        }
598
599
#  define mul_add(r,a,bl,bh,c) { \
600
        BN_ULONG l,h; \
601
 \
602
        h= (a); \
603
        l=LBITS(h); \
604
        h=HBITS(h); \
605
        mul64(l,h,(bl),(bh)); \
606
 \
607
        /* non-multiply part */ \
608
        l=(l+(c))&BN_MASK2; h += (l < (c)); \
609
        (c)=(r); \
610
        l=(l+(c))&BN_MASK2; h += (l < (c)); \
611
        (c)=h&BN_MASK2; \
612
        (r)=l; \
613
        }
614
615
#  define mul(r,a,bl,bh,c) { \
616
        BN_ULONG l,h; \
617
 \
618
        h= (a); \
619
        l=LBITS(h); \
620
        h=HBITS(h); \
621
        mul64(l,h,(bl),(bh)); \
622
 \
623
        /* non-multiply part */ \
624
        l+=(c); h += ((l&BN_MASK2) < (c)); \
625
        (c)=h&BN_MASK2; \
626
        (r)=l&BN_MASK2; \
627
        }
628
# endif                         /* !BN_LLONG */
629
630
void BN_RECP_CTX_init(BN_RECP_CTX *recp);
631
void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
632
633
void bn_init(BIGNUM *a);
634
void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb);
635
void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
636
void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
637
void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp);
638
void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a);
639
void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a);
640
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n);
641
int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl);
642
void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
643
                      int dna, int dnb, BN_ULONG *t);
644
void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b,
645
                           int n, int tna, int tnb, BN_ULONG *t);
646
void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t);
647
void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n);
648
void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
649
                          BN_ULONG *t);
650
BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
651
                           int cl, int dl);
652
int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
653
                const BN_ULONG *np, const BN_ULONG *n0, int num);
654
void bn_correct_top_consttime(BIGNUM *a);
655
BIGNUM *int_bn_mod_inverse(BIGNUM *in,
656
                           const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,
657
                           int *noinv);
658
659
static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)
660
48
{
661
48
    if (bits > (INT_MAX - BN_BITS2 + 1))
662
0
        return NULL;
663
664
48
    if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)
665
0
        return a;
666
667
48
    return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);
668
48
}
bn_conv.c:bn_expand
Line
Count
Source
660
32
{
661
32
    if (bits > (INT_MAX - BN_BITS2 + 1))
662
0
        return NULL;
663
664
32
    if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)
665
0
        return a;
666
667
32
    return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);
668
32
}
Unexecuted instantiation: bn_dh.c:bn_expand
bn_lib.c:bn_expand
Line
Count
Source
660
16
{
661
16
    if (bits > (INT_MAX - BN_BITS2 + 1))
662
0
        return NULL;
663
664
16
    if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)
665
0
        return a;
666
667
16
    return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);
668
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
669
670
int ossl_bn_check_prime(const BIGNUM *w, int checks, BN_CTX *ctx,
671
                        int do_trial_division, BN_GENCB *cb);
672
673
#endif