blob: c953a89490a1a04c9fe63e13b031848f07f38683 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010033#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#endif
Paul Bakkered27a042013-04-18 22:46:23 +020037
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010038#include "mbedtls/bignum.h"
39#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_THREADING_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010042#include "mbedtls/threading.h"
Paul Bakkerc9965dc2013-09-29 14:58:17 +020043#endif
44
Paul Bakker13e2dfe2009-07-28 07:18:38 +000045/*
46 * RSA Error codes
47 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
49#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
50#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Rose Zadik042e97f2018-01-26 16:35:10 +000051#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 +020052#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
53#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
54#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
55#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
56#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030057
Paul Bakker5121ce52009-01-03 21:22:43 +000058/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020059 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000060 */
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Rose Zadike8b5b992018-03-27 12:19:47 +010062#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
63#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Rose Zadik042e97f2018-01-26 16:35:10 +000065#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
66#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020069
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020070/*
71 * The above constants may be used even if the RSA module is compile out,
72 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
73 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010074
Paul Bakker407a0da2013-06-27 14:29:21 +020075#ifdef __cplusplus
76extern "C" {
77#endif
78
Ron Eldor4e6d55d2018-02-07 16:36:15 +020079#if !defined(MBEDTLS_RSA_ALT)
80// Regular implementation
81//
82
Paul Bakker5121ce52009-01-03 21:22:43 +000083/**
Rose Zadik042e97f2018-01-26 16:35:10 +000084 * \brief The RSA context structure.
Paul Bakker5121ce52009-01-03 21:22:43 +000085 */
Dawid Drozd428cc522018-07-24 10:02:47 +020086typedef struct mbedtls_rsa_context
Paul Bakker5121ce52009-01-03 21:22:43 +000087{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020088 int MBEDTLS_PRIVATE(ver); /*!< Reserved for internal purposes.
Gilles Peskine4337a9c2021-02-09 18:59:42 +010089 * Do not set this field in application
90 * code. Its meaning might change without
91 * notice. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020092 size_t MBEDTLS_PRIVATE(len); /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +000093
Mateusz Starzyk846f0212021-05-19 19:44:07 +020094 mbedtls_mpi MBEDTLS_PRIVATE(N); /*!< The public modulus. */
95 mbedtls_mpi MBEDTLS_PRIVATE(E); /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Mateusz Starzyk846f0212021-05-19 19:44:07 +020097 mbedtls_mpi MBEDTLS_PRIVATE(D); /*!< The private exponent. */
98 mbedtls_mpi MBEDTLS_PRIVATE(P); /*!< The first prime factor. */
99 mbedtls_mpi MBEDTLS_PRIVATE(Q); /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100100
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200101 mbedtls_mpi MBEDTLS_PRIVATE(DP); /*!< <code>D % (P - 1)</code>. */
102 mbedtls_mpi MBEDTLS_PRIVATE(DQ); /*!< <code>D % (Q - 1)</code>. */
103 mbedtls_mpi MBEDTLS_PRIVATE(QP); /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200105 mbedtls_mpi MBEDTLS_PRIVATE(RN); /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200107 mbedtls_mpi MBEDTLS_PRIVATE(RP); /*!< cached <code>R^2 mod P</code>. */
108 mbedtls_mpi MBEDTLS_PRIVATE(RQ); /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200109
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200110 mbedtls_mpi MBEDTLS_PRIVATE(Vi); /*!< The cached blinding value. */
111 mbedtls_mpi MBEDTLS_PRIVATE(Vf); /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000112
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200113 int MBEDTLS_PRIVATE(padding); /*!< Selects padding mode:
Rose Zadik042e97f2018-01-26 16:35:10 +0000114 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
115 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200116 int MBEDTLS_PRIVATE(hash_id); /*!< Hash identifier of mbedtls_md_type_t type,
Rose Zadik042e97f2018-01-26 16:35:10 +0000117 as specified in md.h for use in the MGF
118 mask generating function used in the
119 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100121 /* Invariant: the mutex is initialized iff ver != 0. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200122 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex); /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200123#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000124}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200127#else /* MBEDTLS_RSA_ALT */
128#include "rsa_alt.h"
129#endif /* MBEDTLS_RSA_ALT */
130
Paul Bakker5121ce52009-01-03 21:22:43 +0000131/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000132 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200134 * \note This function initializes the padding and the hash
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200135 * identifier to respectively #MBEDTLS_RSA_PKCS_V15 and
136 * #MBEDTLS_MD_NONE. See mbedtls_rsa_set_padding() for more
137 * information about those parameters.
Ronald Cronc1905a12021-06-05 11:11:14 +0200138 *
139 * \param ctx The RSA context to initialize. This must not be \c NULL.
140 */
141void mbedtls_rsa_init( mbedtls_rsa_context *ctx );
142
143/**
144 * \brief This function sets padding for an already initialized RSA
145 * context.
146 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000147 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000148 * encryption scheme and the RSASSA-PSS signature scheme.
149 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000150 * \note The \p hash_id parameter is ignored when using
151 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200152 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200153 * \note The choice of padding mode is strictly enforced for private
154 * key operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000155 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100156 * a default value, which can be overridden by calling specific
Ronald Cronc1905a12021-06-05 11:11:14 +0200157 * \c mbedtls_rsa_rsaes_xxx or \c mbedtls_rsa_rsassa_xxx
158 * functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200159 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000160 * \note The hash selected in \p hash_id is always used for OEAP
161 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100162 * making signatures, but can be overridden for verifying them.
163 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100164 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200165 * \param ctx The initialized RSA context to be configured.
Hanno Becker9a467772018-12-13 09:54:59 +0000166 * \param padding The padding mode to use. This must be either
167 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200168 * \param hash_id The hash identifier for PSS or OAEP, if \p padding is
169 * #MBEDTLS_RSA_PKCS_V21. #MBEDTLS_MD_NONE is accepted by this
170 * function but may be not suitable for some operations.
171 * Ignored if \p padding is #MBEDTLS_RSA_PKCS_V15.
Ronald Cronc1905a12021-06-05 11:11:14 +0200172 *
173 * \return \c 0 on success.
174 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING failure:
175 * \p padding or \p hash_id is invalid.
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 */
Ronald Cronc1905a12021-06-05 11:11:14 +0200177int mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
178 mbedtls_md_type_t hash_id );
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
180/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000181 * \brief This function imports a set of core parameters into an
182 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100183 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100184 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000185 * imports, if the parameters are not simultaneously present.
186 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100187 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000188 * by a call to mbedtls_rsa_complete(), which checks and
189 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100190 * public or private RSA key.
191 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000192 * \note See mbedtls_rsa_complete() for more information on which
193 * parameters are necessary to set up a private or public
194 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100195 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100196 * \note The imported parameters are copied and need not be preserved
197 * for the lifetime of the RSA context being set up.
198 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100199 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000200 * \param N The RSA modulus. This may be \c NULL.
201 * \param P The first prime factor of \p N. This may be \c NULL.
202 * \param Q The second prime factor of \p N. This may be \c NULL.
203 * \param D The private exponent. This may be \c NULL.
204 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100205 *
206 * \return \c 0 on success.
207 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100208 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100209int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
210 const mbedtls_mpi *N,
211 const mbedtls_mpi *P, const mbedtls_mpi *Q,
212 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100213
214/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000215 * \brief This function imports core RSA parameters, in raw big-endian
216 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100218 * \note This function can be called multiple times for successive
219 * imports, if the parameters are not simultaneously present.
220 *
221 * Any sequence of calls to this function should be followed
222 * by a call to mbedtls_rsa_complete(), which checks and
223 * completes the provided information to a ready-for-use
224 * public or private RSA key.
225 *
226 * \note See mbedtls_rsa_complete() for more information on which
227 * parameters are necessary to set up a private or public
228 * RSA key.
229 *
230 * \note The imported parameters are copied and need not be preserved
231 * for the lifetime of the RSA context being set up.
232 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000233 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000234 * \param N The RSA modulus. This may be \c NULL.
235 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
236 * \param P The first prime factor of \p N. This may be \c NULL.
237 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
238 * \param Q The second prime factor of \p N. This may be \c NULL.
239 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
240 * \param D The private exponent. This may be \c NULL.
241 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
242 * \param E The public exponent. This may be \c NULL.
243 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100245 * \return \c 0 on success.
246 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100247 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100248int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100249 unsigned char const *N, size_t N_len,
250 unsigned char const *P, size_t P_len,
251 unsigned char const *Q, size_t Q_len,
252 unsigned char const *D, size_t D_len,
253 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100254
255/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000256 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100257 * a set of imported core parameters.
258 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000259 * To setup an RSA public key, precisely \p N and \p E
260 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100261 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000262 * To setup an RSA private key, sufficient information must
263 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100264 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000265 * The default implementation supports the following:
266 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
267 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
268 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100269 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000270 * If this function runs successfully, it guarantees that
271 * the RSA context can be used for RSA operations without
272 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100273 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100274 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000275 * for the imported parameters. In particular, parameters that
276 * are not needed by the implementation might be silently
277 * discarded and left unchecked. To check the consistency
278 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100279 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100280 * \param ctx The initialized RSA context holding imported parameters.
281 *
282 * \return \c 0 on success.
283 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
284 * failed.
285 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100286 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100287int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100288
289/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000290 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100291 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000292 * If this function runs successfully, the non-NULL buffers
293 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
294 * written, with additional unused space filled leading by
295 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100296 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000297 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300298 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000299 * <li>An alternative RSA implementation is in use, which
300 * stores the key externally, and either cannot or should
301 * not export it into RAM.</li>
302 * <li>A SW or HW implementation might not support a certain
303 * deduction. For example, \p P, \p Q from \p N, \p D,
304 * and \p E if the former are not part of the
305 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100306 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000307 * If the function fails due to an unsupported operation,
308 * the RSA context stays intact and remains usable.
309 *
310 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000311 * \param N The MPI to hold the RSA modulus.
312 * This may be \c NULL if this field need not be exported.
313 * \param P The MPI to hold the first prime factor of \p N.
314 * This may be \c NULL if this field need not be exported.
315 * \param Q The MPI to hold the second prime factor of \p N.
316 * This may be \c NULL if this field need not be exported.
317 * \param D The MPI to hold the private exponent.
318 * This may be \c NULL if this field need not be exported.
319 * \param E The MPI to hold the public exponent.
320 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000321 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100322 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300323 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000324 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100325 * functionality or because of security policies.
326 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100327 *
328 */
329int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
330 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
331 mbedtls_mpi *D, mbedtls_mpi *E );
332
333/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000334 * \brief This function exports core parameters of an RSA key
335 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100336 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000337 * If this function runs successfully, the non-NULL buffers
338 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
339 * written, with additional unused space filled leading by
340 * zero Bytes.
341 *
342 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300343 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000344 * <li>An alternative RSA implementation is in use, which
345 * stores the key externally, and either cannot or should
346 * not export it into RAM.</li>
347 * <li>A SW or HW implementation might not support a certain
348 * deduction. For example, \p P, \p Q from \p N, \p D,
349 * and \p E if the former are not part of the
350 * implementation.</li></ul>
351 * If the function fails due to an unsupported operation,
352 * the RSA context stays intact and remains usable.
353 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100354 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100355 * buffer pointers are NULL.
356 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000357 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000358 * \param N The Byte array to store the RSA modulus,
359 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000360 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000361 * \param P The Byte array to hold the first prime factor of \p N,
362 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000363 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000364 * \param Q The Byte array to hold the second prime factor of \p N,
365 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000366 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000367 * \param D The Byte array to hold the private exponent,
368 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000369 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000370 * \param E The Byte array to hold the public exponent,
371 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000372 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100373 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100374 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300375 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000376 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100377 * functionality or because of security policies.
378 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100379 */
380int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
381 unsigned char *N, size_t N_len,
382 unsigned char *P, size_t P_len,
383 unsigned char *Q, size_t Q_len,
384 unsigned char *D, size_t D_len,
385 unsigned char *E, size_t E_len );
386
387/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000388 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100389 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100390 * \note Alternative RSA implementations not using CRT-parameters
391 * internally can implement this function based on
392 * mbedtls_rsa_deduce_opt().
393 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000394 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000395 * \param DP The MPI to hold \c D modulo `P-1`,
396 * or \c NULL if it need not be exported.
397 * \param DQ The MPI to hold \c D modulo `Q-1`,
398 * or \c NULL if it need not be exported.
399 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
400 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100401 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100402 * \return \c 0 on success.
403 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100404 *
405 */
406int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
407 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
408
Paul Bakker5121ce52009-01-03 21:22:43 +0000409/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000410 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100411 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000412 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100413 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000414 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100415 *
416 */
417size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
418
419/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000420 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000422 * \note mbedtls_rsa_init() must be called before this function,
423 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 *
Hanno Becker9a467772018-12-13 09:54:59 +0000425 * \param ctx The initialized RSA context used to hold the key.
426 * \param f_rng The RNG function to be used for key generation.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100427 * This is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000428 * \param p_rng The RNG context to be passed to \p f_rng.
429 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100430 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000431 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000432 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100433 *
434 * \return \c 0 on success.
435 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000436 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100438 int (*f_rng)(void *, unsigned char *, size_t),
439 void *p_rng,
440 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000441
442/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000443 * \brief This function checks if a context contains at least an RSA
444 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000445 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000446 * If the function runs successfully, it is guaranteed that
447 * enough information is present to perform an RSA public key
448 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000449 *
Hanno Becker9a467772018-12-13 09:54:59 +0000450 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000451 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100452 * \return \c 0 on success.
453 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100454 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000457
458/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000459 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100460 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 *
Hanno Becker68767a62017-10-17 10:13:31 +0100462 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000463 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100464 * on the given context, but that the various parameters are
465 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000466 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100467 *
468 * \warning This function should catch accidental misconfigurations
469 * like swapping of parameters, but it cannot establish full
470 * trust in neither the quality nor the consistency of the key
471 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000472 * <ul><li>Consistency: Imported parameters that are irrelevant
473 * for the implementation might be silently dropped. If dropped,
474 * the current function does not have access to them,
475 * and therefore cannot check them. See mbedtls_rsa_complete().
476 * If you want to check the consistency of the entire
477 * content of an PKCS1-encoded RSA private key, for example, you
478 * should use mbedtls_rsa_validate_params() before setting
479 * up the RSA context.
480 * Additionally, if the implementation performs empirical checks,
481 * these checks substantiate but do not guarantee consistency.</li>
482 * <li>Quality: This function is not expected to perform
483 * extended quality assessments like checking that the prime
484 * factors are safe. Additionally, it is the responsibility of the
485 * user to ensure the trustworthiness of the source of his RSA
486 * parameters, which goes beyond what is effectively checkable
487 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100488 *
Hanno Becker9a467772018-12-13 09:54:59 +0000489 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100490 *
491 * \return \c 0 on success.
492 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000497 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100498 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000499 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100500 *
Hanno Becker9a467772018-12-13 09:54:59 +0000501 * \param pub The initialized RSA context holding the public key.
502 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000503 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100504 * \return \c 0 on success.
505 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100506 */
Hanno Becker98838b02017-10-02 13:16:10 +0100507int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
508 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100509
510/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000511 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 *
Hanno Becker9a467772018-12-13 09:54:59 +0000513 * \param ctx The initialized RSA context to use.
514 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000515 * of length \c ctx->len Bytes. For example, \c 256 Bytes
516 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000517 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000518 * of length \c ctx->len Bytes. For example, \c 256 Bytes
519 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000520 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000521 * \note This function does not handle message padding.
522 *
523 * \note Make sure to set \p input[0] = 0 or ensure that
524 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000525 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100526 * \return \c 0 on success.
527 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000530 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 unsigned char *output );
532
533/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000534 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 *
Hanno Becker24120612017-10-26 11:53:35 +0100536 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100537 *
538 * \note If blinding is used, both the base of exponentation
Hanno Becker24120612017-10-26 11:53:35 +0100539 * and the exponent are blinded, providing protection
540 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100541 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100542 * \warning It is deprecated and a security risk to not provide
543 * a PRNG here and thereby prevent the use of blinding.
544 * Future versions of the library may enforce the presence
545 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100546 *
Hanno Becker9a467772018-12-13 09:54:59 +0000547 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100548 * \param f_rng The RNG function, used for blinding. It is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000549 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
Thomas Daubney03412782021-05-20 15:31:17 +0100550 * if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000551 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000552 * of length \c ctx->len Bytes. For example, \c 256 Bytes
553 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000554 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000555 * of length \c ctx->len Bytes. For example, \c 256 Bytes
556 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100557 *
558 * \return \c 0 on success.
559 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
560 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200563 int (*f_rng)(void *, unsigned char *, size_t),
564 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000565 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 unsigned char *output );
567
568/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000569 * \brief This function adds the message padding, then performs an RSA
570 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000571 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000572 * It is the generic wrapper for performing a PKCS#1 encryption
Thomas Daubney21772772021-05-13 17:30:32 +0100573 * operation.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100574 *
Hanno Becker9a467772018-12-13 09:54:59 +0000575 * \param ctx The initialized RSA context to use.
Thomas Daubneyf54c5c52021-05-21 17:00:30 +0100576 * \param f_rng The RNG to use. It is used for padding generation
Thomas Daubney2c65db92021-05-21 10:58:28 +0100577 * and it is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000578 * \param p_rng The RNG context to be passed to \p f_rng. May be
Thomas Daubney03412782021-05-20 15:31:17 +0100579 * \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000580 * \param ilen The length of the plaintext in Bytes.
581 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000582 * buffer of size \p ilen Bytes. It may be \c NULL if
583 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000584 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000585 * of length \c ctx->len Bytes. For example, \c 256 Bytes
586 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100587 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100588 * \return \c 0 on success.
589 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000592 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000593 void *p_rng,
Thomas Daubney21772772021-05-13 17:30:32 +0100594 size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000595 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000596 unsigned char *output );
597
598/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000599 * \brief This function performs a PKCS#1 v1.5 encryption operation
600 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100601 *
Hanno Becker9a467772018-12-13 09:54:59 +0000602 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100603 * \param f_rng The RNG function to use. It is mandatory and used for
604 * padding generation.
Hanno Becker9a467772018-12-13 09:54:59 +0000605 * \param p_rng The RNG context to be passed to \p f_rng. This may
Thomas Daubney03412782021-05-20 15:31:17 +0100606 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000607 * \param ilen The length of the plaintext in Bytes.
608 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000609 * buffer of size \p ilen Bytes. It may be \c NULL if
610 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000611 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000612 * of length \c ctx->len Bytes. For example, \c 256 Bytes
613 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100614 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100615 * \return \c 0 on success.
616 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100619 int (*f_rng)(void *, unsigned char *, size_t),
620 void *p_rng,
Thomas Daubney53e4ac62021-05-13 18:26:49 +0100621 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100622 const unsigned char *input,
623 unsigned char *output );
624
625/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000626 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
627 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100628 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100629 * \note The output buffer must be as large as the size
630 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
631 *
Hanno Becker9a467772018-12-13 09:54:59 +0000632 * \param ctx The initnialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000633 * \param f_rng The RNG function to use. This is needed for padding
Thomas Daubney2c65db92021-05-21 10:58:28 +0100634 * generation and is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000635 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000636 * be \c NULL if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000637 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000638 * This must be a readable buffer of length \p label_len
639 * Bytes. It may be \c NULL if \p label_len is \c 0.
640 * \param label_len The length of the label in Bytes.
641 * \param ilen The length of the plaintext buffer \p input in Bytes.
642 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000643 * buffer of size \p ilen Bytes. It may be \c NULL if
644 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000645 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000646 * of length \c ctx->len Bytes. For example, \c 256 Bytes
647 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100648 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100649 * \return \c 0 on success.
650 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100651 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100653 int (*f_rng)(void *, unsigned char *, size_t),
654 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100655 const unsigned char *label, size_t label_len,
656 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100657 const unsigned char *input,
658 unsigned char *output );
659
660/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000661 * \brief This function performs an RSA operation, then removes the
662 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000664 * It is the generic wrapper for performing a PKCS#1 decryption
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100665 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100667 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000668 * as large as the size \p ctx->len of \p ctx->N (for example,
669 * 128 Bytes if RSA-1024 is used) to be able to hold an
670 * arbitrary decrypted message. If it is not large enough to
671 * hold the decryption of the particular ciphertext provided,
672 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100673 *
Hanno Becker9a467772018-12-13 09:54:59 +0000674 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100675 * \param f_rng The RNG function. This is used for blinding and is
676 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000677 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100678 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000679 * \param olen The address at which to store the length of
680 * the plaintext. This must not be \c NULL.
681 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000682 * of length \c ctx->len Bytes. For example, \c 256 Bytes
683 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000684 * \param output The buffer used to hold the plaintext. This must
685 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000686 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100687 *
688 * \return \c 0 on success.
689 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000690 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200692 int (*f_rng)(void *, unsigned char *, size_t),
693 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100694 size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000695 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000696 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000697 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000698
699/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000700 * \brief This function performs a PKCS#1 v1.5 decryption
701 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100702 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100703 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000704 * as large as the size \p ctx->len of \p ctx->N, for example,
705 * 128 Bytes if RSA-1024 is used, to be able to hold an
706 * arbitrary decrypted message. If it is not large enough to
707 * hold the decryption of the particular ciphertext provided,
708 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100709 *
Hanno Becker9a467772018-12-13 09:54:59 +0000710 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100711 * \param f_rng The RNG function. This is used for blinding and is
712 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000713 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100714 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000715 * \param olen The address at which to store the length of
716 * the plaintext. This must not be \c NULL.
717 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000718 * of length \c ctx->len Bytes. For example, \c 256 Bytes
719 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000720 * \param output The buffer used to hold the plaintext. This must
721 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000722 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100723 *
724 * \return \c 0 on success.
725 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
726 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200729 int (*f_rng)(void *, unsigned char *, size_t),
730 void *p_rng,
Thomas Daubney34733082021-05-12 09:24:29 +0100731 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100732 const unsigned char *input,
733 unsigned char *output,
734 size_t output_max_len );
735
736/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100737 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
738 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100739 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100740 * \note The output buffer length \c output_max_len should be
741 * as large as the size \p ctx->len of \p ctx->N, for
742 * example, 128 Bytes if RSA-1024 is used, to be able to
743 * hold an arbitrary decrypted message. If it is not
744 * large enough to hold the decryption of the particular
745 * ciphertext provided, the function returns
746 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100747 *
Hanno Becker9a467772018-12-13 09:54:59 +0000748 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100749 * \param f_rng The RNG function. This is used for blinding and is
750 * mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000751 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100752 * \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100753 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000754 * This must be a readable buffer of length \p label_len
755 * Bytes. It may be \c NULL if \p label_len is \c 0.
756 * \param label_len The length of the label in Bytes.
757 * \param olen The address at which to store the length of
758 * the plaintext. This must not be \c NULL.
759 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000760 * of length \c ctx->len Bytes. For example, \c 256 Bytes
761 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000762 * \param output The buffer used to hold the plaintext. This must
763 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000764 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100765 *
766 * \return \c 0 on success.
767 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100768 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200770 int (*f_rng)(void *, unsigned char *, size_t),
771 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100772 const unsigned char *label, size_t label_len,
773 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100774 const unsigned char *input,
775 unsigned char *output,
776 size_t output_max_len );
777
778/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000779 * \brief This function performs a private RSA operation to sign
780 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000781 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000782 * It is the generic wrapper for performing a PKCS#1
Thomas Daubney140184d2021-05-18 16:04:07 +0100783 * signature.
Paul Bakker5121ce52009-01-03 21:22:43 +0000784 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000785 * \note The \p sig buffer must be as large as the size
786 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000787 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000788 * \note For PKCS#1 v2.1 encoding, see comments on
789 * mbedtls_rsa_rsassa_pss_sign() for details on
790 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100791 *
Hanno Becker9a467772018-12-13 09:54:59 +0000792 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100793 * \param f_rng The RNG function to use. This is mandatory and
794 * must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000795 * \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 +0100796 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100797 * \param md_alg The message-digest algorithm used to hash the original data.
798 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000799 * \param hashlen The length of the message digest.
800 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
801 * \param hash The buffer holding the message digest or raw data.
802 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
803 * buffer of length \p hashlen Bytes. If \p md_alg is not
804 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
805 * the size of the hash corresponding to \p md_alg.
806 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000807 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100808 * for an 2048-bit RSA modulus. A buffer length of
809 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100810 *
811 * \return \c 0 if the signing operation was successful.
812 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000813 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000815 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000816 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000818 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000819 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000820 unsigned char *sig );
821
822/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000823 * \brief This function performs a PKCS#1 v1.5 signature
824 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100825 *
Hanno Becker9a467772018-12-13 09:54:59 +0000826 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100827 * \param f_rng The RNG function. This is used for blinding and is
828 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000829 * \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 +0100830 * if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000831 * \param md_alg The message-digest algorithm used to hash the original data.
832 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000833 * \param hashlen The length of the message digest.
834 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
835 * \param hash The buffer holding the message digest or raw data.
836 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
837 * buffer of length \p hashlen Bytes. If \p md_alg is not
838 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
839 * the size of the hash corresponding to \p md_alg.
840 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000841 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100842 * for an 2048-bit RSA modulus. A buffer length of
843 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100844 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100845 * \return \c 0 if the signing operation was successful.
846 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200849 int (*f_rng)(void *, unsigned char *, size_t),
850 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100852 unsigned int hashlen,
853 const unsigned char *hash,
854 unsigned char *sig );
855
856/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000857 * \brief This function performs a PKCS#1 v2.1 PSS signature
858 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100859 *
Janos Follathb7953322021-04-01 14:44:17 +0100860 * \note The \c hash_id set in \p ctx by calling
861 * mbedtls_rsa_set_padding() selects the hash used for the
862 * encoding operation and for the mask generation function
863 * (MGF1). For more details on the encoding operation and the
864 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +0000865 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100866 * Specifications</em>.
Rose Zadike8b5b992018-03-27 12:19:47 +0100867 *
Cédric Meuter010ddc22020-04-25 09:24:11 +0200868 * \note This function enforces that the provided salt length complies
869 * with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 v2.2) §9.1.1
870 * step 3. The constraint is that the hash length plus the salt
871 * length plus 2 bytes must be at most the key length. If this
872 * constraint is not met, this function returns
Jaeden Amero3725bb22018-09-07 19:12:36 +0100873 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
874 *
Hanno Becker9a467772018-12-13 09:54:59 +0000875 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100876 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000877 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
878 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100879 * \param md_alg The message-digest algorithm used to hash the original data.
880 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000881 * \param hashlen The length of the message digest.
882 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
883 * \param hash The buffer holding the message digest or raw data.
884 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
885 * buffer of length \p hashlen Bytes. If \p md_alg is not
886 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
887 * the size of the hash corresponding to \p md_alg.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200888 * \param saltlen The length of the salt that should be used.
Cédric Meuter010ddc22020-04-25 09:24:11 +0200889 * If passed #MBEDTLS_RSA_SALT_LEN_ANY, the function will use
890 * the largest possible salt length up to the hash length,
891 * which is the largest permitted by some standards including
892 * FIPS 186-4 §5.5.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200893 * \param sig The buffer to hold the signature. This must be a writable
894 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
895 * for an 2048-bit RSA modulus. A buffer length of
896 * #MBEDTLS_MPI_MAX_SIZE is always safe.
897 *
898 * \return \c 0 if the signing operation was successful.
899 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
900 */
901int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
902 int (*f_rng)(void *, unsigned char *, size_t),
903 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200904 mbedtls_md_type_t md_alg,
905 unsigned int hashlen,
906 const unsigned char *hash,
907 int saltlen,
908 unsigned char *sig );
909
910/**
911 * \brief This function performs a PKCS#1 v2.1 PSS signature
912 * operation (RSASSA-PSS-SIGN).
913 *
Janos Follathb7953322021-04-01 14:44:17 +0100914 * \note The \c hash_id set in \p ctx by calling
915 * mbedtls_rsa_set_padding() selects the hash used for the
916 * encoding operation and for the mask generation function
917 * (MGF1). For more details on the encoding operation and the
918 * mask generation function, consult <em>RFC-3447: Public-Key
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200919 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100920 * Specifications</em>.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200921 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000922 * \note This function always uses the maximum possible salt size,
923 * up to the length of the payload hash. This choice of salt
924 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
925 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
Rose Zadike8b5b992018-03-27 12:19:47 +0100926 * minimum salt size which is the hash size minus 2 bytes. If
927 * this minimum size is too large given the key size (the salt
928 * size, plus the hash size, plus 2 bytes must be no more than
929 * the key size in bytes), this function returns
930 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
931 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100932 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100933 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100934 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
935 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100936 * \param md_alg The message-digest algorithm used to hash the original data.
937 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000938 * \param hashlen The length of the message digest.
Janos Follathb7953322021-04-01 14:44:17 +0100939 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
Hanno Becker9a467772018-12-13 09:54:59 +0000940 * \param hash The buffer holding the message digest or raw data.
941 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
942 * buffer of length \p hashlen Bytes. If \p md_alg is not
943 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
944 * the size of the hash corresponding to \p md_alg.
945 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000946 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100947 * for an 2048-bit RSA modulus. A buffer length of
948 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100949 *
950 * \return \c 0 if the signing operation was successful.
951 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100952 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100954 int (*f_rng)(void *, unsigned char *, size_t),
955 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100957 unsigned int hashlen,
958 const unsigned char *hash,
959 unsigned char *sig );
960
961/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000962 * \brief This function performs a public RSA operation and checks
963 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +0000964 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000965 * This is the generic wrapper for performing a PKCS#1
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100966 * verification.
Paul Bakker5121ce52009-01-03 21:22:43 +0000967 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000968 * \note For PKCS#1 v2.1 encoding, see comments on
969 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
970 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100971 *
Hanno Becker9a467772018-12-13 09:54:59 +0000972 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +0100973 * \param md_alg The message-digest algorithm used to hash the original data.
974 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000975 * \param hashlen The length of the message digest.
976 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
977 * \param hash The buffer holding the message digest or raw data.
978 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
979 * buffer of length \p hashlen Bytes. If \p md_alg is not
980 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
981 * the size of the hash corresponding to \p md_alg.
982 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +0000983 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
984 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100985 *
986 * \return \c 0 if the verify operation was successful.
987 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000991 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000992 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200993 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000994
995/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000996 * \brief This function performs a PKCS#1 v1.5 verification
997 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +0100998 *
Hanno Becker9a467772018-12-13 09:54:59 +0000999 * \param ctx The initialized RSA public key context to use.
Rose Zadik042e97f2018-01-26 16:35:10 +00001000 * \param md_alg The message-digest algorithm used to hash the original data.
1001 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001002 * \param hashlen The length of the message digest.
1003 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1004 * \param hash The buffer holding the message digest or raw data.
1005 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1006 * buffer of length \p hashlen Bytes. If \p md_alg is not
1007 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1008 * the size of the hash corresponding to \p md_alg.
1009 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001010 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1011 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001012 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001013 * \return \c 0 if the verify operation was successful.
1014 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001018 unsigned int hashlen,
1019 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001020 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001021
1022/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001023 * \brief This function performs a PKCS#1 v2.1 PSS verification
1024 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001025 *
Janos Follathb7953322021-04-01 14:44:17 +01001026 * \note The \c hash_id set in \p ctx by calling
1027 * mbedtls_rsa_set_padding() selects the hash used for the
1028 * encoding operation and for the mask generation function
1029 * (MGF1). For more details on the encoding operation and the
1030 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +00001031 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +01001032 * Specifications</em>. If the \c hash_id set in \p ctx by
1033 * mbedtls_rsa_set_padding() is #MBEDTLS_MD_NONE, the \p md_alg
1034 * parameter is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001035 *
Hanno Becker9a467772018-12-13 09:54:59 +00001036 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001037 * \param md_alg The message-digest algorithm used to hash the original data.
1038 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001039 * \param hashlen The length of the message digest.
1040 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1041 * \param hash The buffer holding the message digest or raw data.
1042 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1043 * buffer of length \p hashlen Bytes. If \p md_alg is not
1044 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1045 * the size of the hash corresponding to \p md_alg.
1046 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001047 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1048 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001049 *
1050 * \return \c 0 if the verify operation was successful.
1051 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001052 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001055 unsigned int hashlen,
1056 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001057 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001058
1059/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001060 * \brief This function performs a PKCS#1 v2.1 PSS verification
1061 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001062 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001063 * \note The \p sig buffer must be as large as the size
1064 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001065 *
Janos Follathb7953322021-04-01 14:44:17 +01001066 * \note The \c hash_id set in \p ctx by mbedtls_rsa_set_padding() is
1067 * ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001068 *
Hanno Becker9a467772018-12-13 09:54:59 +00001069 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001070 * \param md_alg The message-digest algorithm used to hash the original data.
1071 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001072 * \param hashlen The length of the message digest.
1073 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1074 * \param hash The buffer holding the message digest or raw data.
1075 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1076 * buffer of length \p hashlen Bytes. If \p md_alg is not
1077 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1078 * the size of the hash corresponding to \p md_alg.
Janos Follathb7953322021-04-01 14:44:17 +01001079 * \param mgf1_hash_id The message digest algorithm used for the
1080 * verification operation and the mask generation
1081 * function (MGF1). For more details on the encoding
1082 * operation and the mask generation function, consult
1083 * <em>RFC-3447: Public-Key Cryptography Standards
1084 * (PKCS) #1 v2.1: RSA Cryptography
1085 * Specifications</em>.
Hanno Becker9a467772018-12-13 09:54:59 +00001086 * \param expected_salt_len The length of the salt used in padding. Use
1087 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1088 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001089 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1090 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001091 *
1092 * \return \c 0 if the verify operation was successful.
1093 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001097 unsigned int hashlen,
1098 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001100 int expected_salt_len,
1101 const unsigned char *sig );
1102
1103/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001104 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001105 *
Hanno Becker9a467772018-12-13 09:54:59 +00001106 * \param dst The destination context. This must be initialized.
1107 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001108 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001109 * \return \c 0 on success.
1110 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001113
1114/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001115 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001116 *
Hanno Becker9a467772018-12-13 09:54:59 +00001117 * \param ctx The RSA context to free. May be \c NULL, in which case
1118 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001119 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001122
Ron Eldorfa8f6352017-06-20 15:48:46 +03001123#if defined(MBEDTLS_SELF_TEST)
1124
Paul Bakker5121ce52009-01-03 21:22:43 +00001125/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001126 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001127 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001128 * \return \c 0 on success.
1129 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001132
Ron Eldorfa8f6352017-06-20 15:48:46 +03001133#endif /* MBEDTLS_SELF_TEST */
1134
Paul Bakker5121ce52009-01-03 21:22:43 +00001135#ifdef __cplusplus
1136}
1137#endif
1138
Paul Bakker5121ce52009-01-03 21:22:43 +00001139#endif /* rsa.h */