blob: 667e6257e6782b0b6127858def15bcd4394fa6da [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 Rodgman0f2971a2023-11-03 12:04:52 +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
Paul Bakker5121ce52009-01-03 21:22:43 +000018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010020#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020021#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#endif
Paul Bakkered27a042013-04-18 22:46:23 +020024
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010025#include "mbedtls/bignum.h"
26#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_THREADING_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010029#include "mbedtls/threading.h"
Paul Bakkerc9965dc2013-09-29 14:58:17 +020030#endif
31
Paul Bakker13e2dfe2009-07-28 07:18:38 +000032/*
33 * RSA Error codes
34 */
Gilles Peskinea3974432021-07-26 18:48:10 +020035/** Bad input parameters to function. */
36#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080
37/** Input data contains invalid padding and is rejected. */
38#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100
39/** Something failed during generation of a key. */
40#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180
41/** Key failed to pass the validity check of the library. */
42#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200
43/** The public key operation failed. */
44#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280
45/** The private key operation failed. */
46#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300
47/** The PKCS#1 verification failed. */
48#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380
49/** The output buffer for decryption is not large enough. */
50#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400
51/** The random generator failed to generate non-zeros. */
52#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480
Ron Eldor9924bdc2018-10-04 10:59:13 +030053
54/* MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is deprecated and should not be used.
55 */
Gilles Peskinea3974432021-07-26 18:48:10 +020056/** The implementation does not offer the requested operation, for example, because of security violations or lack of functionality. */
57#define MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION -0x4500
Ron Eldor9924bdc2018-10-04 10:59:13 +030058
59/* MBEDTLS_ERR_RSA_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea3974432021-07-26 18:48:10 +020060/** RSA hardware accelerator failed. */
61#define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580
Paul Bakker5121ce52009-01-03 21:22:43 +000062
63/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020064 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000065 */
Rose Zadik042e97f2018-01-26 16:35:10 +000066#define MBEDTLS_RSA_PUBLIC 0 /**< Request private key operation. */
67#define MBEDTLS_RSA_PRIVATE 1 /**< Request public key operation. */
Paul Bakker5121ce52009-01-03 21:22:43 +000068
Rose Zadike8b5b992018-03-27 12:19:47 +010069#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
70#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Rose Zadik042e97f2018-01-26 16:35:10 +000072#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
73#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020076
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020077/*
78 * The above constants may be used even if the RSA module is compile out,
Shaun Case0e7791f2021-12-20 21:14:10 -080079 * eg for alternative (PKCS#11) RSA implementations in the PK layers.
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020080 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010081
Paul Bakker407a0da2013-06-27 14:29:21 +020082#ifdef __cplusplus
83extern "C" {
84#endif
85
Ron Eldor4e6d55d2018-02-07 16:36:15 +020086#if !defined(MBEDTLS_RSA_ALT)
87// Regular implementation
88//
89
Paul Bakker5121ce52009-01-03 21:22:43 +000090/**
Rose Zadik042e97f2018-01-26 16:35:10 +000091 * \brief The RSA context structure.
Hanno Becker5063cd22017-09-29 11:49:12 +010092 *
93 * \note Direct manipulation of the members of this structure
Rose Zadik042e97f2018-01-26 16:35:10 +000094 * is deprecated. All manipulation should instead be done through
95 * the public interface functions.
Paul Bakker5121ce52009-01-03 21:22:43 +000096 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097typedef struct mbedtls_rsa_context {
Gilles Peskine4337a9c2021-02-09 18:59:42 +010098 int ver; /*!< Reserved for internal purposes.
99 * Do not set this field in application
100 * code. Its meaning might change without
101 * notice. */
Rose Zadik042e97f2018-01-26 16:35:10 +0000102 size_t len; /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
Rose Zadike8b5b992018-03-27 12:19:47 +0100104 mbedtls_mpi N; /*!< The public modulus. */
105 mbedtls_mpi E; /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
Rose Zadike8b5b992018-03-27 12:19:47 +0100107 mbedtls_mpi D; /*!< The private exponent. */
108 mbedtls_mpi P; /*!< The first prime factor. */
109 mbedtls_mpi Q; /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100110
Rose Zadikf2ec2882018-04-17 10:27:25 +0100111 mbedtls_mpi DP; /*!< <code>D % (P - 1)</code>. */
112 mbedtls_mpi DQ; /*!< <code>D % (Q - 1)</code>. */
113 mbedtls_mpi QP; /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Rose Zadikf2ec2882018-04-17 10:27:25 +0100115 mbedtls_mpi RN; /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
Rose Zadikf2ec2882018-04-17 10:27:25 +0100117 mbedtls_mpi RP; /*!< cached <code>R^2 mod P</code>. */
118 mbedtls_mpi RQ; /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200119
Rose Zadike8b5b992018-03-27 12:19:47 +0100120 mbedtls_mpi Vi; /*!< The cached blinding value. */
121 mbedtls_mpi Vf; /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000122
Rose Zadik042e97f2018-01-26 16:35:10 +0000123 int padding; /*!< Selects padding mode:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
125 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
Rose Zadik042e97f2018-01-26 16:35:10 +0000126 int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
127 as specified in md.h for use in the MGF
128 mask generating function used in the
129 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100131 /* Invariant: the mutex is initialized iff ver != 0. */
Rose Zadik042e97f2018-01-26 16:35:10 +0000132 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200133#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200137#else /* MBEDTLS_RSA_ALT */
138#include "rsa_alt.h"
139#endif /* MBEDTLS_RSA_ALT */
140
Paul Bakker5121ce52009-01-03 21:22:43 +0000141/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000142 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000144 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000145 * encryption scheme and the RSASSA-PSS signature scheme.
146 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000147 * \note The \p hash_id parameter is ignored when using
148 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200149 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000150 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200151 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000152 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100153 * a default value, which can be overridden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000154 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200155 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000156 * \note The hash selected in \p hash_id is always used for OEAP
157 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100158 * making signatures, but can be overridden for verifying them.
159 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100160 *
Hanno Becker9a467772018-12-13 09:54:59 +0000161 * \param ctx The RSA context to initialize. This must not be \c NULL.
162 * \param padding The padding mode to use. This must be either
163 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Hanno Becker385ce912018-12-13 18:33:12 +0000164 * \param hash_id The hash identifier of ::mbedtls_md_type_t type, if
Hanno Becker9a467772018-12-13 09:54:59 +0000165 * \p padding is #MBEDTLS_RSA_PKCS_V21. It is unused
166 * otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168void mbedtls_rsa_init(mbedtls_rsa_context *ctx,
169 int padding,
170 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
172/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000173 * \brief This function imports a set of core parameters into an
174 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100175 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100176 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000177 * imports, if the parameters are not simultaneously present.
178 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100179 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000180 * by a call to mbedtls_rsa_complete(), which checks and
181 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100182 * public or private RSA key.
183 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000184 * \note See mbedtls_rsa_complete() for more information on which
185 * parameters are necessary to set up a private or public
186 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100187 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100188 * \note The imported parameters are copied and need not be preserved
189 * for the lifetime of the RSA context being set up.
190 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100191 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000192 * \param N The RSA modulus. This may be \c NULL.
193 * \param P The first prime factor of \p N. This may be \c NULL.
194 * \param Q The second prime factor of \p N. This may be \c NULL.
195 * \param D The private exponent. This may be \c NULL.
196 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100197 *
198 * \return \c 0 on success.
199 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100200 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
202 const mbedtls_mpi *N,
203 const mbedtls_mpi *P, const mbedtls_mpi *Q,
204 const mbedtls_mpi *D, const mbedtls_mpi *E);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100205
206/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000207 * \brief This function imports core RSA parameters, in raw big-endian
208 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100210 * \note This function can be called multiple times for successive
211 * imports, if the parameters are not simultaneously present.
212 *
213 * Any sequence of calls to this function should be followed
214 * by a call to mbedtls_rsa_complete(), which checks and
215 * completes the provided information to a ready-for-use
216 * public or private RSA key.
217 *
218 * \note See mbedtls_rsa_complete() for more information on which
219 * parameters are necessary to set up a private or public
220 * RSA key.
221 *
222 * \note The imported parameters are copied and need not be preserved
223 * for the lifetime of the RSA context being set up.
224 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000225 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000226 * \param N The RSA modulus. This may be \c NULL.
227 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
228 * \param P The first prime factor of \p N. This may be \c NULL.
229 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
230 * \param Q The second prime factor of \p N. This may be \c NULL.
231 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
232 * \param D The private exponent. This may be \c NULL.
233 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
234 * \param E The public exponent. This may be \c NULL.
235 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100237 * \return \c 0 on success.
238 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100239 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
241 unsigned char const *N, size_t N_len,
242 unsigned char const *P, size_t P_len,
243 unsigned char const *Q, size_t Q_len,
244 unsigned char const *D, size_t D_len,
245 unsigned char const *E, size_t E_len);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100246
247/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000248 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100249 * a set of imported core parameters.
250 *
Andrzej Kurek0cbfbd92022-02-18 08:10:37 -0500251 * To setup an RSA public key, precisely \c N and \c E
Rose Zadik042e97f2018-01-26 16:35:10 +0000252 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100253 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000254 * To setup an RSA private key, sufficient information must
255 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100256 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000257 * The default implementation supports the following:
Andrzej Kurek0cbfbd92022-02-18 08:10:37 -0500258 * <ul><li>Derive \c P, \c Q from \c N, \c D, \c E.</li>
259 * <li>Derive \c N, \c D from \c P, \c Q, \c E.</li></ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000260 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100261 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000262 * If this function runs successfully, it guarantees that
263 * the RSA context can be used for RSA operations without
264 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100265 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100266 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000267 * for the imported parameters. In particular, parameters that
268 * are not needed by the implementation might be silently
269 * discarded and left unchecked. To check the consistency
270 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100271 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100272 * \param ctx The initialized RSA context holding imported parameters.
273 *
274 * \return \c 0 on success.
275 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
276 * failed.
277 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100278 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279int mbedtls_rsa_complete(mbedtls_rsa_context *ctx);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100280
281/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000282 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100283 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000284 * If this function runs successfully, the non-NULL buffers
285 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
286 * written, with additional unused space filled leading by
287 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100288 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000289 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300290 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000291 * <li>An alternative RSA implementation is in use, which
292 * stores the key externally, and either cannot or should
293 * not export it into RAM.</li>
294 * <li>A SW or HW implementation might not support a certain
295 * deduction. For example, \p P, \p Q from \p N, \p D,
296 * and \p E if the former are not part of the
297 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100298 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000299 * If the function fails due to an unsupported operation,
300 * the RSA context stays intact and remains usable.
301 *
302 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000303 * \param N The MPI to hold the RSA modulus.
304 * This may be \c NULL if this field need not be exported.
305 * \param P The MPI to hold the first prime factor of \p N.
306 * This may be \c NULL if this field need not be exported.
307 * \param Q The MPI to hold the second prime factor of \p N.
308 * This may be \c NULL if this field need not be exported.
309 * \param D The MPI to hold the private exponent.
310 * This may be \c NULL if this field need not be exported.
311 * \param E The MPI to hold the public exponent.
312 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000313 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100314 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300315 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000316 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100317 * functionality or because of security policies.
318 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100319 *
320 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
322 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
323 mbedtls_mpi *D, mbedtls_mpi *E);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100324
325/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000326 * \brief This function exports core parameters of an RSA key
327 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100328 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000329 * If this function runs successfully, the non-NULL buffers
330 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
331 * written, with additional unused space filled leading by
332 * zero Bytes.
333 *
334 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300335 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000336 * <li>An alternative RSA implementation is in use, which
337 * stores the key externally, and either cannot or should
338 * not export it into RAM.</li>
339 * <li>A SW or HW implementation might not support a certain
340 * deduction. For example, \p P, \p Q from \p N, \p D,
341 * and \p E if the former are not part of the
342 * implementation.</li></ul>
343 * If the function fails due to an unsupported operation,
344 * the RSA context stays intact and remains usable.
345 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100346 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100347 * buffer pointers are NULL.
348 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000349 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000350 * \param N The Byte array to store the RSA modulus,
351 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000352 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000353 * \param P The Byte array to hold the first prime factor of \p N,
354 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000355 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000356 * \param Q The Byte array to hold the second prime factor of \p N,
357 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000358 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000359 * \param D The Byte array to hold the private exponent,
360 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000361 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000362 * \param E The Byte array to hold the public exponent,
363 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000364 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100365 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100366 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300367 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000368 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100369 * functionality or because of security policies.
370 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100371 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
373 unsigned char *N, size_t N_len,
374 unsigned char *P, size_t P_len,
375 unsigned char *Q, size_t Q_len,
376 unsigned char *D, size_t D_len,
377 unsigned char *E, size_t E_len);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100378
379/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000380 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100381 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100382 * \note Alternative RSA implementations not using CRT-parameters
383 * internally can implement this function based on
384 * mbedtls_rsa_deduce_opt().
385 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000386 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000387 * \param DP The MPI to hold \c D modulo `P-1`,
388 * or \c NULL if it need not be exported.
389 * \param DQ The MPI to hold \c D modulo `Q-1`,
390 * or \c NULL if it need not be exported.
391 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
392 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100393 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100394 * \return \c 0 on success.
395 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100396 *
397 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
399 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100400
Paul Bakker5121ce52009-01-03 21:22:43 +0000401/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000402 * \brief This function sets padding for an already initialized RSA
403 * context. See mbedtls_rsa_init() for details.
Paul Bakker5121ce52009-01-03 21:22:43 +0000404 *
Hanno Becker9a467772018-12-13 09:54:59 +0000405 * \param ctx The initialized RSA context to be configured.
406 * \param padding The padding mode to use. This must be either
407 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Rose Zadik042e97f2018-01-26 16:35:10 +0000408 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Paul Bakker40e46942009-01-03 21:51:57 +0000409 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410void mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
411 int hash_id);
Paul Bakker21eb2802010-08-16 11:10:02 +0000412
Paul Bakkera3d195c2011-11-27 21:07:34 +0000413/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000414 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100415 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000416 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100417 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000418 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100419 *
420 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100421size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100422
423/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000424 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000426 * \note mbedtls_rsa_init() must be called before this function,
427 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 *
Hanno Becker9a467772018-12-13 09:54:59 +0000429 * \param ctx The initialized RSA context used to hold the key.
430 * \param f_rng The RNG function to be used for key generation.
431 * This must not be \c NULL.
432 * \param p_rng The RNG context to be passed to \p f_rng.
433 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100434 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000435 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000436 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100437 *
438 * \return \c 0 on success.
439 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000440 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
442 int (*f_rng)(void *, unsigned char *, size_t),
443 void *p_rng,
444 unsigned int nbits, int exponent);
Paul Bakker5121ce52009-01-03 21:22:43 +0000445
446/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000447 * \brief This function checks if a context contains at least an RSA
448 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000449 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000450 * If the function runs successfully, it is guaranteed that
451 * enough information is present to perform an RSA public key
452 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 *
Hanno Becker9a467772018-12-13 09:54:59 +0000454 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000455 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100456 * \return \c 0 on success.
457 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100458 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100460int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx);
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 an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100464 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000465 *
Hanno Becker68767a62017-10-17 10:13:31 +0100466 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000467 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100468 * on the given context, but that the various parameters are
469 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000470 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100471 *
472 * \warning This function should catch accidental misconfigurations
473 * like swapping of parameters, but it cannot establish full
474 * trust in neither the quality nor the consistency of the key
475 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000476 * <ul><li>Consistency: Imported parameters that are irrelevant
477 * for the implementation might be silently dropped. If dropped,
478 * the current function does not have access to them,
479 * and therefore cannot check them. See mbedtls_rsa_complete().
480 * If you want to check the consistency of the entire
Tom Cosgrove5205c972022-07-28 06:12:08 +0100481 * content of a PKCS1-encoded RSA private key, for example, you
Rose Zadik042e97f2018-01-26 16:35:10 +0000482 * should use mbedtls_rsa_validate_params() before setting
483 * up the RSA context.
484 * Additionally, if the implementation performs empirical checks,
485 * these checks substantiate but do not guarantee consistency.</li>
486 * <li>Quality: This function is not expected to perform
487 * extended quality assessments like checking that the prime
488 * factors are safe. Additionally, it is the responsibility of the
489 * user to ensure the trustworthiness of the source of his RSA
490 * parameters, which goes beyond what is effectively checkable
491 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100492 *
Hanno Becker9a467772018-12-13 09:54:59 +0000493 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100494 *
495 * \return \c 0 on success.
496 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100498int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000501 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100502 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000503 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100504 *
Hanno Becker9a467772018-12-13 09:54:59 +0000505 * \param pub The initialized RSA context holding the public key.
506 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000507 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100508 * \return \c 0 on success.
509 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100510 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100511int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
512 const mbedtls_rsa_context *prv);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100513
514/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000515 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 *
Hanno Becker9a467772018-12-13 09:54:59 +0000517 * \param ctx The initialized RSA context to use.
518 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000519 * of length \c ctx->len Bytes. For example, \c 256 Bytes
520 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000521 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000522 * of length \c ctx->len Bytes. For example, \c 256 Bytes
523 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000524 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000525 * \note This function does not handle message padding.
526 *
527 * \note Make sure to set \p input[0] = 0 or ensure that
Andrzej Kurek96ce1b02023-07-14 05:22:42 -0400528 * input is smaller than \c N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100530 * \return \c 0 on success.
531 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100533int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
534 const unsigned char *input,
535 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000536
537/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000538 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000539 *
Hanno Becker24120612017-10-26 11:53:35 +0100540 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100541 *
Shaun Case0e7791f2021-12-20 21:14:10 -0800542 * \note If blinding is used, both the base of exponentiation
Hanno Becker24120612017-10-26 11:53:35 +0100543 * and the exponent are blinded, providing protection
544 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100545 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100546 * \warning It is deprecated and a security risk to not provide
547 * a PRNG here and thereby prevent the use of blinding.
548 * Future versions of the library may enforce the presence
549 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100550 *
Hanno Becker9a467772018-12-13 09:54:59 +0000551 * \param ctx The initialized RSA context to use.
552 * \param f_rng The RNG function, used for blinding. It is discouraged
553 * and deprecated to pass \c NULL here, in which case
554 * blinding will be omitted.
555 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
556 * if \p f_rng is \c NULL or if \p f_rng doesn't need a context.
557 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000558 * of length \c ctx->len Bytes. For example, \c 256 Bytes
559 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000560 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000561 * of length \c ctx->len Bytes. For example, \c 256 Bytes
562 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100563 *
564 * \return \c 0 on success.
565 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
566 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100568int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
569 int (*f_rng)(void *, unsigned char *, size_t),
570 void *p_rng,
571 const unsigned char *input,
572 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
574/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000575 * \brief This function adds the message padding, then performs an RSA
576 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000578 * It is the generic wrapper for performing a PKCS#1 encryption
579 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100581 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000582 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
583 * are likely to remove the \p mode argument and have it
584 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100585 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100586 * \note Alternative implementations of RSA need not support
587 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300588 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100589 *
Hanno Becker9a467772018-12-13 09:54:59 +0000590 * \param ctx The initialized RSA context to use.
Hanno Becker974ca0d2018-12-18 18:03:24 +0000591 * \param f_rng The RNG to use. It is mandatory for PKCS#1 v2.1 padding
592 * encoding, and for PKCS#1 v1.5 padding encoding when used
593 * with \p mode set to #MBEDTLS_RSA_PUBLIC. For PKCS#1 v1.5
594 * padding encoding and \p mode set to #MBEDTLS_RSA_PRIVATE,
595 * it is used for blinding and should be provided in this
596 * case; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000597 * \param p_rng The RNG context to be passed to \p f_rng. May be
598 * \c NULL if \p f_rng is \c NULL or if \p f_rng doesn't
599 * need a context argument.
600 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000601 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
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 Peskine1b6c09a2023-01-11 14:52:35 +0100613int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
614 int (*f_rng)(void *, unsigned char *, size_t),
615 void *p_rng,
616 int mode, 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 Becker3cdc7112017-10-05 10:09:31 +0100624 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000625 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
626 * are likely to remove the \p mode argument and have it
627 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100628 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100629 * \note Alternative implementations of RSA need not support
630 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300631 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100632 *
Hanno Becker9a467772018-12-13 09:54:59 +0000633 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000634 * \param f_rng The RNG function to use. It is needed for padding generation
635 * if \p mode is #MBEDTLS_RSA_PUBLIC. If \p mode is
636 * #MBEDTLS_RSA_PRIVATE (discouraged), it is used for
637 * blinding and should be provided; see mbedtls_rsa_private().
Hanno Becker9a467772018-12-13 09:54:59 +0000638 * \param p_rng The RNG context to be passed to \p f_rng. This may
639 * be \c NULL if \p f_rng is \c NULL or if \p f_rng
640 * doesn't need a context argument.
641 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000642 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000643 * \param ilen The length of the plaintext in Bytes.
644 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000645 * buffer of size \p ilen Bytes. It may be \c NULL if
646 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000647 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000648 * of length \c ctx->len Bytes. For example, \c 256 Bytes
649 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100650 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100651 * \return \c 0 on success.
652 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100653 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100654int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
655 int (*f_rng)(void *, unsigned char *, size_t),
656 void *p_rng,
657 int mode, size_t ilen,
658 const unsigned char *input,
659 unsigned char *output);
Paul Bakkerb3869132013-02-28 17:21:01 +0100660
661/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000662 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
663 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100664 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100665 * \note The output buffer must be as large as the size
666 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
667 *
668 * \deprecated It is deprecated and discouraged to call this function
669 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
670 * are likely to remove the \p mode argument and have it
671 * implicitly set to #MBEDTLS_RSA_PUBLIC.
672 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100673 * \note Alternative implementations of RSA need not support
674 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300675 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100676 *
Tom Cosgrove2b150752022-05-26 11:55:43 +0100677 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000678 * \param f_rng The RNG function to use. This is needed for padding
679 * generation and must be provided.
Hanno Becker9a467772018-12-13 09:54:59 +0000680 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000681 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000682 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000683 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000684 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000685 * This must be a readable buffer of length \p label_len
686 * Bytes. It may be \c NULL if \p label_len is \c 0.
687 * \param label_len The length of the label in Bytes.
688 * \param ilen The length of the plaintext buffer \p input in Bytes.
689 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000690 * buffer of size \p ilen Bytes. It may be \c NULL if
691 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000692 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000693 * of length \c ctx->len Bytes. For example, \c 256 Bytes
694 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100695 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100696 * \return \c 0 on success.
697 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100698 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100699int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
700 int (*f_rng)(void *, unsigned char *, size_t),
701 void *p_rng,
702 int mode,
703 const unsigned char *label, size_t label_len,
704 size_t ilen,
705 const unsigned char *input,
706 unsigned char *output);
Paul Bakkerb3869132013-02-28 17:21:01 +0100707
708/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000709 * \brief This function performs an RSA operation, then removes the
710 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000711 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000712 * It is the generic wrapper for performing a PKCS#1 decryption
713 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000714 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100715 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000716 * as large as the size \p ctx->len of \p ctx->N (for example,
717 * 128 Bytes if RSA-1024 is used) to be able to hold an
718 * arbitrary decrypted message. If it is not large enough to
719 * hold the decryption of the particular ciphertext provided,
720 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100721 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100722 * \deprecated It is deprecated and discouraged to call this function
723 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
724 * are likely to remove the \p mode argument and have it
725 * implicitly set to #MBEDTLS_RSA_PRIVATE.
726 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100727 * \note Alternative implementations of RSA need not support
728 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300729 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100730 *
Hanno Becker9a467772018-12-13 09:54:59 +0000731 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000732 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
733 * this is used for blinding and should be provided; see
734 * mbedtls_rsa_private() for more. If \p mode is
735 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000736 * \param p_rng The RNG context to be passed to \p f_rng. This may be
737 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
738 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000739 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000740 * \param olen The address at which to store the length of
741 * the plaintext. This must not be \c NULL.
742 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000743 * of length \c ctx->len Bytes. For example, \c 256 Bytes
744 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000745 * \param output The buffer used to hold the plaintext. This must
746 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000747 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100748 *
749 * \return \c 0 on success.
750 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000751 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100752int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
753 int (*f_rng)(void *, unsigned char *, size_t),
754 void *p_rng,
755 int mode, size_t *olen,
756 const unsigned char *input,
757 unsigned char *output,
758 size_t output_max_len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
760/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000761 * \brief This function performs a PKCS#1 v1.5 decryption
762 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100763 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100764 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000765 * as large as the size \p ctx->len of \p ctx->N, for example,
766 * 128 Bytes if RSA-1024 is used, to be able to hold an
767 * arbitrary decrypted message. If it is not large enough to
768 * hold the decryption of the particular ciphertext provided,
769 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100770 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100771 * \deprecated It is deprecated and discouraged to call this function
772 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
773 * are likely to remove the \p mode argument and have it
774 * implicitly set to #MBEDTLS_RSA_PRIVATE.
775 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100776 * \note Alternative implementations of RSA need not support
777 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300778 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100779 *
Hanno Becker9a467772018-12-13 09:54:59 +0000780 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000781 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
782 * this is used for blinding and should be provided; see
783 * mbedtls_rsa_private() for more. If \p mode is
784 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000785 * \param p_rng The RNG context to be passed to \p f_rng. This may be
786 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
787 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000788 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000789 * \param olen The address at which to store the length of
790 * the plaintext. This must not be \c NULL.
791 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000792 * of length \c ctx->len Bytes. For example, \c 256 Bytes
793 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000794 * \param output The buffer used to hold the plaintext. This must
795 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000796 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100797 *
798 * \return \c 0 on success.
799 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
800 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100801 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100802int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
803 int (*f_rng)(void *, unsigned char *, size_t),
804 void *p_rng,
805 int mode, size_t *olen,
806 const unsigned char *input,
807 unsigned char *output,
808 size_t output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +0100809
810/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100811 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
812 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100813 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100814 * \note The output buffer length \c output_max_len should be
815 * as large as the size \p ctx->len of \p ctx->N, for
816 * example, 128 Bytes if RSA-1024 is used, to be able to
817 * hold an arbitrary decrypted message. If it is not
818 * large enough to hold the decryption of the particular
819 * ciphertext provided, the function returns
820 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100821 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100822 * \deprecated It is deprecated and discouraged to call this function
823 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
824 * are likely to remove the \p mode argument and have it
825 * implicitly set to #MBEDTLS_RSA_PRIVATE.
826 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100827 * \note Alternative implementations of RSA need not support
828 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300829 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100830 *
Hanno Becker9a467772018-12-13 09:54:59 +0000831 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000832 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
833 * this is used for blinding and should be provided; see
834 * mbedtls_rsa_private() for more. If \p mode is
835 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000836 * \param p_rng The RNG context to be passed to \p f_rng. This may be
837 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
838 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000839 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100840 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000841 * This must be a readable buffer of length \p label_len
842 * Bytes. It may be \c NULL if \p label_len is \c 0.
843 * \param label_len The length of the label in Bytes.
844 * \param olen The address at which to store the length of
845 * the plaintext. This must not be \c NULL.
846 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000847 * of length \c ctx->len Bytes. For example, \c 256 Bytes
848 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000849 * \param output The buffer used to hold the plaintext. This must
850 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000851 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100852 *
853 * \return \c 0 on success.
854 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100855 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100856int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
857 int (*f_rng)(void *, unsigned char *, size_t),
858 void *p_rng,
859 int mode,
860 const unsigned char *label, size_t label_len,
861 size_t *olen,
862 const unsigned char *input,
863 unsigned char *output,
864 size_t output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +0100865
866/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000867 * \brief This function performs a private RSA operation to sign
868 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000870 * It is the generic wrapper for performing a PKCS#1
871 * signature using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000872 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000873 * \note The \p sig buffer must be as large as the size
874 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000875 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000876 * \note For PKCS#1 v2.1 encoding, see comments on
877 * mbedtls_rsa_rsassa_pss_sign() for details on
878 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100879 *
880 * \deprecated It is deprecated and discouraged to call this function
881 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
882 * are likely to remove the \p mode argument and have it
883 * implicitly set to #MBEDTLS_RSA_PRIVATE.
884 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100885 * \note Alternative implementations of RSA need not support
886 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300887 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100888 *
Hanno Becker9a467772018-12-13 09:54:59 +0000889 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000890 * \param f_rng The RNG function to use. If the padding mode is PKCS#1 v2.1,
891 * this must be provided. If the padding mode is PKCS#1 v1.5 and
892 * \p mode is #MBEDTLS_RSA_PRIVATE, it is used for blinding
893 * and should be provided; see mbedtls_rsa_private() for more
894 * more. It is ignored otherwise.
Hanno Becker9a467772018-12-13 09:54:59 +0000895 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
896 * if \p f_rng is \c NULL or doesn't need a context argument.
897 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000898 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100899 * \param md_alg The message-digest algorithm used to hash the original data.
900 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000901 * \param hashlen The length of the message digest.
902 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
903 * \param hash The buffer holding the message digest or raw data.
904 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
905 * buffer of length \p hashlen Bytes. If \p md_alg is not
906 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
907 * the size of the hash corresponding to \p md_alg.
908 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000909 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100910 * for an 2048-bit RSA modulus. A buffer length of
911 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100912 *
913 * \return \c 0 if the signing operation was successful.
914 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000915 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100916int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
917 int (*f_rng)(void *, unsigned char *, size_t),
918 void *p_rng,
919 int mode,
920 mbedtls_md_type_t md_alg,
921 unsigned int hashlen,
922 const unsigned char *hash,
923 unsigned char *sig);
Paul Bakker5121ce52009-01-03 21:22:43 +0000924
925/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000926 * \brief This function performs a PKCS#1 v1.5 signature
927 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100928 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100929 * \deprecated It is deprecated and discouraged to call this function
930 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
931 * are likely to remove the \p mode argument and have it
932 * implicitly set to #MBEDTLS_RSA_PRIVATE.
933 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100934 * \note Alternative implementations of RSA need not support
935 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300936 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100937 *
Hanno Becker9a467772018-12-13 09:54:59 +0000938 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000939 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
940 * this is used for blinding and should be provided; see
941 * mbedtls_rsa_private() for more. If \p mode is
942 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000943 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
944 * if \p f_rng is \c NULL or doesn't need a context argument.
945 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000946 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000947 * \param md_alg The message-digest algorithm used to hash the original data.
948 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000949 * \param hashlen The length of the message digest.
950 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
951 * \param hash The buffer holding the message digest or raw data.
952 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
953 * buffer of length \p hashlen Bytes. If \p md_alg is not
954 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
955 * the size of the hash corresponding to \p md_alg.
956 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000957 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100958 * for an 2048-bit RSA modulus. A buffer length of
959 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100960 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100961 * \return \c 0 if the signing operation was successful.
962 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100963 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100964int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
965 int (*f_rng)(void *, unsigned char *, size_t),
966 void *p_rng,
967 int mode,
968 mbedtls_md_type_t md_alg,
969 unsigned int hashlen,
970 const unsigned char *hash,
971 unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +0100972
973/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000974 * \brief This function performs a PKCS#1 v2.1 PSS signature
975 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100976 *
Manuel Pégourié-Gonnard727e1f12021-06-23 10:35:55 +0200977 * \note The \c hash_id set in \p ctx (when calling
978 * mbedtls_rsa_init() or by calling mbedtls_rsa_set_padding()
979 * afterwards) selects the hash used for the
Janos Follath456d7e02021-04-01 14:44:17 +0100980 * encoding operation and for the mask generation function
981 * (MGF1). For more details on the encoding operation and the
982 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +0000983 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follath456d7e02021-04-01 14:44:17 +0100984 * Specifications</em>.
Rose Zadike8b5b992018-03-27 12:19:47 +0100985 *
Cédric Meuter010ddc22020-04-25 09:24:11 +0200986 * \note This function enforces that the provided salt length complies
987 * with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 v2.2) §9.1.1
988 * step 3. The constraint is that the hash length plus the salt
989 * length plus 2 bytes must be at most the key length. If this
990 * constraint is not met, this function returns
Jaeden Amero3725bb22018-09-07 19:12:36 +0100991 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
992 *
Hanno Becker9a467772018-12-13 09:54:59 +0000993 * \param ctx The initialized RSA context to use.
994 * \param f_rng The RNG function. It must not be \c NULL.
995 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
996 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100997 * \param md_alg The message-digest algorithm used to hash the original data.
998 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000999 * \param hashlen The length of the message digest.
1000 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
1001 * \param hash The buffer holding the message digest or raw data.
1002 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1003 * buffer of length \p hashlen Bytes. If \p md_alg is not
1004 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1005 * the size of the hash corresponding to \p md_alg.
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001006 * \param saltlen The length of the salt that should be used.
Cédric Meuter010ddc22020-04-25 09:24:11 +02001007 * If passed #MBEDTLS_RSA_SALT_LEN_ANY, the function will use
1008 * the largest possible salt length up to the hash length,
1009 * which is the largest permitted by some standards including
1010 * FIPS 186-4 §5.5.
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001011 * \param sig The buffer to hold the signature. This must be a writable
1012 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1013 * for an 2048-bit RSA modulus. A buffer length of
1014 * #MBEDTLS_MPI_MAX_SIZE is always safe.
1015 *
1016 * \return \c 0 if the signing operation was successful.
1017 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
1018 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001019int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1020 int (*f_rng)(void *, unsigned char *, size_t),
1021 void *p_rng,
1022 mbedtls_md_type_t md_alg,
1023 unsigned int hashlen,
1024 const unsigned char *hash,
1025 int saltlen,
1026 unsigned char *sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001027
1028/**
1029 * \brief This function performs a PKCS#1 v2.1 PSS signature
1030 * operation (RSASSA-PSS-SIGN).
1031 *
Manuel Pégourié-Gonnard727e1f12021-06-23 10:35:55 +02001032 * \note The \c hash_id set in \p ctx (when calling
1033 * mbedtls_rsa_init() or by calling mbedtls_rsa_set_padding()
1034 * afterwards) selects the hash used for the
Janos Follath456d7e02021-04-01 14:44:17 +01001035 * encoding operation and for the mask generation function
1036 * (MGF1). For more details on the encoding operation and the
1037 * mask generation function, consult <em>RFC-3447: Public-Key
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001038 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follath456d7e02021-04-01 14:44:17 +01001039 * Specifications</em>.
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001040 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001041 * \note This function always uses the maximum possible salt size,
1042 * up to the length of the payload hash. This choice of salt
1043 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
1044 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
Rose Zadike8b5b992018-03-27 12:19:47 +01001045 * minimum salt size which is the hash size minus 2 bytes. If
1046 * this minimum size is too large given the key size (the salt
1047 * size, plus the hash size, plus 2 bytes must be no more than
1048 * the key size in bytes), this function returns
1049 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
1050 *
1051 * \deprecated It is deprecated and discouraged to call this function
1052 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
1053 * are likely to remove the \p mode argument and have it
1054 * implicitly set to #MBEDTLS_RSA_PRIVATE.
1055 *
1056 * \note Alternative implementations of RSA need not support
1057 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
1058 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
1059 *
1060 * \param ctx The initialized RSA context to use.
1061 * \param f_rng The RNG function. It must not be \c NULL.
1062 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
1063 * if \p f_rng doesn't need a context argument.
Paul Bakkerb3869132013-02-28 17:21:01 +01001064 * \param mode The mode of operation. This must be either
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Paul Bakkerb3869132013-02-28 17:21:01 +01001066 * \param md_alg The message-digest algorithm used to hash the original data.
1067 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001068 * \param hashlen The length of the message digest.
Janos Follath456d7e02021-04-01 14:44:17 +01001069 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
Hanno Becker9a467772018-12-13 09:54:59 +00001070 * \param hash The buffer holding the message digest or raw data.
1071 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1072 * buffer of length \p hashlen Bytes. If \p md_alg is not
1073 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1074 * the size of the hash corresponding to \p md_alg.
1075 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +00001076 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +01001077 * for an 2048-bit RSA modulus. A buffer length of
1078 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +01001079 *
1080 * \return \c 0 if the signing operation was successful.
1081 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
1082 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001083int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1084 int (*f_rng)(void *, unsigned char *, size_t),
1085 void *p_rng,
1086 int mode,
1087 mbedtls_md_type_t md_alg,
1088 unsigned int hashlen,
1089 const unsigned char *hash,
1090 unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001091
1092/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001093 * \brief This function performs a public RSA operation and checks
1094 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +00001095 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001096 * This is the generic wrapper for performing a PKCS#1
1097 * verification using the mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001098 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001099 * \note For PKCS#1 v2.1 encoding, see comments on
Andrzej Kurek96ce1b02023-07-14 05:22:42 -04001100 * mbedtls_rsa_rsassa_pss_verify() about \c md_alg and
1101 * \c hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +01001102 *
1103 * \deprecated It is deprecated and discouraged to call this function
1104 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1105 * are likely to remove the \p mode argument and have it
1106 * set to #MBEDTLS_RSA_PUBLIC.
1107 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001108 * \note Alternative implementations of RSA need not support
1109 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001110 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001111 *
Hanno Becker9a467772018-12-13 09:54:59 +00001112 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001113 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1114 * this is used for blinding and should be provided; see
1115 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001116 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1117 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1118 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001119 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001120 * \param md_alg The message-digest algorithm used to hash the original data.
1121 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001122 * \param hashlen The length of the message digest.
1123 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1124 * \param hash The buffer holding the message digest or raw data.
1125 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1126 * buffer of length \p hashlen Bytes. If \p md_alg is not
1127 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1128 * the size of the hash corresponding to \p md_alg.
1129 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001130 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1131 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001132 *
1133 * \return \c 0 if the verify operation was successful.
1134 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001136int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
1137 int (*f_rng)(void *, unsigned char *, size_t),
1138 void *p_rng,
1139 int mode,
1140 mbedtls_md_type_t md_alg,
1141 unsigned int hashlen,
1142 const unsigned char *hash,
1143 const unsigned char *sig);
Paul Bakker5121ce52009-01-03 21:22:43 +00001144
1145/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001146 * \brief This function performs a PKCS#1 v1.5 verification
1147 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001148 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001149 * \deprecated It is deprecated and discouraged to call this function
1150 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1151 * are likely to remove the \p mode argument and have it
1152 * set to #MBEDTLS_RSA_PUBLIC.
1153 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001154 * \note Alternative implementations of RSA need not support
1155 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001156 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001157 *
Hanno Becker9a467772018-12-13 09:54:59 +00001158 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001159 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1160 * this is used for blinding and should be provided; see
1161 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001162 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1163 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1164 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001165 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +00001166 * \param md_alg The message-digest algorithm used to hash the original data.
1167 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001168 * \param hashlen The length of the message digest.
1169 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1170 * \param hash The buffer holding the message digest or raw data.
1171 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1172 * buffer of length \p hashlen Bytes. If \p md_alg is not
1173 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1174 * the size of the hash corresponding to \p md_alg.
1175 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001176 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1177 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001178 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001179 * \return \c 0 if the verify operation was successful.
1180 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001181 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001182int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
1183 int (*f_rng)(void *, unsigned char *, size_t),
1184 void *p_rng,
1185 int mode,
1186 mbedtls_md_type_t md_alg,
1187 unsigned int hashlen,
1188 const unsigned char *hash,
1189 const unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001190
1191/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001192 * \brief This function performs a PKCS#1 v2.1 PSS verification
1193 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001194 *
Manuel Pégourié-Gonnard727e1f12021-06-23 10:35:55 +02001195 * \note The \c hash_id set in \p ctx (when calling
1196 * mbedtls_rsa_init() or by calling mbedtls_rsa_set_padding()
1197 * afterwards) selects the hash used for the
Janos Follath456d7e02021-04-01 14:44:17 +01001198 * encoding operation and for the mask generation function
1199 * (MGF1). For more details on the encoding operation and the
1200 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +00001201 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Manuel Pégourié-Gonnard727e1f12021-06-23 10:35:55 +02001202 * Specifications</em>. If the \c hash_id set in \p ctx is
1203 * #MBEDTLS_MD_NONE, the \p md_alg parameter is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001204 *
1205 * \deprecated It is deprecated and discouraged to call this function
1206 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1207 * are likely to remove the \p mode argument and have it
1208 * implicitly set to #MBEDTLS_RSA_PUBLIC.
1209 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001210 * \note Alternative implementations of RSA need not support
1211 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001212 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001213 *
Hanno Becker9a467772018-12-13 09:54:59 +00001214 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001215 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1216 * this is used for blinding and should be provided; see
1217 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001218 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1219 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1220 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001221 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001222 * \param md_alg The message-digest algorithm used to hash the original data.
1223 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001224 * \param hashlen The length of the message digest.
1225 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1226 * \param hash The buffer holding the message digest or raw data.
1227 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1228 * buffer of length \p hashlen Bytes. If \p md_alg is not
1229 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1230 * the size of the hash corresponding to \p md_alg.
1231 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001232 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1233 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001234 *
1235 * \return \c 0 if the verify operation was successful.
1236 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001237 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001238int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
1239 int (*f_rng)(void *, unsigned char *, size_t),
1240 void *p_rng,
1241 int mode,
1242 mbedtls_md_type_t md_alg,
1243 unsigned int hashlen,
1244 const unsigned char *hash,
1245 const unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001246
1247/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001248 * \brief This function performs a PKCS#1 v2.1 PSS verification
1249 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001250 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001251 * \note The \p sig buffer must be as large as the size
1252 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001253 *
Manuel Pégourié-Gonnard727e1f12021-06-23 10:35:55 +02001254 * \note The \c hash_id set in \p ctx (when calling
1255 * mbedtls_rsa_init() or by calling mbedtls_rsa_set_padding()
1256 * afterwards) is ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001257 *
Hanno Becker9a467772018-12-13 09:54:59 +00001258 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001259 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1260 * this is used for blinding and should be provided; see
1261 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001262 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1263 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1264 * \param mode The mode of operation. This must be either
1265 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +01001266 * \param md_alg The message-digest algorithm used to hash the original data.
1267 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001268 * \param hashlen The length of the message digest.
1269 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1270 * \param hash The buffer holding the message digest or raw data.
1271 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1272 * buffer of length \p hashlen Bytes. If \p md_alg is not
1273 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1274 * the size of the hash corresponding to \p md_alg.
Janos Follath456d7e02021-04-01 14:44:17 +01001275 * \param mgf1_hash_id The message digest algorithm used for the
1276 * verification operation and the mask generation
1277 * function (MGF1). For more details on the encoding
1278 * operation and the mask generation function, consult
1279 * <em>RFC-3447: Public-Key Cryptography Standards
1280 * (PKCS) #1 v2.1: RSA Cryptography
1281 * Specifications</em>.
Hanno Becker9a467772018-12-13 09:54:59 +00001282 * \param expected_salt_len The length of the salt used in padding. Use
1283 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1284 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001285 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1286 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001287 *
1288 * \return \c 0 if the verify operation was successful.
1289 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001290 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001291int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
1292 int (*f_rng)(void *, unsigned char *, size_t),
1293 void *p_rng,
1294 int mode,
1295 mbedtls_md_type_t md_alg,
1296 unsigned int hashlen,
1297 const unsigned char *hash,
1298 mbedtls_md_type_t mgf1_hash_id,
1299 int expected_salt_len,
1300 const unsigned char *sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001301
1302/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001303 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001304 *
Hanno Becker9a467772018-12-13 09:54:59 +00001305 * \param dst The destination context. This must be initialized.
1306 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001307 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001308 * \return \c 0 on success.
1309 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001310 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001311int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src);
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001312
1313/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001314 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001315 *
Hanno Becker9a467772018-12-13 09:54:59 +00001316 * \param ctx The RSA context to free. May be \c NULL, in which case
1317 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001318 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001320void mbedtls_rsa_free(mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00001321
Ron Eldorfa8f6352017-06-20 15:48:46 +03001322#if defined(MBEDTLS_SELF_TEST)
1323
Paul Bakker5121ce52009-01-03 21:22:43 +00001324/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001325 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001327 * \return \c 0 on success.
1328 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001329 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001330int mbedtls_rsa_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +00001331
Ron Eldorfa8f6352017-06-20 15:48:46 +03001332#endif /* MBEDTLS_SELF_TEST */
1333
Paul Bakker5121ce52009-01-03 21:22:43 +00001334#ifdef __cplusplus
1335}
1336#endif
1337
Paul Bakker5121ce52009-01-03 21:22:43 +00001338#endif /* rsa.h */