rsa_alt_helpers.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * \file rsa_alt_helpers.h
  3. *
  4. * \brief Context-independent RSA helper functions
  5. *
  6. * This module declares some RSA-related helper functions useful when
  7. * implementing the RSA interface. These functions are provided in a separate
  8. * compilation unit in order to make it easy for designers of alternative RSA
  9. * implementations to use them in their own code, as it is conceived that the
  10. * functionality they provide will be necessary for most complete
  11. * implementations.
  12. *
  13. * End-users of Mbed TLS who are not providing their own alternative RSA
  14. * implementations should not use these functions directly, and should instead
  15. * use only the functions declared in rsa.h.
  16. *
  17. * The interface provided by this module will be maintained through LTS (Long
  18. * Term Support) branches of Mbed TLS, but may otherwise be subject to change,
  19. * and must be considered an internal interface of the library.
  20. *
  21. * There are two classes of helper functions:
  22. *
  23. * (1) Parameter-generating helpers. These are:
  24. * - mbedtls_rsa_deduce_primes
  25. * - mbedtls_rsa_deduce_private_exponent
  26. * - mbedtls_rsa_deduce_crt
  27. * Each of these functions takes a set of core RSA parameters and
  28. * generates some other, or CRT related parameters.
  29. *
  30. * (2) Parameter-checking helpers. These are:
  31. * - mbedtls_rsa_validate_params
  32. * - mbedtls_rsa_validate_crt
  33. * They take a set of core or CRT related RSA parameters and check their
  34. * validity.
  35. *
  36. */
  37. /*
  38. * Copyright The Mbed TLS Contributors
  39. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  40. */
  41. #ifndef MBEDTLS_RSA_ALT_HELPERS_H
  42. #define MBEDTLS_RSA_ALT_HELPERS_H
  43. #include "mbedtls/build_info.h"
  44. #include "mbedtls/bignum.h"
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * \brief Compute RSA prime moduli P, Q from public modulus N=PQ
  50. * and a pair of private and public key.
  51. *
  52. * \note This is a 'static' helper function not operating on
  53. * an RSA context. Alternative implementations need not
  54. * overwrite it.
  55. *
  56. * \param N RSA modulus N = PQ, with P, Q to be found
  57. * \param E RSA public exponent
  58. * \param D RSA private exponent
  59. * \param P Pointer to MPI holding first prime factor of N on success
  60. * \param Q Pointer to MPI holding second prime factor of N on success
  61. *
  62. * \return
  63. * - 0 if successful. In this case, P and Q constitute a
  64. * factorization of N.
  65. * - A non-zero error code otherwise.
  66. *
  67. * \note It is neither checked that P, Q are prime nor that
  68. * D, E are modular inverses wrt. P-1 and Q-1. For that,
  69. * use the helper function \c mbedtls_rsa_validate_params.
  70. *
  71. */
  72. int mbedtls_rsa_deduce_primes(mbedtls_mpi const *N, mbedtls_mpi const *E,
  73. mbedtls_mpi const *D,
  74. mbedtls_mpi *P, mbedtls_mpi *Q);
  75. /**
  76. * \brief Compute RSA private exponent from
  77. * prime moduli and public key.
  78. *
  79. * \note This is a 'static' helper function not operating on
  80. * an RSA context. Alternative implementations need not
  81. * overwrite it.
  82. *
  83. * \param P First prime factor of RSA modulus
  84. * \param Q Second prime factor of RSA modulus
  85. * \param E RSA public exponent
  86. * \param D Pointer to MPI holding the private exponent on success,
  87. * i.e. the modular inverse of E modulo LCM(P-1,Q-1).
  88. *
  89. * \return \c 0 if successful.
  90. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  91. * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if E is not coprime to P-1
  92. * and Q-1, that is, if GCD( E, (P-1)*(Q-1) ) != 1.
  93. * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if inputs are otherwise
  94. * invalid.
  95. *
  96. * \note This function does not check whether P and Q are primes.
  97. *
  98. */
  99. int mbedtls_rsa_deduce_private_exponent(mbedtls_mpi const *P,
  100. mbedtls_mpi const *Q,
  101. mbedtls_mpi const *E,
  102. mbedtls_mpi *D);
  103. /**
  104. * \brief Generate RSA-CRT parameters
  105. *
  106. * \note This is a 'static' helper function not operating on
  107. * an RSA context. Alternative implementations need not
  108. * overwrite it.
  109. *
  110. * \param P First prime factor of N
  111. * \param Q Second prime factor of N
  112. * \param D RSA private exponent
  113. * \param DP Output variable for D modulo P-1
  114. * \param DQ Output variable for D modulo Q-1
  115. * \param QP Output variable for the modular inverse of Q modulo P.
  116. *
  117. * \return 0 on success, non-zero error code otherwise.
  118. *
  119. * \note This function does not check whether P, Q are
  120. * prime and whether D is a valid private exponent.
  121. *
  122. */
  123. int mbedtls_rsa_deduce_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
  124. const mbedtls_mpi *D, mbedtls_mpi *DP,
  125. mbedtls_mpi *DQ, mbedtls_mpi *QP);
  126. /**
  127. * \brief Check validity of core RSA parameters
  128. *
  129. * \note This is a 'static' helper function not operating on
  130. * an RSA context. Alternative implementations need not
  131. * overwrite it.
  132. *
  133. * \param N RSA modulus N = PQ
  134. * \param P First prime factor of N
  135. * \param Q Second prime factor of N
  136. * \param D RSA private exponent
  137. * \param E RSA public exponent
  138. * \param f_rng PRNG to be used for primality check, or NULL
  139. * \param p_rng PRNG context for f_rng, or NULL
  140. *
  141. * \return
  142. * - 0 if the following conditions are satisfied
  143. * if all relevant parameters are provided:
  144. * - P prime if f_rng != NULL (%)
  145. * - Q prime if f_rng != NULL (%)
  146. * - 1 < N = P * Q
  147. * - 1 < D, E < N
  148. * - D and E are modular inverses modulo P-1 and Q-1
  149. * (%) This is only done if MBEDTLS_GENPRIME is defined.
  150. * - A non-zero error code otherwise.
  151. *
  152. * \note The function can be used with a restricted set of arguments
  153. * to perform specific checks only. E.g., calling it with
  154. * (-,P,-,-,-) and a PRNG amounts to a primality check for P.
  155. */
  156. int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P,
  157. const mbedtls_mpi *Q, const mbedtls_mpi *D,
  158. const mbedtls_mpi *E,
  159. int (*f_rng)(void *, unsigned char *, size_t),
  160. void *p_rng);
  161. /**
  162. * \brief Check validity of RSA CRT parameters
  163. *
  164. * \note This is a 'static' helper function not operating on
  165. * an RSA context. Alternative implementations need not
  166. * overwrite it.
  167. *
  168. * \param P First prime factor of RSA modulus
  169. * \param Q Second prime factor of RSA modulus
  170. * \param D RSA private exponent
  171. * \param DP MPI to check for D modulo P-1
  172. * \param DQ MPI to check for D modulo P-1
  173. * \param QP MPI to check for the modular inverse of Q modulo P.
  174. *
  175. * \return
  176. * - 0 if the following conditions are satisfied:
  177. * - D = DP mod P-1 if P, D, DP != NULL
  178. * - Q = DQ mod P-1 if P, D, DQ != NULL
  179. * - QP = Q^-1 mod P if P, Q, QP != NULL
  180. * - \c MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if check failed,
  181. * potentially including \c MBEDTLS_ERR_MPI_XXX if some
  182. * MPI calculations failed.
  183. * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if insufficient
  184. * data was provided to check DP, DQ or QP.
  185. *
  186. * \note The function can be used with a restricted set of arguments
  187. * to perform specific checks only. E.g., calling it with the
  188. * parameters (P, -, D, DP, -, -) will check DP = D mod P-1.
  189. */
  190. int mbedtls_rsa_validate_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
  191. const mbedtls_mpi *D, const mbedtls_mpi *DP,
  192. const mbedtls_mpi *DQ, const mbedtls_mpi *QP);
  193. #ifdef __cplusplus
  194. }
  195. #endif
  196. #endif /* rsa_alt_helpers.h */