]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/WolfSSL/cyassl/ctaocrypt/integer.h
77b5552c70fba06b16f332568a81a3161c7faa72
[freertos] / FreeRTOS-Plus / Source / WolfSSL / cyassl / ctaocrypt / integer.h
1 /* integer.h
2  *
3  * Copyright (C) 2006-2014 wolfSSL Inc.
4  *
5  * This file is part of CyaSSL.
6  *
7  * CyaSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * CyaSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 /*
23  * Based on public domain LibTomMath 0.38 by Tom St Denis, tomstdenis@iahu.ca,
24  * http://math.libtomcrypt.com
25  */
26
27
28 #ifndef CTAO_CRYPT_INTEGER_H
29 #define CTAO_CRYPT_INTEGER_H
30
31 /* may optionally use fast math instead, not yet supported on all platforms and
32    may not be faster on all
33 */
34 #include <cyassl/ctaocrypt/types.h>       /* will set MP_xxBIT if not default */
35 #ifdef USE_FAST_MATH
36     #include <cyassl/ctaocrypt/tfm.h>
37 #else
38
39 #ifndef CHAR_BIT
40     #include <limits.h>
41 #endif
42
43 #include <cyassl/ctaocrypt/mpi_class.h>
44
45 #ifndef MIN
46    #define MIN(x,y) ((x)<(y)?(x):(y))
47 #endif
48
49 #ifndef MAX
50    #define MAX(x,y) ((x)>(y)?(x):(y))
51 #endif
52
53 #ifdef __cplusplus
54 extern "C" {
55
56 /* C++ compilers don't like assigning void * to mp_digit * */
57 #define  OPT_CAST(x)  (x *)
58
59 #else
60
61 /* C on the other hand doesn't care */
62 #define  OPT_CAST(x)
63
64 #endif
65
66
67 /* detect 64-bit mode if possible */
68 #if defined(__x86_64__) 
69    #if !(defined(MP_64BIT) && defined(MP_16BIT) && defined(MP_8BIT))
70       #define MP_64BIT
71    #endif
72 #endif
73 /* if intel compiler doesn't provide 128 bit type don't turn on 64bit */
74 #if defined(MP_64BIT) && defined(__INTEL_COMPILER) && !defined(HAVE___UINT128_T)
75     #undef MP_64BIT
76 #endif
77
78 /* some default configurations.
79  *
80  * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
81  * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
82  *
83  * At the very least a mp_digit must be able to hold 7 bits
84  * [any size beyond that is ok provided it doesn't overflow the data type]
85  */
86 #ifdef MP_8BIT
87    typedef unsigned char      mp_digit;
88    typedef unsigned short     mp_word;
89 #elif defined(MP_16BIT) || defined(NO_64BIT)
90    typedef unsigned short     mp_digit;
91    typedef unsigned int       mp_word;
92 #elif defined(MP_64BIT)
93    /* for GCC only on supported platforms */
94    typedef unsigned long long mp_digit;  /* 64 bit type, 128 uses mode(TI) */
95    typedef unsigned long      mp_word __attribute__ ((mode(TI)));
96
97    #define DIGIT_BIT          60
98 #else
99    /* this is the default case, 28-bit digits */
100    
101    #if defined(_MSC_VER) || defined(__BORLANDC__) 
102       typedef unsigned __int64   ulong64;
103    #else
104       typedef unsigned long long ulong64;
105    #endif
106
107    typedef unsigned int       mp_digit;  /* long could be 64 now, changed TAO */
108    typedef ulong64            mp_word;
109
110 #ifdef MP_31BIT   
111    /* this is an extension that uses 31-bit digits */
112    #define DIGIT_BIT          31
113 #else
114    /* default case is 28-bit digits, defines MP_28BIT as a handy test macro */
115    #define DIGIT_BIT          28
116    #define MP_28BIT
117 #endif   
118 #endif
119
120
121 /* otherwise the bits per digit is calculated automatically from the size of
122    a mp_digit */
123 #ifndef DIGIT_BIT
124    #define DIGIT_BIT ((int)((CHAR_BIT * sizeof(mp_digit) - 1)))
125       /* bits per digit */
126 #endif
127
128 #define MP_DIGIT_BIT     DIGIT_BIT
129 #define MP_MASK          ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
130 #define MP_DIGIT_MAX     MP_MASK
131
132 /* equalities */
133 #define MP_LT        -1   /* less than */
134 #define MP_EQ         0   /* equal to */
135 #define MP_GT         1   /* greater than */
136
137 #define MP_ZPOS       0   /* positive integer */
138 #define MP_NEG        1   /* negative */
139
140 #define MP_OKAY       0   /* ok result */
141 #define MP_MEM        -2  /* out of mem */
142 #define MP_VAL        -3  /* invalid input */
143 #define MP_RANGE      MP_VAL
144
145 #define MP_YES        1   /* yes response */
146 #define MP_NO         0   /* no response */
147
148 /* Primality generation flags */
149 #define LTM_PRIME_BBS      0x0001 /* BBS style prime */
150 #define LTM_PRIME_SAFE     0x0002 /* Safe prime (p-1)/2 == prime */
151 #define LTM_PRIME_2MSB_ON  0x0008 /* force 2nd MSB to 1 */
152
153 typedef int           mp_err;
154
155 /* define this to use lower memory usage routines (exptmods mostly) */
156 #define MP_LOW_MEM
157
158 /* default precision */
159 #ifndef MP_PREC
160    #ifndef MP_LOW_MEM
161       #define MP_PREC                 32     /* default digits of precision */
162    #else
163       #define MP_PREC                 1      /* default digits of precision */
164    #endif   
165 #endif
166
167 /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - 
168    BITS_PER_DIGIT*2) */
169 #define MP_WARRAY  (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
170
171 /* the infamous mp_int structure */
172 typedef struct  {
173     int used, alloc, sign;
174     mp_digit *dp;
175 } mp_int;
176
177 /* callback for mp_prime_random, should fill dst with random bytes and return
178    how many read [upto len] */
179 typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
180
181
182 #define USED(m)    ((m)->used)
183 #define DIGIT(m,k) ((m)->dp[(k)])
184 #define SIGN(m)    ((m)->sign)
185
186
187 /* ---> Basic Manipulations <--- */
188 #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
189 #define mp_iseven(a) \
190     (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
191 #define mp_isodd(a) \
192     (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
193
194
195 /* number of primes */
196 #ifdef MP_8BIT
197    #define PRIME_SIZE      31
198 #else
199    #define PRIME_SIZE      256
200 #endif
201
202 #define mp_prime_random(a, t, size, bbs, cb, dat) \
203    mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?LTM_PRIME_BBS:0, cb, dat)
204
205 #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
206 #define mp_raw_size(mp)           mp_signed_bin_size(mp)
207 #define mp_toraw(mp, str)         mp_to_signed_bin((mp), (str))
208 #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
209 #define mp_mag_size(mp)           mp_unsigned_bin_size(mp)
210 #define mp_tomag(mp, str)         mp_to_unsigned_bin((mp), (str))
211
212 #define mp_tobinary(M, S)  mp_toradix((M), (S), 2)
213 #define mp_tooctal(M, S)   mp_toradix((M), (S), 8)
214 #define mp_todecimal(M, S) mp_toradix((M), (S), 10)
215 #define mp_tohex(M, S)     mp_toradix((M), (S), 16)
216
217 #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
218
219 extern const char *mp_s_rmap;
220
221 /* 6 functions needed by Rsa */
222 int  mp_init (mp_int * a);
223 void mp_clear (mp_int * a);
224 int  mp_unsigned_bin_size(mp_int * a);
225 int  mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c);
226 int  mp_to_unsigned_bin (mp_int * a, unsigned char *b);
227 int  mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y);
228 /* end functions needed by Rsa */
229
230 /* functions added to support above needed, removed TOOM and KARATSUBA */
231 int  mp_count_bits (mp_int * a);
232 int  mp_leading_bit (mp_int * a);
233 int  mp_init_copy (mp_int * a, mp_int * b);
234 int  mp_copy (mp_int * a, mp_int * b);
235 int  mp_grow (mp_int * a, int size);
236 int  mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d);
237 void mp_zero (mp_int * a);
238 void mp_clamp (mp_int * a);
239 void mp_exch (mp_int * a, mp_int * b);
240 void mp_rshd (mp_int * a, int b);
241 void mp_rshb (mp_int * a, int b);
242 int  mp_mod_2d (mp_int * a, int b, mp_int * c);
243 int  mp_mul_2d (mp_int * a, int b, mp_int * c);
244 int  mp_lshd (mp_int * a, int b);
245 int  mp_abs (mp_int * a, mp_int * b);
246 int  mp_invmod (mp_int * a, mp_int * b, mp_int * c);
247 int  fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c);
248 int  mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c);
249 int  mp_cmp_mag (mp_int * a, mp_int * b);
250 int  mp_cmp (mp_int * a, mp_int * b);
251 int  mp_cmp_d(mp_int * a, mp_digit b);
252 void mp_set (mp_int * a, mp_digit b);
253 int  mp_mod (mp_int * a, mp_int * b, mp_int * c);
254 int  mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d);
255 int  mp_div_2(mp_int * a, mp_int * b);
256 int  mp_add (mp_int * a, mp_int * b, mp_int * c);
257 int  s_mp_add (mp_int * a, mp_int * b, mp_int * c);
258 int  s_mp_sub (mp_int * a, mp_int * b, mp_int * c);
259 int  mp_sub (mp_int * a, mp_int * b, mp_int * c);
260 int  mp_reduce_is_2k_l(mp_int *a);
261 int  mp_reduce_is_2k(mp_int *a);
262 int  mp_dr_is_modulus(mp_int *a);
263 int  mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int);
264 int  mp_montgomery_setup (mp_int * n, mp_digit * rho);
265 int  fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho);
266 int  mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho);
267 void mp_dr_setup(mp_int *a, mp_digit *d);
268 int  mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k);
269 int  mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d);
270 int  fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
271 int  s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
272 int  mp_reduce_2k_setup_l(mp_int *a, mp_int *d);
273 int  mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d);
274 int  mp_reduce (mp_int * x, mp_int * m, mp_int * mu);
275 int  mp_reduce_setup (mp_int * a, mp_int * b);
276 int  s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode);
277 int  mp_montgomery_calc_normalization (mp_int * a, mp_int * b);
278 int  s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
279 int  s_mp_sqr (mp_int * a, mp_int * b);
280 int  fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
281 int  fast_s_mp_sqr (mp_int * a, mp_int * b);
282 int  mp_init_size (mp_int * a, int size);
283 int  mp_div_3 (mp_int * a, mp_int *c, mp_digit * d);
284 int  mp_mul_2(mp_int * a, mp_int * b);
285 int  mp_mul (mp_int * a, mp_int * b, mp_int * c);
286 int  mp_sqr (mp_int * a, mp_int * b);
287 int  mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d);
288 int  mp_mul_d (mp_int * a, mp_digit b, mp_int * c);
289 int  mp_2expt (mp_int * a, int b);
290 int  mp_reduce_2k_setup(mp_int *a, mp_digit *d);
291 int  mp_add_d (mp_int* a, mp_digit b, mp_int* c);
292 int mp_set_int (mp_int * a, unsigned long b);
293 int mp_sub_d (mp_int * a, mp_digit b, mp_int * c);
294 /* end support added functions */
295
296 /* added */
297 int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e,
298                   mp_int* f);
299
300 #if defined(HAVE_ECC) || defined(CYASSL_KEY_GEN)
301     int mp_sqrmod(mp_int* a, mp_int* b, mp_int* c);
302 #endif
303 #ifdef HAVE_ECC
304     int mp_read_radix(mp_int* a, const char* str, int radix);
305 #endif
306
307 #ifdef CYASSL_KEY_GEN
308     int mp_prime_is_prime (mp_int * a, int t, int *result);
309     int mp_gcd (mp_int * a, mp_int * b, mp_int * c);
310     int mp_lcm (mp_int * a, mp_int * b, mp_int * c);
311 #endif
312
313 #ifdef __cplusplus
314    }
315 #endif
316
317
318 #endif /* USE_FAST_MATH */
319
320 #endif  /* CTAO_CRYPT_INTEGER_H */
321