tfm.h
Go to the documentation of this file.
1 /* tfm.h
2  *
3  * Copyright (C) 2006-2020 wolfSSL Inc.
4  *
5  * This file is part of wolfSSL.
6  *
7  * wolfSSL 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  * wolfSSL 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20  */
21 
22 
23 
24 /*
25  * Based on public domain TomsFastMath 0.10 by Tom St Denis, tomstdenis@iahu.ca,
26  * http://math.libtomcrypt.com
27  */
28 
29 
39 #ifndef WOLF_CRYPT_TFM_H
40 #define WOLF_CRYPT_TFM_H
41 
43 #ifndef CHAR_BIT
44  #include <limits.h>
45 #endif
46 
48 
49 #ifdef __cplusplus
50  extern "C" {
51 #endif
52 
53 #ifdef WOLFSSL_NO_ASM
54  #undef TFM_NO_ASM
55  #define TFM_NO_ASM
56 #endif
57 
58 #ifdef NO_64BIT
59  #undef NO_TFM_64BIT
60  #define NO_TFM_64BIT
61 #endif
62 
63 #ifndef NO_TFM_64BIT
64 /* autodetect x86-64 and make sure we are using 64-bit digits with x86-64 asm */
65 #if defined(__x86_64__)
66  #if defined(TFM_X86) || defined(TFM_SSE2) || defined(TFM_ARM)
67  #error x86-64 detected, x86-32/SSE2/ARM optimizations are not valid!
68  #endif
69  #if !defined(TFM_X86_64) && !defined(TFM_NO_ASM)
70  #define TFM_X86_64
71  #endif
72 #endif
73 #if defined(TFM_X86_64)
74  #if !defined(FP_64BIT)
75  #define FP_64BIT
76  #endif
77 #endif
78 /* use 64-bit digit even if not using asm on x86_64 */
79 #if defined(__x86_64__) && !defined(FP_64BIT)
80  #define FP_64BIT
81 #endif
82 /* if intel compiler doesn't provide 128 bit type don't turn on 64bit */
83 #if defined(FP_64BIT) && defined(__INTEL_COMPILER) && !defined(HAVE___UINT128_T)
84  #undef FP_64BIT
85  #undef TFM_X86_64
86 #endif
87 #endif /* NO_TFM_64BIT */
88 
89 /* try to detect x86-32 */
90 #if defined(__i386__) && !defined(TFM_SSE2)
91  #if defined(TFM_X86_64) || defined(TFM_ARM)
92  #error x86-32 detected, x86-64/ARM optimizations are not valid!
93  #endif
94  #if !defined(TFM_X86) && !defined(TFM_NO_ASM)
95  #define TFM_X86
96  #endif
97 #endif
98 
99 /* make sure we're 32-bit for x86-32/sse/arm/ppc32 */
100 #if (defined(TFM_X86) || defined(TFM_SSE2) || defined(TFM_ARM) || defined(TFM_PPC32)) && defined(FP_64BIT)
101  #warning x86-32, SSE2 and ARM, PPC32 optimizations require 32-bit digits (undefining)
102  #undef FP_64BIT
103 #endif
104 
105 /* multi asms? */
106 #ifdef TFM_X86
107  #define TFM_ASM
108 #endif
109 #ifdef TFM_X86_64
110  #ifdef TFM_ASM
111  #error TFM_ASM already defined!
112  #endif
113  #define TFM_ASM
114 #endif
115 #ifdef TFM_SSE2
116  #ifdef TFM_ASM
117  #error TFM_ASM already defined!
118  #endif
119  #define TFM_ASM
120 #endif
121 #ifdef TFM_ARM
122  #ifdef TFM_ASM
123  #error TFM_ASM already defined!
124  #endif
125  #define TFM_ASM
126 #endif
127 #ifdef TFM_PPC32
128  #ifdef TFM_ASM
129  #error TFM_ASM already defined!
130  #endif
131  #define TFM_ASM
132 #endif
133 #ifdef TFM_PPC64
134  #ifdef TFM_ASM
135  #error TFM_ASM already defined!
136  #endif
137  #define TFM_ASM
138 #endif
139 #ifdef TFM_AVR32
140  #ifdef TFM_ASM
141  #error TFM_ASM already defined!
142  #endif
143  #define TFM_ASM
144 #endif
145 
146 /* we want no asm? */
147 #ifdef TFM_NO_ASM
148  #undef TFM_X86
149  #undef TFM_X86_64
150  #undef TFM_SSE2
151  #undef TFM_ARM
152  #undef TFM_PPC32
153  #undef TFM_PPC64
154  #undef TFM_AVR32
155  #undef TFM_ASM
156 #endif
157 
158 /* ECC helpers */
159 #ifdef TFM_ECC192
160  #ifdef FP_64BIT
161  #define TFM_MUL3
162  #define TFM_SQR3
163  #else
164  #define TFM_MUL6
165  #define TFM_SQR6
166  #endif
167 #endif
168 
169 #ifdef TFM_ECC224
170  #ifdef FP_64BIT
171  #define TFM_MUL4
172  #define TFM_SQR4
173  #else
174  #define TFM_MUL7
175  #define TFM_SQR7
176  #endif
177 #endif
178 
179 #ifdef TFM_ECC256
180  #ifdef FP_64BIT
181  #define TFM_MUL4
182  #define TFM_SQR4
183  #else
184  #define TFM_MUL8
185  #define TFM_SQR8
186  #endif
187 #endif
188 
189 #ifdef TFM_ECC384
190  #ifdef FP_64BIT
191  #define TFM_MUL6
192  #define TFM_SQR6
193  #else
194  #define TFM_MUL12
195  #define TFM_SQR12
196  #endif
197 #endif
198 
199 #ifdef TFM_ECC521
200  #ifdef FP_64BIT
201  #define TFM_MUL9
202  #define TFM_SQR9
203  #else
204  #define TFM_MUL17
205  #define TFM_SQR17
206  #endif
207 #endif
208 
209 
210 /* allow user to define on fp_digit, fp_word types */
211 #ifndef WOLFSSL_BIGINT_TYPES
212 
213 /* some default configurations.
214  */
215 #if defined(WC_16BIT_CPU)
216  typedef unsigned int fp_digit;
217  #define SIZEOF_FP_DIGIT 2
218  typedef unsigned long fp_word;
219 #elif defined(FP_64BIT)
220  /* for GCC only on supported platforms */
221  typedef unsigned long long fp_digit; /* 64bit, 128 uses mode(TI) below */
222  #define SIZEOF_FP_DIGIT 8
223  typedef unsigned long fp_word __attribute__ ((mode(TI)));
224 #else
225 
226  #ifndef NO_TFM_64BIT
227  #if defined(_MSC_VER) || defined(__BORLANDC__)
228  typedef unsigned __int64 ulong64;
229  #else
230  typedef unsigned long long ulong64;
231  #endif
232  typedef unsigned int fp_digit;
233  #define SIZEOF_FP_DIGIT 4
234  typedef ulong64 fp_word;
235  #define FP_32BIT
236  #else
237  /* some procs like coldfire prefer not to place multiply into 64bit type
238  even though it exists */
239  typedef unsigned short fp_digit;
240  #define SIZEOF_FP_DIGIT 2
241  typedef unsigned int fp_word;
242  #endif
243 #endif
244 
245 #endif /* WOLFSSL_BIGINT_TYPES */
246 
247 
248 /* # of digits this is */
249 #define DIGIT_BIT ((CHAR_BIT) * SIZEOF_FP_DIGIT)
250 
251 /* Max size of any number in bits. Basically the largest size you will be
252  * multiplying should be half [or smaller] of FP_MAX_SIZE-four_digit
253  *
254  * It defaults to 4096-bits [allowing multiplications up to 2048x2048 bits ]
255  */
256 
257 
258 #ifndef FP_MAX_BITS
259  #define FP_MAX_BITS 4096
260 #endif
261 #ifdef WOLFSSL_OPENSSH
262  /* OpenSSH uses some BIG primes so we need to accommodate for that */
263  #undef FP_MAX_BITS
264  #define FP_MAX_BITS 16384
265 #endif
266 #define FP_MAX_SIZE (FP_MAX_BITS+(8*DIGIT_BIT))
267 
268 /* will this lib work? */
269 #if (CHAR_BIT & 7)
270  #error CHAR_BIT must be a multiple of eight.
271 #endif
272 #if FP_MAX_BITS % CHAR_BIT
273  #error FP_MAX_BITS must be a multiple of CHAR_BIT
274 #endif
275 
276 #define FP_MASK (fp_digit)(-1)
277 #define FP_DIGIT_MAX FP_MASK
278 #define FP_SIZE (FP_MAX_SIZE/DIGIT_BIT)
279 
280 #define FP_MAX_PRIME_SIZE (FP_MAX_BITS/(2*CHAR_BIT))
281 /* In terms of FP_MAX_BITS, it is double the size possible for a number
282  * to allow for multiplication, divide that 2 out. Also divide by CHAR_BIT
283  * to convert from bits to bytes. (Note, FP_PRIME_SIZE is the number of
284  * values in the canned prime number list.) */
285 
286 /* signs */
287 #define FP_ZPOS 0
288 #define FP_NEG 1
289 
290 /* return codes */
291 #define FP_OKAY 0
292 #define FP_VAL -1
293 #define FP_MEM -2
294 #define FP_NOT_INF -3
295 #define FP_WOULDBLOCK -4
296 
297 /* equalities */
298 #define FP_LT -1 /* less than */
299 #define FP_EQ 0 /* equal to */
300 #define FP_GT 1 /* greater than */
301 
302 /* replies */
303 #define FP_YES 1 /* yes response */
304 #define FP_NO 0 /* no response */
305 
306 #ifdef HAVE_WOLF_BIGINT
307  /* raw big integer */
308  typedef struct WC_BIGINT {
309  byte* buf;
310  word32 len;
311  void* heap;
312  } WC_BIGINT;
313  #define WOLF_BIGINT_DEFINED
314 #endif
315 
316 /* a FP type */
317 typedef struct fp_int {
318  int used;
319  int sign;
320 #if defined(ALT_ECC_SIZE) || defined(HAVE_WOLF_BIGINT)
321  int size;
322 #endif
323  fp_digit dp[FP_SIZE];
324 
325 #ifdef HAVE_WOLF_BIGINT
326  struct WC_BIGINT raw; /* unsigned binary (big endian) */
327 #endif
328 } fp_int;
329 
330 /* Types */
331 typedef fp_digit mp_digit;
332 typedef fp_word mp_word;
333 typedef fp_int mp_int;
334 
335 
336 /* wolf big int and common functions */
337 #include <wolfssl/wolfcrypt/wolfmath.h>
338 
339 
340 /* externally define this symbol to ignore the default settings, useful for changing the build from the make process */
341 #ifndef TFM_ALREADY_SET
342 
343 /* do we want the large set of small multiplications ?
344  Enable these if you are going to be doing a lot of small (<= 16 digit) multiplications say in ECC
345  Or if you're on a 64-bit machine doing RSA as a 1024-bit integer == 16 digits ;-)
346  */
347 /* need to refactor the function */
348 /*#define TFM_SMALL_SET */
349 
350 /* do we want huge code
351  Enable these if you are doing 20, 24, 28, 32, 48, 64 digit multiplications (useful for RSA)
352  Less important on 64-bit machines as 32 digits == 2048 bits
353  */
354 #if 0
355 #define TFM_MUL3
356 #define TFM_MUL4
357 #define TFM_MUL6
358 #define TFM_MUL7
359 #define TFM_MUL8
360 #define TFM_MUL9
361 #define TFM_MUL12
362 #define TFM_MUL17
363 #endif
364 #ifdef TFM_HUGE_SET
365 #define TFM_MUL20
366 #define TFM_MUL24
367 #define TFM_MUL28
368 #define TFM_MUL32
369 #if (FP_MAX_BITS >= 6144) && defined(FP_64BIT)
370  #define TFM_MUL48
371 #endif
372 #if (FP_MAX_BITS >= 8192) && defined(FP_64BIT)
373  #define TFM_MUL64
374 #endif
375 #endif
376 
377 #if 0
378 #define TFM_SQR3
379 #define TFM_SQR4
380 #define TFM_SQR6
381 #define TFM_SQR7
382 #define TFM_SQR8
383 #define TFM_SQR9
384 #define TFM_SQR12
385 #define TFM_SQR17
386 #endif
387 #ifdef TFM_HUGE_SET
388 #define TFM_SQR20
389 #define TFM_SQR24
390 #define TFM_SQR28
391 #define TFM_SQR32
392 #define TFM_SQR48
393 #define TFM_SQR64
394 #endif
395 
396 /* Optional math checks (enable WOLFSSL_DEBUG_MATH to print info) */
397 /* #define TFM_CHECK */
398 
399 /* Is the target a P4 Prescott
400  */
401 /* #define TFM_PRESCOTT */
402 
403 /* Do we want timing resistant fp_exptmod() ?
404  * This makes it slower but also timing invariant with respect to the exponent
405  */
406 /* #define TFM_TIMING_RESISTANT */
407 
408 #endif /* TFM_ALREADY_SET */
409 
410 /* functions */
411 
412 /* returns a TFM ident string useful for debugging... */
413 /*const char *fp_ident(void);*/
414 
415 /* initialize [or zero] an fp int */
416 void fp_init(fp_int *a);
417 MP_API void fp_zero(fp_int *a);
418 MP_API void fp_clear(fp_int *a); /* uses ForceZero to clear sensitive memory */
419 MP_API void fp_forcezero (fp_int * a);
420 MP_API void fp_free(fp_int* a);
421 
422 /* zero/one/even/odd/neg/word ? */
423 #define fp_iszero(a) (((a)->used == 0) ? FP_YES : FP_NO)
424 #define fp_isone(a) \
425  ((((a)->used == 1) && ((a)->dp[0] == 1)) ? FP_YES : FP_NO)
426 #define fp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? FP_YES : FP_NO)
427 #define fp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? FP_YES : FP_NO)
428 #define fp_isneg(a) (((a)->sign != 0) ? FP_YES : FP_NO)
429 #define fp_isword(a, w) \
430  ((((a)->used == 1) && ((a)->dp[0] == w)) || ((w == 0) && ((a)->used == 0)) \
431  ? FP_YES : FP_NO)
432 
433 /* set to a small digit */
434 void fp_set(fp_int *a, fp_digit b);
435 void fp_set_int(fp_int *a, unsigned long b);
436 
437 /* check if a bit is set */
438 int fp_is_bit_set(fp_int *a, fp_digit b);
439 /* set the b bit to 1 */
440 int fp_set_bit (fp_int * a, fp_digit b);
441 
442 /* copy from a to b */
443 void fp_copy(fp_int *a, fp_int *b);
444 void fp_init_copy(fp_int *a, fp_int *b);
445 
446 /* clamp digits */
447 #define fp_clamp(a) { while ((a)->used && (a)->dp[(a)->used-1] == 0) --((a)->used); (a)->sign = (a)->used ? (a)->sign : FP_ZPOS; }
448 #define mp_clamp(a) fp_clamp(a)
449 #define mp_grow(a,s) MP_OKAY
450 
451 /* negate and absolute */
452 #define fp_neg(a, b) { fp_copy(a, b); (b)->sign ^= 1; fp_clamp(b); }
453 #define fp_abs(a, b) { fp_copy(a, b); (b)->sign = 0; }
454 
455 /* right shift x digits */
456 void fp_rshd(fp_int *a, int x);
457 
458 /* right shift x bits */
459 void fp_rshb(fp_int *a, int x);
460 
461 /* left shift x digits */
462 void fp_lshd(fp_int *a, int x);
463 
464 /* signed comparison */
465 int fp_cmp(fp_int *a, fp_int *b);
466 
467 /* unsigned comparison */
468 int fp_cmp_mag(fp_int *a, fp_int *b);
469 
470 /* power of 2 operations */
471 void fp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d);
472 void fp_mod_2d(fp_int *a, int b, fp_int *c);
473 void fp_mul_2d(fp_int *a, int b, fp_int *c);
474 void fp_2expt (fp_int *a, int b);
475 void fp_mul_2(fp_int *a, fp_int *c);
476 void fp_div_2(fp_int *a, fp_int *c);
477 
478 /* Counts the number of lsbs which are zero before the first zero bit */
479 int fp_cnt_lsb(fp_int *a);
480 
481 /* c = a + b */
482 void fp_add(fp_int *a, fp_int *b, fp_int *c);
483 
484 /* c = a - b */
485 void fp_sub(fp_int *a, fp_int *b, fp_int *c);
486 
487 /* c = a * b */
488 int fp_mul(fp_int *a, fp_int *b, fp_int *c);
489 
490 /* b = a*a */
491 int fp_sqr(fp_int *a, fp_int *b);
492 
493 /* a/b => cb + d == a */
494 int fp_div(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
495 
496 /* c = a mod b, 0 <= c < b */
497 int fp_mod(fp_int *a, fp_int *b, fp_int *c);
498 
499 /* compare against a single digit */
500 int fp_cmp_d(fp_int *a, fp_digit b);
501 
502 /* c = a + b */
503 void fp_add_d(fp_int *a, fp_digit b, fp_int *c);
504 
505 /* c = a - b */
506 int fp_sub_d(fp_int *a, fp_digit b, fp_int *c);
507 
508 /* c = a * b */
509 void fp_mul_d(fp_int *a, fp_digit b, fp_int *c);
510 
511 /* a/b => cb + d == a */
512 /*int fp_div_d(fp_int *a, fp_digit b, fp_int *c, fp_digit *d);*/
513 
514 /* c = a mod b, 0 <= c < b */
515 /*int fp_mod_d(fp_int *a, fp_digit b, fp_digit *c);*/
516 
517 /* ---> number theory <--- */
518 /* d = a + b (mod c) */
519 /*int fp_addmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);*/
520 
521 /* d = a - b (mod c) */
522 /*int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);*/
523 
524 /* d = a * b (mod c) */
525 int fp_mulmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
526 
527 /* d = a - b (mod c) */
528 int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
529 
530 /* d = a + b (mod c) */
531 int fp_addmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
532 
533 /* c = a * a (mod b) */
534 int fp_sqrmod(fp_int *a, fp_int *b, fp_int *c);
535 
536 /* c = 1/a (mod b) */
537 int fp_invmod(fp_int *a, fp_int *b, fp_int *c);
538 int fp_invmod_mont_ct(fp_int *a, fp_int *b, fp_int *c, fp_digit mp);
539 
540 /* c = (a, b) */
541 /*int fp_gcd(fp_int *a, fp_int *b, fp_int *c);*/
542 
543 /* c = [a, b] */
544 /*int fp_lcm(fp_int *a, fp_int *b, fp_int *c);*/
545 
546 /* setups the montgomery reduction */
547 int fp_montgomery_setup(fp_int *a, fp_digit *mp);
548 
549 /* computes a = B**n mod b without division or multiplication useful for
550  * normalizing numbers in a Montgomery system.
551  */
552 void fp_montgomery_calc_normalization(fp_int *a, fp_int *b);
553 
554 /* computes x/R == x (mod N) via Montgomery Reduction */
555 int fp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp);
556 
557 /* d = a**b (mod c) */
558 int fp_exptmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
559 int fp_exptmod_ex(fp_int *a, fp_int *b, int minDigits, fp_int *c, fp_int *d);
560 int fp_exptmod_nct(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
561 
562 #ifdef WC_RSA_NONBLOCK
563 
564 enum tfmExptModNbState {
565  TFM_EXPTMOD_NB_INIT = 0,
566  TFM_EXPTMOD_NB_MONT,
567  TFM_EXPTMOD_NB_MONT_RED,
568  TFM_EXPTMOD_NB_MONT_MUL,
569  TFM_EXPTMOD_NB_MONT_MOD,
570  TFM_EXPTMOD_NB_MONT_MODCHK,
571  TFM_EXPTMOD_NB_NEXT,
572  TFM_EXPTMOD_NB_MUL,
573  TFM_EXPTMOD_NB_MUL_RED,
574  TFM_EXPTMOD_NB_SQR,
575  TFM_EXPTMOD_NB_SQR_RED,
576  TFM_EXPTMOD_NB_RED,
577  TFM_EXPTMOD_NB_COUNT /* last item for total state count only */
578 };
579 
580 typedef struct {
581 #ifndef WC_NO_CACHE_RESISTANT
582  fp_int R[3];
583 #else
584  fp_int R[2];
585 #endif
586  fp_digit buf;
587  fp_digit mp;
588  int bitcnt;
589  int digidx;
590  int y;
591  int state; /* tfmExptModNbState */
592 #ifdef WC_RSA_NONBLOCK_TIME
593  word32 maxBlockInst; /* maximum instructions to block */
594  word32 totalInst; /* tracks total instructions */
595 #endif
596 } exptModNb_t;
597 
598 #ifdef WC_RSA_NONBLOCK_TIME
599 enum {
600  TFM_EXPTMOD_NB_STOP = 0, /* stop and return FP_WOULDBLOCK */
601  TFM_EXPTMOD_NB_CONTINUE = 1, /* keep blocking */
602 };
603 #endif
604 
605 /* non-blocking version of timing resistant fp_exptmod function */
606 /* supports cache resistance */
607 int fp_exptmod_nb(exptModNb_t* nb, fp_int* G, fp_int* X, fp_int* P, fp_int* Y);
608 
609 #endif /* WC_RSA_NONBLOCK */
610 
611 /* primality stuff */
612 
613 /* perform a Miller-Rabin test of a to the base b and store result in "result" */
614 /*void fp_prime_miller_rabin (fp_int * a, fp_int * b, int *result);*/
615 
616 #define FP_PRIME_SIZE 256
617 /* 256 trial divisions + 8 Miller-Rabins, returns FP_YES if probable prime */
618 /*int fp_isprime(fp_int *a);*/
619 /* extended version of fp_isprime, do 't' Miller-Rabins instead of only 8 */
620 /*int fp_isprime_ex(fp_int *a, int t, int* result);*/
621 
622 /* Primality generation flags */
623 /*#define TFM_PRIME_BBS 0x0001 */ /* BBS style prime */
624 /*#define TFM_PRIME_SAFE 0x0002 */ /* Safe prime (p-1)/2 == prime */
625 /*#define TFM_PRIME_2MSB_OFF 0x0004 */ /* force 2nd MSB to 0 */
626 /*#define TFM_PRIME_2MSB_ON 0x0008 */ /* force 2nd MSB to 1 */
627 
628 /* callback for fp_prime_random, should fill dst with random bytes and return how many read [up to len] */
629 /*typedef int tfm_prime_callback(unsigned char *dst, int len, void *dat);*/
630 
631 /*#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)*/
632 
633 /*int fp_prime_random_ex(fp_int *a, int t, int size, int flags, tfm_prime_callback cb, void *dat);*/
634 
635 /* radix conversions */
636 int fp_count_bits(fp_int *a);
637 int fp_leading_bit(fp_int *a);
638 
639 int fp_unsigned_bin_size(fp_int *a);
640 void fp_read_unsigned_bin(fp_int *a, const unsigned char *b, int c);
641 int fp_to_unsigned_bin(fp_int *a, unsigned char *b);
642 int fp_to_unsigned_bin_len(fp_int *a, unsigned char *b, int c);
643 int fp_to_unsigned_bin_at_pos(int x, fp_int *t, unsigned char *b);
644 
645 /*int fp_signed_bin_size(fp_int *a);*/
646 /*void fp_read_signed_bin(fp_int *a, const unsigned char *b, int c);*/
647 /*void fp_to_signed_bin(fp_int *a, unsigned char *b);*/
648 
649 /*int fp_read_radix(fp_int *a, char *str, int radix);*/
650 /*int fp_toradix(fp_int *a, char *str, int radix);*/
651 /*int fp_toradix_n(fp_int * a, char *str, int radix, int maxlen);*/
652 
653 
654 /* VARIOUS LOW LEVEL STUFFS */
655 void s_fp_add(fp_int *a, fp_int *b, fp_int *c);
656 void s_fp_sub(fp_int *a, fp_int *b, fp_int *c);
657 void fp_reverse(unsigned char *s, int len);
658 
659 int fp_mul_comba(fp_int *a, fp_int *b, fp_int *c);
660 
661 int fp_mul_comba_small(fp_int *a, fp_int *b, fp_int *c);
662 int fp_mul_comba3(fp_int *a, fp_int *b, fp_int *c);
663 int fp_mul_comba4(fp_int *a, fp_int *b, fp_int *c);
664 int fp_mul_comba6(fp_int *a, fp_int *b, fp_int *c);
665 int fp_mul_comba7(fp_int *a, fp_int *b, fp_int *c);
666 int fp_mul_comba8(fp_int *a, fp_int *b, fp_int *c);
667 int fp_mul_comba9(fp_int *a, fp_int *b, fp_int *c);
668 int fp_mul_comba12(fp_int *a, fp_int *b, fp_int *c);
669 int fp_mul_comba17(fp_int *a, fp_int *b, fp_int *c);
670 int fp_mul_comba20(fp_int *a, fp_int *b, fp_int *c);
671 int fp_mul_comba24(fp_int *a, fp_int *b, fp_int *c);
672 int fp_mul_comba28(fp_int *a, fp_int *b, fp_int *c);
673 int fp_mul_comba32(fp_int *a, fp_int *b, fp_int *c);
674 int fp_mul_comba48(fp_int *a, fp_int *b, fp_int *c);
675 int fp_mul_comba64(fp_int *a, fp_int *b, fp_int *c);
676 int fp_sqr_comba(fp_int *a, fp_int *b);
677 int fp_sqr_comba_small(fp_int *a, fp_int *b);
678 int fp_sqr_comba3(fp_int *a, fp_int *b);
679 int fp_sqr_comba4(fp_int *a, fp_int *b);
680 int fp_sqr_comba6(fp_int *a, fp_int *b);
681 int fp_sqr_comba7(fp_int *a, fp_int *b);
682 int fp_sqr_comba8(fp_int *a, fp_int *b);
683 int fp_sqr_comba9(fp_int *a, fp_int *b);
684 int fp_sqr_comba12(fp_int *a, fp_int *b);
685 int fp_sqr_comba17(fp_int *a, fp_int *b);
686 int fp_sqr_comba20(fp_int *a, fp_int *b);
687 int fp_sqr_comba24(fp_int *a, fp_int *b);
688 int fp_sqr_comba28(fp_int *a, fp_int *b);
689 int fp_sqr_comba32(fp_int *a, fp_int *b);
690 int fp_sqr_comba48(fp_int *a, fp_int *b);
691 int fp_sqr_comba64(fp_int *a, fp_int *b);
692 
693 
698 /* Constants */
699 #define MP_LT FP_LT /* less than */
700 #define MP_EQ FP_EQ /* equal to */
701 #define MP_GT FP_GT /* greater than */
702 #define MP_VAL FP_VAL /* invalid */
703 #define MP_MEM FP_MEM /* memory error */
704 #define MP_NOT_INF FP_NOT_INF /* point not at infinity */
705 #define MP_OKAY FP_OKAY /* ok result */
706 #define MP_NO FP_NO /* yes/no result */
707 #define MP_YES FP_YES /* yes/no result */
708 #define MP_ZPOS FP_ZPOS
709 #define MP_NEG FP_NEG
710 #define MP_MASK FP_MASK
711 
712 /* Prototypes */
713 #define mp_zero(a) fp_zero(a)
714 #define mp_isone(a) fp_isone(a)
715 #define mp_iseven(a) fp_iseven(a)
716 #define mp_isneg(a) fp_isneg(a)
717 #define mp_isword(a, w) fp_isword(a, w)
718 
719 #define MP_RADIX_BIN 2
720 #define MP_RADIX_OCT 8
721 #define MP_RADIX_DEC 10
722 #define MP_RADIX_HEX 16
723 #define MP_RADIX_MAX 64
724 
725 #define mp_tobinary(M, S) mp_toradix((M), (S), MP_RADIX_BIN)
726 #define mp_tooctal(M, S) mp_toradix((M), (S), MP_RADIX_OCT)
727 #define mp_todecimal(M, S) mp_toradix((M), (S), MP_RADIX_DEC)
728 #define mp_tohex(M, S) mp_toradix((M), (S), MP_RADIX_HEX)
729 
730 MP_API int mp_init (mp_int * a);
731 MP_API void mp_clear (mp_int * a);
732 MP_API void mp_free (mp_int * a);
733 MP_API void mp_forcezero (mp_int * a);
734 MP_API int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e,
735  mp_int* f);
736 
737 MP_API int mp_add (mp_int * a, mp_int * b, mp_int * c);
738 MP_API int mp_sub (mp_int * a, mp_int * b, mp_int * c);
739 MP_API int mp_add_d (mp_int * a, mp_digit b, mp_int * c);
740 
741 MP_API int mp_mul (mp_int * a, mp_int * b, mp_int * c);
742 MP_API int mp_mul_d (mp_int * a, mp_digit b, mp_int * c);
743 MP_API int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d);
744 MP_API int mp_submod (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
745 MP_API int mp_addmod (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
746 MP_API int mp_mod(mp_int *a, mp_int *b, mp_int *c);
747 MP_API int mp_invmod(mp_int *a, mp_int *b, mp_int *c);
748 MP_API int mp_invmod_mont_ct(mp_int *a, mp_int *b, mp_int *c, fp_digit mp);
749 MP_API int mp_exptmod (mp_int * g, mp_int * x, mp_int * p, mp_int * y);
750 MP_API int mp_exptmod_ex (mp_int * g, mp_int * x, int minDigits, mp_int * p,
751  mp_int * y);
752 MP_API int mp_exptmod_nct (mp_int * g, mp_int * x, mp_int * p, mp_int * y);
753 MP_API int mp_mul_2d(mp_int *a, int b, mp_int *c);
754 MP_API int mp_2expt(mp_int* a, int b);
755 
756 MP_API int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d);
757 
758 MP_API int mp_cmp(mp_int *a, mp_int *b);
759 MP_API int mp_cmp_d(mp_int *a, mp_digit b);
760 
761 MP_API int mp_unsigned_bin_size(mp_int * a);
762 MP_API int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c);
763 MP_API int mp_to_unsigned_bin_at_pos(int x, mp_int *t, unsigned char *b);
764 MP_API int mp_to_unsigned_bin (mp_int * a, unsigned char *b);
765 MP_API int mp_to_unsigned_bin_len(mp_int * a, unsigned char *b, int c);
766 
767 MP_API int mp_sub_d(fp_int *a, fp_digit b, fp_int *c);
768 MP_API int mp_copy(fp_int* a, fp_int* b);
769 MP_API int mp_isodd(mp_int* a);
770 MP_API int mp_iszero(mp_int* a);
771 MP_API int mp_count_bits(mp_int *a);
772 MP_API int mp_leading_bit(mp_int *a);
773 MP_API int mp_set_int(mp_int *a, unsigned long b);
774 MP_API int mp_is_bit_set (mp_int * a, mp_digit b);
775 MP_API int mp_set_bit (mp_int * a, mp_digit b);
776 MP_API void mp_rshb(mp_int *a, int x);
777 MP_API void mp_rshd(mp_int *a, int x);
778 MP_API int mp_toradix (mp_int *a, char *str, int radix);
779 MP_API int mp_radix_size (mp_int * a, int radix, int *size);
780 
781 #ifdef WOLFSSL_DEBUG_MATH
782  MP_API void mp_dump(const char* desc, mp_int* a, byte verbose);
783 #else
784  #define mp_dump(desc, a, verbose)
785 #endif
786 
787 #if !defined(NO_DSA) || defined(HAVE_ECC)
788  MP_API int mp_read_radix(mp_int* a, const char* str, int radix);
789 #endif
790 
791 #ifdef HAVE_ECC
792  MP_API int mp_sqr(fp_int *a, fp_int *b);
793  MP_API int mp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp);
794  MP_API int mp_montgomery_setup(fp_int *a, fp_digit *rho);
795  MP_API int mp_div_2(fp_int * a, fp_int * b);
796  MP_API int mp_init_copy(fp_int * a, fp_int * b);
797 #endif
798 
799 #if defined(HAVE_ECC) || !defined(NO_RSA) || !defined(NO_DSA) || \
800  defined(WOLFSSL_KEY_GEN)
801  MP_API int mp_set(fp_int *a, fp_digit b);
802 #endif
803 
804 #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || !defined(NO_RSA) || \
805  !defined(NO_DSA) || !defined(NO_DH)
806  MP_API int mp_sqrmod(mp_int* a, mp_int* b, mp_int* c);
807  MP_API int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
808 #endif
809 
810 #if !defined(NO_DH) || !defined(NO_DSA) || !defined(NO_RSA) || defined(WOLFSSL_KEY_GEN)
811 MP_API int mp_prime_is_prime(mp_int* a, int t, int* result);
812 MP_API int mp_prime_is_prime_ex(mp_int* a, int t, int* result, WC_RNG* rng);
813 #endif /* !NO_DH || !NO_DSA || !NO_RSA || WOLFSSL_KEY_GEN */
814 #ifdef WOLFSSL_KEY_GEN
815 MP_API int mp_gcd(fp_int *a, fp_int *b, fp_int *c);
816 MP_API int mp_lcm(fp_int *a, fp_int *b, fp_int *c);
817 MP_API int mp_rand_prime(mp_int* N, int len, WC_RNG* rng, void* heap);
818 MP_API int mp_exch(mp_int *a, mp_int *b);
819 #endif /* WOLFSSL_KEY_GEN */
820 
821 MP_API int mp_cnt_lsb(fp_int *a);
822 MP_API int mp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d);
823 MP_API int mp_mod_d(fp_int* a, fp_digit b, fp_digit* c);
824 MP_API int mp_lshd (mp_int * a, int b);
825 MP_API int mp_abs(mp_int* a, mp_int* b);
826 
827 WOLFSSL_API word32 CheckRunTimeFastMath(void);
828 
829 /* If user uses RSA, DH, DSA, or ECC math lib directly then fast math FP_SIZE
830  must match, return 1 if a match otherwise 0 */
831 #define CheckFastMathSettings() (FP_SIZE == CheckRunTimeFastMath())
832 
833 
834 #ifdef __cplusplus
835  }
836 #endif
837 
838 #endif /* WOLF_CRYPT_TFM_H */
839 
Definition: tfm.h:317
Definition: integer.h:200
WOLFSSL_API word32 CheckRunTimeFastMath(void)
This function checks the runtime fastmath settings for the maximum size of an integer. It is important when a user is using a wolfCrypt library independently, as the FP_SIZE must match for each library in order for math to work correctly. This check is defined as CheckFastMathSettings(), which simply compares CheckRunTimeFastMath and FP_SIZE, returning 0 if there is a mismatch, or 1 if they match.
Definition: tfm.c:98
Definition: integer.h:191
Definition: random.h:153
Definition: tfm.h:580