blob: 71b5c2e8b701150edc87b8b8204990bb3fd8e090 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Rose Zadik21e29262018-04-17 14:08:56 +01004 * \brief This file provides an API for the RSA public-key cryptosystem.
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
Rose Zadike8b5b992018-03-27 12:19:47 +01006 * The RSA public-key cryptosystem is defined in <em>Public-Key
7 * Cryptography Standards (PKCS) #1 v1.5: RSA Encryption</em>
Darryl Green11999bb2018-03-13 15:22:58 +00008 * and <em>Public-Key Cryptography Standards (PKCS) #1 v2.1:
Rose Zadike8b5b992018-03-27 12:19:47 +01009 * RSA Cryptography Specifications</em>.
Rose Zadik042e97f2018-01-26 16:35:10 +000010 *
Darryl Greena40a1012018-01-05 15:33:17 +000011 */
12/*
Rose Zadik042e97f2018-01-26 16:35:10 +000013 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
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
Paul Bakker407a0da2013-06-27 14:29:21 +020080#ifdef __cplusplus
81extern "C" {
82#endif
83
Ron Eldor4e6d55d2018-02-07 16:36:15 +020084#if !defined(MBEDTLS_RSA_ALT)
85// Regular implementation
86//
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 Zadikf2ec2882018-04-17 10:27:25 +0100107 mbedtls_mpi DP; /*!< <code>D % (P - 1)</code>. */
108 mbedtls_mpi DQ; /*!< <code>D % (Q - 1)</code>. */
109 mbedtls_mpi QP; /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Rose Zadikf2ec2882018-04-17 10:27:25 +0100111 mbedtls_mpi RN; /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
Rose Zadikf2ec2882018-04-17 10:27:25 +0100113 mbedtls_mpi RP; /*!< cached <code>R^2 mod P</code>. */
114 mbedtls_mpi RQ; /*!< cached <code>R^2 mod Q</code>. */
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
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200132#else /* MBEDTLS_RSA_ALT */
133#include "rsa_alt.h"
134#endif /* MBEDTLS_RSA_ALT */
135
Paul Bakker5121ce52009-01-03 21:22:43 +0000136/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000137 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000139 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000140 * encryption scheme and the RSASSA-PSS signature scheme.
141 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000142 * \note The \p hash_id parameter is ignored when using
143 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200144 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000145 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200146 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000147 * mixing padding modes. For public key operations it is
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200148 * a default value, which can be overriden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000149 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200150 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000151 * \note The hash selected in \p hash_id is always used for OEAP
152 * encryption. For PSS signatures, it is always used for
153 * making signatures, but can be overriden for verifying them.
154 * If set to #MBEDTLS_MD_NONE, it is always overriden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100155 *
156 * \param ctx The RSA context to initialize.
157 * \param padding Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or
158 * #MBEDTLS_RSA_PKCS_V21.
159 * \param hash_id The hash identifier of #mbedtls_md_type_t type, if
160 * \p padding is #MBEDTLS_RSA_PKCS_V21.
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100163 int padding,
164 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
166/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000167 * \brief This function imports a set of core parameters into an
168 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100169 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100170 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000171 * imports, if the parameters are not simultaneously present.
172 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100173 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000174 * by a call to mbedtls_rsa_complete(), which checks and
175 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100176 * public or private RSA key.
177 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000178 * \note See mbedtls_rsa_complete() for more information on which
179 * parameters are necessary to set up a private or public
180 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100181 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100182 * \note The imported parameters are copied and need not be preserved
183 * for the lifetime of the RSA context being set up.
184 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100185 * \param ctx The initialized RSA context to store the parameters in.
186 * \param N The RSA modulus, or NULL.
187 * \param P The first prime factor of \p N, or NULL.
188 * \param Q The second prime factor of \p N, or NULL.
189 * \param D The private exponent, or NULL.
190 * \param E The public exponent, or NULL.
191 *
192 * \return \c 0 on success.
193 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100194 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100195int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
196 const mbedtls_mpi *N,
197 const mbedtls_mpi *P, const mbedtls_mpi *Q,
198 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100199
200/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000201 * \brief This function imports core RSA parameters, in raw big-endian
202 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100204 * \note This function can be called multiple times for successive
205 * imports, if the parameters are not simultaneously present.
206 *
207 * Any sequence of calls to this function should be followed
208 * by a call to mbedtls_rsa_complete(), which checks and
209 * completes the provided information to a ready-for-use
210 * public or private RSA key.
211 *
212 * \note See mbedtls_rsa_complete() for more information on which
213 * parameters are necessary to set up a private or public
214 * RSA key.
215 *
216 * \note The imported parameters are copied and need not be preserved
217 * for the lifetime of the RSA context being set up.
218 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000219 * \param ctx The initialized RSA context to store the parameters in.
220 * \param N The RSA modulus, or NULL.
221 * \param N_len The Byte length of \p N, ignored if \p N == NULL.
222 * \param P The first prime factor of \p N, or NULL.
223 * \param P_len The Byte length of \p P, ignored if \p P == NULL.
224 * \param Q The second prime factor of \p N, or NULL.
225 * \param Q_len The Byte length of \p Q, ignored if \p Q == NULL.
226 * \param D The private exponent, or NULL.
227 * \param D_len The Byte length of \p D, ignored if \p D == NULL.
228 * \param E The public exponent, or NULL.
229 * \param E_len The Byte length of \p E, ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100231 * \return \c 0 on success.
232 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100233 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100234int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100235 unsigned char const *N, size_t N_len,
236 unsigned char const *P, size_t P_len,
237 unsigned char const *Q, size_t Q_len,
238 unsigned char const *D, size_t D_len,
239 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100240
241/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000242 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100243 * a set of imported core parameters.
244 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000245 * To setup an RSA public key, precisely \p N and \p E
246 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100247 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000248 * To setup an RSA private key, sufficient information must
249 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100250 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000251 * The default implementation supports the following:
252 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
253 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
254 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100255 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000256 * If this function runs successfully, it guarantees that
257 * the RSA context can be used for RSA operations without
258 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100259 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100260 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000261 * for the imported parameters. In particular, parameters that
262 * are not needed by the implementation might be silently
263 * discarded and left unchecked. To check the consistency
264 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100265 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100266 * \param ctx The initialized RSA context holding imported parameters.
267 *
268 * \return \c 0 on success.
269 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
270 * failed.
271 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100272 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100273int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100274
275/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000276 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100277 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000278 * If this function runs successfully, the non-NULL buffers
279 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
280 * written, with additional unused space filled leading by
281 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100282 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000283 * Possible reasons for returning
284 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:<ul>
285 * <li>An alternative RSA implementation is in use, which
286 * stores the key externally, and either cannot or should
287 * not export it into RAM.</li>
288 * <li>A SW or HW implementation might not support a certain
289 * deduction. For example, \p P, \p Q from \p N, \p D,
290 * and \p E if the former are not part of the
291 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100292 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000293 * If the function fails due to an unsupported operation,
294 * the RSA context stays intact and remains usable.
295 *
296 * \param ctx The initialized RSA context.
297 * \param N The MPI to hold the RSA modulus, or NULL.
298 * \param P The MPI to hold the first prime factor of \p N, or NULL.
299 * \param Q The MPI to hold the second prime factor of \p N, or NULL.
300 * \param D The MPI to hold the private exponent, or NULL.
301 * \param E The MPI to hold the public exponent, or NULL.
302 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100303 * \return \c 0 on success.
304 * \return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000305 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100306 * functionality or because of security policies.
307 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100308 *
309 */
310int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
311 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
312 mbedtls_mpi *D, mbedtls_mpi *E );
313
314/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000315 * \brief This function exports core parameters of an RSA key
316 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100317 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000318 * If this function runs successfully, the non-NULL buffers
319 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
320 * written, with additional unused space filled leading by
321 * zero Bytes.
322 *
323 * Possible reasons for returning
324 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:<ul>
325 * <li>An alternative RSA implementation is in use, which
326 * stores the key externally, and either cannot or should
327 * not export it into RAM.</li>
328 * <li>A SW or HW implementation might not support a certain
329 * deduction. For example, \p P, \p Q from \p N, \p D,
330 * and \p E if the former are not part of the
331 * implementation.</li></ul>
332 * If the function fails due to an unsupported operation,
333 * the RSA context stays intact and remains usable.
334 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100335 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100336 * buffer pointers are NULL.
337 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000338 * \param ctx The initialized RSA context.
339 * \param N The Byte array to store the RSA modulus, or NULL.
340 * \param N_len The size of the buffer for the modulus.
341 * \param P The Byte array to hold the first prime factor of \p N, or
342 * NULL.
343 * \param P_len The size of the buffer for the first prime factor.
344 * \param Q The Byte array to hold the second prime factor of \p N, or
Rose Zadikf2ec2882018-04-17 10:27:25 +0100345 * NULL.
Rose Zadik042e97f2018-01-26 16:35:10 +0000346 * \param Q_len The size of the buffer for the second prime factor.
347 * \param D The Byte array to hold the private exponent, or NULL.
348 * \param D_len The size of the buffer for the private exponent.
349 * \param E The Byte array to hold the public exponent, or NULL.
350 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100351 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100352 * \return \c 0 on success.
353 * \return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000354 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100355 * functionality or because of security policies.
356 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100357 */
358int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
359 unsigned char *N, size_t N_len,
360 unsigned char *P, size_t P_len,
361 unsigned char *Q, size_t Q_len,
362 unsigned char *D, size_t D_len,
363 unsigned char *E, size_t E_len );
364
365/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000366 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100367 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100368 * \note Alternative RSA implementations not using CRT-parameters
369 * internally can implement this function based on
370 * mbedtls_rsa_deduce_opt().
371 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000372 * \param ctx The initialized RSA context.
373 * \param DP The MPI to hold D modulo P-1, or NULL.
374 * \param DQ The MPI to hold D modulo Q-1, or NULL.
375 * \param QP The MPI to hold modular inverse of Q modulo P, or NULL.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100376 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100377 * \return \c 0 on success.
378 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100379 *
380 */
381int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
382 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
383
Paul Bakker5121ce52009-01-03 21:22:43 +0000384/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000385 * \brief This function sets padding for an already initialized RSA
386 * context. See mbedtls_rsa_init() for details.
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000388 * \param ctx The RSA context to be set.
389 * \param padding Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or
390 * #MBEDTLS_RSA_PKCS_V21.
391 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Paul Bakker40e46942009-01-03 21:51:57 +0000392 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100393void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
394 int hash_id);
Paul Bakker21eb2802010-08-16 11:10:02 +0000395
Paul Bakkera3d195c2011-11-27 21:07:34 +0000396/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000397 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100398 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000399 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100400 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000401 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100402 *
403 */
404size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
405
406/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000407 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000409 * \note mbedtls_rsa_init() must be called before this function,
410 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100412 * \param ctx The RSA context used to hold the key.
413 * \param f_rng The RNG function.
414 * \param p_rng The RNG context.
415 * \param nbits The size of the public key in bits.
416 * \param exponent The public exponent. For example, 65537.
417 *
418 * \return \c 0 on success.
419 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100422 int (*f_rng)(void *, unsigned char *, size_t),
423 void *p_rng,
424 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
426/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000427 * \brief This function checks if a context contains at least an RSA
428 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000430 * If the function runs successfully, it is guaranteed that
431 * enough information is present to perform an RSA public key
432 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000434 * \param ctx The RSA context to check.
435 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100436 * \return \c 0 on success.
437 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100438 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000441
442/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000443 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100444 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000445 *
Hanno Becker68767a62017-10-17 10:13:31 +0100446 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000447 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100448 * on the given context, but that the various parameters are
449 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000450 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100451 *
452 * \warning This function should catch accidental misconfigurations
453 * like swapping of parameters, but it cannot establish full
454 * trust in neither the quality nor the consistency of the key
455 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000456 * <ul><li>Consistency: Imported parameters that are irrelevant
457 * for the implementation might be silently dropped. If dropped,
458 * the current function does not have access to them,
459 * and therefore cannot check them. See mbedtls_rsa_complete().
460 * If you want to check the consistency of the entire
461 * content of an PKCS1-encoded RSA private key, for example, you
462 * should use mbedtls_rsa_validate_params() before setting
463 * up the RSA context.
464 * Additionally, if the implementation performs empirical checks,
465 * these checks substantiate but do not guarantee consistency.</li>
466 * <li>Quality: This function is not expected to perform
467 * extended quality assessments like checking that the prime
468 * factors are safe. Additionally, it is the responsibility of the
469 * user to ensure the trustworthiness of the source of his RSA
470 * parameters, which goes beyond what is effectively checkable
471 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100472 *
473 * \param ctx The RSA context to check.
474 *
475 * \return \c 0 on success.
476 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
480/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000481 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100482 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000483 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100484 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000485 * \param pub The RSA context holding the public key.
486 * \param prv The RSA context holding the private key.
487 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100488 * \return \c 0 on success.
489 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100490 */
Hanno Becker98838b02017-10-02 13:16:10 +0100491int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
492 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100493
494/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000495 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000496 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000497 * \note This function does not handle message padding.
498 *
499 * \note Make sure to set \p input[0] = 0 or ensure that
500 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 *
502 * \note The input and output buffers must be large
Rose Zadik042e97f2018-01-26 16:35:10 +0000503 * enough. For example, 128 Bytes if RSA-1024 is used.
Rose Zadike8b5b992018-03-27 12:19:47 +0100504 *
505 * \param ctx The RSA context.
506 * \param input The input buffer.
507 * \param output The output buffer.
508 *
509 * \return \c 0 on success.
510 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000513 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 unsigned char *output );
515
516/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000517 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 * \note The input and output buffers must be large
Rose Zadik042e97f2018-01-26 16:35:10 +0000520 * enough. For example, 128 Bytes if RSA-1024 is used.
Hanno Becker88ec2382017-05-03 13:51:16 +0100521 *
Hanno Becker24120612017-10-26 11:53:35 +0100522 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100523 *
524 * \note If blinding is used, both the base of exponentation
Hanno Becker24120612017-10-26 11:53:35 +0100525 * and the exponent are blinded, providing protection
526 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100527 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100528 * \warning It is deprecated and a security risk to not provide
529 * a PRNG here and thereby prevent the use of blinding.
530 * Future versions of the library may enforce the presence
531 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100532 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100533 * \param ctx The RSA context.
534 * \param f_rng The RNG function. Needed for blinding.
535 * \param p_rng The RNG context.
536 * \param input The input buffer.
537 * \param output The output buffer.
538 *
539 * \return \c 0 on success.
540 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
541 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000542 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200544 int (*f_rng)(void *, unsigned char *, size_t),
545 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000546 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 unsigned char *output );
548
549/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000550 * \brief This function adds the message padding, then performs an RSA
551 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000553 * It is the generic wrapper for performing a PKCS#1 encryption
554 * operation using the \p mode from the context.
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 Zadikf2ec2882018-04-17 10:27:25 +0100564 * \note Alternative implementations of RSA need not support
565 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
566 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
567 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100568 * \param ctx The RSA context.
569 * \param f_rng The RNG function. Needed for padding, PKCS#1 v2.1
570 * encoding, and #MBEDTLS_RSA_PRIVATE.
571 * \param p_rng The RNG context.
572 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
573 * \param ilen The length of the plaintext.
574 * \param input The buffer holding the data to encrypt.
575 * \param output The buffer used to hold the ciphertext.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100576 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100577 * \return \c 0 on success.
578 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000581 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000582 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000583 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000584 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 unsigned char *output );
586
587/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000588 * \brief This function performs a PKCS#1 v1.5 encryption operation
589 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100590 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100591 * \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 Zadikf2ec2882018-04-17 10:27:25 +0100599 * \note Alternative implementations of RSA need not support
600 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
601 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
602 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100603 * \param ctx The RSA context.
604 * \param f_rng The RNG function. Needed for padding and
605 * #MBEDTLS_RSA_PRIVATE.
606 * \param p_rng The RNG context.
607 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
608 * \param ilen The length of the plaintext.
609 * \param input The buffer holding the data to encrypt.
610 * \param output The buffer used to hold the ciphertext.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100611 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100612 * \return \c 0 on success.
613 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100616 int (*f_rng)(void *, unsigned char *, size_t),
617 void *p_rng,
618 int mode, size_t ilen,
619 const unsigned char *input,
620 unsigned char *output );
621
622/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000623 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
624 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100625 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100626 * \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 Zadikf2ec2882018-04-17 10:27:25 +0100634 * \note Alternative implementations of RSA need not support
635 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
636 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
637 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000638 * \param ctx The RSA context.
639 * \param f_rng The RNG function. Needed for padding and PKCS#1 v2.1
640 * encoding and #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +0100641 * \param p_rng The RNG context.
Rose Zadik042e97f2018-01-26 16:35:10 +0000642 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
643 * \param label The buffer holding the custom label to use.
644 * \param label_len The length of the label.
645 * \param ilen The length of the plaintext.
646 * \param input The buffer holding the data to encrypt.
647 * \param output The buffer used to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100648 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100649 * \return \c 0 on success.
650 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100651 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100653 int (*f_rng)(void *, unsigned char *, size_t),
654 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100655 int mode,
656 const unsigned char *label, size_t label_len,
657 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100658 const unsigned char *input,
659 unsigned char *output );
660
661/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000662 * \brief This function performs an RSA operation, then removes the
663 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000664 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000665 * It is the generic wrapper for performing a PKCS#1 decryption
666 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100668 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000669 * as large as the size \p ctx->len of \p ctx->N (for example,
670 * 128 Bytes if RSA-1024 is used) to be able to hold an
671 * arbitrary decrypted message. If it is not large enough to
672 * hold the decryption of the particular ciphertext provided,
673 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100674 *
675 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000676 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Rose Zadike8b5b992018-03-27 12:19:47 +0100677 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100678 * \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 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100683 * \note Alternative implementations of RSA need not support
684 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
685 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
686 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100687 * \param ctx The RSA context.
688 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
689 * \param p_rng The RNG context.
690 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
691 * \param olen The length of the plaintext.
692 * \param input The buffer holding the encrypted data.
693 * \param output The buffer used to hold the plaintext.
694 * \param output_max_len The maximum length of the output buffer.
695 *
696 * \return \c 0 on success.
697 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200700 int (*f_rng)(void *, unsigned char *, size_t),
701 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000702 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000703 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000704 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000705 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000706
707/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000708 * \brief This function performs a PKCS#1 v1.5 decryption
709 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100710 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100711 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000712 * as large as the size \p ctx->len of \p ctx->N, for example,
713 * 128 Bytes if RSA-1024 is used, to be able to hold an
714 * arbitrary decrypted message. If it is not large enough to
715 * hold the decryption of the particular ciphertext provided,
716 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100717 *
718 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000719 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Rose Zadike8b5b992018-03-27 12:19:47 +0100720 *
721 * \deprecated It is deprecated and discouraged to call this function
722 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
723 * are likely to remove the \p mode argument and have it
724 * implicitly set to #MBEDTLS_RSA_PRIVATE.
725 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100726 * \note Alternative implementations of RSA need not support
727 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
728 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
729 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100730 * \param ctx The RSA context.
731 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
732 * \param p_rng The RNG context.
733 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
734 * \param olen The length of the plaintext.
735 * \param input The buffer holding the encrypted data.
736 * \param output The buffer to hold the plaintext.
737 * \param output_max_len The maximum length of the output buffer.
738 *
739 * \return \c 0 on success.
740 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
741 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100742 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200744 int (*f_rng)(void *, unsigned char *, size_t),
745 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100746 int mode, size_t *olen,
747 const unsigned char *input,
748 unsigned char *output,
749 size_t output_max_len );
750
751/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100752 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
753 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100754 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100755 * \note The output buffer length \c output_max_len should be
756 * as large as the size \p ctx->len of \p ctx->N, for
757 * example, 128 Bytes if RSA-1024 is used, to be able to
758 * hold an arbitrary decrypted message. If it is not
759 * large enough to hold the decryption of the particular
760 * ciphertext provided, the function returns
761 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100762 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100763 * \note The input buffer must be as large as the size
764 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Rose Zadike8b5b992018-03-27 12:19:47 +0100765 *
766 * \deprecated It is deprecated and discouraged to call this function
767 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
768 * are likely to remove the \p mode argument and have it
769 * implicitly set to #MBEDTLS_RSA_PRIVATE.
770 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100771 * \note Alternative implementations of RSA need not support
772 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
773 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
774 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100775 * \param ctx The RSA context.
776 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
777 * \param p_rng The RNG context.
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.
Darryl Green11999bb2018-03-13 15:22:58 +0000784 * \param output_max_len The maximum length of the output buffer.
Rose Zadike8b5b992018-03-27 12:19:47 +0100785 *
786 * \return \c 0 on success.
787 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100788 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200790 int (*f_rng)(void *, unsigned char *, size_t),
791 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100792 int mode,
793 const unsigned char *label, size_t label_len,
794 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100795 const unsigned char *input,
796 unsigned char *output,
797 size_t output_max_len );
798
799/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000800 * \brief This function performs a private RSA operation to sign
801 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000802 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000803 * It is the generic wrapper for performing a PKCS#1
804 * signature using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000805 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000806 * \note The \p sig buffer must be as large as the size
807 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000808 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000809 * \note For PKCS#1 v2.1 encoding, see comments on
810 * mbedtls_rsa_rsassa_pss_sign() for details on
811 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100812 *
813 * \deprecated It is deprecated and discouraged to call this function
814 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
815 * are likely to remove the \p mode argument and have it
816 * implicitly set to #MBEDTLS_RSA_PRIVATE.
817 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100818 * \note Alternative implementations of RSA need not support
819 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
820 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
821 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100822 * \param ctx The RSA context.
823 * \param f_rng The RNG function. Needed for PKCS#1 v2.1 encoding and for
824 * #MBEDTLS_RSA_PRIVATE.
825 * \param p_rng The RNG context.
826 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
827 * \param md_alg The message-digest algorithm used to hash the original data.
828 * Use #MBEDTLS_MD_NONE for signing raw data.
829 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
830 * \param hash The buffer holding the message digest.
831 * \param sig The buffer to hold the ciphertext.
832 *
833 * \return \c 0 if the signing operation was successful.
834 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000835 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000837 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000838 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000839 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000841 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000842 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000843 unsigned char *sig );
844
845/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000846 * \brief This function performs a PKCS#1 v1.5 signature
847 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100848 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100849 * \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.
851 *
852 * \deprecated It is deprecated and discouraged to call this function
853 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
854 * are likely to remove the \p mode argument and have it
855 * implicitly set to #MBEDTLS_RSA_PRIVATE.
856 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100857 * \note Alternative implementations of RSA need not support
858 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
859 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
860 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000861 * \param ctx The RSA context.
862 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +0100863 * \param p_rng The RNG context.
Rose Zadik042e97f2018-01-26 16:35:10 +0000864 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
865 * \param md_alg The message-digest algorithm used to hash the original data.
866 * Use #MBEDTLS_MD_NONE for signing raw data.
867 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
868 * \param hash The buffer holding the message digest.
869 * \param sig The buffer to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100870 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100871 * \return \c 0 if the signing operation was successful.
872 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100873 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200875 int (*f_rng)(void *, unsigned char *, size_t),
876 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100877 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100879 unsigned int hashlen,
880 const unsigned char *hash,
881 unsigned char *sig );
882
883/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000884 * \brief This function performs a PKCS#1 v2.1 PSS signature
885 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100886 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000887 * \note The \p sig buffer must be as large as the size
888 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100889 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000890 * \note The \p hash_id in the RSA context is the one used for the
891 * encoding. \p md_alg in the function call is the type of hash
892 * that is encoded. According to <em>RFC-3447: Public-Key
893 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
894 * Specifications</em> it is advised to keep both hashes the
895 * same.
Rose Zadike8b5b992018-03-27 12:19:47 +0100896 *
897 * \deprecated It is deprecated and discouraged to call this function
898 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
899 * are likely to remove the \p mode argument and have it
900 * implicitly set to #MBEDTLS_RSA_PRIVATE.
901 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100902 * \note Alternative implementations of RSA need not support
903 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
904 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
905 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100906 * \param ctx The RSA context.
907 * \param f_rng The RNG function. Needed for PKCS#1 v2.1 encoding and for
908 * #MBEDTLS_RSA_PRIVATE.
909 * \param p_rng The RNG context.
910 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
911 * \param md_alg The message-digest algorithm used to hash the original data.
912 * Use #MBEDTLS_MD_NONE for signing raw data.
913 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
914 * \param hash The buffer holding the message digest.
915 * \param sig The buffer to hold the ciphertext.
916 *
917 * \return \c 0 if the signing operation was successful.
918 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100919 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100921 int (*f_rng)(void *, unsigned char *, size_t),
922 void *p_rng,
923 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100925 unsigned int hashlen,
926 const unsigned char *hash,
927 unsigned char *sig );
928
929/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000930 * \brief This function performs a public RSA operation and checks
931 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +0000932 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000933 * This is the generic wrapper for performing a PKCS#1
934 * verification using the mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000935 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000936 * \note The \p sig buffer must be as large as the size
937 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000938 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000939 * \note For PKCS#1 v2.1 encoding, see comments on
940 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
941 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100942 *
943 * \deprecated It is deprecated and discouraged to call this function
944 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
945 * are likely to remove the \p mode argument and have it
946 * set to #MBEDTLS_RSA_PUBLIC.
947 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100948 * \note Alternative implementations of RSA need not support
949 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
950 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
951 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100952 * \param ctx The RSA public key context.
953 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
954 * \param p_rng The RNG context.
955 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
956 * \param md_alg The message-digest algorithm used to hash the original data.
957 * Use #MBEDTLS_MD_NONE for signing raw data.
958 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
959 * \param hash The buffer holding the message digest.
960 * \param sig The buffer holding the ciphertext.
961 *
962 * \return \c 0 if the verify operation was successful.
963 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000964 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200966 int (*f_rng)(void *, unsigned char *, size_t),
967 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000968 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000970 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000971 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200972 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000973
974/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000975 * \brief This function performs a PKCS#1 v1.5 verification
976 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +0100977 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100978 * \note The \p sig buffer must be as large as the size
979 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
980 *
981 * \deprecated It is deprecated and discouraged to call this function
982 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
983 * are likely to remove the \p mode argument and have it
984 * set to #MBEDTLS_RSA_PUBLIC.
985 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100986 * \note Alternative implementations of RSA need not support
987 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
988 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
989 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000990 * \param ctx The RSA public key context.
991 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +0100992 * \param p_rng The RNG context.
Rose Zadik042e97f2018-01-26 16:35:10 +0000993 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
994 * \param md_alg The message-digest algorithm used to hash the original data.
995 * Use #MBEDTLS_MD_NONE for signing raw data.
996 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
997 * \param hash The buffer holding the message digest.
998 * \param sig The buffer holding the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100999 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001000 * \return \c 0 if the verify operation was successful.
1001 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001004 int (*f_rng)(void *, unsigned char *, size_t),
1005 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001006 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001008 unsigned int hashlen,
1009 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001010 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001011
1012/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001013 * \brief This function performs a PKCS#1 v2.1 PSS verification
1014 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001015 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001016 * The hash function for the MGF mask generating function
1017 * is that specified in the RSA context.
Paul Bakkerb3869132013-02-28 17:21:01 +01001018 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001019 * \note The \p sig buffer must be as large as the size
1020 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001021 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001022 * \note The \p hash_id in the RSA context is the one used for the
1023 * verification. \p md_alg in the function call is the type of
1024 * hash that is verified. According to <em>RFC-3447: Public-Key
1025 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1026 * Specifications</em> it is advised to keep both hashes the
1027 * same. If \p hash_id in the RSA context is unset,
1028 * the \p md_alg from the function call is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001029 *
1030 * \deprecated It is deprecated and discouraged to call this function
1031 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1032 * are likely to remove the \p mode argument and have it
1033 * implicitly set to #MBEDTLS_RSA_PUBLIC.
1034 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001035 * \note Alternative implementations of RSA need not support
1036 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
1037 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
1038 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001039 * \param ctx The RSA public key context.
1040 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
1041 * \param p_rng The RNG context.
1042 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1043 * \param md_alg The message-digest algorithm used to hash the original data.
1044 * Use #MBEDTLS_MD_NONE for signing raw data.
1045 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
1046 * \param hash The buffer holding the message digest.
1047 * \param sig The buffer holding the ciphertext.
1048 *
1049 * \return \c 0 if the verify operation was successful.
1050 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001053 int (*f_rng)(void *, unsigned char *, size_t),
1054 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001055 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001057 unsigned int hashlen,
1058 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001059 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001060
1061/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001062 * \brief This function performs a PKCS#1 v2.1 PSS verification
1063 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001064 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001065 * The hash function for the MGF mask generating function
1066 * is that specified in \p mgf1_hash_id.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001067 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001068 * \note The \p sig buffer must be as large as the size
1069 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001070 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001071 * \note The \p hash_id in the RSA context is ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001072 *
1073 * \param ctx The RSA public key context.
1074 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
1075 * \param p_rng The RNG context.
1076 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1077 * \param md_alg The message-digest algorithm used to hash the original data.
1078 * Use #MBEDTLS_MD_NONE for signing raw data.
1079 * \param hashlen The length of the message digest. Only used if \p md_alg is
1080 * #MBEDTLS_MD_NONE.
1081 * \param hash The buffer holding the message digest.
1082 * \param mgf1_hash_id The message digest used for mask generation.
1083 * \param expected_salt_len The length of the salt used in padding. Use
1084 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1085 * \param sig The buffer holding the ciphertext.
1086 *
1087 * \return \c 0 if the verify operation was successful.
1088 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001091 int (*f_rng)(void *, unsigned char *, size_t),
1092 void *p_rng,
1093 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001095 unsigned int hashlen,
1096 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001098 int expected_salt_len,
1099 const unsigned char *sig );
1100
1101/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001102 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001103 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001104 * \param dst The destination context.
1105 * \param src The source context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001106 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001107 * \return \c 0 on success.
1108 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001111
1112/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001113 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001114 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001115 * \param ctx The RSA Context to free.
Paul Bakker5121ce52009-01-03 21:22:43 +00001116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
Ron Eldorfa8f6352017-06-20 15:48:46 +03001119#if defined(MBEDTLS_SELF_TEST)
1120
Paul Bakker5121ce52009-01-03 21:22:43 +00001121/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001122 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001124 * \return \c 0 on success.
1125 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001128
Ron Eldorfa8f6352017-06-20 15:48:46 +03001129#endif /* MBEDTLS_SELF_TEST */
1130
Paul Bakker5121ce52009-01-03 21:22:43 +00001131#ifdef __cplusplus
1132}
1133#endif
1134
Paul Bakker5121ce52009-01-03 21:22:43 +00001135#endif /* rsa.h */