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