blob: ba3a7605ddb6076e691d933ca6755e3c2730a29c [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
Paul Bakker5121ce52009-01-03 21:22:43 +000088/**
Rose Zadik042e97f2018-01-26 16:35:10 +000089 * \brief The RSA context structure.
Paul Bakker5121ce52009-01-03 21:22:43 +000090 */
Gilles Peskine449bd832023-01-11 14:50:10 +010091typedef struct mbedtls_rsa_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020092 int MBEDTLS_PRIVATE(ver); /*!< Reserved for internal purposes.
Gilles Peskine449bd832023-01-11 14:50:10 +010093 * Do not set this field in application
94 * code. Its meaning might change without
95 * notice. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020096 size_t MBEDTLS_PRIVATE(len); /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +000097
Mateusz Starzyk846f0212021-05-19 19:44:07 +020098 mbedtls_mpi MBEDTLS_PRIVATE(N); /*!< The public modulus. */
99 mbedtls_mpi MBEDTLS_PRIVATE(E); /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200101 mbedtls_mpi MBEDTLS_PRIVATE(D); /*!< The private exponent. */
102 mbedtls_mpi MBEDTLS_PRIVATE(P); /*!< The first prime factor. */
103 mbedtls_mpi MBEDTLS_PRIVATE(Q); /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100104
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200105 mbedtls_mpi MBEDTLS_PRIVATE(DP); /*!< <code>D % (P - 1)</code>. */
106 mbedtls_mpi MBEDTLS_PRIVATE(DQ); /*!< <code>D % (Q - 1)</code>. */
107 mbedtls_mpi MBEDTLS_PRIVATE(QP); /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200109 mbedtls_mpi MBEDTLS_PRIVATE(RN); /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200111 mbedtls_mpi MBEDTLS_PRIVATE(RP); /*!< cached <code>R^2 mod P</code>. */
112 mbedtls_mpi MBEDTLS_PRIVATE(RQ); /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200113
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200114 mbedtls_mpi MBEDTLS_PRIVATE(Vi); /*!< The cached blinding value. */
115 mbedtls_mpi MBEDTLS_PRIVATE(Vf); /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000116
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200117 int MBEDTLS_PRIVATE(padding); /*!< Selects padding mode:
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
119 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200120 int MBEDTLS_PRIVATE(hash_id); /*!< Hash identifier of mbedtls_md_type_t type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 as specified in md.h for use in the MGF
122 mask generating function used in the
123 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100125 /* Invariant: the mutex is initialized iff ver != 0. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200126 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex); /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200127#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000128}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200131#else /* MBEDTLS_RSA_ALT */
132#include "rsa_alt.h"
133#endif /* MBEDTLS_RSA_ALT */
134
Paul Bakker5121ce52009-01-03 21:22:43 +0000135/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000136 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200138 * \note This function initializes the padding and the hash
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200139 * identifier to respectively #MBEDTLS_RSA_PKCS_V15 and
140 * #MBEDTLS_MD_NONE. See mbedtls_rsa_set_padding() for more
141 * information about those parameters.
Ronald Cronc1905a12021-06-05 11:11:14 +0200142 *
143 * \param ctx The RSA context to initialize. This must not be \c NULL.
144 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100145void mbedtls_rsa_init(mbedtls_rsa_context *ctx);
Ronald Cronc1905a12021-06-05 11:11:14 +0200146
147/**
148 * \brief This function sets padding for an already initialized RSA
149 * context.
150 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000151 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000152 * encryption scheme and the RSASSA-PSS signature scheme.
153 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000154 * \note The \p hash_id parameter is ignored when using
155 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200156 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200157 * \note The choice of padding mode is strictly enforced for private
158 * key operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000159 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100160 * a default value, which can be overridden by calling specific
Ronald Cronc1905a12021-06-05 11:11:14 +0200161 * \c mbedtls_rsa_rsaes_xxx or \c mbedtls_rsa_rsassa_xxx
162 * functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200163 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000164 * \note The hash selected in \p hash_id is always used for OEAP
165 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100166 * making signatures, but can be overridden for verifying them.
167 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100168 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200169 * \param ctx The initialized RSA context to be configured.
Hanno Becker9a467772018-12-13 09:54:59 +0000170 * \param padding The padding mode to use. This must be either
171 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200172 * \param hash_id The hash identifier for PSS or OAEP, if \p padding is
173 * #MBEDTLS_RSA_PKCS_V21. #MBEDTLS_MD_NONE is accepted by this
174 * function but may be not suitable for some operations.
175 * Ignored if \p padding is #MBEDTLS_RSA_PKCS_V15.
Ronald Cronc1905a12021-06-05 11:11:14 +0200176 *
177 * \return \c 0 on success.
178 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING failure:
179 * \p padding or \p hash_id is invalid.
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100181int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
182 mbedtls_md_type_t hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
184/**
Yanray Wanga730df62023-03-01 10:18:19 +0800185 * \brief This function retrieves padding mode of RSA modulus.
186 *
187 * \param ctx The initialized RSA context.
188 *
189 * \return RSA padding mode.
190 *
191 */
192int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx);
193
194/**
Yanray Wang12cb3962023-03-01 10:20:02 +0800195 * \brief This function retrieves hash identifier of mbedtls_md_type_t
196 * type.
197 *
198 * \param ctx The initialized RSA context.
199 *
200 * \return Hash identifier of mbedtls_md_type_t type.
201 *
202 */
203int mbedtls_rsa_get_hash_id(const mbedtls_rsa_context *ctx);
204
205/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000206 * \brief This function imports a set of core parameters into an
207 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100208 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100209 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000210 * imports, if the parameters are not simultaneously present.
211 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100212 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000213 * by a call to mbedtls_rsa_complete(), which checks and
214 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100215 * public or private RSA key.
216 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000217 * \note See mbedtls_rsa_complete() for more information on which
218 * parameters are necessary to set up a private or public
219 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100220 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100221 * \note The imported parameters are copied and need not be preserved
222 * for the lifetime of the RSA context being set up.
223 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100224 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000225 * \param N The RSA modulus. This may be \c NULL.
226 * \param P The first prime factor of \p N. This may be \c NULL.
227 * \param Q The second prime factor of \p N. This may be \c NULL.
228 * \param D The private exponent. This may be \c NULL.
229 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100230 *
231 * \return \c 0 on success.
232 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100233 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100234int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
235 const mbedtls_mpi *N,
236 const mbedtls_mpi *P, const mbedtls_mpi *Q,
237 const mbedtls_mpi *D, const mbedtls_mpi *E);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100238
239/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000240 * \brief This function imports core RSA parameters, in raw big-endian
241 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100243 * \note This function can be called multiple times for successive
244 * imports, if the parameters are not simultaneously present.
245 *
246 * Any sequence of calls to this function should be followed
247 * by a call to mbedtls_rsa_complete(), which checks and
248 * completes the provided information to a ready-for-use
249 * public or private RSA key.
250 *
251 * \note See mbedtls_rsa_complete() for more information on which
252 * parameters are necessary to set up a private or public
253 * RSA key.
254 *
255 * \note The imported parameters are copied and need not be preserved
256 * for the lifetime of the RSA context being set up.
257 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000258 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000259 * \param N The RSA modulus. This may be \c NULL.
260 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
261 * \param P The first prime factor of \p N. This may be \c NULL.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000262 * \param P_len The Byte length of \p P; it is ignored if \p P == NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000263 * \param Q The second prime factor of \p N. This may be \c NULL.
264 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
265 * \param D The private exponent. This may be \c NULL.
266 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
267 * \param E The public exponent. This may be \c NULL.
268 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100270 * \return \c 0 on success.
271 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100272 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
274 unsigned char const *N, size_t N_len,
275 unsigned char const *P, size_t P_len,
276 unsigned char const *Q, size_t Q_len,
277 unsigned char const *D, size_t D_len,
278 unsigned char const *E, size_t E_len);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100279
280/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000281 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100282 * a set of imported core parameters.
283 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000284 * To setup an RSA public key, precisely \p N and \p E
285 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100286 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000287 * To setup an RSA private key, sufficient information must
288 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100289 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000290 * The default implementation supports the following:
291 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
292 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
293 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100294 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000295 * If this function runs successfully, it guarantees that
296 * the RSA context can be used for RSA operations without
297 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100298 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100299 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000300 * for the imported parameters. In particular, parameters that
301 * are not needed by the implementation might be silently
302 * discarded and left unchecked. To check the consistency
303 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100304 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100305 * \param ctx The initialized RSA context holding imported parameters.
306 *
307 * \return \c 0 on success.
308 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
309 * failed.
310 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100311 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100312int mbedtls_rsa_complete(mbedtls_rsa_context *ctx);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100313
314/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000315 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100316 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000317 * If this function runs successfully, the non-NULL buffers
318 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
319 * written, with additional unused space filled leading by
320 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100321 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000322 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300323 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000324 * <li>An alternative RSA implementation is in use, which
325 * stores the key externally, and either cannot or should
326 * not export it into RAM.</li>
327 * <li>A SW or HW implementation might not support a certain
328 * deduction. For example, \p P, \p Q from \p N, \p D,
329 * and \p E if the former are not part of the
330 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100331 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000332 * If the function fails due to an unsupported operation,
333 * the RSA context stays intact and remains usable.
334 *
335 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000336 * \param N The MPI to hold the RSA modulus.
337 * This may be \c NULL if this field need not be exported.
338 * \param P The MPI to hold the first prime factor of \p N.
339 * This may be \c NULL if this field need not be exported.
340 * \param Q The MPI to hold the second prime factor of \p N.
341 * This may be \c NULL if this field need not be exported.
342 * \param D The MPI to hold the private exponent.
343 * This may be \c NULL if this field need not be exported.
344 * \param E The MPI to hold the public exponent.
345 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000346 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100347 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300348 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000349 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100350 * functionality or because of security policies.
351 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100352 *
353 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
355 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
356 mbedtls_mpi *D, mbedtls_mpi *E);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100357
358/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000359 * \brief This function exports core parameters of an RSA key
360 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100361 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000362 * If this function runs successfully, the non-NULL buffers
363 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
364 * written, with additional unused space filled leading by
365 * zero Bytes.
366 *
367 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300368 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000369 * <li>An alternative RSA implementation is in use, which
370 * stores the key externally, and either cannot or should
371 * not export it into RAM.</li>
372 * <li>A SW or HW implementation might not support a certain
373 * deduction. For example, \p P, \p Q from \p N, \p D,
374 * and \p E if the former are not part of the
375 * implementation.</li></ul>
376 * If the function fails due to an unsupported operation,
377 * the RSA context stays intact and remains usable.
378 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100379 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100380 * buffer pointers are NULL.
381 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000382 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000383 * \param N The Byte array to store the RSA modulus,
384 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000385 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000386 * \param P The Byte array to hold the first prime factor of \p N,
387 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000388 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000389 * \param Q The Byte array to hold the second prime factor of \p N,
390 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000391 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000392 * \param D The Byte array to hold the private exponent,
393 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000394 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000395 * \param E The Byte array to hold the public exponent,
396 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000397 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100398 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100399 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300400 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000401 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100402 * functionality or because of security policies.
403 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100404 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100405int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
406 unsigned char *N, size_t N_len,
407 unsigned char *P, size_t P_len,
408 unsigned char *Q, size_t Q_len,
409 unsigned char *D, size_t D_len,
410 unsigned char *E, size_t E_len);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100411
412/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000413 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100414 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100415 * \note Alternative RSA implementations not using CRT-parameters
416 * internally can implement this function based on
417 * mbedtls_rsa_deduce_opt().
418 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000419 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000420 * \param DP The MPI to hold \c D modulo `P-1`,
421 * or \c NULL if it need not be exported.
422 * \param DQ The MPI to hold \c D modulo `Q-1`,
423 * or \c NULL if it need not be exported.
424 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
425 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100426 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100427 * \return \c 0 on success.
428 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100429 *
430 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100431int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
432 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100433
Paul Bakker5121ce52009-01-03 21:22:43 +0000434/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000435 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100436 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000437 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100438 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000439 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100440 *
441 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100442size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100443
444/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000445 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000447 * \note mbedtls_rsa_init() must be called before this function,
448 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000449 *
Hanno Becker9a467772018-12-13 09:54:59 +0000450 * \param ctx The initialized RSA context used to hold the key.
451 * \param f_rng The RNG function to be used for key generation.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100452 * This is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000453 * \param p_rng The RNG context to be passed to \p f_rng.
454 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100455 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000456 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000457 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100458 *
459 * \return \c 0 on success.
460 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100462int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
463 int (*f_rng)(void *, unsigned char *, size_t),
464 void *p_rng,
465 unsigned int nbits, int exponent);
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
467/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000468 * \brief This function checks if a context contains at least an RSA
469 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000471 * If the function runs successfully, it is guaranteed that
472 * enough information is present to perform an RSA public key
473 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000474 *
Hanno Becker9a467772018-12-13 09:54:59 +0000475 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000476 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100477 * \return \c 0 on success.
478 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100479 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100481int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000482
483/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000484 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100485 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000486 *
Hanno Becker68767a62017-10-17 10:13:31 +0100487 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000488 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100489 * on the given context, but that the various parameters are
490 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000491 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100492 *
493 * \warning This function should catch accidental misconfigurations
494 * like swapping of parameters, but it cannot establish full
495 * trust in neither the quality nor the consistency of the key
496 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000497 * <ul><li>Consistency: Imported parameters that are irrelevant
498 * for the implementation might be silently dropped. If dropped,
499 * the current function does not have access to them,
500 * and therefore cannot check them. See mbedtls_rsa_complete().
501 * If you want to check the consistency of the entire
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100502 * content of a PKCS1-encoded RSA private key, for example, you
Rose Zadik042e97f2018-01-26 16:35:10 +0000503 * should use mbedtls_rsa_validate_params() before setting
504 * up the RSA context.
505 * Additionally, if the implementation performs empirical checks,
506 * these checks substantiate but do not guarantee consistency.</li>
507 * <li>Quality: This function is not expected to perform
508 * extended quality assessments like checking that the prime
509 * factors are safe. Additionally, it is the responsibility of the
510 * user to ensure the trustworthiness of the source of his RSA
511 * parameters, which goes beyond what is effectively checkable
512 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100513 *
Hanno Becker9a467772018-12-13 09:54:59 +0000514 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100515 *
516 * \return \c 0 on success.
517 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100519int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
521/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000522 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100523 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000524 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100525 *
Hanno Becker9a467772018-12-13 09:54:59 +0000526 * \param pub The initialized RSA context holding the public key.
527 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000528 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100529 * \return \c 0 on success.
530 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100531 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100532int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
533 const mbedtls_rsa_context *prv);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100534
535/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000536 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 *
Hanno Becker9a467772018-12-13 09:54:59 +0000538 * \param ctx The initialized RSA context to use.
539 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000540 * of length \c ctx->len Bytes. For example, \c 256 Bytes
541 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000542 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000543 * of length \c ctx->len Bytes. For example, \c 256 Bytes
544 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000545 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000546 * \note This function does not handle message padding.
547 *
548 * \note Make sure to set \p input[0] = 0 or ensure that
549 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100551 * \return \c 0 on success.
552 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100554int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
555 const unsigned char *input,
556 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
558/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000559 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 *
Hanno Becker24120612017-10-26 11:53:35 +0100561 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100562 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800563 * \note If blinding is used, both the base of exponentiation
Hanno Becker24120612017-10-26 11:53:35 +0100564 * and the exponent are blinded, providing protection
565 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100566 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100567 * \warning It is deprecated and a security risk to not provide
568 * a PRNG here and thereby prevent the use of blinding.
569 * Future versions of the library may enforce the presence
570 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100571 *
Hanno Becker9a467772018-12-13 09:54:59 +0000572 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100573 * \param f_rng The RNG function, used for blinding. It is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000574 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
Thomas Daubney03412782021-05-20 15:31:17 +0100575 * if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000576 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000577 * of length \c ctx->len Bytes. For example, \c 256 Bytes
578 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000579 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000580 * of length \c ctx->len Bytes. For example, \c 256 Bytes
581 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100582 *
583 * \return \c 0 on success.
584 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
585 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100587int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
588 int (*f_rng)(void *, unsigned char *, size_t),
589 void *p_rng,
590 const unsigned char *input,
591 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
593/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000594 * \brief This function adds the message padding, then performs an RSA
595 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000596 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000597 * It is the generic wrapper for performing a PKCS#1 encryption
Thomas Daubney21772772021-05-13 17:30:32 +0100598 * operation.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100599 *
Hanno Becker9a467772018-12-13 09:54:59 +0000600 * \param ctx The initialized RSA context to use.
Thomas Daubneyf54c5c52021-05-21 17:00:30 +0100601 * \param f_rng The RNG to use. It is used for padding generation
Thomas Daubney2c65db92021-05-21 10:58:28 +0100602 * and it is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000603 * \param p_rng The RNG context to be passed to \p f_rng. May be
Thomas Daubney03412782021-05-20 15:31:17 +0100604 * \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000605 * \param ilen The length of the plaintext in Bytes.
606 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000607 * buffer of size \p ilen Bytes. It may be \c NULL if
608 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000609 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000610 * of length \c ctx->len Bytes. For example, \c 256 Bytes
611 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100612 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100613 * \return \c 0 on success.
614 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000615 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100616int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
617 int (*f_rng)(void *, unsigned char *, size_t),
618 void *p_rng,
619 size_t ilen,
620 const unsigned char *input,
621 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
623/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000624 * \brief This function performs a PKCS#1 v1.5 encryption operation
625 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100626 *
Hanno Becker9a467772018-12-13 09:54:59 +0000627 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100628 * \param f_rng The RNG function to use. It is mandatory and used for
629 * padding generation.
Hanno Becker9a467772018-12-13 09:54:59 +0000630 * \param p_rng The RNG context to be passed to \p f_rng. This may
Thomas Daubney03412782021-05-20 15:31:17 +0100631 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000632 * \param ilen The length of the plaintext in Bytes.
633 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000634 * buffer of size \p ilen Bytes. It may be \c NULL if
635 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000636 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000637 * of length \c ctx->len Bytes. For example, \c 256 Bytes
638 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100639 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100640 * \return \c 0 on success.
641 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100642 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100643int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
644 int (*f_rng)(void *, unsigned char *, size_t),
645 void *p_rng,
646 size_t ilen,
647 const unsigned char *input,
648 unsigned char *output);
Paul Bakkerb3869132013-02-28 17:21:01 +0100649
650/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000651 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
652 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100653 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100654 * \note The output buffer must be as large as the size
655 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
656 *
Tom Cosgrove1e211442022-05-26 11:51:00 +0100657 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000658 * \param f_rng The RNG function to use. This is needed for padding
Thomas Daubney2c65db92021-05-21 10:58:28 +0100659 * generation and is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000660 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000661 * be \c NULL if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000662 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000663 * This must be a readable buffer of length \p label_len
664 * Bytes. It may be \c NULL if \p label_len is \c 0.
665 * \param label_len The length of the label in Bytes.
666 * \param ilen The length of the plaintext buffer \p input in Bytes.
667 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000668 * buffer of size \p ilen Bytes. It may be \c NULL if
669 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000670 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000671 * of length \c ctx->len Bytes. For example, \c 256 Bytes
672 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100673 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100674 * \return \c 0 on success.
675 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100676 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100677int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
678 int (*f_rng)(void *, unsigned char *, size_t),
679 void *p_rng,
680 const unsigned char *label, size_t label_len,
681 size_t ilen,
682 const unsigned char *input,
683 unsigned char *output);
Paul Bakkerb3869132013-02-28 17:21:01 +0100684
685/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000686 * \brief This function performs an RSA operation, then removes the
687 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000688 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000689 * It is the generic wrapper for performing a PKCS#1 decryption
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100690 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000691 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100692 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000693 * as large as the size \p ctx->len of \p ctx->N (for example,
694 * 128 Bytes if RSA-1024 is used) to be able to hold an
695 * arbitrary decrypted message. If it is not large enough to
696 * hold the decryption of the particular ciphertext provided,
697 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100698 *
Hanno Becker9a467772018-12-13 09:54:59 +0000699 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100700 * \param f_rng The RNG function. This is used for blinding and is
701 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000702 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100703 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000704 * \param olen The address at which to store the length of
705 * the plaintext. This must not be \c NULL.
706 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000707 * of length \c ctx->len Bytes. For example, \c 256 Bytes
708 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000709 * \param output The buffer used to hold the plaintext. This must
710 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000711 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100712 *
713 * \return \c 0 on success.
714 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100716int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
717 int (*f_rng)(void *, unsigned char *, size_t),
718 void *p_rng,
719 size_t *olen,
720 const unsigned char *input,
721 unsigned char *output,
722 size_t output_max_len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000723
724/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000725 * \brief This function performs a PKCS#1 v1.5 decryption
726 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100727 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100728 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000729 * as large as the size \p ctx->len of \p ctx->N, for example,
730 * 128 Bytes if RSA-1024 is used, to be able to hold an
731 * arbitrary decrypted message. If it is not large enough to
732 * hold the decryption of the particular ciphertext provided,
733 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100734 *
Hanno Becker9a467772018-12-13 09:54:59 +0000735 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100736 * \param f_rng The RNG function. This is used for blinding and is
737 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000738 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100739 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000740 * \param olen The address at which to store the length of
741 * the plaintext. This must not be \c NULL.
742 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000743 * of length \c ctx->len Bytes. For example, \c 256 Bytes
744 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000745 * \param output The buffer used to hold the plaintext. This must
746 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000747 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100748 *
749 * \return \c 0 on success.
750 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
751 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100752 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100753int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
754 int (*f_rng)(void *, unsigned char *, size_t),
755 void *p_rng,
756 size_t *olen,
757 const unsigned char *input,
758 unsigned char *output,
759 size_t output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +0100760
761/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100762 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
763 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100764 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100765 * \note The output buffer length \c output_max_len should be
766 * as large as the size \p ctx->len of \p ctx->N, for
767 * example, 128 Bytes if RSA-1024 is used, to be able to
768 * hold an arbitrary decrypted message. If it is not
769 * large enough to hold the decryption of the particular
770 * ciphertext provided, the function returns
771 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100772 *
Hanno Becker9a467772018-12-13 09:54:59 +0000773 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100774 * \param f_rng The RNG function. This is used for blinding and is
775 * mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000776 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100777 * \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100778 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000779 * This must be a readable buffer of length \p label_len
780 * Bytes. It may be \c NULL if \p label_len is \c 0.
781 * \param label_len The length of the label in Bytes.
782 * \param olen The address at which to store the length of
783 * the plaintext. This must not be \c NULL.
784 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000785 * of length \c ctx->len Bytes. For example, \c 256 Bytes
786 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000787 * \param output The buffer used to hold the plaintext. This must
788 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000789 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100790 *
791 * \return \c 0 on success.
792 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100793 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100794int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
795 int (*f_rng)(void *, unsigned char *, size_t),
796 void *p_rng,
797 const unsigned char *label, size_t label_len,
798 size_t *olen,
799 const unsigned char *input,
800 unsigned char *output,
801 size_t output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +0100802
803/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000804 * \brief This function performs a private RSA operation to sign
805 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000806 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000807 * It is the generic wrapper for performing a PKCS#1
Thomas Daubney140184d2021-05-18 16:04:07 +0100808 * signature.
Paul Bakker5121ce52009-01-03 21:22:43 +0000809 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000810 * \note The \p sig buffer must be as large as the size
811 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000812 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000813 * \note For PKCS#1 v2.1 encoding, see comments on
814 * mbedtls_rsa_rsassa_pss_sign() for details on
815 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100816 *
Hanno Becker9a467772018-12-13 09:54:59 +0000817 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100818 * \param f_rng The RNG function to use. This is mandatory and
819 * must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000820 * \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 +0100821 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100822 * \param md_alg The message-digest algorithm used to hash the original data.
823 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200824 * \param hashlen The length of the message digest or raw data in Bytes.
825 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
826 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000827 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200828 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000829 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000830 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100831 * for an 2048-bit RSA modulus. A buffer length of
832 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100833 *
834 * \return \c 0 if the signing operation was successful.
835 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000836 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100837int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
838 int (*f_rng)(void *, unsigned char *, size_t),
839 void *p_rng,
840 mbedtls_md_type_t md_alg,
841 unsigned int hashlen,
842 const unsigned char *hash,
843 unsigned char *sig);
Paul Bakker5121ce52009-01-03 21:22:43 +0000844
845/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000846 * \brief This function performs a PKCS#1 v1.5 signature
847 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100848 *
Hanno Becker9a467772018-12-13 09:54:59 +0000849 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100850 * \param f_rng The RNG function. This is used for blinding and is
851 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000852 * \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 +0100853 * if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000854 * \param md_alg The message-digest algorithm used to hash the original data.
855 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200856 * \param hashlen The length of the message digest or raw data in Bytes.
857 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
858 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000859 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200860 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000861 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000862 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100863 * for an 2048-bit RSA modulus. A buffer length of
864 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100865 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100866 * \return \c 0 if the signing operation was successful.
867 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100868 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100869int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
870 int (*f_rng)(void *, unsigned char *, size_t),
871 void *p_rng,
872 mbedtls_md_type_t md_alg,
873 unsigned int hashlen,
874 const unsigned char *hash,
875 unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +0100876
877/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000878 * \brief This function performs a PKCS#1 v2.1 PSS signature
879 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100880 *
Janos Follathb7953322021-04-01 14:44:17 +0100881 * \note The \c hash_id set in \p ctx by calling
882 * mbedtls_rsa_set_padding() selects the hash used for the
883 * encoding operation and for the mask generation function
884 * (MGF1). For more details on the encoding operation and the
885 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +0000886 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100887 * Specifications</em>.
Rose Zadike8b5b992018-03-27 12:19:47 +0100888 *
Cédric Meuter010ddc22020-04-25 09:24:11 +0200889 * \note This function enforces that the provided salt length complies
890 * with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 v2.2) §9.1.1
891 * step 3. The constraint is that the hash length plus the salt
892 * length plus 2 bytes must be at most the key length. If this
893 * constraint is not met, this function returns
Jaeden Amero3725bb22018-09-07 19:12:36 +0100894 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
895 *
Hanno Becker9a467772018-12-13 09:54:59 +0000896 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100897 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000898 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
899 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100900 * \param md_alg The message-digest algorithm used to hash the original data.
901 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200902 * \param hashlen The length of the message digest or raw data in Bytes.
903 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
904 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000905 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200906 * This must be a readable buffer of at least \p hashlen Bytes.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200907 * \param saltlen The length of the salt that should be used.
Cédric Meuter010ddc22020-04-25 09:24:11 +0200908 * If passed #MBEDTLS_RSA_SALT_LEN_ANY, the function will use
909 * the largest possible salt length up to the hash length,
910 * which is the largest permitted by some standards including
911 * FIPS 186-4 §5.5.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200912 * \param sig The buffer to hold the signature. This must be a writable
913 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
914 * for an 2048-bit RSA modulus. A buffer length of
915 * #MBEDTLS_MPI_MAX_SIZE is always safe.
916 *
917 * \return \c 0 if the signing operation was successful.
918 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
919 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100920int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
921 int (*f_rng)(void *, unsigned char *, size_t),
922 void *p_rng,
923 mbedtls_md_type_t md_alg,
924 unsigned int hashlen,
925 const unsigned char *hash,
926 int saltlen,
927 unsigned char *sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200928
929/**
930 * \brief This function performs a PKCS#1 v2.1 PSS signature
931 * operation (RSASSA-PSS-SIGN).
932 *
Janos Follathb7953322021-04-01 14:44:17 +0100933 * \note The \c hash_id set in \p ctx by calling
934 * mbedtls_rsa_set_padding() selects the hash used for the
935 * encoding operation and for the mask generation function
936 * (MGF1). For more details on the encoding operation and the
937 * mask generation function, consult <em>RFC-3447: Public-Key
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200938 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100939 * Specifications</em>.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200940 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000941 * \note This function always uses the maximum possible salt size,
942 * up to the length of the payload hash. This choice of salt
943 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
944 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
Rose Zadike8b5b992018-03-27 12:19:47 +0100945 * minimum salt size which is the hash size minus 2 bytes. If
946 * this minimum size is too large given the key size (the salt
947 * size, plus the hash size, plus 2 bytes must be no more than
948 * the key size in bytes), this function returns
949 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
950 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100951 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100952 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100953 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
954 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100955 * \param md_alg The message-digest algorithm used to hash the original data.
956 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200957 * \param hashlen The length of the message digest or raw data in Bytes.
958 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
959 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000960 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200961 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000962 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000963 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100964 * for an 2048-bit RSA modulus. A buffer length of
965 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100966 *
967 * \return \c 0 if the signing operation was successful.
968 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100969 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100970int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
971 int (*f_rng)(void *, unsigned char *, size_t),
972 void *p_rng,
973 mbedtls_md_type_t md_alg,
974 unsigned int hashlen,
975 const unsigned char *hash,
976 unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +0100977
978/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000979 * \brief This function performs a public RSA operation and checks
980 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +0000981 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000982 * This is the generic wrapper for performing a PKCS#1
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100983 * verification.
Paul Bakker5121ce52009-01-03 21:22:43 +0000984 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000985 * \note For PKCS#1 v2.1 encoding, see comments on
986 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
987 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100988 *
Hanno Becker9a467772018-12-13 09:54:59 +0000989 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +0100990 * \param md_alg The message-digest algorithm used to hash the original data.
991 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200992 * \param hashlen The length of the message digest or raw data in Bytes.
993 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
994 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000995 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200996 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000997 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +0000998 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
999 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001000 *
1001 * \return \c 0 if the verify operation was successful.
1002 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001003 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001004int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
1005 mbedtls_md_type_t md_alg,
1006 unsigned int hashlen,
1007 const unsigned char *hash,
1008 const unsigned char *sig);
Paul Bakker5121ce52009-01-03 21:22:43 +00001009
1010/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001011 * \brief This function performs a PKCS#1 v1.5 verification
1012 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001013 *
Hanno Becker9a467772018-12-13 09:54:59 +00001014 * \param ctx The initialized RSA public key context to use.
Rose Zadik042e97f2018-01-26 16:35:10 +00001015 * \param md_alg The message-digest algorithm used to hash the original data.
1016 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001017 * \param hashlen The length of the message digest or raw data in Bytes.
1018 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1019 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001020 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001021 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001022 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001023 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1024 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001025 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001026 * \return \c 0 if the verify operation was successful.
1027 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001028 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001029int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
1030 mbedtls_md_type_t md_alg,
1031 unsigned int hashlen,
1032 const unsigned char *hash,
1033 const unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001034
1035/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001036 * \brief This function performs a PKCS#1 v2.1 PSS verification
1037 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001038 *
Janos Follathb7953322021-04-01 14:44:17 +01001039 * \note The \c hash_id set in \p ctx by calling
1040 * mbedtls_rsa_set_padding() selects the hash used for the
1041 * encoding operation and for the mask generation function
1042 * (MGF1). For more details on the encoding operation and the
1043 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +00001044 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +01001045 * Specifications</em>. If the \c hash_id set in \p ctx by
1046 * mbedtls_rsa_set_padding() is #MBEDTLS_MD_NONE, the \p md_alg
1047 * parameter is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001048 *
Hanno Becker9a467772018-12-13 09:54:59 +00001049 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001050 * \param md_alg The message-digest algorithm used to hash the original data.
1051 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001052 * \param hashlen The length of the message digest or raw data in Bytes.
1053 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1054 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001055 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001056 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001057 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001058 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1059 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001060 *
1061 * \return \c 0 if the verify operation was successful.
1062 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001063 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001064int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
1065 mbedtls_md_type_t md_alg,
1066 unsigned int hashlen,
1067 const unsigned char *hash,
1068 const unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001069
1070/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001071 * \brief This function performs a PKCS#1 v2.1 PSS verification
1072 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001073 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001074 * \note The \p sig buffer must be as large as the size
1075 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001076 *
Janos Follathb7953322021-04-01 14:44:17 +01001077 * \note The \c hash_id set in \p ctx by mbedtls_rsa_set_padding() is
1078 * ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001079 *
Hanno Becker9a467772018-12-13 09:54:59 +00001080 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001081 * \param md_alg The message-digest algorithm used to hash the original data.
1082 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001083 * \param hashlen The length of the message digest or raw data in Bytes.
1084 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1085 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001086 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001087 * This must be a readable buffer of at least \p hashlen Bytes.
Janos Follathb7953322021-04-01 14:44:17 +01001088 * \param mgf1_hash_id The message digest algorithm used for the
1089 * verification operation and the mask generation
1090 * function (MGF1). For more details on the encoding
1091 * operation and the mask generation function, consult
1092 * <em>RFC-3447: Public-Key Cryptography Standards
1093 * (PKCS) #1 v2.1: RSA Cryptography
1094 * Specifications</em>.
Hanno Becker9a467772018-12-13 09:54:59 +00001095 * \param expected_salt_len The length of the salt used in padding. Use
1096 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1097 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001098 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1099 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001100 *
1101 * \return \c 0 if the verify operation was successful.
1102 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001103 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001104int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
1105 mbedtls_md_type_t md_alg,
1106 unsigned int hashlen,
1107 const unsigned char *hash,
1108 mbedtls_md_type_t mgf1_hash_id,
1109 int expected_salt_len,
1110 const unsigned char *sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001111
1112/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001113 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001114 *
Hanno Becker9a467772018-12-13 09:54:59 +00001115 * \param dst The destination context. This must be initialized.
1116 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001117 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001118 * \return \c 0 on success.
1119 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001120 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001121int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src);
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001122
1123/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001124 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001125 *
Hanno Becker9a467772018-12-13 09:54:59 +00001126 * \param ctx The RSA context to free. May be \c NULL, in which case
1127 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001128 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001129 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001130void mbedtls_rsa_free(mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00001131
Ron Eldorfa8f6352017-06-20 15:48:46 +03001132#if defined(MBEDTLS_SELF_TEST)
1133
Paul Bakker5121ce52009-01-03 21:22:43 +00001134/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001135 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001137 * \return \c 0 on success.
1138 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001139 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001140int mbedtls_rsa_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +00001141
Ron Eldorfa8f6352017-06-20 15:48:46 +03001142#endif /* MBEDTLS_SELF_TEST */
1143
Paul Bakker5121ce52009-01-03 21:22:43 +00001144#ifdef __cplusplus
1145}
1146#endif
1147
Paul Bakker5121ce52009-01-03 21:22:43 +00001148#endif /* rsa.h */