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