blob: 8045e6b71342615f9a44d9392a2e7f1502050508 [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 Elmelegyab570712023-07-05 16:40:58 +000088#if !defined(MBEDTLS_RSA_MIN_KEY_SIZE)
89#define MBEDTLS_RSA_MIN_KEY_SIZE 1024
90#endif
91
Paul Bakker5121ce52009-01-03 21:22:43 +000092/**
Rose Zadik042e97f2018-01-26 16:35:10 +000093 * \brief The RSA context structure.
Paul Bakker5121ce52009-01-03 21:22:43 +000094 */
Gilles Peskine449bd832023-01-11 14:50:10 +010095typedef struct mbedtls_rsa_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020096 int MBEDTLS_PRIVATE(ver); /*!< Reserved for internal purposes.
Gilles Peskine449bd832023-01-11 14:50:10 +010097 * Do not set this field in application
98 * code. Its meaning might change without
99 * notice. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200100 size_t MBEDTLS_PRIVATE(len); /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200102 mbedtls_mpi MBEDTLS_PRIVATE(N); /*!< The public modulus. */
103 mbedtls_mpi MBEDTLS_PRIVATE(E); /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200105 mbedtls_mpi MBEDTLS_PRIVATE(D); /*!< The private exponent. */
106 mbedtls_mpi MBEDTLS_PRIVATE(P); /*!< The first prime factor. */
107 mbedtls_mpi MBEDTLS_PRIVATE(Q); /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100108
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200109 mbedtls_mpi MBEDTLS_PRIVATE(DP); /*!< <code>D % (P - 1)</code>. */
110 mbedtls_mpi MBEDTLS_PRIVATE(DQ); /*!< <code>D % (Q - 1)</code>. */
111 mbedtls_mpi MBEDTLS_PRIVATE(QP); /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200113 mbedtls_mpi MBEDTLS_PRIVATE(RN); /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200115 mbedtls_mpi MBEDTLS_PRIVATE(RP); /*!< cached <code>R^2 mod P</code>. */
116 mbedtls_mpi MBEDTLS_PRIVATE(RQ); /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200117
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200118 mbedtls_mpi MBEDTLS_PRIVATE(Vi); /*!< The cached blinding value. */
119 mbedtls_mpi MBEDTLS_PRIVATE(Vf); /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000120
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200121 int MBEDTLS_PRIVATE(padding); /*!< Selects padding mode:
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
123 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200124 int MBEDTLS_PRIVATE(hash_id); /*!< Hash identifier of mbedtls_md_type_t type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 as specified in md.h for use in the MGF
126 mask generating function used in the
127 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128#if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100129 /* Invariant: the mutex is initialized iff ver != 0. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200130 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex); /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200131#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000132}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200135#else /* MBEDTLS_RSA_ALT */
136#include "rsa_alt.h"
137#endif /* MBEDTLS_RSA_ALT */
138
Paul Bakker5121ce52009-01-03 21:22:43 +0000139/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000140 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200142 * \note This function initializes the padding and the hash
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200143 * identifier to respectively #MBEDTLS_RSA_PKCS_V15 and
144 * #MBEDTLS_MD_NONE. See mbedtls_rsa_set_padding() for more
145 * information about those parameters.
Ronald Cronc1905a12021-06-05 11:11:14 +0200146 *
147 * \param ctx The RSA context to initialize. This must not be \c NULL.
148 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100149void mbedtls_rsa_init(mbedtls_rsa_context *ctx);
Ronald Cronc1905a12021-06-05 11:11:14 +0200150
151/**
152 * \brief This function sets padding for an already initialized RSA
153 * context.
154 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000155 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000156 * encryption scheme and the RSASSA-PSS signature scheme.
157 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000158 * \note The \p hash_id parameter is ignored when using
159 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200160 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200161 * \note The choice of padding mode is strictly enforced for private
162 * key operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000163 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100164 * a default value, which can be overridden by calling specific
Ronald Cronc1905a12021-06-05 11:11:14 +0200165 * \c mbedtls_rsa_rsaes_xxx or \c mbedtls_rsa_rsassa_xxx
166 * functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200167 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000168 * \note The hash selected in \p hash_id is always used for OEAP
169 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100170 * making signatures, but can be overridden for verifying them.
171 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100172 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200173 * \param ctx The initialized RSA context to be configured.
Hanno Becker9a467772018-12-13 09:54:59 +0000174 * \param padding The padding mode to use. This must be either
175 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200176 * \param hash_id The hash identifier for PSS or OAEP, if \p padding is
177 * #MBEDTLS_RSA_PKCS_V21. #MBEDTLS_MD_NONE is accepted by this
178 * function but may be not suitable for some operations.
179 * Ignored if \p padding is #MBEDTLS_RSA_PKCS_V15.
Ronald Cronc1905a12021-06-05 11:11:14 +0200180 *
181 * \return \c 0 on success.
182 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING failure:
183 * \p padding or \p hash_id is invalid.
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100185int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
186 mbedtls_md_type_t hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
188/**
Yanray Wang83548b52023-03-15 16:46:34 +0800189 * \brief This function retrieves padding mode of initialized
190 * RSA context.
Yanray Wanga730df62023-03-01 10:18:19 +0800191 *
192 * \param ctx The initialized RSA context.
193 *
194 * \return RSA padding mode.
195 *
196 */
197int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx);
198
199/**
Yanray Wang12cb3962023-03-01 10:20:02 +0800200 * \brief This function retrieves hash identifier of mbedtls_md_type_t
201 * type.
202 *
203 * \param ctx The initialized RSA context.
204 *
205 * \return Hash identifier of mbedtls_md_type_t type.
206 *
207 */
Yanray Wangd41684e2023-03-17 18:54:22 +0800208int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx);
Yanray Wang12cb3962023-03-01 10:20:02 +0800209
210/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000211 * \brief This function imports a set of core parameters into an
212 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100213 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100214 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000215 * imports, if the parameters are not simultaneously present.
216 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100217 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000218 * by a call to mbedtls_rsa_complete(), which checks and
219 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100220 * public or private RSA key.
221 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000222 * \note See mbedtls_rsa_complete() for more information on which
223 * parameters are necessary to set up a private or public
224 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100225 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100226 * \note The imported parameters are copied and need not be preserved
227 * for the lifetime of the RSA context being set up.
228 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100229 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000230 * \param N The RSA modulus. This may be \c NULL.
231 * \param P The first prime factor of \p N. This may be \c NULL.
232 * \param Q The second prime factor of \p N. This may be \c NULL.
233 * \param D The private exponent. This may be \c NULL.
234 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100235 *
236 * \return \c 0 on success.
237 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100238 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100239int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
240 const mbedtls_mpi *N,
241 const mbedtls_mpi *P, const mbedtls_mpi *Q,
242 const mbedtls_mpi *D, const mbedtls_mpi *E);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100243
244/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000245 * \brief This function imports core RSA parameters, in raw big-endian
246 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100248 * \note This function can be called multiple times for successive
249 * imports, if the parameters are not simultaneously present.
250 *
251 * Any sequence of calls to this function should be followed
252 * by a call to mbedtls_rsa_complete(), which checks and
253 * completes the provided information to a ready-for-use
254 * public or private RSA key.
255 *
256 * \note See mbedtls_rsa_complete() for more information on which
257 * parameters are necessary to set up a private or public
258 * RSA key.
259 *
260 * \note The imported parameters are copied and need not be preserved
261 * for the lifetime of the RSA context being set up.
262 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000263 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000264 * \param N The RSA modulus. This may be \c NULL.
265 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
266 * \param P The first prime factor of \p N. This may be \c NULL.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000267 * \param P_len The Byte length of \p P; it is ignored if \p P == NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000268 * \param Q The second prime factor of \p N. This may be \c NULL.
269 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
270 * \param D The private exponent. This may be \c NULL.
271 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
272 * \param E The public exponent. This may be \c NULL.
273 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100275 * \return \c 0 on success.
276 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100277 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100278int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
279 unsigned char const *N, size_t N_len,
280 unsigned char const *P, size_t P_len,
281 unsigned char const *Q, size_t Q_len,
282 unsigned char const *D, size_t D_len,
283 unsigned char const *E, size_t E_len);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100284
285/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000286 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100287 * a set of imported core parameters.
288 *
Andrzej Kurek43dfd512022-02-18 08:10:37 -0500289 * To setup an RSA public key, precisely \c N and \c E
Rose Zadik042e97f2018-01-26 16:35:10 +0000290 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100291 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000292 * To setup an RSA private key, sufficient information must
293 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100294 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000295 * The default implementation supports the following:
Andrzej Kurek43dfd512022-02-18 08:10:37 -0500296 * <ul><li>Derive \c P, \c Q from \c N, \c D, \c E.</li>
297 * <li>Derive \c N, \c D from \c P, \c Q, \c E.</li></ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000298 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100299 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000300 * If this function runs successfully, it guarantees that
301 * the RSA context can be used for RSA operations without
302 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100303 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100304 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000305 * for the imported parameters. In particular, parameters that
306 * are not needed by the implementation might be silently
307 * discarded and left unchecked. To check the consistency
308 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100309 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100310 * \param ctx The initialized RSA context holding imported parameters.
311 *
312 * \return \c 0 on success.
313 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
314 * failed.
315 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100316 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100317int mbedtls_rsa_complete(mbedtls_rsa_context *ctx);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100318
319/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000320 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100321 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000322 * If this function runs successfully, the non-NULL buffers
323 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
324 * written, with additional unused space filled leading by
325 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100326 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000327 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300328 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000329 * <li>An alternative RSA implementation is in use, which
330 * stores the key externally, and either cannot or should
331 * not export it into RAM.</li>
332 * <li>A SW or HW implementation might not support a certain
333 * deduction. For example, \p P, \p Q from \p N, \p D,
334 * and \p E if the former are not part of the
335 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100336 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000337 * If the function fails due to an unsupported operation,
338 * the RSA context stays intact and remains usable.
339 *
340 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000341 * \param N The MPI to hold the RSA modulus.
342 * This may be \c NULL if this field need not be exported.
343 * \param P The MPI to hold the first prime factor of \p N.
344 * This may be \c NULL if this field need not be exported.
345 * \param Q The MPI to hold the second prime factor of \p N.
346 * This may be \c NULL if this field need not be exported.
347 * \param D The MPI to hold the private exponent.
348 * This may be \c NULL if this field need not be exported.
349 * \param E The MPI to hold the public exponent.
350 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000351 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100352 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300353 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000354 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100355 * functionality or because of security policies.
356 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100357 *
358 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100359int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
360 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
361 mbedtls_mpi *D, mbedtls_mpi *E);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100362
363/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000364 * \brief This function exports core parameters of an RSA key
365 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100366 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000367 * If this function runs successfully, the non-NULL buffers
368 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
369 * written, with additional unused space filled leading by
370 * zero Bytes.
371 *
372 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300373 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000374 * <li>An alternative RSA implementation is in use, which
375 * stores the key externally, and either cannot or should
376 * not export it into RAM.</li>
377 * <li>A SW or HW implementation might not support a certain
378 * deduction. For example, \p P, \p Q from \p N, \p D,
379 * and \p E if the former are not part of the
380 * implementation.</li></ul>
381 * If the function fails due to an unsupported operation,
382 * the RSA context stays intact and remains usable.
383 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100384 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100385 * buffer pointers are NULL.
386 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000387 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000388 * \param N The Byte array to store the RSA modulus,
389 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000390 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000391 * \param P The Byte array to hold the first prime factor of \p N,
392 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000393 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000394 * \param Q The Byte array to hold the second prime factor of \p N,
395 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000396 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000397 * \param D The Byte array to hold the private exponent,
398 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000399 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000400 * \param E The Byte array to hold the public exponent,
401 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000402 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100403 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100404 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300405 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000406 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100407 * functionality or because of security policies.
408 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100409 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100410int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
411 unsigned char *N, size_t N_len,
412 unsigned char *P, size_t P_len,
413 unsigned char *Q, size_t Q_len,
414 unsigned char *D, size_t D_len,
415 unsigned char *E, size_t E_len);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100416
417/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000418 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100419 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100420 * \note Alternative RSA implementations not using CRT-parameters
421 * internally can implement this function based on
422 * mbedtls_rsa_deduce_opt().
423 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000424 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000425 * \param DP The MPI to hold \c D modulo `P-1`,
426 * or \c NULL if it need not be exported.
427 * \param DQ The MPI to hold \c D modulo `Q-1`,
428 * or \c NULL if it need not be exported.
429 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
430 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100431 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100432 * \return \c 0 on success.
433 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100434 *
435 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100436int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
437 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100438
Paul Bakker5121ce52009-01-03 21:22:43 +0000439/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000440 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100441 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000442 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100443 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000444 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100445 *
446 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100447size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100448
449/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000450 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000452 * \note mbedtls_rsa_init() must be called before this function,
453 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 *
Hanno Becker9a467772018-12-13 09:54:59 +0000455 * \param ctx The initialized RSA context used to hold the key.
456 * \param f_rng The RNG function to be used for key generation.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100457 * This is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000458 * \param p_rng The RNG context to be passed to \p f_rng.
459 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100460 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000461 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000462 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100463 *
464 * \return \c 0 on success.
465 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100467int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
468 int (*f_rng)(void *, unsigned char *, size_t),
469 void *p_rng,
470 unsigned int nbits, int exponent);
Paul Bakker5121ce52009-01-03 21:22:43 +0000471
472/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000473 * \brief This function checks if a context contains at least an RSA
474 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000476 * If the function runs successfully, it is guaranteed that
477 * enough information is present to perform an RSA public key
478 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 *
Hanno Becker9a467772018-12-13 09:54:59 +0000480 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000481 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100482 * \return \c 0 on success.
483 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100484 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000485 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100486int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000487
488/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000489 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100490 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000491 *
Hanno Becker68767a62017-10-17 10:13:31 +0100492 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000493 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100494 * on the given context, but that the various parameters are
495 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000496 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100497 *
498 * \warning This function should catch accidental misconfigurations
499 * like swapping of parameters, but it cannot establish full
500 * trust in neither the quality nor the consistency of the key
501 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000502 * <ul><li>Consistency: Imported parameters that are irrelevant
503 * for the implementation might be silently dropped. If dropped,
504 * the current function does not have access to them,
505 * and therefore cannot check them. See mbedtls_rsa_complete().
506 * If you want to check the consistency of the entire
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100507 * content of a PKCS1-encoded RSA private key, for example, you
Rose Zadik042e97f2018-01-26 16:35:10 +0000508 * should use mbedtls_rsa_validate_params() before setting
509 * up the RSA context.
510 * Additionally, if the implementation performs empirical checks,
511 * these checks substantiate but do not guarantee consistency.</li>
512 * <li>Quality: This function is not expected to perform
513 * extended quality assessments like checking that the prime
514 * factors are safe. Additionally, it is the responsibility of the
515 * user to ensure the trustworthiness of the source of his RSA
516 * parameters, which goes beyond what is effectively checkable
517 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100518 *
Hanno Becker9a467772018-12-13 09:54:59 +0000519 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100520 *
521 * \return \c 0 on success.
522 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100524int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
526/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000527 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100528 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000529 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100530 *
Hanno Becker9a467772018-12-13 09:54:59 +0000531 * \param pub The initialized RSA context holding the public key.
532 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000533 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100534 * \return \c 0 on success.
535 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100536 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100537int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
538 const mbedtls_rsa_context *prv);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100539
540/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000541 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000542 *
Hanno Becker9a467772018-12-13 09:54:59 +0000543 * \param ctx The initialized RSA context to use.
544 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000545 * of length \c ctx->len Bytes. For example, \c 256 Bytes
546 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000547 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000548 * of length \c ctx->len Bytes. For example, \c 256 Bytes
549 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000550 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000551 * \note This function does not handle message padding.
552 *
553 * \note Make sure to set \p input[0] = 0 or ensure that
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500554 * input is smaller than \c N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000555 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100556 * \return \c 0 on success.
557 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100559int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
560 const unsigned char *input,
561 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000562
563/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000564 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 *
Hanno Becker24120612017-10-26 11:53:35 +0100566 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100567 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800568 * \note If blinding is used, both the base of exponentiation
Hanno Becker24120612017-10-26 11:53:35 +0100569 * and the exponent are blinded, providing protection
570 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100571 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100572 * \warning It is deprecated and a security risk to not provide
573 * a PRNG here and thereby prevent the use of blinding.
574 * Future versions of the library may enforce the presence
575 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100576 *
Hanno Becker9a467772018-12-13 09:54:59 +0000577 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100578 * \param f_rng The RNG function, used for blinding. It is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000579 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
Thomas Daubney03412782021-05-20 15:31:17 +0100580 * if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000581 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000582 * of length \c ctx->len Bytes. For example, \c 256 Bytes
583 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000584 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000585 * of length \c ctx->len Bytes. For example, \c 256 Bytes
586 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100587 *
588 * \return \c 0 on success.
589 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
590 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100592int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
593 int (*f_rng)(void *, unsigned char *, size_t),
594 void *p_rng,
595 const unsigned char *input,
596 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000597
598/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000599 * \brief This function adds the message padding, then performs an RSA
600 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000601 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000602 * It is the generic wrapper for performing a PKCS#1 encryption
Thomas Daubney21772772021-05-13 17:30:32 +0100603 * operation.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100604 *
Hanno Becker9a467772018-12-13 09:54:59 +0000605 * \param ctx The initialized RSA context to use.
Thomas Daubneyf54c5c52021-05-21 17:00:30 +0100606 * \param f_rng The RNG to use. It is used for padding generation
Thomas Daubney2c65db92021-05-21 10:58:28 +0100607 * and it is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000608 * \param p_rng The RNG context to be passed to \p f_rng. May be
Thomas Daubney03412782021-05-20 15:31:17 +0100609 * \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000610 * \param ilen The length of the plaintext in Bytes.
611 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000612 * buffer of size \p ilen Bytes. It may be \c NULL if
613 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000614 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000615 * of length \c ctx->len Bytes. For example, \c 256 Bytes
616 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100617 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100618 * \return \c 0 on success.
619 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100621int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
622 int (*f_rng)(void *, unsigned char *, size_t),
623 void *p_rng,
624 size_t ilen,
625 const unsigned char *input,
626 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
628/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000629 * \brief This function performs a PKCS#1 v1.5 encryption operation
630 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100631 *
Hanno Becker9a467772018-12-13 09:54:59 +0000632 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100633 * \param f_rng The RNG function to use. It is mandatory and used for
634 * padding generation.
Hanno Becker9a467772018-12-13 09:54:59 +0000635 * \param p_rng The RNG context to be passed to \p f_rng. This may
Thomas Daubney03412782021-05-20 15:31:17 +0100636 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000637 * \param ilen The length of the plaintext in Bytes.
638 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000639 * buffer of size \p ilen Bytes. It may be \c NULL if
640 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000641 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000642 * of length \c ctx->len Bytes. For example, \c 256 Bytes
643 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100644 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100645 * \return \c 0 on success.
646 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100647 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100648int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
649 int (*f_rng)(void *, unsigned char *, size_t),
650 void *p_rng,
651 size_t ilen,
652 const unsigned char *input,
653 unsigned char *output);
Paul Bakkerb3869132013-02-28 17:21:01 +0100654
655/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000656 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
657 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100658 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100659 * \note The output buffer must be as large as the size
660 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
661 *
Tom Cosgrove1e211442022-05-26 11:51:00 +0100662 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000663 * \param f_rng The RNG function to use. This is needed for padding
Thomas Daubney2c65db92021-05-21 10:58:28 +0100664 * generation and is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000665 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000666 * be \c NULL if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000667 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000668 * This must be a readable buffer of length \p label_len
669 * Bytes. It may be \c NULL if \p label_len is \c 0.
670 * \param label_len The length of the label in Bytes.
671 * \param ilen The length of the plaintext buffer \p input in Bytes.
672 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000673 * buffer of size \p ilen Bytes. It may be \c NULL if
674 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000675 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000676 * of length \c ctx->len Bytes. For example, \c 256 Bytes
677 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100678 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100679 * \return \c 0 on success.
680 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100681 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100682int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
683 int (*f_rng)(void *, unsigned char *, size_t),
684 void *p_rng,
685 const unsigned char *label, size_t label_len,
686 size_t ilen,
687 const unsigned char *input,
688 unsigned char *output);
Paul Bakkerb3869132013-02-28 17:21:01 +0100689
690/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000691 * \brief This function performs an RSA operation, then removes the
692 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000694 * It is the generic wrapper for performing a PKCS#1 decryption
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100695 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100697 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000698 * as large as the size \p ctx->len of \p ctx->N (for example,
699 * 128 Bytes if RSA-1024 is used) to be able to hold an
700 * arbitrary decrypted message. If it is not large enough to
701 * hold the decryption of the particular ciphertext provided,
702 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100703 *
Hanno Becker9a467772018-12-13 09:54:59 +0000704 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100705 * \param f_rng The RNG function. This is used for blinding and is
706 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000707 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100708 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000709 * \param olen The address at which to store the length of
710 * the plaintext. This must not be \c NULL.
711 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000712 * of length \c ctx->len Bytes. For example, \c 256 Bytes
713 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000714 * \param output The buffer used to hold the plaintext. This must
715 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000716 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100717 *
718 * \return \c 0 on success.
719 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000720 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
722 int (*f_rng)(void *, unsigned char *, size_t),
723 void *p_rng,
724 size_t *olen,
725 const unsigned char *input,
726 unsigned char *output,
727 size_t output_max_len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000728
729/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000730 * \brief This function performs a PKCS#1 v1.5 decryption
731 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100732 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100733 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000734 * as large as the size \p ctx->len of \p ctx->N, for example,
735 * 128 Bytes if RSA-1024 is used, to be able to hold an
736 * arbitrary decrypted message. If it is not large enough to
737 * hold the decryption of the particular ciphertext provided,
738 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100739 *
Hanno Becker9a467772018-12-13 09:54:59 +0000740 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100741 * \param f_rng The RNG function. This is used for blinding and is
742 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000743 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100744 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000745 * \param olen The address at which to store the length of
746 * the plaintext. This must not be \c NULL.
747 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000748 * of length \c ctx->len Bytes. For example, \c 256 Bytes
749 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000750 * \param output The buffer used to hold the plaintext. This must
751 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000752 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100753 *
754 * \return \c 0 on success.
755 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
756 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100757 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100758int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
759 int (*f_rng)(void *, unsigned char *, size_t),
760 void *p_rng,
761 size_t *olen,
762 const unsigned char *input,
763 unsigned char *output,
764 size_t output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +0100765
766/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100767 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
768 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100769 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100770 * \note The output buffer length \c output_max_len should be
771 * as large as the size \p ctx->len of \p ctx->N, for
772 * example, 128 Bytes if RSA-1024 is used, to be able to
773 * hold an arbitrary decrypted message. If it is not
774 * large enough to hold the decryption of the particular
775 * ciphertext provided, the function returns
776 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100777 *
Hanno Becker9a467772018-12-13 09:54:59 +0000778 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100779 * \param f_rng The RNG function. This is used for blinding and is
780 * mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000781 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100782 * \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100783 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000784 * This must be a readable buffer of length \p label_len
785 * Bytes. It may be \c NULL if \p label_len is \c 0.
786 * \param label_len The length of the label in Bytes.
787 * \param olen The address at which to store the length of
788 * the plaintext. This must not be \c NULL.
789 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000790 * of length \c ctx->len Bytes. For example, \c 256 Bytes
791 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000792 * \param output The buffer used to hold the plaintext. This must
793 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000794 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100795 *
796 * \return \c 0 on success.
797 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100798 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100799int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
800 int (*f_rng)(void *, unsigned char *, size_t),
801 void *p_rng,
802 const unsigned char *label, size_t label_len,
803 size_t *olen,
804 const unsigned char *input,
805 unsigned char *output,
806 size_t output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +0100807
808/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000809 * \brief This function performs a private RSA operation to sign
810 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000811 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000812 * It is the generic wrapper for performing a PKCS#1
Thomas Daubney140184d2021-05-18 16:04:07 +0100813 * signature.
Paul Bakker5121ce52009-01-03 21:22:43 +0000814 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000815 * \note The \p sig buffer must be as large as the size
816 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000817 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000818 * \note For PKCS#1 v2.1 encoding, see comments on
819 * mbedtls_rsa_rsassa_pss_sign() for details on
820 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100821 *
Hanno Becker9a467772018-12-13 09:54:59 +0000822 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100823 * \param f_rng The RNG function to use. This is mandatory and
824 * must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000825 * \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 +0100826 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100827 * \param md_alg The message-digest algorithm used to hash the original data.
828 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200829 * \param hashlen The length of the message digest or raw data in Bytes.
830 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
831 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000832 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200833 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000834 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000835 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100836 * for an 2048-bit RSA modulus. A buffer length of
837 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100838 *
839 * \return \c 0 if the signing operation was successful.
840 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000841 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100842int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
843 int (*f_rng)(void *, unsigned char *, size_t),
844 void *p_rng,
845 mbedtls_md_type_t md_alg,
846 unsigned int hashlen,
847 const unsigned char *hash,
848 unsigned char *sig);
Paul Bakker5121ce52009-01-03 21:22:43 +0000849
850/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000851 * \brief This function performs a PKCS#1 v1.5 signature
852 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100853 *
Hanno Becker9a467772018-12-13 09:54:59 +0000854 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100855 * \param f_rng The RNG function. This is used for blinding and is
856 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000857 * \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 +0100858 * if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000859 * \param md_alg The message-digest algorithm used to hash the original data.
860 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200861 * \param hashlen The length of the message digest or raw data in Bytes.
862 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
863 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000864 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200865 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000866 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000867 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100868 * for an 2048-bit RSA modulus. A buffer length of
869 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100870 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100871 * \return \c 0 if the signing operation was successful.
872 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100873 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100874int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
875 int (*f_rng)(void *, unsigned char *, size_t),
876 void *p_rng,
877 mbedtls_md_type_t md_alg,
878 unsigned int hashlen,
879 const unsigned char *hash,
880 unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +0100881
882/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000883 * \brief This function performs a PKCS#1 v2.1 PSS signature
884 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100885 *
Janos Follathb7953322021-04-01 14:44:17 +0100886 * \note The \c hash_id set in \p ctx by calling
887 * mbedtls_rsa_set_padding() selects the hash used for the
888 * encoding operation and for the mask generation function
889 * (MGF1). For more details on the encoding operation and the
890 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +0000891 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100892 * Specifications</em>.
Rose Zadike8b5b992018-03-27 12:19:47 +0100893 *
Cédric Meuter010ddc22020-04-25 09:24:11 +0200894 * \note This function enforces that the provided salt length complies
895 * with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 v2.2) §9.1.1
896 * step 3. The constraint is that the hash length plus the salt
897 * length plus 2 bytes must be at most the key length. If this
898 * constraint is not met, this function returns
Jaeden Amero3725bb22018-09-07 19:12:36 +0100899 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
900 *
Hanno Becker9a467772018-12-13 09:54:59 +0000901 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100902 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000903 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
904 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100905 * \param md_alg The message-digest algorithm used to hash the original data.
906 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200907 * \param hashlen The length of the message digest or raw data in Bytes.
908 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
909 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000910 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200911 * This must be a readable buffer of at least \p hashlen Bytes.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200912 * \param saltlen The length of the salt that should be used.
Cédric Meuter010ddc22020-04-25 09:24:11 +0200913 * If passed #MBEDTLS_RSA_SALT_LEN_ANY, the function will use
914 * the largest possible salt length up to the hash length,
915 * which is the largest permitted by some standards including
916 * FIPS 186-4 §5.5.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200917 * \param sig The buffer to hold the signature. This must be a writable
918 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
919 * for an 2048-bit RSA modulus. A buffer length of
920 * #MBEDTLS_MPI_MAX_SIZE is always safe.
921 *
922 * \return \c 0 if the signing operation was successful.
923 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
924 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100925int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
926 int (*f_rng)(void *, unsigned char *, size_t),
927 void *p_rng,
928 mbedtls_md_type_t md_alg,
929 unsigned int hashlen,
930 const unsigned char *hash,
931 int saltlen,
932 unsigned char *sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200933
934/**
935 * \brief This function performs a PKCS#1 v2.1 PSS signature
936 * operation (RSASSA-PSS-SIGN).
937 *
Janos Follathb7953322021-04-01 14:44:17 +0100938 * \note The \c hash_id set in \p ctx by calling
939 * mbedtls_rsa_set_padding() selects the hash used for the
940 * encoding operation and for the mask generation function
941 * (MGF1). For more details on the encoding operation and the
942 * mask generation function, consult <em>RFC-3447: Public-Key
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200943 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100944 * Specifications</em>.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200945 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000946 * \note This function always uses the maximum possible salt size,
947 * up to the length of the payload hash. This choice of salt
948 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
949 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
Rose Zadike8b5b992018-03-27 12:19:47 +0100950 * minimum salt size which is the hash size minus 2 bytes. If
951 * this minimum size is too large given the key size (the salt
952 * size, plus the hash size, plus 2 bytes must be no more than
953 * the key size in bytes), this function returns
954 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
955 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100956 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100957 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100958 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
959 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100960 * \param md_alg The message-digest algorithm used to hash the original data.
961 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200962 * \param hashlen The length of the message digest or raw data in Bytes.
963 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
964 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000965 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200966 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000967 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000968 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100969 * for an 2048-bit RSA modulus. A buffer length of
970 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100971 *
972 * \return \c 0 if the signing operation was successful.
973 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100974 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100975int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
976 int (*f_rng)(void *, unsigned char *, size_t),
977 void *p_rng,
978 mbedtls_md_type_t md_alg,
979 unsigned int hashlen,
980 const unsigned char *hash,
981 unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +0100982
983/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000984 * \brief This function performs a public RSA operation and checks
985 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +0000986 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000987 * This is the generic wrapper for performing a PKCS#1
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100988 * verification.
Paul Bakker5121ce52009-01-03 21:22:43 +0000989 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000990 * \note For PKCS#1 v2.1 encoding, see comments on
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500991 * mbedtls_rsa_rsassa_pss_verify() about \c md_alg and
992 * \c hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100993 *
Hanno Becker9a467772018-12-13 09:54:59 +0000994 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +0100995 * \param md_alg The message-digest algorithm used to hash the original data.
996 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200997 * \param hashlen The length of the message digest or raw data in Bytes.
998 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
999 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001000 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001001 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001002 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001003 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1004 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001005 *
1006 * \return \c 0 if the verify operation was successful.
1007 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001008 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001009int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
1010 mbedtls_md_type_t md_alg,
1011 unsigned int hashlen,
1012 const unsigned char *hash,
1013 const unsigned char *sig);
Paul Bakker5121ce52009-01-03 21:22:43 +00001014
1015/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001016 * \brief This function performs a PKCS#1 v1.5 verification
1017 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001018 *
Hanno Becker9a467772018-12-13 09:54:59 +00001019 * \param ctx The initialized RSA public key context to use.
Rose Zadik042e97f2018-01-26 16:35:10 +00001020 * \param md_alg The message-digest algorithm used to hash the original data.
1021 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001022 * \param hashlen The length of the message digest or raw data in Bytes.
1023 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1024 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001025 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001026 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001027 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001028 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1029 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001030 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001031 * \return \c 0 if the verify operation was successful.
1032 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001033 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001034int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
1035 mbedtls_md_type_t md_alg,
1036 unsigned int hashlen,
1037 const unsigned char *hash,
1038 const unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001039
1040/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001041 * \brief This function performs a PKCS#1 v2.1 PSS verification
1042 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001043 *
Janos Follathb7953322021-04-01 14:44:17 +01001044 * \note The \c hash_id set in \p ctx by calling
1045 * mbedtls_rsa_set_padding() selects the hash used for the
1046 * encoding operation and for the mask generation function
1047 * (MGF1). For more details on the encoding operation and the
1048 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +00001049 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +01001050 * Specifications</em>. If the \c hash_id set in \p ctx by
1051 * mbedtls_rsa_set_padding() is #MBEDTLS_MD_NONE, the \p md_alg
1052 * parameter is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001053 *
Hanno Becker9a467772018-12-13 09:54:59 +00001054 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001055 * \param md_alg The message-digest algorithm used to hash the original data.
1056 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001057 * \param hashlen The length of the message digest or raw data in Bytes.
1058 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1059 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001060 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001061 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001062 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001063 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1064 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001065 *
1066 * \return \c 0 if the verify operation was successful.
1067 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001068 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001069int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
1070 mbedtls_md_type_t md_alg,
1071 unsigned int hashlen,
1072 const unsigned char *hash,
1073 const unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001074
1075/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001076 * \brief This function performs a PKCS#1 v2.1 PSS verification
1077 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001078 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001079 * \note The \p sig buffer must be as large as the size
1080 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001081 *
Janos Follathb7953322021-04-01 14:44:17 +01001082 * \note The \c hash_id set in \p ctx by mbedtls_rsa_set_padding() is
1083 * ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001084 *
Hanno Becker9a467772018-12-13 09:54:59 +00001085 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001086 * \param md_alg The message-digest algorithm used to hash the original data.
1087 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001088 * \param hashlen The length of the message digest or raw data in Bytes.
1089 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1090 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001091 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001092 * This must be a readable buffer of at least \p hashlen Bytes.
Janos Follathb7953322021-04-01 14:44:17 +01001093 * \param mgf1_hash_id The message digest algorithm used for the
1094 * verification operation and the mask generation
1095 * function (MGF1). For more details on the encoding
1096 * operation and the mask generation function, consult
1097 * <em>RFC-3447: Public-Key Cryptography Standards
1098 * (PKCS) #1 v2.1: RSA Cryptography
1099 * Specifications</em>.
Hanno Becker9a467772018-12-13 09:54:59 +00001100 * \param expected_salt_len The length of the salt used in padding. Use
1101 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1102 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001103 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1104 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001105 *
1106 * \return \c 0 if the verify operation was successful.
1107 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001108 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001109int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
1110 mbedtls_md_type_t md_alg,
1111 unsigned int hashlen,
1112 const unsigned char *hash,
1113 mbedtls_md_type_t mgf1_hash_id,
1114 int expected_salt_len,
1115 const unsigned char *sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001116
1117/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001118 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001119 *
Hanno Becker9a467772018-12-13 09:54:59 +00001120 * \param dst The destination context. This must be initialized.
1121 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001122 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001123 * \return \c 0 on success.
1124 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001125 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001126int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src);
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001127
1128/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001129 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001130 *
Hanno Becker9a467772018-12-13 09:54:59 +00001131 * \param ctx The RSA context to free. May be \c NULL, in which case
1132 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001133 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001134 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001135void mbedtls_rsa_free(mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00001136
Ron Eldorfa8f6352017-06-20 15:48:46 +03001137#if defined(MBEDTLS_SELF_TEST)
1138
Paul Bakker5121ce52009-01-03 21:22:43 +00001139/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001140 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001141 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001142 * \return \c 0 on success.
1143 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001144 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001145int mbedtls_rsa_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +00001146
Ron Eldorfa8f6352017-06-20 15:48:46 +03001147#endif /* MBEDTLS_SELF_TEST */
1148
Paul Bakker5121ce52009-01-03 21:22:43 +00001149#ifdef __cplusplus
1150}
1151#endif
1152
Paul Bakker5121ce52009-01-03 21:22:43 +00001153#endif /* rsa.h */