wolf SSL / wolfSSL-TLS13-Beta

Fork of wolfSSL by wolf SSL

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tfm.h Source File

tfm.h

00001 /* tfm.h
00002  *
00003  * Copyright (C) 2006-2016 wolfSSL Inc.
00004  *
00005  * This file is part of wolfSSL.
00006  *
00007  * wolfSSL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * wolfSSL is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
00020  */
00021 
00022 
00023 
00024 /*
00025  * Based on public domain TomsFastMath 0.10 by Tom St Denis, tomstdenis@iahu.ca,
00026  * http://math.libtomcrypt.com
00027  */
00028 
00029 
00030 /**
00031  *  Edited by Moises Guimaraes (moises.guimaraes@phoebus.com.br)
00032  *  to fit CyaSSL's needs.
00033  */
00034 
00035 
00036 #ifndef WOLF_CRYPT_TFM_H
00037 #define WOLF_CRYPT_TFM_H
00038 
00039 #include <wolfssl/wolfcrypt/types.h>
00040 #ifndef CHAR_BIT
00041     #include <limits.h>
00042 #endif
00043 
00044 #include <wolfssl/wolfcrypt/random.h>
00045 
00046 /* wolf big int and common functions */
00047 #include <wolfssl/wolfcrypt/wolfmath.h>
00048 
00049 #ifdef __cplusplus
00050     extern "C" {
00051 #endif
00052 
00053 #ifdef WOLFSSL_PUBLIC_MP
00054     #define MP_API   WOLFSSL_API
00055 #else
00056     #define MP_API
00057 #endif
00058 
00059 #ifndef MIN
00060    #define MIN(x,y) ((x)<(y)?(x):(y))
00061 #endif
00062 
00063 #ifndef MAX
00064    #define MAX(x,y) ((x)>(y)?(x):(y))
00065 #endif
00066 
00067 
00068 #ifndef NO_64BIT
00069 /* autodetect x86-64 and make sure we are using 64-bit digits with x86-64 asm */
00070 #if defined(__x86_64__)
00071    #if defined(TFM_X86) || defined(TFM_SSE2) || defined(TFM_ARM)
00072        #error x86-64 detected, x86-32/SSE2/ARM optimizations are not valid!
00073    #endif
00074    #if !defined(TFM_X86_64) && !defined(TFM_NO_ASM)
00075       #define TFM_X86_64
00076    #endif
00077 #endif
00078 #if defined(TFM_X86_64)
00079     #if !defined(FP_64BIT)
00080        #define FP_64BIT
00081     #endif
00082 #endif
00083 /* use 64-bit digit even if not using asm on x86_64 */
00084 #if defined(__x86_64__) && !defined(FP_64BIT)
00085     #define FP_64BIT
00086 #endif
00087 /* if intel compiler doesn't provide 128 bit type don't turn on 64bit */
00088 #if defined(FP_64BIT) && defined(__INTEL_COMPILER) && !defined(HAVE___UINT128_T)
00089     #undef FP_64BIT
00090     #undef TFM_X86_64
00091 #endif
00092 #endif /* NO_64BIT */
00093 
00094 /* try to detect x86-32 */
00095 #if defined(__i386__) && !defined(TFM_SSE2)
00096    #if defined(TFM_X86_64) || defined(TFM_ARM)
00097        #error x86-32 detected, x86-64/ARM optimizations are not valid!
00098    #endif
00099    #if !defined(TFM_X86) && !defined(TFM_NO_ASM)
00100       #define TFM_X86
00101    #endif
00102 #endif
00103 
00104 /* make sure we're 32-bit for x86-32/sse/arm/ppc32 */
00105 #if (defined(TFM_X86) || defined(TFM_SSE2) || defined(TFM_ARM) || defined(TFM_PPC32)) && defined(FP_64BIT)
00106    #warning x86-32, SSE2 and ARM, PPC32 optimizations require 32-bit digits (undefining)
00107    #undef FP_64BIT
00108 #endif
00109 
00110 /* multi asms? */
00111 #ifdef TFM_X86
00112    #define TFM_ASM
00113 #endif
00114 #ifdef TFM_X86_64
00115    #ifdef TFM_ASM
00116       #error TFM_ASM already defined!
00117    #endif
00118    #define TFM_ASM
00119 #endif
00120 #ifdef TFM_SSE2
00121    #ifdef TFM_ASM
00122       #error TFM_ASM already defined!
00123    #endif
00124    #define TFM_ASM
00125 #endif
00126 #ifdef TFM_ARM
00127    #ifdef TFM_ASM
00128       #error TFM_ASM already defined!
00129    #endif
00130    #define TFM_ASM
00131 #endif
00132 #ifdef TFM_PPC32
00133    #ifdef TFM_ASM
00134       #error TFM_ASM already defined!
00135    #endif
00136    #define TFM_ASM
00137 #endif
00138 #ifdef TFM_PPC64
00139    #ifdef TFM_ASM
00140       #error TFM_ASM already defined!
00141    #endif
00142    #define TFM_ASM
00143 #endif
00144 #ifdef TFM_AVR32
00145    #ifdef TFM_ASM
00146       #error TFM_ASM already defined!
00147    #endif
00148    #define TFM_ASM
00149 #endif
00150 
00151 /* we want no asm? */
00152 #ifdef TFM_NO_ASM
00153    #undef TFM_X86
00154    #undef TFM_X86_64
00155    #undef TFM_SSE2
00156    #undef TFM_ARM
00157    #undef TFM_PPC32
00158    #undef TFM_PPC64
00159    #undef TFM_AVR32
00160    #undef TFM_ASM
00161 #endif
00162 
00163 /* ECC helpers */
00164 #ifdef TFM_ECC192
00165    #ifdef FP_64BIT
00166        #define TFM_MUL3
00167        #define TFM_SQR3
00168    #else
00169        #define TFM_MUL6
00170        #define TFM_SQR6
00171    #endif
00172 #endif
00173 
00174 #ifdef TFM_ECC224
00175    #ifdef FP_64BIT
00176        #define TFM_MUL4
00177        #define TFM_SQR4
00178    #else
00179        #define TFM_MUL7
00180        #define TFM_SQR7
00181    #endif
00182 #endif
00183 
00184 #ifdef TFM_ECC256
00185    #ifdef FP_64BIT
00186        #define TFM_MUL4
00187        #define TFM_SQR4
00188    #else
00189        #define TFM_MUL8
00190        #define TFM_SQR8
00191    #endif
00192 #endif
00193 
00194 #ifdef TFM_ECC384
00195    #ifdef FP_64BIT
00196        #define TFM_MUL6
00197        #define TFM_SQR6
00198    #else
00199        #define TFM_MUL12
00200        #define TFM_SQR12
00201    #endif
00202 #endif
00203 
00204 #ifdef TFM_ECC521
00205    #ifdef FP_64BIT
00206        #define TFM_MUL9
00207        #define TFM_SQR9
00208    #else
00209        #define TFM_MUL17
00210        #define TFM_SQR17
00211    #endif
00212 #endif
00213 
00214 
00215 /* allow user to define on fp_digit, fp_word types */
00216 #ifndef WOLFSSL_BIGINT_TYPES
00217 
00218 /* some default configurations.
00219  */
00220 #if defined(FP_64BIT)
00221    /* for GCC only on supported platforms */
00222    typedef unsigned long long fp_digit;   /* 64bit, 128 uses mode(TI) below */
00223    #define SIZEOF_FP_DIGIT 8
00224    typedef unsigned long      fp_word __attribute__ ((mode(TI)));
00225 #else
00226 
00227    #ifndef NO_64BIT
00228       #if defined(_MSC_VER) || defined(__BORLANDC__)
00229          typedef unsigned __int64   ulong64;
00230       #else
00231          typedef unsigned long long ulong64;
00232       #endif
00233       typedef unsigned int       fp_digit;
00234       #define SIZEOF_FP_DIGIT 4
00235       typedef ulong64            fp_word;
00236       #define FP_32BIT
00237    #else
00238       /* some procs like coldfire prefer not to place multiply into 64bit type
00239          even though it exists */
00240       typedef unsigned short     fp_digit;
00241       #define SIZEOF_FP_DIGIT 2
00242       typedef unsigned int       fp_word;
00243    #endif
00244 #endif
00245 
00246 #endif /* WOLFSSL_BIGINT_TYPES */
00247 
00248 /* # of digits this is */
00249 #define DIGIT_BIT   ((CHAR_BIT) * SIZEOF_FP_DIGIT)
00250 
00251 /* Max size of any number in bits.  Basically the largest size you will be
00252  * multiplying should be half [or smaller] of FP_MAX_SIZE-four_digit
00253  *
00254  * It defaults to 4096-bits [allowing multiplications up to 2048x2048 bits ]
00255  */
00256 
00257 
00258 #ifndef FP_MAX_BITS
00259     #define FP_MAX_BITS           4096
00260 #endif
00261 #define FP_MAX_SIZE           (FP_MAX_BITS+(8*DIGIT_BIT))
00262 
00263 /* will this lib work? */
00264 #if (CHAR_BIT & 7)
00265    #error CHAR_BIT must be a multiple of eight.
00266 #endif
00267 #if FP_MAX_BITS % CHAR_BIT
00268    #error FP_MAX_BITS must be a multiple of CHAR_BIT
00269 #endif
00270 
00271 #define FP_MASK    (fp_digit)(-1)
00272 #define FP_DIGIT_MAX FP_MASK
00273 #define FP_SIZE    (FP_MAX_SIZE/DIGIT_BIT)
00274 
00275 /* signs */
00276 #define FP_ZPOS     0
00277 #define FP_NEG      1
00278 
00279 /* return codes */
00280 #define FP_OKAY      0
00281 #define FP_VAL      -1
00282 #define FP_MEM      -2
00283 #define FP_NOT_INF  -3
00284 
00285 /* equalities */
00286 #define FP_LT        -1   /* less than */
00287 #define FP_EQ         0   /* equal to */
00288 #define FP_GT         1   /* greater than */
00289 
00290 /* replies */
00291 #define FP_YES        1   /* yes response */
00292 #define FP_NO         0   /* no response */
00293 
00294 #ifdef HAVE_WOLF_BIGINT
00295     struct WC_BIGINT;
00296 #endif
00297 
00298 /* a FP type */
00299 typedef struct fp_int {
00300     int      used;
00301     int      sign;
00302 #if defined(ALT_ECC_SIZE) || defined(HAVE_WOLF_BIGINT)
00303     int      size;
00304 #endif
00305     fp_digit dp[FP_SIZE];
00306 
00307 #ifdef HAVE_WOLF_BIGINT
00308     struct WC_BIGINT raw; /* unsigned binary (big endian) */
00309 #endif
00310 } fp_int;
00311 
00312 /* externally define this symbol to ignore the default settings, useful for changing the build from the make process */
00313 #ifndef TFM_ALREADY_SET
00314 
00315 /* do we want the large set of small multiplications ?
00316    Enable these if you are going to be doing a lot of small (<= 16 digit) multiplications say in ECC
00317    Or if you're on a 64-bit machine doing RSA as a 1024-bit integer == 16 digits ;-)
00318  */
00319 /* need to refactor the function */
00320 /*#define TFM_SMALL_SET */
00321 
00322 /* do we want huge code
00323    Enable these if you are doing 20, 24, 28, 32, 48, 64 digit multiplications (useful for RSA)
00324    Less important on 64-bit machines as 32 digits == 2048 bits
00325  */
00326 #if 0
00327 #define TFM_MUL3
00328 #define TFM_MUL4
00329 #define TFM_MUL6
00330 #define TFM_MUL7
00331 #define TFM_MUL8
00332 #define TFM_MUL9
00333 #define TFM_MUL12
00334 #define TFM_MUL17
00335 #endif
00336 #ifdef TFM_HUGE_SET
00337 #define TFM_MUL20
00338 #define TFM_MUL24
00339 #define TFM_MUL28
00340 #define TFM_MUL32
00341 #if (FP_MAX_BITS >= 6144) && defined(FP_64BIT)
00342     #define TFM_MUL48
00343 #endif
00344 #if (FP_MAX_BITS >= 8192) && defined(FP_64BIT)
00345     #define TFM_MUL64
00346 #endif
00347 #endif
00348 
00349 #if 0
00350 #define TFM_SQR3
00351 #define TFM_SQR4
00352 #define TFM_SQR6
00353 #define TFM_SQR7
00354 #define TFM_SQR8
00355 #define TFM_SQR9
00356 #define TFM_SQR12
00357 #define TFM_SQR17
00358 #endif
00359 #ifdef TFM_HUGE_SET
00360 #define TFM_SQR20
00361 #define TFM_SQR24
00362 #define TFM_SQR28
00363 #define TFM_SQR32
00364 #define TFM_SQR48
00365 #define TFM_SQR64
00366 #endif
00367 
00368 /* Optional math checks (enable WOLFSSL_DEBUG_MATH to print info) */
00369 /* #define TFM_CHECK */
00370 
00371 /* Is the target a P4 Prescott
00372  */
00373 /* #define TFM_PRESCOTT */
00374 
00375 /* Do we want timing resistant fp_exptmod() ?
00376  * This makes it slower but also timing invariant with respect to the exponent
00377  */
00378 /* #define TFM_TIMING_RESISTANT */
00379 
00380 #endif /* TFM_ALREADY_SET */
00381 
00382 /* functions */
00383 
00384 /* returns a TFM ident string useful for debugging... */
00385 /*const char *fp_ident(void);*/
00386 
00387 /* initialize [or zero] an fp int */
00388 void fp_init(fp_int *a);
00389 MP_API void fp_zero(fp_int *a);
00390 MP_API void fp_clear(fp_int *a); /* uses ForceZero to clear sensitive memory */
00391 MP_API void fp_forcezero (fp_int * a);
00392 MP_API void fp_free(fp_int* a);
00393 
00394 /* zero/even/odd ? */
00395 #define fp_iszero(a) (((a)->used == 0) ? FP_YES : FP_NO)
00396 #define fp_isone(a) \
00397     ((((a)->used == 1) && ((a)->dp[0] == 1)) ? FP_YES : FP_NO)
00398 #define fp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? FP_YES : FP_NO)
00399 #define fp_isodd(a)  (((a)->used > 0  && (((a)->dp[0] & 1) == 1)) ? FP_YES : FP_NO)
00400 #define fp_isneg(a)  (((a)->sign != 0) ? FP_YES : FP_NO)
00401 
00402 /* set to a small digit */
00403 void fp_set(fp_int *a, fp_digit b);
00404 void fp_set_int(fp_int *a, unsigned long b);
00405 
00406 /* check if a bit is set */
00407 int fp_is_bit_set(fp_int *a, fp_digit b);
00408 /* set the b bit to 1 */
00409 int fp_set_bit (fp_int * a, fp_digit b);
00410 
00411 /* copy from a to b */
00412 void fp_copy(fp_int *a, fp_int *b);
00413 void fp_init_copy(fp_int *a, fp_int *b);
00414 
00415 /* clamp digits */
00416 #define fp_clamp(a)   { while ((a)->used && (a)->dp[(a)->used-1] == 0) --((a)->used); (a)->sign = (a)->used ? (a)->sign : FP_ZPOS; }
00417 
00418 /* negate and absolute */
00419 #define fp_neg(a, b)  { fp_copy(a, b); (b)->sign ^= 1; fp_clamp(b); }
00420 #define fp_abs(a, b)  { fp_copy(a, b); (b)->sign  = 0; }
00421 
00422 /* right shift x digits */
00423 void fp_rshd(fp_int *a, int x);
00424 
00425 /* right shift x bits */
00426 void fp_rshb(fp_int *a, int x);
00427 
00428 /* left shift x digits */
00429 void fp_lshd(fp_int *a, int x);
00430 
00431 /* signed comparison */
00432 int fp_cmp(fp_int *a, fp_int *b);
00433 
00434 /* unsigned comparison */
00435 int fp_cmp_mag(fp_int *a, fp_int *b);
00436 
00437 /* power of 2 operations */
00438 void fp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d);
00439 void fp_mod_2d(fp_int *a, int b, fp_int *c);
00440 void fp_mul_2d(fp_int *a, int b, fp_int *c);
00441 void fp_2expt (fp_int *a, int b);
00442 void fp_mul_2(fp_int *a, fp_int *c);
00443 void fp_div_2(fp_int *a, fp_int *c);
00444 
00445 /* Counts the number of lsbs which are zero before the first zero bit */
00446 int fp_cnt_lsb(fp_int *a);
00447 
00448 /* c = a + b */
00449 void fp_add(fp_int *a, fp_int *b, fp_int *c);
00450 
00451 /* c = a - b */
00452 void fp_sub(fp_int *a, fp_int *b, fp_int *c);
00453 
00454 /* c = a * b */
00455 void fp_mul(fp_int *a, fp_int *b, fp_int *c);
00456 
00457 /* b = a*a  */
00458 void fp_sqr(fp_int *a, fp_int *b);
00459 
00460 /* a/b => cb + d == a */
00461 int fp_div(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
00462 
00463 /* c = a mod b, 0 <= c < b  */
00464 int fp_mod(fp_int *a, fp_int *b, fp_int *c);
00465 
00466 /* compare against a single digit */
00467 int fp_cmp_d(fp_int *a, fp_digit b);
00468 
00469 /* c = a + b */
00470 void fp_add_d(fp_int *a, fp_digit b, fp_int *c);
00471 
00472 /* c = a - b */
00473 void fp_sub_d(fp_int *a, fp_digit b, fp_int *c);
00474 
00475 /* c = a * b */
00476 void fp_mul_d(fp_int *a, fp_digit b, fp_int *c);
00477 
00478 /* a/b => cb + d == a */
00479 /*int fp_div_d(fp_int *a, fp_digit b, fp_int *c, fp_digit *d);*/
00480 
00481 /* c = a mod b, 0 <= c < b  */
00482 /*int fp_mod_d(fp_int *a, fp_digit b, fp_digit *c);*/
00483 
00484 /* ---> number theory <--- */
00485 /* d = a + b (mod c) */
00486 /*int fp_addmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);*/
00487 
00488 /* d = a - b (mod c) */
00489 /*int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);*/
00490 
00491 /* d = a * b (mod c) */
00492 int fp_mulmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
00493 
00494 /* d = a - b (mod c) */
00495 int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
00496 
00497 /* d = a + b (mod c) */
00498 int fp_addmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
00499 
00500 /* c = a * a (mod b) */
00501 int fp_sqrmod(fp_int *a, fp_int *b, fp_int *c);
00502 
00503 /* c = 1/a (mod b) */
00504 int fp_invmod(fp_int *a, fp_int *b, fp_int *c);
00505 
00506 /* c = (a, b) */
00507 /*void fp_gcd(fp_int *a, fp_int *b, fp_int *c);*/
00508 
00509 /* c = [a, b] */
00510 /*void fp_lcm(fp_int *a, fp_int *b, fp_int *c);*/
00511 
00512 /* setups the montgomery reduction */
00513 int fp_montgomery_setup(fp_int *a, fp_digit *mp);
00514 
00515 /* computes a = B**n mod b without division or multiplication useful for
00516  * normalizing numbers in a Montgomery system.
00517  */
00518 void fp_montgomery_calc_normalization(fp_int *a, fp_int *b);
00519 
00520 /* computes x/R == x (mod N) via Montgomery Reduction */
00521 void fp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp);
00522 
00523 /* d = a**b (mod c) */
00524 int fp_exptmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
00525 
00526 /* primality stuff */
00527 
00528 /* perform a Miller-Rabin test of a to the base b and store result in "result" */
00529 /*void fp_prime_miller_rabin (fp_int * a, fp_int * b, int *result);*/
00530 
00531 #define FP_PRIME_SIZE      256
00532 /* 256 trial divisions + 8 Miller-Rabins, returns FP_YES if probable prime  */
00533 /*int fp_isprime(fp_int *a);*/
00534 /* extended version of fp_isprime, do 't' Miller-Rabins instead of only 8 */
00535 /*int fp_isprime_ex(fp_int *a, int t);*/
00536 
00537 /* Primality generation flags */
00538 /*#define TFM_PRIME_BBS      0x0001 */ /* BBS style prime */
00539 /*#define TFM_PRIME_SAFE     0x0002 */ /* Safe prime (p-1)/2 == prime */
00540 /*#define TFM_PRIME_2MSB_OFF 0x0004 */ /* force 2nd MSB to 0 */
00541 /*#define TFM_PRIME_2MSB_ON  0x0008 */ /* force 2nd MSB to 1 */
00542 
00543 /* callback for fp_prime_random, should fill dst with random bytes and return how many read [up to len] */
00544 /*typedef int tfm_prime_callback(unsigned char *dst, int len, void *dat);*/
00545 
00546 /*#define fp_prime_random(a, t, size, bbs, cb, dat) fp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?TFM_PRIME_BBS:0, cb, dat)*/
00547 
00548 /*int fp_prime_random_ex(fp_int *a, int t, int size, int flags, tfm_prime_callback cb, void *dat);*/
00549 
00550 /* radix conversions */
00551 int fp_count_bits(fp_int *a);
00552 int fp_leading_bit(fp_int *a);
00553 
00554 int fp_unsigned_bin_size(fp_int *a);
00555 void fp_read_unsigned_bin(fp_int *a, const unsigned char *b, int c);
00556 void fp_to_unsigned_bin(fp_int *a, unsigned char *b);
00557 int fp_to_unsigned_bin_at_pos(int x, fp_int *t, unsigned char *b);
00558 
00559 /*int fp_signed_bin_size(fp_int *a);*/
00560 /*void fp_read_signed_bin(fp_int *a, const unsigned char *b, int c);*/
00561 /*void fp_to_signed_bin(fp_int *a, unsigned char *b);*/
00562 
00563 /*int fp_read_radix(fp_int *a, char *str, int radix);*/
00564 /*int fp_toradix(fp_int *a, char *str, int radix);*/
00565 /*int fp_toradix_n(fp_int * a, char *str, int radix, int maxlen);*/
00566 
00567 
00568 /* VARIOUS LOW LEVEL STUFFS */
00569 void s_fp_add(fp_int *a, fp_int *b, fp_int *c);
00570 void s_fp_sub(fp_int *a, fp_int *b, fp_int *c);
00571 void fp_reverse(unsigned char *s, int len);
00572 
00573 void fp_mul_comba(fp_int *a, fp_int *b, fp_int *c);
00574 
00575 void fp_mul_comba_small(fp_int *a, fp_int *b, fp_int *c);
00576 void fp_mul_comba3(fp_int *a, fp_int *b, fp_int *c);
00577 void fp_mul_comba4(fp_int *a, fp_int *b, fp_int *c);
00578 void fp_mul_comba6(fp_int *a, fp_int *b, fp_int *c);
00579 void fp_mul_comba7(fp_int *a, fp_int *b, fp_int *c);
00580 void fp_mul_comba8(fp_int *a, fp_int *b, fp_int *c);
00581 void fp_mul_comba9(fp_int *a, fp_int *b, fp_int *c);
00582 void fp_mul_comba12(fp_int *a, fp_int *b, fp_int *c);
00583 void fp_mul_comba17(fp_int *a, fp_int *b, fp_int *c);
00584 void fp_mul_comba20(fp_int *a, fp_int *b, fp_int *c);
00585 void fp_mul_comba24(fp_int *a, fp_int *b, fp_int *c);
00586 void fp_mul_comba28(fp_int *a, fp_int *b, fp_int *c);
00587 void fp_mul_comba32(fp_int *a, fp_int *b, fp_int *c);
00588 void fp_mul_comba48(fp_int *a, fp_int *b, fp_int *c);
00589 void fp_mul_comba64(fp_int *a, fp_int *b, fp_int *c);
00590 void fp_sqr_comba(fp_int *a, fp_int *b);
00591 void fp_sqr_comba_small(fp_int *a, fp_int *b);
00592 void fp_sqr_comba3(fp_int *a, fp_int *b);
00593 void fp_sqr_comba4(fp_int *a, fp_int *b);
00594 void fp_sqr_comba6(fp_int *a, fp_int *b);
00595 void fp_sqr_comba7(fp_int *a, fp_int *b);
00596 void fp_sqr_comba8(fp_int *a, fp_int *b);
00597 void fp_sqr_comba9(fp_int *a, fp_int *b);
00598 void fp_sqr_comba12(fp_int *a, fp_int *b);
00599 void fp_sqr_comba17(fp_int *a, fp_int *b);
00600 void fp_sqr_comba20(fp_int *a, fp_int *b);
00601 void fp_sqr_comba24(fp_int *a, fp_int *b);
00602 void fp_sqr_comba28(fp_int *a, fp_int *b);
00603 void fp_sqr_comba32(fp_int *a, fp_int *b);
00604 void fp_sqr_comba48(fp_int *a, fp_int *b);
00605 void fp_sqr_comba64(fp_int *a, fp_int *b);
00606 
00607 /*extern const char *fp_s_rmap;*/
00608 
00609 
00610 /**
00611  * Used by wolfSSL
00612  */
00613 
00614 /* Types */
00615 typedef fp_digit mp_digit;
00616 typedef fp_word  mp_word;
00617 typedef fp_int mp_int;
00618 #define MP_INT_DEFINED
00619 
00620 /* Constants */
00621 #define MP_LT   FP_LT   /* less than    */
00622 #define MP_EQ   FP_EQ   /* equal to     */
00623 #define MP_GT   FP_GT   /* greater than */
00624 #define MP_VAL  FP_VAL  /* invalid */
00625 #define MP_MEM  FP_MEM  /* memory error */
00626 #define MP_NOT_INF FP_NOT_INF /* point not at infinity */
00627 #define MP_OKAY FP_OKAY /* ok result    */
00628 #define MP_NO   FP_NO   /* yes/no result */
00629 #define MP_YES  FP_YES  /* yes/no result */
00630 #define MP_ZPOS FP_ZPOS
00631 #define MP_NEG  FP_NEG
00632 #define MP_MASK FP_MASK
00633 
00634 /* Prototypes */
00635 #define mp_zero(a)  fp_zero(a)
00636 #define mp_isone(a)  fp_isone(a)
00637 #define mp_iseven(a)  fp_iseven(a)
00638 #define mp_isneg(a)   fp_isneg(a)
00639 MP_API int  mp_init (mp_int * a);
00640 MP_API void mp_clear (mp_int * a);
00641 MP_API void mp_free (mp_int * a);
00642 MP_API void mp_forcezero (mp_int * a);
00643 MP_API int  mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e,
00644                          mp_int* f);
00645 
00646 MP_API int  mp_add (mp_int * a, mp_int * b, mp_int * c);
00647 MP_API int  mp_sub (mp_int * a, mp_int * b, mp_int * c);
00648 MP_API int  mp_add_d (mp_int * a, mp_digit b, mp_int * c);
00649 
00650 MP_API int  mp_mul (mp_int * a, mp_int * b, mp_int * c);
00651 MP_API int  mp_mul_d (mp_int * a, mp_digit b, mp_int * c);
00652 MP_API int  mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d);
00653 MP_API int  mp_submod (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
00654 MP_API int  mp_addmod (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
00655 MP_API int  mp_mod(mp_int *a, mp_int *b, mp_int *c);
00656 MP_API int  mp_invmod(mp_int *a, mp_int *b, mp_int *c);
00657 MP_API int  mp_exptmod (mp_int * g, mp_int * x, mp_int * p, mp_int * y);
00658 MP_API int  mp_mul_2d(mp_int *a, int b, mp_int *c);
00659 
00660 MP_API int  mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d);
00661 
00662 MP_API int  mp_cmp(mp_int *a, mp_int *b);
00663 MP_API int  mp_cmp_d(mp_int *a, mp_digit b);
00664 
00665 MP_API int  mp_unsigned_bin_size(mp_int * a);
00666 MP_API int  mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c);
00667 MP_API int  mp_to_unsigned_bin_at_pos(int x, mp_int *t, unsigned char *b);
00668 MP_API int  mp_to_unsigned_bin (mp_int * a, unsigned char *b);
00669 
00670 MP_API int  mp_sub_d(fp_int *a, fp_digit b, fp_int *c);
00671 MP_API int  mp_copy(fp_int* a, fp_int* b);
00672 MP_API int  mp_isodd(mp_int* a);
00673 MP_API int  mp_iszero(mp_int* a);
00674 MP_API int  mp_count_bits(mp_int *a);
00675 MP_API int  mp_leading_bit(mp_int *a);
00676 MP_API int  mp_set_int(mp_int *a, unsigned long b);
00677 MP_API int  mp_is_bit_set (mp_int * a, mp_digit b);
00678 MP_API int  mp_set_bit (mp_int * a, mp_digit b);
00679 MP_API void mp_rshb(mp_int *a, int x);
00680 MP_API void mp_rshd(mp_int *a, int x);
00681 MP_API int mp_toradix (mp_int *a, char *str, int radix);
00682 MP_API int mp_radix_size (mp_int * a, int radix, int *size);
00683 
00684 #ifdef WOLFSSL_DEBUG_MATH
00685     MP_API void mp_dump(const char* desc, mp_int* a, byte verbose);
00686 #else
00687     #define mp_dump(desc, a, verbose)
00688 #endif
00689 
00690 #ifdef HAVE_ECC
00691     MP_API int mp_read_radix(mp_int* a, const char* str, int radix);
00692     MP_API int mp_sqr(fp_int *a, fp_int *b);
00693     MP_API int mp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp);
00694     MP_API int mp_montgomery_setup(fp_int *a, fp_digit *rho);
00695     MP_API int mp_div_2(fp_int * a, fp_int * b);
00696     MP_API int mp_init_copy(fp_int * a, fp_int * b);
00697 #endif
00698 
00699 #if defined(HAVE_ECC) || !defined(NO_RSA) || !defined(NO_DSA)
00700     MP_API int mp_set(fp_int *a, fp_digit b);
00701 #endif
00702 
00703 #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
00704     MP_API int mp_sqrmod(mp_int* a, mp_int* b, mp_int* c);
00705     MP_API int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
00706 #endif
00707 
00708 #ifdef WOLFSSL_KEY_GEN
00709 MP_API int  mp_gcd(fp_int *a, fp_int *b, fp_int *c);
00710 MP_API int  mp_lcm(fp_int *a, fp_int *b, fp_int *c);
00711 MP_API int  mp_prime_is_prime(mp_int* a, int t, int* result);
00712 MP_API int  mp_rand_prime(mp_int* N, int len, WC_RNG* rng, void* heap);
00713 MP_API int  mp_exch(mp_int *a, mp_int *b);
00714 #endif /* WOLFSSL_KEY_GEN */
00715 
00716 MP_API int  mp_cnt_lsb(fp_int *a);
00717 MP_API int  mp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d);
00718 MP_API int  mp_mod_d(fp_int* a, fp_digit b, fp_digit* c);
00719 MP_API int  mp_lshd (mp_int * a, int b);
00720 
00721 WOLFSSL_API word32 CheckRunTimeFastMath(void);
00722 
00723 /* If user uses RSA, DH, DSA, or ECC math lib directly then fast math FP_SIZE
00724    must match, return 1 if a match otherwise 0 */
00725 #define CheckFastMathSettings() (FP_SIZE == CheckRunTimeFastMath())
00726 
00727 
00728 /* wolf big int and common functions */
00729 #include <wolfssl/wolfcrypt/wolfmath.h>
00730 
00731 
00732 #ifdef __cplusplus
00733    }
00734 #endif
00735 
00736 #endif  /* WOLF_CRYPT_TFM_H */
00737 
00738