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