blob: 9ed5c81dd7e64f06fa6691b589ebce74b8a075ab [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Rose Zadik21e29262018-04-17 14:08:56 +01004 * \brief This file provides an API for the RSA public-key cryptosystem.
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
Rose Zadike8b5b992018-03-27 12:19:47 +01006 * The RSA public-key cryptosystem is defined in <em>Public-Key
7 * Cryptography Standards (PKCS) #1 v1.5: RSA Encryption</em>
Darryl Green11999bb2018-03-13 15:22:58 +00008 * and <em>Public-Key Cryptography Standards (PKCS) #1 v2.1:
Rose Zadike8b5b992018-03-27 12:19:47 +01009 * RSA Cryptography Specifications</em>.
Rose Zadik042e97f2018-01-26 16:35:10 +000010 *
Darryl Greena40a1012018-01-05 15:33:17 +000011 */
12/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020013 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020014 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#ifndef MBEDTLS_RSA_H
29#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010032#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#endif
Paul Bakkered27a042013-04-18 22:46:23 +020036
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010037#include "mbedtls/bignum.h"
38#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_THREADING_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010041#include "mbedtls/threading.h"
Paul Bakkerc9965dc2013-09-29 14:58:17 +020042#endif
43
Paul Bakker13e2dfe2009-07-28 07:18:38 +000044/*
45 * RSA Error codes
46 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
48#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
49#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Rose Zadik042e97f2018-01-26 16:35:10 +000050#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 +020051#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
52#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
53#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
54#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
55#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030056
57/* MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is deprecated and should not be used.
58 */
Rose Zadik042e97f2018-01-26 16:35:10 +000059#define MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION -0x4500 /**< The implementation does not offer the requested operation, for example, because of security violations or lack of functionality. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030060
Paul Bakker5121ce52009-01-03 21:22:43 +000061/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020062 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000063 */
Rose Zadik042e97f2018-01-26 16:35:10 +000064#define MBEDTLS_RSA_PUBLIC 0 /**< Request private key operation. */
65#define MBEDTLS_RSA_PRIVATE 1 /**< Request public key operation. */
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Rose Zadike8b5b992018-03-27 12:19:47 +010067#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
68#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Rose Zadik042e97f2018-01-26 16:35:10 +000070#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
71#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020074
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020075/*
76 * The above constants may be used even if the RSA module is compile out,
77 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
78 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010079
Paul Bakker407a0da2013-06-27 14:29:21 +020080#ifdef __cplusplus
81extern "C" {
82#endif
83
Ron Eldor4e6d55d2018-02-07 16:36:15 +020084#if !defined(MBEDTLS_RSA_ALT)
85// Regular implementation
86//
87
Paul Bakker5121ce52009-01-03 21:22:43 +000088/**
Rose Zadik042e97f2018-01-26 16:35:10 +000089 * \brief The RSA context structure.
Hanno Becker5063cd22017-09-29 11:49:12 +010090 *
91 * \note Direct manipulation of the members of this structure
Rose Zadik042e97f2018-01-26 16:35:10 +000092 * is deprecated. All manipulation should instead be done through
93 * the public interface functions.
Paul Bakker5121ce52009-01-03 21:22:43 +000094 */
Dawid Drozd428cc522018-07-24 10:02:47 +020095typedef struct mbedtls_rsa_context
Paul Bakker5121ce52009-01-03 21:22:43 +000096{
Gilles Peskine4337a9c2021-02-09 18:59:42 +010097 int ver; /*!< Reserved for internal purposes.
98 * Do not set this field in application
99 * code. Its meaning might change without
100 * notice. */
Rose Zadik042e97f2018-01-26 16:35:10 +0000101 size_t len; /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Rose Zadike8b5b992018-03-27 12:19:47 +0100103 mbedtls_mpi N; /*!< The public modulus. */
104 mbedtls_mpi E; /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Rose Zadike8b5b992018-03-27 12:19:47 +0100106 mbedtls_mpi D; /*!< The private exponent. */
107 mbedtls_mpi P; /*!< The first prime factor. */
108 mbedtls_mpi Q; /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100109
Rose Zadikf2ec2882018-04-17 10:27:25 +0100110 mbedtls_mpi DP; /*!< <code>D % (P - 1)</code>. */
111 mbedtls_mpi DQ; /*!< <code>D % (Q - 1)</code>. */
112 mbedtls_mpi QP; /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
Rose Zadikf2ec2882018-04-17 10:27:25 +0100114 mbedtls_mpi RN; /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
Rose Zadikf2ec2882018-04-17 10:27:25 +0100116 mbedtls_mpi RP; /*!< cached <code>R^2 mod P</code>. */
117 mbedtls_mpi RQ; /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200118
Rose Zadike8b5b992018-03-27 12:19:47 +0100119 mbedtls_mpi Vi; /*!< The cached blinding value. */
120 mbedtls_mpi Vf; /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000121
Rose Zadik042e97f2018-01-26 16:35:10 +0000122 int padding; /*!< Selects padding mode:
123 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
124 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
125 int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
126 as specified in md.h for use in the MGF
127 mask generating function used in the
128 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129#if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100130 /* Invariant: the mutex is initialized iff ver != 0. */
Rose Zadik042e97f2018-01-26 16:35:10 +0000131 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200132#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000133}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200136#else /* MBEDTLS_RSA_ALT */
137#include "rsa_alt.h"
138#endif /* MBEDTLS_RSA_ALT */
139
Paul Bakker5121ce52009-01-03 21:22:43 +0000140/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000141 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000143 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000144 * encryption scheme and the RSASSA-PSS signature scheme.
145 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000146 * \note The \p hash_id parameter is ignored when using
147 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200148 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000149 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200150 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000151 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100152 * a default value, which can be overridden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000153 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200154 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000155 * \note The hash selected in \p hash_id is always used for OEAP
156 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100157 * making signatures, but can be overridden for verifying them.
158 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100159 *
Hanno Becker9a467772018-12-13 09:54:59 +0000160 * \param ctx The RSA context to initialize. This must not be \c NULL.
161 * \param padding The padding mode to use. This must be either
162 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Hanno Becker385ce912018-12-13 18:33:12 +0000163 * \param hash_id The hash identifier of ::mbedtls_md_type_t type, if
Hanno Becker9a467772018-12-13 09:54:59 +0000164 * \p padding is #MBEDTLS_RSA_PKCS_V21. It is unused
165 * otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100168 int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000169 int hash_id );
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
171/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000172 * \brief This function imports a set of core parameters into an
173 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100174 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100175 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000176 * imports, if the parameters are not simultaneously present.
177 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100178 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000179 * by a call to mbedtls_rsa_complete(), which checks and
180 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100181 * public or private RSA key.
182 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000183 * \note See mbedtls_rsa_complete() for more information on which
184 * parameters are necessary to set up a private or public
185 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100186 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100187 * \note The imported parameters are copied and need not be preserved
188 * for the lifetime of the RSA context being set up.
189 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100190 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000191 * \param N The RSA modulus. This may be \c NULL.
192 * \param P The first prime factor of \p N. This may be \c NULL.
193 * \param Q The second prime factor of \p N. This may be \c NULL.
194 * \param D The private exponent. This may be \c NULL.
195 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100196 *
197 * \return \c 0 on success.
198 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100199 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100200int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
201 const mbedtls_mpi *N,
202 const mbedtls_mpi *P, const mbedtls_mpi *Q,
203 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100204
205/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000206 * \brief This function imports core RSA parameters, in raw big-endian
207 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100209 * \note This function can be called multiple times for successive
210 * imports, if the parameters are not simultaneously present.
211 *
212 * Any sequence of calls to this function should be followed
213 * by a call to mbedtls_rsa_complete(), which checks and
214 * completes the provided information to a ready-for-use
215 * public or private RSA key.
216 *
217 * \note See mbedtls_rsa_complete() for more information on which
218 * parameters are necessary to set up a private or public
219 * RSA key.
220 *
221 * \note The imported parameters are copied and need not be preserved
222 * for the lifetime of the RSA context being set up.
223 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000224 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000225 * \param N The RSA modulus. This may be \c NULL.
226 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
227 * \param P The first prime factor of \p N. This may be \c NULL.
228 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
229 * \param Q The second prime factor of \p N. This may be \c NULL.
230 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
231 * \param D The private exponent. This may be \c NULL.
232 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
233 * \param E The public exponent. This may be \c NULL.
234 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100236 * \return \c 0 on success.
237 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100238 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100239int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100240 unsigned char const *N, size_t N_len,
241 unsigned char const *P, size_t P_len,
242 unsigned char const *Q, size_t Q_len,
243 unsigned char const *D, size_t D_len,
244 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100245
246/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000247 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100248 * a set of imported core parameters.
249 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000250 * To setup an RSA public key, precisely \p N and \p E
251 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100252 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000253 * To setup an RSA private key, sufficient information must
254 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100255 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000256 * The default implementation supports the following:
257 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
258 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
259 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100260 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000261 * If this function runs successfully, it guarantees that
262 * the RSA context can be used for RSA operations without
263 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100264 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100265 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000266 * for the imported parameters. In particular, parameters that
267 * are not needed by the implementation might be silently
268 * discarded and left unchecked. To check the consistency
269 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100270 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100271 * \param ctx The initialized RSA context holding imported parameters.
272 *
273 * \return \c 0 on success.
274 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
275 * failed.
276 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100277 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100278int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100279
280/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000281 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100282 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000283 * If this function runs successfully, the non-NULL buffers
284 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
285 * written, with additional unused space filled leading by
286 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100287 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000288 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300289 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000290 * <li>An alternative RSA implementation is in use, which
291 * stores the key externally, and either cannot or should
292 * not export it into RAM.</li>
293 * <li>A SW or HW implementation might not support a certain
294 * deduction. For example, \p P, \p Q from \p N, \p D,
295 * and \p E if the former are not part of the
296 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100297 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000298 * If the function fails due to an unsupported operation,
299 * the RSA context stays intact and remains usable.
300 *
301 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000302 * \param N The MPI to hold the RSA modulus.
303 * This may be \c NULL if this field need not be exported.
304 * \param P The MPI to hold the first prime factor of \p N.
305 * This may be \c NULL if this field need not be exported.
306 * \param Q The MPI to hold the second prime factor of \p N.
307 * This may be \c NULL if this field need not be exported.
308 * \param D The MPI to hold the private exponent.
309 * This may be \c NULL if this field need not be exported.
310 * \param E The MPI to hold the public exponent.
311 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000312 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100313 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300314 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000315 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100316 * functionality or because of security policies.
317 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100318 *
319 */
320int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
321 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
322 mbedtls_mpi *D, mbedtls_mpi *E );
323
324/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000325 * \brief This function exports core parameters of an RSA key
326 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100327 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000328 * If this function runs successfully, the non-NULL buffers
329 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
330 * written, with additional unused space filled leading by
331 * zero Bytes.
332 *
333 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300334 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000335 * <li>An alternative RSA implementation is in use, which
336 * stores the key externally, and either cannot or should
337 * not export it into RAM.</li>
338 * <li>A SW or HW implementation might not support a certain
339 * deduction. For example, \p P, \p Q from \p N, \p D,
340 * and \p E if the former are not part of the
341 * implementation.</li></ul>
342 * If the function fails due to an unsupported operation,
343 * the RSA context stays intact and remains usable.
344 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100345 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100346 * buffer pointers are NULL.
347 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000348 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000349 * \param N The Byte array to store the RSA modulus,
350 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000351 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000352 * \param P The Byte array to hold the first 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 P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000355 * \param Q The Byte array to hold the second prime factor of \p N,
356 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000357 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000358 * \param D The Byte array to hold the private exponent,
359 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000360 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000361 * \param E The Byte array to hold the public exponent,
362 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000363 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100364 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100365 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300366 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000367 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100368 * functionality or because of security policies.
369 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100370 */
371int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
372 unsigned char *N, size_t N_len,
373 unsigned char *P, size_t P_len,
374 unsigned char *Q, size_t Q_len,
375 unsigned char *D, size_t D_len,
376 unsigned char *E, size_t E_len );
377
378/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000379 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100380 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100381 * \note Alternative RSA implementations not using CRT-parameters
382 * internally can implement this function based on
383 * mbedtls_rsa_deduce_opt().
384 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000385 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000386 * \param DP The MPI to hold \c D modulo `P-1`,
387 * or \c NULL if it need not be exported.
388 * \param DQ The MPI to hold \c D modulo `Q-1`,
389 * or \c NULL if it need not be exported.
390 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
391 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100392 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100393 * \return \c 0 on success.
394 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100395 *
396 */
397int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
398 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
399
Paul Bakker5121ce52009-01-03 21:22:43 +0000400/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000401 * \brief This function sets padding for an already initialized RSA
402 * context. See mbedtls_rsa_init() for details.
Paul Bakker5121ce52009-01-03 21:22:43 +0000403 *
Hanno Becker9a467772018-12-13 09:54:59 +0000404 * \param ctx The initialized RSA context to be configured.
405 * \param padding The padding mode to use. This must be either
406 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Rose Zadik042e97f2018-01-26 16:35:10 +0000407 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Paul Bakker40e46942009-01-03 21:51:57 +0000408 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100409void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000410 int hash_id );
Paul Bakker21eb2802010-08-16 11:10:02 +0000411
Paul Bakkera3d195c2011-11-27 21:07:34 +0000412/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000413 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100414 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000415 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100416 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000417 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100418 *
419 */
420size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
421
422/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000423 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000425 * \note mbedtls_rsa_init() must be called before this function,
426 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 *
Hanno Becker9a467772018-12-13 09:54:59 +0000428 * \param ctx The initialized RSA context used to hold the key.
429 * \param f_rng The RNG function to be used for key generation.
430 * This must not be \c NULL.
431 * \param p_rng The RNG context to be passed to \p f_rng.
432 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100433 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000434 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000435 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100436 *
437 * \return \c 0 on success.
438 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100441 int (*f_rng)(void *, unsigned char *, size_t),
442 void *p_rng,
443 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000444
445/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000446 * \brief This function checks if a context contains at least an RSA
447 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000448 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000449 * If the function runs successfully, it is guaranteed that
450 * enough information is present to perform an RSA public key
451 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 *
Hanno Becker9a467772018-12-13 09:54:59 +0000453 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000454 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100455 * \return \c 0 on success.
456 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100457 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000462 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100463 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000464 *
Hanno Becker68767a62017-10-17 10:13:31 +0100465 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000466 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100467 * on the given context, but that the various parameters are
468 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000469 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100470 *
471 * \warning This function should catch accidental misconfigurations
472 * like swapping of parameters, but it cannot establish full
473 * trust in neither the quality nor the consistency of the key
474 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000475 * <ul><li>Consistency: Imported parameters that are irrelevant
476 * for the implementation might be silently dropped. If dropped,
477 * the current function does not have access to them,
478 * and therefore cannot check them. See mbedtls_rsa_complete().
479 * If you want to check the consistency of the entire
480 * content of an PKCS1-encoded RSA private key, for example, you
481 * should use mbedtls_rsa_validate_params() before setting
482 * up the RSA context.
483 * Additionally, if the implementation performs empirical checks,
484 * these checks substantiate but do not guarantee consistency.</li>
485 * <li>Quality: This function is not expected to perform
486 * extended quality assessments like checking that the prime
487 * factors are safe. Additionally, it is the responsibility of the
488 * user to ensure the trustworthiness of the source of his RSA
489 * parameters, which goes beyond what is effectively checkable
490 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100491 *
Hanno Becker9a467772018-12-13 09:54:59 +0000492 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100493 *
494 * \return \c 0 on success.
495 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000498
499/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000500 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100501 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000502 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100503 *
Hanno Becker9a467772018-12-13 09:54:59 +0000504 * \param pub The initialized RSA context holding the public key.
505 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000506 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100507 * \return \c 0 on success.
508 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100509 */
Hanno Becker98838b02017-10-02 13:16:10 +0100510int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
511 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100512
513/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000514 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 *
Hanno Becker9a467772018-12-13 09:54:59 +0000516 * \param ctx The initialized RSA context to use.
517 * \param input The input buffer. This must be a readable 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 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000521 * of length \c ctx->len Bytes. For example, \c 256 Bytes
522 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000523 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000524 * \note This function does not handle message padding.
525 *
526 * \note Make sure to set \p input[0] = 0 or ensure that
527 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100529 * \return \c 0 on success.
530 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000533 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 unsigned char *output );
535
536/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000537 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 *
Hanno Becker24120612017-10-26 11:53:35 +0100539 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100540 *
541 * \note If blinding is used, both the base of exponentation
Hanno Becker24120612017-10-26 11:53:35 +0100542 * and the exponent are blinded, providing protection
543 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100544 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100545 * \warning It is deprecated and a security risk to not provide
546 * a PRNG here and thereby prevent the use of blinding.
547 * Future versions of the library may enforce the presence
548 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100549 *
Hanno Becker9a467772018-12-13 09:54:59 +0000550 * \param ctx The initialized RSA context to use.
551 * \param f_rng The RNG function, used for blinding. It is discouraged
552 * and deprecated to pass \c NULL here, in which case
553 * blinding will be omitted.
554 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
555 * if \p f_rng is \c NULL or if \p f_rng doesn't need a context.
556 * \param input The input buffer. This must be a readable 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.
Hanno Becker9a467772018-12-13 09:54:59 +0000559 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000560 * of length \c ctx->len Bytes. For example, \c 256 Bytes
561 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100562 *
563 * \return \c 0 on success.
564 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
565 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200568 int (*f_rng)(void *, unsigned char *, size_t),
569 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000570 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000571 unsigned char *output );
572
573/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000574 * \brief This function adds the message padding, then performs an RSA
575 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000577 * It is the generic wrapper for performing a PKCS#1 encryption
578 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100580 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000581 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
582 * are likely to remove the \p mode argument and have it
583 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100584 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100585 * \note Alternative implementations of RSA need not support
586 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300587 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100588 *
Hanno Becker9a467772018-12-13 09:54:59 +0000589 * \param ctx The initialized RSA context to use.
Hanno Becker974ca0d2018-12-18 18:03:24 +0000590 * \param f_rng The RNG to use. It is mandatory for PKCS#1 v2.1 padding
591 * encoding, and for PKCS#1 v1.5 padding encoding when used
592 * with \p mode set to #MBEDTLS_RSA_PUBLIC. For PKCS#1 v1.5
593 * padding encoding and \p mode set to #MBEDTLS_RSA_PRIVATE,
594 * it is used for blinding and should be provided in this
595 * case; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000596 * \param p_rng The RNG context to be passed to \p f_rng. May be
597 * \c NULL if \p f_rng is \c NULL or if \p f_rng doesn't
598 * need a context argument.
599 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000600 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000601 * \param ilen The length of the plaintext in Bytes.
602 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000603 * buffer of size \p ilen Bytes. It may be \c NULL if
604 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000605 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000606 * of length \c ctx->len Bytes. For example, \c 256 Bytes
607 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100608 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100609 * \return \c 0 on success.
610 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000613 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000614 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000615 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000616 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 unsigned char *output );
618
619/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000620 * \brief This function performs a PKCS#1 v1.5 encryption operation
621 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100622 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100623 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000624 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
625 * are likely to remove the \p mode argument and have it
626 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100627 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100628 * \note Alternative implementations of RSA need not support
629 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300630 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100631 *
Hanno Becker9a467772018-12-13 09:54:59 +0000632 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000633 * \param f_rng The RNG function to use. It is needed for padding generation
634 * if \p mode is #MBEDTLS_RSA_PUBLIC. If \p mode is
635 * #MBEDTLS_RSA_PRIVATE (discouraged), it is used for
636 * blinding and should be provided; see mbedtls_rsa_private().
Hanno Becker9a467772018-12-13 09:54:59 +0000637 * \param p_rng The RNG context to be passed to \p f_rng. This may
638 * be \c NULL if \p f_rng is \c NULL or if \p f_rng
639 * doesn't need a context argument.
640 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000641 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000642 * \param ilen The length of the plaintext in Bytes.
643 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000644 * buffer of size \p ilen Bytes. It may be \c NULL if
645 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000646 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000647 * of length \c ctx->len Bytes. For example, \c 256 Bytes
648 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100649 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100650 * \return \c 0 on success.
651 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100654 int (*f_rng)(void *, unsigned char *, size_t),
655 void *p_rng,
656 int mode, size_t ilen,
657 const unsigned char *input,
658 unsigned char *output );
659
660/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000661 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
662 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100663 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100664 * \note The output buffer must be as large as the size
665 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
666 *
667 * \deprecated It is deprecated and discouraged to call this function
668 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
669 * are likely to remove the \p mode argument and have it
670 * implicitly set to #MBEDTLS_RSA_PUBLIC.
671 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100672 * \note Alternative implementations of RSA need not support
673 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300674 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100675 *
Hanno Becker9a467772018-12-13 09:54:59 +0000676 * \param ctx The initnialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000677 * \param f_rng The RNG function to use. This is needed for padding
678 * generation and must be provided.
Hanno Becker9a467772018-12-13 09:54:59 +0000679 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000680 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000681 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000682 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000683 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000684 * This must be a readable buffer of length \p label_len
685 * Bytes. It may be \c NULL if \p label_len is \c 0.
686 * \param label_len The length of the label in Bytes.
687 * \param ilen The length of the plaintext buffer \p input in Bytes.
688 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000689 * buffer of size \p ilen Bytes. It may be \c NULL if
690 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000691 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000692 * of length \c ctx->len Bytes. For example, \c 256 Bytes
693 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100694 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100695 * \return \c 0 on success.
696 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100699 int (*f_rng)(void *, unsigned char *, size_t),
700 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100701 int mode,
702 const unsigned char *label, size_t label_len,
703 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100704 const unsigned char *input,
705 unsigned char *output );
706
707/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000708 * \brief This function performs an RSA operation, then removes the
709 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000710 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000711 * It is the generic wrapper for performing a PKCS#1 decryption
712 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000713 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100714 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000715 * as large as the size \p ctx->len of \p ctx->N (for example,
716 * 128 Bytes if RSA-1024 is used) to be able to hold an
717 * arbitrary decrypted message. If it is not large enough to
718 * hold the decryption of the particular ciphertext provided,
719 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100720 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100721 * \deprecated It is deprecated and discouraged to call this function
722 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
723 * are likely to remove the \p mode argument and have it
724 * implicitly set to #MBEDTLS_RSA_PRIVATE.
725 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100726 * \note Alternative implementations of RSA need not support
727 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300728 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100729 *
Hanno Becker9a467772018-12-13 09:54:59 +0000730 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000731 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
732 * this is used for blinding and should be provided; see
733 * mbedtls_rsa_private() for more. If \p mode is
734 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000735 * \param p_rng The RNG context to be passed to \p f_rng. This may be
736 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
737 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000738 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000739 * \param olen The address at which to store the length of
740 * the plaintext. This must not be \c NULL.
741 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000742 * of length \c ctx->len Bytes. For example, \c 256 Bytes
743 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000744 * \param output The buffer used to hold the plaintext. This must
745 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000746 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100747 *
748 * \return \c 0 on success.
749 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200752 int (*f_rng)(void *, unsigned char *, size_t),
753 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000754 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000755 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000756 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000757 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000758
759/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000760 * \brief This function performs a PKCS#1 v1.5 decryption
761 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100762 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100763 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000764 * as large as the size \p ctx->len of \p ctx->N, for example,
765 * 128 Bytes if RSA-1024 is used, to be able to hold an
766 * arbitrary decrypted message. If it is not large enough to
767 * hold the decryption of the particular ciphertext provided,
768 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100769 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100770 * \deprecated It is deprecated and discouraged to call this function
771 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
772 * are likely to remove the \p mode argument and have it
773 * implicitly set to #MBEDTLS_RSA_PRIVATE.
774 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100775 * \note Alternative implementations of RSA need not support
776 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300777 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100778 *
Hanno Becker9a467772018-12-13 09:54:59 +0000779 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000780 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
781 * this is used for blinding and should be provided; see
782 * mbedtls_rsa_private() for more. If \p mode is
783 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000784 * \param p_rng The RNG context to be passed to \p f_rng. This may be
785 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
786 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000787 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000788 * \param olen The address at which to store the length of
789 * the plaintext. This must not be \c NULL.
790 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000791 * of length \c ctx->len Bytes. For example, \c 256 Bytes
792 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000793 * \param output The buffer used to hold the plaintext. This must
794 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000795 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100796 *
797 * \return \c 0 on success.
798 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
799 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200802 int (*f_rng)(void *, unsigned char *, size_t),
803 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100804 int mode, size_t *olen,
805 const unsigned char *input,
806 unsigned char *output,
807 size_t output_max_len );
808
809/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100810 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
811 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100812 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100813 * \note The output buffer length \c output_max_len should be
814 * as large as the size \p ctx->len of \p ctx->N, for
815 * example, 128 Bytes if RSA-1024 is used, to be able to
816 * hold an arbitrary decrypted message. If it is not
817 * large enough to hold the decryption of the particular
818 * ciphertext provided, the function returns
819 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100820 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100821 * \deprecated It is deprecated and discouraged to call this function
822 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
823 * are likely to remove the \p mode argument and have it
824 * implicitly set to #MBEDTLS_RSA_PRIVATE.
825 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100826 * \note Alternative implementations of RSA need not support
827 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300828 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100829 *
Hanno Becker9a467772018-12-13 09:54:59 +0000830 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000831 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
832 * this is used for blinding and should be provided; see
833 * mbedtls_rsa_private() for more. If \p mode is
834 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000835 * \param p_rng The RNG context to be passed to \p f_rng. This may be
836 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
837 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000838 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100839 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000840 * This must be a readable buffer of length \p label_len
841 * Bytes. It may be \c NULL if \p label_len is \c 0.
842 * \param label_len The length of the label in Bytes.
843 * \param olen The address at which to store the length of
844 * the plaintext. This must not be \c NULL.
845 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000846 * of length \c ctx->len Bytes. For example, \c 256 Bytes
847 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000848 * \param output The buffer used to hold the plaintext. This must
849 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000850 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100851 *
852 * \return \c 0 on success.
853 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100854 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200856 int (*f_rng)(void *, unsigned char *, size_t),
857 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100858 int mode,
859 const unsigned char *label, size_t label_len,
860 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100861 const unsigned char *input,
862 unsigned char *output,
863 size_t output_max_len );
864
865/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000866 * \brief This function performs a private RSA operation to sign
867 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000868 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000869 * It is the generic wrapper for performing a PKCS#1
870 * signature using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000871 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000872 * \note The \p sig buffer must be as large as the size
873 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000874 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000875 * \note For PKCS#1 v2.1 encoding, see comments on
876 * mbedtls_rsa_rsassa_pss_sign() for details on
877 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100878 *
879 * \deprecated It is deprecated and discouraged to call this function
880 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
881 * are likely to remove the \p mode argument and have it
882 * implicitly set to #MBEDTLS_RSA_PRIVATE.
883 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100884 * \note Alternative implementations of RSA need not support
885 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300886 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100887 *
Hanno Becker9a467772018-12-13 09:54:59 +0000888 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000889 * \param f_rng The RNG function to use. If the padding mode is PKCS#1 v2.1,
890 * this must be provided. If the padding mode is PKCS#1 v1.5 and
891 * \p mode is #MBEDTLS_RSA_PRIVATE, it is used for blinding
892 * and should be provided; see mbedtls_rsa_private() for more
893 * more. It is ignored otherwise.
Hanno Becker9a467772018-12-13 09:54:59 +0000894 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
895 * if \p f_rng is \c NULL or doesn't need a context argument.
896 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000897 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100898 * \param md_alg The message-digest algorithm used to hash the original data.
899 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000900 * \param hashlen The length of the message digest.
901 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
902 * \param hash The buffer holding the message digest or raw data.
903 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
904 * buffer of length \p hashlen Bytes. If \p md_alg is not
905 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
906 * the size of the hash corresponding to \p md_alg.
907 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000908 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100909 * for an 2048-bit RSA modulus. A buffer length of
910 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100911 *
912 * \return \c 0 if the signing operation was successful.
913 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000914 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000916 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000917 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000918 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000920 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000921 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000922 unsigned char *sig );
923
924/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000925 * \brief This function performs a PKCS#1 v1.5 signature
926 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100927 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100928 * \deprecated It is deprecated and discouraged to call this function
929 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
930 * are likely to remove the \p mode argument and have it
931 * implicitly set to #MBEDTLS_RSA_PRIVATE.
932 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100933 * \note Alternative implementations of RSA need not support
934 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300935 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100936 *
Hanno Becker9a467772018-12-13 09:54:59 +0000937 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000938 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
939 * this is used for blinding and should be provided; see
940 * mbedtls_rsa_private() for more. If \p mode is
941 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000942 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
943 * if \p f_rng is \c NULL or doesn't need a context argument.
944 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000945 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000946 * \param md_alg The message-digest algorithm used to hash the original data.
947 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000948 * \param hashlen The length of the message digest.
949 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
950 * \param hash The buffer holding the message digest or raw data.
951 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
952 * buffer of length \p hashlen Bytes. If \p md_alg is not
953 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
954 * the size of the hash corresponding to \p md_alg.
955 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000956 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100957 * for an 2048-bit RSA modulus. A buffer length of
958 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100959 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100960 * \return \c 0 if the signing operation was successful.
961 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200964 int (*f_rng)(void *, unsigned char *, size_t),
965 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100966 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100968 unsigned int hashlen,
969 const unsigned char *hash,
970 unsigned char *sig );
971
972/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000973 * \brief This function performs a PKCS#1 v2.1 PSS signature
974 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100975 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000976 * \note The \p hash_id in the RSA context is the one used for the
977 * encoding. \p md_alg in the function call is the type of hash
978 * that is encoded. According to <em>RFC-3447: Public-Key
979 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
980 * Specifications</em> it is advised to keep both hashes the
981 * same.
Rose Zadike8b5b992018-03-27 12:19:47 +0100982 *
Jaeden Amero3725bb22018-09-07 19:12:36 +0100983 * \note This function always uses the maximum possible salt size,
984 * up to the length of the payload hash. This choice of salt
985 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
986 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
987 * minimum salt size which is the hash size minus 2 bytes. If
988 * this minimum size is too large given the key size (the salt
989 * size, plus the hash size, plus 2 bytes must be no more than
990 * the key size in bytes), this function returns
991 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
992 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100993 * \deprecated It is deprecated and discouraged to call this function
994 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
995 * are likely to remove the \p mode argument and have it
996 * implicitly set to #MBEDTLS_RSA_PRIVATE.
997 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100998 * \note Alternative implementations of RSA need not support
999 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001000 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001001 *
Hanno Becker9a467772018-12-13 09:54:59 +00001002 * \param ctx The initialized RSA context to use.
1003 * \param f_rng The RNG function. It must not be \c NULL.
1004 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
1005 * if \p f_rng doesn't need a context argument.
1006 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001007 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001008 * \param md_alg The message-digest algorithm used to hash the original data.
1009 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001010 * \param hashlen The length of the message digest.
1011 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
1012 * \param hash The buffer holding the message digest or raw data.
1013 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1014 * buffer of length \p hashlen Bytes. If \p md_alg is not
1015 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1016 * the size of the hash corresponding to \p md_alg.
1017 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +00001018 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +01001019 * for an 2048-bit RSA modulus. A buffer length of
1020 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +01001021 *
1022 * \return \c 0 if the signing operation was successful.
1023 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001026 int (*f_rng)(void *, unsigned char *, size_t),
1027 void *p_rng,
1028 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001030 unsigned int hashlen,
1031 const unsigned char *hash,
1032 unsigned char *sig );
1033
1034/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001035 * \brief This function performs a public RSA operation and checks
1036 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +00001037 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001038 * This is the generic wrapper for performing a PKCS#1
1039 * verification using the mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001040 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001041 * \note For PKCS#1 v2.1 encoding, see comments on
1042 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
1043 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +01001044 *
1045 * \deprecated It is deprecated and discouraged to call this function
1046 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1047 * are likely to remove the \p mode argument and have it
1048 * set to #MBEDTLS_RSA_PUBLIC.
1049 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001050 * \note Alternative implementations of RSA need not support
1051 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001052 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001053 *
Hanno Becker9a467772018-12-13 09:54:59 +00001054 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001055 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1056 * this is used for blinding and should be provided; see
1057 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001058 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1059 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1060 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001061 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001062 * \param md_alg The message-digest algorithm used to hash the original data.
1063 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001064 * \param hashlen The length of the message digest.
1065 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1066 * \param hash The buffer holding the message digest or raw data.
1067 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1068 * buffer of length \p hashlen Bytes. If \p md_alg is not
1069 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1070 * the size of the hash corresponding to \p md_alg.
1071 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001072 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1073 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001074 *
1075 * \return \c 0 if the verify operation was successful.
1076 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001079 int (*f_rng)(void *, unsigned char *, size_t),
1080 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001083 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001084 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001085 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +00001086
1087/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001088 * \brief This function performs a PKCS#1 v1.5 verification
1089 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001090 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001091 * \deprecated It is deprecated and discouraged to call this function
1092 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1093 * are likely to remove the \p mode argument and have it
1094 * set to #MBEDTLS_RSA_PUBLIC.
1095 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001096 * \note Alternative implementations of RSA need not support
1097 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001098 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001099 *
Hanno Becker9a467772018-12-13 09:54:59 +00001100 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001101 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1102 * this is used for blinding and should be provided; see
1103 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001104 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1105 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1106 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001107 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +00001108 * \param md_alg The message-digest algorithm used to hash the original data.
1109 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001110 * \param hashlen The length of the message digest.
1111 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1112 * \param hash The buffer holding the message digest or raw data.
1113 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1114 * buffer of length \p hashlen Bytes. If \p md_alg is not
1115 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1116 * the size of the hash corresponding to \p md_alg.
1117 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001118 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1119 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001120 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001121 * \return \c 0 if the verify operation was successful.
1122 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001125 int (*f_rng)(void *, unsigned char *, size_t),
1126 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001127 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001129 unsigned int hashlen,
1130 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001131 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001132
1133/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001134 * \brief This function performs a PKCS#1 v2.1 PSS verification
1135 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001136 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001137 * The hash function for the MGF mask generating function
1138 * is that specified in the RSA context.
Paul Bakkerb3869132013-02-28 17:21:01 +01001139 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001140 * \note The \p hash_id in the RSA context is the one used for the
1141 * verification. \p md_alg in the function call is the type of
1142 * hash that is verified. According to <em>RFC-3447: Public-Key
1143 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1144 * Specifications</em> it is advised to keep both hashes the
1145 * same. If \p hash_id in the RSA context is unset,
1146 * the \p md_alg from the function call is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001147 *
1148 * \deprecated It is deprecated and discouraged to call this function
1149 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1150 * are likely to remove the \p mode argument and have it
1151 * implicitly set to #MBEDTLS_RSA_PUBLIC.
1152 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001153 * \note Alternative implementations of RSA need not support
1154 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001155 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001156 *
Hanno Becker9a467772018-12-13 09:54:59 +00001157 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001158 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1159 * this is used for blinding and should be provided; see
1160 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001161 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1162 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1163 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001164 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001165 * \param md_alg The message-digest algorithm used to hash the original data.
1166 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001167 * \param hashlen The length of the message digest.
1168 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1169 * \param hash The buffer holding the message digest or raw data.
1170 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1171 * buffer of length \p hashlen Bytes. If \p md_alg is not
1172 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1173 * the size of the hash corresponding to \p md_alg.
1174 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001175 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1176 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001177 *
1178 * \return \c 0 if the verify operation was successful.
1179 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001182 int (*f_rng)(void *, unsigned char *, size_t),
1183 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001184 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001186 unsigned int hashlen,
1187 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001188 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001189
1190/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001191 * \brief This function performs a PKCS#1 v2.1 PSS verification
1192 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001193 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001194 * The hash function for the MGF mask generating function
1195 * is that specified in \p mgf1_hash_id.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001196 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001197 * \note The \p sig buffer must be as large as the size
1198 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001199 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001200 * \note The \p hash_id in the RSA context is ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001201 *
Hanno Becker9a467772018-12-13 09:54:59 +00001202 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001203 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1204 * this is used for blinding and should be provided; see
1205 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001206 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1207 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1208 * \param mode The mode of operation. This must be either
1209 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +01001210 * \param md_alg The message-digest algorithm used to hash the original data.
1211 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001212 * \param hashlen The length of the message digest.
1213 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1214 * \param hash The buffer holding the message digest or raw data.
1215 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1216 * buffer of length \p hashlen Bytes. If \p md_alg is not
1217 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1218 * the size of the hash corresponding to \p md_alg.
1219 * \param mgf1_hash_id The message digest used for mask generation.
1220 * \param expected_salt_len The length of the salt used in padding. Use
1221 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1222 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001223 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1224 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001225 *
1226 * \return \c 0 if the verify operation was successful.
1227 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001230 int (*f_rng)(void *, unsigned char *, size_t),
1231 void *p_rng,
1232 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001234 unsigned int hashlen,
1235 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001237 int expected_salt_len,
1238 const unsigned char *sig );
1239
1240/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001241 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001242 *
Hanno Becker9a467772018-12-13 09:54:59 +00001243 * \param dst The destination context. This must be initialized.
1244 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001245 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001246 * \return \c 0 on success.
1247 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001250
1251/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001252 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001253 *
Hanno Becker9a467772018-12-13 09:54:59 +00001254 * \param ctx The RSA context to free. May be \c NULL, in which case
1255 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001256 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001259
Ron Eldorfa8f6352017-06-20 15:48:46 +03001260#if defined(MBEDTLS_SELF_TEST)
1261
Paul Bakker5121ce52009-01-03 21:22:43 +00001262/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001263 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001264 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001265 * \return \c 0 on success.
1266 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Ron Eldorfa8f6352017-06-20 15:48:46 +03001270#endif /* MBEDTLS_SELF_TEST */
1271
Paul Bakker5121ce52009-01-03 21:22:43 +00001272#ifdef __cplusplus
1273}
1274#endif
1275
Paul Bakker5121ce52009-01-03 21:22:43 +00001276#endif /* rsa.h */