integer.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /* integer.h
  2. *
  3. * Copyright (C) 2006-2022 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 LibTomMath 0.38 by Tom St Denis, tomstdenis@iahu.ca,
  23. * http://math.libtomcrypt.com
  24. */
  25. #ifndef WOLF_CRYPT_INTEGER_H
  26. #define WOLF_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 <wolfssl/wolfcrypt/types.h> /* will set MP_xxBIT if not default */
  31. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  32. #include <wolfssl/wolfcrypt/sp_int.h>
  33. #elif defined(USE_FAST_MATH)
  34. #include <wolfssl/wolfcrypt/tfm.h>
  35. #else
  36. #include <wolfssl/wolfcrypt/random.h>
  37. #ifndef CHAR_BIT
  38. #if defined(WOLFSSL_LINUXKM)
  39. #include <linux/limits.h>
  40. #else
  41. #include <limits.h>
  42. #endif
  43. #endif
  44. #include <wolfssl/wolfcrypt/mpi_class.h>
  45. #ifdef __cplusplus
  46. extern "C" {
  47. /* C++ compilers don't like assigning void * to mp_digit * */
  48. #define OPT_CAST(x) (x *)
  49. #elif defined(_SH3)
  50. /* SuperH SH3 compiler doesn't like assigning voi* to mp_digit* */
  51. #define OPT_CAST(x) (x *)
  52. #else
  53. /* C on the other hand doesn't care */
  54. #define OPT_CAST(x)
  55. #endif /* __cplusplus */
  56. /* detect 64-bit mode if possible */
  57. #if (defined(__x86_64__) || defined(__aarch64__)) && !(defined (_MSC_VER) && defined(__clang__))
  58. #if !(defined(MP_64BIT) && defined(MP_16BIT) && defined(MP_8BIT))
  59. #define MP_64BIT
  60. #endif
  61. #endif
  62. /* if intel compiler doesn't provide 128 bit type don't turn on 64bit */
  63. #if defined(MP_64BIT) && defined(__INTEL_COMPILER) && !defined(HAVE___UINT128_T)
  64. #undef MP_64BIT
  65. #endif
  66. /* allow user to define on mp_digit, mp_word, DIGIT_BIT types */
  67. #ifndef WOLFSSL_BIGINT_TYPES
  68. /* some default configurations.
  69. *
  70. * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
  71. * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
  72. *
  73. * At the very least a mp_digit must be able to hold 7 bits
  74. * [any size beyond that is ok provided it doesn't overflow the data type]
  75. */
  76. #ifdef MP_8BIT
  77. /* 8-bit */
  78. typedef unsigned char mp_digit;
  79. typedef unsigned short mp_word;
  80. /* don't define DIGIT_BIT, so its calculated below */
  81. #elif defined(MP_16BIT)
  82. /* 16-bit */
  83. typedef unsigned int mp_digit;
  84. typedef unsigned long mp_word;
  85. /* don't define DIGIT_BIT, so its calculated below */
  86. #elif defined(NO_64BIT)
  87. /* 32-bit forced to 16-bit */
  88. typedef unsigned short mp_digit;
  89. typedef unsigned int mp_word;
  90. #define DIGIT_BIT 12
  91. #elif defined(MP_64BIT)
  92. /* 64-bit */
  93. /* for GCC only on supported platforms */
  94. typedef unsigned long long mp_digit; /* 64 bit type, 128 uses mode(TI) */
  95. typedef unsigned long mp_word __attribute__ ((mode(TI)));
  96. #define DIGIT_BIT 60
  97. #else
  98. /* 32-bit default case */
  99. #if defined(_MSC_VER) || defined(__BORLANDC__)
  100. typedef unsigned __int64 ulong64;
  101. #else
  102. typedef unsigned long long ulong64;
  103. #endif
  104. typedef unsigned int mp_digit; /* long could be 64 now, changed TAO */
  105. typedef ulong64 mp_word;
  106. #ifdef MP_31BIT
  107. /* this is an extension that uses 31-bit digits */
  108. #define DIGIT_BIT 31
  109. #else
  110. /* default case is 28-bit digits, defines MP_28BIT as a handy test macro */
  111. #define DIGIT_BIT 28
  112. #define MP_28BIT
  113. #endif
  114. #endif
  115. #endif /* WOLFSSL_BIGINT_TYPES */
  116. /* otherwise the bits per digit is calculated automatically from the size of
  117. a mp_digit */
  118. #ifndef DIGIT_BIT
  119. #define DIGIT_BIT ((int)((CHAR_BIT * sizeof(mp_digit) - 1)))
  120. /* bits per digit */
  121. #endif
  122. #define MP_DIGIT_BIT DIGIT_BIT
  123. #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
  124. #define MP_DIGIT_MAX MP_MASK
  125. /* equalities */
  126. #define MP_LT (-1) /* less than */
  127. #define MP_EQ 0 /* equal to */
  128. #define MP_GT 1 /* greater than */
  129. #define MP_ZPOS 0 /* positive integer */
  130. #define MP_NEG 1 /* negative */
  131. #define MP_OKAY 0 /* ok result */
  132. #define MP_MEM (-2) /* out of mem */
  133. #define MP_VAL (-3) /* invalid input */
  134. #define MP_NOT_INF (-4) /* point not at infinity */
  135. #define MP_RANGE MP_NOT_INF
  136. #define MP_YES 1 /* yes response */
  137. #define MP_NO 0 /* no response */
  138. /* Primality generation flags */
  139. #define LTM_PRIME_BBS 0x0001 /* BBS style prime */
  140. #define LTM_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */
  141. #define LTM_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */
  142. typedef int mp_err;
  143. /* define this to use lower memory usage routines (exptmods mostly) */
  144. #define MP_LOW_MEM
  145. /* default precision */
  146. #ifndef MP_PREC
  147. #ifndef MP_LOW_MEM
  148. #define MP_PREC 32 /* default digits of precision */
  149. #else
  150. #define MP_PREC 1 /* default digits of precision */
  151. #endif
  152. #endif
  153. /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD -
  154. BITS_PER_DIGIT*2) */
  155. #define MP_WARRAY ((mp_word)1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
  156. #ifdef HAVE_WOLF_BIGINT
  157. /* raw big integer */
  158. typedef struct WC_BIGINT {
  159. byte* buf;
  160. word32 len;
  161. void* heap;
  162. } WC_BIGINT;
  163. #define WOLF_BIGINT_DEFINED
  164. #endif
  165. /* the mp_int structure */
  166. typedef struct mp_int {
  167. int used, alloc, sign;
  168. mp_digit *dp;
  169. #ifdef HAVE_WOLF_BIGINT
  170. struct WC_BIGINT raw; /* unsigned binary (big endian) */
  171. #endif
  172. } mp_int;
  173. /* wolf big int and common functions */
  174. #include <wolfssl/wolfcrypt/wolfmath.h>
  175. /* callback for mp_prime_random, should fill dst with random bytes and return
  176. how many read [up to len] */
  177. typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
  178. #define USED(m) ((m)->used)
  179. #define DIGIT(m,k) ((m)->dp[(k)])
  180. #define SIGN(m) ((m)->sign)
  181. /* ---> Basic Manipulations <--- */
  182. #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
  183. #define mp_isone(a) \
  184. (((((a)->used == 1)) && ((a)->dp[0] == 1u) && ((a)->sign == MP_ZPOS)) \
  185. ? MP_YES : MP_NO)
  186. #define mp_iseven(a) \
  187. (((a)->used > 0 && (((a)->dp[0] & 1u) == 0u)) ? MP_YES : MP_NO)
  188. #define mp_isodd(a) \
  189. (((a)->used > 0 && (((a)->dp[0] & 1u) == 1u)) ? MP_YES : MP_NO)
  190. #define mp_isneg(a) (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO)
  191. #define mp_isword(a, w) \
  192. ((((a)->used == 1) && ((a)->dp[0] == (w))) || (((w) == 0) && ((a)->used == 0)) \
  193. ? MP_YES : MP_NO)
  194. /* number of primes */
  195. #ifdef MP_8BIT
  196. #define PRIME_SIZE 31
  197. #else
  198. #define PRIME_SIZE 256
  199. #endif
  200. #ifndef MAX_INVMOD_SZ
  201. #if defined(WOLFSSL_MYSQL_COMPATIBLE)
  202. #define MAX_INVMOD_SZ 8192
  203. #else
  204. #define MAX_INVMOD_SZ 4096
  205. #endif
  206. #endif
  207. #define mp_prime_random(a, t, size, bbs, cb, dat) \
  208. mp_prime_random_ex(a, t, ((size) * 8) + 1, ((bbs)==1)?LTM_PRIME_BBS:0, cb, dat)
  209. #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
  210. #define mp_mag_size(mp) mp_unsigned_bin_size(mp)
  211. #define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str))
  212. #define MP_RADIX_BIN 2
  213. #define MP_RADIX_OCT 8
  214. #define MP_RADIX_DEC 10
  215. #define MP_RADIX_HEX 16
  216. #define MP_RADIX_MAX 64
  217. #define mp_tobinary(M, S) mp_toradix((M), (S), MP_RADIX_BIN)
  218. #define mp_tooctal(M, S) mp_toradix((M), (S), MP_RADIX_OCT)
  219. #define mp_todecimal(M, S) mp_toradix((M), (S), MP_RADIX_DEC)
  220. #define mp_tohex(M, S) mp_toradix((M), (S), MP_RADIX_HEX)
  221. #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
  222. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) || \
  223. defined(WOLFSSL_DEBUG_MATH) || defined(DEBUG_WOLFSSL)
  224. extern const char *mp_s_rmap;
  225. #endif
  226. /* 6 functions needed by Rsa */
  227. MP_API int mp_init (mp_int * a);
  228. MP_API void mp_clear (mp_int * a);
  229. MP_API void mp_free (mp_int * a);
  230. MP_API void mp_forcezero(mp_int * a);
  231. MP_API int mp_unsigned_bin_size(const mp_int * a);
  232. MP_API int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c);
  233. MP_API int mp_to_unsigned_bin_at_pos(int x, mp_int *t, unsigned char *b);
  234. MP_API int mp_to_unsigned_bin (mp_int * a, unsigned char *b);
  235. MP_API int mp_to_unsigned_bin_len(mp_int * a, unsigned char *b, int c);
  236. MP_API int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y);
  237. MP_API int mp_exptmod_ex (mp_int * G, mp_int * X, int digits, mp_int * P,
  238. mp_int * Y);
  239. /* end functions needed by Rsa */
  240. /* functions added to support above needed, removed TOOM and KARATSUBA */
  241. MP_API int mp_count_bits (const mp_int * a);
  242. MP_API int mp_leading_bit (mp_int * a);
  243. MP_API int mp_init_copy (mp_int * a, mp_int * b);
  244. MP_API int mp_copy (const mp_int * a, mp_int * b);
  245. MP_API int mp_grow (mp_int * a, int size);
  246. MP_API int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d);
  247. MP_API void mp_zero (mp_int * a);
  248. MP_API void mp_clamp (mp_int * a);
  249. MP_API int mp_exch (mp_int * a, mp_int * b);
  250. MP_API int mp_cond_swap_ct (mp_int * a, mp_int * b, int c, int m);
  251. MP_API void mp_rshd (mp_int * a, int b);
  252. MP_API void mp_rshb (mp_int * a, int b);
  253. MP_API int mp_mod_2d (mp_int * a, int b, mp_int * c);
  254. MP_API int mp_mul_2d (mp_int * a, int b, mp_int * c);
  255. MP_API int mp_lshd (mp_int * a, int b);
  256. MP_API int mp_abs (mp_int * a, mp_int * b);
  257. MP_API int mp_invmod (mp_int * a, mp_int * b, mp_int * c);
  258. int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c);
  259. MP_API int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c);
  260. MP_API int mp_cmp_mag (mp_int * a, mp_int * b);
  261. MP_API int mp_cmp (mp_int * a, mp_int * b);
  262. MP_API int mp_cmp_d(mp_int * a, mp_digit b);
  263. MP_API int mp_set (mp_int * a, mp_digit b);
  264. MP_API int mp_is_bit_set (mp_int * a, mp_digit b);
  265. MP_API int mp_mod (mp_int * a, mp_int * b, mp_int * c);
  266. MP_API int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d);
  267. MP_API int mp_div_2(mp_int * a, mp_int * b);
  268. MP_API int mp_div_2_mod_ct (mp_int* a, mp_int* b, mp_int* c);
  269. MP_API int mp_add (mp_int * a, mp_int * b, mp_int * c);
  270. int s_mp_add (mp_int * a, mp_int * b, mp_int * c);
  271. int s_mp_sub (mp_int * a, mp_int * b, mp_int * c);
  272. MP_API int mp_sub (mp_int * a, mp_int * b, mp_int * c);
  273. MP_API int mp_reduce_is_2k_l(mp_int *a);
  274. MP_API int mp_reduce_is_2k(mp_int *a);
  275. MP_API int mp_dr_is_modulus(mp_int *a);
  276. MP_API int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y,
  277. int redmode);
  278. MP_API int mp_exptmod_base_2 (mp_int * X, mp_int * P, mp_int * Y);
  279. #define mp_exptmod_nct(G,X,P,Y) mp_exptmod_fast(G,X,P,Y,0)
  280. MP_API int mp_montgomery_setup (mp_int * n, mp_digit * rho);
  281. int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho);
  282. MP_API int mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho);
  283. #define mp_montgomery_reduce_ex(x, n, rho, ct) mp_montgomery_reduce (x, n, rho)
  284. MP_API void mp_dr_setup(mp_int *a, mp_digit *d);
  285. MP_API int mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k);
  286. MP_API int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d);
  287. int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
  288. int s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
  289. MP_API int mp_reduce_2k_setup_l(mp_int *a, mp_int *d);
  290. MP_API int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d);
  291. MP_API int mp_reduce (mp_int * x, mp_int * m, mp_int * mu);
  292. MP_API int mp_reduce_setup (mp_int * a, mp_int * b);
  293. int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode);
  294. MP_API int mp_montgomery_calc_normalization (mp_int * a, mp_int * b);
  295. int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
  296. int s_mp_sqr (mp_int * a, mp_int * b);
  297. int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
  298. int fast_s_mp_sqr (mp_int * a, mp_int * b);
  299. MP_API int mp_init_size (mp_int * a, int size);
  300. MP_API int mp_div_3 (mp_int * a, mp_int *c, mp_digit * d);
  301. MP_API int mp_mul_2(mp_int * a, mp_int * b);
  302. MP_API int mp_mul (mp_int * a, mp_int * b, mp_int * c);
  303. MP_API int mp_sqr (mp_int * a, mp_int * b);
  304. MP_API int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d);
  305. MP_API int mp_submod (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
  306. MP_API int mp_addmod (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
  307. MP_API int mp_submod_ct (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
  308. MP_API int mp_addmod_ct (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
  309. MP_API int mp_mul_d (mp_int * a, mp_digit b, mp_int * c);
  310. MP_API int mp_2expt (mp_int * a, int b);
  311. MP_API int mp_set_bit (mp_int * a, int b);
  312. MP_API int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
  313. MP_API int mp_add_d (mp_int* a, mp_digit b, mp_int* c);
  314. MP_API int mp_set_int (mp_int * a, unsigned long b);
  315. MP_API int mp_sub_d (mp_int * a, mp_digit b, mp_int * c);
  316. /* end support added functions */
  317. /* added */
  318. MP_API int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e,
  319. mp_int* f);
  320. MP_API int mp_toradix (mp_int *a, char *str, int radix);
  321. MP_API int mp_radix_size (mp_int * a, int radix, int *size);
  322. #ifdef WOLFSSL_DEBUG_MATH
  323. MP_API void mp_dump(const char* desc, mp_int* a, byte verbose);
  324. #else
  325. #define mp_dump(desc, a, verbose)
  326. #endif
  327. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || !defined(NO_RSA) || \
  328. !defined(NO_DSA) || !defined(NO_DH)
  329. MP_API int mp_sqrmod(mp_int* a, mp_int* b, mp_int* c);
  330. #endif
  331. #if !defined(NO_DSA) || defined(HAVE_ECC)
  332. MP_API int mp_read_radix(mp_int* a, const char* str, int radix);
  333. #endif
  334. #if defined(WOLFSSL_KEY_GEN) || !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH)
  335. MP_API int mp_prime_is_prime (mp_int * a, int t, int *result);
  336. MP_API int mp_prime_is_prime_ex (mp_int * a, int t, int *result, WC_RNG* rng);
  337. #endif /* WOLFSSL_KEY_GEN NO_RSA NO_DSA NO_DH */
  338. #ifdef WOLFSSL_KEY_GEN
  339. MP_API int mp_gcd (mp_int * a, mp_int * b, mp_int * c);
  340. MP_API int mp_lcm (mp_int * a, mp_int * b, mp_int * c);
  341. MP_API int mp_rand_prime(mp_int* a, int len, WC_RNG* rng, void* heap);
  342. #endif
  343. MP_API int mp_cnt_lsb(mp_int *a);
  344. MP_API int mp_mod_d(mp_int* a, mp_digit b, mp_digit* c);
  345. #ifdef __cplusplus
  346. }
  347. #endif
  348. #endif /* USE_FAST_MATH */
  349. #endif /* WOLF_CRYPT_INTEGER_H */