sl@0: /* crypto/bn/bn_lcl.h */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: sl@0: #ifndef HEADER_BN_LCL_H sl@0: #define HEADER_BN_LCL_H sl@0: sl@0: #include sl@0: sl@0: #ifdef __cplusplus sl@0: extern "C" { sl@0: #endif sl@0: sl@0: sl@0: /* sl@0: * BN_window_bits_for_exponent_size -- macro for sliding window mod_exp functions sl@0: * sl@0: * sl@0: * For window size 'w' (w >= 2) and a random 'b' bits exponent, sl@0: * the number of multiplications is a constant plus on average sl@0: * sl@0: * 2^(w-1) + (b-w)/(w+1); sl@0: * sl@0: * here 2^(w-1) is for precomputing the table (we actually need sl@0: * entries only for windows that have the lowest bit set), and sl@0: * (b-w)/(w+1) is an approximation for the expected number of sl@0: * w-bit windows, not counting the first one. sl@0: * sl@0: * Thus we should use sl@0: * sl@0: * w >= 6 if b > 671 sl@0: * w = 5 if 671 > b > 239 sl@0: * w = 4 if 239 > b > 79 sl@0: * w = 3 if 79 > b > 23 sl@0: * w <= 2 if 23 > b sl@0: * sl@0: * (with draws in between). Very small exponents are often selected sl@0: * with low Hamming weight, so we use w = 1 for b <= 23. sl@0: */ sl@0: #if 1 sl@0: #define BN_window_bits_for_exponent_size(b) \ sl@0: ((b) > 671 ? 6 : \ sl@0: (b) > 239 ? 5 : \ sl@0: (b) > 79 ? 4 : \ sl@0: (b) > 23 ? 3 : 1) sl@0: #else sl@0: /* Old SSLeay/OpenSSL table. sl@0: * Maximum window size was 5, so this table differs for b==1024; sl@0: * but it coincides for other interesting values (b==160, b==512). sl@0: */ sl@0: #define BN_window_bits_for_exponent_size(b) \ sl@0: ((b) > 255 ? 5 : \ sl@0: (b) > 127 ? 4 : \ sl@0: (b) > 17 ? 3 : 1) sl@0: #endif sl@0: sl@0: sl@0: sl@0: /* BN_mod_exp_mont_conttime is based on the assumption that the sl@0: * L1 data cache line width of the target processor is at least sl@0: * the following value. sl@0: */ sl@0: #define MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH ( 64 ) sl@0: #define MOD_EXP_CTIME_MIN_CACHE_LINE_MASK (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - 1) sl@0: sl@0: /* Window sizes optimized for fixed window size modular exponentiation sl@0: * algorithm (BN_mod_exp_mont_consttime). sl@0: * sl@0: * To achieve the security goals of BN_mode_exp_mont_consttime, the sl@0: * maximum size of the window must not exceed sl@0: * log_2(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH). sl@0: * sl@0: * Window size thresholds are defined for cache line sizes of 32 and 64, sl@0: * cache line sizes where log_2(32)=5 and log_2(64)=6 respectively. A sl@0: * window size of 7 should only be used on processors that have a 128 sl@0: * byte or greater cache line size. sl@0: */ sl@0: #if MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 64 sl@0: sl@0: # define BN_window_bits_for_ctime_exponent_size(b) \ sl@0: ((b) > 937 ? 6 : \ sl@0: (b) > 306 ? 5 : \ sl@0: (b) > 89 ? 4 : \ sl@0: (b) > 22 ? 3 : 1) sl@0: # define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (6) sl@0: sl@0: #elif MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 32 sl@0: sl@0: # define BN_window_bits_for_ctime_exponent_size(b) \ sl@0: ((b) > 306 ? 5 : \ sl@0: (b) > 89 ? 4 : \ sl@0: (b) > 22 ? 3 : 1) sl@0: # define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (5) sl@0: sl@0: #endif sl@0: sl@0: sl@0: /* Pentium pro 16,16,16,32,64 */ sl@0: /* Alpha 16,16,16,16.64 */ sl@0: #define BN_MULL_SIZE_NORMAL (16) /* 32 */ sl@0: #define BN_MUL_RECURSIVE_SIZE_NORMAL (16) /* 32 less than */ sl@0: #define BN_SQR_RECURSIVE_SIZE_NORMAL (16) /* 32 */ sl@0: #define BN_MUL_LOW_RECURSIVE_SIZE_NORMAL (32) /* 32 */ sl@0: #define BN_MONT_CTX_SET_SIZE_WORD (64) /* 32 */ sl@0: sl@0: #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC) sl@0: /* sl@0: * BN_UMULT_HIGH section. sl@0: * sl@0: * No, I'm not trying to overwhelm you when stating that the sl@0: * product of N-bit numbers is 2*N bits wide:-) No, I don't expect sl@0: * you to be impressed when I say that if the compiler doesn't sl@0: * support 2*N integer type, then you have to replace every N*N sl@0: * multiplication with 4 (N/2)*(N/2) accompanied by some shifts sl@0: * and additions which unavoidably results in severe performance sl@0: * penalties. Of course provided that the hardware is capable of sl@0: * producing 2*N result... That's when you normally start sl@0: * considering assembler implementation. However! It should be sl@0: * pointed out that some CPUs (most notably Alpha, PowerPC and sl@0: * upcoming IA-64 family:-) provide *separate* instruction sl@0: * calculating the upper half of the product placing the result sl@0: * into a general purpose register. Now *if* the compiler supports sl@0: * inline assembler, then it's not impossible to implement the sl@0: * "bignum" routines (and have the compiler optimize 'em) sl@0: * exhibiting "native" performance in C. That's what BN_UMULT_HIGH sl@0: * macro is about:-) sl@0: * sl@0: * sl@0: */ sl@0: # if defined(__alpha) && (defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT)) sl@0: # if defined(__DECC) sl@0: # include sl@0: # define BN_UMULT_HIGH(a,b) (BN_ULONG)asm("umulh %a0,%a1,%v0",(a),(b)) sl@0: # elif defined(__GNUC__) sl@0: # define BN_UMULT_HIGH(a,b) ({ \ sl@0: register BN_ULONG ret; \ sl@0: asm ("umulh %1,%2,%0" \ sl@0: : "=r"(ret) \ sl@0: : "r"(a), "r"(b)); \ sl@0: ret; }) sl@0: # endif /* compiler */ sl@0: # elif defined(_ARCH_PPC) && defined(__64BIT__) && defined(SIXTY_FOUR_BIT_LONG) sl@0: # if defined(__GNUC__) sl@0: # define BN_UMULT_HIGH(a,b) ({ \ sl@0: register BN_ULONG ret; \ sl@0: asm ("mulhdu %0,%1,%2" \ sl@0: : "=r"(ret) \ sl@0: : "r"(a), "r"(b)); \ sl@0: ret; }) sl@0: # endif /* compiler */ sl@0: # elif defined(__x86_64) && defined(SIXTY_FOUR_BIT_LONG) sl@0: # if defined(__GNUC__) sl@0: # define BN_UMULT_HIGH(a,b) ({ \ sl@0: register BN_ULONG ret,discard; \ sl@0: asm ("mulq %3" \ sl@0: : "=a"(discard),"=d"(ret) \ sl@0: : "a"(a), "g"(b) \ sl@0: : "cc"); \ sl@0: ret; }) sl@0: # define BN_UMULT_LOHI(low,high,a,b) \ sl@0: asm ("mulq %3" \ sl@0: : "=a"(low),"=d"(high) \ sl@0: : "a"(a),"g"(b) \ sl@0: : "cc"); sl@0: # endif sl@0: # elif (defined(_M_AMD64) || defined(_M_X64)) && defined(SIXTY_FOUR_BIT) sl@0: # if defined(_MSC_VER) && _MSC_VER>=1400 sl@0: unsigned __int64 __umulh (unsigned __int64 a,unsigned __int64 b); sl@0: unsigned __int64 _umul128 (unsigned __int64 a,unsigned __int64 b, sl@0: unsigned __int64 *h); sl@0: # pragma intrinsic(__umulh,_umul128) sl@0: # define BN_UMULT_HIGH(a,b) __umulh((a),(b)) sl@0: # define BN_UMULT_LOHI(low,high,a,b) ((low)=_umul128((a),(b),&(high))) sl@0: # endif sl@0: # endif /* cpu */ sl@0: #endif /* OPENSSL_NO_ASM */ sl@0: sl@0: /************************************************************* sl@0: * Using the long long type sl@0: */ sl@0: #define Lw(t) (((BN_ULONG)(t))&BN_MASK2) sl@0: #define Hw(t) (((BN_ULONG)((t)>>BN_BITS2))&BN_MASK2) sl@0: sl@0: #ifdef BN_DEBUG_RAND sl@0: #define bn_clear_top2max(a) \ sl@0: { \ sl@0: int ind = (a)->dmax - (a)->top; \ sl@0: BN_ULONG *ftl = &(a)->d[(a)->top-1]; \ sl@0: for (; ind != 0; ind--) \ sl@0: *(++ftl) = 0x0; \ sl@0: } sl@0: #else sl@0: #define bn_clear_top2max(a) sl@0: #endif sl@0: sl@0: #ifdef BN_LLONG sl@0: #define mul_add(r,a,w,c) { \ sl@0: BN_ULLONG t; \ sl@0: t=(BN_ULLONG)w * (a) + (r) + (c); \ sl@0: (r)= Lw(t); \ sl@0: (c)= Hw(t); \ sl@0: } sl@0: sl@0: #define mul(r,a,w,c) { \ sl@0: BN_ULLONG t; \ sl@0: t=(BN_ULLONG)w * (a) + (c); \ sl@0: (r)= Lw(t); \ sl@0: (c)= Hw(t); \ sl@0: } sl@0: sl@0: #define sqr(r0,r1,a) { \ sl@0: BN_ULLONG t; \ sl@0: t=(BN_ULLONG)(a)*(a); \ sl@0: (r0)=Lw(t); \ sl@0: (r1)=Hw(t); \ sl@0: } sl@0: sl@0: #elif defined(BN_UMULT_LOHI) sl@0: #define mul_add(r,a,w,c) { \ sl@0: BN_ULONG high,low,ret,tmp=(a); \ sl@0: ret = (r); \ sl@0: BN_UMULT_LOHI(low,high,w,tmp); \ sl@0: ret += (c); \ sl@0: (c) = (ret<(c))?1:0; \ sl@0: (c) += high; \ sl@0: ret += low; \ sl@0: (c) += (ret>BN_BITS4)&BN_MASK2l) sl@0: #define L2HBITS(a) (((a)<>BN_BITS2)&BN_MASKl) sl@0: #define LL2HBITS(a) ((BN_ULLONG)((a)&BN_MASKl)<>(BN_BITS4-1); \ sl@0: m =(m&BN_MASK2l)<<(BN_BITS4+1); \ sl@0: l=(l+m)&BN_MASK2; if (l < m) h++; \ sl@0: (lo)=l; \ sl@0: (ho)=h; \ sl@0: } sl@0: sl@0: #define mul_add(r,a,bl,bh,c) { \ sl@0: BN_ULONG l,h; \ sl@0: \ sl@0: h= (a); \ sl@0: l=LBITS(h); \ sl@0: h=HBITS(h); \ sl@0: mul64(l,h,(bl),(bh)); \ sl@0: \ sl@0: /* non-multiply part */ \ sl@0: l=(l+(c))&BN_MASK2; if (l < (c)) h++; \ sl@0: (c)=(r); \ sl@0: l=(l+(c))&BN_MASK2; if (l < (c)) h++; \ sl@0: (c)=h&BN_MASK2; \ sl@0: (r)=l; \ sl@0: } sl@0: sl@0: #define mul(r,a,bl,bh,c) { \ sl@0: BN_ULONG l,h; \ sl@0: \ sl@0: h= (a); \ sl@0: l=LBITS(h); \ sl@0: h=HBITS(h); \ sl@0: mul64(l,h,(bl),(bh)); \ sl@0: \ sl@0: /* non-multiply part */ \ sl@0: l+=(c); if ((l&BN_MASK2) < (c)) h++; \ sl@0: (c)=h&BN_MASK2; \ sl@0: (r)=l&BN_MASK2; \ sl@0: } sl@0: #endif /* !BN_LLONG */ sl@0: sl@0: void bn_mul_normal(BN_ULONG *r,BN_ULONG *a,int na,BN_ULONG *b,int nb); sl@0: IMPORT_C void bn_mul_comba8(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b); sl@0: IMPORT_C void bn_mul_comba4(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b); sl@0: void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp); sl@0: IMPORT_C void bn_sqr_comba8(BN_ULONG *r,const BN_ULONG *a); sl@0: IMPORT_C void bn_sqr_comba4(BN_ULONG *r,const BN_ULONG *a); sl@0: IMPORT_C int bn_cmp_words(const BN_ULONG *a,const BN_ULONG *b,int n); sl@0: IMPORT_C int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, sl@0: int cl, int dl); sl@0: void bn_mul_recursive(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b,int n2, sl@0: int dna,int dnb,BN_ULONG *t); sl@0: void bn_mul_part_recursive(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b, sl@0: int n,int tna,int tnb,BN_ULONG *t); sl@0: void bn_sqr_recursive(BN_ULONG *r,const BN_ULONG *a, int n2, BN_ULONG *t); sl@0: void bn_mul_low_normal(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b, int n); sl@0: void bn_mul_low_recursive(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b,int n2, sl@0: BN_ULONG *t); sl@0: void bn_mul_high(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b,BN_ULONG *l,int n2, sl@0: BN_ULONG *t); sl@0: BN_ULONG bn_add_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, sl@0: int cl, int dl); sl@0: BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, sl@0: int cl, int dl); sl@0: sl@0: #ifdef __cplusplus sl@0: } sl@0: #endif sl@0: sl@0: #endif