Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file rsa.h |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 4 | * \brief The RSA public-key cryptosystem |
| 5 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: Apache-2.0 |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | * not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 20 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 21 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 22 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #ifndef MBEDTLS_RSA_H |
| 24 | #define MBEDTLS_RSA_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if !defined(MBEDTLS_CONFIG_FILE) |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 27 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 28 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 30 | #endif |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 31 | |
Paul Bakker | 314052f | 2011-08-15 09:07:52 +0000 | [diff] [blame] | 32 | #include "bignum.h" |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 33 | #include "md.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 34 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_THREADING_C) |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 36 | #include "threading.h" |
| 37 | #endif |
| 38 | |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 39 | /* |
| 40 | * RSA Error codes |
| 41 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | #define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */ |
| 43 | #define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */ |
| 44 | #define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */ |
Manuel Pégourié-Gonnard | eecb43c | 2015-05-12 12:56:41 +0200 | [diff] [blame] | 45 | #define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the library's validity check. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | #define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */ |
| 47 | #define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */ |
| 48 | #define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */ |
| 49 | #define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */ |
| 50 | #define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */ |
Hanno Becker | 91c194d | 2017-09-29 12:50:12 +0100 | [diff] [blame] | 51 | #define MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED -0x4500 /**< The requested parameter export is not possible/allowed. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 52 | |
| 53 | /* |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 54 | * RSA constants |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 55 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | #define MBEDTLS_RSA_PUBLIC 0 |
| 57 | #define MBEDTLS_RSA_PRIVATE 1 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 58 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | #define MBEDTLS_RSA_PKCS_V15 0 |
| 60 | #define MBEDTLS_RSA_PKCS_V21 1 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 61 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | #define MBEDTLS_RSA_SIGN 1 |
| 63 | #define MBEDTLS_RSA_CRYPT 2 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 64 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 65 | #define MBEDTLS_RSA_SALT_LEN_ANY -1 |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 66 | |
Manuel Pégourié-Gonnard | e511ffc | 2013-08-22 17:33:21 +0200 | [diff] [blame] | 67 | /* |
| 68 | * The above constants may be used even if the RSA module is compile out, |
| 69 | * eg for alternative (PKCS#11) RSA implemenations in the PK layers. |
| 70 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 71 | #if defined(MBEDTLS_RSA_C) |
Manuel Pégourié-Gonnard | e511ffc | 2013-08-22 17:33:21 +0200 | [diff] [blame] | 72 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 73 | #ifdef __cplusplus |
| 74 | extern "C" { |
| 75 | #endif |
| 76 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 77 | /** |
Hanno Becker | a3ebec2 | 2017-08-23 14:06:24 +0100 | [diff] [blame] | 78 | * Helper functions for RSA-related operations on MPI's. |
| 79 | */ |
| 80 | |
| 81 | /** |
| 82 | * \brief Compute RSA prime moduli P, Q from public modulus N=PQ |
Hanno Becker | b0c5edc | 2017-08-23 22:16:10 +0100 | [diff] [blame] | 83 | * and a pair of private and public key. |
Hanno Becker | a3ebec2 | 2017-08-23 14:06:24 +0100 | [diff] [blame] | 84 | * |
| 85 | * \note This is a 'static' helper function not operating on |
| 86 | * an RSA context. Alternative implementations need not |
| 87 | * overwrite it. |
| 88 | * |
| 89 | * \param N RSA modulus N = PQ, with P, Q to be found |
| 90 | * \param D RSA private exponent |
| 91 | * \param E RSA public exponent |
| 92 | * \param f_rng PRNG to be used for randomization, or NULL |
| 93 | * \param p_rng PRNG context for f_rng, or NULL |
| 94 | * \param P Pointer to MPI holding first prime factor of N on success |
| 95 | * \param Q Pointer to MPI holding second prime factor of N on success |
| 96 | * |
Hanno Becker | ed20361 | 2017-09-29 13:34:25 +0100 | [diff] [blame] | 97 | * \return |
| 98 | * - 0 if successful. In this case, P and Q constitute a |
Hanno Becker | a3ebec2 | 2017-08-23 14:06:24 +0100 | [diff] [blame] | 99 | * factorization of N, and it is guaranteed that D and E |
| 100 | * are indeed modular inverses modulo P-1 and modulo Q-1. |
| 101 | * The values of N, D and E are unchanged. It is checked |
| 102 | * that P, Q are prime if a PRNG is provided. |
| 103 | * - A non-zero error code otherwise. In this case, the values |
| 104 | * of N, D, E are undefined. |
| 105 | * |
| 106 | * \note The input MPI's are deliberately not declared as constant |
| 107 | * and may therefore be used for in-place calculations by |
| 108 | * the implementation. In particular, their values can be |
| 109 | * corrupted when the function fails. If the user cannot |
| 110 | * tolerate this, he has to make copies of the MPI's prior |
| 111 | * to calling this function. See \c mbedtls_mpi_copy for this. |
| 112 | */ |
| 113 | int mbedtls_rsa_deduce_moduli( mbedtls_mpi *N, mbedtls_mpi *D, mbedtls_mpi *E, |
| 114 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 115 | mbedtls_mpi *P, mbedtls_mpi *Q ); |
| 116 | |
| 117 | /** |
| 118 | * \brief Compute RSA private exponent from |
| 119 | * prime moduli and public key. |
| 120 | * |
| 121 | * \note This is a 'static' helper function not operating on |
| 122 | * an RSA context. Alternative implementations need not |
| 123 | * overwrite it. |
| 124 | * |
| 125 | * \param P First prime factor of RSA modulus |
| 126 | * \param Q Second prime factor of RSA modulus |
| 127 | * \param E RSA public exponent |
| 128 | * \param D Pointer to MPI holding the private exponent on success. |
| 129 | * |
| 130 | * \note This function does not check whether P and Q are primes. |
| 131 | * |
Hanno Becker | ed20361 | 2017-09-29 13:34:25 +0100 | [diff] [blame] | 132 | * \return |
| 133 | * - 0 if successful. In this case, D is set to a simultaneous |
Hanno Becker | a3ebec2 | 2017-08-23 14:06:24 +0100 | [diff] [blame] | 134 | * modular inverse of E modulo both P-1 and Q-1. |
| 135 | * - A non-zero error code otherwise. In this case, the values |
| 136 | * of P, Q, E are undefined. |
| 137 | * |
| 138 | * \note The input MPI's are deliberately not declared as constant |
| 139 | * and may therefore be used for in-place calculations by |
| 140 | * the implementation. In particular, their values can be |
| 141 | * corrupted when the function fails. If the user cannot |
| 142 | * tolerate this, he has to make copies of the MPI's prior |
| 143 | * to calling this function. See \c mbedtls_mpi_copy for this. |
| 144 | */ |
| 145 | int mbedtls_rsa_deduce_private( mbedtls_mpi *P, mbedtls_mpi *Q, mbedtls_mpi *E, |
| 146 | mbedtls_mpi *D ); |
| 147 | |
| 148 | |
| 149 | /** |
| 150 | * \brief Generate RSA-CRT parameters |
| 151 | * |
| 152 | * \note This is a 'static' helper function not operating on |
| 153 | * an RSA context. Alternative implementations need not |
| 154 | * overwrite it. |
| 155 | * |
| 156 | * \param P First prime factor of N |
| 157 | * \param Q Second prime factor of N |
| 158 | * \param D RSA private exponent |
| 159 | * \param DP Output variable for D modulo P-1 |
| 160 | * \param DQ Output variable for D modulo Q-1 |
| 161 | * \param QP Output variable for the modular inverse of Q modulo P. |
| 162 | * |
| 163 | * \return 0 on success, non-zero error code otherwise. |
| 164 | * |
| 165 | */ |
| 166 | int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q, |
| 167 | const mbedtls_mpi *D, mbedtls_mpi *DP, |
| 168 | mbedtls_mpi *DQ, mbedtls_mpi *QP ); |
| 169 | |
| 170 | |
| 171 | /** |
| 172 | * \brief Check validity of core RSA 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 N RSA modulus N = PQ |
| 179 | * \param P First prime factor of N |
| 180 | * \param Q Second prime factor of N |
| 181 | * \param D RSA private exponent |
| 182 | * \param E RSA public exponent |
Hanno Becker | 56bae95 | 2017-09-29 15:33:10 +0100 | [diff] [blame^] | 183 | * \param f_rng PRNG to be used for primality check, or NULL |
Hanno Becker | a3ebec2 | 2017-08-23 14:06:24 +0100 | [diff] [blame] | 184 | * \param p_rng PRNG context for f_rng, or NULL |
| 185 | * |
Hanno Becker | ed20361 | 2017-09-29 13:34:25 +0100 | [diff] [blame] | 186 | * \return |
| 187 | * - 0 if the following conditions are satisfied: |
| 188 | * - N = PQ if N,P,Q != NULL |
| 189 | * - D and E are modular inverses modulo P-1 and Q-1 |
| 190 | * if D,E,P,Q != NULL |
| 191 | * - P prime if f_rng, P != NULL |
| 192 | * - Q prime if f_rng, Q != NULL |
Hanno Becker | 750e8b4 | 2017-08-25 07:54:27 +0100 | [diff] [blame] | 193 | * - A non-zero error code otherwise. |
Hanno Becker | a3ebec2 | 2017-08-23 14:06:24 +0100 | [diff] [blame] | 194 | * |
| 195 | * \note The function can be used with a restricted set of arguments |
| 196 | * to perform specific checks only. E.g., calling it with |
| 197 | * (-,P,-,-,-) and a PRNG amounts to a primality check for P. |
Hanno Becker | a3ebec2 | 2017-08-23 14:06:24 +0100 | [diff] [blame] | 198 | */ |
Hanno Becker | 750e8b4 | 2017-08-25 07:54:27 +0100 | [diff] [blame] | 199 | int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P, |
| 200 | const mbedtls_mpi *Q, const mbedtls_mpi *D, |
| 201 | const mbedtls_mpi *E, |
| 202 | int (*f_rng)(void *, unsigned char *, size_t), |
| 203 | void *p_rng ); |
Hanno Becker | a3ebec2 | 2017-08-23 14:06:24 +0100 | [diff] [blame] | 204 | |
| 205 | /** |
Hanno Becker | d363799 | 2017-08-25 07:55:03 +0100 | [diff] [blame] | 206 | * \brief Check validity of RSA CRT parameters |
| 207 | * |
| 208 | * \note This is a 'static' helper function not operating on |
| 209 | * an RSA context. Alternative implementations need not |
| 210 | * overwrite it. |
| 211 | * |
| 212 | * \param P First prime factor of RSA modulus |
| 213 | * \param Q Second prime factor of RSA modulus |
| 214 | * \param D RSA private exponent |
| 215 | * \param DP MPI to check for D modulo P-1 |
| 216 | * \param DQ MPI to check for D modulo P-1 |
| 217 | * \param QP MPI to check for the modular inverse of Q modulo P. |
| 218 | * |
Hanno Becker | ed20361 | 2017-09-29 13:34:25 +0100 | [diff] [blame] | 219 | * \return |
| 220 | * - 0 if the following conditions are satisfied: |
| 221 | * - D = DP mod P-1 if P, D, DP != NULL |
| 222 | * - Q = DQ mod P-1 if P, D, DQ != NULL |
| 223 | * - QP = Q^-1 mod P if P, Q, QP != NULL |
| 224 | * - \c MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if check failed, |
| 225 | * potentially including \c MBEDTLS_ERR_MPI_XXX if some |
Hanno Becker | d363799 | 2017-08-25 07:55:03 +0100 | [diff] [blame] | 226 | * MPI calculations failed. |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 227 | * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if insufficient |
Hanno Becker | d363799 | 2017-08-25 07:55:03 +0100 | [diff] [blame] | 228 | * data was provided to check DP, DQ or QP. |
| 229 | * |
| 230 | * \note The function can be used with a restricted set of arguments |
| 231 | * to perform specific checks only. E.g., calling it with the |
| 232 | * parameters (P, -, D, DP, -, -) will check DP = D mod P-1. |
| 233 | */ |
| 234 | int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q, |
| 235 | const mbedtls_mpi *D, const mbedtls_mpi *DP, |
| 236 | const mbedtls_mpi *DQ, const mbedtls_mpi *QP ); |
| 237 | |
| 238 | /** |
Hanno Becker | a3ebec2 | 2017-08-23 14:06:24 +0100 | [diff] [blame] | 239 | * Implementation of RSA interface |
| 240 | */ |
| 241 | |
Hanno Becker | ab37731 | 2017-08-23 16:24:51 +0100 | [diff] [blame] | 242 | #if !defined(MBEDTLS_RSA_ALT) |
| 243 | |
Hanno Becker | a3ebec2 | 2017-08-23 14:06:24 +0100 | [diff] [blame] | 244 | /** |
Hanno Becker | 5063cd2 | 2017-09-29 11:49:12 +0100 | [diff] [blame] | 245 | * \brief RSA context structure |
| 246 | * |
| 247 | * \note Direct manipulation of the members of this structure |
| 248 | * is deprecated and will no longer be supported starting |
| 249 | * from the next major release. All manipulation should instead |
| 250 | * be done through the public interface functions. |
| 251 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 252 | */ |
| 253 | typedef struct |
| 254 | { |
| 255 | int ver; /*!< always 0 */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 256 | size_t len; /*!< size(N) in chars */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 257 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 258 | mbedtls_mpi N; /*!< public modulus */ |
| 259 | mbedtls_mpi E; /*!< public exponent */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 260 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 261 | mbedtls_mpi D; /*!< private exponent */ |
| 262 | mbedtls_mpi P; /*!< 1st prime factor */ |
| 263 | mbedtls_mpi Q; /*!< 2nd prime factor */ |
Hanno Becker | 1a59e79 | 2017-08-23 07:41:10 +0100 | [diff] [blame] | 264 | |
| 265 | #if !defined(MBEDTLS_RSA_NO_CRT) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 266 | mbedtls_mpi DP; /*!< D % (P - 1) */ |
| 267 | mbedtls_mpi DQ; /*!< D % (Q - 1) */ |
| 268 | mbedtls_mpi QP; /*!< 1 / (Q % P) */ |
Hanno Becker | 1a59e79 | 2017-08-23 07:41:10 +0100 | [diff] [blame] | 269 | #endif /* MBEDTLS_RSA_NO_CRT */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 270 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 271 | mbedtls_mpi RN; /*!< cached R^2 mod N */ |
Hanno Becker | 1a59e79 | 2017-08-23 07:41:10 +0100 | [diff] [blame] | 272 | |
| 273 | #if !defined(MBEDTLS_RSA_NO_CRT) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 274 | mbedtls_mpi RP; /*!< cached R^2 mod P */ |
| 275 | mbedtls_mpi RQ; /*!< cached R^2 mod Q */ |
Hanno Becker | 1a59e79 | 2017-08-23 07:41:10 +0100 | [diff] [blame] | 276 | #endif /* MBEDTLS_RSA_NO_CRT */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 277 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 278 | mbedtls_mpi Vi; /*!< cached blinding value */ |
| 279 | mbedtls_mpi Vf; /*!< cached un-blinding value */ |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 280 | |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 281 | int padding; /*!< \c MBEDTLS_RSA_PKCS_V15 for 1.5 padding and |
| 282 | \c MBEDTLS_RSA_PKCS_v21 for OAEP/PSS */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 283 | int hash_id; /*!< Hash identifier of mbedtls_md_type_t as |
| 284 | specified in the mbedtls_md.h header file |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 285 | for the EME-OAEP and EMSA-PSS |
| 286 | encoding */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 287 | #if defined(MBEDTLS_THREADING_C) |
| 288 | mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */ |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 289 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 290 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 291 | mbedtls_rsa_context; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 292 | |
Hanno Becker | ab37731 | 2017-08-23 16:24:51 +0100 | [diff] [blame] | 293 | #else |
| 294 | |
| 295 | #include "rsa_alt.h" |
| 296 | |
| 297 | #endif /* MBEDTLS_RSA_ALT */ |
| 298 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 299 | /** |
| 300 | * \brief Initialize an RSA context |
| 301 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 302 | * Note: Set padding to \c MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 303 | * encryption scheme and the RSASSA-PSS signature scheme. |
| 304 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 305 | * \param ctx RSA context to be initialized |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 306 | * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21 |
| 307 | * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 308 | * |
| 309 | * \note The hash_id parameter is actually ignored |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 310 | * when using \c MBEDTLS_RSA_PKCS_V15 padding. |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 311 | * |
| 312 | * \note Choice of padding mode is strictly enforced for private key |
| 313 | * operations, since there might be security concerns in |
| 314 | * mixing padding modes. For public key operations it's merely |
| 315 | * a default value, which can be overriden by calling specific |
| 316 | * rsa_rsaes_xxx or rsa_rsassa_xxx functions. |
| 317 | * |
| 318 | * \note The chosen hash is always used for OEAP encryption. |
| 319 | * For PSS signatures, it's always used for making signatures, |
| 320 | * but can be overriden (and always is, if set to |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 321 | * \c MBEDTLS_MD_NONE) for verifying them. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 322 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 323 | void mbedtls_rsa_init( mbedtls_rsa_context *ctx, |
Hanno Becker | 8fd5548 | 2017-08-23 14:07:48 +0100 | [diff] [blame] | 324 | int padding, |
| 325 | int hash_id); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 326 | |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 327 | /** |
| 328 | * \brief Import a set of core parameters into an RSA context |
| 329 | * |
| 330 | * \param ctx Initialized RSA context to store parameters |
| 331 | * \param N RSA modulus, or NULL |
| 332 | * \param P First prime factor of N, or NULL |
| 333 | * \param Q Second prime factor of N, or NULL |
| 334 | * \param D Private exponent, or NULL |
| 335 | * \param E Public exponent, or NULL |
| 336 | * |
| 337 | * \note This function can be called multiple times for successive |
| 338 | * imports if the parameters are not simultaneously present. |
| 339 | * Any sequence of calls to this function should be followed |
| 340 | * by a call to \c mbedtls_rsa_complete which will check |
| 341 | * and complete the provided information to a ready-for-use |
| 342 | * public or private RSA key. |
| 343 | * |
| 344 | * \return 0 if successful, non-zero error code on failure. |
| 345 | */ |
| 346 | int mbedtls_rsa_import( mbedtls_rsa_context *ctx, |
| 347 | const mbedtls_mpi *N, |
| 348 | const mbedtls_mpi *P, const mbedtls_mpi *Q, |
| 349 | const mbedtls_mpi *D, const mbedtls_mpi *E ); |
| 350 | |
| 351 | /** |
| 352 | * \brief Import core RSA parameters in raw big-endian |
| 353 | * binary format into an RSA context |
| 354 | * |
| 355 | * \param ctx Initialized RSA context to store parameters |
| 356 | * \param N RSA modulus, or NULL |
| 357 | * \param N_len Byte length of N, ignored if N == NULL |
| 358 | * \param P First prime factor of N, or NULL |
| 359 | * \param P_len Byte length of P, ignored if P == NULL |
| 360 | * \param Q Second prime factor of N, or NULL |
| 361 | * \param Q_len Byte length of Q, ignored if Q == NULL |
| 362 | * \param D Private exponent, or NULL |
| 363 | * \param D_len Byte length of D, ignored if D == NULL |
| 364 | * \param E Public exponent, or NULL |
| 365 | * \param E_len Byte length of E, ignored if E == NULL |
| 366 | * |
| 367 | * \note This function can be called multiple times for successive |
| 368 | * imports if the parameters are not simultaneously present. |
| 369 | * Any sequence of calls to this function should be followed |
| 370 | * by a call to \c mbedtls_rsa_complete which will check |
| 371 | * and complete the provided information to a ready-for-use |
| 372 | * public or private RSA key. |
| 373 | * |
| 374 | * \return 0 if successful, non-zero error code on failure. |
| 375 | */ |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 376 | int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx, |
| 377 | unsigned char *N, size_t N_len, |
| 378 | unsigned char *P, size_t P_len, |
| 379 | unsigned char *Q, size_t Q_len, |
| 380 | unsigned char *D, size_t D_len, |
| 381 | unsigned char *E, size_t E_len ); |
| 382 | |
| 383 | /** |
| 384 | * \brief Attempt to complete an RSA context from |
| 385 | * a set of imported core parameters. |
| 386 | * |
| 387 | * \param ctx Initialized RSA context to store parameters |
| 388 | * \param f_rng RNG function, |
| 389 | * \param p_rng RNG parameter |
| 390 | * |
Hanno Becker | ed20361 | 2017-09-29 13:34:25 +0100 | [diff] [blame] | 391 | * \note |
| 392 | * - To setup an RSA public key, precisely N and E |
| 393 | * must have been imported. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 394 | * |
Hanno Becker | ed20361 | 2017-09-29 13:34:25 +0100 | [diff] [blame] | 395 | * - To setup an RSA private key, enough information must be |
| 396 | * present for the other parameters to be efficiently derivable. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 397 | * |
Hanno Becker | ed20361 | 2017-09-29 13:34:25 +0100 | [diff] [blame] | 398 | * The default implementation supports the following: |
| 399 | * - Derive P, Q from N, D, E |
| 400 | * - Derive N, D from P, Q, E. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 401 | * |
Hanno Becker | ed20361 | 2017-09-29 13:34:25 +0100 | [diff] [blame] | 402 | * - Alternative implementations need not support these |
| 403 | * and may return \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA instead. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 404 | * |
| 405 | * \note The PRNG is used for probabilistic algorithms |
| 406 | * like the derivation of P, Q from N, D, E, as |
| 407 | * well as primality checks. |
| 408 | * |
Hanno Becker | 603b8c6 | 2017-08-25 11:03:07 +0100 | [diff] [blame] | 409 | * \return - 0 if successful. In this case, all imported core |
| 410 | * parameters are guaranteed to be sane, the RSA context |
| 411 | * has been fully setup and is ready for use. |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 412 | * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 413 | * derivations failed. |
| 414 | */ |
| 415 | int mbedtls_rsa_complete( mbedtls_rsa_context *ctx, |
| 416 | int (*f_rng)(void *, unsigned char *, size_t), |
| 417 | void *p_rng ); |
| 418 | |
| 419 | /** |
| 420 | * \brief Check if CRT-parameters match core parameters |
| 421 | * |
| 422 | * \param ctx Complete RSA private key context |
| 423 | * \param DP Private exponent modulo P-1, or NULL |
| 424 | * \param DQ Private exponent modulo Q-1, or NULL |
| 425 | * \param QP Modular inverse of Q modulo P, or NULL |
| 426 | * |
| 427 | * \return 0 if successful, testifying that the non-NULL optional |
| 428 | * parameters provided are in accordance with the core |
| 429 | * RSA parameters. Non-zero error code otherwise. |
| 430 | * |
| 431 | * \note This function performs in-place computations on the |
| 432 | * parameters DP, DQ and QP. If modification cannot be |
| 433 | * tolerated, you should make copies with mbedtls_mpi_copy |
| 434 | * before calling this function. |
| 435 | * |
| 436 | */ |
Hanno Becker | d363799 | 2017-08-25 07:55:03 +0100 | [diff] [blame] | 437 | int mbedtls_rsa_check_crt( const mbedtls_rsa_context *ctx, |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 438 | mbedtls_mpi *DP, |
| 439 | mbedtls_mpi *DQ, |
| 440 | mbedtls_mpi *QP ); |
| 441 | |
| 442 | /** |
| 443 | * \brief Export core parameters of an RSA key |
| 444 | * |
| 445 | * \param ctx Initialized RSA context |
| 446 | * \param N MPI to hold the RSA modulus, or NULL |
| 447 | * \param P MPI to hold the first prime factor of N, or NULL |
| 448 | * \param Q MPI to hold the second prime factor of N, or NULL |
| 449 | * \param D MPI to hold the private exponent, or NULL |
| 450 | * \param E MPI to hold the public exponent, or NULL |
| 451 | * |
Hanno Becker | ed20361 | 2017-09-29 13:34:25 +0100 | [diff] [blame] | 452 | * \return |
| 453 | * - 0 if successful. In this case, the non-NULL buffers |
| 454 | * pointed to by N, P, Q, D, E are fully written, with |
| 455 | * additional unused space filled leading by 0-bytes. |
| 456 | * - Non-zero return code otherwise. In particular, if |
| 457 | * exporting the requested parameters |
| 458 | * cannot be done because of a lack of functionality |
| 459 | * or because of security policies, the error code |
| 460 | * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned. |
| 461 | * In this case, the RSA context stays intact and can |
| 462 | * be continued to be used. |
Hanno Becker | 91c194d | 2017-09-29 12:50:12 +0100 | [diff] [blame] | 463 | * |
Hanno Becker | ed20361 | 2017-09-29 13:34:25 +0100 | [diff] [blame] | 464 | * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED |
Hanno Becker | 91c194d | 2017-09-29 12:50:12 +0100 | [diff] [blame] | 465 | * would be the following: Firstly, it might be that an |
| 466 | * alternative RSA implementation is in use which stores |
| 467 | * the key externally, and which either cannot or should not |
| 468 | * export it into RAM. Alternatively, an implementation |
| 469 | * (regardless of SW or HW) might not support deducing e.g. |
| 470 | * P, Q from N, D, E if the former are not part of the |
| 471 | * implementation. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 472 | * |
| 473 | */ |
| 474 | int mbedtls_rsa_export( const mbedtls_rsa_context *ctx, |
| 475 | mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q, |
| 476 | mbedtls_mpi *D, mbedtls_mpi *E ); |
| 477 | |
| 478 | /** |
| 479 | * \brief Export core parameters of an RSA key |
| 480 | * in raw big-endian binary format |
| 481 | * |
| 482 | * \param ctx Initialized RSA context |
| 483 | * \param N Byte array to store the RSA modulus, or NULL |
| 484 | * \param N_len Size of buffer for modulus |
| 485 | * \param P Byte array to hold the first prime factor of N, or NULL |
| 486 | * \param P_len Size of buffer for first prime factor |
| 487 | * \param Q Byte array to hold the second prime factor of N, or NULL |
| 488 | * \param Q_len Size of buffer for second prime factor |
| 489 | * \param D Byte array to hold the private exponent, or NULL |
| 490 | * \param D_len Size of buffer for private exponent |
| 491 | * \param E Byte array to hold the public exponent, or NULL |
| 492 | * \param E_len Size of buffer for public exponent |
| 493 | * |
| 494 | * \note The length fields are ignored if the corresponding |
| 495 | * buffer pointers are NULL. |
| 496 | * |
Hanno Becker | ed20361 | 2017-09-29 13:34:25 +0100 | [diff] [blame] | 497 | * \return |
| 498 | * - 0 if successful. In this case, the non-NULL buffers |
| 499 | * pointed to by N, P, Q, D, E are fully written, with |
| 500 | * additional unused space filled leading by 0-bytes. |
| 501 | * - Non-zero return code otherwise. In particular, if |
| 502 | * exporting the requested parameters |
| 503 | * cannot be done because of a lack of functionality |
| 504 | * or because of security policies, the error code |
| 505 | * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned. |
| 506 | * In this case, the RSA context stays intact and can |
| 507 | * be continued to be used. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 508 | * |
Hanno Becker | ed20361 | 2017-09-29 13:34:25 +0100 | [diff] [blame] | 509 | * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED |
Hanno Becker | 91c194d | 2017-09-29 12:50:12 +0100 | [diff] [blame] | 510 | * would be the following: Firstly, it might be that an |
| 511 | * alternative RSA implementation is in use which stores |
| 512 | * the key externally, and which either cannot or should not |
| 513 | * export it into RAM. Alternatively, an implementation |
| 514 | * (regardless of SW or HW) might not support deducing e.g. |
| 515 | * P, Q from N, D, E if the former are not part of the |
| 516 | * implementation. |
| 517 | * |
| 518 | * |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 519 | */ |
| 520 | int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx, |
| 521 | unsigned char *N, size_t N_len, |
| 522 | unsigned char *P, size_t P_len, |
| 523 | unsigned char *Q, size_t Q_len, |
| 524 | unsigned char *D, size_t D_len, |
| 525 | unsigned char *E, size_t E_len ); |
| 526 | |
| 527 | /** |
| 528 | * \brief Export CRT parameters of a private RSA key |
| 529 | * |
| 530 | * \param ctx Initialized RSA context |
| 531 | * \param DP MPI to hold D modulo P-1, or NULL |
| 532 | * \param DQ MPI to hold D modulo Q-1, or NULL |
| 533 | * \param QP MPI to hold modular inverse of Q modulo P, or NULL |
| 534 | * |
| 535 | * \return 0 if successful, non-zero error code otherwise. |
| 536 | * |
| 537 | * \note Alternative RSA implementations not using CRT-parameters |
| 538 | * internally can implement this function using based on |
| 539 | * \c mbedtls_rsa_deduce_opt. |
| 540 | * |
| 541 | */ |
| 542 | int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx, |
| 543 | mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP ); |
| 544 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 545 | /** |
Manuel Pégourié-Gonnard | 844a4c0 | 2014-03-10 21:55:35 +0100 | [diff] [blame] | 546 | * \brief Set padding for an already initialized RSA context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 547 | * See \c mbedtls_rsa_init() for details. |
Manuel Pégourié-Gonnard | 844a4c0 | 2014-03-10 21:55:35 +0100 | [diff] [blame] | 548 | * |
| 549 | * \param ctx RSA context to be set |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 550 | * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21 |
| 551 | * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier |
Manuel Pégourié-Gonnard | 844a4c0 | 2014-03-10 21:55:35 +0100 | [diff] [blame] | 552 | */ |
Hanno Becker | 8fd5548 | 2017-08-23 14:07:48 +0100 | [diff] [blame] | 553 | void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, |
| 554 | int hash_id); |
Manuel Pégourié-Gonnard | 844a4c0 | 2014-03-10 21:55:35 +0100 | [diff] [blame] | 555 | |
| 556 | /** |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 557 | * \brief Get length of RSA modulus in bytes |
| 558 | * |
| 559 | * \param ctx Initialized RSA context |
| 560 | * |
| 561 | * \return Length of RSA modulus, in bytes. |
| 562 | * |
| 563 | */ |
| 564 | size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx ); |
| 565 | |
| 566 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 567 | * \brief Generate an RSA keypair |
| 568 | * |
| 569 | * \param ctx RSA context that will hold the key |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 570 | * \param f_rng RNG function |
| 571 | * \param p_rng RNG parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 572 | * \param nbits size of the public key in bits |
| 573 | * \param exponent public exponent (e.g., 65537) |
| 574 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 575 | * \note mbedtls_rsa_init() must be called beforehand to setup |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 576 | * the RSA context. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 577 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 578 | * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 579 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 580 | int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx, |
Hanno Becker | 8fd5548 | 2017-08-23 14:07:48 +0100 | [diff] [blame] | 581 | int (*f_rng)(void *, unsigned char *, size_t), |
| 582 | void *p_rng, |
| 583 | unsigned int nbits, int exponent ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 584 | |
| 585 | /** |
Hanno Becker | 8fd5548 | 2017-08-23 14:07:48 +0100 | [diff] [blame] | 586 | * \brief Check if a context contains an RSA public key |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 587 | * |
| 588 | * \param ctx RSA context to be checked |
| 589 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 590 | * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 591 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 592 | int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 593 | |
| 594 | /** |
Hanno Becker | 8fd5548 | 2017-08-23 14:07:48 +0100 | [diff] [blame] | 595 | * \brief Check if a context contains a complete |
| 596 | * and valid RSA private key. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 597 | * |
| 598 | * \param ctx RSA context to be checked |
| 599 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 600 | * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 601 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 602 | int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 603 | |
| 604 | /** |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 605 | * \brief Check a public-private RSA key pair. |
| 606 | * Check each of the contexts, and make sure they match. |
| 607 | * |
| 608 | * \param pub RSA context holding the public key |
| 609 | * \param prv RSA context holding the private key |
| 610 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 611 | * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 612 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 613 | int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 614 | |
| 615 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 616 | * \brief Do an RSA public key operation |
| 617 | * |
| 618 | * \param ctx RSA context |
| 619 | * \param input input buffer |
| 620 | * \param output output buffer |
| 621 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 622 | * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 623 | * |
| 624 | * \note This function does NOT take care of message |
Brian J Murray | 2adecba | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 625 | * padding. Also, be sure to set input[0] = 0 or ensure that |
Paul Bakker | 619467a | 2009-03-28 23:26:51 +0000 | [diff] [blame] | 626 | * input is smaller than N. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 627 | * |
| 628 | * \note The input and output buffers must be large |
| 629 | * enough (eg. 128 bytes if RSA-1024 is used). |
| 630 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 631 | int mbedtls_rsa_public( mbedtls_rsa_context *ctx, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 632 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 633 | unsigned char *output ); |
| 634 | |
| 635 | /** |
| 636 | * \brief Do an RSA private key operation |
| 637 | * |
| 638 | * \param ctx RSA context |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 639 | * \param f_rng RNG function (Needed for blinding) |
| 640 | * \param p_rng RNG parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 641 | * \param input input buffer |
| 642 | * \param output output buffer |
| 643 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 644 | * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 645 | * |
| 646 | * \note The input and output buffers must be large |
| 647 | * enough (eg. 128 bytes if RSA-1024 is used). |
| 648 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 649 | int mbedtls_rsa_private( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 650 | int (*f_rng)(void *, unsigned char *, size_t), |
| 651 | void *p_rng, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 652 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 653 | unsigned char *output ); |
| 654 | |
| 655 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 656 | * \brief Generic wrapper to perform a PKCS#1 encryption using the |
| 657 | * mode from the context. Add the message padding, then do an |
| 658 | * RSA operation. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 659 | * |
| 660 | * \param ctx RSA context |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 661 | * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 662 | * and \c MBEDTLS_RSA_PRIVATE) |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 663 | * \param p_rng RNG parameter |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 664 | * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE |
Paul Bakker | 592457c | 2009-04-01 19:01:43 +0000 | [diff] [blame] | 665 | * \param ilen contains the plaintext length |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 666 | * \param input buffer holding the data to be encrypted |
| 667 | * \param output buffer that will hold the ciphertext |
| 668 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 669 | * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 670 | * |
| 671 | * \note The output buffer must be as large as the size |
| 672 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 673 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 674 | int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 675 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 676 | void *p_rng, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 677 | int mode, size_t ilen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 678 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 679 | unsigned char *output ); |
| 680 | |
| 681 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 682 | * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT) |
| 683 | * |
| 684 | * \param ctx RSA context |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 685 | * \param f_rng RNG function (Needed for padding and \c MBEDTLS_RSA_PRIVATE) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 686 | * \param p_rng RNG parameter |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 687 | * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 688 | * \param ilen contains the plaintext length |
| 689 | * \param input buffer holding the data to be encrypted |
| 690 | * \param output buffer that will hold the ciphertext |
| 691 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 692 | * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 693 | * |
| 694 | * \note The output buffer must be as large as the size |
| 695 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 696 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 697 | int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 698 | int (*f_rng)(void *, unsigned char *, size_t), |
| 699 | void *p_rng, |
| 700 | int mode, size_t ilen, |
| 701 | const unsigned char *input, |
| 702 | unsigned char *output ); |
| 703 | |
| 704 | /** |
| 705 | * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT) |
| 706 | * |
| 707 | * \param ctx RSA context |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 708 | * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 709 | * and \c MBEDTLS_RSA_PRIVATE) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 710 | * \param p_rng RNG parameter |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 711 | * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 712 | * \param label buffer holding the custom label to use |
| 713 | * \param label_len contains the label length |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 714 | * \param ilen contains the plaintext length |
| 715 | * \param input buffer holding the data to be encrypted |
| 716 | * \param output buffer that will hold the ciphertext |
| 717 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 718 | * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 719 | * |
| 720 | * \note The output buffer must be as large as the size |
| 721 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 722 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 723 | int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 724 | int (*f_rng)(void *, unsigned char *, size_t), |
| 725 | void *p_rng, |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 726 | int mode, |
| 727 | const unsigned char *label, size_t label_len, |
| 728 | size_t ilen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 729 | const unsigned char *input, |
| 730 | unsigned char *output ); |
| 731 | |
| 732 | /** |
| 733 | * \brief Generic wrapper to perform a PKCS#1 decryption using the |
| 734 | * mode from the context. Do an RSA operation, then remove |
| 735 | * the message padding |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 736 | * |
| 737 | * \param ctx RSA context |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 738 | * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 739 | * \param p_rng RNG parameter |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 740 | * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE |
Paul Bakker | 4d8ca70 | 2011-08-09 10:31:05 +0000 | [diff] [blame] | 741 | * \param olen will contain the plaintext length |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 742 | * \param input buffer holding the encrypted data |
| 743 | * \param output buffer that will hold the plaintext |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 744 | * \param output_max_len maximum length of the output buffer |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 745 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 746 | * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 747 | * |
Hanno Becker | 248ae6d | 2017-05-04 11:27:39 +0100 | [diff] [blame] | 748 | * \note The output buffer length \c output_max_len should be |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 749 | * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes |
Hanno Becker | 248ae6d | 2017-05-04 11:27:39 +0100 | [diff] [blame] | 750 | * if RSA-1024 is used) to be able to hold an arbitrary |
| 751 | * decrypted message. If it is not large enough to hold |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 752 | * the decryption of the particular ciphertext provided, |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 753 | * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE. |
Hanno Becker | 248ae6d | 2017-05-04 11:27:39 +0100 | [diff] [blame] | 754 | * |
| 755 | * \note The input buffer must be as large as the size |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 756 | * of \c ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 757 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 758 | int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 759 | int (*f_rng)(void *, unsigned char *, size_t), |
| 760 | void *p_rng, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 761 | int mode, size_t *olen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 762 | const unsigned char *input, |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 763 | unsigned char *output, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 764 | size_t output_max_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 765 | |
| 766 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 767 | * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT) |
| 768 | * |
| 769 | * \param ctx RSA context |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 770 | * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 771 | * \param p_rng RNG parameter |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 772 | * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 773 | * \param olen will contain the plaintext length |
| 774 | * \param input buffer holding the encrypted data |
| 775 | * \param output buffer that will hold the plaintext |
| 776 | * \param output_max_len maximum length of the output buffer |
| 777 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 778 | * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 779 | * |
Hanno Becker | 248ae6d | 2017-05-04 11:27:39 +0100 | [diff] [blame] | 780 | * \note The output buffer length \c output_max_len should be |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 781 | * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes |
Hanno Becker | 248ae6d | 2017-05-04 11:27:39 +0100 | [diff] [blame] | 782 | * if RSA-1024 is used) to be able to hold an arbitrary |
| 783 | * decrypted message. If it is not large enough to hold |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 784 | * the decryption of the particular ciphertext provided, |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 785 | * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE. |
Hanno Becker | 248ae6d | 2017-05-04 11:27:39 +0100 | [diff] [blame] | 786 | * |
| 787 | * \note The input buffer must be as large as the size |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 788 | * of \c ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 789 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 790 | int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 791 | int (*f_rng)(void *, unsigned char *, size_t), |
| 792 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 793 | int mode, size_t *olen, |
| 794 | const unsigned char *input, |
| 795 | unsigned char *output, |
| 796 | size_t output_max_len ); |
| 797 | |
| 798 | /** |
| 799 | * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT) |
| 800 | * |
| 801 | * \param ctx RSA context |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 802 | * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 803 | * \param p_rng RNG parameter |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 804 | * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 805 | * \param label buffer holding the custom label to use |
| 806 | * \param label_len contains the label length |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 807 | * \param olen will contain the plaintext length |
| 808 | * \param input buffer holding the encrypted data |
| 809 | * \param output buffer that will hold the plaintext |
| 810 | * \param output_max_len maximum length of the output buffer |
| 811 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 812 | * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 813 | * |
Hanno Becker | 248ae6d | 2017-05-04 11:27:39 +0100 | [diff] [blame] | 814 | * \note The output buffer length \c output_max_len should be |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 815 | * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes |
Hanno Becker | 248ae6d | 2017-05-04 11:27:39 +0100 | [diff] [blame] | 816 | * if RSA-1024 is used) to be able to hold an arbitrary |
| 817 | * decrypted message. If it is not large enough to hold |
Hanno Becker | 8fd5548 | 2017-08-23 14:07:48 +0100 | [diff] [blame] | 818 | * the decryption of the particular ciphertext provided, |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 819 | * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE. |
Hanno Becker | 248ae6d | 2017-05-04 11:27:39 +0100 | [diff] [blame] | 820 | * |
Hanno Becker | 8fd5548 | 2017-08-23 14:07:48 +0100 | [diff] [blame] | 821 | * \note The input buffer must be as large as the size |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 822 | * of \c ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 823 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 824 | int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 825 | int (*f_rng)(void *, unsigned char *, size_t), |
| 826 | void *p_rng, |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 827 | int mode, |
| 828 | const unsigned char *label, size_t label_len, |
| 829 | size_t *olen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 830 | const unsigned char *input, |
| 831 | unsigned char *output, |
| 832 | size_t output_max_len ); |
| 833 | |
| 834 | /** |
| 835 | * \brief Generic wrapper to perform a PKCS#1 signature using the |
| 836 | * mode from the context. Do a private RSA operation to sign |
| 837 | * a message digest |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 838 | * |
| 839 | * \param ctx RSA context |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 840 | * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 841 | * \c MBEDTLS_RSA_PRIVATE) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 842 | * \param p_rng RNG parameter |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 843 | * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE |
| 844 | * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for |
| 845 | * signing raw data) |
| 846 | * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 847 | * \param hash buffer holding the message digest |
| 848 | * \param sig buffer that will hold the ciphertext |
| 849 | * |
| 850 | * \return 0 if the signing operation was successful, |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 851 | * or an \c MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 852 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 853 | * \note The \c sig buffer must be as large as the size |
| 854 | * of \c ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 855 | * |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 856 | * \note In case of PKCS#1 v2.1 encoding, see comments on |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 857 | * \note \c mbedtls_rsa_rsassa_pss_sign() for details on |
| 858 | * \c md_alg and \c hash_id. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 859 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 860 | int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 861 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 862 | void *p_rng, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 863 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 864 | mbedtls_md_type_t md_alg, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 865 | unsigned int hashlen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 866 | const unsigned char *hash, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 867 | unsigned char *sig ); |
| 868 | |
| 869 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 870 | * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN) |
| 871 | * |
| 872 | * \param ctx RSA context |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 873 | * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 874 | * \param p_rng RNG parameter |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 875 | * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE |
| 876 | * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE |
| 877 | * for signing raw data) |
| 878 | * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 879 | * \param hash buffer holding the message digest |
| 880 | * \param sig buffer that will hold the ciphertext |
| 881 | * |
| 882 | * \return 0 if the signing operation was successful, |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 883 | * or an \c MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 884 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 885 | * \note The \c sig buffer must be as large as the size |
| 886 | * of \c ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 887 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 888 | int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 889 | int (*f_rng)(void *, unsigned char *, size_t), |
| 890 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 891 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 892 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 893 | unsigned int hashlen, |
| 894 | const unsigned char *hash, |
| 895 | unsigned char *sig ); |
| 896 | |
| 897 | /** |
| 898 | * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN) |
| 899 | * |
| 900 | * \param ctx RSA context |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 901 | * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 902 | * \c MBEDTLS_RSA_PRIVATE) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 903 | * \param p_rng RNG parameter |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 904 | * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE |
| 905 | * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE |
| 906 | * for signing raw data) |
| 907 | * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 908 | * \param hash buffer holding the message digest |
| 909 | * \param sig buffer that will hold the ciphertext |
| 910 | * |
| 911 | * \return 0 if the signing operation was successful, |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 912 | * or an \c MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 913 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 914 | * \note The \c sig buffer must be as large as the size |
| 915 | * of \c ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 916 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 917 | * \note The \c hash_id in the RSA context is the one used for the |
| 918 | * encoding. \c md_alg in the function call is the type of hash |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 919 | * that is encoded. According to RFC 3447 it is advised to |
| 920 | * keep both hashes the same. |
| 921 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 922 | int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 923 | int (*f_rng)(void *, unsigned char *, size_t), |
| 924 | void *p_rng, |
| 925 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 926 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 927 | unsigned int hashlen, |
| 928 | const unsigned char *hash, |
| 929 | unsigned char *sig ); |
| 930 | |
| 931 | /** |
| 932 | * \brief Generic wrapper to perform a PKCS#1 verification using the |
| 933 | * mode from the context. Do a public RSA operation and check |
| 934 | * the message digest |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 935 | * |
| 936 | * \param ctx points to an RSA public key |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 937 | * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 938 | * \param p_rng RNG parameter |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 939 | * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE |
| 940 | * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data) |
| 941 | * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 942 | * \param hash buffer holding the message digest |
| 943 | * \param sig buffer holding the ciphertext |
| 944 | * |
| 945 | * \return 0 if the verify operation was successful, |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 946 | * or an \c MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 947 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 948 | * \note The \c sig buffer must be as large as the size |
| 949 | * of \c ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 950 | * |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 951 | * \note In case of PKCS#1 v2.1 encoding, see comments on |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 952 | * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 953 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 954 | int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 955 | int (*f_rng)(void *, unsigned char *, size_t), |
| 956 | void *p_rng, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 957 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 958 | mbedtls_md_type_t md_alg, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 959 | unsigned int hashlen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 960 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 961 | const unsigned char *sig ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 962 | |
| 963 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 964 | * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY) |
| 965 | * |
| 966 | * \param ctx points to an RSA public key |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 967 | * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 968 | * \param p_rng RNG parameter |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 969 | * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE |
| 970 | * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE |
| 971 | * for signing raw data) |
| 972 | * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 973 | * \param hash buffer holding the message digest |
| 974 | * \param sig buffer holding the ciphertext |
| 975 | * |
| 976 | * \return 0 if the verify operation was successful, |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 977 | * or an \c MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 978 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 979 | * \note The \c sig buffer must be as large as the size |
| 980 | * of \c ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 981 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 982 | int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 983 | int (*f_rng)(void *, unsigned char *, size_t), |
| 984 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 985 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 986 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 987 | unsigned int hashlen, |
| 988 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 989 | const unsigned char *sig ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 990 | |
| 991 | /** |
| 992 | * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY) |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 993 | * (This is the "simple" version.) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 994 | * |
| 995 | * \param ctx points to an RSA public key |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 996 | * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 997 | * \param p_rng RNG parameter |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 998 | * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE |
| 999 | * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data) |
| 1000 | * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1001 | * \param hash buffer holding the message digest |
| 1002 | * \param sig buffer holding the ciphertext |
| 1003 | * |
| 1004 | * \return 0 if the verify operation was successful, |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 1005 | * or an \c MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1006 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 1007 | * \note The \c sig buffer must be as large as the size |
| 1008 | * of \c ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1009 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 1010 | * \note The \c hash_id in the RSA context is the one used for the |
| 1011 | * verification. \c md_alg in the function call is the type of |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1012 | * hash that is verified. According to RFC 3447 it is advised to |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 1013 | * keep both hashes the same. If \c hash_id in the RSA context is |
| 1014 | * unset, the \c md_alg from the function call is used. |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1015 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1016 | int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1017 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1018 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1019 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1020 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1021 | unsigned int hashlen, |
| 1022 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 1023 | const unsigned char *sig ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1024 | |
| 1025 | /** |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1026 | * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY) |
| 1027 | * (This is the version with "full" options.) |
| 1028 | * |
| 1029 | * \param ctx points to an RSA public key |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 1030 | * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE) |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1031 | * \param p_rng RNG parameter |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 1032 | * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE |
| 1033 | * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data) |
| 1034 | * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only) |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1035 | * \param hash buffer holding the message digest |
| 1036 | * \param mgf1_hash_id message digest used for mask generation |
| 1037 | * \param expected_salt_len Length of the salt used in padding, use |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 1038 | * \c MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1039 | * \param sig buffer holding the ciphertext |
| 1040 | * |
| 1041 | * \return 0 if the verify operation was successful, |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 1042 | * or an \c MBEDTLS_ERR_RSA_XXX error code |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1043 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 1044 | * \note The \c sig buffer must be as large as the size |
| 1045 | * of \c ctx->N (eg. 128 bytes if RSA-1024 is used). |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1046 | * |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 1047 | * \note The \c hash_id in the RSA context is ignored. |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1048 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1049 | int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1050 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1051 | void *p_rng, |
| 1052 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1053 | mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1054 | unsigned int hashlen, |
| 1055 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1056 | mbedtls_md_type_t mgf1_hash_id, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1057 | int expected_salt_len, |
| 1058 | const unsigned char *sig ); |
| 1059 | |
| 1060 | /** |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 1061 | * \brief Copy the components of an RSA context |
| 1062 | * |
| 1063 | * \param dst Destination context |
| 1064 | * \param src Source context |
| 1065 | * |
Manuel Pégourié-Gonnard | 81abefd | 2015-05-29 12:53:47 +0200 | [diff] [blame] | 1066 | * \return 0 on success, |
Hanno Becker | 4b2f691 | 2017-09-29 13:34:55 +0100 | [diff] [blame] | 1067 | * \c MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 1068 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1069 | int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src ); |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 1070 | |
| 1071 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1072 | * \brief Free the components of an RSA key |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 1073 | * |
| 1074 | * \param ctx RSA Context to free |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1075 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1076 | void mbedtls_rsa_free( mbedtls_rsa_context *ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1077 | |
| 1078 | /** |
| 1079 | * \brief Checkup routine |
| 1080 | * |
| 1081 | * \return 0 if successful, or 1 if the test failed |
| 1082 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1083 | int mbedtls_rsa_self_test( int verbose ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1084 | |
| 1085 | #ifdef __cplusplus |
| 1086 | } |
| 1087 | #endif |
| 1088 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1089 | #endif /* MBEDTLS_RSA_C */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1090 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1091 | #endif /* rsa.h */ |