blob: 188c37cf3aa9291c5fa8c353321d350e6d18860a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Rose Zadik21e29262018-04-17 14:08:56 +01004 * \brief This file provides an API for the RSA public-key cryptosystem.
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
Rose Zadike8b5b992018-03-27 12:19:47 +01006 * The RSA public-key cryptosystem is defined in <em>Public-Key
7 * Cryptography Standards (PKCS) #1 v1.5: RSA Encryption</em>
Darryl Green11999bb2018-03-13 15:22:58 +00008 * and <em>Public-Key Cryptography Standards (PKCS) #1 v2.1:
Rose Zadike8b5b992018-03-27 12:19:47 +01009 * RSA Cryptography Specifications</em>.
Rose Zadik042e97f2018-01-26 16:35:10 +000010 *
Darryl Greena40a1012018-01-05 15:33:17 +000011 */
12/*
Bence Szépkútia2947ac2020-08-19 16:37:36 +020013 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +020014 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
15 *
16 * This file is provided under the Apache License 2.0, or the
17 * GNU General Public License v2.0 or later.
18 *
19 * **********
20 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020021 *
22 * Licensed under the Apache License, Version 2.0 (the "License"); you may
23 * not use this file except in compliance with the License.
24 * You may obtain a copy of the License at
25 *
26 * http://www.apache.org/licenses/LICENSE-2.0
27 *
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
30 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000033 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020034 * **********
35 *
36 * **********
37 * GNU General Public License v2.0 or later:
38 *
39 * This program is free software; you can redistribute it and/or modify
40 * it under the terms of the GNU General Public License as published by
41 * the Free Software Foundation; either version 2 of the License, or
42 * (at your option) any later version.
43 *
44 * This program is distributed in the hope that it will be useful,
45 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47 * GNU General Public License for more details.
48 *
49 * You should have received a copy of the GNU General Public License along
50 * with this program; if not, write to the Free Software Foundation, Inc.,
51 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
52 *
53 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#ifndef MBEDTLS_RSA_H
56#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020059#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020060#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020062#endif
Paul Bakkered27a042013-04-18 22:46:23 +020063
Paul Bakker314052f2011-08-15 09:07:52 +000064#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020065#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_THREADING_C)
Paul Bakkerc9965dc2013-09-29 14:58:17 +020068#include "threading.h"
69#endif
70
Paul Bakker13e2dfe2009-07-28 07:18:38 +000071/*
72 * RSA Error codes
73 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
75#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
76#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Rose Zadik042e97f2018-01-26 16:35:10 +000077#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the validity check of the library. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
79#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
80#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
81#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
82#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030083
84/* MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is deprecated and should not be used.
85 */
Rose Zadik042e97f2018-01-26 16:35:10 +000086#define MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION -0x4500 /**< The implementation does not offer the requested operation, for example, because of security violations or lack of functionality. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030087
88/* MBEDTLS_ERR_RSA_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010089#define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580 /**< RSA hardware accelerator failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000090
91/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020092 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000093 */
Rose Zadik042e97f2018-01-26 16:35:10 +000094#define MBEDTLS_RSA_PUBLIC 0 /**< Request private key operation. */
95#define MBEDTLS_RSA_PRIVATE 1 /**< Request public key operation. */
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Rose Zadike8b5b992018-03-27 12:19:47 +010097#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
98#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Rose Zadik042e97f2018-01-26 16:35:10 +0000100#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
101#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200104
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +0200105/*
106 * The above constants may be used even if the RSA module is compile out,
107 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
108 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +0100109
Paul Bakker407a0da2013-06-27 14:29:21 +0200110#ifdef __cplusplus
111extern "C" {
112#endif
113
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200114#if !defined(MBEDTLS_RSA_ALT)
115// Regular implementation
116//
117
Paul Bakker5121ce52009-01-03 21:22:43 +0000118/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000119 * \brief The RSA context structure.
Hanno Becker5063cd22017-09-29 11:49:12 +0100120 *
121 * \note Direct manipulation of the members of this structure
Rose Zadik042e97f2018-01-26 16:35:10 +0000122 * is deprecated. All manipulation should instead be done through
123 * the public interface functions.
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200125typedef struct mbedtls_rsa_context
Paul Bakker5121ce52009-01-03 21:22:43 +0000126{
Rose Zadik042e97f2018-01-26 16:35:10 +0000127 int ver; /*!< Always 0.*/
128 size_t len; /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Rose Zadike8b5b992018-03-27 12:19:47 +0100130 mbedtls_mpi N; /*!< The public modulus. */
131 mbedtls_mpi E; /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Rose Zadike8b5b992018-03-27 12:19:47 +0100133 mbedtls_mpi D; /*!< The private exponent. */
134 mbedtls_mpi P; /*!< The first prime factor. */
135 mbedtls_mpi Q; /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100136
Rose Zadikf2ec2882018-04-17 10:27:25 +0100137 mbedtls_mpi DP; /*!< <code>D % (P - 1)</code>. */
138 mbedtls_mpi DQ; /*!< <code>D % (Q - 1)</code>. */
139 mbedtls_mpi QP; /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
Rose Zadikf2ec2882018-04-17 10:27:25 +0100141 mbedtls_mpi RN; /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
Rose Zadikf2ec2882018-04-17 10:27:25 +0100143 mbedtls_mpi RP; /*!< cached <code>R^2 mod P</code>. */
144 mbedtls_mpi RQ; /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200145
Rose Zadike8b5b992018-03-27 12:19:47 +0100146 mbedtls_mpi Vi; /*!< The cached blinding value. */
147 mbedtls_mpi Vf; /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000148
Rose Zadik042e97f2018-01-26 16:35:10 +0000149 int padding; /*!< Selects padding mode:
150 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
151 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
152 int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
153 as specified in md.h for use in the MGF
154 mask generating function used in the
155 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_THREADING_C)
Rose Zadik042e97f2018-01-26 16:35:10 +0000157 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200158#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000159}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200162#else /* MBEDTLS_RSA_ALT */
163#include "rsa_alt.h"
164#endif /* MBEDTLS_RSA_ALT */
165
Paul Bakker5121ce52009-01-03 21:22:43 +0000166/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000167 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000169 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000170 * encryption scheme and the RSASSA-PSS signature scheme.
171 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000172 * \note The \p hash_id parameter is ignored when using
173 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200174 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000175 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200176 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000177 * mixing padding modes. For public key operations it is
Antonin Décimod5f47592019-01-23 15:24:37 +0100178 * a default value, which can be overridden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000179 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200180 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000181 * \note The hash selected in \p hash_id is always used for OEAP
182 * encryption. For PSS signatures, it is always used for
Antonin Décimod5f47592019-01-23 15:24:37 +0100183 * making signatures, but can be overridden for verifying them.
184 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100185 *
Hanno Becker9a467772018-12-13 09:54:59 +0000186 * \param ctx The RSA context to initialize. This must not be \c NULL.
187 * \param padding The padding mode to use. This must be either
188 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Hanno Becker385ce912018-12-13 18:33:12 +0000189 * \param hash_id The hash identifier of ::mbedtls_md_type_t type, if
Hanno Becker9a467772018-12-13 09:54:59 +0000190 * \p padding is #MBEDTLS_RSA_PKCS_V21. It is unused
191 * otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100194 int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000195 int hash_id );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
197/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000198 * \brief This function imports a set of core parameters into an
199 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100200 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100201 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000202 * imports, if the parameters are not simultaneously present.
203 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100204 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000205 * by a call to mbedtls_rsa_complete(), which checks and
206 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100207 * public or private RSA key.
208 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000209 * \note See mbedtls_rsa_complete() for more information on which
210 * parameters are necessary to set up a private or public
211 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100212 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100213 * \note The imported parameters are copied and need not be preserved
214 * for the lifetime of the RSA context being set up.
215 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100216 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000217 * \param N The RSA modulus. This may be \c NULL.
218 * \param P The first prime factor of \p N. This may be \c NULL.
219 * \param Q The second prime factor of \p N. This may be \c NULL.
220 * \param D The private exponent. This may be \c NULL.
221 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100222 *
223 * \return \c 0 on success.
224 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100225 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100226int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
227 const mbedtls_mpi *N,
228 const mbedtls_mpi *P, const mbedtls_mpi *Q,
229 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100230
231/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000232 * \brief This function imports core RSA parameters, in raw big-endian
233 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100235 * \note This function can be called multiple times for successive
236 * imports, if the parameters are not simultaneously present.
237 *
238 * Any sequence of calls to this function should be followed
239 * by a call to mbedtls_rsa_complete(), which checks and
240 * completes the provided information to a ready-for-use
241 * public or private RSA key.
242 *
243 * \note See mbedtls_rsa_complete() for more information on which
244 * parameters are necessary to set up a private or public
245 * RSA key.
246 *
247 * \note The imported parameters are copied and need not be preserved
248 * for the lifetime of the RSA context being set up.
249 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000250 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000251 * \param N The RSA modulus. This may be \c NULL.
252 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
253 * \param P The first prime factor of \p N. This may be \c NULL.
254 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
255 * \param Q The second prime factor of \p N. This may be \c NULL.
256 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
257 * \param D The private exponent. This may be \c NULL.
258 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
259 * \param E The public exponent. This may be \c NULL.
260 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000261 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100262 * \return \c 0 on success.
263 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100264 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100265int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100266 unsigned char const *N, size_t N_len,
267 unsigned char const *P, size_t P_len,
268 unsigned char const *Q, size_t Q_len,
269 unsigned char const *D, size_t D_len,
270 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100271
272/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000273 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100274 * a set of imported core parameters.
275 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000276 * To setup an RSA public key, precisely \p N and \p E
277 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100278 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000279 * To setup an RSA private key, sufficient information must
280 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100281 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000282 * The default implementation supports the following:
283 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
284 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
285 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100286 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000287 * If this function runs successfully, it guarantees that
288 * the RSA context can be used for RSA operations without
289 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100290 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100291 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000292 * for the imported parameters. In particular, parameters that
293 * are not needed by the implementation might be silently
294 * discarded and left unchecked. To check the consistency
295 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100296 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100297 * \param ctx The initialized RSA context holding imported parameters.
298 *
299 * \return \c 0 on success.
300 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
301 * failed.
302 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100303 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100304int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100305
306/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000307 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100308 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000309 * If this function runs successfully, the non-NULL buffers
310 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
311 * written, with additional unused space filled leading by
312 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100313 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000314 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300315 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000316 * <li>An alternative RSA implementation is in use, which
317 * stores the key externally, and either cannot or should
318 * not export it into RAM.</li>
319 * <li>A SW or HW implementation might not support a certain
320 * deduction. For example, \p P, \p Q from \p N, \p D,
321 * and \p E if the former are not part of the
322 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100323 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000324 * If the function fails due to an unsupported operation,
325 * the RSA context stays intact and remains usable.
326 *
327 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000328 * \param N The MPI to hold the RSA modulus.
329 * This may be \c NULL if this field need not be exported.
330 * \param P The MPI to hold the first prime factor of \p N.
331 * This may be \c NULL if this field need not be exported.
332 * \param Q The MPI to hold the second prime factor of \p N.
333 * This may be \c NULL if this field need not be exported.
334 * \param D The MPI to hold the private exponent.
335 * This may be \c NULL if this field need not be exported.
336 * \param E The MPI to hold the public exponent.
337 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000338 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100339 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300340 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000341 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100342 * functionality or because of security policies.
343 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100344 *
345 */
346int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
347 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
348 mbedtls_mpi *D, mbedtls_mpi *E );
349
350/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000351 * \brief This function exports core parameters of an RSA key
352 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100353 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000354 * If this function runs successfully, the non-NULL buffers
355 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
356 * written, with additional unused space filled leading by
357 * zero Bytes.
358 *
359 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300360 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000361 * <li>An alternative RSA implementation is in use, which
362 * stores the key externally, and either cannot or should
363 * not export it into RAM.</li>
364 * <li>A SW or HW implementation might not support a certain
365 * deduction. For example, \p P, \p Q from \p N, \p D,
366 * and \p E if the former are not part of the
367 * implementation.</li></ul>
368 * If the function fails due to an unsupported operation,
369 * the RSA context stays intact and remains usable.
370 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100371 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100372 * buffer pointers are NULL.
373 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000374 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000375 * \param N The Byte array to store the RSA modulus,
376 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000377 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000378 * \param P The Byte array to hold the first prime factor of \p N,
379 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000380 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000381 * \param Q The Byte array to hold the second prime factor of \p N,
382 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000383 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000384 * \param D The Byte array to hold the private exponent,
385 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000386 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000387 * \param E The Byte array to hold the public exponent,
388 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000389 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100390 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100391 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300392 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000393 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100394 * functionality or because of security policies.
395 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100396 */
397int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
398 unsigned char *N, size_t N_len,
399 unsigned char *P, size_t P_len,
400 unsigned char *Q, size_t Q_len,
401 unsigned char *D, size_t D_len,
402 unsigned char *E, size_t E_len );
403
404/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000405 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100406 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100407 * \note Alternative RSA implementations not using CRT-parameters
408 * internally can implement this function based on
409 * mbedtls_rsa_deduce_opt().
410 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000411 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000412 * \param DP The MPI to hold \c D modulo `P-1`,
413 * or \c NULL if it need not be exported.
414 * \param DQ The MPI to hold \c D modulo `Q-1`,
415 * or \c NULL if it need not be exported.
416 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
417 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100418 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100419 * \return \c 0 on success.
420 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100421 *
422 */
423int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
424 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
425
Paul Bakker5121ce52009-01-03 21:22:43 +0000426/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000427 * \brief This function sets padding for an already initialized RSA
428 * context. See mbedtls_rsa_init() for details.
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 *
Hanno Becker9a467772018-12-13 09:54:59 +0000430 * \param ctx The initialized RSA context to be configured.
431 * \param padding The padding mode to use. This must be either
432 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Rose Zadik042e97f2018-01-26 16:35:10 +0000433 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Paul Bakker40e46942009-01-03 21:51:57 +0000434 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100435void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000436 int hash_id );
Paul Bakker21eb2802010-08-16 11:10:02 +0000437
Paul Bakkera3d195c2011-11-27 21:07:34 +0000438/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000439 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100440 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000441 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100442 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000443 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100444 *
445 */
446size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
447
448/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000449 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000450 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000451 * \note mbedtls_rsa_init() must be called before this function,
452 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 *
Hanno Becker9a467772018-12-13 09:54:59 +0000454 * \param ctx The initialized RSA context used to hold the key.
455 * \param f_rng The RNG function to be used for key generation.
456 * This must not be \c NULL.
457 * \param p_rng The RNG context to be passed to \p f_rng.
458 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100459 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000460 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000461 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100462 *
463 * \return \c 0 on success.
464 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100467 int (*f_rng)(void *, unsigned char *, size_t),
468 void *p_rng,
469 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
471/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000472 * \brief This function checks if a context contains at least an RSA
473 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000474 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000475 * If the function runs successfully, it is guaranteed that
476 * enough information is present to perform an RSA public key
477 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 *
Hanno Becker9a467772018-12-13 09:54:59 +0000479 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000480 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100481 * \return \c 0 on success.
482 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100483 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
487/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000488 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100489 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000490 *
Hanno Becker68767a62017-10-17 10:13:31 +0100491 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000492 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100493 * on the given context, but that the various parameters are
494 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000495 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100496 *
497 * \warning This function should catch accidental misconfigurations
498 * like swapping of parameters, but it cannot establish full
499 * trust in neither the quality nor the consistency of the key
500 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000501 * <ul><li>Consistency: Imported parameters that are irrelevant
502 * for the implementation might be silently dropped. If dropped,
503 * the current function does not have access to them,
504 * and therefore cannot check them. See mbedtls_rsa_complete().
505 * If you want to check the consistency of the entire
506 * content of an PKCS1-encoded RSA private key, for example, you
507 * should use mbedtls_rsa_validate_params() before setting
508 * up the RSA context.
509 * Additionally, if the implementation performs empirical checks,
510 * these checks substantiate but do not guarantee consistency.</li>
511 * <li>Quality: This function is not expected to perform
512 * extended quality assessments like checking that the prime
513 * factors are safe. Additionally, it is the responsibility of the
514 * user to ensure the trustworthiness of the source of his RSA
515 * parameters, which goes beyond what is effectively checkable
516 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100517 *
Hanno Becker9a467772018-12-13 09:54:59 +0000518 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100519 *
520 * \return \c 0 on success.
521 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000526 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100527 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000528 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100529 *
Hanno Becker9a467772018-12-13 09:54:59 +0000530 * \param pub The initialized RSA context holding the public key.
531 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000532 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100533 * \return \c 0 on success.
534 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100535 */
Hanno Becker98838b02017-10-02 13:16:10 +0100536int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
537 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100538
539/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000540 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000541 *
Hanno Becker9a467772018-12-13 09:54:59 +0000542 * \param ctx The initialized RSA context to use.
543 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000544 * of length \c ctx->len Bytes. For example, \c 256 Bytes
545 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000546 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000547 * of length \c ctx->len Bytes. For example, \c 256 Bytes
548 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000549 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000550 * \note This function does not handle message padding.
551 *
552 * \note Make sure to set \p input[0] = 0 or ensure that
553 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000554 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100555 * \return \c 0 on success.
556 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000559 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 unsigned char *output );
561
562/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000563 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 *
Hanno Becker24120612017-10-26 11:53:35 +0100565 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100566 *
567 * \note If blinding is used, both the base of exponentation
Hanno Becker24120612017-10-26 11:53:35 +0100568 * and the exponent are blinded, providing protection
569 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100570 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100571 * \warning It is deprecated and a security risk to not provide
572 * a PRNG here and thereby prevent the use of blinding.
573 * Future versions of the library may enforce the presence
574 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100575 *
Hanno Becker9a467772018-12-13 09:54:59 +0000576 * \param ctx The initialized RSA context to use.
577 * \param f_rng The RNG function, used for blinding. It is discouraged
578 * and deprecated to pass \c NULL here, in which case
579 * blinding will be omitted.
580 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
581 * if \p f_rng is \c NULL or if \p f_rng doesn't need a context.
582 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000583 * of length \c ctx->len Bytes. For example, \c 256 Bytes
584 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000585 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000586 * of length \c ctx->len Bytes. For example, \c 256 Bytes
587 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100588 *
589 * \return \c 0 on success.
590 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
591 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200594 int (*f_rng)(void *, unsigned char *, size_t),
595 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000596 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000597 unsigned char *output );
598
599/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000600 * \brief This function adds the message padding, then performs an RSA
601 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000602 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000603 * It is the generic wrapper for performing a PKCS#1 encryption
604 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100606 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000607 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
608 * are likely to remove the \p mode argument and have it
609 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100610 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100611 * \note Alternative implementations of RSA need not support
612 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300613 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100614 *
Hanno Becker9a467772018-12-13 09:54:59 +0000615 * \param ctx The initialized RSA context to use.
Hanno Becker974ca0d2018-12-18 18:03:24 +0000616 * \param f_rng The RNG to use. It is mandatory for PKCS#1 v2.1 padding
617 * encoding, and for PKCS#1 v1.5 padding encoding when used
618 * with \p mode set to #MBEDTLS_RSA_PUBLIC. For PKCS#1 v1.5
619 * padding encoding and \p mode set to #MBEDTLS_RSA_PRIVATE,
620 * it is used for blinding and should be provided in this
621 * case; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000622 * \param p_rng The RNG context to be passed to \p f_rng. May be
623 * \c NULL if \p f_rng is \c NULL or if \p f_rng doesn't
624 * need a context argument.
625 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000626 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000627 * \param ilen The length of the plaintext in Bytes.
628 * \param input The input data to encrypt. This must be a readable
Hanno Becker2f660d02018-12-18 17:04:59 +0000629 * buffer of size \p ilen Bytes. This must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000630 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000631 * of length \c ctx->len Bytes. For example, \c 256 Bytes
632 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100633 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100634 * \return \c 0 on success.
635 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000638 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000639 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000640 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000641 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 unsigned char *output );
643
644/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000645 * \brief This function performs a PKCS#1 v1.5 encryption operation
646 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100647 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100648 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000649 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
650 * are likely to remove the \p mode argument and have it
651 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100652 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100653 * \note Alternative implementations of RSA need not support
654 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300655 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100656 *
Hanno Becker9a467772018-12-13 09:54:59 +0000657 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000658 * \param f_rng The RNG function to use. It is needed for padding generation
659 * if \p mode is #MBEDTLS_RSA_PUBLIC. If \p mode is
660 * #MBEDTLS_RSA_PRIVATE (discouraged), it is used for
661 * blinding and should be provided; see mbedtls_rsa_private().
Hanno Becker9a467772018-12-13 09:54:59 +0000662 * \param p_rng The RNG context to be passed to \p f_rng. This may
663 * be \c NULL if \p f_rng is \c NULL or if \p f_rng
664 * doesn't need a context argument.
665 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000666 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000667 * \param ilen The length of the plaintext in Bytes.
668 * \param input The input data to encrypt. This must be a readable
Hanno Becker2f660d02018-12-18 17:04:59 +0000669 * buffer of size \p ilen Bytes. This must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000670 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000671 * of length \c ctx->len Bytes. For example, \c 256 Bytes
672 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100673 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100674 * \return \c 0 on success.
675 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100676 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100678 int (*f_rng)(void *, unsigned char *, size_t),
679 void *p_rng,
680 int mode, size_t ilen,
681 const unsigned char *input,
682 unsigned char *output );
683
684/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000685 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
686 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100687 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100688 * \note The output buffer must be as large as the size
689 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
690 *
691 * \deprecated It is deprecated and discouraged to call this function
692 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
693 * are likely to remove the \p mode argument and have it
694 * implicitly set to #MBEDTLS_RSA_PUBLIC.
695 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100696 * \note Alternative implementations of RSA need not support
697 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300698 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100699 *
Hanno Becker9a467772018-12-13 09:54:59 +0000700 * \param ctx The initnialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000701 * \param f_rng The RNG function to use. This is needed for padding
702 * generation and must be provided.
Hanno Becker9a467772018-12-13 09:54:59 +0000703 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000704 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000705 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000706 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000707 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000708 * This must be a readable buffer of length \p label_len
709 * Bytes. It may be \c NULL if \p label_len is \c 0.
710 * \param label_len The length of the label in Bytes.
711 * \param ilen The length of the plaintext buffer \p input in Bytes.
712 * \param input The input data to encrypt. This must be a readable
Hanno Becker2f660d02018-12-18 17:04:59 +0000713 * buffer of size \p ilen Bytes. This must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000714 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000715 * of length \c ctx->len Bytes. For example, \c 256 Bytes
716 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100717 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100718 * \return \c 0 on success.
719 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100720 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100722 int (*f_rng)(void *, unsigned char *, size_t),
723 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100724 int mode,
725 const unsigned char *label, size_t label_len,
726 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100727 const unsigned char *input,
728 unsigned char *output );
729
730/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000731 * \brief This function performs an RSA operation, then removes the
732 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000734 * It is the generic wrapper for performing a PKCS#1 decryption
735 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000736 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100737 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000738 * as large as the size \p ctx->len of \p ctx->N (for example,
739 * 128 Bytes if RSA-1024 is used) to be able to hold an
740 * arbitrary decrypted message. If it is not large enough to
741 * hold the decryption of the particular ciphertext provided,
742 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100743 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100744 * \deprecated It is deprecated and discouraged to call this function
745 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
746 * are likely to remove the \p mode argument and have it
747 * implicitly set to #MBEDTLS_RSA_PRIVATE.
748 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100749 * \note Alternative implementations of RSA need not support
750 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300751 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100752 *
Hanno Becker9a467772018-12-13 09:54:59 +0000753 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000754 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
755 * this is used for blinding and should be provided; see
756 * mbedtls_rsa_private() for more. If \p mode is
757 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000758 * \param p_rng The RNG context to be passed to \p f_rng. This may be
759 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
760 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000761 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000762 * \param olen The address at which to store the length of
763 * the plaintext. This must not be \c NULL.
764 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000765 * of length \c ctx->len Bytes. For example, \c 256 Bytes
766 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000767 * \param output The buffer used to hold the plaintext. This must
768 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000769 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100770 *
771 * \return \c 0 on success.
772 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000773 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200775 int (*f_rng)(void *, unsigned char *, size_t),
776 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000777 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000778 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000779 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000780 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000781
782/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000783 * \brief This function performs a PKCS#1 v1.5 decryption
784 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100785 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100786 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000787 * as large as the size \p ctx->len of \p ctx->N, for example,
788 * 128 Bytes if RSA-1024 is used, to be able to hold an
789 * arbitrary decrypted message. If it is not large enough to
790 * hold the decryption of the particular ciphertext provided,
791 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100792 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100793 * \deprecated It is deprecated and discouraged to call this function
794 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
795 * are likely to remove the \p mode argument and have it
796 * implicitly set to #MBEDTLS_RSA_PRIVATE.
797 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100798 * \note Alternative implementations of RSA need not support
799 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300800 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100801 *
Hanno Becker9a467772018-12-13 09:54:59 +0000802 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000803 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
804 * this is used for blinding and should be provided; see
805 * mbedtls_rsa_private() for more. If \p mode is
806 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000807 * \param p_rng The RNG context to be passed to \p f_rng. This may be
808 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
809 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000810 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000811 * \param olen The address at which to store the length of
812 * the plaintext. This must not be \c NULL.
813 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000814 * of length \c ctx->len Bytes. For example, \c 256 Bytes
815 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000816 * \param output The buffer used to hold the plaintext. This must
817 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000818 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100819 *
820 * \return \c 0 on success.
821 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
822 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100823 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200825 int (*f_rng)(void *, unsigned char *, size_t),
826 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100827 int mode, size_t *olen,
828 const unsigned char *input,
829 unsigned char *output,
830 size_t output_max_len );
831
832/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100833 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
834 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100835 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100836 * \note The output buffer length \c output_max_len should be
837 * as large as the size \p ctx->len of \p ctx->N, for
838 * example, 128 Bytes if RSA-1024 is used, to be able to
839 * hold an arbitrary decrypted message. If it is not
840 * large enough to hold the decryption of the particular
841 * ciphertext provided, the function returns
842 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100843 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100844 * \deprecated It is deprecated and discouraged to call this function
845 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
846 * are likely to remove the \p mode argument and have it
847 * implicitly set to #MBEDTLS_RSA_PRIVATE.
848 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100849 * \note Alternative implementations of RSA need not support
850 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300851 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100852 *
Hanno Becker9a467772018-12-13 09:54:59 +0000853 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000854 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
855 * this is used for blinding and should be provided; see
856 * mbedtls_rsa_private() for more. If \p mode is
857 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000858 * \param p_rng The RNG context to be passed to \p f_rng. This may be
859 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
860 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000861 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100862 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000863 * This must be a readable buffer of length \p label_len
864 * Bytes. It may be \c NULL if \p label_len is \c 0.
865 * \param label_len The length of the label in Bytes.
866 * \param olen The address at which to store the length of
867 * the plaintext. This must not be \c NULL.
868 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000869 * of length \c ctx->len Bytes. For example, \c 256 Bytes
870 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000871 * \param output The buffer used to hold the plaintext. This must
872 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000873 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100874 *
875 * \return \c 0 on success.
876 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100877 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200879 int (*f_rng)(void *, unsigned char *, size_t),
880 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100881 int mode,
882 const unsigned char *label, size_t label_len,
883 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100884 const unsigned char *input,
885 unsigned char *output,
886 size_t output_max_len );
887
888/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000889 * \brief This function performs a private RSA operation to sign
890 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000891 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000892 * It is the generic wrapper for performing a PKCS#1
893 * signature using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000894 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000895 * \note The \p sig buffer must be as large as the size
896 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000897 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000898 * \note For PKCS#1 v2.1 encoding, see comments on
899 * mbedtls_rsa_rsassa_pss_sign() for details on
900 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100901 *
902 * \deprecated It is deprecated and discouraged to call this function
903 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
904 * are likely to remove the \p mode argument and have it
905 * implicitly set to #MBEDTLS_RSA_PRIVATE.
906 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100907 * \note Alternative implementations of RSA need not support
908 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300909 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100910 *
Hanno Becker9a467772018-12-13 09:54:59 +0000911 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000912 * \param f_rng The RNG function to use. If the padding mode is PKCS#1 v2.1,
913 * this must be provided. If the padding mode is PKCS#1 v1.5 and
914 * \p mode is #MBEDTLS_RSA_PRIVATE, it is used for blinding
915 * and should be provided; see mbedtls_rsa_private() for more
916 * more. It is ignored otherwise.
Hanno Becker9a467772018-12-13 09:54:59 +0000917 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
918 * if \p f_rng is \c NULL or doesn't need a context argument.
919 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000920 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100921 * \param md_alg The message-digest algorithm used to hash the original data.
922 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000923 * \param hashlen The length of the message digest.
924 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
925 * \param hash The buffer holding the message digest or raw data.
926 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
927 * buffer of length \p hashlen Bytes. If \p md_alg is not
928 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
929 * the size of the hash corresponding to \p md_alg.
930 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000931 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
k-stachowiak4e36da32019-05-31 20:16:50 +0200932 * for an 2048-bit RSA modulus. A buffer length of
933 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100934 *
935 * \return \c 0 if the signing operation was successful.
936 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000937 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000939 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000940 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000941 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000943 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000944 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000945 unsigned char *sig );
946
947/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000948 * \brief This function performs a PKCS#1 v1.5 signature
949 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100950 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100951 * \deprecated It is deprecated and discouraged to call this function
952 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
953 * are likely to remove the \p mode argument and have it
954 * implicitly set to #MBEDTLS_RSA_PRIVATE.
955 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100956 * \note Alternative implementations of RSA need not support
957 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300958 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100959 *
Hanno Becker9a467772018-12-13 09:54:59 +0000960 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000961 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
962 * this is used for blinding and should be provided; see
963 * mbedtls_rsa_private() for more. If \p mode is
964 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000965 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
966 * if \p f_rng is \c NULL or doesn't need a context argument.
967 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000968 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000969 * \param md_alg The message-digest algorithm used to hash the original data.
970 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000971 * \param hashlen The length of the message digest.
972 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
973 * \param hash The buffer holding the message digest or raw data.
974 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
975 * buffer of length \p hashlen Bytes. If \p md_alg is not
976 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
977 * the size of the hash corresponding to \p md_alg.
978 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000979 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
k-stachowiak4e36da32019-05-31 20:16:50 +0200980 * for an 2048-bit RSA modulus. A buffer length of
981 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100982 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100983 * \return \c 0 if the signing operation was successful.
984 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100985 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200987 int (*f_rng)(void *, unsigned char *, size_t),
988 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100989 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100991 unsigned int hashlen,
992 const unsigned char *hash,
993 unsigned char *sig );
994
995/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000996 * \brief This function performs a PKCS#1 v2.1 PSS signature
997 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100998 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000999 * \note The \p hash_id in the RSA context is the one used for the
1000 * encoding. \p md_alg in the function call is the type of hash
1001 * that is encoded. According to <em>RFC-3447: Public-Key
1002 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1003 * Specifications</em> it is advised to keep both hashes the
1004 * same.
Rose Zadike8b5b992018-03-27 12:19:47 +01001005 *
Jaeden Amero3725bb22018-09-07 19:12:36 +01001006 * \note This function always uses the maximum possible salt size,
1007 * up to the length of the payload hash. This choice of salt
1008 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
1009 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
1010 * minimum salt size which is the hash size minus 2 bytes. If
1011 * this minimum size is too large given the key size (the salt
1012 * size, plus the hash size, plus 2 bytes must be no more than
1013 * the key size in bytes), this function returns
1014 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
1015 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001016 * \deprecated It is deprecated and discouraged to call this function
1017 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
1018 * are likely to remove the \p mode argument and have it
1019 * implicitly set to #MBEDTLS_RSA_PRIVATE.
1020 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001021 * \note Alternative implementations of RSA need not support
1022 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001023 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001024 *
Hanno Becker9a467772018-12-13 09:54:59 +00001025 * \param ctx The initialized RSA context to use.
1026 * \param f_rng The RNG function. It must not be \c NULL.
1027 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
1028 * if \p f_rng doesn't need a context argument.
1029 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001030 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001031 * \param md_alg The message-digest algorithm used to hash the original data.
1032 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001033 * \param hashlen The length of the message digest.
1034 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
1035 * \param hash The buffer holding the message digest or raw data.
1036 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1037 * buffer of length \p hashlen Bytes. If \p md_alg is not
1038 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1039 * the size of the hash corresponding to \p md_alg.
1040 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +00001041 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
k-stachowiak4e36da32019-05-31 20:16:50 +02001042 * for an 2048-bit RSA modulus. A buffer length of
1043 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +01001044 *
1045 * \return \c 0 if the signing operation was successful.
1046 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001047 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001049 int (*f_rng)(void *, unsigned char *, size_t),
1050 void *p_rng,
1051 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001053 unsigned int hashlen,
1054 const unsigned char *hash,
1055 unsigned char *sig );
1056
1057/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001058 * \brief This function performs a public RSA operation and checks
1059 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +00001060 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001061 * This is the generic wrapper for performing a PKCS#1
1062 * verification using the mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001063 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001064 * \note For PKCS#1 v2.1 encoding, see comments on
1065 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
1066 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +01001067 *
1068 * \deprecated It is deprecated and discouraged to call this function
1069 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1070 * are likely to remove the \p mode argument and have it
1071 * set to #MBEDTLS_RSA_PUBLIC.
1072 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001073 * \note Alternative implementations of RSA need not support
1074 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001075 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001076 *
Hanno Becker9a467772018-12-13 09:54:59 +00001077 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001078 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1079 * this is used for blinding and should be provided; see
1080 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001081 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1082 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1083 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001084 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001085 * \param md_alg The message-digest algorithm used to hash the original data.
1086 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001087 * \param hashlen The length of the message digest.
1088 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1089 * \param hash The buffer holding the message digest or raw data.
1090 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1091 * buffer of length \p hashlen Bytes. If \p md_alg is not
1092 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1093 * the size of the hash corresponding to \p md_alg.
1094 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001095 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1096 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001097 *
1098 * \return \c 0 if the verify operation was successful.
1099 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001102 int (*f_rng)(void *, unsigned char *, size_t),
1103 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001106 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001107 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001108 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +00001109
1110/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001111 * \brief This function performs a PKCS#1 v1.5 verification
1112 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001113 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001114 * \deprecated It is deprecated and discouraged to call this function
1115 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1116 * are likely to remove the \p mode argument and have it
1117 * set to #MBEDTLS_RSA_PUBLIC.
1118 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001119 * \note Alternative implementations of RSA need not support
1120 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001121 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001122 *
Hanno Becker9a467772018-12-13 09:54:59 +00001123 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001124 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1125 * this is used for blinding and should be provided; see
1126 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001127 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1128 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1129 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001130 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +00001131 * \param md_alg The message-digest algorithm used to hash the original data.
1132 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001133 * \param hashlen The length of the message digest.
1134 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1135 * \param hash The buffer holding the message digest or raw data.
1136 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1137 * buffer of length \p hashlen Bytes. If \p md_alg is not
1138 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1139 * the size of the hash corresponding to \p md_alg.
1140 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001141 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1142 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001143 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001144 * \return \c 0 if the verify operation was successful.
1145 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001148 int (*f_rng)(void *, unsigned char *, size_t),
1149 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001150 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001152 unsigned int hashlen,
1153 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001154 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001155
1156/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001157 * \brief This function performs a PKCS#1 v2.1 PSS verification
1158 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001159 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001160 * The hash function for the MGF mask generating function
1161 * is that specified in the RSA context.
Paul Bakkerb3869132013-02-28 17:21:01 +01001162 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001163 * \note The \p hash_id in the RSA context is the one used for the
1164 * verification. \p md_alg in the function call is the type of
1165 * hash that is verified. According to <em>RFC-3447: Public-Key
1166 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1167 * Specifications</em> it is advised to keep both hashes the
1168 * same. If \p hash_id in the RSA context is unset,
1169 * the \p md_alg from the function call is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001170 *
1171 * \deprecated It is deprecated and discouraged to call this function
1172 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1173 * are likely to remove the \p mode argument and have it
1174 * implicitly set to #MBEDTLS_RSA_PUBLIC.
1175 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001176 * \note Alternative implementations of RSA need not support
1177 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001178 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001179 *
Hanno Becker9a467772018-12-13 09:54:59 +00001180 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001181 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1182 * this is used for blinding and should be provided; see
1183 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001184 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1185 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1186 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001187 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001188 * \param md_alg The message-digest algorithm used to hash the original data.
1189 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001190 * \param hashlen The length of the message digest.
1191 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1192 * \param hash The buffer holding the message digest or raw data.
1193 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1194 * buffer of length \p hashlen Bytes. If \p md_alg is not
1195 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1196 * the size of the hash corresponding to \p md_alg.
1197 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001198 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1199 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001200 *
1201 * \return \c 0 if the verify operation was successful.
1202 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001205 int (*f_rng)(void *, unsigned char *, size_t),
1206 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001207 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001209 unsigned int hashlen,
1210 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001211 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001212
1213/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001214 * \brief This function performs a PKCS#1 v2.1 PSS verification
1215 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001216 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001217 * The hash function for the MGF mask generating function
1218 * is that specified in \p mgf1_hash_id.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001219 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001220 * \note The \p sig buffer must be as large as the size
1221 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001222 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001223 * \note The \p hash_id in the RSA context is ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001224 *
Hanno Becker9a467772018-12-13 09:54:59 +00001225 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001226 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1227 * this is used for blinding and should be provided; see
1228 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001229 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1230 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1231 * \param mode The mode of operation. This must be either
1232 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +01001233 * \param md_alg The message-digest algorithm used to hash the original data.
1234 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001235 * \param hashlen The length of the message digest.
1236 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1237 * \param hash The buffer holding the message digest or raw data.
1238 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1239 * buffer of length \p hashlen Bytes. If \p md_alg is not
1240 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1241 * the size of the hash corresponding to \p md_alg.
1242 * \param mgf1_hash_id The message digest used for mask generation.
1243 * \param expected_salt_len The length of the salt used in padding. Use
1244 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1245 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001246 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1247 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001248 *
1249 * \return \c 0 if the verify operation was successful.
1250 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001253 int (*f_rng)(void *, unsigned char *, size_t),
1254 void *p_rng,
1255 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001257 unsigned int hashlen,
1258 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001260 int expected_salt_len,
1261 const unsigned char *sig );
1262
1263/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001264 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001265 *
Hanno Becker9a467772018-12-13 09:54:59 +00001266 * \param dst The destination context. This must be initialized.
1267 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001268 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001269 * \return \c 0 on success.
1270 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001273
1274/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001275 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001276 *
Hanno Becker9a467772018-12-13 09:54:59 +00001277 * \param ctx The RSA context to free. May be \c NULL, in which case
1278 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001279 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001282
Ron Eldorfa8f6352017-06-20 15:48:46 +03001283#if defined(MBEDTLS_SELF_TEST)
1284
Paul Bakker5121ce52009-01-03 21:22:43 +00001285/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001286 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001287 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001288 * \return \c 0 on success.
1289 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001292
Ron Eldorfa8f6352017-06-20 15:48:46 +03001293#endif /* MBEDTLS_SELF_TEST */
1294
Paul Bakker5121ce52009-01-03 21:22:43 +00001295#ifdef __cplusplus
1296}
1297#endif
1298
Paul Bakker5121ce52009-01-03 21:22:43 +00001299#endif /* rsa.h */