~ubuntu-branches/ubuntu/utopic/clamav/utopic-security

« back to all changes in this revision

Viewing changes to libclamav/bignum.h

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman
  • Date: 2014-02-01 11:06:17 UTC
  • mfrom: (0.35.37 sid)
  • Revision ID: package-import@ubuntu.com-20140201110617-33h2xxk09dep0ui4
Tags: 0.98.1+dfsg-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - Drop build-dep on electric-fence (in Universe)
  - Add apparmor profiles for clamd and freshclam along with maintainer
    script changes
  - Add autopkgtest

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* LibTomMath, multiple-precision integer library -- Tom St Denis
2
 
 *
3
 
 * LibTomMath is a library that provides multiple-precision
4
 
 * integer arithmetic as well as number theoretic functionality.
5
 
 *
6
 
 * The library was 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@gmail.com, http://math.libtomcrypt.com
14
 
 */
15
 
#ifndef __BIGNUM_H
16
 
#define __BIGNUM_H
17
 
 
18
 
#ifdef HAVE_SYSTEM_TOMMATH
19
 
#include <tommath.h>
20
 
#else
21
 
#include <stdio.h>
22
 
#include <string.h>
23
 
#include <stdlib.h>
24
 
#include <ctype.h>
25
 
#include <limits.h>
26
 
 
27
 
/*
28
 
#define BN_MP_INIT_C
29
 
#define BN_MP_ZERO_C
30
 
#define BN_MP_READ_RADIX_C
31
 
#define BN_MP_RADIX_SMAP_C
32
 
#define BN_MP_SET_INT_C
33
 
#define BN_MP_MUL_2D_C
34
 
#define BN_MP_MUL_D_C
35
 
#define BN_MP_CLAMP_C
36
 
#define BN_MP_ADD_D_C
37
 
#define BN_S_MP_ADD_C
38
 
#define BN_MP_LSHD_C
39
 
#define BN_MP_GROW_C
40
 
#define BN_MP_CMP_MAG_C
41
 
#define BN_MP_COPY_C
42
 
*/
43
 
 
44
 
#define LTM_ALL /* FIXME: tk: limit to the above class */
45
 
#include "bignum_class.h"
46
 
 
47
 
#ifdef __cplusplus
48
 
extern "C" {
49
 
 
50
 
/* C++ compilers don't like assigning void * to mp_digit * */
51
 
#define  OPT_CAST(x)  (x *)
52
 
 
53
 
#else
54
 
 
55
 
/* C on the other hand doesn't care */
56
 
#define  OPT_CAST(x)
57
 
 
58
 
#endif
59
 
 
60
 
#if 0
61
 
Don't enable this, some old gcc's have broken udivti3.
62
 
Also divisions get translated into libcalls, not worth using this mode.
63
 
/* detect 64-bit mode if possible */
64
 
#if defined(__x86_64__) 
65
 
   #if !(defined(MP_64BIT) && defined(MP_16BIT) && defined(MP_8BIT))
66
 
      #define MP_64BIT
67
 
   #endif
68
 
#endif
69
 
#endif
70
 
 
71
 
/* some default configurations.
72
 
 *
73
 
 * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
74
 
 * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
75
 
 *
76
 
 * At the very least a mp_digit must be able to hold 7 bits
77
 
 * [any size beyond that is ok provided it doesn't overflow the data type]
78
 
 */
79
 
#ifdef MP_8BIT
80
 
   typedef unsigned char      mp_digit;
81
 
   typedef unsigned short     mp_word;
82
 
#elif defined(MP_16BIT)
83
 
   typedef unsigned short     mp_digit;
84
 
   typedef unsigned long      mp_word;
85
 
#elif defined(MP_64BIT)
86
 
   /* for GCC only on supported platforms */
87
 
#ifndef CRYPT
88
 
   typedef unsigned long long ulong64;
89
 
   typedef signed long long   long64;
90
 
#endif
91
 
 
92
 
   typedef unsigned long      mp_digit;
93
 
   typedef unsigned long      mp_word __attribute__ ((mode(TI)));
94
 
 
95
 
   #define DIGIT_BIT          60
96
 
#else
97
 
   /* this is the default case, 28-bit digits */
98
 
   
99
 
   /* this is to make porting into LibTomCrypt easier :-) */
100
 
#ifndef CRYPT
101
 
   #if defined(_MSC_VER) || defined(__BORLANDC__) 
102
 
      typedef unsigned __int64   ulong64;
103
 
      typedef signed __int64     long64;
104
 
   #else
105
 
      typedef unsigned long long ulong64;
106
 
      typedef signed long long   long64;
107
 
   #endif
108
 
#endif
109
 
 
110
 
   typedef unsigned long      mp_digit;
111
 
   typedef ulong64            mp_word;
112
 
 
113
 
#ifdef MP_31BIT   
114
 
   /* this is an extension that uses 31-bit digits */
115
 
   #define DIGIT_BIT          31
116
 
#else
117
 
   /* default case is 28-bit digits, defines MP_28BIT as a handy macro to test */
118
 
   #define DIGIT_BIT          28
119
 
   #define MP_28BIT
120
 
#endif   
121
 
#endif
122
 
 
123
 
 
124
 
/* otherwise the bits per digit is calculated automatically from the size of a mp_digit */
125
 
#ifndef DIGIT_BIT
126
 
   #define DIGIT_BIT     ((int)((CHAR_BIT * sizeof(mp_digit) - 1)))  /* bits per digit */
127
 
#endif
128
 
 
129
 
#define MP_DIGIT_BIT     DIGIT_BIT
130
 
#define MP_MASK          ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
131
 
#define MP_DIGIT_MAX     MP_MASK
132
 
 
133
 
/* equalities */
134
 
#define MP_LT        -1   /* less than */
135
 
#define MP_EQ         0   /* equal to */
136
 
#define MP_GT         1   /* greater than */
137
 
 
138
 
#define MP_ZPOS       0   /* positive integer */
139
 
#define MP_NEG        1   /* negative */
140
 
 
141
 
#define MP_OKAY       0   /* ok result */
142
 
#define MP_MEM        -2  /* out of mem */
143
 
#define MP_VAL        -3  /* invalid input */
144
 
#define MP_RANGE      MP_VAL
145
 
 
146
 
#define MP_YES        1   /* yes response */
147
 
#define MP_NO         0   /* no response */
148
 
 
149
 
/* Primality generation flags */
150
 
#define LTM_PRIME_BBS      0x0001 /* BBS style prime */
151
 
#define LTM_PRIME_SAFE     0x0002 /* Safe prime (p-1)/2 == prime */
152
 
#define LTM_PRIME_2MSB_ON  0x0008 /* force 2nd MSB to 1 */
153
 
 
154
 
typedef int           mp_err;
155
 
 
156
 
/* you'll have to tune these... */
157
 
extern int KARATSUBA_MUL_CUTOFF,
158
 
           KARATSUBA_SQR_CUTOFF,
159
 
           TOOM_MUL_CUTOFF,
160
 
           TOOM_SQR_CUTOFF;
161
 
 
162
 
/* define this to use lower memory usage routines (exptmods mostly) */
163
 
/* #define MP_LOW_MEM */
164
 
 
165
 
/* default precision */
166
 
#ifndef MP_PREC
167
 
   #ifndef MP_LOW_MEM
168
 
      #define MP_PREC                 32     /* default digits of precision */
169
 
   #else
170
 
      #define MP_PREC                 8      /* default digits of precision */
171
 
   #endif   
172
 
#endif
173
 
 
174
 
/* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
175
 
#define MP_WARRAY               (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
176
 
 
177
 
/* the infamous mp_int structure */
178
 
typedef struct  {
179
 
    int used, alloc, sign;
180
 
    mp_digit *dp;
181
 
} mp_int;
182
 
 
183
 
/* callback for mp_prime_random, should fill dst with random bytes and return how many read [upto len] */
184
 
typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
185
 
 
186
 
 
187
 
#define USED(m)    ((m)->used)
188
 
#define DIGIT(m,k) ((m)->dp[(k)])
189
 
#define SIGN(m)    ((m)->sign)
190
 
 
191
 
/* error code to char* string */
192
 
const char *mp_error_to_string(int code);
193
 
 
194
 
/* ---> init and deinit bignum functions <--- */
195
 
/* init a bignum */
196
 
int mp_init(mp_int *a);
197
 
 
198
 
/* free a bignum */
199
 
void mp_clear(mp_int *a);
200
 
 
201
 
/* init a null terminated series of arguments */
202
 
int mp_init_multi(mp_int *mp, ...);
203
 
 
204
 
/* clear a null terminated series of arguments */
205
 
void mp_clear_multi(mp_int *mp, ...);
206
 
 
207
 
/* exchange two ints */
208
 
void mp_exch(mp_int *a, mp_int *b);
209
 
 
210
 
/* shrink ram required for a bignum */
211
 
int mp_shrink(mp_int *a);
212
 
 
213
 
/* grow an int to a given size */
214
 
int mp_grow(mp_int *a, int size);
215
 
 
216
 
/* init to a given number of digits */
217
 
int mp_init_size(mp_int *a, int size);
218
 
 
219
 
/* ---> Basic Manipulations <--- */
220
 
#define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
221
 
#define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
222
 
#define mp_isodd(a)  (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
223
 
 
224
 
/* set to zero */
225
 
void mp_zero(mp_int *a);
226
 
 
227
 
/* set to a digit */
228
 
void mp_set(mp_int *a, mp_digit b);
229
 
 
230
 
/* set a 32-bit const */
231
 
int mp_set_int(mp_int *a, unsigned long b);
232
 
 
233
 
/* get a 32-bit value */
234
 
unsigned long mp_get_int(mp_int * a);
235
 
 
236
 
/* initialize and set a digit */
237
 
int mp_init_set (mp_int * a, mp_digit b);
238
 
 
239
 
/* initialize and set 32-bit value */
240
 
int mp_init_set_int (mp_int * a, unsigned long b);
241
 
 
242
 
/* copy, b = a */
243
 
int mp_copy(mp_int *a, mp_int *b);
244
 
 
245
 
/* inits and copies, a = b */
246
 
int mp_init_copy(mp_int *a, mp_int *b);
247
 
 
248
 
/* trim unused digits */
249
 
void mp_clamp(mp_int *a);
250
 
 
251
 
/* ---> digit manipulation <--- */
252
 
 
253
 
/* right shift by "b" digits */
254
 
void mp_rshd(mp_int *a, int b);
255
 
 
256
 
/* left shift by "b" digits */
257
 
int mp_lshd(mp_int *a, int b);
258
 
 
259
 
/* c = a / 2**b */
260
 
int mp_div_2d(mp_int *a, int b, mp_int *c, mp_int *d);
261
 
 
262
 
/* b = a/2 */
263
 
int mp_div_2(mp_int *a, mp_int *b);
264
 
 
265
 
/* c = a * 2**b */
266
 
int mp_mul_2d(mp_int *a, int b, mp_int *c);
267
 
 
268
 
/* b = a*2 */
269
 
int mp_mul_2(mp_int *a, mp_int *b);
270
 
 
271
 
/* c = a mod 2**d */
272
 
int mp_mod_2d(mp_int *a, int b, mp_int *c);
273
 
 
274
 
/* computes a = 2**b */
275
 
int mp_2expt(mp_int *a, int b);
276
 
 
277
 
/* Counts the number of lsbs which are zero before the first zero bit */
278
 
int mp_cnt_lsb(mp_int *a);
279
 
 
280
 
/* I Love Earth! */
281
 
 
282
 
/* makes a pseudo-random int of a given size */
283
 
int mp_rand(mp_int *a, int digits);
284
 
 
285
 
/* ---> binary operations <--- */
286
 
/* c = a XOR b  */
287
 
int mp_xor(mp_int *a, mp_int *b, mp_int *c);
288
 
 
289
 
/* c = a OR b */
290
 
int mp_or(mp_int *a, mp_int *b, mp_int *c);
291
 
 
292
 
/* c = a AND b */
293
 
int mp_and(mp_int *a, mp_int *b, mp_int *c);
294
 
 
295
 
/* ---> Basic arithmetic <--- */
296
 
 
297
 
/* b = -a */
298
 
int mp_neg(mp_int *a, mp_int *b);
299
 
 
300
 
/* b = |a| */
301
 
int mp_abs(mp_int *a, mp_int *b);
302
 
 
303
 
/* compare a to b */
304
 
int mp_cmp(mp_int *a, mp_int *b);
305
 
 
306
 
/* compare |a| to |b| */
307
 
int mp_cmp_mag(mp_int *a, mp_int *b);
308
 
 
309
 
/* c = a + b */
310
 
int mp_add(mp_int *a, mp_int *b, mp_int *c);
311
 
 
312
 
/* c = a - b */
313
 
int mp_sub(mp_int *a, mp_int *b, mp_int *c);
314
 
 
315
 
/* c = a * b */
316
 
int mp_mul(mp_int *a, mp_int *b, mp_int *c);
317
 
 
318
 
/* b = a*a  */
319
 
int mp_sqr(mp_int *a, mp_int *b);
320
 
 
321
 
/* a/b => cb + d == a */
322
 
int mp_div(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
323
 
 
324
 
/* c = a mod b, 0 <= c < b  */
325
 
int mp_mod(mp_int *a, mp_int *b, mp_int *c);
326
 
 
327
 
/* ---> single digit functions <--- */
328
 
 
329
 
/* compare against a single digit */
330
 
int mp_cmp_d(mp_int *a, mp_digit b);
331
 
 
332
 
/* c = a + b */
333
 
int mp_add_d(mp_int *a, mp_digit b, mp_int *c);
334
 
 
335
 
/* c = a - b */
336
 
int mp_sub_d(mp_int *a, mp_digit b, mp_int *c);
337
 
 
338
 
/* c = a * b */
339
 
int mp_mul_d(mp_int *a, mp_digit b, mp_int *c);
340
 
 
341
 
/* a/b => cb + d == a */
342
 
int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d);
343
 
 
344
 
/* a/3 => 3c + d == a */
345
 
int mp_div_3(mp_int *a, mp_int *c, mp_digit *d);
346
 
 
347
 
/* c = a**b */
348
 
int mp_expt_d(mp_int *a, mp_digit b, mp_int *c);
349
 
 
350
 
/* c = a mod b, 0 <= c < b  */
351
 
int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c);
352
 
 
353
 
/* ---> number theory <--- */
354
 
 
355
 
/* d = a + b (mod c) */
356
 
int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
357
 
 
358
 
/* d = a - b (mod c) */
359
 
int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
360
 
 
361
 
/* d = a * b (mod c) */
362
 
int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
363
 
 
364
 
/* c = a * a (mod b) */
365
 
int mp_sqrmod(mp_int *a, mp_int *b, mp_int *c);
366
 
 
367
 
/* c = 1/a (mod b) */
368
 
int mp_invmod(mp_int *a, mp_int *b, mp_int *c);
369
 
 
370
 
/* c = (a, b) */
371
 
int mp_gcd(mp_int *a, mp_int *b, mp_int *c);
372
 
 
373
 
/* produces value such that U1*a + U2*b = U3 */
374
 
int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3);
375
 
 
376
 
/* c = [a, b] or (a*b)/(a, b) */
377
 
int mp_lcm(mp_int *a, mp_int *b, mp_int *c);
378
 
 
379
 
/* finds one of the b'th root of a, such that |c|**b <= |a|
380
 
 *
381
 
 * returns error if a < 0 and b is even
382
 
 */
383
 
int mp_n_root(mp_int *a, mp_digit b, mp_int *c);
384
 
 
385
 
/* special sqrt algo */
386
 
int mp_sqrt(mp_int *arg, mp_int *ret);
387
 
 
388
 
/* is number a square? */
389
 
int mp_is_square(mp_int *arg, int *ret);
390
 
 
391
 
/* computes the jacobi c = (a | n) (or Legendre if b is prime)  */
392
 
int mp_jacobi(mp_int *a, mp_int *n, int *c);
393
 
 
394
 
/* used to setup the Barrett reduction for a given modulus b */
395
 
int mp_reduce_setup(mp_int *a, mp_int *b);
396
 
 
397
 
/* Barrett Reduction, computes a (mod b) with a precomputed value c
398
 
 *
399
 
 * Assumes that 0 < a <= b*b, note if 0 > a > -(b*b) then you can merely
400
 
 * compute the reduction as -1 * mp_reduce(mp_abs(a)) [pseudo code].
401
 
 */
402
 
int mp_reduce(mp_int *a, mp_int *b, mp_int *c);
403
 
 
404
 
/* setups the montgomery reduction */
405
 
int mp_montgomery_setup(mp_int *a, mp_digit *mp);
406
 
 
407
 
/* computes a = B**n mod b without division or multiplication useful for
408
 
 * normalizing numbers in a Montgomery system.
409
 
 */
410
 
int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
411
 
 
412
 
/* computes x/R == x (mod N) via Montgomery Reduction */
413
 
int mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
414
 
 
415
 
/* returns 1 if a is a valid DR modulus */
416
 
int mp_dr_is_modulus(mp_int *a);
417
 
 
418
 
/* sets the value of "d" required for mp_dr_reduce */
419
 
void mp_dr_setup(mp_int *a, mp_digit *d);
420
 
 
421
 
/* reduces a modulo b using the Diminished Radix method */
422
 
int mp_dr_reduce(mp_int *a, mp_int *b, mp_digit mp);
423
 
 
424
 
/* returns true if a can be reduced with mp_reduce_2k */
425
 
int mp_reduce_is_2k(mp_int *a);
426
 
 
427
 
/* determines k value for 2k reduction */
428
 
int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
429
 
 
430
 
/* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
431
 
int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d);
432
 
 
433
 
/* returns true if a can be reduced with mp_reduce_2k_l */
434
 
int mp_reduce_is_2k_l(mp_int *a);
435
 
 
436
 
/* determines k value for 2k reduction */
437
 
int mp_reduce_2k_setup_l(mp_int *a, mp_int *d);
438
 
 
439
 
/* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
440
 
int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d);
441
 
 
442
 
/* d = a**b (mod c) */
443
 
int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
444
 
 
445
 
/* ---> Primes <--- */
446
 
 
447
 
/* number of primes */
448
 
#ifdef MP_8BIT
449
 
   #define PRIME_SIZE      31
450
 
#else
451
 
   #define PRIME_SIZE      256
452
 
#endif
453
 
 
454
 
/* table of first PRIME_SIZE primes */
455
 
extern const mp_digit ltm_prime_tab[];
456
 
 
457
 
/* result=1 if a is divisible by one of the first PRIME_SIZE primes */
458
 
int mp_prime_is_divisible(mp_int *a, int *result);
459
 
 
460
 
/* performs one Fermat test of "a" using base "b".
461
 
 * Sets result to 0 if composite or 1 if probable prime
462
 
 */
463
 
int mp_prime_fermat(mp_int *a, mp_int *b, int *result);
464
 
 
465
 
/* performs one Miller-Rabin test of "a" using base "b".
466
 
 * Sets result to 0 if composite or 1 if probable prime
467
 
 */
468
 
int mp_prime_miller_rabin(mp_int *a, mp_int *b, int *result);
469
 
 
470
 
/* This gives [for a given bit size] the number of trials required
471
 
 * such that Miller-Rabin gives a prob of failure lower than 2^-96 
472
 
 */
473
 
int mp_prime_rabin_miller_trials(int size);
474
 
 
475
 
/* performs t rounds of Miller-Rabin on "a" using the first
476
 
 * t prime bases.  Also performs an initial sieve of trial
477
 
 * division.  Determines if "a" is prime with probability
478
 
 * of error no more than (1/4)**t.
479
 
 *
480
 
 * Sets result to 1 if probably prime, 0 otherwise
481
 
 */
482
 
int mp_prime_is_prime(mp_int *a, int t, int *result);
483
 
 
484
 
/* finds the next prime after the number "a" using "t" trials
485
 
 * of Miller-Rabin.
486
 
 *
487
 
 * bbs_style = 1 means the prime must be congruent to 3 mod 4
488
 
 */
489
 
int mp_prime_next_prime(mp_int *a, int t, int bbs_style);
490
 
 
491
 
/* makes a truly random prime of a given size (bytes),
492
 
 * call with bbs = 1 if you want it to be congruent to 3 mod 4 
493
 
 *
494
 
 * You have to supply a callback which fills in a buffer with random bytes.  "dat" is a parameter you can
495
 
 * have passed to the callback (e.g. a state or something).  This function doesn't use "dat" itself
496
 
 * so it can be NULL
497
 
 *
498
 
 * The prime generated will be larger than 2^(8*size).
499
 
 */
500
 
#define mp_prime_random(a, t, size, bbs, cb, dat) mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?LTM_PRIME_BBS:0, cb, dat)
501
 
 
502
 
/* makes a truly random prime of a given size (bits),
503
 
 *
504
 
 * Flags are as follows:
505
 
 * 
506
 
 *   LTM_PRIME_BBS      - make prime congruent to 3 mod 4
507
 
 *   LTM_PRIME_SAFE     - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS)
508
 
 *   LTM_PRIME_2MSB_OFF - make the 2nd highest bit zero
509
 
 *   LTM_PRIME_2MSB_ON  - make the 2nd highest bit one
510
 
 *
511
 
 * You have to supply a callback which fills in a buffer with random bytes.  "dat" is a parameter you can
512
 
 * have passed to the callback (e.g. a state or something).  This function doesn't use "dat" itself
513
 
 * so it can be NULL
514
 
 *
515
 
 */
516
 
int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat);
517
 
 
518
 
/* ---> radix conversion <--- */
519
 
int mp_count_bits(mp_int *a);
520
 
 
521
 
int mp_unsigned_bin_size(mp_int *a);
522
 
int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c);
523
 
int mp_to_unsigned_bin(mp_int *a, unsigned char *b);
524
 
int mp_to_unsigned_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen);
525
 
 
526
 
int mp_signed_bin_size(mp_int *a);
527
 
int mp_read_signed_bin(mp_int *a, const unsigned char *b, int c);
528
 
int mp_to_signed_bin(mp_int *a,  unsigned char *b);
529
 
int mp_to_signed_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen);
530
 
 
531
 
int mp_read_radix(mp_int *a, const char *str, int radix);
532
 
int mp_toradix(mp_int *a, char *str, int radix);
533
 
int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen);
534
 
int mp_radix_size(mp_int *a, int radix, int *size);
535
 
 
536
 
int mp_fread(mp_int *a, int radix, FILE *stream);
537
 
int mp_fwrite(mp_int *a, int radix, FILE *stream);
538
 
 
539
 
#define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
540
 
#define mp_raw_size(mp)           mp_signed_bin_size(mp)
541
 
#define mp_toraw(mp, str)         mp_to_signed_bin((mp), (str))
542
 
#define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
543
 
#define mp_mag_size(mp)           mp_unsigned_bin_size(mp)
544
 
#define mp_tomag(mp, str)         mp_to_unsigned_bin((mp), (str))
545
 
 
546
 
#define mp_tobinary(M, S)  mp_toradix((M), (S), 2)
547
 
#define mp_tooctal(M, S)   mp_toradix((M), (S), 8)
548
 
#define mp_todecimal(M, S) mp_toradix((M), (S), 10)
549
 
#define mp_tohex(M, S)     mp_toradix((M), (S), 16)
550
 
 
551
 
/* lowlevel functions, do not call! */
552
 
int s_mp_add(mp_int *a, mp_int *b, mp_int *c);
553
 
int s_mp_sub(mp_int *a, mp_int *b, mp_int *c);
554
 
#define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
555
 
int fast_s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
556
 
int s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
557
 
int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
558
 
int s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
559
 
int fast_s_mp_sqr(mp_int *a, mp_int *b);
560
 
int s_mp_sqr(mp_int *a, mp_int *b);
561
 
int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c);
562
 
int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c);
563
 
int mp_karatsuba_sqr(mp_int *a, mp_int *b);
564
 
int mp_toom_sqr(mp_int *a, mp_int *b);
565
 
int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c);
566
 
int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c);
567
 
int fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
568
 
int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int mode);
569
 
int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int mode);
570
 
void bn_reverse(unsigned char *s, int len);
571
 
 
572
 
extern const char *mp_s_rmap;
573
 
 
574
 
#ifdef __cplusplus
575
 
   }
576
 
#endif
577
 
 
578
 
#endif /* HAVE_SYSTEM_TOMMATH */
579
 
 
580
 
#endif
581
 
 
582
 
 
583
 
/* $Source: /cvs/libtom/libtommath/tommath.h,v $ */
584
 
/* $Revision: 1.8 $ */
585
 
/* $Date: 2006/03/31 14:18:44 $ */
 
1
#ifndef BIGNUM_H_
 
2
#define BIGNUM_H_
 
3
 
 
4
#define TFM_CHECK
 
5
 
 
6
#include "bignum_fast.h"
 
7
typedef fp_int mp_int;
 
8
#define mp_cmp fp_cmp
 
9
#define mp_toradix_n(a,b,c,d) fp_toradix_n(a,b,c,d)
 
10
#define mp_init(a) (fp_init(a), 0)
 
11
#define mp_add fp_add
 
12
 
 
13
#define mp_init_multi(a,b,c,d) (mp_init(a), mp_init(b), mp_init(c), 0)
 
14
 
 
15
#define mp_read_unsigned_bin(a,b,c) (fp_read_unsigned_bin(a, b, c), 0)
 
16
 
 
17
#define mp_div fp_div
 
18
#define mp_clear_multi(...)
 
19
#define mp_copy(a,b) (fp_copy(a,b), 0)
 
20
#define mp_unsigned_bin_size fp_unsigned_bin_size
 
21
#define mp_to_unsigned_bin(a,b) (fp_to_unsigned_bin(a,b), 0)
 
22
#define mp_read_radix fp_read_radix
 
23
#define mp_exptmod fp_exptmod
 
24
#define mp_get_int(a) ((a)->used > 0 ? (a)->dp[0] : 0)
 
25
#define mp_set_int(a, b) fp_set(a, b)
 
26
#define mp_mul_2d fp_mul_2d
 
27
#define mp_clear(x)
 
28
#endif