bignum_mod_raw.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 OR GPL-2.0-or-later
  63. */
  64. #ifndef MBEDTLS_BIGNUM_MOD_RAW_H
  65. #define MBEDTLS_BIGNUM_MOD_RAW_H
  66. #include "common.h"
  67. #if defined(MBEDTLS_BIGNUM_C)
  68. #include "mbedtls/bignum.h"
  69. #endif
  70. #include "bignum_mod.h"
  71. /**
  72. * \brief Perform a safe conditional copy of an MPI which doesn't reveal
  73. * whether the assignment was done or not.
  74. *
  75. * The size to copy is determined by \p N.
  76. *
  77. * \param[out] X The address of the destination MPI.
  78. * This must be initialized. Must have enough limbs to
  79. * store the full value of \p A.
  80. * \param[in] A The address of the source MPI. This must be initialized.
  81. * \param[in] N The address of the modulus related to \p X and \p A.
  82. * \param assign The condition deciding whether to perform the
  83. * assignment or not. Must be either 0 or 1:
  84. * * \c 1: Perform the assignment `X = A`.
  85. * * \c 0: Keep the original value of \p X.
  86. *
  87. * \note This function avoids leaking any information about whether
  88. * the assignment was done or not.
  89. *
  90. * \warning If \p assign is neither 0 nor 1, the result of this function
  91. * is indeterminate, and the resulting value in \p X might be
  92. * neither its original value nor the value in \p A.
  93. */
  94. void mbedtls_mpi_mod_raw_cond_assign(mbedtls_mpi_uint *X,
  95. const mbedtls_mpi_uint *A,
  96. const mbedtls_mpi_mod_modulus *N,
  97. unsigned char assign);
  98. /**
  99. * \brief Perform a safe conditional swap of two MPIs which doesn't reveal
  100. * whether the swap was done or not.
  101. *
  102. * The size to swap is determined by \p N.
  103. *
  104. * \param[in,out] X The address of the first MPI. This must be initialized.
  105. * \param[in,out] Y The address of the second MPI. This must be initialized.
  106. * \param[in] N The address of the modulus related to \p X and \p Y.
  107. * \param swap The condition deciding whether to perform
  108. * the swap or not. Must be either 0 or 1:
  109. * * \c 1: Swap the values of \p X and \p Y.
  110. * * \c 0: Keep the original values of \p X and \p Y.
  111. *
  112. * \note This function avoids leaking any information about whether
  113. * the swap was done or not.
  114. *
  115. * \warning If \p swap is neither 0 nor 1, the result of this function
  116. * is indeterminate, and both \p X and \p Y might end up with
  117. * values different to either of the original ones.
  118. */
  119. void mbedtls_mpi_mod_raw_cond_swap(mbedtls_mpi_uint *X,
  120. mbedtls_mpi_uint *Y,
  121. const mbedtls_mpi_mod_modulus *N,
  122. unsigned char swap);
  123. /** Import X from unsigned binary data.
  124. *
  125. * The MPI needs to have enough limbs to store the full value (including any
  126. * most significant zero bytes in the input).
  127. *
  128. * \param[out] X The address of the MPI. The size is determined by \p N.
  129. * (In particular, it must have at least as many limbs as
  130. * the modulus \p N.)
  131. * \param[in] N The address of the modulus related to \p X.
  132. * \param[in] input The input buffer to import from.
  133. * \param input_length The length in bytes of \p input.
  134. * \param ext_rep The endianness of the number in the input buffer.
  135. *
  136. * \return \c 0 if successful.
  137. * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
  138. * large enough to hold the value in \p input.
  139. * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
  140. * of \p N is invalid or \p X is not less than \p N.
  141. */
  142. int mbedtls_mpi_mod_raw_read(mbedtls_mpi_uint *X,
  143. const mbedtls_mpi_mod_modulus *N,
  144. const unsigned char *input,
  145. size_t input_length,
  146. mbedtls_mpi_mod_ext_rep ext_rep);
  147. /** Export A into unsigned binary data.
  148. *
  149. * \param[in] A The address of the MPI. The size is determined by \p N.
  150. * (In particular, it must have at least as many limbs as
  151. * the modulus \p N.)
  152. * \param[in] N The address of the modulus related to \p A.
  153. * \param[out] output The output buffer to export to.
  154. * \param output_length The length in bytes of \p output.
  155. * \param ext_rep The endianness in which the number should be written into the output buffer.
  156. *
  157. * \return \c 0 if successful.
  158. * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
  159. * large enough to hold the value of \p A.
  160. * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
  161. * of \p N is invalid.
  162. */
  163. int mbedtls_mpi_mod_raw_write(const mbedtls_mpi_uint *A,
  164. const mbedtls_mpi_mod_modulus *N,
  165. unsigned char *output,
  166. size_t output_length,
  167. mbedtls_mpi_mod_ext_rep ext_rep);
  168. /** \brief Subtract two MPIs, returning the residue modulo the specified
  169. * modulus.
  170. *
  171. * The size of the operation is determined by \p N. \p A and \p B must have
  172. * the same number of limbs as \p N.
  173. *
  174. * \p X may be aliased to \p A or \p B, or even both, but may not overlap
  175. * either otherwise.
  176. *
  177. * \param[out] X The address of the result MPI.
  178. * This must be initialized. Must have enough limbs to
  179. * store the full value of the result.
  180. * \param[in] A The address of the first MPI. This must be initialized.
  181. * \param[in] B The address of the second MPI. This must be initialized.
  182. * \param[in] N The address of the modulus. Used to perform a modulo
  183. * operation on the result of the subtraction.
  184. */
  185. void mbedtls_mpi_mod_raw_sub(mbedtls_mpi_uint *X,
  186. const mbedtls_mpi_uint *A,
  187. const mbedtls_mpi_uint *B,
  188. const mbedtls_mpi_mod_modulus *N);
  189. /** \brief Multiply two MPIs, returning the residue modulo the specified
  190. * modulus.
  191. *
  192. * \note Currently handles the case when `N->int_rep` is
  193. * MBEDTLS_MPI_MOD_REP_MONTGOMERY.
  194. *
  195. * The size of the operation is determined by \p N. \p A, \p B and \p X must
  196. * all be associated with the modulus \p N and must all have the same number
  197. * of limbs as \p N.
  198. *
  199. * \p X may be aliased to \p A or \p B, or even both, but may not overlap
  200. * either otherwise. They may not alias \p N (since they must be in canonical
  201. * form, they cannot == \p N).
  202. *
  203. * \param[out] X The address of the result MPI. Must have the same
  204. * number of limbs as \p N.
  205. * On successful completion, \p X contains the result of
  206. * the multiplication `A * B * R^-1` mod N where
  207. * `R = 2^(biL * N->limbs)`.
  208. * \param[in] A The address of the first MPI.
  209. * \param[in] B The address of the second MPI.
  210. * \param[in] N The address of the modulus. Used to perform a modulo
  211. * operation on the result of the multiplication.
  212. * \param[in,out] T Temporary storage of size at least 2 * N->limbs + 1
  213. * limbs. Its initial content is unused and
  214. * its final content is indeterminate.
  215. * It must not alias or otherwise overlap any of the
  216. * other parameters.
  217. */
  218. void mbedtls_mpi_mod_raw_mul(mbedtls_mpi_uint *X,
  219. const mbedtls_mpi_uint *A,
  220. const mbedtls_mpi_uint *B,
  221. const mbedtls_mpi_mod_modulus *N,
  222. mbedtls_mpi_uint *T);
  223. /**
  224. * \brief Returns the number of limbs of working memory required for
  225. * a call to `mbedtls_mpi_mod_raw_inv_prime()`.
  226. *
  227. * \note This will always be at least
  228. * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`,
  229. * i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`.
  230. *
  231. * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
  232. * (they must be the same size) that will be given to
  233. * `mbedtls_mpi_mod_raw_inv_prime()`.
  234. *
  235. * \return The number of limbs of working memory required by
  236. * `mbedtls_mpi_mod_raw_inv_prime()`.
  237. */
  238. size_t mbedtls_mpi_mod_raw_inv_prime_working_limbs(size_t AN_limbs);
  239. /**
  240. * \brief Perform fixed-width modular inversion of a Montgomery-form MPI with
  241. * respect to a modulus \p N that must be prime.
  242. *
  243. * \p X may be aliased to \p A, but not to \p N or \p RR.
  244. *
  245. * \param[out] X The modular inverse of \p A with respect to \p N.
  246. * Will be in Montgomery form.
  247. * \param[in] A The number to calculate the modular inverse of.
  248. * Must be in Montgomery form. Must not be 0.
  249. * \param[in] N The modulus, as a little-endian array of length \p AN_limbs.
  250. * Must be prime.
  251. * \param AN_limbs The number of limbs in \p A, \p N and \p RR.
  252. * \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little-
  253. * endian array of length \p AN_limbs.
  254. * \param[in,out] T Temporary storage of at least the number of limbs returned
  255. * by `mbedtls_mpi_mod_raw_inv_prime_working_limbs()`.
  256. * Its initial content is unused and its final content is
  257. * indeterminate.
  258. * It must not alias or otherwise overlap any of the other
  259. * parameters.
  260. * It is up to the caller to zeroize \p T when it is no
  261. * longer needed, and before freeing it if it was dynamically
  262. * allocated.
  263. */
  264. void mbedtls_mpi_mod_raw_inv_prime(mbedtls_mpi_uint *X,
  265. const mbedtls_mpi_uint *A,
  266. const mbedtls_mpi_uint *N,
  267. size_t AN_limbs,
  268. const mbedtls_mpi_uint *RR,
  269. mbedtls_mpi_uint *T);
  270. /**
  271. * \brief Perform a known-size modular addition.
  272. *
  273. * Calculate `A + B modulo N`.
  274. *
  275. * The number of limbs in each operand, and the result, is given by the
  276. * modulus \p N.
  277. *
  278. * \p X may be aliased to \p A or \p B, or even both, but may not overlap
  279. * either otherwise.
  280. *
  281. * \param[out] X The result of the modular addition.
  282. * \param[in] A Little-endian presentation of the left operand. This
  283. * must be smaller than \p N.
  284. * \param[in] B Little-endian presentation of the right operand. This
  285. * must be smaller than \p N.
  286. * \param[in] N The address of the modulus.
  287. */
  288. void mbedtls_mpi_mod_raw_add(mbedtls_mpi_uint *X,
  289. const mbedtls_mpi_uint *A,
  290. const mbedtls_mpi_uint *B,
  291. const mbedtls_mpi_mod_modulus *N);
  292. /** Convert an MPI from canonical representation (little-endian limb array)
  293. * to the representation associated with the modulus.
  294. *
  295. * \param[in,out] X The limb array to convert.
  296. * It must have as many limbs as \p N.
  297. * It is converted in place.
  298. * If this function returns an error, the content of \p X
  299. * is unspecified.
  300. * \param[in] N The modulus structure.
  301. *
  302. * \return \c 0 if successful.
  303. * Otherwise an \c MBEDTLS_ERR_MPI_xxx error code.
  304. */
  305. int mbedtls_mpi_mod_raw_canonical_to_modulus_rep(
  306. mbedtls_mpi_uint *X,
  307. const mbedtls_mpi_mod_modulus *N);
  308. /** Convert an MPI from the representation associated with the modulus
  309. * to canonical representation (little-endian limb array).
  310. *
  311. * \param[in,out] X The limb array to convert.
  312. * It must have as many limbs as \p N.
  313. * It is converted in place.
  314. * If this function returns an error, the content of \p X
  315. * is unspecified.
  316. * \param[in] N The modulus structure.
  317. *
  318. * \return \c 0 if successful.
  319. * Otherwise an \c MBEDTLS_ERR_MPI_xxx error code.
  320. */
  321. int mbedtls_mpi_mod_raw_modulus_to_canonical_rep(
  322. mbedtls_mpi_uint *X,
  323. const mbedtls_mpi_mod_modulus *N);
  324. /** Generate a random number uniformly in a range.
  325. *
  326. * This function generates a random number between \p min inclusive and
  327. * \p N exclusive.
  328. *
  329. * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
  330. * when the RNG is a suitably parametrized instance of HMAC_DRBG
  331. * and \p min is \c 1.
  332. *
  333. * \note There are `N - min` possible outputs. The lower bound
  334. * \p min can be reached, but the upper bound \p N cannot.
  335. *
  336. * \param X The destination MPI, in canonical representation modulo \p N.
  337. * It must not be aliased with \p N or otherwise overlap it.
  338. * \param min The minimum value to return. It must be strictly smaller
  339. * than \b N.
  340. * \param N The modulus.
  341. * This is the upper bound of the output range, exclusive.
  342. * \param f_rng The RNG function to use. This must not be \c NULL.
  343. * \param p_rng The RNG parameter to be passed to \p f_rng.
  344. *
  345. * \return \c 0 if successful.
  346. * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
  347. * unable to find a suitable value within a limited number
  348. * of attempts. This has a negligible probability if \p N
  349. * is significantly larger than \p min, which is the case
  350. * for all usual cryptographic applications.
  351. */
  352. int mbedtls_mpi_mod_raw_random(mbedtls_mpi_uint *X,
  353. mbedtls_mpi_uint min,
  354. const mbedtls_mpi_mod_modulus *N,
  355. int (*f_rng)(void *, unsigned char *, size_t),
  356. void *p_rng);
  357. /** Convert an MPI into Montgomery form.
  358. *
  359. * \param X The address of the MPI.
  360. * Must have the same number of limbs as \p N.
  361. * \param N The address of the modulus, which gives the size of
  362. * the base `R` = 2^(biL*N->limbs).
  363. *
  364. * \return \c 0 if successful.
  365. */
  366. int mbedtls_mpi_mod_raw_to_mont_rep(mbedtls_mpi_uint *X,
  367. const mbedtls_mpi_mod_modulus *N);
  368. /** Convert an MPI back from Montgomery representation.
  369. *
  370. * \param X The address of the MPI.
  371. * Must have the same number of limbs as \p N.
  372. * \param N The address of the modulus, which gives the size of
  373. * the base `R`= 2^(biL*N->limbs).
  374. *
  375. * \return \c 0 if successful.
  376. */
  377. int mbedtls_mpi_mod_raw_from_mont_rep(mbedtls_mpi_uint *X,
  378. const mbedtls_mpi_mod_modulus *N);
  379. /** \brief Perform fixed width modular negation.
  380. *
  381. * The size of the operation is determined by \p N. \p A must have
  382. * the same number of limbs as \p N.
  383. *
  384. * \p X may be aliased to \p A.
  385. *
  386. * \param[out] X The result of the modular negation.
  387. * This must be initialized.
  388. * \param[in] A Little-endian presentation of the input operand. This
  389. * must be less than or equal to \p N.
  390. * \param[in] N The modulus to use.
  391. */
  392. void mbedtls_mpi_mod_raw_neg(mbedtls_mpi_uint *X,
  393. const mbedtls_mpi_uint *A,
  394. const mbedtls_mpi_mod_modulus *N);
  395. #endif /* MBEDTLS_BIGNUM_MOD_RAW_H */