155714Skris/* crypto/bn/bn_lcl.h */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8296341Sdelphij * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15296341Sdelphij * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22296341Sdelphij * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37296341Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40296341Sdelphij * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52296341Sdelphij * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5868651Skris/* ==================================================================== 5968651Skris * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. 6068651Skris * 6168651Skris * Redistribution and use in source and binary forms, with or without 6268651Skris * modification, are permitted provided that the following conditions 6368651Skris * are met: 6468651Skris * 6568651Skris * 1. Redistributions of source code must retain the above copyright 66296341Sdelphij * notice, this list of conditions and the following disclaimer. 6768651Skris * 6868651Skris * 2. Redistributions in binary form must reproduce the above copyright 6968651Skris * notice, this list of conditions and the following disclaimer in 7068651Skris * the documentation and/or other materials provided with the 7168651Skris * distribution. 7268651Skris * 7368651Skris * 3. All advertising materials mentioning features or use of this 7468651Skris * software must display the following acknowledgment: 7568651Skris * "This product includes software developed by the OpenSSL Project 7668651Skris * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 7768651Skris * 7868651Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 7968651Skris * endorse or promote products derived from this software without 8068651Skris * prior written permission. For written permission, please contact 8168651Skris * openssl-core@openssl.org. 8268651Skris * 8368651Skris * 5. Products derived from this software may not be called "OpenSSL" 8468651Skris * nor may "OpenSSL" appear in their names without prior written 8568651Skris * permission of the OpenSSL Project. 8668651Skris * 8768651Skris * 6. Redistributions of any form whatsoever must retain the following 8868651Skris * acknowledgment: 8968651Skris * "This product includes software developed by the OpenSSL Project 9068651Skris * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 9168651Skris * 9268651Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 9368651Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 9468651Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 9568651Skris * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 9668651Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 9768651Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 9868651Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 9968651Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 10068651Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 10168651Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 10268651Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 10368651Skris * OF THE POSSIBILITY OF SUCH DAMAGE. 10468651Skris * ==================================================================== 10568651Skris * 10668651Skris * This product includes cryptographic software written by Eric Young 10768651Skris * (eay@cryptsoft.com). This product includes software written by Tim 10868651Skris * Hudson (tjh@cryptsoft.com). 10968651Skris * 11068651Skris */ 11155714Skris 11255714Skris#ifndef HEADER_BN_LCL_H 113296341Sdelphij# define HEADER_BN_LCL_H 11455714Skris 115296341Sdelphij# include <openssl/bn.h> 11655714Skris 11755714Skris#ifdef __cplusplus 11855714Skrisextern "C" { 11955714Skris#endif 12055714Skris 121296341Sdelphij/*- 12268651Skris * BN_window_bits_for_exponent_size -- macro for sliding window mod_exp functions 12368651Skris * 12468651Skris * 12568651Skris * For window size 'w' (w >= 2) and a random 'b' bits exponent, 12668651Skris * the number of multiplications is a constant plus on average 12768651Skris * 12868651Skris * 2^(w-1) + (b-w)/(w+1); 12968651Skris * 13068651Skris * here 2^(w-1) is for precomputing the table (we actually need 13168651Skris * entries only for windows that have the lowest bit set), and 13268651Skris * (b-w)/(w+1) is an approximation for the expected number of 13368651Skris * w-bit windows, not counting the first one. 13468651Skris * 13568651Skris * Thus we should use 13668651Skris * 13768651Skris * w >= 6 if b > 671 13868651Skris * w = 5 if 671 > b > 239 13968651Skris * w = 4 if 239 > b > 79 14068651Skris * w = 3 if 79 > b > 23 14168651Skris * w <= 2 if 23 > b 14268651Skris * 14368651Skris * (with draws in between). Very small exponents are often selected 14468651Skris * with low Hamming weight, so we use w = 1 for b <= 23. 14568651Skris */ 146296341Sdelphij# if 1 147296341Sdelphij# define BN_window_bits_for_exponent_size(b) \ 148296341Sdelphij ((b) > 671 ? 6 : \ 149296341Sdelphij (b) > 239 ? 5 : \ 150296341Sdelphij (b) > 79 ? 4 : \ 151296341Sdelphij (b) > 23 ? 3 : 1) 152296341Sdelphij# else 153296341Sdelphij/* 154296341Sdelphij * Old SSLeay/OpenSSL table. Maximum window size was 5, so this table differs 155296341Sdelphij * for b==1024; but it coincides for other interesting values (b==160, 156296341Sdelphij * b==512). 15768651Skris */ 158296341Sdelphij# define BN_window_bits_for_exponent_size(b) \ 159296341Sdelphij ((b) > 255 ? 5 : \ 160296341Sdelphij (b) > 127 ? 4 : \ 161296341Sdelphij (b) > 17 ? 3 : 1) 162296341Sdelphij# endif 16368651Skris 164296341Sdelphij/* 165296341Sdelphij * BN_mod_exp_mont_conttime is based on the assumption that the L1 data cache 166296341Sdelphij * line width of the target processor is at least the following value. 167160814Ssimon */ 168296341Sdelphij# define MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH ( 64 ) 169296341Sdelphij# define MOD_EXP_CTIME_MIN_CACHE_LINE_MASK (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - 1) 170160814Ssimon 171296341Sdelphij/* 172296341Sdelphij * Window sizes optimized for fixed window size modular exponentiation 173296341Sdelphij * algorithm (BN_mod_exp_mont_consttime). To achieve the security goals of 174296341Sdelphij * BN_mode_exp_mont_consttime, the maximum size of the window must not exceed 175296341Sdelphij * log_2(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH). Window size thresholds are 176296341Sdelphij * defined for cache line sizes of 32 and 64, cache line sizes where 177296341Sdelphij * log_2(32)=5 and log_2(64)=6 respectively. A window size of 7 should only be 178296341Sdelphij * used on processors that have a 128 byte or greater cache line size. 179160814Ssimon */ 180296341Sdelphij# if MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 64 181160814Ssimon 182160814Ssimon# define BN_window_bits_for_ctime_exponent_size(b) \ 183296341Sdelphij ((b) > 937 ? 6 : \ 184296341Sdelphij (b) > 306 ? 5 : \ 185296341Sdelphij (b) > 89 ? 4 : \ 186296341Sdelphij (b) > 22 ? 3 : 1) 187296341Sdelphij# define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (6) 188160814Ssimon 189296341Sdelphij# elif MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 32 190160814Ssimon 191160814Ssimon# define BN_window_bits_for_ctime_exponent_size(b) \ 192296341Sdelphij ((b) > 306 ? 5 : \ 193296341Sdelphij (b) > 89 ? 4 : \ 194296341Sdelphij (b) > 22 ? 3 : 1) 195296341Sdelphij# define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (5) 196160814Ssimon 197296341Sdelphij# endif 198160814Ssimon 19955714Skris/* Pentium pro 16,16,16,32,64 */ 20055714Skris/* Alpha 16,16,16,16.64 */ 201296341Sdelphij# define BN_MULL_SIZE_NORMAL (16)/* 32 */ 202296341Sdelphij# define BN_MUL_RECURSIVE_SIZE_NORMAL (16)/* 32 less than */ 203296341Sdelphij# define BN_SQR_RECURSIVE_SIZE_NORMAL (16)/* 32 */ 204296341Sdelphij# define BN_MUL_LOW_RECURSIVE_SIZE_NORMAL (32)/* 32 */ 205296341Sdelphij# define BN_MONT_CTX_SET_SIZE_WORD (64)/* 32 */ 20655714Skris 207296341Sdelphij# if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC) 20859191Skris/* 20959191Skris * BN_UMULT_HIGH section. 21059191Skris * 21159191Skris * No, I'm not trying to overwhelm you when stating that the 21259191Skris * product of N-bit numbers is 2*N bits wide:-) No, I don't expect 21359191Skris * you to be impressed when I say that if the compiler doesn't 21459191Skris * support 2*N integer type, then you have to replace every N*N 21559191Skris * multiplication with 4 (N/2)*(N/2) accompanied by some shifts 21659191Skris * and additions which unavoidably results in severe performance 21759191Skris * penalties. Of course provided that the hardware is capable of 21859191Skris * producing 2*N result... That's when you normally start 21959191Skris * considering assembler implementation. However! It should be 22059191Skris * pointed out that some CPUs (most notably Alpha, PowerPC and 22159191Skris * upcoming IA-64 family:-) provide *separate* instruction 22259191Skris * calculating the upper half of the product placing the result 22359191Skris * into a general purpose register. Now *if* the compiler supports 22459191Skris * inline assembler, then it's not impossible to implement the 22559191Skris * "bignum" routines (and have the compiler optimize 'em) 22659191Skris * exhibiting "native" performance in C. That's what BN_UMULT_HIGH 22759191Skris * macro is about:-) 22859191Skris * 229296341Sdelphij * <appro@fy.chalmers.se> 23059191Skris */ 231296341Sdelphij# if defined(__alpha) && (defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT)) 232296341Sdelphij# if defined(__DECC) 233296341Sdelphij# include <c_asm.h> 234296341Sdelphij# define BN_UMULT_HIGH(a,b) (BN_ULONG)asm("umulh %a0,%a1,%v0",(a),(b)) 235296341Sdelphij# elif defined(__GNUC__) && __GNUC__>=2 236296341Sdelphij# define BN_UMULT_HIGH(a,b) ({ \ 237296341Sdelphij register BN_ULONG ret; \ 238296341Sdelphij asm ("umulh %1,%2,%0" \ 239296341Sdelphij : "=r"(ret) \ 240296341Sdelphij : "r"(a), "r"(b)); \ 241296341Sdelphij ret; }) 242296341Sdelphij# endif /* compiler */ 243296341Sdelphij# elif defined(_ARCH_PPC) && defined(__64BIT__) && defined(SIXTY_FOUR_BIT_LONG) 244296341Sdelphij# if defined(__GNUC__) && __GNUC__>=2 245296341Sdelphij# define BN_UMULT_HIGH(a,b) ({ \ 246296341Sdelphij register BN_ULONG ret; \ 247296341Sdelphij asm ("mulhdu %0,%1,%2" \ 248296341Sdelphij : "=r"(ret) \ 249296341Sdelphij : "r"(a), "r"(b)); \ 250296341Sdelphij ret; }) 251296341Sdelphij# endif /* compiler */ 252296341Sdelphij# elif (defined(__x86_64) || defined(__x86_64__)) && \ 253238405Sjkim (defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT)) 254296341Sdelphij# if defined(__GNUC__) && __GNUC__>=2 255296341Sdelphij# define BN_UMULT_HIGH(a,b) ({ \ 256296341Sdelphij register BN_ULONG ret,discard; \ 257296341Sdelphij asm ("mulq %3" \ 258296341Sdelphij : "=a"(discard),"=d"(ret) \ 259296341Sdelphij : "a"(a), "g"(b) \ 260296341Sdelphij : "cc"); \ 261296341Sdelphij ret; }) 262296341Sdelphij# define BN_UMULT_LOHI(low,high,a,b) \ 263296341Sdelphij asm ("mulq %3" \ 264296341Sdelphij : "=a"(low),"=d"(high) \ 265296341Sdelphij : "a"(a),"g"(b) \ 266296341Sdelphij : "cc"); 267296341Sdelphij# endif 268296341Sdelphij# elif (defined(_M_AMD64) || defined(_M_X64)) && defined(SIXTY_FOUR_BIT) 269296341Sdelphij# if defined(_MSC_VER) && _MSC_VER>=1400 270296341Sdelphijunsigned __int64 __umulh(unsigned __int64 a, unsigned __int64 b); 271296341Sdelphijunsigned __int64 _umul128(unsigned __int64 a, unsigned __int64 b, 272296341Sdelphij unsigned __int64 *h); 273296341Sdelphij# pragma intrinsic(__umulh,_umul128) 274296341Sdelphij# define BN_UMULT_HIGH(a,b) __umulh((a),(b)) 275296341Sdelphij# define BN_UMULT_LOHI(low,high,a,b) ((low)=_umul128((a),(b),&(high))) 276296341Sdelphij# endif 277296341Sdelphij# elif defined(__mips) && (defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)) 278296341Sdelphij# if defined(__GNUC__) && __GNUC__>=2 279296341Sdelphij# if __GNUC__>4 || (__GNUC__>=4 && __GNUC_MINOR__>=4) 280296341Sdelphij /* "h" constraint is no more since 4.4 */ 281296341Sdelphij# define BN_UMULT_HIGH(a,b) (((__uint128_t)(a)*(b))>>64) 282296341Sdelphij# define BN_UMULT_LOHI(low,high,a,b) ({ \ 283296341Sdelphij __uint128_t ret=(__uint128_t)(a)*(b); \ 284296341Sdelphij (high)=ret>>64; (low)=ret; }) 285296341Sdelphij# else 286296341Sdelphij# define BN_UMULT_HIGH(a,b) ({ \ 287296341Sdelphij register BN_ULONG ret; \ 288296341Sdelphij asm ("dmultu %1,%2" \ 289296341Sdelphij : "=h"(ret) \ 290296341Sdelphij : "r"(a), "r"(b) : "l"); \ 291296341Sdelphij ret; }) 292246772Sjkim# define BN_UMULT_LOHI(low,high,a,b)\ 293296341Sdelphij asm ("dmultu %2,%3" \ 294296341Sdelphij : "=l"(low),"=h"(high) \ 295296341Sdelphij : "r"(a), "r"(b)); 296246772Sjkim# endif 297296341Sdelphij# endif 298296341Sdelphij# endif /* cpu */ 299296341Sdelphij# endif /* OPENSSL_NO_ASM */ 30055714Skris 30155714Skris/************************************************************* 30255714Skris * Using the long long type 30355714Skris */ 304296341Sdelphij# define Lw(t) (((BN_ULONG)(t))&BN_MASK2) 305296341Sdelphij# define Hw(t) (((BN_ULONG)((t)>>BN_BITS2))&BN_MASK2) 30655714Skris 307296341Sdelphij# ifdef BN_DEBUG_RAND 308296341Sdelphij# define bn_clear_top2max(a) \ 309296341Sdelphij { \ 310296341Sdelphij int ind = (a)->dmax - (a)->top; \ 311296341Sdelphij BN_ULONG *ftl = &(a)->d[(a)->top-1]; \ 312296341Sdelphij for (; ind != 0; ind--) \ 313296341Sdelphij *(++ftl) = 0x0; \ 314296341Sdelphij } 315296341Sdelphij# else 316296341Sdelphij# define bn_clear_top2max(a) 317296341Sdelphij# endif 31855714Skris 319296341Sdelphij# ifdef BN_LLONG 320296341Sdelphij# define mul_add(r,a,w,c) { \ 321296341Sdelphij BN_ULLONG t; \ 322296341Sdelphij t=(BN_ULLONG)w * (a) + (r) + (c); \ 323296341Sdelphij (r)= Lw(t); \ 324296341Sdelphij (c)= Hw(t); \ 325296341Sdelphij } 32655714Skris 327296341Sdelphij# define mul(r,a,w,c) { \ 328296341Sdelphij BN_ULLONG t; \ 329296341Sdelphij t=(BN_ULLONG)w * (a) + (c); \ 330296341Sdelphij (r)= Lw(t); \ 331296341Sdelphij (c)= Hw(t); \ 332296341Sdelphij } 33355714Skris 334296341Sdelphij# define sqr(r0,r1,a) { \ 335296341Sdelphij BN_ULLONG t; \ 336296341Sdelphij t=(BN_ULLONG)(a)*(a); \ 337296341Sdelphij (r0)=Lw(t); \ 338296341Sdelphij (r1)=Hw(t); \ 339296341Sdelphij } 34059191Skris 341296341Sdelphij# elif defined(BN_UMULT_LOHI) 342296341Sdelphij# define mul_add(r,a,w,c) { \ 343296341Sdelphij BN_ULONG high,low,ret,tmp=(a); \ 344296341Sdelphij ret = (r); \ 345296341Sdelphij BN_UMULT_LOHI(low,high,w,tmp); \ 346296341Sdelphij ret += (c); \ 347296341Sdelphij (c) = (ret<(c))?1:0; \ 348296341Sdelphij (c) += high; \ 349296341Sdelphij ret += low; \ 350296341Sdelphij (c) += (ret<low)?1:0; \ 351296341Sdelphij (r) = ret; \ 352296341Sdelphij } 353160814Ssimon 354296341Sdelphij# define mul(r,a,w,c) { \ 355296341Sdelphij BN_ULONG high,low,ret,ta=(a); \ 356296341Sdelphij BN_UMULT_LOHI(low,high,w,ta); \ 357296341Sdelphij ret = low + (c); \ 358296341Sdelphij (c) = high; \ 359296341Sdelphij (c) += (ret<low)?1:0; \ 360296341Sdelphij (r) = ret; \ 361296341Sdelphij } 362160814Ssimon 363296341Sdelphij# define sqr(r0,r1,a) { \ 364296341Sdelphij BN_ULONG tmp=(a); \ 365296341Sdelphij BN_UMULT_LOHI(r0,r1,tmp,tmp); \ 366296341Sdelphij } 367160814Ssimon 368296341Sdelphij# elif defined(BN_UMULT_HIGH) 369296341Sdelphij# define mul_add(r,a,w,c) { \ 370296341Sdelphij BN_ULONG high,low,ret,tmp=(a); \ 371296341Sdelphij ret = (r); \ 372296341Sdelphij high= BN_UMULT_HIGH(w,tmp); \ 373296341Sdelphij ret += (c); \ 374296341Sdelphij low = (w) * tmp; \ 375296341Sdelphij (c) = (ret<(c))?1:0; \ 376296341Sdelphij (c) += high; \ 377296341Sdelphij ret += low; \ 378296341Sdelphij (c) += (ret<low)?1:0; \ 379296341Sdelphij (r) = ret; \ 380296341Sdelphij } 38159191Skris 382296341Sdelphij# define mul(r,a,w,c) { \ 383296341Sdelphij BN_ULONG high,low,ret,ta=(a); \ 384296341Sdelphij low = (w) * ta; \ 385296341Sdelphij high= BN_UMULT_HIGH(w,ta); \ 386296341Sdelphij ret = low + (c); \ 387296341Sdelphij (c) = high; \ 388296341Sdelphij (c) += (ret<low)?1:0; \ 389296341Sdelphij (r) = ret; \ 390296341Sdelphij } 39159191Skris 392296341Sdelphij# define sqr(r0,r1,a) { \ 393296341Sdelphij BN_ULONG tmp=(a); \ 394296341Sdelphij (r0) = tmp * tmp; \ 395296341Sdelphij (r1) = BN_UMULT_HIGH(tmp,tmp); \ 396296341Sdelphij } 39759191Skris 398296341Sdelphij# else 39955714Skris/************************************************************* 40055714Skris * No long long type 40155714Skris */ 40255714Skris 403296341Sdelphij# define LBITS(a) ((a)&BN_MASK2l) 404296341Sdelphij# define HBITS(a) (((a)>>BN_BITS4)&BN_MASK2l) 405296341Sdelphij# define L2HBITS(a) (((a)<<BN_BITS4)&BN_MASK2) 40655714Skris 407296341Sdelphij# define LLBITS(a) ((a)&BN_MASKl) 408296341Sdelphij# define LHBITS(a) (((a)>>BN_BITS2)&BN_MASKl) 409296341Sdelphij# define LL2HBITS(a) ((BN_ULLONG)((a)&BN_MASKl)<<BN_BITS2) 41055714Skris 411296341Sdelphij# define mul64(l,h,bl,bh) \ 412296341Sdelphij { \ 413296341Sdelphij BN_ULONG m,m1,lt,ht; \ 41455714Skris \ 415296341Sdelphij lt=l; \ 416296341Sdelphij ht=h; \ 417296341Sdelphij m =(bh)*(lt); \ 418296341Sdelphij lt=(bl)*(lt); \ 419296341Sdelphij m1=(bl)*(ht); \ 420296341Sdelphij ht =(bh)*(ht); \ 421296341Sdelphij m=(m+m1)&BN_MASK2; if (m < m1) ht+=L2HBITS((BN_ULONG)1); \ 422296341Sdelphij ht+=HBITS(m); \ 423296341Sdelphij m1=L2HBITS(m); \ 424296341Sdelphij lt=(lt+m1)&BN_MASK2; if (lt < m1) ht++; \ 425296341Sdelphij (l)=lt; \ 426296341Sdelphij (h)=ht; \ 427296341Sdelphij } 42855714Skris 429296341Sdelphij# define sqr64(lo,ho,in) \ 430296341Sdelphij { \ 431296341Sdelphij BN_ULONG l,h,m; \ 43255714Skris \ 433296341Sdelphij h=(in); \ 434296341Sdelphij l=LBITS(h); \ 435296341Sdelphij h=HBITS(h); \ 436296341Sdelphij m =(l)*(h); \ 437296341Sdelphij l*=l; \ 438296341Sdelphij h*=h; \ 439296341Sdelphij h+=(m&BN_MASK2h1)>>(BN_BITS4-1); \ 440296341Sdelphij m =(m&BN_MASK2l)<<(BN_BITS4+1); \ 441296341Sdelphij l=(l+m)&BN_MASK2; if (l < m) h++; \ 442296341Sdelphij (lo)=l; \ 443296341Sdelphij (ho)=h; \ 444296341Sdelphij } 44555714Skris 446296341Sdelphij# define mul_add(r,a,bl,bh,c) { \ 447296341Sdelphij BN_ULONG l,h; \ 44855714Skris \ 449296341Sdelphij h= (a); \ 450296341Sdelphij l=LBITS(h); \ 451296341Sdelphij h=HBITS(h); \ 452296341Sdelphij mul64(l,h,(bl),(bh)); \ 45355714Skris \ 454296341Sdelphij /* non-multiply part */ \ 455296341Sdelphij l=(l+(c))&BN_MASK2; if (l < (c)) h++; \ 456296341Sdelphij (c)=(r); \ 457296341Sdelphij l=(l+(c))&BN_MASK2; if (l < (c)) h++; \ 458296341Sdelphij (c)=h&BN_MASK2; \ 459296341Sdelphij (r)=l; \ 460296341Sdelphij } 46155714Skris 462296341Sdelphij# define mul(r,a,bl,bh,c) { \ 463296341Sdelphij BN_ULONG l,h; \ 46455714Skris \ 465296341Sdelphij h= (a); \ 466296341Sdelphij l=LBITS(h); \ 467296341Sdelphij h=HBITS(h); \ 468296341Sdelphij mul64(l,h,(bl),(bh)); \ 46955714Skris \ 470296341Sdelphij /* non-multiply part */ \ 471296341Sdelphij l+=(c); if ((l&BN_MASK2) < (c)) h++; \ 472296341Sdelphij (c)=h&BN_MASK2; \ 473296341Sdelphij (r)=l&BN_MASK2; \ 474296341Sdelphij } 475296341Sdelphij# endif /* !BN_LLONG */ 47655714Skris 477296341Sdelphij# if defined(OPENSSL_DOING_MAKEDEPEND) && defined(OPENSSL_FIPS) 478296341Sdelphij# undef bn_div_words 479296341Sdelphij# endif 480238405Sjkim 481296341Sdelphijvoid bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb); 482296341Sdelphijvoid bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b); 483296341Sdelphijvoid bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b); 484109998Smarkmvoid bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp); 485296341Sdelphijvoid bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a); 486296341Sdelphijvoid bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a); 487296341Sdelphijint bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n); 488296341Sdelphijint bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl); 489296341Sdelphijvoid bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, 490296341Sdelphij int dna, int dnb, BN_ULONG *t); 491296341Sdelphijvoid bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, 492296341Sdelphij int n, int tna, int tnb, BN_ULONG *t); 493296341Sdelphijvoid bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t); 494296341Sdelphijvoid bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n); 495296341Sdelphijvoid bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, 496296341Sdelphij BN_ULONG *t); 497296341Sdelphijvoid bn_mul_high(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, BN_ULONG *l, int n2, 498296341Sdelphij BN_ULONG *t); 499160814SsimonBN_ULONG bn_add_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, 500296341Sdelphij int cl, int dl); 501160814SsimonBN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, 502296341Sdelphij int cl, int dl); 503296341Sdelphijint bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, 504296341Sdelphij const BN_ULONG *np, const BN_ULONG *n0, int num); 50555714Skris 50655714Skris#ifdef __cplusplus 50755714Skris} 50855714Skris#endif 50955714Skris 51055714Skris#endif 511