10Sstevel@tonic-gate /* crypto/bn/bn_lcl.h */ 20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 30Sstevel@tonic-gate * All rights reserved. 40Sstevel@tonic-gate * 50Sstevel@tonic-gate * This package is an SSL implementation written 60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com). 70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as 100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions 110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA, 120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation 130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms 140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com). 150Sstevel@tonic-gate * 160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in 170Sstevel@tonic-gate * the code are not to be removed. 180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution 190Sstevel@tonic-gate * as the author of the parts of the library used. 200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or 210Sstevel@tonic-gate * in documentation (online or textual) provided with the package. 220Sstevel@tonic-gate * 230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 240Sstevel@tonic-gate * modification, are permitted provided that the following conditions 250Sstevel@tonic-gate * are met: 260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright 270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 320Sstevel@tonic-gate * must display the following acknowledgement: 330Sstevel@tonic-gate * "This product includes cryptographic software written by 340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)" 350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library 360Sstevel@tonic-gate * being used are not cryptographic related :-). 370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from 380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement: 390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 400Sstevel@tonic-gate * 410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 510Sstevel@tonic-gate * SUCH DAMAGE. 520Sstevel@tonic-gate * 530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or 540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be 550Sstevel@tonic-gate * copied and put under another distribution licence 560Sstevel@tonic-gate * [including the GNU Public Licence.] 570Sstevel@tonic-gate */ 580Sstevel@tonic-gate /* ==================================================================== 590Sstevel@tonic-gate * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. 600Sstevel@tonic-gate * 610Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 620Sstevel@tonic-gate * modification, are permitted provided that the following conditions 630Sstevel@tonic-gate * are met: 640Sstevel@tonic-gate * 650Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 660Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 670Sstevel@tonic-gate * 680Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 690Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in 700Sstevel@tonic-gate * the documentation and/or other materials provided with the 710Sstevel@tonic-gate * distribution. 720Sstevel@tonic-gate * 730Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this 740Sstevel@tonic-gate * software must display the following acknowledgment: 750Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project 760Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 770Sstevel@tonic-gate * 780Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 790Sstevel@tonic-gate * endorse or promote products derived from this software without 800Sstevel@tonic-gate * prior written permission. For written permission, please contact 810Sstevel@tonic-gate * openssl-core@openssl.org. 820Sstevel@tonic-gate * 830Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL" 840Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written 850Sstevel@tonic-gate * permission of the OpenSSL Project. 860Sstevel@tonic-gate * 870Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following 880Sstevel@tonic-gate * acknowledgment: 890Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project 900Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 910Sstevel@tonic-gate * 920Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 930Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 940Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 950Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 960Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 970Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 980Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 990Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 1000Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 1010Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1020Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 1030Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE. 1040Sstevel@tonic-gate * ==================================================================== 1050Sstevel@tonic-gate * 1060Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young 1070Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim 1080Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com). 1090Sstevel@tonic-gate * 1100Sstevel@tonic-gate */ 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate #ifndef HEADER_BN_LCL_H 1130Sstevel@tonic-gate #define HEADER_BN_LCL_H 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate #include <openssl/bn.h> 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate #ifdef __cplusplus 1180Sstevel@tonic-gate extern "C" { 1190Sstevel@tonic-gate #endif 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate /* 1230Sstevel@tonic-gate * BN_window_bits_for_exponent_size -- macro for sliding window mod_exp functions 1240Sstevel@tonic-gate * 1250Sstevel@tonic-gate * 1260Sstevel@tonic-gate * For window size 'w' (w >= 2) and a random 'b' bits exponent, 1270Sstevel@tonic-gate * the number of multiplications is a constant plus on average 1280Sstevel@tonic-gate * 1290Sstevel@tonic-gate * 2^(w-1) + (b-w)/(w+1); 1300Sstevel@tonic-gate * 1310Sstevel@tonic-gate * here 2^(w-1) is for precomputing the table (we actually need 1320Sstevel@tonic-gate * entries only for windows that have the lowest bit set), and 1330Sstevel@tonic-gate * (b-w)/(w+1) is an approximation for the expected number of 1340Sstevel@tonic-gate * w-bit windows, not counting the first one. 1350Sstevel@tonic-gate * 1360Sstevel@tonic-gate * Thus we should use 1370Sstevel@tonic-gate * 1380Sstevel@tonic-gate * w >= 6 if b > 671 1390Sstevel@tonic-gate * w = 5 if 671 > b > 239 1400Sstevel@tonic-gate * w = 4 if 239 > b > 79 1410Sstevel@tonic-gate * w = 3 if 79 > b > 23 1420Sstevel@tonic-gate * w <= 2 if 23 > b 1430Sstevel@tonic-gate * 1440Sstevel@tonic-gate * (with draws in between). Very small exponents are often selected 1450Sstevel@tonic-gate * with low Hamming weight, so we use w = 1 for b <= 23. 1460Sstevel@tonic-gate */ 1470Sstevel@tonic-gate #if 1 1480Sstevel@tonic-gate #define BN_window_bits_for_exponent_size(b) \ 1490Sstevel@tonic-gate ((b) > 671 ? 6 : \ 1500Sstevel@tonic-gate (b) > 239 ? 5 : \ 1510Sstevel@tonic-gate (b) > 79 ? 4 : \ 1520Sstevel@tonic-gate (b) > 23 ? 3 : 1) 1530Sstevel@tonic-gate #else 1540Sstevel@tonic-gate /* Old SSLeay/OpenSSL table. 1550Sstevel@tonic-gate * Maximum window size was 5, so this table differs for b==1024; 1560Sstevel@tonic-gate * but it coincides for other interesting values (b==160, b==512). 1570Sstevel@tonic-gate */ 1580Sstevel@tonic-gate #define BN_window_bits_for_exponent_size(b) \ 1590Sstevel@tonic-gate ((b) > 255 ? 5 : \ 1600Sstevel@tonic-gate (b) > 127 ? 4 : \ 1610Sstevel@tonic-gate (b) > 17 ? 3 : 1) 1620Sstevel@tonic-gate #endif 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate 166*2139Sjp161948 /* BN_mod_exp_mont_conttime is based on the assumption that the 167*2139Sjp161948 * L1 data cache line width of the target processor is at least 168*2139Sjp161948 * the following value. 169*2139Sjp161948 */ 170*2139Sjp161948 #define MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH ( 64 ) 171*2139Sjp161948 #define MOD_EXP_CTIME_MIN_CACHE_LINE_MASK (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - 1) 172*2139Sjp161948 173*2139Sjp161948 /* Window sizes optimized for fixed window size modular exponentiation 174*2139Sjp161948 * algorithm (BN_mod_exp_mont_consttime). 175*2139Sjp161948 * 176*2139Sjp161948 * To achieve the security goals of BN_mode_exp_mont_consttime, the 177*2139Sjp161948 * maximum size of the window must not exceed 178*2139Sjp161948 * log_2(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH). 179*2139Sjp161948 * 180*2139Sjp161948 * Window size thresholds are defined for cache line sizes of 32 and 64, 181*2139Sjp161948 * cache line sizes where log_2(32)=5 and log_2(64)=6 respectively. A 182*2139Sjp161948 * window size of 7 should only be used on processors that have a 128 183*2139Sjp161948 * byte or greater cache line size. 184*2139Sjp161948 */ 185*2139Sjp161948 #if MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 64 186*2139Sjp161948 187*2139Sjp161948 # define BN_window_bits_for_ctime_exponent_size(b) \ 188*2139Sjp161948 ((b) > 937 ? 6 : \ 189*2139Sjp161948 (b) > 306 ? 5 : \ 190*2139Sjp161948 (b) > 89 ? 4 : \ 191*2139Sjp161948 (b) > 22 ? 3 : 1) 192*2139Sjp161948 # define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (6) 193*2139Sjp161948 194*2139Sjp161948 #elif MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 32 195*2139Sjp161948 196*2139Sjp161948 # define BN_window_bits_for_ctime_exponent_size(b) \ 197*2139Sjp161948 ((b) > 306 ? 5 : \ 198*2139Sjp161948 (b) > 89 ? 4 : \ 199*2139Sjp161948 (b) > 22 ? 3 : 1) 200*2139Sjp161948 # define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (5) 201*2139Sjp161948 202*2139Sjp161948 #endif 203*2139Sjp161948 204*2139Sjp161948 2050Sstevel@tonic-gate /* Pentium pro 16,16,16,32,64 */ 2060Sstevel@tonic-gate /* Alpha 16,16,16,16.64 */ 2070Sstevel@tonic-gate #define BN_MULL_SIZE_NORMAL (16) /* 32 */ 2080Sstevel@tonic-gate #define BN_MUL_RECURSIVE_SIZE_NORMAL (16) /* 32 less than */ 2090Sstevel@tonic-gate #define BN_SQR_RECURSIVE_SIZE_NORMAL (16) /* 32 */ 2100Sstevel@tonic-gate #define BN_MUL_LOW_RECURSIVE_SIZE_NORMAL (32) /* 32 */ 2110Sstevel@tonic-gate #define BN_MONT_CTX_SET_SIZE_WORD (64) /* 32 */ 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC) 2140Sstevel@tonic-gate /* 2150Sstevel@tonic-gate * BN_UMULT_HIGH section. 2160Sstevel@tonic-gate * 2170Sstevel@tonic-gate * No, I'm not trying to overwhelm you when stating that the 2180Sstevel@tonic-gate * product of N-bit numbers is 2*N bits wide:-) No, I don't expect 2190Sstevel@tonic-gate * you to be impressed when I say that if the compiler doesn't 2200Sstevel@tonic-gate * support 2*N integer type, then you have to replace every N*N 2210Sstevel@tonic-gate * multiplication with 4 (N/2)*(N/2) accompanied by some shifts 2220Sstevel@tonic-gate * and additions which unavoidably results in severe performance 2230Sstevel@tonic-gate * penalties. Of course provided that the hardware is capable of 2240Sstevel@tonic-gate * producing 2*N result... That's when you normally start 2250Sstevel@tonic-gate * considering assembler implementation. However! It should be 2260Sstevel@tonic-gate * pointed out that some CPUs (most notably Alpha, PowerPC and 2270Sstevel@tonic-gate * upcoming IA-64 family:-) provide *separate* instruction 2280Sstevel@tonic-gate * calculating the upper half of the product placing the result 2290Sstevel@tonic-gate * into a general purpose register. Now *if* the compiler supports 2300Sstevel@tonic-gate * inline assembler, then it's not impossible to implement the 2310Sstevel@tonic-gate * "bignum" routines (and have the compiler optimize 'em) 2320Sstevel@tonic-gate * exhibiting "native" performance in C. That's what BN_UMULT_HIGH 2330Sstevel@tonic-gate * macro is about:-) 2340Sstevel@tonic-gate * 2350Sstevel@tonic-gate * <appro@fy.chalmers.se> 2360Sstevel@tonic-gate */ 2370Sstevel@tonic-gate # if defined(__alpha) && (defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT)) 2380Sstevel@tonic-gate # if defined(__DECC) 2390Sstevel@tonic-gate # include <c_asm.h> 2400Sstevel@tonic-gate # define BN_UMULT_HIGH(a,b) (BN_ULONG)asm("umulh %a0,%a1,%v0",(a),(b)) 2410Sstevel@tonic-gate # elif defined(__GNUC__) 2420Sstevel@tonic-gate # define BN_UMULT_HIGH(a,b) ({ \ 2430Sstevel@tonic-gate register BN_ULONG ret; \ 2440Sstevel@tonic-gate asm ("umulh %1,%2,%0" \ 2450Sstevel@tonic-gate : "=r"(ret) \ 2460Sstevel@tonic-gate : "r"(a), "r"(b)); \ 2470Sstevel@tonic-gate ret; }) 2480Sstevel@tonic-gate # endif /* compiler */ 2490Sstevel@tonic-gate # elif defined(_ARCH_PPC) && defined(__64BIT__) && defined(SIXTY_FOUR_BIT_LONG) 2500Sstevel@tonic-gate # if defined(__GNUC__) 2510Sstevel@tonic-gate # define BN_UMULT_HIGH(a,b) ({ \ 2520Sstevel@tonic-gate register BN_ULONG ret; \ 2530Sstevel@tonic-gate asm ("mulhdu %0,%1,%2" \ 2540Sstevel@tonic-gate : "=r"(ret) \ 2550Sstevel@tonic-gate : "r"(a), "r"(b)); \ 2560Sstevel@tonic-gate ret; }) 2570Sstevel@tonic-gate # endif /* compiler */ 2580Sstevel@tonic-gate # elif defined(__x86_64) && defined(SIXTY_FOUR_BIT_LONG) 2590Sstevel@tonic-gate # if defined(__GNUC__) 2600Sstevel@tonic-gate # define BN_UMULT_HIGH(a,b) ({ \ 2610Sstevel@tonic-gate register BN_ULONG ret,discard; \ 262*2139Sjp161948 __asm__ ("mulq %3" \ 2630Sstevel@tonic-gate : "=a"(discard),"=d"(ret) \ 2640Sstevel@tonic-gate : "a"(a), "g"(b) \ 2650Sstevel@tonic-gate : "cc"); \ 2660Sstevel@tonic-gate ret; }) 2670Sstevel@tonic-gate # define BN_UMULT_LOHI(low,high,a,b) \ 268*2139Sjp161948 __asm__ ("mulq %3" \ 2690Sstevel@tonic-gate : "=a"(low),"=d"(high) \ 2700Sstevel@tonic-gate : "a"(a),"g"(b) \ 2710Sstevel@tonic-gate : "cc"); 2720Sstevel@tonic-gate # endif 273*2139Sjp161948 # elif (defined(_M_AMD64) || defined(_M_X64)) && defined(SIXTY_FOUR_BIT) 274*2139Sjp161948 # if defined(_MSC_VER) && _MSC_VER>=1400 275*2139Sjp161948 unsigned __int64 __umulh (unsigned __int64 a,unsigned __int64 b); 276*2139Sjp161948 unsigned __int64 _umul128 (unsigned __int64 a,unsigned __int64 b, 277*2139Sjp161948 unsigned __int64 *h); 278*2139Sjp161948 # pragma intrinsic(__umulh,_umul128) 279*2139Sjp161948 # define BN_UMULT_HIGH(a,b) __umulh((a),(b)) 280*2139Sjp161948 # define BN_UMULT_LOHI(low,high,a,b) ((low)=_umul128((a),(b),&(high))) 281*2139Sjp161948 # endif 2820Sstevel@tonic-gate # endif /* cpu */ 2830Sstevel@tonic-gate #endif /* OPENSSL_NO_ASM */ 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate /************************************************************* 2860Sstevel@tonic-gate * Using the long long type 2870Sstevel@tonic-gate */ 2880Sstevel@tonic-gate #define Lw(t) (((BN_ULONG)(t))&BN_MASK2) 2890Sstevel@tonic-gate #define Hw(t) (((BN_ULONG)((t)>>BN_BITS2))&BN_MASK2) 2900Sstevel@tonic-gate 291*2139Sjp161948 #ifdef BN_DEBUG_RAND 292*2139Sjp161948 #define bn_clear_top2max(a) \ 2930Sstevel@tonic-gate { \ 294*2139Sjp161948 int ind = (a)->dmax - (a)->top; \ 295*2139Sjp161948 BN_ULONG *ftl = &(a)->d[(a)->top-1]; \ 296*2139Sjp161948 for (; ind != 0; ind--) \ 297*2139Sjp161948 *(++ftl) = 0x0; \ 2980Sstevel@tonic-gate } 299*2139Sjp161948 #else 300*2139Sjp161948 #define bn_clear_top2max(a) 301*2139Sjp161948 #endif 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate #ifdef BN_LLONG 3040Sstevel@tonic-gate #define mul_add(r,a,w,c) { \ 3050Sstevel@tonic-gate BN_ULLONG t; \ 3060Sstevel@tonic-gate t=(BN_ULLONG)w * (a) + (r) + (c); \ 3070Sstevel@tonic-gate (r)= Lw(t); \ 3080Sstevel@tonic-gate (c)= Hw(t); \ 3090Sstevel@tonic-gate } 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate #define mul(r,a,w,c) { \ 3120Sstevel@tonic-gate BN_ULLONG t; \ 3130Sstevel@tonic-gate t=(BN_ULLONG)w * (a) + (c); \ 3140Sstevel@tonic-gate (r)= Lw(t); \ 3150Sstevel@tonic-gate (c)= Hw(t); \ 3160Sstevel@tonic-gate } 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate #define sqr(r0,r1,a) { \ 3190Sstevel@tonic-gate BN_ULLONG t; \ 3200Sstevel@tonic-gate t=(BN_ULLONG)(a)*(a); \ 3210Sstevel@tonic-gate (r0)=Lw(t); \ 3220Sstevel@tonic-gate (r1)=Hw(t); \ 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate 325*2139Sjp161948 #elif defined(BN_UMULT_LOHI) 326*2139Sjp161948 #define mul_add(r,a,w,c) { \ 327*2139Sjp161948 BN_ULONG high,low,ret,tmp=(a); \ 328*2139Sjp161948 ret = (r); \ 329*2139Sjp161948 BN_UMULT_LOHI(low,high,w,tmp); \ 330*2139Sjp161948 ret += (c); \ 331*2139Sjp161948 (c) = (ret<(c))?1:0; \ 332*2139Sjp161948 (c) += high; \ 333*2139Sjp161948 ret += low; \ 334*2139Sjp161948 (c) += (ret<low)?1:0; \ 335*2139Sjp161948 (r) = ret; \ 336*2139Sjp161948 } 337*2139Sjp161948 338*2139Sjp161948 #define mul(r,a,w,c) { \ 339*2139Sjp161948 BN_ULONG high,low,ret,ta=(a); \ 340*2139Sjp161948 BN_UMULT_LOHI(low,high,w,ta); \ 341*2139Sjp161948 ret = low + (c); \ 342*2139Sjp161948 (c) = high; \ 343*2139Sjp161948 (c) += (ret<low)?1:0; \ 344*2139Sjp161948 (r) = ret; \ 345*2139Sjp161948 } 346*2139Sjp161948 347*2139Sjp161948 #define sqr(r0,r1,a) { \ 348*2139Sjp161948 BN_ULONG tmp=(a); \ 349*2139Sjp161948 BN_UMULT_LOHI(r0,r1,tmp,tmp); \ 350*2139Sjp161948 } 351*2139Sjp161948 3520Sstevel@tonic-gate #elif defined(BN_UMULT_HIGH) 3530Sstevel@tonic-gate #define mul_add(r,a,w,c) { \ 3540Sstevel@tonic-gate BN_ULONG high,low,ret,tmp=(a); \ 3550Sstevel@tonic-gate ret = (r); \ 3560Sstevel@tonic-gate high= BN_UMULT_HIGH(w,tmp); \ 3570Sstevel@tonic-gate ret += (c); \ 3580Sstevel@tonic-gate low = (w) * tmp; \ 3590Sstevel@tonic-gate (c) = (ret<(c))?1:0; \ 3600Sstevel@tonic-gate (c) += high; \ 3610Sstevel@tonic-gate ret += low; \ 3620Sstevel@tonic-gate (c) += (ret<low)?1:0; \ 3630Sstevel@tonic-gate (r) = ret; \ 3640Sstevel@tonic-gate } 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate #define mul(r,a,w,c) { \ 3670Sstevel@tonic-gate BN_ULONG high,low,ret,ta=(a); \ 3680Sstevel@tonic-gate low = (w) * ta; \ 3690Sstevel@tonic-gate high= BN_UMULT_HIGH(w,ta); \ 3700Sstevel@tonic-gate ret = low + (c); \ 3710Sstevel@tonic-gate (c) = high; \ 3720Sstevel@tonic-gate (c) += (ret<low)?1:0; \ 3730Sstevel@tonic-gate (r) = ret; \ 3740Sstevel@tonic-gate } 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate #define sqr(r0,r1,a) { \ 3770Sstevel@tonic-gate BN_ULONG tmp=(a); \ 3780Sstevel@tonic-gate (r0) = tmp * tmp; \ 3790Sstevel@tonic-gate (r1) = BN_UMULT_HIGH(tmp,tmp); \ 3800Sstevel@tonic-gate } 3810Sstevel@tonic-gate 3820Sstevel@tonic-gate #else 3830Sstevel@tonic-gate /************************************************************* 3840Sstevel@tonic-gate * No long long type 3850Sstevel@tonic-gate */ 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate #define LBITS(a) ((a)&BN_MASK2l) 3880Sstevel@tonic-gate #define HBITS(a) (((a)>>BN_BITS4)&BN_MASK2l) 3890Sstevel@tonic-gate #define L2HBITS(a) (((a)<<BN_BITS4)&BN_MASK2) 3900Sstevel@tonic-gate 3910Sstevel@tonic-gate #define LLBITS(a) ((a)&BN_MASKl) 3920Sstevel@tonic-gate #define LHBITS(a) (((a)>>BN_BITS2)&BN_MASKl) 3930Sstevel@tonic-gate #define LL2HBITS(a) ((BN_ULLONG)((a)&BN_MASKl)<<BN_BITS2) 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate #define mul64(l,h,bl,bh) \ 3960Sstevel@tonic-gate { \ 3970Sstevel@tonic-gate BN_ULONG m,m1,lt,ht; \ 3980Sstevel@tonic-gate \ 3990Sstevel@tonic-gate lt=l; \ 4000Sstevel@tonic-gate ht=h; \ 4010Sstevel@tonic-gate m =(bh)*(lt); \ 4020Sstevel@tonic-gate lt=(bl)*(lt); \ 4030Sstevel@tonic-gate m1=(bl)*(ht); \ 4040Sstevel@tonic-gate ht =(bh)*(ht); \ 4050Sstevel@tonic-gate m=(m+m1)&BN_MASK2; if (m < m1) ht+=L2HBITS((BN_ULONG)1); \ 4060Sstevel@tonic-gate ht+=HBITS(m); \ 4070Sstevel@tonic-gate m1=L2HBITS(m); \ 4080Sstevel@tonic-gate lt=(lt+m1)&BN_MASK2; if (lt < m1) ht++; \ 4090Sstevel@tonic-gate (l)=lt; \ 4100Sstevel@tonic-gate (h)=ht; \ 4110Sstevel@tonic-gate } 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate #define sqr64(lo,ho,in) \ 4140Sstevel@tonic-gate { \ 4150Sstevel@tonic-gate BN_ULONG l,h,m; \ 4160Sstevel@tonic-gate \ 4170Sstevel@tonic-gate h=(in); \ 4180Sstevel@tonic-gate l=LBITS(h); \ 4190Sstevel@tonic-gate h=HBITS(h); \ 4200Sstevel@tonic-gate m =(l)*(h); \ 4210Sstevel@tonic-gate l*=l; \ 4220Sstevel@tonic-gate h*=h; \ 4230Sstevel@tonic-gate h+=(m&BN_MASK2h1)>>(BN_BITS4-1); \ 4240Sstevel@tonic-gate m =(m&BN_MASK2l)<<(BN_BITS4+1); \ 4250Sstevel@tonic-gate l=(l+m)&BN_MASK2; if (l < m) h++; \ 4260Sstevel@tonic-gate (lo)=l; \ 4270Sstevel@tonic-gate (ho)=h; \ 4280Sstevel@tonic-gate } 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate #define mul_add(r,a,bl,bh,c) { \ 4310Sstevel@tonic-gate BN_ULONG l,h; \ 4320Sstevel@tonic-gate \ 4330Sstevel@tonic-gate h= (a); \ 4340Sstevel@tonic-gate l=LBITS(h); \ 4350Sstevel@tonic-gate h=HBITS(h); \ 4360Sstevel@tonic-gate mul64(l,h,(bl),(bh)); \ 4370Sstevel@tonic-gate \ 4380Sstevel@tonic-gate /* non-multiply part */ \ 4390Sstevel@tonic-gate l=(l+(c))&BN_MASK2; if (l < (c)) h++; \ 4400Sstevel@tonic-gate (c)=(r); \ 4410Sstevel@tonic-gate l=(l+(c))&BN_MASK2; if (l < (c)) h++; \ 4420Sstevel@tonic-gate (c)=h&BN_MASK2; \ 4430Sstevel@tonic-gate (r)=l; \ 4440Sstevel@tonic-gate } 4450Sstevel@tonic-gate 4460Sstevel@tonic-gate #define mul(r,a,bl,bh,c) { \ 4470Sstevel@tonic-gate BN_ULONG l,h; \ 4480Sstevel@tonic-gate \ 4490Sstevel@tonic-gate h= (a); \ 4500Sstevel@tonic-gate l=LBITS(h); \ 4510Sstevel@tonic-gate h=HBITS(h); \ 4520Sstevel@tonic-gate mul64(l,h,(bl),(bh)); \ 4530Sstevel@tonic-gate \ 4540Sstevel@tonic-gate /* non-multiply part */ \ 4550Sstevel@tonic-gate l+=(c); if ((l&BN_MASK2) < (c)) h++; \ 4560Sstevel@tonic-gate (c)=h&BN_MASK2; \ 4570Sstevel@tonic-gate (r)=l&BN_MASK2; \ 4580Sstevel@tonic-gate } 4590Sstevel@tonic-gate #endif /* !BN_LLONG */ 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate void bn_mul_normal(BN_ULONG *r,BN_ULONG *a,int na,BN_ULONG *b,int nb); 4620Sstevel@tonic-gate void bn_mul_comba8(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b); 4630Sstevel@tonic-gate void bn_mul_comba4(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b); 4640Sstevel@tonic-gate void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp); 4650Sstevel@tonic-gate void bn_sqr_comba8(BN_ULONG *r,const BN_ULONG *a); 4660Sstevel@tonic-gate void bn_sqr_comba4(BN_ULONG *r,const BN_ULONG *a); 4670Sstevel@tonic-gate int bn_cmp_words(const BN_ULONG *a,const BN_ULONG *b,int n); 4680Sstevel@tonic-gate int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, 4690Sstevel@tonic-gate int cl, int dl); 470*2139Sjp161948 void bn_mul_recursive(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b,int n2, 471*2139Sjp161948 int dna,int dnb,BN_ULONG *t); 472*2139Sjp161948 void bn_mul_part_recursive(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b, 473*2139Sjp161948 int n,int tna,int tnb,BN_ULONG *t); 474*2139Sjp161948 void bn_sqr_recursive(BN_ULONG *r,const BN_ULONG *a, int n2, BN_ULONG *t); 475*2139Sjp161948 void bn_mul_low_normal(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b, int n); 4760Sstevel@tonic-gate void bn_mul_low_recursive(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b,int n2, 4770Sstevel@tonic-gate BN_ULONG *t); 4780Sstevel@tonic-gate void bn_mul_high(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b,BN_ULONG *l,int n2, 4790Sstevel@tonic-gate BN_ULONG *t); 480*2139Sjp161948 BN_ULONG bn_add_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, 481*2139Sjp161948 int cl, int dl); 482*2139Sjp161948 BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, 483*2139Sjp161948 int cl, int dl); 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate #ifdef __cplusplus 4860Sstevel@tonic-gate } 4870Sstevel@tonic-gate #endif 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate #endif 490