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