blob: f8725ffb1e34ceefaa0ccc80097f84d9f1c2fa01 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Rose Zadik21e29262018-04-17 14:08:56 +01004 * \brief This file provides an API for the RSA public-key cryptosystem.
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
Rose Zadike8b5b992018-03-27 12:19:47 +01006 * The RSA public-key cryptosystem is defined in <em>Public-Key
7 * Cryptography Standards (PKCS) #1 v1.5: RSA Encryption</em>
Darryl Green11999bb2018-03-13 15:22:58 +00008 * and <em>Public-Key Cryptography Standards (PKCS) #1 v2.1:
Rose Zadike8b5b992018-03-27 12:19:47 +01009 * RSA Cryptography Specifications</em>.
Rose Zadik042e97f2018-01-26 16:35:10 +000010 *
Darryl Greena40a1012018-01-05 15:33:17 +000011 */
12/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020013 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020014 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#ifndef MBEDTLS_RSA_H
29#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010032#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#endif
Paul Bakkered27a042013-04-18 22:46:23 +020036
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010037#include "mbedtls/bignum.h"
38#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_THREADING_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010041#include "mbedtls/threading.h"
Paul Bakkerc9965dc2013-09-29 14:58:17 +020042#endif
43
Paul Bakker13e2dfe2009-07-28 07:18:38 +000044/*
45 * RSA Error codes
46 */
Gilles Peskinea3974432021-07-26 18:48:10 +020047/** Bad input parameters to function. */
48#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080
49/** Input data contains invalid padding and is rejected. */
50#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100
51/** Something failed during generation of a key. */
52#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180
53/** Key failed to pass the validity check of the library. */
54#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200
55/** The public key operation failed. */
56#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280
57/** The private key operation failed. */
58#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300
59/** The PKCS#1 verification failed. */
60#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380
61/** The output buffer for decryption is not large enough. */
62#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400
63/** The random generator failed to generate non-zeros. */
64#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480
Ron Eldor9924bdc2018-10-04 10:59:13 +030065
66/* MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is deprecated and should not be used.
67 */
Gilles Peskinea3974432021-07-26 18:48:10 +020068/** The implementation does not offer the requested operation, for example, because of security violations or lack of functionality. */
69#define MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION -0x4500
Ron Eldor9924bdc2018-10-04 10:59:13 +030070
71/* MBEDTLS_ERR_RSA_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea3974432021-07-26 18:48:10 +020072/** RSA hardware accelerator failed. */
73#define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580
Paul Bakker5121ce52009-01-03 21:22:43 +000074
75/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020076 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000077 */
Rose Zadik042e97f2018-01-26 16:35:10 +000078#define MBEDTLS_RSA_PUBLIC 0 /**< Request private key operation. */
79#define MBEDTLS_RSA_PRIVATE 1 /**< Request public key operation. */
Paul Bakker5121ce52009-01-03 21:22:43 +000080
Rose Zadike8b5b992018-03-27 12:19:47 +010081#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
82#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000083
Rose Zadik042e97f2018-01-26 16:35:10 +000084#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
85#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +000086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020088
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020089/*
90 * The above constants may be used even if the RSA module is compile out,
Shaun Case0e7791f2021-12-20 21:14:10 -080091 * eg for alternative (PKCS#11) RSA implementations in the PK layers.
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020092 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010093
Paul Bakker407a0da2013-06-27 14:29:21 +020094#ifdef __cplusplus
95extern "C" {
96#endif
97
Ron Eldor4e6d55d2018-02-07 16:36:15 +020098#if !defined(MBEDTLS_RSA_ALT)
99// Regular implementation
100//
101
Paul Bakker5121ce52009-01-03 21:22:43 +0000102/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000103 * \brief The RSA context structure.
Hanno Becker5063cd22017-09-29 11:49:12 +0100104 *
105 * \note Direct manipulation of the members of this structure
Rose Zadik042e97f2018-01-26 16:35:10 +0000106 * is deprecated. All manipulation should instead be done through
107 * the public interface functions.
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109typedef struct mbedtls_rsa_context {
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100110 int ver; /*!< Reserved for internal purposes.
111 * Do not set this field in application
112 * code. Its meaning might change without
113 * notice. */
Rose Zadik042e97f2018-01-26 16:35:10 +0000114 size_t len; /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
Rose Zadike8b5b992018-03-27 12:19:47 +0100116 mbedtls_mpi N; /*!< The public modulus. */
117 mbedtls_mpi E; /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
Rose Zadike8b5b992018-03-27 12:19:47 +0100119 mbedtls_mpi D; /*!< The private exponent. */
120 mbedtls_mpi P; /*!< The first prime factor. */
121 mbedtls_mpi Q; /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100122
Rose Zadikf2ec2882018-04-17 10:27:25 +0100123 mbedtls_mpi DP; /*!< <code>D % (P - 1)</code>. */
124 mbedtls_mpi DQ; /*!< <code>D % (Q - 1)</code>. */
125 mbedtls_mpi QP; /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
Rose Zadikf2ec2882018-04-17 10:27:25 +0100127 mbedtls_mpi RN; /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
Rose Zadikf2ec2882018-04-17 10:27:25 +0100129 mbedtls_mpi RP; /*!< cached <code>R^2 mod P</code>. */
130 mbedtls_mpi RQ; /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200131
Rose Zadike8b5b992018-03-27 12:19:47 +0100132 mbedtls_mpi Vi; /*!< The cached blinding value. */
133 mbedtls_mpi Vf; /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000134
Rose Zadik042e97f2018-01-26 16:35:10 +0000135 int padding; /*!< Selects padding mode:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
137 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
Rose Zadik042e97f2018-01-26 16:35:10 +0000138 int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
139 as specified in md.h for use in the MGF
140 mask generating function used in the
141 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100143 /* Invariant: the mutex is initialized iff ver != 0. */
Rose Zadik042e97f2018-01-26 16:35:10 +0000144 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200145#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000146}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200149#else /* MBEDTLS_RSA_ALT */
150#include "rsa_alt.h"
151#endif /* MBEDTLS_RSA_ALT */
152
Paul Bakker5121ce52009-01-03 21:22:43 +0000153/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000154 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000156 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000157 * encryption scheme and the RSASSA-PSS signature scheme.
158 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000159 * \note The \p hash_id parameter is ignored when using
160 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200161 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000162 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200163 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000164 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100165 * a default value, which can be overridden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000166 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200167 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000168 * \note The hash selected in \p hash_id is always used for OEAP
169 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100170 * making signatures, but can be overridden for verifying them.
171 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100172 *
Hanno Becker9a467772018-12-13 09:54:59 +0000173 * \param ctx The RSA context to initialize. This must not be \c NULL.
174 * \param padding The padding mode to use. This must be either
175 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Hanno Becker385ce912018-12-13 18:33:12 +0000176 * \param hash_id The hash identifier of ::mbedtls_md_type_t type, if
Hanno Becker9a467772018-12-13 09:54:59 +0000177 * \p padding is #MBEDTLS_RSA_PKCS_V21. It is unused
178 * otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180void mbedtls_rsa_init(mbedtls_rsa_context *ctx,
181 int padding,
182 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
184/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000185 * \brief This function imports a set of core parameters into an
186 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100187 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100188 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000189 * imports, if the parameters are not simultaneously present.
190 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100191 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000192 * by a call to mbedtls_rsa_complete(), which checks and
193 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100194 * public or private RSA key.
195 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000196 * \note See mbedtls_rsa_complete() for more information on which
197 * parameters are necessary to set up a private or public
198 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100199 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100200 * \note The imported parameters are copied and need not be preserved
201 * for the lifetime of the RSA context being set up.
202 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100203 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000204 * \param N The RSA modulus. This may be \c NULL.
205 * \param P The first prime factor of \p N. This may be \c NULL.
206 * \param Q The second prime factor of \p N. This may be \c NULL.
207 * \param D The private exponent. This may be \c NULL.
208 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100209 *
210 * \return \c 0 on success.
211 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100212 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
214 const mbedtls_mpi *N,
215 const mbedtls_mpi *P, const mbedtls_mpi *Q,
216 const mbedtls_mpi *D, const mbedtls_mpi *E);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100217
218/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000219 * \brief This function imports core RSA parameters, in raw big-endian
220 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100222 * \note This function can be called multiple times for successive
223 * imports, if the parameters are not simultaneously present.
224 *
225 * Any sequence of calls to this function should be followed
226 * by a call to mbedtls_rsa_complete(), which checks and
227 * completes the provided information to a ready-for-use
228 * public or private RSA key.
229 *
230 * \note See mbedtls_rsa_complete() for more information on which
231 * parameters are necessary to set up a private or public
232 * RSA key.
233 *
234 * \note The imported parameters are copied and need not be preserved
235 * for the lifetime of the RSA context being set up.
236 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000237 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000238 * \param N The RSA modulus. This may be \c NULL.
239 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
240 * \param P The first prime factor of \p N. This may be \c NULL.
241 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
242 * \param Q The second prime factor of \p N. This may be \c NULL.
243 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
244 * \param D The private exponent. This may be \c NULL.
245 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
246 * \param E The public exponent. This may be \c NULL.
247 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100249 * \return \c 0 on success.
250 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100251 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
253 unsigned char const *N, size_t N_len,
254 unsigned char const *P, size_t P_len,
255 unsigned char const *Q, size_t Q_len,
256 unsigned char const *D, size_t D_len,
257 unsigned char const *E, size_t E_len);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100258
259/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000260 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100261 * a set of imported core parameters.
262 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000263 * To setup an RSA public key, precisely \p N and \p E
264 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100265 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000266 * To setup an RSA private key, sufficient information must
267 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100268 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000269 * The default implementation supports the following:
270 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
271 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
272 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100273 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000274 * If this function runs successfully, it guarantees that
275 * the RSA context can be used for RSA operations without
276 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100277 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100278 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000279 * for the imported parameters. In particular, parameters that
280 * are not needed by the implementation might be silently
281 * discarded and left unchecked. To check the consistency
282 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100283 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100284 * \param ctx The initialized RSA context holding imported parameters.
285 *
286 * \return \c 0 on success.
287 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
288 * failed.
289 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100290 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291int mbedtls_rsa_complete(mbedtls_rsa_context *ctx);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100292
293/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000294 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100295 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000296 * If this function runs successfully, the non-NULL buffers
297 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
298 * written, with additional unused space filled leading by
299 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100300 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000301 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300302 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000303 * <li>An alternative RSA implementation is in use, which
304 * stores the key externally, and either cannot or should
305 * not export it into RAM.</li>
306 * <li>A SW or HW implementation might not support a certain
307 * deduction. For example, \p P, \p Q from \p N, \p D,
308 * and \p E if the former are not part of the
309 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100310 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000311 * If the function fails due to an unsupported operation,
312 * the RSA context stays intact and remains usable.
313 *
314 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000315 * \param N The MPI to hold the RSA modulus.
316 * This may be \c NULL if this field need not be exported.
317 * \param P The MPI to hold the first prime factor of \p N.
318 * This may be \c NULL if this field need not be exported.
319 * \param Q The MPI to hold the second prime factor of \p N.
320 * This may be \c NULL if this field need not be exported.
321 * \param D The MPI to hold the private exponent.
322 * This may be \c NULL if this field need not be exported.
323 * \param E The MPI to hold the public exponent.
324 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000325 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100326 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300327 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000328 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100329 * functionality or because of security policies.
330 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100331 *
332 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
334 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
335 mbedtls_mpi *D, mbedtls_mpi *E);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100336
337/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000338 * \brief This function exports core parameters of an RSA key
339 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100340 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000341 * If this function runs successfully, the non-NULL buffers
342 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
343 * written, with additional unused space filled leading by
344 * zero Bytes.
345 *
346 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300347 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000348 * <li>An alternative RSA implementation is in use, which
349 * stores the key externally, and either cannot or should
350 * not export it into RAM.</li>
351 * <li>A SW or HW implementation might not support a certain
352 * deduction. For example, \p P, \p Q from \p N, \p D,
353 * and \p E if the former are not part of the
354 * implementation.</li></ul>
355 * If the function fails due to an unsupported operation,
356 * the RSA context stays intact and remains usable.
357 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100358 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100359 * buffer pointers are NULL.
360 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000361 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000362 * \param N The Byte array to store the RSA modulus,
363 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000364 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000365 * \param P The Byte array to hold the first prime factor of \p N,
366 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000367 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000368 * \param Q The Byte array to hold the second prime factor of \p N,
369 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000370 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000371 * \param D The Byte array to hold the private exponent,
372 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000373 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000374 * \param E The Byte array to hold the public exponent,
375 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000376 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100377 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100378 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300379 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000380 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100381 * functionality or because of security policies.
382 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100383 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100384int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
385 unsigned char *N, size_t N_len,
386 unsigned char *P, size_t P_len,
387 unsigned char *Q, size_t Q_len,
388 unsigned char *D, size_t D_len,
389 unsigned char *E, size_t E_len);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100390
391/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000392 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100393 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100394 * \note Alternative RSA implementations not using CRT-parameters
395 * internally can implement this function based on
396 * mbedtls_rsa_deduce_opt().
397 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000398 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000399 * \param DP The MPI to hold \c D modulo `P-1`,
400 * or \c NULL if it need not be exported.
401 * \param DQ The MPI to hold \c D modulo `Q-1`,
402 * or \c NULL if it need not be exported.
403 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
404 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100405 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100406 * \return \c 0 on success.
407 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100408 *
409 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
411 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100412
Paul Bakker5121ce52009-01-03 21:22:43 +0000413/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000414 * \brief This function sets padding for an already initialized RSA
415 * context. See mbedtls_rsa_init() for details.
Paul Bakker5121ce52009-01-03 21:22:43 +0000416 *
Hanno Becker9a467772018-12-13 09:54:59 +0000417 * \param ctx The initialized RSA context to be configured.
418 * \param padding The padding mode to use. This must be either
419 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Rose Zadik042e97f2018-01-26 16:35:10 +0000420 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Paul Bakker40e46942009-01-03 21:51:57 +0000421 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422void mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
423 int hash_id);
Paul Bakker21eb2802010-08-16 11:10:02 +0000424
Paul Bakkera3d195c2011-11-27 21:07:34 +0000425/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000426 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100427 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000428 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100429 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000430 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100431 *
432 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100433size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100434
435/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000436 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000437 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000438 * \note mbedtls_rsa_init() must be called before this function,
439 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000440 *
Hanno Becker9a467772018-12-13 09:54:59 +0000441 * \param ctx The initialized RSA context used to hold the key.
442 * \param f_rng The RNG function to be used for key generation.
443 * This must not be \c NULL.
444 * \param p_rng The RNG context to be passed to \p f_rng.
445 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100446 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000447 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000448 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100449 *
450 * \return \c 0 on success.
451 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100453int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
454 int (*f_rng)(void *, unsigned char *, size_t),
455 void *p_rng,
456 unsigned int nbits, int exponent);
Paul Bakker5121ce52009-01-03 21:22:43 +0000457
458/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000459 * \brief This function checks if a context contains at least an RSA
460 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000462 * If the function runs successfully, it is guaranteed that
463 * enough information is present to perform an RSA public key
464 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000465 *
Hanno Becker9a467772018-12-13 09:54:59 +0000466 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000467 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100468 * \return \c 0 on success.
469 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100470 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000471 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100472int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
474/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000475 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100476 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 *
Hanno Becker68767a62017-10-17 10:13:31 +0100478 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000479 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100480 * on the given context, but that the various parameters are
481 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000482 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100483 *
484 * \warning This function should catch accidental misconfigurations
485 * like swapping of parameters, but it cannot establish full
486 * trust in neither the quality nor the consistency of the key
487 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000488 * <ul><li>Consistency: Imported parameters that are irrelevant
489 * for the implementation might be silently dropped. If dropped,
490 * the current function does not have access to them,
491 * and therefore cannot check them. See mbedtls_rsa_complete().
492 * If you want to check the consistency of the entire
Tom Cosgrove5205c972022-07-28 06:12:08 +0100493 * content of a PKCS1-encoded RSA private key, for example, you
Rose Zadik042e97f2018-01-26 16:35:10 +0000494 * should use mbedtls_rsa_validate_params() before setting
495 * up the RSA context.
496 * Additionally, if the implementation performs empirical checks,
497 * these checks substantiate but do not guarantee consistency.</li>
498 * <li>Quality: This function is not expected to perform
499 * extended quality assessments like checking that the prime
500 * factors are safe. Additionally, it is the responsibility of the
501 * user to ensure the trustworthiness of the source of his RSA
502 * parameters, which goes beyond what is effectively checkable
503 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100504 *
Hanno Becker9a467772018-12-13 09:54:59 +0000505 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100506 *
507 * \return \c 0 on success.
508 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100510int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
512/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000513 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100514 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000515 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100516 *
Hanno Becker9a467772018-12-13 09:54:59 +0000517 * \param pub The initialized RSA context holding the public key.
518 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000519 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100520 * \return \c 0 on success.
521 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100522 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100523int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
524 const mbedtls_rsa_context *prv);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100525
526/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000527 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 *
Hanno Becker9a467772018-12-13 09:54:59 +0000529 * \param ctx The initialized RSA context to use.
530 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000531 * of length \c ctx->len Bytes. For example, \c 256 Bytes
532 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000533 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000534 * of length \c ctx->len Bytes. For example, \c 256 Bytes
535 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000536 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000537 * \note This function does not handle message padding.
538 *
539 * \note Make sure to set \p input[0] = 0 or ensure that
540 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000541 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100542 * \return \c 0 on success.
543 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100545int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
546 const unsigned char *input,
547 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000550 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 *
Hanno Becker24120612017-10-26 11:53:35 +0100552 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100553 *
Shaun Case0e7791f2021-12-20 21:14:10 -0800554 * \note If blinding is used, both the base of exponentiation
Hanno Becker24120612017-10-26 11:53:35 +0100555 * and the exponent are blinded, providing protection
556 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100557 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100558 * \warning It is deprecated and a security risk to not provide
559 * a PRNG here and thereby prevent the use of blinding.
560 * Future versions of the library may enforce the presence
561 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100562 *
Hanno Becker9a467772018-12-13 09:54:59 +0000563 * \param ctx The initialized RSA context to use.
564 * \param f_rng The RNG function, used for blinding. It is discouraged
565 * and deprecated to pass \c NULL here, in which case
566 * blinding will be omitted.
567 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
568 * if \p f_rng is \c NULL or if \p f_rng doesn't need a context.
569 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000570 * of length \c ctx->len Bytes. For example, \c 256 Bytes
571 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000572 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000573 * of length \c ctx->len Bytes. For example, \c 256 Bytes
574 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100575 *
576 * \return \c 0 on success.
577 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
578 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100580int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
581 int (*f_rng)(void *, unsigned char *, size_t),
582 void *p_rng,
583 const unsigned char *input,
584 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
586/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000587 * \brief This function adds the message padding, then performs an RSA
588 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000589 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000590 * It is the generic wrapper for performing a PKCS#1 encryption
591 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000592 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100593 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000594 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
595 * are likely to remove the \p mode argument and have it
596 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100597 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100598 * \note Alternative implementations of RSA need not support
599 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300600 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100601 *
Hanno Becker9a467772018-12-13 09:54:59 +0000602 * \param ctx The initialized RSA context to use.
Hanno Becker974ca0d2018-12-18 18:03:24 +0000603 * \param f_rng The RNG to use. It is mandatory for PKCS#1 v2.1 padding
604 * encoding, and for PKCS#1 v1.5 padding encoding when used
605 * with \p mode set to #MBEDTLS_RSA_PUBLIC. For PKCS#1 v1.5
606 * padding encoding and \p mode set to #MBEDTLS_RSA_PRIVATE,
607 * it is used for blinding and should be provided in this
608 * case; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000609 * \param p_rng The RNG context to be passed to \p f_rng. May be
610 * \c NULL if \p f_rng is \c NULL or if \p f_rng doesn't
611 * need a context argument.
612 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000613 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000614 * \param ilen The length of the plaintext in Bytes.
615 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000616 * buffer of size \p ilen Bytes. It may be \c NULL if
617 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000618 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000619 * of length \c ctx->len Bytes. For example, \c 256 Bytes
620 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100621 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100622 * \return \c 0 on success.
623 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000624 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100625int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
626 int (*f_rng)(void *, unsigned char *, size_t),
627 void *p_rng,
628 int mode, size_t ilen,
629 const unsigned char *input,
630 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
632/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000633 * \brief This function performs a PKCS#1 v1.5 encryption operation
634 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100635 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100636 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000637 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
638 * are likely to remove the \p mode argument and have it
639 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100640 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100641 * \note Alternative implementations of RSA need not support
642 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300643 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100644 *
Hanno Becker9a467772018-12-13 09:54:59 +0000645 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000646 * \param f_rng The RNG function to use. It is needed for padding generation
647 * if \p mode is #MBEDTLS_RSA_PUBLIC. If \p mode is
648 * #MBEDTLS_RSA_PRIVATE (discouraged), it is used for
649 * blinding and should be provided; see mbedtls_rsa_private().
Hanno Becker9a467772018-12-13 09:54:59 +0000650 * \param p_rng The RNG context to be passed to \p f_rng. This may
651 * be \c NULL if \p f_rng is \c NULL or if \p f_rng
652 * doesn't need a context argument.
653 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000654 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000655 * \param ilen The length of the plaintext in Bytes.
656 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000657 * buffer of size \p ilen Bytes. It may be \c NULL if
658 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000659 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000660 * of length \c ctx->len Bytes. For example, \c 256 Bytes
661 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100662 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100663 * \return \c 0 on success.
664 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100665 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100666int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
667 int (*f_rng)(void *, unsigned char *, size_t),
668 void *p_rng,
669 int mode, size_t ilen,
670 const unsigned char *input,
671 unsigned char *output);
Paul Bakkerb3869132013-02-28 17:21:01 +0100672
673/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000674 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
675 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100676 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100677 * \note The output buffer must be as large as the size
678 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
679 *
680 * \deprecated It is deprecated and discouraged to call this function
681 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
682 * are likely to remove the \p mode argument and have it
683 * implicitly set to #MBEDTLS_RSA_PUBLIC.
684 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100685 * \note Alternative implementations of RSA need not support
686 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300687 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100688 *
Tom Cosgrove2b150752022-05-26 11:55:43 +0100689 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000690 * \param f_rng The RNG function to use. This is needed for padding
691 * generation and must be provided.
Hanno Becker9a467772018-12-13 09:54:59 +0000692 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000693 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000694 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000695 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000696 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000697 * This must be a readable buffer of length \p label_len
698 * Bytes. It may be \c NULL if \p label_len is \c 0.
699 * \param label_len The length of the label in Bytes.
700 * \param ilen The length of the plaintext buffer \p input in Bytes.
701 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000702 * buffer of size \p ilen Bytes. It may be \c NULL if
703 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000704 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000705 * of length \c ctx->len Bytes. For example, \c 256 Bytes
706 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100707 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100708 * \return \c 0 on success.
709 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100710 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100711int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
712 int (*f_rng)(void *, unsigned char *, size_t),
713 void *p_rng,
714 int mode,
715 const unsigned char *label, size_t label_len,
716 size_t ilen,
717 const unsigned char *input,
718 unsigned char *output);
Paul Bakkerb3869132013-02-28 17:21:01 +0100719
720/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000721 * \brief This function performs an RSA operation, then removes the
722 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000723 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000724 * It is the generic wrapper for performing a PKCS#1 decryption
725 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000726 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100727 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000728 * as large as the size \p ctx->len of \p ctx->N (for example,
729 * 128 Bytes if RSA-1024 is used) to be able to hold an
730 * arbitrary decrypted message. If it is not large enough to
731 * hold the decryption of the particular ciphertext provided,
732 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100733 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100734 * \deprecated It is deprecated and discouraged to call this function
735 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
736 * are likely to remove the \p mode argument and have it
737 * implicitly set to #MBEDTLS_RSA_PRIVATE.
738 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100739 * \note Alternative implementations of RSA need not support
740 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300741 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100742 *
Hanno Becker9a467772018-12-13 09:54:59 +0000743 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000744 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
745 * this is used for blinding and should be provided; see
746 * mbedtls_rsa_private() for more. If \p mode is
747 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000748 * \param p_rng The RNG context to be passed to \p f_rng. This may be
749 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
750 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000751 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000752 * \param olen The address at which to store the length of
753 * the plaintext. This must not be \c NULL.
754 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000755 * of length \c ctx->len Bytes. For example, \c 256 Bytes
756 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000757 * \param output The buffer used to hold the plaintext. This must
758 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000759 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100760 *
761 * \return \c 0 on success.
762 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000763 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100764int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
765 int (*f_rng)(void *, unsigned char *, size_t),
766 void *p_rng,
767 int mode, size_t *olen,
768 const unsigned char *input,
769 unsigned char *output,
770 size_t output_max_len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000771
772/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000773 * \brief This function performs a PKCS#1 v1.5 decryption
774 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100775 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100776 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000777 * as large as the size \p ctx->len of \p ctx->N, for example,
778 * 128 Bytes if RSA-1024 is used, to be able to hold an
779 * arbitrary decrypted message. If it is not large enough to
780 * hold the decryption of the particular ciphertext provided,
781 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100782 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100783 * \deprecated It is deprecated and discouraged to call this function
784 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
785 * are likely to remove the \p mode argument and have it
786 * implicitly set to #MBEDTLS_RSA_PRIVATE.
787 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100788 * \note Alternative implementations of RSA need not support
789 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300790 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100791 *
Hanno Becker9a467772018-12-13 09:54:59 +0000792 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000793 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
794 * this is used for blinding and should be provided; see
795 * mbedtls_rsa_private() for more. If \p mode is
796 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000797 * \param p_rng The RNG context to be passed to \p f_rng. This may be
798 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
799 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000800 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000801 * \param olen The address at which to store the length of
802 * the plaintext. This must not be \c NULL.
803 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000804 * of length \c ctx->len Bytes. For example, \c 256 Bytes
805 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000806 * \param output The buffer used to hold the plaintext. This must
807 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000808 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100809 *
810 * \return \c 0 on success.
811 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
812 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100813 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100814int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
815 int (*f_rng)(void *, unsigned char *, size_t),
816 void *p_rng,
817 int mode, size_t *olen,
818 const unsigned char *input,
819 unsigned char *output,
820 size_t output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +0100821
822/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100823 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
824 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100825 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100826 * \note The output buffer length \c output_max_len should be
827 * as large as the size \p ctx->len of \p ctx->N, for
828 * example, 128 Bytes if RSA-1024 is used, to be able to
829 * hold an arbitrary decrypted message. If it is not
830 * large enough to hold the decryption of the particular
831 * ciphertext provided, the function returns
832 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100833 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100834 * \deprecated It is deprecated and discouraged to call this function
835 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
836 * are likely to remove the \p mode argument and have it
837 * implicitly set to #MBEDTLS_RSA_PRIVATE.
838 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100839 * \note Alternative implementations of RSA need not support
840 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300841 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100842 *
Hanno Becker9a467772018-12-13 09:54:59 +0000843 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000844 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
845 * this is used for blinding and should be provided; see
846 * mbedtls_rsa_private() for more. If \p mode is
847 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000848 * \param p_rng The RNG context to be passed to \p f_rng. This may be
849 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
850 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000851 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100852 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000853 * This must be a readable buffer of length \p label_len
854 * Bytes. It may be \c NULL if \p label_len is \c 0.
855 * \param label_len The length of the label in Bytes.
856 * \param olen The address at which to store the length of
857 * the plaintext. This must not be \c NULL.
858 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000859 * of length \c ctx->len Bytes. For example, \c 256 Bytes
860 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000861 * \param output The buffer used to hold the plaintext. This must
862 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000863 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100864 *
865 * \return \c 0 on success.
866 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100867 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100868int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
869 int (*f_rng)(void *, unsigned char *, size_t),
870 void *p_rng,
871 int mode,
872 const unsigned char *label, size_t label_len,
873 size_t *olen,
874 const unsigned char *input,
875 unsigned char *output,
876 size_t output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +0100877
878/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000879 * \brief This function performs a private RSA operation to sign
880 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000881 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000882 * It is the generic wrapper for performing a PKCS#1
883 * signature using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000884 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000885 * \note The \p sig buffer must be as large as the size
886 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000887 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000888 * \note For PKCS#1 v2.1 encoding, see comments on
889 * mbedtls_rsa_rsassa_pss_sign() for details on
890 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100891 *
892 * \deprecated It is deprecated and discouraged to call this function
893 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
894 * are likely to remove the \p mode argument and have it
895 * implicitly set to #MBEDTLS_RSA_PRIVATE.
896 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100897 * \note Alternative implementations of RSA need not support
898 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300899 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100900 *
Hanno Becker9a467772018-12-13 09:54:59 +0000901 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000902 * \param f_rng The RNG function to use. If the padding mode is PKCS#1 v2.1,
903 * this must be provided. If the padding mode is PKCS#1 v1.5 and
904 * \p mode is #MBEDTLS_RSA_PRIVATE, it is used for blinding
905 * and should be provided; see mbedtls_rsa_private() for more
906 * more. It is ignored otherwise.
Hanno Becker9a467772018-12-13 09:54:59 +0000907 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
908 * if \p f_rng is \c NULL or doesn't need a context argument.
909 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000910 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100911 * \param md_alg The message-digest algorithm used to hash the original data.
912 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000913 * \param hashlen The length of the message digest.
914 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
915 * \param hash The buffer holding the message digest or raw data.
916 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
917 * buffer of length \p hashlen Bytes. If \p md_alg is not
918 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
919 * the size of the hash corresponding to \p md_alg.
920 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000921 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100922 * for an 2048-bit RSA modulus. A buffer length of
923 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100924 *
925 * \return \c 0 if the signing operation was successful.
926 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000927 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100928int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
929 int (*f_rng)(void *, unsigned char *, size_t),
930 void *p_rng,
931 int mode,
932 mbedtls_md_type_t md_alg,
933 unsigned int hashlen,
934 const unsigned char *hash,
935 unsigned char *sig);
Paul Bakker5121ce52009-01-03 21:22:43 +0000936
937/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000938 * \brief This function performs a PKCS#1 v1.5 signature
939 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100940 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100941 * \deprecated It is deprecated and discouraged to call this function
942 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
943 * are likely to remove the \p mode argument and have it
944 * implicitly set to #MBEDTLS_RSA_PRIVATE.
945 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100946 * \note Alternative implementations of RSA need not support
947 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300948 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100949 *
Hanno Becker9a467772018-12-13 09:54:59 +0000950 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000951 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
952 * this is used for blinding and should be provided; see
953 * mbedtls_rsa_private() for more. If \p mode is
954 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000955 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
956 * if \p f_rng is \c NULL or doesn't need a context argument.
957 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000958 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000959 * \param md_alg The message-digest algorithm used to hash the original data.
960 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000961 * \param hashlen The length of the message digest.
962 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
963 * \param hash The buffer holding the message digest or raw data.
964 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
965 * buffer of length \p hashlen Bytes. If \p md_alg is not
966 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
967 * the size of the hash corresponding to \p md_alg.
968 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000969 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100970 * for an 2048-bit RSA modulus. A buffer length of
971 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100972 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100973 * \return \c 0 if the signing operation was successful.
974 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100975 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100976int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
977 int (*f_rng)(void *, unsigned char *, size_t),
978 void *p_rng,
979 int mode,
980 mbedtls_md_type_t md_alg,
981 unsigned int hashlen,
982 const unsigned char *hash,
983 unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +0100984
985/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000986 * \brief This function performs a PKCS#1 v2.1 PSS signature
987 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100988 *
Manuel Pégourié-Gonnard727e1f12021-06-23 10:35:55 +0200989 * \note The \c hash_id set in \p ctx (when calling
990 * mbedtls_rsa_init() or by calling mbedtls_rsa_set_padding()
991 * afterwards) selects the hash used for the
Janos Follath456d7e02021-04-01 14:44:17 +0100992 * encoding operation and for the mask generation function
993 * (MGF1). For more details on the encoding operation and the
994 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +0000995 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follath456d7e02021-04-01 14:44:17 +0100996 * Specifications</em>.
Rose Zadike8b5b992018-03-27 12:19:47 +0100997 *
Cédric Meuter010ddc22020-04-25 09:24:11 +0200998 * \note This function enforces that the provided salt length complies
999 * with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 v2.2) §9.1.1
1000 * step 3. The constraint is that the hash length plus the salt
1001 * length plus 2 bytes must be at most the key length. If this
1002 * constraint is not met, this function returns
Jaeden Amero3725bb22018-09-07 19:12:36 +01001003 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
1004 *
Hanno Becker9a467772018-12-13 09:54:59 +00001005 * \param ctx The initialized RSA context to use.
1006 * \param f_rng The RNG function. It must not be \c NULL.
1007 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
1008 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +01001009 * \param md_alg The message-digest algorithm used to hash the original data.
1010 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001011 * \param hashlen The length of the message digest.
1012 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
1013 * \param hash The buffer holding the message digest or raw data.
1014 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1015 * buffer of length \p hashlen Bytes. If \p md_alg is not
1016 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1017 * the size of the hash corresponding to \p md_alg.
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001018 * \param saltlen The length of the salt that should be used.
Cédric Meuter010ddc22020-04-25 09:24:11 +02001019 * If passed #MBEDTLS_RSA_SALT_LEN_ANY, the function will use
1020 * the largest possible salt length up to the hash length,
1021 * which is the largest permitted by some standards including
1022 * FIPS 186-4 §5.5.
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001023 * \param sig The buffer to hold the signature. This must be a writable
1024 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1025 * for an 2048-bit RSA modulus. A buffer length of
1026 * #MBEDTLS_MPI_MAX_SIZE is always safe.
1027 *
1028 * \return \c 0 if the signing operation was successful.
1029 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
1030 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001031int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1032 int (*f_rng)(void *, unsigned char *, size_t),
1033 void *p_rng,
1034 mbedtls_md_type_t md_alg,
1035 unsigned int hashlen,
1036 const unsigned char *hash,
1037 int saltlen,
1038 unsigned char *sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001039
1040/**
1041 * \brief This function performs a PKCS#1 v2.1 PSS signature
1042 * operation (RSASSA-PSS-SIGN).
1043 *
Manuel Pégourié-Gonnard727e1f12021-06-23 10:35:55 +02001044 * \note The \c hash_id set in \p ctx (when calling
1045 * mbedtls_rsa_init() or by calling mbedtls_rsa_set_padding()
1046 * afterwards) selects the hash used for the
Janos Follath456d7e02021-04-01 14:44:17 +01001047 * encoding operation and for the mask generation function
1048 * (MGF1). For more details on the encoding operation and the
1049 * mask generation function, consult <em>RFC-3447: Public-Key
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001050 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follath456d7e02021-04-01 14:44:17 +01001051 * Specifications</em>.
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001052 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001053 * \note This function always uses the maximum possible salt size,
1054 * up to the length of the payload hash. This choice of salt
1055 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
1056 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
Rose Zadike8b5b992018-03-27 12:19:47 +01001057 * minimum salt size which is the hash size minus 2 bytes. If
1058 * this minimum size is too large given the key size (the salt
1059 * size, plus the hash size, plus 2 bytes must be no more than
1060 * the key size in bytes), this function returns
1061 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
1062 *
1063 * \deprecated It is deprecated and discouraged to call this function
1064 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
1065 * are likely to remove the \p mode argument and have it
1066 * implicitly set to #MBEDTLS_RSA_PRIVATE.
1067 *
1068 * \note Alternative implementations of RSA need not support
1069 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
1070 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
1071 *
1072 * \param ctx The initialized RSA context to use.
1073 * \param f_rng The RNG function. It must not be \c NULL.
1074 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
1075 * if \p f_rng doesn't need a context argument.
Paul Bakkerb3869132013-02-28 17:21:01 +01001076 * \param mode The mode of operation. This must be either
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Paul Bakkerb3869132013-02-28 17:21:01 +01001078 * \param md_alg The message-digest algorithm used to hash the original data.
1079 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001080 * \param hashlen The length of the message digest.
Janos Follath456d7e02021-04-01 14:44:17 +01001081 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
Hanno Becker9a467772018-12-13 09:54:59 +00001082 * \param hash The buffer holding the message digest or raw data.
1083 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1084 * buffer of length \p hashlen Bytes. If \p md_alg is not
1085 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1086 * the size of the hash corresponding to \p md_alg.
1087 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +00001088 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +01001089 * for an 2048-bit RSA modulus. A buffer length of
1090 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +01001091 *
1092 * \return \c 0 if the signing operation was successful.
1093 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
1094 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001095int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1096 int (*f_rng)(void *, unsigned char *, size_t),
1097 void *p_rng,
1098 int mode,
1099 mbedtls_md_type_t md_alg,
1100 unsigned int hashlen,
1101 const unsigned char *hash,
1102 unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001103
1104/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001105 * \brief This function performs a public RSA operation and checks
1106 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +00001107 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001108 * This is the generic wrapper for performing a PKCS#1
1109 * verification using the mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001111 * \note For PKCS#1 v2.1 encoding, see comments on
1112 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
1113 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +01001114 *
1115 * \deprecated It is deprecated and discouraged to call this function
1116 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1117 * are likely to remove the \p mode argument and have it
1118 * set to #MBEDTLS_RSA_PUBLIC.
1119 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001120 * \note Alternative implementations of RSA need not support
1121 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001122 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001123 *
Hanno Becker9a467772018-12-13 09:54:59 +00001124 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001125 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1126 * this is used for blinding and should be provided; see
1127 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001128 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1129 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1130 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001131 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001132 * \param md_alg The message-digest algorithm used to hash the original data.
1133 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001134 * \param hashlen The length of the message digest.
1135 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1136 * \param hash The buffer holding the message digest or raw data.
1137 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1138 * buffer of length \p hashlen Bytes. If \p md_alg is not
1139 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1140 * the size of the hash corresponding to \p md_alg.
1141 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001142 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1143 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001144 *
1145 * \return \c 0 if the verify operation was successful.
1146 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001147 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001148int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
1149 int (*f_rng)(void *, unsigned char *, size_t),
1150 void *p_rng,
1151 int mode,
1152 mbedtls_md_type_t md_alg,
1153 unsigned int hashlen,
1154 const unsigned char *hash,
1155 const unsigned char *sig);
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
1157/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001158 * \brief This function performs a PKCS#1 v1.5 verification
1159 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001160 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001161 * \deprecated It is deprecated and discouraged to call this function
1162 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1163 * are likely to remove the \p mode argument and have it
1164 * set to #MBEDTLS_RSA_PUBLIC.
1165 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001166 * \note Alternative implementations of RSA need not support
1167 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001168 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001169 *
Hanno Becker9a467772018-12-13 09:54:59 +00001170 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001171 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1172 * this is used for blinding and should be provided; see
1173 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001174 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1175 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1176 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001177 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +00001178 * \param md_alg The message-digest algorithm used to hash the original data.
1179 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001180 * \param hashlen The length of the message digest.
1181 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1182 * \param hash The buffer holding the message digest or raw data.
1183 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1184 * buffer of length \p hashlen Bytes. If \p md_alg is not
1185 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1186 * the size of the hash corresponding to \p md_alg.
1187 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001188 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1189 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001190 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001191 * \return \c 0 if the verify operation was successful.
1192 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001193 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001194int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
1195 int (*f_rng)(void *, unsigned char *, size_t),
1196 void *p_rng,
1197 int mode,
1198 mbedtls_md_type_t md_alg,
1199 unsigned int hashlen,
1200 const unsigned char *hash,
1201 const unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001202
1203/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001204 * \brief This function performs a PKCS#1 v2.1 PSS verification
1205 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001206 *
Manuel Pégourié-Gonnard727e1f12021-06-23 10:35:55 +02001207 * \note The \c hash_id set in \p ctx (when calling
1208 * mbedtls_rsa_init() or by calling mbedtls_rsa_set_padding()
1209 * afterwards) selects the hash used for the
Janos Follath456d7e02021-04-01 14:44:17 +01001210 * encoding operation and for the mask generation function
1211 * (MGF1). For more details on the encoding operation and the
1212 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +00001213 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Manuel Pégourié-Gonnard727e1f12021-06-23 10:35:55 +02001214 * Specifications</em>. If the \c hash_id set in \p ctx is
1215 * #MBEDTLS_MD_NONE, the \p md_alg parameter is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001216 *
1217 * \deprecated It is deprecated and discouraged to call this function
1218 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1219 * are likely to remove the \p mode argument and have it
1220 * implicitly set to #MBEDTLS_RSA_PUBLIC.
1221 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001222 * \note Alternative implementations of RSA need not support
1223 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001224 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001225 *
Hanno Becker9a467772018-12-13 09:54:59 +00001226 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001227 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1228 * this is used for blinding and should be provided; see
1229 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001230 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1231 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1232 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001233 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001234 * \param md_alg The message-digest algorithm used to hash the original data.
1235 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001236 * \param hashlen The length of the message digest.
1237 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1238 * \param hash The buffer holding the message digest or raw data.
1239 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1240 * buffer of length \p hashlen Bytes. If \p md_alg is not
1241 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1242 * the size of the hash corresponding to \p md_alg.
1243 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001244 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1245 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001246 *
1247 * \return \c 0 if the verify operation was successful.
1248 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001249 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001250int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
1251 int (*f_rng)(void *, unsigned char *, size_t),
1252 void *p_rng,
1253 int mode,
1254 mbedtls_md_type_t md_alg,
1255 unsigned int hashlen,
1256 const unsigned char *hash,
1257 const unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001258
1259/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001260 * \brief This function performs a PKCS#1 v2.1 PSS verification
1261 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001262 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001263 * \note The \p sig buffer must be as large as the size
1264 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001265 *
Manuel Pégourié-Gonnard727e1f12021-06-23 10:35:55 +02001266 * \note The \c hash_id set in \p ctx (when calling
1267 * mbedtls_rsa_init() or by calling mbedtls_rsa_set_padding()
1268 * afterwards) is ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001269 *
Hanno Becker9a467772018-12-13 09:54:59 +00001270 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001271 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1272 * this is used for blinding and should be provided; see
1273 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001274 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1275 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1276 * \param mode The mode of operation. This must be either
1277 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +01001278 * \param md_alg The message-digest algorithm used to hash the original data.
1279 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001280 * \param hashlen The length of the message digest.
1281 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1282 * \param hash The buffer holding the message digest or raw data.
1283 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1284 * buffer of length \p hashlen Bytes. If \p md_alg is not
1285 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1286 * the size of the hash corresponding to \p md_alg.
Janos Follath456d7e02021-04-01 14:44:17 +01001287 * \param mgf1_hash_id The message digest algorithm used for the
1288 * verification operation and the mask generation
1289 * function (MGF1). For more details on the encoding
1290 * operation and the mask generation function, consult
1291 * <em>RFC-3447: Public-Key Cryptography Standards
1292 * (PKCS) #1 v2.1: RSA Cryptography
1293 * Specifications</em>.
Hanno Becker9a467772018-12-13 09:54:59 +00001294 * \param expected_salt_len The length of the salt used in padding. Use
1295 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1296 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001297 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1298 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001299 *
1300 * \return \c 0 if the verify operation was successful.
1301 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001302 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001303int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
1304 int (*f_rng)(void *, unsigned char *, size_t),
1305 void *p_rng,
1306 int mode,
1307 mbedtls_md_type_t md_alg,
1308 unsigned int hashlen,
1309 const unsigned char *hash,
1310 mbedtls_md_type_t mgf1_hash_id,
1311 int expected_salt_len,
1312 const unsigned char *sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001313
1314/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001315 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001316 *
Hanno Becker9a467772018-12-13 09:54:59 +00001317 * \param dst The destination context. This must be initialized.
1318 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001319 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001320 * \return \c 0 on success.
1321 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001322 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001323int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src);
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001324
1325/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001326 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001327 *
Hanno Becker9a467772018-12-13 09:54:59 +00001328 * \param ctx The RSA context to free. May be \c NULL, in which case
1329 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001330 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001332void mbedtls_rsa_free(mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00001333
Ron Eldorfa8f6352017-06-20 15:48:46 +03001334#if defined(MBEDTLS_SELF_TEST)
1335
Paul Bakker5121ce52009-01-03 21:22:43 +00001336/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001337 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001339 * \return \c 0 on success.
1340 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001341 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001342int mbedtls_rsa_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +00001343
Ron Eldorfa8f6352017-06-20 15:48:46 +03001344#endif /* MBEDTLS_SELF_TEST */
1345
Paul Bakker5121ce52009-01-03 21:22:43 +00001346#ifdef __cplusplus
1347}
1348#endif
1349
Paul Bakker5121ce52009-01-03 21:22:43 +00001350#endif /* rsa.h */