1 /** 2 * \file rsa_internal.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 * This file is provided under the Apache License 2.0, or the 42 * GNU General Public License v2.0 or later. 43 * 44 * ********** 45 * Apache License 2.0: 46 * 47 * Licensed under the Apache License, Version 2.0 (the "License"); you may 48 * not use this file except in compliance with the License. 49 * You may obtain a copy of the License at 50 * 51 * http://www.apache.org/licenses/LICENSE-2.0 52 * 53 * Unless required by applicable law or agreed to in writing, software 54 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 55 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 56 * See the License for the specific language governing permissions and 57 * limitations under the License. 58 * 59 * ********** 60 * 61 * ********** 62 * GNU General Public License v2.0 or later: 63 * 64 * This program is free software; you can redistribute it and/or modify 65 * it under the terms of the GNU General Public License as published by 66 * the Free Software Foundation; either version 2 of the License, or 67 * (at your option) any later version. 68 * 69 * This program is distributed in the hope that it will be useful, 70 * but WITHOUT ANY WARRANTY; without even the implied warranty of 71 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 72 * GNU General Public License for more details. 73 * 74 * You should have received a copy of the GNU General Public License along 75 * with this program; if not, write to the Free Software Foundation, Inc., 76 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 77 * 78 * ********** 79 * 80 */ 81 82 #ifndef MBEDTLS_RSA_INTERNAL_H 83 #define MBEDTLS_RSA_INTERNAL_H 84 85 #if !defined(MBEDTLS_CONFIG_FILE) 86 #include "config.h" 87 #else 88 #include MBEDTLS_CONFIG_FILE 89 #endif 90 91 #include "bignum.h" 92 93 #ifdef __cplusplus 94 extern "C" { 95 #endif 96 97 98 /** 99 * \brief Compute RSA prime moduli P, Q from public modulus N=PQ 100 * and a pair of private and public key. 101 * 102 * \note This is a 'static' helper function not operating on 103 * an RSA context. Alternative implementations need not 104 * overwrite it. 105 * 106 * \param N RSA modulus N = PQ, with P, Q to be found 107 * \param E RSA public exponent 108 * \param D RSA private exponent 109 * \param P Pointer to MPI holding first prime factor of N on success 110 * \param Q Pointer to MPI holding second prime factor of N on success 111 * 112 * \return 113 * - 0 if successful. In this case, P and Q constitute a 114 * factorization of N. 115 * - A non-zero error code otherwise. 116 * 117 * \note It is neither checked that P, Q are prime nor that 118 * D, E are modular inverses wrt. P-1 and Q-1. For that, 119 * use the helper function \c mbedtls_rsa_validate_params. 120 * 121 */ 122 int mbedtls_rsa_deduce_primes( mbedtls_mpi const *N, mbedtls_mpi const *E, 123 mbedtls_mpi const *D, 124 mbedtls_mpi *P, mbedtls_mpi *Q ); 125 126 /** 127 * \brief Compute RSA private exponent from 128 * prime moduli and public key. 129 * 130 * \note This is a 'static' helper function not operating on 131 * an RSA context. Alternative implementations need not 132 * overwrite it. 133 * 134 * \param P First prime factor of RSA modulus 135 * \param Q Second prime factor of RSA modulus 136 * \param E RSA public exponent 137 * \param D Pointer to MPI holding the private exponent on success. 138 * 139 * \return 140 * - 0 if successful. In this case, D is set to a simultaneous 141 * modular inverse of E modulo both P-1 and Q-1. 142 * - A non-zero error code otherwise. 143 * 144 * \note This function does not check whether P and Q are primes. 145 * 146 */ 147 int mbedtls_rsa_deduce_private_exponent( mbedtls_mpi const *P, 148 mbedtls_mpi const *Q, 149 mbedtls_mpi const *E, 150 mbedtls_mpi *D ); 151 152 153 /** 154 * \brief Generate RSA-CRT parameters 155 * 156 * \note This is a 'static' helper function not operating on 157 * an RSA context. Alternative implementations need not 158 * overwrite it. 159 * 160 * \param P First prime factor of N 161 * \param Q Second prime factor of N 162 * \param D RSA private exponent 163 * \param DP Output variable for D modulo P-1 164 * \param DQ Output variable for D modulo Q-1 165 * \param QP Output variable for the modular inverse of Q modulo P. 166 * 167 * \return 0 on success, non-zero error code otherwise. 168 * 169 * \note This function does not check whether P, Q are 170 * prime and whether D is a valid private exponent. 171 * 172 */ 173 int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q, 174 const mbedtls_mpi *D, mbedtls_mpi *DP, 175 mbedtls_mpi *DQ, mbedtls_mpi *QP ); 176 177 178 /** 179 * \brief Check validity of core RSA parameters 180 * 181 * \note This is a 'static' helper function not operating on 182 * an RSA context. Alternative implementations need not 183 * overwrite it. 184 * 185 * \param N RSA modulus N = PQ 186 * \param P First prime factor of N 187 * \param Q Second prime factor of N 188 * \param D RSA private exponent 189 * \param E RSA public exponent 190 * \param f_rng PRNG to be used for primality check, or NULL 191 * \param p_rng PRNG context for f_rng, or NULL 192 * 193 * \return 194 * - 0 if the following conditions are satisfied 195 * if all relevant parameters are provided: 196 * - P prime if f_rng != NULL (%) 197 * - Q prime if f_rng != NULL (%) 198 * - 1 < N = P * Q 199 * - 1 < D, E < N 200 * - D and E are modular inverses modulo P-1 and Q-1 201 * (%) This is only done if MBEDTLS_GENPRIME is defined. 202 * - A non-zero error code otherwise. 203 * 204 * \note The function can be used with a restricted set of arguments 205 * to perform specific checks only. E.g., calling it with 206 * (-,P,-,-,-) and a PRNG amounts to a primality check for P. 207 */ 208 int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P, 209 const mbedtls_mpi *Q, const mbedtls_mpi *D, 210 const mbedtls_mpi *E, 211 int (*f_rng)(void *, unsigned char *, size_t), 212 void *p_rng ); 213 214 /** 215 * \brief Check validity of RSA CRT parameters 216 * 217 * \note This is a 'static' helper function not operating on 218 * an RSA context. Alternative implementations need not 219 * overwrite it. 220 * 221 * \param P First prime factor of RSA modulus 222 * \param Q Second prime factor of RSA modulus 223 * \param D RSA private exponent 224 * \param DP MPI to check for D modulo P-1 225 * \param DQ MPI to check for D modulo P-1 226 * \param QP MPI to check for the modular inverse of Q modulo P. 227 * 228 * \return 229 * - 0 if the following conditions are satisfied: 230 * - D = DP mod P-1 if P, D, DP != NULL 231 * - Q = DQ mod P-1 if P, D, DQ != NULL 232 * - QP = Q^-1 mod P if P, Q, QP != NULL 233 * - \c MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if check failed, 234 * potentially including \c MBEDTLS_ERR_MPI_XXX if some 235 * MPI calculations failed. 236 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if insufficient 237 * data was provided to check DP, DQ or QP. 238 * 239 * \note The function can be used with a restricted set of arguments 240 * to perform specific checks only. E.g., calling it with the 241 * parameters (P, -, D, DP, -, -) will check DP = D mod P-1. 242 */ 243 int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q, 244 const mbedtls_mpi *D, const mbedtls_mpi *DP, 245 const mbedtls_mpi *DQ, const mbedtls_mpi *QP ); 246 247 #ifdef __cplusplus 248 } 249 #endif 250 251 #endif /* rsa_internal.h */ 252