blob: d77a5383262ad8552123d98303bc6528e2ac78af [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Rose Zadik21e29262018-04-17 14:08:56 +01004 * \brief This file provides an API for the RSA public-key cryptosystem.
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
Rose Zadike8b5b992018-03-27 12:19:47 +01006 * The RSA public-key cryptosystem is defined in <em>Public-Key
7 * Cryptography Standards (PKCS) #1 v1.5: RSA Encryption</em>
Darryl Green11999bb2018-03-13 15:22:58 +00008 * and <em>Public-Key Cryptography Standards (PKCS) #1 v2.1:
Rose Zadike8b5b992018-03-27 12:19:47 +01009 * RSA Cryptography Specifications</em>.
Rose Zadik042e97f2018-01-26 16:35:10 +000010 *
Darryl Greena40a1012018-01-05 15:33:17 +000011 */
12/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020013 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020014 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#ifndef MBEDTLS_RSA_H
29#define MBEDTLS_RSA_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020030#include "mbedtls/private_access.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Bence Szépkútic662b362021-05-27 11:25:03 +020032#include "mbedtls/build_info.h"
Paul Bakkered27a042013-04-18 22:46:23 +020033
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010034#include "mbedtls/bignum.h"
35#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_THREADING_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010038#include "mbedtls/threading.h"
Paul Bakkerc9965dc2013-09-29 14:58:17 +020039#endif
40
Paul Bakker13e2dfe2009-07-28 07:18:38 +000041/*
42 * RSA Error codes
43 */
Gilles Peskined2971572021-07-26 18:48:10 +020044/** Bad input parameters to function. */
45#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080
46/** Input data contains invalid padding and is rejected. */
47#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100
48/** Something failed during generation of a key. */
49#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180
50/** Key failed to pass the validity check of the library. */
51#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200
52/** The public key operation failed. */
53#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280
54/** The private key operation failed. */
55#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300
56/** The PKCS#1 verification failed. */
57#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380
58/** The output buffer for decryption is not large enough. */
59#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400
60/** The random generator failed to generate non-zeros. */
61#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480
Ron Eldor9924bdc2018-10-04 10:59:13 +030062
Paul Bakker5121ce52009-01-03 21:22:43 +000063/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020064 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000065 */
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Rose Zadike8b5b992018-03-27 12:19:47 +010067#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
68#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Rose Zadik042e97f2018-01-26 16:35:10 +000070#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
71#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020074
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020075/*
76 * The above constants may be used even if the RSA module is compile out,
Shaun Case8b0ecbc2021-12-20 21:14:10 -080077 * eg for alternative (PKCS#11) RSA implementations in the PK layers.
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020078 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010079
Paul Bakker407a0da2013-06-27 14:29:21 +020080#ifdef __cplusplus
81extern "C" {
82#endif
83
Ron Eldor4e6d55d2018-02-07 16:36:15 +020084#if !defined(MBEDTLS_RSA_ALT)
85// Regular implementation
86//
87
Paul Bakker5121ce52009-01-03 21:22:43 +000088/**
Rose Zadik042e97f2018-01-26 16:35:10 +000089 * \brief The RSA context structure.
Paul Bakker5121ce52009-01-03 21:22:43 +000090 */
Gilles Peskine449bd832023-01-11 14:50:10 +010091typedef struct mbedtls_rsa_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020092 int MBEDTLS_PRIVATE(ver); /*!< Reserved for internal purposes.
Gilles Peskine449bd832023-01-11 14:50:10 +010093 * Do not set this field in application
94 * code. Its meaning might change without
95 * notice. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020096 size_t MBEDTLS_PRIVATE(len); /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +000097
Mateusz Starzyk846f0212021-05-19 19:44:07 +020098 mbedtls_mpi MBEDTLS_PRIVATE(N); /*!< The public modulus. */
99 mbedtls_mpi MBEDTLS_PRIVATE(E); /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200101 mbedtls_mpi MBEDTLS_PRIVATE(D); /*!< The private exponent. */
102 mbedtls_mpi MBEDTLS_PRIVATE(P); /*!< The first prime factor. */
103 mbedtls_mpi MBEDTLS_PRIVATE(Q); /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100104
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200105 mbedtls_mpi MBEDTLS_PRIVATE(DP); /*!< <code>D % (P - 1)</code>. */
106 mbedtls_mpi MBEDTLS_PRIVATE(DQ); /*!< <code>D % (Q - 1)</code>. */
107 mbedtls_mpi MBEDTLS_PRIVATE(QP); /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200109 mbedtls_mpi MBEDTLS_PRIVATE(RN); /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200111 mbedtls_mpi MBEDTLS_PRIVATE(RP); /*!< cached <code>R^2 mod P</code>. */
112 mbedtls_mpi MBEDTLS_PRIVATE(RQ); /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200113
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200114 mbedtls_mpi MBEDTLS_PRIVATE(Vi); /*!< The cached blinding value. */
115 mbedtls_mpi MBEDTLS_PRIVATE(Vf); /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000116
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200117 int MBEDTLS_PRIVATE(padding); /*!< Selects padding mode:
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
119 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200120 int MBEDTLS_PRIVATE(hash_id); /*!< Hash identifier of mbedtls_md_type_t type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 as specified in md.h for use in the MGF
122 mask generating function used in the
123 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100125 /* Invariant: the mutex is initialized iff ver != 0. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200126 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex); /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200127#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000128}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200131#else /* MBEDTLS_RSA_ALT */
132#include "rsa_alt.h"
133#endif /* MBEDTLS_RSA_ALT */
134
Paul Bakker5121ce52009-01-03 21:22:43 +0000135/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000136 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200138 * \note This function initializes the padding and the hash
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200139 * identifier to respectively #MBEDTLS_RSA_PKCS_V15 and
140 * #MBEDTLS_MD_NONE. See mbedtls_rsa_set_padding() for more
141 * information about those parameters.
Ronald Cronc1905a12021-06-05 11:11:14 +0200142 *
143 * \param ctx The RSA context to initialize. This must not be \c NULL.
144 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100145void mbedtls_rsa_init(mbedtls_rsa_context *ctx);
Ronald Cronc1905a12021-06-05 11:11:14 +0200146
147/**
148 * \brief This function sets padding for an already initialized RSA
149 * context.
150 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000151 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000152 * encryption scheme and the RSASSA-PSS signature scheme.
153 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000154 * \note The \p hash_id parameter is ignored when using
155 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200156 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200157 * \note The choice of padding mode is strictly enforced for private
158 * key operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000159 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100160 * a default value, which can be overridden by calling specific
Ronald Cronc1905a12021-06-05 11:11:14 +0200161 * \c mbedtls_rsa_rsaes_xxx or \c mbedtls_rsa_rsassa_xxx
162 * functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200163 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000164 * \note The hash selected in \p hash_id is always used for OEAP
165 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100166 * making signatures, but can be overridden for verifying them.
167 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100168 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200169 * \param ctx The initialized RSA context to be configured.
Hanno Becker9a467772018-12-13 09:54:59 +0000170 * \param padding The padding mode to use. This must be either
171 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200172 * \param hash_id The hash identifier for PSS or OAEP, if \p padding is
173 * #MBEDTLS_RSA_PKCS_V21. #MBEDTLS_MD_NONE is accepted by this
174 * function but may be not suitable for some operations.
175 * Ignored if \p padding is #MBEDTLS_RSA_PKCS_V15.
Ronald Cronc1905a12021-06-05 11:11:14 +0200176 *
177 * \return \c 0 on success.
178 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING failure:
179 * \p padding or \p hash_id is invalid.
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100181int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
182 mbedtls_md_type_t hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
184/**
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 Peskine449bd832023-01-11 14:50:10 +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.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000241 * \param P_len The Byte length of \p P; it is ignored if \p P == NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000242 * \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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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 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 Peskine449bd832023-01-11 14:50:10 +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.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100431 * This is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000432 * \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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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 Cosgrovece7f18c2022-07-28 05:50:56 +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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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
528 * input is smaller than \p 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 Peskine449bd832023-01-11 14:50:10 +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 Case8b0ecbc2021-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.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100552 * \param f_rng The RNG function, used for blinding. It is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000553 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
Thomas Daubney03412782021-05-20 15:31:17 +0100554 * if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000555 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000556 * of length \c ctx->len Bytes. For example, \c 256 Bytes
557 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000558 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000559 * of length \c ctx->len Bytes. For example, \c 256 Bytes
560 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100561 *
562 * \return \c 0 on success.
563 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
564 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100566int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
567 int (*f_rng)(void *, unsigned char *, size_t),
568 void *p_rng,
569 const unsigned char *input,
570 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
572/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000573 * \brief This function adds the message padding, then performs an RSA
574 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000576 * It is the generic wrapper for performing a PKCS#1 encryption
Thomas Daubney21772772021-05-13 17:30:32 +0100577 * operation.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100578 *
Hanno Becker9a467772018-12-13 09:54:59 +0000579 * \param ctx The initialized RSA context to use.
Thomas Daubneyf54c5c52021-05-21 17:00:30 +0100580 * \param f_rng The RNG to use. It is used for padding generation
Thomas Daubney2c65db92021-05-21 10:58:28 +0100581 * and it is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000582 * \param p_rng The RNG context to be passed to \p f_rng. May be
Thomas Daubney03412782021-05-20 15:31:17 +0100583 * \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000584 * \param ilen The length of the plaintext in Bytes.
585 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000586 * buffer of size \p ilen Bytes. It may be \c NULL if
587 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000588 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000589 * of length \c ctx->len Bytes. For example, \c 256 Bytes
590 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100591 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100592 * \return \c 0 on success.
593 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100595int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
596 int (*f_rng)(void *, unsigned char *, size_t),
597 void *p_rng,
598 size_t ilen,
599 const unsigned char *input,
600 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
602/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000603 * \brief This function performs a PKCS#1 v1.5 encryption operation
604 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100605 *
Hanno Becker9a467772018-12-13 09:54:59 +0000606 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100607 * \param f_rng The RNG function to use. It is mandatory and used for
608 * padding generation.
Hanno Becker9a467772018-12-13 09:54:59 +0000609 * \param p_rng The RNG context to be passed to \p f_rng. This may
Thomas Daubney03412782021-05-20 15:31:17 +0100610 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000611 * \param ilen The length of the plaintext in Bytes.
612 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000613 * buffer of size \p ilen Bytes. It may be \c NULL if
614 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000615 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000616 * of length \c ctx->len Bytes. For example, \c 256 Bytes
617 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100618 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100619 * \return \c 0 on success.
620 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100621 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100622int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
623 int (*f_rng)(void *, unsigned char *, size_t),
624 void *p_rng,
625 size_t ilen,
626 const unsigned char *input,
627 unsigned char *output);
Paul Bakkerb3869132013-02-28 17:21:01 +0100628
629/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000630 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
631 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100632 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100633 * \note The output buffer must be as large as the size
634 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
635 *
Tom Cosgrove1e211442022-05-26 11:51:00 +0100636 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000637 * \param f_rng The RNG function to use. This is needed for padding
Thomas Daubney2c65db92021-05-21 10:58:28 +0100638 * generation and is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000639 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000640 * be \c NULL if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000641 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000642 * This must be a readable buffer of length \p label_len
643 * Bytes. It may be \c NULL if \p label_len is \c 0.
644 * \param label_len The length of the label in Bytes.
645 * \param ilen The length of the plaintext buffer \p input in Bytes.
646 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000647 * buffer of size \p ilen Bytes. It may be \c NULL if
648 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000649 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000650 * of length \c ctx->len Bytes. For example, \c 256 Bytes
651 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100652 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100653 * \return \c 0 on success.
654 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100655 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100656int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
657 int (*f_rng)(void *, unsigned char *, size_t),
658 void *p_rng,
659 const unsigned char *label, size_t label_len,
660 size_t ilen,
661 const unsigned char *input,
662 unsigned char *output);
Paul Bakkerb3869132013-02-28 17:21:01 +0100663
664/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000665 * \brief This function performs an RSA operation, then removes the
666 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000668 * It is the generic wrapper for performing a PKCS#1 decryption
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100669 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100671 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000672 * as large as the size \p ctx->len of \p ctx->N (for example,
673 * 128 Bytes if RSA-1024 is used) to be able to hold an
674 * arbitrary decrypted message. If it is not large enough to
675 * hold the decryption of the particular ciphertext provided,
676 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100677 *
Hanno Becker9a467772018-12-13 09:54:59 +0000678 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100679 * \param f_rng The RNG function. This is used for blinding and is
680 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000681 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100682 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000683 * \param olen The address at which to store the length of
684 * the plaintext. This must not be \c NULL.
685 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000686 * of length \c ctx->len Bytes. For example, \c 256 Bytes
687 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000688 * \param output The buffer used to hold the plaintext. This must
689 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000690 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100691 *
692 * \return \c 0 on success.
693 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000694 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
696 int (*f_rng)(void *, unsigned char *, size_t),
697 void *p_rng,
698 size_t *olen,
699 const unsigned char *input,
700 unsigned char *output,
701 size_t output_max_len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000702
703/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000704 * \brief This function performs a PKCS#1 v1.5 decryption
705 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100706 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100707 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000708 * as large as the size \p ctx->len of \p ctx->N, for example,
709 * 128 Bytes if RSA-1024 is used, to be able to hold an
710 * arbitrary decrypted message. If it is not large enough to
711 * hold the decryption of the particular ciphertext provided,
712 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100713 *
Hanno Becker9a467772018-12-13 09:54:59 +0000714 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100715 * \param f_rng The RNG function. This is used for blinding and is
716 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000717 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100718 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000719 * \param olen The address at which to store the length of
720 * the plaintext. This must not be \c NULL.
721 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000722 * of length \c ctx->len Bytes. For example, \c 256 Bytes
723 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000724 * \param output The buffer used to hold the plaintext. This must
725 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000726 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100727 *
728 * \return \c 0 on success.
729 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
730 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100731 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
733 int (*f_rng)(void *, unsigned char *, size_t),
734 void *p_rng,
735 size_t *olen,
736 const unsigned char *input,
737 unsigned char *output,
738 size_t output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +0100739
740/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100741 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
742 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100743 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100744 * \note The output buffer length \c output_max_len should be
745 * as large as the size \p ctx->len of \p ctx->N, for
746 * example, 128 Bytes if RSA-1024 is used, to be able to
747 * hold an arbitrary decrypted message. If it is not
748 * large enough to hold the decryption of the particular
749 * ciphertext provided, the function returns
750 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100751 *
Hanno Becker9a467772018-12-13 09:54:59 +0000752 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100753 * \param f_rng The RNG function. This is used for blinding and is
754 * mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000755 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100756 * \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100757 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000758 * This must be a readable buffer of length \p label_len
759 * Bytes. It may be \c NULL if \p label_len is \c 0.
760 * \param label_len The length of the label in Bytes.
761 * \param olen The address at which to store the length of
762 * the plaintext. This must not be \c NULL.
763 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000764 * of length \c ctx->len Bytes. For example, \c 256 Bytes
765 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000766 * \param output The buffer used to hold the plaintext. This must
767 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000768 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100769 *
770 * \return \c 0 on success.
771 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100772 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100773int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
774 int (*f_rng)(void *, unsigned char *, size_t),
775 void *p_rng,
776 const unsigned char *label, size_t label_len,
777 size_t *olen,
778 const unsigned char *input,
779 unsigned char *output,
780 size_t output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +0100781
782/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000783 * \brief This function performs a private RSA operation to sign
784 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000785 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000786 * It is the generic wrapper for performing a PKCS#1
Thomas Daubney140184d2021-05-18 16:04:07 +0100787 * signature.
Paul Bakker5121ce52009-01-03 21:22:43 +0000788 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000789 * \note The \p sig buffer must be as large as the size
790 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000791 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000792 * \note For PKCS#1 v2.1 encoding, see comments on
793 * mbedtls_rsa_rsassa_pss_sign() for details on
794 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100795 *
Hanno Becker9a467772018-12-13 09:54:59 +0000796 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100797 * \param f_rng The RNG function to use. This is mandatory and
798 * must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000799 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
Thomas Daubney03412782021-05-20 15:31:17 +0100800 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100801 * \param md_alg The message-digest algorithm used to hash the original data.
802 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200803 * \param hashlen The length of the message digest or raw data in Bytes.
804 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
805 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000806 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200807 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000808 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000809 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100810 * for an 2048-bit RSA modulus. A buffer length of
811 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100812 *
813 * \return \c 0 if the signing operation was successful.
814 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000815 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
817 int (*f_rng)(void *, unsigned char *, size_t),
818 void *p_rng,
819 mbedtls_md_type_t md_alg,
820 unsigned int hashlen,
821 const unsigned char *hash,
822 unsigned char *sig);
Paul Bakker5121ce52009-01-03 21:22:43 +0000823
824/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000825 * \brief This function performs a PKCS#1 v1.5 signature
826 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100827 *
Hanno Becker9a467772018-12-13 09:54:59 +0000828 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100829 * \param f_rng The RNG function. This is used for blinding and is
830 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000831 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
Thomas Daubney2c65db92021-05-21 10:58:28 +0100832 * if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000833 * \param md_alg The message-digest algorithm used to hash the original data.
834 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200835 * \param hashlen The length of the message digest or raw data in Bytes.
836 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
837 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000838 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200839 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000840 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000841 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100842 * for an 2048-bit RSA modulus. A buffer length of
843 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100844 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100845 * \return \c 0 if the signing operation was successful.
846 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100847 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100848int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
849 int (*f_rng)(void *, unsigned char *, size_t),
850 void *p_rng,
851 mbedtls_md_type_t md_alg,
852 unsigned int hashlen,
853 const unsigned char *hash,
854 unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +0100855
856/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000857 * \brief This function performs a PKCS#1 v2.1 PSS signature
858 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100859 *
Janos Follathb7953322021-04-01 14:44:17 +0100860 * \note The \c hash_id set in \p ctx by calling
861 * mbedtls_rsa_set_padding() selects the hash used for the
862 * encoding operation and for the mask generation function
863 * (MGF1). For more details on the encoding operation and the
864 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +0000865 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100866 * Specifications</em>.
Rose Zadike8b5b992018-03-27 12:19:47 +0100867 *
Cédric Meuter010ddc22020-04-25 09:24:11 +0200868 * \note This function enforces that the provided salt length complies
869 * with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 v2.2) §9.1.1
870 * step 3. The constraint is that the hash length plus the salt
871 * length plus 2 bytes must be at most the key length. If this
872 * constraint is not met, this function returns
Jaeden Amero3725bb22018-09-07 19:12:36 +0100873 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
874 *
Hanno Becker9a467772018-12-13 09:54:59 +0000875 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100876 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000877 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
878 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100879 * \param md_alg The message-digest algorithm used to hash the original data.
880 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200881 * \param hashlen The length of the message digest or raw data in Bytes.
882 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
883 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000884 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200885 * This must be a readable buffer of at least \p hashlen Bytes.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200886 * \param saltlen The length of the salt that should be used.
Cédric Meuter010ddc22020-04-25 09:24:11 +0200887 * If passed #MBEDTLS_RSA_SALT_LEN_ANY, the function will use
888 * the largest possible salt length up to the hash length,
889 * which is the largest permitted by some standards including
890 * FIPS 186-4 §5.5.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200891 * \param sig The buffer to hold the signature. This must be a writable
892 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
893 * for an 2048-bit RSA modulus. A buffer length of
894 * #MBEDTLS_MPI_MAX_SIZE is always safe.
895 *
896 * \return \c 0 if the signing operation was successful.
897 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
898 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100899int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
900 int (*f_rng)(void *, unsigned char *, size_t),
901 void *p_rng,
902 mbedtls_md_type_t md_alg,
903 unsigned int hashlen,
904 const unsigned char *hash,
905 int saltlen,
906 unsigned char *sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200907
908/**
909 * \brief This function performs a PKCS#1 v2.1 PSS signature
910 * operation (RSASSA-PSS-SIGN).
911 *
Janos Follathb7953322021-04-01 14:44:17 +0100912 * \note The \c hash_id set in \p ctx by calling
913 * mbedtls_rsa_set_padding() selects the hash used for the
914 * encoding operation and for the mask generation function
915 * (MGF1). For more details on the encoding operation and the
916 * mask generation function, consult <em>RFC-3447: Public-Key
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200917 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100918 * Specifications</em>.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200919 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000920 * \note This function always uses the maximum possible salt size,
921 * up to the length of the payload hash. This choice of salt
922 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
923 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
Rose Zadike8b5b992018-03-27 12:19:47 +0100924 * minimum salt size which is the hash size minus 2 bytes. If
925 * this minimum size is too large given the key size (the salt
926 * size, plus the hash size, plus 2 bytes must be no more than
927 * the key size in bytes), this function returns
928 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
929 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100930 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100931 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100932 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
933 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100934 * \param md_alg The message-digest algorithm used to hash the original data.
935 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200936 * \param hashlen The length of the message digest or raw data in Bytes.
937 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
938 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000939 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200940 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000941 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000942 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100943 * for an 2048-bit RSA modulus. A buffer length of
944 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100945 *
946 * \return \c 0 if the signing operation was successful.
947 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100948 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100949int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
950 int (*f_rng)(void *, unsigned char *, size_t),
951 void *p_rng,
952 mbedtls_md_type_t md_alg,
953 unsigned int hashlen,
954 const unsigned char *hash,
955 unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +0100956
957/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000958 * \brief This function performs a public RSA operation and checks
959 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +0000960 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000961 * This is the generic wrapper for performing a PKCS#1
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100962 * verification.
Paul Bakker5121ce52009-01-03 21:22:43 +0000963 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000964 * \note For PKCS#1 v2.1 encoding, see comments on
965 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
966 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100967 *
Hanno Becker9a467772018-12-13 09:54:59 +0000968 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +0100969 * \param md_alg The message-digest algorithm used to hash the original data.
970 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200971 * \param hashlen The length of the message digest or raw data in Bytes.
972 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
973 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000974 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200975 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000976 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +0000977 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
978 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100979 *
980 * \return \c 0 if the verify operation was successful.
981 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000982 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100983int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
984 mbedtls_md_type_t md_alg,
985 unsigned int hashlen,
986 const unsigned char *hash,
987 const unsigned char *sig);
Paul Bakker5121ce52009-01-03 21:22:43 +0000988
989/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000990 * \brief This function performs a PKCS#1 v1.5 verification
991 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +0100992 *
Hanno Becker9a467772018-12-13 09:54:59 +0000993 * \param ctx The initialized RSA public key context to use.
Rose Zadik042e97f2018-01-26 16:35:10 +0000994 * \param md_alg The message-digest algorithm used to hash the original data.
995 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200996 * \param hashlen The length of the message digest or raw data in Bytes.
997 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
998 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000999 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001000 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001001 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001002 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1003 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001004 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001005 * \return \c 0 if the verify operation was successful.
1006 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001007 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001008int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
1009 mbedtls_md_type_t md_alg,
1010 unsigned int hashlen,
1011 const unsigned char *hash,
1012 const unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001013
1014/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001015 * \brief This function performs a PKCS#1 v2.1 PSS verification
1016 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001017 *
Janos Follathb7953322021-04-01 14:44:17 +01001018 * \note The \c hash_id set in \p ctx by calling
1019 * mbedtls_rsa_set_padding() selects the hash used for the
1020 * encoding operation and for the mask generation function
1021 * (MGF1). For more details on the encoding operation and the
1022 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +00001023 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +01001024 * Specifications</em>. If the \c hash_id set in \p ctx by
1025 * mbedtls_rsa_set_padding() is #MBEDTLS_MD_NONE, the \p md_alg
1026 * parameter is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001027 *
Hanno Becker9a467772018-12-13 09:54:59 +00001028 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001029 * \param md_alg The message-digest algorithm used to hash the original data.
1030 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001031 * \param hashlen The length of the message digest or raw data in Bytes.
1032 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1033 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001034 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001035 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001036 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001037 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1038 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001039 *
1040 * \return \c 0 if the verify operation was successful.
1041 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001042 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001043int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
1044 mbedtls_md_type_t md_alg,
1045 unsigned int hashlen,
1046 const unsigned char *hash,
1047 const unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001048
1049/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001050 * \brief This function performs a PKCS#1 v2.1 PSS verification
1051 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001052 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001053 * \note The \p sig buffer must be as large as the size
1054 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001055 *
Janos Follathb7953322021-04-01 14:44:17 +01001056 * \note The \c hash_id set in \p ctx by mbedtls_rsa_set_padding() is
1057 * ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001058 *
Hanno Becker9a467772018-12-13 09:54:59 +00001059 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001060 * \param md_alg The message-digest algorithm used to hash the original data.
1061 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001062 * \param hashlen The length of the message digest or raw data in Bytes.
1063 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1064 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001065 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001066 * This must be a readable buffer of at least \p hashlen Bytes.
Janos Follathb7953322021-04-01 14:44:17 +01001067 * \param mgf1_hash_id The message digest algorithm used for the
1068 * verification operation and the mask generation
1069 * function (MGF1). For more details on the encoding
1070 * operation and the mask generation function, consult
1071 * <em>RFC-3447: Public-Key Cryptography Standards
1072 * (PKCS) #1 v2.1: RSA Cryptography
1073 * Specifications</em>.
Hanno Becker9a467772018-12-13 09:54:59 +00001074 * \param expected_salt_len The length of the salt used in padding. Use
1075 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1076 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001077 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1078 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001079 *
1080 * \return \c 0 if the verify operation was successful.
1081 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001082 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001083int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
1084 mbedtls_md_type_t md_alg,
1085 unsigned int hashlen,
1086 const unsigned char *hash,
1087 mbedtls_md_type_t mgf1_hash_id,
1088 int expected_salt_len,
1089 const unsigned char *sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001090
1091/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001092 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001093 *
Hanno Becker9a467772018-12-13 09:54:59 +00001094 * \param dst The destination context. This must be initialized.
1095 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001096 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001097 * \return \c 0 on success.
1098 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001099 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001100int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src);
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001101
1102/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001103 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001104 *
Hanno Becker9a467772018-12-13 09:54:59 +00001105 * \param ctx The RSA context to free. May be \c NULL, in which case
1106 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001107 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001108 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001109void mbedtls_rsa_free(mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00001110
Ron Eldorfa8f6352017-06-20 15:48:46 +03001111#if defined(MBEDTLS_SELF_TEST)
1112
Paul Bakker5121ce52009-01-03 21:22:43 +00001113/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001114 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001115 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001116 * \return \c 0 on success.
1117 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001118 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001119int mbedtls_rsa_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +00001120
Ron Eldorfa8f6352017-06-20 15:48:46 +03001121#endif /* MBEDTLS_SELF_TEST */
1122
Paul Bakker5121ce52009-01-03 21:22:43 +00001123#ifdef __cplusplus
1124}
1125#endif
1126
Paul Bakker5121ce52009-01-03 21:22:43 +00001127#endif /* rsa.h */