blob: 752e047cd8a22d031aea7f951804e9c63ca9fdfb [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/*
Rose Zadik042e97f2018-01-26 16:35:10 +000012 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
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 * **********
53 *
Rose Zadik042e97f2018-01-26 16:35:10 +000054 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#ifndef MBEDTLS_RSA_H
57#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020060#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020061#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020063#endif
Paul Bakkered27a042013-04-18 22:46:23 +020064
Paul Bakker314052f2011-08-15 09:07:52 +000065#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020066#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_THREADING_C)
Paul Bakkerc9965dc2013-09-29 14:58:17 +020069#include "threading.h"
70#endif
71
Paul Bakker13e2dfe2009-07-28 07:18:38 +000072/*
73 * RSA Error codes
74 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
76#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
77#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Rose Zadik042e97f2018-01-26 16:35:10 +000078#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 +020079#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
80#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
81#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
82#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
83#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Rose Zadik042e97f2018-01-26 16:35:10 +000084#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 +010085#define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580 /**< RSA hardware accelerator failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020088 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000089 */
Rose Zadik042e97f2018-01-26 16:35:10 +000090#define MBEDTLS_RSA_PUBLIC 0 /**< Request private key operation. */
91#define MBEDTLS_RSA_PRIVATE 1 /**< Request public key operation. */
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Rose Zadik042e97f2018-01-26 16:35:10 +000093#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS-1 v1.5 encoding. */
94#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS-1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000095
Rose Zadik042e97f2018-01-26 16:35:10 +000096#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
97#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +000098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200100
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +0200101/*
102 * The above constants may be used even if the RSA module is compile out,
103 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
104 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +0100105
106#if !defined(MBEDTLS_RSA_ALT)
107// Regular implementation
108//
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +0200109
Paul Bakker407a0da2013-06-27 14:29:21 +0200110#ifdef __cplusplus
111extern "C" {
112#endif
113
Hanno Beckera3ebec22017-08-23 14:06:24 +0100114/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000115 * \brief The RSA context structure.
Hanno Becker5063cd22017-09-29 11:49:12 +0100116 *
117 * \note Direct manipulation of the members of this structure
Rose Zadik042e97f2018-01-26 16:35:10 +0000118 * is deprecated. All manipulation should instead be done through
119 * the public interface functions.
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 */
121typedef struct
122{
Rose Zadik042e97f2018-01-26 16:35:10 +0000123 int ver; /*!< Always 0.*/
124 size_t len; /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
Rose Zadik042e97f2018-01-26 16:35:10 +0000126 mbedtls_mpi N; /*!< The public modulus. */
127 mbedtls_mpi E; /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
Rose Zadik042e97f2018-01-26 16:35:10 +0000129 mbedtls_mpi D; /*!< The private exponent. */
130 mbedtls_mpi P; /*!< The first prime factor. */
131 mbedtls_mpi Q; /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100132
Rose Zadik042e97f2018-01-26 16:35:10 +0000133 mbedtls_mpi DP; /*!< \p D % (P - 1) */
134 mbedtls_mpi DQ; /*!< \p D % (Q - 1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
Rose Zadik042e97f2018-01-26 16:35:10 +0000137 mbedtls_mpi RN; /*!< cached R^2 mod \p N */
Hanno Becker1a59e792017-08-23 07:41:10 +0100138
Rose Zadik042e97f2018-01-26 16:35:10 +0000139 mbedtls_mpi RP; /*!< cached R^2 mod \p P */
140 mbedtls_mpi RQ; /*!< cached R^2 mod \p Q */
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
Rose Zadik042e97f2018-01-26 16:35:10 +0000142 mbedtls_mpi Vi; /*!< The cached blinding value. */
143 mbedtls_mpi Vf; /*!< The cached un-blinding value. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200144
Rose Zadik042e97f2018-01-26 16:35:10 +0000145 int padding; /*!< Selects padding mode:
146 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
147 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
148 int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
149 as specified in md.h for use in the MGF
150 mask generating function used in the
151 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#if defined(MBEDTLS_THREADING_C)
Rose Zadik042e97f2018-01-26 16:35:10 +0000153 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200154#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000155}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
Paul Bakker5121ce52009-01-03 21:22:43 +0000158/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000159 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000161 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000162 * encryption scheme and the RSASSA-PSS signature scheme.
163 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000164 * \param ctx The RSA context to initialize.
165 * \param padding Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or
166 * #MBEDTLS_RSA_PKCS_V21.
167 * \param hash_id The hash identifier of #mbedtls_md_type_t type, if
168 * \p padding is #MBEDTLS_RSA_PKCS_V21.
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000170 * \note The \p hash_id parameter is ignored when using
171 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200172 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000173 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200174 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000175 * mixing padding modes. For public key operations it is
Antonin Décimo8fd91562019-01-23 15:24:37 +0100176 * a default value, which can be overridden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000177 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200178 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000179 * \note The hash selected in \p hash_id is always used for OEAP
180 * encryption. For PSS signatures, it is always used for
Antonin Décimo8fd91562019-01-23 15:24:37 +0100181 * making signatures, but can be overridden for verifying them.
182 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100185 int padding,
186 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100188/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000189 * \brief This function imports a set of core parameters into an
190 * RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100191 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000192 * \param ctx The initialized RSA context to store the parameters in.
193 * \param N The RSA modulus, or NULL.
194 * \param P The first prime factor of \p N, or NULL.
195 * \param Q The second prime factor of \p N, or NULL.
196 * \param D The private exponent, or NULL.
197 * \param E The public exponent, or NULL.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100198 *
199 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000200 * imports, if the parameters are not simultaneously present.
201 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100202 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000203 * by a call to mbedtls_rsa_complete(), which checks and
204 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100205 * public or private RSA key.
206 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000207 * \note See mbedtls_rsa_complete() for more information on which
208 * parameters are necessary to set up a private or public
209 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100210 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100211 * \note The imported parameters are copied and need not be preserved
212 * for the lifetime of the RSA context being set up.
213 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000214 * \return \c 0 on success, or a non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100215 */
216int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
217 const mbedtls_mpi *N,
218 const mbedtls_mpi *P, const mbedtls_mpi *Q,
219 const mbedtls_mpi *D, const mbedtls_mpi *E );
220
221/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000222 * \brief This function imports core RSA parameters, in raw big-endian
223 * binary format, into an RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100224 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000225 * \param ctx The initialized RSA context to store the parameters in.
226 * \param N The RSA modulus, or NULL.
227 * \param N_len The Byte length of \p N, ignored if \p N == NULL.
228 * \param P The first prime factor of \p N, or NULL.
229 * \param P_len The Byte length of \p P, ignored if \p P == NULL.
230 * \param Q The second prime factor of \p N, or NULL.
231 * \param Q_len The Byte length of \p Q, ignored if \p Q == NULL.
232 * \param D The private exponent, or NULL.
233 * \param D_len The Byte length of \p D, ignored if \p D == NULL.
234 * \param E The public exponent, or NULL.
235 * \param E_len The Byte length of \p E, ignored if \p E == NULL.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100236 *
237 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000238 * imports, if the parameters are not simultaneously present.
239 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100240 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000241 * by a call to mbedtls_rsa_complete(), which checks and
242 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100243 * public or private RSA key.
244 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000245 * \note See mbedtls_rsa_complete() for more information on which
246 * parameters are necessary to set up a private or public
247 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100248 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100249 * \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 * \return \c 0 on success, or a non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100253 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100254int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100255 unsigned char const *N, size_t N_len,
256 unsigned char const *P, size_t P_len,
257 unsigned char const *Q, size_t Q_len,
258 unsigned char const *D, size_t D_len,
259 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100260
261/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000262 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100263 * a set of imported core parameters.
264 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000265 * To setup an RSA public key, precisely \p N and \p E
266 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100267 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000268 * To setup an RSA private key, sufficient information must
269 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100270 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000271 * The default implementation supports the following:
272 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
273 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
274 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100275 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000276 * If this function runs successfully, it guarantees that
277 * the RSA context can be used for RSA operations without
278 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100279 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000280 * \param ctx The initialized RSA context holding imported parameters.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100281 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000282 * \return \c 0 on success, or #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the
283 * attempted derivations failed.
Hanno Becker43a08d02017-10-02 13:16:35 +0100284 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100285 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000286 * for the imported parameters. In particular, parameters that
287 * are not needed by the implementation might be silently
288 * discarded and left unchecked. To check the consistency
289 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100290 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100291 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100292int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100293
294/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000295 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100296 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000297 * If this function runs successfully, the non-NULL buffers
298 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
299 * written, with additional unused space filled leading by
300 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100301 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000302 * Possible reasons for returning
303 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:<ul>
304 * <li>An alternative RSA implementation is in use, which
305 * stores the key externally, and either cannot or should
306 * not export it into RAM.</li>
307 * <li>A SW or HW implementation might not support a certain
308 * deduction. For example, \p P, \p Q from \p N, \p D,
309 * and \p E if the former are not part of the
310 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100311 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000312 * If the function fails due to an unsupported operation,
313 * the RSA context stays intact and remains usable.
314 *
315 * \param ctx The initialized RSA context.
316 * \param N The MPI to hold the RSA modulus, or NULL.
317 * \param P The MPI to hold the first prime factor of \p N, or NULL.
318 * \param Q The MPI to hold the second prime factor of \p N, or NULL.
319 * \param D The MPI to hold the private exponent, or NULL.
320 * \param E The MPI to hold the public exponent, or NULL.
321 *
322 * \return \c 0 on success,
323 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION if exporting the
324 * requested parameters cannot be done due to missing
325 * functionality or because of security policies,
326 * or a non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100327 *
328 */
329int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
330 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
331 mbedtls_mpi *D, mbedtls_mpi *E );
332
333/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000334 * \brief This function exports core parameters of an RSA key
335 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100336 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000337 * If this function runs successfully, the non-NULL buffers
338 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
339 * written, with additional unused space filled leading by
340 * zero Bytes.
341 *
342 * Possible reasons for returning
343 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:<ul>
344 * <li>An alternative RSA implementation is in use, which
345 * stores the key externally, and either cannot or should
346 * not export it into RAM.</li>
347 * <li>A SW or HW implementation might not support a certain
348 * deduction. For example, \p P, \p Q from \p N, \p D,
349 * and \p E if the former are not part of the
350 * implementation.</li></ul>
351 * If the function fails due to an unsupported operation,
352 * the RSA context stays intact and remains usable.
353 *
354 * \param ctx The initialized RSA context.
355 * \param N The Byte array to store the RSA modulus, or NULL.
356 * \param N_len The size of the buffer for the modulus.
357 * \param P The Byte array to hold the first prime factor of \p N, or
358 * NULL.
359 * \param P_len The size of the buffer for the first prime factor.
360 * \param Q The Byte array to hold the second prime factor of \p N, or
361 NULL.
362 * \param Q_len The size of the buffer for the second prime factor.
363 * \param D The Byte array to hold the private exponent, or NULL.
364 * \param D_len The size of the buffer for the private exponent.
365 * \param E The Byte array to hold the public exponent, or NULL.
366 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100367 *
368 * \note The length fields are ignored if the corresponding
369 * buffer pointers are NULL.
370 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000371 * \return \c 0 on success,
372 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION if exporting the
373 * requested parameters cannot be done due to missing
374 * functionality or because of security policies,
375 * or a non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100376 */
377int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
378 unsigned char *N, size_t N_len,
379 unsigned char *P, size_t P_len,
380 unsigned char *Q, size_t Q_len,
381 unsigned char *D, size_t D_len,
382 unsigned char *E, size_t E_len );
383
384/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000385 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100386 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000387 * \param ctx The initialized RSA context.
388 * \param DP The MPI to hold D modulo P-1, or NULL.
389 * \param DQ The MPI to hold D modulo Q-1, or NULL.
390 * \param QP The MPI to hold modular inverse of Q modulo P, or NULL.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100391 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000392 * \return \c 0 on success, non-zero error code otherwise.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100393 *
394 * \note Alternative RSA implementations not using CRT-parameters
Rose Zadik042e97f2018-01-26 16:35:10 +0000395 * internally can implement this function based on
396 * mbedtls_rsa_deduce_opt().
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100397 *
398 */
399int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
400 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
401
Paul Bakker5121ce52009-01-03 21:22:43 +0000402/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000403 * \brief This function sets padding for an already initialized RSA
404 * context. See mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100405 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000406 * \param ctx The RSA context to be set.
407 * \param padding Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or
408 * #MBEDTLS_RSA_PKCS_V21.
409 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100410 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100411void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
412 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100413
414/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000415 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100416 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000417 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100418 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000419 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100420 *
421 */
422size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
423
424/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000425 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000426 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000427 * \param ctx The RSA context used to hold the key.
428 * \param f_rng The RNG function.
429 * \param p_rng The RNG parameter.
430 * \param nbits The size of the public key in bits.
431 * \param exponent The public exponent. For example, 65537.
Paul Bakker5121ce52009-01-03 21:22:43 +0000432 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000433 * \note mbedtls_rsa_init() must be called before this function,
434 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000436 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
437 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100440 int (*f_rng)(void *, unsigned char *, size_t),
441 void *p_rng,
442 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000443
444/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000445 * \brief This function checks if a context contains at least an RSA
446 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000447 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000448 * If the function runs successfully, it is guaranteed that
449 * enough information is present to perform an RSA public key
450 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000452 * \param ctx The RSA context to check.
453 *
454 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
455 * on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100456 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000459
460/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000461 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100462 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000464 * \param ctx The RSA context to check.
Paul Bakker5121ce52009-01-03 21:22:43 +0000465 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000466 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code on
467 * failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100468 *
Hanno Becker68767a62017-10-17 10:13:31 +0100469 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000470 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100471 * on the given context, but that the various parameters are
472 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000473 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100474 *
475 * \warning This function should catch accidental misconfigurations
476 * like swapping of parameters, but it cannot establish full
477 * trust in neither the quality nor the consistency of the key
478 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000479 * <ul><li>Consistency: Imported parameters that are irrelevant
480 * for the implementation might be silently dropped. If dropped,
481 * the current function does not have access to them,
482 * and therefore cannot check them. See mbedtls_rsa_complete().
483 * If you want to check the consistency of the entire
484 * content of an PKCS1-encoded RSA private key, for example, you
485 * should use mbedtls_rsa_validate_params() before setting
486 * up the RSA context.
487 * Additionally, if the implementation performs empirical checks,
488 * these checks substantiate but do not guarantee consistency.</li>
489 * <li>Quality: This function is not expected to perform
490 * extended quality assessments like checking that the prime
491 * factors are safe. Additionally, it is the responsibility of the
492 * user to ensure the trustworthiness of the source of his RSA
493 * parameters, which goes beyond what is effectively checkable
494 * by the library.</li></ul>
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000497
498/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000499 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100500 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000501 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100502 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000503 * \param pub The RSA context holding the public key.
504 * \param prv The RSA context holding the private key.
505 *
506 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
507 * on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100508 */
Hanno Becker98838b02017-10-02 13:16:10 +0100509int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
510 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100511
512/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000513 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000515 * \param ctx The RSA context.
516 * \param input The input buffer.
517 * \param output The output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000519 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
520 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000522 * \note This function does not handle message padding.
523 *
524 * \note Make sure to set \p input[0] = 0 or ensure that
525 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 *
527 * \note The input and output buffers must be large
Rose Zadik042e97f2018-01-26 16:35:10 +0000528 * enough. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000531 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 unsigned char *output );
533
534/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000535 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000537 * \param ctx The RSA context.
538 * \param f_rng The RNG function. Needed for blinding.
539 * \param p_rng The RNG parameter.
540 * \param input The input buffer.
541 * \param output The output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000542 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000543 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
544 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 *
546 * \note The input and output buffers must be large
Rose Zadik042e97f2018-01-26 16:35:10 +0000547 * enough. For example, 128 Bytes if RSA-1024 is used.
Hanno Becker26f1f602018-03-09 10:47:30 +0000548 *
549 * \note Blinding is used if and only if a PRNG is provided.
550 *
551 * \note If blinding is used, both the base of exponentation
552 * and the exponent are blinded, providing protection
553 * against some side-channel attacks.
554 *
555 * \warning It is deprecated and a security risk to not provide
556 * a PRNG here and thereby prevent the use of blinding.
557 * Future versions of the library may enforce the presence
558 * of a PRNG.
559 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200562 int (*f_rng)(void *, unsigned char *, size_t),
563 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000564 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 unsigned char *output );
566
567/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000568 * \brief This function adds the message padding, then performs an RSA
569 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000570 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000571 * It is the generic wrapper for performing a PKCS#1 encryption
572 * operation using the \p mode from the context.
573 *
574 *
575 * \param ctx The RSA context.
576 * \param f_rng The RNG function. Needed for padding, PKCS#1 v2.1
577 * encoding, and #MBEDTLS_RSA_PRIVATE.
578 * \param p_rng The RNG parameter.
579 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
580 * \param ilen The length of the plaintext.
581 * \param input The buffer holding the data to encrypt.
582 * \param output The buffer used to hold the ciphertext.
Paul Bakker5121ce52009-01-03 21:22:43 +0000583 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100584 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000585 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
586 * are likely to remove the \p mode argument and have it
587 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100588 *
589 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000590 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
591 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100592 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000593 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
594 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000596 * \note The input and output buffers must be as large as the size
597 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker5121ce52009-01-03 21:22:43 +0000598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000600 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000601 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000602 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000603 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 unsigned char *output );
605
606/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000607 * \brief This function performs a PKCS#1 v1.5 encryption operation
608 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100609 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000610 * \param ctx The RSA context.
611 * \param f_rng The RNG function. Needed for padding and
612 * #MBEDTLS_RSA_PRIVATE.
613 * \param p_rng The RNG parameter.
614 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
615 * \param ilen The length of the plaintext.
616 * \param input The buffer holding the data to encrypt.
617 * \param output The buffer used to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100618 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100619 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000620 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
621 * are likely to remove the \p mode argument and have it
622 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100623 *
624 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000625 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
626 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100627 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000628 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
629 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100630 *
631 * \note The output buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000632 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100633 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100635 int (*f_rng)(void *, unsigned char *, size_t),
636 void *p_rng,
637 int mode, size_t ilen,
638 const unsigned char *input,
639 unsigned char *output );
640
641/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000642 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
643 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100644 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000645 * \param ctx The RSA context.
646 * \param f_rng The RNG function. Needed for padding and PKCS#1 v2.1
647 * encoding and #MBEDTLS_RSA_PRIVATE.
648 * \param p_rng The RNG parameter.
649 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
650 * \param label The buffer holding the custom label to use.
651 * \param label_len The length of the label.
652 * \param ilen The length of the plaintext.
653 * \param input The buffer holding the data to encrypt.
654 * \param output The buffer used to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100655 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100656 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000657 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
658 * are likely to remove the \p mode argument and have it
659 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100660 *
661 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000662 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
663 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100664 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000665 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
666 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100667 *
668 * \note The output buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000669 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100670 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100672 int (*f_rng)(void *, unsigned char *, size_t),
673 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100674 int mode,
675 const unsigned char *label, size_t label_len,
676 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100677 const unsigned char *input,
678 unsigned char *output );
679
680/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000681 * \brief This function performs an RSA operation, then removes the
682 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000684 * It is the generic wrapper for performing a PKCS#1 decryption
685 * operation using the \p mode from the context.
686 *
687 * \param ctx The RSA context.
688 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
689 * \param p_rng The RNG parameter.
690 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
691 * \param olen The length of the plaintext.
692 * \param input The buffer holding the encrypted data.
693 * \param output The buffer used to hold the plaintext.
694 * \param output_max_len The maximum length of the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000695 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100696 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000697 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
698 * are likely to remove the \p mode argument and have it
699 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100700 *
701 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000702 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
703 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100704 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000705 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
706 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000707 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100708 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000709 * as large as the size \p ctx->len of \p ctx->N (for example,
710 * 128 Bytes if RSA-1024 is used) to be able to hold an
711 * arbitrary decrypted message. If it is not large enough to
712 * hold the decryption of the particular ciphertext provided,
713 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100714 *
715 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000716 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker5121ce52009-01-03 21:22:43 +0000717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200719 int (*f_rng)(void *, unsigned char *, size_t),
720 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000721 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000722 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000723 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000724 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000725
726/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000727 * \brief This function performs a PKCS#1 v1.5 decryption
728 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100729 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000730 * \param ctx The RSA context.
731 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
732 * \param p_rng The RNG parameter.
733 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
734 * \param olen The length of the plaintext.
735 * \param input The buffer holding the encrypted data.
736 * \param output The buffer to hold the plaintext.
737 * \param output_max_len The maximum length of the output buffer.
Paul Bakkerb3869132013-02-28 17:21:01 +0100738 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100739 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000740 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
741 * are likely to remove the \p mode argument and have it
742 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100743 *
744 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000745 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
746 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100747 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000748 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
749 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100750 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100751 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000752 * as large as the size \p ctx->len of \p ctx->N, for example,
753 * 128 Bytes if RSA-1024 is used, to be able to hold an
754 * arbitrary decrypted message. If it is not large enough to
755 * hold the decryption of the particular ciphertext provided,
756 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100757 *
758 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000759 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200762 int (*f_rng)(void *, unsigned char *, size_t),
763 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100764 int mode, size_t *olen,
765 const unsigned char *input,
766 unsigned char *output,
767 size_t output_max_len );
768
769/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000770 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
771 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100772 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000773 * \param ctx The RSA context.
774 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
775 * \param p_rng The RNG parameter.
776 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
777 * \param label The buffer holding the custom label to use.
778 * \param label_len The length of the label.
779 * \param olen The length of the plaintext.
780 * \param input The buffer holding the encrypted data.
781 * \param output The buffer to hold the plaintext.
782 * \param output_max_len The maximum length of the output buffer.
Paul Bakkerb3869132013-02-28 17:21:01 +0100783 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100784 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000785 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
786 * are likely to remove the \p mode argument and have it
787 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100788 *
789 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000790 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
791 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100792 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000793 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
794 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100795 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100796 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000797 * as large as the size \p ctx->len of \p ctx->N, for
798 * example, 128 Bytes if RSA-1024 is used, to be able to
799 * hold an arbitrary decrypted message. If it is not
800 * large enough to hold the decryption of the particular
801 * ciphertext provided, the function returns
802 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100803 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100804 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000805 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100806 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200808 int (*f_rng)(void *, unsigned char *, size_t),
809 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100810 int mode,
811 const unsigned char *label, size_t label_len,
812 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100813 const unsigned char *input,
814 unsigned char *output,
815 size_t output_max_len );
816
817/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000818 * \brief This function performs a private RSA operation to sign
819 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000820 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000821 * It is the generic wrapper for performing a PKCS#1
822 * signature using the \p mode from the context.
823 *
824 * \param ctx The RSA context.
825 * \param f_rng The RNG function. Needed for PKCS#1 v2.1 encoding and for
826 * #MBEDTLS_RSA_PRIVATE.
827 * \param p_rng The RNG parameter.
828 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
829 * \param md_alg The message-digest algorithm used to hash the original data.
830 * Use #MBEDTLS_MD_NONE for signing raw data.
831 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
832 * \param hash The buffer holding the message digest.
833 * \param sig The buffer to hold the ciphertext.
Paul Bakker5121ce52009-01-03 21:22:43 +0000834 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100835 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000836 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
837 * are likely to remove the \p mode argument and have it
838 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100839 *
840 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000841 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
842 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100843 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000844 * \return \c 0 if the signing operation was successful,
845 * or an \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000846 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000847 * \note The \p sig buffer must be as large as the size
848 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
k-stachowiakeee98e92019-05-31 20:16:50 +0200849 * A buffer length of #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000850 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000851 * \note For PKCS#1 v2.1 encoding, see comments on
852 * mbedtls_rsa_rsassa_pss_sign() for details on
853 * \p md_alg and \p hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000854 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000856 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000857 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000858 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000860 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000861 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000862 unsigned char *sig );
863
864/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000865 * \brief This function performs a PKCS#1 v1.5 signature
866 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100867 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000868 * \param ctx The RSA context.
869 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
870 * \param p_rng The RNG parameter.
871 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
872 * \param md_alg The message-digest algorithm used to hash the original data.
873 * Use #MBEDTLS_MD_NONE for signing raw data.
874 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
875 * \param hash The buffer holding the message digest.
876 * \param sig The buffer to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100877 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100878 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000879 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
880 * are likely to remove the \p mode argument and have it
881 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100882 *
883 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000884 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
885 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100886 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000887 * \return \c 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100888 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +0000889 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100890 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000891 * \note The \p sig buffer must be as large as the size
892 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
k-stachowiakeee98e92019-05-31 20:16:50 +0200893 * A buffer length of #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100894 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200896 int (*f_rng)(void *, unsigned char *, size_t),
897 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100898 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100900 unsigned int hashlen,
901 const unsigned char *hash,
902 unsigned char *sig );
903
904/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000905 * \brief This function performs a PKCS#1 v2.1 PSS signature
906 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100907 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000908 * \param ctx The RSA context.
909 * \param f_rng The RNG function. Needed for PKCS#1 v2.1 encoding and for
910 * #MBEDTLS_RSA_PRIVATE.
911 * \param p_rng The RNG parameter.
912 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
913 * \param md_alg The message-digest algorithm used to hash the original data.
914 * Use #MBEDTLS_MD_NONE for signing raw data.
915 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
916 * \param hash The buffer holding the message digest.
917 * \param sig The buffer to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100918 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100919 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000920 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
921 * are likely to remove the \p mode argument and have it
922 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100923 *
924 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000925 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
926 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100927 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000928 * \return \c 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100929 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +0000930 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100931 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000932 * \note The \p sig buffer must be as large as the size
933 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
k-stachowiakeee98e92019-05-31 20:16:50 +0200934 * A buffer length of #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100935 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000936 * \note The \p hash_id in the RSA context is the one used for the
937 * encoding. \p md_alg in the function call is the type of hash
938 * that is encoded. According to <em>RFC-3447: Public-Key
939 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
940 * Specifications</em> it is advised to keep both hashes the
941 * same.
Paul Bakkerb3869132013-02-28 17:21:01 +0100942 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100944 int (*f_rng)(void *, unsigned char *, size_t),
945 void *p_rng,
946 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100948 unsigned int hashlen,
949 const unsigned char *hash,
950 unsigned char *sig );
951
952/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000953 * \brief This function performs a public RSA operation and checks
954 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000956 * This is the generic wrapper for performing a PKCS#1
957 * verification using the mode from the context.
958 *
959 * \param ctx The RSA public key context.
960 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
961 * \param p_rng The RNG parameter.
962 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
963 * \param md_alg The message-digest algorithm used to hash the original data.
964 * Use #MBEDTLS_MD_NONE for signing raw data.
965 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
966 * \param hash The buffer holding the message digest.
967 * \param sig The buffer holding the ciphertext.
Paul Bakker5121ce52009-01-03 21:22:43 +0000968 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100969 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000970 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
971 * are likely to remove the \p mode argument and have it
972 * set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100973 *
974 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000975 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
976 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100977 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000978 * \return \c 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100979 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +0000980 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000981 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000982 * \note The \p sig buffer must be as large as the size
983 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000984 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000985 * \note For PKCS#1 v2.1 encoding, see comments on
986 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
987 * \p hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200990 int (*f_rng)(void *, unsigned char *, size_t),
991 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000994 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000995 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200996 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
998/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000999 * \brief This function performs a PKCS#1 v1.5 verification
1000 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001001 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001002 * \param ctx The RSA public key context.
1003 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
1004 * \param p_rng The RNG parameter.
1005 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1006 * \param md_alg The message-digest algorithm used to hash the original data.
1007 * Use #MBEDTLS_MD_NONE for signing raw data.
1008 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
1009 * \param hash The buffer holding the message digest.
1010 * \param sig The buffer holding the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +01001011 *
Hanno Becker3cdc7112017-10-05 10:09:31 +01001012 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +00001013 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1014 * are likely to remove the \p mode argument and have it
1015 * set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +01001016 *
1017 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +00001018 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
1019 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +01001020 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001021 * \return \c 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001022 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +00001023 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001024 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001025 * \note The \p sig buffer must be as large as the size
1026 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001029 int (*f_rng)(void *, unsigned char *, size_t),
1030 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001031 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001033 unsigned int hashlen,
1034 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001035 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001036
1037/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001038 * \brief This function performs a PKCS#1 v2.1 PSS verification
1039 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001040 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001041 * The hash function for the MGF mask generating function
1042 * is that specified in the RSA context.
1043 *
1044 * \param ctx The RSA public key context.
1045 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
1046 * \param p_rng The RNG parameter.
1047 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1048 * \param md_alg The message-digest algorithm used to hash the original data.
1049 * Use #MBEDTLS_MD_NONE for signing raw data.
1050 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
1051 * \param hash The buffer holding the message digest.
1052 * \param sig The buffer holding the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +01001053 *
Hanno Becker3cdc7112017-10-05 10:09:31 +01001054 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +00001055 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1056 * are likely to remove the \p mode argument and have it
1057 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +01001058 *
1059 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +00001060 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
1061 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +01001062 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001063 * \return \c 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001064 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +00001065 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001066 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001067 * \note The \p sig buffer must be as large as the size
1068 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001069 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001070 * \note The \p hash_id in the RSA context is the one used for the
1071 * verification. \p md_alg in the function call is the type of
1072 * hash that is verified. According to <em>RFC-3447: Public-Key
1073 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1074 * Specifications</em> it is advised to keep both hashes the
1075 * same. If \p hash_id in the RSA context is unset,
1076 * the \p md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001079 int (*f_rng)(void *, unsigned char *, size_t),
1080 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001081 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001083 unsigned int hashlen,
1084 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001085 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001086
1087/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001088 * \brief This function performs a PKCS#1 v2.1 PSS verification
1089 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001090 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001091 * The hash function for the MGF mask generating function
1092 * is that specified in \p mgf1_hash_id.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001093 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001094 * \param ctx The RSA public key context.
1095 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
1096 * \param p_rng The RNG parameter.
1097 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1098 * \param md_alg The message-digest algorithm used to hash the original data.
1099 * Use #MBEDTLS_MD_NONE for signing raw data.
1100 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
1101 * \param hash The buffer holding the message digest.
1102 * \param mgf1_hash_id The message digest used for mask generation.
1103 * \param expected_salt_len The length of the salt used in padding. Use
1104 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1105 * \param sig The buffer holding the ciphertext.
1106 *
1107 * \return \c 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001108 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +00001109 * on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001110 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001111 * \note The \p sig buffer must be as large as the size
1112 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001113 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001114 * \note The \p hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001117 int (*f_rng)(void *, unsigned char *, size_t),
1118 void *p_rng,
1119 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001121 unsigned int hashlen,
1122 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001124 int expected_salt_len,
1125 const unsigned char *sig );
1126
1127/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001128 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001129 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001130 * \param dst The destination context.
1131 * \param src The source context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001132 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001133 * \return \c 0 on success,
1134 * #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001137
1138/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001139 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001140 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001141 * \param ctx The RSA Context to free.
Paul Bakker5121ce52009-01-03 21:22:43 +00001142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001144
Hanno Beckerd22b78b2017-10-12 11:42:17 +01001145#ifdef __cplusplus
1146}
1147#endif
1148
1149#else /* MBEDTLS_RSA_ALT */
1150#include "rsa_alt.h"
1151#endif /* MBEDTLS_RSA_ALT */
1152
1153#ifdef __cplusplus
1154extern "C" {
1155#endif
1156
Paul Bakker5121ce52009-01-03 21:22:43 +00001157/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001158 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001159 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001160 * \return \c 0 on success, or \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001163
1164#ifdef __cplusplus
1165}
1166#endif
1167
Paul Bakker5121ce52009-01-03 21:22:43 +00001168#endif /* rsa.h */