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