rsa_internal.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * \file rsa_internal.h
  3. *
  4. * \brief Context-independent RSA helper functions
  5. */
  6. /*
  7. * Copyright (C) 2006-2017, ARM Limited, All Rights Reserved
  8. * SPDX-License-Identifier: Apache-2.0
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11. * not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. *
  22. * This file is part of mbed TLS (https://tls.mbed.org)
  23. *
  24. *
  25. * This file declares some RSA-related helper functions useful when
  26. * implementing the RSA interface. They are public and provided in a
  27. * separate compilation unit in order to make it easy for designers of
  28. * alternative RSA implementations to use them in their code, as it is
  29. * conceived that the functionality they provide will be necessary
  30. * for most complete implementations.
  31. *
  32. * End-users of Mbed TLS not intending to re-implement the RSA functionality
  33. * are not expected to get into the need of making use of these functions directly,
  34. * but instead should be able to use the functions declared in rsa.h.
  35. *
  36. * There are two classes of helper functions:
  37. * (1) Parameter-generating helpers. These are:
  38. * - mbedtls_rsa_deduce_primes
  39. * - mbedtls_rsa_deduce_private_exponent
  40. * - mbedtls_rsa_deduce_crt
  41. * Each of these functions takes a set of core RSA parameters
  42. * and generates some other, or CRT related parameters.
  43. * (2) Parameter-checking helpers. These are:
  44. * - mbedtls_rsa_validate_params
  45. * - mbedtls_rsa_validate_crt
  46. * They take a set of core or CRT related RSA parameters
  47. * and check their validity.
  48. *
  49. */
  50. #ifndef MBEDTLS_RSA_INTERNAL_H
  51. #define MBEDTLS_RSA_INTERNAL_H
  52. #if !defined(MBEDTLS_CONFIG_FILE)
  53. #include "config.h"
  54. #else
  55. #include MBEDTLS_CONFIG_FILE
  56. #endif
  57. #include "bignum.h"
  58. #ifdef __cplusplus
  59. extern "C" {
  60. #endif
  61. /**
  62. * \brief Compute RSA prime moduli P, Q from public modulus N=PQ
  63. * and a pair of private and public key.
  64. *
  65. * \note This is a 'static' helper function not operating on
  66. * an RSA context. Alternative implementations need not
  67. * overwrite it.
  68. *
  69. * \param N RSA modulus N = PQ, with P, Q to be found
  70. * \param E RSA public exponent
  71. * \param D RSA private exponent
  72. * \param P Pointer to MPI holding first prime factor of N on success
  73. * \param Q Pointer to MPI holding second prime factor of N on success
  74. *
  75. * \return
  76. * - 0 if successful. In this case, P and Q constitute a
  77. * factorization of N.
  78. * - A non-zero error code otherwise.
  79. *
  80. * \note It is neither checked that P, Q are prime nor that
  81. * D, E are modular inverses wrt. P-1 and Q-1. For that,
  82. * use the helper function \c mbedtls_rsa_validate_params.
  83. *
  84. */
  85. int mbedtls_rsa_deduce_primes( mbedtls_mpi const *N, mbedtls_mpi const *E,
  86. mbedtls_mpi const *D,
  87. mbedtls_mpi *P, mbedtls_mpi *Q );
  88. /**
  89. * \brief Compute RSA private exponent from
  90. * prime moduli and public key.
  91. *
  92. * \note This is a 'static' helper function not operating on
  93. * an RSA context. Alternative implementations need not
  94. * overwrite it.
  95. *
  96. * \param P First prime factor of RSA modulus
  97. * \param Q Second prime factor of RSA modulus
  98. * \param E RSA public exponent
  99. * \param D Pointer to MPI holding the private exponent on success.
  100. *
  101. * \return
  102. * - 0 if successful. In this case, D is set to a simultaneous
  103. * modular inverse of E modulo both P-1 and Q-1.
  104. * - A non-zero error code otherwise.
  105. *
  106. * \note This function does not check whether P and Q are primes.
  107. *
  108. */
  109. int mbedtls_rsa_deduce_private_exponent( mbedtls_mpi const *P,
  110. mbedtls_mpi const *Q,
  111. mbedtls_mpi const *E,
  112. mbedtls_mpi *D );
  113. /**
  114. * \brief Generate RSA-CRT parameters
  115. *
  116. * \note This is a 'static' helper function not operating on
  117. * an RSA context. Alternative implementations need not
  118. * overwrite it.
  119. *
  120. * \param P First prime factor of N
  121. * \param Q Second prime factor of N
  122. * \param D RSA private exponent
  123. * \param DP Output variable for D modulo P-1
  124. * \param DQ Output variable for D modulo Q-1
  125. * \param QP Output variable for the modular inverse of Q modulo P.
  126. *
  127. * \return 0 on success, non-zero error code otherwise.
  128. *
  129. * \note This function does not check whether P, Q are
  130. * prime and whether D is a valid private exponent.
  131. *
  132. */
  133. int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
  134. const mbedtls_mpi *D, mbedtls_mpi *DP,
  135. mbedtls_mpi *DQ, mbedtls_mpi *QP );
  136. /**
  137. * \brief Check validity of core RSA parameters
  138. *
  139. * \note This is a 'static' helper function not operating on
  140. * an RSA context. Alternative implementations need not
  141. * overwrite it.
  142. *
  143. * \param N RSA modulus N = PQ
  144. * \param P First prime factor of N
  145. * \param Q Second prime factor of N
  146. * \param D RSA private exponent
  147. * \param E RSA public exponent
  148. * \param f_rng PRNG to be used for primality check, or NULL
  149. * \param p_rng PRNG context for f_rng, or NULL
  150. *
  151. * \return
  152. * - 0 if the following conditions are satisfied
  153. * if all relevant parameters are provided:
  154. * - P prime if f_rng != NULL (%)
  155. * - Q prime if f_rng != NULL (%)
  156. * - 1 < N = P * Q
  157. * - 1 < D, E < N
  158. * - D and E are modular inverses modulo P-1 and Q-1
  159. * (%) This is only done if MBEDTLS_GENPRIME is defined.
  160. * - A non-zero error code otherwise.
  161. *
  162. * \note The function can be used with a restricted set of arguments
  163. * to perform specific checks only. E.g., calling it with
  164. * (-,P,-,-,-) and a PRNG amounts to a primality check for P.
  165. */
  166. int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
  167. const mbedtls_mpi *Q, const mbedtls_mpi *D,
  168. const mbedtls_mpi *E,
  169. int (*f_rng)(void *, unsigned char *, size_t),
  170. void *p_rng );
  171. /**
  172. * \brief Check validity of RSA CRT parameters
  173. *
  174. * \note This is a 'static' helper function not operating on
  175. * an RSA context. Alternative implementations need not
  176. * overwrite it.
  177. *
  178. * \param P First prime factor of RSA modulus
  179. * \param Q Second prime factor of RSA modulus
  180. * \param D RSA private exponent
  181. * \param DP MPI to check for D modulo P-1
  182. * \param DQ MPI to check for D modulo P-1
  183. * \param QP MPI to check for the modular inverse of Q modulo P.
  184. *
  185. * \return
  186. * - 0 if the following conditions are satisfied:
  187. * - D = DP mod P-1 if P, D, DP != NULL
  188. * - Q = DQ mod P-1 if P, D, DQ != NULL
  189. * - QP = Q^-1 mod P if P, Q, QP != NULL
  190. * - \c MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if check failed,
  191. * potentially including \c MBEDTLS_ERR_MPI_XXX if some
  192. * MPI calculations failed.
  193. * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if insufficient
  194. * data was provided to check DP, DQ or QP.
  195. *
  196. * \note The function can be used with a restricted set of arguments
  197. * to perform specific checks only. E.g., calling it with the
  198. * parameters (P, -, D, DP, -, -) will check DP = D mod P-1.
  199. */
  200. int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
  201. const mbedtls_mpi *D, const mbedtls_mpi *DP,
  202. const mbedtls_mpi *DQ, const mbedtls_mpi *QP );
  203. #endif /* rsa_internal.h */