ltc_tommath.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /* LibTomMath, multiple-precision integer library -- Tom St Denis
  2. *
  3. * LibTomMath is a library that provides multiple-precision
  4. * integer arithmetic as well as number theoretic functionality.
  5. *
  6. * The library was designed directly after the MPI library by
  7. * Michael Fromberger but has been written from scratch with
  8. * additional optimizations in place.
  9. *
  10. * The library is free for all purposes without any express
  11. * guarantee it works.
  12. *
  13. * Tom St Denis, [email protected], http://math.libtomcrypt.org
  14. */
  15. #ifndef BN_H_
  16. #define BN_H_
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <ctype.h>
  21. #include <limits.h>
  22. #include <mycrypt_custom.h>
  23. #undef MIN
  24. #define MIN(x,y) ((x)<(y)?(x):(y))
  25. #undef MAX
  26. #define MAX(x,y) ((x)>(y)?(x):(y))
  27. #ifdef __cplusplus
  28. extern "C" {
  29. /* C++ compilers don't like assigning void * to mp_digit * */
  30. #define OPT_CAST(x) (x *)
  31. #else
  32. /* C on the other hand doesn't care */
  33. #define OPT_CAST(x)
  34. #endif
  35. /* some default configurations.
  36. *
  37. * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
  38. * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
  39. *
  40. * At the very least a mp_digit must be able to hold 7 bits
  41. * [any size beyond that is ok provided it doesn't overflow the data type]
  42. */
  43. #ifdef MP_8BIT
  44. typedef unsigned char mp_digit;
  45. typedef unsigned short mp_word;
  46. #elif defined(MP_16BIT)
  47. typedef unsigned short mp_digit;
  48. typedef unsigned long mp_word;
  49. #elif defined(MP_64BIT)
  50. /* for GCC only on supported platforms */
  51. #ifndef CRYPT
  52. typedef unsigned long long ulong64;
  53. typedef signed long long long64;
  54. #endif
  55. typedef ulong64 mp_digit;
  56. typedef unsigned long mp_word __attribute__ ((mode(TI)));
  57. #define DIGIT_BIT 60
  58. #else
  59. /* this is the default case, 28-bit digits */
  60. /* this is to make porting into LibTomCrypt easier :-) */
  61. #ifndef CRYPT
  62. #if defined(_MSC_VER) || defined(__BORLANDC__)
  63. typedef unsigned __int64 ulong64;
  64. typedef signed __int64 long64;
  65. #else
  66. typedef unsigned long long ulong64;
  67. typedef signed long long long64;
  68. #endif
  69. #endif
  70. typedef unsigned long mp_digit;
  71. typedef ulong64 mp_word;
  72. #ifdef MP_31BIT
  73. /* this is an extension that uses 31-bit digits */
  74. #define DIGIT_BIT 31
  75. #else
  76. /* default case is 28-bit digits, defines MP_28BIT as a handy macro to test */
  77. #define DIGIT_BIT 28
  78. #define MP_28BIT
  79. #endif
  80. #endif
  81. /* define heap macros */
  82. #ifndef CRYPT
  83. /* default to libc stuff */
  84. #ifndef XMALLOC
  85. #define XMALLOC malloc
  86. #define XFREE free
  87. #define XREALLOC realloc
  88. #define XCALLOC calloc
  89. #define XMEMSET memset
  90. #define XMEMCPY memcpy
  91. #else
  92. /* prototypes for our heap functions */
  93. void *XMALLOC(size_t n);
  94. void *REALLOC(void *p, size_t n);
  95. void *XCALLOC(size_t n, size_t s);
  96. void XFREE(void *p);
  97. void *XMEMCPY(void *dest, const void *src, size_t n);
  98. int XMEMCMP(const void *s1, const void *s2, size_t n);
  99. #endif
  100. #endif
  101. /* otherwise the bits per digit is calculated automatically from the size of a mp_digit */
  102. #ifndef DIGIT_BIT
  103. #define DIGIT_BIT ((int)((CHAR_BIT * sizeof(mp_digit) - 1))) /* bits per digit */
  104. #endif
  105. #define MP_DIGIT_BIT DIGIT_BIT
  106. #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
  107. #define MP_DIGIT_MAX MP_MASK
  108. /* equalities */
  109. #define MP_LT -1 /* less than */
  110. #define MP_EQ 0 /* equal to */
  111. #define MP_GT 1 /* greater than */
  112. #define MP_ZPOS 0 /* positive integer */
  113. #define MP_NEG 1 /* negative */
  114. #define MP_OKAY 0 /* ok result */
  115. #define MP_MEM -2 /* out of mem */
  116. #define MP_VAL -3 /* invalid input */
  117. #define MP_RANGE MP_VAL
  118. #define MP_YES 1 /* yes response */
  119. #define MP_NO 0 /* no response */
  120. /* Primality generation flags */
  121. #define LTM_PRIME_BBS 0x0001 /* BBS style prime */
  122. #define LTM_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */
  123. #define LTM_PRIME_2MSB_OFF 0x0004 /* force 2nd MSB to 0 */
  124. #define LTM_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */
  125. typedef int mp_err;
  126. /* you'll have to tune these... */
  127. extern int KARATSUBA_MUL_CUTOFF,
  128. KARATSUBA_SQR_CUTOFF,
  129. TOOM_MUL_CUTOFF,
  130. TOOM_SQR_CUTOFF;
  131. /* define this to use lower memory usage routines (exptmods mostly) */
  132. /* #define MP_LOW_MEM */
  133. /* default precision */
  134. #ifndef MP_PREC
  135. #ifdef MP_LOW_MEM
  136. #define MP_PREC 64 /* default digits of precision */
  137. #else
  138. #define MP_PREC 8 /* default digits of precision */
  139. #endif
  140. #endif
  141. /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - 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 how many read [upto len] */
  149. typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
  150. #define USED(m) ((m)->used)
  151. #define DIGIT(m,k) ((m)->dp[(k)])
  152. #define SIGN(m) ((m)->sign)
  153. /* error code to char* string */
  154. char *mp_error_to_string(int code);
  155. /* ---> init and deinit bignum functions <--- */
  156. /* init a bignum */
  157. int mp_init(mp_int *a);
  158. /* free a bignum */
  159. void mp_clear(mp_int *a);
  160. /* init a null terminated series of arguments */
  161. int mp_init_multi(mp_int *mp, ...);
  162. /* clear a null terminated series of arguments */
  163. void mp_clear_multi(mp_int *mp, ...);
  164. /* exchange two ints */
  165. void mp_exch(mp_int *a, mp_int *b);
  166. /* shrink ram required for a bignum */
  167. int mp_shrink(mp_int *a);
  168. /* grow an int to a given size */
  169. int mp_grow(mp_int *a, int size);
  170. /* init to a given number of digits */
  171. int mp_init_size(mp_int *a, int size);
  172. /* ---> Basic Manipulations <--- */
  173. #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
  174. #define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
  175. #define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
  176. /* set to zero */
  177. void mp_zero(mp_int *a);
  178. /* set to a digit */
  179. void mp_set(mp_int *a, mp_digit b);
  180. /* set a 32-bit const */
  181. int mp_set_int(mp_int *a, unsigned long b);
  182. /* get a 32-bit value */
  183. unsigned long mp_get_int(mp_int * a);
  184. /* initialize and set a digit */
  185. int mp_init_set (mp_int * a, mp_digit b);
  186. /* initialize and set 32-bit value */
  187. int mp_init_set_int (mp_int * a, unsigned long b);
  188. /* copy, b = a */
  189. int mp_copy(mp_int *a, mp_int *b);
  190. /* inits and copies, a = b */
  191. int mp_init_copy(mp_int *a, mp_int *b);
  192. /* trim unused digits */
  193. void mp_clamp(mp_int *a);
  194. /* ---> digit manipulation <--- */
  195. /* right shift by "b" digits */
  196. void mp_rshd(mp_int *a, int b);
  197. /* left shift by "b" digits */
  198. int mp_lshd(mp_int *a, int b);
  199. /* c = a / 2**b */
  200. int mp_div_2d(mp_int *a, int b, mp_int *c, mp_int *d);
  201. /* b = a/2 */
  202. int mp_div_2(mp_int *a, mp_int *b);
  203. /* c = a * 2**b */
  204. int mp_mul_2d(mp_int *a, int b, mp_int *c);
  205. /* b = a*2 */
  206. int mp_mul_2(mp_int *a, mp_int *b);
  207. /* c = a mod 2**d */
  208. int mp_mod_2d(mp_int *a, int b, mp_int *c);
  209. /* computes a = 2**b */
  210. int mp_2expt(mp_int *a, int b);
  211. /* Counts the number of lsbs which are zero before the first zero bit */
  212. int mp_cnt_lsb(mp_int *a);
  213. /* I Love Earth! */
  214. /* makes a pseudo-random int of a given size */
  215. int mp_rand(mp_int *a, int digits);
  216. /* ---> binary operations <--- */
  217. /* c = a XOR b */
  218. int mp_xor(mp_int *a, mp_int *b, mp_int *c);
  219. /* c = a OR b */
  220. int mp_or(mp_int *a, mp_int *b, mp_int *c);
  221. /* c = a AND b */
  222. int mp_and(mp_int *a, mp_int *b, mp_int *c);
  223. /* ---> Basic arithmetic <--- */
  224. /* b = -a */
  225. int mp_neg(mp_int *a, mp_int *b);
  226. /* b = |a| */
  227. int mp_abs(mp_int *a, mp_int *b);
  228. /* compare a to b */
  229. int mp_cmp(mp_int *a, mp_int *b);
  230. /* compare |a| to |b| */
  231. int mp_cmp_mag(mp_int *a, mp_int *b);
  232. /* c = a + b */
  233. int mp_add(mp_int *a, mp_int *b, mp_int *c);
  234. /* c = a - b */
  235. int mp_sub(mp_int *a, mp_int *b, mp_int *c);
  236. /* c = a * b */
  237. int mp_mul(mp_int *a, mp_int *b, mp_int *c);
  238. /* b = a*a */
  239. int mp_sqr(mp_int *a, mp_int *b);
  240. /* a/b => cb + d == a */
  241. int mp_div(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
  242. /* c = a mod b, 0 <= c < b */
  243. int mp_mod(mp_int *a, mp_int *b, mp_int *c);
  244. /* ---> single digit functions <--- */
  245. /* compare against a single digit */
  246. int mp_cmp_d(mp_int *a, mp_digit b);
  247. /* c = a + b */
  248. int mp_add_d(mp_int *a, mp_digit b, mp_int *c);
  249. /* c = a - b */
  250. int mp_sub_d(mp_int *a, mp_digit b, mp_int *c);
  251. /* c = a * b */
  252. int mp_mul_d(mp_int *a, mp_digit b, mp_int *c);
  253. /* a/b => cb + d == a */
  254. int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d);
  255. /* a/3 => 3c + d == a */
  256. int mp_div_3(mp_int *a, mp_int *c, mp_digit *d);
  257. /* c = a**b */
  258. int mp_expt_d(mp_int *a, mp_digit b, mp_int *c);
  259. /* c = a mod b, 0 <= c < b */
  260. int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c);
  261. /* ---> number theory <--- */
  262. /* d = a + b (mod c) */
  263. int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
  264. /* d = a - b (mod c) */
  265. int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
  266. /* d = a * b (mod c) */
  267. int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
  268. /* c = a * a (mod b) */
  269. int mp_sqrmod(mp_int *a, mp_int *b, mp_int *c);
  270. /* c = 1/a (mod b) */
  271. int mp_invmod(mp_int *a, mp_int *b, mp_int *c);
  272. /* c = (a, b) */
  273. int mp_gcd(mp_int *a, mp_int *b, mp_int *c);
  274. /* produces value such that U1*a + U2*b = U3 */
  275. int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3);
  276. /* c = [a, b] or (a*b)/(a, b) */
  277. int mp_lcm(mp_int *a, mp_int *b, mp_int *c);
  278. /* finds one of the b'th root of a, such that |c|**b <= |a|
  279. *
  280. * returns error if a < 0 and b is even
  281. */
  282. int mp_n_root(mp_int *a, mp_digit b, mp_int *c);
  283. /* special sqrt algo */
  284. int mp_sqrt(mp_int *arg, mp_int *ret);
  285. /* is number a square? */
  286. int mp_is_square(mp_int *arg, int *ret);
  287. /* computes the jacobi c = (a | n) (or Legendre if b is prime) */
  288. int mp_jacobi(mp_int *a, mp_int *n, int *c);
  289. /* used to setup the Barrett reduction for a given modulus b */
  290. int mp_reduce_setup(mp_int *a, mp_int *b);
  291. /* Barrett Reduction, computes a (mod b) with a precomputed value c
  292. *
  293. * Assumes that 0 < a <= b*b, note if 0 > a > -(b*b) then you can merely
  294. * compute the reduction as -1 * mp_reduce(mp_abs(a)) [pseudo code].
  295. */
  296. int mp_reduce(mp_int *a, mp_int *b, mp_int *c);
  297. /* setups the montgomery reduction */
  298. int mp_montgomery_setup(mp_int *a, mp_digit *mp);
  299. /* computes a = B**n mod b without division or multiplication useful for
  300. * normalizing numbers in a Montgomery system.
  301. */
  302. int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
  303. /* computes x/R == x (mod N) via Montgomery Reduction */
  304. int mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
  305. /* returns 1 if a is a valid DR modulus */
  306. int mp_dr_is_modulus(mp_int *a);
  307. /* sets the value of "d" required for mp_dr_reduce */
  308. void mp_dr_setup(mp_int *a, mp_digit *d);
  309. /* reduces a modulo b using the Diminished Radix method */
  310. int mp_dr_reduce(mp_int *a, mp_int *b, mp_digit mp);
  311. /* returns true if a can be reduced with mp_reduce_2k */
  312. int mp_reduce_is_2k(mp_int *a);
  313. /* determines k value for 2k reduction */
  314. int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
  315. /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
  316. int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d);
  317. /* d = a**b (mod c) */
  318. int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
  319. /* ---> Primes <--- */
  320. /* number of primes */
  321. #ifdef MP_8BIT
  322. #define PRIME_SIZE 31
  323. #else
  324. #define PRIME_SIZE 256
  325. #endif
  326. /* table of first PRIME_SIZE primes */
  327. extern const mp_digit __prime_tab[];
  328. /* result=1 if a is divisible by one of the first PRIME_SIZE primes */
  329. int mp_prime_is_divisible(mp_int *a, int *result);
  330. /* performs one Fermat test of "a" using base "b".
  331. * Sets result to 0 if composite or 1 if probable prime
  332. */
  333. int mp_prime_fermat(mp_int *a, mp_int *b, int *result);
  334. /* performs one Miller-Rabin test of "a" using base "b".
  335. * Sets result to 0 if composite or 1 if probable prime
  336. */
  337. int mp_prime_miller_rabin(mp_int *a, mp_int *b, int *result);
  338. /* This gives [for a given bit size] the number of trials required
  339. * such that Miller-Rabin gives a prob of failure lower than 2^-96
  340. */
  341. int mp_prime_rabin_miller_trials(int size);
  342. /* performs t rounds of Miller-Rabin on "a" using the first
  343. * t prime bases. Also performs an initial sieve of trial
  344. * division. Determines if "a" is prime with probability
  345. * of error no more than (1/4)**t.
  346. *
  347. * Sets result to 1 if probably prime, 0 otherwise
  348. */
  349. int mp_prime_is_prime(mp_int *a, int t, int *result);
  350. /* finds the next prime after the number "a" using "t" trials
  351. * of Miller-Rabin.
  352. *
  353. * bbs_style = 1 means the prime must be congruent to 3 mod 4
  354. */
  355. int mp_prime_next_prime(mp_int *a, int t, int bbs_style);
  356. /* makes a truly random prime of a given size (bytes),
  357. * call with bbs = 1 if you want it to be congruent to 3 mod 4
  358. *
  359. * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
  360. * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
  361. * so it can be NULL
  362. *
  363. * The prime generated will be larger than 2^(8*size).
  364. */
  365. #define mp_prime_random(a, t, size, bbs, cb, dat) mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?LTM_PRIME_BBS:0, cb, dat)
  366. /* makes a truly random prime of a given size (bits),
  367. *
  368. * Flags are as follows:
  369. *
  370. * LTM_PRIME_BBS - make prime congruent to 3 mod 4
  371. * LTM_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS)
  372. * LTM_PRIME_2MSB_OFF - make the 2nd highest bit zero
  373. * LTM_PRIME_2MSB_ON - make the 2nd highest bit one
  374. *
  375. * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
  376. * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
  377. * so it can be NULL
  378. *
  379. */
  380. int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat);
  381. /* ---> radix conversion <--- */
  382. int mp_count_bits(mp_int *a);
  383. int mp_unsigned_bin_size(mp_int *a);
  384. int mp_read_unsigned_bin(mp_int *a, unsigned char *b, int c);
  385. int mp_to_unsigned_bin(mp_int *a, unsigned char *b);
  386. int mp_signed_bin_size(mp_int *a);
  387. int mp_read_signed_bin(mp_int *a, unsigned char *b, int c);
  388. int mp_to_signed_bin(mp_int *a, unsigned char *b);
  389. int mp_read_radix(mp_int *a, char *str, int radix);
  390. int mp_toradix(mp_int *a, char *str, int radix);
  391. int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen);
  392. int mp_radix_size(mp_int *a, int radix, int *size);
  393. int mp_fread(mp_int *a, int radix, FILE *stream);
  394. int mp_fwrite(mp_int *a, int radix, FILE *stream);
  395. #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
  396. #define mp_raw_size(mp) mp_signed_bin_size(mp)
  397. #define mp_toraw(mp, str) mp_to_signed_bin((mp), (str))
  398. #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
  399. #define mp_mag_size(mp) mp_unsigned_bin_size(mp)
  400. #define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str))
  401. #define mp_tobinary(M, S) mp_toradix((M), (S), 2)
  402. #define mp_tooctal(M, S) mp_toradix((M), (S), 8)
  403. #define mp_todecimal(M, S) mp_toradix((M), (S), 10)
  404. #define mp_tohex(M, S) mp_toradix((M), (S), 16)
  405. /* lowlevel functions, do not call! */
  406. int s_mp_add(mp_int *a, mp_int *b, mp_int *c);
  407. int s_mp_sub(mp_int *a, mp_int *b, mp_int *c);
  408. #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
  409. int fast_s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
  410. int s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
  411. int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
  412. int s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
  413. int fast_s_mp_sqr(mp_int *a, mp_int *b);
  414. int s_mp_sqr(mp_int *a, mp_int *b);
  415. int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c);
  416. int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c);
  417. int mp_karatsuba_sqr(mp_int *a, mp_int *b);
  418. int mp_toom_sqr(mp_int *a, mp_int *b);
  419. int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c);
  420. int fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
  421. int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int mode);
  422. int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y);
  423. void bn_reverse(unsigned char *s, int len);
  424. const char *mp_s_rmap;
  425. #ifdef __cplusplus
  426. }
  427. #endif
  428. #endif