A library for setting up Secure Socket Layer (SSL) connections and verifying remote hosts using certificates. Contains only the source files for mbed platform implementation of the library.

Dependents:   HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL

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