bignum_core.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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
  65. *
  66. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  67. * not use this file except in compliance with the License.
  68. * You may obtain a copy of the License at
  69. *
  70. * http://www.apache.org/licenses/LICENSE-2.0
  71. *
  72. * Unless required by applicable law or agreed to in writing, software
  73. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  74. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  75. * See the License for the specific language governing permissions and
  76. * limitations under the License.
  77. */
  78. #ifndef MBEDTLS_BIGNUM_CORE_H
  79. #define MBEDTLS_BIGNUM_CORE_H
  80. #include "common.h"
  81. #if defined(MBEDTLS_BIGNUM_C)
  82. #include "mbedtls/bignum.h"
  83. #endif
  84. #define ciL (sizeof(mbedtls_mpi_uint)) /** chars in limb */
  85. #define biL (ciL << 3) /** bits in limb */
  86. #define biH (ciL << 2) /** half limb size */
  87. /*
  88. * Convert between bits/chars and number of limbs
  89. * Divide first in order to avoid potential overflows
  90. */
  91. #define BITS_TO_LIMBS(i) ((i) / biL + ((i) % biL != 0))
  92. #define CHARS_TO_LIMBS(i) ((i) / ciL + ((i) % ciL != 0))
  93. /* Get a specific byte, without range checks. */
  94. #define GET_BYTE(X, i) \
  95. (((X)[(i) / ciL] >> (((i) % ciL) * 8)) & 0xff)
  96. /** Count leading zero bits in a given integer.
  97. *
  98. * \param a Integer to count leading zero bits.
  99. *
  100. * \return The number of leading zero bits in \p a.
  101. */
  102. size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a);
  103. /** Return the minimum number of bits required to represent the value held
  104. * in the MPI.
  105. *
  106. * \note This function returns 0 if all the limbs of \p A are 0.
  107. *
  108. * \param[in] A The address of the MPI.
  109. * \param A_limbs The number of limbs of \p A.
  110. *
  111. * \return The number of bits in \p A.
  112. */
  113. size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs);
  114. /** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
  115. * into the storage form used by mbedtls_mpi.
  116. *
  117. * \param[in,out] A The address of the MPI.
  118. * \param A_limbs The number of limbs of \p A.
  119. */
  120. void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A,
  121. size_t A_limbs);
  122. /** \brief Compare a machine integer with an MPI.
  123. *
  124. * This function operates in constant time with respect
  125. * to the values of \p min and \p A.
  126. *
  127. * \param min A machine integer.
  128. * \param[in] A An MPI.
  129. * \param A_limbs The number of limbs of \p A.
  130. * This must be at least 1.
  131. *
  132. * \return 1 if \p min is less than or equal to \p A, otherwise 0.
  133. */
  134. unsigned mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min,
  135. const mbedtls_mpi_uint *A,
  136. size_t A_limbs);
  137. /**
  138. * \brief Perform a safe conditional copy of an MPI which doesn't reveal
  139. * whether assignment was done or not.
  140. *
  141. * \param[out] X The address of the destination MPI.
  142. * This must be initialized. Must have enough limbs to
  143. * store the full value of \p A.
  144. * \param[in] A The address of the source MPI. This must be initialized.
  145. * \param limbs The number of limbs of \p A.
  146. * \param assign The condition deciding whether to perform the
  147. * assignment or not. Must be either 0 or 1:
  148. * * \c 1: Perform the assignment `X = A`.
  149. * * \c 0: Keep the original value of \p X.
  150. *
  151. * \note This function avoids leaking any information about whether
  152. * the assignment was done or not.
  153. *
  154. * \warning If \p assign is neither 0 nor 1, the result of this function
  155. * is indeterminate, and the resulting value in \p X might be
  156. * neither its original value nor the value in \p A.
  157. */
  158. void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X,
  159. const mbedtls_mpi_uint *A,
  160. size_t limbs,
  161. unsigned char assign);
  162. /**
  163. * \brief Perform a safe conditional swap of two MPIs which doesn't reveal
  164. * whether the swap was done or not.
  165. *
  166. * \param[in,out] X The address of the first MPI.
  167. * This must be initialized.
  168. * \param[in,out] Y The address of the second MPI.
  169. * This must be initialized.
  170. * \param limbs The number of limbs of \p X and \p Y.
  171. * \param swap The condition deciding whether to perform
  172. * the swap or not. Must be either 0 or 1:
  173. * * \c 1: Swap the values of \p X and \p Y.
  174. * * \c 0: Keep the original values of \p X and \p Y.
  175. *
  176. * \note This function avoids leaking any information about whether
  177. * the swap was done or not.
  178. *
  179. * \warning If \p swap is neither 0 nor 1, the result of this function
  180. * is indeterminate, and both \p X and \p Y might end up with
  181. * values different to either of the original ones.
  182. */
  183. void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X,
  184. mbedtls_mpi_uint *Y,
  185. size_t limbs,
  186. unsigned char swap);
  187. /** Import X from unsigned binary data, little-endian.
  188. *
  189. * The MPI needs to have enough limbs to store the full value (including any
  190. * most significant zero bytes in the input).
  191. *
  192. * \param[out] X The address of the MPI.
  193. * \param X_limbs The number of limbs of \p X.
  194. * \param[in] input The input buffer to import from.
  195. * \param input_length The length bytes of \p input.
  196. *
  197. * \return \c 0 if successful.
  198. * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
  199. * large enough to hold the value in \p input.
  200. */
  201. int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X,
  202. size_t X_limbs,
  203. const unsigned char *input,
  204. size_t input_length);
  205. /** Import X from unsigned binary data, big-endian.
  206. *
  207. * The MPI needs to have enough limbs to store the full value (including any
  208. * most significant zero bytes in the input).
  209. *
  210. * \param[out] X The address of the MPI.
  211. * May only be #NULL if \p X_limbs is 0 and \p input_length
  212. * is 0.
  213. * \param X_limbs The number of limbs of \p X.
  214. * \param[in] input The input buffer to import from.
  215. * May only be #NULL if \p input_length is 0.
  216. * \param input_length The length in bytes of \p input.
  217. *
  218. * \return \c 0 if successful.
  219. * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
  220. * large enough to hold the value in \p input.
  221. */
  222. int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X,
  223. size_t X_limbs,
  224. const unsigned char *input,
  225. size_t input_length);
  226. /** Export A into unsigned binary data, little-endian.
  227. *
  228. * \note If \p output is shorter than \p A the export is still successful if the
  229. * value held in \p A fits in the buffer (that is, if enough of the most
  230. * significant bytes of \p A are 0).
  231. *
  232. * \param[in] A The address of the MPI.
  233. * \param A_limbs The number of limbs of \p A.
  234. * \param[out] output The output buffer to export to.
  235. * \param output_length The length in bytes of \p output.
  236. *
  237. * \return \c 0 if successful.
  238. * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
  239. * large enough to hold the value of \p A.
  240. */
  241. int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A,
  242. size_t A_limbs,
  243. unsigned char *output,
  244. size_t output_length);
  245. /** Export A into unsigned binary data, big-endian.
  246. *
  247. * \note If \p output is shorter than \p A the export is still successful if the
  248. * value held in \p A fits in the buffer (that is, if enough of the most
  249. * significant bytes of \p A are 0).
  250. *
  251. * \param[in] A The address of the MPI.
  252. * \param A_limbs The number of limbs of \p A.
  253. * \param[out] output The output buffer to export to.
  254. * \param output_length The length in bytes of \p output.
  255. *
  256. * \return \c 0 if successful.
  257. * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
  258. * large enough to hold the value of \p A.
  259. */
  260. int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *A,
  261. size_t A_limbs,
  262. unsigned char *output,
  263. size_t output_length);
  264. /** \brief Shift an MPI right in place by a number of bits.
  265. *
  266. * Shifting by more bits than there are bit positions
  267. * in \p X is valid and results in setting \p X to 0.
  268. *
  269. * This function's execution time depends on the value
  270. * of \p count (and of course \p limbs).
  271. *
  272. * \param[in,out] X The number to shift.
  273. * \param limbs The number of limbs of \p X. This must be at least 1.
  274. * \param count The number of bits to shift by.
  275. */
  276. void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
  277. size_t count);
  278. /**
  279. * \brief Add two fixed-size large unsigned integers, returning the carry.
  280. *
  281. * Calculates `A + B` where `A` and `B` have the same size.
  282. *
  283. * This function operates modulo `2^(biL*limbs)` and returns the carry
  284. * (1 if there was a wraparound, and 0 otherwise).
  285. *
  286. * \p X may be aliased to \p A or \p B.
  287. *
  288. * \param[out] X The result of the addition.
  289. * \param[in] A Little-endian presentation of the left operand.
  290. * \param[in] B Little-endian presentation of the right operand.
  291. * \param limbs Number of limbs of \p X, \p A and \p B.
  292. *
  293. * \return 1 if `A + B >= 2^(biL*limbs)`, 0 otherwise.
  294. */
  295. mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,
  296. const mbedtls_mpi_uint *A,
  297. const mbedtls_mpi_uint *B,
  298. size_t limbs);
  299. /**
  300. * \brief Conditional addition of two fixed-size large unsigned integers,
  301. * returning the carry.
  302. *
  303. * Functionally equivalent to
  304. *
  305. * ```
  306. * if( cond )
  307. * X += A;
  308. * return carry;
  309. * ```
  310. *
  311. * This function operates modulo `2^(biL*limbs)`.
  312. *
  313. * \param[in,out] X The pointer to the (little-endian) array
  314. * representing the bignum to accumulate onto.
  315. * \param[in] A The pointer to the (little-endian) array
  316. * representing the bignum to conditionally add
  317. * to \p X. This may be aliased to \p X but may not
  318. * overlap otherwise.
  319. * \param limbs Number of limbs of \p X and \p A.
  320. * \param cond Condition bit dictating whether addition should
  321. * happen or not. This must be \c 0 or \c 1.
  322. *
  323. * \warning If \p cond is neither 0 nor 1, the result of this function
  324. * is unspecified, and the resulting value in \p X might be
  325. * neither its original value nor \p X + \p A.
  326. *
  327. * \return 1 if `X + cond * A >= 2^(biL*limbs)`, 0 otherwise.
  328. */
  329. mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X,
  330. const mbedtls_mpi_uint *A,
  331. size_t limbs,
  332. unsigned cond);
  333. /**
  334. * \brief Subtract two fixed-size large unsigned integers, returning the borrow.
  335. *
  336. * Calculate `A - B` where \p A and \p B have the same size.
  337. * This function operates modulo `2^(biL*limbs)` and returns the carry
  338. * (1 if there was a wraparound, i.e. if `A < B`, and 0 otherwise).
  339. *
  340. * \p X may be aliased to \p A or \p B, or even both, but may not overlap
  341. * either otherwise.
  342. *
  343. * \param[out] X The result of the subtraction.
  344. * \param[in] A Little-endian presentation of left operand.
  345. * \param[in] B Little-endian presentation of right operand.
  346. * \param limbs Number of limbs of \p X, \p A and \p B.
  347. *
  348. * \return 1 if `A < B`.
  349. * 0 if `A >= B`.
  350. */
  351. mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X,
  352. const mbedtls_mpi_uint *A,
  353. const mbedtls_mpi_uint *B,
  354. size_t limbs);
  355. /**
  356. * \brief Perform a fixed-size multiply accumulate operation: X += b * A
  357. *
  358. * \p X may be aliased to \p A (when \p X_limbs == \p A_limbs), but may not
  359. * otherwise overlap.
  360. *
  361. * This function operates modulo `2^(biL*X_limbs)`.
  362. *
  363. * \param[in,out] X The pointer to the (little-endian) array
  364. * representing the bignum to accumulate onto.
  365. * \param X_limbs The number of limbs of \p X. This must be
  366. * at least \p A_limbs.
  367. * \param[in] A The pointer to the (little-endian) array
  368. * representing the bignum to multiply with.
  369. * This may be aliased to \p X but may not overlap
  370. * otherwise.
  371. * \param A_limbs The number of limbs of \p A.
  372. * \param b X scalar to multiply with.
  373. *
  374. * \return The carry at the end of the operation.
  375. */
  376. mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *X, size_t X_limbs,
  377. const mbedtls_mpi_uint *A, size_t A_limbs,
  378. mbedtls_mpi_uint b);
  379. /**
  380. * \brief Calculate initialisation value for fast Montgomery modular
  381. * multiplication
  382. *
  383. * \param[in] N Little-endian presentation of the modulus. This must have
  384. * at least one limb.
  385. *
  386. * \return The initialisation value for fast Montgomery modular multiplication
  387. */
  388. mbedtls_mpi_uint mbedtls_mpi_core_montmul_init(const mbedtls_mpi_uint *N);
  389. /**
  390. * \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
  391. *
  392. * \p A and \p B must be in canonical form. That is, < \p N.
  393. *
  394. * \p X may be aliased to \p A or \p N, or even \p B (if \p AN_limbs ==
  395. * \p B_limbs) but may not overlap any parameters otherwise.
  396. *
  397. * \p A and \p B may alias each other, if \p AN_limbs == \p B_limbs. They may
  398. * not alias \p N (since they must be in canonical form, they cannot == \p N).
  399. *
  400. * \param[out] X The destination MPI, as a little-endian array of
  401. * length \p AN_limbs.
  402. * On successful completion, X contains the result of
  403. * the multiplication `A * B * R^-1` mod N where
  404. * `R = 2^(biL*AN_limbs)`.
  405. * \param[in] A Little-endian presentation of first operand.
  406. * Must have the same number of limbs as \p N.
  407. * \param[in] B Little-endian presentation of second operand.
  408. * \param[in] B_limbs The number of limbs in \p B.
  409. * Must be <= \p AN_limbs.
  410. * \param[in] N Little-endian presentation of the modulus.
  411. * This must be odd, and have exactly the same number
  412. * of limbs as \p A.
  413. * It may alias \p X, but must not alias or otherwise
  414. * overlap any of the other parameters.
  415. * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
  416. * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
  417. * This can be calculated by `mbedtls_mpi_core_montmul_init()`.
  418. * \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs.
  419. * Its initial content is unused and
  420. * its final content is indeterminate.
  421. * It must not alias or otherwise overlap any of the
  422. * other parameters.
  423. */
  424. void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,
  425. const mbedtls_mpi_uint *A,
  426. const mbedtls_mpi_uint *B, size_t B_limbs,
  427. const mbedtls_mpi_uint *N, size_t AN_limbs,
  428. mbedtls_mpi_uint mm, mbedtls_mpi_uint *T);
  429. /**
  430. * \brief Calculate the square of the Montgomery constant. (Needed
  431. * for conversion and operations in Montgomery form.)
  432. *
  433. * \param[out] X A pointer to the result of the calculation of
  434. * the square of the Montgomery constant:
  435. * 2^{2*n*biL} mod N.
  436. * \param[in] N Little-endian presentation of the modulus, which must be odd.
  437. *
  438. * \return 0 if successful.
  439. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if there is not enough space
  440. * to store the value of Montgomery constant squared.
  441. * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p N modulus is zero.
  442. * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p N modulus is negative.
  443. */
  444. int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X,
  445. const mbedtls_mpi *N);
  446. #if defined(MBEDTLS_TEST_HOOKS)
  447. /**
  448. * Copy an MPI from a table without leaking the index.
  449. *
  450. * \param dest The destination buffer. This must point to a writable
  451. * buffer of at least \p limbs limbs.
  452. * \param table The address of the table. This must point to a readable
  453. * array of \p count elements of \p limbs limbs each.
  454. * \param limbs The number of limbs in each table entry.
  455. * \param count The number of entries in \p table.
  456. * \param index The (secret) table index to look up. This must be in the
  457. * range `0 .. count-1`.
  458. */
  459. void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest,
  460. const mbedtls_mpi_uint *table,
  461. size_t limbs,
  462. size_t count,
  463. size_t index);
  464. #endif /* MBEDTLS_TEST_HOOKS */
  465. /**
  466. * \brief Fill an integer with a number of random bytes.
  467. *
  468. * \param X The destination MPI.
  469. * \param X_limbs The number of limbs of \p X.
  470. * \param bytes The number of random bytes to generate.
  471. * \param f_rng The RNG function to use. This must not be \c NULL.
  472. * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
  473. * \c NULL if \p f_rng doesn't need a context argument.
  474. *
  475. * \return \c 0 if successful.
  476. * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p X does not have
  477. * enough room for \p bytes bytes.
  478. * \return A negative error code on RNG failure.
  479. *
  480. * \note The bytes obtained from the RNG are interpreted
  481. * as a big-endian representation of an MPI; this can
  482. * be relevant in applications like deterministic ECDSA.
  483. */
  484. int mbedtls_mpi_core_fill_random(mbedtls_mpi_uint *X, size_t X_limbs,
  485. size_t bytes,
  486. int (*f_rng)(void *, unsigned char *, size_t),
  487. void *p_rng);
  488. /** Generate a random number uniformly in a range.
  489. *
  490. * This function generates a random number between \p min inclusive and
  491. * \p N exclusive.
  492. *
  493. * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
  494. * when the RNG is a suitably parametrized instance of HMAC_DRBG
  495. * and \p min is \c 1.
  496. *
  497. * \note There are `N - min` possible outputs. The lower bound
  498. * \p min can be reached, but the upper bound \p N cannot.
  499. *
  500. * \param X The destination MPI, with \p limbs limbs.
  501. * It must not be aliased with \p N or otherwise overlap it.
  502. * \param min The minimum value to return.
  503. * \param N The upper bound of the range, exclusive, with \p limbs limbs.
  504. * In other words, this is one plus the maximum value to return.
  505. * \p N must be strictly larger than \p min.
  506. * \param limbs The number of limbs of \p N and \p X.
  507. * This must not be 0.
  508. * \param f_rng The RNG function to use. This must not be \c NULL.
  509. * \param p_rng The RNG parameter to be passed to \p f_rng.
  510. *
  511. * \return \c 0 if successful.
  512. * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
  513. * unable to find a suitable value within a limited number
  514. * of attempts. This has a negligible probability if \p N
  515. * is significantly larger than \p min, which is the case
  516. * for all usual cryptographic applications.
  517. */
  518. int mbedtls_mpi_core_random(mbedtls_mpi_uint *X,
  519. mbedtls_mpi_uint min,
  520. const mbedtls_mpi_uint *N,
  521. size_t limbs,
  522. int (*f_rng)(void *, unsigned char *, size_t),
  523. void *p_rng);
  524. /* BEGIN MERGE SLOT 1 */
  525. /**
  526. * \brief Returns the number of limbs of working memory required for
  527. * a call to `mbedtls_mpi_core_exp_mod()`.
  528. *
  529. * \note This will always be at least
  530. * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`,
  531. * i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`.
  532. *
  533. * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
  534. * (they must be the same size) that will be given to
  535. * `mbedtls_mpi_core_exp_mod()`.
  536. * \param E_limbs The number of limbs in the exponent `E` that will be given
  537. * to `mbedtls_mpi_core_exp_mod()`.
  538. *
  539. * \return The number of limbs of working memory required by
  540. * `mbedtls_mpi_core_exp_mod()`.
  541. */
  542. size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs);
  543. /**
  544. * \brief Perform a modular exponentiation with secret exponent:
  545. * X = A^E mod N, where \p A is already in Montgomery form.
  546. *
  547. * \p X may be aliased to \p A, but not to \p RR or \p E, even if \p E_limbs ==
  548. * \p AN_limbs.
  549. *
  550. * \param[out] X The destination MPI, as a little endian array of length
  551. * \p AN_limbs.
  552. * \param[in] A The base MPI, as a little endian array of length \p AN_limbs.
  553. * Must be in Montgomery form.
  554. * \param[in] N The modulus, as a little endian array of length \p AN_limbs.
  555. * \param AN_limbs The number of limbs in \p X, \p A, \p N, \p RR.
  556. * \param[in] E The exponent, as a little endian array of length \p E_limbs.
  557. * \param E_limbs The number of limbs in \p E.
  558. * \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little
  559. * endian array of length \p AN_limbs.
  560. * \param[in,out] T Temporary storage of at least the number of limbs returned
  561. * by `mbedtls_mpi_core_exp_mod_working_limbs()`.
  562. * Its initial content is unused and its final content is
  563. * indeterminate.
  564. * It must not alias or otherwise overlap any of the other
  565. * parameters.
  566. * It is up to the caller to zeroize \p T when it is no
  567. * longer needed, and before freeing it if it was dynamically
  568. * allocated.
  569. */
  570. void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,
  571. const mbedtls_mpi_uint *A,
  572. const mbedtls_mpi_uint *N, size_t AN_limbs,
  573. const mbedtls_mpi_uint *E, size_t E_limbs,
  574. const mbedtls_mpi_uint *RR,
  575. mbedtls_mpi_uint *T);
  576. /* END MERGE SLOT 1 */
  577. /* BEGIN MERGE SLOT 2 */
  578. /* END MERGE SLOT 2 */
  579. /* BEGIN MERGE SLOT 3 */
  580. /**
  581. * \brief Subtract unsigned integer from known-size large unsigned integers.
  582. * Return the borrow.
  583. *
  584. * \param[out] X The result of the subtraction.
  585. * \param[in] A The left operand.
  586. * \param b The unsigned scalar to subtract.
  587. * \param limbs Number of limbs of \p X and \p A.
  588. *
  589. * \return 1 if `A < b`.
  590. * 0 if `A >= b`.
  591. */
  592. mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,
  593. const mbedtls_mpi_uint *A,
  594. mbedtls_mpi_uint b,
  595. size_t limbs);
  596. /**
  597. * \brief Determine if a given MPI has the value \c 0 in constant time with
  598. * respect to the value (but not with respect to the number of limbs).
  599. *
  600. * \param[in] A The MPI to test.
  601. * \param limbs Number of limbs in \p A.
  602. *
  603. * \return 0 if `A == 0`
  604. * non-0 (may be any value) if `A != 0`.
  605. */
  606. mbedtls_mpi_uint mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,
  607. size_t limbs);
  608. /**
  609. * \brief Returns the number of limbs of working memory required for
  610. * a call to `mbedtls_mpi_core_montmul()`.
  611. *
  612. * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
  613. * (they must be the same size) that will be given to
  614. * `mbedtls_mpi_core_montmul()` or one of the other functions
  615. * that specifies this as the amount of working memory needed.
  616. *
  617. * \return The number of limbs of working memory required by
  618. * `mbedtls_mpi_core_montmul()` (or other similar function).
  619. */
  620. static inline size_t mbedtls_mpi_core_montmul_working_limbs(size_t AN_limbs)
  621. {
  622. return 2 * AN_limbs + 1;
  623. }
  624. /** Convert an MPI into Montgomery form.
  625. *
  626. * \p X may be aliased to \p A, but may not otherwise overlap it.
  627. *
  628. * \p X may not alias \p N (it is in canonical form, so must be strictly less
  629. * than \p N). Nor may it alias or overlap \p rr (this is unlikely to be
  630. * required in practice.)
  631. *
  632. * This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is
  633. * an alternative to calling `mbedtls_mpi_mod_raw_to_mont_rep()` when we
  634. * don't want to allocate memory.
  635. *
  636. * \param[out] X The result of the conversion.
  637. * Must have the same number of limbs as \p A.
  638. * \param[in] A The MPI to convert into Montgomery form.
  639. * Must have the same number of limbs as the modulus.
  640. * \param[in] N The address of the modulus, which gives the size of
  641. * the base `R` = 2^(biL*N->limbs).
  642. * \param[in] AN_limbs The number of limbs in \p X, \p A, \p N and \p rr.
  643. * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
  644. * This can be determined by calling
  645. * `mbedtls_mpi_core_montmul_init()`.
  646. * \param[in] rr The residue for `2^{2*n*biL} mod N`.
  647. * \param[in,out] T Temporary storage of size at least
  648. * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`
  649. * limbs.
  650. * Its initial content is unused and
  651. * its final content is indeterminate.
  652. * It must not alias or otherwise overlap any of the
  653. * other parameters.
  654. */
  655. void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
  656. const mbedtls_mpi_uint *A,
  657. const mbedtls_mpi_uint *N,
  658. size_t AN_limbs,
  659. mbedtls_mpi_uint mm,
  660. const mbedtls_mpi_uint *rr,
  661. mbedtls_mpi_uint *T);
  662. /** Convert an MPI from Montgomery form.
  663. *
  664. * \p X may be aliased to \p A, but may not otherwise overlap it.
  665. *
  666. * \p X may not alias \p N (it is in canonical form, so must be strictly less
  667. * than \p N).
  668. *
  669. * This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is
  670. * an alternative to calling `mbedtls_mpi_mod_raw_from_mont_rep()` when we
  671. * don't want to allocate memory.
  672. *
  673. * \param[out] X The result of the conversion.
  674. * Must have the same number of limbs as \p A.
  675. * \param[in] A The MPI to convert from Montgomery form.
  676. * Must have the same number of limbs as the modulus.
  677. * \param[in] N The address of the modulus, which gives the size of
  678. * the base `R` = 2^(biL*N->limbs).
  679. * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
  680. * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
  681. * This can be determined by calling
  682. * `mbedtls_mpi_core_montmul_init()`.
  683. * \param[in,out] T Temporary storage of size at least
  684. * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`
  685. * limbs.
  686. * Its initial content is unused and
  687. * its final content is indeterminate.
  688. * It must not alias or otherwise overlap any of the
  689. * other parameters.
  690. */
  691. void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,
  692. const mbedtls_mpi_uint *A,
  693. const mbedtls_mpi_uint *N,
  694. size_t AN_limbs,
  695. mbedtls_mpi_uint mm,
  696. mbedtls_mpi_uint *T);
  697. /* END MERGE SLOT 3 */
  698. /* BEGIN MERGE SLOT 4 */
  699. /* END MERGE SLOT 4 */
  700. /* BEGIN MERGE SLOT 5 */
  701. /* END MERGE SLOT 5 */
  702. /* BEGIN MERGE SLOT 6 */
  703. /* END MERGE SLOT 6 */
  704. /* BEGIN MERGE SLOT 7 */
  705. /* END MERGE SLOT 7 */
  706. /* BEGIN MERGE SLOT 8 */
  707. /* END MERGE SLOT 8 */
  708. /* BEGIN MERGE SLOT 9 */
  709. /* END MERGE SLOT 9 */
  710. /* BEGIN MERGE SLOT 10 */
  711. /* END MERGE SLOT 10 */
  712. #endif /* MBEDTLS_BIGNUM_CORE_H */