bignum.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /**
  2. * \file bignum.h
  3. *
  4. * \brief Multi-precision integer library
  5. *
  6. * Copyright (C) 2006-2010, Brainspark B.V.
  7. *
  8. * This file is part of PolarSSL (http://www.polarssl.org)
  9. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  10. *
  11. * All rights reserved.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License along
  24. * with this program; if not, write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  26. */
  27. #ifndef POLARSSL_BIGNUM_H
  28. #define POLARSSL_BIGNUM_H
  29. #include <stdio.h>
  30. #include <string.h>
  31. #define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
  32. #define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
  33. #define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
  34. #define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The output buffer is too small to write too. */
  35. #define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
  36. #define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
  37. #define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
  38. #define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
  39. /*
  40. * Maximum size MPIs are allowed to grow to in number of limbs.
  41. */
  42. #define POLARSSL_MPI_MAX_LIMBS 10000
  43. /*
  44. * Define the base integer type, architecture-wise
  45. */
  46. #if defined(POLARSSL_HAVE_INT8)
  47. typedef signed char t_sint;
  48. typedef unsigned char t_uint;
  49. typedef unsigned short t_udbl;
  50. #else
  51. #if defined(POLARSSL_HAVE_INT16)
  52. typedef signed short t_sint;
  53. typedef unsigned short t_uint;
  54. typedef unsigned long t_udbl;
  55. #else
  56. typedef signed long t_sint;
  57. typedef unsigned long t_uint;
  58. #if defined(_MSC_VER) && defined(_M_IX86)
  59. typedef unsigned __int64 t_udbl;
  60. #else
  61. #if defined(__amd64__) || defined(__x86_64__) || \
  62. defined(__ppc64__) || defined(__powerpc64__) || \
  63. defined(__ia64__) || defined(__alpha__)
  64. typedef unsigned int t_udbl __attribute__((mode(TI)));
  65. #else
  66. #if defined(POLARSSL_HAVE_LONGLONG)
  67. typedef unsigned long long t_udbl;
  68. #endif
  69. #endif
  70. #endif
  71. #endif
  72. #endif
  73. /**
  74. * \brief MPI structure
  75. */
  76. typedef struct
  77. {
  78. int s; /*!< integer sign */
  79. size_t n; /*!< total # of limbs */
  80. t_uint *p; /*!< pointer to limbs */
  81. }
  82. mpi;
  83. #ifdef __cplusplus
  84. extern "C" {
  85. #endif
  86. /**
  87. * \brief Initialize one MPI
  88. *
  89. * \param X One MPI to initialize.
  90. */
  91. void mpi_init( mpi *X );
  92. /**
  93. * \brief Unallocate one MPI
  94. *
  95. * \param X One MPI to unallocate.
  96. */
  97. void mpi_free( mpi *X );
  98. /**
  99. * \brief Enlarge to the specified number of limbs
  100. *
  101. * \param X MPI to grow
  102. * \param nblimbs The target number of limbs
  103. *
  104. * \return 0 if successful,
  105. * 1 if memory allocation failed
  106. */
  107. int mpi_grow( mpi *X, size_t nblimbs );
  108. /**
  109. * \brief Copy the contents of Y into X
  110. *
  111. * \param X Destination MPI
  112. * \param Y Source MPI
  113. *
  114. * \return 0 if successful,
  115. * 1 if memory allocation failed
  116. */
  117. int mpi_copy( mpi *X, const mpi *Y );
  118. /**
  119. * \brief Swap the contents of X and Y
  120. *
  121. * \param X First MPI value
  122. * \param Y Second MPI value
  123. */
  124. void mpi_swap( mpi *X, mpi *Y );
  125. /**
  126. * \brief Set value from integer
  127. *
  128. * \param X MPI to set
  129. * \param z Value to use
  130. *
  131. * \return 0 if successful,
  132. * 1 if memory allocation failed
  133. */
  134. int mpi_lset( mpi *X, t_sint z );
  135. /*
  136. * \brief Get a specific bit from X
  137. *
  138. * \param X MPI to use
  139. * \param pos Zero-based index of the bit in X
  140. *
  141. * \return Either a 0 or a 1
  142. */
  143. int mpi_get_bit( mpi *X, size_t pos );
  144. /*
  145. * \brief Set a bit of X to a specific value of 0 or 1
  146. *
  147. * \note Will grow X if necessary to set a bit to 1 in a not yet
  148. * existing limb. Will not grow if bit should be set to 0
  149. *
  150. * \param X MPI to use
  151. * \param pos Zero-based index of the bit in X
  152. * \param val The value to set the bit to (0 or 1)
  153. *
  154. * \return 0 if successful,
  155. * 1 if memory allocation failed,
  156. * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
  157. */
  158. int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
  159. /**
  160. * \brief Return the number of least significant bits
  161. *
  162. * \param X MPI to use
  163. */
  164. size_t mpi_lsb( const mpi *X );
  165. /**
  166. * \brief Return the number of most significant bits
  167. *
  168. * \param X MPI to use
  169. */
  170. size_t mpi_msb( const mpi *X );
  171. /**
  172. * \brief Return the total size in bytes
  173. *
  174. * \param X MPI to use
  175. */
  176. size_t mpi_size( const mpi *X );
  177. /**
  178. * \brief Import from an ASCII string
  179. *
  180. * \param X Destination MPI
  181. * \param radix Input numeric base
  182. * \param s Null-terminated string buffer
  183. *
  184. * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
  185. */
  186. int mpi_read_string( mpi *X, int radix, const char *s );
  187. /**
  188. * \brief Export into an ASCII string
  189. *
  190. * \param X Source MPI
  191. * \param radix Output numeric base
  192. * \param s String buffer
  193. * \param slen String buffer size
  194. *
  195. * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code.
  196. * *slen is always updated to reflect the amount
  197. * of data that has (or would have) been written.
  198. *
  199. * \note Call this function with *slen = 0 to obtain the
  200. * minimum required buffer size in *slen.
  201. */
  202. int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
  203. /**
  204. * \brief Read X from an opened file
  205. *
  206. * \param X Destination MPI
  207. * \param radix Input numeric base
  208. * \param fin Input file handle
  209. *
  210. * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
  211. */
  212. //int mpi_read_file( mpi *X, int radix, FILE *fin );
  213. /**
  214. * \brief Write X into an opened file, or stdout if fout is NULL
  215. *
  216. * \param p Prefix, can be NULL
  217. * \param X Source MPI
  218. * \param radix Output numeric base
  219. * \param fout Output file handle (can be NULL)
  220. *
  221. * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
  222. *
  223. * \note Set fout == NULL to print X on the console.
  224. */
  225. //int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
  226. /**
  227. * \brief Import X from unsigned binary data, big endian
  228. *
  229. * \param X Destination MPI
  230. * \param buf Input buffer
  231. * \param buflen Input buffer size
  232. *
  233. * \return 0 if successful,
  234. * 1 if memory allocation failed
  235. */
  236. int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
  237. /**
  238. * \brief Export X into unsigned binary data, big endian
  239. *
  240. * \param X Source MPI
  241. * \param buf Output buffer
  242. * \param buflen Output buffer size
  243. *
  244. * \return 0 if successful,
  245. * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
  246. */
  247. int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
  248. /**
  249. * \brief Left-shift: X <<= count
  250. *
  251. * \param X MPI to shift
  252. * \param count Amount to shift
  253. *
  254. * \return 0 if successful,
  255. * 1 if memory allocation failed
  256. */
  257. int mpi_shift_l( mpi *X, size_t count );
  258. /**
  259. * \brief Right-shift: X >>= count
  260. *
  261. * \param X MPI to shift
  262. * \param count Amount to shift
  263. *
  264. * \return 0 if successful,
  265. * 1 if memory allocation failed
  266. */
  267. int mpi_shift_r( mpi *X, size_t count );
  268. /**
  269. * \brief Compare unsigned values
  270. *
  271. * \param X Left-hand MPI
  272. * \param Y Right-hand MPI
  273. *
  274. * \return 1 if |X| is greater than |Y|,
  275. * -1 if |X| is lesser than |Y| or
  276. * 0 if |X| is equal to |Y|
  277. */
  278. int mpi_cmp_abs( const mpi *X, const mpi *Y );
  279. /**
  280. * \brief Compare signed values
  281. *
  282. * \param X Left-hand MPI
  283. * \param Y Right-hand MPI
  284. *
  285. * \return 1 if X is greater than Y,
  286. * -1 if X is lesser than Y or
  287. * 0 if X is equal to Y
  288. */
  289. int mpi_cmp_mpi( const mpi *X, const mpi *Y );
  290. /**
  291. * \brief Compare signed values
  292. *
  293. * \param X Left-hand MPI
  294. * \param z The integer value to compare to
  295. *
  296. * \return 1 if X is greater than z,
  297. * -1 if X is lesser than z or
  298. * 0 if X is equal to z
  299. */
  300. int mpi_cmp_int( const mpi *X, t_sint z );
  301. /**
  302. * \brief Unsigned addition: X = |A| + |B|
  303. *
  304. * \param X Destination MPI
  305. * \param A Left-hand MPI
  306. * \param B Right-hand MPI
  307. *
  308. * \return 0 if successful,
  309. * 1 if memory allocation failed
  310. */
  311. int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
  312. /**
  313. * \brief Unsigned substraction: X = |A| - |B|
  314. *
  315. * \param X Destination MPI
  316. * \param A Left-hand MPI
  317. * \param B Right-hand MPI
  318. *
  319. * \return 0 if successful,
  320. * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
  321. */
  322. int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
  323. /**
  324. * \brief Signed addition: X = A + B
  325. *
  326. * \param X Destination MPI
  327. * \param A Left-hand MPI
  328. * \param B Right-hand MPI
  329. *
  330. * \return 0 if successful,
  331. * 1 if memory allocation failed
  332. */
  333. int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
  334. /**
  335. * \brief Signed substraction: X = A - B
  336. *
  337. * \param X Destination MPI
  338. * \param A Left-hand MPI
  339. * \param B Right-hand MPI
  340. *
  341. * \return 0 if successful,
  342. * 1 if memory allocation failed
  343. */
  344. int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
  345. /**
  346. * \brief Signed addition: X = A + b
  347. *
  348. * \param X Destination MPI
  349. * \param A Left-hand MPI
  350. * \param b The integer value to add
  351. *
  352. * \return 0 if successful,
  353. * 1 if memory allocation failed
  354. */
  355. int mpi_add_int( mpi *X, const mpi *A, t_sint b );
  356. /**
  357. * \brief Signed substraction: X = A - b
  358. *
  359. * \param X Destination MPI
  360. * \param A Left-hand MPI
  361. * \param b The integer value to subtract
  362. *
  363. * \return 0 if successful,
  364. * 1 if memory allocation failed
  365. */
  366. int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
  367. /**
  368. * \brief Baseline multiplication: X = A * B
  369. *
  370. * \param X Destination MPI
  371. * \param A Left-hand MPI
  372. * \param B Right-hand MPI
  373. *
  374. * \return 0 if successful,
  375. * 1 if memory allocation failed
  376. */
  377. int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
  378. /**
  379. * \brief Baseline multiplication: X = A * b
  380. * Note: b is an unsigned integer type, thus
  381. * Negative values of b are ignored.
  382. *
  383. * \param X Destination MPI
  384. * \param A Left-hand MPI
  385. * \param b The integer value to multiply with
  386. *
  387. * \return 0 if successful,
  388. * 1 if memory allocation failed
  389. */
  390. int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
  391. /**
  392. * \brief Division by mpi: A = Q * B + R
  393. *
  394. * \param Q Destination MPI for the quotient
  395. * \param R Destination MPI for the rest value
  396. * \param A Left-hand MPI
  397. * \param B Right-hand MPI
  398. *
  399. * \return 0 if successful,
  400. * 1 if memory allocation failed,
  401. * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
  402. *
  403. * \note Either Q or R can be NULL.
  404. */
  405. int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
  406. /**
  407. * \brief Division by int: A = Q * b + R
  408. *
  409. * \param Q Destination MPI for the quotient
  410. * \param R Destination MPI for the rest value
  411. * \param A Left-hand MPI
  412. * \param b Integer to divide by
  413. *
  414. * \return 0 if successful,
  415. * 1 if memory allocation failed,
  416. * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
  417. *
  418. * \note Either Q or R can be NULL.
  419. */
  420. int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
  421. /**
  422. * \brief Modulo: R = A mod B
  423. *
  424. * \param R Destination MPI for the rest value
  425. * \param A Left-hand MPI
  426. * \param B Right-hand MPI
  427. *
  428. * \return 0 if successful,
  429. * 1 if memory allocation failed,
  430. * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
  431. * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
  432. */
  433. int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
  434. /**
  435. * \brief Modulo: r = A mod b
  436. *
  437. * \param r Destination t_uint
  438. * \param A Left-hand MPI
  439. * \param b Integer to divide by
  440. *
  441. * \return 0 if successful,
  442. * 1 if memory allocation failed,
  443. * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
  444. * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
  445. */
  446. int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
  447. /**
  448. * \brief Sliding-window exponentiation: X = A^E mod N
  449. *
  450. * \param X Destination MPI
  451. * \param A Left-hand MPI
  452. * \param E Exponent MPI
  453. * \param N Modular MPI
  454. * \param _RR Speed-up MPI used for recalculations
  455. *
  456. * \return 0 if successful,
  457. * 1 if memory allocation failed,
  458. * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
  459. *
  460. * \note _RR is used to avoid re-computing R*R mod N across
  461. * multiple calls, which speeds up things a bit. It can
  462. * be set to NULL if the extra performance is unneeded.
  463. */
  464. int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
  465. /**
  466. * \brief Fill an MPI X with size bytes of random
  467. *
  468. * \param X Destination MPI
  469. * \param size Size in bytes
  470. * \param f_rng RNG function
  471. * \param p_rng RNG parameter
  472. *
  473. * \return 0 if successful,
  474. * 1 if memory allocation failed
  475. */
  476. int mpi_fill_random( mpi *X, size_t size, int (*f_rng)(void *), void *p_rng );
  477. /**
  478. * \brief Greatest common divisor: G = gcd(A, B)
  479. *
  480. * \param G Destination MPI
  481. * \param A Left-hand MPI
  482. * \param B Right-hand MPI
  483. *
  484. * \return 0 if successful,
  485. * 1 if memory allocation failed
  486. */
  487. int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
  488. /**
  489. * \brief Modular inverse: X = A^-1 mod N
  490. *
  491. * \param X Destination MPI
  492. * \param A Left-hand MPI
  493. * \param N Right-hand MPI
  494. *
  495. * \return 0 if successful,
  496. * 1 if memory allocation failed,
  497. * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
  498. POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
  499. */
  500. int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
  501. /**
  502. * \brief Miller-Rabin primality test
  503. *
  504. * \param X MPI to check
  505. * \param f_rng RNG function
  506. * \param p_rng RNG parameter
  507. *
  508. * \return 0 if successful (probably prime),
  509. * 1 if memory allocation failed,
  510. * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
  511. */
  512. int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
  513. /**
  514. * \brief Prime number generation
  515. *
  516. * \param X Destination MPI
  517. * \param nbits Required size of X in bits ( 3 <= nbits <= 4096 )
  518. * \param dh_flag If 1, then (X-1)/2 will be prime too
  519. * \param f_rng RNG function
  520. * \param p_rng RNG parameter
  521. *
  522. * \return 0 if successful (probably prime),
  523. * 1 if memory allocation failed,
  524. * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
  525. */
  526. int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
  527. int (*f_rng)(void *), void *p_rng );
  528. /**
  529. * \brief Checkup routine
  530. *
  531. * \return 0 if successful, or 1 if the test failed
  532. */
  533. int mpi_self_test( int verbose );
  534. #ifdef __cplusplus
  535. }
  536. #endif
  537. #endif /* bignum.h */