3 * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
5 * This file is part of CyaSSL.
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
23 * Based on public domain LibTomMath 0.38 by Tom St Denis, tomstdenis@iahu.ca,
24 * http://math.libtomcrypt.com
28 #ifndef CTAO_CRYPT_INTEGER_H
29 #define CTAO_CRYPT_INTEGER_H
31 /* may optionally use fast math instead, not yet supported on all platforms and
32 may not be faster on all
34 #include <cyassl/ctaocrypt/types.h> /* will set MP_xxBIT if not default */
36 #include <cyassl/ctaocrypt/tfm.h>
43 #include <cyassl/ctaocrypt/mpi_class.h>
46 #define MIN(x,y) ((x)<(y)?(x):(y))
50 #define MAX(x,y) ((x)>(y)?(x):(y))
56 /* C++ compilers don't like assigning void * to mp_digit * */
57 #define OPT_CAST(x) (x *)
61 /* C on the other hand doesn't care */
67 /* detect 64-bit mode if possible */
68 #if defined(__x86_64__)
69 #if !(defined(MP_64BIT) && defined(MP_16BIT) && defined(MP_8BIT))
74 /* some default configurations.
76 * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
77 * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
79 * At the very least a mp_digit must be able to hold 7 bits
80 * [any size beyond that is ok provided it doesn't overflow the data type]
83 typedef unsigned char mp_digit;
84 typedef unsigned short mp_word;
85 #elif defined(MP_16BIT)
86 typedef unsigned short mp_digit;
87 typedef unsigned long mp_word;
88 #elif defined(MP_64BIT)
89 /* for GCC only on supported platforms */
91 typedef unsigned long long ulong64;
92 typedef signed long long long64;
95 typedef unsigned long mp_digit;
96 typedef unsigned long mp_word __attribute__ ((mode(TI)));
100 /* this is the default case, 28-bit digits */
102 /* this is to make porting into LibTomCrypt easier :-) */
104 #if defined(_MSC_VER) || defined(__BORLANDC__)
105 typedef unsigned __int64 ulong64;
106 typedef signed __int64 long64;
108 typedef unsigned long long ulong64;
109 typedef signed long long long64;
113 typedef unsigned int mp_digit; /* long could be 64 now, changed TAO */
114 typedef ulong64 mp_word;
117 /* this is an extension that uses 31-bit digits */
120 /* default case is 28-bit digits, defines MP_28BIT as a handy test macro */
127 /* otherwise the bits per digit is calculated automatically from the size of
130 #define DIGIT_BIT ((int)((CHAR_BIT * sizeof(mp_digit) - 1)))
134 #define MP_DIGIT_BIT DIGIT_BIT
135 #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
136 #define MP_DIGIT_MAX MP_MASK
139 #define MP_LT -1 /* less than */
140 #define MP_EQ 0 /* equal to */
141 #define MP_GT 1 /* greater than */
143 #define MP_ZPOS 0 /* positive integer */
144 #define MP_NEG 1 /* negative */
146 #define MP_OKAY 0 /* ok result */
147 #define MP_MEM -2 /* out of mem */
148 #define MP_VAL -3 /* invalid input */
149 #define MP_RANGE MP_VAL
151 #define MP_YES 1 /* yes response */
152 #define MP_NO 0 /* no response */
154 /* Primality generation flags */
155 #define LTM_PRIME_BBS 0x0001 /* BBS style prime */
156 #define LTM_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */
157 #define LTM_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */
161 /* define this to use lower memory usage routines (exptmods mostly) */
164 /* default precision */
167 #define MP_PREC 32 /* default digits of precision */
169 #define MP_PREC 1 /* default digits of precision */
173 /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD -
175 #define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
177 /* the infamous mp_int structure */
179 int used, alloc, sign;
183 /* callback for mp_prime_random, should fill dst with random bytes and return
184 how many read [upto len] */
185 typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
188 #define USED(m) ((m)->used)
189 #define DIGIT(m,k) ((m)->dp[(k)])
190 #define SIGN(m) ((m)->sign)
193 /* ---> Basic Manipulations <--- */
194 #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
195 #define mp_iseven(a) \
196 (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
197 #define mp_isodd(a) \
198 (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
201 /* number of primes */
203 #define PRIME_SIZE 31
205 #define PRIME_SIZE 256
208 #define mp_prime_random(a, t, size, bbs, cb, dat) \
209 mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?LTM_PRIME_BBS:0, cb, dat)
211 #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
212 #define mp_raw_size(mp) mp_signed_bin_size(mp)
213 #define mp_toraw(mp, str) mp_to_signed_bin((mp), (str))
214 #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
215 #define mp_mag_size(mp) mp_unsigned_bin_size(mp)
216 #define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str))
218 #define mp_tobinary(M, S) mp_toradix((M), (S), 2)
219 #define mp_tooctal(M, S) mp_toradix((M), (S), 8)
220 #define mp_todecimal(M, S) mp_toradix((M), (S), 10)
221 #define mp_tohex(M, S) mp_toradix((M), (S), 16)
223 #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
225 extern const char *mp_s_rmap;
227 /* 6 functions needed by Rsa */
228 int mp_init (mp_int * a);
229 void mp_clear (mp_int * a);
230 int mp_unsigned_bin_size(mp_int * a);
231 int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c);
232 int mp_to_unsigned_bin (mp_int * a, unsigned char *b);
233 int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y);
234 /* end functions needed by Rsa */
236 /* functions added to support above needed, removed TOOM and KARATSUBA */
237 int mp_count_bits (mp_int * a);
238 int mp_init_copy (mp_int * a, mp_int * b);
239 int mp_copy (mp_int * a, mp_int * b);
240 int mp_grow (mp_int * a, int size);
241 void bn_reverse (unsigned char *s, int len);
242 int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d);
243 void mp_zero (mp_int * a);
244 void mp_clamp (mp_int * a);
245 void mp_exch (mp_int * a, mp_int * b);
246 void mp_rshd (mp_int * a, int b);
247 int mp_mod_2d (mp_int * a, int b, mp_int * c);
248 int mp_mul_2d (mp_int * a, int b, mp_int * c);
249 int mp_lshd (mp_int * a, int b);
250 int mp_abs (mp_int * a, mp_int * b);
251 int mp_invmod (mp_int * a, mp_int * b, mp_int * c);
252 int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c);
253 int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c);
254 int mp_cmp_mag (mp_int * a, mp_int * b);
255 int mp_cmp (mp_int * a, mp_int * b);
256 int mp_cmp_d(mp_int * a, mp_digit b);
257 void mp_set (mp_int * a, mp_digit b);
258 int mp_mod (mp_int * a, mp_int * b, mp_int * c);
259 int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d);
260 int mp_div_2(mp_int * a, mp_int * b);
261 int mp_add (mp_int * a, mp_int * b, mp_int * c);
262 int s_mp_add (mp_int * a, mp_int * b, mp_int * c);
263 int s_mp_sub (mp_int * a, mp_int * b, mp_int * c);
264 int mp_sub (mp_int * a, mp_int * b, mp_int * c);
265 int mp_reduce_is_2k_l(mp_int *a);
266 int mp_reduce_is_2k(mp_int *a);
267 int mp_dr_is_modulus(mp_int *a);
268 int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int);
269 int mp_montgomery_setup (mp_int * n, mp_digit * rho);
270 int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho);
271 int mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho);
272 void mp_dr_setup(mp_int *a, mp_digit *d);
273 int mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k);
274 int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d);
275 int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
276 int s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
277 int mp_reduce_2k_setup_l(mp_int *a, mp_int *d);
278 int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d);
279 int mp_reduce (mp_int * x, mp_int * m, mp_int * mu);
280 int mp_reduce_setup (mp_int * a, mp_int * b);
281 int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode);
282 int mp_montgomery_calc_normalization (mp_int * a, mp_int * b);
283 int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
284 int s_mp_sqr (mp_int * a, mp_int * b);
285 int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
286 int fast_s_mp_sqr (mp_int * a, mp_int * b);
287 int mp_init_size (mp_int * a, int size);
288 int mp_div_3 (mp_int * a, mp_int *c, mp_digit * d);
289 int mp_mul_2(mp_int * a, mp_int * b);
290 int mp_mul (mp_int * a, mp_int * b, mp_int * c);
291 int mp_sqr (mp_int * a, mp_int * b);
292 int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d);
293 int mp_mul_d (mp_int * a, mp_digit b, mp_int * c);
294 int mp_2expt (mp_int * a, int b);
295 int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
296 int mp_add_d (mp_int* a, mp_digit b, mp_int* c);
297 int mp_set_int (mp_int * a, unsigned long b);
298 /* end support added functions */
301 int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e,
304 #if defined(HAVE_ECC) || defined(CYASSL_KEY_GEN)
305 int mp_sqrmod(mp_int* a, mp_int* b, mp_int* c);
308 int mp_read_radix(mp_int* a, const char* str, int radix);
311 #ifdef CYASSL_KEY_GEN
312 int mp_prime_is_prime (mp_int * a, int t, int *result);
313 int mp_gcd (mp_int * a, mp_int * b, mp_int * c);
314 int mp_lcm (mp_int * a, mp_int * b, mp_int * c);
317 #if defined(CYASSL_KEY_GEN) || defined(HAVE_ECC) || !defined(NO_PWDBASED)
318 int mp_sub_d (mp_int * a, mp_digit b, mp_int * c);
326 #endif /* USE_FAST_MATH */
328 #endif /* CTAO_CRYPT_INTEGER_H */