blob: cffbe3b5086c0d133636415194dcae70ab924613 [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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
45#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
46#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Rose Zadik042e97f2018-01-26 16:35:10 +000047#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the validity check of the library. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
49#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
50#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
51#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
52#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030053
Paul Bakker5121ce52009-01-03 21:22:43 +000054/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020055 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000056 */
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Rose Zadike8b5b992018-03-27 12:19:47 +010058#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
59#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Rose Zadik042e97f2018-01-26 16:35:10 +000061#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
62#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020065
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020066/*
67 * The above constants may be used even if the RSA module is compile out,
68 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
69 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010070
Paul Bakker407a0da2013-06-27 14:29:21 +020071#ifdef __cplusplus
72extern "C" {
73#endif
74
Ron Eldor4e6d55d2018-02-07 16:36:15 +020075#if !defined(MBEDTLS_RSA_ALT)
76// Regular implementation
77//
78
Paul Bakker5121ce52009-01-03 21:22:43 +000079/**
Rose Zadik042e97f2018-01-26 16:35:10 +000080 * \brief The RSA context structure.
Paul Bakker5121ce52009-01-03 21:22:43 +000081 */
Dawid Drozd428cc522018-07-24 10:02:47 +020082typedef struct mbedtls_rsa_context
Paul Bakker5121ce52009-01-03 21:22:43 +000083{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020084 int MBEDTLS_PRIVATE(ver); /*!< Reserved for internal purposes.
Gilles Peskine4337a9c2021-02-09 18:59:42 +010085 * Do not set this field in application
86 * code. Its meaning might change without
87 * notice. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020088 size_t MBEDTLS_PRIVATE(len); /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +000089
Mateusz Starzyk846f0212021-05-19 19:44:07 +020090 mbedtls_mpi MBEDTLS_PRIVATE(N); /*!< The public modulus. */
91 mbedtls_mpi MBEDTLS_PRIVATE(E); /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Mateusz Starzyk846f0212021-05-19 19:44:07 +020093 mbedtls_mpi MBEDTLS_PRIVATE(D); /*!< The private exponent. */
94 mbedtls_mpi MBEDTLS_PRIVATE(P); /*!< The first prime factor. */
95 mbedtls_mpi MBEDTLS_PRIVATE(Q); /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +010096
Mateusz Starzyk846f0212021-05-19 19:44:07 +020097 mbedtls_mpi MBEDTLS_PRIVATE(DP); /*!< <code>D % (P - 1)</code>. */
98 mbedtls_mpi MBEDTLS_PRIVATE(DQ); /*!< <code>D % (Q - 1)</code>. */
99 mbedtls_mpi MBEDTLS_PRIVATE(QP); /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200101 mbedtls_mpi MBEDTLS_PRIVATE(RN); /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200103 mbedtls_mpi MBEDTLS_PRIVATE(RP); /*!< cached <code>R^2 mod P</code>. */
104 mbedtls_mpi MBEDTLS_PRIVATE(RQ); /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200105
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200106 mbedtls_mpi MBEDTLS_PRIVATE(Vi); /*!< The cached blinding value. */
107 mbedtls_mpi MBEDTLS_PRIVATE(Vf); /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000108
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200109 int MBEDTLS_PRIVATE(padding); /*!< Selects padding mode:
Rose Zadik042e97f2018-01-26 16:35:10 +0000110 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
111 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200112 int MBEDTLS_PRIVATE(hash_id); /*!< Hash identifier of mbedtls_md_type_t type,
Rose Zadik042e97f2018-01-26 16:35:10 +0000113 as specified in md.h for use in the MGF
114 mask generating function used in the
115 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116#if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100117 /* Invariant: the mutex is initialized iff ver != 0. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200118 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex); /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200119#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000120}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200123#else /* MBEDTLS_RSA_ALT */
124#include "rsa_alt.h"
125#endif /* MBEDTLS_RSA_ALT */
126
Paul Bakker5121ce52009-01-03 21:22:43 +0000127/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000128 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200130 * \note This function initializes the padding and the hash
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200131 * identifier to respectively #MBEDTLS_RSA_PKCS_V15 and
132 * #MBEDTLS_MD_NONE. See mbedtls_rsa_set_padding() for more
133 * information about those parameters.
Ronald Cronc1905a12021-06-05 11:11:14 +0200134 *
135 * \param ctx The RSA context to initialize. This must not be \c NULL.
136 */
137void mbedtls_rsa_init( mbedtls_rsa_context *ctx );
138
139/**
140 * \brief This function sets padding for an already initialized RSA
141 * context.
142 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000143 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000144 * encryption scheme and the RSASSA-PSS signature scheme.
145 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000146 * \note The \p hash_id parameter is ignored when using
147 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200148 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200149 * \note The choice of padding mode is strictly enforced for private
150 * key operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000151 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100152 * a default value, which can be overridden by calling specific
Ronald Cronc1905a12021-06-05 11:11:14 +0200153 * \c mbedtls_rsa_rsaes_xxx or \c mbedtls_rsa_rsassa_xxx
154 * functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200155 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000156 * \note The hash selected in \p hash_id is always used for OEAP
157 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100158 * making signatures, but can be overridden for verifying them.
159 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100160 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200161 * \param ctx The initialized RSA context to be configured.
Hanno Becker9a467772018-12-13 09:54:59 +0000162 * \param padding The padding mode to use. This must be either
163 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200164 * \param hash_id The hash identifier for PSS or OAEP, if \p padding is
165 * #MBEDTLS_RSA_PKCS_V21. #MBEDTLS_MD_NONE is accepted by this
166 * function but may be not suitable for some operations.
167 * Ignored if \p padding is #MBEDTLS_RSA_PKCS_V15.
Ronald Cronc1905a12021-06-05 11:11:14 +0200168 *
169 * \return \c 0 on success.
170 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING failure:
171 * \p padding or \p hash_id is invalid.
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 */
Ronald Cronc1905a12021-06-05 11:11:14 +0200173int mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
174 mbedtls_md_type_t hash_id );
Paul Bakker5121ce52009-01-03 21:22:43 +0000175
176/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000177 * \brief This function imports a set of core parameters into an
178 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100179 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100180 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000181 * imports, if the parameters are not simultaneously present.
182 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100183 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000184 * by a call to mbedtls_rsa_complete(), which checks and
185 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100186 * public or private RSA key.
187 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000188 * \note See mbedtls_rsa_complete() for more information on which
189 * parameters are necessary to set up a private or public
190 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100191 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100192 * \note The imported parameters are copied and need not be preserved
193 * for the lifetime of the RSA context being set up.
194 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100195 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000196 * \param N The RSA modulus. This may be \c NULL.
197 * \param P The first prime factor of \p N. This may be \c NULL.
198 * \param Q The second prime factor of \p N. This may be \c NULL.
199 * \param D The private exponent. This may be \c NULL.
200 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100201 *
202 * \return \c 0 on success.
203 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100204 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100205int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
206 const mbedtls_mpi *N,
207 const mbedtls_mpi *P, const mbedtls_mpi *Q,
208 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100209
210/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000211 * \brief This function imports core RSA parameters, in raw big-endian
212 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100214 * \note This function can be called multiple times for successive
215 * imports, if the parameters are not simultaneously present.
216 *
217 * Any sequence of calls to this function should be followed
218 * by a call to mbedtls_rsa_complete(), which checks and
219 * completes the provided information to a ready-for-use
220 * public or private RSA key.
221 *
222 * \note See mbedtls_rsa_complete() for more information on which
223 * parameters are necessary to set up a private or public
224 * RSA key.
225 *
226 * \note The imported parameters are copied and need not be preserved
227 * for the lifetime of the RSA context being set up.
228 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000229 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000230 * \param N The RSA modulus. This may be \c NULL.
231 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
232 * \param P The first prime factor of \p N. This may be \c NULL.
233 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
234 * \param Q The second prime factor of \p N. This may be \c NULL.
235 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
236 * \param D The private exponent. This may be \c NULL.
237 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
238 * \param E The public exponent. This may be \c NULL.
239 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100241 * \return \c 0 on success.
242 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100243 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100244int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100245 unsigned char const *N, size_t N_len,
246 unsigned char const *P, size_t P_len,
247 unsigned char const *Q, size_t Q_len,
248 unsigned char const *D, size_t D_len,
249 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100250
251/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000252 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100253 * a set of imported core parameters.
254 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000255 * To setup an RSA public key, precisely \p N and \p E
256 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100257 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000258 * To setup an RSA private key, sufficient information must
259 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100260 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000261 * The default implementation supports the following:
262 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
263 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
264 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100265 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000266 * If this function runs successfully, it guarantees that
267 * the RSA context can be used for RSA operations without
268 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100269 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100270 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000271 * for the imported parameters. In particular, parameters that
272 * are not needed by the implementation might be silently
273 * discarded and left unchecked. To check the consistency
274 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100275 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100276 * \param ctx The initialized RSA context holding imported parameters.
277 *
278 * \return \c 0 on success.
279 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
280 * failed.
281 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100282 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100283int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100284
285/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000286 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100287 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000288 * If this function runs successfully, the non-NULL buffers
289 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
290 * written, with additional unused space filled leading by
291 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100292 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000293 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300294 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000295 * <li>An alternative RSA implementation is in use, which
296 * stores the key externally, and either cannot or should
297 * not export it into RAM.</li>
298 * <li>A SW or HW implementation might not support a certain
299 * deduction. For example, \p P, \p Q from \p N, \p D,
300 * and \p E if the former are not part of the
301 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100302 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000303 * If the function fails due to an unsupported operation,
304 * the RSA context stays intact and remains usable.
305 *
306 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000307 * \param N The MPI to hold the RSA modulus.
308 * This may be \c NULL if this field need not be exported.
309 * \param P The MPI to hold the first prime factor of \p N.
310 * This may be \c NULL if this field need not be exported.
311 * \param Q The MPI to hold the second prime factor of \p N.
312 * This may be \c NULL if this field need not be exported.
313 * \param D The MPI to hold the private exponent.
314 * This may be \c NULL if this field need not be exported.
315 * \param E The MPI to hold the public exponent.
316 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000317 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100318 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300319 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000320 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100321 * functionality or because of security policies.
322 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100323 *
324 */
325int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
326 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
327 mbedtls_mpi *D, mbedtls_mpi *E );
328
329/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000330 * \brief This function exports core parameters of an RSA key
331 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100332 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000333 * If this function runs successfully, the non-NULL buffers
334 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
335 * written, with additional unused space filled leading by
336 * zero Bytes.
337 *
338 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300339 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000340 * <li>An alternative RSA implementation is in use, which
341 * stores the key externally, and either cannot or should
342 * not export it into RAM.</li>
343 * <li>A SW or HW implementation might not support a certain
344 * deduction. For example, \p P, \p Q from \p N, \p D,
345 * and \p E if the former are not part of the
346 * implementation.</li></ul>
347 * If the function fails due to an unsupported operation,
348 * the RSA context stays intact and remains usable.
349 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100350 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100351 * buffer pointers are NULL.
352 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000353 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000354 * \param N The Byte array to store the RSA modulus,
355 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000356 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000357 * \param P The Byte array to hold the first prime factor of \p N,
358 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000359 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000360 * \param Q The Byte array to hold the second prime factor of \p N,
361 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000362 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000363 * \param D The Byte array to hold the private exponent,
364 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000365 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000366 * \param E The Byte array to hold the public exponent,
367 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000368 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100369 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100370 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300371 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000372 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100373 * functionality or because of security policies.
374 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100375 */
376int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
377 unsigned char *N, size_t N_len,
378 unsigned char *P, size_t P_len,
379 unsigned char *Q, size_t Q_len,
380 unsigned char *D, size_t D_len,
381 unsigned char *E, size_t E_len );
382
383/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000384 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100385 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100386 * \note Alternative RSA implementations not using CRT-parameters
387 * internally can implement this function based on
388 * mbedtls_rsa_deduce_opt().
389 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000390 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000391 * \param DP The MPI to hold \c D modulo `P-1`,
392 * or \c NULL if it need not be exported.
393 * \param DQ The MPI to hold \c D modulo `Q-1`,
394 * or \c NULL if it need not be exported.
395 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
396 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100397 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100398 * \return \c 0 on success.
399 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100400 *
401 */
402int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
403 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
404
Paul Bakker5121ce52009-01-03 21:22:43 +0000405/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000406 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100407 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000408 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100409 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000410 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100411 *
412 */
413size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
414
415/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000416 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000417 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000418 * \note mbedtls_rsa_init() must be called before this function,
419 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 *
Hanno Becker9a467772018-12-13 09:54:59 +0000421 * \param ctx The initialized RSA context used to hold the key.
422 * \param f_rng The RNG function to be used for key generation.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100423 * This is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000424 * \param p_rng The RNG context to be passed to \p f_rng.
425 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100426 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000427 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000428 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100429 *
430 * \return \c 0 on success.
431 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000432 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100434 int (*f_rng)(void *, unsigned char *, size_t),
435 void *p_rng,
436 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
438/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000439 * \brief This function checks if a context contains at least an RSA
440 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000441 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000442 * If the function runs successfully, it is guaranteed that
443 * enough information is present to perform an RSA public key
444 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000445 *
Hanno Becker9a467772018-12-13 09:54:59 +0000446 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000447 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100448 * \return \c 0 on success.
449 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100450 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000453
454/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000455 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100456 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 *
Hanno Becker68767a62017-10-17 10:13:31 +0100458 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000459 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100460 * on the given context, but that the various parameters are
461 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000462 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100463 *
464 * \warning This function should catch accidental misconfigurations
465 * like swapping of parameters, but it cannot establish full
466 * trust in neither the quality nor the consistency of the key
467 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000468 * <ul><li>Consistency: Imported parameters that are irrelevant
469 * for the implementation might be silently dropped. If dropped,
470 * the current function does not have access to them,
471 * and therefore cannot check them. See mbedtls_rsa_complete().
472 * If you want to check the consistency of the entire
473 * content of an PKCS1-encoded RSA private key, for example, you
474 * should use mbedtls_rsa_validate_params() before setting
475 * up the RSA context.
476 * Additionally, if the implementation performs empirical checks,
477 * these checks substantiate but do not guarantee consistency.</li>
478 * <li>Quality: This function is not expected to perform
479 * extended quality assessments like checking that the prime
480 * factors are safe. Additionally, it is the responsibility of the
481 * user to ensure the trustworthiness of the source of his RSA
482 * parameters, which goes beyond what is effectively checkable
483 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100484 *
Hanno Becker9a467772018-12-13 09:54:59 +0000485 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100486 *
487 * \return \c 0 on success.
488 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000489 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000493 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100494 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000495 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100496 *
Hanno Becker9a467772018-12-13 09:54:59 +0000497 * \param pub The initialized RSA context holding the public key.
498 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000499 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100500 * \return \c 0 on success.
501 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100502 */
Hanno Becker98838b02017-10-02 13:16:10 +0100503int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
504 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100505
506/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000507 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 *
Hanno Becker9a467772018-12-13 09:54:59 +0000509 * \param ctx The initialized RSA context to use.
510 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000511 * of length \c ctx->len Bytes. For example, \c 256 Bytes
512 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000513 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000514 * of length \c ctx->len Bytes. For example, \c 256 Bytes
515 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000516 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000517 * \note This function does not handle message padding.
518 *
519 * \note Make sure to set \p input[0] = 0 or ensure that
520 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100522 * \return \c 0 on success.
523 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000526 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000527 unsigned char *output );
528
529/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000530 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 *
Hanno Becker24120612017-10-26 11:53:35 +0100532 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100533 *
534 * \note If blinding is used, both the base of exponentation
Hanno Becker24120612017-10-26 11:53:35 +0100535 * and the exponent are blinded, providing protection
536 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100537 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100538 * \warning It is deprecated and a security risk to not provide
539 * a PRNG here and thereby prevent the use of blinding.
540 * Future versions of the library may enforce the presence
541 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100542 *
Hanno Becker9a467772018-12-13 09:54:59 +0000543 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100544 * \param f_rng The RNG function, used for blinding. It is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000545 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
Thomas Daubney03412782021-05-20 15:31:17 +0100546 * if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000547 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000548 * of length \c ctx->len Bytes. For example, \c 256 Bytes
549 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000550 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000551 * of length \c ctx->len Bytes. For example, \c 256 Bytes
552 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100553 *
554 * \return \c 0 on success.
555 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
556 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200559 int (*f_rng)(void *, unsigned char *, size_t),
560 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000561 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 unsigned char *output );
563
564/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000565 * \brief This function adds the message padding, then performs an RSA
566 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000568 * It is the generic wrapper for performing a PKCS#1 encryption
Thomas Daubney21772772021-05-13 17:30:32 +0100569 * operation.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100570 *
Hanno Becker9a467772018-12-13 09:54:59 +0000571 * \param ctx The initialized RSA context to use.
Thomas Daubneyf54c5c52021-05-21 17:00:30 +0100572 * \param f_rng The RNG to use. It is used for padding generation
Thomas Daubney2c65db92021-05-21 10:58:28 +0100573 * and it is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000574 * \param p_rng The RNG context to be passed to \p f_rng. May be
Thomas Daubney03412782021-05-20 15:31:17 +0100575 * \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000576 * \param ilen The length of the plaintext in Bytes.
577 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000578 * buffer of size \p ilen Bytes. It may be \c NULL if
579 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000580 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000581 * of length \c ctx->len Bytes. For example, \c 256 Bytes
582 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100583 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100584 * \return \c 0 on success.
585 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000588 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000589 void *p_rng,
Thomas Daubney21772772021-05-13 17:30:32 +0100590 size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000591 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000592 unsigned char *output );
593
594/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000595 * \brief This function performs a PKCS#1 v1.5 encryption operation
596 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100597 *
Hanno Becker9a467772018-12-13 09:54:59 +0000598 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100599 * \param f_rng The RNG function to use. It is mandatory and used for
600 * padding generation.
Hanno Becker9a467772018-12-13 09:54:59 +0000601 * \param p_rng The RNG context to be passed to \p f_rng. This may
Thomas Daubney03412782021-05-20 15:31:17 +0100602 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000603 * \param ilen The length of the plaintext in Bytes.
604 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000605 * buffer of size \p ilen Bytes. It may be \c NULL if
606 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000607 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000608 * of length \c ctx->len Bytes. For example, \c 256 Bytes
609 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100610 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100611 * \return \c 0 on success.
612 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100613 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100615 int (*f_rng)(void *, unsigned char *, size_t),
616 void *p_rng,
Thomas Daubney53e4ac62021-05-13 18:26:49 +0100617 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100618 const unsigned char *input,
619 unsigned char *output );
620
621/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000622 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
623 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100624 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100625 * \note The output buffer must be as large as the size
626 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
627 *
Hanno Becker9a467772018-12-13 09:54:59 +0000628 * \param ctx The initnialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000629 * \param f_rng The RNG function to use. This is needed for padding
Thomas Daubney2c65db92021-05-21 10:58:28 +0100630 * generation and is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000631 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000632 * be \c NULL if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000633 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000634 * This must be a readable buffer of length \p label_len
635 * Bytes. It may be \c NULL if \p label_len is \c 0.
636 * \param label_len The length of the label in Bytes.
637 * \param ilen The length of the plaintext buffer \p input in Bytes.
638 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000639 * buffer of size \p ilen Bytes. It may be \c NULL if
640 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000641 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000642 * of length \c ctx->len Bytes. For example, \c 256 Bytes
643 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100644 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100645 * \return \c 0 on success.
646 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100649 int (*f_rng)(void *, unsigned char *, size_t),
650 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100651 const unsigned char *label, size_t label_len,
652 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100653 const unsigned char *input,
654 unsigned char *output );
655
656/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000657 * \brief This function performs an RSA operation, then removes the
658 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000659 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000660 * It is the generic wrapper for performing a PKCS#1 decryption
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100661 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000662 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100663 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000664 * as large as the size \p ctx->len of \p ctx->N (for example,
665 * 128 Bytes if RSA-1024 is used) to be able to hold an
666 * arbitrary decrypted message. If it is not large enough to
667 * hold the decryption of the particular ciphertext provided,
668 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100669 *
Hanno Becker9a467772018-12-13 09:54:59 +0000670 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100671 * \param f_rng The RNG function. This is used for blinding and is
672 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000673 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100674 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000675 * \param olen The address at which to store the length of
676 * the plaintext. This must not be \c NULL.
677 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000678 * of length \c ctx->len Bytes. For example, \c 256 Bytes
679 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000680 * \param output The buffer used to hold the plaintext. This must
681 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000682 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100683 *
684 * \return \c 0 on success.
685 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200688 int (*f_rng)(void *, unsigned char *, size_t),
689 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100690 size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000691 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000692 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000693 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000694
695/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000696 * \brief This function performs a PKCS#1 v1.5 decryption
697 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100698 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100699 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000700 * as large as the size \p ctx->len of \p ctx->N, for example,
701 * 128 Bytes if RSA-1024 is used, to be able to hold an
702 * arbitrary decrypted message. If it is not large enough to
703 * hold the decryption of the particular ciphertext provided,
704 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100705 *
Hanno Becker9a467772018-12-13 09:54:59 +0000706 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100707 * \param f_rng The RNG function. This is used for blinding and is
708 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000709 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100710 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000711 * \param olen The address at which to store the length of
712 * the plaintext. This must not be \c NULL.
713 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000714 * of length \c ctx->len Bytes. For example, \c 256 Bytes
715 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000716 * \param output The buffer used to hold the plaintext. This must
717 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000718 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100719 *
720 * \return \c 0 on success.
721 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
722 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200725 int (*f_rng)(void *, unsigned char *, size_t),
726 void *p_rng,
Thomas Daubney34733082021-05-12 09:24:29 +0100727 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100728 const unsigned char *input,
729 unsigned char *output,
730 size_t output_max_len );
731
732/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100733 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
734 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100735 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100736 * \note The output buffer length \c output_max_len should be
737 * as large as the size \p ctx->len of \p ctx->N, for
738 * example, 128 Bytes if RSA-1024 is used, to be able to
739 * hold an arbitrary decrypted message. If it is not
740 * large enough to hold the decryption of the particular
741 * ciphertext provided, the function returns
742 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100743 *
Hanno Becker9a467772018-12-13 09:54:59 +0000744 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100745 * \param f_rng The RNG function. This is used for blinding and is
746 * mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000747 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100748 * \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100749 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000750 * This must be a readable buffer of length \p label_len
751 * Bytes. It may be \c NULL if \p label_len is \c 0.
752 * \param label_len The length of the label in Bytes.
753 * \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 Bakkerb3869132013-02-28 17:21:01 +0100764 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765int mbedtls_rsa_rsaes_oaep_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 Bakkera43231c2013-02-28 17:33:49 +0100768 const unsigned char *label, size_t label_len,
769 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100770 const unsigned char *input,
771 unsigned char *output,
772 size_t output_max_len );
773
774/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000775 * \brief This function performs a private RSA operation to sign
776 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000777 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000778 * It is the generic wrapper for performing a PKCS#1
Thomas Daubney140184d2021-05-18 16:04:07 +0100779 * signature.
Paul Bakker5121ce52009-01-03 21:22:43 +0000780 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000781 * \note The \p sig buffer must be as large as the size
782 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000783 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000784 * \note For PKCS#1 v2.1 encoding, see comments on
785 * mbedtls_rsa_rsassa_pss_sign() for details on
786 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100787 *
Hanno Becker9a467772018-12-13 09:54:59 +0000788 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100789 * \param f_rng The RNG function to use. This is mandatory and
790 * must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000791 * \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 +0100792 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100793 * \param md_alg The message-digest algorithm used to hash the original data.
794 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200795 * \param hashlen The length of the message digest or raw data in Bytes.
796 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
797 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000798 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200799 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000800 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000801 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100802 * for an 2048-bit RSA modulus. A buffer length of
803 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100804 *
805 * \return \c 0 if the signing operation was successful.
806 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000807 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000809 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000810 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000812 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000813 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000814 unsigned char *sig );
815
816/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000817 * \brief This function performs a PKCS#1 v1.5 signature
818 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100819 *
Hanno Becker9a467772018-12-13 09:54:59 +0000820 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100821 * \param f_rng The RNG function. This is used for blinding and is
822 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000823 * \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 +0100824 * if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000825 * \param md_alg The message-digest algorithm used to hash the original data.
826 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200827 * \param hashlen The length of the message digest or raw data in Bytes.
828 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
829 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000830 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200831 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000832 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000833 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100834 * for an 2048-bit RSA modulus. A buffer length of
835 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100836 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100837 * \return \c 0 if the signing operation was successful.
838 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100839 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200841 int (*f_rng)(void *, unsigned char *, size_t),
842 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100844 unsigned int hashlen,
845 const unsigned char *hash,
846 unsigned char *sig );
847
848/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000849 * \brief This function performs a PKCS#1 v2.1 PSS signature
850 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100851 *
Janos Follathb7953322021-04-01 14:44:17 +0100852 * \note The \c hash_id set in \p ctx by calling
853 * mbedtls_rsa_set_padding() selects the hash used for the
854 * encoding operation and for the mask generation function
855 * (MGF1). For more details on the encoding operation and the
856 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +0000857 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100858 * Specifications</em>.
Rose Zadike8b5b992018-03-27 12:19:47 +0100859 *
Cédric Meuter010ddc22020-04-25 09:24:11 +0200860 * \note This function enforces that the provided salt length complies
861 * with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 v2.2) §9.1.1
862 * step 3. The constraint is that the hash length plus the salt
863 * length plus 2 bytes must be at most the key length. If this
864 * constraint is not met, this function returns
Jaeden Amero3725bb22018-09-07 19:12:36 +0100865 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
866 *
Hanno Becker9a467772018-12-13 09:54:59 +0000867 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100868 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000869 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
870 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100871 * \param md_alg The message-digest algorithm used to hash the original data.
872 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200873 * \param hashlen The length of the message digest or raw data in Bytes.
874 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
875 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000876 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200877 * This must be a readable buffer of at least \p hashlen Bytes.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200878 * \param saltlen The length of the salt that should be used.
Cédric Meuter010ddc22020-04-25 09:24:11 +0200879 * If passed #MBEDTLS_RSA_SALT_LEN_ANY, the function will use
880 * the largest possible salt length up to the hash length,
881 * which is the largest permitted by some standards including
882 * FIPS 186-4 §5.5.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200883 * \param sig The buffer to hold the signature. This must be a writable
884 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
885 * for an 2048-bit RSA modulus. A buffer length of
886 * #MBEDTLS_MPI_MAX_SIZE is always safe.
887 *
888 * \return \c 0 if the signing operation was successful.
889 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
890 */
891int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
892 int (*f_rng)(void *, unsigned char *, size_t),
893 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200894 mbedtls_md_type_t md_alg,
895 unsigned int hashlen,
896 const unsigned char *hash,
897 int saltlen,
898 unsigned char *sig );
899
900/**
901 * \brief This function performs a PKCS#1 v2.1 PSS signature
902 * operation (RSASSA-PSS-SIGN).
903 *
Janos Follathb7953322021-04-01 14:44:17 +0100904 * \note The \c hash_id set in \p ctx by calling
905 * mbedtls_rsa_set_padding() selects the hash used for the
906 * encoding operation and for the mask generation function
907 * (MGF1). For more details on the encoding operation and the
908 * mask generation function, consult <em>RFC-3447: Public-Key
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200909 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100910 * Specifications</em>.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200911 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000912 * \note This function always uses the maximum possible salt size,
913 * up to the length of the payload hash. This choice of salt
914 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
915 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
Rose Zadike8b5b992018-03-27 12:19:47 +0100916 * minimum salt size which is the hash size minus 2 bytes. If
917 * this minimum size is too large given the key size (the salt
918 * size, plus the hash size, plus 2 bytes must be no more than
919 * the key size in bytes), this function returns
920 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
921 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100922 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100923 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100924 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
925 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100926 * \param md_alg The message-digest algorithm used to hash the original data.
927 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200928 * \param hashlen The length of the message digest or raw data in Bytes.
929 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
930 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000931 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200932 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000933 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000934 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100935 * for an 2048-bit RSA modulus. A buffer length of
936 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100937 *
938 * \return \c 0 if the signing operation was successful.
939 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100940 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100942 int (*f_rng)(void *, unsigned char *, size_t),
943 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100945 unsigned int hashlen,
946 const unsigned char *hash,
947 unsigned char *sig );
948
949/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000950 * \brief This function performs a public RSA operation and checks
951 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +0000952 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000953 * This is the generic wrapper for performing a PKCS#1
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100954 * verification.
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000956 * \note For PKCS#1 v2.1 encoding, see comments on
957 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
958 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100959 *
Hanno Becker9a467772018-12-13 09:54:59 +0000960 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +0100961 * \param md_alg The message-digest algorithm used to hash the original data.
962 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200963 * \param hashlen The length of the message digest or raw data in Bytes.
964 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
965 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000966 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200967 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000968 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +0000969 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
970 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100971 *
972 * \return \c 0 if the verify operation was successful.
973 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000977 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000978 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200979 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000980
981/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000982 * \brief This function performs a PKCS#1 v1.5 verification
983 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +0100984 *
Hanno Becker9a467772018-12-13 09:54:59 +0000985 * \param ctx The initialized RSA public key context to use.
Rose Zadik042e97f2018-01-26 16:35:10 +0000986 * \param md_alg The message-digest algorithm used to hash the original data.
987 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200988 * \param hashlen The length of the message digest or raw data in Bytes.
989 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
990 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000991 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200992 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000993 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +0000994 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
995 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100996 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100997 * \return \c 0 if the verify operation was successful.
998 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100999 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001002 unsigned int hashlen,
1003 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001004 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001005
1006/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001007 * \brief This function performs a PKCS#1 v2.1 PSS verification
1008 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001009 *
Janos Follathb7953322021-04-01 14:44:17 +01001010 * \note The \c hash_id set in \p ctx by calling
1011 * mbedtls_rsa_set_padding() selects the hash used for the
1012 * encoding operation and for the mask generation function
1013 * (MGF1). For more details on the encoding operation and the
1014 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +00001015 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +01001016 * Specifications</em>. If the \c hash_id set in \p ctx by
1017 * mbedtls_rsa_set_padding() is #MBEDTLS_MD_NONE, the \p md_alg
1018 * parameter is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001019 *
Hanno Becker9a467772018-12-13 09:54:59 +00001020 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001021 * \param md_alg The message-digest algorithm used to hash the original data.
1022 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001023 * \param hashlen The length of the message digest or raw data in Bytes.
1024 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1025 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001026 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001027 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001028 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001029 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1030 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001031 *
1032 * \return \c 0 if the verify operation was successful.
1033 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001034 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001037 unsigned int hashlen,
1038 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001039 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001040
1041/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001042 * \brief This function performs a PKCS#1 v2.1 PSS verification
1043 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001044 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001045 * \note The \p sig buffer must be as large as the size
1046 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001047 *
Janos Follathb7953322021-04-01 14:44:17 +01001048 * \note The \c hash_id set in \p ctx by mbedtls_rsa_set_padding() is
1049 * ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001050 *
Hanno Becker9a467772018-12-13 09:54:59 +00001051 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001052 * \param md_alg The message-digest algorithm used to hash the original data.
1053 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001054 * \param hashlen The length of the message digest or raw data in Bytes.
1055 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1056 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001057 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001058 * This must be a readable buffer of at least \p hashlen Bytes.
Janos Follathb7953322021-04-01 14:44:17 +01001059 * \param mgf1_hash_id The message digest algorithm used for the
1060 * verification operation and the mask generation
1061 * function (MGF1). For more details on the encoding
1062 * operation and the mask generation function, consult
1063 * <em>RFC-3447: Public-Key Cryptography Standards
1064 * (PKCS) #1 v2.1: RSA Cryptography
1065 * Specifications</em>.
Hanno Becker9a467772018-12-13 09:54:59 +00001066 * \param expected_salt_len The length of the salt used in padding. Use
1067 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1068 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001069 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1070 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001071 *
1072 * \return \c 0 if the verify operation was successful.
1073 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001077 unsigned int hashlen,
1078 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001080 int expected_salt_len,
1081 const unsigned char *sig );
1082
1083/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001084 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001085 *
Hanno Becker9a467772018-12-13 09:54:59 +00001086 * \param dst The destination context. This must be initialized.
1087 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001088 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001089 * \return \c 0 on success.
1090 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001093
1094/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001095 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001096 *
Hanno Becker9a467772018-12-13 09:54:59 +00001097 * \param ctx The RSA context to free. May be \c NULL, in which case
1098 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001099 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001102
Ron Eldorfa8f6352017-06-20 15:48:46 +03001103#if defined(MBEDTLS_SELF_TEST)
1104
Paul Bakker5121ce52009-01-03 21:22:43 +00001105/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001106 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001107 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001108 * \return \c 0 on success.
1109 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001112
Ron Eldorfa8f6352017-06-20 15:48:46 +03001113#endif /* MBEDTLS_SELF_TEST */
1114
Paul Bakker5121ce52009-01-03 21:22:43 +00001115#ifdef __cplusplus
1116}
1117#endif
1118
Paul Bakker5121ce52009-01-03 21:22:43 +00001119#endif /* rsa.h */