]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/CyaSSL/cyassl/ctaocrypt/tfm.h
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS-Plus / CyaSSL / cyassl / ctaocrypt / tfm.h
1 /* tfm.h
2  *
3  * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20  */
21
22
23 /*
24  * Based on public domain TomsFastMath 0.10 by Tom St Denis, tomstdenis@iahu.ca,
25  * http://math.libtomcrypt.com
26  */
27
28
29 /**
30  *  Edited by Moisés Guimarães (moises.guimaraes@phoebus.com.br)
31  *  to fit CyaSSL's needs.
32  */
33
34
35 #ifndef CTAO_CRYPT_TFM_H
36 #define CTAO_CRYPT_TFM_H
37
38 #include <cyassl/ctaocrypt/types.h>
39 #ifndef CHAR_BIT
40     #include <limits.h>
41 #endif
42
43
44 #ifdef __cplusplus
45     extern "C" {
46 #endif
47
48 #ifndef MIN
49    #define MIN(x,y) ((x)<(y)?(x):(y))
50 #endif
51
52 #ifndef MAX
53    #define MAX(x,y) ((x)>(y)?(x):(y))
54 #endif
55
56
57 /* autodetect x86-64 and make sure we are using 64-bit digits with x86-64 asm */
58 #if defined(__x86_64__)
59    #if defined(TFM_X86) || defined(TFM_SSE2) || defined(TFM_ARM) 
60        #error x86-64 detected, x86-32/SSE2/ARM optimizations are not valid!
61    #endif
62    #if !defined(TFM_X86_64) && !defined(TFM_NO_ASM)
63       #define TFM_X86_64
64    #endif
65 #endif
66 #if defined(TFM_X86_64)
67     #if !defined(FP_64BIT)
68        #define FP_64BIT
69     #endif
70 #endif
71 /* use 64-bit digit even if not using asm on x86_64 */
72 #if defined(__x86_64__) && !defined(FP_64BIT)
73     #define FP_64BIT
74 #endif
75
76 /* try to detect x86-32 */
77 #if defined(__i386__) && !defined(TFM_SSE2)
78    #if defined(TFM_X86_64) || defined(TFM_ARM) 
79        #error x86-32 detected, x86-64/ARM optimizations are not valid!
80    #endif
81    #if !defined(TFM_X86) && !defined(TFM_NO_ASM)
82       #define TFM_X86
83    #endif
84 #endif
85
86 /* make sure we're 32-bit for x86-32/sse/arm/ppc32 */
87 #if (defined(TFM_X86) || defined(TFM_SSE2) || defined(TFM_ARM) || defined(TFM_PPC32)) && defined(FP_64BIT)
88    #warning x86-32, SSE2 and ARM, PPC32 optimizations require 32-bit digits (undefining)
89    #undef FP_64BIT
90 #endif
91
92 /* multi asms? */
93 #ifdef TFM_X86
94    #define TFM_ASM
95 #endif
96 #ifdef TFM_X86_64
97    #ifdef TFM_ASM
98       #error TFM_ASM already defined!
99    #endif
100    #define TFM_ASM
101 #endif
102 #ifdef TFM_SSE2
103    #ifdef TFM_ASM
104       #error TFM_ASM already defined!
105    #endif
106    #define TFM_ASM
107 #endif
108 #ifdef TFM_ARM
109    #ifdef TFM_ASM
110       #error TFM_ASM already defined!
111    #endif
112    #define TFM_ASM
113 #endif
114 #ifdef TFM_PPC32
115    #ifdef TFM_ASM
116       #error TFM_ASM already defined!
117    #endif
118    #define TFM_ASM
119 #endif
120 #ifdef TFM_PPC64
121    #ifdef TFM_ASM
122       #error TFM_ASM already defined!
123    #endif
124    #define TFM_ASM
125 #endif
126 #ifdef TFM_AVR32
127    #ifdef TFM_ASM
128       #error TFM_ASM already defined!
129    #endif
130    #define TFM_ASM
131 #endif
132
133 /* we want no asm? */
134 #ifdef TFM_NO_ASM
135    #undef TFM_X86
136    #undef TFM_X86_64
137    #undef TFM_SSE2
138    #undef TFM_ARM
139    #undef TFM_PPC32
140    #undef TFM_PPC64
141    #undef TFM_AVR32
142    #undef TFM_ASM   
143 #endif
144
145 /* ECC helpers */
146 #ifdef TFM_ECC192
147    #ifdef FP_64BIT
148        #define TFM_MUL3
149        #define TFM_SQR3
150    #else
151        #define TFM_MUL6
152        #define TFM_SQR6
153    #endif
154 #endif
155
156 #ifdef TFM_ECC224
157    #ifdef FP_64BIT
158        #define TFM_MUL4
159        #define TFM_SQR4
160    #else
161        #define TFM_MUL7
162        #define TFM_SQR7
163    #endif
164 #endif
165
166 #ifdef TFM_ECC256
167    #ifdef FP_64BIT
168        #define TFM_MUL4
169        #define TFM_SQR4
170    #else
171        #define TFM_MUL8
172        #define TFM_SQR8
173    #endif
174 #endif
175
176 #ifdef TFM_ECC384
177    #ifdef FP_64BIT
178        #define TFM_MUL6
179        #define TFM_SQR6
180    #else
181        #define TFM_MUL12
182        #define TFM_SQR12
183    #endif
184 #endif
185
186 #ifdef TFM_ECC521
187    #ifdef FP_64BIT
188        #define TFM_MUL9
189        #define TFM_SQR9
190    #else
191        #define TFM_MUL17
192        #define TFM_SQR17
193    #endif
194 #endif
195
196
197 /* some default configurations.
198  */
199 #if defined(FP_64BIT)
200    /* for GCC only on supported platforms */
201 #ifndef CRYPT
202    typedef unsigned long ulong64;
203 #endif
204    typedef ulong64            fp_digit;
205    typedef unsigned long      fp_word __attribute__ ((mode(TI)));
206 #else
207    /* this is to make porting into LibTomCrypt easier :-) */
208 #ifndef CRYPT
209    #if defined(_MSC_VER) || defined(__BORLANDC__) 
210       typedef unsigned __int64   ulong64;
211       typedef signed __int64     long64;
212    #else
213       typedef unsigned long long ulong64;
214       typedef signed long long   long64;
215    #endif
216 #endif
217    typedef unsigned int       fp_digit;
218    typedef ulong64            fp_word;
219 #endif
220
221 /* # of digits this is */
222 #define DIGIT_BIT  (int)((CHAR_BIT) * sizeof(fp_digit))
223
224 /* Max size of any number in bits.  Basically the largest size you will be
225  * multiplying should be half [or smaller] of FP_MAX_SIZE-four_digit
226  *
227  * It defaults to 4096-bits [allowing multiplications upto 2048x2048 bits ]
228  */
229 #ifndef FP_MAX_BITS
230     #define FP_MAX_BITS           4096
231 #endif
232 #define FP_MAX_SIZE           (FP_MAX_BITS+(8*DIGIT_BIT))
233
234 /* will this lib work? */
235 #if (CHAR_BIT & 7)
236    #error CHAR_BIT must be a multiple of eight.
237 #endif
238 #if FP_MAX_BITS % CHAR_BIT
239    #error FP_MAX_BITS must be a multiple of CHAR_BIT
240 #endif
241
242 #define FP_MASK    (fp_digit)(-1)
243 #define FP_SIZE    (FP_MAX_SIZE/DIGIT_BIT)
244
245 /* signs */
246 #define FP_ZPOS     0
247 #define FP_NEG      1
248
249 /* return codes */
250 #define FP_OKAY     0
251 #define FP_VAL      1
252 #define FP_MEM      2
253
254 /* equalities */
255 #define FP_LT        -1   /* less than */
256 #define FP_EQ         0   /* equal to */
257 #define FP_GT         1   /* greater than */
258
259 /* replies */
260 #define FP_YES        1   /* yes response */
261 #define FP_NO         0   /* no response */
262
263 /* a FP type */
264 typedef struct {
265     fp_digit dp[FP_SIZE];
266     int      used, 
267              sign;
268 } fp_int;
269
270 /* externally define this symbol to ignore the default settings, useful for changing the build from the make process */
271 #ifndef TFM_ALREADY_SET
272
273 /* do we want the large set of small multiplications ? 
274    Enable these if you are going to be doing a lot of small (<= 16 digit) multiplications say in ECC
275    Or if you're on a 64-bit machine doing RSA as a 1024-bit integer == 16 digits ;-)
276  */
277 /* need to refactor the function */
278 /*#define TFM_SMALL_SET */
279
280 /* do we want huge code 
281    Enable these if you are doing 20, 24, 28, 32, 48, 64 digit multiplications (useful for RSA)
282    Less important on 64-bit machines as 32 digits == 2048 bits
283  */
284 #if 0
285 #define TFM_MUL3
286 #define TFM_MUL4
287 #define TFM_MUL6
288 #define TFM_MUL7
289 #define TFM_MUL8
290 #define TFM_MUL9
291 #define TFM_MUL12
292 #define TFM_MUL17
293 #endif
294 #ifdef TFM_SMALL_SET
295 #define TFM_MUL20
296 #define TFM_MUL24
297 #define TFM_MUL28
298 #define TFM_MUL32
299 #if (FP_MAX_BITS >= 6144) && defined(FP_64BIT)
300     #define TFM_MUL48
301 #endif
302 #if (FP_MAX_BITS >= 8192) && defined(FP_64BIT)
303     #define TFM_MUL64
304 #endif
305 #endif
306
307 #if 0
308 #define TFM_SQR3
309 #define TFM_SQR4
310 #define TFM_SQR6
311 #define TFM_SQR7
312 #define TFM_SQR8
313 #define TFM_SQR9
314 #define TFM_SQR12
315 #define TFM_SQR17
316 #endif
317 #ifdef TFM_SMALL_SET
318 #define TFM_SQR20
319 #define TFM_SQR24
320 #define TFM_SQR28
321 #define TFM_SQR32
322 #define TFM_SQR48
323 #define TFM_SQR64
324 #endif
325
326 /* do we want some overflow checks
327    Not required if you make sure your numbers are within range (e.g. by default a modulus for fp_exptmod() can only be upto 2048 bits long)
328  */
329 /* #define TFM_CHECK */
330
331 /* Is the target a P4 Prescott
332  */
333 /* #define TFM_PRESCOTT */
334
335 /* Do we want timing resistant fp_exptmod() ?
336  * This makes it slower but also timing invariant with respect to the exponent 
337  */
338 /* #define TFM_TIMING_RESISTANT */
339
340 #endif /* TFM_ALREADY_SET */
341
342 /* functions */
343
344 /* returns a TFM ident string useful for debugging... */
345 /*const char *fp_ident(void);*/
346
347 /* initialize [or zero] an fp int */
348 #define fp_init(a)  (void)XMEMSET((a), 0, sizeof(fp_int))
349 #define fp_zero(a)  fp_init(a)
350
351 /* zero/even/odd ? */
352 #define fp_iszero(a) (((a)->used == 0) ? FP_YES : FP_NO)
353 #define fp_iseven(a) (((a)->used >= 0 && (((a)->dp[0] & 1) == 0)) ? FP_YES : FP_NO)
354 #define fp_isodd(a)  (((a)->used > 0  && (((a)->dp[0] & 1) == 1)) ? FP_YES : FP_NO)
355
356 /* set to a small digit */
357 void fp_set(fp_int *a, fp_digit b);
358
359 /* copy from a to b */
360 #define fp_copy(a, b)  (void)(((a) != (b)) ? (XMEMCPY((b), (a), sizeof(fp_int))) : (void)0)
361 #define fp_init_copy(a, b) fp_copy(b, a)
362
363 /* clamp digits */
364 #define fp_clamp(a)   { while ((a)->used && (a)->dp[(a)->used-1] == 0) --((a)->used); (a)->sign = (a)->used ? (a)->sign : FP_ZPOS; }
365
366 /* negate and absolute */
367 #define fp_neg(a, b)  { fp_copy(a, b); (b)->sign ^= 1; fp_clamp(b); }
368 #define fp_abs(a, b)  { fp_copy(a, b); (b)->sign  = 0; }
369
370 /* right shift x digits */
371 void fp_rshd(fp_int *a, int x);
372
373 /* left shift x digits */
374 void fp_lshd(fp_int *a, int x);
375
376 /* signed comparison */
377 int fp_cmp(fp_int *a, fp_int *b);
378
379 /* unsigned comparison */
380 int fp_cmp_mag(fp_int *a, fp_int *b);
381
382 /* power of 2 operations */
383 void fp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d);
384 void fp_mod_2d(fp_int *a, int b, fp_int *c);
385 void fp_mul_2d(fp_int *a, int b, fp_int *c);
386 void fp_2expt (fp_int *a, int b);
387 void fp_mul_2(fp_int *a, fp_int *c);
388 void fp_div_2(fp_int *a, fp_int *c);
389
390 /* Counts the number of lsbs which are zero before the first zero bit */
391 /*int fp_cnt_lsb(fp_int *a);*/
392
393 /* c = a + b */
394 void fp_add(fp_int *a, fp_int *b, fp_int *c);
395
396 /* c = a - b */
397 void fp_sub(fp_int *a, fp_int *b, fp_int *c);
398
399 /* c = a * b */
400 void fp_mul(fp_int *a, fp_int *b, fp_int *c);
401
402 /* b = a*a  */
403 void fp_sqr(fp_int *a, fp_int *b);
404
405 /* a/b => cb + d == a */
406 int fp_div(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
407
408 /* c = a mod b, 0 <= c < b  */
409 int fp_mod(fp_int *a, fp_int *b, fp_int *c);
410
411 /* compare against a single digit */
412 int fp_cmp_d(fp_int *a, fp_digit b);
413
414 /* c = a + b */
415 void fp_add_d(fp_int *a, fp_digit b, fp_int *c);
416
417 /* c = a - b */
418 void fp_sub_d(fp_int *a, fp_digit b, fp_int *c);
419
420 /* c = a * b */
421 void fp_mul_d(fp_int *a, fp_digit b, fp_int *c);
422
423 /* a/b => cb + d == a */
424 /*int fp_div_d(fp_int *a, fp_digit b, fp_int *c, fp_digit *d);*/
425
426 /* c = a mod b, 0 <= c < b  */
427 /*int fp_mod_d(fp_int *a, fp_digit b, fp_digit *c);*/
428
429 /* ---> number theory <--- */
430 /* d = a + b (mod c) */
431 /*int fp_addmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);*/
432
433 /* d = a - b (mod c) */
434 /*int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);*/
435
436 /* d = a * b (mod c) */
437 int fp_mulmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
438
439 /* c = a * a (mod b) */
440 int fp_sqrmod(fp_int *a, fp_int *b, fp_int *c);
441
442 /* c = 1/a (mod b) */
443 int fp_invmod(fp_int *a, fp_int *b, fp_int *c);
444
445 /* c = (a, b) */
446 /*void fp_gcd(fp_int *a, fp_int *b, fp_int *c);*/
447
448 /* c = [a, b] */
449 /*void fp_lcm(fp_int *a, fp_int *b, fp_int *c);*/
450
451 /* setups the montgomery reduction */
452 int fp_montgomery_setup(fp_int *a, fp_digit *mp);
453
454 /* computes a = B**n mod b without division or multiplication useful for
455  * normalizing numbers in a Montgomery system.
456  */
457 void fp_montgomery_calc_normalization(fp_int *a, fp_int *b);
458
459 /* computes x/R == x (mod N) via Montgomery Reduction */
460 void fp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp);
461
462 /* d = a**b (mod c) */
463 int fp_exptmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
464
465 /* primality stuff */
466
467 /* perform a Miller-Rabin test of a to the base b and store result in "result" */
468 /*void fp_prime_miller_rabin (fp_int * a, fp_int * b, int *result);*/
469
470 /* 256 trial divisions + 8 Miller-Rabins, returns FP_YES if probable prime  */
471 /*int fp_isprime(fp_int *a);*/
472
473 /* Primality generation flags */
474 /*#define TFM_PRIME_BBS      0x0001 */ /* BBS style prime */
475 /*#define TFM_PRIME_SAFE     0x0002 */ /* Safe prime (p-1)/2 == prime */
476 /*#define TFM_PRIME_2MSB_OFF 0x0004 */ /* force 2nd MSB to 0 */
477 /*#define TFM_PRIME_2MSB_ON  0x0008 */ /* force 2nd MSB to 1 */
478
479 /* callback for fp_prime_random, should fill dst with random bytes and return how many read [upto len] */
480 /*typedef int tfm_prime_callback(unsigned char *dst, int len, void *dat);*/
481
482 /*#define fp_prime_random(a, t, size, bbs, cb, dat) fp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?TFM_PRIME_BBS:0, cb, dat)*/
483
484 /*int fp_prime_random_ex(fp_int *a, int t, int size, int flags, tfm_prime_callback cb, void *dat);*/
485
486 /* radix conersions */
487 int fp_count_bits(fp_int *a);
488
489 int fp_unsigned_bin_size(fp_int *a);
490 void fp_read_unsigned_bin(fp_int *a, unsigned char *b, int c);
491 void fp_to_unsigned_bin(fp_int *a, unsigned char *b);
492
493 /*int fp_signed_bin_size(fp_int *a);*/
494 /*void fp_read_signed_bin(fp_int *a, unsigned char *b, int c);*/
495 /*void fp_to_signed_bin(fp_int *a, unsigned char *b);*/
496
497 /*int fp_read_radix(fp_int *a, char *str, int radix);*/
498 /*int fp_toradix(fp_int *a, char *str, int radix);*/
499 /*int fp_toradix_n(fp_int * a, char *str, int radix, int maxlen);*/
500
501
502 /* VARIOUS LOW LEVEL STUFFS */
503 void s_fp_add(fp_int *a, fp_int *b, fp_int *c);
504 void s_fp_sub(fp_int *a, fp_int *b, fp_int *c);
505 void fp_reverse(unsigned char *s, int len);
506
507 void fp_mul_comba(fp_int *A, fp_int *B, fp_int *C);
508
509 #ifdef TFM_SMALL_SET
510 void fp_mul_comba_small(fp_int *A, fp_int *B, fp_int *C);
511 #endif
512
513 #ifdef TFM_MUL3
514 void fp_mul_comba3(fp_int *A, fp_int *B, fp_int *C);
515 #endif
516 #ifdef TFM_MUL4
517 void fp_mul_comba4(fp_int *A, fp_int *B, fp_int *C);
518 #endif
519 #ifdef TFM_MUL6
520 void fp_mul_comba6(fp_int *A, fp_int *B, fp_int *C);
521 #endif
522 #ifdef TFM_MUL7
523 void fp_mul_comba7(fp_int *A, fp_int *B, fp_int *C);
524 #endif
525 #ifdef TFM_MUL8
526 void fp_mul_comba8(fp_int *A, fp_int *B, fp_int *C);
527 #endif
528 #ifdef TFM_MUL9
529 void fp_mul_comba9(fp_int *A, fp_int *B, fp_int *C);
530 #endif
531 #ifdef TFM_MUL12
532 void fp_mul_comba12(fp_int *A, fp_int *B, fp_int *C);
533 #endif
534 #ifdef TFM_MUL17
535 void fp_mul_comba17(fp_int *A, fp_int *B, fp_int *C);
536 #endif
537
538 #ifdef TFM_MUL20
539 void fp_mul_comba20(fp_int *A, fp_int *B, fp_int *C);
540 #endif
541 #ifdef TFM_MUL24
542 void fp_mul_comba24(fp_int *A, fp_int *B, fp_int *C);
543 #endif
544 #ifdef TFM_MUL28
545 void fp_mul_comba28(fp_int *A, fp_int *B, fp_int *C);
546 #endif
547 #ifdef TFM_MUL32
548 void fp_mul_comba32(fp_int *A, fp_int *B, fp_int *C);
549 #endif
550 #ifdef TFM_MUL48
551 void fp_mul_comba48(fp_int *A, fp_int *B, fp_int *C);
552 #endif
553 #ifdef TFM_MUL64
554 void fp_mul_comba64(fp_int *A, fp_int *B, fp_int *C);
555 #endif
556
557 void fp_sqr_comba(fp_int *A, fp_int *B);
558
559 #ifdef TFM_SMALL_SET
560 void fp_sqr_comba_small(fp_int *A, fp_int *B);
561 #endif
562
563 #ifdef TFM_SQR3
564 void fp_sqr_comba3(fp_int *A, fp_int *B);
565 #endif
566 #ifdef TFM_SQR4
567 void fp_sqr_comba4(fp_int *A, fp_int *B);
568 #endif
569 #ifdef TFM_SQR6
570 void fp_sqr_comba6(fp_int *A, fp_int *B);
571 #endif
572 #ifdef TFM_SQR7
573 void fp_sqr_comba7(fp_int *A, fp_int *B);
574 #endif
575 #ifdef TFM_SQR8
576 void fp_sqr_comba8(fp_int *A, fp_int *B);
577 #endif
578 #ifdef TFM_SQR9
579 void fp_sqr_comba9(fp_int *A, fp_int *B);
580 #endif
581 #ifdef TFM_SQR12
582 void fp_sqr_comba12(fp_int *A, fp_int *B);
583 #endif
584 #ifdef TFM_SQR17
585 void fp_sqr_comba17(fp_int *A, fp_int *B);
586 #endif
587
588 #ifdef TFM_SQR20
589 void fp_sqr_comba20(fp_int *A, fp_int *B);
590 #endif
591 #ifdef TFM_SQR24
592 void fp_sqr_comba24(fp_int *A, fp_int *B);
593 #endif
594 #ifdef TFM_SQR28
595 void fp_sqr_comba28(fp_int *A, fp_int *B);
596 #endif
597 #ifdef TFM_SQR32
598 void fp_sqr_comba32(fp_int *A, fp_int *B);
599 #endif
600 #ifdef TFM_SQR48
601 void fp_sqr_comba48(fp_int *A, fp_int *B);
602 #endif
603 #ifdef TFM_SQR64
604 void fp_sqr_comba64(fp_int *A, fp_int *B);
605 #endif
606 /*extern const char *fp_s_rmap;*/
607
608
609 /**
610  * Used by CyaSSL 
611  */
612
613 /* Types */
614     typedef fp_digit mp_digit;
615     typedef fp_word  mp_word;
616     typedef fp_int mp_int;
617
618 /* Constants */
619     #define MP_LT   FP_LT   /* less than    */
620     #define MP_EQ   FP_EQ   /* equal to     */
621     #define MP_GT   FP_GT   /* greater than */
622     #define MP_OKAY FP_OKAY /* ok result    */
623     #define MP_NO   FP_NO   /* yes/no result */
624     #define MP_YES  FP_YES  /* yes/no result */
625
626 /* Prototypes */
627 int  mp_init (mp_int * a);
628 void mp_clear (mp_int * a);
629 int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e, mp_int* f);
630
631 int  mp_add (mp_int * a, mp_int * b, mp_int * c);
632 int  mp_sub (mp_int * a, mp_int * b, mp_int * c);
633 int  mp_add_d (mp_int * a, mp_digit b, mp_int * c);
634
635 int  mp_mul (mp_int * a, mp_int * b, mp_int * c);
636 int  mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d);
637 int  mp_mod(mp_int *a, mp_int *b, mp_int *c);
638 int  mp_invmod(mp_int *a, mp_int *b, mp_int *c);
639 int  mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y);
640
641 int  mp_cmp(mp_int *a, mp_int *b);
642 int  mp_cmp_d(mp_int *a, mp_digit b);
643
644 int  mp_unsigned_bin_size(mp_int * a);
645 int  mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c);
646 int  mp_to_unsigned_bin (mp_int * a, unsigned char *b);
647
648 int  mp_sub_d(fp_int *a, fp_digit b, fp_int *c);
649 int  mp_copy(fp_int* a, fp_int* b);
650 int  mp_isodd(mp_int* a);
651 int  mp_iszero(mp_int* a);
652 int  mp_count_bits(mp_int *a);
653 int  mp_set_int(fp_int *a, fp_digit b);
654
655 #ifdef HAVE_ECC
656     int mp_read_radix(mp_int* a, const char* str, int radix);
657     int mp_set(fp_int *a, fp_digit b);
658     int mp_sqr(fp_int *A, fp_int *B);
659     int mp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp);
660     int mp_montgomery_setup(fp_int *a, fp_digit *rho);
661     int mp_div_2(fp_int * a, fp_int * b);
662     int mp_init_copy(fp_int * a, fp_int * b); 
663 #endif
664
665 #if defined(HAVE_ECC) || defined(CYASSL_KEY_GEN)
666     int mp_sqrmod(mp_int* a, mp_int* b, mp_int* c);
667     int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
668 #endif
669
670 #ifdef CYASSL_KEY_GEN
671 int  mp_gcd(fp_int *a, fp_int *b, fp_int *c);
672 int  mp_lcm(fp_int *a, fp_int *b, fp_int *c);
673 int  mp_prime_is_prime(mp_int* a, int t, int* result);
674 #endif /* CYASSL_KEY_GEN */
675
676 CYASSL_API word32 CheckRunTimeFastMath(void);
677
678 /* If user uses RSA, DH, DSA, or ECC math lib directly then fast math FP_SIZE
679    must match, return 1 if a match otherwise 0 */
680 #define CheckFastMathSettings() (FP_SIZE == CheckRunTimeFastMath())
681 #ifdef __cplusplus
682    }
683 #endif
684
685
686 #endif  /* CTAO_CRYPT_TFM_H */