You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mpi.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * mpi.h
  3. * Release $Name$
  4. *
  5. * multiple-precision integer library
  6. */
  7. /*
  8. * Copyright (c) PeerSec Networks, 2002-2006. All Rights Reserved.
  9. * The latest version of this code is available at http://www.matrixssl.org
  10. *
  11. * This software is open source; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This General Public License does NOT permit incorporating this software
  17. * into proprietary programs. If you are unable to comply with the GPL, a
  18. * commercial license for this software may be purchased from PeerSec Networks
  19. * at http://www.peersec.com
  20. *
  21. * This program is distributed in WITHOUT ANY WARRANTY; without even the
  22. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  23. * See the GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. * http://www.gnu.org/copyleft/gpl.html
  29. */
  30. /******************************************************************************/
  31. #ifndef _h_MPI
  32. #define _h_MPI
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <stdlib.h>
  36. #include <ctype.h>
  37. #include <limits.h>
  38. #undef MIN
  39. #define MIN(x,y) ((x)<(y)?(x):(y))
  40. #undef MAX
  41. #define MAX(x,y) ((x)>(y)?(x):(y))
  42. #ifdef __cplusplus
  43. extern "C" {
  44. /*
  45. C++ compilers don't like assigning void * to mp_digit *
  46. */
  47. #define OPT_CAST(x) (x *)
  48. #else
  49. /*
  50. C on the other hand doesn't care
  51. */
  52. #define OPT_CAST(x)
  53. #endif /* __cplusplus */
  54. /******************************************************************************/
  55. /*
  56. some default configurations.
  57. A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
  58. A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
  59. At the very least a mp_digit must be able to hold 7 bits
  60. [any size beyond that is ok provided it doesn't overflow the data type]
  61. */
  62. #ifdef MP_8BIT
  63. typedef unsigned char mp_digit;
  64. typedef unsigned short mp_word;
  65. #elif defined(MP_16BIT)
  66. typedef unsigned short mp_digit;
  67. typedef unsigned long mp_word;
  68. #elif defined(MP_64BIT)
  69. /*
  70. for GCC only on supported platforms
  71. */
  72. #ifndef CRYPT
  73. typedef unsigned long long ulong64;
  74. typedef signed long long long64;
  75. #endif /* CRYPT */
  76. typedef ulong64 mp_digit;
  77. typedef unsigned long mp_word __attribute__ ((mode(TI)));
  78. #define DIGIT_BIT 60
  79. #else /* MP_8BIT */
  80. /*
  81. this is the default case, 28-bit digits
  82. */
  83. #ifndef CRYPT
  84. #if defined(_MSC_VER) || defined(__BORLANDC__)
  85. typedef unsigned __int64 ulong64;
  86. typedef signed __int64 long64;
  87. #else
  88. typedef unsigned long long ulong64;
  89. typedef signed long long long64;
  90. #endif
  91. #endif /* CRYPT */
  92. typedef unsigned long mp_digit;
  93. typedef ulong64 mp_word;
  94. #ifdef MP_31BIT
  95. /*
  96. this is an extension that uses 31-bit digits
  97. */
  98. #define DIGIT_BIT 31
  99. #else /* MP_31BIT */
  100. /*
  101. default case is 28-bit digits, defines MP_28BIT as a handy macro to test
  102. */
  103. #define DIGIT_BIT 28
  104. #define MP_28BIT
  105. #endif /* MP_31BIT */
  106. #endif /* MP_8BIT */
  107. /*
  108. otherwise the bits per digit is calculated automatically from the size of
  109. a mp_digit
  110. */
  111. #ifndef DIGIT_BIT
  112. #define DIGIT_BIT ((int32)((CHAR_BIT * sizeof(mp_digit) - 1))) /* bits per digit */
  113. #endif /* DIGIT_BIT */
  114. #define MP_DIGIT_BIT DIGIT_BIT
  115. #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
  116. #define MP_DIGIT_MAX MP_MASK
  117. /******************************************************************************/
  118. /*
  119. equalities
  120. */
  121. #define MP_LT -1 /* less than */
  122. #define MP_EQ 0 /* equal to */
  123. #define MP_GT 1 /* greater than */
  124. #define MP_ZPOS 0 /* positive integer */
  125. #define MP_NEG 1 /* negative */
  126. #define MP_OKAY 0 /* ok result */
  127. #define MP_MEM -2 /* out of mem */
  128. #define MP_VAL -3 /* invalid input */
  129. #define MP_RANGE MP_VAL
  130. #define MP_YES 1 /* yes response */
  131. #define MP_NO 0 /* no response */
  132. typedef int32 mp_err;
  133. /******************************************************************************/
  134. /*
  135. various build options
  136. */
  137. #define MP_PREC 64 /* default digits of precision */
  138. /*
  139. define this to use lower memory usage routines (exptmods mostly)
  140. */
  141. #define MP_LOW_MEM
  142. /*
  143. size of comba arrays, should be at least
  144. 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2)
  145. */
  146. #define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
  147. typedef struct {
  148. int32 used, alloc, sign;
  149. mp_digit *dp;
  150. } mp_int;
  151. #define USED(m) ((m)->used)
  152. #define DIGIT(m,k) ((m)->dp[(k)])
  153. #define SIGN(m) ((m)->sign)
  154. /******************************************************************************/
  155. /*
  156. init and deinit bignum functions
  157. */
  158. /*
  159. init a bignum
  160. */
  161. extern int32 mp_init(psPool_t *pool, mp_int *a);
  162. /*
  163. free a bignum
  164. */
  165. extern void mp_clear(mp_int *a);
  166. /*
  167. init a series of arguments
  168. */
  169. extern int32 _mp_init_multi(psPool_t *pool, mp_int *mp0, mp_int *mp1, mp_int *mp2,
  170. mp_int *mp3, mp_int *mp4, mp_int *mp5, mp_int *mp6,
  171. mp_int *mp7);
  172. /*
  173. clear a series of arguments
  174. */
  175. extern void _mp_clear_multi(mp_int *mp0, mp_int *mp1, mp_int *mp2, mp_int *mp3,
  176. mp_int *mp4, mp_int *mp5, mp_int *mp6, mp_int *mp7);
  177. /*
  178. exchange two ints
  179. */
  180. extern void mp_exch(mp_int *a, mp_int *b);
  181. /*
  182. shrink ram required for a bignum
  183. */
  184. extern int32 mp_shrink(mp_int *a);
  185. /*
  186. grow an int32 to a given size
  187. */
  188. extern int32 mp_grow(mp_int *a, int32 size);
  189. /*
  190. init to a given number of digits
  191. */
  192. extern int32 mp_init_size(psPool_t *pool, mp_int *a, int32 size);
  193. /******************************************************************************/
  194. /*
  195. Basic Manipulations
  196. */
  197. #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
  198. #define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
  199. #define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
  200. extern int32 mp_add_d (mp_int * a, mp_digit b, mp_int * c);
  201. extern int32 mp_sub_d (mp_int * a, mp_digit b, mp_int * c);
  202. /*
  203. set to zero
  204. */
  205. extern void mp_zero(mp_int *a);
  206. /*
  207. set to a digit
  208. */
  209. extern void mp_set(mp_int *a, mp_digit b);
  210. /*
  211. copy, b = a
  212. */
  213. extern int32 mp_copy(mp_int *a, mp_int *b);
  214. /*
  215. inits and copies, a = b
  216. */
  217. extern int32 mp_init_copy(psPool_t *pool, mp_int *a, mp_int *b);
  218. /*
  219. trim unused digits
  220. */
  221. extern void mp_clamp(mp_int *a);
  222. /******************************************************************************/
  223. /*
  224. digit manipulation
  225. */
  226. /*
  227. right shift by "b" digits
  228. */
  229. extern void mp_rshd(mp_int *a, int32 b);
  230. /*
  231. left shift by "b" digits
  232. */
  233. extern int32 mp_lshd(mp_int *a, int32 b);
  234. /*
  235. c = a / 2**b
  236. */
  237. extern int32 mp_div_2d(psPool_t *pool, mp_int *a, int32 b, mp_int *c, mp_int *d);
  238. /*
  239. b = a/2
  240. */
  241. extern int32 mp_div_2(mp_int *a, mp_int *b);
  242. /*
  243. c = a * 2**b
  244. */
  245. extern int32 mp_mul_2d(mp_int *a, int32 b, mp_int *c);
  246. /*
  247. c = a mod 2**d
  248. */
  249. extern int32 mp_mod_2d(mp_int *a, int32 b, mp_int *c);
  250. /*
  251. computes a = 2**b
  252. */
  253. extern int32 mp_2expt(mp_int *a, int32 b);
  254. /******************************************************************************/
  255. /*
  256. Basic arithmetic
  257. */
  258. /*
  259. b = |a|
  260. */
  261. extern int32 mp_abs(mp_int *a, mp_int *b);
  262. /*
  263. compare a to b
  264. */
  265. extern int32 mp_cmp(mp_int *a, mp_int *b);
  266. /*
  267. compare |a| to |b|
  268. */
  269. extern int32 mp_cmp_mag(mp_int *a, mp_int *b);
  270. /*
  271. c = a + b
  272. */
  273. extern int32 mp_add(mp_int *a, mp_int *b, mp_int *c);
  274. /*
  275. c = a - b
  276. */
  277. extern int32 mp_sub(mp_int *a, mp_int *b, mp_int *c);
  278. /*
  279. c = a * b
  280. b = a*a
  281. */
  282. /* STEVE - moved mp_mul out of SLOW case */
  283. extern int32 mp_mul(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c);
  284. #ifdef USE_SMALL_WORD
  285. extern int32 mp_sqr(psPool_t *pool, mp_int *a, mp_int *b);
  286. #endif
  287. /*
  288. a/b => cb + d == a
  289. */
  290. extern int32 mp_div(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c, mp_int *d);
  291. /*
  292. c = a mod b, 0 <= c < b
  293. */
  294. extern int32 mp_mod(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c);
  295. /******************************************************************************/
  296. /*
  297. single digit functions
  298. */
  299. /*
  300. compare against a single digit
  301. */
  302. extern int32 mp_cmp_d(mp_int *a, mp_digit b);
  303. /*
  304. c = a * b
  305. */
  306. extern int32 mp_mul_d(mp_int *a, mp_digit b, mp_int *c);
  307. /******************************************************************************/
  308. /*
  309. number theory
  310. */
  311. /*
  312. d = a + b (mod c)
  313. */
  314. extern int32 mp_addmod(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c, mp_int *d);
  315. /*
  316. d = a * b (mod c)
  317. */
  318. extern int32 mp_mulmod(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c, mp_int *d);
  319. /*
  320. c = 1/a (mod b)
  321. */
  322. #ifdef USE_SMALL_WORD
  323. extern int32 mp_invmod(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c);
  324. #endif
  325. /*
  326. setups the montgomery reduction
  327. */
  328. extern int32 mp_montgomery_setup(mp_int *a, mp_digit *mp);
  329. /*
  330. computes a = B**n mod b without division or multiplication useful for
  331. normalizing numbers in a Montgomery system.
  332. */
  333. extern int32 mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
  334. /*
  335. computes x/R == x (mod N) via Montgomery Reduction
  336. */
  337. #ifdef USE_SMALL_WORD
  338. extern int32 mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
  339. #endif
  340. /*
  341. d = a**b (mod c)
  342. */
  343. /* TODO - we never define this */
  344. extern int32 mp_exptmod(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c, mp_int *d);
  345. /******************************************************************************/
  346. /*
  347. If we're using 1024 or 2048 bit keys and 28 bit digits, we only need the
  348. fast_ versions of these functions, removing the others to save space.
  349. Otherwise, we include the slow versions as well and which version to use
  350. is done at runtime.
  351. */
  352. #ifdef USE_SMALL_WORD
  353. extern int32 s_mp_mul_digs(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c,
  354. int32 digs);
  355. extern int32 s_mp_sqr(psPool_t *pool, mp_int *a, mp_int *b);
  356. #else
  357. #define mp_montgomery_reduce fast_mp_montgomery_reduce
  358. #define mp_sqr fast_s_mp_sqr
  359. #if STEVE
  360. #define mp_mul(P, A, B, C) fast_s_mp_mul_digs(P, A, B, C, (A)->used + (B)->used + 1)
  361. #endif
  362. #define s_mp_mul_digs fast_s_mp_mul_digs
  363. #define mp_invmod fast_mp_invmod
  364. #endif
  365. /******************************************************************************/
  366. /*
  367. radix conversion
  368. */
  369. extern int32 mp_count_bits(mp_int *a);
  370. extern int32 mp_unsigned_bin_size(mp_int *a);
  371. extern int32 mp_read_unsigned_bin(mp_int *a, unsigned char *b, int32 c);
  372. extern int32 mp_to_unsigned_bin(psPool_t *pool, mp_int *a, unsigned char *b);
  373. extern int32 mp_signed_bin_size(mp_int *a);
  374. /*
  375. lowlevel functions, do not call!
  376. */
  377. #if STEVE
  378. #ifdef USE_SMALL_WORD
  379. #define s_mp_mul(P, A, B, C) s_mp_mul_digs(P, A, B, C, (A)->used + (B)->used + 1)
  380. #else
  381. #define s_mp_mul(P, A, B, C) sslAssert();
  382. #endif
  383. #endif /* STEVE */
  384. /* define this in all cases for now STEVE */
  385. #define s_mp_mul(P, A, B, C) s_mp_mul_digs(P, A, B, C, (A)->used + (B)->used + 1)
  386. /*
  387. b = a*2
  388. */
  389. extern int32 mp_mul_2(mp_int *a, mp_int *b);
  390. extern int32 s_mp_add(mp_int *a, mp_int *b, mp_int *c);
  391. extern int32 s_mp_sub(mp_int *a, mp_int *b, mp_int *c);
  392. extern int32 fast_s_mp_mul_digs(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c,
  393. int32 digs);
  394. extern int32 fast_s_mp_sqr(psPool_t *pool, mp_int *a, mp_int *b);
  395. extern int32 fast_mp_invmod(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c);
  396. extern int32 fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
  397. extern void bn_reverse(unsigned char *s, int32 len);
  398. #ifdef __cplusplus
  399. }
  400. #endif /* __cplusplus */
  401. #endif /* _h_MPI */