blob: 711329c5203555876a395a55655f59f884164192 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Rose Zadike8b5b992018-03-27 12:19:47 +01004 * \brief This file contains RSA definitions and functions.
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
Rose Zadike8b5b992018-03-27 12:19:47 +01006 * The RSA public-key cryptosystem is defined in <em>Public-Key
7 * Cryptography Standards (PKCS) #1 v1.5: RSA Encryption</em>
8 * and <em>Public-Key Cryptography Standards (PKCS) #1 v2.1:
9 * RSA Cryptography Specifications</em>.
Rose Zadik042e97f2018-01-26 16:35:10 +000010 *
Darryl Greena40a1012018-01-05 15:33:17 +000011 */
12/*
Rose Zadik042e97f2018-01-26 16:35:10 +000013 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020014 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000027 *
Rose Zadik042e97f2018-01-26 16:35:10 +000028 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#ifndef MBEDTLS_RSA_H
31#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020034#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020037#endif
Paul Bakkered27a042013-04-18 22:46:23 +020038
Paul Bakker314052f2011-08-15 09:07:52 +000039#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020040#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_THREADING_C)
Paul Bakkerc9965dc2013-09-29 14:58:17 +020043#include "threading.h"
44#endif
45
Paul Bakker13e2dfe2009-07-28 07:18:38 +000046/*
47 * RSA Error codes
48 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
50#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
51#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Rose Zadik042e97f2018-01-26 16:35:10 +000052#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 +020053#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
54#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
55#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
56#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
57#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Rose Zadik042e97f2018-01-26 16:35:10 +000058#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 +010059#define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580 /**< RSA hardware accelerator failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000060
61/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020062 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000063 */
Rose Zadik042e97f2018-01-26 16:35:10 +000064#define MBEDTLS_RSA_PUBLIC 0 /**< Request private key operation. */
65#define MBEDTLS_RSA_PRIVATE 1 /**< Request public key operation. */
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Rose Zadike8b5b992018-03-27 12:19:47 +010067#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
68#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Rose Zadik042e97f2018-01-26 16:35:10 +000070#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
71#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020074
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020075/*
76 * The above constants may be used even if the RSA module is compile out,
77 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
78 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010079
80#if !defined(MBEDTLS_RSA_ALT)
81// Regular implementation
82//
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020083
Paul Bakker407a0da2013-06-27 14:29:21 +020084#ifdef __cplusplus
85extern "C" {
86#endif
87
Paul Bakker5121ce52009-01-03 21:22:43 +000088/**
Rose Zadik042e97f2018-01-26 16:35:10 +000089 * \brief The RSA context structure.
Hanno Becker5063cd22017-09-29 11:49:12 +010090 *
91 * \note Direct manipulation of the members of this structure
Rose Zadik042e97f2018-01-26 16:35:10 +000092 * is deprecated. All manipulation should instead be done through
93 * the public interface functions.
Paul Bakker5121ce52009-01-03 21:22:43 +000094 */
95typedef struct
96{
Rose Zadik042e97f2018-01-26 16:35:10 +000097 int ver; /*!< Always 0.*/
98 size_t len; /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Rose Zadike8b5b992018-03-27 12:19:47 +0100100 mbedtls_mpi N; /*!< The public modulus. */
101 mbedtls_mpi E; /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Rose Zadike8b5b992018-03-27 12:19:47 +0100103 mbedtls_mpi D; /*!< The private exponent. */
104 mbedtls_mpi P; /*!< The first prime factor. */
105 mbedtls_mpi Q; /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100106
Rose Zadike8b5b992018-03-27 12:19:47 +0100107 mbedtls_mpi DP; /*!< \p D % (P - 1) */
108 mbedtls_mpi DQ; /*!< \p D % (Q - 1) */
109 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Rose Zadike8b5b992018-03-27 12:19:47 +0100111 mbedtls_mpi RN; /*!< cached R^2 mod \p N */
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
Rose Zadike8b5b992018-03-27 12:19:47 +0100113 mbedtls_mpi RP; /*!< cached R^2 mod \p P */
114 mbedtls_mpi RQ; /*!< cached R^2 mod \p Q */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200115
Rose Zadike8b5b992018-03-27 12:19:47 +0100116 mbedtls_mpi Vi; /*!< The cached blinding value. */
117 mbedtls_mpi Vf; /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000118
Rose Zadik042e97f2018-01-26 16:35:10 +0000119 int padding; /*!< Selects padding mode:
120 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
121 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
122 int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
123 as specified in md.h for use in the MGF
124 mask generating function used in the
125 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126#if defined(MBEDTLS_THREADING_C)
Rose Zadik042e97f2018-01-26 16:35:10 +0000127 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200128#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000129}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
Paul Bakker5121ce52009-01-03 21:22:43 +0000132/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000133 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000135 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000136 * encryption scheme and the RSASSA-PSS signature scheme.
137 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000138 * \note The \p hash_id parameter is ignored when using
139 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200140 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000141 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200142 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000143 * mixing padding modes. For public key operations it is
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200144 * a default value, which can be overriden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000145 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200146 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000147 * \note The hash selected in \p hash_id is always used for OEAP
148 * encryption. For PSS signatures, it is always used for
149 * making signatures, but can be overriden for verifying them.
150 * If set to #MBEDTLS_MD_NONE, it is always overriden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100151 *
152 * \param ctx The RSA context to initialize.
153 * \param padding Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or
154 * #MBEDTLS_RSA_PKCS_V21.
155 * \param hash_id The hash identifier of #mbedtls_md_type_t type, if
156 * \p padding is #MBEDTLS_RSA_PKCS_V21.
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100159 int padding,
160 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
162/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000163 * \brief This function imports a set of core parameters into an
164 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100165 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100166 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000167 * imports, if the parameters are not simultaneously present.
168 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100169 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000170 * by a call to mbedtls_rsa_complete(), which checks and
171 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100172 * public or private RSA key.
173 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000174 * \note See mbedtls_rsa_complete() for more information on which
175 * parameters are necessary to set up a private or public
176 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100177 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100178 * \note The imported parameters are copied and need not be preserved
179 * for the lifetime of the RSA context being set up.
180 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100181 * \param ctx The initialized RSA context to store the parameters in.
182 * \param N The RSA modulus, or NULL.
183 * \param P The first prime factor of \p N, or NULL.
184 * \param Q The second prime factor of \p N, or NULL.
185 * \param D The private exponent, or NULL.
186 * \param E The public exponent, or NULL.
187 *
188 * \return \c 0 on success.
189 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100190 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100191int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
192 const mbedtls_mpi *N,
193 const mbedtls_mpi *P, const mbedtls_mpi *Q,
194 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100195
196/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000197 * \brief This function imports core RSA parameters, in raw big-endian
198 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100200 * \note This function can be called multiple times for successive
201 * imports, if the parameters are not simultaneously present.
202 *
203 * Any sequence of calls to this function should be followed
204 * by a call to mbedtls_rsa_complete(), which checks and
205 * completes the provided information to a ready-for-use
206 * public or private RSA key.
207 *
208 * \note See mbedtls_rsa_complete() for more information on which
209 * parameters are necessary to set up a private or public
210 * RSA key.
211 *
212 * \note The imported parameters are copied and need not be preserved
213 * for the lifetime of the RSA context being set up.
214 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000215 * \param ctx The initialized RSA context to store the parameters in.
216 * \param N The RSA modulus, or NULL.
217 * \param N_len The Byte length of \p N, ignored if \p N == NULL.
218 * \param P The first prime factor of \p N, or NULL.
219 * \param P_len The Byte length of \p P, ignored if \p P == NULL.
220 * \param Q The second prime factor of \p N, or NULL.
221 * \param Q_len The Byte length of \p Q, ignored if \p Q == NULL.
222 * \param D The private exponent, or NULL.
223 * \param D_len The Byte length of \p D, ignored if \p D == NULL.
224 * \param E The public exponent, or NULL.
225 * \param E_len The Byte length of \p E, ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100227 * \return \c 0 on success.
228 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100229 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100230int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100231 unsigned char const *N, size_t N_len,
232 unsigned char const *P, size_t P_len,
233 unsigned char const *Q, size_t Q_len,
234 unsigned char const *D, size_t D_len,
235 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100236
237/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000238 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100239 * a set of imported core parameters.
240 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000241 * To setup an RSA public key, precisely \p N and \p E
242 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100243 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000244 * To setup an RSA private key, sufficient information must
245 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100246 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000247 * The default implementation supports the following:
248 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
249 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
250 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100251 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000252 * If this function runs successfully, it guarantees that
253 * the RSA context can be used for RSA operations without
254 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100255 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100256 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000257 * for the imported parameters. In particular, parameters that
258 * are not needed by the implementation might be silently
259 * discarded and left unchecked. To check the consistency
260 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100261 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100262 * \param ctx The initialized RSA context holding imported parameters.
263 *
264 * \return \c 0 on success.
265 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
266 * failed.
267 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100268 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100269int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100270
271/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000272 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100273 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000274 * If this function runs successfully, the non-NULL buffers
275 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
276 * written, with additional unused space filled leading by
277 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100278 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000279 * Possible reasons for returning
280 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:<ul>
281 * <li>An alternative RSA implementation is in use, which
282 * stores the key externally, and either cannot or should
283 * not export it into RAM.</li>
284 * <li>A SW or HW implementation might not support a certain
285 * deduction. For example, \p P, \p Q from \p N, \p D,
286 * and \p E if the former are not part of the
287 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100288 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000289 * If the function fails due to an unsupported operation,
290 * the RSA context stays intact and remains usable.
291 *
292 * \param ctx The initialized RSA context.
293 * \param N The MPI to hold the RSA modulus, or NULL.
294 * \param P The MPI to hold the first prime factor of \p N, or NULL.
295 * \param Q The MPI to hold the second prime factor of \p N, or NULL.
296 * \param D The MPI to hold the private exponent, or NULL.
297 * \param E The MPI to hold the public exponent, or NULL.
298 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100299 * \return \c 0 on success.
300 * \return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000301 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100302 * functionality or because of security policies.
303 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100304 *
305 */
306int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
307 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
308 mbedtls_mpi *D, mbedtls_mpi *E );
309
310/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000311 * \brief This function exports core parameters of an RSA key
312 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100313 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000314 * If this function runs successfully, the non-NULL buffers
315 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
316 * written, with additional unused space filled leading by
317 * zero Bytes.
318 *
319 * Possible reasons for returning
320 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:<ul>
321 * <li>An alternative RSA implementation is in use, which
322 * stores the key externally, and either cannot or should
323 * not export it into RAM.</li>
324 * <li>A SW or HW implementation might not support a certain
325 * deduction. For example, \p P, \p Q from \p N, \p D,
326 * and \p E if the former are not part of the
327 * implementation.</li></ul>
328 * If the function fails due to an unsupported operation,
329 * the RSA context stays intact and remains usable.
330 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100331 * \note The length fields are ignored if the corresponding
332 * buffer pointers are NULL.
333 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000334 * \param ctx The initialized RSA context.
335 * \param N The Byte array to store the RSA modulus, or NULL.
336 * \param N_len The size of the buffer for the modulus.
337 * \param P The Byte array to hold the first prime factor of \p N, or
338 * NULL.
339 * \param P_len The size of the buffer for the first prime factor.
340 * \param Q The Byte array to hold the second prime factor of \p N, or
341 NULL.
342 * \param Q_len The size of the buffer for the second prime factor.
343 * \param D The Byte array to hold the private exponent, or NULL.
344 * \param D_len The size of the buffer for the private exponent.
345 * \param E The Byte array to hold the public exponent, or NULL.
346 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100347 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100348 * \return \c 0 on success.
349 * \return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000350 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100351 * functionality or because of security policies.
352 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100353 */
354int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
355 unsigned char *N, size_t N_len,
356 unsigned char *P, size_t P_len,
357 unsigned char *Q, size_t Q_len,
358 unsigned char *D, size_t D_len,
359 unsigned char *E, size_t E_len );
360
361/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000362 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100363 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100364 * \note Alternative RSA implementations not using CRT-parameters
365 * internally can implement this function based on
366 * mbedtls_rsa_deduce_opt().
367 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000368 * \param ctx The initialized RSA context.
369 * \param DP The MPI to hold D modulo P-1, or NULL.
370 * \param DQ The MPI to hold D modulo Q-1, or NULL.
371 * \param QP The MPI to hold modular inverse of Q modulo P, or NULL.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100372 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100373 * \return \c 0 on success.
374 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100375 *
376 */
377int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
378 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
379
Paul Bakker5121ce52009-01-03 21:22:43 +0000380/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000381 * \brief This function sets padding for an already initialized RSA
382 * context. See mbedtls_rsa_init() for details.
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000384 * \param ctx The RSA context to be set.
385 * \param padding Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or
386 * #MBEDTLS_RSA_PKCS_V21.
387 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Paul Bakker40e46942009-01-03 21:51:57 +0000388 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100389void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
390 int hash_id);
Paul Bakker21eb2802010-08-16 11:10:02 +0000391
Paul Bakkera3d195c2011-11-27 21:07:34 +0000392/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000393 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100394 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000395 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100396 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000397 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100398 *
399 */
400size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
401
402/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000403 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000404 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000405 * \note mbedtls_rsa_init() must be called before this function,
406 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100408 * \param ctx The RSA context used to hold the key.
409 * \param f_rng The RNG function.
410 * \param p_rng The RNG context.
411 * \param nbits The size of the public key in bits.
412 * \param exponent The public exponent. For example, 65537.
413 *
414 * \return \c 0 on success.
415 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100418 int (*f_rng)(void *, unsigned char *, size_t),
419 void *p_rng,
420 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000421
422/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000423 * \brief This function checks if a context contains at least an RSA
424 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000426 * If the function runs successfully, it is guaranteed that
427 * enough information is present to perform an RSA public key
428 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000430 * \param ctx The RSA context to check.
431 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100432 * \return \c 0 on success.
433 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100434 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
438/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000439 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100440 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000441 *
Hanno Becker68767a62017-10-17 10:13:31 +0100442 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000443 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100444 * on the given context, but that the various parameters are
445 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000446 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100447 *
448 * \warning This function should catch accidental misconfigurations
449 * like swapping of parameters, but it cannot establish full
450 * trust in neither the quality nor the consistency of the key
451 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000452 * <ul><li>Consistency: Imported parameters that are irrelevant
453 * for the implementation might be silently dropped. If dropped,
454 * the current function does not have access to them,
455 * and therefore cannot check them. See mbedtls_rsa_complete().
456 * If you want to check the consistency of the entire
457 * content of an PKCS1-encoded RSA private key, for example, you
458 * should use mbedtls_rsa_validate_params() before setting
459 * up the RSA context.
460 * Additionally, if the implementation performs empirical checks,
461 * these checks substantiate but do not guarantee consistency.</li>
462 * <li>Quality: This function is not expected to perform
463 * extended quality assessments like checking that the prime
464 * factors are safe. Additionally, it is the responsibility of the
465 * user to ensure the trustworthiness of the source of his RSA
466 * parameters, which goes beyond what is effectively checkable
467 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100468 *
469 * \param ctx The RSA context to check.
470 *
471 * \return \c 0 on success.
472 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
476/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000477 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100478 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000479 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100480 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000481 * \param pub The RSA context holding the public key.
482 * \param prv The RSA context holding the private key.
483 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100484 * \return \c 0 on success.
485 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100486 */
Hanno Becker98838b02017-10-02 13:16:10 +0100487int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
488 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100489
490/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000491 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000492 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000493 * \note This function does not handle message padding.
494 *
495 * \note Make sure to set \p input[0] = 0 or ensure that
496 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 *
498 * \note The input and output buffers must be large
Rose Zadik042e97f2018-01-26 16:35:10 +0000499 * enough. For example, 128 Bytes if RSA-1024 is used.
Rose Zadike8b5b992018-03-27 12:19:47 +0100500 *
501 * \param ctx The RSA context.
502 * \param input The input buffer.
503 * \param output The output buffer.
504 *
505 * \return \c 0 on success.
506 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000509 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 unsigned char *output );
511
512/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000513 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 * \note The input and output buffers must be large
Rose Zadik042e97f2018-01-26 16:35:10 +0000516 * enough. For example, 128 Bytes if RSA-1024 is used.
Hanno Becker88ec2382017-05-03 13:51:16 +0100517 *
Hanno Becker24120612017-10-26 11:53:35 +0100518 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100519 *
520 * \note If blinding is used, both the base of exponentation
Hanno Becker24120612017-10-26 11:53:35 +0100521 * and the exponent are blinded, providing protection
522 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100523 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100524 * \warning It is deprecated and a security risk to not provide
525 * a PRNG here and thereby prevent the use of blinding.
526 * Future versions of the library may enforce the presence
527 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100528 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100529 * \param ctx The RSA context.
530 * \param f_rng The RNG function. Needed for blinding.
531 * \param p_rng The RNG context.
532 * \param input The input buffer.
533 * \param output The output buffer.
534 *
535 * \return \c 0 on success.
536 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
537 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200540 int (*f_rng)(void *, unsigned char *, size_t),
541 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000542 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000543 unsigned char *output );
544
545/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000546 * \brief This function adds the message padding, then performs an RSA
547 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000548 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000549 * It is the generic wrapper for performing a PKCS#1 encryption
550 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100552 * \note Alternative implementations of RSA need not support
553 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
554 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Paul Bakker5121ce52009-01-03 21:22:43 +0000555 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100556 * \note The input and output buffers must be as large as the size
557 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100559 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000560 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
561 * are likely to remove the \p mode argument and have it
562 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100563 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100564 * \param ctx The RSA context.
565 * \param f_rng The RNG function. Needed for padding, PKCS#1 v2.1
566 * encoding, and #MBEDTLS_RSA_PRIVATE.
567 * \param p_rng The RNG context.
568 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
569 * \param ilen The length of the plaintext.
570 * \param input The buffer holding the data to encrypt.
571 * \param output The buffer used to hold the ciphertext.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100572 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100573 * \return \c 0 on success.
574 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000577 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000578 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000579 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000580 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 unsigned char *output );
582
583/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000584 * \brief This function performs a PKCS#1 v1.5 encryption operation
585 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100586 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100587 * \note Alternative implementations of RSA need not support
588 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
589 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
590 *
591 * \note The output buffer must be as large as the size
592 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100593 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100594 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000595 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
596 * are likely to remove the \p mode argument and have it
597 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100598 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100599 * \param ctx The RSA context.
600 * \param f_rng The RNG function. Needed for padding and
601 * #MBEDTLS_RSA_PRIVATE.
602 * \param p_rng The RNG context.
603 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
604 * \param ilen The length of the plaintext.
605 * \param input The buffer holding the data to encrypt.
606 * \param output The buffer used to hold the ciphertext.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100607 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100608 * \return \c 0 on success.
609 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100612 int (*f_rng)(void *, unsigned char *, size_t),
613 void *p_rng,
614 int mode, size_t ilen,
615 const unsigned char *input,
616 unsigned char *output );
617
618/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000619 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
620 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100621 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100622 * \note Alternative implementations of RSA need not support
623 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
624 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
625 *
626 * \note The output buffer must be as large as the size
627 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
628 *
629 * \deprecated It is deprecated and discouraged to call this function
630 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
631 * are likely to remove the \p mode argument and have it
632 * implicitly set to #MBEDTLS_RSA_PUBLIC.
633 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000634 * \param ctx The RSA context.
635 * \param f_rng The RNG function. Needed for padding and PKCS#1 v2.1
636 * encoding and #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +0100637 * \param p_rng The RNG context.
Rose Zadik042e97f2018-01-26 16:35:10 +0000638 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
639 * \param label The buffer holding the custom label to use.
640 * \param label_len The length of the label.
641 * \param ilen The length of the plaintext.
642 * \param input The buffer holding the data to encrypt.
643 * \param output The buffer used to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100644 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100645 * \return \c 0 on success.
646 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100649 int (*f_rng)(void *, unsigned char *, size_t),
650 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100651 int mode,
652 const unsigned char *label, size_t label_len,
653 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100654 const unsigned char *input,
655 unsigned char *output );
656
657/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000658 * \brief This function performs an RSA operation, then removes the
659 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000660 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000661 * It is the generic wrapper for performing a PKCS#1 decryption
662 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100664 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000665 * as large as the size \p ctx->len of \p ctx->N (for example,
666 * 128 Bytes if RSA-1024 is used) to be able to hold an
667 * arbitrary decrypted message. If it is not large enough to
668 * hold the decryption of the particular ciphertext provided,
669 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100670 *
671 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000672 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Rose Zadike8b5b992018-03-27 12:19:47 +0100673 *
674 * \note Alternative implementations of RSA need not support
675 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
676 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
677 *
678 * \deprecated It is deprecated and discouraged to call this function
679 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
680 * are likely to remove the \p mode argument and have it
681 * implicitly set to #MBEDTLS_RSA_PRIVATE.
682 *
683 * \param ctx The RSA context.
684 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
685 * \param p_rng The RNG context.
686 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
687 * \param olen The length of the plaintext.
688 * \param input The buffer holding the encrypted data.
689 * \param output The buffer used to hold the plaintext.
690 * \param output_max_len The maximum length of the output buffer.
691 *
692 * \return \c 0 on success.
693 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
694
Paul Bakker5121ce52009-01-03 21:22:43 +0000695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200697 int (*f_rng)(void *, unsigned char *, size_t),
698 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000699 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000700 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000701 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000702 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
704/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000705 * \brief This function performs a PKCS#1 v1.5 decryption
706 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100707 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100708 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000709 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
710 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100711 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100712 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000713 * as large as the size \p ctx->len of \p ctx->N, for example,
714 * 128 Bytes if RSA-1024 is used, to be able to hold an
715 * arbitrary decrypted message. If it is not large enough to
716 * hold the decryption of the particular ciphertext provided,
717 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100718 *
719 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000720 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Rose Zadike8b5b992018-03-27 12:19:47 +0100721 *
722 * \deprecated It is deprecated and discouraged to call this function
723 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
724 * are likely to remove the \p mode argument and have it
725 * implicitly set to #MBEDTLS_RSA_PRIVATE.
726 *
727 * \param ctx The RSA context.
728 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
729 * \param p_rng The RNG context.
730 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
731 * \param olen The length of the plaintext.
732 * \param input The buffer holding the encrypted data.
733 * \param output The buffer to hold the plaintext.
734 * \param output_max_len The maximum length of the output buffer.
735 *
736 * \return \c 0 on success.
737 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
738 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200741 int (*f_rng)(void *, unsigned char *, size_t),
742 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100743 int mode, size_t *olen,
744 const unsigned char *input,
745 unsigned char *output,
746 size_t output_max_len );
747
748/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100749 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
750 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100751 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100752 * \note Alternative implementations of RSA need not support
753 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
754 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Paul Bakkerb3869132013-02-28 17:21:01 +0100755 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100756 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000757 * as large as the size \p ctx->len of \p ctx->N, for
758 * example, 128 Bytes if RSA-1024 is used, to be able to
759 * hold an arbitrary decrypted message. If it is not
760 * large enough to hold the decryption of the particular
761 * ciphertext provided, the function returns
762 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100763 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100764 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000765 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Rose Zadike8b5b992018-03-27 12:19:47 +0100766 *
767 * \deprecated It is deprecated and discouraged to call this function
768 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
769 * are likely to remove the \p mode argument and have it
770 * implicitly set to #MBEDTLS_RSA_PRIVATE.
771 *
772 * \param ctx The RSA context.
773 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
774 * \param p_rng The RNG context.
775 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
776 * \param label The buffer holding the custom label to use.
777 * \param label_len The length of the label.
778 * \param olen The length of the plaintext.
779 * \param input The buffer holding the encrypted data.
780 * \param output The buffer to hold the plaintext.
781 * \param output_max_len The maximum length of the output buffer.
782 *
783 * \return \c 0 on success.
784 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100785 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200787 int (*f_rng)(void *, unsigned char *, size_t),
788 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100789 int mode,
790 const unsigned char *label, size_t label_len,
791 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100792 const unsigned char *input,
793 unsigned char *output,
794 size_t output_max_len );
795
796/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000797 * \brief This function performs a private RSA operation to sign
798 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000799 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000800 * It is the generic wrapper for performing a PKCS#1
801 * signature using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000802 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100803 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000804 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
805 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100806 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000807 * \note The \p sig buffer must be as large as the size
808 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000809 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000810 * \note For PKCS#1 v2.1 encoding, see comments on
811 * mbedtls_rsa_rsassa_pss_sign() for details on
812 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100813 *
814 * \deprecated It is deprecated and discouraged to call this function
815 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
816 * are likely to remove the \p mode argument and have it
817 * implicitly set to #MBEDTLS_RSA_PRIVATE.
818 *
819 * \param ctx The RSA context.
820 * \param f_rng The RNG function. Needed for PKCS#1 v2.1 encoding and for
821 * #MBEDTLS_RSA_PRIVATE.
822 * \param p_rng The RNG context.
823 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
824 * \param md_alg The message-digest algorithm used to hash the original data.
825 * Use #MBEDTLS_MD_NONE for signing raw data.
826 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
827 * \param hash The buffer holding the message digest.
828 * \param sig The buffer to hold the ciphertext.
829 *
830 * \return \c 0 if the signing operation was successful.
831 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000832 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000834 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000835 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000836 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000838 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000839 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000840 unsigned char *sig );
841
842/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000843 * \brief This function performs a PKCS#1 v1.5 signature
844 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100845 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100846 * \note Alternative implementations of RSA need not support
847 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
848 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
849 *
850 * \note The \p sig buffer must be as large as the size
851 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
852 *
853 * \deprecated It is deprecated and discouraged to call this function
854 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
855 * are likely to remove the \p mode argument and have it
856 * implicitly set to #MBEDTLS_RSA_PRIVATE.
857 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000858 * \param ctx The RSA context.
859 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +0100860 * \param p_rng The RNG context.
Rose Zadik042e97f2018-01-26 16:35:10 +0000861 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
862 * \param md_alg The message-digest algorithm used to hash the original data.
863 * Use #MBEDTLS_MD_NONE for signing raw data.
864 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
865 * \param hash The buffer holding the message digest.
866 * \param sig The buffer to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100867 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100868 * \return \c 0 if the signing operation was successful.
869 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200872 int (*f_rng)(void *, unsigned char *, size_t),
873 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100874 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100876 unsigned int hashlen,
877 const unsigned char *hash,
878 unsigned char *sig );
879
880/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000881 * \brief This function performs a PKCS#1 v2.1 PSS signature
882 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100883 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100884 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000885 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
886 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Paul Bakkerb3869132013-02-28 17:21:01 +0100887 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000888 * \note The \p sig buffer must be as large as the size
889 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100890 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000891 * \note The \p hash_id in the RSA context is the one used for the
892 * encoding. \p md_alg in the function call is the type of hash
893 * that is encoded. According to <em>RFC-3447: Public-Key
894 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
895 * Specifications</em> it is advised to keep both hashes the
896 * same.
Rose Zadike8b5b992018-03-27 12:19:47 +0100897 *
898 * \deprecated It is deprecated and discouraged to call this function
899 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
900 * are likely to remove the \p mode argument and have it
901 * implicitly set to #MBEDTLS_RSA_PRIVATE.
902 *
903 * \param ctx The RSA context.
904 * \param f_rng The RNG function. Needed for PKCS#1 v2.1 encoding and for
905 * #MBEDTLS_RSA_PRIVATE.
906 * \param p_rng The RNG context.
907 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
908 * \param md_alg The message-digest algorithm used to hash the original data.
909 * Use #MBEDTLS_MD_NONE for signing raw data.
910 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
911 * \param hash The buffer holding the message digest.
912 * \param sig The buffer to hold the ciphertext.
913 *
914 * \return \c 0 if the signing operation was successful.
915 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100916 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100918 int (*f_rng)(void *, unsigned char *, size_t),
919 void *p_rng,
920 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100922 unsigned int hashlen,
923 const unsigned char *hash,
924 unsigned char *sig );
925
926/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000927 * \brief This function performs a public RSA operation and checks
928 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000930 * This is the generic wrapper for performing a PKCS#1
931 * verification using the mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000932 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100933 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000934 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
935 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100936 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000937 * \note The \p sig buffer must be as large as the size
938 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000939 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000940 * \note For PKCS#1 v2.1 encoding, see comments on
941 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
942 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100943 *
944 * \deprecated It is deprecated and discouraged to call this function
945 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
946 * are likely to remove the \p mode argument and have it
947 * set to #MBEDTLS_RSA_PUBLIC.
948 *
949 * \param ctx The RSA public key context.
950 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
951 * \param p_rng The RNG context.
952 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
953 * \param md_alg The message-digest algorithm used to hash the original data.
954 * Use #MBEDTLS_MD_NONE for signing raw data.
955 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
956 * \param hash The buffer holding the message digest.
957 * \param sig The buffer holding the ciphertext.
958 *
959 * \return \c 0 if the verify operation was successful.
960 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000961 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200963 int (*f_rng)(void *, unsigned char *, size_t),
964 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000965 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000967 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000968 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200969 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000970
971/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000972 * \brief This function performs a PKCS#1 v1.5 verification
973 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +0100974 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100975 * \note Alternative implementations of RSA need not support
976 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
977 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
978 *
979 * \note The \p sig buffer must be as large as the size
980 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
981 *
982 * \deprecated It is deprecated and discouraged to call this function
983 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
984 * are likely to remove the \p mode argument and have it
985 * set to #MBEDTLS_RSA_PUBLIC.
986 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000987 * \param ctx The RSA public key context.
988 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +0100989 * \param p_rng The RNG context.
Rose Zadik042e97f2018-01-26 16:35:10 +0000990 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
991 * \param md_alg The message-digest algorithm used to hash the original data.
992 * Use #MBEDTLS_MD_NONE for signing raw data.
993 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
994 * \param hash The buffer holding the message digest.
995 * \param sig The buffer holding the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100996 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100997 * \return \c 0 if the verify operation was successful.
998 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100999 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001001 int (*f_rng)(void *, unsigned char *, size_t),
1002 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001003 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001005 unsigned int hashlen,
1006 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001007 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001008
1009/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001010 * \brief This function performs a PKCS#1 v2.1 PSS verification
1011 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001012 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001013 * The hash function for the MGF mask generating function
1014 * is that specified in the RSA context.
Paul Bakkerb3869132013-02-28 17:21:01 +01001015 *
Hanno Becker3cdc7112017-10-05 10:09:31 +01001016 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +00001017 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
1018 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +01001019 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001020 * \note The \p sig buffer must be as large as the size
1021 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001022 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001023 * \note The \p hash_id in the RSA context is the one used for the
1024 * verification. \p md_alg in the function call is the type of
1025 * hash that is verified. According to <em>RFC-3447: Public-Key
1026 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1027 * Specifications</em> it is advised to keep both hashes the
1028 * same. If \p hash_id in the RSA context is unset,
1029 * the \p md_alg from the function call is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001030 *
1031 * \deprecated It is deprecated and discouraged to call this function
1032 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1033 * are likely to remove the \p mode argument and have it
1034 * implicitly set to #MBEDTLS_RSA_PUBLIC.
1035 *
1036 * \param ctx The RSA public key context.
1037 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
1038 * \param p_rng The RNG context.
1039 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1040 * \param md_alg The message-digest algorithm used to hash the original data.
1041 * Use #MBEDTLS_MD_NONE for signing raw data.
1042 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
1043 * \param hash The buffer holding the message digest.
1044 * \param sig The buffer holding the ciphertext.
1045 *
1046 * \return \c 0 if the verify operation was successful.
1047 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001050 int (*f_rng)(void *, unsigned char *, size_t),
1051 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001052 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001054 unsigned int hashlen,
1055 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001056 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001057
1058/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001059 * \brief This function performs a PKCS#1 v2.1 PSS verification
1060 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001061 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001062 * The hash function for the MGF mask generating function
1063 * is that specified in \p mgf1_hash_id.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001064 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001065 * \note The \p sig buffer must be as large as the size
1066 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001067 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001068 * \note The \p hash_id in the RSA context is ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001069 *
1070 * \param ctx The RSA public key context.
1071 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
1072 * \param p_rng The RNG context.
1073 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1074 * \param md_alg The message-digest algorithm used to hash the original data.
1075 * Use #MBEDTLS_MD_NONE for signing raw data.
1076 * \param hashlen The length of the message digest. Only used if \p md_alg is
1077 * #MBEDTLS_MD_NONE.
1078 * \param hash The buffer holding the message digest.
1079 * \param mgf1_hash_id The message digest used for mask generation.
1080 * \param expected_salt_len The length of the salt used in padding. Use
1081 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1082 * \param sig The buffer holding the ciphertext.
1083 *
1084 * \return \c 0 if the verify operation was successful.
1085 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001088 int (*f_rng)(void *, unsigned char *, size_t),
1089 void *p_rng,
1090 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001092 unsigned int hashlen,
1093 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001095 int expected_salt_len,
1096 const unsigned char *sig );
1097
1098/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001099 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001100 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001101 * \param dst The destination context.
1102 * \param src The source context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001103 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001104 * \return \c 0 on success.
1105 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001108
1109/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001110 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001111 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001112 * \param ctx The RSA Context to free.
Paul Bakker5121ce52009-01-03 21:22:43 +00001113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001115
Hanno Beckerd22b78b2017-10-12 11:42:17 +01001116#ifdef __cplusplus
1117}
1118#endif
1119
1120#else /* MBEDTLS_RSA_ALT */
1121#include "rsa_alt.h"
1122#endif /* MBEDTLS_RSA_ALT */
1123
1124#ifdef __cplusplus
1125extern "C" {
1126#endif
1127
Paul Bakker5121ce52009-01-03 21:22:43 +00001128/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001129 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001131 * \return \c 0 on success.
1132 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001135
1136#ifdef __cplusplus
1137}
1138#endif
1139
Paul Bakker5121ce52009-01-03 21:22:43 +00001140#endif /* rsa.h */