tfm.h 24 KB

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