ctc_integer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* ctc_integer.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 LibTomMath 0.38 by Tom St Denis, tomstdenis@iahu.ca,
  23. * http://math.libtomcrypt.com
  24. */
  25. #ifndef CTAO_CRYPT_INTEGER_H
  26. #define CTAO_CRYPT_INTEGER_H
  27. /* may optionally use fast math instead, not yet supported on all platforms and
  28. may not be faster on all
  29. */
  30. #include "ctc_types.h" /* will set MP_xxBIT if not default */
  31. #ifdef USE_FAST_MATH
  32. #include "ctc_tfm.h"
  33. #else
  34. #ifndef CHAR_BIT
  35. #include <limits.h>
  36. #endif
  37. #include "mpi_class.h"
  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. #ifdef __cplusplus
  45. extern "C" {
  46. /* C++ compilers don't like assigning void * to mp_digit * */
  47. #define OPT_CAST(x) (x *)
  48. #else
  49. /* C on the other hand doesn't care */
  50. #define OPT_CAST(x)
  51. #endif
  52. /* detect 64-bit mode if possible */
  53. #if defined(__x86_64__)
  54. #if !(defined(MP_64BIT) && defined(MP_16BIT) && defined(MP_8BIT))
  55. #define MP_64BIT
  56. #endif
  57. #endif
  58. /* some default configurations.
  59. *
  60. * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
  61. * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
  62. *
  63. * At the very least a mp_digit must be able to hold 7 bits
  64. * [any size beyond that is ok provided it doesn't overflow the data type]
  65. */
  66. #ifdef MP_8BIT
  67. typedef unsigned char mp_digit;
  68. typedef unsigned short mp_word;
  69. #elif defined(MP_16BIT)
  70. typedef unsigned short mp_digit;
  71. typedef unsigned long mp_word;
  72. #elif defined(MP_64BIT)
  73. /* for GCC only on supported platforms */
  74. #ifndef CRYPT
  75. typedef unsigned long long ulong64;
  76. typedef signed long long long64;
  77. #endif
  78. typedef unsigned long mp_digit;
  79. typedef unsigned long mp_word __attribute__ ((mode(TI)));
  80. #define DIGIT_BIT 60
  81. #else
  82. /* this is the default case, 28-bit digits */
  83. /* this is to make porting into LibTomCrypt easier :-) */
  84. #ifndef CRYPT
  85. #if defined(_MSC_VER) || defined(__BORLANDC__)
  86. typedef unsigned __int64 ulong64;
  87. typedef signed __int64 long64;
  88. #else
  89. typedef unsigned long long ulong64;
  90. typedef signed long long long64;
  91. #endif
  92. #endif
  93. typedef unsigned long mp_digit;
  94. typedef ulong64 mp_word;
  95. #ifdef MP_31BIT
  96. /* this is an extension that uses 31-bit digits */
  97. #define DIGIT_BIT 31
  98. #else
  99. /* default case is 28-bit digits, defines MP_28BIT as a handy test macro */
  100. #define DIGIT_BIT 28
  101. #define MP_28BIT
  102. #endif
  103. #endif
  104. /* otherwise the bits per digit is calculated automatically from the size of
  105. a mp_digit */
  106. #ifndef DIGIT_BIT
  107. #define DIGIT_BIT ((int)((CHAR_BIT * sizeof(mp_digit) - 1)))
  108. /* bits per digit */
  109. #endif
  110. #define MP_DIGIT_BIT DIGIT_BIT
  111. #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
  112. #define MP_DIGIT_MAX MP_MASK
  113. /* equalities */
  114. #define MP_LT -1 /* less than */
  115. #define MP_EQ 0 /* equal to */
  116. #define MP_GT 1 /* greater than */
  117. #define MP_ZPOS 0 /* positive integer */
  118. #define MP_NEG 1 /* negative */
  119. #define MP_OKAY 0 /* ok result */
  120. #define MP_MEM -2 /* out of mem */
  121. #define MP_VAL -3 /* invalid input */
  122. #define MP_RANGE MP_VAL
  123. #define MP_YES 1 /* yes response */
  124. #define MP_NO 0 /* no response */
  125. /* Primality generation flags */
  126. #define LTM_PRIME_BBS 0x0001 /* BBS style prime */
  127. #define LTM_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */
  128. #define LTM_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */
  129. typedef int mp_err;
  130. /* define this to use lower memory usage routines (exptmods mostly) */
  131. #define MP_LOW_MEM
  132. /* default precision */
  133. #ifndef MP_PREC
  134. #ifndef MP_LOW_MEM
  135. #define MP_PREC 32 /* default digits of precision */
  136. #else
  137. #define MP_PREC 1 /* default digits of precision */
  138. #endif
  139. #endif
  140. /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD -
  141. BITS_PER_DIGIT*2) */
  142. #define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
  143. /* the infamous mp_int structure */
  144. typedef struct {
  145. int used, alloc, sign;
  146. mp_digit *dp;
  147. } mp_int;
  148. /* callback for mp_prime_random, should fill dst with random bytes and return
  149. how many read [upto len] */
  150. typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
  151. #define USED(m) ((m)->used)
  152. #define DIGIT(m,k) ((m)->dp[(k)])
  153. #define SIGN(m) ((m)->sign)
  154. /* ---> Basic Manipulations <--- */
  155. #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
  156. #define mp_iseven(a) \
  157. (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
  158. #define mp_isodd(a) \
  159. (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
  160. /* number of primes */
  161. #ifdef MP_8BIT
  162. #define PRIME_SIZE 31
  163. #else
  164. #define PRIME_SIZE 256
  165. #endif
  166. #define mp_prime_random(a, t, size, bbs, cb, dat) \
  167. mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?LTM_PRIME_BBS:0, cb, dat)
  168. #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
  169. #define mp_raw_size(mp) mp_signed_bin_size(mp)
  170. #define mp_toraw(mp, str) mp_to_signed_bin((mp), (str))
  171. #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
  172. #define mp_mag_size(mp) mp_unsigned_bin_size(mp)
  173. #define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str))
  174. #define mp_tobinary(M, S) mp_toradix((M), (S), 2)
  175. #define mp_tooctal(M, S) mp_toradix((M), (S), 8)
  176. #define mp_todecimal(M, S) mp_toradix((M), (S), 10)
  177. #define mp_tohex(M, S) mp_toradix((M), (S), 16)
  178. #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
  179. extern const char *mp_s_rmap;
  180. /* 6 functions needed by Rsa */
  181. int mp_init (mp_int * a);
  182. void mp_clear (mp_int * a);
  183. int mp_unsigned_bin_size(mp_int * a);
  184. int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c);
  185. int mp_to_unsigned_bin (mp_int * a, unsigned char *b);
  186. int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y);
  187. /* end functions needed by Rsa */
  188. /* functions added to support above needed, removed TOOM and KARATSUBA */
  189. int mp_count_bits (mp_int * a);
  190. int mp_init_copy (mp_int * a, mp_int * b);
  191. int mp_copy (mp_int * a, mp_int * b);
  192. int mp_grow (mp_int * a, int size);
  193. void bn_reverse (unsigned char *s, int len);
  194. int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d);
  195. void mp_zero (mp_int * a);
  196. void mp_clamp (mp_int * a);
  197. void mp_exch (mp_int * a, mp_int * b);
  198. void mp_rshd (mp_int * a, int b);
  199. int mp_mod_2d (mp_int * a, int b, mp_int * c);
  200. int mp_mul_2d (mp_int * a, int b, mp_int * c);
  201. int mp_lshd (mp_int * a, int b);
  202. int mp_abs (mp_int * a, mp_int * b);
  203. int mp_invmod (mp_int * a, mp_int * b, mp_int * c);
  204. int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c);
  205. int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c);
  206. int mp_cmp_mag (mp_int * a, mp_int * b);
  207. int mp_cmp (mp_int * a, mp_int * b);
  208. int mp_cmp_d(mp_int * a, mp_digit b);
  209. void mp_set (mp_int * a, mp_digit b);
  210. int mp_mod (mp_int * a, mp_int * b, mp_int * c);
  211. int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d);
  212. int mp_div_2(mp_int * a, mp_int * b);
  213. int mp_add (mp_int * a, mp_int * b, mp_int * c);
  214. int s_mp_add (mp_int * a, mp_int * b, mp_int * c);
  215. int s_mp_sub (mp_int * a, mp_int * b, mp_int * c);
  216. int mp_sub (mp_int * a, mp_int * b, mp_int * c);
  217. int mp_reduce_is_2k_l(mp_int *a);
  218. int mp_reduce_is_2k(mp_int *a);
  219. int mp_dr_is_modulus(mp_int *a);
  220. int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int);
  221. int mp_montgomery_setup (mp_int * n, mp_digit * rho);
  222. int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho);
  223. int mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho);
  224. void mp_dr_setup(mp_int *a, mp_digit *d);
  225. int mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k);
  226. int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d);
  227. int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
  228. int s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
  229. int mp_reduce_2k_setup_l(mp_int *a, mp_int *d);
  230. int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d);
  231. int mp_reduce (mp_int * x, mp_int * m, mp_int * mu);
  232. int mp_reduce_setup (mp_int * a, mp_int * b);
  233. int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode);
  234. int mp_montgomery_calc_normalization (mp_int * a, mp_int * b);
  235. int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
  236. int s_mp_sqr (mp_int * a, mp_int * b);
  237. int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
  238. int fast_s_mp_sqr (mp_int * a, mp_int * b);
  239. int mp_init_size (mp_int * a, int size);
  240. int mp_div_3 (mp_int * a, mp_int *c, mp_digit * d);
  241. int mp_mul_2(mp_int * a, mp_int * b);
  242. int mp_mul (mp_int * a, mp_int * b, mp_int * c);
  243. int mp_sqr (mp_int * a, mp_int * b);
  244. int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d);
  245. int mp_mul_d (mp_int * a, mp_digit b, mp_int * c);
  246. int mp_2expt (mp_int * a, int b);
  247. int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
  248. int mp_add_d (mp_int* a, mp_digit b, mp_int* c);
  249. /* end support added functions */
  250. /* added */
  251. int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e,
  252. mp_int* f);
  253. #if defined(HAVE_ECC) || defined(CYASSL_KEY_GEN)
  254. int mp_sqrmod(mp_int* a, mp_int* b, mp_int* c);
  255. #endif
  256. #ifdef HAVE_ECC
  257. int mp_read_radix(mp_int* a, const char* str, int radix);
  258. #endif
  259. #ifdef CYASSL_KEY_GEN
  260. int mp_prime_is_prime (mp_int * a, int t, int *result);
  261. int mp_set_int (mp_int * a, unsigned long b);
  262. int mp_gcd (mp_int * a, mp_int * b, mp_int * c);
  263. int mp_lcm (mp_int * a, mp_int * b, mp_int * c);
  264. #endif
  265. #if defined(CYASSL_KEY_GEN) || defined(HAVE_ECC) || !defined(NO_PWDBASED)
  266. int mp_sub_d (mp_int * a, mp_digit b, mp_int * c);
  267. #endif
  268. #ifdef __cplusplus
  269. }
  270. #endif
  271. #endif /* USE_FAST_MATH */
  272. #endif /* CTAO_CRYPT_INTEGER_H */