ecp_invasive.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**
  2. * \file ecp_invasive.h
  3. *
  4. * \brief ECP module: interfaces for invasive testing only.
  5. *
  6. * The interfaces in this file are intended for testing purposes only.
  7. * They SHOULD NOT be made available in library integrations except when
  8. * building the library for testing.
  9. */
  10. /*
  11. * Copyright The Mbed TLS Contributors
  12. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  13. */
  14. #ifndef MBEDTLS_ECP_INVASIVE_H
  15. #define MBEDTLS_ECP_INVASIVE_H
  16. #include "common.h"
  17. #include "mbedtls/bignum.h"
  18. #include "bignum_mod.h"
  19. #include "mbedtls/ecp.h"
  20. /*
  21. * Curve modulus types
  22. */
  23. typedef enum {
  24. MBEDTLS_ECP_MOD_NONE = 0,
  25. MBEDTLS_ECP_MOD_COORDINATE,
  26. MBEDTLS_ECP_MOD_SCALAR
  27. } mbedtls_ecp_modulus_type;
  28. typedef enum {
  29. MBEDTLS_ECP_VARIANT_NONE = 0,
  30. MBEDTLS_ECP_VARIANT_WITH_MPI_STRUCT,
  31. MBEDTLS_ECP_VARIANT_WITH_MPI_UINT
  32. } mbedtls_ecp_variant;
  33. #if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ECP_LIGHT)
  34. /** Queries the ecp variant.
  35. *
  36. * \return The id of the ecp variant.
  37. */
  38. MBEDTLS_STATIC_TESTABLE
  39. mbedtls_ecp_variant mbedtls_ecp_get_variant(void);
  40. #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
  41. /** Generate a private key on a Montgomery curve (Curve25519 or Curve448).
  42. *
  43. * This function implements key generation for the set of secret keys
  44. * specified in [Curve25519] p. 5 and in [Curve448]. The resulting value
  45. * has the lower bits masked but is not necessarily canonical.
  46. *
  47. * \note - [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
  48. * - [RFC7748] https://tools.ietf.org/html/rfc7748
  49. *
  50. * \p high_bit The position of the high-order bit of the key to generate.
  51. * This is the bit-size of the key minus 1:
  52. * 254 for Curve25519 or 447 for Curve448.
  53. * \param d The randomly generated key. This is a number of size
  54. * exactly \p high_bit + 1 bits, with the least significant bits
  55. * masked as specified in [Curve25519] and in [RFC7748] §5.
  56. * \param f_rng The RNG function.
  57. * \param p_rng The RNG context to be passed to \p f_rng.
  58. *
  59. * \return \c 0 on success.
  60. * \return \c MBEDTLS_ERR_ECP_xxx or MBEDTLS_ERR_MPI_xxx on failure.
  61. */
  62. int mbedtls_ecp_gen_privkey_mx(size_t high_bit,
  63. mbedtls_mpi *d,
  64. int (*f_rng)(void *, unsigned char *, size_t),
  65. void *p_rng);
  66. #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
  67. #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
  68. /** Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
  69. *
  70. * This operation expects a 384 bit MPI and the result of the reduction
  71. * is a 192 bit MPI.
  72. *
  73. * \param[in,out] Np The address of the MPI to be converted.
  74. * Must have twice as many limbs as the modulus.
  75. * Upon return this holds the reduced value. The bitlength
  76. * of the reduced value is the same as that of the modulus
  77. * (192 bits).
  78. * \param[in] Nn The length of \p Np in limbs.
  79. */
  80. MBEDTLS_STATIC_TESTABLE
  81. int mbedtls_ecp_mod_p192_raw(mbedtls_mpi_uint *Np, size_t Nn);
  82. #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
  83. #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
  84. /** Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
  85. *
  86. * \param[in,out] X The address of the MPI to be converted.
  87. * Must have exact limb size that stores a 448-bit MPI
  88. * (double the bitlength of the modulus).
  89. * Upon return holds the reduced value which is
  90. * in range `0 <= X < 2 * N` (where N is the modulus).
  91. * The bitlength of the reduced value is the same as
  92. * that of the modulus (224 bits).
  93. * \param[in] X_limbs The length of \p X in limbs.
  94. *
  95. * \return \c 0 on success.
  96. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs is not the
  97. * limb size that sores a 448-bit MPI.
  98. */
  99. MBEDTLS_STATIC_TESTABLE
  100. int mbedtls_ecp_mod_p224_raw(mbedtls_mpi_uint *X, size_t X_limbs);
  101. #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
  102. #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
  103. /** Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
  104. *
  105. * \param[in,out] X The address of the MPI to be converted.
  106. * Must have exact limb size that stores a 512-bit MPI
  107. * (double the bitlength of the modulus).
  108. * Upon return holds the reduced value which is
  109. * in range `0 <= X < 2 * N` (where N is the modulus).
  110. * The bitlength of the reduced value is the same as
  111. * that of the modulus (256 bits).
  112. * \param[in] X_limbs The length of \p X in limbs.
  113. *
  114. * \return \c 0 on success.
  115. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs is not the
  116. * limb size that sores a 512-bit MPI.
  117. */
  118. MBEDTLS_STATIC_TESTABLE
  119. int mbedtls_ecp_mod_p256_raw(mbedtls_mpi_uint *X, size_t X_limbs);
  120. #endif
  121. #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
  122. /** Fast quasi-reduction modulo p521 = 2^521 - 1 (FIPS 186-3 D.2.5)
  123. *
  124. * \param[in,out] X The address of the MPI to be converted.
  125. * Must have twice as many limbs as the modulus
  126. * (the modulus is 521 bits long). Upon return this
  127. * holds the reduced value. The reduced value is
  128. * in range `0 <= X < 2 * N` (where N is the modulus).
  129. * and its the bitlength is one plus the bitlength
  130. * of the modulus.
  131. * \param[in] X_limbs The length of \p X in limbs.
  132. *
  133. * \return \c 0 on success.
  134. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs does not have
  135. * twice as many limbs as the modulus.
  136. */
  137. MBEDTLS_STATIC_TESTABLE
  138. int mbedtls_ecp_mod_p521_raw(mbedtls_mpi_uint *X, size_t X_limbs);
  139. #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
  140. #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
  141. /** Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)
  142. *
  143. * \param[in,out] X The address of the MPI to be converted.
  144. * Must have exact limb size that stores a 768-bit MPI
  145. * (double the bitlength of the modulus).
  146. * Upon return holds the reduced value which is
  147. * in range `0 <= X < 2 * N` (where N is the modulus).
  148. * The bitlength of the reduced value is the same as
  149. * that of the modulus (384 bits).
  150. * \param[in] X_limbs The length of \p N in limbs.
  151. *
  152. * \return \c 0 on success.
  153. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p N_n does not have
  154. * twice as many limbs as the modulus.
  155. */
  156. MBEDTLS_STATIC_TESTABLE
  157. int mbedtls_ecp_mod_p384_raw(mbedtls_mpi_uint *X, size_t X_limbs);
  158. #endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
  159. #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
  160. /** Fast quasi-reduction modulo p192k1 = 2^192 - R,
  161. * with R = 2^32 + 2^12 + 2^8 + 2^7 + 2^6 + 2^3 + 1 = 0x01000011C9
  162. *
  163. * \param[in,out] X The address of the MPI to be converted.
  164. * Must have exact limb size that stores a 384-bit MPI
  165. * (double the bitlength of the modulus).
  166. * Upon return holds the reduced value which is
  167. * in range `0 <= X < 2 * N` (where N is the modulus).
  168. * The bitlength of the reduced value is the same as
  169. * that of the modulus (192 bits).
  170. * \param[in] X_limbs The length of \p X in limbs.
  171. *
  172. * \return \c 0 on success.
  173. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
  174. * twice as many limbs as the modulus.
  175. * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
  176. */
  177. MBEDTLS_STATIC_TESTABLE
  178. int mbedtls_ecp_mod_p192k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
  179. #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
  180. #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
  181. /** Fast quasi-reduction modulo p224k1 = 2^224 - R,
  182. * with R = 2^32 + 2^12 + 2^11 + 2^9 + 2^7 + 2^4 + 2 + 1 = 0x0100001A93
  183. *
  184. * \param[in,out] X The address of the MPI to be converted.
  185. * Must have exact limb size that stores a 448-bit MPI
  186. * (double the bitlength of the modulus).
  187. * Upon return holds the reduced value which is
  188. * in range `0 <= X < 2 * N` (where N is the modulus).
  189. * The bitlength of the reduced value is the same as
  190. * that of the modulus (224 bits).
  191. * \param[in] X_limbs The length of \p X in limbs.
  192. *
  193. * \return \c 0 on success.
  194. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
  195. * twice as many limbs as the modulus.
  196. * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
  197. */
  198. MBEDTLS_STATIC_TESTABLE
  199. int mbedtls_ecp_mod_p224k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
  200. #endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
  201. #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
  202. /** Fast quasi-reduction modulo p256k1 = 2^256 - R,
  203. * with R = 2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1 = 0x01000003D1
  204. *
  205. * \param[in,out] X The address of the MPI to be converted.
  206. * Must have exact limb size that stores a 512-bit MPI
  207. * (double the bitlength of the modulus).
  208. * Upon return holds the reduced value which is
  209. * in range `0 <= X < 2 * N` (where N is the modulus).
  210. * The bitlength of the reduced value is the same as
  211. * that of the modulus (256 bits).
  212. * \param[in] X_limbs The length of \p X in limbs.
  213. *
  214. * \return \c 0 on success.
  215. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
  216. * twice as many limbs as the modulus.
  217. * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
  218. */
  219. MBEDTLS_STATIC_TESTABLE
  220. int mbedtls_ecp_mod_p256k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
  221. #endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
  222. #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
  223. /** Fast quasi-reduction modulo p255 = 2^255 - 19
  224. *
  225. * \param[in,out] X The address of the MPI to be converted.
  226. * Must have exact limb size that stores a 510-bit MPI
  227. * (double the bitlength of the modulus).
  228. * Upon return holds the reduced value which is
  229. * in range `0 <= X < 2 * N` (where N is the modulus).
  230. * \param[in] X_limbs The length of \p X in limbs.
  231. *
  232. * \return \c 0 on success.
  233. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
  234. * twice as many limbs as the modulus.
  235. * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
  236. */
  237. MBEDTLS_STATIC_TESTABLE
  238. int mbedtls_ecp_mod_p255_raw(mbedtls_mpi_uint *X, size_t X_limbs);
  239. #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
  240. #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
  241. /** Fast quasi-reduction modulo p448 = 2^448 - 2^224 - 1
  242. * Write X as A0 + 2^448 A1 and A1 as B0 + 2^224 B1, and return A0 + A1 + B1 +
  243. * (B0 + B1) * 2^224.
  244. *
  245. * \param[in,out] X The address of the MPI to be converted.
  246. * Must have exact limb size that stores a 896-bit MPI
  247. * (double the bitlength of the modulus). Upon return
  248. * holds the reduced value which is in range `0 <= X <
  249. * N` (where N is the modulus). The bitlength of the
  250. * reduced value is the same as that of the modulus
  251. * (448 bits).
  252. * \param[in] X_limbs The length of \p X in limbs.
  253. *
  254. * \return \c 0 on Success.
  255. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
  256. * twice as many limbs as the modulus.
  257. * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation
  258. * failed.
  259. */
  260. MBEDTLS_STATIC_TESTABLE
  261. int mbedtls_ecp_mod_p448_raw(mbedtls_mpi_uint *X, size_t X_limbs);
  262. #endif /* MBEDTLS_ECP_DP_CURVE448_ENABLED */
  263. /** Initialise a modulus with hard-coded const curve data.
  264. *
  265. * \note The caller is responsible for the \p N modulus' memory.
  266. * mbedtls_mpi_mod_modulus_free(&N) should be invoked at the
  267. * end of its lifecycle.
  268. *
  269. * \param[in,out] N The address of the modulus structure to populate.
  270. * Must be initialized.
  271. * \param[in] id The mbedtls_ecp_group_id for which to initialise the modulus.
  272. * \param[in] ctype The mbedtls_ecp_modulus_type identifier for a coordinate modulus (P)
  273. * or a scalar modulus (N).
  274. *
  275. * \return \c 0 if successful.
  276. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the given MPIs do not
  277. * have the correct number of limbs.
  278. *
  279. */
  280. MBEDTLS_STATIC_TESTABLE
  281. int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N,
  282. const mbedtls_ecp_group_id id,
  283. const mbedtls_ecp_modulus_type ctype);
  284. #endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_ECP_C */
  285. #endif /* MBEDTLS_ECP_INVASIVE_H */