www.usr.com/support/gpl/USR9107_release.1.4.tar.gz
[bcm963xx.git] / userapps / opensource / sshd / libtomcrypt / tommath.h
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis
2  *
3  * LibTomMath is library that provides for multiple-precision
4  * integer arithmetic as well as number theoretic functionality.
5  *
6  * The library is designed directly after the MPI library by
7  * Michael Fromberger but has been written from scratch with
8  * additional optimizations in place.
9  *
10  * The library is free for all purposes without any express
11  * guarantee it works.
12  *
13  * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
14  */
15 #ifndef BN_H_
16 #define BN_H_
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <limits.h>
23
24 #define NO_LTM_TOOM 1
25
26 #undef MIN
27 #define MIN(x,y) ((x)<(y)?(x):(y))
28 #undef MAX
29 #define MAX(x,y) ((x)>(y)?(x):(y))
30
31 #ifdef __cplusplus
32 extern "C" {
33
34 /* C++ compilers don't like assigning void * to mp_digit * */
35 #define  OPT_CAST  (mp_digit *)
36
37 #else
38
39 /* C on the other hand doesn't care */
40 #define  OPT_CAST
41
42 #endif
43
44 /* some default configurations.
45  *
46  * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
47  * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
48  *
49  * At the very least a mp_digit must be able to hold 7 bits
50  * [any size beyond that is ok provided it doesn't overflow the data type]
51  */
52 #ifdef MP_8BIT
53    typedef unsigned char      mp_digit;
54    typedef unsigned short     mp_word;
55 #elif defined(MP_16BIT)
56    typedef unsigned short     mp_digit;
57    typedef unsigned long      mp_word;
58 #elif defined(MP_64BIT)
59    /* for GCC only on supported platforms */
60 #ifndef CRYPT
61    typedef unsigned long long ulong64;
62    typedef signed long long   long64;
63 #endif
64
65    typedef ulong64            mp_digit;
66    typedef unsigned long      mp_word __attribute__ ((mode(TI)));
67
68    #define DIGIT_BIT          60
69 #else
70    /* this is the default case, 28-bit digits */
71    
72    /* this is to make porting into LibTomCrypt easier :-) */
73 #ifndef CRYPT
74    #if defined(_MSC_VER) || defined(__BORLANDC__) 
75       typedef unsigned __int64   ulong64;
76       typedef signed __int64     long64;
77    #else
78       typedef unsigned long long ulong64;
79       typedef signed long long   long64;
80    #endif
81 #endif
82
83    typedef unsigned long      mp_digit;
84    typedef ulong64            mp_word;
85
86 #ifdef MP_31BIT   
87    #define DIGIT_BIT          31
88 #else
89    #define DIGIT_BIT          28
90 #endif   
91 #endif
92
93 /* otherwise the bits per digit is calculated automatically from the size of a mp_digit */
94 #ifndef DIGIT_BIT
95    #define DIGIT_BIT     ((CHAR_BIT * sizeof(mp_digit) - 1))  /* bits per digit */
96 #endif
97
98
99 #define MP_DIGIT_BIT     DIGIT_BIT
100 #define MP_MASK          ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
101 #define MP_DIGIT_MAX     MP_MASK
102
103 /* equalities */
104 #define MP_LT        -1   /* less than */
105 #define MP_EQ         0   /* equal to */
106 #define MP_GT         1   /* greater than */
107
108 #define MP_ZPOS       0   /* positive integer */
109 #define MP_NEG        1   /* negative */
110
111 #define MP_OKAY       0   /* ok result */
112 #define MP_MEM        -2  /* out of mem */
113 #define MP_VAL        -3  /* invalid input */
114 #define MP_RANGE      MP_VAL
115
116 typedef int           mp_err;
117
118 /* you'll have to tune these... */
119 extern int KARATSUBA_MUL_CUTOFF,
120            KARATSUBA_SQR_CUTOFF,
121            TOOM_MUL_CUTOFF,
122            TOOM_SQR_CUTOFF;
123
124 /* various build options */
125 #define MP_PREC                 64      /* default digits of precision (must be power of two) */
126
127 /* define this to use lower memory usage routines (exptmods mostly) */
128 /* #define MP_LOW_MEM */
129
130 /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
131 #define MP_WARRAY               (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
132
133 typedef struct  {
134     int used, alloc, sign;
135     mp_digit *dp;
136 } mp_int;
137
138 #define USED(m)    ((m)->used)
139 #define DIGIT(m,k) ((m)->dp[k])
140 #define SIGN(m)    ((m)->sign)
141
142 /* ---> init and deinit bignum functions <--- */
143
144 /* init a bignum */
145 int mp_init(mp_int *a);
146
147 /* free a bignum */
148 void mp_clear(mp_int *a);
149
150 /* init a null terminated series of arguments */
151 int mp_init_multi(mp_int *mp, ...);
152
153 /* clear a null terminated series of arguments */
154 void mp_clear_multi(mp_int *mp, ...);
155
156 /* exchange two ints */
157 void mp_exch(mp_int *a, mp_int *b);
158
159 /* shrink ram required for a bignum */
160 int mp_shrink(mp_int *a);
161
162 /* grow an int to a given size */
163 int mp_grow(mp_int *a, int size);
164
165 /* init to a given number of digits */
166 int mp_init_size(mp_int *a, int size);
167
168 /* ---> Basic Manipulations <--- */
169
170 #define mp_iszero(a) (((a)->used == 0) ? 1 : 0)
171 #define mp_iseven(a) (((a)->used == 0 || (((a)->dp[0] & 1) == 0)) ? 1 : 0)
172 #define mp_isodd(a)  (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? 1 : 0)
173
174 /* set to zero */
175 void mp_zero(mp_int *a);
176
177 /* set to a digit */
178 void mp_set(mp_int *a, mp_digit b);
179
180 /* set a 32-bit const */
181 int mp_set_int(mp_int *a, unsigned int b);
182
183 /* copy, b = a */
184 int mp_copy(mp_int *a, mp_int *b);
185
186 /* inits and copies, a = b */
187 int mp_init_copy(mp_int *a, mp_int *b);
188
189 /* trim unused digits */
190 void mp_clamp(mp_int *a);
191
192 /* ---> digit manipulation <--- */
193
194 /* right shift by "b" digits */
195 void mp_rshd(mp_int *a, int b);
196
197 /* left shift by "b" digits */
198 int mp_lshd(mp_int *a, int b);
199
200 /* c = a / 2**b */
201 int mp_div_2d(mp_int *a, int b, mp_int *c, mp_int *d);
202
203 /* b = a/2 */
204 int mp_div_2(mp_int *a, mp_int *b);
205
206 /* c = a * 2**b */
207 int mp_mul_2d(mp_int *a, int b, mp_int *c);
208
209 /* b = a*2 */
210 int mp_mul_2(mp_int *a, mp_int *b);
211
212 /* c = a mod 2**d */
213 int mp_mod_2d(mp_int *a, int b, mp_int *c);
214
215 /* computes a = 2**b */
216 int mp_2expt(mp_int *a, int b);
217
218 /* makes a pseudo-random int of a given size */
219 int mp_rand(mp_int *a, int digits);
220
221 /* ---> binary operations <--- */
222 /* c = a XOR b  */
223 int mp_xor(mp_int *a, mp_int *b, mp_int *c);
224
225 /* c = a OR b */
226 int mp_or(mp_int *a, mp_int *b, mp_int *c);
227
228 /* c = a AND b */
229 int mp_and(mp_int *a, mp_int *b, mp_int *c);
230
231 /* ---> Basic arithmetic <--- */
232
233 /* b = -a */
234 int mp_neg(mp_int *a, mp_int *b);
235
236 /* b = |a| */
237 int mp_abs(mp_int *a, mp_int *b);
238
239 /* compare a to b */
240 int mp_cmp(mp_int *a, mp_int *b);
241
242 /* compare |a| to |b| */
243 int mp_cmp_mag(mp_int *a, mp_int *b);
244
245 /* c = a + b */
246 int mp_add(mp_int *a, mp_int *b, mp_int *c);
247
248 /* c = a - b */
249 int mp_sub(mp_int *a, mp_int *b, mp_int *c);
250
251 /* c = a * b */
252 int mp_mul(mp_int *a, mp_int *b, mp_int *c);
253
254 /* b = a*a  */
255 int mp_sqr(mp_int *a, mp_int *b);
256
257 /* a/b => cb + d == a */
258 int mp_div(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
259
260 /* c = a mod b, 0 <= c < b  */
261 int mp_mod(mp_int *a, mp_int *b, mp_int *c);
262
263 /* ---> single digit functions <--- */
264
265 /* compare against a single digit */
266 int mp_cmp_d(mp_int *a, mp_digit b);
267
268 /* c = a + b */
269 int mp_add_d(mp_int *a, mp_digit b, mp_int *c);
270
271 /* c = a - b */
272 int mp_sub_d(mp_int *a, mp_digit b, mp_int *c);
273
274 /* c = a * b */
275 int mp_mul_d(mp_int *a, mp_digit b, mp_int *c);
276
277 /* a/b => cb + d == a */
278 int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d);
279
280 /* a/3 => 3c + d == a */
281 int mp_div_3(mp_int *a, mp_int *c, mp_digit *d);
282
283 /* c = a**b */
284 int mp_expt_d(mp_int *a, mp_digit b, mp_int *c);
285
286 /* c = a mod b, 0 <= c < b  */
287 int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c);
288
289 /* ---> number theory <--- */
290
291 /* d = a + b (mod c) */
292 int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
293
294 /* d = a - b (mod c) */
295 int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
296
297 /* d = a * b (mod c) */
298 int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
299
300 /* c = a * a (mod b) */
301 int mp_sqrmod(mp_int *a, mp_int *b, mp_int *c);
302
303 /* c = 1/a (mod b) */
304 int mp_invmod(mp_int *a, mp_int *b, mp_int *c);
305
306 /* c = (a, b) */
307 int mp_gcd(mp_int *a, mp_int *b, mp_int *c);
308
309 /* c = [a, b] or (a*b)/(a, b) */
310 int mp_lcm(mp_int *a, mp_int *b, mp_int *c);
311
312 /* finds one of the b'th root of a, such that |c|**b <= |a|
313  *
314  * returns error if a < 0 and b is even
315  */
316 int mp_n_root(mp_int *a, mp_digit b, mp_int *c);
317
318 /* shortcut for square root */
319 #define mp_sqrt(a, b) mp_n_root(a, 2, b)
320
321 /* computes the jacobi c = (a | n) (or Legendre if b is prime)  */
322 int mp_jacobi(mp_int *a, mp_int *n, int *c);
323
324 /* used to setup the Barrett reduction for a given modulus b */
325 int mp_reduce_setup(mp_int *a, mp_int *b);
326
327 /* Barrett Reduction, computes a (mod b) with a precomputed value c
328  *
329  * Assumes that 0 < a <= b*b, note if 0 > a > -(b*b) then you can merely
330  * compute the reduction as -1 * mp_reduce(mp_abs(a)) [pseudo code].
331  */
332 int mp_reduce(mp_int *a, mp_int *b, mp_int *c);
333
334 /* setups the montgomery reduction */
335 int mp_montgomery_setup(mp_int *a, mp_digit *mp);
336
337 /* computes a = B**n mod b without division or multiplication useful for
338  * normalizing numbers in a Montgomery system.
339  */
340 int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
341
342 /* computes x/R == x (mod N) via Montgomery Reduction */
343 int mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
344
345 /* returns 1 if a is a valid DR modulus */
346 int mp_dr_is_modulus(mp_int *a);
347
348 /* sets the value of "d" required for mp_dr_reduce */
349 void mp_dr_setup(mp_int *a, mp_digit *d);
350
351 /* reduces a modulo b using the Diminished Radix method */
352 int mp_dr_reduce(mp_int *a, mp_int *b, mp_digit mp);
353
354 /* returns true if a can be reduced with mp_reduce_2k */
355 int mp_reduce_is_2k(mp_int *a);
356
357 /* determines k value for 2k reduction */
358 int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
359
360 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
361 int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit k);
362
363 /* d = a**b (mod c) */
364 int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
365
366 /* ---> Primes <--- */
367
368 /* number of primes */
369 #ifdef MP_8BIT
370    #define PRIME_SIZE      31
371 #else
372    #define PRIME_SIZE      256
373 #endif
374
375 /* table of first PRIME_SIZE primes */
376 extern const mp_digit __prime_tab[];
377
378 /* result=1 if a is divisible by one of the first PRIME_SIZE primes */
379 int mp_prime_is_divisible(mp_int *a, int *result);
380
381 /* performs one Fermat test of "a" using base "b".
382  * Sets result to 0 if composite or 1 if probable prime
383  */
384 int mp_prime_fermat(mp_int *a, mp_int *b, int *result);
385
386 /* performs one Miller-Rabin test of "a" using base "b".
387  * Sets result to 0 if composite or 1 if probable prime
388  */
389 int mp_prime_miller_rabin(mp_int *a, mp_int *b, int *result);
390
391 /* performs t rounds of Miller-Rabin on "a" using the first
392  * t prime bases.  Also performs an initial sieve of trial
393  * division.  Determines if "a" is prime with probability
394  * of error no more than (1/4)**t.
395  *
396  * Sets result to 1 if probably prime, 0 otherwise
397  */
398 int mp_prime_is_prime(mp_int *a, int t, int *result);
399
400 /* finds the next prime after the number "a" using "t" trials
401  * of Miller-Rabin.
402  */
403 int mp_prime_next_prime(mp_int *a, int t);
404
405
406 /* ---> radix conversion <--- */
407 int mp_count_bits(mp_int *a);
408
409 int mp_unsigned_bin_size(mp_int *a);
410 int mp_read_unsigned_bin(mp_int *a, unsigned char *b, int c);
411 int mp_to_unsigned_bin(mp_int *a, unsigned char *b);
412
413 int mp_signed_bin_size(mp_int *a);
414 int mp_read_signed_bin(mp_int *a, unsigned char *b, int c);
415 int mp_to_signed_bin(mp_int *a, unsigned char *b);
416
417 int mp_read_radix(mp_int *a, char *str, int radix);
418 int mp_toradix(mp_int *a, char *str, int radix);
419 int mp_radix_size(mp_int *a, int radix);
420
421 int mp_fread(mp_int *a, int radix, FILE *stream);
422 int mp_fwrite(mp_int *a, int radix, FILE *stream);
423
424 #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
425 #define mp_raw_size(mp)           mp_signed_bin_size(mp)
426 #define mp_toraw(mp, str)         mp_to_signed_bin((mp), (str))
427 #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
428 #define mp_mag_size(mp)           mp_unsigned_bin_size(mp)
429 #define mp_tomag(mp, str)         mp_to_unsigned_bin((mp), (str))
430
431 #define mp_tobinary(M, S)  mp_toradix((M), (S), 2)
432 #define mp_tooctal(M, S)   mp_toradix((M), (S), 8)
433 #define mp_todecimal(M, S) mp_toradix((M), (S), 10)
434 #define mp_tohex(M, S)     mp_toradix((M), (S), 16)
435
436 /* lowlevel functions, do not call! */
437 int s_mp_add(mp_int *a, mp_int *b, mp_int *c);
438 int s_mp_sub(mp_int *a, mp_int *b, mp_int *c);
439 #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
440 int fast_s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
441 int s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
442 int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
443 int s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
444 int fast_s_mp_sqr(mp_int *a, mp_int *b);
445 int s_mp_sqr(mp_int *a, mp_int *b);
446 int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c);
447 int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c);
448 int mp_karatsuba_sqr(mp_int *a, mp_int *b);
449 int mp_toom_sqr(mp_int *a, mp_int *b);
450 int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c);
451 int fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
452 int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int mode);
453 int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y);
454 void bn_reverse(unsigned char *s, int len);
455
456 #ifdef __cplusplus
457    }
458 #endif
459
460 #endif
461