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
24 * Based on public domain TomsFastMath 0.10 by Tom St Denis, tomstdenis@iahu.ca,
25 * http://math.libtomcrypt.com
30 * Edited by Moisés Guimarães (moises.guimaraes@phoebus.com.br)
31 * to fit CyaSSL's needs.
35 #ifndef CTAO_CRYPT_TFM_H
36 #define CTAO_CRYPT_TFM_H
38 #include <cyassl/ctaocrypt/types.h>
49 #define MIN(x,y) ((x)<(y)?(x):(y))
53 #define MAX(x,y) ((x)>(y)?(x):(y))
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!
62 #if !defined(TFM_X86_64) && !defined(TFM_NO_ASM)
66 #if defined(TFM_X86_64)
67 #if !defined(FP_64BIT)
71 /* use 64-bit digit even if not using asm on x86_64 */
72 #if defined(__x86_64__) && !defined(FP_64BIT)
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!
81 #if !defined(TFM_X86) && !defined(TFM_NO_ASM)
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)
98 #error TFM_ASM already defined!
104 #error TFM_ASM already defined!
110 #error TFM_ASM already defined!
116 #error TFM_ASM already defined!
122 #error TFM_ASM already defined!
128 #error TFM_ASM already defined!
133 /* we want no asm? */
197 /* some default configurations.
199 #if defined(FP_64BIT)
200 /* for GCC only on supported platforms */
202 typedef unsigned long ulong64;
204 typedef ulong64 fp_digit;
205 typedef unsigned long fp_word __attribute__ ((mode(TI)));
207 /* this is to make porting into LibTomCrypt easier :-) */
209 #if defined(_MSC_VER) || defined(__BORLANDC__)
210 typedef unsigned __int64 ulong64;
211 typedef signed __int64 long64;
213 typedef unsigned long long ulong64;
214 typedef signed long long long64;
217 typedef unsigned int fp_digit;
218 typedef ulong64 fp_word;
221 /* # of digits this is */
222 #define DIGIT_BIT (int)((CHAR_BIT) * sizeof(fp_digit))
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
227 * It defaults to 4096-bits [allowing multiplications upto 2048x2048 bits ]
230 #define FP_MAX_BITS 4096
232 #define FP_MAX_SIZE (FP_MAX_BITS+(8*DIGIT_BIT))
234 /* will this lib work? */
236 #error CHAR_BIT must be a multiple of eight.
238 #if FP_MAX_BITS % CHAR_BIT
239 #error FP_MAX_BITS must be a multiple of CHAR_BIT
242 #define FP_MASK (fp_digit)(-1)
243 #define FP_SIZE (FP_MAX_SIZE/DIGIT_BIT)
255 #define FP_LT -1 /* less than */
256 #define FP_EQ 0 /* equal to */
257 #define FP_GT 1 /* greater than */
260 #define FP_YES 1 /* yes response */
261 #define FP_NO 0 /* no response */
265 fp_digit dp[FP_SIZE];
270 /* externally define this symbol to ignore the default settings, useful for changing the build from the make process */
271 #ifndef TFM_ALREADY_SET
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 ;-)
277 /* need to refactor the function */
278 /*#define TFM_SMALL_SET */
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
299 #if (FP_MAX_BITS >= 6144) && defined(FP_64BIT)
302 #if (FP_MAX_BITS >= 8192) && defined(FP_64BIT)
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)
329 /* #define TFM_CHECK */
331 /* Is the target a P4 Prescott
333 /* #define TFM_PRESCOTT */
335 /* Do we want timing resistant fp_exptmod() ?
336 * This makes it slower but also timing invariant with respect to the exponent
338 /* #define TFM_TIMING_RESISTANT */
340 #endif /* TFM_ALREADY_SET */
344 /* returns a TFM ident string useful for debugging... */
345 /*const char *fp_ident(void);*/
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)
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)
356 /* set to a small digit */
357 void fp_set(fp_int *a, fp_digit b);
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)
364 #define fp_clamp(a) { while ((a)->used && (a)->dp[(a)->used-1] == 0) --((a)->used); (a)->sign = (a)->used ? (a)->sign : FP_ZPOS; }
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; }
370 /* right shift x digits */
371 void fp_rshd(fp_int *a, int x);
373 /* left shift x digits */
374 void fp_lshd(fp_int *a, int x);
376 /* signed comparison */
377 int fp_cmp(fp_int *a, fp_int *b);
379 /* unsigned comparison */
380 int fp_cmp_mag(fp_int *a, fp_int *b);
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);
390 /* Counts the number of lsbs which are zero before the first zero bit */
391 /*int fp_cnt_lsb(fp_int *a);*/
394 void fp_add(fp_int *a, fp_int *b, fp_int *c);
397 void fp_sub(fp_int *a, fp_int *b, fp_int *c);
400 void fp_mul(fp_int *a, fp_int *b, fp_int *c);
403 void fp_sqr(fp_int *a, fp_int *b);
405 /* a/b => cb + d == a */
406 int fp_div(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
408 /* c = a mod b, 0 <= c < b */
409 int fp_mod(fp_int *a, fp_int *b, fp_int *c);
411 /* compare against a single digit */
412 int fp_cmp_d(fp_int *a, fp_digit b);
415 void fp_add_d(fp_int *a, fp_digit b, fp_int *c);
418 void fp_sub_d(fp_int *a, fp_digit b, fp_int *c);
421 void fp_mul_d(fp_int *a, fp_digit b, fp_int *c);
423 /* a/b => cb + d == a */
424 /*int fp_div_d(fp_int *a, fp_digit b, fp_int *c, fp_digit *d);*/
426 /* c = a mod b, 0 <= c < b */
427 /*int fp_mod_d(fp_int *a, fp_digit b, fp_digit *c);*/
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);*/
433 /* d = a - b (mod c) */
434 /*int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);*/
436 /* d = a * b (mod c) */
437 int fp_mulmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
439 /* c = a * a (mod b) */
440 int fp_sqrmod(fp_int *a, fp_int *b, fp_int *c);
442 /* c = 1/a (mod b) */
443 int fp_invmod(fp_int *a, fp_int *b, fp_int *c);
446 /*void fp_gcd(fp_int *a, fp_int *b, fp_int *c);*/
449 /*void fp_lcm(fp_int *a, fp_int *b, fp_int *c);*/
451 /* setups the montgomery reduction */
452 int fp_montgomery_setup(fp_int *a, fp_digit *mp);
454 /* computes a = B**n mod b without division or multiplication useful for
455 * normalizing numbers in a Montgomery system.
457 void fp_montgomery_calc_normalization(fp_int *a, fp_int *b);
459 /* computes x/R == x (mod N) via Montgomery Reduction */
460 void fp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp);
462 /* d = a**b (mod c) */
463 int fp_exptmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
465 /* primality stuff */
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);*/
470 /* 256 trial divisions + 8 Miller-Rabins, returns FP_YES if probable prime */
471 /*int fp_isprime(fp_int *a);*/
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 */
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);*/
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)*/
484 /*int fp_prime_random_ex(fp_int *a, int t, int size, int flags, tfm_prime_callback cb, void *dat);*/
486 /* radix conersions */
487 int fp_count_bits(fp_int *a);
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);
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);*/
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);*/
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);
507 void fp_mul_comba(fp_int *A, fp_int *B, fp_int *C);
510 void fp_mul_comba_small(fp_int *A, fp_int *B, fp_int *C);
514 void fp_mul_comba3(fp_int *A, fp_int *B, fp_int *C);
517 void fp_mul_comba4(fp_int *A, fp_int *B, fp_int *C);
520 void fp_mul_comba6(fp_int *A, fp_int *B, fp_int *C);
523 void fp_mul_comba7(fp_int *A, fp_int *B, fp_int *C);
526 void fp_mul_comba8(fp_int *A, fp_int *B, fp_int *C);
529 void fp_mul_comba9(fp_int *A, fp_int *B, fp_int *C);
532 void fp_mul_comba12(fp_int *A, fp_int *B, fp_int *C);
535 void fp_mul_comba17(fp_int *A, fp_int *B, fp_int *C);
539 void fp_mul_comba20(fp_int *A, fp_int *B, fp_int *C);
542 void fp_mul_comba24(fp_int *A, fp_int *B, fp_int *C);
545 void fp_mul_comba28(fp_int *A, fp_int *B, fp_int *C);
548 void fp_mul_comba32(fp_int *A, fp_int *B, fp_int *C);
551 void fp_mul_comba48(fp_int *A, fp_int *B, fp_int *C);
554 void fp_mul_comba64(fp_int *A, fp_int *B, fp_int *C);
557 void fp_sqr_comba(fp_int *A, fp_int *B);
560 void fp_sqr_comba_small(fp_int *A, fp_int *B);
564 void fp_sqr_comba3(fp_int *A, fp_int *B);
567 void fp_sqr_comba4(fp_int *A, fp_int *B);
570 void fp_sqr_comba6(fp_int *A, fp_int *B);
573 void fp_sqr_comba7(fp_int *A, fp_int *B);
576 void fp_sqr_comba8(fp_int *A, fp_int *B);
579 void fp_sqr_comba9(fp_int *A, fp_int *B);
582 void fp_sqr_comba12(fp_int *A, fp_int *B);
585 void fp_sqr_comba17(fp_int *A, fp_int *B);
589 void fp_sqr_comba20(fp_int *A, fp_int *B);
592 void fp_sqr_comba24(fp_int *A, fp_int *B);
595 void fp_sqr_comba28(fp_int *A, fp_int *B);
598 void fp_sqr_comba32(fp_int *A, fp_int *B);
601 void fp_sqr_comba48(fp_int *A, fp_int *B);
604 void fp_sqr_comba64(fp_int *A, fp_int *B);
606 /*extern const char *fp_s_rmap;*/
614 typedef fp_digit mp_digit;
615 typedef fp_word mp_word;
616 typedef fp_int mp_int;
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 */
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);
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);
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);
641 int mp_cmp(mp_int *a, mp_int *b);
642 int mp_cmp_d(mp_int *a, mp_digit b);
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);
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);
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);
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);
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 */
676 CYASSL_API word32 CheckRunTimeFastMath(void);
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())
686 #endif /* CTAO_CRYPT_TFM_H */