bignum_mod_raw.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /**
  2. * Low-level modular bignum functions
  3. *
  4. * This interface should only be used by the higher-level modular bignum
  5. * module (bignum_mod.c) and the ECP module (ecp.c, ecp_curves.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 is a low-level interface to operations on integers modulo which
  10. * has no protection against passing invalid arguments such as arrays of
  11. * the wrong size. The functions in bignum_mod.h provide a higher-level
  12. * interface that includes protections against accidental misuse, at the
  13. * expense of code size and sometimes more cumbersome memory management.
  14. *
  15. * The functions in this module obey the following conventions unless
  16. * explicitly indicated otherwise:
  17. * - **Modulus parameters**: the modulus is passed as a pointer to a structure
  18. * of type #mbedtls_mpi_mod_modulus. The structure must be set up with an
  19. * array of limbs storing the bignum value of the modulus. The modulus must
  20. * be odd and is assumed to have no leading zeroes. The modulus is usually
  21. * named \c N and is usually input-only.
  22. * - **Bignum parameters**: Bignums are passed as pointers to an array of
  23. * limbs. A limb has the type #mbedtls_mpi_uint. Unless otherwise specified:
  24. * - Bignum parameters called \c A, \c B, ... are inputs, and are not
  25. * modified by the function.
  26. * - Bignum parameters called \c X, \c Y are outputs or input-output.
  27. * The initial content of output-only parameters is ignored.
  28. * - \c T is a temporary storage area. The initial content of such a
  29. * parameter is ignored and the final content is unspecified.
  30. * - **Bignum sizes**: bignum sizes are usually expressed by the \c limbs
  31. * member of the modulus argument. All bignum parameters must have the same
  32. * number of limbs as the modulus. All bignum sizes must be at least 1 and
  33. * must be significantly less than #SIZE_MAX. The behavior if a size is 0 is
  34. * undefined.
  35. * - **Bignum representation**: the representation of inputs and outputs is
  36. * specified by the \c int_rep field of the modulus for arithmetic
  37. * functions. Utility functions may allow for different representation.
  38. * - **Parameter ordering**: for bignum parameters, outputs come before inputs.
  39. * The modulus is passed after other bignum input parameters. Temporaries
  40. * come last.
  41. * - **Aliasing**: in general, output bignums may be aliased to one or more
  42. * inputs. Modulus values may not be aliased to any other parameter. Outputs
  43. * may not be aliased to one another. Temporaries may not be aliased to any
  44. * other parameter.
  45. * - **Overlap**: apart from aliasing of limb array pointers (where two
  46. * arguments are equal pointers), overlap is not supported and may result
  47. * in undefined behavior.
  48. * - **Error handling**: This is a low-level module. Functions generally do not
  49. * try to protect against invalid arguments such as nonsensical sizes or
  50. * null pointers. Note that passing bignums with a different size than the
  51. * modulus may lead to buffer overflows. Some functions which allocate
  52. * memory or handle reading/writing of bignums will return an error if
  53. * memory allocation fails or if buffer sizes are invalid.
  54. * - **Modular representatives**: all functions expect inputs to be in the
  55. * range [0, \c N - 1] and guarantee outputs in the range [0, \c N - 1]. If
  56. * an input is out of range, outputs are fully unspecified, though bignum
  57. * values out of range should not cause buffer overflows (beware that this is
  58. * not extensively tested).
  59. */
  60. /*
  61. * Copyright The Mbed TLS Contributors
  62. * SPDX-License-Identifier: Apache-2.0
  63. *
  64. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  65. * not use this file except in compliance with the License.
  66. * You may obtain a copy of the License at
  67. *
  68. * http://www.apache.org/licenses/LICENSE-2.0
  69. *
  70. * Unless required by applicable law or agreed to in writing, software
  71. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  72. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  73. * See the License for the specific language governing permissions and
  74. * limitations under the License.
  75. */
  76. #ifndef MBEDTLS_BIGNUM_MOD_RAW_H
  77. #define MBEDTLS_BIGNUM_MOD_RAW_H
  78. #include "common.h"
  79. #if defined(MBEDTLS_BIGNUM_C)
  80. #include "mbedtls/bignum.h"
  81. #endif
  82. #include "bignum_mod.h"
  83. /**
  84. * \brief Perform a safe conditional copy of an MPI which doesn't reveal
  85. * whether the assignment was done or not.
  86. *
  87. * The size to copy is determined by \p N.
  88. *
  89. * \param[out] X The address of the destination MPI.
  90. * This must be initialized. Must have enough limbs to
  91. * store the full value of \p A.
  92. * \param[in] A The address of the source MPI. This must be initialized.
  93. * \param[in] N The address of the modulus related to \p X and \p A.
  94. * \param assign The condition deciding whether to perform the
  95. * assignment or not. Must be either 0 or 1:
  96. * * \c 1: Perform the assignment `X = A`.
  97. * * \c 0: Keep the original value of \p X.
  98. *
  99. * \note This function avoids leaking any information about whether
  100. * the assignment was done or not.
  101. *
  102. * \warning If \p assign is neither 0 nor 1, the result of this function
  103. * is indeterminate, and the resulting value in \p X might be
  104. * neither its original value nor the value in \p A.
  105. */
  106. void mbedtls_mpi_mod_raw_cond_assign(mbedtls_mpi_uint *X,
  107. const mbedtls_mpi_uint *A,
  108. const mbedtls_mpi_mod_modulus *N,
  109. unsigned char assign);
  110. /**
  111. * \brief Perform a safe conditional swap of two MPIs which doesn't reveal
  112. * whether the swap was done or not.
  113. *
  114. * The size to swap is determined by \p N.
  115. *
  116. * \param[in,out] X The address of the first MPI. This must be initialized.
  117. * \param[in,out] Y The address of the second MPI. This must be initialized.
  118. * \param[in] N The address of the modulus related to \p X and \p Y.
  119. * \param swap The condition deciding whether to perform
  120. * the swap or not. Must be either 0 or 1:
  121. * * \c 1: Swap the values of \p X and \p Y.
  122. * * \c 0: Keep the original values of \p X and \p Y.
  123. *
  124. * \note This function avoids leaking any information about whether
  125. * the swap was done or not.
  126. *
  127. * \warning If \p swap is neither 0 nor 1, the result of this function
  128. * is indeterminate, and both \p X and \p Y might end up with
  129. * values different to either of the original ones.
  130. */
  131. void mbedtls_mpi_mod_raw_cond_swap(mbedtls_mpi_uint *X,
  132. mbedtls_mpi_uint *Y,
  133. const mbedtls_mpi_mod_modulus *N,
  134. unsigned char swap);
  135. /** Import X from unsigned binary data.
  136. *
  137. * The MPI needs to have enough limbs to store the full value (including any
  138. * most significant zero bytes in the input).
  139. *
  140. * \param[out] X The address of the MPI. The size is determined by \p N.
  141. * (In particular, it must have at least as many limbs as
  142. * the modulus \p N.)
  143. * \param[in] N The address of the modulus related to \p X.
  144. * \param[in] input The input buffer to import from.
  145. * \param input_length The length in bytes of \p input.
  146. * \param ext_rep The endianness of the number in the input buffer.
  147. *
  148. * \return \c 0 if successful.
  149. * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
  150. * large enough to hold the value in \p input.
  151. * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
  152. * of \p N is invalid or \p X is not less than \p N.
  153. */
  154. int mbedtls_mpi_mod_raw_read(mbedtls_mpi_uint *X,
  155. const mbedtls_mpi_mod_modulus *N,
  156. const unsigned char *input,
  157. size_t input_length,
  158. mbedtls_mpi_mod_ext_rep ext_rep);
  159. /** Export A into unsigned binary data.
  160. *
  161. * \param[in] A The address of the MPI. The size is determined by \p N.
  162. * (In particular, it must have at least as many limbs as
  163. * the modulus \p N.)
  164. * \param[in] N The address of the modulus related to \p A.
  165. * \param[out] output The output buffer to export to.
  166. * \param output_length The length in bytes of \p output.
  167. * \param ext_rep The endianness in which the number should be written into the output buffer.
  168. *
  169. * \return \c 0 if successful.
  170. * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
  171. * large enough to hold the value of \p A.
  172. * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
  173. * of \p N is invalid.
  174. */
  175. int mbedtls_mpi_mod_raw_write(const mbedtls_mpi_uint *A,
  176. const mbedtls_mpi_mod_modulus *N,
  177. unsigned char *output,
  178. size_t output_length,
  179. mbedtls_mpi_mod_ext_rep ext_rep);
  180. /* BEGIN MERGE SLOT 1 */
  181. /* END MERGE SLOT 1 */
  182. /* BEGIN MERGE SLOT 2 */
  183. /** \brief Subtract two MPIs, returning the residue modulo the specified
  184. * modulus.
  185. *
  186. * The size of the operation is determined by \p N. \p A and \p B must have
  187. * the same number of limbs as \p N.
  188. *
  189. * \p X may be aliased to \p A or \p B, or even both, but may not overlap
  190. * either otherwise.
  191. *
  192. * \param[out] X The address of the result MPI.
  193. * This must be initialized. Must have enough limbs to
  194. * store the full value of the result.
  195. * \param[in] A The address of the first MPI. This must be initialized.
  196. * \param[in] B The address of the second MPI. This must be initialized.
  197. * \param[in] N The address of the modulus. Used to perform a modulo
  198. * operation on the result of the subtraction.
  199. */
  200. void mbedtls_mpi_mod_raw_sub(mbedtls_mpi_uint *X,
  201. const mbedtls_mpi_uint *A,
  202. const mbedtls_mpi_uint *B,
  203. const mbedtls_mpi_mod_modulus *N);
  204. /** \brief Multiply two MPIs, returning the residue modulo the specified
  205. * modulus.
  206. *
  207. * \note Currently handles the case when `N->int_rep` is
  208. * MBEDTLS_MPI_MOD_REP_MONTGOMERY.
  209. *
  210. * The size of the operation is determined by \p N. \p A, \p B and \p X must
  211. * all be associated with the modulus \p N and must all have the same number
  212. * of limbs as \p N.
  213. *
  214. * \p X may be aliased to \p A or \p B, or even both, but may not overlap
  215. * either otherwise. They may not alias \p N (since they must be in canonical
  216. * form, they cannot == \p N).
  217. *
  218. * \param[out] X The address of the result MPI. Must have the same
  219. * number of limbs as \p N.
  220. * On successful completion, \p X contains the result of
  221. * the multiplication `A * B * R^-1` mod N where
  222. * `R = 2^(biL * N->limbs)`.
  223. * \param[in] A The address of the first MPI.
  224. * \param[in] B The address of the second MPI.
  225. * \param[in] N The address of the modulus. Used to perform a modulo
  226. * operation on the result of the multiplication.
  227. * \param[in,out] T Temporary storage of size at least 2 * N->limbs + 1
  228. * limbs. Its initial content is unused and
  229. * its final content is indeterminate.
  230. * It must not alias or otherwise overlap any of the
  231. * other parameters.
  232. */
  233. void mbedtls_mpi_mod_raw_mul(mbedtls_mpi_uint *X,
  234. const mbedtls_mpi_uint *A,
  235. const mbedtls_mpi_uint *B,
  236. const mbedtls_mpi_mod_modulus *N,
  237. mbedtls_mpi_uint *T);
  238. /* END MERGE SLOT 2 */
  239. /* BEGIN MERGE SLOT 3 */
  240. /**
  241. * \brief Returns the number of limbs of working memory required for
  242. * a call to `mbedtls_mpi_mod_raw_inv_prime()`.
  243. *
  244. * \note This will always be at least
  245. * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`,
  246. * i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`.
  247. *
  248. * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
  249. * (they must be the same size) that will be given to
  250. * `mbedtls_mpi_mod_raw_inv_prime()`.
  251. *
  252. * \return The number of limbs of working memory required by
  253. * `mbedtls_mpi_mod_raw_inv_prime()`.
  254. */
  255. size_t mbedtls_mpi_mod_raw_inv_prime_working_limbs(size_t AN_limbs);
  256. /**
  257. * \brief Perform fixed-width modular inversion of a Montgomery-form MPI with
  258. * respect to a modulus \p N that must be prime.
  259. *
  260. * \p X may be aliased to \p A, but not to \p N or \p RR.
  261. *
  262. * \param[out] X The modular inverse of \p A with respect to \p N.
  263. * Will be in Montgomery form.
  264. * \param[in] A The number to calculate the modular inverse of.
  265. * Must be in Montgomery form. Must not be 0.
  266. * \param[in] N The modulus, as a little-endian array of length \p AN_limbs.
  267. * Must be prime.
  268. * \param AN_limbs The number of limbs in \p A, \p N and \p RR.
  269. * \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little-
  270. * endian array of length \p AN_limbs.
  271. * \param[in,out] T Temporary storage of at least the number of limbs returned
  272. * by `mbedtls_mpi_mod_raw_inv_prime_working_limbs()`.
  273. * Its initial content is unused and its final content is
  274. * indeterminate.
  275. * It must not alias or otherwise overlap any of the other
  276. * parameters.
  277. * It is up to the caller to zeroize \p T when it is no
  278. * longer needed, and before freeing it if it was dynamically
  279. * allocated.
  280. */
  281. void mbedtls_mpi_mod_raw_inv_prime(mbedtls_mpi_uint *X,
  282. const mbedtls_mpi_uint *A,
  283. const mbedtls_mpi_uint *N,
  284. size_t AN_limbs,
  285. const mbedtls_mpi_uint *RR,
  286. mbedtls_mpi_uint *T);
  287. /* END MERGE SLOT 3 */
  288. /* BEGIN MERGE SLOT 4 */
  289. /* END MERGE SLOT 4 */
  290. /* BEGIN MERGE SLOT 5 */
  291. /**
  292. * \brief Perform a known-size modular addition.
  293. *
  294. * Calculate `A + B modulo N`.
  295. *
  296. * The number of limbs in each operand, and the result, is given by the
  297. * modulus \p N.
  298. *
  299. * \p X may be aliased to \p A or \p B, or even both, but may not overlap
  300. * either otherwise.
  301. *
  302. * \param[out] X The result of the modular addition.
  303. * \param[in] A Little-endian presentation of the left operand. This
  304. * must be smaller than \p N.
  305. * \param[in] B Little-endian presentation of the right operand. This
  306. * must be smaller than \p N.
  307. * \param[in] N The address of the modulus.
  308. */
  309. void mbedtls_mpi_mod_raw_add(mbedtls_mpi_uint *X,
  310. const mbedtls_mpi_uint *A,
  311. const mbedtls_mpi_uint *B,
  312. const mbedtls_mpi_mod_modulus *N);
  313. /* END MERGE SLOT 5 */
  314. /* BEGIN MERGE SLOT 6 */
  315. /** Convert an MPI from canonical representation (little-endian limb array)
  316. * to the representation associated with the modulus.
  317. *
  318. * \param[in,out] X The limb array to convert.
  319. * It must have as many limbs as \p N.
  320. * It is converted in place.
  321. * If this function returns an error, the content of \p X
  322. * is unspecified.
  323. * \param[in] N The modulus structure.
  324. *
  325. * \return \c 0 if successful.
  326. * Otherwise an \c MBEDTLS_ERR_MPI_xxx error code.
  327. */
  328. int mbedtls_mpi_mod_raw_canonical_to_modulus_rep(
  329. mbedtls_mpi_uint *X,
  330. const mbedtls_mpi_mod_modulus *N);
  331. /** Convert an MPI from the representation associated with the modulus
  332. * to canonical representation (little-endian limb array).
  333. *
  334. * \param[in,out] X The limb array to convert.
  335. * It must have as many limbs as \p N.
  336. * It is converted in place.
  337. * If this function returns an error, the content of \p X
  338. * is unspecified.
  339. * \param[in] N The modulus structure.
  340. *
  341. * \return \c 0 if successful.
  342. * Otherwise an \c MBEDTLS_ERR_MPI_xxx error code.
  343. */
  344. int mbedtls_mpi_mod_raw_modulus_to_canonical_rep(
  345. mbedtls_mpi_uint *X,
  346. const mbedtls_mpi_mod_modulus *N);
  347. /** Generate a random number uniformly in a range.
  348. *
  349. * This function generates a random number between \p min inclusive and
  350. * \p N exclusive.
  351. *
  352. * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
  353. * when the RNG is a suitably parametrized instance of HMAC_DRBG
  354. * and \p min is \c 1.
  355. *
  356. * \note There are `N - min` possible outputs. The lower bound
  357. * \p min can be reached, but the upper bound \p N cannot.
  358. *
  359. * \param X The destination MPI, in canonical representation modulo \p N.
  360. * It must not be aliased with \p N or otherwise overlap it.
  361. * \param min The minimum value to return. It must be strictly smaller
  362. * than \b N.
  363. * \param N The modulus.
  364. * This is the upper bound of the output range, exclusive.
  365. * \param f_rng The RNG function to use. This must not be \c NULL.
  366. * \param p_rng The RNG parameter to be passed to \p f_rng.
  367. *
  368. * \return \c 0 if successful.
  369. * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
  370. * unable to find a suitable value within a limited number
  371. * of attempts. This has a negligible probability if \p N
  372. * is significantly larger than \p min, which is the case
  373. * for all usual cryptographic applications.
  374. */
  375. int mbedtls_mpi_mod_raw_random(mbedtls_mpi_uint *X,
  376. mbedtls_mpi_uint min,
  377. const mbedtls_mpi_mod_modulus *N,
  378. int (*f_rng)(void *, unsigned char *, size_t),
  379. void *p_rng);
  380. /* END MERGE SLOT 6 */
  381. /* BEGIN MERGE SLOT 7 */
  382. /** Convert an MPI into Montgomery form.
  383. *
  384. * \param X The address of the MPI.
  385. * Must have the same number of limbs as \p N.
  386. * \param N The address of the modulus, which gives the size of
  387. * the base `R` = 2^(biL*N->limbs).
  388. *
  389. * \return \c 0 if successful.
  390. */
  391. int mbedtls_mpi_mod_raw_to_mont_rep(mbedtls_mpi_uint *X,
  392. const mbedtls_mpi_mod_modulus *N);
  393. /** Convert an MPI back from Montgomery representation.
  394. *
  395. * \param X The address of the MPI.
  396. * Must have the same number of limbs as \p N.
  397. * \param N The address of the modulus, which gives the size of
  398. * the base `R`= 2^(biL*N->limbs).
  399. *
  400. * \return \c 0 if successful.
  401. */
  402. int mbedtls_mpi_mod_raw_from_mont_rep(mbedtls_mpi_uint *X,
  403. const mbedtls_mpi_mod_modulus *N);
  404. /** \brief Perform fixed width modular negation.
  405. *
  406. * The size of the operation is determined by \p N. \p A must have
  407. * the same number of limbs as \p N.
  408. *
  409. * \p X may be aliased to \p A.
  410. *
  411. * \param[out] X The result of the modular negation.
  412. * This must be initialized.
  413. * \param[in] A Little-endian presentation of the input operand. This
  414. * must be less than or equal to \p N.
  415. * \param[in] N The modulus to use.
  416. */
  417. void mbedtls_mpi_mod_raw_neg(mbedtls_mpi_uint *X,
  418. const mbedtls_mpi_uint *A,
  419. const mbedtls_mpi_mod_modulus *N);
  420. /* END MERGE SLOT 7 */
  421. /* BEGIN MERGE SLOT 8 */
  422. /* END MERGE SLOT 8 */
  423. /* BEGIN MERGE SLOT 9 */
  424. /* END MERGE SLOT 9 */
  425. /* BEGIN MERGE SLOT 10 */
  426. /* END MERGE SLOT 10 */
  427. #endif /* MBEDTLS_BIGNUM_MOD_RAW_H */