blob: be831f19dc1cb36f8aae7fdc172c0c4ca5789915 [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
Ron Eldor4e6d55d2018-02-07 16:36:15 +020072#if !defined(MBEDTLS_RSA_ALT)
73// Regular implementation
74//
75
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +000076#if !defined(MBEDTLS_RSA_GEN_KEY_MIN_BITS)
77#define MBEDTLS_RSA_GEN_KEY_MIN_BITS 1024
78#elif MBEDTLS_RSA_GEN_KEY_MIN_BITS < 128
79#error "MBEDTLS_RSA_GEN_KEY_MIN_BITS must be at least 128 bits"
Waleed Elmelegyab570712023-07-05 16:40:58 +000080#endif
81
Paul Bakker5121ce52009-01-03 21:22:43 +000082/**
Rose Zadik042e97f2018-01-26 16:35:10 +000083 * \brief The RSA context structure.
Paul Bakker5121ce52009-01-03 21:22:43 +000084 */
Gilles Peskine449bd832023-01-11 14:50:10 +010085typedef struct mbedtls_rsa_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020086 int MBEDTLS_PRIVATE(ver); /*!< Reserved for internal purposes.
Gilles Peskine449bd832023-01-11 14:50:10 +010087 * Do not set this field in application
88 * code. Its meaning might change without
89 * notice. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020090 size_t MBEDTLS_PRIVATE(len); /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +000091
Mateusz Starzyk846f0212021-05-19 19:44:07 +020092 mbedtls_mpi MBEDTLS_PRIVATE(N); /*!< The public modulus. */
93 mbedtls_mpi MBEDTLS_PRIVATE(E); /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +000094
Mateusz Starzyk846f0212021-05-19 19:44:07 +020095 mbedtls_mpi MBEDTLS_PRIVATE(D); /*!< The private exponent. */
96 mbedtls_mpi MBEDTLS_PRIVATE(P); /*!< The first prime factor. */
97 mbedtls_mpi MBEDTLS_PRIVATE(Q); /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +010098
Mateusz Starzyk846f0212021-05-19 19:44:07 +020099 mbedtls_mpi MBEDTLS_PRIVATE(DP); /*!< <code>D % (P - 1)</code>. */
100 mbedtls_mpi MBEDTLS_PRIVATE(DQ); /*!< <code>D % (Q - 1)</code>. */
101 mbedtls_mpi MBEDTLS_PRIVATE(QP); /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200103 mbedtls_mpi MBEDTLS_PRIVATE(RN); /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200105 mbedtls_mpi MBEDTLS_PRIVATE(RP); /*!< cached <code>R^2 mod P</code>. */
106 mbedtls_mpi MBEDTLS_PRIVATE(RQ); /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200107
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200108 mbedtls_mpi MBEDTLS_PRIVATE(Vi); /*!< The cached blinding value. */
109 mbedtls_mpi MBEDTLS_PRIVATE(Vf); /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000110
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200111 int MBEDTLS_PRIVATE(padding); /*!< Selects padding mode:
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
113 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200114 int MBEDTLS_PRIVATE(hash_id); /*!< Hash identifier of mbedtls_md_type_t type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 as specified in md.h for use in the MGF
116 mask generating function used in the
117 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100119 /* Invariant: the mutex is initialized iff ver != 0. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200120 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex); /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200121#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000122}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200125#else /* MBEDTLS_RSA_ALT */
126#include "rsa_alt.h"
127#endif /* MBEDTLS_RSA_ALT */
128
Paul Bakker5121ce52009-01-03 21:22:43 +0000129/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000130 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200132 * \note This function initializes the padding and the hash
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200133 * identifier to respectively #MBEDTLS_RSA_PKCS_V15 and
134 * #MBEDTLS_MD_NONE. See mbedtls_rsa_set_padding() for more
135 * information about those parameters.
Ronald Cronc1905a12021-06-05 11:11:14 +0200136 *
137 * \param ctx The RSA context to initialize. This must not be \c NULL.
138 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100139void mbedtls_rsa_init(mbedtls_rsa_context *ctx);
Ronald Cronc1905a12021-06-05 11:11:14 +0200140
141/**
142 * \brief This function sets padding for an already initialized RSA
143 * context.
144 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000145 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000146 * encryption scheme and the RSASSA-PSS signature scheme.
147 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000148 * \note The \p hash_id parameter is ignored when using
149 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200150 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200151 * \note The choice of padding mode is strictly enforced for private
152 * key operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000153 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100154 * a default value, which can be overridden by calling specific
Ronald Cronc1905a12021-06-05 11:11:14 +0200155 * \c mbedtls_rsa_rsaes_xxx or \c mbedtls_rsa_rsassa_xxx
156 * functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200157 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000158 * \note The hash selected in \p hash_id is always used for OEAP
159 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100160 * making signatures, but can be overridden for verifying them.
161 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100162 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200163 * \param ctx The initialized RSA context to be configured.
Hanno Becker9a467772018-12-13 09:54:59 +0000164 * \param padding The padding mode to use. This must be either
165 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200166 * \param hash_id The hash identifier for PSS or OAEP, if \p padding is
167 * #MBEDTLS_RSA_PKCS_V21. #MBEDTLS_MD_NONE is accepted by this
168 * function but may be not suitable for some operations.
169 * Ignored if \p padding is #MBEDTLS_RSA_PKCS_V15.
Ronald Cronc1905a12021-06-05 11:11:14 +0200170 *
171 * \return \c 0 on success.
172 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING failure:
173 * \p padding or \p hash_id is invalid.
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100175int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
176 mbedtls_md_type_t hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000177
178/**
Yanray Wang83548b52023-03-15 16:46:34 +0800179 * \brief This function retrieves padding mode of initialized
180 * RSA context.
Yanray Wanga730df62023-03-01 10:18:19 +0800181 *
182 * \param ctx The initialized RSA context.
183 *
184 * \return RSA padding mode.
185 *
186 */
187int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx);
188
189/**
Yanray Wang12cb3962023-03-01 10:20:02 +0800190 * \brief This function retrieves hash identifier of mbedtls_md_type_t
191 * type.
192 *
193 * \param ctx The initialized RSA context.
194 *
195 * \return Hash identifier of mbedtls_md_type_t type.
196 *
197 */
Yanray Wangd41684e2023-03-17 18:54:22 +0800198int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx);
Yanray Wang12cb3962023-03-01 10:20:02 +0800199
200/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000201 * \brief This function imports a set of core parameters into an
202 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100203 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100204 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000205 * imports, if the parameters are not simultaneously present.
206 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100207 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000208 * by a call to mbedtls_rsa_complete(), which checks and
209 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100210 * public or private RSA key.
211 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000212 * \note See mbedtls_rsa_complete() for more information on which
213 * parameters are necessary to set up a private or public
214 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100215 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100216 * \note The imported parameters are copied and need not be preserved
217 * for the lifetime of the RSA context being set up.
218 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100219 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000220 * \param N The RSA modulus. This may be \c NULL.
221 * \param P The first prime factor of \p N. This may be \c NULL.
222 * \param Q The second prime factor of \p N. This may be \c NULL.
223 * \param D The private exponent. This may be \c NULL.
224 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100225 *
226 * \return \c 0 on success.
227 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100228 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100229int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
230 const mbedtls_mpi *N,
231 const mbedtls_mpi *P, const mbedtls_mpi *Q,
232 const mbedtls_mpi *D, const mbedtls_mpi *E);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100233
234/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000235 * \brief This function imports core RSA parameters, in raw big-endian
236 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100238 * \note This function can be called multiple times for successive
239 * imports, if the parameters are not simultaneously present.
240 *
241 * Any sequence of calls to this function should be followed
242 * by a call to mbedtls_rsa_complete(), which checks and
243 * completes the provided information to a ready-for-use
244 * public or private RSA key.
245 *
246 * \note See mbedtls_rsa_complete() for more information on which
247 * parameters are necessary to set up a private or public
248 * RSA key.
249 *
250 * \note The imported parameters are copied and need not be preserved
251 * for the lifetime of the RSA context being set up.
252 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000253 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000254 * \param N The RSA modulus. This may be \c NULL.
255 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
256 * \param P The first prime factor of \p N. This may be \c NULL.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000257 * \param P_len The Byte length of \p P; it is ignored if \p P == NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000258 * \param Q The second prime factor of \p N. This may be \c NULL.
259 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
260 * \param D The private exponent. This may be \c NULL.
261 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
262 * \param E The public exponent. This may be \c NULL.
263 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100265 * \return \c 0 on success.
266 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100267 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100268int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
269 unsigned char const *N, size_t N_len,
270 unsigned char const *P, size_t P_len,
271 unsigned char const *Q, size_t Q_len,
272 unsigned char const *D, size_t D_len,
273 unsigned char const *E, size_t E_len);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100274
275/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000276 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100277 * a set of imported core parameters.
278 *
Andrzej Kurek43dfd512022-02-18 08:10:37 -0500279 * To setup an RSA public key, precisely \c N and \c E
Rose Zadik042e97f2018-01-26 16:35:10 +0000280 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100281 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000282 * To setup an RSA private key, sufficient information must
283 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100284 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000285 * The default implementation supports the following:
Andrzej Kurek43dfd512022-02-18 08:10:37 -0500286 * <ul><li>Derive \c P, \c Q from \c N, \c D, \c E.</li>
287 * <li>Derive \c N, \c D from \c P, \c Q, \c E.</li></ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000288 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100289 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000290 * If this function runs successfully, it guarantees that
291 * the RSA context can be used for RSA operations without
292 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100293 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100294 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000295 * for the imported parameters. In particular, parameters that
296 * are not needed by the implementation might be silently
297 * discarded and left unchecked. To check the consistency
298 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100299 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100300 * \param ctx The initialized RSA context holding imported parameters.
301 *
302 * \return \c 0 on success.
303 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
304 * failed.
305 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100306 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100307int mbedtls_rsa_complete(mbedtls_rsa_context *ctx);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100308
309/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000310 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100311 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000312 * If this function runs successfully, the non-NULL buffers
313 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
314 * written, with additional unused space filled leading by
315 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100316 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000317 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300318 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000319 * <li>An alternative RSA implementation is in use, which
320 * stores the key externally, and either cannot or should
321 * not export it into RAM.</li>
322 * <li>A SW or HW implementation might not support a certain
323 * deduction. For example, \p P, \p Q from \p N, \p D,
324 * and \p E if the former are not part of the
325 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100326 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000327 * If the function fails due to an unsupported operation,
328 * the RSA context stays intact and remains usable.
329 *
330 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000331 * \param N The MPI to hold the RSA modulus.
332 * This may be \c NULL if this field need not be exported.
333 * \param P The MPI to hold the first prime factor of \p N.
334 * This may be \c NULL if this field need not be exported.
335 * \param Q The MPI to hold the second prime factor of \p N.
336 * This may be \c NULL if this field need not be exported.
337 * \param D The MPI to hold the private exponent.
338 * This may be \c NULL if this field need not be exported.
339 * \param E The MPI to hold the public exponent.
340 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000341 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100342 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300343 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000344 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100345 * functionality or because of security policies.
346 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100347 *
348 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100349int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
350 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
351 mbedtls_mpi *D, mbedtls_mpi *E);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100352
353/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000354 * \brief This function exports core parameters of an RSA key
355 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100356 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000357 * If this function runs successfully, the non-NULL buffers
358 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
359 * written, with additional unused space filled leading by
360 * zero Bytes.
361 *
362 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300363 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000364 * <li>An alternative RSA implementation is in use, which
365 * stores the key externally, and either cannot or should
366 * not export it into RAM.</li>
367 * <li>A SW or HW implementation might not support a certain
368 * deduction. For example, \p P, \p Q from \p N, \p D,
369 * and \p E if the former are not part of the
370 * implementation.</li></ul>
371 * If the function fails due to an unsupported operation,
372 * the RSA context stays intact and remains usable.
373 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100374 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100375 * buffer pointers are NULL.
376 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000377 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000378 * \param N The Byte array to store the RSA modulus,
379 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000380 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000381 * \param P The Byte array to hold the first prime factor of \p N,
382 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000383 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000384 * \param Q The Byte array to hold the second prime factor of \p N,
385 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000386 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000387 * \param D The Byte array to hold the private exponent,
388 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000389 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000390 * \param E The Byte array to hold the public exponent,
391 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000392 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100393 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100394 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300395 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000396 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100397 * functionality or because of security policies.
398 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100399 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100400int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
401 unsigned char *N, size_t N_len,
402 unsigned char *P, size_t P_len,
403 unsigned char *Q, size_t Q_len,
404 unsigned char *D, size_t D_len,
405 unsigned char *E, size_t E_len);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100406
407/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000408 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100409 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100410 * \note Alternative RSA implementations not using CRT-parameters
411 * internally can implement this function based on
412 * mbedtls_rsa_deduce_opt().
413 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000414 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000415 * \param DP The MPI to hold \c D modulo `P-1`,
416 * or \c NULL if it need not be exported.
417 * \param DQ The MPI to hold \c D modulo `Q-1`,
418 * or \c NULL if it need not be exported.
419 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
420 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100421 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100422 * \return \c 0 on success.
423 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100424 *
425 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100426int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
427 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100428
Paul Bakker5121ce52009-01-03 21:22:43 +0000429/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000430 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100431 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000432 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100433 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000434 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100435 *
436 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100437size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100438
439/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000440 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000441 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000442 * \note mbedtls_rsa_init() must be called before this function,
443 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 *
Hanno Becker9a467772018-12-13 09:54:59 +0000445 * \param ctx The initialized RSA context used to hold the key.
446 * \param f_rng The RNG function to be used for key generation.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100447 * This is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000448 * \param p_rng The RNG context to be passed to \p f_rng.
449 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100450 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000451 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000452 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100453 *
454 * \return \c 0 on success.
455 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100457int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
458 int (*f_rng)(void *, unsigned char *, size_t),
459 void *p_rng,
460 unsigned int nbits, int exponent);
Paul Bakker5121ce52009-01-03 21:22:43 +0000461
462/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000463 * \brief This function checks if a context contains at least an RSA
464 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000465 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000466 * If the function runs successfully, it is guaranteed that
467 * enough information is present to perform an RSA public key
468 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 *
Hanno Becker9a467772018-12-13 09:54:59 +0000470 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000471 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100472 * \return \c 0 on success.
473 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100474 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100476int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000477
478/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000479 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100480 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 *
Hanno Becker68767a62017-10-17 10:13:31 +0100482 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000483 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100484 * on the given context, but that the various parameters are
485 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000486 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100487 *
488 * \warning This function should catch accidental misconfigurations
489 * like swapping of parameters, but it cannot establish full
490 * trust in neither the quality nor the consistency of the key
491 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000492 * <ul><li>Consistency: Imported parameters that are irrelevant
493 * for the implementation might be silently dropped. If dropped,
494 * the current function does not have access to them,
495 * and therefore cannot check them. See mbedtls_rsa_complete().
496 * If you want to check the consistency of the entire
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100497 * content of a PKCS1-encoded RSA private key, for example, you
Rose Zadik042e97f2018-01-26 16:35:10 +0000498 * should use mbedtls_rsa_validate_params() before setting
499 * up the RSA context.
500 * Additionally, if the implementation performs empirical checks,
501 * these checks substantiate but do not guarantee consistency.</li>
502 * <li>Quality: This function is not expected to perform
503 * extended quality assessments like checking that the prime
504 * factors are safe. Additionally, it is the responsibility of the
505 * user to ensure the trustworthiness of the source of his RSA
506 * parameters, which goes beyond what is effectively checkable
507 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100508 *
Hanno Becker9a467772018-12-13 09:54:59 +0000509 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100510 *
511 * \return \c 0 on success.
512 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100514int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
516/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000517 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100518 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000519 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100520 *
Hanno Becker9a467772018-12-13 09:54:59 +0000521 * \param pub The initialized RSA context holding the public key.
522 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000523 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100524 * \return \c 0 on success.
525 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100526 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100527int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
528 const mbedtls_rsa_context *prv);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100529
530/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000531 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 *
Hanno Becker9a467772018-12-13 09:54:59 +0000533 * \param ctx The initialized RSA context to use.
534 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000535 * of length \c ctx->len Bytes. For example, \c 256 Bytes
536 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000537 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000538 * of length \c ctx->len Bytes. For example, \c 256 Bytes
539 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000540 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000541 * \note This function does not handle message padding.
542 *
543 * \note Make sure to set \p input[0] = 0 or ensure that
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500544 * input is smaller than \c N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100546 * \return \c 0 on success.
547 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000548 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100549int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
550 const unsigned char *input,
551 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
553/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000554 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000555 *
Hanno Becker24120612017-10-26 11:53:35 +0100556 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100557 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800558 * \note If blinding is used, both the base of exponentiation
Hanno Becker24120612017-10-26 11:53:35 +0100559 * and the exponent are blinded, providing protection
560 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100561 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100562 * \warning It is deprecated and a security risk to not provide
563 * a PRNG here and thereby prevent the use of blinding.
564 * Future versions of the library may enforce the presence
565 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100566 *
Hanno Becker9a467772018-12-13 09:54:59 +0000567 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100568 * \param f_rng The RNG function, used for blinding. It is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000569 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
Thomas Daubney03412782021-05-20 15:31:17 +0100570 * if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000571 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000572 * of length \c ctx->len Bytes. For example, \c 256 Bytes
573 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000574 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000575 * of length \c ctx->len Bytes. For example, \c 256 Bytes
576 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100577 *
578 * \return \c 0 on success.
579 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
580 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100582int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
583 int (*f_rng)(void *, unsigned char *, size_t),
584 void *p_rng,
585 const unsigned char *input,
586 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000587
588/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000589 * \brief This function adds the message padding, then performs an RSA
590 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000592 * It is the generic wrapper for performing a PKCS#1 encryption
Thomas Daubney21772772021-05-13 17:30:32 +0100593 * operation.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100594 *
Hanno Becker9a467772018-12-13 09:54:59 +0000595 * \param ctx The initialized RSA context to use.
Thomas Daubneyf54c5c52021-05-21 17:00:30 +0100596 * \param f_rng The RNG to use. It is used for padding generation
Thomas Daubney2c65db92021-05-21 10:58:28 +0100597 * and it is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000598 * \param p_rng The RNG context to be passed to \p f_rng. May be
Thomas Daubney03412782021-05-20 15:31:17 +0100599 * \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000600 * \param ilen The length of the plaintext in Bytes.
601 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000602 * buffer of size \p ilen Bytes. It may be \c NULL if
603 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000604 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000605 * of length \c ctx->len Bytes. For example, \c 256 Bytes
606 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100607 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100608 * \return \c 0 on success.
609 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100611int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
612 int (*f_rng)(void *, unsigned char *, size_t),
613 void *p_rng,
614 size_t ilen,
615 const unsigned char *input,
616 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000617
618/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000619 * \brief This function performs a PKCS#1 v1.5 encryption operation
620 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100621 *
Hanno Becker9a467772018-12-13 09:54:59 +0000622 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100623 * \param f_rng The RNG function to use. It is mandatory and used for
624 * padding generation.
Hanno Becker9a467772018-12-13 09:54:59 +0000625 * \param p_rng The RNG context to be passed to \p f_rng. This may
Thomas Daubney03412782021-05-20 15:31:17 +0100626 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000627 * \param ilen The length of the plaintext in Bytes.
628 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000629 * buffer of size \p ilen Bytes. It may be \c NULL if
630 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000631 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000632 * of length \c ctx->len Bytes. For example, \c 256 Bytes
633 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100634 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100635 * \return \c 0 on success.
636 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100637 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100638int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
639 int (*f_rng)(void *, unsigned char *, size_t),
640 void *p_rng,
641 size_t ilen,
642 const unsigned char *input,
643 unsigned char *output);
Paul Bakkerb3869132013-02-28 17:21:01 +0100644
645/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000646 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
647 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100648 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100649 * \note The output buffer must be as large as the size
650 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
651 *
Tom Cosgrove1e211442022-05-26 11:51:00 +0100652 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000653 * \param f_rng The RNG function to use. This is needed for padding
Thomas Daubney2c65db92021-05-21 10:58:28 +0100654 * generation and is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000655 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000656 * be \c NULL if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000657 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000658 * This must be a readable buffer of length \p label_len
659 * Bytes. It may be \c NULL if \p label_len is \c 0.
660 * \param label_len The length of the label in Bytes.
661 * \param ilen The length of the plaintext buffer \p input in Bytes.
662 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000663 * buffer of size \p ilen Bytes. It may be \c NULL if
664 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000665 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000666 * of length \c ctx->len Bytes. For example, \c 256 Bytes
667 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100668 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100669 * \return \c 0 on success.
670 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100671 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100672int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
673 int (*f_rng)(void *, unsigned char *, size_t),
674 void *p_rng,
675 const unsigned char *label, size_t label_len,
676 size_t ilen,
677 const unsigned char *input,
678 unsigned char *output);
Paul Bakkerb3869132013-02-28 17:21:01 +0100679
680/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000681 * \brief This function performs an RSA operation, then removes the
682 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000684 * It is the generic wrapper for performing a PKCS#1 decryption
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100685 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000686 *
Janos Follath393df9c2023-12-29 11:14:58 +0000687 * \warning When \p ctx->padding is set to #MBEDTLS_RSA_PKCS_V15,
688 * mbedtls_rsa_rsaes_pkcs1_v15_decrypt() is called, which is an
689 * inherently dangerous function (CWE-242).
690 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100691 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000692 * as large as the size \p ctx->len of \p ctx->N (for example,
693 * 128 Bytes if RSA-1024 is used) to be able to hold an
694 * arbitrary decrypted message. If it is not large enough to
695 * hold the decryption of the particular ciphertext provided,
696 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100697 *
Hanno Becker9a467772018-12-13 09:54:59 +0000698 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100699 * \param f_rng The RNG function. This is used for blinding and is
700 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000701 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100702 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000703 * \param olen The address at which to store the length of
704 * the plaintext. This must not be \c NULL.
705 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000706 * of length \c ctx->len Bytes. For example, \c 256 Bytes
707 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000708 * \param output The buffer used to hold the plaintext. This must
709 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000710 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100711 *
712 * \return \c 0 on success.
713 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000714 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100715int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
716 int (*f_rng)(void *, unsigned char *, size_t),
717 void *p_rng,
718 size_t *olen,
719 const unsigned char *input,
720 unsigned char *output,
721 size_t output_max_len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000722
723/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000724 * \brief This function performs a PKCS#1 v1.5 decryption
725 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100726 *
Janos Follath393df9c2023-12-29 11:14:58 +0000727 * \warning This is an inherently dangerous function (CWE-242). Unless
728 * it is used in a side channel free and safe way (eg.
729 * implementing the TLS protocol as per 7.4.7.1 of RFC 5246),
730 * the calling code is vulnerable.
731 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100732 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000733 * as large as the size \p ctx->len of \p ctx->N, for example,
734 * 128 Bytes if RSA-1024 is used, to be able to hold an
735 * arbitrary decrypted message. If it is not large enough to
736 * hold the decryption of the particular ciphertext provided,
737 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100738 *
Hanno Becker9a467772018-12-13 09:54:59 +0000739 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100740 * \param f_rng The RNG function. This is used for blinding and is
741 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000742 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100743 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000744 * \param olen The address at which to store the length of
745 * the plaintext. This must not be \c NULL.
746 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000747 * of length \c ctx->len Bytes. For example, \c 256 Bytes
748 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000749 * \param output The buffer used to hold the plaintext. This must
750 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000751 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100752 *
753 * \return \c 0 on success.
754 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
755 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100756 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100757int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
758 int (*f_rng)(void *, unsigned char *, size_t),
759 void *p_rng,
760 size_t *olen,
761 const unsigned char *input,
762 unsigned char *output,
763 size_t output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +0100764
765/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100766 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
767 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100768 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100769 * \note The output buffer length \c output_max_len should be
770 * as large as the size \p ctx->len of \p ctx->N, for
771 * example, 128 Bytes if RSA-1024 is used, to be able to
772 * hold an arbitrary decrypted message. If it is not
773 * large enough to hold the decryption of the particular
774 * ciphertext provided, the function returns
775 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100776 *
Hanno Becker9a467772018-12-13 09:54:59 +0000777 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100778 * \param f_rng The RNG function. This is used for blinding and is
779 * mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000780 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100781 * \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100782 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000783 * This must be a readable buffer of length \p label_len
784 * Bytes. It may be \c NULL if \p label_len is \c 0.
785 * \param label_len The length of the label in Bytes.
786 * \param olen The address at which to store the length of
787 * the plaintext. This must not be \c NULL.
788 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000789 * of length \c ctx->len Bytes. For example, \c 256 Bytes
790 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000791 * \param output The buffer used to hold the plaintext. This must
792 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000793 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100794 *
795 * \return \c 0 on success.
796 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100797 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100798int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
799 int (*f_rng)(void *, unsigned char *, size_t),
800 void *p_rng,
801 const unsigned char *label, size_t label_len,
802 size_t *olen,
803 const unsigned char *input,
804 unsigned char *output,
805 size_t output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +0100806
807/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000808 * \brief This function performs a private RSA operation to sign
809 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000810 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000811 * It is the generic wrapper for performing a PKCS#1
Thomas Daubney140184d2021-05-18 16:04:07 +0100812 * signature.
Paul Bakker5121ce52009-01-03 21:22:43 +0000813 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000814 * \note The \p sig buffer must be as large as the size
815 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000816 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000817 * \note For PKCS#1 v2.1 encoding, see comments on
818 * mbedtls_rsa_rsassa_pss_sign() for details on
819 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100820 *
Hanno Becker9a467772018-12-13 09:54:59 +0000821 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100822 * \param f_rng The RNG function to use. This is mandatory and
823 * must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000824 * \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 +0100825 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100826 * \param md_alg The message-digest algorithm used to hash the original data.
827 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200828 * \param hashlen The length of the message digest or raw data in Bytes.
829 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
830 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000831 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200832 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000833 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000834 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100835 * for an 2048-bit RSA modulus. A buffer length of
836 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100837 *
838 * \return \c 0 if the signing operation was successful.
839 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000840 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100841int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
842 int (*f_rng)(void *, unsigned char *, size_t),
843 void *p_rng,
844 mbedtls_md_type_t md_alg,
845 unsigned int hashlen,
846 const unsigned char *hash,
847 unsigned char *sig);
Paul Bakker5121ce52009-01-03 21:22:43 +0000848
849/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000850 * \brief This function performs a PKCS#1 v1.5 signature
851 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100852 *
Hanno Becker9a467772018-12-13 09:54:59 +0000853 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100854 * \param f_rng The RNG function. This is used for blinding and is
855 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000856 * \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 +0100857 * if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000858 * \param md_alg The message-digest algorithm used to hash the original data.
859 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200860 * \param hashlen The length of the message digest or raw data in Bytes.
861 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
862 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000863 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200864 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000865 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000866 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100867 * for an 2048-bit RSA modulus. A buffer length of
868 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100869 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100870 * \return \c 0 if the signing operation was successful.
871 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100872 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100873int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
874 int (*f_rng)(void *, unsigned char *, size_t),
875 void *p_rng,
876 mbedtls_md_type_t md_alg,
877 unsigned int hashlen,
878 const unsigned char *hash,
879 unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +0100880
881/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000882 * \brief This function performs a PKCS#1 v2.1 PSS signature
883 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100884 *
Janos Follathb7953322021-04-01 14:44:17 +0100885 * \note The \c hash_id set in \p ctx by calling
886 * mbedtls_rsa_set_padding() selects the hash used for the
887 * encoding operation and for the mask generation function
888 * (MGF1). For more details on the encoding operation and the
889 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +0000890 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100891 * Specifications</em>.
Rose Zadike8b5b992018-03-27 12:19:47 +0100892 *
Cédric Meuter010ddc22020-04-25 09:24:11 +0200893 * \note This function enforces that the provided salt length complies
894 * with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 v2.2) §9.1.1
895 * step 3. The constraint is that the hash length plus the salt
896 * length plus 2 bytes must be at most the key length. If this
897 * constraint is not met, this function returns
Jaeden Amero3725bb22018-09-07 19:12:36 +0100898 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
899 *
Hanno Becker9a467772018-12-13 09:54:59 +0000900 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100901 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000902 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
903 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100904 * \param md_alg The message-digest algorithm used to hash the original data.
905 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200906 * \param hashlen The length of the message digest or raw data in Bytes.
907 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
908 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000909 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200910 * This must be a readable buffer of at least \p hashlen Bytes.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200911 * \param saltlen The length of the salt that should be used.
Cédric Meuter010ddc22020-04-25 09:24:11 +0200912 * If passed #MBEDTLS_RSA_SALT_LEN_ANY, the function will use
913 * the largest possible salt length up to the hash length,
914 * which is the largest permitted by some standards including
915 * FIPS 186-4 §5.5.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200916 * \param sig The buffer to hold the signature. This must be a writable
917 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
918 * for an 2048-bit RSA modulus. A buffer length of
919 * #MBEDTLS_MPI_MAX_SIZE is always safe.
920 *
921 * \return \c 0 if the signing operation was successful.
922 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
923 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100924int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
925 int (*f_rng)(void *, unsigned char *, size_t),
926 void *p_rng,
927 mbedtls_md_type_t md_alg,
928 unsigned int hashlen,
929 const unsigned char *hash,
930 int saltlen,
931 unsigned char *sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200932
933/**
934 * \brief This function performs a PKCS#1 v2.1 PSS signature
935 * operation (RSASSA-PSS-SIGN).
936 *
Janos Follathb7953322021-04-01 14:44:17 +0100937 * \note The \c hash_id set in \p ctx by calling
938 * mbedtls_rsa_set_padding() selects the hash used for the
939 * encoding operation and for the mask generation function
940 * (MGF1). For more details on the encoding operation and the
941 * mask generation function, consult <em>RFC-3447: Public-Key
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200942 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100943 * Specifications</em>.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200944 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000945 * \note This function always uses the maximum possible salt size,
946 * up to the length of the payload hash. This choice of salt
947 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
948 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
Rose Zadike8b5b992018-03-27 12:19:47 +0100949 * minimum salt size which is the hash size minus 2 bytes. If
950 * this minimum size is too large given the key size (the salt
951 * size, plus the hash size, plus 2 bytes must be no more than
952 * the key size in bytes), this function returns
953 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
954 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100955 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100956 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100957 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
958 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100959 * \param md_alg The message-digest algorithm used to hash the original data.
960 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200961 * \param hashlen The length of the message digest or raw data in Bytes.
962 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
963 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000964 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200965 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000966 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000967 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100968 * for an 2048-bit RSA modulus. A buffer length of
969 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100970 *
971 * \return \c 0 if the signing operation was successful.
972 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100973 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100974int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
975 int (*f_rng)(void *, unsigned char *, size_t),
976 void *p_rng,
977 mbedtls_md_type_t md_alg,
978 unsigned int hashlen,
979 const unsigned char *hash,
980 unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +0100981
982/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000983 * \brief This function performs a public RSA operation and checks
984 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +0000985 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000986 * This is the generic wrapper for performing a PKCS#1
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100987 * verification.
Paul Bakker5121ce52009-01-03 21:22:43 +0000988 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000989 * \note For PKCS#1 v2.1 encoding, see comments on
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500990 * mbedtls_rsa_rsassa_pss_verify() about \c md_alg and
991 * \c hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100992 *
Hanno Becker9a467772018-12-13 09:54:59 +0000993 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +0100994 * \param md_alg The message-digest algorithm used to hash the original data.
995 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200996 * \param hashlen The length of the message digest or raw data in Bytes.
997 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
998 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000999 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001000 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001001 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001002 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1003 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001004 *
1005 * \return \c 0 if the verify operation was successful.
1006 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001007 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001008int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
1009 mbedtls_md_type_t md_alg,
1010 unsigned int hashlen,
1011 const unsigned char *hash,
1012 const unsigned char *sig);
Paul Bakker5121ce52009-01-03 21:22:43 +00001013
1014/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001015 * \brief This function performs a PKCS#1 v1.5 verification
1016 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001017 *
Hanno Becker9a467772018-12-13 09:54:59 +00001018 * \param ctx The initialized RSA public key context to use.
Rose Zadik042e97f2018-01-26 16:35:10 +00001019 * \param md_alg The message-digest algorithm used to hash the original data.
1020 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001021 * \param hashlen The length of the message digest or raw data in Bytes.
1022 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1023 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001024 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001025 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001026 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001027 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1028 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001029 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001030 * \return \c 0 if the verify operation was successful.
1031 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001032 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001033int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
1034 mbedtls_md_type_t md_alg,
1035 unsigned int hashlen,
1036 const unsigned char *hash,
1037 const unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001038
1039/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001040 * \brief This function performs a PKCS#1 v2.1 PSS verification
1041 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001042 *
Janos Follathb7953322021-04-01 14:44:17 +01001043 * \note The \c hash_id set in \p ctx by calling
1044 * mbedtls_rsa_set_padding() selects the hash used for the
1045 * encoding operation and for the mask generation function
1046 * (MGF1). For more details on the encoding operation and the
1047 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +00001048 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +01001049 * Specifications</em>. If the \c hash_id set in \p ctx by
1050 * mbedtls_rsa_set_padding() is #MBEDTLS_MD_NONE, the \p md_alg
1051 * parameter is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001052 *
Hanno Becker9a467772018-12-13 09:54:59 +00001053 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001054 * \param md_alg The message-digest algorithm used to hash the original data.
1055 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001056 * \param hashlen The length of the message digest or raw data in Bytes.
1057 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1058 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001059 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001060 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001061 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001062 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1063 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001064 *
1065 * \return \c 0 if the verify operation was successful.
1066 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001067 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001068int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
1069 mbedtls_md_type_t md_alg,
1070 unsigned int hashlen,
1071 const unsigned char *hash,
1072 const unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001073
1074/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001075 * \brief This function performs a PKCS#1 v2.1 PSS verification
1076 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001077 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001078 * \note The \p sig buffer must be as large as the size
1079 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001080 *
Janos Follathb7953322021-04-01 14:44:17 +01001081 * \note The \c hash_id set in \p ctx by mbedtls_rsa_set_padding() is
1082 * ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001083 *
Hanno Becker9a467772018-12-13 09:54:59 +00001084 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001085 * \param md_alg The message-digest algorithm used to hash the original data.
1086 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001087 * \param hashlen The length of the message digest or raw data in Bytes.
1088 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1089 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001090 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001091 * This must be a readable buffer of at least \p hashlen Bytes.
Janos Follathb7953322021-04-01 14:44:17 +01001092 * \param mgf1_hash_id The message digest algorithm used for the
1093 * verification operation and the mask generation
1094 * function (MGF1). For more details on the encoding
1095 * operation and the mask generation function, consult
1096 * <em>RFC-3447: Public-Key Cryptography Standards
1097 * (PKCS) #1 v2.1: RSA Cryptography
1098 * Specifications</em>.
Hanno Becker9a467772018-12-13 09:54:59 +00001099 * \param expected_salt_len The length of the salt used in padding. Use
1100 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1101 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001102 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1103 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001104 *
1105 * \return \c 0 if the verify operation was successful.
1106 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001107 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001108int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
1109 mbedtls_md_type_t md_alg,
1110 unsigned int hashlen,
1111 const unsigned char *hash,
1112 mbedtls_md_type_t mgf1_hash_id,
1113 int expected_salt_len,
1114 const unsigned char *sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001115
1116/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001117 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001118 *
Hanno Becker9a467772018-12-13 09:54:59 +00001119 * \param dst The destination context. This must be initialized.
1120 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001121 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001122 * \return \c 0 on success.
1123 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001124 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001125int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src);
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001126
1127/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001128 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001129 *
Hanno Becker9a467772018-12-13 09:54:59 +00001130 * \param ctx The RSA context to free. May be \c NULL, in which case
1131 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001132 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001133 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001134void mbedtls_rsa_free(mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00001135
Ron Eldorfa8f6352017-06-20 15:48:46 +03001136#if defined(MBEDTLS_SELF_TEST)
1137
Paul Bakker5121ce52009-01-03 21:22:43 +00001138/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001139 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001140 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001141 * \return \c 0 on success.
1142 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001143 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001144int mbedtls_rsa_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +00001145
Ron Eldorfa8f6352017-06-20 15:48:46 +03001146#endif /* MBEDTLS_SELF_TEST */
1147
Paul Bakker5121ce52009-01-03 21:22:43 +00001148#ifdef __cplusplus
1149}
1150#endif
1151
Paul Bakker5121ce52009-01-03 21:22:43 +00001152#endif /* rsa.h */