Coverage Report

Created: 2023-06-08 06:41

/src/openssl111/crypto/bn/bn_local.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#ifndef OSSL_CRYPTO_BN_LOCAL_H
11
# define OSSL_CRYPTO_BN_LOCAL_H
12
13
/*
14
 * The EDK2 build doesn't use bn_conf.h; it sets THIRTY_TWO_BIT or
15
 * SIXTY_FOUR_BIT in its own environment since it doesn't re-run our
16
 * Configure script and needs to support both 32-bit and 64-bit.
17
 */
18
# include <openssl/opensslconf.h>
19
20
# if !defined(OPENSSL_SYS_UEFI)
21
#  include "crypto/bn_conf.h"
22
# endif
23
24
# include "crypto/bn.h"
25
26
/*
27
 * These preprocessor symbols control various aspects of the bignum headers
28
 * and library code. They're not defined by any "normal" configuration, as
29
 * they are intended for development and testing purposes. NB: defining all
30
 * three can be useful for debugging application code as well as openssl
31
 * itself. BN_DEBUG - turn on various debugging alterations to the bignum
32
 * code BN_DEBUG_RAND - uses random poisoning of unused words to trip up
33
 * mismanagement of bignum internals. You must also define BN_DEBUG.
34
 */
35
/* #define BN_DEBUG */
36
/* #define BN_DEBUG_RAND */
37
38
/*
39
 * This should limit the stack usage due to alloca to about 4K.
40
 * BN_SOFT_LIMIT is a soft limit equivalent to 2*OPENSSL_RSA_MAX_MODULUS_BITS.
41
 * Beyond that size bn_mul_mont is no longer used, and the constant time
42
 * assembler code is disabled, due to the blatant alloca and bn_mul_mont usage.
43
 * Note that bn_mul_mont does an alloca that is hidden away in assembly.
44
 * It is not recommended to do computations with numbers exceeding this limit,
45
 * since the result will be highly version dependent:
46
 * While the current OpenSSL version will use non-optimized, but safe code,
47
 * previous versions will use optimized code, that may crash due to unexpected
48
 * stack overflow, and future versions may very well turn this into a hard
49
 * limit.
50
 * Note however, that it is possible to override the size limit using
51
 * "./config -DBN_SOFT_LIMIT=<limit>" if necessary, and the O/S specific
52
 * stack limit is known and taken into consideration.
53
 */
54
# ifndef BN_SOFT_LIMIT
55
6.53M
#  define BN_SOFT_LIMIT         (4096 / BN_BYTES)
56
# endif
57
58
# ifndef OPENSSL_SMALL_FOOTPRINT
59
#  define BN_MUL_COMBA
60
#  define BN_SQR_COMBA
61
#  define BN_RECURSION
62
# endif
63
64
/*
65
 * This next option uses the C libraries (2 word)/(1 word) function. If it is
66
 * not defined, I use my C version (which is slower). The reason for this
67
 * flag is that when the particular C compiler library routine is used, and
68
 * the library is linked with a different compiler, the library is missing.
69
 * This mostly happens when the library is built with gcc and then linked
70
 * using normal cc.  This would be a common occurrence because gcc normally
71
 * produces code that is 2 times faster than system compilers for the big
72
 * number stuff. For machines with only one compiler (or shared libraries),
73
 * this should be on.  Again this in only really a problem on machines using
74
 * "long long's", are 32bit, and are not using my assembler code.
75
 */
76
# if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || \
77
    defined(OPENSSL_SYS_WIN32) || defined(linux)
78
#  define BN_DIV2W
79
# endif
80
81
/*
82
 * 64-bit processor with LP64 ABI
83
 */
84
# ifdef SIXTY_FOUR_BIT_LONG
85
#  define BN_ULLONG       unsigned long long
86
242M
#  define BN_BITS4        32
87
489M
#  define BN_MASK2        (0xffffffffffffffffL)
88
242M
#  define BN_MASK2l       (0xffffffffL)
89
147k
#  define BN_MASK2h       (0xffffffff00000000L)
90
#  define BN_MASK2h1      (0xffffffff80000000L)
91
0
#  define BN_DEC_CONV     (10000000000000000000UL)
92
0
#  define BN_DEC_NUM      19
93
0
#  define BN_DEC_FMT1     "%lu"
94
0
#  define BN_DEC_FMT2     "%019lu"
95
# endif
96
97
/*
98
 * 64-bit processor other than LP64 ABI
99
 */
100
# ifdef SIXTY_FOUR_BIT
101
#  undef BN_LLONG
102
#  undef BN_ULLONG
103
#  define BN_BITS4        32
104
#  define BN_MASK2        (0xffffffffffffffffLL)
105
#  define BN_MASK2l       (0xffffffffL)
106
#  define BN_MASK2h       (0xffffffff00000000LL)
107
#  define BN_MASK2h1      (0xffffffff80000000LL)
108
#  define BN_DEC_CONV     (10000000000000000000ULL)
109
#  define BN_DEC_NUM      19
110
#  define BN_DEC_FMT1     "%llu"
111
#  define BN_DEC_FMT2     "%019llu"
112
# endif
113
114
# ifdef THIRTY_TWO_BIT
115
#  ifdef BN_LLONG
116
#   if defined(_WIN32) && !defined(__GNUC__)
117
#    define BN_ULLONG     unsigned __int64
118
#   else
119
#    define BN_ULLONG     unsigned long long
120
#   endif
121
#  endif
122
#  define BN_BITS4        16
123
#  define BN_MASK2        (0xffffffffL)
124
#  define BN_MASK2l       (0xffff)
125
#  define BN_MASK2h1      (0xffff8000L)
126
#  define BN_MASK2h       (0xffff0000L)
127
#  define BN_DEC_CONV     (1000000000L)
128
#  define BN_DEC_NUM      9
129
#  define BN_DEC_FMT1     "%u"
130
#  define BN_DEC_FMT2     "%09u"
131
# endif
132
133
134
/*-
135
 * Bignum consistency macros
136
 * There is one "API" macro, bn_fix_top(), for stripping leading zeroes from
137
 * bignum data after direct manipulations on the data. There is also an
138
 * "internal" macro, bn_check_top(), for verifying that there are no leading
139
 * zeroes. Unfortunately, some auditing is required due to the fact that
140
 * bn_fix_top() has become an overabused duct-tape because bignum data is
141
 * occasionally passed around in an inconsistent state. So the following
142
 * changes have been made to sort this out;
143
 * - bn_fix_top()s implementation has been moved to bn_correct_top()
144
 * - if BN_DEBUG isn't defined, bn_fix_top() maps to bn_correct_top(), and
145
 *   bn_check_top() is as before.
146
 * - if BN_DEBUG *is* defined;
147
 *   - bn_check_top() tries to pollute unused words even if the bignum 'top' is
148
 *     consistent. (ed: only if BN_DEBUG_RAND is defined)
149
 *   - bn_fix_top() maps to bn_check_top() rather than "fixing" anything.
150
 * The idea is to have debug builds flag up inconsistent bignums when they
151
 * occur. If that occurs in a bn_fix_top(), we examine the code in question; if
152
 * the use of bn_fix_top() was appropriate (ie. it follows directly after code
153
 * that manipulates the bignum) it is converted to bn_correct_top(), and if it
154
 * was not appropriate, we convert it permanently to bn_check_top() and track
155
 * down the cause of the bug. Eventually, no internal code should be using the
156
 * bn_fix_top() macro. External applications and libraries should try this with
157
 * their own code too, both in terms of building against the openssl headers
158
 * with BN_DEBUG defined *and* linking with a version of OpenSSL built with it
159
 * defined. This not only improves external code, it provides more test
160
 * coverage for openssl's own code.
161
 */
162
163
# ifdef BN_DEBUG
164
/*
165
 * The new BN_FLG_FIXED_TOP flag marks vectors that were not treated with
166
 * bn_correct_top, in other words such vectors are permitted to have zeros
167
 * in most significant limbs. Such vectors are used internally to achieve
168
 * execution time invariance for critical operations with private keys.
169
 * It's BN_DEBUG-only flag, because user application is not supposed to
170
 * observe it anyway. Moreover, optimizing compiler would actually remove
171
 * all operations manipulating the bit in question in non-BN_DEBUG build.
172
 */
173
#  define BN_FLG_FIXED_TOP 0x10000
174
#  ifdef BN_DEBUG_RAND
175
#   define bn_pollute(a) \
176
        do { \
177
            const BIGNUM *_bnum1 = (a); \
178
            if (_bnum1->top < _bnum1->dmax) { \
179
                unsigned char _tmp_char; \
180
                /* We cast away const without the compiler knowing, any \
181
                 * *genuinely* constant variables that aren't mutable \
182
                 * wouldn't be constructed with top!=dmax. */ \
183
                BN_ULONG *_not_const; \
184
                memcpy(&_not_const, &_bnum1->d, sizeof(_not_const)); \
185
                RAND_bytes(&_tmp_char, 1); /* Debug only - safe to ignore error return */\
186
                memset(_not_const + _bnum1->top, _tmp_char, \
187
                       sizeof(*_not_const) * (_bnum1->dmax - _bnum1->top)); \
188
            } \
189
        } while(0)
190
#  else
191
#   define bn_pollute(a)
192
#  endif
193
#  define bn_check_top(a) \
194
        do { \
195
                const BIGNUM *_bnum2 = (a); \
196
                if (_bnum2 != NULL) { \
197
                        int _top = _bnum2->top; \
198
                        (void)ossl_assert((_top == 0 && !_bnum2->neg) || \
199
                                  (_top && ((_bnum2->flags & BN_FLG_FIXED_TOP) \
200
                                            || _bnum2->d[_top - 1] != 0))); \
201
                        bn_pollute(_bnum2); \
202
                } \
203
        } while(0)
204
205
#  define bn_fix_top(a)           bn_check_top(a)
206
207
#  define bn_check_size(bn, bits) bn_wcheck_size(bn, ((bits+BN_BITS2-1))/BN_BITS2)
208
#  define bn_wcheck_size(bn, words) \
209
        do { \
210
                const BIGNUM *_bnum2 = (bn); \
211
                assert((words) <= (_bnum2)->dmax && \
212
                       (words) >= (_bnum2)->top); \
213
                /* avoid unused variable warning with NDEBUG */ \
214
                (void)(_bnum2); \
215
        } while(0)
216
217
# else                          /* !BN_DEBUG */
218
219
211M
#  define BN_FLG_FIXED_TOP 0
220
#  define bn_pollute(a)
221
#  define bn_check_top(a)
222
#  define bn_fix_top(a)           bn_correct_top(a)
223
#  define bn_check_size(bn, bits)
224
#  define bn_wcheck_size(bn, words)
225
226
# endif
227
228
BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
229
                          BN_ULONG w);
230
BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
231
void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num);
232
BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
233
BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
234
                      int num);
235
BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
236
                      int num);
237
238
struct bignum_st {
239
    BN_ULONG *d;                /* Pointer to an array of 'BN_BITS2' bit
240
                                 * chunks. */
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
    int ri;                     /* number of bits in R */
251
    BIGNUM RR;                  /* used to convert to montgomery form,
252
                                   possibly zero-padded */
253
    BIGNUM N;                   /* The modulus */
254
    BIGNUM Ni;                  /* R*(1/R mod N) - N*Ni = 1 (Ni is only
255
                                 * stored for bignum algorithm) */
256
    BN_ULONG n0[2];             /* least significant word(s) of Ni; (type
257
                                 * changed with 0.9.9, was "BN_ULONG n0;"
258
                                 * before) */
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
9.67k
                ((b) > 671 ? 6 : \
313
9.67k
                 (b) > 239 ? 5 : \
314
9.67k
                 (b) >  79 ? 4 : \
315
7.92k
                 (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
518k
# define BN_MULL_SIZE_NORMAL                     (16)/* 32 */
355
0
# define BN_MUL_RECURSIVE_SIZE_NORMAL            (16)/* 32 less than */
356
30.7k
# 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
/*
361
 * 2011-02-22 SMS. In various places, a size_t variable or a type cast to
362
 * size_t was used to perform integer-only operations on pointers.  This
363
 * failed on VMS with 64-bit pointers (CC /POINTER_SIZE = 64) because size_t
364
 * is still only 32 bits.  What's needed in these cases is an integer type
365
 * with the same size as a pointer, which size_t is not certain to be. The
366
 * only fix here is VMS-specific.
367
 */
368
# if defined(OPENSSL_SYS_VMS)
369
#  if __INITIAL_POINTER_SIZE == 64
370
#   define PTR_SIZE_INT long long
371
#  else                         /* __INITIAL_POINTER_SIZE == 64 */
372
#   define PTR_SIZE_INT int
373
#  endif                        /* __INITIAL_POINTER_SIZE == 64 [else] */
374
# elif !defined(PTR_SIZE_INT)   /* defined(OPENSSL_SYS_VMS) */
375
48.4k
#  define PTR_SIZE_INT size_t
376
# endif                         /* defined(OPENSSL_SYS_VMS) [else] */
377
378
# if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)
379
/*
380
 * BN_UMULT_HIGH section.
381
 * If the compiler doesn't support 2*N integer type, then you have to
382
 * replace every N*N multiplication with 4 (N/2)*(N/2) accompanied by some
383
 * shifts and additions which unavoidably results in severe performance
384
 * penalties. Of course provided that the hardware is capable of producing
385
 * 2*N result... That's when you normally start considering assembler
386
 * implementation. However! It should be pointed out that some CPUs (e.g.,
387
 * PowerPC, Alpha, and IA-64) provide *separate* instruction calculating
388
 * the upper half of the product placing the result into a general
389
 * purpose register. Now *if* the compiler supports inline assembler,
390
 * then it's not impossible to implement the "bignum" routines (and have
391
 * the compiler optimize 'em) exhibiting "native" performance in C. That's
392
 * what BN_UMULT_HIGH macro is about:-) Note that more recent compilers do
393
 * support 2*64 integer type, which is also used here.
394
 */
395
#  if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__==16 && \
396
      (defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG))
397
#   define BN_UMULT_HIGH(a,b)          (((__uint128_t)(a)*(b))>>64)
398
#   define BN_UMULT_LOHI(low,high,a,b) ({       \
399
        __uint128_t ret=(__uint128_t)(a)*(b);   \
400
        (high)=ret>>64; (low)=ret;      })
401
#  elif defined(__alpha) && (defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT))
402
#   if defined(__DECC)
403
#    include <c_asm.h>
404
#    define BN_UMULT_HIGH(a,b)   (BN_ULONG)asm("umulh %a0,%a1,%v0",(a),(b))
405
#   elif defined(__GNUC__) && __GNUC__>=2
406
#    define BN_UMULT_HIGH(a,b)   ({     \
407
        register BN_ULONG ret;          \
408
        asm ("umulh     %1,%2,%0"       \
409
             : "=r"(ret)                \
410
             : "r"(a), "r"(b));         \
411
        ret;                      })
412
#   endif                       /* compiler */
413
#  elif defined(_ARCH_PPC64) && defined(SIXTY_FOUR_BIT_LONG)
414
#   if defined(__GNUC__) && __GNUC__>=2
415
#    define BN_UMULT_HIGH(a,b)   ({     \
416
        register BN_ULONG ret;          \
417
        asm ("mulhdu    %0,%1,%2"       \
418
             : "=r"(ret)                \
419
             : "r"(a), "r"(b));         \
420
        ret;                      })
421
#   endif                       /* compiler */
422
#  elif (defined(__x86_64) || defined(__x86_64__)) && \
423
       (defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT))
424
#   if defined(__GNUC__) && __GNUC__>=2
425
#    define BN_UMULT_HIGH(a,b)   ({     \
426
        register BN_ULONG ret,discard;  \
427
        asm ("mulq      %3"             \
428
             : "=a"(discard),"=d"(ret)  \
429
             : "a"(a), "g"(b)           \
430
             : "cc");                   \
431
        ret;                      })
432
#    define BN_UMULT_LOHI(low,high,a,b) \
433
        asm ("mulq      %3"             \
434
                : "=a"(low),"=d"(high)  \
435
                : "a"(a),"g"(b)         \
436
                : "cc");
437
#   endif
438
#  elif (defined(_M_AMD64) || defined(_M_X64)) && defined(SIXTY_FOUR_BIT)
439
#   if defined(_MSC_VER) && _MSC_VER>=1400
440
unsigned __int64 __umulh(unsigned __int64 a, unsigned __int64 b);
441
unsigned __int64 _umul128(unsigned __int64 a, unsigned __int64 b,
442
                          unsigned __int64 *h);
443
#    pragma intrinsic(__umulh,_umul128)
444
#    define BN_UMULT_HIGH(a,b)           __umulh((a),(b))
445
#    define BN_UMULT_LOHI(low,high,a,b)  ((low)=_umul128((a),(b),&(high)))
446
#   endif
447
#  elif defined(__mips) && (defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG))
448
#   if defined(__GNUC__) && __GNUC__>=2
449
#    define BN_UMULT_HIGH(a,b) ({       \
450
        register BN_ULONG ret;          \
451
        asm ("dmultu    %1,%2"          \
452
             : "=h"(ret)                \
453
             : "r"(a), "r"(b) : "l");   \
454
        ret;                    })
455
#    define BN_UMULT_LOHI(low,high,a,b) \
456
        asm ("dmultu    %2,%3"          \
457
             : "=l"(low),"=h"(high)     \
458
             : "r"(a), "r"(b));
459
#   endif
460
#  elif defined(__aarch64__) && defined(SIXTY_FOUR_BIT_LONG)
461
#   if defined(__GNUC__) && __GNUC__>=2
462
#    define BN_UMULT_HIGH(a,b)   ({     \
463
        register BN_ULONG ret;          \
464
        asm ("umulh     %0,%1,%2"       \
465
             : "=r"(ret)                \
466
             : "r"(a), "r"(b));         \
467
        ret;                      })
468
#   endif
469
#  endif                        /* cpu */
470
# endif                         /* OPENSSL_NO_ASM */
471
472
# ifdef BN_DEBUG_RAND
473
#  define bn_clear_top2max(a) \
474
        { \
475
        int      ind = (a)->dmax - (a)->top; \
476
        BN_ULONG *ftl = &(a)->d[(a)->top-1]; \
477
        for (; ind != 0; ind--) \
478
                *(++ftl) = 0x0; \
479
        }
480
# else
481
#  define bn_clear_top2max(a)
482
# endif
483
484
# ifdef BN_LLONG
485
/*******************************************************************
486
 * Using the long long type, has to be twice as wide as BN_ULONG...
487
 */
488
#  define Lw(t)    (((BN_ULONG)(t))&BN_MASK2)
489
#  define Hw(t)    (((BN_ULONG)((t)>>BN_BITS2))&BN_MASK2)
490
491
#  define mul_add(r,a,w,c) { \
492
        BN_ULLONG t; \
493
        t=(BN_ULLONG)w * (a) + (r) + (c); \
494
        (r)= Lw(t); \
495
        (c)= Hw(t); \
496
        }
497
498
#  define mul(r,a,w,c) { \
499
        BN_ULLONG t; \
500
        t=(BN_ULLONG)w * (a) + (c); \
501
        (r)= Lw(t); \
502
        (c)= Hw(t); \
503
        }
504
505
#  define sqr(r0,r1,a) { \
506
        BN_ULLONG t; \
507
        t=(BN_ULLONG)(a)*(a); \
508
        (r0)=Lw(t); \
509
        (r1)=Hw(t); \
510
        }
511
512
# elif defined(BN_UMULT_LOHI)
513
#  define mul_add(r,a,w,c) {              \
514
        BN_ULONG high,low,ret,tmp=(a);  \
515
        ret =  (r);                     \
516
        BN_UMULT_LOHI(low,high,w,tmp);  \
517
        ret += (c);                     \
518
        (c) =  (ret<(c));               \
519
        (c) += high;                    \
520
        ret += low;                     \
521
        (c) += (ret<low);               \
522
        (r) =  ret;                     \
523
        }
524
525
#  define mul(r,a,w,c)    {               \
526
        BN_ULONG high,low,ret,ta=(a);   \
527
        BN_UMULT_LOHI(low,high,w,ta);   \
528
        ret =  low + (c);               \
529
        (c) =  high;                    \
530
        (c) += (ret<low);               \
531
        (r) =  ret;                     \
532
        }
533
534
#  define sqr(r0,r1,a)    {               \
535
        BN_ULONG tmp=(a);               \
536
        BN_UMULT_LOHI(r0,r1,tmp,tmp);   \
537
        }
538
539
# elif defined(BN_UMULT_HIGH)
540
#  define mul_add(r,a,w,c) {              \
541
        BN_ULONG high,low,ret,tmp=(a);  \
542
        ret =  (r);                     \
543
        high=  BN_UMULT_HIGH(w,tmp);    \
544
        ret += (c);                     \
545
        low =  (w) * tmp;               \
546
        (c) =  (ret<(c));               \
547
        (c) += high;                    \
548
        ret += low;                     \
549
        (c) += (ret<low);               \
550
        (r) =  ret;                     \
551
        }
552
553
#  define mul(r,a,w,c)    {               \
554
        BN_ULONG high,low,ret,ta=(a);   \
555
        low =  (w) * ta;                \
556
        high=  BN_UMULT_HIGH(w,ta);     \
557
        ret =  low + (c);               \
558
        (c) =  high;                    \
559
        (c) += (ret<low);               \
560
        (r) =  ret;                     \
561
        }
562
563
#  define sqr(r0,r1,a)    {               \
564
        BN_ULONG tmp=(a);               \
565
        (r0) = tmp * tmp;               \
566
        (r1) = BN_UMULT_HIGH(tmp,tmp);  \
567
        }
568
569
# else
570
/*************************************************************
571
 * No long long type
572
 */
573
574
96.8M
#  define LBITS(a)        ((a)&BN_MASK2l)
575
145M
#  define HBITS(a)        (((a)>>BN_BITS4)&BN_MASK2l)
576
96.8M
#  define L2HBITS(a)      (((a)<<BN_BITS4)&BN_MASK2)
577
578
#  define LLBITS(a)       ((a)&BN_MASKl)
579
#  define LHBITS(a)       (((a)>>BN_BITS2)&BN_MASKl)
580
#  define LL2HBITS(a)     ((BN_ULLONG)((a)&BN_MASKl)<<BN_BITS2)
581
582
#  define mul64(l,h,bl,bh) \
583
48.4M
        { \
584
48.4M
        BN_ULONG m,m1,lt,ht; \
585
48.4M
 \
586
48.4M
        lt=l; \
587
48.4M
        ht=h; \
588
48.4M
        m =(bh)*(lt); \
589
48.4M
        lt=(bl)*(lt); \
590
48.4M
        m1=(bl)*(ht); \
591
48.4M
        ht =(bh)*(ht); \
592
48.4M
        m=(m+m1)&BN_MASK2; ht += L2HBITS((BN_ULONG)(m < m1)); \
593
48.4M
        ht+=HBITS(m); \
594
48.4M
        m1=L2HBITS(m); \
595
48.4M
        lt=(lt+m1)&BN_MASK2; ht += (lt < m1); \
596
48.4M
        (l)=lt; \
597
48.4M
        (h)=ht; \
598
48.4M
        }
599
600
#  define sqr64(lo,ho,in) \
601
        { \
602
        BN_ULONG l,h,m; \
603
 \
604
        h=(in); \
605
        l=LBITS(h); \
606
        h=HBITS(h); \
607
        m =(l)*(h); \
608
        l*=l; \
609
        h*=h; \
610
        h+=(m&BN_MASK2h1)>>(BN_BITS4-1); \
611
        m =(m&BN_MASK2l)<<(BN_BITS4+1); \
612
        l=(l+m)&BN_MASK2; h += (l < m); \
613
        (lo)=l; \
614
        (ho)=h; \
615
        }
616
617
#  define mul_add(r,a,bl,bh,c) { \
618
        BN_ULONG l,h; \
619
 \
620
        h= (a); \
621
        l=LBITS(h); \
622
        h=HBITS(h); \
623
        mul64(l,h,(bl),(bh)); \
624
 \
625
        /* non-multiply part */ \
626
        l=(l+(c))&BN_MASK2; h += (l < (c)); \
627
        (c)=(r); \
628
        l=(l+(c))&BN_MASK2; h += (l < (c)); \
629
        (c)=h&BN_MASK2; \
630
        (r)=l; \
631
        }
632
633
#  define mul(r,a,bl,bh,c) { \
634
        BN_ULONG l,h; \
635
 \
636
        h= (a); \
637
        l=LBITS(h); \
638
        h=HBITS(h); \
639
        mul64(l,h,(bl),(bh)); \
640
 \
641
        /* non-multiply part */ \
642
        l+=(c); h += ((l&BN_MASK2) < (c)); \
643
        (c)=h&BN_MASK2; \
644
        (r)=l&BN_MASK2; \
645
        }
646
# endif                         /* !BN_LLONG */
647
648
void BN_RECP_CTX_init(BN_RECP_CTX *recp);
649
void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
650
651
void bn_init(BIGNUM *a);
652
void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb);
653
void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
654
void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
655
void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp);
656
void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a);
657
void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a);
658
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n);
659
int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl);
660
void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
661
                      int dna, int dnb, BN_ULONG *t);
662
void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b,
663
                           int n, int tna, int tnb, BN_ULONG *t);
664
void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t);
665
void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n);
666
void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
667
                          BN_ULONG *t);
668
BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
669
                           int cl, int dl);
670
int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
671
                const BN_ULONG *np, const BN_ULONG *n0, int num);
672
void bn_correct_top_consttime(BIGNUM *a);
673
BIGNUM *int_bn_mod_inverse(BIGNUM *in,
674
                           const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,
675
                           int *noinv);
676
677
static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)
678
81.4M
{
679
81.4M
    if (bits > (INT_MAX - BN_BITS2 + 1))
680
0
        return NULL;
681
682
81.4M
    if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)
683
80.4M
        return a;
684
685
1.04M
    return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);
686
81.4M
}
Unexecuted instantiation: bn_add.c:bn_expand
Unexecuted instantiation: bn_blind.c:bn_expand
Unexecuted instantiation: bn_ctx.c:bn_expand
Unexecuted instantiation: bn_div.c:bn_expand
Unexecuted instantiation: bn_exp.c:bn_expand
Unexecuted instantiation: bn_gcd.c:bn_expand
Unexecuted instantiation: bn_intern.c:bn_expand
bn_lib.c:bn_expand
Line
Count
Source
678
81.4M
{
679
81.4M
    if (bits > (INT_MAX - BN_BITS2 + 1))
680
0
        return NULL;
681
682
81.4M
    if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)
683
80.4M
        return a;
684
685
1.04M
    return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);
686
81.4M
}
Unexecuted instantiation: bn_mod.c:bn_expand
Unexecuted instantiation: bn_mont.c:bn_expand
Unexecuted instantiation: bn_mul.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_shift.c:bn_expand
Unexecuted instantiation: bn_sqr.c:bn_expand
Unexecuted instantiation: bn_word.c:bn_expand
Unexecuted instantiation: rsaz_exp.c:bn_expand
Unexecuted instantiation: x86_64-gcc.c:bn_expand
Unexecuted instantiation: bn_dh.c:bn_expand
Unexecuted instantiation: bn_exp2.c:bn_expand
Unexecuted instantiation: bn_kron.c:bn_expand
Unexecuted instantiation: bn_nist.c:bn_expand
Unexecuted instantiation: bn_prime.c:bn_expand
Unexecuted instantiation: bn_sqrt.c:bn_expand
Unexecuted instantiation: bn_gf2m.c:bn_expand
687
688
#endif