bignum_core.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /**
  2. * Core bignum functions
  3. *
  4. * This interface should only be used by the legacy bignum module (bignum.h)
  5. * and the modular bignum modules (bignum_mod.c, bignum_mod_raw.c). All other
  6. * modules should use the high-level modular bignum interface (bignum_mod.h)
  7. * or the legacy bignum interface (bignum.h).
  8. *
  9. * This module is about processing non-negative integers with a fixed upper
  10. * bound that's of the form 2^n-1 where n is a multiple of #biL.
  11. * These can be thought of integers written in base 2^#biL with a fixed
  12. * number of digits. Digits in this base are called *limbs*.
  13. * Many operations treat these numbers as the principal representation of
  14. * a number modulo 2^n or a smaller bound.
  15. *
  16. * The functions in this module obey the following conventions unless
  17. * explicitly indicated otherwise:
  18. *
  19. * - **Overflow**: some functions indicate overflow from the range
  20. * [0, 2^n-1] by returning carry parameters, while others operate
  21. * modulo and so cannot overflow. This should be clear from the function
  22. * documentation.
  23. * - **Bignum parameters**: Bignums are passed as pointers to an array of
  24. * limbs. A limb has the type #mbedtls_mpi_uint. Unless otherwise specified:
  25. * - Bignum parameters called \p A, \p B, ... are inputs, and are
  26. * not modified by the function.
  27. * - For operations modulo some number, the modulus is called \p N
  28. * and is input-only.
  29. * - Bignum parameters called \p X, \p Y are outputs or input-output.
  30. * The initial content of output-only parameters is ignored.
  31. * - Some functions use different names that reflect traditional
  32. * naming of operands of certain operations (e.g.
  33. * divisor/dividend/quotient/remainder).
  34. * - \p T is a temporary storage area. The initial content of such
  35. * parameter is ignored and the final content is unspecified.
  36. * - **Bignum sizes**: bignum sizes are always expressed in limbs.
  37. * Most functions work on bignums of a given size and take a single
  38. * \p limbs parameter that applies to all parameters that are limb arrays.
  39. * All bignum sizes must be at least 1 and must be significantly less than
  40. * #SIZE_MAX. The behavior if a size is 0 is undefined. The behavior if the
  41. * total size of all parameters overflows #SIZE_MAX is undefined.
  42. * - **Parameter ordering**: for bignum parameters, outputs come before inputs.
  43. * Temporaries come last.
  44. * - **Aliasing**: in general, output bignums may be aliased to one or more
  45. * inputs. As an exception, parameters that are documented as a modulus value
  46. * may not be aliased to an output. Outputs may not be aliased to one another.
  47. * Temporaries may not be aliased to any other parameter.
  48. * - **Overlap**: apart from aliasing of limb array pointers (where two
  49. * arguments are equal pointers), overlap is not supported and may result
  50. * in undefined behavior.
  51. * - **Error handling**: This is a low-level module. Functions generally do not
  52. * try to protect against invalid arguments such as nonsensical sizes or
  53. * null pointers. Note that some functions that operate on bignums of
  54. * different sizes have constraints about their size, and violating those
  55. * constraints may lead to buffer overflows.
  56. * - **Modular representatives**: functions that operate modulo \p N expect
  57. * all modular inputs to be in the range [0, \p N - 1] and guarantee outputs
  58. * in the range [0, \p N - 1]. If an input is out of range, outputs are
  59. * fully unspecified, though bignum values out of range should not cause
  60. * buffer overflows (beware that this is not extensively tested).
  61. */
  62. /*
  63. * Copyright The Mbed TLS Contributors
  64. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  65. */
  66. #ifndef MBEDTLS_BIGNUM_CORE_H
  67. #define MBEDTLS_BIGNUM_CORE_H
  68. #include "common.h"
  69. #include "mbedtls/bignum.h"
  70. #include "constant_time_internal.h"
  71. #define ciL (sizeof(mbedtls_mpi_uint)) /** chars in limb */
  72. #define biL (ciL << 3) /** bits in limb */
  73. #define biH (ciL << 2) /** half limb size */
  74. /*
  75. * Convert between bits/chars and number of limbs
  76. * Divide first in order to avoid potential overflows
  77. */
  78. #define BITS_TO_LIMBS(i) ((i) / biL + ((i) % biL != 0))
  79. #define CHARS_TO_LIMBS(i) ((i) / ciL + ((i) % ciL != 0))
  80. /* Get a specific byte, without range checks. */
  81. #define GET_BYTE(X, i) \
  82. (((X)[(i) / ciL] >> (((i) % ciL) * 8)) & 0xff)
  83. /* Constants to identify whether a value is public or secret. If a parameter is marked as secret by
  84. * this constant, the function must be constant time with respect to the parameter.
  85. *
  86. * This is only needed for functions with the _optionally_safe postfix. All other functions have
  87. * fixed behavior that can't be changed at runtime and are constant time with respect to their
  88. * parameters as prescribed by their documentation or by conventions in their module's documentation.
  89. *
  90. * Parameters should be named X_public where X is the name of the
  91. * corresponding input parameter.
  92. *
  93. * Implementation should always check using
  94. * if (X_public == MBEDTLS_MPI_IS_PUBLIC) {
  95. * // unsafe path
  96. * } else {
  97. * // safe path
  98. * }
  99. * not the other way round, in order to prevent misuse. (That is, if a value
  100. * other than the two below is passed, default to the safe path.)
  101. *
  102. * The value of MBEDTLS_MPI_IS_PUBLIC is chosen in a way that is unlikely to happen by accident, but
  103. * which can be used as an immediate value in a Thumb2 comparison (for code size). */
  104. #define MBEDTLS_MPI_IS_PUBLIC 0x2a2a2a2a
  105. #define MBEDTLS_MPI_IS_SECRET 0
  106. #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
  107. // Default value for testing that is neither MBEDTLS_MPI_IS_PUBLIC nor MBEDTLS_MPI_IS_SECRET
  108. #define MBEDTLS_MPI_IS_TEST 1
  109. #endif
  110. /** Count leading zero bits in a given integer.
  111. *
  112. * \warning The result is undefined if \p a == 0
  113. *
  114. * \param a Integer to count leading zero bits.
  115. *
  116. * \return The number of leading zero bits in \p a, if \p a != 0.
  117. * If \p a == 0, the result is undefined.
  118. */
  119. size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a);
  120. /** Return the minimum number of bits required to represent the value held
  121. * in the MPI.
  122. *
  123. * \note This function returns 0 if all the limbs of \p A are 0.
  124. *
  125. * \param[in] A The address of the MPI.
  126. * \param A_limbs The number of limbs of \p A.
  127. *
  128. * \return The number of bits in \p A.
  129. */
  130. size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs);
  131. /** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
  132. * into the storage form used by mbedtls_mpi.
  133. *
  134. * \param[in,out] A The address of the MPI.
  135. * \param A_limbs The number of limbs of \p A.
  136. */
  137. void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A,
  138. size_t A_limbs);
  139. /** \brief Compare a machine integer with an MPI.
  140. *
  141. * This function operates in constant time with respect
  142. * to the values of \p min and \p A.
  143. *
  144. * \param min A machine integer.
  145. * \param[in] A An MPI.
  146. * \param A_limbs The number of limbs of \p A.
  147. * This must be at least 1.
  148. *
  149. * \return MBEDTLS_CT_TRUE if \p min is less than or equal to \p A, otherwise MBEDTLS_CT_FALSE.
  150. */
  151. mbedtls_ct_condition_t mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min,
  152. const mbedtls_mpi_uint *A,
  153. size_t A_limbs);
  154. /**
  155. * \brief Check if one unsigned MPI is less than another in constant
  156. * time.
  157. *
  158. * \param A The left-hand MPI. This must point to an array of limbs
  159. * with the same allocated length as \p B.
  160. * \param B The right-hand MPI. This must point to an array of limbs
  161. * with the same allocated length as \p A.
  162. * \param limbs The number of limbs in \p A and \p B.
  163. * This must not be 0.
  164. *
  165. * \return MBEDTLS_CT_TRUE if \p A is less than \p B.
  166. * MBEDTLS_CT_FALSE if \p A is greater than or equal to \p B.
  167. */
  168. mbedtls_ct_condition_t mbedtls_mpi_core_lt_ct(const mbedtls_mpi_uint *A,
  169. const mbedtls_mpi_uint *B,
  170. size_t limbs);
  171. /**
  172. * \brief Perform a safe conditional copy of an MPI which doesn't reveal
  173. * whether assignment was done or not.
  174. *
  175. * \param[out] X The address of the destination MPI.
  176. * This must be initialized. Must have enough limbs to
  177. * store the full value of \p A.
  178. * \param[in] A The address of the source MPI. This must be initialized.
  179. * \param limbs The number of limbs of \p A.
  180. * \param assign The condition deciding whether to perform the
  181. * assignment or not. Callers will need to use
  182. * the constant time interface (e.g. `mbedtls_ct_bool()`)
  183. * to construct this argument.
  184. *
  185. * \note This function avoids leaking any information about whether
  186. * the assignment was done or not.
  187. */
  188. void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X,
  189. const mbedtls_mpi_uint *A,
  190. size_t limbs,
  191. mbedtls_ct_condition_t assign);
  192. /**
  193. * \brief Perform a safe conditional swap of two MPIs which doesn't reveal
  194. * whether the swap was done or not.
  195. *
  196. * \param[in,out] X The address of the first MPI.
  197. * This must be initialized.
  198. * \param[in,out] Y The address of the second MPI.
  199. * This must be initialized.
  200. * \param limbs The number of limbs of \p X and \p Y.
  201. * \param swap The condition deciding whether to perform
  202. * the swap or not.
  203. *
  204. * \note This function avoids leaking any information about whether
  205. * the swap was done or not.
  206. */
  207. void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X,
  208. mbedtls_mpi_uint *Y,
  209. size_t limbs,
  210. mbedtls_ct_condition_t swap);
  211. /** Import X from unsigned binary data, little-endian.
  212. *
  213. * The MPI needs to have enough limbs to store the full value (including any
  214. * most significant zero bytes in the input).
  215. *
  216. * \param[out] X The address of the MPI.
  217. * \param X_limbs The number of limbs of \p X.
  218. * \param[in] input The input buffer to import from.
  219. * \param input_length The length bytes of \p input.
  220. *
  221. * \return \c 0 if successful.
  222. * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
  223. * large enough to hold the value in \p input.
  224. */
  225. int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X,
  226. size_t X_limbs,
  227. const unsigned char *input,
  228. size_t input_length);
  229. /** Import X from unsigned binary data, big-endian.
  230. *
  231. * The MPI needs to have enough limbs to store the full value (including any
  232. * most significant zero bytes in the input).
  233. *
  234. * \param[out] X The address of the MPI.
  235. * May only be #NULL if \p X_limbs is 0 and \p input_length
  236. * is 0.
  237. * \param X_limbs The number of limbs of \p X.
  238. * \param[in] input The input buffer to import from.
  239. * May only be #NULL if \p input_length is 0.
  240. * \param input_length The length in bytes of \p input.
  241. *
  242. * \return \c 0 if successful.
  243. * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
  244. * large enough to hold the value in \p input.
  245. */
  246. int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X,
  247. size_t X_limbs,
  248. const unsigned char *input,
  249. size_t input_length);
  250. /** Export A into unsigned binary data, little-endian.
  251. *
  252. * \note If \p output is shorter than \p A the export is still successful if the
  253. * value held in \p A fits in the buffer (that is, if enough of the most
  254. * significant bytes of \p A are 0).
  255. *
  256. * \param[in] A The address of the MPI.
  257. * \param A_limbs The number of limbs of \p A.
  258. * \param[out] output The output buffer to export to.
  259. * \param output_length The length in bytes of \p output.
  260. *
  261. * \return \c 0 if successful.
  262. * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
  263. * large enough to hold the value of \p A.
  264. */
  265. int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A,
  266. size_t A_limbs,
  267. unsigned char *output,
  268. size_t output_length);
  269. /** Export A into unsigned binary data, big-endian.
  270. *
  271. * \note If \p output is shorter than \p A the export is still successful if the
  272. * value held in \p A fits in the buffer (that is, if enough of the most
  273. * significant bytes of \p A are 0).
  274. *
  275. * \param[in] A The address of the MPI.
  276. * \param A_limbs The number of limbs of \p A.
  277. * \param[out] output The output buffer to export to.
  278. * \param output_length The length in bytes of \p output.
  279. *
  280. * \return \c 0 if successful.
  281. * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
  282. * large enough to hold the value of \p A.
  283. */
  284. int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *A,
  285. size_t A_limbs,
  286. unsigned char *output,
  287. size_t output_length);
  288. /** \brief Shift an MPI in-place right by a number of bits.
  289. *
  290. * Shifting by more bits than there are bit positions
  291. * in \p X is valid and results in setting \p X to 0.
  292. *
  293. * This function's execution time depends on the value
  294. * of \p count (and of course \p limbs).
  295. *
  296. * \param[in,out] X The number to shift.
  297. * \param limbs The number of limbs of \p X. This must be at least 1.
  298. * \param count The number of bits to shift by.
  299. */
  300. void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
  301. size_t count);
  302. /**
  303. * \brief Shift an MPI in-place left by a number of bits.
  304. *
  305. * Shifting by more bits than there are bit positions
  306. * in \p X will produce an unspecified result.
  307. *
  308. * This function's execution time depends on the value
  309. * of \p count (and of course \p limbs).
  310. * \param[in,out] X The number to shift.
  311. * \param limbs The number of limbs of \p X. This must be at least 1.
  312. * \param count The number of bits to shift by.
  313. */
  314. void mbedtls_mpi_core_shift_l(mbedtls_mpi_uint *X, size_t limbs,
  315. size_t count);
  316. /**
  317. * \brief Add two fixed-size large unsigned integers, returning the carry.
  318. *
  319. * Calculates `A + B` where `A` and `B` have the same size.
  320. *
  321. * This function operates modulo `2^(biL*limbs)` and returns the carry
  322. * (1 if there was a wraparound, and 0 otherwise).
  323. *
  324. * \p X may be aliased to \p A or \p B.
  325. *
  326. * \param[out] X The result of the addition.
  327. * \param[in] A Little-endian presentation of the left operand.
  328. * \param[in] B Little-endian presentation of the right operand.
  329. * \param limbs Number of limbs of \p X, \p A and \p B.
  330. *
  331. * \return 1 if `A + B >= 2^(biL*limbs)`, 0 otherwise.
  332. */
  333. mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,
  334. const mbedtls_mpi_uint *A,
  335. const mbedtls_mpi_uint *B,
  336. size_t limbs);
  337. /**
  338. * \brief Conditional addition of two fixed-size large unsigned integers,
  339. * returning the carry.
  340. *
  341. * Functionally equivalent to
  342. *
  343. * ```
  344. * if( cond )
  345. * X += A;
  346. * return carry;
  347. * ```
  348. *
  349. * This function operates modulo `2^(biL*limbs)`.
  350. *
  351. * \param[in,out] X The pointer to the (little-endian) array
  352. * representing the bignum to accumulate onto.
  353. * \param[in] A The pointer to the (little-endian) array
  354. * representing the bignum to conditionally add
  355. * to \p X. This may be aliased to \p X but may not
  356. * overlap otherwise.
  357. * \param limbs Number of limbs of \p X and \p A.
  358. * \param cond Condition bit dictating whether addition should
  359. * happen or not. This must be \c 0 or \c 1.
  360. *
  361. * \warning If \p cond is neither 0 nor 1, the result of this function
  362. * is unspecified, and the resulting value in \p X might be
  363. * neither its original value nor \p X + \p A.
  364. *
  365. * \return 1 if `X + cond * A >= 2^(biL*limbs)`, 0 otherwise.
  366. */
  367. mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X,
  368. const mbedtls_mpi_uint *A,
  369. size_t limbs,
  370. unsigned cond);
  371. /**
  372. * \brief Subtract two fixed-size large unsigned integers, returning the borrow.
  373. *
  374. * Calculate `A - B` where \p A and \p B have the same size.
  375. * This function operates modulo `2^(biL*limbs)` and returns the carry
  376. * (1 if there was a wraparound, i.e. if `A < B`, and 0 otherwise).
  377. *
  378. * \p X may be aliased to \p A or \p B, or even both, but may not overlap
  379. * either otherwise.
  380. *
  381. * \param[out] X The result of the subtraction.
  382. * \param[in] A Little-endian presentation of left operand.
  383. * \param[in] B Little-endian presentation of right operand.
  384. * \param limbs Number of limbs of \p X, \p A and \p B.
  385. *
  386. * \return 1 if `A < B`.
  387. * 0 if `A >= B`.
  388. */
  389. mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X,
  390. const mbedtls_mpi_uint *A,
  391. const mbedtls_mpi_uint *B,
  392. size_t limbs);
  393. /**
  394. * \brief Perform a fixed-size multiply accumulate operation: X += b * A
  395. *
  396. * \p X may be aliased to \p A (when \p X_limbs == \p A_limbs), but may not
  397. * otherwise overlap.
  398. *
  399. * This function operates modulo `2^(biL*X_limbs)`.
  400. *
  401. * \param[in,out] X The pointer to the (little-endian) array
  402. * representing the bignum to accumulate onto.
  403. * \param X_limbs The number of limbs of \p X. This must be
  404. * at least \p A_limbs.
  405. * \param[in] A The pointer to the (little-endian) array
  406. * representing the bignum to multiply with.
  407. * This may be aliased to \p X but may not overlap
  408. * otherwise.
  409. * \param A_limbs The number of limbs of \p A.
  410. * \param b X scalar to multiply with.
  411. *
  412. * \return The carry at the end of the operation.
  413. */
  414. mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *X, size_t X_limbs,
  415. const mbedtls_mpi_uint *A, size_t A_limbs,
  416. mbedtls_mpi_uint b);
  417. /**
  418. * \brief Perform a known-size multiplication
  419. *
  420. * \p X may not be aliased to any of the inputs for this function.
  421. * \p A may be aliased to \p B.
  422. *
  423. * \param[out] X The pointer to the (little-endian) array to receive
  424. * the product of \p A_limbs and \p B_limbs.
  425. * This must be of length \p A_limbs + \p B_limbs.
  426. * \param[in] A The pointer to the (little-endian) array
  427. * representing the first factor.
  428. * \param A_limbs The number of limbs in \p A.
  429. * \param[in] B The pointer to the (little-endian) array
  430. * representing the second factor.
  431. * \param B_limbs The number of limbs in \p B.
  432. */
  433. void mbedtls_mpi_core_mul(mbedtls_mpi_uint *X,
  434. const mbedtls_mpi_uint *A, size_t A_limbs,
  435. const mbedtls_mpi_uint *B, size_t B_limbs);
  436. /**
  437. * \brief Calculate initialisation value for fast Montgomery modular
  438. * multiplication
  439. *
  440. * \param[in] N Little-endian presentation of the modulus. This must have
  441. * at least one limb.
  442. *
  443. * \return The initialisation value for fast Montgomery modular multiplication
  444. */
  445. mbedtls_mpi_uint mbedtls_mpi_core_montmul_init(const mbedtls_mpi_uint *N);
  446. /**
  447. * \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
  448. *
  449. * \p A and \p B must be in canonical form. That is, < \p N.
  450. *
  451. * \p X may be aliased to \p A or \p N, or even \p B (if \p AN_limbs ==
  452. * \p B_limbs) but may not overlap any parameters otherwise.
  453. *
  454. * \p A and \p B may alias each other, if \p AN_limbs == \p B_limbs. They may
  455. * not alias \p N (since they must be in canonical form, they cannot == \p N).
  456. *
  457. * \param[out] X The destination MPI, as a little-endian array of
  458. * length \p AN_limbs.
  459. * On successful completion, X contains the result of
  460. * the multiplication `A * B * R^-1` mod N where
  461. * `R = 2^(biL*AN_limbs)`.
  462. * \param[in] A Little-endian presentation of first operand.
  463. * Must have the same number of limbs as \p N.
  464. * \param[in] B Little-endian presentation of second operand.
  465. * \param[in] B_limbs The number of limbs in \p B.
  466. * Must be <= \p AN_limbs.
  467. * \param[in] N Little-endian presentation of the modulus.
  468. * This must be odd, and have exactly the same number
  469. * of limbs as \p A.
  470. * It may alias \p X, but must not alias or otherwise
  471. * overlap any of the other parameters.
  472. * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
  473. * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
  474. * This can be calculated by `mbedtls_mpi_core_montmul_init()`.
  475. * \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs.
  476. * Its initial content is unused and
  477. * its final content is indeterminate.
  478. * It must not alias or otherwise overlap any of the
  479. * other parameters.
  480. */
  481. void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,
  482. const mbedtls_mpi_uint *A,
  483. const mbedtls_mpi_uint *B, size_t B_limbs,
  484. const mbedtls_mpi_uint *N, size_t AN_limbs,
  485. mbedtls_mpi_uint mm, mbedtls_mpi_uint *T);
  486. /**
  487. * \brief Calculate the square of the Montgomery constant. (Needed
  488. * for conversion and operations in Montgomery form.)
  489. *
  490. * \param[out] X A pointer to the result of the calculation of
  491. * the square of the Montgomery constant:
  492. * 2^{2*n*biL} mod N.
  493. * \param[in] N Little-endian presentation of the modulus, which must be odd.
  494. *
  495. * \return 0 if successful.
  496. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if there is not enough space
  497. * to store the value of Montgomery constant squared.
  498. * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p N modulus is zero.
  499. * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p N modulus is negative.
  500. */
  501. int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X,
  502. const mbedtls_mpi *N);
  503. #if defined(MBEDTLS_TEST_HOOKS)
  504. /**
  505. * Copy an MPI from a table without leaking the index.
  506. *
  507. * \param dest The destination buffer. This must point to a writable
  508. * buffer of at least \p limbs limbs.
  509. * \param table The address of the table. This must point to a readable
  510. * array of \p count elements of \p limbs limbs each.
  511. * \param limbs The number of limbs in each table entry.
  512. * \param count The number of entries in \p table.
  513. * \param index The (secret) table index to look up. This must be in the
  514. * range `0 .. count-1`.
  515. */
  516. void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest,
  517. const mbedtls_mpi_uint *table,
  518. size_t limbs,
  519. size_t count,
  520. size_t index);
  521. #endif /* MBEDTLS_TEST_HOOKS */
  522. /**
  523. * \brief Fill an integer with a number of random bytes.
  524. *
  525. * \param X The destination MPI.
  526. * \param X_limbs The number of limbs of \p X.
  527. * \param bytes The number of random bytes to generate.
  528. * \param f_rng The RNG function to use. This must not be \c NULL.
  529. * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
  530. * \c NULL if \p f_rng doesn't need a context argument.
  531. *
  532. * \return \c 0 if successful.
  533. * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p X does not have
  534. * enough room for \p bytes bytes.
  535. * \return A negative error code on RNG failure.
  536. *
  537. * \note The bytes obtained from the RNG are interpreted
  538. * as a big-endian representation of an MPI; this can
  539. * be relevant in applications like deterministic ECDSA.
  540. */
  541. int mbedtls_mpi_core_fill_random(mbedtls_mpi_uint *X, size_t X_limbs,
  542. size_t bytes,
  543. int (*f_rng)(void *, unsigned char *, size_t),
  544. void *p_rng);
  545. /** Generate a random number uniformly in a range.
  546. *
  547. * This function generates a random number between \p min inclusive and
  548. * \p N exclusive.
  549. *
  550. * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
  551. * when the RNG is a suitably parametrized instance of HMAC_DRBG
  552. * and \p min is \c 1.
  553. *
  554. * \note There are `N - min` possible outputs. The lower bound
  555. * \p min can be reached, but the upper bound \p N cannot.
  556. *
  557. * \param X The destination MPI, with \p limbs limbs.
  558. * It must not be aliased with \p N or otherwise overlap it.
  559. * \param min The minimum value to return.
  560. * \param N The upper bound of the range, exclusive, with \p limbs limbs.
  561. * In other words, this is one plus the maximum value to return.
  562. * \p N must be strictly larger than \p min.
  563. * \param limbs The number of limbs of \p N and \p X.
  564. * This must not be 0.
  565. * \param f_rng The RNG function to use. This must not be \c NULL.
  566. * \param p_rng The RNG parameter to be passed to \p f_rng.
  567. *
  568. * \return \c 0 if successful.
  569. * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
  570. * unable to find a suitable value within a limited number
  571. * of attempts. This has a negligible probability if \p N
  572. * is significantly larger than \p min, which is the case
  573. * for all usual cryptographic applications.
  574. */
  575. int mbedtls_mpi_core_random(mbedtls_mpi_uint *X,
  576. mbedtls_mpi_uint min,
  577. const mbedtls_mpi_uint *N,
  578. size_t limbs,
  579. int (*f_rng)(void *, unsigned char *, size_t),
  580. void *p_rng);
  581. /**
  582. * \brief Returns the number of limbs of working memory required for
  583. * a call to `mbedtls_mpi_core_exp_mod()`.
  584. *
  585. * \note This will always be at least
  586. * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`,
  587. * i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`.
  588. *
  589. * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
  590. * (they must be the same size) that will be given to
  591. * `mbedtls_mpi_core_exp_mod()`.
  592. * \param E_limbs The number of limbs in the exponent `E` that will be given
  593. * to `mbedtls_mpi_core_exp_mod()`.
  594. *
  595. * \return The number of limbs of working memory required by
  596. * `mbedtls_mpi_core_exp_mod()`.
  597. */
  598. size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs);
  599. /**
  600. * \brief Perform a modular exponentiation with public or secret exponent:
  601. * X = A^E mod N, where \p A is already in Montgomery form.
  602. *
  603. * \warning This function is not constant time with respect to \p E (the exponent).
  604. *
  605. * \p X may be aliased to \p A, but not to \p RR or \p E, even if \p E_limbs ==
  606. * \p AN_limbs.
  607. *
  608. * \param[out] X The destination MPI, as a little endian array of length
  609. * \p AN_limbs.
  610. * \param[in] A The base MPI, as a little endian array of length \p AN_limbs.
  611. * Must be in Montgomery form.
  612. * \param[in] N The modulus, as a little endian array of length \p AN_limbs.
  613. * \param AN_limbs The number of limbs in \p X, \p A, \p N, \p RR.
  614. * \param[in] E The exponent, as a little endian array of length \p E_limbs.
  615. * \param E_limbs The number of limbs in \p E.
  616. * \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little
  617. * endian array of length \p AN_limbs.
  618. * \param[in,out] T Temporary storage of at least the number of limbs returned
  619. * by `mbedtls_mpi_core_exp_mod_working_limbs()`.
  620. * Its initial content is unused and its final content is
  621. * indeterminate.
  622. * It must not alias or otherwise overlap any of the other
  623. * parameters.
  624. * It is up to the caller to zeroize \p T when it is no
  625. * longer needed, and before freeing it if it was dynamically
  626. * allocated.
  627. */
  628. void mbedtls_mpi_core_exp_mod_unsafe(mbedtls_mpi_uint *X,
  629. const mbedtls_mpi_uint *A,
  630. const mbedtls_mpi_uint *N, size_t AN_limbs,
  631. const mbedtls_mpi_uint *E, size_t E_limbs,
  632. const mbedtls_mpi_uint *RR,
  633. mbedtls_mpi_uint *T);
  634. /**
  635. * \brief Perform a modular exponentiation with secret exponent:
  636. * X = A^E mod N, where \p A is already in Montgomery form.
  637. *
  638. * \p X may be aliased to \p A, but not to \p RR or \p E, even if \p E_limbs ==
  639. * \p AN_limbs.
  640. *
  641. * \param[out] X The destination MPI, as a little endian array of length
  642. * \p AN_limbs.
  643. * \param[in] A The base MPI, as a little endian array of length \p AN_limbs.
  644. * Must be in Montgomery form.
  645. * \param[in] N The modulus, as a little endian array of length \p AN_limbs.
  646. * \param AN_limbs The number of limbs in \p X, \p A, \p N, \p RR.
  647. * \param[in] E The exponent, as a little endian array of length \p E_limbs.
  648. * \param E_limbs The number of limbs in \p E.
  649. * \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little
  650. * endian array of length \p AN_limbs.
  651. * \param[in,out] T Temporary storage of at least the number of limbs returned
  652. * by `mbedtls_mpi_core_exp_mod_working_limbs()`.
  653. * Its initial content is unused and its final content is
  654. * indeterminate.
  655. * It must not alias or otherwise overlap any of the other
  656. * parameters.
  657. * It is up to the caller to zeroize \p T when it is no
  658. * longer needed, and before freeing it if it was dynamically
  659. * allocated.
  660. */
  661. void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,
  662. const mbedtls_mpi_uint *A,
  663. const mbedtls_mpi_uint *N, size_t AN_limbs,
  664. const mbedtls_mpi_uint *E, size_t E_limbs,
  665. const mbedtls_mpi_uint *RR,
  666. mbedtls_mpi_uint *T);
  667. /**
  668. * \brief Subtract unsigned integer from known-size large unsigned integers.
  669. * Return the borrow.
  670. *
  671. * \param[out] X The result of the subtraction.
  672. * \param[in] A The left operand.
  673. * \param b The unsigned scalar to subtract.
  674. * \param limbs Number of limbs of \p X and \p A.
  675. *
  676. * \return 1 if `A < b`.
  677. * 0 if `A >= b`.
  678. */
  679. mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,
  680. const mbedtls_mpi_uint *A,
  681. mbedtls_mpi_uint b,
  682. size_t limbs);
  683. /**
  684. * \brief Determine if a given MPI has the value \c 0 in constant time with
  685. * respect to the value (but not with respect to the number of limbs).
  686. *
  687. * \param[in] A The MPI to test.
  688. * \param limbs Number of limbs in \p A.
  689. *
  690. * \return MBEDTLS_CT_FALSE if `A == 0`
  691. * MBEDTLS_CT_TRUE if `A != 0`.
  692. */
  693. mbedtls_ct_condition_t mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,
  694. size_t limbs);
  695. /**
  696. * \brief Returns the number of limbs of working memory required for
  697. * a call to `mbedtls_mpi_core_montmul()`.
  698. *
  699. * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
  700. * (they must be the same size) that will be given to
  701. * `mbedtls_mpi_core_montmul()` or one of the other functions
  702. * that specifies this as the amount of working memory needed.
  703. *
  704. * \return The number of limbs of working memory required by
  705. * `mbedtls_mpi_core_montmul()` (or other similar function).
  706. */
  707. static inline size_t mbedtls_mpi_core_montmul_working_limbs(size_t AN_limbs)
  708. {
  709. return 2 * AN_limbs + 1;
  710. }
  711. /** Convert an MPI into Montgomery form.
  712. *
  713. * \p X may be aliased to \p A, but may not otherwise overlap it.
  714. *
  715. * \p X may not alias \p N (it is in canonical form, so must be strictly less
  716. * than \p N). Nor may it alias or overlap \p rr (this is unlikely to be
  717. * required in practice.)
  718. *
  719. * This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is
  720. * an alternative to calling `mbedtls_mpi_mod_raw_to_mont_rep()` when we
  721. * don't want to allocate memory.
  722. *
  723. * \param[out] X The result of the conversion.
  724. * Must have the same number of limbs as \p A.
  725. * \param[in] A The MPI to convert into Montgomery form.
  726. * Must have the same number of limbs as the modulus.
  727. * \param[in] N The address of the modulus, which gives the size of
  728. * the base `R` = 2^(biL*N->limbs).
  729. * \param[in] AN_limbs The number of limbs in \p X, \p A, \p N and \p rr.
  730. * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
  731. * This can be determined by calling
  732. * `mbedtls_mpi_core_montmul_init()`.
  733. * \param[in] rr The residue for `2^{2*n*biL} mod N`.
  734. * \param[in,out] T Temporary storage of size at least
  735. * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`
  736. * limbs.
  737. * Its initial content is unused and
  738. * its final content is indeterminate.
  739. * It must not alias or otherwise overlap any of the
  740. * other parameters.
  741. */
  742. void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
  743. const mbedtls_mpi_uint *A,
  744. const mbedtls_mpi_uint *N,
  745. size_t AN_limbs,
  746. mbedtls_mpi_uint mm,
  747. const mbedtls_mpi_uint *rr,
  748. mbedtls_mpi_uint *T);
  749. /** Convert an MPI from Montgomery form.
  750. *
  751. * \p X may be aliased to \p A, but may not otherwise overlap it.
  752. *
  753. * \p X may not alias \p N (it is in canonical form, so must be strictly less
  754. * than \p N).
  755. *
  756. * This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is
  757. * an alternative to calling `mbedtls_mpi_mod_raw_from_mont_rep()` when we
  758. * don't want to allocate memory.
  759. *
  760. * \param[out] X The result of the conversion.
  761. * Must have the same number of limbs as \p A.
  762. * \param[in] A The MPI to convert from Montgomery form.
  763. * Must have the same number of limbs as the modulus.
  764. * \param[in] N The address of the modulus, which gives the size of
  765. * the base `R` = 2^(biL*N->limbs).
  766. * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
  767. * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
  768. * This can be determined by calling
  769. * `mbedtls_mpi_core_montmul_init()`.
  770. * \param[in,out] T Temporary storage of size at least
  771. * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`
  772. * limbs.
  773. * Its initial content is unused and
  774. * its final content is indeterminate.
  775. * It must not alias or otherwise overlap any of the
  776. * other parameters.
  777. */
  778. void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,
  779. const mbedtls_mpi_uint *A,
  780. const mbedtls_mpi_uint *N,
  781. size_t AN_limbs,
  782. mbedtls_mpi_uint mm,
  783. mbedtls_mpi_uint *T);
  784. #endif /* MBEDTLS_BIGNUM_CORE_H */