blob: fe29548ead7b7f6c52fc97e5952ae017cd246b62 [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
Mateusz Starzyk16fec332021-07-22 16:43:35 +020067/** Use PKCS#1 v1.5 encoding. */
68#define MBEDTLS_RSA_PKCS_V15 0
69/** Use PKCS#1 v2.1 encoding. */
70#define MBEDTLS_RSA_PKCS_V21 1
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Mateusz Starzyk16fec332021-07-22 16:43:35 +020072/** Identifier for RSA signature operations. */
73#define MBEDTLS_RSA_SIGN 1
74/** Identifier for RSA encryption and decryption operations. */
75#define MBEDTLS_RSA_CRYPT 2
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020078
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020079/*
80 * The above constants may be used even if the RSA module is compile out,
81 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
82 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010083
Paul Bakker407a0da2013-06-27 14:29:21 +020084#ifdef __cplusplus
85extern "C" {
86#endif
87
Ron Eldor4e6d55d2018-02-07 16:36:15 +020088#if !defined(MBEDTLS_RSA_ALT)
89// Regular implementation
90//
91
Paul Bakker5121ce52009-01-03 21:22:43 +000092/**
Rose Zadik042e97f2018-01-26 16:35:10 +000093 * \brief The RSA context structure.
Paul Bakker5121ce52009-01-03 21:22:43 +000094 */
Dawid Drozd428cc522018-07-24 10:02:47 +020095typedef struct mbedtls_rsa_context
Paul Bakker5121ce52009-01-03 21:22:43 +000096{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020097 int MBEDTLS_PRIVATE(ver); /*!< Reserved for internal purposes.
Gilles Peskine4337a9c2021-02-09 18:59:42 +010098 * Do not set this field in application
99 * code. Its meaning might change without
100 * notice. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200101 size_t MBEDTLS_PRIVATE(len); /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200103 mbedtls_mpi MBEDTLS_PRIVATE(N); /*!< The public modulus. */
104 mbedtls_mpi MBEDTLS_PRIVATE(E); /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200106 mbedtls_mpi MBEDTLS_PRIVATE(D); /*!< The private exponent. */
107 mbedtls_mpi MBEDTLS_PRIVATE(P); /*!< The first prime factor. */
108 mbedtls_mpi MBEDTLS_PRIVATE(Q); /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100109
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200110 mbedtls_mpi MBEDTLS_PRIVATE(DP); /*!< <code>D % (P - 1)</code>. */
111 mbedtls_mpi MBEDTLS_PRIVATE(DQ); /*!< <code>D % (Q - 1)</code>. */
112 mbedtls_mpi MBEDTLS_PRIVATE(QP); /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200114 mbedtls_mpi MBEDTLS_PRIVATE(RN); /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200116 mbedtls_mpi MBEDTLS_PRIVATE(RP); /*!< cached <code>R^2 mod P</code>. */
117 mbedtls_mpi MBEDTLS_PRIVATE(RQ); /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200118
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200119 mbedtls_mpi MBEDTLS_PRIVATE(Vi); /*!< The cached blinding value. */
120 mbedtls_mpi MBEDTLS_PRIVATE(Vf); /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000121
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200122 int MBEDTLS_PRIVATE(padding); /*!< Selects padding mode:
Rose Zadik042e97f2018-01-26 16:35:10 +0000123 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
124 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200125 int MBEDTLS_PRIVATE(hash_id); /*!< Hash identifier of mbedtls_md_type_t type,
Rose Zadik042e97f2018-01-26 16:35:10 +0000126 as specified in md.h for use in the MGF
127 mask generating function used in the
128 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129#if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100130 /* Invariant: the mutex is initialized iff ver != 0. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200131 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex); /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200132#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000133}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200136#else /* MBEDTLS_RSA_ALT */
137#include "rsa_alt.h"
138#endif /* MBEDTLS_RSA_ALT */
139
Paul Bakker5121ce52009-01-03 21:22:43 +0000140/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000141 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200143 * \note This function initializes the padding and the hash
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200144 * identifier to respectively #MBEDTLS_RSA_PKCS_V15 and
145 * #MBEDTLS_MD_NONE. See mbedtls_rsa_set_padding() for more
146 * information about those parameters.
Ronald Cronc1905a12021-06-05 11:11:14 +0200147 *
148 * \param ctx The RSA context to initialize. This must not be \c NULL.
149 */
150void mbedtls_rsa_init( mbedtls_rsa_context *ctx );
151
152/**
153 * \brief This function sets padding for an already initialized RSA
154 * context.
155 *
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 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200162 * \note The choice of padding mode is strictly enforced for private
163 * key 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
Ronald Cronc1905a12021-06-05 11:11:14 +0200166 * \c mbedtls_rsa_rsaes_xxx or \c mbedtls_rsa_rsassa_xxx
167 * functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200168 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000169 * \note The hash selected in \p hash_id is always used for OEAP
170 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100171 * making signatures, but can be overridden for verifying them.
172 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100173 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200174 * \param ctx The initialized RSA context to be configured.
Hanno Becker9a467772018-12-13 09:54:59 +0000175 * \param padding The padding mode to use. This must be either
176 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200177 * \param hash_id The hash identifier for PSS or OAEP, if \p padding is
178 * #MBEDTLS_RSA_PKCS_V21. #MBEDTLS_MD_NONE is accepted by this
179 * function but may be not suitable for some operations.
180 * Ignored if \p padding is #MBEDTLS_RSA_PKCS_V15.
Ronald Cronc1905a12021-06-05 11:11:14 +0200181 *
182 * \return \c 0 on success.
183 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING failure:
184 * \p padding or \p hash_id is invalid.
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 */
Ronald Cronc1905a12021-06-05 11:11:14 +0200186int mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
187 mbedtls_md_type_t hash_id );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
189/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000190 * \brief This function imports a set of core parameters into an
191 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100192 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100193 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000194 * imports, if the parameters are not simultaneously present.
195 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100196 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000197 * by a call to mbedtls_rsa_complete(), which checks and
198 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100199 * public or private RSA key.
200 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000201 * \note See mbedtls_rsa_complete() for more information on which
202 * parameters are necessary to set up a private or public
203 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100204 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100205 * \note The imported parameters are copied and need not be preserved
206 * for the lifetime of the RSA context being set up.
207 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100208 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000209 * \param N The RSA modulus. This may be \c NULL.
210 * \param P The first prime factor of \p N. This may be \c NULL.
211 * \param Q The second prime factor of \p N. This may be \c NULL.
212 * \param D The private exponent. This may be \c NULL.
213 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100214 *
215 * \return \c 0 on success.
216 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100217 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100218int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
219 const mbedtls_mpi *N,
220 const mbedtls_mpi *P, const mbedtls_mpi *Q,
221 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100222
223/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000224 * \brief This function imports core RSA parameters, in raw big-endian
225 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100227 * \note This function can be called multiple times for successive
228 * imports, if the parameters are not simultaneously present.
229 *
230 * Any sequence of calls to this function should be followed
231 * by a call to mbedtls_rsa_complete(), which checks and
232 * completes the provided information to a ready-for-use
233 * public or private RSA key.
234 *
235 * \note See mbedtls_rsa_complete() for more information on which
236 * parameters are necessary to set up a private or public
237 * RSA key.
238 *
239 * \note The imported parameters are copied and need not be preserved
240 * for the lifetime of the RSA context being set up.
241 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000242 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000243 * \param N The RSA modulus. This may be \c NULL.
244 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
245 * \param P The first prime factor of \p N. This may be \c NULL.
246 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
247 * \param Q The second prime factor of \p N. This may be \c NULL.
248 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
249 * \param D The private exponent. This may be \c NULL.
250 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
251 * \param E The public exponent. This may be \c NULL.
252 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100254 * \return \c 0 on success.
255 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100256 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100257int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100258 unsigned char const *N, size_t N_len,
259 unsigned char const *P, size_t P_len,
260 unsigned char const *Q, size_t Q_len,
261 unsigned char const *D, size_t D_len,
262 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100263
264/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000265 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100266 * a set of imported core parameters.
267 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000268 * To setup an RSA public key, precisely \p N and \p E
269 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100270 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000271 * To setup an RSA private key, sufficient information must
272 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100273 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000274 * The default implementation supports the following:
275 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
276 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
277 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100278 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000279 * If this function runs successfully, it guarantees that
280 * the RSA context can be used for RSA operations without
281 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100282 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100283 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000284 * for the imported parameters. In particular, parameters that
285 * are not needed by the implementation might be silently
286 * discarded and left unchecked. To check the consistency
287 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100288 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100289 * \param ctx The initialized RSA context holding imported parameters.
290 *
291 * \return \c 0 on success.
292 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
293 * failed.
294 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100295 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100296int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100297
298/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000299 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100300 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000301 * If this function runs successfully, the non-NULL buffers
302 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
303 * written, with additional unused space filled leading by
304 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100305 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000306 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300307 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000308 * <li>An alternative RSA implementation is in use, which
309 * stores the key externally, and either cannot or should
310 * not export it into RAM.</li>
311 * <li>A SW or HW implementation might not support a certain
312 * deduction. For example, \p P, \p Q from \p N, \p D,
313 * and \p E if the former are not part of the
314 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100315 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000316 * If the function fails due to an unsupported operation,
317 * the RSA context stays intact and remains usable.
318 *
319 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000320 * \param N The MPI to hold the RSA modulus.
321 * This may be \c NULL if this field need not be exported.
322 * \param P The MPI to hold the first prime factor of \p N.
323 * This may be \c NULL if this field need not be exported.
324 * \param Q The MPI to hold the second prime factor of \p N.
325 * This may be \c NULL if this field need not be exported.
326 * \param D The MPI to hold the private exponent.
327 * This may be \c NULL if this field need not be exported.
328 * \param E The MPI to hold the public exponent.
329 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000330 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100331 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300332 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000333 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100334 * functionality or because of security policies.
335 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100336 *
337 */
338int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
339 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
340 mbedtls_mpi *D, mbedtls_mpi *E );
341
342/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000343 * \brief This function exports core parameters of an RSA key
344 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100345 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000346 * If this function runs successfully, the non-NULL buffers
347 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
348 * written, with additional unused space filled leading by
349 * zero Bytes.
350 *
351 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300352 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000353 * <li>An alternative RSA implementation is in use, which
354 * stores the key externally, and either cannot or should
355 * not export it into RAM.</li>
356 * <li>A SW or HW implementation might not support a certain
357 * deduction. For example, \p P, \p Q from \p N, \p D,
358 * and \p E if the former are not part of the
359 * implementation.</li></ul>
360 * If the function fails due to an unsupported operation,
361 * the RSA context stays intact and remains usable.
362 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100363 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100364 * buffer pointers are NULL.
365 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000366 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000367 * \param N The Byte array to store the RSA modulus,
368 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000369 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000370 * \param P The Byte array to hold the first prime factor of \p N,
371 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000372 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000373 * \param Q The Byte array to hold the second prime factor of \p N,
374 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000375 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000376 * \param D The Byte array to hold the private exponent,
377 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000378 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000379 * \param E The Byte array to hold the public exponent,
380 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000381 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100382 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100383 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300384 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000385 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100386 * functionality or because of security policies.
387 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100388 */
389int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
390 unsigned char *N, size_t N_len,
391 unsigned char *P, size_t P_len,
392 unsigned char *Q, size_t Q_len,
393 unsigned char *D, size_t D_len,
394 unsigned char *E, size_t E_len );
395
396/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000397 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100398 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100399 * \note Alternative RSA implementations not using CRT-parameters
400 * internally can implement this function based on
401 * mbedtls_rsa_deduce_opt().
402 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000403 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000404 * \param DP The MPI to hold \c D modulo `P-1`,
405 * or \c NULL if it need not be exported.
406 * \param DQ The MPI to hold \c D modulo `Q-1`,
407 * or \c NULL if it need not be exported.
408 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
409 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100410 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100411 * \return \c 0 on success.
412 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100413 *
414 */
415int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
416 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
417
Paul Bakker5121ce52009-01-03 21:22:43 +0000418/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000419 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100420 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000421 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100422 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000423 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100424 *
425 */
426size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
427
428/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000429 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000431 * \note mbedtls_rsa_init() must be called before this function,
432 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 *
Hanno Becker9a467772018-12-13 09:54:59 +0000434 * \param ctx The initialized RSA context used to hold the key.
435 * \param f_rng The RNG function to be used for key generation.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100436 * This is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000437 * \param p_rng The RNG context to be passed to \p f_rng.
438 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100439 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000440 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000441 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100442 *
443 * \return \c 0 on success.
444 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000445 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100447 int (*f_rng)(void *, unsigned char *, size_t),
448 void *p_rng,
449 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000450
451/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000452 * \brief This function checks if a context contains at least an RSA
453 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000455 * If the function runs successfully, it is guaranteed that
456 * enough information is present to perform an RSA public key
457 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 *
Hanno Becker9a467772018-12-13 09:54:59 +0000459 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000460 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100461 * \return \c 0 on success.
462 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100463 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
467/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000468 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100469 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 *
Hanno Becker68767a62017-10-17 10:13:31 +0100471 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000472 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100473 * on the given context, but that the various parameters are
474 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000475 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100476 *
477 * \warning This function should catch accidental misconfigurations
478 * like swapping of parameters, but it cannot establish full
479 * trust in neither the quality nor the consistency of the key
480 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000481 * <ul><li>Consistency: Imported parameters that are irrelevant
482 * for the implementation might be silently dropped. If dropped,
483 * the current function does not have access to them,
484 * and therefore cannot check them. See mbedtls_rsa_complete().
485 * If you want to check the consistency of the entire
486 * content of an PKCS1-encoded RSA private key, for example, you
487 * should use mbedtls_rsa_validate_params() before setting
488 * up the RSA context.
489 * Additionally, if the implementation performs empirical checks,
490 * these checks substantiate but do not guarantee consistency.</li>
491 * <li>Quality: This function is not expected to perform
492 * extended quality assessments like checking that the prime
493 * factors are safe. Additionally, it is the responsibility of the
494 * user to ensure the trustworthiness of the source of his RSA
495 * parameters, which goes beyond what is effectively checkable
496 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100497 *
Hanno Becker9a467772018-12-13 09:54:59 +0000498 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100499 *
500 * \return \c 0 on success.
501 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
505/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000506 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100507 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000508 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100509 *
Hanno Becker9a467772018-12-13 09:54:59 +0000510 * \param pub The initialized RSA context holding the public key.
511 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000512 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100513 * \return \c 0 on success.
514 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100515 */
Hanno Becker98838b02017-10-02 13:16:10 +0100516int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
517 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100518
519/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000520 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 *
Hanno Becker9a467772018-12-13 09:54:59 +0000522 * \param ctx The initialized RSA context to use.
523 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000524 * of length \c ctx->len Bytes. For example, \c 256 Bytes
525 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000526 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000527 * of length \c ctx->len Bytes. For example, \c 256 Bytes
528 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000529 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000530 * \note This function does not handle message padding.
531 *
532 * \note Make sure to set \p input[0] = 0 or ensure that
533 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100535 * \return \c 0 on success.
536 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000539 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 unsigned char *output );
541
542/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000543 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 *
Hanno Becker24120612017-10-26 11:53:35 +0100545 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100546 *
547 * \note If blinding is used, both the base of exponentation
Hanno Becker24120612017-10-26 11:53:35 +0100548 * and the exponent are blinded, providing protection
549 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100550 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100551 * \warning It is deprecated and a security risk to not provide
552 * a PRNG here and thereby prevent the use of blinding.
553 * Future versions of the library may enforce the presence
554 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100555 *
Hanno Becker9a467772018-12-13 09:54:59 +0000556 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100557 * \param f_rng The RNG function, used for blinding. It is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000558 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
Thomas Daubney03412782021-05-20 15:31:17 +0100559 * if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000560 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000561 * of length \c ctx->len Bytes. For example, \c 256 Bytes
562 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000563 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000564 * of length \c ctx->len Bytes. For example, \c 256 Bytes
565 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100566 *
567 * \return \c 0 on success.
568 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
569 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200572 int (*f_rng)(void *, unsigned char *, size_t),
573 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000574 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 unsigned char *output );
576
577/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000578 * \brief This function adds the message padding, then performs an RSA
579 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000581 * It is the generic wrapper for performing a PKCS#1 encryption
Thomas Daubney21772772021-05-13 17:30:32 +0100582 * operation.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100583 *
Hanno Becker9a467772018-12-13 09:54:59 +0000584 * \param ctx The initialized RSA context to use.
Thomas Daubneyf54c5c52021-05-21 17:00:30 +0100585 * \param f_rng The RNG to use. It is used for padding generation
Thomas Daubney2c65db92021-05-21 10:58:28 +0100586 * and it is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000587 * \param p_rng The RNG context to be passed to \p f_rng. May be
Thomas Daubney03412782021-05-20 15:31:17 +0100588 * \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000589 * \param ilen The length of the plaintext in Bytes.
590 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000591 * buffer of size \p ilen Bytes. It may be \c NULL if
592 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000593 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000594 * of length \c ctx->len Bytes. For example, \c 256 Bytes
595 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100596 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100597 * \return \c 0 on success.
598 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000601 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000602 void *p_rng,
Thomas Daubney21772772021-05-13 17:30:32 +0100603 size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000604 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 unsigned char *output );
606
607/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000608 * \brief This function performs a PKCS#1 v1.5 encryption operation
609 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100610 *
Hanno Becker9a467772018-12-13 09:54:59 +0000611 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100612 * \param f_rng The RNG function to use. It is mandatory and used for
613 * padding generation.
Hanno Becker9a467772018-12-13 09:54:59 +0000614 * \param p_rng The RNG context to be passed to \p f_rng. This may
Thomas Daubney03412782021-05-20 15:31:17 +0100615 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000616 * \param ilen The length of the plaintext in Bytes.
617 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000618 * buffer of size \p ilen Bytes. It may be \c NULL if
619 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000620 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000621 * of length \c ctx->len Bytes. For example, \c 256 Bytes
622 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100623 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100624 * \return \c 0 on success.
625 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100626 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100628 int (*f_rng)(void *, unsigned char *, size_t),
629 void *p_rng,
Thomas Daubney53e4ac62021-05-13 18:26:49 +0100630 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100631 const unsigned char *input,
632 unsigned char *output );
633
634/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000635 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
636 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100637 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100638 * \note The output buffer must be as large as the size
639 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
640 *
Hanno Becker9a467772018-12-13 09:54:59 +0000641 * \param ctx The initnialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000642 * \param f_rng The RNG function to use. This is needed for padding
Thomas Daubney2c65db92021-05-21 10:58:28 +0100643 * generation and is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000644 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000645 * be \c NULL if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000646 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000647 * This must be a readable buffer of length \p label_len
648 * Bytes. It may be \c NULL if \p label_len is \c 0.
649 * \param label_len The length of the label in Bytes.
650 * \param ilen The length of the plaintext buffer \p input in Bytes.
651 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000652 * buffer of size \p ilen Bytes. It may be \c NULL if
653 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000654 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000655 * of length \c ctx->len Bytes. For example, \c 256 Bytes
656 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100657 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100658 * \return \c 0 on success.
659 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100662 int (*f_rng)(void *, unsigned char *, size_t),
663 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100664 const unsigned char *label, size_t label_len,
665 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100666 const unsigned char *input,
667 unsigned char *output );
668
669/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000670 * \brief This function performs an RSA operation, then removes the
671 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000673 * It is the generic wrapper for performing a PKCS#1 decryption
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100674 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000675 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100676 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000677 * as large as the size \p ctx->len of \p ctx->N (for example,
678 * 128 Bytes if RSA-1024 is used) to be able to hold an
679 * arbitrary decrypted message. If it is not large enough to
680 * hold the decryption of the particular ciphertext provided,
681 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100682 *
Hanno Becker9a467772018-12-13 09:54:59 +0000683 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100684 * \param f_rng The RNG function. This is used for blinding and is
685 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000686 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100687 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000688 * \param olen The address at which to store the length of
689 * the plaintext. This must not be \c NULL.
690 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000691 * of length \c ctx->len Bytes. For example, \c 256 Bytes
692 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000693 * \param output The buffer used to hold the plaintext. This must
694 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000695 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100696 *
697 * \return \c 0 on success.
698 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200701 int (*f_rng)(void *, unsigned char *, size_t),
702 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100703 size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000704 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000705 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000706 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000707
708/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000709 * \brief This function performs a PKCS#1 v1.5 decryption
710 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100711 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100712 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000713 * as large as the size \p ctx->len of \p ctx->N, for example,
714 * 128 Bytes if RSA-1024 is used, to be able to hold an
715 * arbitrary decrypted message. If it is not large enough to
716 * hold the decryption of the particular ciphertext provided,
717 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100718 *
Hanno Becker9a467772018-12-13 09:54:59 +0000719 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100720 * \param f_rng The RNG function. This is used for blinding and is
721 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000722 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100723 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000724 * \param olen The address at which to store the length of
725 * the plaintext. This must not be \c NULL.
726 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000727 * of length \c ctx->len Bytes. For example, \c 256 Bytes
728 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000729 * \param output The buffer used to hold the plaintext. This must
730 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000731 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100732 *
733 * \return \c 0 on success.
734 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
735 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200738 int (*f_rng)(void *, unsigned char *, size_t),
739 void *p_rng,
Thomas Daubney34733082021-05-12 09:24:29 +0100740 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100741 const unsigned char *input,
742 unsigned char *output,
743 size_t output_max_len );
744
745/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100746 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
747 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100748 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100749 * \note The output buffer length \c output_max_len should be
750 * as large as the size \p ctx->len of \p ctx->N, for
751 * example, 128 Bytes if RSA-1024 is used, to be able to
752 * hold an arbitrary decrypted message. If it is not
753 * large enough to hold the decryption of the particular
754 * ciphertext provided, the function returns
755 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100756 *
Hanno Becker9a467772018-12-13 09:54:59 +0000757 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100758 * \param f_rng The RNG function. This is used for blinding and is
759 * mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000760 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100761 * \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100762 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000763 * This must be a readable buffer of length \p label_len
764 * Bytes. It may be \c NULL if \p label_len is \c 0.
765 * \param label_len The length of the label in Bytes.
766 * \param olen The address at which to store the length of
767 * the plaintext. This must not be \c NULL.
768 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000769 * of length \c ctx->len Bytes. For example, \c 256 Bytes
770 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000771 * \param output The buffer used to hold the plaintext. This must
772 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000773 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100774 *
775 * \return \c 0 on success.
776 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100777 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200779 int (*f_rng)(void *, unsigned char *, size_t),
780 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100781 const unsigned char *label, size_t label_len,
782 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100783 const unsigned char *input,
784 unsigned char *output,
785 size_t output_max_len );
786
787/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000788 * \brief This function performs a private RSA operation to sign
789 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000790 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000791 * It is the generic wrapper for performing a PKCS#1
Thomas Daubney140184d2021-05-18 16:04:07 +0100792 * signature.
Paul Bakker5121ce52009-01-03 21:22:43 +0000793 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000794 * \note The \p sig buffer must be as large as the size
795 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000796 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000797 * \note For PKCS#1 v2.1 encoding, see comments on
798 * mbedtls_rsa_rsassa_pss_sign() for details on
799 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100800 *
Hanno Becker9a467772018-12-13 09:54:59 +0000801 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100802 * \param f_rng The RNG function to use. This is mandatory and
803 * must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000804 * \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 +0100805 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100806 * \param md_alg The message-digest algorithm used to hash the original data.
807 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200808 * \param hashlen The length of the message digest or raw data in Bytes.
809 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
810 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000811 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200812 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000813 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000814 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100815 * for an 2048-bit RSA modulus. A buffer length of
816 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100817 *
818 * \return \c 0 if the signing operation was successful.
819 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000822 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000823 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000825 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000826 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000827 unsigned char *sig );
828
829/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000830 * \brief This function performs a PKCS#1 v1.5 signature
831 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100832 *
Hanno Becker9a467772018-12-13 09:54:59 +0000833 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100834 * \param f_rng The RNG function. This is used for blinding and is
835 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000836 * \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 +0100837 * if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000838 * \param md_alg The message-digest algorithm used to hash the original data.
839 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200840 * \param hashlen The length of the message digest or raw data in Bytes.
841 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
842 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000843 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200844 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000845 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000846 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100847 * for an 2048-bit RSA modulus. A buffer length of
848 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100849 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100850 * \return \c 0 if the signing operation was successful.
851 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100852 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200854 int (*f_rng)(void *, unsigned char *, size_t),
855 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100857 unsigned int hashlen,
858 const unsigned char *hash,
859 unsigned char *sig );
860
861/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000862 * \brief This function performs a PKCS#1 v2.1 PSS signature
863 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100864 *
Janos Follathb7953322021-04-01 14:44:17 +0100865 * \note The \c hash_id set in \p ctx by calling
866 * mbedtls_rsa_set_padding() selects the hash used for the
867 * encoding operation and for the mask generation function
868 * (MGF1). For more details on the encoding operation and the
869 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +0000870 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100871 * Specifications</em>.
Rose Zadike8b5b992018-03-27 12:19:47 +0100872 *
Cédric Meuter010ddc22020-04-25 09:24:11 +0200873 * \note This function enforces that the provided salt length complies
874 * with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 v2.2) §9.1.1
875 * step 3. The constraint is that the hash length plus the salt
876 * length plus 2 bytes must be at most the key length. If this
877 * constraint is not met, this function returns
Jaeden Amero3725bb22018-09-07 19:12:36 +0100878 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
879 *
Hanno Becker9a467772018-12-13 09:54:59 +0000880 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100881 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000882 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
883 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100884 * \param md_alg The message-digest algorithm used to hash the original data.
885 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200886 * \param hashlen The length of the message digest or raw data in Bytes.
887 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
888 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000889 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200890 * This must be a readable buffer of at least \p hashlen Bytes.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200891 * \param saltlen The length of the salt that should be used.
Cédric Meuter010ddc22020-04-25 09:24:11 +0200892 * If passed #MBEDTLS_RSA_SALT_LEN_ANY, the function will use
893 * the largest possible salt length up to the hash length,
894 * which is the largest permitted by some standards including
895 * FIPS 186-4 §5.5.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200896 * \param sig The buffer to hold the signature. This must be a writable
897 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
898 * for an 2048-bit RSA modulus. A buffer length of
899 * #MBEDTLS_MPI_MAX_SIZE is always safe.
900 *
901 * \return \c 0 if the signing operation was successful.
902 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
903 */
904int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
905 int (*f_rng)(void *, unsigned char *, size_t),
906 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200907 mbedtls_md_type_t md_alg,
908 unsigned int hashlen,
909 const unsigned char *hash,
910 int saltlen,
911 unsigned char *sig );
912
913/**
914 * \brief This function performs a PKCS#1 v2.1 PSS signature
915 * operation (RSASSA-PSS-SIGN).
916 *
Janos Follathb7953322021-04-01 14:44:17 +0100917 * \note The \c hash_id set in \p ctx by calling
918 * mbedtls_rsa_set_padding() selects the hash used for the
919 * encoding operation and for the mask generation function
920 * (MGF1). For more details on the encoding operation and the
921 * mask generation function, consult <em>RFC-3447: Public-Key
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200922 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100923 * Specifications</em>.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200924 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000925 * \note This function always uses the maximum possible salt size,
926 * up to the length of the payload hash. This choice of salt
927 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
928 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
Rose Zadike8b5b992018-03-27 12:19:47 +0100929 * minimum salt size which is the hash size minus 2 bytes. If
930 * this minimum size is too large given the key size (the salt
931 * size, plus the hash size, plus 2 bytes must be no more than
932 * the key size in bytes), this function returns
933 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
934 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100935 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100936 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100937 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
938 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100939 * \param md_alg The message-digest algorithm used to hash the original data.
940 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200941 * \param hashlen The length of the message digest or raw data in Bytes.
942 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
943 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000944 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200945 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000946 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000947 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100948 * for an 2048-bit RSA modulus. A buffer length of
949 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100950 *
951 * \return \c 0 if the signing operation was successful.
952 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100953 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100955 int (*f_rng)(void *, unsigned char *, size_t),
956 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100958 unsigned int hashlen,
959 const unsigned char *hash,
960 unsigned char *sig );
961
962/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000963 * \brief This function performs a public RSA operation and checks
964 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +0000965 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000966 * This is the generic wrapper for performing a PKCS#1
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100967 * verification.
Paul Bakker5121ce52009-01-03 21:22:43 +0000968 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000969 * \note For PKCS#1 v2.1 encoding, see comments on
970 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
971 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100972 *
Hanno Becker9a467772018-12-13 09:54:59 +0000973 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +0100974 * \param md_alg The message-digest algorithm used to hash the original data.
975 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200976 * \param hashlen The length of the message digest or raw data in Bytes.
977 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
978 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000979 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200980 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000981 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +0000982 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
983 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100984 *
985 * \return \c 0 if the verify operation was successful.
986 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000987 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000990 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000991 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200992 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000993
994/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000995 * \brief This function performs a PKCS#1 v1.5 verification
996 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +0100997 *
Hanno Becker9a467772018-12-13 09:54:59 +0000998 * \param ctx The initialized RSA public key context to use.
Rose Zadik042e97f2018-01-26 16:35:10 +0000999 * \param md_alg The message-digest algorithm used to hash the original data.
1000 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001001 * \param hashlen The length of the message digest or raw data in Bytes.
1002 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1003 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001004 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001005 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001006 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001007 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1008 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001009 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001010 * \return \c 0 if the verify operation was successful.
1011 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001015 unsigned int hashlen,
1016 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001017 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001018
1019/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001020 * \brief This function performs a PKCS#1 v2.1 PSS verification
1021 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001022 *
Janos Follathb7953322021-04-01 14:44:17 +01001023 * \note The \c hash_id set in \p ctx by calling
1024 * mbedtls_rsa_set_padding() selects the hash used for the
1025 * encoding operation and for the mask generation function
1026 * (MGF1). For more details on the encoding operation and the
1027 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +00001028 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +01001029 * Specifications</em>. If the \c hash_id set in \p ctx by
1030 * mbedtls_rsa_set_padding() is #MBEDTLS_MD_NONE, the \p md_alg
1031 * parameter is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001032 *
Hanno Becker9a467772018-12-13 09:54:59 +00001033 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001034 * \param md_alg The message-digest algorithm used to hash the original data.
1035 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001036 * \param hashlen The length of the message digest or raw data in Bytes.
1037 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1038 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001039 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001040 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001041 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001042 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1043 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001044 *
1045 * \return \c 0 if the verify operation was successful.
1046 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001047 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001050 unsigned int hashlen,
1051 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001052 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001053
1054/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001055 * \brief This function performs a PKCS#1 v2.1 PSS verification
1056 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001057 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001058 * \note The \p sig buffer must be as large as the size
1059 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001060 *
Janos Follathb7953322021-04-01 14:44:17 +01001061 * \note The \c hash_id set in \p ctx by mbedtls_rsa_set_padding() is
1062 * ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001063 *
Hanno Becker9a467772018-12-13 09:54:59 +00001064 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001065 * \param md_alg The message-digest algorithm used to hash the original data.
1066 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001067 * \param hashlen The length of the message digest or raw data in Bytes.
1068 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1069 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001070 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001071 * This must be a readable buffer of at least \p hashlen Bytes.
Janos Follathb7953322021-04-01 14:44:17 +01001072 * \param mgf1_hash_id The message digest algorithm used for the
1073 * verification operation and the mask generation
1074 * function (MGF1). For more details on the encoding
1075 * operation and the mask generation function, consult
1076 * <em>RFC-3447: Public-Key Cryptography Standards
1077 * (PKCS) #1 v2.1: RSA Cryptography
1078 * Specifications</em>.
Hanno Becker9a467772018-12-13 09:54:59 +00001079 * \param expected_salt_len The length of the salt used in padding. Use
1080 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1081 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001082 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1083 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001084 *
1085 * \return \c 0 if the verify operation was successful.
1086 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001090 unsigned int hashlen,
1091 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001093 int expected_salt_len,
1094 const unsigned char *sig );
1095
1096/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001097 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001098 *
Hanno Becker9a467772018-12-13 09:54:59 +00001099 * \param dst The destination context. This must be initialized.
1100 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001101 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001102 * \return \c 0 on success.
1103 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001106
1107/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001108 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001109 *
Hanno Becker9a467772018-12-13 09:54:59 +00001110 * \param ctx The RSA context to free. May be \c NULL, in which case
1111 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001112 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001115
Ron Eldorfa8f6352017-06-20 15:48:46 +03001116#if defined(MBEDTLS_SELF_TEST)
1117
Paul Bakker5121ce52009-01-03 21:22:43 +00001118/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001119 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001120 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001121 * \return \c 0 on success.
1122 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001125
Ron Eldorfa8f6352017-06-20 15:48:46 +03001126#endif /* MBEDTLS_SELF_TEST */
1127
Paul Bakker5121ce52009-01-03 21:22:43 +00001128#ifdef __cplusplus
1129}
1130#endif
1131
Paul Bakker5121ce52009-01-03 21:22:43 +00001132#endif /* rsa.h */