blob: 08db213f45f202b0429719b261525ee55e75170f [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.
Hanno Becker5063cd22017-09-29 11:49:12 +010085 *
86 * \note Direct manipulation of the members of this structure
Rose Zadik042e97f2018-01-26 16:35:10 +000087 * is deprecated. All manipulation should instead be done through
88 * the public interface functions.
Paul Bakker5121ce52009-01-03 21:22:43 +000089 */
Dawid Drozd428cc522018-07-24 10:02:47 +020090typedef struct mbedtls_rsa_context
Paul Bakker5121ce52009-01-03 21:22:43 +000091{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020092 int MBEDTLS_PRIVATE(ver); /*!< Reserved for internal purposes.
Gilles Peskine4337a9c2021-02-09 18:59:42 +010093 * Do not set this field in application
94 * code. Its meaning might change without
95 * notice. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020096 size_t MBEDTLS_PRIVATE(len); /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +000097
Mateusz Starzyk846f0212021-05-19 19:44:07 +020098 mbedtls_mpi MBEDTLS_PRIVATE(N); /*!< The public modulus. */
99 mbedtls_mpi MBEDTLS_PRIVATE(E); /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200101 mbedtls_mpi MBEDTLS_PRIVATE(D); /*!< The private exponent. */
102 mbedtls_mpi MBEDTLS_PRIVATE(P); /*!< The first prime factor. */
103 mbedtls_mpi MBEDTLS_PRIVATE(Q); /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100104
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200105 mbedtls_mpi MBEDTLS_PRIVATE(DP); /*!< <code>D % (P - 1)</code>. */
106 mbedtls_mpi MBEDTLS_PRIVATE(DQ); /*!< <code>D % (Q - 1)</code>. */
107 mbedtls_mpi MBEDTLS_PRIVATE(QP); /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200109 mbedtls_mpi MBEDTLS_PRIVATE(RN); /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200111 mbedtls_mpi MBEDTLS_PRIVATE(RP); /*!< cached <code>R^2 mod P</code>. */
112 mbedtls_mpi MBEDTLS_PRIVATE(RQ); /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200113
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200114 mbedtls_mpi MBEDTLS_PRIVATE(Vi); /*!< The cached blinding value. */
115 mbedtls_mpi MBEDTLS_PRIVATE(Vf); /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000116
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200117 int MBEDTLS_PRIVATE(padding); /*!< Selects padding mode:
Rose Zadik042e97f2018-01-26 16:35:10 +0000118 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
119 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200120 int MBEDTLS_PRIVATE(hash_id); /*!< Hash identifier of mbedtls_md_type_t type,
Rose Zadik042e97f2018-01-26 16:35:10 +0000121 as specified in md.h for use in the MGF
122 mask generating function used in the
123 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100125 /* Invariant: the mutex is initialized iff ver != 0. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200126 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex); /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200127#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000128}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200131#else /* MBEDTLS_RSA_ALT */
132#include "rsa_alt.h"
133#endif /* MBEDTLS_RSA_ALT */
134
Paul Bakker5121ce52009-01-03 21:22:43 +0000135/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000136 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000138 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000139 * encryption scheme and the RSASSA-PSS signature scheme.
140 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000141 * \note The \p hash_id parameter is ignored when using
142 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200143 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000144 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200145 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000146 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100147 * a default value, which can be overridden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000148 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200149 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000150 * \note The hash selected in \p hash_id is always used for OEAP
151 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100152 * making signatures, but can be overridden for verifying them.
153 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100154 *
Hanno Becker9a467772018-12-13 09:54:59 +0000155 * \param ctx The RSA context to initialize. This must not be \c NULL.
156 * \param padding The padding mode to use. This must be either
157 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Hanno Becker385ce912018-12-13 18:33:12 +0000158 * \param hash_id The hash identifier of ::mbedtls_md_type_t type, if
Hanno Becker9a467772018-12-13 09:54:59 +0000159 * \p padding is #MBEDTLS_RSA_PKCS_V21. It is unused
160 * otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100163 int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000164 int hash_id );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
166/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000167 * \brief This function imports a set of core parameters into an
168 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100169 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100170 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000171 * imports, if the parameters are not simultaneously present.
172 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100173 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000174 * by a call to mbedtls_rsa_complete(), which checks and
175 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100176 * public or private RSA key.
177 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000178 * \note See mbedtls_rsa_complete() for more information on which
179 * parameters are necessary to set up a private or public
180 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100181 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100182 * \note The imported parameters are copied and need not be preserved
183 * for the lifetime of the RSA context being set up.
184 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100185 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000186 * \param N The RSA modulus. This may be \c NULL.
187 * \param P The first prime factor of \p N. This may be \c NULL.
188 * \param Q The second prime factor of \p N. This may be \c NULL.
189 * \param D The private exponent. This may be \c NULL.
190 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100191 *
192 * \return \c 0 on success.
193 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100194 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100195int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
196 const mbedtls_mpi *N,
197 const mbedtls_mpi *P, const mbedtls_mpi *Q,
198 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100199
200/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000201 * \brief This function imports core RSA parameters, in raw big-endian
202 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100204 * \note This function can be called multiple times for successive
205 * imports, if the parameters are not simultaneously present.
206 *
207 * Any sequence of calls to this function should be followed
208 * by a call to mbedtls_rsa_complete(), which checks and
209 * completes the provided information to a ready-for-use
210 * public or private RSA key.
211 *
212 * \note See mbedtls_rsa_complete() for more information on which
213 * parameters are necessary to set up a private or public
214 * RSA key.
215 *
216 * \note The imported parameters are copied and need not be preserved
217 * for the lifetime of the RSA context being set up.
218 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000219 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000220 * \param N The RSA modulus. This may be \c NULL.
221 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
222 * \param P The first prime factor of \p N. This may be \c NULL.
223 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
224 * \param Q The second prime factor of \p N. This may be \c NULL.
225 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
226 * \param D The private exponent. This may be \c NULL.
227 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
228 * \param E The public exponent. This may be \c NULL.
229 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100231 * \return \c 0 on success.
232 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100233 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100234int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100235 unsigned char const *N, size_t N_len,
236 unsigned char const *P, size_t P_len,
237 unsigned char const *Q, size_t Q_len,
238 unsigned char const *D, size_t D_len,
239 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100240
241/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000242 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100243 * a set of imported core parameters.
244 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000245 * To setup an RSA public key, precisely \p N and \p E
246 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100247 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000248 * To setup an RSA private key, sufficient information must
249 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100250 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000251 * The default implementation supports the following:
252 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
253 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
254 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100255 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000256 * If this function runs successfully, it guarantees that
257 * the RSA context can be used for RSA operations without
258 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100259 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100260 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000261 * for the imported parameters. In particular, parameters that
262 * are not needed by the implementation might be silently
263 * discarded and left unchecked. To check the consistency
264 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100265 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100266 * \param ctx The initialized RSA context holding imported parameters.
267 *
268 * \return \c 0 on success.
269 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
270 * failed.
271 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100272 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100273int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100274
275/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000276 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100277 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000278 * If this function runs successfully, the non-NULL buffers
279 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
280 * written, with additional unused space filled leading by
281 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100282 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000283 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300284 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000285 * <li>An alternative RSA implementation is in use, which
286 * stores the key externally, and either cannot or should
287 * not export it into RAM.</li>
288 * <li>A SW or HW implementation might not support a certain
289 * deduction. For example, \p P, \p Q from \p N, \p D,
290 * and \p E if the former are not part of the
291 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100292 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000293 * If the function fails due to an unsupported operation,
294 * the RSA context stays intact and remains usable.
295 *
296 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000297 * \param N The MPI to hold the RSA modulus.
298 * This may be \c NULL if this field need not be exported.
299 * \param P The MPI to hold the first prime factor of \p N.
300 * This may be \c NULL if this field need not be exported.
301 * \param Q The MPI to hold the second prime factor of \p N.
302 * This may be \c NULL if this field need not be exported.
303 * \param D The MPI to hold the private exponent.
304 * This may be \c NULL if this field need not be exported.
305 * \param E The MPI to hold the public exponent.
306 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000307 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100308 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300309 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000310 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100311 * functionality or because of security policies.
312 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100313 *
314 */
315int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
316 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
317 mbedtls_mpi *D, mbedtls_mpi *E );
318
319/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000320 * \brief This function exports core parameters of an RSA key
321 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100322 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000323 * If this function runs successfully, the non-NULL buffers
324 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
325 * written, with additional unused space filled leading by
326 * zero Bytes.
327 *
328 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300329 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000330 * <li>An alternative RSA implementation is in use, which
331 * stores the key externally, and either cannot or should
332 * not export it into RAM.</li>
333 * <li>A SW or HW implementation might not support a certain
334 * deduction. For example, \p P, \p Q from \p N, \p D,
335 * and \p E if the former are not part of the
336 * implementation.</li></ul>
337 * If the function fails due to an unsupported operation,
338 * the RSA context stays intact and remains usable.
339 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100340 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100341 * buffer pointers are NULL.
342 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000343 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000344 * \param N The Byte array to store the RSA modulus,
345 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000346 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000347 * \param P The Byte array to hold the first prime factor of \p N,
348 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000349 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000350 * \param Q The Byte array to hold the second prime factor of \p N,
351 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000352 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000353 * \param D The Byte array to hold the private exponent,
354 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000355 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000356 * \param E The Byte array to hold the public exponent,
357 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000358 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100359 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100360 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300361 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000362 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100363 * functionality or because of security policies.
364 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100365 */
366int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
367 unsigned char *N, size_t N_len,
368 unsigned char *P, size_t P_len,
369 unsigned char *Q, size_t Q_len,
370 unsigned char *D, size_t D_len,
371 unsigned char *E, size_t E_len );
372
373/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000374 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100375 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100376 * \note Alternative RSA implementations not using CRT-parameters
377 * internally can implement this function based on
378 * mbedtls_rsa_deduce_opt().
379 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000380 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000381 * \param DP The MPI to hold \c D modulo `P-1`,
382 * or \c NULL if it need not be exported.
383 * \param DQ The MPI to hold \c D modulo `Q-1`,
384 * or \c NULL if it need not be exported.
385 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
386 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100387 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100388 * \return \c 0 on success.
389 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100390 *
391 */
392int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
393 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
394
Paul Bakker5121ce52009-01-03 21:22:43 +0000395/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000396 * \brief This function sets padding for an already initialized RSA
397 * context. See mbedtls_rsa_init() for details.
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 *
Hanno Becker9a467772018-12-13 09:54:59 +0000399 * \param ctx The initialized RSA context to be configured.
400 * \param padding The padding mode to use. This must be either
401 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Rose Zadik042e97f2018-01-26 16:35:10 +0000402 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Paul Bakker40e46942009-01-03 21:51:57 +0000403 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100404void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000405 int hash_id );
Paul Bakker21eb2802010-08-16 11:10:02 +0000406
Paul Bakkera3d195c2011-11-27 21:07:34 +0000407/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000408 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100409 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000410 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100411 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000412 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100413 *
414 */
415size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
416
417/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000418 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000419 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000420 * \note mbedtls_rsa_init() must be called before this function,
421 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 *
Hanno Becker9a467772018-12-13 09:54:59 +0000423 * \param ctx The initialized RSA context used to hold the key.
424 * \param f_rng The RNG function to be used for key generation.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100425 * This is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000426 * \param p_rng The RNG context to be passed to \p f_rng.
427 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100428 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000429 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000430 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100431 *
432 * \return \c 0 on success.
433 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100436 int (*f_rng)(void *, unsigned char *, size_t),
437 void *p_rng,
438 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000439
440/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000441 * \brief This function checks if a context contains at least an RSA
442 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000443 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000444 * If the function runs successfully, it is guaranteed that
445 * enough information is present to perform an RSA public key
446 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000447 *
Hanno Becker9a467772018-12-13 09:54:59 +0000448 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000449 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100450 * \return \c 0 on success.
451 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100452 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000455
456/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000457 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100458 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 *
Hanno Becker68767a62017-10-17 10:13:31 +0100460 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000461 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100462 * on the given context, but that the various parameters are
463 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000464 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100465 *
466 * \warning This function should catch accidental misconfigurations
467 * like swapping of parameters, but it cannot establish full
468 * trust in neither the quality nor the consistency of the key
469 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000470 * <ul><li>Consistency: Imported parameters that are irrelevant
471 * for the implementation might be silently dropped. If dropped,
472 * the current function does not have access to them,
473 * and therefore cannot check them. See mbedtls_rsa_complete().
474 * If you want to check the consistency of the entire
475 * content of an PKCS1-encoded RSA private key, for example, you
476 * should use mbedtls_rsa_validate_params() before setting
477 * up the RSA context.
478 * Additionally, if the implementation performs empirical checks,
479 * these checks substantiate but do not guarantee consistency.</li>
480 * <li>Quality: This function is not expected to perform
481 * extended quality assessments like checking that the prime
482 * factors are safe. Additionally, it is the responsibility of the
483 * user to ensure the trustworthiness of the source of his RSA
484 * parameters, which goes beyond what is effectively checkable
485 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100486 *
Hanno Becker9a467772018-12-13 09:54:59 +0000487 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100488 *
489 * \return \c 0 on success.
490 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
494/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000495 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100496 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000497 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100498 *
Hanno Becker9a467772018-12-13 09:54:59 +0000499 * \param pub The initialized RSA context holding the public key.
500 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000501 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100502 * \return \c 0 on success.
503 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100504 */
Hanno Becker98838b02017-10-02 13:16:10 +0100505int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
506 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100507
508/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000509 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 *
Hanno Becker9a467772018-12-13 09:54:59 +0000511 * \param ctx The initialized RSA context to use.
512 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000513 * of length \c ctx->len Bytes. For example, \c 256 Bytes
514 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000515 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000516 * of length \c ctx->len Bytes. For example, \c 256 Bytes
517 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000518 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000519 * \note This function does not handle message padding.
520 *
521 * \note Make sure to set \p input[0] = 0 or ensure that
522 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100524 * \return \c 0 on success.
525 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000528 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 unsigned char *output );
530
531/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000532 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 *
Hanno Becker24120612017-10-26 11:53:35 +0100534 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100535 *
536 * \note If blinding is used, both the base of exponentation
Hanno Becker24120612017-10-26 11:53:35 +0100537 * and the exponent are blinded, providing protection
538 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100539 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100540 * \warning It is deprecated and a security risk to not provide
541 * a PRNG here and thereby prevent the use of blinding.
542 * Future versions of the library may enforce the presence
543 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100544 *
Hanno Becker9a467772018-12-13 09:54:59 +0000545 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100546 * \param f_rng The RNG function, used for blinding. It is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000547 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
Thomas Daubney03412782021-05-20 15:31:17 +0100548 * if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000549 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000550 * of length \c ctx->len Bytes. For example, \c 256 Bytes
551 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000552 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000553 * of length \c ctx->len Bytes. For example, \c 256 Bytes
554 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100555 *
556 * \return \c 0 on success.
557 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
558 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200561 int (*f_rng)(void *, unsigned char *, size_t),
562 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000563 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 unsigned char *output );
565
566/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000567 * \brief This function adds the message padding, then performs an RSA
568 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000570 * It is the generic wrapper for performing a PKCS#1 encryption
Thomas Daubney21772772021-05-13 17:30:32 +0100571 * operation.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100572 *
Hanno Becker9a467772018-12-13 09:54:59 +0000573 * \param ctx The initialized RSA context to use.
Thomas Daubneyf54c5c52021-05-21 17:00:30 +0100574 * \param f_rng The RNG to use. It is used for padding generation
Thomas Daubney2c65db92021-05-21 10:58:28 +0100575 * and it is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000576 * \param p_rng The RNG context to be passed to \p f_rng. May be
Thomas Daubney03412782021-05-20 15:31:17 +0100577 * \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000578 * \param ilen The length of the plaintext in Bytes.
579 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000580 * buffer of size \p ilen Bytes. It may be \c NULL if
581 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000582 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000583 * of length \c ctx->len Bytes. For example, \c 256 Bytes
584 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100585 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100586 * \return \c 0 on success.
587 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000590 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000591 void *p_rng,
Thomas Daubney21772772021-05-13 17:30:32 +0100592 size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000593 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 unsigned char *output );
595
596/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000597 * \brief This function performs a PKCS#1 v1.5 encryption operation
598 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100599 *
Hanno Becker9a467772018-12-13 09:54:59 +0000600 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100601 * \param f_rng The RNG function to use. It is mandatory and used for
602 * padding generation.
Hanno Becker9a467772018-12-13 09:54:59 +0000603 * \param p_rng The RNG context to be passed to \p f_rng. This may
Thomas Daubney03412782021-05-20 15:31:17 +0100604 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000605 * \param ilen The length of the plaintext in Bytes.
606 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000607 * buffer of size \p ilen Bytes. It may be \c NULL if
608 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000609 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000610 * of length \c ctx->len Bytes. For example, \c 256 Bytes
611 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100612 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100613 * \return \c 0 on success.
614 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100615 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100617 int (*f_rng)(void *, unsigned char *, size_t),
618 void *p_rng,
Thomas Daubney53e4ac62021-05-13 18:26:49 +0100619 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100620 const unsigned char *input,
621 unsigned char *output );
622
623/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000624 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
625 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100626 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100627 * \note The output buffer must be as large as the size
628 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
629 *
Hanno Becker9a467772018-12-13 09:54:59 +0000630 * \param ctx The initnialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000631 * \param f_rng The RNG function to use. This is needed for padding
Thomas Daubney2c65db92021-05-21 10:58:28 +0100632 * generation and is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000633 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000634 * be \c NULL if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000635 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000636 * This must be a readable buffer of length \p label_len
637 * Bytes. It may be \c NULL if \p label_len is \c 0.
638 * \param label_len The length of the label in Bytes.
639 * \param ilen The length of the plaintext buffer \p input in Bytes.
640 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000641 * buffer of size \p ilen Bytes. It may be \c NULL if
642 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000643 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000644 * of length \c ctx->len Bytes. For example, \c 256 Bytes
645 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100646 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100647 * \return \c 0 on success.
648 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100649 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100651 int (*f_rng)(void *, unsigned char *, size_t),
652 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100653 const unsigned char *label, size_t label_len,
654 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100655 const unsigned char *input,
656 unsigned char *output );
657
658/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000659 * \brief This function performs an RSA operation, then removes the
660 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000662 * It is the generic wrapper for performing a PKCS#1 decryption
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100663 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000664 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100665 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000666 * as large as the size \p ctx->len of \p ctx->N (for example,
667 * 128 Bytes if RSA-1024 is used) to be able to hold an
668 * arbitrary decrypted message. If it is not large enough to
669 * hold the decryption of the particular ciphertext provided,
670 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100671 *
Hanno Becker9a467772018-12-13 09:54:59 +0000672 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100673 * \param f_rng The RNG function. This is used for blinding and is
674 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000675 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100676 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000677 * \param olen The address at which to store the length of
678 * the plaintext. This must not be \c NULL.
679 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000680 * of length \c ctx->len Bytes. For example, \c 256 Bytes
681 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000682 * \param output The buffer used to hold the plaintext. This must
683 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000684 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100685 *
686 * \return \c 0 on success.
687 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200690 int (*f_rng)(void *, unsigned char *, size_t),
691 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100692 size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000693 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000694 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000695 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000696
697/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000698 * \brief This function performs a PKCS#1 v1.5 decryption
699 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100700 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100701 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000702 * as large as the size \p ctx->len of \p ctx->N, for example,
703 * 128 Bytes if RSA-1024 is used, to be able to hold an
704 * arbitrary decrypted message. If it is not large enough to
705 * hold the decryption of the particular ciphertext provided,
706 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100707 *
Hanno Becker9a467772018-12-13 09:54:59 +0000708 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100709 * \param f_rng The RNG function. This is used for blinding and is
710 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000711 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100712 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000713 * \param olen The address at which to store the length of
714 * the plaintext. This must not be \c NULL.
715 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000716 * of length \c ctx->len Bytes. For example, \c 256 Bytes
717 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000718 * \param output The buffer used to hold the plaintext. This must
719 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000720 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100721 *
722 * \return \c 0 on success.
723 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
724 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200727 int (*f_rng)(void *, unsigned char *, size_t),
728 void *p_rng,
Thomas Daubney34733082021-05-12 09:24:29 +0100729 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100730 const unsigned char *input,
731 unsigned char *output,
732 size_t output_max_len );
733
734/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100735 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
736 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100737 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100738 * \note The output buffer length \c output_max_len should be
739 * as large as the size \p ctx->len of \p ctx->N, for
740 * example, 128 Bytes if RSA-1024 is used, to be able to
741 * hold an arbitrary decrypted message. If it is not
742 * large enough to hold the decryption of the particular
743 * ciphertext provided, the function returns
744 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100745 *
Hanno Becker9a467772018-12-13 09:54:59 +0000746 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100747 * \param f_rng The RNG function. This is used for blinding and is
748 * mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000749 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100750 * \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100751 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000752 * This must be a readable buffer of length \p label_len
753 * Bytes. It may be \c NULL if \p label_len is \c 0.
754 * \param label_len The length of the label in Bytes.
755 * \param olen The address at which to store the length of
756 * the plaintext. This must not be \c NULL.
757 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000758 * of length \c ctx->len Bytes. For example, \c 256 Bytes
759 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000760 * \param output The buffer used to hold the plaintext. This must
761 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000762 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100763 *
764 * \return \c 0 on success.
765 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100766 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200768 int (*f_rng)(void *, unsigned char *, size_t),
769 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100770 const unsigned char *label, size_t label_len,
771 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100772 const unsigned char *input,
773 unsigned char *output,
774 size_t output_max_len );
775
776/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000777 * \brief This function performs a private RSA operation to sign
778 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000779 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000780 * It is the generic wrapper for performing a PKCS#1
Thomas Daubney140184d2021-05-18 16:04:07 +0100781 * signature.
Paul Bakker5121ce52009-01-03 21:22:43 +0000782 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000783 * \note The \p sig buffer must be as large as the size
784 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000785 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000786 * \note For PKCS#1 v2.1 encoding, see comments on
787 * mbedtls_rsa_rsassa_pss_sign() for details on
788 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100789 *
Hanno Becker9a467772018-12-13 09:54:59 +0000790 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100791 * \param f_rng The RNG function to use. This is mandatory and
792 * must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000793 * \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 +0100794 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100795 * \param md_alg The message-digest algorithm used to hash the original data.
796 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000797 * \param hashlen The length of the message digest.
798 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
799 * \param hash The buffer holding the message digest or raw data.
800 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
801 * buffer of length \p hashlen Bytes. If \p md_alg is not
802 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
803 * the size of the hash corresponding to \p md_alg.
804 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000805 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100806 * for an 2048-bit RSA modulus. A buffer length of
807 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100808 *
809 * \return \c 0 if the signing operation was successful.
810 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000813 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000814 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000816 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000817 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000818 unsigned char *sig );
819
820/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000821 * \brief This function performs a PKCS#1 v1.5 signature
822 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100823 *
Hanno Becker9a467772018-12-13 09:54:59 +0000824 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100825 * \param f_rng The RNG function. This is used for blinding and is
826 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000827 * \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 +0100828 * if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000829 * \param md_alg The message-digest algorithm used to hash the original data.
830 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000831 * \param hashlen The length of the message digest.
832 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
833 * \param hash The buffer holding the message digest or raw data.
834 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
835 * buffer of length \p hashlen Bytes. If \p md_alg is not
836 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
837 * the size of the hash corresponding to \p md_alg.
838 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000839 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100840 * for an 2048-bit RSA modulus. A buffer length of
841 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100842 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100843 * \return \c 0 if the signing operation was successful.
844 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100845 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200847 int (*f_rng)(void *, unsigned char *, size_t),
848 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100850 unsigned int hashlen,
851 const unsigned char *hash,
852 unsigned char *sig );
853
854/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000855 * \brief This function performs a PKCS#1 v2.1 PSS signature
856 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100857 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000858 * \note The \p hash_id in the RSA context is the one used for the
859 * encoding. \p md_alg in the function call is the type of hash
860 * that is encoded. According to <em>RFC-3447: Public-Key
861 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
862 * Specifications</em> it is advised to keep both hashes the
863 * same.
Rose Zadike8b5b992018-03-27 12:19:47 +0100864 *
Cédric Meuter010ddc22020-04-25 09:24:11 +0200865 * \note This function enforces that the provided salt length complies
866 * with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 v2.2) §9.1.1
867 * step 3. The constraint is that the hash length plus the salt
868 * length plus 2 bytes must be at most the key length. If this
869 * constraint is not met, this function returns
Jaeden Amero3725bb22018-09-07 19:12:36 +0100870 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
871 *
Hanno Becker9a467772018-12-13 09:54:59 +0000872 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100873 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000874 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
875 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100876 * \param md_alg The message-digest algorithm used to hash the original data.
877 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000878 * \param hashlen The length of the message digest.
879 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
880 * \param hash The buffer holding the message digest or raw data.
881 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
882 * buffer of length \p hashlen Bytes. If \p md_alg is not
883 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
884 * the size of the hash corresponding to \p md_alg.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200885 * \param saltlen The length of the salt that should be used.
Cédric Meuter010ddc22020-04-25 09:24:11 +0200886 * If passed #MBEDTLS_RSA_SALT_LEN_ANY, the function will use
887 * the largest possible salt length up to the hash length,
888 * which is the largest permitted by some standards including
889 * FIPS 186-4 §5.5.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200890 * \param sig The buffer to hold the signature. This must be a writable
891 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
892 * for an 2048-bit RSA modulus. A buffer length of
893 * #MBEDTLS_MPI_MAX_SIZE is always safe.
894 *
895 * \return \c 0 if the signing operation was successful.
896 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
897 */
898int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
899 int (*f_rng)(void *, unsigned char *, size_t),
900 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200901 mbedtls_md_type_t md_alg,
902 unsigned int hashlen,
903 const unsigned char *hash,
904 int saltlen,
905 unsigned char *sig );
906
907/**
908 * \brief This function performs a PKCS#1 v2.1 PSS signature
909 * operation (RSASSA-PSS-SIGN).
910 *
911 * \note The \p hash_id in the RSA context is the one used for the
912 * encoding. \p md_alg in the function call is the type of hash
913 * that is encoded. According to <em>RFC-3447: Public-Key
914 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
915 * Specifications</em> it is advised to keep both hashes the
916 * same.
917 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200918 * \note This function always uses the maximum possible salt size,
919 * up to the length of the payload hash. This choice of salt
Paul Bakkerb3869132013-02-28 17:21:01 +0100920 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
921 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
Rose Zadike8b5b992018-03-27 12:19:47 +0100922 * minimum salt size which is the hash size minus 2 bytes. If
923 * this minimum size is too large given the key size (the salt
924 * size, plus the hash size, plus 2 bytes must be no more than
925 * the key size in bytes), this function returns
926 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
927 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100928 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100929 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100930 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
931 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100932 * \param md_alg The message-digest algorithm used to hash the original data.
933 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000934 * \param hashlen The length of the message digest.
935 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
936 * \param hash The buffer holding the message digest or raw data.
937 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
938 * buffer of length \p hashlen Bytes. If \p md_alg is not
939 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
940 * the size of the hash corresponding to \p md_alg.
941 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000942 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100943 * for an 2048-bit RSA modulus. A buffer length of
944 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100945 *
946 * \return \c 0 if the signing operation was successful.
947 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100948 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100950 int (*f_rng)(void *, unsigned char *, size_t),
951 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100953 unsigned int hashlen,
954 const unsigned char *hash,
955 unsigned char *sig );
956
957/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000958 * \brief This function performs a public RSA operation and checks
959 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +0000960 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000961 * This is the generic wrapper for performing a PKCS#1
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100962 * verification.
Paul Bakker5121ce52009-01-03 21:22:43 +0000963 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000964 * \note For PKCS#1 v2.1 encoding, see comments on
965 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
966 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100967 *
Hanno Becker9a467772018-12-13 09:54:59 +0000968 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +0100969 * \param md_alg The message-digest algorithm used to hash the original data.
970 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000971 * \param hashlen The length of the message digest.
972 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
973 * \param hash The buffer holding the message digest or raw data.
974 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
975 * buffer of length \p hashlen Bytes. If \p md_alg is not
976 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
977 * the size of the hash corresponding to \p md_alg.
978 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +0000979 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
980 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100981 *
982 * \return \c 0 if the verify operation was successful.
983 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000984 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000987 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000988 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200989 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000990
991/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000992 * \brief This function performs a PKCS#1 v1.5 verification
993 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +0100994 *
Hanno Becker9a467772018-12-13 09:54:59 +0000995 * \param ctx The initialized RSA public key context to use.
Rose Zadik042e97f2018-01-26 16:35:10 +0000996 * \param md_alg The message-digest algorithm used to hash the original data.
997 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000998 * \param hashlen The length of the message digest.
999 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1000 * \param hash The buffer holding the message digest or raw data.
1001 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1002 * buffer of length \p hashlen Bytes. If \p md_alg is not
1003 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1004 * the size of the hash corresponding to \p md_alg.
1005 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001006 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1007 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001008 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001009 * \return \c 0 if the verify operation was successful.
1010 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001014 unsigned int hashlen,
1015 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001016 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001017
1018/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001019 * \brief This function performs a PKCS#1 v2.1 PSS verification
1020 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001021 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001022 * The hash function for the MGF mask generating function
1023 * is that specified in the RSA context.
Paul Bakkerb3869132013-02-28 17:21:01 +01001024 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001025 * \note The \p hash_id in the RSA context is the one used for the
1026 * verification. \p md_alg in the function call is the type of
1027 * hash that is verified. According to <em>RFC-3447: Public-Key
1028 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1029 * Specifications</em> it is advised to keep both hashes the
1030 * same. If \p hash_id in the RSA context is unset,
1031 * the \p md_alg from the function call is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001032 *
Hanno Becker9a467772018-12-13 09:54:59 +00001033 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001034 * \param md_alg The message-digest algorithm used to hash the original data.
1035 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001036 * \param hashlen The length of the message digest.
1037 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1038 * \param hash The buffer holding the message digest or raw data.
1039 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1040 * buffer of length \p hashlen Bytes. If \p md_alg is not
1041 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1042 * the size of the hash corresponding to \p md_alg.
1043 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001044 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1045 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001046 *
1047 * \return \c 0 if the verify operation was successful.
1048 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001049 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001052 unsigned int hashlen,
1053 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001054 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001055
1056/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001057 * \brief This function performs a PKCS#1 v2.1 PSS verification
1058 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001059 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001060 * The hash function for the MGF mask generating function
1061 * is that specified in \p mgf1_hash_id.
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 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001066 * \note The \p hash_id in the RSA context is ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001067 *
Hanno Becker9a467772018-12-13 09:54:59 +00001068 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001069 * \param md_alg The message-digest algorithm used to hash the original data.
1070 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001071 * \param hashlen The length of the message digest.
1072 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1073 * \param hash The buffer holding the message digest or raw data.
1074 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1075 * buffer of length \p hashlen Bytes. If \p md_alg is not
1076 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1077 * the size of the hash corresponding to \p md_alg.
1078 * \param mgf1_hash_id The message digest used for mask generation.
1079 * \param expected_salt_len The length of the salt used in padding. Use
1080 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1081 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001082 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1083 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001084 *
1085 * \return \c 0 if the verify operation was successful.
1086 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001090 unsigned int hashlen,
1091 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001093 int expected_salt_len,
1094 const unsigned char *sig );
1095
1096/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001097 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001098 *
Hanno Becker9a467772018-12-13 09:54:59 +00001099 * \param dst The destination context. This must be initialized.
1100 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001101 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001102 * \return \c 0 on success.
1103 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001106
1107/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001108 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001109 *
Hanno Becker9a467772018-12-13 09:54:59 +00001110 * \param ctx The RSA context to free. May be \c NULL, in which case
1111 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001112 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001115
Ron Eldorfa8f6352017-06-20 15:48:46 +03001116#if defined(MBEDTLS_SELF_TEST)
1117
Paul Bakker5121ce52009-01-03 21:22:43 +00001118/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001119 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001120 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001121 * \return \c 0 on success.
1122 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001125
Ron Eldorfa8f6352017-06-20 15:48:46 +03001126#endif /* MBEDTLS_SELF_TEST */
1127
Paul Bakker5121ce52009-01-03 21:22:43 +00001128#ifdef __cplusplus
1129}
1130#endif
1131
Paul Bakker5121ce52009-01-03 21:22:43 +00001132#endif /* rsa.h */