Eneboo - Documentación para desarrolladores
|
00001 /* crypto/bn/bn_lcl.h */ 00002 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 00003 * All rights reserved. 00004 * 00005 * This package is an SSL implementation written 00006 * by Eric Young (eay@cryptsoft.com). 00007 * The implementation was written so as to conform with Netscapes SSL. 00008 * 00009 * This library is free for commercial and non-commercial use as long as 00010 * the following conditions are aheared to. The following conditions 00011 * apply to all code found in this distribution, be it the RC4, RSA, 00012 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 00013 * included with this distribution is covered by the same copyright terms 00014 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 00015 * 00016 * Copyright remains Eric Young's, and as such any Copyright notices in 00017 * the code are not to be removed. 00018 * If this package is used in a product, Eric Young should be given attribution 00019 * as the author of the parts of the library used. 00020 * This can be in the form of a textual message at program startup or 00021 * in documentation (online or textual) provided with the package. 00022 * 00023 * Redistribution and use in source and binary forms, with or without 00024 * modification, are permitted provided that the following conditions 00025 * are met: 00026 * 1. Redistributions of source code must retain the copyright 00027 * notice, this list of conditions and the following disclaimer. 00028 * 2. Redistributions in binary form must reproduce the above copyright 00029 * notice, this list of conditions and the following disclaimer in the 00030 * documentation and/or other materials provided with the distribution. 00031 * 3. All advertising materials mentioning features or use of this software 00032 * must display the following acknowledgement: 00033 * "This product includes cryptographic software written by 00034 * Eric Young (eay@cryptsoft.com)" 00035 * The word 'cryptographic' can be left out if the rouines from the library 00036 * being used are not cryptographic related :-). 00037 * 4. If you include any Windows specific code (or a derivative thereof) from 00038 * the apps directory (application code) you must include an acknowledgement: 00039 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 00040 * 00041 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 00042 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00043 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00044 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 00045 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00046 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00047 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00048 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00049 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00050 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00051 * SUCH DAMAGE. 00052 * 00053 * The licence and distribution terms for any publically available version or 00054 * derivative of this code cannot be changed. i.e. this code cannot simply be 00055 * copied and put under another distribution licence 00056 * [including the GNU Public Licence.] 00057 */ 00058 /* ==================================================================== 00059 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. 00060 * 00061 * Redistribution and use in source and binary forms, with or without 00062 * modification, are permitted provided that the following conditions 00063 * are met: 00064 * 00065 * 1. Redistributions of source code must retain the above copyright 00066 * notice, this list of conditions and the following disclaimer. 00067 * 00068 * 2. Redistributions in binary form must reproduce the above copyright 00069 * notice, this list of conditions and the following disclaimer in 00070 * the documentation and/or other materials provided with the 00071 * distribution. 00072 * 00073 * 3. All advertising materials mentioning features or use of this 00074 * software must display the following acknowledgment: 00075 * "This product includes software developed by the OpenSSL Project 00076 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 00077 * 00078 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 00079 * endorse or promote products derived from this software without 00080 * prior written permission. For written permission, please contact 00081 * openssl-core@openssl.org. 00082 * 00083 * 5. Products derived from this software may not be called "OpenSSL" 00084 * nor may "OpenSSL" appear in their names without prior written 00085 * permission of the OpenSSL Project. 00086 * 00087 * 6. Redistributions of any form whatsoever must retain the following 00088 * acknowledgment: 00089 * "This product includes software developed by the OpenSSL Project 00090 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 00091 * 00092 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 00093 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00094 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00095 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 00096 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00097 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00098 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00099 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 00101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 00103 * OF THE POSSIBILITY OF SUCH DAMAGE. 00104 * ==================================================================== 00105 * 00106 * This product includes cryptographic software written by Eric Young 00107 * (eay@cryptsoft.com). This product includes software written by Tim 00108 * Hudson (tjh@cryptsoft.com). 00109 * 00110 */ 00111 00112 #ifndef HEADER_BN_LCL_H 00113 #define HEADER_BN_LCL_H 00114 00115 #include <openssl/bn.h> 00116 00117 #ifdef __cplusplus 00118 extern "C" { 00119 #endif 00120 00121 00122 /* 00123 * BN_window_bits_for_exponent_size -- macro for sliding window mod_exp functions 00124 * 00125 * 00126 * For window size 'w' (w >= 2) and a random 'b' bits exponent, 00127 * the number of multiplications is a constant plus on average 00128 * 00129 * 2^(w-1) + (b-w)/(w+1); 00130 * 00131 * here 2^(w-1) is for precomputing the table (we actually need 00132 * entries only for windows that have the lowest bit set), and 00133 * (b-w)/(w+1) is an approximation for the expected number of 00134 * w-bit windows, not counting the first one. 00135 * 00136 * Thus we should use 00137 * 00138 * w >= 6 if b > 671 00139 * w = 5 if 671 > b > 239 00140 * w = 4 if 239 > b > 79 00141 * w = 3 if 79 > b > 23 00142 * w <= 2 if 23 > b 00143 * 00144 * (with draws in between). Very small exponents are often selected 00145 * with low Hamming weight, so we use w = 1 for b <= 23. 00146 */ 00147 #if 1 00148 #define BN_window_bits_for_exponent_size(b) \ 00149 ((b) > 671 ? 6 : \ 00150 (b) > 239 ? 5 : \ 00151 (b) > 79 ? 4 : \ 00152 (b) > 23 ? 3 : 1) 00153 #else 00154 /* Old SSLeay/OpenSSL table. 00155 * Maximum window size was 5, so this table differs for b==1024; 00156 * but it coincides for other interesting values (b==160, b==512). 00157 */ 00158 #define BN_window_bits_for_exponent_size(b) \ 00159 ((b) > 255 ? 5 : \ 00160 (b) > 127 ? 4 : \ 00161 (b) > 17 ? 3 : 1) 00162 #endif 00163 00164 00165 00166 /* BN_mod_exp_mont_conttime is based on the assumption that the 00167 * L1 data cache line width of the target processor is at least 00168 * the following value. 00169 */ 00170 #define MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH ( 64 ) 00171 #define MOD_EXP_CTIME_MIN_CACHE_LINE_MASK (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - 1) 00172 00173 /* Window sizes optimized for fixed window size modular exponentiation 00174 * algorithm (BN_mod_exp_mont_consttime). 00175 * 00176 * To achieve the security goals of BN_mode_exp_mont_consttime, the 00177 * maximum size of the window must not exceed 00178 * log_2(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH). 00179 * 00180 * Window size thresholds are defined for cache line sizes of 32 and 64, 00181 * cache line sizes where log_2(32)=5 and log_2(64)=6 respectively. A 00182 * window size of 7 should only be used on processors that have a 128 00183 * byte or greater cache line size. 00184 */ 00185 #if MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 64 00186 00187 # define BN_window_bits_for_ctime_exponent_size(b) \ 00188 ((b) > 937 ? 6 : \ 00189 (b) > 306 ? 5 : \ 00190 (b) > 89 ? 4 : \ 00191 (b) > 22 ? 3 : 1) 00192 # define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (6) 00193 00194 #elif MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 32 00195 00196 # define BN_window_bits_for_ctime_exponent_size(b) \ 00197 ((b) > 306 ? 5 : \ 00198 (b) > 89 ? 4 : \ 00199 (b) > 22 ? 3 : 1) 00200 # define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (5) 00201 00202 #endif 00203 00204 00205 /* Pentium pro 16,16,16,32,64 */ 00206 /* Alpha 16,16,16,16.64 */ 00207 #define BN_MULL_SIZE_NORMAL (16) /* 32 */ 00208 #define BN_MUL_RECURSIVE_SIZE_NORMAL (16) /* 32 less than */ 00209 #define BN_SQR_RECURSIVE_SIZE_NORMAL (16) /* 32 */ 00210 #define BN_MUL_LOW_RECURSIVE_SIZE_NORMAL (32) /* 32 */ 00211 #define BN_MONT_CTX_SET_SIZE_WORD (64) /* 32 */ 00212 00213 #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC) 00214 /* 00215 * BN_UMULT_HIGH section. 00216 * 00217 * No, I'm not trying to overwhelm you when stating that the 00218 * product of N-bit numbers is 2*N bits wide:-) No, I don't expect 00219 * you to be impressed when I say that if the compiler doesn't 00220 * support 2*N integer type, then you have to replace every N*N 00221 * multiplication with 4 (N/2)*(N/2) accompanied by some shifts 00222 * and additions which unavoidably results in severe performance 00223 * penalties. Of course provided that the hardware is capable of 00224 * producing 2*N result... That's when you normally start 00225 * considering assembler implementation. However! It should be 00226 * pointed out that some CPUs (most notably Alpha, PowerPC and 00227 * upcoming IA-64 family:-) provide *separate* instruction 00228 * calculating the upper half of the product placing the result 00229 * into a general purpose register. Now *if* the compiler supports 00230 * inline assembler, then it's not impossible to implement the 00231 * "bignum" routines (and have the compiler optimize 'em) 00232 * exhibiting "native" performance in C. That's what BN_UMULT_HIGH 00233 * macro is about:-) 00234 * 00235 * <appro@fy.chalmers.se> 00236 */ 00237 # if defined(__alpha) && (defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT)) 00238 # if defined(__DECC) 00239 # include <c_asm.h> 00240 # define BN_UMULT_HIGH(a,b) (BN_ULONG)asm("umulh %a0,%a1,%v0",(a),(b)) 00241 # elif defined(__GNUC__) 00242 # define BN_UMULT_HIGH(a,b) ({ \ 00243 register BN_ULONG ret; \ 00244 asm ("umulh %1,%2,%0" \ 00245 : "=r"(ret) \ 00246 : "r"(a), "r"(b)); \ 00247 ret; }) 00248 # endif /* compiler */ 00249 # elif defined(_ARCH_PPC) && defined(__64BIT__) && defined(SIXTY_FOUR_BIT_LONG) 00250 # if defined(__GNUC__) 00251 # define BN_UMULT_HIGH(a,b) ({ \ 00252 register BN_ULONG ret; \ 00253 asm ("mulhdu %0,%1,%2" \ 00254 : "=r"(ret) \ 00255 : "r"(a), "r"(b)); \ 00256 ret; }) 00257 # endif /* compiler */ 00258 # elif defined(__x86_64) && defined(SIXTY_FOUR_BIT_LONG) 00259 # if defined(__GNUC__) 00260 # define BN_UMULT_HIGH(a,b) ({ \ 00261 register BN_ULONG ret,discard; \ 00262 asm ("mulq %3" \ 00263 : "=a"(discard),"=d"(ret) \ 00264 : "a"(a), "g"(b) \ 00265 : "cc"); \ 00266 ret; }) 00267 # define BN_UMULT_LOHI(low,high,a,b) \ 00268 asm ("mulq %3" \ 00269 : "=a"(low),"=d"(high) \ 00270 : "a"(a),"g"(b) \ 00271 : "cc"); 00272 # endif 00273 # elif (defined(_M_AMD64) || defined(_M_X64)) && defined(SIXTY_FOUR_BIT) 00274 # if defined(_MSC_VER) && _MSC_VER>=1400 00275 unsigned __int64 __umulh (unsigned __int64 a,unsigned __int64 b); 00276 unsigned __int64 _umul128 (unsigned __int64 a,unsigned __int64 b, 00277 unsigned __int64 *h); 00278 # pragma intrinsic(__umulh,_umul128) 00279 # define BN_UMULT_HIGH(a,b) __umulh((a),(b)) 00280 # define BN_UMULT_LOHI(low,high,a,b) ((low)=_umul128((a),(b),&(high))) 00281 # endif 00282 # endif /* cpu */ 00283 #endif /* OPENSSL_NO_ASM */ 00284 00285 /************************************************************* 00286 * Using the long long type 00287 */ 00288 #define Lw(t) (((BN_ULONG)(t))&BN_MASK2) 00289 #define Hw(t) (((BN_ULONG)((t)>>BN_BITS2))&BN_MASK2) 00290 00291 #ifdef BN_DEBUG_RAND 00292 #define bn_clear_top2max(a) \ 00293 { \ 00294 int ind = (a)->dmax - (a)->top; \ 00295 BN_ULONG *ftl = &(a)->d[(a)->top-1]; \ 00296 for (; ind != 0; ind--) \ 00297 *(++ftl) = 0x0; \ 00298 } 00299 #else 00300 #define bn_clear_top2max(a) 00301 #endif 00302 00303 #ifdef BN_LLONG 00304 #define mul_add(r,a,w,c) { \ 00305 BN_ULLONG t; \ 00306 t=(BN_ULLONG)w * (a) + (r) + (c); \ 00307 (r)= Lw(t); \ 00308 (c)= Hw(t); \ 00309 } 00310 00311 #define mul(r,a,w,c) { \ 00312 BN_ULLONG t; \ 00313 t=(BN_ULLONG)w * (a) + (c); \ 00314 (r)= Lw(t); \ 00315 (c)= Hw(t); \ 00316 } 00317 00318 #define sqr(r0,r1,a) { \ 00319 BN_ULLONG t; \ 00320 t=(BN_ULLONG)(a)*(a); \ 00321 (r0)=Lw(t); \ 00322 (r1)=Hw(t); \ 00323 } 00324 00325 #elif defined(BN_UMULT_LOHI) 00326 #define mul_add(r,a,w,c) { \ 00327 BN_ULONG high,low,ret,tmp=(a); \ 00328 ret = (r); \ 00329 BN_UMULT_LOHI(low,high,w,tmp); \ 00330 ret += (c); \ 00331 (c) = (ret<(c))?1:0; \ 00332 (c) += high; \ 00333 ret += low; \ 00334 (c) += (ret<low)?1:0; \ 00335 (r) = ret; \ 00336 } 00337 00338 #define mul(r,a,w,c) { \ 00339 BN_ULONG high,low,ret,ta=(a); \ 00340 BN_UMULT_LOHI(low,high,w,ta); \ 00341 ret = low + (c); \ 00342 (c) = high; \ 00343 (c) += (ret<low)?1:0; \ 00344 (r) = ret; \ 00345 } 00346 00347 #define sqr(r0,r1,a) { \ 00348 BN_ULONG tmp=(a); \ 00349 BN_UMULT_LOHI(r0,r1,tmp,tmp); \ 00350 } 00351 00352 #elif defined(BN_UMULT_HIGH) 00353 #define mul_add(r,a,w,c) { \ 00354 BN_ULONG high,low,ret,tmp=(a); \ 00355 ret = (r); \ 00356 high= BN_UMULT_HIGH(w,tmp); \ 00357 ret += (c); \ 00358 low = (w) * tmp; \ 00359 (c) = (ret<(c))?1:0; \ 00360 (c) += high; \ 00361 ret += low; \ 00362 (c) += (ret<low)?1:0; \ 00363 (r) = ret; \ 00364 } 00365 00366 #define mul(r,a,w,c) { \ 00367 BN_ULONG high,low,ret,ta=(a); \ 00368 low = (w) * ta; \ 00369 high= BN_UMULT_HIGH(w,ta); \ 00370 ret = low + (c); \ 00371 (c) = high; \ 00372 (c) += (ret<low)?1:0; \ 00373 (r) = ret; \ 00374 } 00375 00376 #define sqr(r0,r1,a) { \ 00377 BN_ULONG tmp=(a); \ 00378 (r0) = tmp * tmp; \ 00379 (r1) = BN_UMULT_HIGH(tmp,tmp); \ 00380 } 00381 00382 #else 00383 /************************************************************* 00384 * No long long type 00385 */ 00386 00387 #define LBITS(a) ((a)&BN_MASK2l) 00388 #define HBITS(a) (((a)>>BN_BITS4)&BN_MASK2l) 00389 #define L2HBITS(a) (((a)<<BN_BITS4)&BN_MASK2) 00390 00391 #define LLBITS(a) ((a)&BN_MASKl) 00392 #define LHBITS(a) (((a)>>BN_BITS2)&BN_MASKl) 00393 #define LL2HBITS(a) ((BN_ULLONG)((a)&BN_MASKl)<<BN_BITS2) 00394 00395 #define mul64(l,h,bl,bh) \ 00396 { \ 00397 BN_ULONG m,m1,lt,ht; \ 00398 \ 00399 lt=l; \ 00400 ht=h; \ 00401 m =(bh)*(lt); \ 00402 lt=(bl)*(lt); \ 00403 m1=(bl)*(ht); \ 00404 ht =(bh)*(ht); \ 00405 m=(m+m1)&BN_MASK2; if (m < m1) ht+=L2HBITS((BN_ULONG)1); \ 00406 ht+=HBITS(m); \ 00407 m1=L2HBITS(m); \ 00408 lt=(lt+m1)&BN_MASK2; if (lt < m1) ht++; \ 00409 (l)=lt; \ 00410 (h)=ht; \ 00411 } 00412 00413 #define sqr64(lo,ho,in) \ 00414 { \ 00415 BN_ULONG l,h,m; \ 00416 \ 00417 h=(in); \ 00418 l=LBITS(h); \ 00419 h=HBITS(h); \ 00420 m =(l)*(h); \ 00421 l*=l; \ 00422 h*=h; \ 00423 h+=(m&BN_MASK2h1)>>(BN_BITS4-1); \ 00424 m =(m&BN_MASK2l)<<(BN_BITS4+1); \ 00425 l=(l+m)&BN_MASK2; if (l < m) h++; \ 00426 (lo)=l; \ 00427 (ho)=h; \ 00428 } 00429 00430 #define mul_add(r,a,bl,bh,c) { \ 00431 BN_ULONG l,h; \ 00432 \ 00433 h= (a); \ 00434 l=LBITS(h); \ 00435 h=HBITS(h); \ 00436 mul64(l,h,(bl),(bh)); \ 00437 \ 00438 /* non-multiply part */ \ 00439 l=(l+(c))&BN_MASK2; if (l < (c)) h++; \ 00440 (c)=(r); \ 00441 l=(l+(c))&BN_MASK2; if (l < (c)) h++; \ 00442 (c)=h&BN_MASK2; \ 00443 (r)=l; \ 00444 } 00445 00446 #define mul(r,a,bl,bh,c) { \ 00447 BN_ULONG l,h; \ 00448 \ 00449 h= (a); \ 00450 l=LBITS(h); \ 00451 h=HBITS(h); \ 00452 mul64(l,h,(bl),(bh)); \ 00453 \ 00454 /* non-multiply part */ \ 00455 l+=(c); if ((l&BN_MASK2) < (c)) h++; \ 00456 (c)=h&BN_MASK2; \ 00457 (r)=l&BN_MASK2; \ 00458 } 00459 #endif /* !BN_LLONG */ 00460 00461 void bn_mul_normal(BN_ULONG *r,BN_ULONG *a,int na,BN_ULONG *b,int nb); 00462 void bn_mul_comba8(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b); 00463 void bn_mul_comba4(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b); 00464 void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp); 00465 void bn_sqr_comba8(BN_ULONG *r,const BN_ULONG *a); 00466 void bn_sqr_comba4(BN_ULONG *r,const BN_ULONG *a); 00467 int bn_cmp_words(const BN_ULONG *a,const BN_ULONG *b,int n); 00468 int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, 00469 int cl, int dl); 00470 void bn_mul_recursive(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b,int n2, 00471 int dna,int dnb,BN_ULONG *t); 00472 void bn_mul_part_recursive(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b, 00473 int n,int tna,int tnb,BN_ULONG *t); 00474 void bn_sqr_recursive(BN_ULONG *r,const BN_ULONG *a, int n2, BN_ULONG *t); 00475 void bn_mul_low_normal(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b, int n); 00476 void bn_mul_low_recursive(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b,int n2, 00477 BN_ULONG *t); 00478 void bn_mul_high(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b,BN_ULONG *l,int n2, 00479 BN_ULONG *t); 00480 BN_ULONG bn_add_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, 00481 int cl, int dl); 00482 BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, 00483 int cl, int dl); 00484 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np,const BN_ULONG *n0, int num); 00485 00486 #ifdef __cplusplus 00487 } 00488 #endif 00489 00490 #endif