blob: b2f65334fe74d99312440dd74df7527828b3eb17 [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útia2947ac2020-08-19 16:37:36 +020013 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +020014 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
15 *
16 * This file is provided under the Apache License 2.0, or the
17 * GNU General Public License v2.0 or later.
18 *
19 * **********
20 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020021 *
22 * Licensed under the Apache License, Version 2.0 (the "License"); you may
23 * not use this file except in compliance with the License.
24 * You may obtain a copy of the License at
25 *
26 * http://www.apache.org/licenses/LICENSE-2.0
27 *
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
30 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000033 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020034 * **********
35 *
36 * **********
37 * GNU General Public License v2.0 or later:
38 *
39 * This program is free software; you can redistribute it and/or modify
40 * it under the terms of the GNU General Public License as published by
41 * the Free Software Foundation; either version 2 of the License, or
42 * (at your option) any later version.
43 *
44 * This program is distributed in the hope that it will be useful,
45 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47 * GNU General Public License for more details.
48 *
49 * You should have received a copy of the GNU General Public License along
50 * with this program; if not, write to the Free Software Foundation, Inc.,
51 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
52 *
53 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#ifndef MBEDTLS_RSA_H
56#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020059#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020060#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020062#endif
Paul Bakkered27a042013-04-18 22:46:23 +020063
Paul Bakker314052f2011-08-15 09:07:52 +000064#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020065#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_THREADING_C)
Paul Bakkerc9965dc2013-09-29 14:58:17 +020068#include "threading.h"
69#endif
70
Paul Bakker13e2dfe2009-07-28 07:18:38 +000071/*
72 * RSA Error codes
73 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
75#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
76#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Rose Zadik042e97f2018-01-26 16:35:10 +000077#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 +020078#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
79#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
80#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
81#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
82#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030083
84/* MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is deprecated and should not be used.
85 */
Rose Zadik042e97f2018-01-26 16:35:10 +000086#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 +030087
88/* MBEDTLS_ERR_RSA_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010089#define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580 /**< RSA hardware accelerator failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000090
91/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020092 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000093 */
Rose Zadik042e97f2018-01-26 16:35:10 +000094#define MBEDTLS_RSA_PUBLIC 0 /**< Request private key operation. */
95#define MBEDTLS_RSA_PRIVATE 1 /**< Request public key operation. */
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Rose Zadike8b5b992018-03-27 12:19:47 +010097#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
98#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Rose Zadik042e97f2018-01-26 16:35:10 +0000100#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
101#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200104
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +0200105/*
106 * The above constants may be used even if the RSA module is compile out,
107 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
108 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +0100109
Paul Bakker407a0da2013-06-27 14:29:21 +0200110#ifdef __cplusplus
111extern "C" {
112#endif
113
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200114#if !defined(MBEDTLS_RSA_ALT)
115// Regular implementation
116//
117
Paul Bakker5121ce52009-01-03 21:22:43 +0000118/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000119 * \brief The RSA context structure.
Hanno Becker5063cd22017-09-29 11:49:12 +0100120 *
121 * \note Direct manipulation of the members of this structure
Rose Zadik042e97f2018-01-26 16:35:10 +0000122 * is deprecated. All manipulation should instead be done through
123 * the public interface functions.
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200125typedef struct mbedtls_rsa_context
Paul Bakker5121ce52009-01-03 21:22:43 +0000126{
Gilles Peskinece455dd2021-02-09 18:59:42 +0100127 int ver; /*!< Reserved for internal purposes.
128 * Do not set this field in application
129 * code. Its meaning might change without
130 * notice. */
Rose Zadik042e97f2018-01-26 16:35:10 +0000131 size_t len; /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Rose Zadike8b5b992018-03-27 12:19:47 +0100133 mbedtls_mpi N; /*!< The public modulus. */
134 mbedtls_mpi E; /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
Rose Zadike8b5b992018-03-27 12:19:47 +0100136 mbedtls_mpi D; /*!< The private exponent. */
137 mbedtls_mpi P; /*!< The first prime factor. */
138 mbedtls_mpi Q; /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100139
Rose Zadikf2ec2882018-04-17 10:27:25 +0100140 mbedtls_mpi DP; /*!< <code>D % (P - 1)</code>. */
141 mbedtls_mpi DQ; /*!< <code>D % (Q - 1)</code>. */
142 mbedtls_mpi QP; /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
Rose Zadikf2ec2882018-04-17 10:27:25 +0100144 mbedtls_mpi RN; /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
Rose Zadikf2ec2882018-04-17 10:27:25 +0100146 mbedtls_mpi RP; /*!< cached <code>R^2 mod P</code>. */
147 mbedtls_mpi RQ; /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200148
Rose Zadike8b5b992018-03-27 12:19:47 +0100149 mbedtls_mpi Vi; /*!< The cached blinding value. */
150 mbedtls_mpi Vf; /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000151
Rose Zadik042e97f2018-01-26 16:35:10 +0000152 int padding; /*!< Selects padding mode:
153 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
154 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
155 int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
156 as specified in md.h for use in the MGF
157 mask generating function used in the
158 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#if defined(MBEDTLS_THREADING_C)
Gilles Peskinece455dd2021-02-09 18:59:42 +0100160 /* Invariant: the mutex is initialized iff ver != 0. */
Rose Zadik042e97f2018-01-26 16:35:10 +0000161 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200162#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000163}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200166#else /* MBEDTLS_RSA_ALT */
167#include "rsa_alt.h"
168#endif /* MBEDTLS_RSA_ALT */
169
Paul Bakker5121ce52009-01-03 21:22:43 +0000170/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000171 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000173 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000174 * encryption scheme and the RSASSA-PSS signature scheme.
175 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000176 * \note The \p hash_id parameter is ignored when using
177 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200178 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000179 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200180 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000181 * mixing padding modes. For public key operations it is
Antonin Décimod5f47592019-01-23 15:24:37 +0100182 * a default value, which can be overridden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000183 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200184 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000185 * \note The hash selected in \p hash_id is always used for OEAP
186 * encryption. For PSS signatures, it is always used for
Antonin Décimod5f47592019-01-23 15:24:37 +0100187 * making signatures, but can be overridden for verifying them.
188 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100189 *
Hanno Becker9a467772018-12-13 09:54:59 +0000190 * \param ctx The RSA context to initialize. This must not be \c NULL.
191 * \param padding The padding mode to use. This must be either
192 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Hanno Becker385ce912018-12-13 18:33:12 +0000193 * \param hash_id The hash identifier of ::mbedtls_md_type_t type, if
Hanno Becker9a467772018-12-13 09:54:59 +0000194 * \p padding is #MBEDTLS_RSA_PKCS_V21. It is unused
195 * otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100198 int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000199 int hash_id );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
201/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000202 * \brief This function imports a set of core parameters into an
203 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100204 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100205 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000206 * imports, if the parameters are not simultaneously present.
207 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100208 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000209 * by a call to mbedtls_rsa_complete(), which checks and
210 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100211 * public or private RSA key.
212 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000213 * \note See mbedtls_rsa_complete() for more information on which
214 * parameters are necessary to set up a private or public
215 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100216 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100217 * \note The imported parameters are copied and need not be preserved
218 * for the lifetime of the RSA context being set up.
219 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100220 * \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 P The first prime factor of \p N. This may be \c NULL.
223 * \param Q The second prime factor of \p N. This may be \c NULL.
224 * \param D The private exponent. This may be \c NULL.
225 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100226 *
227 * \return \c 0 on success.
228 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100229 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100230int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
231 const mbedtls_mpi *N,
232 const mbedtls_mpi *P, const mbedtls_mpi *Q,
233 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100234
235/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000236 * \brief This function imports core RSA parameters, in raw big-endian
237 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100239 * \note This function can be called multiple times for successive
240 * imports, if the parameters are not simultaneously present.
241 *
242 * Any sequence of calls to this function should be followed
243 * by a call to mbedtls_rsa_complete(), which checks and
244 * completes the provided information to a ready-for-use
245 * public or private RSA key.
246 *
247 * \note See mbedtls_rsa_complete() for more information on which
248 * parameters are necessary to set up a private or public
249 * RSA key.
250 *
251 * \note The imported parameters are copied and need not be preserved
252 * for the lifetime of the RSA context being set up.
253 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000254 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000255 * \param N The RSA modulus. This may be \c NULL.
256 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
257 * \param P The first prime factor of \p N. This may be \c NULL.
258 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
259 * \param Q The second prime factor of \p N. This may be \c NULL.
260 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
261 * \param D The private exponent. This may be \c NULL.
262 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
263 * \param E The public exponent. This may be \c NULL.
264 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100266 * \return \c 0 on success.
267 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100268 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100269int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100270 unsigned char const *N, size_t N_len,
271 unsigned char const *P, size_t P_len,
272 unsigned char const *Q, size_t Q_len,
273 unsigned char const *D, size_t D_len,
274 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100275
276/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000277 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100278 * a set of imported core parameters.
279 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000280 * To setup an RSA public key, precisely \p N and \p E
281 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100282 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000283 * To setup an RSA private key, sufficient information must
284 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100285 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000286 * The default implementation supports the following:
287 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
288 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
289 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100290 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000291 * If this function runs successfully, it guarantees that
292 * the RSA context can be used for RSA operations without
293 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100294 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100295 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000296 * for the imported parameters. In particular, parameters that
297 * are not needed by the implementation might be silently
298 * discarded and left unchecked. To check the consistency
299 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100300 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100301 * \param ctx The initialized RSA context holding imported parameters.
302 *
303 * \return \c 0 on success.
304 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
305 * failed.
306 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100307 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100308int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100309
310/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000311 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100312 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000313 * If this function runs successfully, the non-NULL buffers
314 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
315 * written, with additional unused space filled leading by
316 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100317 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000318 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300319 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000320 * <li>An alternative RSA implementation is in use, which
321 * stores the key externally, and either cannot or should
322 * not export it into RAM.</li>
323 * <li>A SW or HW implementation might not support a certain
324 * deduction. For example, \p P, \p Q from \p N, \p D,
325 * and \p E if the former are not part of the
326 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100327 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000328 * If the function fails due to an unsupported operation,
329 * the RSA context stays intact and remains usable.
330 *
331 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000332 * \param N The MPI to hold the RSA modulus.
333 * This may be \c NULL if this field need not be exported.
334 * \param P The MPI to hold the first prime factor of \p N.
335 * This may be \c NULL if this field need not be exported.
336 * \param Q The MPI to hold the second prime factor of \p N.
337 * This may be \c NULL if this field need not be exported.
338 * \param D The MPI to hold the private exponent.
339 * This may be \c NULL if this field need not be exported.
340 * \param E The MPI to hold the public exponent.
341 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000342 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100343 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300344 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000345 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100346 * functionality or because of security policies.
347 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100348 *
349 */
350int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
351 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
352 mbedtls_mpi *D, mbedtls_mpi *E );
353
354/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000355 * \brief This function exports core parameters of an RSA key
356 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100357 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000358 * If this function runs successfully, the non-NULL buffers
359 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
360 * written, with additional unused space filled leading by
361 * zero Bytes.
362 *
363 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300364 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000365 * <li>An alternative RSA implementation is in use, which
366 * stores the key externally, and either cannot or should
367 * not export it into RAM.</li>
368 * <li>A SW or HW implementation might not support a certain
369 * deduction. For example, \p P, \p Q from \p N, \p D,
370 * and \p E if the former are not part of the
371 * implementation.</li></ul>
372 * If the function fails due to an unsupported operation,
373 * the RSA context stays intact and remains usable.
374 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100375 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100376 * buffer pointers are NULL.
377 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000378 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000379 * \param N The Byte array to store the RSA modulus,
380 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000381 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000382 * \param P The Byte array to hold the first prime factor of \p N,
383 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000384 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000385 * \param Q The Byte array to hold the second prime factor of \p N,
386 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000387 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000388 * \param D The Byte array to hold the private exponent,
389 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000390 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000391 * \param E The Byte array to hold the public exponent,
392 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000393 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100394 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100395 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300396 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000397 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100398 * functionality or because of security policies.
399 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100400 */
401int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
402 unsigned char *N, size_t N_len,
403 unsigned char *P, size_t P_len,
404 unsigned char *Q, size_t Q_len,
405 unsigned char *D, size_t D_len,
406 unsigned char *E, size_t E_len );
407
408/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000409 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100410 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100411 * \note Alternative RSA implementations not using CRT-parameters
412 * internally can implement this function based on
413 * mbedtls_rsa_deduce_opt().
414 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000415 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000416 * \param DP The MPI to hold \c D modulo `P-1`,
417 * or \c NULL if it need not be exported.
418 * \param DQ The MPI to hold \c D modulo `Q-1`,
419 * or \c NULL if it need not be exported.
420 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
421 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100422 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100423 * \return \c 0 on success.
424 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100425 *
426 */
427int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
428 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
429
Paul Bakker5121ce52009-01-03 21:22:43 +0000430/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000431 * \brief This function sets padding for an already initialized RSA
432 * context. See mbedtls_rsa_init() for details.
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 *
Hanno Becker9a467772018-12-13 09:54:59 +0000434 * \param ctx The initialized RSA context to be configured.
435 * \param padding The padding mode to use. This must be either
436 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Rose Zadik042e97f2018-01-26 16:35:10 +0000437 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Paul Bakker40e46942009-01-03 21:51:57 +0000438 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100439void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000440 int hash_id );
Paul Bakker21eb2802010-08-16 11:10:02 +0000441
Paul Bakkera3d195c2011-11-27 21:07:34 +0000442/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000443 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100444 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000445 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100446 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000447 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100448 *
449 */
450size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
451
452/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000453 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000455 * \note mbedtls_rsa_init() must be called before this function,
456 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 *
Hanno Becker9a467772018-12-13 09:54:59 +0000458 * \param ctx The initialized RSA context used to hold the key.
459 * \param f_rng The RNG function to be used for key generation.
460 * This must not be \c NULL.
461 * \param p_rng The RNG context to be passed to \p f_rng.
462 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100463 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000464 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000465 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100466 *
467 * \return \c 0 on success.
468 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100471 int (*f_rng)(void *, unsigned char *, size_t),
472 void *p_rng,
473 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000474
475/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000476 * \brief This function checks if a context contains at least an RSA
477 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000479 * If the function runs successfully, it is guaranteed that
480 * enough information is present to perform an RSA public key
481 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000482 *
Hanno Becker9a467772018-12-13 09:54:59 +0000483 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000484 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100485 * \return \c 0 on success.
486 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100487 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000490
491/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000492 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100493 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000494 *
Hanno Becker68767a62017-10-17 10:13:31 +0100495 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000496 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100497 * on the given context, but that the various parameters are
498 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000499 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100500 *
501 * \warning This function should catch accidental misconfigurations
502 * like swapping of parameters, but it cannot establish full
503 * trust in neither the quality nor the consistency of the key
504 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000505 * <ul><li>Consistency: Imported parameters that are irrelevant
506 * for the implementation might be silently dropped. If dropped,
507 * the current function does not have access to them,
508 * and therefore cannot check them. See mbedtls_rsa_complete().
509 * If you want to check the consistency of the entire
510 * content of an PKCS1-encoded RSA private key, for example, you
511 * should use mbedtls_rsa_validate_params() before setting
512 * up the RSA context.
513 * Additionally, if the implementation performs empirical checks,
514 * these checks substantiate but do not guarantee consistency.</li>
515 * <li>Quality: This function is not expected to perform
516 * extended quality assessments like checking that the prime
517 * factors are safe. Additionally, it is the responsibility of the
518 * user to ensure the trustworthiness of the source of his RSA
519 * parameters, which goes beyond what is effectively checkable
520 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100521 *
Hanno Becker9a467772018-12-13 09:54:59 +0000522 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100523 *
524 * \return \c 0 on success.
525 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000530 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100531 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000532 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100533 *
Hanno Becker9a467772018-12-13 09:54:59 +0000534 * \param pub The initialized RSA context holding the public key.
535 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000536 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100537 * \return \c 0 on success.
538 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100539 */
Hanno Becker98838b02017-10-02 13:16:10 +0100540int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
541 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100542
543/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000544 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 *
Hanno Becker9a467772018-12-13 09:54:59 +0000546 * \param ctx The initialized RSA context to use.
547 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000548 * of length \c ctx->len Bytes. For example, \c 256 Bytes
549 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000550 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000551 * of length \c ctx->len Bytes. For example, \c 256 Bytes
552 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000553 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000554 * \note This function does not handle message padding.
555 *
556 * \note Make sure to set \p input[0] = 0 or ensure that
557 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100559 * \return \c 0 on success.
560 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000563 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 unsigned char *output );
565
566/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000567 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 *
Hanno Becker24120612017-10-26 11:53:35 +0100569 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100570 *
571 * \note If blinding is used, both the base of exponentation
Hanno Becker24120612017-10-26 11:53:35 +0100572 * and the exponent are blinded, providing protection
573 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100574 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100575 * \warning It is deprecated and a security risk to not provide
576 * a PRNG here and thereby prevent the use of blinding.
577 * Future versions of the library may enforce the presence
578 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100579 *
Hanno Becker9a467772018-12-13 09:54:59 +0000580 * \param ctx The initialized RSA context to use.
581 * \param f_rng The RNG function, used for blinding. It is discouraged
582 * and deprecated to pass \c NULL here, in which case
583 * blinding will be omitted.
584 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
585 * if \p f_rng is \c NULL or if \p f_rng doesn't need a context.
586 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000587 * of length \c ctx->len Bytes. For example, \c 256 Bytes
588 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000589 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000590 * of length \c ctx->len Bytes. For example, \c 256 Bytes
591 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100592 *
593 * \return \c 0 on success.
594 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
595 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200598 int (*f_rng)(void *, unsigned char *, size_t),
599 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000600 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000601 unsigned char *output );
602
603/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000604 * \brief This function adds the message padding, then performs an RSA
605 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000606 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000607 * It is the generic wrapper for performing a PKCS#1 encryption
608 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000609 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100610 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000611 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
612 * are likely to remove the \p mode argument and have it
613 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100614 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100615 * \note Alternative implementations of RSA need not support
616 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300617 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100618 *
Hanno Becker9a467772018-12-13 09:54:59 +0000619 * \param ctx The initialized RSA context to use.
Hanno Becker974ca0d2018-12-18 18:03:24 +0000620 * \param f_rng The RNG to use. It is mandatory for PKCS#1 v2.1 padding
621 * encoding, and for PKCS#1 v1.5 padding encoding when used
622 * with \p mode set to #MBEDTLS_RSA_PUBLIC. For PKCS#1 v1.5
623 * padding encoding and \p mode set to #MBEDTLS_RSA_PRIVATE,
624 * it is used for blinding and should be provided in this
625 * case; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000626 * \param p_rng The RNG context to be passed to \p f_rng. May be
627 * \c NULL if \p f_rng is \c NULL or if \p f_rng doesn't
628 * need a context argument.
629 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000630 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000631 * \param ilen The length of the plaintext in Bytes.
632 * \param input The input data to encrypt. This must be a readable
Hanno Becker2f660d02018-12-18 17:04:59 +0000633 * buffer of size \p ilen Bytes. This must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000634 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000635 * of length \c ctx->len Bytes. For example, \c 256 Bytes
636 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100637 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100638 * \return \c 0 on success.
639 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000640 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000642 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000643 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000644 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000645 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000646 unsigned char *output );
647
648/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000649 * \brief This function performs a PKCS#1 v1.5 encryption operation
650 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100651 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100652 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000653 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
654 * are likely to remove the \p mode argument and have it
655 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100656 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100657 * \note Alternative implementations of RSA need not support
658 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300659 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100660 *
Hanno Becker9a467772018-12-13 09:54:59 +0000661 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000662 * \param f_rng The RNG function to use. It is needed for padding generation
663 * if \p mode is #MBEDTLS_RSA_PUBLIC. If \p mode is
664 * #MBEDTLS_RSA_PRIVATE (discouraged), it is used for
665 * blinding and should be provided; see mbedtls_rsa_private().
Hanno Becker9a467772018-12-13 09:54:59 +0000666 * \param p_rng The RNG context to be passed to \p f_rng. This may
667 * be \c NULL if \p f_rng is \c NULL or if \p f_rng
668 * doesn't need a context argument.
669 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000670 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000671 * \param ilen The length of the plaintext in Bytes.
672 * \param input The input data to encrypt. This must be a readable
Hanno Becker2f660d02018-12-18 17:04:59 +0000673 * buffer of size \p ilen Bytes. This must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000674 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000675 * of length \c ctx->len Bytes. For example, \c 256 Bytes
676 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100677 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100678 * \return \c 0 on success.
679 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100682 int (*f_rng)(void *, unsigned char *, size_t),
683 void *p_rng,
684 int mode, size_t ilen,
685 const unsigned char *input,
686 unsigned char *output );
687
688/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000689 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
690 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100691 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100692 * \note The output buffer must be as large as the size
693 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
694 *
695 * \deprecated It is deprecated and discouraged to call this function
696 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
697 * are likely to remove the \p mode argument and have it
698 * implicitly set to #MBEDTLS_RSA_PUBLIC.
699 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100700 * \note Alternative implementations of RSA need not support
701 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300702 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100703 *
Hanno Becker9a467772018-12-13 09:54:59 +0000704 * \param ctx The initnialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000705 * \param f_rng The RNG function to use. This is needed for padding
706 * generation and must be provided.
Hanno Becker9a467772018-12-13 09:54:59 +0000707 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000708 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000709 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000710 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000711 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000712 * This must be a readable buffer of length \p label_len
713 * Bytes. It may be \c NULL if \p label_len is \c 0.
714 * \param label_len The length of the label in Bytes.
715 * \param ilen The length of the plaintext buffer \p input in Bytes.
716 * \param input The input data to encrypt. This must be a readable
Hanno Becker2f660d02018-12-18 17:04:59 +0000717 * buffer of size \p ilen Bytes. This must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000718 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000719 * of length \c ctx->len Bytes. For example, \c 256 Bytes
720 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100721 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100722 * \return \c 0 on success.
723 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100726 int (*f_rng)(void *, unsigned char *, size_t),
727 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100728 int mode,
729 const unsigned char *label, size_t label_len,
730 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100731 const unsigned char *input,
732 unsigned char *output );
733
734/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000735 * \brief This function performs an RSA operation, then removes the
736 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000737 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000738 * It is the generic wrapper for performing a PKCS#1 decryption
739 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000740 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100741 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000742 * as large as the size \p ctx->len of \p ctx->N (for example,
743 * 128 Bytes if RSA-1024 is used) to be able to hold an
744 * arbitrary decrypted message. If it is not large enough to
745 * hold the decryption of the particular ciphertext provided,
746 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100747 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100748 * \deprecated It is deprecated and discouraged to call this function
749 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
750 * are likely to remove the \p mode argument and have it
751 * implicitly set to #MBEDTLS_RSA_PRIVATE.
752 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100753 * \note Alternative implementations of RSA need not support
754 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300755 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100756 *
Hanno Becker9a467772018-12-13 09:54:59 +0000757 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000758 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
759 * this is used for blinding and should be provided; see
760 * mbedtls_rsa_private() for more. If \p mode is
761 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000762 * \param p_rng The RNG context to be passed to \p f_rng. This may be
763 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
764 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000765 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000766 * \param olen The address at which to store the length of
767 * the plaintext. This must not be \c NULL.
768 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000769 * of length \c ctx->len Bytes. For example, \c 256 Bytes
770 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000771 * \param output The buffer used to hold the plaintext. This must
772 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000773 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100774 *
775 * \return \c 0 on success.
776 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000777 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200779 int (*f_rng)(void *, unsigned char *, size_t),
780 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000781 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000782 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000783 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000784 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000785
786/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000787 * \brief This function performs a PKCS#1 v1.5 decryption
788 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100789 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100790 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000791 * as large as the size \p ctx->len of \p ctx->N, for example,
792 * 128 Bytes if RSA-1024 is used, to be able to hold an
793 * arbitrary decrypted message. If it is not large enough to
794 * hold the decryption of the particular ciphertext provided,
795 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100796 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100797 * \deprecated It is deprecated and discouraged to call this function
798 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
799 * are likely to remove the \p mode argument and have it
800 * implicitly set to #MBEDTLS_RSA_PRIVATE.
801 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100802 * \note Alternative implementations of RSA need not support
803 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300804 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100805 *
Hanno Becker9a467772018-12-13 09:54:59 +0000806 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000807 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
808 * this is used for blinding and should be provided; see
809 * mbedtls_rsa_private() for more. If \p mode is
810 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000811 * \param p_rng The RNG context to be passed to \p f_rng. This may be
812 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
813 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000814 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000815 * \param olen The address at which to store the length of
816 * the plaintext. This must not be \c NULL.
817 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000818 * of length \c ctx->len Bytes. For example, \c 256 Bytes
819 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000820 * \param output The buffer used to hold the plaintext. This must
821 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000822 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100823 *
824 * \return \c 0 on success.
825 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
826 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200829 int (*f_rng)(void *, unsigned char *, size_t),
830 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100831 int mode, size_t *olen,
832 const unsigned char *input,
833 unsigned char *output,
834 size_t output_max_len );
835
836/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100837 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
838 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100839 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100840 * \note The output buffer length \c output_max_len should be
841 * as large as the size \p ctx->len of \p ctx->N, for
842 * example, 128 Bytes if RSA-1024 is used, to be able to
843 * hold an arbitrary decrypted message. If it is not
844 * large enough to hold the decryption of the particular
845 * ciphertext provided, the function returns
846 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100847 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100848 * \deprecated It is deprecated and discouraged to call this function
849 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
850 * are likely to remove the \p mode argument and have it
851 * implicitly set to #MBEDTLS_RSA_PRIVATE.
852 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100853 * \note Alternative implementations of RSA need not support
854 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300855 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100856 *
Hanno Becker9a467772018-12-13 09:54:59 +0000857 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000858 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
859 * this is used for blinding and should be provided; see
860 * mbedtls_rsa_private() for more. If \p mode is
861 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000862 * \param p_rng The RNG context to be passed to \p f_rng. This may be
863 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
864 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000865 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100866 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000867 * This must be a readable buffer of length \p label_len
868 * Bytes. It may be \c NULL if \p label_len is \c 0.
869 * \param label_len The length of the label in Bytes.
870 * \param olen The address at which to store the length of
871 * the plaintext. This must not be \c NULL.
872 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000873 * of length \c ctx->len Bytes. For example, \c 256 Bytes
874 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000875 * \param output The buffer used to hold the plaintext. This must
876 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000877 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100878 *
879 * \return \c 0 on success.
880 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200883 int (*f_rng)(void *, unsigned char *, size_t),
884 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100885 int mode,
886 const unsigned char *label, size_t label_len,
887 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100888 const unsigned char *input,
889 unsigned char *output,
890 size_t output_max_len );
891
892/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000893 * \brief This function performs a private RSA operation to sign
894 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000895 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000896 * It is the generic wrapper for performing a PKCS#1
897 * signature using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000898 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000899 * \note The \p sig buffer must be as large as the size
900 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000901 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000902 * \note For PKCS#1 v2.1 encoding, see comments on
903 * mbedtls_rsa_rsassa_pss_sign() for details on
904 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100905 *
906 * \deprecated It is deprecated and discouraged to call this function
907 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
908 * are likely to remove the \p mode argument and have it
909 * implicitly set to #MBEDTLS_RSA_PRIVATE.
910 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100911 * \note Alternative implementations of RSA need not support
912 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300913 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100914 *
Hanno Becker9a467772018-12-13 09:54:59 +0000915 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000916 * \param f_rng The RNG function to use. If the padding mode is PKCS#1 v2.1,
917 * this must be provided. If the padding mode is PKCS#1 v1.5 and
918 * \p mode is #MBEDTLS_RSA_PRIVATE, it is used for blinding
919 * and should be provided; see mbedtls_rsa_private() for more
920 * more. It is ignored otherwise.
Hanno Becker9a467772018-12-13 09:54:59 +0000921 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
922 * if \p f_rng is \c NULL or doesn't need a context argument.
923 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000924 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100925 * \param md_alg The message-digest algorithm used to hash the original data.
926 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000927 * \param hashlen The length of the message digest.
928 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
929 * \param hash The buffer holding the message digest or raw data.
930 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
931 * buffer of length \p hashlen Bytes. If \p md_alg is not
932 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
933 * the size of the hash corresponding to \p md_alg.
934 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000935 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
k-stachowiak4e36da32019-05-31 20:16:50 +0200936 * for an 2048-bit RSA modulus. A buffer length of
937 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100938 *
939 * \return \c 0 if the signing operation was successful.
940 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000941 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000943 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000944 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000945 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000947 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000948 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000949 unsigned char *sig );
950
951/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000952 * \brief This function performs a PKCS#1 v1.5 signature
953 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100954 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100955 * \deprecated It is deprecated and discouraged to call this function
956 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
957 * are likely to remove the \p mode argument and have it
958 * implicitly set to #MBEDTLS_RSA_PRIVATE.
959 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100960 * \note Alternative implementations of RSA need not support
961 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300962 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100963 *
Hanno Becker9a467772018-12-13 09:54:59 +0000964 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000965 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
966 * this is used for blinding and should be provided; see
967 * mbedtls_rsa_private() for more. If \p mode is
968 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000969 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
970 * if \p f_rng is \c NULL or doesn't need a context argument.
971 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000972 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000973 * \param md_alg The message-digest algorithm used to hash the original data.
974 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000975 * \param hashlen The length of the message digest.
976 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
977 * \param hash The buffer holding the message digest or raw data.
978 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
979 * buffer of length \p hashlen Bytes. If \p md_alg is not
980 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
981 * the size of the hash corresponding to \p md_alg.
982 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000983 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
k-stachowiak4e36da32019-05-31 20:16:50 +0200984 * for an 2048-bit RSA modulus. A buffer length of
985 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100986 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100987 * \return \c 0 if the signing operation was successful.
988 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200991 int (*f_rng)(void *, unsigned char *, size_t),
992 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100993 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100995 unsigned int hashlen,
996 const unsigned char *hash,
997 unsigned char *sig );
998
999/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001000 * \brief This function performs a PKCS#1 v2.1 PSS signature
1001 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +01001002 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001003 * \note The \p hash_id in the RSA context is the one used for the
1004 * encoding. \p md_alg in the function call is the type of hash
1005 * that is encoded. According to <em>RFC-3447: Public-Key
1006 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1007 * Specifications</em> it is advised to keep both hashes the
1008 * same.
Rose Zadike8b5b992018-03-27 12:19:47 +01001009 *
Jaeden Amero3725bb22018-09-07 19:12:36 +01001010 * \note This function always uses the maximum possible salt size,
1011 * up to the length of the payload hash. This choice of salt
1012 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
1013 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
1014 * minimum salt size which is the hash size minus 2 bytes. If
1015 * this minimum size is too large given the key size (the salt
1016 * size, plus the hash size, plus 2 bytes must be no more than
1017 * the key size in bytes), this function returns
1018 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
1019 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001020 * \deprecated It is deprecated and discouraged to call this function
1021 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
1022 * are likely to remove the \p mode argument and have it
1023 * implicitly set to #MBEDTLS_RSA_PRIVATE.
1024 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001025 * \note Alternative implementations of RSA need not support
1026 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001027 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001028 *
Hanno Becker9a467772018-12-13 09:54:59 +00001029 * \param ctx The initialized RSA context to use.
1030 * \param f_rng The RNG function. It must not be \c NULL.
1031 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
1032 * if \p f_rng doesn't need a context argument.
1033 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001034 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001035 * \param md_alg The message-digest algorithm used to hash the original data.
1036 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001037 * \param hashlen The length of the message digest.
1038 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
1039 * \param hash The buffer holding the message digest or raw data.
1040 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1041 * buffer of length \p hashlen Bytes. If \p md_alg is not
1042 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1043 * the size of the hash corresponding to \p md_alg.
1044 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +00001045 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
k-stachowiak4e36da32019-05-31 20:16:50 +02001046 * for an 2048-bit RSA modulus. A buffer length of
1047 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +01001048 *
1049 * \return \c 0 if the signing operation was successful.
1050 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001053 int (*f_rng)(void *, unsigned char *, size_t),
1054 void *p_rng,
1055 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001057 unsigned int hashlen,
1058 const unsigned char *hash,
1059 unsigned char *sig );
1060
1061/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001062 * \brief This function performs a public RSA operation and checks
1063 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +00001064 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001065 * This is the generic wrapper for performing a PKCS#1
1066 * verification using the mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001067 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001068 * \note For PKCS#1 v2.1 encoding, see comments on
1069 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
1070 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +01001071 *
1072 * \deprecated It is deprecated and discouraged to call this function
1073 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1074 * are likely to remove the \p mode argument and have it
1075 * set to #MBEDTLS_RSA_PUBLIC.
1076 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001077 * \note Alternative implementations of RSA need not support
1078 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001079 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001080 *
Hanno Becker9a467772018-12-13 09:54:59 +00001081 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001082 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1083 * this is used for blinding and should be provided; see
1084 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001085 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1086 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1087 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001088 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001089 * \param md_alg The message-digest algorithm used to hash the original data.
1090 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001091 * \param hashlen The length of the message digest.
1092 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1093 * \param hash The buffer holding the message digest or raw data.
1094 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1095 * buffer of length \p hashlen Bytes. If \p md_alg is not
1096 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1097 * the size of the hash corresponding to \p md_alg.
1098 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001099 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1100 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001101 *
1102 * \return \c 0 if the verify operation was successful.
1103 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001106 int (*f_rng)(void *, unsigned char *, size_t),
1107 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001108 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001110 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001111 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001112 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +00001113
1114/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001115 * \brief This function performs a PKCS#1 v1.5 verification
1116 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001117 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001118 * \deprecated It is deprecated and discouraged to call this function
1119 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1120 * are likely to remove the \p mode argument and have it
1121 * set to #MBEDTLS_RSA_PUBLIC.
1122 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001123 * \note Alternative implementations of RSA need not support
1124 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001125 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001126 *
Hanno Becker9a467772018-12-13 09:54:59 +00001127 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001128 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1129 * this is used for blinding and should be provided; see
1130 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001131 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1132 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1133 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001134 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +00001135 * \param md_alg The message-digest algorithm used to hash the original data.
1136 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001137 * \param hashlen The length of the message digest.
1138 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1139 * \param hash The buffer holding the message digest or raw data.
1140 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1141 * buffer of length \p hashlen Bytes. If \p md_alg is not
1142 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1143 * the size of the hash corresponding to \p md_alg.
1144 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001145 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1146 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001147 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001148 * \return \c 0 if the verify operation was successful.
1149 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001152 int (*f_rng)(void *, unsigned char *, size_t),
1153 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001154 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001156 unsigned int hashlen,
1157 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001158 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001159
1160/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001161 * \brief This function performs a PKCS#1 v2.1 PSS verification
1162 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001163 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001164 * The hash function for the MGF mask generating function
1165 * is that specified in the RSA context.
Paul Bakkerb3869132013-02-28 17:21:01 +01001166 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001167 * \note The \p hash_id in the RSA context is the one used for the
1168 * verification. \p md_alg in the function call is the type of
1169 * hash that is verified. According to <em>RFC-3447: Public-Key
1170 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1171 * Specifications</em> it is advised to keep both hashes the
1172 * same. If \p hash_id in the RSA context is unset,
1173 * the \p md_alg from the function call is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001174 *
1175 * \deprecated It is deprecated and discouraged to call this function
1176 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1177 * are likely to remove the \p mode argument and have it
1178 * implicitly set to #MBEDTLS_RSA_PUBLIC.
1179 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001180 * \note Alternative implementations of RSA need not support
1181 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001182 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001183 *
Hanno Becker9a467772018-12-13 09:54:59 +00001184 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001185 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1186 * this is used for blinding and should be provided; see
1187 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001188 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1189 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1190 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001191 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001192 * \param md_alg The message-digest algorithm used to hash the original data.
1193 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001194 * \param hashlen The length of the message digest.
1195 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1196 * \param hash The buffer holding the message digest or raw data.
1197 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1198 * buffer of length \p hashlen Bytes. If \p md_alg is not
1199 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1200 * the size of the hash corresponding to \p md_alg.
1201 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001202 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1203 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001204 *
1205 * \return \c 0 if the verify operation was successful.
1206 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001209 int (*f_rng)(void *, unsigned char *, size_t),
1210 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001211 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001213 unsigned int hashlen,
1214 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001215 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001216
1217/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001218 * \brief This function performs a PKCS#1 v2.1 PSS verification
1219 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001220 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001221 * The hash function for the MGF mask generating function
1222 * is that specified in \p mgf1_hash_id.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001223 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001224 * \note The \p sig buffer must be as large as the size
1225 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001226 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001227 * \note The \p hash_id in the RSA context is ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001228 *
Hanno Becker9a467772018-12-13 09:54:59 +00001229 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001230 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1231 * this is used for blinding and should be provided; see
1232 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001233 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1234 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1235 * \param mode The mode of operation. This must be either
1236 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +01001237 * \param md_alg The message-digest algorithm used to hash the original data.
1238 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001239 * \param hashlen The length of the message digest.
1240 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1241 * \param hash The buffer holding the message digest or raw data.
1242 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1243 * buffer of length \p hashlen Bytes. If \p md_alg is not
1244 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1245 * the size of the hash corresponding to \p md_alg.
1246 * \param mgf1_hash_id The message digest used for mask generation.
1247 * \param expected_salt_len The length of the salt used in padding. Use
1248 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1249 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001250 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1251 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001252 *
1253 * \return \c 0 if the verify operation was successful.
1254 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001255 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001257 int (*f_rng)(void *, unsigned char *, size_t),
1258 void *p_rng,
1259 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001261 unsigned int hashlen,
1262 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001264 int expected_salt_len,
1265 const unsigned char *sig );
1266
1267/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001268 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001269 *
Hanno Becker9a467772018-12-13 09:54:59 +00001270 * \param dst The destination context. This must be initialized.
1271 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001272 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001273 * \return \c 0 on success.
1274 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001277
1278/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001279 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001280 *
Hanno Becker9a467772018-12-13 09:54:59 +00001281 * \param ctx The RSA context to free. May be \c NULL, in which case
1282 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001283 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001286
Ron Eldorfa8f6352017-06-20 15:48:46 +03001287#if defined(MBEDTLS_SELF_TEST)
1288
Paul Bakker5121ce52009-01-03 21:22:43 +00001289/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001290 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001292 * \return \c 0 on success.
1293 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001296
Ron Eldorfa8f6352017-06-20 15:48:46 +03001297#endif /* MBEDTLS_SELF_TEST */
1298
Paul Bakker5121ce52009-01-03 21:22:43 +00001299#ifdef __cplusplus
1300}
1301#endif
1302
Paul Bakker5121ce52009-01-03 21:22:43 +00001303#endif /* rsa.h */