blob: cd22fc4c1fb18f927d79ec569b3c553523f244e4 [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/*
Rose Zadik042e97f2018-01-26 16:35:10 +000013 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
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 * **********
54 *
Rose Zadik042e97f2018-01-26 16:35:10 +000055 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000056 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#ifndef MBEDTLS_RSA_H
58#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020061#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020062#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020064#endif
Paul Bakkered27a042013-04-18 22:46:23 +020065
Paul Bakker314052f2011-08-15 09:07:52 +000066#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020067#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_THREADING_C)
Paul Bakkerc9965dc2013-09-29 14:58:17 +020070#include "threading.h"
71#endif
72
Paul Bakker13e2dfe2009-07-28 07:18:38 +000073/*
74 * RSA Error codes
75 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
77#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
78#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Rose Zadik042e97f2018-01-26 16:35:10 +000079#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 +020080#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
81#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
82#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
83#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
84#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030085
86/* MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is deprecated and should not be used.
87 */
Rose Zadik042e97f2018-01-26 16:35:10 +000088#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 +030089
90/* MBEDTLS_ERR_RSA_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010091#define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580 /**< RSA hardware accelerator failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000092
93/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020094 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000095 */
Rose Zadik042e97f2018-01-26 16:35:10 +000096#define MBEDTLS_RSA_PUBLIC 0 /**< Request private key operation. */
97#define MBEDTLS_RSA_PRIVATE 1 /**< Request public key operation. */
Paul Bakker5121ce52009-01-03 21:22:43 +000098
Rose Zadike8b5b992018-03-27 12:19:47 +010099#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
100#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
Rose Zadik042e97f2018-01-26 16:35:10 +0000102#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
103#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200106
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +0200107/*
108 * The above constants may be used even if the RSA module is compile out,
109 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
110 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +0100111
Paul Bakker407a0da2013-06-27 14:29:21 +0200112#ifdef __cplusplus
113extern "C" {
114#endif
115
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200116#if !defined(MBEDTLS_RSA_ALT)
117// Regular implementation
118//
119
Paul Bakker5121ce52009-01-03 21:22:43 +0000120/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000121 * \brief The RSA context structure.
Hanno Becker5063cd22017-09-29 11:49:12 +0100122 *
123 * \note Direct manipulation of the members of this structure
Rose Zadik042e97f2018-01-26 16:35:10 +0000124 * is deprecated. All manipulation should instead be done through
125 * the public interface functions.
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200127typedef struct mbedtls_rsa_context
Paul Bakker5121ce52009-01-03 21:22:43 +0000128{
Rose Zadik042e97f2018-01-26 16:35:10 +0000129 int ver; /*!< Always 0.*/
130 size_t len; /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
Rose Zadike8b5b992018-03-27 12:19:47 +0100132 mbedtls_mpi N; /*!< The public modulus. */
133 mbedtls_mpi E; /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
Rose Zadike8b5b992018-03-27 12:19:47 +0100135 mbedtls_mpi D; /*!< The private exponent. */
136 mbedtls_mpi P; /*!< The first prime factor. */
137 mbedtls_mpi Q; /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100138
Rose Zadikf2ec2882018-04-17 10:27:25 +0100139 mbedtls_mpi DP; /*!< <code>D % (P - 1)</code>. */
140 mbedtls_mpi DQ; /*!< <code>D % (Q - 1)</code>. */
141 mbedtls_mpi QP; /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
Rose Zadikf2ec2882018-04-17 10:27:25 +0100143 mbedtls_mpi RN; /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
Rose Zadikf2ec2882018-04-17 10:27:25 +0100145 mbedtls_mpi RP; /*!< cached <code>R^2 mod P</code>. */
146 mbedtls_mpi RQ; /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200147
Rose Zadike8b5b992018-03-27 12:19:47 +0100148 mbedtls_mpi Vi; /*!< The cached blinding value. */
149 mbedtls_mpi Vf; /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000150
Rose Zadik042e97f2018-01-26 16:35:10 +0000151 int padding; /*!< Selects padding mode:
152 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
153 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
154 int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
155 as specified in md.h for use in the MGF
156 mask generating function used in the
157 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#if defined(MBEDTLS_THREADING_C)
Rose Zadik042e97f2018-01-26 16:35:10 +0000159 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200160#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200164#else /* MBEDTLS_RSA_ALT */
165#include "rsa_alt.h"
166#endif /* MBEDTLS_RSA_ALT */
167
Paul Bakker5121ce52009-01-03 21:22:43 +0000168/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000169 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000171 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000172 * encryption scheme and the RSASSA-PSS signature scheme.
173 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000174 * \note The \p hash_id parameter is ignored when using
175 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200176 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000177 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200178 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000179 * mixing padding modes. For public key operations it is
Antonin Décimod5f47592019-01-23 15:24:37 +0100180 * a default value, which can be overridden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000181 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200182 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000183 * \note The hash selected in \p hash_id is always used for OEAP
184 * encryption. For PSS signatures, it is always used for
Antonin Décimod5f47592019-01-23 15:24:37 +0100185 * making signatures, but can be overridden for verifying them.
186 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100187 *
Hanno Becker9a467772018-12-13 09:54:59 +0000188 * \param ctx The RSA context to initialize. This must not be \c NULL.
189 * \param padding The padding mode to use. This must be either
190 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Hanno Becker385ce912018-12-13 18:33:12 +0000191 * \param hash_id The hash identifier of ::mbedtls_md_type_t type, if
Hanno Becker9a467772018-12-13 09:54:59 +0000192 * \p padding is #MBEDTLS_RSA_PKCS_V21. It is unused
193 * otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100196 int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000197 int hash_id );
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
199/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000200 * \brief This function imports a set of core parameters into an
201 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100202 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100203 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000204 * imports, if the parameters are not simultaneously present.
205 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100206 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000207 * by a call to mbedtls_rsa_complete(), which checks and
208 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100209 * public or private RSA key.
210 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000211 * \note See mbedtls_rsa_complete() for more information on which
212 * parameters are necessary to set up a private or public
213 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100214 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100215 * \note The imported parameters are copied and need not be preserved
216 * for the lifetime of the RSA context being set up.
217 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100218 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000219 * \param N The RSA modulus. This may be \c NULL.
220 * \param P The first prime factor of \p N. This may be \c NULL.
221 * \param Q The second prime factor of \p N. This may be \c NULL.
222 * \param D The private exponent. This may be \c NULL.
223 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100224 *
225 * \return \c 0 on success.
226 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100227 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100228int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
229 const mbedtls_mpi *N,
230 const mbedtls_mpi *P, const mbedtls_mpi *Q,
231 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100232
233/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000234 * \brief This function imports core RSA parameters, in raw big-endian
235 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100237 * \note This function can be called multiple times for successive
238 * imports, if the parameters are not simultaneously present.
239 *
240 * Any sequence of calls to this function should be followed
241 * by a call to mbedtls_rsa_complete(), which checks and
242 * completes the provided information to a ready-for-use
243 * public or private RSA key.
244 *
245 * \note See mbedtls_rsa_complete() for more information on which
246 * parameters are necessary to set up a private or public
247 * RSA key.
248 *
249 * \note The imported parameters are copied and need not be preserved
250 * for the lifetime of the RSA context being set up.
251 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000252 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000253 * \param N The RSA modulus. This may be \c NULL.
254 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
255 * \param P The first prime factor of \p N. This may be \c NULL.
256 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
257 * \param Q The second prime factor of \p N. This may be \c NULL.
258 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
259 * \param D The private exponent. This may be \c NULL.
260 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
261 * \param E The public exponent. This may be \c NULL.
262 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000263 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100264 * \return \c 0 on success.
265 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100266 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100267int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100268 unsigned char const *N, size_t N_len,
269 unsigned char const *P, size_t P_len,
270 unsigned char const *Q, size_t Q_len,
271 unsigned char const *D, size_t D_len,
272 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100273
274/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000275 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100276 * a set of imported core parameters.
277 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000278 * To setup an RSA public key, precisely \p N and \p E
279 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100280 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000281 * To setup an RSA private key, sufficient information must
282 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100283 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000284 * The default implementation supports the following:
285 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
286 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
287 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100288 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000289 * If this function runs successfully, it guarantees that
290 * the RSA context can be used for RSA operations without
291 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100292 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100293 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000294 * for the imported parameters. In particular, parameters that
295 * are not needed by the implementation might be silently
296 * discarded and left unchecked. To check the consistency
297 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100298 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100299 * \param ctx The initialized RSA context holding imported parameters.
300 *
301 * \return \c 0 on success.
302 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
303 * failed.
304 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100305 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100306int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100307
308/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000309 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100310 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000311 * If this function runs successfully, the non-NULL buffers
312 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
313 * written, with additional unused space filled leading by
314 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100315 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000316 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300317 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000318 * <li>An alternative RSA implementation is in use, which
319 * stores the key externally, and either cannot or should
320 * not export it into RAM.</li>
321 * <li>A SW or HW implementation might not support a certain
322 * deduction. For example, \p P, \p Q from \p N, \p D,
323 * and \p E if the former are not part of the
324 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100325 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000326 * If the function fails due to an unsupported operation,
327 * the RSA context stays intact and remains usable.
328 *
329 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000330 * \param N The MPI to hold the RSA modulus.
331 * This may be \c NULL if this field need not be exported.
332 * \param P The MPI to hold the first prime factor of \p N.
333 * This may be \c NULL if this field need not be exported.
334 * \param Q The MPI to hold the second prime factor of \p N.
335 * This may be \c NULL if this field need not be exported.
336 * \param D The MPI to hold the private exponent.
337 * This may be \c NULL if this field need not be exported.
338 * \param E The MPI to hold the public exponent.
339 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000340 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100341 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300342 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000343 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100344 * functionality or because of security policies.
345 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100346 *
347 */
348int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
349 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
350 mbedtls_mpi *D, mbedtls_mpi *E );
351
352/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000353 * \brief This function exports core parameters of an RSA key
354 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100355 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000356 * If this function runs successfully, the non-NULL buffers
357 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
358 * written, with additional unused space filled leading by
359 * zero Bytes.
360 *
361 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300362 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000363 * <li>An alternative RSA implementation is in use, which
364 * stores the key externally, and either cannot or should
365 * not export it into RAM.</li>
366 * <li>A SW or HW implementation might not support a certain
367 * deduction. For example, \p P, \p Q from \p N, \p D,
368 * and \p E if the former are not part of the
369 * implementation.</li></ul>
370 * If the function fails due to an unsupported operation,
371 * the RSA context stays intact and remains usable.
372 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100373 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100374 * buffer pointers are NULL.
375 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000376 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000377 * \param N The Byte array to store the RSA modulus,
378 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000379 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000380 * \param P The Byte array to hold the first prime factor of \p N,
381 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000382 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000383 * \param Q The Byte array to hold the second prime factor of \p N,
384 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000385 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000386 * \param D The Byte array to hold the private exponent,
387 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000388 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000389 * \param E The Byte array to hold the public exponent,
390 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000391 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100392 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100393 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300394 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000395 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100396 * functionality or because of security policies.
397 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100398 */
399int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
400 unsigned char *N, size_t N_len,
401 unsigned char *P, size_t P_len,
402 unsigned char *Q, size_t Q_len,
403 unsigned char *D, size_t D_len,
404 unsigned char *E, size_t E_len );
405
406/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000407 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100408 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100409 * \note Alternative RSA implementations not using CRT-parameters
410 * internally can implement this function based on
411 * mbedtls_rsa_deduce_opt().
412 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000413 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000414 * \param DP The MPI to hold \c D modulo `P-1`,
415 * or \c NULL if it need not be exported.
416 * \param DQ The MPI to hold \c D modulo `Q-1`,
417 * or \c NULL if it need not be exported.
418 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
419 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100420 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100421 * \return \c 0 on success.
422 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100423 *
424 */
425int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
426 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
427
Paul Bakker5121ce52009-01-03 21:22:43 +0000428/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000429 * \brief This function sets padding for an already initialized RSA
430 * context. See mbedtls_rsa_init() for details.
Paul Bakker5121ce52009-01-03 21:22:43 +0000431 *
Hanno Becker9a467772018-12-13 09:54:59 +0000432 * \param ctx The initialized RSA context to be configured.
433 * \param padding The padding mode to use. This must be either
434 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Rose Zadik042e97f2018-01-26 16:35:10 +0000435 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Paul Bakker40e46942009-01-03 21:51:57 +0000436 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100437void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000438 int hash_id );
Paul Bakker21eb2802010-08-16 11:10:02 +0000439
Paul Bakkera3d195c2011-11-27 21:07:34 +0000440/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000441 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100442 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000443 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100444 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000445 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100446 *
447 */
448size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
449
450/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000451 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000453 * \note mbedtls_rsa_init() must be called before this function,
454 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 *
Hanno Becker9a467772018-12-13 09:54:59 +0000456 * \param ctx The initialized RSA context used to hold the key.
457 * \param f_rng The RNG function to be used for key generation.
458 * This must not be \c NULL.
459 * \param p_rng The RNG context to be passed to \p f_rng.
460 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100461 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000462 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000463 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100464 *
465 * \return \c 0 on success.
466 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100469 int (*f_rng)(void *, unsigned char *, size_t),
470 void *p_rng,
471 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
473/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000474 * \brief This function checks if a context contains at least an RSA
475 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000477 * If the function runs successfully, it is guaranteed that
478 * enough information is present to perform an RSA public key
479 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 *
Hanno Becker9a467772018-12-13 09:54:59 +0000481 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000482 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100483 * \return \c 0 on success.
484 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100485 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000486 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000488
489/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000490 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100491 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000492 *
Hanno Becker68767a62017-10-17 10:13:31 +0100493 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000494 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100495 * on the given context, but that the various parameters are
496 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000497 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100498 *
499 * \warning This function should catch accidental misconfigurations
500 * like swapping of parameters, but it cannot establish full
501 * trust in neither the quality nor the consistency of the key
502 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000503 * <ul><li>Consistency: Imported parameters that are irrelevant
504 * for the implementation might be silently dropped. If dropped,
505 * the current function does not have access to them,
506 * and therefore cannot check them. See mbedtls_rsa_complete().
507 * If you want to check the consistency of the entire
508 * content of an PKCS1-encoded RSA private key, for example, you
509 * should use mbedtls_rsa_validate_params() before setting
510 * up the RSA context.
511 * Additionally, if the implementation performs empirical checks,
512 * these checks substantiate but do not guarantee consistency.</li>
513 * <li>Quality: This function is not expected to perform
514 * extended quality assessments like checking that the prime
515 * factors are safe. Additionally, it is the responsibility of the
516 * user to ensure the trustworthiness of the source of his RSA
517 * parameters, which goes beyond what is effectively checkable
518 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100519 *
Hanno Becker9a467772018-12-13 09:54:59 +0000520 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100521 *
522 * \return \c 0 on success.
523 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000528 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100529 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000530 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100531 *
Hanno Becker9a467772018-12-13 09:54:59 +0000532 * \param pub The initialized RSA context holding the public key.
533 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000534 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100535 * \return \c 0 on success.
536 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100537 */
Hanno Becker98838b02017-10-02 13:16:10 +0100538int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
539 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100540
541/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000542 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000543 *
Hanno Becker9a467772018-12-13 09:54:59 +0000544 * \param ctx The initialized RSA context to use.
545 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000546 * of length \c ctx->len Bytes. For example, \c 256 Bytes
547 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000548 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000549 * of length \c ctx->len Bytes. For example, \c 256 Bytes
550 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000551 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000552 * \note This function does not handle message padding.
553 *
554 * \note Make sure to set \p input[0] = 0 or ensure that
555 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100557 * \return \c 0 on success.
558 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000561 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 unsigned char *output );
563
564/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000565 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 *
Hanno Becker24120612017-10-26 11:53:35 +0100567 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100568 *
569 * \note If blinding is used, both the base of exponentation
Hanno Becker24120612017-10-26 11:53:35 +0100570 * and the exponent are blinded, providing protection
571 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100572 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100573 * \warning It is deprecated and a security risk to not provide
574 * a PRNG here and thereby prevent the use of blinding.
575 * Future versions of the library may enforce the presence
576 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100577 *
Hanno Becker9a467772018-12-13 09:54:59 +0000578 * \param ctx The initialized RSA context to use.
579 * \param f_rng The RNG function, used for blinding. It is discouraged
580 * and deprecated to pass \c NULL here, in which case
581 * blinding will be omitted.
582 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
583 * if \p f_rng is \c NULL or if \p f_rng doesn't need a context.
584 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000585 * of length \c ctx->len Bytes. For example, \c 256 Bytes
586 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000587 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000588 * of length \c ctx->len Bytes. For example, \c 256 Bytes
589 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100590 *
591 * \return \c 0 on success.
592 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
593 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200596 int (*f_rng)(void *, unsigned char *, size_t),
597 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000598 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 unsigned char *output );
600
601/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000602 * \brief This function adds the message padding, then performs an RSA
603 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000605 * It is the generic wrapper for performing a PKCS#1 encryption
606 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100608 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000609 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
610 * are likely to remove the \p mode argument and have it
611 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100612 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100613 * \note Alternative implementations of RSA need not support
614 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300615 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100616 *
Hanno Becker9a467772018-12-13 09:54:59 +0000617 * \param ctx The initialized RSA context to use.
Hanno Becker974ca0d2018-12-18 18:03:24 +0000618 * \param f_rng The RNG to use. It is mandatory for PKCS#1 v2.1 padding
619 * encoding, and for PKCS#1 v1.5 padding encoding when used
620 * with \p mode set to #MBEDTLS_RSA_PUBLIC. For PKCS#1 v1.5
621 * padding encoding and \p mode set to #MBEDTLS_RSA_PRIVATE,
622 * it is used for blinding and should be provided in this
623 * case; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000624 * \param p_rng The RNG context to be passed to \p f_rng. May be
625 * \c NULL if \p f_rng is \c NULL or if \p f_rng doesn't
626 * need a context argument.
627 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000628 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000629 * \param ilen The length of the plaintext in Bytes.
630 * \param input The input data to encrypt. This must be a readable
Hanno Becker2f660d02018-12-18 17:04:59 +0000631 * buffer of size \p ilen Bytes. This must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000632 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000633 * of length \c ctx->len Bytes. For example, \c 256 Bytes
634 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100635 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100636 * \return \c 0 on success.
637 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000640 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000641 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000642 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000643 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 unsigned char *output );
645
646/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000647 * \brief This function performs a PKCS#1 v1.5 encryption operation
648 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100649 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100650 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000651 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
652 * are likely to remove the \p mode argument and have it
653 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100654 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100655 * \note Alternative implementations of RSA need not support
656 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300657 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100658 *
Hanno Becker9a467772018-12-13 09:54:59 +0000659 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000660 * \param f_rng The RNG function to use. It is needed for padding generation
661 * if \p mode is #MBEDTLS_RSA_PUBLIC. If \p mode is
662 * #MBEDTLS_RSA_PRIVATE (discouraged), it is used for
663 * blinding and should be provided; see mbedtls_rsa_private().
Hanno Becker9a467772018-12-13 09:54:59 +0000664 * \param p_rng The RNG context to be passed to \p f_rng. This may
665 * be \c NULL if \p f_rng is \c NULL or if \p f_rng
666 * doesn't need a context argument.
667 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000668 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000669 * \param ilen The length of the plaintext in Bytes.
670 * \param input The input data to encrypt. This must be a readable
Hanno Becker2f660d02018-12-18 17:04:59 +0000671 * buffer of size \p ilen Bytes. This must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000672 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000673 * of length \c ctx->len Bytes. For example, \c 256 Bytes
674 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100675 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100676 * \return \c 0 on success.
677 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100680 int (*f_rng)(void *, unsigned char *, size_t),
681 void *p_rng,
682 int mode, size_t ilen,
683 const unsigned char *input,
684 unsigned char *output );
685
686/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000687 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
688 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100689 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100690 * \note The output buffer must be as large as the size
691 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
692 *
693 * \deprecated It is deprecated and discouraged to call this function
694 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
695 * are likely to remove the \p mode argument and have it
696 * implicitly set to #MBEDTLS_RSA_PUBLIC.
697 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100698 * \note Alternative implementations of RSA need not support
699 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300700 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100701 *
Hanno Becker9a467772018-12-13 09:54:59 +0000702 * \param ctx The initnialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000703 * \param f_rng The RNG function to use. This is needed for padding
704 * generation and must be provided.
Hanno Becker9a467772018-12-13 09:54:59 +0000705 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000706 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000707 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000708 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000709 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000710 * This must be a readable buffer of length \p label_len
711 * Bytes. It may be \c NULL if \p label_len is \c 0.
712 * \param label_len The length of the label in Bytes.
713 * \param ilen The length of the plaintext buffer \p input in Bytes.
714 * \param input The input data to encrypt. This must be a readable
Hanno Becker2f660d02018-12-18 17:04:59 +0000715 * buffer of size \p ilen Bytes. This must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000716 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000717 * of length \c ctx->len Bytes. For example, \c 256 Bytes
718 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100719 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100720 * \return \c 0 on success.
721 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100724 int (*f_rng)(void *, unsigned char *, size_t),
725 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100726 int mode,
727 const unsigned char *label, size_t label_len,
728 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100729 const unsigned char *input,
730 unsigned char *output );
731
732/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000733 * \brief This function performs an RSA operation, then removes the
734 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000735 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000736 * It is the generic wrapper for performing a PKCS#1 decryption
737 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000738 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100739 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000740 * as large as the size \p ctx->len of \p ctx->N (for example,
741 * 128 Bytes if RSA-1024 is used) to be able to hold an
742 * arbitrary decrypted message. If it is not large enough to
743 * hold the decryption of the particular ciphertext provided,
744 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100745 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100746 * \deprecated It is deprecated and discouraged to call this function
747 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
748 * are likely to remove the \p mode argument and have it
749 * implicitly set to #MBEDTLS_RSA_PRIVATE.
750 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100751 * \note Alternative implementations of RSA need not support
752 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300753 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100754 *
Hanno Becker9a467772018-12-13 09:54:59 +0000755 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000756 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
757 * this is used for blinding and should be provided; see
758 * mbedtls_rsa_private() for more. If \p mode is
759 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000760 * \param p_rng The RNG context to be passed to \p f_rng. This may be
761 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
762 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000763 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000764 * \param olen The address at which to store the length of
765 * the plaintext. This must not be \c NULL.
766 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000767 * of length \c ctx->len Bytes. For example, \c 256 Bytes
768 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000769 * \param output The buffer used to hold the plaintext. This must
770 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000771 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100772 *
773 * \return \c 0 on success.
774 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200777 int (*f_rng)(void *, unsigned char *, size_t),
778 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000779 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000780 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000781 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000782 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000783
784/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000785 * \brief This function performs a PKCS#1 v1.5 decryption
786 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100787 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100788 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000789 * as large as the size \p ctx->len of \p ctx->N, for example,
790 * 128 Bytes if RSA-1024 is used, to be able to hold an
791 * arbitrary decrypted message. If it is not large enough to
792 * hold the decryption of the particular ciphertext provided,
793 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100794 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100795 * \deprecated It is deprecated and discouraged to call this function
796 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
797 * are likely to remove the \p mode argument and have it
798 * implicitly set to #MBEDTLS_RSA_PRIVATE.
799 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100800 * \note Alternative implementations of RSA need not support
801 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300802 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100803 *
Hanno Becker9a467772018-12-13 09:54:59 +0000804 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000805 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
806 * this is used for blinding and should be provided; see
807 * mbedtls_rsa_private() for more. If \p mode is
808 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000809 * \param p_rng The RNG context to be passed to \p f_rng. This may be
810 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
811 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000812 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000813 * \param olen The address at which to store the length of
814 * the plaintext. This must not be \c NULL.
815 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000816 * of length \c ctx->len Bytes. For example, \c 256 Bytes
817 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000818 * \param output The buffer used to hold the plaintext. This must
819 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000820 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100821 *
822 * \return \c 0 on success.
823 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
824 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200827 int (*f_rng)(void *, unsigned char *, size_t),
828 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100829 int mode, size_t *olen,
830 const unsigned char *input,
831 unsigned char *output,
832 size_t output_max_len );
833
834/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100835 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
836 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100837 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100838 * \note The output buffer length \c output_max_len should be
839 * as large as the size \p ctx->len of \p ctx->N, for
840 * example, 128 Bytes if RSA-1024 is used, to be able to
841 * hold an arbitrary decrypted message. If it is not
842 * large enough to hold the decryption of the particular
843 * ciphertext provided, the function returns
844 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100845 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100846 * \deprecated It is deprecated and discouraged to call this function
847 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
848 * are likely to remove the \p mode argument and have it
849 * implicitly set to #MBEDTLS_RSA_PRIVATE.
850 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100851 * \note Alternative implementations of RSA need not support
852 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300853 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100854 *
Hanno Becker9a467772018-12-13 09:54:59 +0000855 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000856 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
857 * this is used for blinding and should be provided; see
858 * mbedtls_rsa_private() for more. If \p mode is
859 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000860 * \param p_rng The RNG context to be passed to \p f_rng. This may be
861 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
862 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000863 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100864 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000865 * This must be a readable buffer of length \p label_len
866 * Bytes. It may be \c NULL if \p label_len is \c 0.
867 * \param label_len The length of the label in Bytes.
868 * \param olen The address at which to store the length of
869 * the plaintext. This must not be \c NULL.
870 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000871 * of length \c ctx->len Bytes. For example, \c 256 Bytes
872 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000873 * \param output The buffer used to hold the plaintext. This must
874 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000875 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100876 *
877 * \return \c 0 on success.
878 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100879 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200881 int (*f_rng)(void *, unsigned char *, size_t),
882 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100883 int mode,
884 const unsigned char *label, size_t label_len,
885 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100886 const unsigned char *input,
887 unsigned char *output,
888 size_t output_max_len );
889
890/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000891 * \brief This function performs a private RSA operation to sign
892 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000893 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000894 * It is the generic wrapper for performing a PKCS#1
895 * signature using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000896 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000897 * \note The \p sig buffer must be as large as the size
898 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000899 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000900 * \note For PKCS#1 v2.1 encoding, see comments on
901 * mbedtls_rsa_rsassa_pss_sign() for details on
902 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100903 *
904 * \deprecated It is deprecated and discouraged to call this function
905 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
906 * are likely to remove the \p mode argument and have it
907 * implicitly set to #MBEDTLS_RSA_PRIVATE.
908 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100909 * \note Alternative implementations of RSA need not support
910 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300911 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100912 *
Hanno Becker9a467772018-12-13 09:54:59 +0000913 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000914 * \param f_rng The RNG function to use. If the padding mode is PKCS#1 v2.1,
915 * this must be provided. If the padding mode is PKCS#1 v1.5 and
916 * \p mode is #MBEDTLS_RSA_PRIVATE, it is used for blinding
917 * and should be provided; see mbedtls_rsa_private() for more
918 * more. It is ignored otherwise.
Hanno Becker9a467772018-12-13 09:54:59 +0000919 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
920 * if \p f_rng is \c NULL or doesn't need a context argument.
921 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000922 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100923 * \param md_alg The message-digest algorithm used to hash the original data.
924 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000925 * \param hashlen The length of the message digest.
926 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
927 * \param hash The buffer holding the message digest or raw data.
928 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
929 * buffer of length \p hashlen Bytes. If \p md_alg is not
930 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
931 * the size of the hash corresponding to \p md_alg.
932 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000933 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
k-stachowiak4e36da32019-05-31 20:16:50 +0200934 * for an 2048-bit RSA modulus. A buffer length of
935 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100936 *
937 * \return \c 0 if the signing operation was successful.
938 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000939 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000941 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000942 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000943 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000945 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000946 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000947 unsigned char *sig );
948
949/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000950 * \brief This function performs a PKCS#1 v1.5 signature
951 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100952 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100953 * \deprecated It is deprecated and discouraged to call this function
954 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
955 * are likely to remove the \p mode argument and have it
956 * implicitly set to #MBEDTLS_RSA_PRIVATE.
957 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100958 * \note Alternative implementations of RSA need not support
959 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300960 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100961 *
Hanno Becker9a467772018-12-13 09:54:59 +0000962 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000963 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
964 * this is used for blinding and should be provided; see
965 * mbedtls_rsa_private() for more. If \p mode is
966 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000967 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
968 * if \p f_rng is \c NULL or doesn't need a context argument.
969 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000970 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000971 * \param md_alg The message-digest algorithm used to hash the original data.
972 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000973 * \param hashlen The length of the message digest.
974 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
975 * \param hash The buffer holding the message digest or raw data.
976 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
977 * buffer of length \p hashlen Bytes. If \p md_alg is not
978 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
979 * the size of the hash corresponding to \p md_alg.
980 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000981 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
k-stachowiak4e36da32019-05-31 20:16:50 +0200982 * for an 2048-bit RSA modulus. A buffer length of
983 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100984 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100985 * \return \c 0 if the signing operation was successful.
986 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100987 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200989 int (*f_rng)(void *, unsigned char *, size_t),
990 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100991 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100993 unsigned int hashlen,
994 const unsigned char *hash,
995 unsigned char *sig );
996
997/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000998 * \brief This function performs a PKCS#1 v2.1 PSS signature
999 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +01001000 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001001 * \note The \p hash_id in the RSA context is the one used for the
1002 * encoding. \p md_alg in the function call is the type of hash
1003 * that is encoded. According to <em>RFC-3447: Public-Key
1004 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1005 * Specifications</em> it is advised to keep both hashes the
1006 * same.
Rose Zadike8b5b992018-03-27 12:19:47 +01001007 *
Jaeden Amero3725bb22018-09-07 19:12:36 +01001008 * \note This function always uses the maximum possible salt size,
1009 * up to the length of the payload hash. This choice of salt
1010 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
1011 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
1012 * minimum salt size which is the hash size minus 2 bytes. If
1013 * this minimum size is too large given the key size (the salt
1014 * size, plus the hash size, plus 2 bytes must be no more than
1015 * the key size in bytes), this function returns
1016 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
1017 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001018 * \deprecated It is deprecated and discouraged to call this function
1019 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
1020 * are likely to remove the \p mode argument and have it
1021 * implicitly set to #MBEDTLS_RSA_PRIVATE.
1022 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001023 * \note Alternative implementations of RSA need not support
1024 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001025 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001026 *
Hanno Becker9a467772018-12-13 09:54:59 +00001027 * \param ctx The initialized RSA context to use.
1028 * \param f_rng The RNG function. It must not be \c NULL.
1029 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
1030 * if \p f_rng doesn't need a context argument.
1031 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001032 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001033 * \param md_alg The message-digest algorithm used to hash the original data.
1034 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001035 * \param hashlen The length of the message digest.
1036 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
1037 * \param hash The buffer holding the message digest or raw data.
1038 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1039 * buffer of length \p hashlen Bytes. If \p md_alg is not
1040 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1041 * the size of the hash corresponding to \p md_alg.
1042 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +00001043 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
k-stachowiak4e36da32019-05-31 20:16:50 +02001044 * for an 2048-bit RSA modulus. A buffer length of
1045 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +01001046 *
1047 * \return \c 0 if the signing operation was successful.
1048 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001049 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001051 int (*f_rng)(void *, unsigned char *, size_t),
1052 void *p_rng,
1053 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001055 unsigned int hashlen,
1056 const unsigned char *hash,
1057 unsigned char *sig );
1058
1059/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001060 * \brief This function performs a public RSA operation and checks
1061 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +00001062 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001063 * This is the generic wrapper for performing a PKCS#1
1064 * verification using the mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001066 * \note For PKCS#1 v2.1 encoding, see comments on
1067 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
1068 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +01001069 *
1070 * \deprecated It is deprecated and discouraged to call this function
1071 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1072 * are likely to remove the \p mode argument and have it
1073 * set to #MBEDTLS_RSA_PUBLIC.
1074 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001075 * \note Alternative implementations of RSA need not support
1076 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001077 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001078 *
Hanno Becker9a467772018-12-13 09:54:59 +00001079 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001080 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1081 * this is used for blinding and should be provided; see
1082 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001083 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1084 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1085 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001086 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001087 * \param md_alg The message-digest algorithm used to hash the original data.
1088 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001089 * \param hashlen The length of the message digest.
1090 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1091 * \param hash The buffer holding the message digest or raw data.
1092 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1093 * buffer of length \p hashlen Bytes. If \p md_alg is not
1094 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1095 * the size of the hash corresponding to \p md_alg.
1096 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001097 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1098 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001099 *
1100 * \return \c 0 if the verify operation was successful.
1101 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001104 int (*f_rng)(void *, unsigned char *, size_t),
1105 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001106 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001108 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001109 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001110 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +00001111
1112/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001113 * \brief This function performs a PKCS#1 v1.5 verification
1114 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001115 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001116 * \deprecated It is deprecated and discouraged to call this function
1117 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1118 * are likely to remove the \p mode argument and have it
1119 * set to #MBEDTLS_RSA_PUBLIC.
1120 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001121 * \note Alternative implementations of RSA need not support
1122 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001123 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001124 *
Hanno Becker9a467772018-12-13 09:54:59 +00001125 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001126 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1127 * this is used for blinding and should be provided; see
1128 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001129 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1130 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1131 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001132 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +00001133 * \param md_alg The message-digest algorithm used to hash the original data.
1134 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001135 * \param hashlen The length of the message digest.
1136 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1137 * \param hash The buffer holding the message digest or raw data.
1138 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1139 * buffer of length \p hashlen Bytes. If \p md_alg is not
1140 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1141 * the size of the hash corresponding to \p md_alg.
1142 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001143 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1144 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001145 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001146 * \return \c 0 if the verify operation was successful.
1147 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001150 int (*f_rng)(void *, unsigned char *, size_t),
1151 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001152 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001154 unsigned int hashlen,
1155 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001156 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001157
1158/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001159 * \brief This function performs a PKCS#1 v2.1 PSS verification
1160 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001161 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001162 * The hash function for the MGF mask generating function
1163 * is that specified in the RSA context.
Paul Bakkerb3869132013-02-28 17:21:01 +01001164 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001165 * \note The \p hash_id in the RSA context is the one used for the
1166 * verification. \p md_alg in the function call is the type of
1167 * hash that is verified. According to <em>RFC-3447: Public-Key
1168 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1169 * Specifications</em> it is advised to keep both hashes the
1170 * same. If \p hash_id in the RSA context is unset,
1171 * the \p md_alg from the function call is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001172 *
1173 * \deprecated It is deprecated and discouraged to call this function
1174 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1175 * are likely to remove the \p mode argument and have it
1176 * implicitly set to #MBEDTLS_RSA_PUBLIC.
1177 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001178 * \note Alternative implementations of RSA need not support
1179 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001180 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001181 *
Hanno Becker9a467772018-12-13 09:54:59 +00001182 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001183 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1184 * this is used for blinding and should be provided; see
1185 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001186 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1187 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1188 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001189 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001190 * \param md_alg The message-digest algorithm used to hash the original data.
1191 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001192 * \param hashlen The length of the message digest.
1193 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1194 * \param hash The buffer holding the message digest or raw data.
1195 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1196 * buffer of length \p hashlen Bytes. If \p md_alg is not
1197 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1198 * the size of the hash corresponding to \p md_alg.
1199 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001200 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1201 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001202 *
1203 * \return \c 0 if the verify operation was successful.
1204 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001207 int (*f_rng)(void *, unsigned char *, size_t),
1208 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001209 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001211 unsigned int hashlen,
1212 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001213 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001214
1215/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001216 * \brief This function performs a PKCS#1 v2.1 PSS verification
1217 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001218 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001219 * The hash function for the MGF mask generating function
1220 * is that specified in \p mgf1_hash_id.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001221 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001222 * \note The \p sig buffer must be as large as the size
1223 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001224 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001225 * \note The \p hash_id in the RSA context is ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001226 *
Hanno Becker9a467772018-12-13 09:54:59 +00001227 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001228 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1229 * this is used for blinding and should be provided; see
1230 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001231 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1232 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1233 * \param mode The mode of operation. This must be either
1234 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +01001235 * \param md_alg The message-digest algorithm used to hash the original data.
1236 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001237 * \param hashlen The length of the message digest.
1238 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1239 * \param hash The buffer holding the message digest or raw data.
1240 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1241 * buffer of length \p hashlen Bytes. If \p md_alg is not
1242 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1243 * the size of the hash corresponding to \p md_alg.
1244 * \param mgf1_hash_id The message digest used for mask generation.
1245 * \param expected_salt_len The length of the salt used in padding. Use
1246 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1247 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001248 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1249 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001250 *
1251 * \return \c 0 if the verify operation was successful.
1252 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001255 int (*f_rng)(void *, unsigned char *, size_t),
1256 void *p_rng,
1257 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001259 unsigned int hashlen,
1260 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001262 int expected_salt_len,
1263 const unsigned char *sig );
1264
1265/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001266 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001267 *
Hanno Becker9a467772018-12-13 09:54:59 +00001268 * \param dst The destination context. This must be initialized.
1269 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001270 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001271 * \return \c 0 on success.
1272 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001275
1276/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001277 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001278 *
Hanno Becker9a467772018-12-13 09:54:59 +00001279 * \param ctx The RSA context to free. May be \c NULL, in which case
1280 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001281 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001284
Ron Eldorfa8f6352017-06-20 15:48:46 +03001285#if defined(MBEDTLS_SELF_TEST)
1286
Paul Bakker5121ce52009-01-03 21:22:43 +00001287/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001288 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001290 * \return \c 0 on success.
1291 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001294
Ron Eldorfa8f6352017-06-20 15:48:46 +03001295#endif /* MBEDTLS_SELF_TEST */
1296
Paul Bakker5121ce52009-01-03 21:22:43 +00001297#ifdef __cplusplus
1298}
1299#endif
1300
Paul Bakker5121ce52009-01-03 21:22:43 +00001301#endif /* rsa.h */