tfm.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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 MP_SIZE (FP_MAX_SIZE/DIGIT_BIT) /* for compatibility with SP_INT */
  260. #define FP_MAX_PRIME_SIZE (FP_MAX_BITS/(2*CHAR_BIT))
  261. /* In terms of FP_MAX_BITS, it is double the size possible for a number
  262. * to allow for multiplication, divide that 2 out. Also divide by CHAR_BIT
  263. * to convert from bits to bytes. (Note, FP_PRIME_SIZE is the number of
  264. * values in the canned prime number list.) */
  265. /* signs */
  266. #define FP_ZPOS 0
  267. #define FP_NEG 1
  268. /* return codes */
  269. #define FP_OKAY 0
  270. #define FP_VAL (-1)
  271. #define FP_MEM (-2)
  272. #define FP_NOT_INF (-3)
  273. #define FP_WOULDBLOCK (-4)
  274. /* equalities */
  275. #define FP_LT (-1) /* less than */
  276. #define FP_EQ 0 /* equal to */
  277. #define FP_GT 1 /* greater than */
  278. /* replies */
  279. #define FP_YES 1 /* yes response */
  280. #define FP_NO 0 /* no response */
  281. #ifdef WOLFSSL_SMALL_STACK
  282. /*
  283. * Dynamic memory allocation of mp_int.
  284. */
  285. /* Declare a dynamically allocated mp_int. */
  286. #define DECL_MP_INT_SIZE(name, bits) \
  287. mp_int* name = NULL
  288. /* Declare a dynamically allocated mp_int. */
  289. #define DECL_MP_INT_SIZE_DYN(name, bits, max) \
  290. mp_int* name = NULL
  291. /* Allocate an mp_int of minimal size and zero out. */
  292. #define NEW_MP_INT_SIZE(name, bits, heap, type) \
  293. do { \
  294. name = (mp_int*)XMALLOC(sizeof(mp_int), heap, type); \
  295. if (name != NULL) { \
  296. XMEMSET(name, 0, sizeof(mp_int)); \
  297. } \
  298. } \
  299. while (0)
  300. /* Dispose of dynamically allocated mp_int. */
  301. #define FREE_MP_INT_SIZE(name, heap, type) \
  302. XFREE(name, heap, type)
  303. /* Must check for mp_int pointer for NULL. */
  304. #define MP_INT_SIZE_CHECK_NULL
  305. #else
  306. /*
  307. * Static allocation of mp_int.
  308. */
  309. /* Declare a statically allocated mp_int. */
  310. #define DECL_MP_INT_SIZE(name, bits) \
  311. mp_int name[1]
  312. /* Declare a statically allocated mp_int. */
  313. #define DECL_MP_INT_SIZE_DYN(name, bits, max) \
  314. mp_int name[1]
  315. /* Zero out mp_int of minimal size. */
  316. #define NEW_MP_INT_SIZE(name, bits, heap, type) \
  317. XMEMSET(name, 0, sizeof(mp_int))
  318. /* Dispose of static mp_int. */
  319. #define FREE_MP_INT_SIZE(name, heap, type) WC_DO_NOTHING
  320. #endif
  321. /* Initialize an mp_int. */
  322. #define INIT_MP_INT_SIZE(name, bits) \
  323. mp_init(name)
  324. /* Type to cast to when using size marcos. */
  325. #define MP_INT_SIZE mp_int
  326. #ifdef HAVE_WOLF_BIGINT
  327. /* raw big integer */
  328. typedef struct WC_BIGINT {
  329. byte* buf;
  330. word32 len;
  331. void* heap;
  332. } WC_BIGINT;
  333. #define WOLF_BIGINT_DEFINED
  334. #endif
  335. /* a FP type */
  336. typedef struct fp_int {
  337. int used;
  338. int sign;
  339. #if defined(ALT_ECC_SIZE) || defined(HAVE_WOLF_BIGINT)
  340. int size;
  341. #endif
  342. fp_digit dp[FP_SIZE];
  343. #ifdef HAVE_WOLF_BIGINT
  344. struct WC_BIGINT raw; /* unsigned binary (big endian) */
  345. #endif
  346. } fp_int;
  347. /* Types */
  348. typedef fp_digit mp_digit;
  349. typedef fp_word mp_word;
  350. typedef fp_int mp_int;
  351. /* wolf big int and common functions */
  352. #include <wolfssl/wolfcrypt/wolfmath.h>
  353. /* externally define this symbol to ignore the default settings, useful for changing the build from the make process */
  354. #ifndef TFM_ALREADY_SET
  355. /* do we want the large set of small multiplications ?
  356. Enable these if you are going to be doing a lot of small (<= 16 digit) multiplications say in ECC
  357. Or if you're on a 64-bit machine doing RSA as a 1024-bit integer == 16 digits ;-)
  358. */
  359. /* need to refactor the function */
  360. /*#define TFM_SMALL_SET */
  361. /* do we want huge code
  362. Enable these if you are doing 20, 24, 28, 32, 48, 64 digit multiplications (useful for RSA)
  363. Less important on 64-bit machines as 32 digits == 2048 bits
  364. */
  365. #if 0
  366. #define TFM_MUL3
  367. #define TFM_MUL4
  368. #define TFM_MUL6
  369. #define TFM_MUL7
  370. #define TFM_MUL8
  371. #define TFM_MUL9
  372. #define TFM_MUL12
  373. #define TFM_MUL17
  374. #endif
  375. #ifdef TFM_HUGE_SET
  376. #define TFM_MUL20
  377. #define TFM_MUL24
  378. #define TFM_MUL28
  379. #define TFM_MUL32
  380. #if (FP_MAX_BITS >= 6144) && defined(FP_64BIT)
  381. #define TFM_MUL48
  382. #endif
  383. #if (FP_MAX_BITS >= 8192) && defined(FP_64BIT)
  384. #define TFM_MUL64
  385. #endif
  386. #endif
  387. #if 0
  388. #define TFM_SQR3
  389. #define TFM_SQR4
  390. #define TFM_SQR6
  391. #define TFM_SQR7
  392. #define TFM_SQR8
  393. #define TFM_SQR9
  394. #define TFM_SQR12
  395. #define TFM_SQR17
  396. #endif
  397. #ifdef TFM_HUGE_SET
  398. #define TFM_SQR20
  399. #define TFM_SQR24
  400. #define TFM_SQR28
  401. #define TFM_SQR32
  402. #define TFM_SQR48
  403. #define TFM_SQR64
  404. #endif
  405. /* Optional math checks (enable WOLFSSL_DEBUG_MATH to print info) */
  406. /* #define TFM_CHECK */
  407. /* Is the target a P4 Prescott
  408. */
  409. /* #define TFM_PRESCOTT */
  410. /* Do we want timing resistant fp_exptmod() ?
  411. * This makes it slower but also timing invariant with respect to the exponent
  412. */
  413. /* #define TFM_TIMING_RESISTANT */
  414. #endif /* TFM_ALREADY_SET */
  415. /* functions */
  416. /* returns a TFM ident string useful for debugging... */
  417. /*const char *fp_ident(void);*/
  418. /* initialize [or zero] an fp int */
  419. void fp_init(fp_int *a);
  420. MP_API void fp_zero(fp_int *a);
  421. MP_API void fp_clear(fp_int *a);
  422. /* uses ForceZero to clear sensitive memory */
  423. MP_API void fp_forcezero (fp_int * a);
  424. MP_API void fp_free(fp_int* a);
  425. /* zero/one/even/odd/neg/word ? */
  426. #define fp_iszero(a) (((a)->used == 0) ? FP_YES : FP_NO)
  427. #define fp_isone(a) \
  428. ((((a)->used == 1) && ((a)->dp[0] == 1) && ((a)->sign == FP_ZPOS)) \
  429. ? FP_YES : FP_NO)
  430. #define fp_iseven(a) \
  431. (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? FP_YES : FP_NO)
  432. #define fp_isodd(a) \
  433. (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? FP_YES : FP_NO)
  434. #define fp_isneg(a) (((a)->sign != FP_ZPOS) ? FP_YES : FP_NO)
  435. #define fp_setneg(a) ((a)->sign = FP_NEG)
  436. #define fp_isword(a, w) \
  437. (((((a)->used == 1) && ((a)->dp[0] == (w))) || \
  438. (((w) == 0) && ((a)->used == 0))) ? FP_YES : FP_NO)
  439. /* Number of bits used based on used field only. */
  440. #define fp_bitsused(a) ((a)->used * DIGIT_BIT)
  441. /* set to a small digit */
  442. void fp_set(fp_int *a, fp_digit b);
  443. int fp_set_int(fp_int *a, unsigned long b);
  444. /* check if a bit is set */
  445. int fp_is_bit_set(fp_int *a, fp_digit b);
  446. /* set the b bit to 1 */
  447. int fp_set_bit (fp_int * a, fp_digit b);
  448. /* copy from a to b */
  449. void fp_copy(const fp_int *a, fp_int *b);
  450. void fp_init_copy(fp_int *a, fp_int *b);
  451. /* clamp digits */
  452. #define fp_clamp(a) { while ((a)->used && (a)->dp[(a)->used-1] == 0) --((a)->used); (a)->sign = (a)->used ? (a)->sign : FP_ZPOS; }
  453. #define mp_clamp(a) fp_clamp(a)
  454. #define mp_grow(a,s) MP_OKAY
  455. /* negate and absolute */
  456. #define fp_neg(a, b) { fp_copy(a, b); (b)->sign ^= 1; fp_clamp(b); }
  457. #define fp_abs(a, b) { fp_copy(a, b); (b)->sign = 0; }
  458. /* right shift x digits */
  459. void fp_rshd(fp_int *a, int x);
  460. /* right shift x bits */
  461. void fp_rshb(fp_int *c, int x);
  462. /* left shift x digits */
  463. int fp_lshd(fp_int *a, int x);
  464. /* signed comparison */
  465. int fp_cmp(fp_int *a, fp_int *b);
  466. /* unsigned comparison */
  467. int fp_cmp_mag(fp_int *a, fp_int *b);
  468. /* power of 2 operations */
  469. void fp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d);
  470. void fp_mod_2d(fp_int *a, int b, fp_int *c);
  471. int fp_mul_2d(fp_int *a, int b, fp_int *c);
  472. void fp_2expt (fp_int *a, int b);
  473. int fp_mul_2(fp_int *a, fp_int *b);
  474. void fp_div_2(fp_int *a, fp_int *b);
  475. /* c = a / 2 (mod b) - constant time (a < b and positive) */
  476. int fp_div_2_mod_ct(fp_int *a, fp_int *b, fp_int *c);
  477. /* Counts the number of lsbs which are zero before the first zero bit */
  478. int fp_cnt_lsb(fp_int *a);
  479. /* c = a + b */
  480. int fp_add(fp_int *a, fp_int *b, fp_int *c);
  481. /* c = a - b */
  482. int fp_sub(fp_int *a, fp_int *b, fp_int *c);
  483. /* c = a * b */
  484. int fp_mul(fp_int *a, fp_int *b, fp_int *c);
  485. /* b = a*a */
  486. int fp_sqr(fp_int *a, fp_int *b);
  487. /* a/b => cb + d == a */
  488. int fp_div(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
  489. /* c = a mod b, 0 <= c < b */
  490. int fp_mod(fp_int *a, fp_int *b, fp_int *c);
  491. /* compare against a single digit */
  492. int fp_cmp_d(fp_int *a, fp_digit b);
  493. /* c = a + b */
  494. int fp_add_d(fp_int *a, fp_digit b, fp_int *c);
  495. /* c = a - b */
  496. int fp_sub_d(fp_int *a, fp_digit b, fp_int *c);
  497. /* c = a * b */
  498. int fp_mul_d(fp_int *a, fp_digit b, fp_int *c);
  499. /* a/b => cb + d == a */
  500. /*int fp_div_d(fp_int *a, fp_digit b, fp_int *c, fp_digit *d);*/
  501. /* c = a mod b, 0 <= c < b */
  502. /*int fp_mod_d(fp_int *a, fp_digit b, fp_digit *c);*/
  503. /* ---> number theory <--- */
  504. /* d = a + b (mod c) */
  505. /*int fp_addmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);*/
  506. /* d = a - b (mod c) */
  507. /*int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);*/
  508. /* d = a * b (mod c) */
  509. int fp_mulmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
  510. /* d = a - b (mod c) */
  511. int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
  512. /* d = a + b (mod c) */
  513. int fp_addmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
  514. /* d = a - b (mod c) - constant time (a < c and b < c) */
  515. int fp_submod_ct(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
  516. /* d = a + b (mod c) - constant time (a < c and b < c) */
  517. int fp_addmod_ct(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
  518. /* c = a * a (mod b) */
  519. int fp_sqrmod(fp_int *a, fp_int *b, fp_int *c);
  520. /* c = 1/a (mod b) */
  521. int fp_invmod(fp_int *a, fp_int *b, fp_int *c);
  522. int fp_invmod_mont_ct(fp_int *a, fp_int *b, fp_int *c, fp_digit mp);
  523. /* c = (a, b) */
  524. /*int fp_gcd(fp_int *a, fp_int *b, fp_int *c);*/
  525. /* c = [a, b] */
  526. /*int fp_lcm(fp_int *a, fp_int *b, fp_int *c);*/
  527. /* setups the montgomery reduction */
  528. int fp_montgomery_setup(fp_int *a, fp_digit *rho);
  529. /* computes a = B**n mod b without division or multiplication useful for
  530. * normalizing numbers in a Montgomery system.
  531. */
  532. int fp_montgomery_calc_normalization(fp_int *a, fp_int *b);
  533. /* computes x/R == x (mod N) via Montgomery Reduction */
  534. int fp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp);
  535. int fp_montgomery_reduce_ex(fp_int *a, fp_int *m, fp_digit mp, int ct);
  536. /* d = a**b (mod c) */
  537. int fp_exptmod(fp_int *G, fp_int *X, fp_int *P, fp_int *Y);
  538. int fp_exptmod_ex(fp_int *G, fp_int *X, int minDigits, fp_int *P, fp_int *Y);
  539. int fp_exptmod_nct(fp_int *G, fp_int *X, fp_int *P, fp_int *Y);
  540. #ifdef WC_RSA_NONBLOCK
  541. enum tfmExptModNbState {
  542. TFM_EXPTMOD_NB_INIT = 0,
  543. TFM_EXPTMOD_NB_MONT,
  544. TFM_EXPTMOD_NB_MONT_RED,
  545. TFM_EXPTMOD_NB_MONT_MUL,
  546. TFM_EXPTMOD_NB_MONT_MOD,
  547. TFM_EXPTMOD_NB_MONT_MODCHK,
  548. TFM_EXPTMOD_NB_NEXT,
  549. TFM_EXPTMOD_NB_MUL,
  550. TFM_EXPTMOD_NB_MUL_RED,
  551. TFM_EXPTMOD_NB_SQR,
  552. TFM_EXPTMOD_NB_SQR_RED,
  553. TFM_EXPTMOD_NB_RED,
  554. TFM_EXPTMOD_NB_COUNT /* last item for total state count only */
  555. };
  556. typedef struct {
  557. #ifndef WC_NO_CACHE_RESISTANT
  558. fp_int R[3];
  559. #else
  560. fp_int R[2];
  561. #endif
  562. fp_digit buf;
  563. fp_digit mp;
  564. int bitcnt;
  565. int digidx;
  566. int y;
  567. int state; /* tfmExptModNbState */
  568. #ifdef WC_RSA_NONBLOCK_TIME
  569. word32 maxBlockInst; /* maximum instructions to block */
  570. word32 totalInst; /* tracks total instructions */
  571. #endif
  572. } exptModNb_t;
  573. #ifdef WC_RSA_NONBLOCK_TIME
  574. enum {
  575. TFM_EXPTMOD_NB_STOP = 0, /* stop and return FP_WOULDBLOCK */
  576. TFM_EXPTMOD_NB_CONTINUE = 1, /* keep blocking */
  577. };
  578. #endif
  579. /* non-blocking version of timing resistant fp_exptmod function */
  580. /* supports cache resistance */
  581. int fp_exptmod_nb(exptModNb_t* nb, fp_int* G, fp_int* X, fp_int* P, fp_int* Y);
  582. #endif /* WC_RSA_NONBLOCK */
  583. /* primality stuff */
  584. /* perform a Miller-Rabin test of a to the base b and store result in "result" */
  585. /*void fp_prime_miller_rabin (fp_int * a, fp_int * b, int *result);*/
  586. #define FP_PRIME_SIZE 256
  587. /* 256 trial divisions + 8 Miller-Rabins, returns FP_YES if probable prime */
  588. /*int fp_isprime(fp_int *a);*/
  589. /* extended version of fp_isprime, do 't' Miller-Rabins instead of only 8 */
  590. /*int fp_isprime_ex(fp_int *a, int t, int* result);*/
  591. /* Primality generation flags */
  592. /*#define TFM_PRIME_BBS 0x0001 */ /* BBS style prime */
  593. /*#define TFM_PRIME_SAFE 0x0002 */ /* Safe prime (p-1)/2 == prime */
  594. /*#define TFM_PRIME_2MSB_OFF 0x0004 */ /* force 2nd MSB to 0 */
  595. /*#define TFM_PRIME_2MSB_ON 0x0008 */ /* force 2nd MSB to 1 */
  596. /* callback for fp_prime_random, should fill dst with random bytes and return how many read [up to len] */
  597. /*typedef int tfm_prime_callback(unsigned char *dst, int len, void *dat);*/
  598. /*#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)*/
  599. /*int fp_prime_random_ex(fp_int *a, int t, int size, int flags, tfm_prime_callback cb, void *dat);*/
  600. /* radix conversions */
  601. int fp_count_bits(const fp_int *a);
  602. int fp_leading_bit(fp_int *a);
  603. int fp_unsigned_bin_size(const fp_int *a);
  604. int fp_read_unsigned_bin(fp_int *a, const unsigned char *b, int c);
  605. int fp_to_unsigned_bin(fp_int *a, unsigned char *b);
  606. int fp_to_unsigned_bin_len(fp_int *a, unsigned char *b, int c);
  607. int fp_to_unsigned_bin_at_pos(int x, fp_int *t, unsigned char *b);
  608. /*int fp_read_radix(fp_int *a, char *str, int radix);*/
  609. /*int fp_toradix(fp_int *a, char *str, int radix);*/
  610. /*int fp_toradix_n(fp_int * a, char *str, int radix, int maxlen);*/
  611. /* VARIOUS LOW LEVEL STUFFS */
  612. int s_fp_add(fp_int *a, fp_int *b, fp_int *c);
  613. void s_fp_sub(fp_int *a, fp_int *b, fp_int *c);
  614. int fp_mul_comba(fp_int *a, fp_int *b, fp_int *c);
  615. int fp_mul_comba_small(fp_int *a, fp_int *b, fp_int *c);
  616. int fp_mul_comba3(fp_int *a, fp_int *b, fp_int *c);
  617. int fp_mul_comba4(fp_int *a, fp_int *b, fp_int *c);
  618. int fp_mul_comba6(fp_int *a, fp_int *b, fp_int *c);
  619. int fp_mul_comba7(fp_int *a, fp_int *b, fp_int *c);
  620. int fp_mul_comba8(fp_int *a, fp_int *b, fp_int *c);
  621. int fp_mul_comba9(fp_int *a, fp_int *b, fp_int *c);
  622. int fp_mul_comba12(fp_int *a, fp_int *b, fp_int *c);
  623. int fp_mul_comba17(fp_int *a, fp_int *b, fp_int *c);
  624. int fp_mul_comba20(fp_int *a, fp_int *b, fp_int *c);
  625. int fp_mul_comba24(fp_int *a, fp_int *b, fp_int *c);
  626. int fp_mul_comba28(fp_int *a, fp_int *b, fp_int *c);
  627. int fp_mul_comba32(fp_int *a, fp_int *b, fp_int *c);
  628. int fp_mul_comba48(fp_int *a, fp_int *b, fp_int *c);
  629. int fp_mul_comba64(fp_int *a, fp_int *b, fp_int *c);
  630. int fp_sqr_comba(fp_int *a, fp_int *b);
  631. int fp_sqr_comba_small(fp_int *a, fp_int *b);
  632. int fp_sqr_comba3(fp_int *a, fp_int *b);
  633. int fp_sqr_comba4(fp_int *a, fp_int *b);
  634. int fp_sqr_comba6(fp_int *a, fp_int *b);
  635. int fp_sqr_comba7(fp_int *a, fp_int *b);
  636. int fp_sqr_comba8(fp_int *a, fp_int *b);
  637. int fp_sqr_comba9(fp_int *a, fp_int *b);
  638. int fp_sqr_comba12(fp_int *a, fp_int *b);
  639. int fp_sqr_comba17(fp_int *a, fp_int *b);
  640. int fp_sqr_comba20(fp_int *a, fp_int *b);
  641. int fp_sqr_comba24(fp_int *a, fp_int *b);
  642. int fp_sqr_comba28(fp_int *a, fp_int *b);
  643. int fp_sqr_comba32(fp_int *a, fp_int *b);
  644. int fp_sqr_comba48(fp_int *a, fp_int *b);
  645. int fp_sqr_comba64(fp_int *a, fp_int *b);
  646. /**
  647. * Used by wolfSSL
  648. */
  649. /* Constants */
  650. #define MP_LT FP_LT /* less than */
  651. #define MP_EQ FP_EQ /* equal to */
  652. #define MP_GT FP_GT /* greater than */
  653. #define MP_VAL FP_VAL /* invalid */
  654. #define MP_MEM FP_MEM /* memory error */
  655. #define MP_NOT_INF FP_NOT_INF /* point not at infinity */
  656. #define MP_OKAY FP_OKAY /* ok result */
  657. #define MP_NO FP_NO /* yes/no result */
  658. #define MP_YES FP_YES /* yes/no result */
  659. #define MP_ZPOS FP_ZPOS
  660. #define MP_NEG FP_NEG
  661. #define MP_MASK FP_MASK
  662. /* Prototypes */
  663. #define mp_zero(a) fp_zero(a)
  664. #define mp_isone(a) fp_isone(a)
  665. #define mp_iseven(a) fp_iseven(a)
  666. #define mp_isneg(a) fp_isneg(a)
  667. #define mp_setneg(a) fp_setneg(a)
  668. #define mp_isword(a, w) fp_isword(a, w)
  669. #define mp_bitsused(a) fp_bitsused(a)
  670. #define MP_RADIX_BIN 2
  671. #define MP_RADIX_OCT 8
  672. #define MP_RADIX_DEC 10
  673. #define MP_RADIX_HEX 16
  674. #define MP_RADIX_MAX 64
  675. #define mp_tobinary(M, S) mp_toradix((M), (S), MP_RADIX_BIN)
  676. #define mp_tooctal(M, S) mp_toradix((M), (S), MP_RADIX_OCT)
  677. #define mp_todecimal(M, S) mp_toradix((M), (S), MP_RADIX_DEC)
  678. #define mp_tohex(M, S) mp_toradix((M), (S), MP_RADIX_HEX)
  679. MP_API int mp_init (mp_int * a);
  680. MP_API int mp_init_copy(fp_int * a, fp_int * b);
  681. MP_API void mp_clear (mp_int * a);
  682. MP_API void mp_free (mp_int * a);
  683. MP_API void mp_forcezero (mp_int * a);
  684. MP_API int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e,
  685. mp_int* f);
  686. MP_API int mp_add (mp_int * a, mp_int * b, mp_int * c);
  687. MP_API int mp_sub (mp_int * a, mp_int * b, mp_int * c);
  688. MP_API int mp_add_d (mp_int * a, mp_digit b, mp_int * c);
  689. MP_API int mp_mul (mp_int * a, mp_int * b, mp_int * c);
  690. MP_API int mp_mul_d (mp_int * a, mp_digit b, mp_int * c);
  691. MP_API int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d);
  692. MP_API int mp_submod (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
  693. MP_API int mp_addmod (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
  694. MP_API int mp_submod_ct (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
  695. MP_API int mp_addmod_ct (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
  696. MP_API int mp_mod(mp_int *a, mp_int *b, mp_int *c);
  697. MP_API int mp_invmod(mp_int *a, mp_int *b, mp_int *c);
  698. MP_API int mp_invmod_mont_ct(mp_int *a, mp_int *b, mp_int *c, fp_digit mp);
  699. MP_API int mp_exptmod (mp_int * g, mp_int * x, mp_int * p, mp_int * y);
  700. MP_API int mp_exptmod_ex (mp_int * g, mp_int * x, int minDigits, mp_int * p,
  701. mp_int * y);
  702. MP_API int mp_exptmod_nct (mp_int * g, mp_int * x, mp_int * p, mp_int * y);
  703. MP_API int mp_mul_2d(mp_int *a, int b, mp_int *c);
  704. MP_API int mp_2expt(mp_int* a, int b);
  705. MP_API int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d);
  706. MP_API int mp_cmp(mp_int *a, mp_int *b);
  707. #define mp_cmp_ct(a, b, n) mp_cmp(a, b)
  708. MP_API int mp_cmp_d(mp_int *a, mp_digit b);
  709. MP_API int mp_unsigned_bin_size(const mp_int * a);
  710. MP_API int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c);
  711. MP_API int mp_to_unsigned_bin_at_pos(int x, mp_int *t, unsigned char *b);
  712. MP_API int mp_to_unsigned_bin (mp_int * a, unsigned char *b);
  713. #define mp_to_unsigned_bin_len_ct mp_to_unsigned_bin_len
  714. MP_API int mp_to_unsigned_bin_len(mp_int * a, unsigned char *b, int c);
  715. MP_API int mp_sub_d(fp_int *a, fp_digit b, fp_int *c);
  716. MP_API int mp_copy(const fp_int* a, fp_int* b);
  717. MP_API int mp_isodd(const mp_int* a);
  718. MP_API int mp_iszero(const mp_int* a);
  719. MP_API int mp_count_bits(const mp_int *a);
  720. MP_API int mp_leading_bit(mp_int *a);
  721. MP_API int mp_set_int(mp_int *a, unsigned long b);
  722. MP_API int mp_is_bit_set (mp_int * a, mp_digit b);
  723. MP_API int mp_set_bit (mp_int * a, mp_digit b);
  724. MP_API void mp_rshb(mp_int *a, int x);
  725. MP_API void mp_rshd(mp_int *a, int x);
  726. MP_API int mp_toradix (mp_int *a, char *str, int radix);
  727. MP_API int mp_radix_size (mp_int * a, int radix, int *size);
  728. #ifdef WOLFSSL_DEBUG_MATH
  729. MP_API void mp_dump(const char* desc, mp_int* a, byte verbose);
  730. #else
  731. #define mp_dump(desc, a, verbose) WC_DO_NOTHING
  732. #endif
  733. #if defined(OPENSSL_EXTRA) || !defined(NO_DSA) || defined(HAVE_ECC)
  734. MP_API int mp_read_radix(mp_int* a, const char* str, int radix);
  735. #endif
  736. #define mp_montgomery_reduce_ct(a, m, mp) \
  737. mp_montgomery_reduce_ex(a, m, mp, 1)
  738. MP_API int mp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp);
  739. MP_API int mp_montgomery_reduce_ex(fp_int *a, fp_int *m, fp_digit mp, int ct);
  740. MP_API int mp_montgomery_setup(fp_int *a, fp_digit *rho);
  741. #ifdef HAVE_ECC
  742. MP_API int mp_sqr(fp_int *a, fp_int *b);
  743. MP_API int mp_div_2(fp_int * a, fp_int * b);
  744. MP_API int mp_div_2_mod_ct(mp_int *a, mp_int *b, mp_int *c);
  745. #endif
  746. #if defined(HAVE_ECC) || !defined(NO_RSA) || !defined(NO_DSA) || \
  747. defined(WOLFSSL_KEY_GEN)
  748. MP_API int mp_set(fp_int *a, fp_digit b);
  749. #endif
  750. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || !defined(NO_RSA) || \
  751. !defined(NO_DSA) || !defined(NO_DH)
  752. MP_API int mp_sqrmod(mp_int* a, mp_int* b, mp_int* c);
  753. MP_API int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
  754. #endif
  755. #if !defined(NO_DH) || !defined(NO_DSA) || !defined(NO_RSA) || defined(WOLFSSL_KEY_GEN)
  756. MP_API int mp_prime_is_prime(mp_int* a, int t, int* result);
  757. MP_API int mp_prime_is_prime_ex(mp_int* a, int t, int* result, WC_RNG* rng);
  758. #endif /* !NO_DH || !NO_DSA || !NO_RSA || WOLFSSL_KEY_GEN */
  759. #ifdef WOLFSSL_KEY_GEN
  760. MP_API int mp_gcd(fp_int *a, fp_int *b, fp_int *c);
  761. MP_API int mp_lcm(fp_int *a, fp_int *b, fp_int *c);
  762. MP_API int mp_rand_prime(mp_int* a, int len, WC_RNG* rng, void* heap);
  763. MP_API int mp_exch(mp_int *a, mp_int *b);
  764. #endif /* WOLFSSL_KEY_GEN */
  765. MP_API int mp_cond_swap_ct_ex(mp_int* a, mp_int* b, int c, int m, mp_int* t);
  766. MP_API int mp_cond_swap_ct(mp_int* a, mp_int* b, int c, int m);
  767. MP_API int mp_cnt_lsb(fp_int *a);
  768. MP_API int mp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d);
  769. MP_API int mp_mod_2d(fp_int *a, int b, fp_int *c);
  770. MP_API int mp_mod_d(fp_int* a, fp_digit b, fp_digit* c);
  771. MP_API int mp_lshd (mp_int * a, int b);
  772. MP_API int mp_abs(mp_int* a, mp_int* b);
  773. WOLFSSL_API word32 CheckRunTimeFastMath(void);
  774. #ifdef WOLFSSL_CHECK_MEM_ZERO
  775. void mp_memzero_add(const char* name, mp_int* a);
  776. void mp_memzero_check(mp_int* a);
  777. #endif
  778. /* If user uses RSA, DH, DSA, or ECC math lib directly then fast math FP_SIZE
  779. must match, return 1 if a match otherwise 0 */
  780. #define CheckFastMathSettings() (FP_SIZE == CheckRunTimeFastMath())
  781. #ifdef __cplusplus
  782. }
  783. #endif
  784. #endif /* WOLF_CRYPT_TFM_H */