blob: 38784fc5a5fff6bf790491579a4525967383cba9 [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
Paul Bakker5121ce52009-01-03 21:22:43 +000057/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020058 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000059 */
Rose Zadik042e97f2018-01-26 16:35:10 +000060#define MBEDTLS_RSA_PUBLIC 0 /**< Request private key operation. */
61#define MBEDTLS_RSA_PRIVATE 1 /**< Request public key operation. */
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Rose Zadike8b5b992018-03-27 12:19:47 +010063#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
64#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Rose Zadik042e97f2018-01-26 16:35:10 +000066#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
67#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +000068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020070
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020071/*
72 * The above constants may be used even if the RSA module is compile out,
73 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
74 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010075
Paul Bakker407a0da2013-06-27 14:29:21 +020076#ifdef __cplusplus
77extern "C" {
78#endif
79
Ron Eldor4e6d55d2018-02-07 16:36:15 +020080#if !defined(MBEDTLS_RSA_ALT)
81// Regular implementation
82//
83
Paul Bakker5121ce52009-01-03 21:22:43 +000084/**
Rose Zadik042e97f2018-01-26 16:35:10 +000085 * \brief The RSA context structure.
Hanno Becker5063cd22017-09-29 11:49:12 +010086 *
87 * \note Direct manipulation of the members of this structure
Rose Zadik042e97f2018-01-26 16:35:10 +000088 * is deprecated. All manipulation should instead be done through
89 * the public interface functions.
Paul Bakker5121ce52009-01-03 21:22:43 +000090 */
Dawid Drozd428cc522018-07-24 10:02:47 +020091typedef struct mbedtls_rsa_context
Paul Bakker5121ce52009-01-03 21:22:43 +000092{
Gilles Peskine4337a9c2021-02-09 18:59:42 +010093 int ver; /*!< Reserved for internal purposes.
94 * Do not set this field in application
95 * code. Its meaning might change without
96 * notice. */
Rose Zadik042e97f2018-01-26 16:35:10 +000097 size_t len; /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +000098
Rose Zadike8b5b992018-03-27 12:19:47 +010099 mbedtls_mpi N; /*!< The public modulus. */
100 mbedtls_mpi E; /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
Rose Zadike8b5b992018-03-27 12:19:47 +0100102 mbedtls_mpi D; /*!< The private exponent. */
103 mbedtls_mpi P; /*!< The first prime factor. */
104 mbedtls_mpi Q; /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100105
Rose Zadikf2ec2882018-04-17 10:27:25 +0100106 mbedtls_mpi DP; /*!< <code>D % (P - 1)</code>. */
107 mbedtls_mpi DQ; /*!< <code>D % (Q - 1)</code>. */
108 mbedtls_mpi QP; /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
Rose Zadikf2ec2882018-04-17 10:27:25 +0100110 mbedtls_mpi RN; /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Rose Zadikf2ec2882018-04-17 10:27:25 +0100112 mbedtls_mpi RP; /*!< cached <code>R^2 mod P</code>. */
113 mbedtls_mpi RQ; /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200114
Rose Zadike8b5b992018-03-27 12:19:47 +0100115 mbedtls_mpi Vi; /*!< The cached blinding value. */
116 mbedtls_mpi Vf; /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000117
Rose Zadik042e97f2018-01-26 16:35:10 +0000118 int padding; /*!< Selects padding mode:
119 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
120 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
121 int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
122 as specified in md.h for use in the MGF
123 mask generating function used in the
124 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100126 /* Invariant: the mutex is initialized iff ver != 0. */
Rose Zadik042e97f2018-01-26 16:35:10 +0000127 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200128#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000129}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200132#else /* MBEDTLS_RSA_ALT */
133#include "rsa_alt.h"
134#endif /* MBEDTLS_RSA_ALT */
135
Paul Bakker5121ce52009-01-03 21:22:43 +0000136/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000137 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000139 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000140 * encryption scheme and the RSASSA-PSS signature scheme.
141 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000142 * \note The \p hash_id parameter is ignored when using
143 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200144 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000145 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200146 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000147 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100148 * a default value, which can be overridden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000149 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200150 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000151 * \note The hash selected in \p hash_id is always used for OEAP
152 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100153 * making signatures, but can be overridden for verifying them.
154 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100155 *
Hanno Becker9a467772018-12-13 09:54:59 +0000156 * \param ctx The RSA context to initialize. This must not be \c NULL.
157 * \param padding The padding mode to use. This must be either
158 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Hanno Becker385ce912018-12-13 18:33:12 +0000159 * \param hash_id The hash identifier of ::mbedtls_md_type_t type, if
Hanno Becker9a467772018-12-13 09:54:59 +0000160 * \p padding is #MBEDTLS_RSA_PKCS_V21. It is unused
161 * otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100164 int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000165 int hash_id );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
167/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000168 * \brief This function imports a set of core parameters into an
169 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100170 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100171 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000172 * imports, if the parameters are not simultaneously present.
173 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100174 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000175 * by a call to mbedtls_rsa_complete(), which checks and
176 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100177 * public or private RSA key.
178 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000179 * \note See mbedtls_rsa_complete() for more information on which
180 * parameters are necessary to set up a private or public
181 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100182 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100183 * \note The imported parameters are copied and need not be preserved
184 * for the lifetime of the RSA context being set up.
185 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100186 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000187 * \param N The RSA modulus. This may be \c NULL.
188 * \param P The first prime factor of \p N. This may be \c NULL.
189 * \param Q The second prime factor of \p N. This may be \c NULL.
190 * \param D The private exponent. This may be \c NULL.
191 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100192 *
193 * \return \c 0 on success.
194 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100195 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100196int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
197 const mbedtls_mpi *N,
198 const mbedtls_mpi *P, const mbedtls_mpi *Q,
199 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100200
201/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000202 * \brief This function imports core RSA parameters, in raw big-endian
203 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100205 * \note This function can be called multiple times for successive
206 * imports, if the parameters are not simultaneously present.
207 *
208 * Any sequence of calls to this function should be followed
209 * by a call to mbedtls_rsa_complete(), which checks and
210 * completes the provided information to a ready-for-use
211 * public or private RSA key.
212 *
213 * \note See mbedtls_rsa_complete() for more information on which
214 * parameters are necessary to set up a private or public
215 * RSA key.
216 *
217 * \note The imported parameters are copied and need not be preserved
218 * for the lifetime of the RSA context being set up.
219 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000220 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000221 * \param N The RSA modulus. This may be \c NULL.
222 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
223 * \param P The first prime factor of \p N. This may be \c NULL.
224 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
225 * \param Q The second prime factor of \p N. This may be \c NULL.
226 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
227 * \param D The private exponent. This may be \c NULL.
228 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
229 * \param E The public exponent. This may be \c NULL.
230 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100232 * \return \c 0 on success.
233 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100234 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100235int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100236 unsigned char const *N, size_t N_len,
237 unsigned char const *P, size_t P_len,
238 unsigned char const *Q, size_t Q_len,
239 unsigned char const *D, size_t D_len,
240 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100241
242/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000243 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100244 * a set of imported core parameters.
245 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000246 * To setup an RSA public key, precisely \p N and \p E
247 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100248 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000249 * To setup an RSA private key, sufficient information must
250 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100251 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000252 * The default implementation supports the following:
253 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
254 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
255 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100256 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000257 * If this function runs successfully, it guarantees that
258 * the RSA context can be used for RSA operations without
259 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100260 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100261 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000262 * for the imported parameters. In particular, parameters that
263 * are not needed by the implementation might be silently
264 * discarded and left unchecked. To check the consistency
265 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100266 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100267 * \param ctx The initialized RSA context holding imported parameters.
268 *
269 * \return \c 0 on success.
270 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
271 * failed.
272 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100273 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100274int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100275
276/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000277 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100278 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000279 * If this function runs successfully, the non-NULL buffers
280 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
281 * written, with additional unused space filled leading by
282 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100283 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000284 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300285 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000286 * <li>An alternative RSA implementation is in use, which
287 * stores the key externally, and either cannot or should
288 * not export it into RAM.</li>
289 * <li>A SW or HW implementation might not support a certain
290 * deduction. For example, \p P, \p Q from \p N, \p D,
291 * and \p E if the former are not part of the
292 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100293 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000294 * If the function fails due to an unsupported operation,
295 * the RSA context stays intact and remains usable.
296 *
297 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000298 * \param N The MPI to hold the RSA modulus.
299 * This may be \c NULL if this field need not be exported.
300 * \param P The MPI to hold the first prime factor of \p N.
301 * This may be \c NULL if this field need not be exported.
302 * \param Q The MPI to hold the second prime factor of \p N.
303 * This may be \c NULL if this field need not be exported.
304 * \param D The MPI to hold the private exponent.
305 * This may be \c NULL if this field need not be exported.
306 * \param E The MPI to hold the public exponent.
307 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000308 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100309 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300310 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000311 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100312 * functionality or because of security policies.
313 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100314 *
315 */
316int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
317 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
318 mbedtls_mpi *D, mbedtls_mpi *E );
319
320/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000321 * \brief This function exports core parameters of an RSA key
322 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100323 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000324 * If this function runs successfully, the non-NULL buffers
325 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
326 * written, with additional unused space filled leading by
327 * zero Bytes.
328 *
329 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300330 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000331 * <li>An alternative RSA implementation is in use, which
332 * stores the key externally, and either cannot or should
333 * not export it into RAM.</li>
334 * <li>A SW or HW implementation might not support a certain
335 * deduction. For example, \p P, \p Q from \p N, \p D,
336 * and \p E if the former are not part of the
337 * implementation.</li></ul>
338 * If the function fails due to an unsupported operation,
339 * the RSA context stays intact and remains usable.
340 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100341 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100342 * buffer pointers are NULL.
343 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000344 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000345 * \param N The Byte array to store the RSA modulus,
346 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000347 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000348 * \param P The Byte array to hold the first prime factor of \p N,
349 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000350 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000351 * \param Q The Byte array to hold the second prime factor of \p N,
352 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000353 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000354 * \param D The Byte array to hold the private exponent,
355 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000356 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000357 * \param E The Byte array to hold the public exponent,
358 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000359 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100360 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100361 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300362 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000363 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100364 * functionality or because of security policies.
365 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100366 */
367int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
368 unsigned char *N, size_t N_len,
369 unsigned char *P, size_t P_len,
370 unsigned char *Q, size_t Q_len,
371 unsigned char *D, size_t D_len,
372 unsigned char *E, size_t E_len );
373
374/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000375 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100376 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100377 * \note Alternative RSA implementations not using CRT-parameters
378 * internally can implement this function based on
379 * mbedtls_rsa_deduce_opt().
380 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000381 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000382 * \param DP The MPI to hold \c D modulo `P-1`,
383 * or \c NULL if it need not be exported.
384 * \param DQ The MPI to hold \c D modulo `Q-1`,
385 * or \c NULL if it need not be exported.
386 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
387 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100388 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100389 * \return \c 0 on success.
390 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100391 *
392 */
393int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
394 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
395
Paul Bakker5121ce52009-01-03 21:22:43 +0000396/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000397 * \brief This function sets padding for an already initialized RSA
398 * context. See mbedtls_rsa_init() for details.
Paul Bakker5121ce52009-01-03 21:22:43 +0000399 *
Hanno Becker9a467772018-12-13 09:54:59 +0000400 * \param ctx The initialized RSA context to be configured.
401 * \param padding The padding mode to use. This must be either
402 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Rose Zadik042e97f2018-01-26 16:35:10 +0000403 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Paul Bakker40e46942009-01-03 21:51:57 +0000404 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100405void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000406 int hash_id );
Paul Bakker21eb2802010-08-16 11:10:02 +0000407
Paul Bakkera3d195c2011-11-27 21:07:34 +0000408/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000409 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100410 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000411 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100412 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000413 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100414 *
415 */
416size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
417
418/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000419 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000421 * \note mbedtls_rsa_init() must be called before this function,
422 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000423 *
Hanno Becker9a467772018-12-13 09:54:59 +0000424 * \param ctx The initialized RSA context used to hold the key.
425 * \param f_rng The RNG function to be used for key generation.
426 * This must not be \c NULL.
427 * \param p_rng The RNG context to be passed to \p f_rng.
428 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100429 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000430 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000431 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100432 *
433 * \return \c 0 on success.
434 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100437 int (*f_rng)(void *, unsigned char *, size_t),
438 void *p_rng,
439 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000440
441/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000442 * \brief This function checks if a context contains at least an RSA
443 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000445 * If the function runs successfully, it is guaranteed that
446 * enough information is present to perform an RSA public key
447 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000448 *
Hanno Becker9a467772018-12-13 09:54:59 +0000449 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000450 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100451 * \return \c 0 on success.
452 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100453 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000456
457/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000458 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100459 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 *
Hanno Becker68767a62017-10-17 10:13:31 +0100461 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000462 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100463 * on the given context, but that the various parameters are
464 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000465 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100466 *
467 * \warning This function should catch accidental misconfigurations
468 * like swapping of parameters, but it cannot establish full
469 * trust in neither the quality nor the consistency of the key
470 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000471 * <ul><li>Consistency: Imported parameters that are irrelevant
472 * for the implementation might be silently dropped. If dropped,
473 * the current function does not have access to them,
474 * and therefore cannot check them. See mbedtls_rsa_complete().
475 * If you want to check the consistency of the entire
476 * content of an PKCS1-encoded RSA private key, for example, you
477 * should use mbedtls_rsa_validate_params() before setting
478 * up the RSA context.
479 * Additionally, if the implementation performs empirical checks,
480 * these checks substantiate but do not guarantee consistency.</li>
481 * <li>Quality: This function is not expected to perform
482 * extended quality assessments like checking that the prime
483 * factors are safe. Additionally, it is the responsibility of the
484 * user to ensure the trustworthiness of the source of his RSA
485 * parameters, which goes beyond what is effectively checkable
486 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100487 *
Hanno Becker9a467772018-12-13 09:54:59 +0000488 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100489 *
490 * \return \c 0 on success.
491 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000494
495/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000496 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100497 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000498 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100499 *
Hanno Becker9a467772018-12-13 09:54:59 +0000500 * \param pub The initialized RSA context holding the public key.
501 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000502 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100503 * \return \c 0 on success.
504 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100505 */
Hanno Becker98838b02017-10-02 13:16:10 +0100506int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
507 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100508
509/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000510 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 *
Hanno Becker9a467772018-12-13 09:54:59 +0000512 * \param ctx The initialized RSA context to use.
513 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000514 * of length \c ctx->len Bytes. For example, \c 256 Bytes
515 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000516 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000517 * of length \c ctx->len Bytes. For example, \c 256 Bytes
518 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000519 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000520 * \note This function does not handle message padding.
521 *
522 * \note Make sure to set \p input[0] = 0 or ensure that
523 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100525 * \return \c 0 on success.
526 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000527 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000529 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000530 unsigned char *output );
531
532/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000533 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 *
Hanno Becker24120612017-10-26 11:53:35 +0100535 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100536 *
537 * \note If blinding is used, both the base of exponentation
Hanno Becker24120612017-10-26 11:53:35 +0100538 * and the exponent are blinded, providing protection
539 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100540 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100541 * \warning It is deprecated and a security risk to not provide
542 * a PRNG here and thereby prevent the use of blinding.
543 * Future versions of the library may enforce the presence
544 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100545 *
Hanno Becker9a467772018-12-13 09:54:59 +0000546 * \param ctx The initialized RSA context to use.
547 * \param f_rng The RNG function, used for blinding. It is discouraged
548 * and deprecated to pass \c NULL here, in which case
549 * blinding will be omitted.
550 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
551 * if \p f_rng is \c NULL or if \p f_rng doesn't need a context.
552 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000553 * of length \c ctx->len Bytes. For example, \c 256 Bytes
554 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000555 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000556 * of length \c ctx->len Bytes. For example, \c 256 Bytes
557 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100558 *
559 * \return \c 0 on success.
560 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
561 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200564 int (*f_rng)(void *, unsigned char *, size_t),
565 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000566 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 unsigned char *output );
568
569/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000570 * \brief This function adds the message padding, then performs an RSA
571 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000573 * It is the generic wrapper for performing a PKCS#1 encryption
574 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100576 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000577 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
578 * are likely to remove the \p mode argument and have it
579 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100580 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100581 * \note Alternative implementations of RSA need not support
582 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300583 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100584 *
Hanno Becker9a467772018-12-13 09:54:59 +0000585 * \param ctx The initialized RSA context to use.
Hanno Becker974ca0d2018-12-18 18:03:24 +0000586 * \param f_rng The RNG to use. It is mandatory for PKCS#1 v2.1 padding
587 * encoding, and for PKCS#1 v1.5 padding encoding when used
588 * with \p mode set to #MBEDTLS_RSA_PUBLIC. For PKCS#1 v1.5
589 * padding encoding and \p mode set to #MBEDTLS_RSA_PRIVATE,
590 * it is used for blinding and should be provided in this
591 * case; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000592 * \param p_rng The RNG context to be passed to \p f_rng. May be
593 * \c NULL if \p f_rng is \c NULL or if \p f_rng doesn't
594 * need a context argument.
595 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000596 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000597 * \param ilen The length of the plaintext in Bytes.
598 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000599 * buffer of size \p ilen Bytes. It may be \c NULL if
600 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000601 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000602 * of length \c ctx->len Bytes. For example, \c 256 Bytes
603 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100604 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100605 * \return \c 0 on success.
606 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000609 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000610 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000611 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000612 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 unsigned char *output );
614
615/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000616 * \brief This function performs a PKCS#1 v1.5 encryption operation
617 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100618 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100619 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000620 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
621 * are likely to remove the \p mode argument and have it
622 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100623 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100624 * \note Alternative implementations of RSA need not support
625 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300626 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100627 *
Hanno Becker9a467772018-12-13 09:54:59 +0000628 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000629 * \param f_rng The RNG function to use. It is needed for padding generation
630 * if \p mode is #MBEDTLS_RSA_PUBLIC. If \p mode is
631 * #MBEDTLS_RSA_PRIVATE (discouraged), it is used for
632 * blinding and should be provided; see mbedtls_rsa_private().
Hanno Becker9a467772018-12-13 09:54:59 +0000633 * \param p_rng The RNG context to be passed to \p f_rng. This may
634 * be \c NULL if \p f_rng is \c NULL or if \p f_rng
635 * doesn't need a context argument.
636 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000637 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000638 * \param ilen The length of the plaintext in Bytes.
639 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000640 * buffer of size \p ilen Bytes. It may be \c NULL if
641 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000642 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000643 * of length \c ctx->len Bytes. For example, \c 256 Bytes
644 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100645 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100646 * \return \c 0 on success.
647 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100648 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100650 int (*f_rng)(void *, unsigned char *, size_t),
651 void *p_rng,
652 int mode, size_t ilen,
653 const unsigned char *input,
654 unsigned char *output );
655
656/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000657 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
658 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100659 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100660 * \note The output buffer must be as large as the size
661 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
662 *
663 * \deprecated It is deprecated and discouraged to call this function
664 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
665 * are likely to remove the \p mode argument and have it
666 * implicitly set to #MBEDTLS_RSA_PUBLIC.
667 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100668 * \note Alternative implementations of RSA need not support
669 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300670 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100671 *
Hanno Becker9a467772018-12-13 09:54:59 +0000672 * \param ctx The initnialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000673 * \param f_rng The RNG function to use. This is needed for padding
674 * generation and must be provided.
Hanno Becker9a467772018-12-13 09:54:59 +0000675 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000676 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000677 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000678 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000679 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000680 * This must be a readable buffer of length \p label_len
681 * Bytes. It may be \c NULL if \p label_len is \c 0.
682 * \param label_len The length of the label in Bytes.
683 * \param ilen The length of the plaintext buffer \p input in Bytes.
684 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000685 * buffer of size \p ilen Bytes. It may be \c NULL if
686 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000687 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000688 * of length \c ctx->len Bytes. For example, \c 256 Bytes
689 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100690 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100691 * \return \c 0 on success.
692 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100695 int (*f_rng)(void *, unsigned char *, size_t),
696 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100697 int mode,
698 const unsigned char *label, size_t label_len,
699 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100700 const unsigned char *input,
701 unsigned char *output );
702
703/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000704 * \brief This function performs an RSA operation, then removes the
705 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000706 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000707 * It is the generic wrapper for performing a PKCS#1 decryption
708 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000709 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100710 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000711 * as large as the size \p ctx->len of \p ctx->N (for example,
712 * 128 Bytes if RSA-1024 is used) to be able to hold an
713 * arbitrary decrypted message. If it is not large enough to
714 * hold the decryption of the particular ciphertext provided,
715 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100716 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100717 * \deprecated It is deprecated and discouraged to call this function
718 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
719 * are likely to remove the \p mode argument and have it
720 * implicitly set to #MBEDTLS_RSA_PRIVATE.
721 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100722 * \note Alternative implementations of RSA need not support
723 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300724 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100725 *
Hanno Becker9a467772018-12-13 09:54:59 +0000726 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000727 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
728 * this is used for blinding and should be provided; see
729 * mbedtls_rsa_private() for more. If \p mode is
730 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000731 * \param p_rng The RNG context to be passed to \p f_rng. This may be
732 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
733 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000734 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000735 * \param olen The address at which to store the length of
736 * the plaintext. This must not be \c NULL.
737 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000738 * of length \c ctx->len Bytes. For example, \c 256 Bytes
739 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000740 * \param output The buffer used to hold the plaintext. This must
741 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000742 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100743 *
744 * \return \c 0 on success.
745 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000746 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200748 int (*f_rng)(void *, unsigned char *, size_t),
749 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000750 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000751 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000752 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000753 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000754
755/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000756 * \brief This function performs a PKCS#1 v1.5 decryption
757 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100758 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100759 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000760 * as large as the size \p ctx->len of \p ctx->N, for example,
761 * 128 Bytes if RSA-1024 is used, to be able to hold an
762 * arbitrary decrypted message. If it is not large enough to
763 * hold the decryption of the particular ciphertext provided,
764 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100765 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100766 * \deprecated It is deprecated and discouraged to call this function
767 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
768 * are likely to remove the \p mode argument and have it
769 * implicitly set to #MBEDTLS_RSA_PRIVATE.
770 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100771 * \note Alternative implementations of RSA need not support
772 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300773 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100774 *
Hanno Becker9a467772018-12-13 09:54:59 +0000775 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000776 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
777 * this is used for blinding and should be provided; see
778 * mbedtls_rsa_private() for more. If \p mode is
779 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000780 * \param p_rng The RNG context to be passed to \p f_rng. This may be
781 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
782 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000783 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000784 * \param olen The address at which to store the length of
785 * the plaintext. This must not be \c NULL.
786 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000787 * of length \c ctx->len Bytes. For example, \c 256 Bytes
788 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000789 * \param output The buffer used to hold the plaintext. This must
790 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000791 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100792 *
793 * \return \c 0 on success.
794 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
795 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100796 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200798 int (*f_rng)(void *, unsigned char *, size_t),
799 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100800 int mode, size_t *olen,
801 const unsigned char *input,
802 unsigned char *output,
803 size_t output_max_len );
804
805/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100806 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
807 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100808 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100809 * \note The output buffer length \c output_max_len should be
810 * as large as the size \p ctx->len of \p ctx->N, for
811 * example, 128 Bytes if RSA-1024 is used, to be able to
812 * hold an arbitrary decrypted message. If it is not
813 * large enough to hold the decryption of the particular
814 * ciphertext provided, the function returns
815 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100816 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100817 * \deprecated It is deprecated and discouraged to call this function
818 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
819 * are likely to remove the \p mode argument and have it
820 * implicitly set to #MBEDTLS_RSA_PRIVATE.
821 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100822 * \note Alternative implementations of RSA need not support
823 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300824 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100825 *
Hanno Becker9a467772018-12-13 09:54:59 +0000826 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000827 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
828 * this is used for blinding and should be provided; see
829 * mbedtls_rsa_private() for more. If \p mode is
830 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000831 * \param p_rng The RNG context to be passed to \p f_rng. This may be
832 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
833 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000834 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100835 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000836 * This must be a readable buffer of length \p label_len
837 * Bytes. It may be \c NULL if \p label_len is \c 0.
838 * \param label_len The length of the label in Bytes.
839 * \param olen The address at which to store the length of
840 * the plaintext. This must not be \c NULL.
841 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000842 * of length \c ctx->len Bytes. For example, \c 256 Bytes
843 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000844 * \param output The buffer used to hold the plaintext. This must
845 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000846 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100847 *
848 * \return \c 0 on success.
849 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100850 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200852 int (*f_rng)(void *, unsigned char *, size_t),
853 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100854 int mode,
855 const unsigned char *label, size_t label_len,
856 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100857 const unsigned char *input,
858 unsigned char *output,
859 size_t output_max_len );
860
861/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000862 * \brief This function performs a private RSA operation to sign
863 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000864 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000865 * It is the generic wrapper for performing a PKCS#1
866 * signature using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000867 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000868 * \note The \p sig buffer must be as large as the size
869 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000870 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000871 * \note For PKCS#1 v2.1 encoding, see comments on
872 * mbedtls_rsa_rsassa_pss_sign() for details on
873 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100874 *
875 * \deprecated It is deprecated and discouraged to call this function
876 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
877 * are likely to remove the \p mode argument and have it
878 * implicitly set to #MBEDTLS_RSA_PRIVATE.
879 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100880 * \note Alternative implementations of RSA need not support
881 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300882 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100883 *
Hanno Becker9a467772018-12-13 09:54:59 +0000884 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000885 * \param f_rng The RNG function to use. If the padding mode is PKCS#1 v2.1,
886 * this must be provided. If the padding mode is PKCS#1 v1.5 and
887 * \p mode is #MBEDTLS_RSA_PRIVATE, it is used for blinding
888 * and should be provided; see mbedtls_rsa_private() for more
889 * more. It is ignored otherwise.
Hanno Becker9a467772018-12-13 09:54:59 +0000890 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
891 * if \p f_rng is \c NULL or doesn't need a context argument.
892 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000893 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100894 * \param md_alg The message-digest algorithm used to hash the original data.
895 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000896 * \param hashlen The length of the message digest.
897 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
898 * \param hash The buffer holding the message digest or raw data.
899 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
900 * buffer of length \p hashlen Bytes. If \p md_alg is not
901 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
902 * the size of the hash corresponding to \p md_alg.
903 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000904 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100905 * for an 2048-bit RSA modulus. A buffer length of
906 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100907 *
908 * \return \c 0 if the signing operation was successful.
909 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000910 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000912 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000913 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000914 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000916 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000917 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000918 unsigned char *sig );
919
920/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000921 * \brief This function performs a PKCS#1 v1.5 signature
922 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100923 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100924 * \deprecated It is deprecated and discouraged to call this function
925 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
926 * are likely to remove the \p mode argument and have it
927 * implicitly set to #MBEDTLS_RSA_PRIVATE.
928 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100929 * \note Alternative implementations of RSA need not support
930 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300931 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100932 *
Hanno Becker9a467772018-12-13 09:54:59 +0000933 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000934 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
935 * this is used for blinding and should be provided; see
936 * mbedtls_rsa_private() for more. If \p mode is
937 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000938 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
939 * if \p f_rng is \c NULL or doesn't need a context argument.
940 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000941 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000942 * \param md_alg The message-digest algorithm used to hash the original data.
943 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000944 * \param hashlen The length of the message digest.
945 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
946 * \param hash The buffer holding the message digest or raw data.
947 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
948 * buffer of length \p hashlen Bytes. If \p md_alg is not
949 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
950 * the size of the hash corresponding to \p md_alg.
951 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000952 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100953 * for an 2048-bit RSA modulus. A buffer length of
954 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100955 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100956 * \return \c 0 if the signing operation was successful.
957 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100958 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200960 int (*f_rng)(void *, unsigned char *, size_t),
961 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100962 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100964 unsigned int hashlen,
965 const unsigned char *hash,
966 unsigned char *sig );
967
968/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000969 * \brief This function performs a PKCS#1 v2.1 PSS signature
970 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100971 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000972 * \note The \p hash_id in the RSA context is the one used for the
973 * encoding. \p md_alg in the function call is the type of hash
974 * that is encoded. According to <em>RFC-3447: Public-Key
975 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
976 * Specifications</em> it is advised to keep both hashes the
977 * same.
Rose Zadike8b5b992018-03-27 12:19:47 +0100978 *
Cédric Meuter010ddc22020-04-25 09:24:11 +0200979 * \note This function enforces that the provided salt length complies
980 * with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 v2.2) §9.1.1
981 * step 3. The constraint is that the hash length plus the salt
982 * length plus 2 bytes must be at most the key length. If this
983 * constraint is not met, this function returns
Jaeden Amero3725bb22018-09-07 19:12:36 +0100984 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
985 *
Hanno Becker9a467772018-12-13 09:54:59 +0000986 * \param ctx The initialized RSA context to use.
987 * \param f_rng The RNG function. It must not be \c NULL.
988 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
989 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100990 * \param md_alg The message-digest algorithm used to hash the original data.
991 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000992 * \param hashlen The length of the message digest.
993 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
994 * \param hash The buffer holding the message digest or raw data.
995 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
996 * buffer of length \p hashlen Bytes. If \p md_alg is not
997 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
998 * the size of the hash corresponding to \p md_alg.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200999 * \param saltlen The length of the salt that should be used.
Cédric Meuter010ddc22020-04-25 09:24:11 +02001000 * If passed #MBEDTLS_RSA_SALT_LEN_ANY, the function will use
1001 * the largest possible salt length up to the hash length,
1002 * which is the largest permitted by some standards including
1003 * FIPS 186-4 §5.5.
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001004 * \param sig The buffer to hold the signature. This must be a writable
1005 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1006 * for an 2048-bit RSA modulus. A buffer length of
1007 * #MBEDTLS_MPI_MAX_SIZE is always safe.
1008 *
1009 * \return \c 0 if the signing operation was successful.
1010 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
1011 */
1012int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
1013 int (*f_rng)(void *, unsigned char *, size_t),
1014 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001015 mbedtls_md_type_t md_alg,
1016 unsigned int hashlen,
1017 const unsigned char *hash,
1018 int saltlen,
1019 unsigned char *sig );
1020
1021/**
1022 * \brief This function performs a PKCS#1 v2.1 PSS signature
1023 * operation (RSASSA-PSS-SIGN).
1024 *
1025 * \note The \p hash_id in the RSA context is the one used for the
1026 * encoding. \p md_alg in the function call is the type of hash
1027 * that is encoded. According to <em>RFC-3447: Public-Key
1028 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1029 * Specifications</em> it is advised to keep both hashes the
1030 * same.
1031 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001032 * \note This function always uses the maximum possible salt size,
1033 * up to the length of the payload hash. This choice of salt
Paul Bakkerb3869132013-02-28 17:21:01 +01001034 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
1035 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
Rose Zadike8b5b992018-03-27 12:19:47 +01001036 * minimum salt size which is the hash size minus 2 bytes. If
1037 * this minimum size is too large given the key size (the salt
1038 * size, plus the hash size, plus 2 bytes must be no more than
1039 * the key size in bytes), this function returns
1040 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
1041 *
1042 * \deprecated It is deprecated and discouraged to call this function
1043 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
1044 * are likely to remove the \p mode argument and have it
1045 * implicitly set to #MBEDTLS_RSA_PRIVATE.
1046 *
1047 * \note Alternative implementations of RSA need not support
1048 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
1049 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
1050 *
1051 * \param ctx The initialized RSA context to use.
1052 * \param f_rng The RNG function. It must not be \c NULL.
1053 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
1054 * if \p f_rng doesn't need a context argument.
Paul Bakkerb3869132013-02-28 17:21:01 +01001055 * \param mode The mode of operation. This must be either
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Paul Bakkerb3869132013-02-28 17:21:01 +01001057 * \param md_alg The message-digest algorithm used to hash the original data.
1058 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001059 * \param hashlen The length of the message digest.
1060 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
1061 * \param hash The buffer holding the message digest or raw data.
1062 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1063 * buffer of length \p hashlen Bytes. If \p md_alg is not
1064 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1065 * the size of the hash corresponding to \p md_alg.
1066 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +00001067 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +01001068 * for an 2048-bit RSA modulus. A buffer length of
1069 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +01001070 *
1071 * \return \c 0 if the signing operation was successful.
1072 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
1073 */
1074int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1075 int (*f_rng)(void *, unsigned char *, size_t),
1076 void *p_rng,
1077 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001079 unsigned int hashlen,
1080 const unsigned char *hash,
1081 unsigned char *sig );
1082
1083/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001084 * \brief This function performs a public RSA operation and checks
1085 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +00001086 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001087 * This is the generic wrapper for performing a PKCS#1
1088 * verification using the mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001089 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001090 * \note For PKCS#1 v2.1 encoding, see comments on
1091 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
1092 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +01001093 *
1094 * \deprecated It is deprecated and discouraged to call this function
1095 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1096 * are likely to remove the \p mode argument and have it
1097 * set to #MBEDTLS_RSA_PUBLIC.
1098 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001099 * \note Alternative implementations of RSA need not support
1100 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001101 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001102 *
Hanno Becker9a467772018-12-13 09:54:59 +00001103 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001104 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1105 * this is used for blinding and should be provided; see
1106 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001107 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1108 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1109 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001110 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001111 * \param md_alg The message-digest algorithm used to hash the original data.
1112 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001113 * \param hashlen The length of the message digest.
1114 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1115 * \param hash The buffer holding the message digest or raw data.
1116 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1117 * buffer of length \p hashlen Bytes. If \p md_alg is not
1118 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1119 * the size of the hash corresponding to \p md_alg.
1120 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001121 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1122 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001123 *
1124 * \return \c 0 if the verify operation was successful.
1125 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001128 int (*f_rng)(void *, unsigned char *, size_t),
1129 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001132 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001133 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001134 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +00001135
1136/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001137 * \brief This function performs a PKCS#1 v1.5 verification
1138 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001139 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001140 * \deprecated It is deprecated and discouraged to call this function
1141 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1142 * are likely to remove the \p mode argument and have it
1143 * set to #MBEDTLS_RSA_PUBLIC.
1144 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001145 * \note Alternative implementations of RSA need not support
1146 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001147 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001148 *
Hanno Becker9a467772018-12-13 09:54:59 +00001149 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001150 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1151 * this is used for blinding and should be provided; see
1152 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001153 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1154 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1155 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001156 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +00001157 * \param md_alg The message-digest algorithm used to hash the original data.
1158 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001159 * \param hashlen The length of the message digest.
1160 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1161 * \param hash The buffer holding the message digest or raw data.
1162 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1163 * buffer of length \p hashlen Bytes. If \p md_alg is not
1164 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1165 * the size of the hash corresponding to \p md_alg.
1166 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001167 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1168 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001169 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001170 * \return \c 0 if the verify operation was successful.
1171 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001174 int (*f_rng)(void *, unsigned char *, size_t),
1175 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001176 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001178 unsigned int hashlen,
1179 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001180 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001181
1182/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001183 * \brief This function performs a PKCS#1 v2.1 PSS verification
1184 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001185 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001186 * The hash function for the MGF mask generating function
1187 * is that specified in the RSA context.
Paul Bakkerb3869132013-02-28 17:21:01 +01001188 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001189 * \note The \p hash_id in the RSA context is the one used for the
1190 * verification. \p md_alg in the function call is the type of
1191 * hash that is verified. According to <em>RFC-3447: Public-Key
1192 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1193 * Specifications</em> it is advised to keep both hashes the
1194 * same. If \p hash_id in the RSA context is unset,
1195 * the \p md_alg from the function call is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001196 *
1197 * \deprecated It is deprecated and discouraged to call this function
1198 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1199 * are likely to remove the \p mode argument and have it
1200 * implicitly set to #MBEDTLS_RSA_PUBLIC.
1201 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001202 * \note Alternative implementations of RSA need not support
1203 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001204 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001205 *
Hanno Becker9a467772018-12-13 09:54:59 +00001206 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001207 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1208 * this is used for blinding and should be provided; see
1209 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001210 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1211 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1212 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001213 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001214 * \param md_alg The message-digest algorithm used to hash the original data.
1215 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001216 * \param hashlen The length of the message digest.
1217 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1218 * \param hash The buffer holding the message digest or raw data.
1219 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1220 * buffer of length \p hashlen Bytes. If \p md_alg is not
1221 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1222 * the size of the hash corresponding to \p md_alg.
1223 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001224 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1225 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001226 *
1227 * \return \c 0 if the verify operation was successful.
1228 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001231 int (*f_rng)(void *, unsigned char *, size_t),
1232 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001233 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001235 unsigned int hashlen,
1236 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001237 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001238
1239/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001240 * \brief This function performs a PKCS#1 v2.1 PSS verification
1241 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001242 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001243 * The hash function for the MGF mask generating function
1244 * is that specified in \p mgf1_hash_id.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001245 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001246 * \note The \p sig buffer must be as large as the size
1247 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001248 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001249 * \note The \p hash_id in the RSA context is ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001250 *
Hanno Becker9a467772018-12-13 09:54:59 +00001251 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001252 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1253 * this is used for blinding and should be provided; see
1254 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001255 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1256 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1257 * \param mode The mode of operation. This must be either
1258 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +01001259 * \param md_alg The message-digest algorithm used to hash the original data.
1260 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001261 * \param hashlen The length of the message digest.
1262 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1263 * \param hash The buffer holding the message digest or raw data.
1264 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1265 * buffer of length \p hashlen Bytes. If \p md_alg is not
1266 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1267 * the size of the hash corresponding to \p md_alg.
1268 * \param mgf1_hash_id The message digest used for mask generation.
1269 * \param expected_salt_len The length of the salt used in padding. Use
1270 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1271 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001272 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1273 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001274 *
1275 * \return \c 0 if the verify operation was successful.
1276 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001279 int (*f_rng)(void *, unsigned char *, size_t),
1280 void *p_rng,
1281 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001283 unsigned int hashlen,
1284 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001286 int expected_salt_len,
1287 const unsigned char *sig );
1288
1289/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001290 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001291 *
Hanno Becker9a467772018-12-13 09:54:59 +00001292 * \param dst The destination context. This must be initialized.
1293 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001294 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001295 * \return \c 0 on success.
1296 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001299
1300/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001301 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001302 *
Hanno Becker9a467772018-12-13 09:54:59 +00001303 * \param ctx The RSA context to free. May be \c NULL, in which case
1304 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001305 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001306 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001308
Ron Eldorfa8f6352017-06-20 15:48:46 +03001309#if defined(MBEDTLS_SELF_TEST)
1310
Paul Bakker5121ce52009-01-03 21:22:43 +00001311/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001312 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001314 * \return \c 0 on success.
1315 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001318
Ron Eldorfa8f6352017-06-20 15:48:46 +03001319#endif /* MBEDTLS_SELF_TEST */
1320
Paul Bakker5121ce52009-01-03 21:22:43 +00001321#ifdef __cplusplus
1322}
1323#endif
1324
Paul Bakker5121ce52009-01-03 21:22:43 +00001325#endif /* rsa.h */