blob: 945050c3ca173515d4425458de5d4063e9663f51 [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 */
Rose Zadik042e97f2018-01-26 16:35:10 +000061#define MBEDTLS_RSA_PUBLIC 0 /**< Request private key operation. */
62#define MBEDTLS_RSA_PRIVATE 1 /**< Request public key operation. */
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Rose Zadike8b5b992018-03-27 12:19:47 +010064#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
65#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Rose Zadik042e97f2018-01-26 16:35:10 +000067#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
68#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020071
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020072/*
73 * The above constants may be used even if the RSA module is compile out,
74 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
75 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010076
Paul Bakker407a0da2013-06-27 14:29:21 +020077#ifdef __cplusplus
78extern "C" {
79#endif
80
Ron Eldor4e6d55d2018-02-07 16:36:15 +020081#if !defined(MBEDTLS_RSA_ALT)
82// Regular implementation
83//
84
Paul Bakker5121ce52009-01-03 21:22:43 +000085/**
Rose Zadik042e97f2018-01-26 16:35:10 +000086 * \brief The RSA context structure.
Hanno Becker5063cd22017-09-29 11:49:12 +010087 *
88 * \note Direct manipulation of the members of this structure
Rose Zadik042e97f2018-01-26 16:35:10 +000089 * is deprecated. All manipulation should instead be done through
90 * the public interface functions.
Paul Bakker5121ce52009-01-03 21:22:43 +000091 */
Dawid Drozd428cc522018-07-24 10:02:47 +020092typedef struct mbedtls_rsa_context
Paul Bakker5121ce52009-01-03 21:22:43 +000093{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020094 int MBEDTLS_PRIVATE(ver); /*!< Reserved for internal purposes.
Gilles Peskine4337a9c2021-02-09 18:59:42 +010095 * Do not set this field in application
96 * code. Its meaning might change without
97 * notice. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020098 size_t MBEDTLS_PRIVATE(len); /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200100 mbedtls_mpi MBEDTLS_PRIVATE(N); /*!< The public modulus. */
101 mbedtls_mpi MBEDTLS_PRIVATE(E); /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200103 mbedtls_mpi MBEDTLS_PRIVATE(D); /*!< The private exponent. */
104 mbedtls_mpi MBEDTLS_PRIVATE(P); /*!< The first prime factor. */
105 mbedtls_mpi MBEDTLS_PRIVATE(Q); /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100106
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200107 mbedtls_mpi MBEDTLS_PRIVATE(DP); /*!< <code>D % (P - 1)</code>. */
108 mbedtls_mpi MBEDTLS_PRIVATE(DQ); /*!< <code>D % (Q - 1)</code>. */
109 mbedtls_mpi MBEDTLS_PRIVATE(QP); /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200111 mbedtls_mpi MBEDTLS_PRIVATE(RN); /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200113 mbedtls_mpi MBEDTLS_PRIVATE(RP); /*!< cached <code>R^2 mod P</code>. */
114 mbedtls_mpi MBEDTLS_PRIVATE(RQ); /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200115
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200116 mbedtls_mpi MBEDTLS_PRIVATE(Vi); /*!< The cached blinding value. */
117 mbedtls_mpi MBEDTLS_PRIVATE(Vf); /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000118
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200119 int MBEDTLS_PRIVATE(padding); /*!< Selects padding mode:
Rose Zadik042e97f2018-01-26 16:35:10 +0000120 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
121 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200122 int MBEDTLS_PRIVATE(hash_id); /*!< Hash identifier of mbedtls_md_type_t type,
Rose Zadik042e97f2018-01-26 16:35:10 +0000123 as specified in md.h for use in the MGF
124 mask generating function used in the
125 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126#if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100127 /* Invariant: the mutex is initialized iff ver != 0. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200128 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex); /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200129#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000130}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200133#else /* MBEDTLS_RSA_ALT */
134#include "rsa_alt.h"
135#endif /* MBEDTLS_RSA_ALT */
136
Paul Bakker5121ce52009-01-03 21:22:43 +0000137/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000138 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000140 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000141 * encryption scheme and the RSASSA-PSS signature scheme.
142 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000143 * \note The \p hash_id parameter is ignored when using
144 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200145 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000146 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200147 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000148 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100149 * a default value, which can be overridden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000150 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200151 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000152 * \note The hash selected in \p hash_id is always used for OEAP
153 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100154 * making signatures, but can be overridden for verifying them.
155 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100156 *
Hanno Becker9a467772018-12-13 09:54:59 +0000157 * \param ctx The RSA context to initialize. This must not be \c NULL.
158 * \param padding The padding mode to use. This must be either
159 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Hanno Becker385ce912018-12-13 18:33:12 +0000160 * \param hash_id The hash identifier of ::mbedtls_md_type_t type, if
Hanno Becker9a467772018-12-13 09:54:59 +0000161 * \p padding is #MBEDTLS_RSA_PKCS_V21. It is unused
162 * otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100165 int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000166 int hash_id );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
168/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000169 * \brief This function imports a set of core parameters into an
170 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100171 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100172 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000173 * imports, if the parameters are not simultaneously present.
174 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100175 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000176 * by a call to mbedtls_rsa_complete(), which checks and
177 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100178 * public or private RSA key.
179 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000180 * \note See mbedtls_rsa_complete() for more information on which
181 * parameters are necessary to set up a private or public
182 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100183 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100184 * \note The imported parameters are copied and need not be preserved
185 * for the lifetime of the RSA context being set up.
186 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100187 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000188 * \param N The RSA modulus. This may be \c NULL.
189 * \param P The first prime factor of \p N. This may be \c NULL.
190 * \param Q The second prime factor of \p N. This may be \c NULL.
191 * \param D The private exponent. This may be \c NULL.
192 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100193 *
194 * \return \c 0 on success.
195 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100196 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100197int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
198 const mbedtls_mpi *N,
199 const mbedtls_mpi *P, const mbedtls_mpi *Q,
200 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100201
202/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000203 * \brief This function imports core RSA parameters, in raw big-endian
204 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100206 * \note This function can be called multiple times for successive
207 * imports, if the parameters are not simultaneously present.
208 *
209 * Any sequence of calls to this function should be followed
210 * by a call to mbedtls_rsa_complete(), which checks and
211 * completes the provided information to a ready-for-use
212 * public or private RSA key.
213 *
214 * \note See mbedtls_rsa_complete() for more information on which
215 * parameters are necessary to set up a private or public
216 * RSA key.
217 *
218 * \note The imported parameters are copied and need not be preserved
219 * for the lifetime of the RSA context being set up.
220 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000221 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000222 * \param N The RSA modulus. This may be \c NULL.
223 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
224 * \param P The first prime factor of \p N. This may be \c NULL.
225 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
226 * \param Q The second prime factor of \p N. This may be \c NULL.
227 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
228 * \param D The private exponent. This may be \c NULL.
229 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
230 * \param E The public exponent. This may be \c NULL.
231 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100233 * \return \c 0 on success.
234 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100235 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100236int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100237 unsigned char const *N, size_t N_len,
238 unsigned char const *P, size_t P_len,
239 unsigned char const *Q, size_t Q_len,
240 unsigned char const *D, size_t D_len,
241 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100242
243/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000244 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100245 * a set of imported core parameters.
246 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000247 * To setup an RSA public key, precisely \p N and \p E
248 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100249 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000250 * To setup an RSA private key, sufficient information must
251 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100252 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000253 * The default implementation supports the following:
254 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
255 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
256 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100257 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000258 * If this function runs successfully, it guarantees that
259 * the RSA context can be used for RSA operations without
260 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100261 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100262 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000263 * for the imported parameters. In particular, parameters that
264 * are not needed by the implementation might be silently
265 * discarded and left unchecked. To check the consistency
266 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100267 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100268 * \param ctx The initialized RSA context holding imported parameters.
269 *
270 * \return \c 0 on success.
271 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
272 * failed.
273 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100274 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100275int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100276
277/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000278 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100279 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000280 * If this function runs successfully, the non-NULL buffers
281 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
282 * written, with additional unused space filled leading by
283 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100284 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000285 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300286 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000287 * <li>An alternative RSA implementation is in use, which
288 * stores the key externally, and either cannot or should
289 * not export it into RAM.</li>
290 * <li>A SW or HW implementation might not support a certain
291 * deduction. For example, \p P, \p Q from \p N, \p D,
292 * and \p E if the former are not part of the
293 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100294 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000295 * If the function fails due to an unsupported operation,
296 * the RSA context stays intact and remains usable.
297 *
298 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000299 * \param N The MPI to hold the RSA modulus.
300 * This may be \c NULL if this field need not be exported.
301 * \param P The MPI to hold the first prime factor of \p N.
302 * This may be \c NULL if this field need not be exported.
303 * \param Q The MPI to hold the second prime factor of \p N.
304 * This may be \c NULL if this field need not be exported.
305 * \param D The MPI to hold the private exponent.
306 * This may be \c NULL if this field need not be exported.
307 * \param E The MPI to hold the public exponent.
308 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000309 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100310 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300311 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000312 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100313 * functionality or because of security policies.
314 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100315 *
316 */
317int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
318 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
319 mbedtls_mpi *D, mbedtls_mpi *E );
320
321/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000322 * \brief This function exports core parameters of an RSA key
323 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100324 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000325 * If this function runs successfully, the non-NULL buffers
326 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
327 * written, with additional unused space filled leading by
328 * zero Bytes.
329 *
330 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300331 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000332 * <li>An alternative RSA implementation is in use, which
333 * stores the key externally, and either cannot or should
334 * not export it into RAM.</li>
335 * <li>A SW or HW implementation might not support a certain
336 * deduction. For example, \p P, \p Q from \p N, \p D,
337 * and \p E if the former are not part of the
338 * implementation.</li></ul>
339 * If the function fails due to an unsupported operation,
340 * the RSA context stays intact and remains usable.
341 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100342 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100343 * buffer pointers are NULL.
344 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000345 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000346 * \param N The Byte array to store the RSA modulus,
347 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000348 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000349 * \param P The Byte array to hold the first prime factor of \p N,
350 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000351 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000352 * \param Q The Byte array to hold the second prime factor of \p N,
353 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000354 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000355 * \param D The Byte array to hold the private exponent,
356 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000357 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000358 * \param E The Byte array to hold the public exponent,
359 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000360 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100361 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100362 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300363 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000364 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100365 * functionality or because of security policies.
366 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100367 */
368int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
369 unsigned char *N, size_t N_len,
370 unsigned char *P, size_t P_len,
371 unsigned char *Q, size_t Q_len,
372 unsigned char *D, size_t D_len,
373 unsigned char *E, size_t E_len );
374
375/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000376 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100377 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100378 * \note Alternative RSA implementations not using CRT-parameters
379 * internally can implement this function based on
380 * mbedtls_rsa_deduce_opt().
381 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000382 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000383 * \param DP The MPI to hold \c D modulo `P-1`,
384 * or \c NULL if it need not be exported.
385 * \param DQ The MPI to hold \c D modulo `Q-1`,
386 * or \c NULL if it need not be exported.
387 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
388 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100389 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100390 * \return \c 0 on success.
391 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100392 *
393 */
394int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
395 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
396
Paul Bakker5121ce52009-01-03 21:22:43 +0000397/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000398 * \brief This function sets padding for an already initialized RSA
399 * context. See mbedtls_rsa_init() for details.
Paul Bakker5121ce52009-01-03 21:22:43 +0000400 *
Hanno Becker9a467772018-12-13 09:54:59 +0000401 * \param ctx The initialized RSA context to be configured.
402 * \param padding The padding mode to use. This must be either
403 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Rose Zadik042e97f2018-01-26 16:35:10 +0000404 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Paul Bakker40e46942009-01-03 21:51:57 +0000405 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100406void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000407 int hash_id );
Paul Bakker21eb2802010-08-16 11:10:02 +0000408
Paul Bakkera3d195c2011-11-27 21:07:34 +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.
427 * This must not be \c NULL.
428 * \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.
548 * \param f_rng The RNG function, used for blinding. It is discouraged
549 * and deprecated to pass \c NULL here, in which case
550 * blinding will be omitted.
551 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
552 * if \p f_rng is \c NULL or if \p f_rng doesn't need a context.
553 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000554 * of length \c ctx->len Bytes. For example, \c 256 Bytes
555 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000556 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000557 * of length \c ctx->len Bytes. For example, \c 256 Bytes
558 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100559 *
560 * \return \c 0 on success.
561 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
562 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200565 int (*f_rng)(void *, unsigned char *, size_t),
566 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000567 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 unsigned char *output );
569
570/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000571 * \brief This function adds the message padding, then performs an RSA
572 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000573 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000574 * It is the generic wrapper for performing a PKCS#1 encryption
575 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100577 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000578 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
579 * are likely to remove the \p mode argument and have it
580 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100581 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100582 * \note Alternative implementations of RSA need not support
583 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300584 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100585 *
Hanno Becker9a467772018-12-13 09:54:59 +0000586 * \param ctx The initialized RSA context to use.
Hanno Becker974ca0d2018-12-18 18:03:24 +0000587 * \param f_rng The RNG to use. It is mandatory for PKCS#1 v2.1 padding
588 * encoding, and for PKCS#1 v1.5 padding encoding when used
589 * with \p mode set to #MBEDTLS_RSA_PUBLIC. For PKCS#1 v1.5
590 * padding encoding and \p mode set to #MBEDTLS_RSA_PRIVATE,
591 * it is used for blinding and should be provided in this
592 * case; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000593 * \param p_rng The RNG context to be passed to \p f_rng. May be
594 * \c NULL if \p f_rng is \c NULL or if \p f_rng doesn't
595 * need a context argument.
596 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000597 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000598 * \param ilen The length of the plaintext in Bytes.
599 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000600 * buffer of size \p ilen Bytes. It may be \c NULL if
601 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000602 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000603 * of length \c ctx->len Bytes. For example, \c 256 Bytes
604 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100605 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100606 * \return \c 0 on success.
607 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000610 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000611 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000612 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000613 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 unsigned char *output );
615
616/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000617 * \brief This function performs a PKCS#1 v1.5 encryption operation
618 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100619 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100620 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000621 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
622 * are likely to remove the \p mode argument and have it
623 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100624 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100625 * \note Alternative implementations of RSA need not support
626 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300627 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100628 *
Hanno Becker9a467772018-12-13 09:54:59 +0000629 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000630 * \param f_rng The RNG function to use. It is needed for padding generation
631 * if \p mode is #MBEDTLS_RSA_PUBLIC. If \p mode is
632 * #MBEDTLS_RSA_PRIVATE (discouraged), it is used for
633 * blinding and should be provided; see mbedtls_rsa_private().
Hanno Becker9a467772018-12-13 09:54:59 +0000634 * \param p_rng The RNG context to be passed to \p f_rng. This may
635 * be \c NULL if \p f_rng is \c NULL or if \p f_rng
636 * doesn't need a context argument.
637 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000638 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000639 * \param ilen The length of the plaintext 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.
Hanno Becker3cdc7112017-10-05 10:09:31 +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_pkcs1_v15_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,
653 int mode, size_t ilen,
654 const unsigned char *input,
655 unsigned char *output );
656
657/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000658 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
659 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100660 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100661 * \note The output buffer must be as large as the size
662 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
663 *
664 * \deprecated It is deprecated and discouraged to call this function
665 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
666 * are likely to remove the \p mode argument and have it
667 * implicitly set to #MBEDTLS_RSA_PUBLIC.
668 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100669 * \note Alternative implementations of RSA need not support
670 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300671 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100672 *
Hanno Becker9a467772018-12-13 09:54:59 +0000673 * \param ctx The initnialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000674 * \param f_rng The RNG function to use. This is needed for padding
675 * generation and must be provided.
Hanno Becker9a467772018-12-13 09:54:59 +0000676 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000677 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000678 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000679 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000680 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000681 * This must be a readable buffer of length \p label_len
682 * Bytes. It may be \c NULL if \p label_len is \c 0.
683 * \param label_len The length of the label in Bytes.
684 * \param ilen The length of the plaintext buffer \p input in Bytes.
685 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000686 * buffer of size \p ilen Bytes. It may be \c NULL if
687 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000688 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000689 * of length \c ctx->len Bytes. For example, \c 256 Bytes
690 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100691 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100692 * \return \c 0 on success.
693 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100696 int (*f_rng)(void *, unsigned char *, size_t),
697 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100698 int mode,
699 const unsigned char *label, size_t label_len,
700 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100701 const unsigned char *input,
702 unsigned char *output );
703
704/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000705 * \brief This function performs an RSA operation, then removes the
706 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000707 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000708 * It is the generic wrapper for performing a PKCS#1 decryption
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100709 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000710 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100711 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000712 * as large as the size \p ctx->len of \p ctx->N (for example,
713 * 128 Bytes if RSA-1024 is used) to be able to hold an
714 * arbitrary decrypted message. If it is not large enough to
715 * hold the decryption of the particular ciphertext provided,
716 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100717 *
Hanno Becker9a467772018-12-13 09:54:59 +0000718 * \param ctx The initialized RSA context to use.
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100719 * \param f_rng The RNG function. This is used for blinding and should
720 * be provided; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000721 * \param p_rng The RNG context to be passed to \p f_rng. This may be
722 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000723 * \param olen The address at which to store the length of
724 * the plaintext. This must not be \c NULL.
725 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000726 * of length \c ctx->len Bytes. For example, \c 256 Bytes
727 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000728 * \param output The buffer used to hold the plaintext. This must
729 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000730 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100731 *
732 * \return \c 0 on success.
733 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000734 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200736 int (*f_rng)(void *, unsigned char *, size_t),
737 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100738 size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000739 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000740 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000741 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
743/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000744 * \brief This function performs a PKCS#1 v1.5 decryption
745 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100746 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100747 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000748 * as large as the size \p ctx->len of \p ctx->N, for example,
749 * 128 Bytes if RSA-1024 is used, to be able to hold an
750 * arbitrary decrypted message. If it is not large enough to
751 * hold the decryption of the particular ciphertext provided,
752 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100753 *
Hanno Becker9a467772018-12-13 09:54:59 +0000754 * \param ctx The initialized RSA context to use.
Thomas Daubney34733082021-05-12 09:24:29 +0100755 * \param f_rng The RNG function. This is used for blinding and should
756 * be provided; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000757 * \param p_rng The RNG context to be passed to \p f_rng. This may be
758 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000759 * \param olen The address at which to store the length of
760 * the plaintext. This must not be \c NULL.
761 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000762 * of length \c ctx->len Bytes. For example, \c 256 Bytes
763 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000764 * \param output The buffer used to hold the plaintext. This must
765 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000766 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100767 *
768 * \return \c 0 on success.
769 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
770 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200773 int (*f_rng)(void *, unsigned char *, size_t),
774 void *p_rng,
Thomas Daubney34733082021-05-12 09:24:29 +0100775 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100776 const unsigned char *input,
777 unsigned char *output,
778 size_t output_max_len );
779
780/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100781 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
782 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100783 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100784 * \note The output buffer length \c output_max_len should be
785 * as large as the size \p ctx->len of \p ctx->N, for
786 * example, 128 Bytes if RSA-1024 is used, to be able to
787 * hold an arbitrary decrypted message. If it is not
788 * large enough to hold the decryption of the particular
789 * ciphertext provided, the function returns
790 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100791 *
Hanno Becker9a467772018-12-13 09:54:59 +0000792 * \param ctx The initialized RSA context to use.
Thomas Daubneyd21e0b72021-05-06 11:41:09 +0100793 * \param f_rng The RNG function. This is used for blinding and should
794 * be provided; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000795 * \param p_rng The RNG context to be passed to \p f_rng. This may be
796 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100797 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000798 * This must be a readable buffer of length \p label_len
799 * Bytes. It may be \c NULL if \p label_len is \c 0.
800 * \param label_len The length of the label in Bytes.
801 * \param olen The address at which to store the length of
802 * the plaintext. This must not be \c NULL.
803 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000804 * of length \c ctx->len Bytes. For example, \c 256 Bytes
805 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000806 * \param output The buffer used to hold the plaintext. This must
807 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000808 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100809 *
810 * \return \c 0 on success.
811 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100812 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200814 int (*f_rng)(void *, unsigned char *, size_t),
815 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100816 const unsigned char *label, size_t label_len,
817 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100818 const unsigned char *input,
819 unsigned char *output,
820 size_t output_max_len );
821
822/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000823 * \brief This function performs a private RSA operation to sign
824 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000825 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000826 * It is the generic wrapper for performing a PKCS#1
827 * signature using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000828 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000829 * \note The \p sig buffer must be as large as the size
830 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000831 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000832 * \note For PKCS#1 v2.1 encoding, see comments on
833 * mbedtls_rsa_rsassa_pss_sign() for details on
834 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100835 *
836 * \deprecated It is deprecated and discouraged to call this function
837 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
838 * are likely to remove the \p mode argument and have it
839 * implicitly set to #MBEDTLS_RSA_PRIVATE.
840 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100841 * \note Alternative implementations of RSA need not support
842 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300843 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100844 *
Hanno Becker9a467772018-12-13 09:54:59 +0000845 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000846 * \param f_rng The RNG function to use. If the padding mode is PKCS#1 v2.1,
847 * this must be provided. If the padding mode is PKCS#1 v1.5 and
848 * \p mode is #MBEDTLS_RSA_PRIVATE, it is used for blinding
849 * and should be provided; see mbedtls_rsa_private() for more
850 * more. It is ignored otherwise.
Hanno Becker9a467772018-12-13 09:54:59 +0000851 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
852 * if \p f_rng is \c NULL or doesn't need a context argument.
853 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000854 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100855 * \param md_alg The message-digest algorithm used to hash the original data.
856 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000857 * \param hashlen The length of the message digest.
858 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
859 * \param hash The buffer holding the message digest or raw data.
860 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
861 * buffer of length \p hashlen Bytes. If \p md_alg is not
862 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
863 * the size of the hash corresponding to \p md_alg.
864 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000865 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100866 * for an 2048-bit RSA modulus. A buffer length of
867 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100868 *
869 * \return \c 0 if the signing operation was successful.
870 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000873 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000874 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000875 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000877 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000878 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000879 unsigned char *sig );
880
881/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000882 * \brief This function performs a PKCS#1 v1.5 signature
883 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100884 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100885 * \deprecated It is deprecated and discouraged to call this function
886 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
887 * are likely to remove the \p mode argument and have it
888 * implicitly set to #MBEDTLS_RSA_PRIVATE.
889 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100890 * \note Alternative implementations of RSA need not support
891 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300892 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100893 *
Hanno Becker9a467772018-12-13 09:54:59 +0000894 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000895 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
896 * this is used for blinding and should be provided; see
897 * mbedtls_rsa_private() for more. If \p mode is
898 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000899 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
900 * if \p f_rng is \c NULL or doesn't need a context argument.
901 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000902 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000903 * \param md_alg The message-digest algorithm used to hash the original data.
904 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000905 * \param hashlen The length of the message digest.
906 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
907 * \param hash The buffer holding the message digest or raw data.
908 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
909 * buffer of length \p hashlen Bytes. If \p md_alg is not
910 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
911 * the size of the hash corresponding to \p md_alg.
912 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000913 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100914 * for an 2048-bit RSA modulus. A buffer length of
915 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100916 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100917 * \return \c 0 if the signing operation was successful.
918 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100919 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200921 int (*f_rng)(void *, unsigned char *, size_t),
922 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100923 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100925 unsigned int hashlen,
926 const unsigned char *hash,
927 unsigned char *sig );
928
929/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000930 * \brief This function performs a PKCS#1 v2.1 PSS signature
931 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100932 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000933 * \note The \p hash_id in the RSA context is the one used for the
934 * encoding. \p md_alg in the function call is the type of hash
935 * that is encoded. According to <em>RFC-3447: Public-Key
936 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
937 * Specifications</em> it is advised to keep both hashes the
938 * same.
Rose Zadike8b5b992018-03-27 12:19:47 +0100939 *
Cédric Meuter010ddc22020-04-25 09:24:11 +0200940 * \note This function enforces that the provided salt length complies
941 * with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 v2.2) §9.1.1
942 * step 3. The constraint is that the hash length plus the salt
943 * length plus 2 bytes must be at most the key length. If this
944 * constraint is not met, this function returns
Jaeden Amero3725bb22018-09-07 19:12:36 +0100945 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
946 *
Hanno Becker9a467772018-12-13 09:54:59 +0000947 * \param ctx The initialized RSA context to use.
948 * \param f_rng The RNG function. It must not be \c NULL.
949 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
950 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100951 * \param md_alg The message-digest algorithm used to hash the original data.
952 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000953 * \param hashlen The length of the message digest.
954 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
955 * \param hash The buffer holding the message digest or raw data.
956 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
957 * buffer of length \p hashlen Bytes. If \p md_alg is not
958 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
959 * the size of the hash corresponding to \p md_alg.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200960 * \param saltlen The length of the salt that should be used.
Cédric Meuter010ddc22020-04-25 09:24:11 +0200961 * If passed #MBEDTLS_RSA_SALT_LEN_ANY, the function will use
962 * the largest possible salt length up to the hash length,
963 * which is the largest permitted by some standards including
964 * FIPS 186-4 §5.5.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200965 * \param sig The buffer to hold the signature. This must be a writable
966 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
967 * for an 2048-bit RSA modulus. A buffer length of
968 * #MBEDTLS_MPI_MAX_SIZE is always safe.
969 *
970 * \return \c 0 if the signing operation was successful.
971 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
972 */
973int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
974 int (*f_rng)(void *, unsigned char *, size_t),
975 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200976 mbedtls_md_type_t md_alg,
977 unsigned int hashlen,
978 const unsigned char *hash,
979 int saltlen,
980 unsigned char *sig );
981
982/**
983 * \brief This function performs a PKCS#1 v2.1 PSS signature
984 * operation (RSASSA-PSS-SIGN).
985 *
986 * \note The \p hash_id in the RSA context is the one used for the
987 * encoding. \p md_alg in the function call is the type of hash
988 * that is encoded. According to <em>RFC-3447: Public-Key
989 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
990 * Specifications</em> it is advised to keep both hashes the
991 * same.
992 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200993 * \note This function always uses the maximum possible salt size,
994 * up to the length of the payload hash. This choice of salt
Paul Bakkerb3869132013-02-28 17:21:01 +0100995 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
996 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
Rose Zadike8b5b992018-03-27 12:19:47 +0100997 * minimum salt size which is the hash size minus 2 bytes. If
998 * this minimum size is too large given the key size (the salt
999 * size, plus the hash size, plus 2 bytes must be no more than
1000 * the key size in bytes), this function returns
1001 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
1002 *
1003 * \deprecated It is deprecated and discouraged to call this function
1004 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
1005 * are likely to remove the \p mode argument and have it
1006 * implicitly set to #MBEDTLS_RSA_PRIVATE.
1007 *
1008 * \note Alternative implementations of RSA need not support
1009 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
1010 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
1011 *
1012 * \param ctx The initialized RSA context to use.
1013 * \param f_rng The RNG function. It must not be \c NULL.
1014 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
1015 * if \p f_rng doesn't need a context argument.
Paul Bakkerb3869132013-02-28 17:21:01 +01001016 * \param mode The mode of operation. This must be either
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Paul Bakkerb3869132013-02-28 17:21:01 +01001018 * \param md_alg The message-digest algorithm used to hash the original data.
1019 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001020 * \param hashlen The length of the message digest.
1021 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
1022 * \param hash The buffer holding the message digest or raw data.
1023 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1024 * buffer of length \p hashlen Bytes. If \p md_alg is not
1025 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1026 * the size of the hash corresponding to \p md_alg.
1027 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +00001028 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +01001029 * for an 2048-bit RSA modulus. A buffer length of
1030 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +01001031 *
1032 * \return \c 0 if the signing operation was successful.
1033 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
1034 */
1035int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1036 int (*f_rng)(void *, unsigned char *, size_t),
1037 void *p_rng,
1038 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001040 unsigned int hashlen,
1041 const unsigned char *hash,
1042 unsigned char *sig );
1043
1044/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001045 * \brief This function performs a public RSA operation and checks
1046 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +00001047 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001048 * This is the generic wrapper for performing a PKCS#1
1049 * verification using the mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001051 * \note For PKCS#1 v2.1 encoding, see comments on
1052 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
1053 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +01001054 *
1055 * \deprecated It is deprecated and discouraged to call this function
1056 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1057 * are likely to remove the \p mode argument and have it
1058 * set to #MBEDTLS_RSA_PUBLIC.
1059 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001060 * \note Alternative implementations of RSA need not support
1061 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001062 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001063 *
Hanno Becker9a467772018-12-13 09:54:59 +00001064 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001065 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1066 * this is used for blinding and should be provided; see
1067 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001068 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1069 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1070 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001071 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001072 * \param md_alg The message-digest algorithm used to hash the original data.
1073 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001074 * \param hashlen The length of the message digest.
1075 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1076 * \param hash The buffer holding the message digest or raw data.
1077 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1078 * buffer of length \p hashlen Bytes. If \p md_alg is not
1079 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1080 * the size of the hash corresponding to \p md_alg.
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.
Paul Bakker5121ce52009-01-03 21:22:43 +00001087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001089 int (*f_rng)(void *, unsigned char *, size_t),
1090 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001093 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001094 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001095 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +00001096
1097/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001098 * \brief This function performs a PKCS#1 v1.5 verification
1099 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001100 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001101 * \deprecated It is deprecated and discouraged to call this function
1102 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1103 * are likely to remove the \p mode argument and have it
1104 * set to #MBEDTLS_RSA_PUBLIC.
1105 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001106 * \note Alternative implementations of RSA need not support
1107 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001108 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001109 *
Hanno Becker9a467772018-12-13 09:54:59 +00001110 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001111 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1112 * this is used for blinding and should be provided; see
1113 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001114 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1115 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1116 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001117 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +00001118 * \param md_alg The message-digest algorithm used to hash the original data.
1119 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001120 * \param hashlen The length of the message digest.
1121 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1122 * \param hash The buffer holding the message digest or raw data.
1123 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1124 * buffer of length \p hashlen Bytes. If \p md_alg is not
1125 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1126 * the size of the hash corresponding to \p md_alg.
1127 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001128 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1129 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001130 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001131 * \return \c 0 if the verify operation was successful.
1132 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001135 int (*f_rng)(void *, unsigned char *, size_t),
1136 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001137 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001139 unsigned int hashlen,
1140 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001141 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001142
1143/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001144 * \brief This function performs a PKCS#1 v2.1 PSS verification
1145 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001146 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001147 * The hash function for the MGF mask generating function
1148 * is that specified in the RSA context.
Paul Bakkerb3869132013-02-28 17:21:01 +01001149 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001150 * \note The \p hash_id in the RSA context is the one used for the
1151 * verification. \p md_alg in the function call is the type of
1152 * hash that is verified. According to <em>RFC-3447: Public-Key
1153 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1154 * Specifications</em> it is advised to keep both hashes the
1155 * same. If \p hash_id in the RSA context is unset,
1156 * the \p md_alg from the function call is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001157 *
1158 * \deprecated It is deprecated and discouraged to call this function
1159 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1160 * are likely to remove the \p mode argument and have it
1161 * implicitly set to #MBEDTLS_RSA_PUBLIC.
1162 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001163 * \note Alternative implementations of RSA need not support
1164 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001165 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001166 *
Hanno Becker9a467772018-12-13 09:54:59 +00001167 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001168 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1169 * this is used for blinding and should be provided; see
1170 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001171 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1172 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1173 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001174 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001175 * \param md_alg The message-digest algorithm used to hash the original data.
1176 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001177 * \param hashlen The length of the message digest.
1178 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1179 * \param hash The buffer holding the message digest or raw data.
1180 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1181 * buffer of length \p hashlen Bytes. If \p md_alg is not
1182 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1183 * the size of the hash corresponding to \p md_alg.
1184 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001185 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1186 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001187 *
1188 * \return \c 0 if the verify operation was successful.
1189 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001192 int (*f_rng)(void *, unsigned char *, size_t),
1193 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001194 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001196 unsigned int hashlen,
1197 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001198 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001199
1200/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001201 * \brief This function performs a PKCS#1 v2.1 PSS verification
1202 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001203 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001204 * The hash function for the MGF mask generating function
1205 * is that specified in \p mgf1_hash_id.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001206 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001207 * \note The \p sig buffer must be as large as the size
1208 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001209 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001210 * \note The \p hash_id in the RSA context is ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001211 *
Hanno Becker9a467772018-12-13 09:54:59 +00001212 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001213 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1214 * this is used for blinding and should be provided; see
1215 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001216 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1217 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1218 * \param mode The mode of operation. This must be either
1219 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +01001220 * \param md_alg The message-digest algorithm used to hash the original data.
1221 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001222 * \param hashlen The length of the message digest.
1223 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1224 * \param hash The buffer holding the message digest or raw data.
1225 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1226 * buffer of length \p hashlen Bytes. If \p md_alg is not
1227 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1228 * the size of the hash corresponding to \p md_alg.
1229 * \param mgf1_hash_id The message digest used for mask generation.
1230 * \param expected_salt_len The length of the salt used in padding. Use
1231 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1232 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001233 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1234 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001235 *
1236 * \return \c 0 if the verify operation was successful.
1237 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001240 int (*f_rng)(void *, unsigned char *, size_t),
1241 void *p_rng,
1242 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001244 unsigned int hashlen,
1245 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001247 int expected_salt_len,
1248 const unsigned char *sig );
1249
1250/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001251 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001252 *
Hanno Becker9a467772018-12-13 09:54:59 +00001253 * \param dst The destination context. This must be initialized.
1254 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001255 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001256 * \return \c 0 on success.
1257 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001260
1261/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001262 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001263 *
Hanno Becker9a467772018-12-13 09:54:59 +00001264 * \param ctx The RSA context to free. May be \c NULL, in which case
1265 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001266 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Ron Eldorfa8f6352017-06-20 15:48:46 +03001270#if defined(MBEDTLS_SELF_TEST)
1271
Paul Bakker5121ce52009-01-03 21:22:43 +00001272/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001273 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001274 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001275 * \return \c 0 on success.
1276 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001279
Ron Eldorfa8f6352017-06-20 15:48:46 +03001280#endif /* MBEDTLS_SELF_TEST */
1281
Paul Bakker5121ce52009-01-03 21:22:43 +00001282#ifdef __cplusplus
1283}
1284#endif
1285
Paul Bakker5121ce52009-01-03 21:22:43 +00001286#endif /* rsa.h */