bignum.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /**
  2. * \file bignum.h
  3. *
  4. * \brief Multi-precision integer library
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_BIGNUM_H
  26. #define MBEDTLS_BIGNUM_H
  27. #if !defined(MBEDTLS_CONFIG_FILE)
  28. #include "config.h"
  29. #else
  30. #include MBEDTLS_CONFIG_FILE
  31. #endif
  32. #include <stddef.h>
  33. #include <stdint.h>
  34. #if defined(MBEDTLS_FS_IO)
  35. #include <stdio.h>
  36. #endif
  37. #define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
  38. #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
  39. #define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
  40. #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
  41. #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
  42. #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
  43. #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
  44. #define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010 /**< Memory allocation failed. */
  45. #define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
  46. /*
  47. * Maximum size MPIs are allowed to grow to in number of limbs.
  48. */
  49. #define MBEDTLS_MPI_MAX_LIMBS 10000
  50. #if !defined(MBEDTLS_MPI_WINDOW_SIZE)
  51. /*
  52. * Maximum window size used for modular exponentiation. Default: 6
  53. * Minimum value: 1. Maximum value: 6.
  54. *
  55. * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
  56. * for the sliding window calculation. (So 64 by default)
  57. *
  58. * Reduction in size, reduces speed.
  59. */
  60. #define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
  61. #endif /* !MBEDTLS_MPI_WINDOW_SIZE */
  62. #if !defined(MBEDTLS_MPI_MAX_SIZE)
  63. /*
  64. * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
  65. * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
  66. *
  67. * Note: Calculations can results temporarily in larger MPIs. So the number
  68. * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
  69. */
  70. #define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
  71. #endif /* !MBEDTLS_MPI_MAX_SIZE */
  72. #define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
  73. /*
  74. * When reading from files with mbedtls_mpi_read_file() and writing to files with
  75. * mbedtls_mpi_write_file() the buffer should have space
  76. * for a (short) label, the MPI (in the provided radix), the newline
  77. * characters and the '\0'.
  78. *
  79. * By default we assume at least a 10 char label, a minimum radix of 10
  80. * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
  81. * Autosized at compile time for at least a 10 char label, a minimum radix
  82. * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
  83. *
  84. * This used to be statically sized to 1250 for a maximum of 4096 bit
  85. * numbers (1234 decimal chars).
  86. *
  87. * Calculate using the formula:
  88. * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
  89. * LabelSize + 6
  90. */
  91. #define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
  92. #define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
  93. #define MBEDTLS_MPI_RW_BUFFER_SIZE ( ((MBEDTLS_MPI_MAX_BITS_SCALE100 + MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / MBEDTLS_LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
  94. /*
  95. * Define the base integer type, architecture-wise.
  96. *
  97. * 32-bit integers can be forced on 64-bit arches (eg. for testing purposes)
  98. * by defining MBEDTLS_HAVE_INT32 and undefining MBEDTLS_HAVE_ASM
  99. */
  100. #if ( ! defined(MBEDTLS_HAVE_INT32) && \
  101. defined(_MSC_VER) && defined(_M_AMD64) )
  102. #define MBEDTLS_HAVE_INT64
  103. typedef int64_t mbedtls_mpi_sint;
  104. typedef uint64_t mbedtls_mpi_uint;
  105. #else
  106. #if ( ! defined(MBEDTLS_HAVE_INT32) && \
  107. defined(__GNUC__) && ( \
  108. defined(__amd64__) || defined(__x86_64__) || \
  109. defined(__ppc64__) || defined(__powerpc64__) || \
  110. defined(__ia64__) || defined(__alpha__) || \
  111. (defined(__sparc__) && defined(__arch64__)) || \
  112. defined(__s390x__) || defined(__mips64) ) )
  113. #define MBEDTLS_HAVE_INT64
  114. typedef int64_t mbedtls_mpi_sint;
  115. typedef uint64_t mbedtls_mpi_uint;
  116. /* mbedtls_t_udbl defined as 128-bit unsigned int */
  117. typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
  118. #define MBEDTLS_HAVE_UDBL
  119. #else
  120. #define MBEDTLS_HAVE_INT32
  121. typedef int32_t mbedtls_mpi_sint;
  122. typedef uint32_t mbedtls_mpi_uint;
  123. typedef uint64_t mbedtls_t_udbl;
  124. #define MBEDTLS_HAVE_UDBL
  125. #endif /* !MBEDTLS_HAVE_INT32 && __GNUC__ && 64-bit platform */
  126. #endif /* !MBEDTLS_HAVE_INT32 && _MSC_VER && _M_AMD64 */
  127. #ifdef __cplusplus
  128. extern "C" {
  129. #endif
  130. /**
  131. * \brief MPI structure
  132. */
  133. typedef struct
  134. {
  135. int s; /*!< integer sign */
  136. size_t n; /*!< total # of limbs */
  137. mbedtls_mpi_uint *p; /*!< pointer to limbs */
  138. }
  139. mbedtls_mpi;
  140. /**
  141. * \brief Initialize one MPI (make internal references valid)
  142. * This just makes it ready to be set or freed,
  143. * but does not define a value for the MPI.
  144. *
  145. * \param X One MPI to initialize.
  146. */
  147. void mbedtls_mpi_init( mbedtls_mpi *X );
  148. /**
  149. * \brief Unallocate one MPI
  150. *
  151. * \param X One MPI to unallocate.
  152. */
  153. void mbedtls_mpi_free( mbedtls_mpi *X );
  154. /**
  155. * \brief Enlarge to the specified number of limbs
  156. *
  157. * \param X MPI to grow
  158. * \param nblimbs The target number of limbs
  159. *
  160. * \return 0 if successful,
  161. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  162. */
  163. int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
  164. /**
  165. * \brief Resize down, keeping at least the specified number of limbs
  166. *
  167. * \param X MPI to shrink
  168. * \param nblimbs The minimum number of limbs to keep
  169. *
  170. * \return 0 if successful,
  171. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  172. */
  173. int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
  174. /**
  175. * \brief Copy the contents of Y into X
  176. *
  177. * \param X Destination MPI
  178. * \param Y Source MPI
  179. *
  180. * \return 0 if successful,
  181. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  182. */
  183. int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
  184. /**
  185. * \brief Swap the contents of X and Y
  186. *
  187. * \param X First MPI value
  188. * \param Y Second MPI value
  189. */
  190. void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
  191. /**
  192. * \brief Safe conditional assignement X = Y if assign is 1
  193. *
  194. * \param X MPI to conditionally assign to
  195. * \param Y Value to be assigned
  196. * \param assign 1: perform the assignment, 0: keep X's original value
  197. *
  198. * \return 0 if successful,
  199. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  200. *
  201. * \note This function is equivalent to
  202. * if( assign ) mbedtls_mpi_copy( X, Y );
  203. * except that it avoids leaking any information about whether
  204. * the assignment was done or not (the above code may leak
  205. * information through branch prediction and/or memory access
  206. * patterns analysis).
  207. */
  208. int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
  209. /**
  210. * \brief Safe conditional swap X <-> Y if swap is 1
  211. *
  212. * \param X First mbedtls_mpi value
  213. * \param Y Second mbedtls_mpi value
  214. * \param assign 1: perform the swap, 0: keep X and Y's original values
  215. *
  216. * \return 0 if successful,
  217. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  218. *
  219. * \note This function is equivalent to
  220. * if( assign ) mbedtls_mpi_swap( X, Y );
  221. * except that it avoids leaking any information about whether
  222. * the assignment was done or not (the above code may leak
  223. * information through branch prediction and/or memory access
  224. * patterns analysis).
  225. */
  226. int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
  227. /**
  228. * \brief Set value from integer
  229. *
  230. * \param X MPI to set
  231. * \param z Value to use
  232. *
  233. * \return 0 if successful,
  234. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  235. */
  236. int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
  237. /**
  238. * \brief Get a specific bit from X
  239. *
  240. * \param X MPI to use
  241. * \param pos Zero-based index of the bit in X
  242. *
  243. * \return Either a 0 or a 1
  244. */
  245. int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
  246. /**
  247. * \brief Set a bit of X to a specific value of 0 or 1
  248. *
  249. * \note Will grow X if necessary to set a bit to 1 in a not yet
  250. * existing limb. Will not grow if bit should be set to 0
  251. *
  252. * \param X MPI to use
  253. * \param pos Zero-based index of the bit in X
  254. * \param val The value to set the bit to (0 or 1)
  255. *
  256. * \return 0 if successful,
  257. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  258. * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
  259. */
  260. int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
  261. /**
  262. * \brief Return the number of zero-bits before the least significant
  263. * '1' bit
  264. *
  265. * Note: Thus also the zero-based index of the least significant '1' bit
  266. *
  267. * \param X MPI to use
  268. */
  269. size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
  270. /**
  271. * \brief Return the number of bits up to and including the most
  272. * significant '1' bit'
  273. *
  274. * Note: Thus also the one-based index of the most significant '1' bit
  275. *
  276. * \param X MPI to use
  277. */
  278. size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
  279. /**
  280. * \brief Return the total size in bytes
  281. *
  282. * \param X MPI to use
  283. */
  284. size_t mbedtls_mpi_size( const mbedtls_mpi *X );
  285. /**
  286. * \brief Import from an ASCII string
  287. *
  288. * \param X Destination MPI
  289. * \param radix Input numeric base
  290. * \param s Null-terminated string buffer
  291. *
  292. * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
  293. */
  294. int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
  295. /**
  296. * \brief Export into an ASCII string
  297. *
  298. * \param X Source MPI
  299. * \param radix Output numeric base
  300. * \param buf Buffer to write the string to
  301. * \param buflen Length of buf
  302. * \param olen Length of the string written, including final NUL byte
  303. *
  304. * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code.
  305. * *olen is always updated to reflect the amount
  306. * of data that has (or would have) been written.
  307. *
  308. * \note Call this function with buflen = 0 to obtain the
  309. * minimum required buffer size in *olen.
  310. */
  311. int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
  312. char *buf, size_t buflen, size_t *olen );
  313. #if defined(MBEDTLS_FS_IO)
  314. /**
  315. * \brief Read X from an opened file
  316. *
  317. * \param X Destination MPI
  318. * \param radix Input numeric base
  319. * \param fin Input file handle
  320. *
  321. * \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if
  322. * the file read buffer is too small or a
  323. * MBEDTLS_ERR_MPI_XXX error code
  324. */
  325. int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
  326. /**
  327. * \brief Write X into an opened file, or stdout if fout is NULL
  328. *
  329. * \param p Prefix, can be NULL
  330. * \param X Source MPI
  331. * \param radix Output numeric base
  332. * \param fout Output file handle (can be NULL)
  333. *
  334. * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
  335. *
  336. * \note Set fout == NULL to print X on the console.
  337. */
  338. int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
  339. #endif /* MBEDTLS_FS_IO */
  340. /**
  341. * \brief Import X from unsigned binary data, big endian
  342. *
  343. * \param X Destination MPI
  344. * \param buf Input buffer
  345. * \param buflen Input buffer size
  346. *
  347. * \return 0 if successful,
  348. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  349. */
  350. int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
  351. /**
  352. * \brief Export X into unsigned binary data, big endian.
  353. * Always fills the whole buffer, which will start with zeros
  354. * if the number is smaller.
  355. *
  356. * \param X Source MPI
  357. * \param buf Output buffer
  358. * \param buflen Output buffer size
  359. *
  360. * \return 0 if successful,
  361. * MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
  362. */
  363. int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
  364. /**
  365. * \brief Left-shift: X <<= count
  366. *
  367. * \param X MPI to shift
  368. * \param count Amount to shift
  369. *
  370. * \return 0 if successful,
  371. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  372. */
  373. int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
  374. /**
  375. * \brief Right-shift: X >>= count
  376. *
  377. * \param X MPI to shift
  378. * \param count Amount to shift
  379. *
  380. * \return 0 if successful,
  381. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  382. */
  383. int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
  384. /**
  385. * \brief Compare unsigned values
  386. *
  387. * \param X Left-hand MPI
  388. * \param Y Right-hand MPI
  389. *
  390. * \return 1 if |X| is greater than |Y|,
  391. * -1 if |X| is lesser than |Y| or
  392. * 0 if |X| is equal to |Y|
  393. */
  394. int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
  395. /**
  396. * \brief Compare signed values
  397. *
  398. * \param X Left-hand MPI
  399. * \param Y Right-hand MPI
  400. *
  401. * \return 1 if X is greater than Y,
  402. * -1 if X is lesser than Y or
  403. * 0 if X is equal to Y
  404. */
  405. int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
  406. /**
  407. * \brief Compare signed values
  408. *
  409. * \param X Left-hand MPI
  410. * \param z The integer value to compare to
  411. *
  412. * \return 1 if X is greater than z,
  413. * -1 if X is lesser than z or
  414. * 0 if X is equal to z
  415. */
  416. int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
  417. /**
  418. * \brief Unsigned addition: X = |A| + |B|
  419. *
  420. * \param X Destination MPI
  421. * \param A Left-hand MPI
  422. * \param B Right-hand MPI
  423. *
  424. * \return 0 if successful,
  425. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  426. */
  427. int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
  428. /**
  429. * \brief Unsigned subtraction: X = |A| - |B|
  430. *
  431. * \param X Destination MPI
  432. * \param A Left-hand MPI
  433. * \param B Right-hand MPI
  434. *
  435. * \return 0 if successful,
  436. * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
  437. */
  438. int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
  439. /**
  440. * \brief Signed addition: X = A + B
  441. *
  442. * \param X Destination MPI
  443. * \param A Left-hand MPI
  444. * \param B Right-hand MPI
  445. *
  446. * \return 0 if successful,
  447. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  448. */
  449. int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
  450. /**
  451. * \brief Signed subtraction: X = A - B
  452. *
  453. * \param X Destination MPI
  454. * \param A Left-hand MPI
  455. * \param B Right-hand MPI
  456. *
  457. * \return 0 if successful,
  458. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  459. */
  460. int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
  461. /**
  462. * \brief Signed addition: X = A + b
  463. *
  464. * \param X Destination MPI
  465. * \param A Left-hand MPI
  466. * \param b The integer value to add
  467. *
  468. * \return 0 if successful,
  469. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  470. */
  471. int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
  472. /**
  473. * \brief Signed subtraction: X = A - b
  474. *
  475. * \param X Destination MPI
  476. * \param A Left-hand MPI
  477. * \param b The integer value to subtract
  478. *
  479. * \return 0 if successful,
  480. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  481. */
  482. int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
  483. /**
  484. * \brief Baseline multiplication: X = A * B
  485. *
  486. * \param X Destination MPI
  487. * \param A Left-hand MPI
  488. * \param B Right-hand MPI
  489. *
  490. * \return 0 if successful,
  491. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  492. */
  493. int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
  494. /**
  495. * \brief Baseline multiplication: X = A * b
  496. *
  497. * \param X Destination MPI
  498. * \param A Left-hand MPI
  499. * \param b The unsigned integer value to multiply with
  500. *
  501. * \note b is unsigned
  502. *
  503. * \return 0 if successful,
  504. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  505. */
  506. int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
  507. /**
  508. * \brief Division by mbedtls_mpi: A = Q * B + R
  509. *
  510. * \param Q Destination MPI for the quotient
  511. * \param R Destination MPI for the rest value
  512. * \param A Left-hand MPI
  513. * \param B Right-hand MPI
  514. *
  515. * \return 0 if successful,
  516. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  517. * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0
  518. *
  519. * \note Either Q or R can be NULL.
  520. */
  521. int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
  522. /**
  523. * \brief Division by int: A = Q * b + R
  524. *
  525. * \param Q Destination MPI for the quotient
  526. * \param R Destination MPI for the rest value
  527. * \param A Left-hand MPI
  528. * \param b Integer to divide by
  529. *
  530. * \return 0 if successful,
  531. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  532. * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0
  533. *
  534. * \note Either Q or R can be NULL.
  535. */
  536. int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b );
  537. /**
  538. * \brief Modulo: R = A mod B
  539. *
  540. * \param R Destination MPI for the rest value
  541. * \param A Left-hand MPI
  542. * \param B Right-hand MPI
  543. *
  544. * \return 0 if successful,
  545. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  546. * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0,
  547. * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0
  548. */
  549. int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
  550. /**
  551. * \brief Modulo: r = A mod b
  552. *
  553. * \param r Destination mbedtls_mpi_uint
  554. * \param A Left-hand MPI
  555. * \param b Integer to divide by
  556. *
  557. * \return 0 if successful,
  558. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  559. * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0,
  560. * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0
  561. */
  562. int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
  563. /**
  564. * \brief Sliding-window exponentiation: X = A^E mod N
  565. *
  566. * \param X Destination MPI
  567. * \param A Left-hand MPI
  568. * \param E Exponent MPI
  569. * \param N Modular MPI
  570. * \param _RR Speed-up MPI used for recalculations
  571. *
  572. * \return 0 if successful,
  573. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  574. * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
  575. * if E is negative
  576. *
  577. * \note _RR is used to avoid re-computing R*R mod N across
  578. * multiple calls, which speeds up things a bit. It can
  579. * be set to NULL if the extra performance is unneeded.
  580. */
  581. int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR );
  582. /**
  583. * \brief Fill an MPI X with size bytes of random
  584. *
  585. * \param X Destination MPI
  586. * \param size Size in bytes
  587. * \param f_rng RNG function
  588. * \param p_rng RNG parameter
  589. *
  590. * \return 0 if successful,
  591. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  592. */
  593. int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
  594. int (*f_rng)(void *, unsigned char *, size_t),
  595. void *p_rng );
  596. /**
  597. * \brief Greatest common divisor: G = gcd(A, B)
  598. *
  599. * \param G Destination MPI
  600. * \param A Left-hand MPI
  601. * \param B Right-hand MPI
  602. *
  603. * \return 0 if successful,
  604. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  605. */
  606. int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
  607. /**
  608. * \brief Modular inverse: X = A^-1 mod N
  609. *
  610. * \param X Destination MPI
  611. * \param A Left-hand MPI
  612. * \param N Right-hand MPI
  613. *
  614. * \return 0 if successful,
  615. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  616. * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
  617. MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
  618. */
  619. int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
  620. /**
  621. * \brief Miller-Rabin primality test
  622. *
  623. * \param X MPI to check
  624. * \param f_rng RNG function
  625. * \param p_rng RNG parameter
  626. *
  627. * \return 0 if successful (probably prime),
  628. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  629. * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime
  630. */
  631. int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
  632. int (*f_rng)(void *, unsigned char *, size_t),
  633. void *p_rng );
  634. /**
  635. * \brief Prime number generation
  636. *
  637. * \param X Destination MPI
  638. * \param nbits Required size of X in bits
  639. * ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS )
  640. * \param dh_flag If 1, then (X-1)/2 will be prime too
  641. * \param f_rng RNG function
  642. * \param p_rng RNG parameter
  643. *
  644. * \return 0 if successful (probably prime),
  645. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  646. * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
  647. */
  648. int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
  649. int (*f_rng)(void *, unsigned char *, size_t),
  650. void *p_rng );
  651. /**
  652. * \brief Checkup routine
  653. *
  654. * \return 0 if successful, or 1 if the test failed
  655. */
  656. int mbedtls_mpi_self_test( int verbose );
  657. #ifdef __cplusplus
  658. }
  659. #endif
  660. #endif /* bignum.h */