tfm.h 27 KB

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