Xuyi Wang / wolfcrypt

Dependents:   OS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers integer.h Source File

integer.h

00001 /* integer.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  * Based on public domain LibTomMath 0.38 by Tom St Denis, tomstdenis@iahu.ca,
00025  * http://math.libtomcrypt.com
00026  */
00027 
00028 
00029 #ifndef WOLF_CRYPT_INTEGER_H
00030 #define WOLF_CRYPT_INTEGER_H
00031 
00032 /* may optionally use fast math instead, not yet supported on all platforms and
00033    may not be faster on all
00034 */
00035 #include <wolfcrypt/types.h>       /* will set MP_xxBIT if not default */
00036 #ifdef WOLFSSL_SP_MATH
00037     #include <wolfcrypt/sp_int.h>
00038 #elif defined(USE_FAST_MATH)
00039     #include <wolfcrypt/tfm.h>
00040 #else
00041 
00042 #include <wolfcrypt/random.h>
00043 
00044 #ifndef CHAR_BIT
00045     #include <limits.h>
00046 #endif
00047 
00048 #include <wolfcrypt/mpi_class.h>
00049 
00050 /* wolf big int and common functions */
00051 #include <wolfcrypt/wolfmath.h>
00052 
00053 
00054 #ifdef WOLFSSL_PUBLIC_MP
00055     #define MP_API   WOLFSSL_API
00056 #else
00057     #define MP_API
00058 #endif
00059 
00060 #ifndef MIN
00061    #define MIN(x,y) ((x)<(y)?(x):(y))
00062 #endif
00063 
00064 #ifndef MAX
00065    #define MAX(x,y) ((x)>(y)?(x):(y))
00066 #endif
00067 
00068 #ifdef __cplusplus
00069 extern "C" {
00070 
00071 /* C++ compilers don't like assigning void * to mp_digit * */
00072 #define  OPT_CAST(x)  (x *)
00073 
00074 #elif defined(_SH3)
00075 
00076 /* SuperH SH3 compiler doesn't like assigning voi* to mp_digit* */
00077 #define  OPT_CAST(x)  (x *)
00078 
00079 #else
00080 
00081 /* C on the other hand doesn't care */
00082 #define  OPT_CAST(x)
00083 
00084 #endif /* __cplusplus */
00085 
00086 
00087 /* detect 64-bit mode if possible */
00088 #if defined(__x86_64__)
00089    #if !(defined(MP_64BIT) && defined(MP_16BIT) && defined(MP_8BIT))
00090       #define MP_64BIT
00091    #endif
00092 #endif
00093 /* if intel compiler doesn't provide 128 bit type don't turn on 64bit */
00094 #if defined(MP_64BIT) && defined(__INTEL_COMPILER) && !defined(HAVE___UINT128_T)
00095     #undef MP_64BIT
00096 #endif
00097 
00098 
00099 /* allow user to define on mp_digit, mp_word, DIGIT_BIT types */
00100 #ifndef WOLFSSL_BIGINT_TYPES
00101 
00102 /* some default configurations.
00103  *
00104  * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
00105  * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
00106  *
00107  * At the very least a mp_digit must be able to hold 7 bits
00108  * [any size beyond that is ok provided it doesn't overflow the data type]
00109  */
00110 #ifdef MP_8BIT
00111    typedef unsigned char      mp_digit;
00112    typedef unsigned short     mp_word;
00113 #elif defined(MP_16BIT) || defined(NO_64BIT)
00114    typedef unsigned short     mp_digit;
00115    typedef unsigned int       mp_word;
00116    #define DIGIT_BIT          12
00117 #elif defined(MP_64BIT)
00118    /* for GCC only on supported platforms */
00119    typedef unsigned long long mp_digit;  /* 64 bit type, 128 uses mode(TI) */
00120    typedef unsigned long      mp_word __attribute__ ((mode(TI)));
00121 
00122    #define DIGIT_BIT          60
00123 #else
00124    /* this is the default case, 28-bit digits */
00125 
00126    #if defined(_MSC_VER) || defined(__BORLANDC__)
00127       typedef unsigned __int64   ulong64;
00128    #else
00129       typedef unsigned long long ulong64;
00130    #endif
00131 
00132    typedef unsigned int       mp_digit;  /* long could be 64 now, changed TAO */
00133    typedef ulong64            mp_word;
00134 
00135 #ifdef MP_31BIT
00136    /* this is an extension that uses 31-bit digits */
00137    #define DIGIT_BIT          31
00138 #else
00139    /* default case is 28-bit digits, defines MP_28BIT as a handy test macro */
00140    #define DIGIT_BIT          28
00141    #define MP_28BIT
00142 #endif
00143 #endif
00144 
00145 #endif /* WOLFSSL_BIGINT_TYPES */
00146 
00147 /* otherwise the bits per digit is calculated automatically from the size of
00148    a mp_digit */
00149 #ifndef DIGIT_BIT
00150    #define DIGIT_BIT ((int)((CHAR_BIT * sizeof(mp_digit) - 1)))
00151       /* bits per digit */
00152 #endif
00153 
00154 #define MP_DIGIT_BIT     DIGIT_BIT
00155 #define MP_MASK          ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
00156 #define MP_DIGIT_MAX     MP_MASK
00157 
00158 /* equalities */
00159 #define MP_LT        -1   /* less than */
00160 #define MP_EQ         0   /* equal to */
00161 #define MP_GT         1   /* greater than */
00162 
00163 #define MP_ZPOS       0   /* positive integer */
00164 #define MP_NEG        1   /* negative */
00165 
00166 #define MP_OKAY       0   /* ok result */
00167 #define MP_MEM        -2  /* out of mem */
00168 #define MP_VAL        -3  /* invalid input */
00169 #define MP_NOT_INF    -4  /* point not at infinity */
00170 #define MP_RANGE      MP_NOT_INF
00171 
00172 #define MP_YES        1   /* yes response */
00173 #define MP_NO         0   /* no response */
00174 
00175 /* Primality generation flags */
00176 #define LTM_PRIME_BBS      0x0001 /* BBS style prime */
00177 #define LTM_PRIME_SAFE     0x0002 /* Safe prime (p-1)/2 == prime */
00178 #define LTM_PRIME_2MSB_ON  0x0008 /* force 2nd MSB to 1 */
00179 
00180 typedef int           mp_err;
00181 
00182 /* define this to use lower memory usage routines (exptmods mostly) */
00183 #define MP_LOW_MEM
00184 
00185 /* default precision */
00186 #ifndef MP_PREC
00187    #ifndef MP_LOW_MEM
00188       #define MP_PREC                 32     /* default digits of precision */
00189    #else
00190       #define MP_PREC                 1      /* default digits of precision */
00191    #endif
00192 #endif
00193 
00194 /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD -
00195    BITS_PER_DIGIT*2) */
00196 #define MP_WARRAY  (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
00197 
00198 #ifdef HAVE_WOLF_BIGINT
00199     struct WC_BIGINT;
00200 #endif
00201 
00202 /* the mp_int structure */
00203 typedef struct mp_int {
00204     int used, alloc, sign;
00205     mp_digit *dp;
00206 
00207 #ifdef HAVE_WOLF_BIGINT
00208     struct WC_BIGINT raw; /* unsigned binary (big endian) */
00209 #endif
00210 } mp_int;
00211 #define MP_INT_DEFINED
00212 
00213 /* callback for mp_prime_random, should fill dst with random bytes and return
00214    how many read [up to len] */
00215 typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
00216 
00217 
00218 #define USED(m)    ((m)->used)
00219 #define DIGIT(m,k) ((m)->dp[(k)])
00220 #define SIGN(m)    ((m)->sign)
00221 
00222 
00223 /* ---> Basic Manipulations <--- */
00224 #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
00225 #define mp_isone(a) \
00226     (((((a)->used == 1)) && ((a)->dp[0] == 1u)) ? MP_YES : MP_NO)
00227 #define mp_iseven(a) \
00228     (((a)->used > 0 && (((a)->dp[0] & 1u) == 0u)) ? MP_YES : MP_NO)
00229 #define mp_isodd(a) \
00230     (((a)->used > 0 && (((a)->dp[0] & 1u) == 1u)) ? MP_YES : MP_NO)
00231 #define mp_isneg(a)  (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO)
00232 
00233 /* number of primes */
00234 #ifdef MP_8BIT
00235    #define PRIME_SIZE      31
00236 #else
00237    #define PRIME_SIZE      256
00238 #endif
00239 
00240 #ifndef MAX_INVMOD_SZ
00241     #if defined(WOLFSSL_MYSQL_COMPATIBLE)
00242         #define MAX_INVMOD_SZ 8192
00243     #else
00244         #define MAX_INVMOD_SZ 4096
00245     #endif
00246 #endif
00247 
00248 #define mp_prime_random(a, t, size, bbs, cb, dat) \
00249    mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?LTM_PRIME_BBS:0, cb, dat)
00250 
00251 #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
00252 #define mp_raw_size(mp)           mp_signed_bin_size(mp)
00253 #define mp_toraw(mp, str)         mp_to_signed_bin((mp), (str))
00254 #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
00255 #define mp_mag_size(mp)           mp_unsigned_bin_size(mp)
00256 #define mp_tomag(mp, str)         mp_to_unsigned_bin((mp), (str))
00257 
00258 #define MP_RADIX_BIN  2
00259 #define MP_RADIX_OCT  8
00260 #define MP_RADIX_DEC  10
00261 #define MP_RADIX_HEX  16
00262 #define MP_RADIX_MAX  64
00263 
00264 #define mp_tobinary(M, S)  mp_toradix((M), (S), MP_RADIX_BIN)
00265 #define mp_tooctal(M, S)   mp_toradix((M), (S), MP_RADIX_OCT)
00266 #define mp_todecimal(M, S) mp_toradix((M), (S), MP_RADIX_DEC)
00267 #define mp_tohex(M, S)     mp_toradix((M), (S), MP_RADIX_HEX)
00268 
00269 #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
00270 
00271 #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) || \
00272     defined(WOLFSSL_DEBUG_MATH) || defined(DEBUG_WOLFSSL)
00273 extern const char *mp_s_rmap;
00274 #endif
00275 
00276 /* 6 functions needed by Rsa */
00277 MP_API int  mp_init (mp_int * a);
00278 MP_API void mp_clear (mp_int * a);
00279 MP_API void mp_free (mp_int * a);
00280 MP_API void mp_forcezero(mp_int * a);
00281 MP_API int  mp_unsigned_bin_size(mp_int * a);
00282 MP_API int  mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c);
00283 MP_API int  mp_to_unsigned_bin_at_pos(int x, mp_int *t, unsigned char *b);
00284 MP_API int  mp_to_unsigned_bin (mp_int * a, unsigned char *b);
00285 MP_API int  mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y);
00286 /* end functions needed by Rsa */
00287 
00288 /* functions added to support above needed, removed TOOM and KARATSUBA */
00289 MP_API int  mp_count_bits (mp_int * a);
00290 MP_API int  mp_leading_bit (mp_int * a);
00291 MP_API int  mp_init_copy (mp_int * a, mp_int * b);
00292 MP_API int  mp_copy (mp_int * a, mp_int * b);
00293 MP_API int  mp_grow (mp_int * a, int size);
00294 MP_API int  mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d);
00295 MP_API void mp_zero (mp_int * a);
00296 MP_API void mp_clamp (mp_int * a);
00297 MP_API void mp_exch (mp_int * a, mp_int * b);
00298 MP_API void mp_rshd (mp_int * a, int b);
00299 MP_API void mp_rshb (mp_int * a, int b);
00300 MP_API int  mp_mod_2d (mp_int * a, int b, mp_int * c);
00301 MP_API int  mp_mul_2d (mp_int * a, int b, mp_int * c);
00302 MP_API int  mp_lshd (mp_int * a, int b);
00303 MP_API int  mp_abs (mp_int * a, mp_int * b);
00304 MP_API int  mp_invmod (mp_int * a, mp_int * b, mp_int * c);
00305 int  fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c);
00306 MP_API int  mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c);
00307 MP_API int  mp_cmp_mag (mp_int * a, mp_int * b);
00308 MP_API int  mp_cmp (mp_int * a, mp_int * b);
00309 MP_API int  mp_cmp_d(mp_int * a, mp_digit b);
00310 MP_API int  mp_set (mp_int * a, mp_digit b);
00311 MP_API int  mp_is_bit_set (mp_int * a, mp_digit b);
00312 MP_API int  mp_mod (mp_int * a, mp_int * b, mp_int * c);
00313 MP_API int  mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d);
00314 MP_API int  mp_div_2(mp_int * a, mp_int * b);
00315 MP_API int  mp_add (mp_int * a, mp_int * b, mp_int * c);
00316 int  s_mp_add (mp_int * a, mp_int * b, mp_int * c);
00317 int  s_mp_sub (mp_int * a, mp_int * b, mp_int * c);
00318 MP_API int  mp_sub (mp_int * a, mp_int * b, mp_int * c);
00319 MP_API int  mp_reduce_is_2k_l(mp_int *a);
00320 MP_API int  mp_reduce_is_2k(mp_int *a);
00321 MP_API int  mp_dr_is_modulus(mp_int *a);
00322 MP_API int  mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y,
00323                              int);
00324 MP_API int  mp_montgomery_setup (mp_int * n, mp_digit * rho);
00325 int  fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho);
00326 MP_API int  mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho);
00327 MP_API void mp_dr_setup(mp_int *a, mp_digit *d);
00328 MP_API int  mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k);
00329 MP_API int  mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d);
00330 int  fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
00331 int  s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
00332 MP_API int  mp_reduce_2k_setup_l(mp_int *a, mp_int *d);
00333 MP_API int  mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d);
00334 MP_API int  mp_reduce (mp_int * x, mp_int * m, mp_int * mu);
00335 MP_API int  mp_reduce_setup (mp_int * a, mp_int * b);
00336 int  s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode);
00337 MP_API int  mp_montgomery_calc_normalization (mp_int * a, mp_int * b);
00338 int  s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
00339 int  s_mp_sqr (mp_int * a, mp_int * b);
00340 int  fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
00341 int  fast_s_mp_sqr (mp_int * a, mp_int * b);
00342 MP_API int  mp_init_size (mp_int * a, int size);
00343 MP_API int  mp_div_3 (mp_int * a, mp_int *c, mp_digit * d);
00344 MP_API int  mp_mul_2(mp_int * a, mp_int * b);
00345 MP_API int  mp_mul (mp_int * a, mp_int * b, mp_int * c);
00346 MP_API int  mp_sqr (mp_int * a, mp_int * b);
00347 MP_API int  mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d);
00348 MP_API int  mp_submod (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
00349 MP_API int  mp_addmod (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
00350 MP_API int  mp_mul_d (mp_int * a, mp_digit b, mp_int * c);
00351 MP_API int  mp_2expt (mp_int * a, int b);
00352 MP_API int  mp_set_bit (mp_int * a, int b);
00353 MP_API int  mp_reduce_2k_setup(mp_int *a, mp_digit *d);
00354 MP_API int  mp_add_d (mp_int* a, mp_digit b, mp_int* c);
00355 MP_API int  mp_set_int (mp_int * a, unsigned long b);
00356 MP_API int  mp_sub_d (mp_int * a, mp_digit b, mp_int * c);
00357 /* end support added functions */
00358 
00359 /* added */
00360 MP_API int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e,
00361                          mp_int* f);
00362 MP_API int mp_toradix (mp_int *a, char *str, int radix);
00363 MP_API int mp_radix_size (mp_int * a, int radix, int *size);
00364 
00365 #ifdef WOLFSSL_DEBUG_MATH
00366     MP_API void mp_dump(const char* desc, mp_int* a, byte verbose);
00367 #else
00368     #define mp_dump(desc, a, verbose)
00369 #endif
00370 
00371 #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
00372     MP_API int mp_sqrmod(mp_int* a, mp_int* b, mp_int* c);
00373 #endif
00374 #if !defined(NO_DSA) || defined(HAVE_ECC)
00375     MP_API int mp_read_radix(mp_int* a, const char* str, int radix);
00376 #endif
00377 
00378 #ifdef WOLFSSL_KEY_GEN
00379     MP_API int mp_prime_is_prime (mp_int * a, int t, int *result);
00380     MP_API int mp_gcd (mp_int * a, mp_int * b, mp_int * c);
00381     MP_API int mp_lcm (mp_int * a, mp_int * b, mp_int * c);
00382     MP_API int mp_rand_prime(mp_int* N, int len, WC_RNG* rng, void* heap);
00383 #endif
00384 
00385 MP_API int mp_cnt_lsb(mp_int *a);
00386 MP_API int mp_mod_d(mp_int* a, mp_digit b, mp_digit* c);
00387 
00388 
00389 /* wolf big int and common functions */
00390 #include <wolfcrypt/wolfmath.h>
00391 
00392 
00393 #ifdef __cplusplus
00394    }
00395 #endif
00396 
00397 
00398 #endif /* USE_FAST_MATH */
00399 
00400 #endif  /* WOLF_CRYPT_INTEGER_H */
00401 
00402