bignum.h 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /**
  2. * \file bignum.h
  3. *
  4. * \brief Multi-precision integer library
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  9. */
  10. #ifndef MBEDTLS_BIGNUM_H
  11. #define MBEDTLS_BIGNUM_H
  12. #include "mbedtls/private_access.h"
  13. #include "mbedtls/build_info.h"
  14. #include "mbedtls/platform_util.h"
  15. #include <stddef.h>
  16. #include <stdint.h>
  17. #if defined(MBEDTLS_FS_IO)
  18. #include <stdio.h>
  19. #endif
  20. /** An error occurred while reading from or writing to a file. */
  21. #define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002
  22. /** Bad input parameters to function. */
  23. #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004
  24. /** There is an invalid character in the digit string. */
  25. #define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006
  26. /** The buffer is too small to write to. */
  27. #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008
  28. /** The input arguments are negative or result in illegal output. */
  29. #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A
  30. /** The input argument for division is zero, which is not allowed. */
  31. #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C
  32. /** The input arguments are not acceptable. */
  33. #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E
  34. /** Memory allocation failed. */
  35. #define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010
  36. #define MBEDTLS_MPI_CHK(f) \
  37. do \
  38. { \
  39. if ((ret = (f)) != 0) \
  40. goto cleanup; \
  41. } while (0)
  42. /*
  43. * Maximum size MPIs are allowed to grow to in number of limbs.
  44. */
  45. #define MBEDTLS_MPI_MAX_LIMBS 10000
  46. #if !defined(MBEDTLS_MPI_WINDOW_SIZE)
  47. /*
  48. * Maximum window size used for modular exponentiation. Default: 3
  49. * Minimum value: 1. Maximum value: 6.
  50. *
  51. * Result is an array of ( 2 ** MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
  52. * for the sliding window calculation. (So 8 by default)
  53. *
  54. * Reduction in size, reduces speed.
  55. */
  56. #define MBEDTLS_MPI_WINDOW_SIZE 3 /**< Maximum window size used. */
  57. #endif /* !MBEDTLS_MPI_WINDOW_SIZE */
  58. #if !defined(MBEDTLS_MPI_MAX_SIZE)
  59. /*
  60. * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
  61. * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
  62. *
  63. * Note: Calculations can temporarily result in larger MPIs. So the number
  64. * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
  65. */
  66. #define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
  67. #endif /* !MBEDTLS_MPI_MAX_SIZE */
  68. #define MBEDTLS_MPI_MAX_BITS (8 * MBEDTLS_MPI_MAX_SIZE) /**< Maximum number of bits for usable MPIs. */
  69. /*
  70. * When reading from files with mbedtls_mpi_read_file() and writing to files with
  71. * mbedtls_mpi_write_file() the buffer should have space
  72. * for a (short) label, the MPI (in the provided radix), the newline
  73. * characters and the '\0'.
  74. *
  75. * By default we assume at least a 10 char label, a minimum radix of 10
  76. * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
  77. * Autosized at compile time for at least a 10 char label, a minimum radix
  78. * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
  79. *
  80. * This used to be statically sized to 1250 for a maximum of 4096 bit
  81. * numbers (1234 decimal chars).
  82. *
  83. * Calculate using the formula:
  84. * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
  85. * LabelSize + 6
  86. */
  87. #define MBEDTLS_MPI_MAX_BITS_SCALE100 (100 * MBEDTLS_MPI_MAX_BITS)
  88. #define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
  89. #define MBEDTLS_MPI_RW_BUFFER_SIZE (((MBEDTLS_MPI_MAX_BITS_SCALE100 + \
  90. MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / \
  91. MBEDTLS_LN_2_DIV_LN_10_SCALE100) + 10 + 6)
  92. /*
  93. * Define the base integer type, architecture-wise.
  94. *
  95. * 32 or 64-bit integer types can be forced regardless of the underlying
  96. * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
  97. * respectively and undefining MBEDTLS_HAVE_ASM.
  98. *
  99. * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
  100. * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
  101. */
  102. #if !defined(MBEDTLS_HAVE_INT32)
  103. #if defined(_MSC_VER) && defined(_M_AMD64)
  104. /* Always choose 64-bit when using MSC */
  105. #if !defined(MBEDTLS_HAVE_INT64)
  106. #define MBEDTLS_HAVE_INT64
  107. #endif /* !MBEDTLS_HAVE_INT64 */
  108. typedef int64_t mbedtls_mpi_sint;
  109. typedef uint64_t mbedtls_mpi_uint;
  110. #define MBEDTLS_MPI_UINT_MAX UINT64_MAX
  111. #elif defined(__GNUC__) && ( \
  112. defined(__amd64__) || defined(__x86_64__) || \
  113. defined(__ppc64__) || defined(__powerpc64__) || \
  114. defined(__ia64__) || defined(__alpha__) || \
  115. (defined(__sparc__) && defined(__arch64__)) || \
  116. defined(__s390x__) || defined(__mips64) || \
  117. defined(__aarch64__))
  118. #if !defined(MBEDTLS_HAVE_INT64)
  119. #define MBEDTLS_HAVE_INT64
  120. #endif /* MBEDTLS_HAVE_INT64 */
  121. typedef int64_t mbedtls_mpi_sint;
  122. typedef uint64_t mbedtls_mpi_uint;
  123. #define MBEDTLS_MPI_UINT_MAX UINT64_MAX
  124. #if !defined(MBEDTLS_NO_UDBL_DIVISION)
  125. /* mbedtls_t_udbl defined as 128-bit unsigned int */
  126. typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
  127. #define MBEDTLS_HAVE_UDBL
  128. #endif /* !MBEDTLS_NO_UDBL_DIVISION */
  129. #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
  130. /*
  131. * __ARMCC_VERSION is defined for both armcc and armclang and
  132. * __aarch64__ is only defined by armclang when compiling 64-bit code
  133. */
  134. #if !defined(MBEDTLS_HAVE_INT64)
  135. #define MBEDTLS_HAVE_INT64
  136. #endif /* !MBEDTLS_HAVE_INT64 */
  137. typedef int64_t mbedtls_mpi_sint;
  138. typedef uint64_t mbedtls_mpi_uint;
  139. #define MBEDTLS_MPI_UINT_MAX UINT64_MAX
  140. #if !defined(MBEDTLS_NO_UDBL_DIVISION)
  141. /* mbedtls_t_udbl defined as 128-bit unsigned int */
  142. typedef __uint128_t mbedtls_t_udbl;
  143. #define MBEDTLS_HAVE_UDBL
  144. #endif /* !MBEDTLS_NO_UDBL_DIVISION */
  145. #elif defined(MBEDTLS_HAVE_INT64)
  146. /* Force 64-bit integers with unknown compiler */
  147. typedef int64_t mbedtls_mpi_sint;
  148. typedef uint64_t mbedtls_mpi_uint;
  149. #define MBEDTLS_MPI_UINT_MAX UINT64_MAX
  150. #endif
  151. #endif /* !MBEDTLS_HAVE_INT32 */
  152. #if !defined(MBEDTLS_HAVE_INT64)
  153. /* Default to 32-bit compilation */
  154. #if !defined(MBEDTLS_HAVE_INT32)
  155. #define MBEDTLS_HAVE_INT32
  156. #endif /* !MBEDTLS_HAVE_INT32 */
  157. typedef int32_t mbedtls_mpi_sint;
  158. typedef uint32_t mbedtls_mpi_uint;
  159. #define MBEDTLS_MPI_UINT_MAX UINT32_MAX
  160. #if !defined(MBEDTLS_NO_UDBL_DIVISION)
  161. typedef uint64_t mbedtls_t_udbl;
  162. #define MBEDTLS_HAVE_UDBL
  163. #endif /* !MBEDTLS_NO_UDBL_DIVISION */
  164. #endif /* !MBEDTLS_HAVE_INT64 */
  165. /*
  166. * Sanity check that exactly one of MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64 is defined,
  167. * so that code elsewhere doesn't have to check.
  168. */
  169. #if (!(defined(MBEDTLS_HAVE_INT32) || defined(MBEDTLS_HAVE_INT64))) || \
  170. (defined(MBEDTLS_HAVE_INT32) && defined(MBEDTLS_HAVE_INT64))
  171. #error "Only 32-bit or 64-bit limbs are supported in bignum"
  172. #endif
  173. /** \typedef mbedtls_mpi_uint
  174. * \brief The type of machine digits in a bignum, called _limbs_.
  175. *
  176. * This is always an unsigned integer type with no padding bits. The size
  177. * is platform-dependent.
  178. */
  179. /** \typedef mbedtls_mpi_sint
  180. * \brief The signed type corresponding to #mbedtls_mpi_uint.
  181. *
  182. * This is always an signed integer type with no padding bits. The size
  183. * is platform-dependent.
  184. */
  185. #ifdef __cplusplus
  186. extern "C" {
  187. #endif
  188. /**
  189. * \brief MPI structure
  190. */
  191. typedef struct mbedtls_mpi {
  192. /** Pointer to limbs.
  193. *
  194. * This may be \c NULL if \c n is 0.
  195. */
  196. mbedtls_mpi_uint *MBEDTLS_PRIVATE(p);
  197. /** Sign: -1 if the mpi is negative, 1 otherwise.
  198. *
  199. * The number 0 must be represented with `s = +1`. Although many library
  200. * functions treat all-limbs-zero as equivalent to a valid representation
  201. * of 0 regardless of the sign bit, there are exceptions, so bignum
  202. * functions and external callers must always set \c s to +1 for the
  203. * number zero.
  204. *
  205. * Note that this implies that calloc() or `... = {0}` does not create
  206. * a valid MPI representation. You must call mbedtls_mpi_init().
  207. */
  208. signed short MBEDTLS_PRIVATE(s);
  209. /** Total number of limbs in \c p. */
  210. unsigned short MBEDTLS_PRIVATE(n);
  211. /* Make sure that MBEDTLS_MPI_MAX_LIMBS fits in n.
  212. * Use the same limit value on all platforms so that we don't have to
  213. * think about different behavior on the rare platforms where
  214. * unsigned short can store values larger than the minimum required by
  215. * the C language, which is 65535.
  216. */
  217. #if MBEDTLS_MPI_MAX_LIMBS > 65535
  218. #error "MBEDTLS_MPI_MAX_LIMBS > 65535 is not supported"
  219. #endif
  220. }
  221. mbedtls_mpi;
  222. /**
  223. * \brief Initialize an MPI context.
  224. *
  225. * This makes the MPI ready to be set or freed,
  226. * but does not define a value for the MPI.
  227. *
  228. * \param X The MPI context to initialize. This must not be \c NULL.
  229. */
  230. void mbedtls_mpi_init(mbedtls_mpi *X);
  231. /**
  232. * \brief This function frees the components of an MPI context.
  233. *
  234. * \param X The MPI context to be cleared. This may be \c NULL,
  235. * in which case this function is a no-op. If it is
  236. * not \c NULL, it must point to an initialized MPI.
  237. */
  238. void mbedtls_mpi_free(mbedtls_mpi *X);
  239. /**
  240. * \brief Enlarge an MPI to the specified number of limbs.
  241. *
  242. * \note This function does nothing if the MPI is
  243. * already large enough.
  244. *
  245. * \param X The MPI to grow. It must be initialized.
  246. * \param nblimbs The target number of limbs.
  247. *
  248. * \return \c 0 if successful.
  249. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
  250. * \return Another negative error code on other kinds of failure.
  251. */
  252. int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs);
  253. /**
  254. * \brief This function resizes an MPI downwards, keeping at least the
  255. * specified number of limbs.
  256. *
  257. * If \c X is smaller than \c nblimbs, it is resized up
  258. * instead.
  259. *
  260. * \param X The MPI to shrink. This must point to an initialized MPI.
  261. * \param nblimbs The minimum number of limbs to keep.
  262. *
  263. * \return \c 0 if successful.
  264. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  265. * (this can only happen when resizing up).
  266. * \return Another negative error code on other kinds of failure.
  267. */
  268. int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs);
  269. /**
  270. * \brief Make a copy of an MPI.
  271. *
  272. * \param X The destination MPI. This must point to an initialized MPI.
  273. * \param Y The source MPI. This must point to an initialized MPI.
  274. *
  275. * \note The limb-buffer in the destination MPI is enlarged
  276. * if necessary to hold the value in the source MPI.
  277. *
  278. * \return \c 0 if successful.
  279. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
  280. * \return Another negative error code on other kinds of failure.
  281. */
  282. int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y);
  283. /**
  284. * \brief Swap the contents of two MPIs.
  285. *
  286. * \param X The first MPI. It must be initialized.
  287. * \param Y The second MPI. It must be initialized.
  288. */
  289. void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y);
  290. /**
  291. * \brief Perform a safe conditional copy of MPI which doesn't
  292. * reveal whether the condition was true or not.
  293. *
  294. * \param X The MPI to conditionally assign to. This must point
  295. * to an initialized MPI.
  296. * \param Y The MPI to be assigned from. This must point to an
  297. * initialized MPI.
  298. * \param assign The condition deciding whether to perform the
  299. * assignment or not. Must be either 0 or 1:
  300. * * \c 1: Perform the assignment `X = Y`.
  301. * * \c 0: Keep the original value of \p X.
  302. *
  303. * \note This function is equivalent to
  304. * `if( assign ) mbedtls_mpi_copy( X, Y );`
  305. * except that it avoids leaking any information about whether
  306. * the assignment was done or not (the above code may leak
  307. * information through branch prediction and/or memory access
  308. * patterns analysis).
  309. *
  310. * \warning If \p assign is neither 0 nor 1, the result of this function
  311. * is indeterminate, and the resulting value in \p X might be
  312. * neither its original value nor the value in \p Y.
  313. *
  314. * \return \c 0 if successful.
  315. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
  316. * \return Another negative error code on other kinds of failure.
  317. */
  318. int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign);
  319. /**
  320. * \brief Perform a safe conditional swap which doesn't
  321. * reveal whether the condition was true or not.
  322. *
  323. * \param X The first MPI. This must be initialized.
  324. * \param Y The second MPI. This must be initialized.
  325. * \param swap The condition deciding whether to perform
  326. * the swap or not. Must be either 0 or 1:
  327. * * \c 1: Swap the values of \p X and \p Y.
  328. * * \c 0: Keep the original values of \p X and \p Y.
  329. *
  330. * \note This function is equivalent to
  331. * if( swap ) mbedtls_mpi_swap( X, Y );
  332. * except that it avoids leaking any information about whether
  333. * the swap was done or not (the above code may leak
  334. * information through branch prediction and/or memory access
  335. * patterns analysis).
  336. *
  337. * \warning If \p swap is neither 0 nor 1, the result of this function
  338. * is indeterminate, and both \p X and \p Y might end up with
  339. * values different to either of the original ones.
  340. *
  341. * \return \c 0 if successful.
  342. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
  343. * \return Another negative error code on other kinds of failure.
  344. *
  345. */
  346. int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap);
  347. /**
  348. * \brief Store integer value in MPI.
  349. *
  350. * \param X The MPI to set. This must be initialized.
  351. * \param z The value to use.
  352. *
  353. * \return \c 0 if successful.
  354. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
  355. * \return Another negative error code on other kinds of failure.
  356. */
  357. int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z);
  358. /**
  359. * \brief Get a specific bit from an MPI.
  360. *
  361. * \param X The MPI to query. This must be initialized.
  362. * \param pos Zero-based index of the bit to query.
  363. *
  364. * \return \c 0 or \c 1 on success, depending on whether bit \c pos
  365. * of \c X is unset or set.
  366. * \return A negative error code on failure.
  367. */
  368. int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos);
  369. /**
  370. * \brief Modify a specific bit in an MPI.
  371. *
  372. * \note This function will grow the target MPI if necessary to set a
  373. * bit to \c 1 in a not yet existing limb. It will not grow if
  374. * the bit should be set to \c 0.
  375. *
  376. * \param X The MPI to modify. This must be initialized.
  377. * \param pos Zero-based index of the bit to modify.
  378. * \param val The desired value of bit \c pos: \c 0 or \c 1.
  379. *
  380. * \return \c 0 if successful.
  381. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
  382. * \return Another negative error code on other kinds of failure.
  383. */
  384. int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val);
  385. /**
  386. * \brief Return the number of bits of value \c 0 before the
  387. * least significant bit of value \c 1.
  388. *
  389. * \note This is the same as the zero-based index of
  390. * the least significant bit of value \c 1.
  391. *
  392. * \param X The MPI to query.
  393. *
  394. * \return The number of bits of value \c 0 before the least significant
  395. * bit of value \c 1 in \p X.
  396. */
  397. size_t mbedtls_mpi_lsb(const mbedtls_mpi *X);
  398. /**
  399. * \brief Return the number of bits up to and including the most
  400. * significant bit of value \c 1.
  401. *
  402. * * \note This is same as the one-based index of the most
  403. * significant bit of value \c 1.
  404. *
  405. * \param X The MPI to query. This must point to an initialized MPI.
  406. *
  407. * \return The number of bits up to and including the most
  408. * significant bit of value \c 1.
  409. */
  410. size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X);
  411. /**
  412. * \brief Return the total size of an MPI value in bytes.
  413. *
  414. * \param X The MPI to use. This must point to an initialized MPI.
  415. *
  416. * \note The value returned by this function may be less than
  417. * the number of bytes used to store \p X internally.
  418. * This happens if and only if there are trailing bytes
  419. * of value zero.
  420. *
  421. * \return The least number of bytes capable of storing
  422. * the absolute value of \p X.
  423. */
  424. size_t mbedtls_mpi_size(const mbedtls_mpi *X);
  425. /**
  426. * \brief Import an MPI from an ASCII string.
  427. *
  428. * \param X The destination MPI. This must point to an initialized MPI.
  429. * \param radix The numeric base of the input string.
  430. * \param s Null-terminated string buffer.
  431. *
  432. * \return \c 0 if successful.
  433. * \return A negative error code on failure.
  434. */
  435. int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s);
  436. /**
  437. * \brief Export an MPI to an ASCII string.
  438. *
  439. * \param X The source MPI. This must point to an initialized MPI.
  440. * \param radix The numeric base of the output string.
  441. * \param buf The buffer to write the string to. This must be writable
  442. * buffer of length \p buflen Bytes.
  443. * \param buflen The available size in Bytes of \p buf.
  444. * \param olen The address at which to store the length of the string
  445. * written, including the final \c NULL byte. This must
  446. * not be \c NULL.
  447. *
  448. * \note You can call this function with `buflen == 0` to obtain the
  449. * minimum required buffer size in `*olen`.
  450. *
  451. * \return \c 0 if successful.
  452. * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the target buffer \p buf
  453. * is too small to hold the value of \p X in the desired base.
  454. * In this case, `*olen` is nonetheless updated to contain the
  455. * size of \p buf required for a successful call.
  456. * \return Another negative error code on different kinds of failure.
  457. */
  458. int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
  459. char *buf, size_t buflen, size_t *olen);
  460. #if defined(MBEDTLS_FS_IO)
  461. /**
  462. * \brief Read an MPI from a line in an opened file.
  463. *
  464. * \param X The destination MPI. This must point to an initialized MPI.
  465. * \param radix The numeric base of the string representation used
  466. * in the source line.
  467. * \param fin The input file handle to use. This must not be \c NULL.
  468. *
  469. * \note On success, this function advances the file stream
  470. * to the end of the current line or to EOF.
  471. *
  472. * The function returns \c 0 on an empty line.
  473. *
  474. * Leading whitespaces are ignored, as is a
  475. * '0x' prefix for radix \c 16.
  476. *
  477. * \return \c 0 if successful.
  478. * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the file read buffer
  479. * is too small.
  480. * \return Another negative error code on failure.
  481. */
  482. int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin);
  483. /**
  484. * \brief Export an MPI into an opened file.
  485. *
  486. * \param p A string prefix to emit prior to the MPI data.
  487. * For example, this might be a label, or "0x" when
  488. * printing in base \c 16. This may be \c NULL if no prefix
  489. * is needed.
  490. * \param X The source MPI. This must point to an initialized MPI.
  491. * \param radix The numeric base to be used in the emitted string.
  492. * \param fout The output file handle. This may be \c NULL, in which case
  493. * the output is written to \c stdout.
  494. *
  495. * \return \c 0 if successful.
  496. * \return A negative error code on failure.
  497. */
  498. int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X,
  499. int radix, FILE *fout);
  500. #endif /* MBEDTLS_FS_IO */
  501. /**
  502. * \brief Import an MPI from unsigned big endian binary data.
  503. *
  504. * \param X The destination MPI. This must point to an initialized MPI.
  505. * \param buf The input buffer. This must be a readable buffer of length
  506. * \p buflen Bytes.
  507. * \param buflen The length of the input buffer \p buf in Bytes.
  508. *
  509. * \return \c 0 if successful.
  510. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
  511. * \return Another negative error code on different kinds of failure.
  512. */
  513. int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf,
  514. size_t buflen);
  515. /**
  516. * \brief Import X from unsigned binary data, little endian
  517. *
  518. * \param X The destination MPI. This must point to an initialized MPI.
  519. * \param buf The input buffer. This must be a readable buffer of length
  520. * \p buflen Bytes.
  521. * \param buflen The length of the input buffer \p buf in Bytes.
  522. *
  523. * \return \c 0 if successful.
  524. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
  525. * \return Another negative error code on different kinds of failure.
  526. */
  527. int mbedtls_mpi_read_binary_le(mbedtls_mpi *X,
  528. const unsigned char *buf, size_t buflen);
  529. /**
  530. * \brief Export X into unsigned binary data, big endian.
  531. * Always fills the whole buffer, which will start with zeros
  532. * if the number is smaller.
  533. *
  534. * \param X The source MPI. This must point to an initialized MPI.
  535. * \param buf The output buffer. This must be a writable buffer of length
  536. * \p buflen Bytes.
  537. * \param buflen The size of the output buffer \p buf in Bytes.
  538. *
  539. * \return \c 0 if successful.
  540. * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
  541. * large enough to hold the value of \p X.
  542. * \return Another negative error code on different kinds of failure.
  543. */
  544. int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf,
  545. size_t buflen);
  546. /**
  547. * \brief Export X into unsigned binary data, little endian.
  548. * Always fills the whole buffer, which will end with zeros
  549. * if the number is smaller.
  550. *
  551. * \param X The source MPI. This must point to an initialized MPI.
  552. * \param buf The output buffer. This must be a writable buffer of length
  553. * \p buflen Bytes.
  554. * \param buflen The size of the output buffer \p buf in Bytes.
  555. *
  556. * \return \c 0 if successful.
  557. * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
  558. * large enough to hold the value of \p X.
  559. * \return Another negative error code on different kinds of failure.
  560. */
  561. int mbedtls_mpi_write_binary_le(const mbedtls_mpi *X,
  562. unsigned char *buf, size_t buflen);
  563. /**
  564. * \brief Perform a left-shift on an MPI: X <<= count
  565. *
  566. * \param X The MPI to shift. This must point to an initialized MPI.
  567. * The MPI pointed by \p X may be resized to fit
  568. * the resulting number.
  569. * \param count The number of bits to shift by.
  570. *
  571. * \return \c 0 if successful.
  572. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  573. * \return Another negative error code on different kinds of failure.
  574. */
  575. int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count);
  576. /**
  577. * \brief Perform a right-shift on an MPI: X >>= count
  578. *
  579. * \param X The MPI to shift. This must point to an initialized MPI.
  580. * \param count The number of bits to shift by.
  581. *
  582. * \return \c 0 if successful.
  583. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  584. * \return Another negative error code on different kinds of failure.
  585. */
  586. int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count);
  587. /**
  588. * \brief Compare the absolute values of two MPIs.
  589. *
  590. * \param X The left-hand MPI. This must point to an initialized MPI.
  591. * \param Y The right-hand MPI. This must point to an initialized MPI.
  592. *
  593. * \return \c 1 if `|X|` is greater than `|Y|`.
  594. * \return \c -1 if `|X|` is lesser than `|Y|`.
  595. * \return \c 0 if `|X|` is equal to `|Y|`.
  596. */
  597. int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y);
  598. /**
  599. * \brief Compare two MPIs.
  600. *
  601. * \param X The left-hand MPI. This must point to an initialized MPI.
  602. * \param Y The right-hand MPI. This must point to an initialized MPI.
  603. *
  604. * \return \c 1 if \p X is greater than \p Y.
  605. * \return \c -1 if \p X is lesser than \p Y.
  606. * \return \c 0 if \p X is equal to \p Y.
  607. */
  608. int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y);
  609. /**
  610. * \brief Check if an MPI is less than the other in constant time.
  611. *
  612. * \param X The left-hand MPI. This must point to an initialized MPI
  613. * with the same allocated length as Y.
  614. * \param Y The right-hand MPI. This must point to an initialized MPI
  615. * with the same allocated length as X.
  616. * \param ret The result of the comparison:
  617. * \c 1 if \p X is less than \p Y.
  618. * \c 0 if \p X is greater than or equal to \p Y.
  619. *
  620. * \return 0 on success.
  621. * \return MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the allocated length of
  622. * the two input MPIs is not the same.
  623. */
  624. int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X, const mbedtls_mpi *Y,
  625. unsigned *ret);
  626. /**
  627. * \brief Compare an MPI with an integer.
  628. *
  629. * \param X The left-hand MPI. This must point to an initialized MPI.
  630. * \param z The integer value to compare \p X to.
  631. *
  632. * \return \c 1 if \p X is greater than \p z.
  633. * \return \c -1 if \p X is lesser than \p z.
  634. * \return \c 0 if \p X is equal to \p z.
  635. */
  636. int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z);
  637. /**
  638. * \brief Perform an unsigned addition of MPIs: X = |A| + |B|
  639. *
  640. * \param X The destination MPI. This must point to an initialized MPI.
  641. * \param A The first summand. This must point to an initialized MPI.
  642. * \param B The second summand. This must point to an initialized MPI.
  643. *
  644. * \return \c 0 if successful.
  645. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  646. * \return Another negative error code on different kinds of failure.
  647. */
  648. int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A,
  649. const mbedtls_mpi *B);
  650. /**
  651. * \brief Perform an unsigned subtraction of MPIs: X = |A| - |B|
  652. *
  653. * \param X The destination MPI. This must point to an initialized MPI.
  654. * \param A The minuend. This must point to an initialized MPI.
  655. * \param B The subtrahend. This must point to an initialized MPI.
  656. *
  657. * \return \c 0 if successful.
  658. * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p B is greater than \p A.
  659. * \return Another negative error code on different kinds of failure.
  660. *
  661. */
  662. int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A,
  663. const mbedtls_mpi *B);
  664. /**
  665. * \brief Perform a signed addition of MPIs: X = A + B
  666. *
  667. * \param X The destination MPI. This must point to an initialized MPI.
  668. * \param A The first summand. This must point to an initialized MPI.
  669. * \param B The second summand. This must point to an initialized MPI.
  670. *
  671. * \return \c 0 if successful.
  672. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  673. * \return Another negative error code on different kinds of failure.
  674. */
  675. int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A,
  676. const mbedtls_mpi *B);
  677. /**
  678. * \brief Perform a signed subtraction of MPIs: X = A - B
  679. *
  680. * \param X The destination MPI. This must point to an initialized MPI.
  681. * \param A The minuend. This must point to an initialized MPI.
  682. * \param B The subtrahend. This must point to an initialized MPI.
  683. *
  684. * \return \c 0 if successful.
  685. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  686. * \return Another negative error code on different kinds of failure.
  687. */
  688. int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A,
  689. const mbedtls_mpi *B);
  690. /**
  691. * \brief Perform a signed addition of an MPI and an integer: X = A + b
  692. *
  693. * \param X The destination MPI. This must point to an initialized MPI.
  694. * \param A The first summand. This must point to an initialized MPI.
  695. * \param b The second summand.
  696. *
  697. * \return \c 0 if successful.
  698. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  699. * \return Another negative error code on different kinds of failure.
  700. */
  701. int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A,
  702. mbedtls_mpi_sint b);
  703. /**
  704. * \brief Perform a signed subtraction of an MPI and an integer:
  705. * X = A - b
  706. *
  707. * \param X The destination MPI. This must point to an initialized MPI.
  708. * \param A The minuend. This must point to an initialized MPI.
  709. * \param b The subtrahend.
  710. *
  711. * \return \c 0 if successful.
  712. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  713. * \return Another negative error code on different kinds of failure.
  714. */
  715. int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A,
  716. mbedtls_mpi_sint b);
  717. /**
  718. * \brief Perform a multiplication of two MPIs: X = A * B
  719. *
  720. * \param X The destination MPI. This must point to an initialized MPI.
  721. * \param A The first factor. This must point to an initialized MPI.
  722. * \param B The second factor. This must point to an initialized MPI.
  723. *
  724. * \return \c 0 if successful.
  725. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  726. * \return Another negative error code on different kinds of failure.
  727. *
  728. */
  729. int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A,
  730. const mbedtls_mpi *B);
  731. /**
  732. * \brief Perform a multiplication of an MPI with an unsigned integer:
  733. * X = A * b
  734. *
  735. * \param X The destination MPI. This must point to an initialized MPI.
  736. * \param A The first factor. This must point to an initialized MPI.
  737. * \param b The second factor.
  738. *
  739. * \return \c 0 if successful.
  740. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  741. * \return Another negative error code on different kinds of failure.
  742. *
  743. */
  744. int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A,
  745. mbedtls_mpi_uint b);
  746. /**
  747. * \brief Perform a division with remainder of two MPIs:
  748. * A = Q * B + R
  749. *
  750. * \param Q The destination MPI for the quotient.
  751. * This may be \c NULL if the value of the
  752. * quotient is not needed. This must not alias A or B.
  753. * \param R The destination MPI for the remainder value.
  754. * This may be \c NULL if the value of the
  755. * remainder is not needed. This must not alias A or B.
  756. * \param A The dividend. This must point to an initialized MPI.
  757. * \param B The divisor. This must point to an initialized MPI.
  758. *
  759. * \return \c 0 if successful.
  760. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
  761. * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p B equals zero.
  762. * \return Another negative error code on different kinds of failure.
  763. */
  764. int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
  765. const mbedtls_mpi *B);
  766. /**
  767. * \brief Perform a division with remainder of an MPI by an integer:
  768. * A = Q * b + R
  769. *
  770. * \param Q The destination MPI for the quotient.
  771. * This may be \c NULL if the value of the
  772. * quotient is not needed. This must not alias A.
  773. * \param R The destination MPI for the remainder value.
  774. * This may be \c NULL if the value of the
  775. * remainder is not needed. This must not alias A.
  776. * \param A The dividend. This must point to an initialized MPi.
  777. * \param b The divisor.
  778. *
  779. * \return \c 0 if successful.
  780. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
  781. * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p b equals zero.
  782. * \return Another negative error code on different kinds of failure.
  783. */
  784. int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
  785. mbedtls_mpi_sint b);
  786. /**
  787. * \brief Perform a modular reduction. R = A mod B
  788. *
  789. * \param R The destination MPI for the residue value.
  790. * This must point to an initialized MPI.
  791. * \param A The MPI to compute the residue of.
  792. * This must point to an initialized MPI.
  793. * \param B The base of the modular reduction.
  794. * This must point to an initialized MPI.
  795. *
  796. * \return \c 0 if successful.
  797. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  798. * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p B equals zero.
  799. * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p B is negative.
  800. * \return Another negative error code on different kinds of failure.
  801. *
  802. */
  803. int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A,
  804. const mbedtls_mpi *B);
  805. /**
  806. * \brief Perform a modular reduction with respect to an integer.
  807. * r = A mod b
  808. *
  809. * \param r The address at which to store the residue.
  810. * This must not be \c NULL.
  811. * \param A The MPI to compute the residue of.
  812. * This must point to an initialized MPi.
  813. * \param b The integer base of the modular reduction.
  814. *
  815. * \return \c 0 if successful.
  816. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  817. * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p b equals zero.
  818. * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p b is negative.
  819. * \return Another negative error code on different kinds of failure.
  820. */
  821. int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A,
  822. mbedtls_mpi_sint b);
  823. /**
  824. * \brief Perform a modular exponentiation: X = A^E mod N
  825. *
  826. * \param X The destination MPI. This must point to an initialized MPI.
  827. * This must not alias E or N.
  828. * \param A The base of the exponentiation.
  829. * This must point to an initialized MPI.
  830. * \param E The exponent MPI. This must point to an initialized MPI.
  831. * \param N The base for the modular reduction. This must point to an
  832. * initialized MPI.
  833. * \param prec_RR A helper MPI depending solely on \p N which can be used to
  834. * speed-up multiple modular exponentiations for the same value
  835. * of \p N. This may be \c NULL. If it is not \c NULL, it must
  836. * point to an initialized MPI. If it hasn't been used after
  837. * the call to mbedtls_mpi_init(), this function will compute
  838. * the helper value and store it in \p prec_RR for reuse on
  839. * subsequent calls to this function. Otherwise, the function
  840. * will assume that \p prec_RR holds the helper value set by a
  841. * previous call to mbedtls_mpi_exp_mod(), and reuse it.
  842. *
  843. * \return \c 0 if successful.
  844. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  845. * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \c N is negative or
  846. * even, or if \c E is negative.
  847. * \return Another negative error code on different kinds of failures.
  848. *
  849. */
  850. int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
  851. const mbedtls_mpi *E, const mbedtls_mpi *N,
  852. mbedtls_mpi *prec_RR);
  853. /**
  854. * \brief Fill an MPI with a number of random bytes.
  855. *
  856. * \param X The destination MPI. This must point to an initialized MPI.
  857. * \param size The number of random bytes to generate.
  858. * \param f_rng The RNG function to use. This must not be \c NULL.
  859. * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
  860. * \c NULL if \p f_rng doesn't need a context argument.
  861. *
  862. * \return \c 0 if successful.
  863. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  864. * \return Another negative error code on failure.
  865. *
  866. * \note The bytes obtained from the RNG are interpreted
  867. * as a big-endian representation of an MPI; this can
  868. * be relevant in applications like deterministic ECDSA.
  869. */
  870. int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
  871. mbedtls_f_rng_t *f_rng,
  872. void *p_rng);
  873. /** Generate a random number uniformly in a range.
  874. *
  875. * This function generates a random number between \p min inclusive and
  876. * \p N exclusive.
  877. *
  878. * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
  879. * when the RNG is a suitably parametrized instance of HMAC_DRBG
  880. * and \p min is \c 1.
  881. *
  882. * \note There are `N - min` possible outputs. The lower bound
  883. * \p min can be reached, but the upper bound \p N cannot.
  884. *
  885. * \param X The destination MPI. This must point to an initialized MPI.
  886. * \param min The minimum value to return.
  887. * It must be nonnegative.
  888. * \param N The upper bound of the range, exclusive.
  889. * In other words, this is one plus the maximum value to return.
  890. * \p N must be strictly larger than \p min.
  891. * \param f_rng The RNG function to use. This must not be \c NULL.
  892. * \param p_rng The RNG parameter to be passed to \p f_rng.
  893. *
  894. * \return \c 0 if successful.
  895. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  896. * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p min or \p N is invalid
  897. * or if they are incompatible.
  898. * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
  899. * unable to find a suitable value within a limited number
  900. * of attempts. This has a negligible probability if \p N
  901. * is significantly larger than \p min, which is the case
  902. * for all usual cryptographic applications.
  903. * \return Another negative error code on failure.
  904. */
  905. int mbedtls_mpi_random(mbedtls_mpi *X,
  906. mbedtls_mpi_sint min,
  907. const mbedtls_mpi *N,
  908. mbedtls_f_rng_t *f_rng,
  909. void *p_rng);
  910. /**
  911. * \brief Compute the greatest common divisor: G = gcd(A, B)
  912. *
  913. * \param G The destination MPI. This must point to an initialized MPI.
  914. * This will always be positive or 0.
  915. * \param A The first operand. This must point to an initialized MPI.
  916. * \param B The second operand. This must point to an initialized MPI.
  917. *
  918. * \return \c 0 if successful.
  919. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  920. * \return Another negative error code on different kinds of failure.
  921. */
  922. int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A,
  923. const mbedtls_mpi *B);
  924. /**
  925. * \brief Compute the modular inverse: X = A^-1 mod N
  926. *
  927. * \param X The destination MPI. This must point to an initialized MPI.
  928. * The value returned on success will be between [1, N-1].
  929. * \param A The MPI to calculate the modular inverse of. This must point
  930. * to an initialized MPI. This value can be negative, in which
  931. * case a positive answer will still be returned in \p X.
  932. * \param N The base of the modular inversion. This must point to an
  933. * initialized MPI and be greater than one.
  934. *
  935. * \return \c 0 if successful.
  936. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  937. * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p N is less than
  938. * or equal to one.
  939. * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p A has no modular
  940. * inverse with respect to \p N.
  941. */
  942. int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
  943. const mbedtls_mpi *N);
  944. /**
  945. * \brief Miller-Rabin primality test.
  946. *
  947. * \warning If \p X is potentially generated by an adversary, for example
  948. * when validating cryptographic parameters that you didn't
  949. * generate yourself and that are supposed to be prime, then
  950. * \p rounds should be at least the half of the security
  951. * strength of the cryptographic algorithm. On the other hand,
  952. * if \p X is chosen uniformly or non-adversarially (as is the
  953. * case when mbedtls_mpi_gen_prime calls this function), then
  954. * \p rounds can be much lower.
  955. *
  956. * \param X The MPI to check for primality.
  957. * This must point to an initialized MPI.
  958. * \param rounds The number of bases to perform the Miller-Rabin primality
  959. * test for. The probability of returning 0 on a composite is
  960. * at most 2<sup>-2*\p rounds </sup>.
  961. * \param f_rng The RNG function to use. This must not be \c NULL.
  962. * \param p_rng The RNG parameter to be passed to \p f_rng.
  963. * This may be \c NULL if \p f_rng doesn't use
  964. * a context parameter.
  965. *
  966. * \return \c 0 if successful, i.e. \p X is probably prime.
  967. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  968. * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p X is not prime.
  969. * \return Another negative error code on other kinds of failure.
  970. */
  971. int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
  972. mbedtls_f_rng_t *f_rng,
  973. void *p_rng);
  974. /**
  975. * \brief Flags for mbedtls_mpi_gen_prime()
  976. *
  977. * Each of these flags is a constraint on the result X returned by
  978. * mbedtls_mpi_gen_prime().
  979. */
  980. typedef enum {
  981. MBEDTLS_MPI_GEN_PRIME_FLAG_DH = 0x0001, /**< (X-1)/2 is prime too */
  982. MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR = 0x0002, /**< lower error rate from 2<sup>-80</sup> to 2<sup>-128</sup> */
  983. } mbedtls_mpi_gen_prime_flag_t;
  984. /**
  985. * \brief Generate a prime number.
  986. *
  987. * \param X The destination MPI to store the generated prime in.
  988. * This must point to an initialized MPi.
  989. * \param nbits The required size of the destination MPI in bits.
  990. * This must be between \c 3 and #MBEDTLS_MPI_MAX_BITS.
  991. * \param flags A mask of flags of type #mbedtls_mpi_gen_prime_flag_t.
  992. * \param f_rng The RNG function to use. This must not be \c NULL.
  993. * \param p_rng The RNG parameter to be passed to \p f_rng.
  994. * This may be \c NULL if \p f_rng doesn't use
  995. * a context parameter.
  996. *
  997. * \return \c 0 if successful, in which case \p X holds a
  998. * probably prime number.
  999. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  1000. * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if `nbits` is not between
  1001. * \c 3 and #MBEDTLS_MPI_MAX_BITS.
  1002. */
  1003. int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
  1004. mbedtls_f_rng_t *f_rng,
  1005. void *p_rng);
  1006. #if defined(MBEDTLS_SELF_TEST)
  1007. /**
  1008. * \brief Checkup routine
  1009. *
  1010. * \return 0 if successful, or 1 if the test failed
  1011. */
  1012. int mbedtls_mpi_self_test(int verbose);
  1013. #endif /* MBEDTLS_SELF_TEST */
  1014. #ifdef __cplusplus
  1015. }
  1016. #endif
  1017. #endif /* bignum.h */