blob: 7b508c5dcaae0fa60f41d27a2ccfc601c98501fa [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Rose Zadik042e97f2018-01-26 16:35:10 +00004 * \brief The RSA public-key cryptosystem.
5 *
6 * For more information, see <em>Public-Key Cryptography Standards (PKCS)
7 * #1 v1.5: RSA Encryption</em> and <em>Public-Key Cryptography Standards
8 * (PKCS) #1 v2.1: RSA Cryptography Specifications</em>.
9 *
Darryl Greena40a1012018-01-05 15:33:17 +000010 */
11/*
Bence Szépkúti44bfbe32020-08-19 16:54:51 +020012 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020013 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
14 *
15 * This file is provided under the Apache License 2.0, or the
16 * GNU General Public License v2.0 or later.
17 *
18 * **********
19 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020020 *
21 * Licensed under the Apache License, Version 2.0 (the "License"); you may
22 * not use this file except in compliance with the License.
23 * You may obtain a copy of the License at
24 *
25 * http://www.apache.org/licenses/LICENSE-2.0
26 *
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 * See the License for the specific language governing permissions and
31 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000032 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020033 * **********
34 *
35 * **********
36 * GNU General Public License v2.0 or later:
37 *
38 * This program is free software; you can redistribute it and/or modify
39 * it under the terms of the GNU General Public License as published by
40 * the Free Software Foundation; either version 2 of the License, or
41 * (at your option) any later version.
42 *
43 * This program is distributed in the hope that it will be useful,
44 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 * GNU General Public License for more details.
47 *
48 * You should have received a copy of the GNU General Public License along
49 * with this program; if not, write to the Free Software Foundation, Inc.,
50 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
51 *
52 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#ifndef MBEDTLS_RSA_H
55#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020058#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020059#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020061#endif
Paul Bakkered27a042013-04-18 22:46:23 +020062
Paul Bakker314052f2011-08-15 09:07:52 +000063#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020064#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_THREADING_C)
Paul Bakkerc9965dc2013-09-29 14:58:17 +020067#include "threading.h"
68#endif
69
Paul Bakker13e2dfe2009-07-28 07:18:38 +000070/*
71 * RSA Error codes
72 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
74#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
75#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Rose Zadik042e97f2018-01-26 16:35:10 +000076#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 +020077#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
78#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
79#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
80#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
81#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Rose Zadik042e97f2018-01-26 16:35:10 +000082#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. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010083#define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580 /**< RSA hardware accelerator failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000084
85/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020086 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000087 */
Rose Zadik042e97f2018-01-26 16:35:10 +000088#define MBEDTLS_RSA_PUBLIC 0 /**< Request private key operation. */
89#define MBEDTLS_RSA_PRIVATE 1 /**< Request public key operation. */
Paul Bakker5121ce52009-01-03 21:22:43 +000090
Rose Zadik042e97f2018-01-26 16:35:10 +000091#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS-1 v1.5 encoding. */
92#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS-1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000093
Rose Zadik042e97f2018-01-26 16:35:10 +000094#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
95#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020098
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020099/*
100 * The above constants may be used even if the RSA module is compile out,
101 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
102 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +0100103
104#if !defined(MBEDTLS_RSA_ALT)
105// Regular implementation
106//
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +0200107
Paul Bakker407a0da2013-06-27 14:29:21 +0200108#ifdef __cplusplus
109extern "C" {
110#endif
111
Hanno Beckera3ebec22017-08-23 14:06:24 +0100112/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000113 * \brief The RSA context structure.
Hanno Becker5063cd22017-09-29 11:49:12 +0100114 *
115 * \note Direct manipulation of the members of this structure
Rose Zadik042e97f2018-01-26 16:35:10 +0000116 * is deprecated. All manipulation should instead be done through
117 * the public interface functions.
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 */
119typedef struct
120{
Rose Zadik042e97f2018-01-26 16:35:10 +0000121 int ver; /*!< Always 0.*/
122 size_t len; /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
Rose Zadik042e97f2018-01-26 16:35:10 +0000124 mbedtls_mpi N; /*!< The public modulus. */
125 mbedtls_mpi E; /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
Rose Zadik042e97f2018-01-26 16:35:10 +0000127 mbedtls_mpi D; /*!< The private exponent. */
128 mbedtls_mpi P; /*!< The first prime factor. */
129 mbedtls_mpi Q; /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100130
Rose Zadik042e97f2018-01-26 16:35:10 +0000131 mbedtls_mpi DP; /*!< \p D % (P - 1) */
132 mbedtls_mpi DQ; /*!< \p D % (Q - 1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
Rose Zadik042e97f2018-01-26 16:35:10 +0000135 mbedtls_mpi RN; /*!< cached R^2 mod \p N */
Hanno Becker1a59e792017-08-23 07:41:10 +0100136
Rose Zadik042e97f2018-01-26 16:35:10 +0000137 mbedtls_mpi RP; /*!< cached R^2 mod \p P */
138 mbedtls_mpi RQ; /*!< cached R^2 mod \p Q */
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
Rose Zadik042e97f2018-01-26 16:35:10 +0000140 mbedtls_mpi Vi; /*!< The cached blinding value. */
141 mbedtls_mpi Vf; /*!< The cached un-blinding value. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200142
Rose Zadik042e97f2018-01-26 16:35:10 +0000143 int padding; /*!< Selects padding mode:
144 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
145 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
146 int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
147 as specified in md.h for use in the MGF
148 mask generating function used in the
149 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#if defined(MBEDTLS_THREADING_C)
Rose Zadik042e97f2018-01-26 16:35:10 +0000151 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200152#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000153}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
Paul Bakker5121ce52009-01-03 21:22:43 +0000156/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000157 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000159 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000160 * encryption scheme and the RSASSA-PSS signature scheme.
161 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000162 * \param ctx The RSA context to initialize.
163 * \param padding Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or
164 * #MBEDTLS_RSA_PKCS_V21.
165 * \param hash_id The hash identifier of #mbedtls_md_type_t type, if
166 * \p padding is #MBEDTLS_RSA_PKCS_V21.
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000168 * \note The \p hash_id parameter is ignored when using
169 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200170 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000171 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200172 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000173 * mixing padding modes. For public key operations it is
Antonin Décimo8fd91562019-01-23 15:24:37 +0100174 * a default value, which can be overridden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000175 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200176 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000177 * \note The hash selected in \p hash_id is always used for OEAP
178 * encryption. For PSS signatures, it is always used for
Antonin Décimo8fd91562019-01-23 15:24:37 +0100179 * making signatures, but can be overridden for verifying them.
180 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100183 int padding,
184 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100186/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000187 * \brief This function imports a set of core parameters into an
188 * RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100189 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000190 * \param ctx The initialized RSA context to store the parameters in.
191 * \param N The RSA modulus, or NULL.
192 * \param P The first prime factor of \p N, or NULL.
193 * \param Q The second prime factor of \p N, or NULL.
194 * \param D The private exponent, or NULL.
195 * \param E The public exponent, or NULL.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100196 *
197 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000198 * imports, if the parameters are not simultaneously present.
199 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100200 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000201 * by a call to mbedtls_rsa_complete(), which checks and
202 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100203 * public or private RSA key.
204 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000205 * \note See mbedtls_rsa_complete() for more information on which
206 * parameters are necessary to set up a private or public
207 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100208 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100209 * \note The imported parameters are copied and need not be preserved
210 * for the lifetime of the RSA context being set up.
211 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000212 * \return \c 0 on success, or a non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100213 */
214int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
215 const mbedtls_mpi *N,
216 const mbedtls_mpi *P, const mbedtls_mpi *Q,
217 const mbedtls_mpi *D, const mbedtls_mpi *E );
218
219/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000220 * \brief This function imports core RSA parameters, in raw big-endian
221 * binary format, into an RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100222 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000223 * \param ctx The initialized RSA context to store the parameters in.
224 * \param N The RSA modulus, or NULL.
225 * \param N_len The Byte length of \p N, ignored if \p N == NULL.
226 * \param P The first prime factor of \p N, or NULL.
227 * \param P_len The Byte length of \p P, ignored if \p P == NULL.
228 * \param Q The second prime factor of \p N, or NULL.
229 * \param Q_len The Byte length of \p Q, ignored if \p Q == NULL.
230 * \param D The private exponent, or NULL.
231 * \param D_len The Byte length of \p D, ignored if \p D == NULL.
232 * \param E The public exponent, or NULL.
233 * \param E_len The Byte length of \p E, ignored if \p E == NULL.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100234 *
235 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000236 * imports, if the parameters are not simultaneously present.
237 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100238 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000239 * by a call to mbedtls_rsa_complete(), which checks and
240 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100241 * public or private RSA key.
242 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000243 * \note See mbedtls_rsa_complete() for more information on which
244 * parameters are necessary to set up a private or public
245 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100246 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100247 * \note The imported parameters are copied and need not be preserved
248 * for the lifetime of the RSA context being set up.
249 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000250 * \return \c 0 on success, or a non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100251 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100252int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100253 unsigned char const *N, size_t N_len,
254 unsigned char const *P, size_t P_len,
255 unsigned char const *Q, size_t Q_len,
256 unsigned char const *D, size_t D_len,
257 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100258
259/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000260 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100261 * a set of imported core parameters.
262 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000263 * To setup an RSA public key, precisely \p N and \p E
264 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100265 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000266 * To setup an RSA private key, sufficient information must
267 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100268 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000269 * The default implementation supports the following:
270 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
271 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
272 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100273 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000274 * If this function runs successfully, it guarantees that
275 * the RSA context can be used for RSA operations without
276 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100277 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000278 * \param ctx The initialized RSA context holding imported parameters.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100279 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000280 * \return \c 0 on success, or #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the
281 * attempted derivations failed.
Hanno Becker43a08d02017-10-02 13:16:35 +0100282 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100283 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000284 * for the imported parameters. In particular, parameters that
285 * are not needed by the implementation might be silently
286 * discarded and left unchecked. To check the consistency
287 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100288 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100289 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100290int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100291
292/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000293 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100294 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000295 * If this function runs successfully, the non-NULL buffers
296 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
297 * written, with additional unused space filled leading by
298 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100299 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000300 * Possible reasons for returning
301 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:<ul>
302 * <li>An alternative RSA implementation is in use, which
303 * stores the key externally, and either cannot or should
304 * not export it into RAM.</li>
305 * <li>A SW or HW implementation might not support a certain
306 * deduction. For example, \p P, \p Q from \p N, \p D,
307 * and \p E if the former are not part of the
308 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100309 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000310 * If the function fails due to an unsupported operation,
311 * the RSA context stays intact and remains usable.
312 *
313 * \param ctx The initialized RSA context.
314 * \param N The MPI to hold the RSA modulus, or NULL.
315 * \param P The MPI to hold the first prime factor of \p N, or NULL.
316 * \param Q The MPI to hold the second prime factor of \p N, or NULL.
317 * \param D The MPI to hold the private exponent, or NULL.
318 * \param E The MPI to hold the public exponent, or NULL.
319 *
320 * \return \c 0 on success,
321 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION if exporting the
322 * requested parameters cannot be done due to missing
323 * functionality or because of security policies,
324 * or a non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100325 *
326 */
327int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
328 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
329 mbedtls_mpi *D, mbedtls_mpi *E );
330
331/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000332 * \brief This function exports core parameters of an RSA key
333 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100334 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000335 * If this function runs successfully, the non-NULL buffers
336 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
337 * written, with additional unused space filled leading by
338 * zero Bytes.
339 *
340 * Possible reasons for returning
341 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:<ul>
342 * <li>An alternative RSA implementation is in use, which
343 * stores the key externally, and either cannot or should
344 * not export it into RAM.</li>
345 * <li>A SW or HW implementation might not support a certain
346 * deduction. For example, \p P, \p Q from \p N, \p D,
347 * and \p E if the former are not part of the
348 * implementation.</li></ul>
349 * If the function fails due to an unsupported operation,
350 * the RSA context stays intact and remains usable.
351 *
352 * \param ctx The initialized RSA context.
353 * \param N The Byte array to store the RSA modulus, or NULL.
354 * \param N_len The size of the buffer for the modulus.
355 * \param P The Byte array to hold the first prime factor of \p N, or
356 * NULL.
357 * \param P_len The size of the buffer for the first prime factor.
358 * \param Q The Byte array to hold the second prime factor of \p N, or
359 NULL.
360 * \param Q_len The size of the buffer for the second prime factor.
361 * \param D The Byte array to hold the private exponent, or NULL.
362 * \param D_len The size of the buffer for the private exponent.
363 * \param E The Byte array to hold the public exponent, or NULL.
364 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100365 *
366 * \note The length fields are ignored if the corresponding
367 * buffer pointers are NULL.
368 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000369 * \return \c 0 on success,
370 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION if exporting the
371 * requested parameters cannot be done due to missing
372 * functionality or because of security policies,
373 * or a non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100374 */
375int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
376 unsigned char *N, size_t N_len,
377 unsigned char *P, size_t P_len,
378 unsigned char *Q, size_t Q_len,
379 unsigned char *D, size_t D_len,
380 unsigned char *E, size_t E_len );
381
382/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000383 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100384 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000385 * \param ctx The initialized RSA context.
386 * \param DP The MPI to hold D modulo P-1, or NULL.
387 * \param DQ The MPI to hold D modulo Q-1, or NULL.
388 * \param QP The MPI to hold modular inverse of Q modulo P, or NULL.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100389 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000390 * \return \c 0 on success, non-zero error code otherwise.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100391 *
392 * \note Alternative RSA implementations not using CRT-parameters
Rose Zadik042e97f2018-01-26 16:35:10 +0000393 * internally can implement this function based on
394 * mbedtls_rsa_deduce_opt().
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100395 *
396 */
397int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
398 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
399
Paul Bakker5121ce52009-01-03 21:22:43 +0000400/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000401 * \brief This function sets padding for an already initialized RSA
402 * context. See mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100403 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000404 * \param ctx The RSA context to be set.
405 * \param padding Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or
406 * #MBEDTLS_RSA_PKCS_V21.
407 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100408 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100409void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
410 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100411
412/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000413 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100414 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000415 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100416 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000417 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100418 *
419 */
420size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
421
422/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000423 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000425 * \param ctx The RSA context used to hold the key.
426 * \param f_rng The RNG function.
427 * \param p_rng The RNG parameter.
428 * \param nbits The size of the public key in bits.
429 * \param exponent The public exponent. For example, 65537.
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000431 * \note mbedtls_rsa_init() must be called before this function,
432 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000434 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
435 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000436 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100438 int (*f_rng)(void *, unsigned char *, size_t),
439 void *p_rng,
440 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000441
442/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000443 * \brief This function checks if a context contains at least an RSA
444 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000445 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000446 * If the function runs successfully, it is guaranteed that
447 * enough information is present to perform an RSA public key
448 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000449 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000450 * \param ctx The RSA context to check.
451 *
452 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
453 * on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100454 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000457
458/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000459 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100460 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000462 * \param ctx The RSA context to check.
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000464 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code on
465 * failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100466 *
Hanno Becker68767a62017-10-17 10:13:31 +0100467 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000468 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100469 * on the given context, but that the various parameters are
470 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000471 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100472 *
473 * \warning This function should catch accidental misconfigurations
474 * like swapping of parameters, but it cannot establish full
475 * trust in neither the quality nor the consistency of the key
476 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000477 * <ul><li>Consistency: Imported parameters that are irrelevant
478 * for the implementation might be silently dropped. If dropped,
479 * the current function does not have access to them,
480 * and therefore cannot check them. See mbedtls_rsa_complete().
481 * If you want to check the consistency of the entire
482 * content of an PKCS1-encoded RSA private key, for example, you
483 * should use mbedtls_rsa_validate_params() before setting
484 * up the RSA context.
485 * Additionally, if the implementation performs empirical checks,
486 * these checks substantiate but do not guarantee consistency.</li>
487 * <li>Quality: This function is not expected to perform
488 * extended quality assessments like checking that the prime
489 * factors are safe. Additionally, it is the responsibility of the
490 * user to ensure the trustworthiness of the source of his RSA
491 * parameters, which goes beyond what is effectively checkable
492 * by the library.</li></ul>
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000497 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100498 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000499 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100500 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000501 * \param pub The RSA context holding the public key.
502 * \param prv The RSA context holding the private key.
503 *
504 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
505 * on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100506 */
Hanno Becker98838b02017-10-02 13:16:10 +0100507int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
508 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100509
510/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000511 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000513 * \param ctx The RSA context.
514 * \param input The input buffer.
515 * \param output The output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000517 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
518 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000520 * \note This function does not handle message padding.
521 *
522 * \note Make sure to set \p input[0] = 0 or ensure that
523 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 *
525 * \note The input and output buffers must be large
Rose Zadik042e97f2018-01-26 16:35:10 +0000526 * enough. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker5121ce52009-01-03 21:22:43 +0000527 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000529 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000530 unsigned char *output );
531
532/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000533 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000535 * \param ctx The RSA context.
536 * \param f_rng The RNG function. Needed for blinding.
537 * \param p_rng The RNG parameter.
538 * \param input The input buffer.
539 * \param output The output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000541 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
542 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000543 *
544 * \note The input and output buffers must be large
Rose Zadik042e97f2018-01-26 16:35:10 +0000545 * enough. For example, 128 Bytes if RSA-1024 is used.
Hanno Becker26f1f602018-03-09 10:47:30 +0000546 *
547 * \note Blinding is used if and only if a PRNG is provided.
548 *
549 * \note If blinding is used, both the base of exponentation
550 * and the exponent are blinded, providing protection
551 * against some side-channel attacks.
552 *
553 * \warning It is deprecated and a security risk to not provide
554 * a PRNG here and thereby prevent the use of blinding.
555 * Future versions of the library may enforce the presence
556 * of a PRNG.
557 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200560 int (*f_rng)(void *, unsigned char *, size_t),
561 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000562 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 unsigned char *output );
564
565/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000566 * \brief This function adds the message padding, then performs an RSA
567 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000569 * It is the generic wrapper for performing a PKCS#1 encryption
570 * operation using the \p mode from the context.
571 *
572 *
573 * \param ctx The RSA context.
574 * \param f_rng The RNG function. Needed for padding, PKCS#1 v2.1
575 * encoding, and #MBEDTLS_RSA_PRIVATE.
576 * \param p_rng The RNG parameter.
577 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
578 * \param ilen The length of the plaintext.
579 * \param input The buffer holding the data to encrypt.
580 * \param output The buffer used to hold the ciphertext.
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100582 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000583 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
584 * are likely to remove the \p mode argument and have it
585 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100586 *
587 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000588 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
589 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100590 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000591 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
592 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000594 * \note The input and output buffers must be as large as the size
595 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker5121ce52009-01-03 21:22:43 +0000596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000598 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000599 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000600 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000601 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000602 unsigned char *output );
603
604/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000605 * \brief This function performs a PKCS#1 v1.5 encryption operation
606 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100607 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000608 * \param ctx The RSA context.
609 * \param f_rng The RNG function. Needed for padding and
610 * #MBEDTLS_RSA_PRIVATE.
611 * \param p_rng The RNG parameter.
612 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
613 * \param ilen The length of the plaintext.
614 * \param input The buffer holding the data to encrypt.
615 * \param output The buffer used to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100616 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100617 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000618 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
619 * are likely to remove the \p mode argument and have it
620 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100621 *
622 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000623 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
624 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100625 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000626 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
627 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100628 *
629 * \note The output buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000630 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100633 int (*f_rng)(void *, unsigned char *, size_t),
634 void *p_rng,
635 int mode, size_t ilen,
636 const unsigned char *input,
637 unsigned char *output );
638
639/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000640 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
641 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100642 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000643 * \param ctx The RSA context.
644 * \param f_rng The RNG function. Needed for padding and PKCS#1 v2.1
645 * encoding and #MBEDTLS_RSA_PRIVATE.
646 * \param p_rng The RNG parameter.
647 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
648 * \param label The buffer holding the custom label to use.
649 * \param label_len The length of the label.
650 * \param ilen The length of the plaintext.
651 * \param input The buffer holding the data to encrypt.
652 * \param output The buffer used to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100653 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100654 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000655 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
656 * are likely to remove the \p mode argument and have it
657 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100658 *
659 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000660 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
661 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100662 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000663 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
664 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100665 *
666 * \note The output buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000667 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100670 int (*f_rng)(void *, unsigned char *, size_t),
671 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100672 int mode,
673 const unsigned char *label, size_t label_len,
674 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100675 const unsigned char *input,
676 unsigned char *output );
677
678/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000679 * \brief This function performs an RSA operation, then removes the
680 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000682 * It is the generic wrapper for performing a PKCS#1 decryption
683 * operation using the \p mode from the context.
684 *
685 * \param ctx The RSA context.
686 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
687 * \param p_rng The RNG parameter.
688 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
689 * \param olen The length of the plaintext.
690 * \param input The buffer holding the encrypted data.
691 * \param output The buffer used to hold the plaintext.
692 * \param output_max_len The maximum length of the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100694 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000695 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
696 * are likely to remove the \p mode argument and have it
697 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100698 *
699 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000700 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
701 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100702 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000703 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
704 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000705 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100706 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000707 * as large as the size \p ctx->len of \p ctx->N (for example,
708 * 128 Bytes if RSA-1024 is used) to be able to hold an
709 * arbitrary decrypted message. If it is not large enough to
710 * hold the decryption of the particular ciphertext provided,
711 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100712 *
713 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000714 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200717 int (*f_rng)(void *, unsigned char *, size_t),
718 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000719 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000720 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000721 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000722 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000723
724/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000725 * \brief This function performs a PKCS#1 v1.5 decryption
726 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100727 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000728 * \param ctx The RSA context.
729 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
730 * \param p_rng The RNG parameter.
731 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
732 * \param olen The length of the plaintext.
733 * \param input The buffer holding the encrypted data.
734 * \param output The buffer to hold the plaintext.
735 * \param output_max_len The maximum length of the output buffer.
Paul Bakkerb3869132013-02-28 17:21:01 +0100736 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100737 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000738 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
739 * are likely to remove the \p mode argument and have it
740 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100741 *
742 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000743 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
744 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100745 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000746 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
747 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100748 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100749 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000750 * as large as the size \p ctx->len of \p ctx->N, for example,
751 * 128 Bytes if RSA-1024 is used, to be able to hold an
752 * arbitrary decrypted message. If it is not large enough to
753 * hold the decryption of the particular ciphertext provided,
754 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100755 *
756 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000757 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200760 int (*f_rng)(void *, unsigned char *, size_t),
761 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100762 int mode, size_t *olen,
763 const unsigned char *input,
764 unsigned char *output,
765 size_t output_max_len );
766
767/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000768 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
769 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100770 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000771 * \param ctx The RSA context.
772 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
773 * \param p_rng The RNG parameter.
774 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
775 * \param label The buffer holding the custom label to use.
776 * \param label_len The length of the label.
777 * \param olen The length of the plaintext.
778 * \param input The buffer holding the encrypted data.
779 * \param output The buffer to hold the plaintext.
780 * \param output_max_len The maximum length of the output buffer.
Paul Bakkerb3869132013-02-28 17:21:01 +0100781 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100782 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000783 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
784 * are likely to remove the \p mode argument and have it
785 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100786 *
787 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000788 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
789 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100790 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000791 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
792 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100793 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100794 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000795 * as large as the size \p ctx->len of \p ctx->N, for
796 * example, 128 Bytes if RSA-1024 is used, to be able to
797 * hold an arbitrary decrypted message. If it is not
798 * large enough to hold the decryption of the particular
799 * ciphertext provided, the function returns
800 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100801 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100802 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000803 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200806 int (*f_rng)(void *, unsigned char *, size_t),
807 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100808 int mode,
809 const unsigned char *label, size_t label_len,
810 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100811 const unsigned char *input,
812 unsigned char *output,
813 size_t output_max_len );
814
815/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000816 * \brief This function performs a private RSA operation to sign
817 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000818 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000819 * It is the generic wrapper for performing a PKCS#1
820 * signature using the \p mode from the context.
821 *
822 * \param ctx The RSA context.
823 * \param f_rng The RNG function. Needed for PKCS#1 v2.1 encoding and for
824 * #MBEDTLS_RSA_PRIVATE.
825 * \param p_rng The RNG parameter.
826 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
827 * \param md_alg The message-digest algorithm used to hash the original data.
828 * Use #MBEDTLS_MD_NONE for signing raw data.
829 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
830 * \param hash The buffer holding the message digest.
831 * \param sig The buffer to hold the ciphertext.
Paul Bakker5121ce52009-01-03 21:22:43 +0000832 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100833 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000834 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
835 * are likely to remove the \p mode argument and have it
836 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100837 *
838 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000839 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
840 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100841 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000842 * \return \c 0 if the signing operation was successful,
843 * or an \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000844 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000845 * \note The \p sig buffer must be as large as the size
846 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
k-stachowiakeee98e92019-05-31 20:16:50 +0200847 * A buffer length of #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000848 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000849 * \note For PKCS#1 v2.1 encoding, see comments on
850 * mbedtls_rsa_rsassa_pss_sign() for details on
851 * \p md_alg and \p hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000852 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000854 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000855 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000856 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000858 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000859 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000860 unsigned char *sig );
861
862/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000863 * \brief This function performs a PKCS#1 v1.5 signature
864 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100865 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000866 * \param ctx The RSA context.
867 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
868 * \param p_rng The RNG parameter.
869 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
870 * \param md_alg The message-digest algorithm used to hash the original data.
871 * Use #MBEDTLS_MD_NONE for signing raw data.
872 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
873 * \param hash The buffer holding the message digest.
874 * \param sig The buffer to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100875 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100876 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000877 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
878 * are likely to remove the \p mode argument and have it
879 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100880 *
881 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000882 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
883 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100884 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000885 * \return \c 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100886 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +0000887 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100888 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000889 * \note The \p sig buffer must be as large as the size
890 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
k-stachowiakeee98e92019-05-31 20:16:50 +0200891 * A buffer length of #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200894 int (*f_rng)(void *, unsigned char *, size_t),
895 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100896 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100898 unsigned int hashlen,
899 const unsigned char *hash,
900 unsigned char *sig );
901
902/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000903 * \brief This function performs a PKCS#1 v2.1 PSS signature
904 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100905 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000906 * \param ctx The RSA context.
907 * \param f_rng The RNG function. Needed for PKCS#1 v2.1 encoding and for
908 * #MBEDTLS_RSA_PRIVATE.
909 * \param p_rng The RNG parameter.
910 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
911 * \param md_alg The message-digest algorithm used to hash the original data.
912 * Use #MBEDTLS_MD_NONE for signing raw data.
913 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
914 * \param hash The buffer holding the message digest.
915 * \param sig The buffer to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100916 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100917 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000918 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
919 * are likely to remove the \p mode argument and have it
920 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100921 *
922 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000923 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
924 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100925 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000926 * \return \c 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100927 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +0000928 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100929 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000930 * \note The \p sig buffer must be as large as the size
931 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
k-stachowiakeee98e92019-05-31 20:16:50 +0200932 * A buffer length of #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100933 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000934 * \note The \p hash_id in the RSA context is the one used for the
935 * encoding. \p md_alg in the function call is the type of hash
936 * that is encoded. According to <em>RFC-3447: Public-Key
937 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
938 * Specifications</em> it is advised to keep both hashes the
939 * same.
Paul Bakkerb3869132013-02-28 17:21:01 +0100940 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100942 int (*f_rng)(void *, unsigned char *, size_t),
943 void *p_rng,
944 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100946 unsigned int hashlen,
947 const unsigned char *hash,
948 unsigned char *sig );
949
950/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000951 * \brief This function performs a public RSA operation and checks
952 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +0000953 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000954 * This is the generic wrapper for performing a PKCS#1
955 * verification using the mode from the context.
956 *
957 * \param ctx The RSA public key context.
958 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
959 * \param p_rng The RNG parameter.
960 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
961 * \param md_alg The message-digest algorithm used to hash the original data.
962 * Use #MBEDTLS_MD_NONE for signing raw data.
963 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
964 * \param hash The buffer holding the message digest.
965 * \param sig The buffer holding the ciphertext.
Paul Bakker5121ce52009-01-03 21:22:43 +0000966 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100967 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000968 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
969 * are likely to remove the \p mode argument and have it
970 * set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100971 *
972 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000973 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
974 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100975 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000976 * \return \c 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100977 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +0000978 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000979 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000980 * \note The \p sig buffer must be as large as the size
981 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000982 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000983 * \note For PKCS#1 v2.1 encoding, see comments on
984 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
985 * \p hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000986 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200988 int (*f_rng)(void *, unsigned char *, size_t),
989 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000990 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000992 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000993 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200994 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000995
996/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000997 * \brief This function performs a PKCS#1 v1.5 verification
998 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +0100999 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001000 * \param ctx The RSA public key context.
1001 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
1002 * \param p_rng The RNG parameter.
1003 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1004 * \param md_alg The message-digest algorithm used to hash the original data.
1005 * Use #MBEDTLS_MD_NONE for signing raw data.
1006 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
1007 * \param hash The buffer holding the message digest.
1008 * \param sig The buffer holding the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +01001009 *
Hanno Becker3cdc7112017-10-05 10:09:31 +01001010 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +00001011 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1012 * are likely to remove the \p mode argument and have it
1013 * set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +01001014 *
1015 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +00001016 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
1017 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +01001018 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001019 * \return \c 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001020 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +00001021 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001022 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001023 * \note The \p sig buffer must be as large as the size
1024 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001027 int (*f_rng)(void *, unsigned char *, size_t),
1028 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001029 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001031 unsigned int hashlen,
1032 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001033 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001034
1035/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001036 * \brief This function performs a PKCS#1 v2.1 PSS verification
1037 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001038 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001039 * The hash function for the MGF mask generating function
1040 * is that specified in the RSA context.
1041 *
1042 * \param ctx The RSA public key context.
1043 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
1044 * \param p_rng The RNG parameter.
1045 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1046 * \param md_alg The message-digest algorithm used to hash the original data.
1047 * Use #MBEDTLS_MD_NONE for signing raw data.
1048 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
1049 * \param hash The buffer holding the message digest.
1050 * \param sig The buffer holding the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +01001051 *
Hanno Becker3cdc7112017-10-05 10:09:31 +01001052 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +00001053 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1054 * are likely to remove the \p mode argument and have it
1055 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +01001056 *
1057 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +00001058 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
1059 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +01001060 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001061 * \return \c 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001062 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +00001063 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001064 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001065 * \note The \p sig buffer must be as large as the size
1066 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001067 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001068 * \note The \p hash_id in the RSA context is the one used for the
1069 * verification. \p md_alg in the function call is the type of
1070 * hash that is verified. According to <em>RFC-3447: Public-Key
1071 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1072 * Specifications</em> it is advised to keep both hashes the
1073 * same. If \p hash_id in the RSA context is unset,
1074 * the \p md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001077 int (*f_rng)(void *, unsigned char *, size_t),
1078 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001079 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001081 unsigned int hashlen,
1082 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001083 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001084
1085/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001086 * \brief This function performs a PKCS#1 v2.1 PSS verification
1087 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001088 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001089 * The hash function for the MGF mask generating function
1090 * is that specified in \p mgf1_hash_id.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001091 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001092 * \param ctx The RSA public key context.
1093 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
1094 * \param p_rng The RNG parameter.
1095 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1096 * \param md_alg The message-digest algorithm used to hash the original data.
1097 * Use #MBEDTLS_MD_NONE for signing raw data.
1098 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
1099 * \param hash The buffer holding the message digest.
1100 * \param mgf1_hash_id The message digest used for mask generation.
1101 * \param expected_salt_len The length of the salt used in padding. Use
1102 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1103 * \param sig The buffer holding the ciphertext.
1104 *
1105 * \return \c 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001106 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +00001107 * on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001108 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001109 * \note The \p sig buffer must be as large as the size
1110 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001111 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001112 * \note The \p hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001115 int (*f_rng)(void *, unsigned char *, size_t),
1116 void *p_rng,
1117 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001119 unsigned int hashlen,
1120 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001122 int expected_salt_len,
1123 const unsigned char *sig );
1124
1125/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001126 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001127 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001128 * \param dst The destination context.
1129 * \param src The source context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001130 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001131 * \return \c 0 on success,
1132 * #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001135
1136/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001137 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001138 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001139 * \param ctx The RSA Context to free.
Paul Bakker5121ce52009-01-03 21:22:43 +00001140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001142
Hanno Beckerd22b78b2017-10-12 11:42:17 +01001143#ifdef __cplusplus
1144}
1145#endif
1146
1147#else /* MBEDTLS_RSA_ALT */
1148#include "rsa_alt.h"
1149#endif /* MBEDTLS_RSA_ALT */
1150
1151#ifdef __cplusplus
1152extern "C" {
1153#endif
1154
Paul Bakker5121ce52009-01-03 21:22:43 +00001155/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001156 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001157 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001158 * \return \c 0 on success, or \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001161
1162#ifdef __cplusplus
1163}
1164#endif
1165
Paul Bakker5121ce52009-01-03 21:22:43 +00001166#endif /* rsa.h */