integer.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /* integer.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 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 SP math all or fast math instead. The heap math requires
  28. * realloc and is not timing resistant. The SP math all is recommended for new
  29. * designs.
  30. */
  31. #ifndef USE_INTEGER_HEAP_MATH
  32. /* Some platforms (like FIPS) may only include integer.h for math. */
  33. /* Handle variations of fast math, integer and sp math */
  34. #include <wolfssl/wolfcrypt/wolfmath.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. /* No point in dynamically allocating mp_int when it is very small.
  157. * The dp field will grow and shrink dynamically.
  158. */
  159. /* Declare a statically allocated mp_int. */
  160. #define DECL_MP_INT_SIZE(name, bits) \
  161. mp_int name[1]
  162. /* Declare statically allocated mp_int. */
  163. #define DECL_MP_INT_SIZE_DYN(name, bits, max) \
  164. mp_int name[1]
  165. /* Zero out mp_int of minimal size. */
  166. #define NEW_MP_INT_SIZE(name, bits, heap, type) \
  167. XMEMSET(name, 0, sizeof(mp_int))
  168. /* Dispose of static mp_int. */
  169. #define FREE_MP_INT_SIZE(name, heap, type)
  170. /* Initialize an mp_int. */
  171. #define INIT_MP_INT_SIZE(name, bits) \
  172. mp_init(name)
  173. /* Type to cast to when using size marcos. */
  174. #define MP_INT_SIZE mp_int
  175. #ifdef HAVE_WOLF_BIGINT
  176. /* raw big integer */
  177. typedef struct WC_BIGINT {
  178. byte* buf;
  179. word32 len;
  180. void* heap;
  181. } WC_BIGINT;
  182. #define WOLF_BIGINT_DEFINED
  183. #endif
  184. /* the mp_int structure */
  185. typedef struct mp_int {
  186. int used, alloc, sign;
  187. mp_digit *dp;
  188. #ifdef HAVE_WOLF_BIGINT
  189. struct WC_BIGINT raw; /* unsigned binary (big endian) */
  190. #endif
  191. } mp_int;
  192. /* wolf big int and common functions */
  193. #include <wolfssl/wolfcrypt/wolfmath.h>
  194. /* callback for mp_prime_random, should fill dst with random bytes and return
  195. how many read [up to len] */
  196. typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
  197. #define USED(m) ((m)->used)
  198. #define DIGIT(m,k) ((m)->dp[(k)])
  199. #define SIGN(m) ((m)->sign)
  200. /* ---> Basic Manipulations <--- */
  201. #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
  202. #define mp_isone(a) \
  203. (((((a)->used == 1)) && ((a)->dp[0] == 1u) && ((a)->sign == MP_ZPOS)) \
  204. ? MP_YES : MP_NO)
  205. #define mp_iseven(a) \
  206. (((a)->used > 0 && (((a)->dp[0] & 1u) == 0u)) ? MP_YES : MP_NO)
  207. #define mp_isodd(a) \
  208. (((a)->used > 0 && (((a)->dp[0] & 1u) == 1u)) ? MP_YES : MP_NO)
  209. #define mp_isneg(a) (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO)
  210. #define mp_setneg(a) ((a)->sign = MP_NEG)
  211. #define mp_isword(a, w) \
  212. ((((a)->used == 1) && ((a)->dp[0] == (w))) || (((w) == 0) && ((a)->used == 0)) \
  213. ? MP_YES : MP_NO)
  214. /* Number of bits used based on used field only. */
  215. #define mp_bitsused(a) ((a)->used * DIGIT_BIT)
  216. /* number of primes */
  217. #ifdef MP_8BIT
  218. #define PRIME_SIZE 31
  219. #else
  220. #define PRIME_SIZE 256
  221. #endif
  222. #ifndef MAX_INVMOD_SZ
  223. #if defined(WOLFSSL_MYSQL_COMPATIBLE)
  224. #define MAX_INVMOD_SZ 8192
  225. #else
  226. #define MAX_INVMOD_SZ 4096
  227. #endif
  228. #endif
  229. #define mp_prime_random(a, t, size, bbs, cb, dat) \
  230. mp_prime_random_ex(a, t, ((size) * 8) + 1, ((bbs)==1)?LTM_PRIME_BBS:0, cb, dat)
  231. #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
  232. #define mp_mag_size(mp) mp_unsigned_bin_size(mp)
  233. #define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str))
  234. #define MP_RADIX_BIN 2
  235. #define MP_RADIX_OCT 8
  236. #define MP_RADIX_DEC 10
  237. #define MP_RADIX_HEX 16
  238. #define MP_RADIX_MAX 64
  239. #define mp_tobinary(M, S) mp_toradix((M), (S), MP_RADIX_BIN)
  240. #define mp_tooctal(M, S) mp_toradix((M), (S), MP_RADIX_OCT)
  241. #define mp_todecimal(M, S) mp_toradix((M), (S), MP_RADIX_DEC)
  242. #define mp_tohex(M, S) mp_toradix((M), (S), MP_RADIX_HEX)
  243. #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
  244. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) || \
  245. defined(WOLFSSL_DEBUG_MATH) || defined(DEBUG_WOLFSSL)
  246. extern const char *mp_s_rmap;
  247. #endif
  248. /* 6 functions needed by Rsa */
  249. MP_API int mp_init (mp_int * a);
  250. MP_API void mp_clear (mp_int * a);
  251. MP_API void mp_free (mp_int * a);
  252. MP_API void mp_forcezero(mp_int * a);
  253. MP_API int mp_unsigned_bin_size(const mp_int * a);
  254. MP_API int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c);
  255. MP_API int mp_to_unsigned_bin_at_pos(int x, mp_int *t, unsigned char *b);
  256. MP_API int mp_to_unsigned_bin (mp_int * a, unsigned char *b);
  257. MP_API int mp_to_unsigned_bin_len(mp_int * a, unsigned char *b, int c);
  258. MP_API int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y);
  259. MP_API int mp_exptmod_ex (mp_int * G, mp_int * X, int digits, mp_int * P,
  260. mp_int * Y);
  261. /* end functions needed by Rsa */
  262. /* functions added to support above needed, removed TOOM and KARATSUBA */
  263. MP_API int mp_count_bits (const mp_int * a);
  264. MP_API int mp_leading_bit (mp_int * a);
  265. MP_API int mp_init_copy (mp_int * a, mp_int * b);
  266. MP_API int mp_copy (const mp_int * a, mp_int * b);
  267. MP_API int mp_grow (mp_int * a, int size);
  268. MP_API int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d);
  269. MP_API void mp_zero (mp_int * a);
  270. MP_API void mp_clamp (mp_int * a);
  271. MP_API int mp_exch (mp_int * a, mp_int * b);
  272. MP_API int mp_cond_swap_ct_ex (mp_int * a, mp_int * b, int c, int m,
  273. mp_int * t);
  274. MP_API int mp_cond_swap_ct (mp_int * a, mp_int * b, int c, int m);
  275. MP_API void mp_rshd (mp_int * a, int b);
  276. MP_API void mp_rshb (mp_int * a, int b);
  277. MP_API int mp_mod_2d (mp_int * a, int b, mp_int * c);
  278. MP_API int mp_mul_2d (mp_int * a, int b, mp_int * c);
  279. MP_API int mp_lshd (mp_int * a, int b);
  280. MP_API int mp_abs (mp_int * a, mp_int * b);
  281. MP_API int mp_invmod (mp_int * a, mp_int * b, mp_int * c);
  282. int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c);
  283. MP_API int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c);
  284. MP_API int mp_cmp_mag (mp_int * a, mp_int * b);
  285. MP_API int mp_cmp (mp_int * a, mp_int * b);
  286. MP_API int mp_cmp_d(mp_int * a, mp_digit b);
  287. MP_API int mp_set (mp_int * a, mp_digit b);
  288. MP_API int mp_is_bit_set (mp_int * a, mp_digit b);
  289. MP_API int mp_mod (mp_int * a, mp_int * b, mp_int * c);
  290. MP_API int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d);
  291. MP_API int mp_div_2(mp_int * a, mp_int * b);
  292. MP_API int mp_div_2_mod_ct (mp_int* a, mp_int* b, mp_int* c);
  293. MP_API int mp_add (mp_int * a, mp_int * b, mp_int * c);
  294. int s_mp_add (mp_int * a, mp_int * b, mp_int * c);
  295. int s_mp_sub (mp_int * a, mp_int * b, mp_int * c);
  296. MP_API int mp_sub (mp_int * a, mp_int * b, mp_int * c);
  297. MP_API int mp_reduce_is_2k_l(mp_int *a);
  298. MP_API int mp_reduce_is_2k(mp_int *a);
  299. MP_API int mp_dr_is_modulus(mp_int *a);
  300. MP_API int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y,
  301. int redmode);
  302. MP_API int mp_exptmod_base_2 (mp_int * X, mp_int * P, mp_int * Y);
  303. #define mp_exptmod_nct(G,X,P,Y) mp_exptmod_fast(G,X,P,Y,0)
  304. MP_API int mp_montgomery_setup (mp_int * n, mp_digit * rho);
  305. int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho);
  306. MP_API int mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho);
  307. #define mp_montgomery_reduce_ex(x, n, rho, ct) mp_montgomery_reduce (x, n, rho)
  308. MP_API void mp_dr_setup(mp_int *a, mp_digit *d);
  309. MP_API int mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k);
  310. MP_API int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d);
  311. int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
  312. int s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
  313. MP_API int mp_reduce_2k_setup_l(mp_int *a, mp_int *d);
  314. MP_API int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d);
  315. MP_API int mp_reduce (mp_int * x, mp_int * m, mp_int * mu);
  316. MP_API int mp_reduce_setup (mp_int * a, mp_int * b);
  317. int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode);
  318. MP_API int mp_montgomery_calc_normalization (mp_int * a, mp_int * b);
  319. int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
  320. int s_mp_sqr (mp_int * a, mp_int * b);
  321. int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
  322. int fast_s_mp_sqr (mp_int * a, mp_int * b);
  323. MP_API int mp_init_size (mp_int * a, int size);
  324. MP_API int mp_div_3 (mp_int * a, mp_int *c, mp_digit * d);
  325. MP_API int mp_mul_2(mp_int * a, mp_int * b);
  326. MP_API int mp_mul (mp_int * a, mp_int * b, mp_int * c);
  327. MP_API int mp_sqr (mp_int * a, mp_int * b);
  328. MP_API int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d);
  329. MP_API int mp_submod (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
  330. MP_API int mp_addmod (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
  331. MP_API int mp_submod_ct (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
  332. MP_API int mp_addmod_ct (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
  333. MP_API int mp_mul_d (mp_int * a, mp_digit b, mp_int * c);
  334. MP_API int mp_2expt (mp_int * a, int b);
  335. MP_API int mp_set_bit (mp_int * a, int b);
  336. MP_API int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
  337. MP_API int mp_add_d (mp_int* a, mp_digit b, mp_int* c);
  338. MP_API int mp_set_int (mp_int * a, unsigned long b);
  339. MP_API int mp_sub_d (mp_int * a, mp_digit b, mp_int * c);
  340. /* end support added functions */
  341. /* added */
  342. MP_API int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e,
  343. mp_int* f);
  344. MP_API int mp_toradix (mp_int *a, char *str, int radix);
  345. MP_API int mp_radix_size (mp_int * a, int radix, int *size);
  346. #ifdef WOLFSSL_DEBUG_MATH
  347. MP_API void mp_dump(const char* desc, mp_int* a, byte verbose);
  348. #else
  349. #define mp_dump(desc, a, verbose)
  350. #endif
  351. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || !defined(NO_RSA) || \
  352. !defined(NO_DSA) || !defined(NO_DH)
  353. MP_API int mp_sqrmod(mp_int* a, mp_int* b, mp_int* c);
  354. #endif
  355. #if !defined(NO_DSA) || defined(HAVE_ECC) || defined(OPENSSL_EXTRA)
  356. MP_API int mp_read_radix(mp_int* a, const char* str, int radix);
  357. #endif
  358. #if defined(WOLFSSL_KEY_GEN) || !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH)
  359. MP_API int mp_prime_is_prime (mp_int * a, int t, int *result);
  360. MP_API int mp_prime_is_prime_ex (mp_int * a, int t, int *result, WC_RNG* rng);
  361. #endif /* WOLFSSL_KEY_GEN NO_RSA NO_DSA NO_DH */
  362. #ifdef WOLFSSL_KEY_GEN
  363. MP_API int mp_gcd (mp_int * a, mp_int * b, mp_int * c);
  364. MP_API int mp_lcm (mp_int * a, mp_int * b, mp_int * c);
  365. MP_API int mp_rand_prime(mp_int* a, int len, WC_RNG* rng, void* heap);
  366. #endif
  367. MP_API int mp_cnt_lsb(mp_int *a);
  368. MP_API int mp_mod_d(mp_int* a, mp_digit b, mp_digit* c);
  369. #ifdef __cplusplus
  370. }
  371. #endif
  372. #endif /* USE_INTEGER_HEAP_MATH */
  373. #endif /* WOLF_CRYPT_INTEGER_H */