blob: 9b5da67e1b2350f3856d65d1c0d6568fa9a9baed [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 */
Gilles Peskine1990fab2021-07-26 18:48:10 +020074/** Bad input parameters to function. */
75#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080
76/** Input data contains invalid padding and is rejected. */
77#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100
78/** Something failed during generation of a key. */
79#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180
80/** Key failed to pass the validity check of the library. */
81#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200
82/** The public key operation failed. */
83#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280
84/** The private key operation failed. */
85#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300
86/** The PKCS#1 verification failed. */
87#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380
88/** The output buffer for decryption is not large enough. */
89#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400
90/** The random generator failed to generate non-zeros. */
91#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480
Ron Eldor9924bdc2018-10-04 10:59:13 +030092
93/* MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is deprecated and should not be used.
94 */
Gilles Peskine1990fab2021-07-26 18:48:10 +020095/** The implementation does not offer the requested operation, for example, because of security violations or lack of functionality. */
96#define MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION -0x4500
Ron Eldor9924bdc2018-10-04 10:59:13 +030097
98/* MBEDTLS_ERR_RSA_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine1990fab2021-07-26 18:48:10 +020099/** RSA hardware accelerator failed. */
100#define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
102/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200103 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 */
Rose Zadik042e97f2018-01-26 16:35:10 +0000105#define MBEDTLS_RSA_PUBLIC 0 /**< Request private key operation. */
106#define MBEDTLS_RSA_PRIVATE 1 /**< Request public key operation. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Rose Zadike8b5b992018-03-27 12:19:47 +0100108#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
109#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Rose Zadik042e97f2018-01-26 16:35:10 +0000111#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
112#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200115
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +0200116/*
117 * The above constants may be used even if the RSA module is compile out,
118 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
119 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +0100120
Paul Bakker407a0da2013-06-27 14:29:21 +0200121#ifdef __cplusplus
122extern "C" {
123#endif
124
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200125#if !defined(MBEDTLS_RSA_ALT)
126// Regular implementation
127//
128
Paul Bakker5121ce52009-01-03 21:22:43 +0000129/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000130 * \brief The RSA context structure.
Hanno Becker5063cd22017-09-29 11:49:12 +0100131 *
132 * \note Direct manipulation of the members of this structure
Rose Zadik042e97f2018-01-26 16:35:10 +0000133 * is deprecated. All manipulation should instead be done through
134 * the public interface functions.
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200136typedef struct mbedtls_rsa_context
Paul Bakker5121ce52009-01-03 21:22:43 +0000137{
Gilles Peskinece455dd2021-02-09 18:59:42 +0100138 int ver; /*!< Reserved for internal purposes.
139 * Do not set this field in application
140 * code. Its meaning might change without
141 * notice. */
Rose Zadik042e97f2018-01-26 16:35:10 +0000142 size_t len; /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
Rose Zadike8b5b992018-03-27 12:19:47 +0100144 mbedtls_mpi N; /*!< The public modulus. */
145 mbedtls_mpi E; /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
Rose Zadike8b5b992018-03-27 12:19:47 +0100147 mbedtls_mpi D; /*!< The private exponent. */
148 mbedtls_mpi P; /*!< The first prime factor. */
149 mbedtls_mpi Q; /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100150
Rose Zadikf2ec2882018-04-17 10:27:25 +0100151 mbedtls_mpi DP; /*!< <code>D % (P - 1)</code>. */
152 mbedtls_mpi DQ; /*!< <code>D % (Q - 1)</code>. */
153 mbedtls_mpi QP; /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Rose Zadikf2ec2882018-04-17 10:27:25 +0100155 mbedtls_mpi RN; /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
Rose Zadikf2ec2882018-04-17 10:27:25 +0100157 mbedtls_mpi RP; /*!< cached <code>R^2 mod P</code>. */
158 mbedtls_mpi RQ; /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200159
Rose Zadike8b5b992018-03-27 12:19:47 +0100160 mbedtls_mpi Vi; /*!< The cached blinding value. */
161 mbedtls_mpi Vf; /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000162
Rose Zadik042e97f2018-01-26 16:35:10 +0000163 int padding; /*!< Selects padding mode:
164 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
165 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
166 int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
167 as specified in md.h for use in the MGF
168 mask generating function used in the
169 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#if defined(MBEDTLS_THREADING_C)
Gilles Peskinece455dd2021-02-09 18:59:42 +0100171 /* Invariant: the mutex is initialized iff ver != 0. */
Rose Zadik042e97f2018-01-26 16:35:10 +0000172 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200173#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000174}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200177#else /* MBEDTLS_RSA_ALT */
178#include "rsa_alt.h"
179#endif /* MBEDTLS_RSA_ALT */
180
Paul Bakker5121ce52009-01-03 21:22:43 +0000181/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000182 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000184 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000185 * encryption scheme and the RSASSA-PSS signature scheme.
186 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000187 * \note The \p hash_id parameter is ignored when using
188 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200189 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000190 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200191 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000192 * mixing padding modes. For public key operations it is
Antonin Décimod5f47592019-01-23 15:24:37 +0100193 * a default value, which can be overridden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000194 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200195 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000196 * \note The hash selected in \p hash_id is always used for OEAP
197 * encryption. For PSS signatures, it is always used for
Antonin Décimod5f47592019-01-23 15:24:37 +0100198 * making signatures, but can be overridden for verifying them.
199 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100200 *
Hanno Becker9a467772018-12-13 09:54:59 +0000201 * \param ctx The RSA context to initialize. This must not be \c NULL.
202 * \param padding The padding mode to use. This must be either
203 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Hanno Becker385ce912018-12-13 18:33:12 +0000204 * \param hash_id The hash identifier of ::mbedtls_md_type_t type, if
Hanno Becker9a467772018-12-13 09:54:59 +0000205 * \p padding is #MBEDTLS_RSA_PKCS_V21. It is unused
206 * otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100209 int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000210 int hash_id );
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
212/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000213 * \brief This function imports a set of core parameters into an
214 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100215 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100216 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000217 * imports, if the parameters are not simultaneously present.
218 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100219 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000220 * by a call to mbedtls_rsa_complete(), which checks and
221 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100222 * public or private RSA key.
223 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000224 * \note See mbedtls_rsa_complete() for more information on which
225 * parameters are necessary to set up a private or public
226 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100227 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100228 * \note The imported parameters are copied and need not be preserved
229 * for the lifetime of the RSA context being set up.
230 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100231 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000232 * \param N The RSA modulus. This may be \c NULL.
233 * \param P The first prime factor of \p N. This may be \c NULL.
234 * \param Q The second prime factor of \p N. This may be \c NULL.
235 * \param D The private exponent. This may be \c NULL.
236 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100237 *
238 * \return \c 0 on success.
239 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100240 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100241int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
242 const mbedtls_mpi *N,
243 const mbedtls_mpi *P, const mbedtls_mpi *Q,
244 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100245
246/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000247 * \brief This function imports core RSA parameters, in raw big-endian
248 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100250 * \note This function can be called multiple times for successive
251 * imports, if the parameters are not simultaneously present.
252 *
253 * Any sequence of calls to this function should be followed
254 * by a call to mbedtls_rsa_complete(), which checks and
255 * completes the provided information to a ready-for-use
256 * public or private RSA key.
257 *
258 * \note See mbedtls_rsa_complete() for more information on which
259 * parameters are necessary to set up a private or public
260 * RSA key.
261 *
262 * \note The imported parameters are copied and need not be preserved
263 * for the lifetime of the RSA context being set up.
264 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000265 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000266 * \param N The RSA modulus. This may be \c NULL.
267 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
268 * \param P The first prime factor of \p N. This may be \c NULL.
269 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
270 * \param Q The second prime factor of \p N. This may be \c NULL.
271 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
272 * \param D The private exponent. This may be \c NULL.
273 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
274 * \param E The public exponent. This may be \c NULL.
275 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000276 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100277 * \return \c 0 on success.
278 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100279 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100280int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100281 unsigned char const *N, size_t N_len,
282 unsigned char const *P, size_t P_len,
283 unsigned char const *Q, size_t Q_len,
284 unsigned char const *D, size_t D_len,
285 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100286
287/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000288 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100289 * a set of imported core parameters.
290 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000291 * To setup an RSA public key, precisely \p N and \p E
292 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100293 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000294 * To setup an RSA private key, sufficient information must
295 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100296 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000297 * The default implementation supports the following:
298 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
299 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
300 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100301 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000302 * If this function runs successfully, it guarantees that
303 * the RSA context can be used for RSA operations without
304 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100305 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100306 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000307 * for the imported parameters. In particular, parameters that
308 * are not needed by the implementation might be silently
309 * discarded and left unchecked. To check the consistency
310 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100311 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100312 * \param ctx The initialized RSA context holding imported parameters.
313 *
314 * \return \c 0 on success.
315 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
316 * failed.
317 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100318 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100319int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100320
321/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000322 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100323 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000324 * If this function runs successfully, the non-NULL buffers
325 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
326 * written, with additional unused space filled leading by
327 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100328 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000329 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300330 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000331 * <li>An alternative RSA implementation is in use, which
332 * stores the key externally, and either cannot or should
333 * not export it into RAM.</li>
334 * <li>A SW or HW implementation might not support a certain
335 * deduction. For example, \p P, \p Q from \p N, \p D,
336 * and \p E if the former are not part of the
337 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100338 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000339 * If the function fails due to an unsupported operation,
340 * the RSA context stays intact and remains usable.
341 *
342 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000343 * \param N The MPI to hold the RSA modulus.
344 * This may be \c NULL if this field need not be exported.
345 * \param P The MPI to hold the first prime factor of \p N.
346 * This may be \c NULL if this field need not be exported.
347 * \param Q The MPI to hold the second prime factor of \p N.
348 * This may be \c NULL if this field need not be exported.
349 * \param D The MPI to hold the private exponent.
350 * This may be \c NULL if this field need not be exported.
351 * \param E The MPI to hold the public exponent.
352 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000353 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100354 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300355 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000356 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100357 * functionality or because of security policies.
358 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100359 *
360 */
361int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
362 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
363 mbedtls_mpi *D, mbedtls_mpi *E );
364
365/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000366 * \brief This function exports core parameters of an RSA key
367 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100368 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000369 * If this function runs successfully, the non-NULL buffers
370 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
371 * written, with additional unused space filled leading by
372 * zero Bytes.
373 *
374 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300375 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000376 * <li>An alternative RSA implementation is in use, which
377 * stores the key externally, and either cannot or should
378 * not export it into RAM.</li>
379 * <li>A SW or HW implementation might not support a certain
380 * deduction. For example, \p P, \p Q from \p N, \p D,
381 * and \p E if the former are not part of the
382 * implementation.</li></ul>
383 * If the function fails due to an unsupported operation,
384 * the RSA context stays intact and remains usable.
385 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100386 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100387 * buffer pointers are NULL.
388 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000389 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000390 * \param N The Byte array to store the RSA modulus,
391 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000392 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000393 * \param P The Byte array to hold the first prime factor of \p N,
394 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000395 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000396 * \param Q The Byte array to hold the second prime factor of \p N,
397 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000398 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000399 * \param D The Byte array to hold the private exponent,
400 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000401 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000402 * \param E The Byte array to hold the public exponent,
403 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000404 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100405 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100406 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300407 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000408 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100409 * functionality or because of security policies.
410 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100411 */
412int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
413 unsigned char *N, size_t N_len,
414 unsigned char *P, size_t P_len,
415 unsigned char *Q, size_t Q_len,
416 unsigned char *D, size_t D_len,
417 unsigned char *E, size_t E_len );
418
419/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000420 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100421 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100422 * \note Alternative RSA implementations not using CRT-parameters
423 * internally can implement this function based on
424 * mbedtls_rsa_deduce_opt().
425 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000426 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000427 * \param DP The MPI to hold \c D modulo `P-1`,
428 * or \c NULL if it need not be exported.
429 * \param DQ The MPI to hold \c D modulo `Q-1`,
430 * or \c NULL if it need not be exported.
431 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
432 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100433 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100434 * \return \c 0 on success.
435 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100436 *
437 */
438int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
439 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
440
Paul Bakker5121ce52009-01-03 21:22:43 +0000441/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000442 * \brief This function sets padding for an already initialized RSA
443 * context. See mbedtls_rsa_init() for details.
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 *
Hanno Becker9a467772018-12-13 09:54:59 +0000445 * \param ctx The initialized RSA context to be configured.
446 * \param padding The padding mode to use. This must be either
447 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Rose Zadik042e97f2018-01-26 16:35:10 +0000448 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Paul Bakker40e46942009-01-03 21:51:57 +0000449 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100450void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000451 int hash_id );
Paul Bakker21eb2802010-08-16 11:10:02 +0000452
Paul Bakkera3d195c2011-11-27 21:07:34 +0000453/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000454 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100455 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000456 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100457 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000458 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100459 *
460 */
461size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
462
463/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000464 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000465 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000466 * \note mbedtls_rsa_init() must be called before this function,
467 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000468 *
Hanno Becker9a467772018-12-13 09:54:59 +0000469 * \param ctx The initialized RSA context used to hold the key.
470 * \param f_rng The RNG function to be used for key generation.
471 * This must not be \c NULL.
472 * \param p_rng The RNG context to be passed to \p f_rng.
473 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100474 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000475 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000476 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100477 *
478 * \return \c 0 on success.
479 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100482 int (*f_rng)(void *, unsigned char *, size_t),
483 void *p_rng,
484 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
486/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000487 * \brief This function checks if a context contains at least an RSA
488 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000489 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000490 * If the function runs successfully, it is guaranteed that
491 * enough information is present to perform an RSA public key
492 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 *
Hanno Becker9a467772018-12-13 09:54:59 +0000494 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000495 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100496 * \return \c 0 on success.
497 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100498 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000503 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100504 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000505 *
Hanno Becker68767a62017-10-17 10:13:31 +0100506 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000507 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100508 * on the given context, but that the various parameters are
509 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000510 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100511 *
512 * \warning This function should catch accidental misconfigurations
513 * like swapping of parameters, but it cannot establish full
514 * trust in neither the quality nor the consistency of the key
515 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000516 * <ul><li>Consistency: Imported parameters that are irrelevant
517 * for the implementation might be silently dropped. If dropped,
518 * the current function does not have access to them,
519 * and therefore cannot check them. See mbedtls_rsa_complete().
520 * If you want to check the consistency of the entire
521 * content of an PKCS1-encoded RSA private key, for example, you
522 * should use mbedtls_rsa_validate_params() before setting
523 * up the RSA context.
524 * Additionally, if the implementation performs empirical checks,
525 * these checks substantiate but do not guarantee consistency.</li>
526 * <li>Quality: This function is not expected to perform
527 * extended quality assessments like checking that the prime
528 * factors are safe. Additionally, it is the responsibility of the
529 * user to ensure the trustworthiness of the source of his RSA
530 * parameters, which goes beyond what is effectively checkable
531 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100532 *
Hanno Becker9a467772018-12-13 09:54:59 +0000533 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100534 *
535 * \return \c 0 on success.
536 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
540/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000541 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100542 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000543 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100544 *
Hanno Becker9a467772018-12-13 09:54:59 +0000545 * \param pub The initialized RSA context holding the public key.
546 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000547 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100548 * \return \c 0 on success.
549 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100550 */
Hanno Becker98838b02017-10-02 13:16:10 +0100551int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
552 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100553
554/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000555 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 *
Hanno Becker9a467772018-12-13 09:54:59 +0000557 * \param ctx The initialized RSA context to use.
558 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000559 * of length \c ctx->len Bytes. For example, \c 256 Bytes
560 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000561 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000562 * of length \c ctx->len Bytes. For example, \c 256 Bytes
563 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000564 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000565 * \note This function does not handle message padding.
566 *
567 * \note Make sure to set \p input[0] = 0 or ensure that
568 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100570 * \return \c 0 on success.
571 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000574 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 unsigned char *output );
576
577/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000578 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 *
Hanno Becker24120612017-10-26 11:53:35 +0100580 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100581 *
582 * \note If blinding is used, both the base of exponentation
Hanno Becker24120612017-10-26 11:53:35 +0100583 * and the exponent are blinded, providing protection
584 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100585 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100586 * \warning It is deprecated and a security risk to not provide
587 * a PRNG here and thereby prevent the use of blinding.
588 * Future versions of the library may enforce the presence
589 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100590 *
Hanno Becker9a467772018-12-13 09:54:59 +0000591 * \param ctx The initialized RSA context to use.
592 * \param f_rng The RNG function, used for blinding. It is discouraged
593 * and deprecated to pass \c NULL here, in which case
594 * blinding will be omitted.
595 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
596 * if \p f_rng is \c NULL or if \p f_rng doesn't need a context.
597 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000598 * of length \c ctx->len Bytes. For example, \c 256 Bytes
599 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000600 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000601 * of length \c ctx->len Bytes. For example, \c 256 Bytes
602 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100603 *
604 * \return \c 0 on success.
605 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
606 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200609 int (*f_rng)(void *, unsigned char *, size_t),
610 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000611 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000612 unsigned char *output );
613
614/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000615 * \brief This function adds the message padding, then performs an RSA
616 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000618 * It is the generic wrapper for performing a PKCS#1 encryption
619 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100621 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000622 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
623 * are likely to remove the \p mode argument and have it
624 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100625 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100626 * \note Alternative implementations of RSA need not support
627 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300628 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100629 *
Hanno Becker9a467772018-12-13 09:54:59 +0000630 * \param ctx The initialized RSA context to use.
Hanno Becker974ca0d2018-12-18 18:03:24 +0000631 * \param f_rng The RNG to use. It is mandatory for PKCS#1 v2.1 padding
632 * encoding, and for PKCS#1 v1.5 padding encoding when used
633 * with \p mode set to #MBEDTLS_RSA_PUBLIC. For PKCS#1 v1.5
634 * padding encoding and \p mode set to #MBEDTLS_RSA_PRIVATE,
635 * it is used for blinding and should be provided in this
636 * case; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000637 * \param p_rng The RNG context to be passed to \p f_rng. May be
638 * \c NULL if \p f_rng is \c NULL or if \p f_rng doesn't
639 * need a context argument.
640 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000641 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000642 * \param ilen The length of the plaintext in Bytes.
643 * \param input The input data to encrypt. This must be a readable
Hanno Becker2f660d02018-12-18 17:04:59 +0000644 * buffer of size \p ilen Bytes. This must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000645 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000646 * of length \c ctx->len Bytes. For example, \c 256 Bytes
647 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100648 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100649 * \return \c 0 on success.
650 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000651 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000653 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000654 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000655 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000656 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000657 unsigned char *output );
658
659/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000660 * \brief This function performs a PKCS#1 v1.5 encryption operation
661 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100662 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100663 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000664 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
665 * are likely to remove the \p mode argument and have it
666 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100667 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100668 * \note Alternative implementations of RSA need not support
669 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300670 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100671 *
Hanno Becker9a467772018-12-13 09:54:59 +0000672 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000673 * \param f_rng The RNG function to use. It is needed for padding generation
674 * if \p mode is #MBEDTLS_RSA_PUBLIC. If \p mode is
675 * #MBEDTLS_RSA_PRIVATE (discouraged), it is used for
676 * blinding and should be provided; see mbedtls_rsa_private().
Hanno Becker9a467772018-12-13 09:54:59 +0000677 * \param p_rng The RNG context to be passed to \p f_rng. This may
678 * be \c NULL if \p f_rng is \c NULL or if \p f_rng
679 * doesn't need a context argument.
680 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000681 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000682 * \param ilen The length of the plaintext in Bytes.
683 * \param input The input data to encrypt. This must be a readable
Hanno Becker2f660d02018-12-18 17:04:59 +0000684 * buffer of size \p ilen Bytes. This must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000685 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000686 * of length \c ctx->len Bytes. For example, \c 256 Bytes
687 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100688 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100689 * \return \c 0 on success.
690 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100691 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100693 int (*f_rng)(void *, unsigned char *, size_t),
694 void *p_rng,
695 int mode, size_t ilen,
696 const unsigned char *input,
697 unsigned char *output );
698
699/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000700 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
701 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100702 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100703 * \note The output buffer must be as large as the size
704 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
705 *
706 * \deprecated It is deprecated and discouraged to call this function
707 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
708 * are likely to remove the \p mode argument and have it
709 * implicitly set to #MBEDTLS_RSA_PUBLIC.
710 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100711 * \note Alternative implementations of RSA need not support
712 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300713 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100714 *
Hanno Becker9a467772018-12-13 09:54:59 +0000715 * \param ctx The initnialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000716 * \param f_rng The RNG function to use. This is needed for padding
717 * generation and must be provided.
Hanno Becker9a467772018-12-13 09:54:59 +0000718 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000719 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000720 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000721 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000722 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000723 * This must be a readable buffer of length \p label_len
724 * Bytes. It may be \c NULL if \p label_len is \c 0.
725 * \param label_len The length of the label in Bytes.
726 * \param ilen The length of the plaintext buffer \p input in Bytes.
727 * \param input The input data to encrypt. This must be a readable
Hanno Becker2f660d02018-12-18 17:04:59 +0000728 * buffer of size \p ilen Bytes. This must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000729 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000730 * of length \c ctx->len Bytes. For example, \c 256 Bytes
731 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100732 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100733 * \return \c 0 on success.
734 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100737 int (*f_rng)(void *, unsigned char *, size_t),
738 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100739 int mode,
740 const unsigned char *label, size_t label_len,
741 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100742 const unsigned char *input,
743 unsigned char *output );
744
745/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000746 * \brief This function performs an RSA operation, then removes the
747 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000748 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000749 * It is the generic wrapper for performing a PKCS#1 decryption
750 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000751 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100752 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000753 * as large as the size \p ctx->len of \p ctx->N (for example,
754 * 128 Bytes if RSA-1024 is used) to be able to hold an
755 * arbitrary decrypted message. If it is not large enough to
756 * hold the decryption of the particular ciphertext provided,
757 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100758 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100759 * \deprecated It is deprecated and discouraged to call this function
760 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
761 * are likely to remove the \p mode argument and have it
762 * implicitly set to #MBEDTLS_RSA_PRIVATE.
763 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100764 * \note Alternative implementations of RSA need not support
765 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300766 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100767 *
Hanno Becker9a467772018-12-13 09:54:59 +0000768 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000769 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
770 * this is used for blinding and should be provided; see
771 * mbedtls_rsa_private() for more. If \p mode is
772 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000773 * \param p_rng The RNG context to be passed to \p f_rng. This may be
774 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
775 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000776 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000777 * \param olen The address at which to store the length of
778 * the plaintext. This must not be \c NULL.
779 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000780 * of length \c ctx->len Bytes. For example, \c 256 Bytes
781 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000782 * \param output The buffer used to hold the plaintext. This must
783 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000784 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100785 *
786 * \return \c 0 on success.
787 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000788 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200790 int (*f_rng)(void *, unsigned char *, size_t),
791 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000792 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000793 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000794 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000795 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000796
797/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000798 * \brief This function performs a PKCS#1 v1.5 decryption
799 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100800 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100801 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000802 * as large as the size \p ctx->len of \p ctx->N, for example,
803 * 128 Bytes if RSA-1024 is used, to be able to hold an
804 * arbitrary decrypted message. If it is not large enough to
805 * hold the decryption of the particular ciphertext provided,
806 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100807 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100808 * \deprecated It is deprecated and discouraged to call this function
809 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
810 * are likely to remove the \p mode argument and have it
811 * implicitly set to #MBEDTLS_RSA_PRIVATE.
812 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100813 * \note Alternative implementations of RSA need not support
814 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300815 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100816 *
Hanno Becker9a467772018-12-13 09:54:59 +0000817 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000818 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
819 * this is used for blinding and should be provided; see
820 * mbedtls_rsa_private() for more. If \p mode is
821 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000822 * \param p_rng The RNG context to be passed to \p f_rng. This may be
823 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
824 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000825 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000826 * \param olen The address at which to store the length of
827 * the plaintext. This must not be \c NULL.
828 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000829 * of length \c ctx->len Bytes. For example, \c 256 Bytes
830 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000831 * \param output The buffer used to hold the plaintext. This must
832 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000833 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100834 *
835 * \return \c 0 on success.
836 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
837 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100838 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200840 int (*f_rng)(void *, unsigned char *, size_t),
841 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100842 int mode, size_t *olen,
843 const unsigned char *input,
844 unsigned char *output,
845 size_t output_max_len );
846
847/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100848 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
849 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100850 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100851 * \note The output buffer length \c output_max_len should be
852 * as large as the size \p ctx->len of \p ctx->N, for
853 * example, 128 Bytes if RSA-1024 is used, to be able to
854 * hold an arbitrary decrypted message. If it is not
855 * large enough to hold the decryption of the particular
856 * ciphertext provided, the function returns
857 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100858 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100859 * \deprecated It is deprecated and discouraged to call this function
860 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
861 * are likely to remove the \p mode argument and have it
862 * implicitly set to #MBEDTLS_RSA_PRIVATE.
863 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100864 * \note Alternative implementations of RSA need not support
865 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300866 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100867 *
Hanno Becker9a467772018-12-13 09:54:59 +0000868 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000869 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
870 * this is used for blinding and should be provided; see
871 * mbedtls_rsa_private() for more. If \p mode is
872 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000873 * \param p_rng The RNG context to be passed to \p f_rng. This may be
874 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
875 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000876 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100877 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000878 * This must be a readable buffer of length \p label_len
879 * Bytes. It may be \c NULL if \p label_len is \c 0.
880 * \param label_len The length of the label in Bytes.
881 * \param olen The address at which to store the length of
882 * the plaintext. This must not be \c NULL.
883 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000884 * of length \c ctx->len Bytes. For example, \c 256 Bytes
885 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000886 * \param output The buffer used to hold the plaintext. This must
887 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000888 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100889 *
890 * \return \c 0 on success.
891 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200894 int (*f_rng)(void *, unsigned char *, size_t),
895 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100896 int mode,
897 const unsigned char *label, size_t label_len,
898 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100899 const unsigned char *input,
900 unsigned char *output,
901 size_t output_max_len );
902
903/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000904 * \brief This function performs a private RSA operation to sign
905 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000906 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000907 * It is the generic wrapper for performing a PKCS#1
908 * signature using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000909 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000910 * \note The \p sig buffer must be as large as the size
911 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000912 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000913 * \note For PKCS#1 v2.1 encoding, see comments on
914 * mbedtls_rsa_rsassa_pss_sign() for details on
915 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100916 *
917 * \deprecated It is deprecated and discouraged to call this function
918 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
919 * are likely to remove the \p mode argument and have it
920 * implicitly set to #MBEDTLS_RSA_PRIVATE.
921 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100922 * \note Alternative implementations of RSA need not support
923 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300924 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100925 *
Hanno Becker9a467772018-12-13 09:54:59 +0000926 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000927 * \param f_rng The RNG function to use. If the padding mode is PKCS#1 v2.1,
928 * this must be provided. If the padding mode is PKCS#1 v1.5 and
929 * \p mode is #MBEDTLS_RSA_PRIVATE, it is used for blinding
930 * and should be provided; see mbedtls_rsa_private() for more
931 * more. It is ignored otherwise.
Hanno Becker9a467772018-12-13 09:54:59 +0000932 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
933 * if \p f_rng is \c NULL or doesn't need a context argument.
934 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000935 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100936 * \param md_alg The message-digest algorithm used to hash the original data.
937 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000938 * \param hashlen The length of the message digest.
939 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
940 * \param hash The buffer holding the message digest or raw data.
941 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
942 * buffer of length \p hashlen Bytes. If \p md_alg is not
943 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
944 * the size of the hash corresponding to \p md_alg.
945 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000946 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
k-stachowiak4e36da32019-05-31 20:16:50 +0200947 * for an 2048-bit RSA modulus. A buffer length of
948 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100949 *
950 * \return \c 0 if the signing operation was successful.
951 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000952 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000954 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000955 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000956 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000958 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000959 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000960 unsigned char *sig );
961
962/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000963 * \brief This function performs a PKCS#1 v1.5 signature
964 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100965 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100966 * \deprecated It is deprecated and discouraged to call this function
967 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
968 * are likely to remove the \p mode argument and have it
969 * implicitly set to #MBEDTLS_RSA_PRIVATE.
970 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100971 * \note Alternative implementations of RSA need not support
972 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300973 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100974 *
Hanno Becker9a467772018-12-13 09:54:59 +0000975 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000976 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
977 * this is used for blinding and should be provided; see
978 * mbedtls_rsa_private() for more. If \p mode is
979 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000980 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
981 * if \p f_rng is \c NULL or doesn't need a context argument.
982 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000983 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000984 * \param md_alg The message-digest algorithm used to hash the original data.
985 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000986 * \param hashlen The length of the message digest.
987 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
988 * \param hash The buffer holding the message digest or raw data.
989 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
990 * buffer of length \p hashlen Bytes. If \p md_alg is not
991 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
992 * the size of the hash corresponding to \p md_alg.
993 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000994 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
k-stachowiak4e36da32019-05-31 20:16:50 +0200995 * for an 2048-bit RSA modulus. A buffer length of
996 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100997 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100998 * \return \c 0 if the signing operation was successful.
999 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001000 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001002 int (*f_rng)(void *, unsigned char *, size_t),
1003 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001004 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001006 unsigned int hashlen,
1007 const unsigned char *hash,
1008 unsigned char *sig );
1009
1010/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001011 * \brief This function performs a PKCS#1 v2.1 PSS signature
1012 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +01001013 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001014 * \note The \p hash_id in the RSA context is the one used for the
1015 * encoding. \p md_alg in the function call is the type of hash
1016 * that is encoded. According to <em>RFC-3447: Public-Key
1017 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1018 * Specifications</em> it is advised to keep both hashes the
1019 * same.
Rose Zadike8b5b992018-03-27 12:19:47 +01001020 *
Jaeden Amero3725bb22018-09-07 19:12:36 +01001021 * \note This function always uses the maximum possible salt size,
1022 * up to the length of the payload hash. This choice of salt
1023 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
1024 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
1025 * minimum salt size which is the hash size minus 2 bytes. If
1026 * this minimum size is too large given the key size (the salt
1027 * size, plus the hash size, plus 2 bytes must be no more than
1028 * the key size in bytes), this function returns
1029 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
1030 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001031 * \deprecated It is deprecated and discouraged to call this function
1032 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
1033 * are likely to remove the \p mode argument and have it
1034 * implicitly set to #MBEDTLS_RSA_PRIVATE.
1035 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001036 * \note Alternative implementations of RSA need not support
1037 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001038 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001039 *
Hanno Becker9a467772018-12-13 09:54:59 +00001040 * \param ctx The initialized RSA context to use.
1041 * \param f_rng The RNG function. It must not be \c NULL.
1042 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
1043 * if \p f_rng doesn't need a context argument.
1044 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001045 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001046 * \param md_alg The message-digest algorithm used to hash the original data.
1047 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001048 * \param hashlen The length of the message digest.
1049 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
1050 * \param hash The buffer holding the message digest or raw data.
1051 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1052 * buffer of length \p hashlen Bytes. If \p md_alg is not
1053 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1054 * the size of the hash corresponding to \p md_alg.
1055 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +00001056 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
k-stachowiak4e36da32019-05-31 20:16:50 +02001057 * for an 2048-bit RSA modulus. A buffer length of
1058 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +01001059 *
1060 * \return \c 0 if the signing operation was successful.
1061 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001062 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001064 int (*f_rng)(void *, unsigned char *, size_t),
1065 void *p_rng,
1066 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001068 unsigned int hashlen,
1069 const unsigned char *hash,
1070 unsigned char *sig );
1071
1072/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001073 * \brief This function performs a public RSA operation and checks
1074 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +00001075 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001076 * This is the generic wrapper for performing a PKCS#1
1077 * verification using the mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001078 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001079 * \note For PKCS#1 v2.1 encoding, see comments on
1080 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
1081 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +01001082 *
1083 * \deprecated It is deprecated and discouraged to call this function
1084 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1085 * are likely to remove the \p mode argument and have it
1086 * set to #MBEDTLS_RSA_PUBLIC.
1087 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001088 * \note Alternative implementations of RSA need not support
1089 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001090 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001091 *
Hanno Becker9a467772018-12-13 09:54:59 +00001092 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001093 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1094 * this is used for blinding and should be provided; see
1095 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001096 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1097 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1098 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001099 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001100 * \param md_alg The message-digest algorithm used to hash the original data.
1101 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001102 * \param hashlen The length of the message digest.
1103 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1104 * \param hash The buffer holding the message digest or raw data.
1105 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1106 * buffer of length \p hashlen Bytes. If \p md_alg is not
1107 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1108 * the size of the hash corresponding to \p md_alg.
1109 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001110 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1111 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001112 *
1113 * \return \c 0 if the verify operation was successful.
1114 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001117 int (*f_rng)(void *, unsigned char *, size_t),
1118 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001119 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001121 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001122 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001123 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +00001124
1125/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001126 * \brief This function performs a PKCS#1 v1.5 verification
1127 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001128 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001129 * \deprecated It is deprecated and discouraged to call this function
1130 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1131 * are likely to remove the \p mode argument and have it
1132 * set to #MBEDTLS_RSA_PUBLIC.
1133 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001134 * \note Alternative implementations of RSA need not support
1135 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001136 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001137 *
Hanno Becker9a467772018-12-13 09:54:59 +00001138 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001139 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1140 * this is used for blinding and should be provided; see
1141 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001142 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1143 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1144 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001145 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +00001146 * \param md_alg The message-digest algorithm used to hash the original data.
1147 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001148 * \param hashlen The length of the message digest.
1149 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1150 * \param hash The buffer holding the message digest or raw data.
1151 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1152 * buffer of length \p hashlen Bytes. If \p md_alg is not
1153 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1154 * the size of the hash corresponding to \p md_alg.
1155 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001156 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1157 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001158 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001159 * \return \c 0 if the verify operation was successful.
1160 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001163 int (*f_rng)(void *, unsigned char *, size_t),
1164 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001165 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001167 unsigned int hashlen,
1168 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001169 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001170
1171/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001172 * \brief This function performs a PKCS#1 v2.1 PSS verification
1173 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001174 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001175 * The hash function for the MGF mask generating function
1176 * is that specified in the RSA context.
Paul Bakkerb3869132013-02-28 17:21:01 +01001177 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001178 * \note The \p hash_id in the RSA context is the one used for the
1179 * verification. \p md_alg in the function call is the type of
1180 * hash that is verified. According to <em>RFC-3447: Public-Key
1181 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1182 * Specifications</em> it is advised to keep both hashes the
1183 * same. If \p hash_id in the RSA context is unset,
1184 * the \p md_alg from the function call is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001185 *
1186 * \deprecated It is deprecated and discouraged to call this function
1187 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1188 * are likely to remove the \p mode argument and have it
1189 * implicitly set to #MBEDTLS_RSA_PUBLIC.
1190 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001191 * \note Alternative implementations of RSA need not support
1192 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001193 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001194 *
Hanno Becker9a467772018-12-13 09:54:59 +00001195 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001196 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1197 * this is used for blinding and should be provided; see
1198 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001199 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1200 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1201 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001202 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001203 * \param md_alg The message-digest algorithm used to hash the original data.
1204 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001205 * \param hashlen The length of the message digest.
1206 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1207 * \param hash The buffer holding the message digest or raw data.
1208 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1209 * buffer of length \p hashlen Bytes. If \p md_alg is not
1210 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1211 * the size of the hash corresponding to \p md_alg.
1212 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001213 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1214 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001215 *
1216 * \return \c 0 if the verify operation was successful.
1217 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001220 int (*f_rng)(void *, unsigned char *, size_t),
1221 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001222 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001224 unsigned int hashlen,
1225 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001226 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001227
1228/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001229 * \brief This function performs a PKCS#1 v2.1 PSS verification
1230 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001231 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001232 * The hash function for the MGF mask generating function
1233 * is that specified in \p mgf1_hash_id.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001234 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001235 * \note The \p sig buffer must be as large as the size
1236 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001237 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001238 * \note The \p hash_id in the RSA context is ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001239 *
Hanno Becker9a467772018-12-13 09:54:59 +00001240 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001241 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1242 * this is used for blinding and should be provided; see
1243 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001244 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1245 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1246 * \param mode The mode of operation. This must be either
1247 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +01001248 * \param md_alg The message-digest algorithm used to hash the original data.
1249 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001250 * \param hashlen The length of the message digest.
1251 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1252 * \param hash The buffer holding the message digest or raw data.
1253 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1254 * buffer of length \p hashlen Bytes. If \p md_alg is not
1255 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1256 * the size of the hash corresponding to \p md_alg.
1257 * \param mgf1_hash_id The message digest used for mask generation.
1258 * \param expected_salt_len The length of the salt used in padding. Use
1259 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1260 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001261 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1262 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001263 *
1264 * \return \c 0 if the verify operation was successful.
1265 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001268 int (*f_rng)(void *, unsigned char *, size_t),
1269 void *p_rng,
1270 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001272 unsigned int hashlen,
1273 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001275 int expected_salt_len,
1276 const unsigned char *sig );
1277
1278/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001279 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001280 *
Hanno Becker9a467772018-12-13 09:54:59 +00001281 * \param dst The destination context. This must be initialized.
1282 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001283 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001284 * \return \c 0 on success.
1285 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001288
1289/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001290 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001291 *
Hanno Becker9a467772018-12-13 09:54:59 +00001292 * \param ctx The RSA context to free. May be \c NULL, in which case
1293 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001294 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001297
Ron Eldorfa8f6352017-06-20 15:48:46 +03001298#if defined(MBEDTLS_SELF_TEST)
1299
Paul Bakker5121ce52009-01-03 21:22:43 +00001300/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001301 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001302 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001303 * \return \c 0 on success.
1304 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001307
Ron Eldorfa8f6352017-06-20 15:48:46 +03001308#endif /* MBEDTLS_SELF_TEST */
1309
Paul Bakker5121ce52009-01-03 21:22:43 +00001310#ifdef __cplusplus
1311}
1312#endif
1313
Paul Bakker5121ce52009-01-03 21:22:43 +00001314#endif /* rsa.h */