ctc_tfm.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /* ctc_tfm.h
  2. *
  3. * Copyright (C) 2006-2011 Sawtooth Consulting Ltd.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  7. * CyaSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * CyaSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 Moisés Guimarães (moises.guimaraes@phoebus.com.br)
  27. * to fit CyaSSL's needs.
  28. */
  29. #ifndef CTAO_CRYPT_TFM_H
  30. #define CTAO_CRYPT_TFM_H
  31. #include "ctc_types.h"
  32. #ifndef CHAR_BIT
  33. #include <limits.h>
  34. #endif
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. #ifndef MIN
  39. #define MIN(x,y) ((x)<(y)?(x):(y))
  40. #endif
  41. #ifndef MAX
  42. #define MAX(x,y) ((x)>(y)?(x):(y))
  43. #endif
  44. /* autodetect x86-64 and make sure we are using 64-bit digits with x86-64 asm */
  45. #if defined(__x86_64__)
  46. #if defined(TFM_X86) || defined(TFM_SSE2) || defined(TFM_ARM)
  47. #error x86-64 detected, x86-32/SSE2/ARM optimizations are not valid!
  48. #endif
  49. #if !defined(TFM_X86_64) && !defined(TFM_NO_ASM)
  50. #define TFM_X86_64
  51. #endif
  52. #endif
  53. #if defined(TFM_X86_64)
  54. #if !defined(FP_64BIT)
  55. #define FP_64BIT
  56. #endif
  57. #endif
  58. /* try to detect x86-32 */
  59. #if defined(__i386__) && !defined(TFM_SSE2)
  60. #if defined(TFM_X86_64) || defined(TFM_ARM)
  61. #error x86-32 detected, x86-64/ARM optimizations are not valid!
  62. #endif
  63. #if !defined(TFM_X86) && !defined(TFM_NO_ASM)
  64. #define TFM_X86
  65. #endif
  66. #endif
  67. /* make sure we're 32-bit for x86-32/sse/arm/ppc32 */
  68. #if (defined(TFM_X86) || defined(TFM_SSE2) || defined(TFM_ARM) || defined(TFM_PPC32)) && defined(FP_64BIT)
  69. #warning x86-32, SSE2 and ARM, PPC32 optimizations require 32-bit digits (undefining)
  70. #undef FP_64BIT
  71. #endif
  72. /* multi asms? */
  73. #ifdef TFM_X86
  74. #define TFM_ASM
  75. #endif
  76. #ifdef TFM_X86_64
  77. #ifdef TFM_ASM
  78. #error TFM_ASM already defined!
  79. #endif
  80. #define TFM_ASM
  81. #endif
  82. #ifdef TFM_SSE2
  83. #ifdef TFM_ASM
  84. #error TFM_ASM already defined!
  85. #endif
  86. #define TFM_ASM
  87. #endif
  88. #ifdef TFM_ARM
  89. #ifdef TFM_ASM
  90. #error TFM_ASM already defined!
  91. #endif
  92. #define TFM_ASM
  93. #endif
  94. #ifdef TFM_PPC32
  95. #ifdef TFM_ASM
  96. #error TFM_ASM already defined!
  97. #endif
  98. #define TFM_ASM
  99. #endif
  100. #ifdef TFM_PPC64
  101. #ifdef TFM_ASM
  102. #error TFM_ASM already defined!
  103. #endif
  104. #define TFM_ASM
  105. #endif
  106. #ifdef TFM_AVR32
  107. #ifdef TFM_ASM
  108. #error TFM_ASM already defined!
  109. #endif
  110. #define TFM_ASM
  111. #endif
  112. /* we want no asm? */
  113. #ifdef TFM_NO_ASM
  114. #undef TFM_X86
  115. #undef TFM_X86_64
  116. #undef TFM_SSE2
  117. #undef TFM_ARM
  118. #undef TFM_PPC32
  119. #undef TFM_PPC64
  120. #undef TFM_AVR32
  121. #undef TFM_ASM
  122. #endif
  123. /* ECC helpers */
  124. #ifdef TFM_ECC192
  125. #ifdef FP_64BIT
  126. #define TFM_MUL3
  127. #define TFM_SQR3
  128. #else
  129. #define TFM_MUL6
  130. #define TFM_SQR6
  131. #endif
  132. #endif
  133. #ifdef TFM_ECC224
  134. #ifdef FP_64BIT
  135. #define TFM_MUL4
  136. #define TFM_SQR4
  137. #else
  138. #define TFM_MUL7
  139. #define TFM_SQR7
  140. #endif
  141. #endif
  142. #ifdef TFM_ECC256
  143. #ifdef FP_64BIT
  144. #define TFM_MUL4
  145. #define TFM_SQR4
  146. #else
  147. #define TFM_MUL8
  148. #define TFM_SQR8
  149. #endif
  150. #endif
  151. #ifdef TFM_ECC384
  152. #ifdef FP_64BIT
  153. #define TFM_MUL6
  154. #define TFM_SQR6
  155. #else
  156. #define TFM_MUL12
  157. #define TFM_SQR12
  158. #endif
  159. #endif
  160. #ifdef TFM_ECC521
  161. #ifdef FP_64BIT
  162. #define TFM_MUL9
  163. #define TFM_SQR9
  164. #else
  165. #define TFM_MUL17
  166. #define TFM_SQR17
  167. #endif
  168. #endif
  169. /* some default configurations.
  170. */
  171. #if defined(FP_64BIT)
  172. /* for GCC only on supported platforms */
  173. #ifndef CRYPT
  174. typedef unsigned long ulong64;
  175. #endif
  176. typedef ulong64 fp_digit;
  177. typedef unsigned long fp_word __attribute__ ((mode(TI)));
  178. #else
  179. /* this is to make porting into LibTomCrypt easier :-) */
  180. #ifndef CRYPT
  181. #if defined(_MSC_VER) || defined(__BORLANDC__)
  182. typedef unsigned __int64 ulong64;
  183. typedef signed __int64 long64;
  184. #else
  185. typedef unsigned long long ulong64;
  186. typedef signed long long long64;
  187. #endif
  188. #endif
  189. typedef unsigned long fp_digit;
  190. typedef ulong64 fp_word;
  191. #endif
  192. /* # of digits this is */
  193. #define DIGIT_BIT (int)((CHAR_BIT) * sizeof(fp_digit))
  194. /* Max size of any number in bits. Basically the largest size you will be
  195. * multiplying should be half [or smaller] of FP_MAX_SIZE-four_digit
  196. *
  197. * It defaults to 4096-bits [allowing multiplications upto 2048x2048 bits ]
  198. */
  199. #define FP_MAX_BITS 4096
  200. #define FP_MAX_SIZE (FP_MAX_BITS+(8*DIGIT_BIT))
  201. /* will this lib work? */
  202. #if (CHAR_BIT & 7)
  203. #error CHAR_BIT must be a multiple of eight.
  204. #endif
  205. #if FP_MAX_BITS % CHAR_BIT
  206. #error FP_MAX_BITS must be a multiple of CHAR_BIT
  207. #endif
  208. #define FP_MASK (fp_digit)(-1)
  209. #define FP_SIZE (FP_MAX_SIZE/DIGIT_BIT)
  210. /* signs */
  211. #define FP_ZPOS 0
  212. #define FP_NEG 1
  213. /* return codes */
  214. #define FP_OKAY 0
  215. #define FP_VAL 1
  216. #define FP_MEM 2
  217. /* equalities */
  218. #define FP_LT -1 /* less than */
  219. #define FP_EQ 0 /* equal to */
  220. #define FP_GT 1 /* greater than */
  221. /* replies */
  222. #define FP_YES 1 /* yes response */
  223. #define FP_NO 0 /* no response */
  224. /* a FP type */
  225. typedef struct {
  226. fp_digit dp[FP_SIZE];
  227. int used,
  228. sign;
  229. } fp_int;
  230. /* externally define this symbol to ignore the default settings, useful for changing the build from the make process */
  231. #ifndef TFM_ALREADY_SET
  232. /* do we want the large set of small multiplications ?
  233. Enable these if you are going to be doing a lot of small (<= 16 digit) multiplications say in ECC
  234. Or if you're on a 64-bit machine doing RSA as a 1024-bit integer == 16 digits ;-)
  235. */
  236. /* need to refactor the function */
  237. /*#define TFM_SMALL_SET */
  238. /* do we want huge code
  239. Enable these if you are doing 20, 24, 28, 32, 48, 64 digit multiplications (useful for RSA)
  240. Less important on 64-bit machines as 32 digits == 2048 bits
  241. */
  242. #if 0
  243. #define TFM_MUL3
  244. #define TFM_MUL4
  245. #define TFM_MUL6
  246. #define TFM_MUL7
  247. #define TFM_MUL8
  248. #define TFM_MUL9
  249. #define TFM_MUL12
  250. #define TFM_MUL17
  251. #endif
  252. #ifdef TFM_SMALL_SET
  253. #define TFM_MUL20
  254. #define TFM_MUL24
  255. #define TFM_MUL28
  256. #define TFM_MUL32
  257. #if (FP_MAX_BITS >= 6144) && defined(FP_64BIT)
  258. #define TFM_MUL48
  259. #endif
  260. #if (FP_MAX_BITS >= 8192) && defined(FP_64BIT)
  261. #define TFM_MUL64
  262. #endif
  263. #endif
  264. #if 0
  265. #define TFM_SQR3
  266. #define TFM_SQR4
  267. #define TFM_SQR6
  268. #define TFM_SQR7
  269. #define TFM_SQR8
  270. #define TFM_SQR9
  271. #define TFM_SQR12
  272. #define TFM_SQR17
  273. #endif
  274. #ifdef TFM_SMALL_SET
  275. #define TFM_SQR20
  276. #define TFM_SQR24
  277. #define TFM_SQR28
  278. #define TFM_SQR32
  279. #define TFM_SQR48
  280. #define TFM_SQR64
  281. #endif
  282. /* do we want some overflow checks
  283. Not required if you make sure your numbers are within range (e.g. by default a modulus for fp_exptmod() can only be upto 2048 bits long)
  284. */
  285. /* #define TFM_CHECK */
  286. /* Is the target a P4 Prescott
  287. */
  288. /* #define TFM_PRESCOTT */
  289. /* Do we want timing resistant fp_exptmod() ?
  290. * This makes it slower but also timing invariant with respect to the exponent
  291. */
  292. /* #define TFM_TIMING_RESISTANT */
  293. #endif /* TFM_ALREADY_SET */
  294. /* functions */
  295. /* returns a TFM ident string useful for debugging... */
  296. /*const char *fp_ident(void);*/
  297. /* initialize [or zero] an fp int */
  298. #define fp_init(a) (void)XMEMSET((a), 0, sizeof(fp_int))
  299. #define fp_zero(a) fp_init(a)
  300. /* zero/even/odd ? */
  301. #define fp_iszero(a) (((a)->used == 0) ? FP_YES : FP_NO)
  302. #define fp_iseven(a) (((a)->used >= 0 && (((a)->dp[0] & 1) == 0)) ? FP_YES : FP_NO)
  303. #define fp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? FP_YES : FP_NO)
  304. /* set to a small digit */
  305. void fp_set(fp_int *a, fp_digit b);
  306. /* copy from a to b */
  307. #define fp_copy(a, b) (void)(((a) != (b)) ? (XMEMCPY((b), (a), sizeof(fp_int))) : (void)0)
  308. #define fp_init_copy(a, b) fp_copy(b, a)
  309. /* clamp digits */
  310. #define fp_clamp(a) { while ((a)->used && (a)->dp[(a)->used-1] == 0) --((a)->used); (a)->sign = (a)->used ? (a)->sign : FP_ZPOS; }
  311. /* negate and absolute */
  312. #define fp_neg(a, b) { fp_copy(a, b); (b)->sign ^= 1; fp_clamp(b); }
  313. #define fp_abs(a, b) { fp_copy(a, b); (b)->sign = 0; }
  314. /* right shift x digits */
  315. void fp_rshd(fp_int *a, int x);
  316. /* left shift x digits */
  317. void fp_lshd(fp_int *a, int x);
  318. /* signed comparison */
  319. int fp_cmp(fp_int *a, fp_int *b);
  320. /* unsigned comparison */
  321. int fp_cmp_mag(fp_int *a, fp_int *b);
  322. /* power of 2 operations */
  323. void fp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d);
  324. void fp_mod_2d(fp_int *a, int b, fp_int *c);
  325. void fp_mul_2d(fp_int *a, int b, fp_int *c);
  326. void fp_2expt (fp_int *a, int b);
  327. void fp_mul_2(fp_int *a, fp_int *c);
  328. void fp_div_2(fp_int *a, fp_int *c);
  329. /* Counts the number of lsbs which are zero before the first zero bit */
  330. /*int fp_cnt_lsb(fp_int *a);*/
  331. /* c = a + b */
  332. void fp_add(fp_int *a, fp_int *b, fp_int *c);
  333. /* c = a - b */
  334. void fp_sub(fp_int *a, fp_int *b, fp_int *c);
  335. /* c = a * b */
  336. void fp_mul(fp_int *a, fp_int *b, fp_int *c);
  337. /* b = a*a */
  338. void fp_sqr(fp_int *a, fp_int *b);
  339. /* a/b => cb + d == a */
  340. int fp_div(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
  341. /* c = a mod b, 0 <= c < b */
  342. int fp_mod(fp_int *a, fp_int *b, fp_int *c);
  343. /* compare against a single digit */
  344. int fp_cmp_d(fp_int *a, fp_digit b);
  345. /* c = a + b */
  346. void fp_add_d(fp_int *a, fp_digit b, fp_int *c);
  347. /* c = a - b */
  348. /*void fp_sub_d(fp_int *a, fp_digit b, fp_int *c);*/
  349. /* c = a * b */
  350. void fp_mul_d(fp_int *a, fp_digit b, fp_int *c);
  351. /* a/b => cb + d == a */
  352. /*int fp_div_d(fp_int *a, fp_digit b, fp_int *c, fp_digit *d);*/
  353. /* c = a mod b, 0 <= c < b */
  354. /*int fp_mod_d(fp_int *a, fp_digit b, fp_digit *c);*/
  355. /* ---> number theory <--- */
  356. /* d = a + b (mod c) */
  357. /*int fp_addmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);*/
  358. /* d = a - b (mod c) */
  359. /*int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);*/
  360. /* d = a * b (mod c) */
  361. int fp_mulmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
  362. /* c = a * a (mod b) */
  363. int fp_sqrmod(fp_int *a, fp_int *b, fp_int *c);
  364. /* c = 1/a (mod b) */
  365. int fp_invmod(fp_int *a, fp_int *b, fp_int *c);
  366. /* c = (a, b) */
  367. /*void fp_gcd(fp_int *a, fp_int *b, fp_int *c);*/
  368. /* c = [a, b] */
  369. /*void fp_lcm(fp_int *a, fp_int *b, fp_int *c);*/
  370. /* setups the montgomery reduction */
  371. int fp_montgomery_setup(fp_int *a, fp_digit *mp);
  372. /* computes a = B**n mod b without division or multiplication useful for
  373. * normalizing numbers in a Montgomery system.
  374. */
  375. void fp_montgomery_calc_normalization(fp_int *a, fp_int *b);
  376. /* computes x/R == x (mod N) via Montgomery Reduction */
  377. void fp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp);
  378. /* d = a**b (mod c) */
  379. int fp_exptmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
  380. /* primality stuff */
  381. /* perform a Miller-Rabin test of a to the base b and store result in "result" */
  382. /*void fp_prime_miller_rabin (fp_int * a, fp_int * b, int *result);*/
  383. /* 256 trial divisions + 8 Miller-Rabins, returns FP_YES if probable prime */
  384. /*int fp_isprime(fp_int *a);*/
  385. /* Primality generation flags */
  386. /*#define TFM_PRIME_BBS 0x0001 */ /* BBS style prime */
  387. /*#define TFM_PRIME_SAFE 0x0002 */ /* Safe prime (p-1)/2 == prime */
  388. /*#define TFM_PRIME_2MSB_OFF 0x0004 */ /* force 2nd MSB to 0 */
  389. /*#define TFM_PRIME_2MSB_ON 0x0008 */ /* force 2nd MSB to 1 */
  390. /* callback for fp_prime_random, should fill dst with random bytes and return how many read [upto len] */
  391. /*typedef int tfm_prime_callback(unsigned char *dst, int len, void *dat);*/
  392. /*#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)*/
  393. /*int fp_prime_random_ex(fp_int *a, int t, int size, int flags, tfm_prime_callback cb, void *dat);*/
  394. /* radix conersions */
  395. int fp_count_bits(fp_int *a);
  396. int fp_unsigned_bin_size(fp_int *a);
  397. void fp_read_unsigned_bin(fp_int *a, unsigned char *b, int c);
  398. void fp_to_unsigned_bin(fp_int *a, unsigned char *b);
  399. /*int fp_signed_bin_size(fp_int *a);*/
  400. /*void fp_read_signed_bin(fp_int *a, unsigned char *b, int c);*/
  401. /*void fp_to_signed_bin(fp_int *a, unsigned char *b);*/
  402. /*int fp_read_radix(fp_int *a, char *str, int radix);*/
  403. /*int fp_toradix(fp_int *a, char *str, int radix);*/
  404. /*int fp_toradix_n(fp_int * a, char *str, int radix, int maxlen);*/
  405. /* VARIOUS LOW LEVEL STUFFS */
  406. void s_fp_add(fp_int *a, fp_int *b, fp_int *c);
  407. void s_fp_sub(fp_int *a, fp_int *b, fp_int *c);
  408. void fp_reverse(unsigned char *s, int len);
  409. void fp_mul_comba(fp_int *A, fp_int *B, fp_int *C);
  410. #ifdef TFM_SMALL_SET
  411. void fp_mul_comba_small(fp_int *A, fp_int *B, fp_int *C);
  412. #endif
  413. #ifdef TFM_MUL3
  414. void fp_mul_comba3(fp_int *A, fp_int *B, fp_int *C);
  415. #endif
  416. #ifdef TFM_MUL4
  417. void fp_mul_comba4(fp_int *A, fp_int *B, fp_int *C);
  418. #endif
  419. #ifdef TFM_MUL6
  420. void fp_mul_comba6(fp_int *A, fp_int *B, fp_int *C);
  421. #endif
  422. #ifdef TFM_MUL7
  423. void fp_mul_comba7(fp_int *A, fp_int *B, fp_int *C);
  424. #endif
  425. #ifdef TFM_MUL8
  426. void fp_mul_comba8(fp_int *A, fp_int *B, fp_int *C);
  427. #endif
  428. #ifdef TFM_MUL9
  429. void fp_mul_comba9(fp_int *A, fp_int *B, fp_int *C);
  430. #endif
  431. #ifdef TFM_MUL12
  432. void fp_mul_comba12(fp_int *A, fp_int *B, fp_int *C);
  433. #endif
  434. #ifdef TFM_MUL17
  435. void fp_mul_comba17(fp_int *A, fp_int *B, fp_int *C);
  436. #endif
  437. #ifdef TFM_MUL20
  438. void fp_mul_comba20(fp_int *A, fp_int *B, fp_int *C);
  439. #endif
  440. #ifdef TFM_MUL24
  441. void fp_mul_comba24(fp_int *A, fp_int *B, fp_int *C);
  442. #endif
  443. #ifdef TFM_MUL28
  444. void fp_mul_comba28(fp_int *A, fp_int *B, fp_int *C);
  445. #endif
  446. #ifdef TFM_MUL32
  447. void fp_mul_comba32(fp_int *A, fp_int *B, fp_int *C);
  448. #endif
  449. #ifdef TFM_MUL48
  450. void fp_mul_comba48(fp_int *A, fp_int *B, fp_int *C);
  451. #endif
  452. #ifdef TFM_MUL64
  453. void fp_mul_comba64(fp_int *A, fp_int *B, fp_int *C);
  454. #endif
  455. void fp_sqr_comba(fp_int *A, fp_int *B);
  456. #ifdef TFM_SMALL_SET
  457. void fp_sqr_comba_small(fp_int *A, fp_int *B);
  458. #endif
  459. #ifdef TFM_SQR3
  460. void fp_sqr_comba3(fp_int *A, fp_int *B);
  461. #endif
  462. #ifdef TFM_SQR4
  463. void fp_sqr_comba4(fp_int *A, fp_int *B);
  464. #endif
  465. #ifdef TFM_SQR6
  466. void fp_sqr_comba6(fp_int *A, fp_int *B);
  467. #endif
  468. #ifdef TFM_SQR7
  469. void fp_sqr_comba7(fp_int *A, fp_int *B);
  470. #endif
  471. #ifdef TFM_SQR8
  472. void fp_sqr_comba8(fp_int *A, fp_int *B);
  473. #endif
  474. #ifdef TFM_SQR9
  475. void fp_sqr_comba9(fp_int *A, fp_int *B);
  476. #endif
  477. #ifdef TFM_SQR12
  478. void fp_sqr_comba12(fp_int *A, fp_int *B);
  479. #endif
  480. #ifdef TFM_SQR17
  481. void fp_sqr_comba17(fp_int *A, fp_int *B);
  482. #endif
  483. #ifdef TFM_SQR20
  484. void fp_sqr_comba20(fp_int *A, fp_int *B);
  485. #endif
  486. #ifdef TFM_SQR24
  487. void fp_sqr_comba24(fp_int *A, fp_int *B);
  488. #endif
  489. #ifdef TFM_SQR28
  490. void fp_sqr_comba28(fp_int *A, fp_int *B);
  491. #endif
  492. #ifdef TFM_SQR32
  493. void fp_sqr_comba32(fp_int *A, fp_int *B);
  494. #endif
  495. #ifdef TFM_SQR48
  496. void fp_sqr_comba48(fp_int *A, fp_int *B);
  497. #endif
  498. #ifdef TFM_SQR64
  499. void fp_sqr_comba64(fp_int *A, fp_int *B);
  500. #endif
  501. /*extern const char *fp_s_rmap;*/
  502. /**
  503. * Used by CyaSSL
  504. */
  505. /* Types */
  506. typedef fp_digit mp_digit;
  507. typedef fp_word mp_word;
  508. typedef fp_int mp_int;
  509. /* Constants */
  510. #define MP_LT FP_LT /* less than */
  511. #define MP_EQ FP_EQ /* equal to */
  512. #define MP_GT FP_GT /* greater than */
  513. #define MP_OKAY FP_OKAY /* ok result */
  514. #define MP_NO FP_NO /* yes/no result */
  515. #define MP_YES FP_YES /* yes/no result */
  516. /* Prototypes */
  517. int mp_init (mp_int * a);
  518. void mp_clear (mp_int * a);
  519. int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e, mp_int* f);
  520. int mp_add (mp_int * a, mp_int * b, mp_int * c);
  521. int mp_sub (mp_int * a, mp_int * b, mp_int * c);
  522. int mp_add_d (mp_int * a, mp_digit b, mp_int * c);
  523. int mp_mul (mp_int * a, mp_int * b, mp_int * c);
  524. int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d);
  525. int mp_mod(mp_int *a, mp_int *b, mp_int *c);
  526. int mp_invmod(mp_int *a, mp_int *b, mp_int *c);
  527. int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y);
  528. int mp_cmp(mp_int *a, mp_int *b);
  529. int mp_cmp_d(mp_int *a, mp_digit b);
  530. int mp_unsigned_bin_size(mp_int * a);
  531. int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c);
  532. int mp_to_unsigned_bin (mp_int * a, unsigned char *b);
  533. #ifdef HAVE_ECC
  534. int mp_sqrmod(mp_int* a, mp_int* b, mp_int* c);
  535. int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
  536. int mp_read_radix(mp_int* a, const char* str, int radix);
  537. int mp_iszero(mp_int* a);
  538. int mp_set(fp_int *a, fp_digit b);
  539. int mp_sqr(fp_int *A, fp_int *B);
  540. int mp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp);
  541. int mp_montgomery_setup(fp_int *a, fp_digit *rho);
  542. int mp_isodd(mp_int* a);
  543. int mp_div_2(fp_int * a, fp_int * b);
  544. #endif
  545. #if defined(HAVE_ECC) || defined(CYASSL_KEY_GEN)
  546. int mp_copy(fp_int* a, fp_int* b);
  547. #endif
  548. #ifdef CYASSL_KEY_GEN
  549. int mp_set_int(fp_int *a, fp_digit b);
  550. int mp_gcd(fp_int *a, fp_int *b, fp_int *c);
  551. int mp_lcm(fp_int *a, fp_int *b, fp_int *c);
  552. int mp_sub_d(fp_int *a, fp_digit b, fp_int *c);
  553. int mp_prime_is_prime(mp_int* a, int t, int* result);
  554. #endif /* CYASSL_KEY_GEN */
  555. #ifdef __cplusplus
  556. }
  557. #endif
  558. #endif /* CTAO_CRYPT_TFM_H */