blob: cfb66dd8e61968f757cf072781cb0f4c94b3d363 [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{
Gilles Peskinee8505e32021-02-09 18:59:42 +0100121 int ver; /*!< Reserved for internal purposes.
122 * Do not set this field in application
123 * code. Its meaning might change without
124 * notice. */
Rose Zadik042e97f2018-01-26 16:35:10 +0000125 size_t len; /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
Rose Zadik042e97f2018-01-26 16:35:10 +0000127 mbedtls_mpi N; /*!< The public modulus. */
128 mbedtls_mpi E; /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Rose Zadik042e97f2018-01-26 16:35:10 +0000130 mbedtls_mpi D; /*!< The private exponent. */
131 mbedtls_mpi P; /*!< The first prime factor. */
132 mbedtls_mpi Q; /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100133
Rose Zadik042e97f2018-01-26 16:35:10 +0000134 mbedtls_mpi DP; /*!< \p D % (P - 1) */
135 mbedtls_mpi DQ; /*!< \p D % (Q - 1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
Rose Zadik042e97f2018-01-26 16:35:10 +0000138 mbedtls_mpi RN; /*!< cached R^2 mod \p N */
Hanno Becker1a59e792017-08-23 07:41:10 +0100139
Rose Zadik042e97f2018-01-26 16:35:10 +0000140 mbedtls_mpi RP; /*!< cached R^2 mod \p P */
141 mbedtls_mpi RQ; /*!< cached R^2 mod \p Q */
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
Rose Zadik042e97f2018-01-26 16:35:10 +0000143 mbedtls_mpi Vi; /*!< The cached blinding value. */
144 mbedtls_mpi Vf; /*!< The cached un-blinding value. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200145
Rose Zadik042e97f2018-01-26 16:35:10 +0000146 int padding; /*!< Selects padding mode:
147 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
148 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
149 int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
150 as specified in md.h for use in the MGF
151 mask generating function used in the
152 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#if defined(MBEDTLS_THREADING_C)
Gilles Peskinee8505e32021-02-09 18:59:42 +0100154 /* Invariant: the mutex is initialized iff ver != 0. */
Rose Zadik042e97f2018-01-26 16:35:10 +0000155 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200156#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000157}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
Paul Bakker5121ce52009-01-03 21:22:43 +0000160/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000161 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000163 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000164 * encryption scheme and the RSASSA-PSS signature scheme.
165 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000166 * \param ctx The RSA context to initialize.
167 * \param padding Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or
168 * #MBEDTLS_RSA_PKCS_V21.
169 * \param hash_id The hash identifier of #mbedtls_md_type_t type, if
170 * \p padding is #MBEDTLS_RSA_PKCS_V21.
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000172 * \note The \p hash_id parameter is ignored when using
173 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200174 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000175 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200176 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000177 * mixing padding modes. For public key operations it is
Antonin Décimo8fd91562019-01-23 15:24:37 +0100178 * a default value, which can be overridden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000179 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200180 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000181 * \note The hash selected in \p hash_id is always used for OEAP
182 * encryption. For PSS signatures, it is always used for
Antonin Décimo8fd91562019-01-23 15:24:37 +0100183 * making signatures, but can be overridden for verifying them.
184 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100187 int padding,
188 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100190/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000191 * \brief This function imports a set of core parameters into an
192 * RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100193 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000194 * \param ctx The initialized RSA context to store the parameters in.
195 * \param N The RSA modulus, or NULL.
196 * \param P The first prime factor of \p N, or NULL.
197 * \param Q The second prime factor of \p N, or NULL.
198 * \param D The private exponent, or NULL.
199 * \param E The public exponent, or NULL.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100200 *
201 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000202 * imports, if the parameters are not simultaneously present.
203 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100204 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000205 * by a call to mbedtls_rsa_complete(), which checks and
206 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100207 * public or private RSA key.
208 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000209 * \note See mbedtls_rsa_complete() for more information on which
210 * parameters are necessary to set up a private or public
211 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100212 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100213 * \note The imported parameters are copied and need not be preserved
214 * for the lifetime of the RSA context being set up.
215 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000216 * \return \c 0 on success, or a non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100217 */
218int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
219 const mbedtls_mpi *N,
220 const mbedtls_mpi *P, const mbedtls_mpi *Q,
221 const mbedtls_mpi *D, const mbedtls_mpi *E );
222
223/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000224 * \brief This function imports core RSA parameters, in raw big-endian
225 * binary format, into an RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100226 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000227 * \param ctx The initialized RSA context to store the parameters in.
228 * \param N The RSA modulus, or NULL.
229 * \param N_len The Byte length of \p N, ignored if \p N == NULL.
230 * \param P The first prime factor of \p N, or NULL.
231 * \param P_len The Byte length of \p P, ignored if \p P == NULL.
232 * \param Q The second prime factor of \p N, or NULL.
233 * \param Q_len The Byte length of \p Q, ignored if \p Q == NULL.
234 * \param D The private exponent, or NULL.
235 * \param D_len The Byte length of \p D, ignored if \p D == NULL.
236 * \param E The public exponent, or NULL.
237 * \param E_len The Byte length of \p E, ignored if \p E == NULL.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100238 *
239 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000240 * imports, if the parameters are not simultaneously present.
241 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100242 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000243 * by a call to mbedtls_rsa_complete(), which checks and
244 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100245 * public or private RSA key.
246 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000247 * \note See mbedtls_rsa_complete() for more information on which
248 * parameters are necessary to set up a private or public
249 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100250 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100251 * \note The imported parameters are copied and need not be preserved
252 * for the lifetime of the RSA context being set up.
253 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000254 * \return \c 0 on success, or a non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100255 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100256int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100257 unsigned char const *N, size_t N_len,
258 unsigned char const *P, size_t P_len,
259 unsigned char const *Q, size_t Q_len,
260 unsigned char const *D, size_t D_len,
261 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100262
263/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000264 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100265 * a set of imported core parameters.
266 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000267 * To setup an RSA public key, precisely \p N and \p E
268 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100269 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000270 * To setup an RSA private key, sufficient information must
271 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100272 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000273 * The default implementation supports the following:
274 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
275 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
276 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100277 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000278 * If this function runs successfully, it guarantees that
279 * the RSA context can be used for RSA operations without
280 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100281 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000282 * \param ctx The initialized RSA context holding imported parameters.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100283 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000284 * \return \c 0 on success, or #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the
285 * attempted derivations failed.
Hanno Becker43a08d02017-10-02 13:16:35 +0100286 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100287 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000288 * for the imported parameters. In particular, parameters that
289 * are not needed by the implementation might be silently
290 * discarded and left unchecked. To check the consistency
291 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100292 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100293 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100294int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100295
296/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000297 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100298 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000299 * If this function runs successfully, the non-NULL buffers
300 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
301 * written, with additional unused space filled leading by
302 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100303 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000304 * Possible reasons for returning
305 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:<ul>
306 * <li>An alternative RSA implementation is in use, which
307 * stores the key externally, and either cannot or should
308 * not export it into RAM.</li>
309 * <li>A SW or HW implementation might not support a certain
310 * deduction. For example, \p P, \p Q from \p N, \p D,
311 * and \p E if the former are not part of the
312 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100313 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000314 * If the function fails due to an unsupported operation,
315 * the RSA context stays intact and remains usable.
316 *
317 * \param ctx The initialized RSA context.
318 * \param N The MPI to hold the RSA modulus, or NULL.
319 * \param P The MPI to hold the first prime factor of \p N, or NULL.
320 * \param Q The MPI to hold the second prime factor of \p N, or NULL.
321 * \param D The MPI to hold the private exponent, or NULL.
322 * \param E The MPI to hold the public exponent, or NULL.
323 *
324 * \return \c 0 on success,
325 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION if exporting the
326 * requested parameters cannot be done due to missing
327 * functionality or because of security policies,
328 * or a non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100329 *
330 */
331int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
332 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
333 mbedtls_mpi *D, mbedtls_mpi *E );
334
335/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000336 * \brief This function exports core parameters of an RSA key
337 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100338 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000339 * If this function runs successfully, the non-NULL buffers
340 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
341 * written, with additional unused space filled leading by
342 * zero Bytes.
343 *
344 * Possible reasons for returning
345 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:<ul>
346 * <li>An alternative RSA implementation is in use, which
347 * stores the key externally, and either cannot or should
348 * not export it into RAM.</li>
349 * <li>A SW or HW implementation might not support a certain
350 * deduction. For example, \p P, \p Q from \p N, \p D,
351 * and \p E if the former are not part of the
352 * implementation.</li></ul>
353 * If the function fails due to an unsupported operation,
354 * the RSA context stays intact and remains usable.
355 *
356 * \param ctx The initialized RSA context.
357 * \param N The Byte array to store the RSA modulus, or NULL.
358 * \param N_len The size of the buffer for the modulus.
359 * \param P The Byte array to hold the first prime factor of \p N, or
360 * NULL.
361 * \param P_len The size of the buffer for the first prime factor.
362 * \param Q The Byte array to hold the second prime factor of \p N, or
363 NULL.
364 * \param Q_len The size of the buffer for the second prime factor.
365 * \param D The Byte array to hold the private exponent, or NULL.
366 * \param D_len The size of the buffer for the private exponent.
367 * \param E The Byte array to hold the public exponent, or NULL.
368 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100369 *
370 * \note The length fields are ignored if the corresponding
371 * buffer pointers are NULL.
372 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000373 * \return \c 0 on success,
374 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION if exporting the
375 * requested parameters cannot be done due to missing
376 * functionality or because of security policies,
377 * or a non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100378 */
379int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
380 unsigned char *N, size_t N_len,
381 unsigned char *P, size_t P_len,
382 unsigned char *Q, size_t Q_len,
383 unsigned char *D, size_t D_len,
384 unsigned char *E, size_t E_len );
385
386/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000387 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100388 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000389 * \param ctx The initialized RSA context.
390 * \param DP The MPI to hold D modulo P-1, or NULL.
391 * \param DQ The MPI to hold D modulo Q-1, or NULL.
392 * \param QP The MPI to hold modular inverse of Q modulo P, or NULL.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100393 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000394 * \return \c 0 on success, non-zero error code otherwise.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100395 *
396 * \note Alternative RSA implementations not using CRT-parameters
Rose Zadik042e97f2018-01-26 16:35:10 +0000397 * internally can implement this function based on
398 * mbedtls_rsa_deduce_opt().
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100399 *
400 */
401int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
402 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
403
Paul Bakker5121ce52009-01-03 21:22:43 +0000404/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000405 * \brief This function sets padding for an already initialized RSA
406 * context. See mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100407 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000408 * \param ctx The RSA context to be set.
409 * \param padding Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or
410 * #MBEDTLS_RSA_PKCS_V21.
411 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100412 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100413void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
414 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100415
416/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000417 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100418 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000419 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100420 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000421 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100422 *
423 */
424size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
425
426/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000427 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000429 * \param ctx The RSA context used to hold the key.
430 * \param f_rng The RNG function.
431 * \param p_rng The RNG parameter.
432 * \param nbits The size of the public key in bits.
433 * \param exponent The public exponent. For example, 65537.
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000435 * \note mbedtls_rsa_init() must be called before this function,
436 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000437 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000438 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
439 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100442 int (*f_rng)(void *, unsigned char *, size_t),
443 void *p_rng,
444 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000445
446/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000447 * \brief This function checks if a context contains at least an RSA
448 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000449 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000450 * If the function runs successfully, it is guaranteed that
451 * enough information is present to perform an RSA public key
452 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000454 * \param ctx The RSA context to check.
455 *
456 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
457 * on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100458 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000461
462/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000463 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100464 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000465 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000466 * \param ctx The RSA context to check.
Paul Bakker5121ce52009-01-03 21:22:43 +0000467 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000468 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code on
469 * failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100470 *
Hanno Becker68767a62017-10-17 10:13:31 +0100471 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000472 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100473 * on the given context, but that the various parameters are
474 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000475 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100476 *
477 * \warning This function should catch accidental misconfigurations
478 * like swapping of parameters, but it cannot establish full
479 * trust in neither the quality nor the consistency of the key
480 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000481 * <ul><li>Consistency: Imported parameters that are irrelevant
482 * for the implementation might be silently dropped. If dropped,
483 * the current function does not have access to them,
484 * and therefore cannot check them. See mbedtls_rsa_complete().
485 * If you want to check the consistency of the entire
486 * content of an PKCS1-encoded RSA private key, for example, you
487 * should use mbedtls_rsa_validate_params() before setting
488 * up the RSA context.
489 * Additionally, if the implementation performs empirical checks,
490 * these checks substantiate but do not guarantee consistency.</li>
491 * <li>Quality: This function is not expected to perform
492 * extended quality assessments like checking that the prime
493 * factors are safe. Additionally, it is the responsibility of the
494 * user to ensure the trustworthiness of the source of his RSA
495 * parameters, which goes beyond what is effectively checkable
496 * by the library.</li></ul>
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000501 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100502 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000503 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100504 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000505 * \param pub The RSA context holding the public key.
506 * \param prv The RSA context holding the private key.
507 *
508 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
509 * on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100510 */
Hanno Becker98838b02017-10-02 13:16:10 +0100511int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
512 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100513
514/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000515 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000517 * \param ctx The RSA context.
518 * \param input The input buffer.
519 * \param output The output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000521 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
522 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000524 * \note This function does not handle message padding.
525 *
526 * \note Make sure to set \p input[0] = 0 or ensure that
527 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 *
529 * \note The input and output buffers must be large
Rose Zadik042e97f2018-01-26 16:35:10 +0000530 * enough. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000533 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 unsigned char *output );
535
536/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000537 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000539 * \param ctx The RSA context.
540 * \param f_rng The RNG function. Needed for blinding.
541 * \param p_rng The RNG parameter.
542 * \param input The input buffer.
543 * \param output The output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000545 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
546 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 *
548 * \note The input and output buffers must be large
Rose Zadik042e97f2018-01-26 16:35:10 +0000549 * enough. For example, 128 Bytes if RSA-1024 is used.
Hanno Becker26f1f602018-03-09 10:47:30 +0000550 *
551 * \note Blinding is used if and only if a PRNG is provided.
552 *
553 * \note If blinding is used, both the base of exponentation
554 * and the exponent are blinded, providing protection
555 * against some side-channel attacks.
556 *
557 * \warning It is deprecated and a security risk to not provide
558 * a PRNG here and thereby prevent the use of blinding.
559 * Future versions of the library may enforce the presence
560 * of a PRNG.
561 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200564 int (*f_rng)(void *, unsigned char *, size_t),
565 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000566 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 unsigned char *output );
568
569/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000570 * \brief This function adds the message padding, then performs an RSA
571 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000573 * It is the generic wrapper for performing a PKCS#1 encryption
574 * operation using the \p mode from the context.
575 *
576 *
577 * \param ctx The RSA context.
578 * \param f_rng The RNG function. Needed for padding, PKCS#1 v2.1
579 * encoding, and #MBEDTLS_RSA_PRIVATE.
580 * \param p_rng The RNG parameter.
581 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
582 * \param ilen The length of the plaintext.
583 * \param input The buffer holding the data to encrypt.
584 * \param output The buffer used to hold the ciphertext.
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100586 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000587 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
588 * are likely to remove the \p mode argument and have it
589 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100590 *
591 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000592 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
593 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100594 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000595 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
596 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000597 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000598 * \note The input and output buffers must be as large as the size
599 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker5121ce52009-01-03 21:22:43 +0000600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000602 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000603 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000604 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000605 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000606 unsigned char *output );
607
608/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000609 * \brief This function performs a PKCS#1 v1.5 encryption operation
610 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100611 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000612 * \param ctx The RSA context.
613 * \param f_rng The RNG function. Needed for padding and
614 * #MBEDTLS_RSA_PRIVATE.
615 * \param p_rng The RNG parameter.
616 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
617 * \param ilen The length of the plaintext.
618 * \param input The buffer holding the data to encrypt.
619 * \param output The buffer used to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100620 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100621 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000622 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
623 * are likely to remove the \p mode argument and have it
624 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100625 *
626 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000627 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
628 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100629 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000630 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
631 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100632 *
633 * \note The output buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000634 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100637 int (*f_rng)(void *, unsigned char *, size_t),
638 void *p_rng,
639 int mode, size_t ilen,
640 const unsigned char *input,
641 unsigned char *output );
642
643/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000644 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
645 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100646 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000647 * \param ctx The RSA context.
648 * \param f_rng The RNG function. Needed for padding and PKCS#1 v2.1
649 * encoding and #MBEDTLS_RSA_PRIVATE.
650 * \param p_rng The RNG parameter.
651 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
652 * \param label The buffer holding the custom label to use.
653 * \param label_len The length of the label.
654 * \param ilen The length of the plaintext.
655 * \param input The buffer holding the data to encrypt.
656 * \param output The buffer used to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100657 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100658 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000659 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
660 * are likely to remove the \p mode argument and have it
661 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100662 *
663 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000664 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
665 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100666 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000667 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
668 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100669 *
670 * \note The output buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000671 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100674 int (*f_rng)(void *, unsigned char *, size_t),
675 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100676 int mode,
677 const unsigned char *label, size_t label_len,
678 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100679 const unsigned char *input,
680 unsigned char *output );
681
682/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000683 * \brief This function performs an RSA operation, then removes the
684 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000686 * It is the generic wrapper for performing a PKCS#1 decryption
687 * operation using the \p mode from the context.
688 *
689 * \param ctx The RSA context.
690 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
691 * \param p_rng The RNG parameter.
692 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
693 * \param olen The length of the plaintext.
694 * \param input The buffer holding the encrypted data.
695 * \param output The buffer used to hold the plaintext.
696 * \param output_max_len The maximum length of the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000697 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100698 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000699 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
700 * are likely to remove the \p mode argument and have it
701 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100702 *
703 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000704 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
705 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100706 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000707 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
708 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000709 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100710 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000711 * as large as the size \p ctx->len of \p ctx->N (for example,
712 * 128 Bytes if RSA-1024 is used) to be able to hold an
713 * arbitrary decrypted message. If it is not large enough to
714 * hold the decryption of the particular ciphertext provided,
715 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100716 *
717 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000718 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker5121ce52009-01-03 21:22:43 +0000719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200721 int (*f_rng)(void *, unsigned char *, size_t),
722 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000723 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000724 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000725 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000726 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000727
728/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000729 * \brief This function performs a PKCS#1 v1.5 decryption
730 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100731 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000732 * \param ctx The RSA context.
733 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
734 * \param p_rng The RNG parameter.
735 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
736 * \param olen The length of the plaintext.
737 * \param input The buffer holding the encrypted data.
738 * \param output The buffer to hold the plaintext.
739 * \param output_max_len The maximum length of the output buffer.
Paul Bakkerb3869132013-02-28 17:21:01 +0100740 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100741 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000742 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
743 * are likely to remove the \p mode argument and have it
744 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100745 *
746 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000747 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
748 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100749 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000750 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
751 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100752 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100753 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000754 * as large as the size \p ctx->len of \p ctx->N, for example,
755 * 128 Bytes if RSA-1024 is used, to be able to hold an
756 * arbitrary decrypted message. If it is not large enough to
757 * hold the decryption of the particular ciphertext provided,
758 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100759 *
760 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000761 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200764 int (*f_rng)(void *, unsigned char *, size_t),
765 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100766 int mode, size_t *olen,
767 const unsigned char *input,
768 unsigned char *output,
769 size_t output_max_len );
770
771/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000772 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
773 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100774 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000775 * \param ctx The RSA context.
776 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
777 * \param p_rng The RNG parameter.
778 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
779 * \param label The buffer holding the custom label to use.
780 * \param label_len The length of the label.
781 * \param olen The length of the plaintext.
782 * \param input The buffer holding the encrypted data.
783 * \param output The buffer to hold the plaintext.
784 * \param output_max_len The maximum length of the output buffer.
Paul Bakkerb3869132013-02-28 17:21:01 +0100785 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100786 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000787 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
788 * are likely to remove the \p mode argument and have it
789 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100790 *
791 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000792 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
793 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100794 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000795 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
796 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100797 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100798 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000799 * as large as the size \p ctx->len of \p ctx->N, for
800 * example, 128 Bytes if RSA-1024 is used, to be able to
801 * hold an arbitrary decrypted message. If it is not
802 * large enough to hold the decryption of the particular
803 * ciphertext provided, the function returns
804 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100805 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100806 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000807 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100808 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200810 int (*f_rng)(void *, unsigned char *, size_t),
811 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100812 int mode,
813 const unsigned char *label, size_t label_len,
814 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100815 const unsigned char *input,
816 unsigned char *output,
817 size_t output_max_len );
818
819/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000820 * \brief This function performs a private RSA operation to sign
821 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000822 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000823 * It is the generic wrapper for performing a PKCS#1
824 * signature using the \p mode from the context.
825 *
826 * \param ctx The RSA context.
827 * \param f_rng The RNG function. Needed for PKCS#1 v2.1 encoding and for
828 * #MBEDTLS_RSA_PRIVATE.
829 * \param p_rng The RNG parameter.
830 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
831 * \param md_alg The message-digest algorithm used to hash the original data.
832 * Use #MBEDTLS_MD_NONE for signing raw data.
833 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
834 * \param hash The buffer holding the message digest.
835 * \param sig The buffer to hold the ciphertext.
Paul Bakker5121ce52009-01-03 21:22:43 +0000836 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100837 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000838 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
839 * are likely to remove the \p mode argument and have it
840 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100841 *
842 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000843 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
844 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100845 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000846 * \return \c 0 if the signing operation was successful,
847 * or an \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000848 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000849 * \note The \p sig buffer must be as large as the size
850 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
k-stachowiakeee98e92019-05-31 20:16:50 +0200851 * A buffer length of #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000852 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000853 * \note For PKCS#1 v2.1 encoding, see comments on
854 * mbedtls_rsa_rsassa_pss_sign() for details on
855 * \p md_alg and \p hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000856 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000858 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000859 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000860 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000862 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000863 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000864 unsigned char *sig );
865
866/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000867 * \brief This function performs a PKCS#1 v1.5 signature
868 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100869 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000870 * \param ctx The RSA context.
871 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
872 * \param p_rng The RNG parameter.
873 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
874 * \param md_alg The message-digest algorithm used to hash the original data.
875 * Use #MBEDTLS_MD_NONE for signing raw data.
876 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
877 * \param hash The buffer holding the message digest.
878 * \param sig The buffer to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100879 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100880 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000881 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
882 * are likely to remove the \p mode argument and have it
883 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100884 *
885 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000886 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
887 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100888 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000889 * \return \c 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100890 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +0000891 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100892 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000893 * \note The \p sig buffer must be as large as the size
894 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
k-stachowiakeee98e92019-05-31 20:16:50 +0200895 * A buffer length of #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200898 int (*f_rng)(void *, unsigned char *, size_t),
899 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100900 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100902 unsigned int hashlen,
903 const unsigned char *hash,
904 unsigned char *sig );
905
906/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000907 * \brief This function performs a PKCS#1 v2.1 PSS signature
908 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100909 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000910 * \param ctx The RSA context.
911 * \param f_rng The RNG function. Needed for PKCS#1 v2.1 encoding and for
912 * #MBEDTLS_RSA_PRIVATE.
913 * \param p_rng The RNG parameter.
914 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
915 * \param md_alg The message-digest algorithm used to hash the original data.
916 * Use #MBEDTLS_MD_NONE for signing raw data.
917 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
918 * \param hash The buffer holding the message digest.
919 * \param sig The buffer to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100920 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100921 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000922 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
923 * are likely to remove the \p mode argument and have it
924 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100925 *
926 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000927 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
928 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100929 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000930 * \return \c 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100931 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +0000932 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100933 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000934 * \note The \p sig buffer must be as large as the size
935 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
k-stachowiakeee98e92019-05-31 20:16:50 +0200936 * A buffer length of #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100937 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000938 * \note The \p hash_id in the RSA context is the one used for the
939 * encoding. \p md_alg in the function call is the type of hash
940 * that is encoded. According to <em>RFC-3447: Public-Key
941 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
942 * Specifications</em> it is advised to keep both hashes the
943 * same.
Paul Bakkerb3869132013-02-28 17:21:01 +0100944 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100946 int (*f_rng)(void *, unsigned char *, size_t),
947 void *p_rng,
948 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100950 unsigned int hashlen,
951 const unsigned char *hash,
952 unsigned char *sig );
953
954/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000955 * \brief This function performs a public RSA operation and checks
956 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +0000957 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000958 * This is the generic wrapper for performing a PKCS#1
959 * verification using the mode from the context.
960 *
961 * \param ctx The RSA public key context.
962 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
963 * \param p_rng The RNG parameter.
964 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
965 * \param md_alg The message-digest algorithm used to hash the original data.
966 * Use #MBEDTLS_MD_NONE for signing raw data.
967 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
968 * \param hash The buffer holding the message digest.
969 * \param sig The buffer holding the ciphertext.
Paul Bakker5121ce52009-01-03 21:22:43 +0000970 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100971 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000972 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
973 * are likely to remove the \p mode argument and have it
974 * set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100975 *
976 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000977 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
978 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100979 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000980 * \return \c 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100981 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +0000982 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000983 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000984 * \note The \p sig buffer must be as large as the size
985 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000986 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000987 * \note For PKCS#1 v2.1 encoding, see comments on
988 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
989 * \p hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000990 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200992 int (*f_rng)(void *, unsigned char *, size_t),
993 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000996 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000997 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200998 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000999
1000/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001001 * \brief This function performs a PKCS#1 v1.5 verification
1002 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001003 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001004 * \param ctx The RSA public key context.
1005 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
1006 * \param p_rng The RNG parameter.
1007 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1008 * \param md_alg The message-digest algorithm used to hash the original data.
1009 * Use #MBEDTLS_MD_NONE for signing raw data.
1010 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
1011 * \param hash The buffer holding the message digest.
1012 * \param sig The buffer holding the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +01001013 *
Hanno Becker3cdc7112017-10-05 10:09:31 +01001014 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +00001015 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1016 * are likely to remove the \p mode argument and have it
1017 * set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +01001018 *
1019 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +00001020 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
1021 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +01001022 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001023 * \return \c 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001024 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +00001025 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001026 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001027 * \note The \p sig buffer must be as large as the size
1028 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001031 int (*f_rng)(void *, unsigned char *, size_t),
1032 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001033 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001035 unsigned int hashlen,
1036 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001037 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001038
1039/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001040 * \brief This function performs a PKCS#1 v2.1 PSS verification
1041 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001042 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001043 * The hash function for the MGF mask generating function
1044 * is that specified in the RSA context.
1045 *
1046 * \param ctx The RSA public key context.
1047 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
1048 * \param p_rng The RNG parameter.
1049 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1050 * \param md_alg The message-digest algorithm used to hash the original data.
1051 * Use #MBEDTLS_MD_NONE for signing raw data.
1052 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
1053 * \param hash The buffer holding the message digest.
1054 * \param sig The buffer holding the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +01001055 *
Hanno Becker3cdc7112017-10-05 10:09:31 +01001056 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +00001057 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1058 * are likely to remove the \p mode argument and have it
1059 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +01001060 *
1061 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +00001062 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
1063 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +01001064 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001065 * \return \c 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001066 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +00001067 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001068 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001069 * \note The \p sig buffer must be as large as the size
1070 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001071 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001072 * \note The \p hash_id in the RSA context is the one used for the
1073 * verification. \p md_alg in the function call is the type of
1074 * hash that is verified. According to <em>RFC-3447: Public-Key
1075 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1076 * Specifications</em> it is advised to keep both hashes the
1077 * same. If \p hash_id in the RSA context is unset,
1078 * the \p md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001081 int (*f_rng)(void *, unsigned char *, size_t),
1082 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001083 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001085 unsigned int hashlen,
1086 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001087 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001088
1089/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001090 * \brief This function performs a PKCS#1 v2.1 PSS verification
1091 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001092 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001093 * The hash function for the MGF mask generating function
1094 * is that specified in \p mgf1_hash_id.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001095 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001096 * \param ctx The RSA public key context.
1097 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
1098 * \param p_rng The RNG parameter.
1099 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1100 * \param md_alg The message-digest algorithm used to hash the original data.
1101 * Use #MBEDTLS_MD_NONE for signing raw data.
1102 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
1103 * \param hash The buffer holding the message digest.
1104 * \param mgf1_hash_id The message digest used for mask generation.
1105 * \param expected_salt_len The length of the salt used in padding. Use
1106 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1107 * \param sig The buffer holding the ciphertext.
1108 *
1109 * \return \c 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001110 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +00001111 * on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001112 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001113 * \note The \p sig buffer must be as large as the size
1114 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001115 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001116 * \note The \p hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001119 int (*f_rng)(void *, unsigned char *, size_t),
1120 void *p_rng,
1121 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001123 unsigned int hashlen,
1124 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001126 int expected_salt_len,
1127 const unsigned char *sig );
1128
1129/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001130 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001131 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001132 * \param dst The destination context.
1133 * \param src The source context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001134 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001135 * \return \c 0 on success,
1136 * #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001139
1140/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001141 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001142 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001143 * \param ctx The RSA Context to free.
Paul Bakker5121ce52009-01-03 21:22:43 +00001144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001146
Hanno Beckerd22b78b2017-10-12 11:42:17 +01001147#ifdef __cplusplus
1148}
1149#endif
1150
1151#else /* MBEDTLS_RSA_ALT */
1152#include "rsa_alt.h"
1153#endif /* MBEDTLS_RSA_ALT */
1154
1155#ifdef __cplusplus
1156extern "C" {
1157#endif
1158
Paul Bakker5121ce52009-01-03 21:22:43 +00001159/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001160 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001161 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001162 * \return \c 0 on success, or \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001165
1166#ifdef __cplusplus
1167}
1168#endif
1169
Paul Bakker5121ce52009-01-03 21:22:43 +00001170#endif /* rsa.h */