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