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