ecp_invasive.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  15. * not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  22. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. #ifndef MBEDTLS_ECP_INVASIVE_H
  27. #define MBEDTLS_ECP_INVASIVE_H
  28. #include "common.h"
  29. #include "mbedtls/bignum.h"
  30. #include "bignum_mod.h"
  31. #include "mbedtls/ecp.h"
  32. #if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ECP_C)
  33. #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
  34. /* Preconditions:
  35. * - bits is a multiple of 64 or is 224
  36. * - c is -1 or -2
  37. * - 0 <= N < 2^bits
  38. * - N has room for bits plus one limb
  39. *
  40. * Behavior:
  41. * Set N to c * 2^bits + old_value_of_N.
  42. */
  43. void mbedtls_ecp_fix_negative(mbedtls_mpi *N, signed char c, size_t bits);
  44. #endif
  45. #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
  46. /** Generate a private key on a Montgomery curve (Curve25519 or Curve448).
  47. *
  48. * This function implements key generation for the set of secret keys
  49. * specified in [Curve25519] p. 5 and in [Curve448]. The resulting value
  50. * has the lower bits masked but is not necessarily canonical.
  51. *
  52. * \note - [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
  53. * - [RFC7748] https://tools.ietf.org/html/rfc7748
  54. *
  55. * \p high_bit The position of the high-order bit of the key to generate.
  56. * This is the bit-size of the key minus 1:
  57. * 254 for Curve25519 or 447 for Curve448.
  58. * \param d The randomly generated key. This is a number of size
  59. * exactly \p n_bits + 1 bits, with the least significant bits
  60. * masked as specified in [Curve25519] and in [RFC7748] §5.
  61. * \param f_rng The RNG function.
  62. * \param p_rng The RNG context to be passed to \p f_rng.
  63. *
  64. * \return \c 0 on success.
  65. * \return \c MBEDTLS_ERR_ECP_xxx or MBEDTLS_ERR_MPI_xxx on failure.
  66. */
  67. int mbedtls_ecp_gen_privkey_mx(size_t n_bits,
  68. mbedtls_mpi *d,
  69. int (*f_rng)(void *, unsigned char *, size_t),
  70. void *p_rng);
  71. #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
  72. #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
  73. /** Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
  74. *
  75. * This operation expects a 384 bit MPI and the result of the reduction
  76. * is a 192 bit MPI.
  77. *
  78. * \param[in,out] Np The address of the MPI to be converted.
  79. * Must have twice as many limbs as the modulus.
  80. * Upon return this holds the reduced value. The bitlength
  81. * of the reduced value is the same as that of the modulus
  82. * (192 bits).
  83. * \param[in] Nn The length of \p Np in limbs.
  84. */
  85. MBEDTLS_STATIC_TESTABLE
  86. int mbedtls_ecp_mod_p192_raw(mbedtls_mpi_uint *Np, size_t Nn);
  87. #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
  88. #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
  89. /** Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
  90. *
  91. * \param[in,out] X The address of the MPI to be converted.
  92. * Must have exact limb size that stores a 448-bit MPI
  93. * (double the bitlength of the modulus).
  94. * Upon return holds the reduced value which is
  95. * in range `0 <= X < 2 * N` (where N is the modulus).
  96. * The bitlength of the reduced value is the same as
  97. * that of the modulus (224 bits).
  98. * \param[in] X_limbs The length of \p X in limbs.
  99. *
  100. * \return \c 0 on success.
  101. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs is not the
  102. * limb size that sores a 448-bit MPI.
  103. */
  104. MBEDTLS_STATIC_TESTABLE
  105. int mbedtls_ecp_mod_p224_raw(mbedtls_mpi_uint *X, size_t X_limbs);
  106. #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
  107. #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
  108. /** Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
  109. *
  110. * \param[in,out] X The address of the MPI to be converted.
  111. * Must have exact limb size that stores a 512-bit MPI
  112. * (double the bitlength of the modulus).
  113. * Upon return holds the reduced value which is
  114. * in range `0 <= X < 2 * N` (where N is the modulus).
  115. * The bitlength of the reduced value is the same as
  116. * that of the modulus (256 bits).
  117. * \param[in] X_limbs The length of \p X in limbs.
  118. *
  119. * \return \c 0 on success.
  120. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs is not the
  121. * limb size that sores a 512-bit MPI.
  122. */
  123. MBEDTLS_STATIC_TESTABLE
  124. int mbedtls_ecp_mod_p256_raw(mbedtls_mpi_uint *X, size_t X_limbs);
  125. #endif
  126. #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
  127. /** Fast quasi-reduction modulo p521 = 2^521 - 1 (FIPS 186-3 D.2.5)
  128. *
  129. * \param[in,out] X The address of the MPI to be converted.
  130. * Must have twice as many limbs as the modulus
  131. * (the modulus is 521 bits long). Upon return this
  132. * holds the reduced value. The reduced value is
  133. * in range `0 <= X < 2 * N` (where N is the modulus).
  134. * and its the bitlength is one plus the bitlength
  135. * of the modulus.
  136. * \param[in] X_limbs The length of \p X in limbs.
  137. *
  138. * \return \c 0 on success.
  139. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs does not have
  140. * twice as many limbs as the modulus.
  141. */
  142. MBEDTLS_STATIC_TESTABLE
  143. int mbedtls_ecp_mod_p521_raw(mbedtls_mpi_uint *X, size_t X_limbs);
  144. #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
  145. /** Initialise a modulus with hard-coded const curve data.
  146. *
  147. * \note The caller is responsible for the \p N modulus' memory.
  148. * mbedtls_mpi_mod_modulus_free(&N) should be invoked at the
  149. * end of its lifecycle.
  150. *
  151. * \param[in,out] N The address of the modulus structure to populate.
  152. * Must be initialized.
  153. * \param[in] id The mbedtls_ecp_group_id for which to initialise the modulus.
  154. * \param[in] ctype The mbedtls_ecp_curve_type identifier for a coordinate modulus (P)
  155. * or a scalar modulus (N).
  156. *
  157. * \return \c 0 if successful.
  158. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the given MPIs do not
  159. * have the correct number of limbs.
  160. *
  161. */
  162. MBEDTLS_STATIC_TESTABLE
  163. int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N,
  164. const mbedtls_ecp_group_id id,
  165. const mbedtls_ecp_curve_type ctype);
  166. #endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_ECP_C */
  167. #endif /* MBEDTLS_ECP_INVASIVE_H */