blob: cc839f21980d6f4a7220e1cdc0c9e6d9064d911b [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
Dave Rodgmane3c05852023-11-03 12:21:36 +000014 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +000015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#ifndef MBEDTLS_RSA_H
17#define MBEDTLS_RSA_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020018#include "mbedtls/private_access.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000019
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakkered27a042013-04-18 22:46:23 +020021
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010022#include "mbedtls/bignum.h"
23#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#if defined(MBEDTLS_THREADING_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010026#include "mbedtls/threading.h"
Paul Bakkerc9965dc2013-09-29 14:58:17 +020027#endif
28
Paul Bakker13e2dfe2009-07-28 07:18:38 +000029/*
30 * RSA Error codes
31 */
Gilles Peskined2971572021-07-26 18:48:10 +020032/** Bad input parameters to function. */
33#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080
34/** Input data contains invalid padding and is rejected. */
35#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100
36/** Something failed during generation of a key. */
37#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180
38/** Key failed to pass the validity check of the library. */
39#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200
40/** The public key operation failed. */
41#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280
42/** The private key operation failed. */
43#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300
44/** The PKCS#1 verification failed. */
45#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380
46/** The output buffer for decryption is not large enough. */
47#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400
48/** The random generator failed to generate non-zeros. */
49#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480
Ron Eldor9924bdc2018-10-04 10:59:13 +030050
Paul Bakker5121ce52009-01-03 21:22:43 +000051/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020052 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000053 */
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Rose Zadike8b5b992018-03-27 12:19:47 +010055#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
56#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Rose Zadik042e97f2018-01-26 16:35:10 +000058#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
59#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020062
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020063/*
64 * The above constants may be used even if the RSA module is compile out,
Shaun Case8b0ecbc2021-12-20 21:14:10 -080065 * eg for alternative (PKCS#11) RSA implementations in the PK layers.
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020066 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010067
Paul Bakker407a0da2013-06-27 14:29:21 +020068#ifdef __cplusplus
69extern "C" {
70#endif
71
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +000072#if !defined(MBEDTLS_RSA_GEN_KEY_MIN_BITS)
73#define MBEDTLS_RSA_GEN_KEY_MIN_BITS 1024
74#elif MBEDTLS_RSA_GEN_KEY_MIN_BITS < 128
75#error "MBEDTLS_RSA_GEN_KEY_MIN_BITS must be at least 128 bits"
Waleed Elmelegyab570712023-07-05 16:40:58 +000076#endif
77
Paul Bakker5121ce52009-01-03 21:22:43 +000078/**
Rose Zadik042e97f2018-01-26 16:35:10 +000079 * \brief The RSA context structure.
Paul Bakker5121ce52009-01-03 21:22:43 +000080 */
Gilles Peskine449bd832023-01-11 14:50:10 +010081typedef struct mbedtls_rsa_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020082 int MBEDTLS_PRIVATE(ver); /*!< Reserved for internal purposes.
Gilles Peskine449bd832023-01-11 14:50:10 +010083 * Do not set this field in application
84 * code. Its meaning might change without
85 * notice. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020086 size_t MBEDTLS_PRIVATE(len); /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +000087
Mateusz Starzyk846f0212021-05-19 19:44:07 +020088 mbedtls_mpi MBEDTLS_PRIVATE(N); /*!< The public modulus. */
89 mbedtls_mpi MBEDTLS_PRIVATE(E); /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +000090
Mateusz Starzyk846f0212021-05-19 19:44:07 +020091 mbedtls_mpi MBEDTLS_PRIVATE(D); /*!< The private exponent. */
92 mbedtls_mpi MBEDTLS_PRIVATE(P); /*!< The first prime factor. */
93 mbedtls_mpi MBEDTLS_PRIVATE(Q); /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +010094
Mateusz Starzyk846f0212021-05-19 19:44:07 +020095 mbedtls_mpi MBEDTLS_PRIVATE(DP); /*!< <code>D % (P - 1)</code>. */
96 mbedtls_mpi MBEDTLS_PRIVATE(DQ); /*!< <code>D % (Q - 1)</code>. */
97 mbedtls_mpi MBEDTLS_PRIVATE(QP); /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +000098
Mateusz Starzyk846f0212021-05-19 19:44:07 +020099 mbedtls_mpi MBEDTLS_PRIVATE(RN); /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200101 mbedtls_mpi MBEDTLS_PRIVATE(RP); /*!< cached <code>R^2 mod P</code>. */
102 mbedtls_mpi MBEDTLS_PRIVATE(RQ); /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200103
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200104 mbedtls_mpi MBEDTLS_PRIVATE(Vi); /*!< The cached blinding value. */
105 mbedtls_mpi MBEDTLS_PRIVATE(Vf); /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000106
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200107 int MBEDTLS_PRIVATE(padding); /*!< Selects padding mode:
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
109 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200110 int MBEDTLS_PRIVATE(hash_id); /*!< Hash identifier of mbedtls_md_type_t type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 as specified in md.h for use in the MGF
112 mask generating function used in the
113 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100115 /* Invariant: the mutex is initialized iff ver != 0. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200116 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex); /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200117#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000118}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
Paul Bakker5121ce52009-01-03 21:22:43 +0000121/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000122 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200124 * \note This function initializes the padding and the hash
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200125 * identifier to respectively #MBEDTLS_RSA_PKCS_V15 and
126 * #MBEDTLS_MD_NONE. See mbedtls_rsa_set_padding() for more
127 * information about those parameters.
Ronald Cronc1905a12021-06-05 11:11:14 +0200128 *
129 * \param ctx The RSA context to initialize. This must not be \c NULL.
130 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100131void mbedtls_rsa_init(mbedtls_rsa_context *ctx);
Ronald Cronc1905a12021-06-05 11:11:14 +0200132
133/**
134 * \brief This function sets padding for an already initialized RSA
135 * context.
136 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000137 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000138 * encryption scheme and the RSASSA-PSS signature scheme.
139 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000140 * \note The \p hash_id parameter is ignored when using
141 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200142 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200143 * \note The choice of padding mode is strictly enforced for private
144 * key operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000145 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100146 * a default value, which can be overridden by calling specific
Ronald Cronc1905a12021-06-05 11:11:14 +0200147 * \c mbedtls_rsa_rsaes_xxx or \c mbedtls_rsa_rsassa_xxx
148 * functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200149 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000150 * \note The hash selected in \p hash_id is always used for OEAP
151 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100152 * making signatures, but can be overridden for verifying them.
153 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100154 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200155 * \param ctx The initialized RSA context to be configured.
Hanno Becker9a467772018-12-13 09:54:59 +0000156 * \param padding The padding mode to use. This must be either
157 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200158 * \param hash_id The hash identifier for PSS or OAEP, if \p padding is
159 * #MBEDTLS_RSA_PKCS_V21. #MBEDTLS_MD_NONE is accepted by this
160 * function but may be not suitable for some operations.
161 * Ignored if \p padding is #MBEDTLS_RSA_PKCS_V15.
Ronald Cronc1905a12021-06-05 11:11:14 +0200162 *
163 * \return \c 0 on success.
164 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING failure:
165 * \p padding or \p hash_id is invalid.
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100167int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
168 mbedtls_md_type_t hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
170/**
Yanray Wang83548b52023-03-15 16:46:34 +0800171 * \brief This function retrieves padding mode of initialized
172 * RSA context.
Yanray Wanga730df62023-03-01 10:18:19 +0800173 *
174 * \param ctx The initialized RSA context.
175 *
176 * \return RSA padding mode.
177 *
178 */
179int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx);
180
181/**
Yanray Wang12cb3962023-03-01 10:20:02 +0800182 * \brief This function retrieves hash identifier of mbedtls_md_type_t
183 * type.
184 *
185 * \param ctx The initialized RSA context.
186 *
187 * \return Hash identifier of mbedtls_md_type_t type.
188 *
189 */
Yanray Wangd41684e2023-03-17 18:54:22 +0800190int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx);
Yanray Wang12cb3962023-03-01 10:20:02 +0800191
192/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000193 * \brief This function imports a set of core parameters into an
194 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100195 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100196 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000197 * imports, if the parameters are not simultaneously present.
198 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100199 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000200 * by a call to mbedtls_rsa_complete(), which checks and
201 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100202 * public or private RSA key.
203 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000204 * \note See mbedtls_rsa_complete() for more information on which
205 * parameters are necessary to set up a private or public
206 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100207 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100208 * \note The imported parameters are copied and need not be preserved
209 * for the lifetime of the RSA context being set up.
210 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100211 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000212 * \param N The RSA modulus. This may be \c NULL.
213 * \param P The first prime factor of \p N. This may be \c NULL.
214 * \param Q The second prime factor of \p N. This may be \c NULL.
215 * \param D The private exponent. This may be \c NULL.
216 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100217 *
218 * \return \c 0 on success.
219 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100220 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100221int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
222 const mbedtls_mpi *N,
223 const mbedtls_mpi *P, const mbedtls_mpi *Q,
224 const mbedtls_mpi *D, const mbedtls_mpi *E);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100225
226/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000227 * \brief This function imports core RSA parameters, in raw big-endian
228 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100230 * \note This function can be called multiple times for successive
231 * imports, if the parameters are not simultaneously present.
232 *
233 * Any sequence of calls to this function should be followed
234 * by a call to mbedtls_rsa_complete(), which checks and
235 * completes the provided information to a ready-for-use
236 * public or private RSA key.
237 *
238 * \note See mbedtls_rsa_complete() for more information on which
239 * parameters are necessary to set up a private or public
240 * RSA key.
241 *
242 * \note The imported parameters are copied and need not be preserved
243 * for the lifetime of the RSA context being set up.
244 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000245 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000246 * \param N The RSA modulus. This may be \c NULL.
247 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
248 * \param P The first prime factor of \p N. This may be \c NULL.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000249 * \param P_len The Byte length of \p P; it is ignored if \p P == NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000250 * \param Q The second prime factor of \p N. This may be \c NULL.
251 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
252 * \param D The private exponent. This may be \c NULL.
253 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
254 * \param E The public exponent. This may be \c NULL.
255 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000256 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100257 * \return \c 0 on success.
258 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100259 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100260int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
261 unsigned char const *N, size_t N_len,
262 unsigned char const *P, size_t P_len,
263 unsigned char const *Q, size_t Q_len,
264 unsigned char const *D, size_t D_len,
265 unsigned char const *E, size_t E_len);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100266
267/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000268 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100269 * a set of imported core parameters.
270 *
Andrzej Kurek43dfd512022-02-18 08:10:37 -0500271 * To setup an RSA public key, precisely \c N and \c E
Rose Zadik042e97f2018-01-26 16:35:10 +0000272 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100273 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000274 * To setup an RSA private key, sufficient information must
275 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100276 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000277 * The default implementation supports the following:
Andrzej Kurek43dfd512022-02-18 08:10:37 -0500278 * <ul><li>Derive \c P, \c Q from \c N, \c D, \c E.</li>
279 * <li>Derive \c N, \c D from \c P, \c Q, \c E.</li></ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000280 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100281 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000282 * If this function runs successfully, it guarantees that
283 * the RSA context can be used for RSA operations without
284 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100285 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100286 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000287 * for the imported parameters. In particular, parameters that
288 * are not needed by the implementation might be silently
289 * discarded and left unchecked. To check the consistency
290 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100291 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100292 * \param ctx The initialized RSA context holding imported parameters.
293 *
294 * \return \c 0 on success.
295 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
296 * failed.
297 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100298 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100299int mbedtls_rsa_complete(mbedtls_rsa_context *ctx);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100300
301/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000302 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100303 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000304 * If this function runs successfully, the non-NULL buffers
305 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
306 * written, with additional unused space filled leading by
307 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100308 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000309 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300310 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000311 * <li>An alternative RSA implementation is in use, which
312 * stores the key externally, and either cannot or should
313 * not export it into RAM.</li>
314 * <li>A SW or HW implementation might not support a certain
315 * deduction. For example, \p P, \p Q from \p N, \p D,
316 * and \p E if the former are not part of the
317 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100318 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000319 * If the function fails due to an unsupported operation,
320 * the RSA context stays intact and remains usable.
321 *
322 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000323 * \param N The MPI to hold the RSA modulus.
324 * This may be \c NULL if this field need not be exported.
325 * \param P The MPI to hold the first prime factor of \p N.
326 * This may be \c NULL if this field need not be exported.
327 * \param Q The MPI to hold the second prime factor of \p N.
328 * This may be \c NULL if this field need not be exported.
329 * \param D The MPI to hold the private exponent.
330 * This may be \c NULL if this field need not be exported.
331 * \param E The MPI to hold the public exponent.
332 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000333 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100334 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300335 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000336 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100337 * functionality or because of security policies.
338 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100339 *
340 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100341int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
342 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
343 mbedtls_mpi *D, mbedtls_mpi *E);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100344
345/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000346 * \brief This function exports core parameters of an RSA key
347 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100348 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000349 * If this function runs successfully, the non-NULL buffers
350 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
351 * written, with additional unused space filled leading by
352 * zero Bytes.
353 *
354 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300355 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000356 * <li>An alternative RSA implementation is in use, which
357 * stores the key externally, and either cannot or should
358 * not export it into RAM.</li>
359 * <li>A SW or HW implementation might not support a certain
360 * deduction. For example, \p P, \p Q from \p N, \p D,
361 * and \p E if the former are not part of the
362 * implementation.</li></ul>
363 * If the function fails due to an unsupported operation,
364 * the RSA context stays intact and remains usable.
365 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100366 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100367 * buffer pointers are NULL.
368 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000369 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000370 * \param N The Byte array to store the RSA modulus,
371 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000372 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000373 * \param P The Byte array to hold the first prime factor of \p N,
374 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000375 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000376 * \param Q The Byte array to hold the second prime factor of \p N,
377 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000378 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000379 * \param D The Byte array to hold the private exponent,
380 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000381 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000382 * \param E The Byte array to hold the public exponent,
383 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000384 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100385 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100386 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300387 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000388 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100389 * functionality or because of security policies.
390 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100391 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100392int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
393 unsigned char *N, size_t N_len,
394 unsigned char *P, size_t P_len,
395 unsigned char *Q, size_t Q_len,
396 unsigned char *D, size_t D_len,
397 unsigned char *E, size_t E_len);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100398
399/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000400 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100401 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100402 * \note Alternative RSA implementations not using CRT-parameters
403 * internally can implement this function based on
404 * mbedtls_rsa_deduce_opt().
405 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000406 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000407 * \param DP The MPI to hold \c D modulo `P-1`,
408 * or \c NULL if it need not be exported.
409 * \param DQ The MPI to hold \c D modulo `Q-1`,
410 * or \c NULL if it need not be exported.
411 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
412 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100413 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100414 * \return \c 0 on success.
415 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100416 *
417 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100418int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
419 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100420
Paul Bakker5121ce52009-01-03 21:22:43 +0000421/**
Gilles Peskine19f1adf2024-02-01 22:17:44 +0100422 * \brief This function retrieves the length of the RSA modulus in bits.
423 *
424 * \param ctx The initialized RSA context.
425 *
426 * \return The length of the RSA modulus in bits.
427 *
428 */
429size_t mbedtls_rsa_get_bitlen(const mbedtls_rsa_context *ctx);
430
431/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000432 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100433 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000434 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100435 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000436 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100437 *
438 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100439size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100440
441/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000442 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000443 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000444 * \note mbedtls_rsa_init() must be called before this function,
445 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 *
Hanno Becker9a467772018-12-13 09:54:59 +0000447 * \param ctx The initialized RSA context used to hold the key.
448 * \param f_rng The RNG function to be used for key generation.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100449 * This is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000450 * \param p_rng The RNG context to be passed to \p f_rng.
451 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100452 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000453 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000454 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100455 *
456 * \return \c 0 on success.
457 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100459int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
460 int (*f_rng)(void *, unsigned char *, size_t),
461 void *p_rng,
462 unsigned int nbits, int exponent);
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
464/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000465 * \brief This function checks if a context contains at least an RSA
466 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000467 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000468 * If the function runs successfully, it is guaranteed that
469 * enough information is present to perform an RSA public key
470 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000471 *
Hanno Becker9a467772018-12-13 09:54:59 +0000472 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000473 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100474 * \return \c 0 on success.
475 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100476 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
480/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000481 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100482 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 *
Hanno Becker68767a62017-10-17 10:13:31 +0100484 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000485 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100486 * on the given context, but that the various parameters are
487 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000488 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100489 *
490 * \warning This function should catch accidental misconfigurations
491 * like swapping of parameters, but it cannot establish full
492 * trust in neither the quality nor the consistency of the key
493 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000494 * <ul><li>Consistency: Imported parameters that are irrelevant
495 * for the implementation might be silently dropped. If dropped,
496 * the current function does not have access to them,
497 * and therefore cannot check them. See mbedtls_rsa_complete().
498 * If you want to check the consistency of the entire
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100499 * content of a PKCS1-encoded RSA private key, for example, you
Rose Zadik042e97f2018-01-26 16:35:10 +0000500 * should use mbedtls_rsa_validate_params() before setting
501 * up the RSA context.
502 * Additionally, if the implementation performs empirical checks,
503 * these checks substantiate but do not guarantee consistency.</li>
504 * <li>Quality: This function is not expected to perform
505 * extended quality assessments like checking that the prime
506 * factors are safe. Additionally, it is the responsibility of the
507 * user to ensure the trustworthiness of the source of his RSA
508 * parameters, which goes beyond what is effectively checkable
509 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100510 *
Hanno Becker9a467772018-12-13 09:54:59 +0000511 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100512 *
513 * \return \c 0 on success.
514 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100516int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
518/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000519 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100520 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000521 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100522 *
Hanno Becker9a467772018-12-13 09:54:59 +0000523 * \param pub The initialized RSA context holding the public key.
524 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000525 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100526 * \return \c 0 on success.
527 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100528 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100529int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
530 const mbedtls_rsa_context *prv);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100531
532/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000533 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 *
Hanno Becker9a467772018-12-13 09:54:59 +0000535 * \param ctx The initialized RSA context to use.
536 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000537 * of length \c ctx->len Bytes. For example, \c 256 Bytes
538 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000539 * \param output The output buffer. This must be a writable 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 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000543 * \note This function does not handle message padding.
544 *
545 * \note Make sure to set \p input[0] = 0 or ensure that
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500546 * input is smaller than \c N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100548 * \return \c 0 on success.
549 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100551int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
552 const unsigned char *input,
553 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
555/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000556 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 *
Hanno Becker24120612017-10-26 11:53:35 +0100558 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100559 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800560 * \note If blinding is used, both the base of exponentiation
Hanno Becker24120612017-10-26 11:53:35 +0100561 * and the exponent are blinded, providing protection
562 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100563 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100564 * \warning It is deprecated and a security risk to not provide
565 * a PRNG here and thereby prevent the use of blinding.
566 * Future versions of the library may enforce the presence
567 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100568 *
Hanno Becker9a467772018-12-13 09:54:59 +0000569 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100570 * \param f_rng The RNG function, used for blinding. It is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000571 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
Thomas Daubney03412782021-05-20 15:31:17 +0100572 * if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000573 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000574 * of length \c ctx->len Bytes. For example, \c 256 Bytes
575 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000576 * \param output The output buffer. This must be a writable 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.
Rose Zadike8b5b992018-03-27 12:19:47 +0100579 *
580 * \return \c 0 on success.
581 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
582 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000583 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100584int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
585 int (*f_rng)(void *, unsigned char *, size_t),
586 void *p_rng,
587 const unsigned char *input,
588 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
590/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000591 * \brief This function adds the message padding, then performs an RSA
592 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000594 * It is the generic wrapper for performing a PKCS#1 encryption
Thomas Daubney21772772021-05-13 17:30:32 +0100595 * operation.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100596 *
Hanno Becker9a467772018-12-13 09:54:59 +0000597 * \param ctx The initialized RSA context to use.
Thomas Daubneyf54c5c52021-05-21 17:00:30 +0100598 * \param f_rng The RNG to use. It is used for padding generation
Thomas Daubney2c65db92021-05-21 10:58:28 +0100599 * and it is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000600 * \param p_rng The RNG context to be passed to \p f_rng. May be
Thomas Daubney03412782021-05-20 15:31:17 +0100601 * \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000602 * \param ilen The length of the plaintext in Bytes.
603 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000604 * buffer of size \p ilen Bytes. It may be \c NULL if
605 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000606 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000607 * of length \c ctx->len Bytes. For example, \c 256 Bytes
608 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100609 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100610 * \return \c 0 on success.
611 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000612 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100613int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
614 int (*f_rng)(void *, unsigned char *, size_t),
615 void *p_rng,
616 size_t ilen,
617 const unsigned char *input,
618 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
620/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000621 * \brief This function performs a PKCS#1 v1.5 encryption operation
622 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100623 *
Hanno Becker9a467772018-12-13 09:54:59 +0000624 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100625 * \param f_rng The RNG function to use. It is mandatory and used for
626 * padding generation.
Hanno Becker9a467772018-12-13 09:54:59 +0000627 * \param p_rng The RNG context to be passed to \p f_rng. This may
Thomas Daubney03412782021-05-20 15:31:17 +0100628 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000629 * \param ilen The length of the plaintext in Bytes.
630 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000631 * buffer of size \p ilen Bytes. It may be \c NULL if
632 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000633 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000634 * of length \c ctx->len Bytes. For example, \c 256 Bytes
635 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100636 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100637 * \return \c 0 on success.
638 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100639 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100640int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
641 int (*f_rng)(void *, unsigned char *, size_t),
642 void *p_rng,
643 size_t ilen,
644 const unsigned char *input,
645 unsigned char *output);
Paul Bakkerb3869132013-02-28 17:21:01 +0100646
647/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000648 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
649 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100650 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100651 * \note The output buffer must be as large as the size
652 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
653 *
Tom Cosgrove1e211442022-05-26 11:51:00 +0100654 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000655 * \param f_rng The RNG function to use. This is needed for padding
Thomas Daubney2c65db92021-05-21 10:58:28 +0100656 * generation and is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000657 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000658 * be \c NULL if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000659 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000660 * This must be a readable buffer of length \p label_len
661 * Bytes. It may be \c NULL if \p label_len is \c 0.
662 * \param label_len The length of the label in Bytes.
663 * \param ilen The length of the plaintext buffer \p input in Bytes.
664 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000665 * buffer of size \p ilen Bytes. It may be \c NULL if
666 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000667 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000668 * of length \c ctx->len Bytes. For example, \c 256 Bytes
669 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100670 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100671 * \return \c 0 on success.
672 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100673 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100674int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
675 int (*f_rng)(void *, unsigned char *, size_t),
676 void *p_rng,
677 const unsigned char *label, size_t label_len,
678 size_t ilen,
679 const unsigned char *input,
680 unsigned char *output);
Paul Bakkerb3869132013-02-28 17:21:01 +0100681
682/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000683 * \brief This function performs an RSA operation, then removes the
684 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000686 * It is the generic wrapper for performing a PKCS#1 decryption
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100687 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000688 *
Janos Follath393df9c2023-12-29 11:14:58 +0000689 * \warning When \p ctx->padding is set to #MBEDTLS_RSA_PKCS_V15,
690 * mbedtls_rsa_rsaes_pkcs1_v15_decrypt() is called, which is an
691 * inherently dangerous function (CWE-242).
692 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100693 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000694 * as large as the size \p ctx->len of \p ctx->N (for example,
695 * 128 Bytes if RSA-1024 is used) to be able to hold an
696 * arbitrary decrypted message. If it is not large enough to
697 * hold the decryption of the particular ciphertext provided,
698 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100699 *
Hanno Becker9a467772018-12-13 09:54:59 +0000700 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100701 * \param f_rng The RNG function. This is used for blinding and is
702 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000703 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100704 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000705 * \param olen The address at which to store the length of
706 * the plaintext. This must not be \c NULL.
707 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000708 * of length \c ctx->len Bytes. For example, \c 256 Bytes
709 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000710 * \param output The buffer used to hold the plaintext. This must
711 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000712 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100713 *
714 * \return \c 0 on success.
715 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000716 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
718 int (*f_rng)(void *, unsigned char *, size_t),
719 void *p_rng,
720 size_t *olen,
721 const unsigned char *input,
722 unsigned char *output,
723 size_t output_max_len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000724
725/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000726 * \brief This function performs a PKCS#1 v1.5 decryption
727 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100728 *
Janos Follath393df9c2023-12-29 11:14:58 +0000729 * \warning This is an inherently dangerous function (CWE-242). Unless
730 * it is used in a side channel free and safe way (eg.
731 * implementing the TLS protocol as per 7.4.7.1 of RFC 5246),
732 * the calling code is vulnerable.
733 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100734 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000735 * as large as the size \p ctx->len of \p ctx->N, for example,
736 * 128 Bytes if RSA-1024 is used, to be able to hold an
737 * arbitrary decrypted message. If it is not large enough to
738 * hold the decryption of the particular ciphertext provided,
739 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100740 *
Hanno Becker9a467772018-12-13 09:54:59 +0000741 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100742 * \param f_rng The RNG function. This is used for blinding and is
743 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000744 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100745 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000746 * \param olen The address at which to store the length of
747 * the plaintext. This must not be \c NULL.
748 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000749 * of length \c ctx->len Bytes. For example, \c 256 Bytes
750 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000751 * \param output The buffer used to hold the plaintext. This must
752 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000753 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100754 *
755 * \return \c 0 on success.
756 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
757 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100758 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100759int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
760 int (*f_rng)(void *, unsigned char *, size_t),
761 void *p_rng,
762 size_t *olen,
763 const unsigned char *input,
764 unsigned char *output,
765 size_t output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +0100766
767/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100768 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
769 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100770 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100771 * \note The output buffer length \c output_max_len should be
772 * as large as the size \p ctx->len of \p ctx->N, for
773 * example, 128 Bytes if RSA-1024 is used, to be able to
774 * hold an arbitrary decrypted message. If it is not
775 * large enough to hold the decryption of the particular
776 * ciphertext provided, the function returns
777 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100778 *
Hanno Becker9a467772018-12-13 09:54:59 +0000779 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100780 * \param f_rng The RNG function. This is used for blinding and is
781 * mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000782 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100783 * \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100784 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000785 * This must be a readable buffer of length \p label_len
786 * Bytes. It may be \c NULL if \p label_len is \c 0.
787 * \param label_len The length of the label in Bytes.
788 * \param olen The address at which to store the length of
789 * the plaintext. This must not be \c NULL.
790 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000791 * of length \c ctx->len Bytes. For example, \c 256 Bytes
792 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000793 * \param output The buffer used to hold the plaintext. This must
794 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000795 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100796 *
797 * \return \c 0 on success.
798 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100799 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
801 int (*f_rng)(void *, unsigned char *, size_t),
802 void *p_rng,
803 const unsigned char *label, size_t label_len,
804 size_t *olen,
805 const unsigned char *input,
806 unsigned char *output,
807 size_t output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +0100808
809/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000810 * \brief This function performs a private RSA operation to sign
811 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000812 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000813 * It is the generic wrapper for performing a PKCS#1
Thomas Daubney140184d2021-05-18 16:04:07 +0100814 * signature.
Paul Bakker5121ce52009-01-03 21:22:43 +0000815 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000816 * \note The \p sig buffer must be as large as the size
817 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000818 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000819 * \note For PKCS#1 v2.1 encoding, see comments on
820 * mbedtls_rsa_rsassa_pss_sign() for details on
821 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100822 *
Hanno Becker9a467772018-12-13 09:54:59 +0000823 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100824 * \param f_rng The RNG function to use. This is mandatory and
825 * must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000826 * \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 +0100827 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100828 * \param md_alg The message-digest algorithm used to hash the original data.
829 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200830 * \param hashlen The length of the message digest or raw data in Bytes.
831 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
832 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000833 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200834 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000835 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000836 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100837 * for an 2048-bit RSA modulus. A buffer length of
838 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100839 *
840 * \return \c 0 if the signing operation was successful.
841 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000842 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100843int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
844 int (*f_rng)(void *, unsigned char *, size_t),
845 void *p_rng,
846 mbedtls_md_type_t md_alg,
847 unsigned int hashlen,
848 const unsigned char *hash,
849 unsigned char *sig);
Paul Bakker5121ce52009-01-03 21:22:43 +0000850
851/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000852 * \brief This function performs a PKCS#1 v1.5 signature
853 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100854 *
Hanno Becker9a467772018-12-13 09:54:59 +0000855 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100856 * \param f_rng The RNG function. This is used for blinding and is
857 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000858 * \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 +0100859 * if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000860 * \param md_alg The message-digest algorithm used to hash the original data.
861 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200862 * \param hashlen The length of the message digest or raw data in Bytes.
863 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
864 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000865 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200866 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000867 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000868 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100869 * for an 2048-bit RSA modulus. A buffer length of
870 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100871 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100872 * \return \c 0 if the signing operation was successful.
873 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100874 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100875int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
876 int (*f_rng)(void *, unsigned char *, size_t),
877 void *p_rng,
878 mbedtls_md_type_t md_alg,
879 unsigned int hashlen,
880 const unsigned char *hash,
881 unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +0100882
Tomi Fontanillesa70b3c22023-07-16 12:06:13 +0300883#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100884/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000885 * \brief This function performs a PKCS#1 v2.1 PSS signature
886 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100887 *
Janos Follathb7953322021-04-01 14:44:17 +0100888 * \note The \c hash_id set in \p ctx by calling
889 * mbedtls_rsa_set_padding() selects the hash used for the
890 * encoding operation and for the mask generation function
891 * (MGF1). For more details on the encoding operation and the
892 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +0000893 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100894 * Specifications</em>.
Rose Zadike8b5b992018-03-27 12:19:47 +0100895 *
Cédric Meuter010ddc22020-04-25 09:24:11 +0200896 * \note This function enforces that the provided salt length complies
897 * with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 v2.2) §9.1.1
898 * step 3. The constraint is that the hash length plus the salt
899 * length plus 2 bytes must be at most the key length. If this
900 * constraint is not met, this function returns
Jaeden Amero3725bb22018-09-07 19:12:36 +0100901 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
902 *
Hanno Becker9a467772018-12-13 09:54:59 +0000903 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100904 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000905 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
906 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100907 * \param md_alg The message-digest algorithm used to hash the original data.
908 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200909 * \param hashlen The length of the message digest or raw data in Bytes.
910 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
911 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000912 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200913 * This must be a readable buffer of at least \p hashlen Bytes.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200914 * \param saltlen The length of the salt that should be used.
Cédric Meuter010ddc22020-04-25 09:24:11 +0200915 * If passed #MBEDTLS_RSA_SALT_LEN_ANY, the function will use
916 * the largest possible salt length up to the hash length,
917 * which is the largest permitted by some standards including
918 * FIPS 186-4 §5.5.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200919 * \param sig The buffer to hold the signature. This must be a writable
920 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
921 * for an 2048-bit RSA modulus. A buffer length of
922 * #MBEDTLS_MPI_MAX_SIZE is always safe.
923 *
924 * \return \c 0 if the signing operation was successful.
925 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
926 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100927int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
928 int (*f_rng)(void *, unsigned char *, size_t),
929 void *p_rng,
930 mbedtls_md_type_t md_alg,
931 unsigned int hashlen,
932 const unsigned char *hash,
933 int saltlen,
934 unsigned char *sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200935
936/**
937 * \brief This function performs a PKCS#1 v2.1 PSS signature
938 * operation (RSASSA-PSS-SIGN).
939 *
Janos Follathb7953322021-04-01 14:44:17 +0100940 * \note The \c hash_id set in \p ctx by calling
941 * mbedtls_rsa_set_padding() selects the hash used for the
942 * encoding operation and for the mask generation function
943 * (MGF1). For more details on the encoding operation and the
944 * mask generation function, consult <em>RFC-3447: Public-Key
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200945 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100946 * Specifications</em>.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200947 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000948 * \note This function always uses the maximum possible salt size,
949 * up to the length of the payload hash. This choice of salt
950 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
951 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
Rose Zadike8b5b992018-03-27 12:19:47 +0100952 * minimum salt size which is the hash size minus 2 bytes. If
953 * this minimum size is too large given the key size (the salt
954 * size, plus the hash size, plus 2 bytes must be no more than
955 * the key size in bytes), this function returns
956 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
957 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100958 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100959 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100960 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
961 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100962 * \param md_alg The message-digest algorithm used to hash the original data.
963 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200964 * \param hashlen The length of the message digest or raw data in Bytes.
965 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
966 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000967 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200968 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000969 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000970 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100971 * for an 2048-bit RSA modulus. A buffer length of
972 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100973 *
974 * \return \c 0 if the signing operation was successful.
975 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100976 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100977int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
978 int (*f_rng)(void *, unsigned char *, size_t),
979 void *p_rng,
980 mbedtls_md_type_t md_alg,
981 unsigned int hashlen,
982 const unsigned char *hash,
983 unsigned char *sig);
Tomi Fontanillesa70b3c22023-07-16 12:06:13 +0300984#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100985
986/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000987 * \brief This function performs a public RSA operation and checks
988 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +0000989 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000990 * This is the generic wrapper for performing a PKCS#1
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100991 * verification.
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000993 * \note For PKCS#1 v2.1 encoding, see comments on
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500994 * mbedtls_rsa_rsassa_pss_verify() about \c md_alg and
995 * \c hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100996 *
Hanno Becker9a467772018-12-13 09:54:59 +0000997 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +0100998 * \param md_alg The message-digest algorithm used to hash the original data.
999 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001000 * \param hashlen The length of the message digest or raw data in Bytes.
1001 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1002 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001003 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001004 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001005 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001006 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1007 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001008 *
1009 * \return \c 0 if the verify operation was successful.
1010 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001011 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001012int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
1013 mbedtls_md_type_t md_alg,
1014 unsigned int hashlen,
1015 const unsigned char *hash,
1016 const unsigned char *sig);
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
1018/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001019 * \brief This function performs a PKCS#1 v1.5 verification
1020 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001021 *
Hanno Becker9a467772018-12-13 09:54:59 +00001022 * \param ctx The initialized RSA public key context to use.
Rose Zadik042e97f2018-01-26 16:35:10 +00001023 * \param md_alg The message-digest algorithm used to hash the original data.
1024 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001025 * \param hashlen The length of the message digest or raw data in Bytes.
1026 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1027 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001028 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001029 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001030 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001031 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1032 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001033 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001034 * \return \c 0 if the verify operation was successful.
1035 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001036 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001037int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
1038 mbedtls_md_type_t md_alg,
1039 unsigned int hashlen,
1040 const unsigned char *hash,
1041 const unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001042
1043/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001044 * \brief This function performs a PKCS#1 v2.1 PSS verification
1045 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001046 *
Janos Follathb7953322021-04-01 14:44:17 +01001047 * \note The \c hash_id set in \p ctx by calling
1048 * mbedtls_rsa_set_padding() selects the hash used for the
1049 * encoding operation and for the mask generation function
1050 * (MGF1). For more details on the encoding operation and the
1051 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +00001052 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +01001053 * Specifications</em>. If the \c hash_id set in \p ctx by
1054 * mbedtls_rsa_set_padding() is #MBEDTLS_MD_NONE, the \p md_alg
1055 * parameter is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001056 *
Hanno Becker9a467772018-12-13 09:54:59 +00001057 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001058 * \param md_alg The message-digest algorithm used to hash the original data.
1059 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001060 * \param hashlen The length of the message digest or raw data in Bytes.
1061 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1062 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001063 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001064 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001065 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001066 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1067 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001068 *
1069 * \return \c 0 if the verify operation was successful.
1070 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001071 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001072int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
1073 mbedtls_md_type_t md_alg,
1074 unsigned int hashlen,
1075 const unsigned char *hash,
1076 const unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001077
1078/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001079 * \brief This function performs a PKCS#1 v2.1 PSS verification
1080 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001081 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001082 * \note The \p sig buffer must be as large as the size
1083 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001084 *
Janos Follathb7953322021-04-01 14:44:17 +01001085 * \note The \c hash_id set in \p ctx by mbedtls_rsa_set_padding() is
1086 * ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001087 *
Hanno Becker9a467772018-12-13 09:54:59 +00001088 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001089 * \param md_alg The message-digest algorithm used to hash the original data.
1090 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001091 * \param hashlen The length of the message digest or raw data in Bytes.
1092 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1093 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001094 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001095 * This must be a readable buffer of at least \p hashlen Bytes.
Janos Follathb7953322021-04-01 14:44:17 +01001096 * \param mgf1_hash_id The message digest algorithm used for the
1097 * verification operation and the mask generation
1098 * function (MGF1). For more details on the encoding
1099 * operation and the mask generation function, consult
1100 * <em>RFC-3447: Public-Key Cryptography Standards
1101 * (PKCS) #1 v2.1: RSA Cryptography
1102 * Specifications</em>.
Hanno Becker9a467772018-12-13 09:54:59 +00001103 * \param expected_salt_len The length of the salt used in padding. Use
1104 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1105 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001106 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1107 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001108 *
1109 * \return \c 0 if the verify operation was successful.
1110 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001111 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001112int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
1113 mbedtls_md_type_t md_alg,
1114 unsigned int hashlen,
1115 const unsigned char *hash,
1116 mbedtls_md_type_t mgf1_hash_id,
1117 int expected_salt_len,
1118 const unsigned char *sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001119
1120/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001121 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001122 *
Hanno Becker9a467772018-12-13 09:54:59 +00001123 * \param dst The destination context. This must be initialized.
1124 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001125 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001126 * \return \c 0 on success.
1127 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001128 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001129int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src);
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001130
1131/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001132 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001133 *
Hanno Becker9a467772018-12-13 09:54:59 +00001134 * \param ctx The RSA context to free. May be \c NULL, in which case
1135 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001136 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001137 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001138void mbedtls_rsa_free(mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
Ron Eldorfa8f6352017-06-20 15:48:46 +03001140#if defined(MBEDTLS_SELF_TEST)
1141
Paul Bakker5121ce52009-01-03 21:22:43 +00001142/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001143 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001144 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001145 * \return \c 0 on success.
1146 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001147 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001148int mbedtls_rsa_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +00001149
Ron Eldorfa8f6352017-06-20 15:48:46 +03001150#endif /* MBEDTLS_SELF_TEST */
1151
Paul Bakker5121ce52009-01-03 21:22:43 +00001152#ifdef __cplusplus
1153}
1154#endif
1155
Paul Bakker5121ce52009-01-03 21:22:43 +00001156#endif /* rsa.h */