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