blob: 062df73aa067946a45d78052d5103f31e72b6e0c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Rose Zadik21e29262018-04-17 14:08:56 +01004 * \brief This file provides an API for the RSA public-key cryptosystem.
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
Rose Zadike8b5b992018-03-27 12:19:47 +01006 * The RSA public-key cryptosystem is defined in <em>Public-Key
7 * Cryptography Standards (PKCS) #1 v1.5: RSA Encryption</em>
Darryl Green11999bb2018-03-13 15:22:58 +00008 * and <em>Public-Key Cryptography Standards (PKCS) #1 v2.1:
Rose Zadike8b5b992018-03-27 12:19:47 +01009 * RSA Cryptography Specifications</em>.
Rose Zadik042e97f2018-01-26 16:35:10 +000010 *
Darryl Greena40a1012018-01-05 15:33:17 +000011 */
12/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020013 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020014 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#ifndef MBEDTLS_RSA_H
29#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010032#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#endif
Paul Bakkered27a042013-04-18 22:46:23 +020036
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010037#include "mbedtls/bignum.h"
38#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_THREADING_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010041#include "mbedtls/threading.h"
Paul Bakkerc9965dc2013-09-29 14:58:17 +020042#endif
43
Paul Bakker13e2dfe2009-07-28 07:18:38 +000044/*
45 * RSA Error codes
46 */
Gilles Peskinea3974432021-07-26 18:48:10 +020047/** Bad input parameters to function. */
48#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080
49/** Input data contains invalid padding and is rejected. */
50#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100
51/** Something failed during generation of a key. */
52#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180
53/** Key failed to pass the validity check of the library. */
54#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200
55/** The public key operation failed. */
56#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280
57/** The private key operation failed. */
58#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300
59/** The PKCS#1 verification failed. */
60#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380
61/** The output buffer for decryption is not large enough. */
62#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400
63/** The random generator failed to generate non-zeros. */
64#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480
Ron Eldor9924bdc2018-10-04 10:59:13 +030065
66/* MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is deprecated and should not be used.
67 */
Gilles Peskinea3974432021-07-26 18:48:10 +020068/** The implementation does not offer the requested operation, for example, because of security violations or lack of functionality. */
69#define MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION -0x4500
Ron Eldor9924bdc2018-10-04 10:59:13 +030070
71/* MBEDTLS_ERR_RSA_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea3974432021-07-26 18:48:10 +020072/** RSA hardware accelerator failed. */
73#define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580
Paul Bakker5121ce52009-01-03 21:22:43 +000074
75/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020076 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000077 */
Rose Zadik042e97f2018-01-26 16:35:10 +000078#define MBEDTLS_RSA_PUBLIC 0 /**< Request private key operation. */
79#define MBEDTLS_RSA_PRIVATE 1 /**< Request public key operation. */
Paul Bakker5121ce52009-01-03 21:22:43 +000080
Rose Zadike8b5b992018-03-27 12:19:47 +010081#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
82#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000083
Rose Zadik042e97f2018-01-26 16:35:10 +000084#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
85#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +000086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020088
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020089/*
90 * The above constants may be used even if the RSA module is compile out,
Shaun Case0e7791f2021-12-20 21:14:10 -080091 * eg for alternative (PKCS#11) RSA implementations in the PK layers.
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020092 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010093
Paul Bakker407a0da2013-06-27 14:29:21 +020094#ifdef __cplusplus
95extern "C" {
96#endif
97
Ron Eldor4e6d55d2018-02-07 16:36:15 +020098#if !defined(MBEDTLS_RSA_ALT)
99// Regular implementation
100//
101
Paul Bakker5121ce52009-01-03 21:22:43 +0000102/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000103 * \brief The RSA context structure.
Hanno Becker5063cd22017-09-29 11:49:12 +0100104 *
105 * \note Direct manipulation of the members of this structure
Rose Zadik042e97f2018-01-26 16:35:10 +0000106 * is deprecated. All manipulation should instead be done through
107 * the public interface functions.
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200109typedef struct mbedtls_rsa_context
Paul Bakker5121ce52009-01-03 21:22:43 +0000110{
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100111 int ver; /*!< Reserved for internal purposes.
112 * Do not set this field in application
113 * code. Its meaning might change without
114 * notice. */
Rose Zadik042e97f2018-01-26 16:35:10 +0000115 size_t len; /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
Rose Zadike8b5b992018-03-27 12:19:47 +0100117 mbedtls_mpi N; /*!< The public modulus. */
118 mbedtls_mpi E; /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
Rose Zadike8b5b992018-03-27 12:19:47 +0100120 mbedtls_mpi D; /*!< The private exponent. */
121 mbedtls_mpi P; /*!< The first prime factor. */
122 mbedtls_mpi Q; /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100123
Rose Zadikf2ec2882018-04-17 10:27:25 +0100124 mbedtls_mpi DP; /*!< <code>D % (P - 1)</code>. */
125 mbedtls_mpi DQ; /*!< <code>D % (Q - 1)</code>. */
126 mbedtls_mpi QP; /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
Rose Zadikf2ec2882018-04-17 10:27:25 +0100128 mbedtls_mpi RN; /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Rose Zadikf2ec2882018-04-17 10:27:25 +0100130 mbedtls_mpi RP; /*!< cached <code>R^2 mod P</code>. */
131 mbedtls_mpi RQ; /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200132
Rose Zadike8b5b992018-03-27 12:19:47 +0100133 mbedtls_mpi Vi; /*!< The cached blinding value. */
134 mbedtls_mpi Vf; /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000135
Rose Zadik042e97f2018-01-26 16:35:10 +0000136 int padding; /*!< Selects padding mode:
137 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
138 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
139 int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
140 as specified in md.h for use in the MGF
141 mask generating function used in the
142 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100144 /* Invariant: the mutex is initialized iff ver != 0. */
Rose Zadik042e97f2018-01-26 16:35:10 +0000145 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200146#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000147}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200150#else /* MBEDTLS_RSA_ALT */
151#include "rsa_alt.h"
152#endif /* MBEDTLS_RSA_ALT */
153
Paul Bakker5121ce52009-01-03 21:22:43 +0000154/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000155 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000157 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000158 * encryption scheme and the RSASSA-PSS signature scheme.
159 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000160 * \note The \p hash_id parameter is ignored when using
161 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200162 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000163 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200164 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000165 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100166 * a default value, which can be overridden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000167 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx 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 *
Hanno Becker9a467772018-12-13 09:54:59 +0000174 * \param ctx The RSA context to initialize. This must not be \c NULL.
175 * \param padding The padding mode to use. This must be either
176 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Hanno Becker385ce912018-12-13 18:33:12 +0000177 * \param hash_id The hash identifier of ::mbedtls_md_type_t type, if
Hanno Becker9a467772018-12-13 09:54:59 +0000178 * \p padding is #MBEDTLS_RSA_PKCS_V21. It is unused
179 * otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100182 int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000183 int hash_id );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
185/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000186 * \brief This function imports a set of core parameters into an
187 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100188 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100189 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000190 * imports, if the parameters are not simultaneously present.
191 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100192 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000193 * by a call to mbedtls_rsa_complete(), which checks and
194 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100195 * public or private RSA key.
196 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000197 * \note See mbedtls_rsa_complete() for more information on which
198 * parameters are necessary to set up a private or public
199 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100200 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100201 * \note The imported parameters are copied and need not be preserved
202 * for the lifetime of the RSA context being set up.
203 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100204 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000205 * \param N The RSA modulus. This may be \c NULL.
206 * \param P The first prime factor of \p N. This may be \c NULL.
207 * \param Q The second prime factor of \p N. This may be \c NULL.
208 * \param D The private exponent. This may be \c NULL.
209 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100210 *
211 * \return \c 0 on success.
212 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100213 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100214int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
215 const mbedtls_mpi *N,
216 const mbedtls_mpi *P, const mbedtls_mpi *Q,
217 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100218
219/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000220 * \brief This function imports core RSA parameters, in raw big-endian
221 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100223 * \note This function can be called multiple times for successive
224 * imports, if the parameters are not simultaneously present.
225 *
226 * Any sequence of calls to this function should be followed
227 * by a call to mbedtls_rsa_complete(), which checks and
228 * completes the provided information to a ready-for-use
229 * public or private RSA key.
230 *
231 * \note See mbedtls_rsa_complete() for more information on which
232 * parameters are necessary to set up a private or public
233 * RSA key.
234 *
235 * \note The imported parameters are copied and need not be preserved
236 * for the lifetime of the RSA context being set up.
237 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000238 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000239 * \param N The RSA modulus. This may be \c NULL.
240 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
241 * \param P The first prime factor of \p N. This may be \c NULL.
242 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
243 * \param Q The second prime factor of \p N. This may be \c NULL.
244 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
245 * \param D The private exponent. This may be \c NULL.
246 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
247 * \param E The public exponent. This may be \c NULL.
248 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100250 * \return \c 0 on success.
251 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100252 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100253int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100254 unsigned char const *N, size_t N_len,
255 unsigned char const *P, size_t P_len,
256 unsigned char const *Q, size_t Q_len,
257 unsigned char const *D, size_t D_len,
258 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100259
260/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000261 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100262 * a set of imported core parameters.
263 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000264 * To setup an RSA public key, precisely \p N and \p E
265 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100266 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000267 * To setup an RSA private key, sufficient information must
268 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100269 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000270 * The default implementation supports the following:
271 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
272 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
273 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100274 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000275 * If this function runs successfully, it guarantees that
276 * the RSA context can be used for RSA operations without
277 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100278 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100279 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000280 * for the imported parameters. In particular, parameters that
281 * are not needed by the implementation might be silently
282 * discarded and left unchecked. To check the consistency
283 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100284 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100285 * \param ctx The initialized RSA context holding imported parameters.
286 *
287 * \return \c 0 on success.
288 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
289 * failed.
290 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100291 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100292int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100293
294/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000295 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100296 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000297 * If this function runs successfully, the non-NULL buffers
298 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
299 * written, with additional unused space filled leading by
300 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100301 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000302 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300303 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000304 * <li>An alternative RSA implementation is in use, which
305 * stores the key externally, and either cannot or should
306 * not export it into RAM.</li>
307 * <li>A SW or HW implementation might not support a certain
308 * deduction. For example, \p P, \p Q from \p N, \p D,
309 * and \p E if the former are not part of the
310 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100311 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000312 * If the function fails due to an unsupported operation,
313 * the RSA context stays intact and remains usable.
314 *
315 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000316 * \param N The MPI to hold the RSA modulus.
317 * This may be \c NULL if this field need not be exported.
318 * \param P The MPI to hold the first prime factor of \p N.
319 * This may be \c NULL if this field need not be exported.
320 * \param Q The MPI to hold the second prime factor of \p N.
321 * This may be \c NULL if this field need not be exported.
322 * \param D The MPI to hold the private exponent.
323 * This may be \c NULL if this field need not be exported.
324 * \param E The MPI to hold the public exponent.
325 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000326 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100327 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300328 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000329 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100330 * functionality or because of security policies.
331 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100332 *
333 */
334int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
335 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
336 mbedtls_mpi *D, mbedtls_mpi *E );
337
338/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000339 * \brief This function exports core parameters of an RSA key
340 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100341 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000342 * If this function runs successfully, the non-NULL buffers
343 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
344 * written, with additional unused space filled leading by
345 * zero Bytes.
346 *
347 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300348 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000349 * <li>An alternative RSA implementation is in use, which
350 * stores the key externally, and either cannot or should
351 * not export it into RAM.</li>
352 * <li>A SW or HW implementation might not support a certain
353 * deduction. For example, \p P, \p Q from \p N, \p D,
354 * and \p E if the former are not part of the
355 * implementation.</li></ul>
356 * If the function fails due to an unsupported operation,
357 * the RSA context stays intact and remains usable.
358 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100359 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100360 * buffer pointers are NULL.
361 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000362 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000363 * \param N The Byte array to store the RSA modulus,
364 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000365 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000366 * \param P The Byte array to hold the first prime factor of \p N,
367 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000368 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000369 * \param Q The Byte array to hold the second prime factor of \p N,
370 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000371 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000372 * \param D The Byte array to hold the private exponent,
373 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000374 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000375 * \param E The Byte array to hold the public exponent,
376 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000377 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100378 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100379 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300380 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000381 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100382 * functionality or because of security policies.
383 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100384 */
385int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
386 unsigned char *N, size_t N_len,
387 unsigned char *P, size_t P_len,
388 unsigned char *Q, size_t Q_len,
389 unsigned char *D, size_t D_len,
390 unsigned char *E, size_t E_len );
391
392/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000393 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100394 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100395 * \note Alternative RSA implementations not using CRT-parameters
396 * internally can implement this function based on
397 * mbedtls_rsa_deduce_opt().
398 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000399 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000400 * \param DP The MPI to hold \c D modulo `P-1`,
401 * or \c NULL if it need not be exported.
402 * \param DQ The MPI to hold \c D modulo `Q-1`,
403 * or \c NULL if it need not be exported.
404 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
405 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100406 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100407 * \return \c 0 on success.
408 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100409 *
410 */
411int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
412 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
413
Paul Bakker5121ce52009-01-03 21:22:43 +0000414/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000415 * \brief This function sets padding for an already initialized RSA
416 * context. See mbedtls_rsa_init() for details.
Paul Bakker5121ce52009-01-03 21:22:43 +0000417 *
Hanno Becker9a467772018-12-13 09:54:59 +0000418 * \param ctx The initialized RSA context to be configured.
419 * \param padding The padding mode to use. This must be either
420 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Rose Zadik042e97f2018-01-26 16:35:10 +0000421 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Paul Bakker40e46942009-01-03 21:51:57 +0000422 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100423void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000424 int hash_id );
Paul Bakker21eb2802010-08-16 11:10:02 +0000425
Paul Bakkera3d195c2011-11-27 21:07:34 +0000426/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000427 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100428 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000429 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100430 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000431 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100432 *
433 */
434size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
435
436/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000437 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000439 * \note mbedtls_rsa_init() must be called before this function,
440 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000441 *
Hanno Becker9a467772018-12-13 09:54:59 +0000442 * \param ctx The initialized RSA context used to hold the key.
443 * \param f_rng The RNG function to be used for key generation.
444 * This must not be \c NULL.
445 * \param p_rng The RNG context to be passed to \p f_rng.
446 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100447 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000448 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000449 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100450 *
451 * \return \c 0 on success.
452 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100455 int (*f_rng)(void *, unsigned char *, size_t),
456 void *p_rng,
457 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
459/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000460 * \brief This function checks if a context contains at least an RSA
461 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000462 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000463 * If the function runs successfully, it is guaranteed that
464 * enough information is present to perform an RSA public key
465 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 *
Hanno Becker9a467772018-12-13 09:54:59 +0000467 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000468 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100469 * \return \c 0 on success.
470 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100471 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000474
475/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000476 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100477 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 *
Hanno Becker68767a62017-10-17 10:13:31 +0100479 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000480 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100481 * on the given context, but that the various parameters are
482 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000483 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100484 *
485 * \warning This function should catch accidental misconfigurations
486 * like swapping of parameters, but it cannot establish full
487 * trust in neither the quality nor the consistency of the key
488 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000489 * <ul><li>Consistency: Imported parameters that are irrelevant
490 * for the implementation might be silently dropped. If dropped,
491 * the current function does not have access to them,
492 * and therefore cannot check them. See mbedtls_rsa_complete().
493 * If you want to check the consistency of the entire
494 * content of an PKCS1-encoded RSA private key, for example, you
495 * should use mbedtls_rsa_validate_params() before setting
496 * up the RSA context.
497 * Additionally, if the implementation performs empirical checks,
498 * these checks substantiate but do not guarantee consistency.</li>
499 * <li>Quality: This function is not expected to perform
500 * extended quality assessments like checking that the prime
501 * factors are safe. Additionally, it is the responsibility of the
502 * user to ensure the trustworthiness of the source of his RSA
503 * parameters, which goes beyond what is effectively checkable
504 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100505 *
Hanno Becker9a467772018-12-13 09:54:59 +0000506 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100507 *
508 * \return \c 0 on success.
509 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
513/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000514 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100515 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000516 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100517 *
Hanno Becker9a467772018-12-13 09:54:59 +0000518 * \param pub The initialized RSA context holding the public key.
519 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000520 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100521 * \return \c 0 on success.
522 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100523 */
Hanno Becker98838b02017-10-02 13:16:10 +0100524int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
525 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100526
527/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000528 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 *
Hanno Becker9a467772018-12-13 09:54:59 +0000530 * \param ctx The initialized RSA context to use.
531 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000532 * of length \c ctx->len Bytes. For example, \c 256 Bytes
533 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000534 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000535 * of length \c ctx->len Bytes. For example, \c 256 Bytes
536 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000537 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000538 * \note This function does not handle message padding.
539 *
540 * \note Make sure to set \p input[0] = 0 or ensure that
541 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000542 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100543 * \return \c 0 on success.
544 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000547 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000548 unsigned char *output );
549
550/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000551 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 *
Hanno Becker24120612017-10-26 11:53:35 +0100553 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100554 *
Shaun Case0e7791f2021-12-20 21:14:10 -0800555 * \note If blinding is used, both the base of exponentiation
Hanno Becker24120612017-10-26 11:53:35 +0100556 * and the exponent are blinded, providing protection
557 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100558 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100559 * \warning It is deprecated and a security risk to not provide
560 * a PRNG here and thereby prevent the use of blinding.
561 * Future versions of the library may enforce the presence
562 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100563 *
Hanno Becker9a467772018-12-13 09:54:59 +0000564 * \param ctx The initialized RSA context to use.
565 * \param f_rng The RNG function, used for blinding. It is discouraged
566 * and deprecated to pass \c NULL here, in which case
567 * blinding will be omitted.
568 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
569 * if \p f_rng is \c NULL or if \p f_rng doesn't need a context.
570 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000571 * of length \c ctx->len Bytes. For example, \c 256 Bytes
572 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000573 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000574 * of length \c ctx->len Bytes. For example, \c 256 Bytes
575 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100576 *
577 * \return \c 0 on success.
578 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
579 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200582 int (*f_rng)(void *, unsigned char *, size_t),
583 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000584 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 unsigned char *output );
586
587/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000588 * \brief This function adds the message padding, then performs an RSA
589 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000591 * It is the generic wrapper for performing a PKCS#1 encryption
592 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100594 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000595 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
596 * are likely to remove the \p mode argument and have it
597 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100598 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100599 * \note Alternative implementations of RSA need not support
600 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300601 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100602 *
Hanno Becker9a467772018-12-13 09:54:59 +0000603 * \param ctx The initialized RSA context to use.
Hanno Becker974ca0d2018-12-18 18:03:24 +0000604 * \param f_rng The RNG to use. It is mandatory for PKCS#1 v2.1 padding
605 * encoding, and for PKCS#1 v1.5 padding encoding when used
606 * with \p mode set to #MBEDTLS_RSA_PUBLIC. For PKCS#1 v1.5
607 * padding encoding and \p mode set to #MBEDTLS_RSA_PRIVATE,
608 * it is used for blinding and should be provided in this
609 * case; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000610 * \param p_rng The RNG context to be passed to \p f_rng. May be
611 * \c NULL if \p f_rng is \c NULL or if \p f_rng doesn't
612 * need a context argument.
613 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000614 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000615 * \param ilen The length of the plaintext in Bytes.
616 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000617 * buffer of size \p ilen Bytes. It may be \c NULL if
618 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000619 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000620 * of length \c ctx->len Bytes. For example, \c 256 Bytes
621 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100622 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100623 * \return \c 0 on success.
624 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000627 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000628 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000629 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000630 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000631 unsigned char *output );
632
633/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000634 * \brief This function performs a PKCS#1 v1.5 encryption operation
635 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100636 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100637 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000638 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
639 * are likely to remove the \p mode argument and have it
640 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100641 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100642 * \note Alternative implementations of RSA need not support
643 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300644 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100645 *
Hanno Becker9a467772018-12-13 09:54:59 +0000646 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000647 * \param f_rng The RNG function to use. It is needed for padding generation
648 * if \p mode is #MBEDTLS_RSA_PUBLIC. If \p mode is
649 * #MBEDTLS_RSA_PRIVATE (discouraged), it is used for
650 * blinding and should be provided; see mbedtls_rsa_private().
Hanno Becker9a467772018-12-13 09:54:59 +0000651 * \param p_rng The RNG context to be passed to \p f_rng. This may
652 * be \c NULL if \p f_rng is \c NULL or if \p f_rng
653 * doesn't need a context argument.
654 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000655 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000656 * \param ilen The length of the plaintext in Bytes.
657 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000658 * buffer of size \p ilen Bytes. It may be \c NULL if
659 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000660 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000661 * of length \c ctx->len Bytes. For example, \c 256 Bytes
662 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100663 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100664 * \return \c 0 on success.
665 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100666 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100668 int (*f_rng)(void *, unsigned char *, size_t),
669 void *p_rng,
670 int mode, size_t ilen,
671 const unsigned char *input,
672 unsigned char *output );
673
674/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000675 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
676 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100677 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100678 * \note The output buffer must be as large as the size
679 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
680 *
681 * \deprecated It is deprecated and discouraged to call this function
682 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
683 * are likely to remove the \p mode argument and have it
684 * implicitly set to #MBEDTLS_RSA_PUBLIC.
685 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100686 * \note Alternative implementations of RSA need not support
687 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300688 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100689 *
Tom Cosgrove2b150752022-05-26 11:55:43 +0100690 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000691 * \param f_rng The RNG function to use. This is needed for padding
692 * generation and must be provided.
Hanno Becker9a467772018-12-13 09:54:59 +0000693 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000694 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000695 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000696 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000697 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000698 * This must be a readable buffer of length \p label_len
699 * Bytes. It may be \c NULL if \p label_len is \c 0.
700 * \param label_len The length of the label in Bytes.
701 * \param ilen The length of the plaintext buffer \p input in Bytes.
702 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000703 * buffer of size \p ilen Bytes. It may be \c NULL if
704 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000705 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000706 * of length \c ctx->len Bytes. For example, \c 256 Bytes
707 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100708 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100709 * \return \c 0 on success.
710 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100713 int (*f_rng)(void *, unsigned char *, size_t),
714 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100715 int mode,
716 const unsigned char *label, size_t label_len,
717 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100718 const unsigned char *input,
719 unsigned char *output );
720
721/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000722 * \brief This function performs an RSA operation, then removes the
723 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000724 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000725 * It is the generic wrapper for performing a PKCS#1 decryption
726 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000727 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100728 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000729 * as large as the size \p ctx->len of \p ctx->N (for example,
730 * 128 Bytes if RSA-1024 is used) to be able to hold an
731 * arbitrary decrypted message. If it is not large enough to
732 * hold the decryption of the particular ciphertext provided,
733 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100734 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100735 * \deprecated It is deprecated and discouraged to call this function
736 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
737 * are likely to remove the \p mode argument and have it
738 * implicitly set to #MBEDTLS_RSA_PRIVATE.
739 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100740 * \note Alternative implementations of RSA need not support
741 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300742 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100743 *
Hanno Becker9a467772018-12-13 09:54:59 +0000744 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000745 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
746 * this is used for blinding and should be provided; see
747 * mbedtls_rsa_private() for more. If \p mode is
748 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000749 * \param p_rng The RNG context to be passed to \p f_rng. This may be
750 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
751 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000752 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000753 * \param olen The address at which to store the length of
754 * the plaintext. This must not be \c NULL.
755 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000756 * of length \c ctx->len Bytes. For example, \c 256 Bytes
757 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000758 * \param output The buffer used to hold the plaintext. This must
759 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000760 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100761 *
762 * \return \c 0 on success.
763 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000764 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200766 int (*f_rng)(void *, unsigned char *, size_t),
767 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000768 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000769 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000770 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000771 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000772
773/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000774 * \brief This function performs a PKCS#1 v1.5 decryption
775 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100776 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100777 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000778 * as large as the size \p ctx->len of \p ctx->N, for example,
779 * 128 Bytes if RSA-1024 is used, to be able to hold an
780 * arbitrary decrypted message. If it is not large enough to
781 * hold the decryption of the particular ciphertext provided,
782 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100783 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100784 * \deprecated It is deprecated and discouraged to call this function
785 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
786 * are likely to remove the \p mode argument and have it
787 * implicitly set to #MBEDTLS_RSA_PRIVATE.
788 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100789 * \note Alternative implementations of RSA need not support
790 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300791 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100792 *
Hanno Becker9a467772018-12-13 09:54:59 +0000793 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000794 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
795 * this is used for blinding and should be provided; see
796 * mbedtls_rsa_private() for more. If \p mode is
797 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000798 * \param p_rng The RNG context to be passed to \p f_rng. This may be
799 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
800 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000801 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000802 * \param olen The address at which to store the length of
803 * the plaintext. This must not be \c NULL.
804 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000805 * of length \c ctx->len Bytes. For example, \c 256 Bytes
806 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000807 * \param output The buffer used to hold the plaintext. This must
808 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000809 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100810 *
811 * \return \c 0 on success.
812 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
813 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100814 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200816 int (*f_rng)(void *, unsigned char *, size_t),
817 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100818 int mode, size_t *olen,
819 const unsigned char *input,
820 unsigned char *output,
821 size_t output_max_len );
822
823/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100824 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
825 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100826 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100827 * \note The output buffer length \c output_max_len should be
828 * as large as the size \p ctx->len of \p ctx->N, for
829 * example, 128 Bytes if RSA-1024 is used, to be able to
830 * hold an arbitrary decrypted message. If it is not
831 * large enough to hold the decryption of the particular
832 * ciphertext provided, the function returns
833 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100834 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100835 * \deprecated It is deprecated and discouraged to call this function
836 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
837 * are likely to remove the \p mode argument and have it
838 * implicitly set to #MBEDTLS_RSA_PRIVATE.
839 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100840 * \note Alternative implementations of RSA need not support
841 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300842 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100843 *
Hanno Becker9a467772018-12-13 09:54:59 +0000844 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000845 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
846 * this is used for blinding and should be provided; see
847 * mbedtls_rsa_private() for more. If \p mode is
848 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000849 * \param p_rng The RNG context to be passed to \p f_rng. This may be
850 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
851 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000852 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100853 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000854 * This must be a readable buffer of length \p label_len
855 * Bytes. It may be \c NULL if \p label_len is \c 0.
856 * \param label_len The length of the label in Bytes.
857 * \param olen The address at which to store the length of
858 * the plaintext. This must not be \c NULL.
859 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000860 * of length \c ctx->len Bytes. For example, \c 256 Bytes
861 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000862 * \param output The buffer used to hold the plaintext. This must
863 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000864 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100865 *
866 * \return \c 0 on success.
867 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100868 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200870 int (*f_rng)(void *, unsigned char *, size_t),
871 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100872 int mode,
873 const unsigned char *label, size_t label_len,
874 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100875 const unsigned char *input,
876 unsigned char *output,
877 size_t output_max_len );
878
879/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000880 * \brief This function performs a private RSA operation to sign
881 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000882 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000883 * It is the generic wrapper for performing a PKCS#1
884 * signature using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000885 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000886 * \note The \p sig buffer must be as large as the size
887 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000888 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000889 * \note For PKCS#1 v2.1 encoding, see comments on
890 * mbedtls_rsa_rsassa_pss_sign() for details on
891 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100892 *
893 * \deprecated It is deprecated and discouraged to call this function
894 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
895 * are likely to remove the \p mode argument and have it
896 * implicitly set to #MBEDTLS_RSA_PRIVATE.
897 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100898 * \note Alternative implementations of RSA need not support
899 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300900 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100901 *
Hanno Becker9a467772018-12-13 09:54:59 +0000902 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000903 * \param f_rng The RNG function to use. If the padding mode is PKCS#1 v2.1,
904 * this must be provided. If the padding mode is PKCS#1 v1.5 and
905 * \p mode is #MBEDTLS_RSA_PRIVATE, it is used for blinding
906 * and should be provided; see mbedtls_rsa_private() for more
907 * more. It is ignored otherwise.
Hanno Becker9a467772018-12-13 09:54:59 +0000908 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
909 * if \p f_rng is \c NULL or doesn't need a context argument.
910 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000911 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100912 * \param md_alg The message-digest algorithm used to hash the original data.
913 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000914 * \param hashlen The length of the message digest.
915 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
916 * \param hash The buffer holding the message digest or raw data.
917 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
918 * buffer of length \p hashlen Bytes. If \p md_alg is not
919 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
920 * the size of the hash corresponding to \p md_alg.
921 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000922 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100923 * for an 2048-bit RSA modulus. A buffer length of
924 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100925 *
926 * \return \c 0 if the signing operation was successful.
927 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000930 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000931 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000932 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000934 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000935 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000936 unsigned char *sig );
937
938/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000939 * \brief This function performs a PKCS#1 v1.5 signature
940 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100941 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100942 * \deprecated It is deprecated and discouraged to call this function
943 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
944 * are likely to remove the \p mode argument and have it
945 * implicitly set to #MBEDTLS_RSA_PRIVATE.
946 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100947 * \note Alternative implementations of RSA need not support
948 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300949 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100950 *
Hanno Becker9a467772018-12-13 09:54:59 +0000951 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000952 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
953 * this is used for blinding and should be provided; see
954 * mbedtls_rsa_private() for more. If \p mode is
955 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000956 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
957 * if \p f_rng is \c NULL or doesn't need a context argument.
958 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000959 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000960 * \param md_alg The message-digest algorithm used to hash the original data.
961 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000962 * \param hashlen The length of the message digest.
963 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
964 * \param hash The buffer holding the message digest or raw data.
965 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
966 * buffer of length \p hashlen Bytes. If \p md_alg is not
967 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
968 * the size of the hash corresponding to \p md_alg.
969 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000970 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100971 * for an 2048-bit RSA modulus. A buffer length of
972 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100973 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100974 * \return \c 0 if the signing operation was successful.
975 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100976 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200978 int (*f_rng)(void *, unsigned char *, size_t),
979 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100980 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100982 unsigned int hashlen,
983 const unsigned char *hash,
984 unsigned char *sig );
985
986/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000987 * \brief This function performs a PKCS#1 v2.1 PSS signature
988 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100989 *
Manuel Pégourié-Gonnard727e1f12021-06-23 10:35:55 +0200990 * \note The \c hash_id set in \p ctx (when calling
991 * mbedtls_rsa_init() or by calling mbedtls_rsa_set_padding()
992 * afterwards) selects the hash used for the
Janos Follath456d7e02021-04-01 14:44:17 +0100993 * encoding operation and for the mask generation function
994 * (MGF1). For more details on the encoding operation and the
995 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +0000996 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follath456d7e02021-04-01 14:44:17 +0100997 * Specifications</em>.
Rose Zadike8b5b992018-03-27 12:19:47 +0100998 *
Cédric Meuter010ddc22020-04-25 09:24:11 +0200999 * \note This function enforces that the provided salt length complies
1000 * with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 v2.2) §9.1.1
1001 * step 3. The constraint is that the hash length plus the salt
1002 * length plus 2 bytes must be at most the key length. If this
1003 * constraint is not met, this function returns
Jaeden Amero3725bb22018-09-07 19:12:36 +01001004 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
1005 *
Hanno Becker9a467772018-12-13 09:54:59 +00001006 * \param ctx The initialized RSA context to use.
1007 * \param f_rng The RNG function. It must not be \c NULL.
1008 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
1009 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +01001010 * \param md_alg The message-digest algorithm used to hash the original data.
1011 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001012 * \param hashlen The length of the message digest.
1013 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
1014 * \param hash The buffer holding the message digest or raw data.
1015 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1016 * buffer of length \p hashlen Bytes. If \p md_alg is not
1017 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1018 * the size of the hash corresponding to \p md_alg.
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001019 * \param saltlen The length of the salt that should be used.
Cédric Meuter010ddc22020-04-25 09:24:11 +02001020 * If passed #MBEDTLS_RSA_SALT_LEN_ANY, the function will use
1021 * the largest possible salt length up to the hash length,
1022 * which is the largest permitted by some standards including
1023 * FIPS 186-4 §5.5.
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001024 * \param sig The buffer to hold the signature. This must be a writable
1025 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1026 * for an 2048-bit RSA modulus. A buffer length of
1027 * #MBEDTLS_MPI_MAX_SIZE is always safe.
1028 *
1029 * \return \c 0 if the signing operation was successful.
1030 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
1031 */
1032int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
1033 int (*f_rng)(void *, unsigned char *, size_t),
1034 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001035 mbedtls_md_type_t md_alg,
1036 unsigned int hashlen,
1037 const unsigned char *hash,
1038 int saltlen,
1039 unsigned char *sig );
1040
1041/**
1042 * \brief This function performs a PKCS#1 v2.1 PSS signature
1043 * operation (RSASSA-PSS-SIGN).
1044 *
Manuel Pégourié-Gonnard727e1f12021-06-23 10:35:55 +02001045 * \note The \c hash_id set in \p ctx (when calling
1046 * mbedtls_rsa_init() or by calling mbedtls_rsa_set_padding()
1047 * afterwards) selects the hash used for the
Janos Follath456d7e02021-04-01 14:44:17 +01001048 * encoding operation and for the mask generation function
1049 * (MGF1). For more details on the encoding operation and the
1050 * mask generation function, consult <em>RFC-3447: Public-Key
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001051 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follath456d7e02021-04-01 14:44:17 +01001052 * Specifications</em>.
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001053 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001054 * \note This function always uses the maximum possible salt size,
1055 * up to the length of the payload hash. This choice of salt
1056 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
1057 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
Rose Zadike8b5b992018-03-27 12:19:47 +01001058 * minimum salt size which is the hash size minus 2 bytes. If
1059 * this minimum size is too large given the key size (the salt
1060 * size, plus the hash size, plus 2 bytes must be no more than
1061 * the key size in bytes), this function returns
1062 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
1063 *
1064 * \deprecated It is deprecated and discouraged to call this function
1065 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
1066 * are likely to remove the \p mode argument and have it
1067 * implicitly set to #MBEDTLS_RSA_PRIVATE.
1068 *
1069 * \note Alternative implementations of RSA need not support
1070 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
1071 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
1072 *
1073 * \param ctx The initialized RSA context to use.
1074 * \param f_rng The RNG function. It must not be \c NULL.
1075 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
1076 * if \p f_rng doesn't need a context argument.
Paul Bakkerb3869132013-02-28 17:21:01 +01001077 * \param mode The mode of operation. This must be either
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Paul Bakkerb3869132013-02-28 17:21:01 +01001079 * \param md_alg The message-digest algorithm used to hash the original data.
1080 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001081 * \param hashlen The length of the message digest.
Janos Follath456d7e02021-04-01 14:44:17 +01001082 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
Hanno Becker9a467772018-12-13 09:54:59 +00001083 * \param hash The buffer holding the message digest or raw data.
1084 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1085 * buffer of length \p hashlen Bytes. If \p md_alg is not
1086 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1087 * the size of the hash corresponding to \p md_alg.
1088 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +00001089 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +01001090 * for an 2048-bit RSA modulus. A buffer length of
1091 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +01001092 *
1093 * \return \c 0 if the signing operation was successful.
1094 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
1095 */
1096int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1097 int (*f_rng)(void *, unsigned char *, size_t),
1098 void *p_rng,
1099 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001101 unsigned int hashlen,
1102 const unsigned char *hash,
1103 unsigned char *sig );
1104
1105/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001106 * \brief This function performs a public RSA operation and checks
1107 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +00001108 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001109 * This is the generic wrapper for performing a PKCS#1
1110 * verification using the mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001111 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001112 * \note For PKCS#1 v2.1 encoding, see comments on
1113 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
1114 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +01001115 *
1116 * \deprecated It is deprecated and discouraged to call this function
1117 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1118 * are likely to remove the \p mode argument and have it
1119 * set to #MBEDTLS_RSA_PUBLIC.
1120 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001121 * \note Alternative implementations of RSA need not support
1122 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001123 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001124 *
Hanno Becker9a467772018-12-13 09:54:59 +00001125 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001126 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1127 * this is used for blinding and should be provided; see
1128 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001129 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1130 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1131 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001132 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001133 * \param md_alg The message-digest algorithm used to hash the original data.
1134 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001135 * \param hashlen The length of the message digest.
1136 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1137 * \param hash The buffer holding the message digest or raw data.
1138 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1139 * buffer of length \p hashlen Bytes. If \p md_alg is not
1140 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1141 * the size of the hash corresponding to \p md_alg.
1142 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001143 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1144 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001145 *
1146 * \return \c 0 if the verify operation was successful.
1147 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001150 int (*f_rng)(void *, unsigned char *, size_t),
1151 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001152 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001154 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001155 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001156 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +00001157
1158/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001159 * \brief This function performs a PKCS#1 v1.5 verification
1160 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001161 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001162 * \deprecated It is deprecated and discouraged to call this function
1163 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1164 * are likely to remove the \p mode argument and have it
1165 * set to #MBEDTLS_RSA_PUBLIC.
1166 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001167 * \note Alternative implementations of RSA need not support
1168 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001169 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001170 *
Hanno Becker9a467772018-12-13 09:54:59 +00001171 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001172 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1173 * this is used for blinding and should be provided; see
1174 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001175 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1176 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1177 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001178 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +00001179 * \param md_alg The message-digest algorithm used to hash the original data.
1180 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001181 * \param hashlen The length of the message digest.
1182 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1183 * \param hash The buffer holding the message digest or raw data.
1184 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1185 * buffer of length \p hashlen Bytes. If \p md_alg is not
1186 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1187 * the size of the hash corresponding to \p md_alg.
1188 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001189 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1190 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001191 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001192 * \return \c 0 if the verify operation was successful.
1193 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001196 int (*f_rng)(void *, unsigned char *, size_t),
1197 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001198 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001200 unsigned int hashlen,
1201 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001202 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001203
1204/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001205 * \brief This function performs a PKCS#1 v2.1 PSS verification
1206 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001207 *
Manuel Pégourié-Gonnard727e1f12021-06-23 10:35:55 +02001208 * \note The \c hash_id set in \p ctx (when calling
1209 * mbedtls_rsa_init() or by calling mbedtls_rsa_set_padding()
1210 * afterwards) selects the hash used for the
Janos Follath456d7e02021-04-01 14:44:17 +01001211 * encoding operation and for the mask generation function
1212 * (MGF1). For more details on the encoding operation and the
1213 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +00001214 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Manuel Pégourié-Gonnard727e1f12021-06-23 10:35:55 +02001215 * Specifications</em>. If the \c hash_id set in \p ctx is
1216 * #MBEDTLS_MD_NONE, the \p md_alg parameter is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001217 *
1218 * \deprecated It is deprecated and discouraged to call this function
1219 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1220 * are likely to remove the \p mode argument and have it
1221 * implicitly set to #MBEDTLS_RSA_PUBLIC.
1222 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001223 * \note Alternative implementations of RSA need not support
1224 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001225 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001226 *
Hanno Becker9a467772018-12-13 09:54:59 +00001227 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001228 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1229 * this is used for blinding and should be provided; see
1230 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001231 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1232 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1233 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001234 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001235 * \param md_alg The message-digest algorithm used to hash the original data.
1236 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001237 * \param hashlen The length of the message digest.
1238 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1239 * \param hash The buffer holding the message digest or raw data.
1240 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1241 * buffer of length \p hashlen Bytes. If \p md_alg is not
1242 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1243 * the size of the hash corresponding to \p md_alg.
1244 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001245 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1246 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001247 *
1248 * \return \c 0 if the verify operation was successful.
1249 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001250 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001252 int (*f_rng)(void *, unsigned char *, size_t),
1253 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001254 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001256 unsigned int hashlen,
1257 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001258 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001259
1260/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001261 * \brief This function performs a PKCS#1 v2.1 PSS verification
1262 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001263 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001264 * \note The \p sig buffer must be as large as the size
1265 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001266 *
Manuel Pégourié-Gonnard727e1f12021-06-23 10:35:55 +02001267 * \note The \c hash_id set in \p ctx (when calling
1268 * mbedtls_rsa_init() or by calling mbedtls_rsa_set_padding()
1269 * afterwards) is ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001270 *
Hanno Becker9a467772018-12-13 09:54:59 +00001271 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001272 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1273 * this is used for blinding and should be provided; see
1274 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001275 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1276 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1277 * \param mode The mode of operation. This must be either
1278 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +01001279 * \param md_alg The message-digest algorithm used to hash the original data.
1280 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001281 * \param hashlen The length of the message digest.
1282 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1283 * \param hash The buffer holding the message digest or raw data.
1284 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1285 * buffer of length \p hashlen Bytes. If \p md_alg is not
1286 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1287 * the size of the hash corresponding to \p md_alg.
Janos Follath456d7e02021-04-01 14:44:17 +01001288 * \param mgf1_hash_id The message digest algorithm used for the
1289 * verification operation and the mask generation
1290 * function (MGF1). For more details on the encoding
1291 * operation and the mask generation function, consult
1292 * <em>RFC-3447: Public-Key Cryptography Standards
1293 * (PKCS) #1 v2.1: RSA Cryptography
1294 * Specifications</em>.
Hanno Becker9a467772018-12-13 09:54:59 +00001295 * \param expected_salt_len The length of the salt used in padding. Use
1296 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1297 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001298 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1299 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001300 *
1301 * \return \c 0 if the verify operation was successful.
1302 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001305 int (*f_rng)(void *, unsigned char *, size_t),
1306 void *p_rng,
1307 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001309 unsigned int hashlen,
1310 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001312 int expected_salt_len,
1313 const unsigned char *sig );
1314
1315/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001316 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001317 *
Hanno Becker9a467772018-12-13 09:54:59 +00001318 * \param dst The destination context. This must be initialized.
1319 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001320 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001321 * \return \c 0 on success.
1322 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001325
1326/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001327 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001328 *
Hanno Becker9a467772018-12-13 09:54:59 +00001329 * \param ctx The RSA context to free. May be \c NULL, in which case
1330 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001331 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
Ron Eldorfa8f6352017-06-20 15:48:46 +03001335#if defined(MBEDTLS_SELF_TEST)
1336
Paul Bakker5121ce52009-01-03 21:22:43 +00001337/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001338 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001340 * \return \c 0 on success.
1341 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001344
Ron Eldorfa8f6352017-06-20 15:48:46 +03001345#endif /* MBEDTLS_SELF_TEST */
1346
Paul Bakker5121ce52009-01-03 21:22:43 +00001347#ifdef __cplusplus
1348}
1349#endif
1350
Paul Bakker5121ce52009-01-03 21:22:43 +00001351#endif /* rsa.h */