bignum.h 17 KB

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