blob: 69f3981ede7cfca9bc6d1cfdccda493321d0b461 [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úti1e148272020-08-07 13:07:28 +020013 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020014 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#ifndef MBEDTLS_RSA_H
29#define MBEDTLS_RSA_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020030#include "mbedtls/private_access.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Bence Szépkútic662b362021-05-27 11:25:03 +020032#include "mbedtls/build_info.h"
Paul Bakkered27a042013-04-18 22:46:23 +020033
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010034#include "mbedtls/bignum.h"
35#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_THREADING_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010038#include "mbedtls/threading.h"
Paul Bakkerc9965dc2013-09-29 14:58:17 +020039#endif
40
Paul Bakker13e2dfe2009-07-28 07:18:38 +000041/*
42 * RSA Error codes
43 */
Gilles Peskined2971572021-07-26 18:48:10 +020044/** Bad input parameters to function. */
45#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080
46/** Input data contains invalid padding and is rejected. */
47#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100
48/** Something failed during generation of a key. */
49#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180
50/** Key failed to pass the validity check of the library. */
51#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200
52/** The public key operation failed. */
53#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280
54/** The private key operation failed. */
55#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300
56/** The PKCS#1 verification failed. */
57#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380
58/** The output buffer for decryption is not large enough. */
59#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400
60/** The random generator failed to generate non-zeros. */
61#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480
Ron Eldor9924bdc2018-10-04 10:59:13 +030062
Paul Bakker5121ce52009-01-03 21:22:43 +000063/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020064 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000065 */
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Rose Zadike8b5b992018-03-27 12:19:47 +010067#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
68#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Rose Zadik042e97f2018-01-26 16:35:10 +000070#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
71#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020074
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020075/*
76 * The above constants may be used even if the RSA module is compile out,
Shaun Case8b0ecbc2021-12-20 21:14:10 -080077 * eg for alternative (PKCS#11) RSA implementations in the PK layers.
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020078 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010079
Paul Bakker407a0da2013-06-27 14:29:21 +020080#ifdef __cplusplus
81extern "C" {
82#endif
83
Ron Eldor4e6d55d2018-02-07 16:36:15 +020084#if !defined(MBEDTLS_RSA_ALT)
85// Regular implementation
86//
87
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +000088#if !defined(MBEDTLS_RSA_GEN_KEY_MIN_BITS)
89#define MBEDTLS_RSA_GEN_KEY_MIN_BITS 1024
90#elif MBEDTLS_RSA_GEN_KEY_MIN_BITS < 128
91#error "MBEDTLS_RSA_GEN_KEY_MIN_BITS must be at least 128 bits"
Waleed Elmelegyab570712023-07-05 16:40:58 +000092#endif
93
Paul Bakker5121ce52009-01-03 21:22:43 +000094/**
Rose Zadik042e97f2018-01-26 16:35:10 +000095 * \brief The RSA context structure.
Paul Bakker5121ce52009-01-03 21:22:43 +000096 */
Gilles Peskine449bd832023-01-11 14:50:10 +010097typedef struct mbedtls_rsa_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020098 int MBEDTLS_PRIVATE(ver); /*!< Reserved for internal purposes.
Gilles Peskine449bd832023-01-11 14:50:10 +010099 * Do not set this field in application
100 * code. Its meaning might change without
101 * notice. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200102 size_t MBEDTLS_PRIVATE(len); /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200104 mbedtls_mpi MBEDTLS_PRIVATE(N); /*!< The public modulus. */
105 mbedtls_mpi MBEDTLS_PRIVATE(E); /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200107 mbedtls_mpi MBEDTLS_PRIVATE(D); /*!< The private exponent. */
108 mbedtls_mpi MBEDTLS_PRIVATE(P); /*!< The first prime factor. */
109 mbedtls_mpi MBEDTLS_PRIVATE(Q); /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100110
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200111 mbedtls_mpi MBEDTLS_PRIVATE(DP); /*!< <code>D % (P - 1)</code>. */
112 mbedtls_mpi MBEDTLS_PRIVATE(DQ); /*!< <code>D % (Q - 1)</code>. */
113 mbedtls_mpi MBEDTLS_PRIVATE(QP); /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200115 mbedtls_mpi MBEDTLS_PRIVATE(RN); /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200117 mbedtls_mpi MBEDTLS_PRIVATE(RP); /*!< cached <code>R^2 mod P</code>. */
118 mbedtls_mpi MBEDTLS_PRIVATE(RQ); /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200119
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200120 mbedtls_mpi MBEDTLS_PRIVATE(Vi); /*!< The cached blinding value. */
121 mbedtls_mpi MBEDTLS_PRIVATE(Vf); /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000122
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200123 int MBEDTLS_PRIVATE(padding); /*!< Selects padding mode:
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
125 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200126 int MBEDTLS_PRIVATE(hash_id); /*!< Hash identifier of mbedtls_md_type_t type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 as specified in md.h for use in the MGF
128 mask generating function used in the
129 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100131 /* Invariant: the mutex is initialized iff ver != 0. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200132 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex); /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200133#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200137#else /* MBEDTLS_RSA_ALT */
138#include "rsa_alt.h"
139#endif /* MBEDTLS_RSA_ALT */
140
Paul Bakker5121ce52009-01-03 21:22:43 +0000141/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000142 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200144 * \note This function initializes the padding and the hash
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200145 * identifier to respectively #MBEDTLS_RSA_PKCS_V15 and
146 * #MBEDTLS_MD_NONE. See mbedtls_rsa_set_padding() for more
147 * information about those parameters.
Ronald Cronc1905a12021-06-05 11:11:14 +0200148 *
149 * \param ctx The RSA context to initialize. This must not be \c NULL.
150 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151void mbedtls_rsa_init(mbedtls_rsa_context *ctx);
Ronald Cronc1905a12021-06-05 11:11:14 +0200152
153/**
154 * \brief This function sets padding for an already initialized RSA
155 * context.
156 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000157 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000158 * encryption scheme and the RSASSA-PSS signature scheme.
159 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000160 * \note The \p hash_id parameter is ignored when using
161 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200162 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200163 * \note The choice of padding mode is strictly enforced for private
164 * key operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000165 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100166 * a default value, which can be overridden by calling specific
Ronald Cronc1905a12021-06-05 11:11:14 +0200167 * \c mbedtls_rsa_rsaes_xxx or \c mbedtls_rsa_rsassa_xxx
168 * functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200169 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000170 * \note The hash selected in \p hash_id is always used for OEAP
171 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100172 * making signatures, but can be overridden for verifying them.
173 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100174 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200175 * \param ctx The initialized RSA context to be configured.
Hanno Becker9a467772018-12-13 09:54:59 +0000176 * \param padding The padding mode to use. This must be either
177 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200178 * \param hash_id The hash identifier for PSS or OAEP, if \p padding is
179 * #MBEDTLS_RSA_PKCS_V21. #MBEDTLS_MD_NONE is accepted by this
180 * function but may be not suitable for some operations.
181 * Ignored if \p padding is #MBEDTLS_RSA_PKCS_V15.
Ronald Cronc1905a12021-06-05 11:11:14 +0200182 *
183 * \return \c 0 on success.
184 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING failure:
185 * \p padding or \p hash_id is invalid.
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100187int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
188 mbedtls_md_type_t hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
190/**
Yanray Wang83548b52023-03-15 16:46:34 +0800191 * \brief This function retrieves padding mode of initialized
192 * RSA context.
Yanray Wanga730df62023-03-01 10:18:19 +0800193 *
194 * \param ctx The initialized RSA context.
195 *
196 * \return RSA padding mode.
197 *
198 */
199int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx);
200
201/**
Yanray Wang12cb3962023-03-01 10:20:02 +0800202 * \brief This function retrieves hash identifier of mbedtls_md_type_t
203 * type.
204 *
205 * \param ctx The initialized RSA context.
206 *
207 * \return Hash identifier of mbedtls_md_type_t type.
208 *
209 */
Yanray Wangd41684e2023-03-17 18:54:22 +0800210int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx);
Yanray Wang12cb3962023-03-01 10:20:02 +0800211
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 */
Gilles Peskine449bd832023-01-11 14:50:10 +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.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000269 * \param P_len The Byte length of \p P; it is ignored if \p P == NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000270 * \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 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100280int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
281 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 *
Andrzej Kurek43dfd512022-02-18 08:10:37 -0500291 * To setup an RSA public key, precisely \c N and \c E
Rose Zadik042e97f2018-01-26 16:35:10 +0000292 * 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:
Andrzej Kurek43dfd512022-02-18 08:10:37 -0500298 * <ul><li>Derive \c P, \c Q from \c N, \c D, \c E.</li>
299 * <li>Derive \c N, \c D from \c P, \c Q, \c E.</li></ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000300 * 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 */
Gilles Peskine449bd832023-01-11 14:50:10 +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 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100361int 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);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100364
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 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100412int 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);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100418
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 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100438int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
439 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100440
Paul Bakker5121ce52009-01-03 21:22:43 +0000441/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000442 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100443 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000444 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100445 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000446 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100447 *
448 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100449size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100450
451/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000452 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000454 * \note mbedtls_rsa_init() must be called before this function,
455 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 *
Hanno Becker9a467772018-12-13 09:54:59 +0000457 * \param ctx The initialized RSA context used to hold the key.
458 * \param f_rng The RNG function to be used for key generation.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100459 * This is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000460 * \param p_rng The RNG context to be passed to \p f_rng.
461 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100462 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000463 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000464 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100465 *
466 * \return \c 0 on success.
467 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000468 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100469int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
470 int (*f_rng)(void *, unsigned char *, size_t),
471 void *p_rng,
472 unsigned int nbits, int exponent);
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
474/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000475 * \brief This function checks if a context contains at least an RSA
476 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000478 * If the function runs successfully, it is guaranteed that
479 * enough information is present to perform an RSA public key
480 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 *
Hanno Becker9a467772018-12-13 09:54:59 +0000482 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000483 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100484 * \return \c 0 on success.
485 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100486 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100488int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000491 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100492 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 *
Hanno Becker68767a62017-10-17 10:13:31 +0100494 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000495 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100496 * on the given context, but that the various parameters are
497 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000498 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100499 *
500 * \warning This function should catch accidental misconfigurations
501 * like swapping of parameters, but it cannot establish full
502 * trust in neither the quality nor the consistency of the key
503 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000504 * <ul><li>Consistency: Imported parameters that are irrelevant
505 * for the implementation might be silently dropped. If dropped,
506 * the current function does not have access to them,
507 * and therefore cannot check them. See mbedtls_rsa_complete().
508 * If you want to check the consistency of the entire
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100509 * content of a PKCS1-encoded RSA private key, for example, you
Rose Zadik042e97f2018-01-26 16:35:10 +0000510 * should use mbedtls_rsa_validate_params() before setting
511 * up the RSA context.
512 * Additionally, if the implementation performs empirical checks,
513 * these checks substantiate but do not guarantee consistency.</li>
514 * <li>Quality: This function is not expected to perform
515 * extended quality assessments like checking that the prime
516 * factors are safe. Additionally, it is the responsibility of the
517 * user to ensure the trustworthiness of the source of his RSA
518 * parameters, which goes beyond what is effectively checkable
519 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100520 *
Hanno Becker9a467772018-12-13 09:54:59 +0000521 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100522 *
523 * \return \c 0 on success.
524 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000525 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100526int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
528/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000529 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100530 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000531 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100532 *
Hanno Becker9a467772018-12-13 09:54:59 +0000533 * \param pub The initialized RSA context holding the public key.
534 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000535 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100536 * \return \c 0 on success.
537 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100538 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100539int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
540 const mbedtls_rsa_context *prv);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100541
542/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000543 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 *
Hanno Becker9a467772018-12-13 09:54:59 +0000545 * \param ctx The initialized RSA context to use.
546 * \param input The input buffer. This must be a readable 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 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000550 * of length \c ctx->len Bytes. For example, \c 256 Bytes
551 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000552 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000553 * \note This function does not handle message padding.
554 *
555 * \note Make sure to set \p input[0] = 0 or ensure that
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500556 * input is smaller than \c N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100558 * \return \c 0 on success.
559 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100561int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
562 const unsigned char *input,
563 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000564
565/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000566 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 *
Hanno Becker24120612017-10-26 11:53:35 +0100568 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100569 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800570 * \note If blinding is used, both the base of exponentiation
Hanno Becker24120612017-10-26 11:53:35 +0100571 * and the exponent are blinded, providing protection
572 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100573 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100574 * \warning It is deprecated and a security risk to not provide
575 * a PRNG here and thereby prevent the use of blinding.
576 * Future versions of the library may enforce the presence
577 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100578 *
Hanno Becker9a467772018-12-13 09:54:59 +0000579 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100580 * \param f_rng The RNG function, used for blinding. It is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000581 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
Thomas Daubney03412782021-05-20 15:31:17 +0100582 * if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000583 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000584 * of length \c ctx->len Bytes. For example, \c 256 Bytes
585 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000586 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000587 * of length \c ctx->len Bytes. For example, \c 256 Bytes
588 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100589 *
590 * \return \c 0 on success.
591 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
592 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100594int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
595 int (*f_rng)(void *, unsigned char *, size_t),
596 void *p_rng,
597 const unsigned char *input,
598 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000599
600/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000601 * \brief This function adds the message padding, then performs an RSA
602 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000604 * It is the generic wrapper for performing a PKCS#1 encryption
Thomas Daubney21772772021-05-13 17:30:32 +0100605 * operation.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100606 *
Hanno Becker9a467772018-12-13 09:54:59 +0000607 * \param ctx The initialized RSA context to use.
Thomas Daubneyf54c5c52021-05-21 17:00:30 +0100608 * \param f_rng The RNG to use. It is used for padding generation
Thomas Daubney2c65db92021-05-21 10:58:28 +0100609 * and it is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000610 * \param p_rng The RNG context to be passed to \p f_rng. May be
Thomas Daubney03412782021-05-20 15:31:17 +0100611 * \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000612 * \param ilen The length of the plaintext in Bytes.
613 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000614 * buffer of size \p ilen Bytes. It may be \c NULL if
615 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000616 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000617 * of length \c ctx->len Bytes. For example, \c 256 Bytes
618 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100619 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100620 * \return \c 0 on success.
621 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000622 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100623int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
624 int (*f_rng)(void *, unsigned char *, size_t),
625 void *p_rng,
626 size_t ilen,
627 const unsigned char *input,
628 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
630/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000631 * \brief This function performs a PKCS#1 v1.5 encryption operation
632 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100633 *
Hanno Becker9a467772018-12-13 09:54:59 +0000634 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100635 * \param f_rng The RNG function to use. It is mandatory and used for
636 * padding generation.
Hanno Becker9a467772018-12-13 09:54:59 +0000637 * \param p_rng The RNG context to be passed to \p f_rng. This may
Thomas Daubney03412782021-05-20 15:31:17 +0100638 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000639 * \param ilen The length of the plaintext in Bytes.
640 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000641 * buffer of size \p ilen Bytes. It may be \c NULL if
642 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000643 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000644 * of length \c ctx->len Bytes. For example, \c 256 Bytes
645 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100646 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100647 * \return \c 0 on success.
648 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100649 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100650int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
651 int (*f_rng)(void *, unsigned char *, size_t),
652 void *p_rng,
653 size_t ilen,
654 const unsigned char *input,
655 unsigned char *output);
Paul Bakkerb3869132013-02-28 17:21:01 +0100656
657/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000658 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
659 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100660 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100661 * \note The output buffer must be as large as the size
662 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
663 *
Tom Cosgrove1e211442022-05-26 11:51:00 +0100664 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000665 * \param f_rng The RNG function to use. This is needed for padding
Thomas Daubney2c65db92021-05-21 10:58:28 +0100666 * generation and is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000667 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000668 * be \c NULL if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000669 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000670 * This must be a readable buffer of length \p label_len
671 * Bytes. It may be \c NULL if \p label_len is \c 0.
672 * \param label_len The length of the label in Bytes.
673 * \param ilen The length of the plaintext buffer \p input in Bytes.
674 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000675 * buffer of size \p ilen Bytes. It may be \c NULL if
676 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000677 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000678 * of length \c ctx->len Bytes. For example, \c 256 Bytes
679 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100680 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100681 * \return \c 0 on success.
682 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100683 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100684int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
685 int (*f_rng)(void *, unsigned char *, size_t),
686 void *p_rng,
687 const unsigned char *label, size_t label_len,
688 size_t ilen,
689 const unsigned char *input,
690 unsigned char *output);
Paul Bakkerb3869132013-02-28 17:21:01 +0100691
692/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000693 * \brief This function performs an RSA operation, then removes the
694 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000695 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000696 * It is the generic wrapper for performing a PKCS#1 decryption
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100697 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100699 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000700 * as large as the size \p ctx->len of \p ctx->N (for example,
701 * 128 Bytes if RSA-1024 is used) to be able to hold an
702 * arbitrary decrypted message. If it is not large enough to
703 * hold the decryption of the particular ciphertext provided,
704 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100705 *
Hanno Becker9a467772018-12-13 09:54:59 +0000706 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100707 * \param f_rng The RNG function. This is used for blinding and is
708 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000709 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100710 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000711 * \param olen The address at which to store the length of
712 * the plaintext. This must not be \c NULL.
713 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000714 * of length \c ctx->len Bytes. For example, \c 256 Bytes
715 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000716 * \param output The buffer used to hold the plaintext. This must
717 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000718 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100719 *
720 * \return \c 0 on success.
721 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000722 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100723int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
724 int (*f_rng)(void *, unsigned char *, size_t),
725 void *p_rng,
726 size_t *olen,
727 const unsigned char *input,
728 unsigned char *output,
729 size_t output_max_len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000730
731/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000732 * \brief This function performs a PKCS#1 v1.5 decryption
733 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100734 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100735 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000736 * as large as the size \p ctx->len of \p ctx->N, for example,
737 * 128 Bytes if RSA-1024 is used, to be able to hold an
738 * arbitrary decrypted message. If it is not large enough to
739 * hold the decryption of the particular ciphertext provided,
740 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100741 *
Hanno Becker9a467772018-12-13 09:54:59 +0000742 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100743 * \param f_rng The RNG function. This is used for blinding and is
744 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000745 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100746 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000747 * \param olen The address at which to store the length of
748 * the plaintext. This must not be \c NULL.
749 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000750 * of length \c ctx->len Bytes. For example, \c 256 Bytes
751 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000752 * \param output The buffer used to hold the plaintext. This must
753 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000754 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100755 *
756 * \return \c 0 on success.
757 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
758 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100759 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100760int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
761 int (*f_rng)(void *, unsigned char *, size_t),
762 void *p_rng,
763 size_t *olen,
764 const unsigned char *input,
765 unsigned char *output,
766 size_t output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +0100767
768/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100769 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
770 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100771 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100772 * \note The output buffer length \c output_max_len should be
773 * as large as the size \p ctx->len of \p ctx->N, for
774 * example, 128 Bytes if RSA-1024 is used, to be able to
775 * hold an arbitrary decrypted message. If it is not
776 * large enough to hold the decryption of the particular
777 * ciphertext provided, the function returns
778 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100779 *
Hanno Becker9a467772018-12-13 09:54:59 +0000780 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100781 * \param f_rng The RNG function. This is used for blinding and is
782 * mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000783 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100784 * \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100785 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000786 * This must be a readable buffer of length \p label_len
787 * Bytes. It may be \c NULL if \p label_len is \c 0.
788 * \param label_len The length of the label in Bytes.
789 * \param olen The address at which to store the length of
790 * the plaintext. This must not be \c NULL.
791 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000792 * of length \c ctx->len Bytes. For example, \c 256 Bytes
793 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000794 * \param output The buffer used to hold the plaintext. This must
795 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000796 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100797 *
798 * \return \c 0 on success.
799 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100800 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100801int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
802 int (*f_rng)(void *, unsigned char *, size_t),
803 void *p_rng,
804 const unsigned char *label, size_t label_len,
805 size_t *olen,
806 const unsigned char *input,
807 unsigned char *output,
808 size_t output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +0100809
810/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000811 * \brief This function performs a private RSA operation to sign
812 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000813 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000814 * It is the generic wrapper for performing a PKCS#1
Thomas Daubney140184d2021-05-18 16:04:07 +0100815 * signature.
Paul Bakker5121ce52009-01-03 21:22:43 +0000816 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000817 * \note The \p sig buffer must be as large as the size
818 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000819 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000820 * \note For PKCS#1 v2.1 encoding, see comments on
821 * mbedtls_rsa_rsassa_pss_sign() for details on
822 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100823 *
Hanno Becker9a467772018-12-13 09:54:59 +0000824 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100825 * \param f_rng The RNG function to use. This is mandatory and
826 * must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000827 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
Thomas Daubney03412782021-05-20 15:31:17 +0100828 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100829 * \param md_alg The message-digest algorithm used to hash the original data.
830 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200831 * \param hashlen The length of the message digest or raw data in Bytes.
832 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
833 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000834 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200835 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000836 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000837 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100838 * for an 2048-bit RSA modulus. A buffer length of
839 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100840 *
841 * \return \c 0 if the signing operation was successful.
842 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000843 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100844int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
845 int (*f_rng)(void *, unsigned char *, size_t),
846 void *p_rng,
847 mbedtls_md_type_t md_alg,
848 unsigned int hashlen,
849 const unsigned char *hash,
850 unsigned char *sig);
Paul Bakker5121ce52009-01-03 21:22:43 +0000851
852/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000853 * \brief This function performs a PKCS#1 v1.5 signature
854 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100855 *
Hanno Becker9a467772018-12-13 09:54:59 +0000856 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100857 * \param f_rng The RNG function. This is used for blinding and is
858 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000859 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
Thomas Daubney2c65db92021-05-21 10:58:28 +0100860 * if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000861 * \param md_alg The message-digest algorithm used to hash the original data.
862 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200863 * \param hashlen The length of the message digest or raw data in Bytes.
864 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
865 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000866 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200867 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000868 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000869 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100870 * for an 2048-bit RSA modulus. A buffer length of
871 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100872 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100873 * \return \c 0 if the signing operation was successful.
874 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100875 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100876int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
877 int (*f_rng)(void *, unsigned char *, size_t),
878 void *p_rng,
879 mbedtls_md_type_t md_alg,
880 unsigned int hashlen,
881 const unsigned char *hash,
882 unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +0100883
884/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000885 * \brief This function performs a PKCS#1 v2.1 PSS signature
886 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100887 *
Janos Follathb7953322021-04-01 14:44:17 +0100888 * \note The \c hash_id set in \p ctx by calling
889 * mbedtls_rsa_set_padding() selects the hash used for the
890 * encoding operation and for the mask generation function
891 * (MGF1). For more details on the encoding operation and the
892 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +0000893 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100894 * Specifications</em>.
Rose Zadike8b5b992018-03-27 12:19:47 +0100895 *
Cédric Meuter010ddc22020-04-25 09:24:11 +0200896 * \note This function enforces that the provided salt length complies
897 * with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 v2.2) §9.1.1
898 * step 3. The constraint is that the hash length plus the salt
899 * length plus 2 bytes must be at most the key length. If this
900 * constraint is not met, this function returns
Jaeden Amero3725bb22018-09-07 19:12:36 +0100901 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
902 *
Hanno Becker9a467772018-12-13 09:54:59 +0000903 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100904 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000905 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
906 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100907 * \param md_alg The message-digest algorithm used to hash the original data.
908 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200909 * \param hashlen The length of the message digest or raw data in Bytes.
910 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
911 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000912 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200913 * This must be a readable buffer of at least \p hashlen Bytes.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200914 * \param saltlen The length of the salt that should be used.
Cédric Meuter010ddc22020-04-25 09:24:11 +0200915 * If passed #MBEDTLS_RSA_SALT_LEN_ANY, the function will use
916 * the largest possible salt length up to the hash length,
917 * which is the largest permitted by some standards including
918 * FIPS 186-4 §5.5.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200919 * \param sig The buffer to hold the signature. This must be a writable
920 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
921 * for an 2048-bit RSA modulus. A buffer length of
922 * #MBEDTLS_MPI_MAX_SIZE is always safe.
923 *
924 * \return \c 0 if the signing operation was successful.
925 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
926 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100927int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
928 int (*f_rng)(void *, unsigned char *, size_t),
929 void *p_rng,
930 mbedtls_md_type_t md_alg,
931 unsigned int hashlen,
932 const unsigned char *hash,
933 int saltlen,
934 unsigned char *sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200935
936/**
937 * \brief This function performs a PKCS#1 v2.1 PSS signature
938 * operation (RSASSA-PSS-SIGN).
939 *
Janos Follathb7953322021-04-01 14:44:17 +0100940 * \note The \c hash_id set in \p ctx by calling
941 * mbedtls_rsa_set_padding() selects the hash used for the
942 * encoding operation and for the mask generation function
943 * (MGF1). For more details on the encoding operation and the
944 * mask generation function, consult <em>RFC-3447: Public-Key
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200945 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100946 * Specifications</em>.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200947 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000948 * \note This function always uses the maximum possible salt size,
949 * up to the length of the payload hash. This choice of salt
950 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
951 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
Rose Zadike8b5b992018-03-27 12:19:47 +0100952 * minimum salt size which is the hash size minus 2 bytes. If
953 * this minimum size is too large given the key size (the salt
954 * size, plus the hash size, plus 2 bytes must be no more than
955 * the key size in bytes), this function returns
956 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
957 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100958 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100959 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100960 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
961 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100962 * \param md_alg The message-digest algorithm used to hash the original data.
963 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200964 * \param hashlen The length of the message digest or raw data in Bytes.
965 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
966 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000967 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200968 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000969 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000970 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100971 * for an 2048-bit RSA modulus. A buffer length of
972 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100973 *
974 * \return \c 0 if the signing operation was successful.
975 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100976 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100977int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
978 int (*f_rng)(void *, unsigned char *, size_t),
979 void *p_rng,
980 mbedtls_md_type_t md_alg,
981 unsigned int hashlen,
982 const unsigned char *hash,
983 unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +0100984
985/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000986 * \brief This function performs a public RSA operation and checks
987 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +0000988 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000989 * This is the generic wrapper for performing a PKCS#1
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100990 * verification.
Paul Bakker5121ce52009-01-03 21:22:43 +0000991 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000992 * \note For PKCS#1 v2.1 encoding, see comments on
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500993 * mbedtls_rsa_rsassa_pss_verify() about \c md_alg and
994 * \c hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100995 *
Hanno Becker9a467772018-12-13 09:54:59 +0000996 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +0100997 * \param md_alg The message-digest algorithm used to hash the original data.
998 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200999 * \param hashlen The length of the message digest or raw data in Bytes.
1000 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1001 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001002 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001003 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001004 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001005 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1006 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001007 *
1008 * \return \c 0 if the verify operation was successful.
1009 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001010 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001011int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
1012 mbedtls_md_type_t md_alg,
1013 unsigned int hashlen,
1014 const unsigned char *hash,
1015 const unsigned char *sig);
Paul Bakker5121ce52009-01-03 21:22:43 +00001016
1017/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001018 * \brief This function performs a PKCS#1 v1.5 verification
1019 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001020 *
Hanno Becker9a467772018-12-13 09:54:59 +00001021 * \param ctx The initialized RSA public key context to use.
Rose Zadik042e97f2018-01-26 16:35:10 +00001022 * \param md_alg The message-digest algorithm used to hash the original data.
1023 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001024 * \param hashlen The length of the message digest or raw data in Bytes.
1025 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1026 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001027 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001028 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001029 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001030 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1031 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001032 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001033 * \return \c 0 if the verify operation was successful.
1034 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001035 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001036int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
1037 mbedtls_md_type_t md_alg,
1038 unsigned int hashlen,
1039 const unsigned char *hash,
1040 const unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001041
1042/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001043 * \brief This function performs a PKCS#1 v2.1 PSS verification
1044 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001045 *
Janos Follathb7953322021-04-01 14:44:17 +01001046 * \note The \c hash_id set in \p ctx by calling
1047 * mbedtls_rsa_set_padding() selects the hash used for the
1048 * encoding operation and for the mask generation function
1049 * (MGF1). For more details on the encoding operation and the
1050 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +00001051 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +01001052 * Specifications</em>. If the \c hash_id set in \p ctx by
1053 * mbedtls_rsa_set_padding() is #MBEDTLS_MD_NONE, the \p md_alg
1054 * parameter is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001055 *
Hanno Becker9a467772018-12-13 09:54:59 +00001056 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001057 * \param md_alg The message-digest algorithm used to hash the original data.
1058 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001059 * \param hashlen The length of the message digest or raw data in Bytes.
1060 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1061 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001062 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001063 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001064 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001065 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1066 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001067 *
1068 * \return \c 0 if the verify operation was successful.
1069 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001070 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001071int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
1072 mbedtls_md_type_t md_alg,
1073 unsigned int hashlen,
1074 const unsigned char *hash,
1075 const unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001076
1077/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001078 * \brief This function performs a PKCS#1 v2.1 PSS verification
1079 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001080 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001081 * \note The \p sig buffer must be as large as the size
1082 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001083 *
Janos Follathb7953322021-04-01 14:44:17 +01001084 * \note The \c hash_id set in \p ctx by mbedtls_rsa_set_padding() is
1085 * ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001086 *
Hanno Becker9a467772018-12-13 09:54:59 +00001087 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001088 * \param md_alg The message-digest algorithm used to hash the original data.
1089 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001090 * \param hashlen The length of the message digest or raw data in Bytes.
1091 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1092 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001093 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001094 * This must be a readable buffer of at least \p hashlen Bytes.
Janos Follathb7953322021-04-01 14:44:17 +01001095 * \param mgf1_hash_id The message digest algorithm used for the
1096 * verification operation and the mask generation
1097 * function (MGF1). For more details on the encoding
1098 * operation and the mask generation function, consult
1099 * <em>RFC-3447: Public-Key Cryptography Standards
1100 * (PKCS) #1 v2.1: RSA Cryptography
1101 * Specifications</em>.
Hanno Becker9a467772018-12-13 09:54:59 +00001102 * \param expected_salt_len The length of the salt used in padding. Use
1103 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1104 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001105 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1106 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001107 *
1108 * \return \c 0 if the verify operation was successful.
1109 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001110 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001111int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
1112 mbedtls_md_type_t md_alg,
1113 unsigned int hashlen,
1114 const unsigned char *hash,
1115 mbedtls_md_type_t mgf1_hash_id,
1116 int expected_salt_len,
1117 const unsigned char *sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001118
1119/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001120 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001121 *
Hanno Becker9a467772018-12-13 09:54:59 +00001122 * \param dst The destination context. This must be initialized.
1123 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001124 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001125 * \return \c 0 on success.
1126 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001127 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001128int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src);
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001129
1130/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001131 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001132 *
Hanno Becker9a467772018-12-13 09:54:59 +00001133 * \param ctx The RSA context to free. May be \c NULL, in which case
1134 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001135 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137void mbedtls_rsa_free(mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00001138
Ron Eldorfa8f6352017-06-20 15:48:46 +03001139#if defined(MBEDTLS_SELF_TEST)
1140
Paul Bakker5121ce52009-01-03 21:22:43 +00001141/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001142 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001143 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001144 * \return \c 0 on success.
1145 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001146 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001147int mbedtls_rsa_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +00001148
Ron Eldorfa8f6352017-06-20 15:48:46 +03001149#endif /* MBEDTLS_SELF_TEST */
1150
Paul Bakker5121ce52009-01-03 21:22:43 +00001151#ifdef __cplusplus
1152}
1153#endif
1154
Paul Bakker5121ce52009-01-03 21:22:43 +00001155#endif /* rsa.h */