blob: 701fe8bed9274cbf6287e4a5b2204370e1ae6384 [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/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020013 * Copyright The Mbed TLS Contributors
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 Bakker5121ce52009-01-03 21:22:43 +000027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#ifndef MBEDTLS_RSA_H
29#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010032#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#endif
Paul Bakkered27a042013-04-18 22:46:23 +020036
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010037#include "mbedtls/bignum.h"
38#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_THREADING_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010041#include "mbedtls/threading.h"
Paul Bakkerc9965dc2013-09-29 14:58:17 +020042#endif
43
Paul Bakker13e2dfe2009-07-28 07:18:38 +000044/*
45 * RSA Error codes
46 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
48#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
49#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Rose Zadik042e97f2018-01-26 16:35:10 +000050#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 +020051#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
52#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
53#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
54#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
55#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030056
57/* MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is deprecated and should not be used.
58 */
Rose Zadik042e97f2018-01-26 16:35:10 +000059#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. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030060
61/* MBEDTLS_ERR_RSA_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010062#define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580 /**< RSA hardware accelerator failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000063
64/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020065 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000066 */
Rose Zadik042e97f2018-01-26 16:35:10 +000067#define MBEDTLS_RSA_PUBLIC 0 /**< Request private key operation. */
68#define MBEDTLS_RSA_PRIVATE 1 /**< Request public key operation. */
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Rose Zadike8b5b992018-03-27 12:19:47 +010070#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
71#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Rose Zadik042e97f2018-01-26 16:35:10 +000073#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
74#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +000075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020077
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020078/*
79 * The above constants may be used even if the RSA module is compile out,
80 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
81 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010082
Paul Bakker407a0da2013-06-27 14:29:21 +020083#ifdef __cplusplus
84extern "C" {
85#endif
86
Ron Eldor4e6d55d2018-02-07 16:36:15 +020087#if !defined(MBEDTLS_RSA_ALT)
88// Regular implementation
89//
90
Paul Bakker5121ce52009-01-03 21:22:43 +000091/**
Rose Zadik042e97f2018-01-26 16:35:10 +000092 * \brief The RSA context structure.
Hanno Becker5063cd22017-09-29 11:49:12 +010093 *
94 * \note Direct manipulation of the members of this structure
Rose Zadik042e97f2018-01-26 16:35:10 +000095 * is deprecated. All manipulation should instead be done through
96 * the public interface functions.
Paul Bakker5121ce52009-01-03 21:22:43 +000097 */
Dawid Drozd428cc522018-07-24 10:02:47 +020098typedef struct mbedtls_rsa_context
Paul Bakker5121ce52009-01-03 21:22:43 +000099{
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100100 int ver; /*!< Reserved for internal purposes.
101 * Do not set this field in application
102 * code. Its meaning might change without
103 * notice. */
Rose Zadik042e97f2018-01-26 16:35:10 +0000104 size_t len; /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Rose Zadike8b5b992018-03-27 12:19:47 +0100106 mbedtls_mpi N; /*!< The public modulus. */
107 mbedtls_mpi E; /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
Rose Zadike8b5b992018-03-27 12:19:47 +0100109 mbedtls_mpi D; /*!< The private exponent. */
110 mbedtls_mpi P; /*!< The first prime factor. */
111 mbedtls_mpi Q; /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100112
Rose Zadikf2ec2882018-04-17 10:27:25 +0100113 mbedtls_mpi DP; /*!< <code>D % (P - 1)</code>. */
114 mbedtls_mpi DQ; /*!< <code>D % (Q - 1)</code>. */
115 mbedtls_mpi QP; /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
Rose Zadikf2ec2882018-04-17 10:27:25 +0100117 mbedtls_mpi RN; /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
Rose Zadikf2ec2882018-04-17 10:27:25 +0100119 mbedtls_mpi RP; /*!< cached <code>R^2 mod P</code>. */
120 mbedtls_mpi RQ; /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200121
Rose Zadike8b5b992018-03-27 12:19:47 +0100122 mbedtls_mpi Vi; /*!< The cached blinding value. */
123 mbedtls_mpi Vf; /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000124
Rose Zadik042e97f2018-01-26 16:35:10 +0000125 int padding; /*!< Selects padding mode:
126 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
127 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
128 int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
129 as specified in md.h for use in the MGF
130 mask generating function used in the
131 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100133 /* Invariant: the mutex is initialized iff ver != 0. */
Rose Zadik042e97f2018-01-26 16:35:10 +0000134 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200135#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000136}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200139#else /* MBEDTLS_RSA_ALT */
140#include "rsa_alt.h"
141#endif /* MBEDTLS_RSA_ALT */
142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000144 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000146 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000147 * encryption scheme and the RSASSA-PSS signature scheme.
148 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000149 * \note The \p hash_id parameter is ignored when using
150 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200151 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000152 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200153 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000154 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100155 * a default value, which can be overridden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000156 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200157 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000158 * \note The hash selected in \p hash_id is always used for OEAP
159 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100160 * making signatures, but can be overridden for verifying them.
161 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100162 *
Hanno Becker9a467772018-12-13 09:54:59 +0000163 * \param ctx The RSA context to initialize. This must not be \c NULL.
164 * \param padding The padding mode to use. This must be either
165 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Hanno Becker385ce912018-12-13 18:33:12 +0000166 * \param hash_id The hash identifier of ::mbedtls_md_type_t type, if
Hanno Becker9a467772018-12-13 09:54:59 +0000167 * \p padding is #MBEDTLS_RSA_PKCS_V21. It is unused
168 * otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100171 int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000172 int hash_id );
Paul Bakker5121ce52009-01-03 21:22:43 +0000173
174/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000175 * \brief This function imports a set of core parameters into an
176 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100177 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100178 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000179 * imports, if the parameters are not simultaneously present.
180 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100181 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000182 * by a call to mbedtls_rsa_complete(), which checks and
183 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100184 * public or private RSA key.
185 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000186 * \note See mbedtls_rsa_complete() for more information on which
187 * parameters are necessary to set up a private or public
188 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100189 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100190 * \note The imported parameters are copied and need not be preserved
191 * for the lifetime of the RSA context being set up.
192 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100193 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000194 * \param N The RSA modulus. This may be \c NULL.
195 * \param P The first prime factor of \p N. This may be \c NULL.
196 * \param Q The second prime factor of \p N. This may be \c NULL.
197 * \param D The private exponent. This may be \c NULL.
198 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100199 *
200 * \return \c 0 on success.
201 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100202 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100203int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
204 const mbedtls_mpi *N,
205 const mbedtls_mpi *P, const mbedtls_mpi *Q,
206 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100207
208/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000209 * \brief This function imports core RSA parameters, in raw big-endian
210 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100212 * \note This function can be called multiple times for successive
213 * imports, if the parameters are not simultaneously present.
214 *
215 * Any sequence of calls to this function should be followed
216 * by a call to mbedtls_rsa_complete(), which checks and
217 * completes the provided information to a ready-for-use
218 * public or private RSA key.
219 *
220 * \note See mbedtls_rsa_complete() for more information on which
221 * parameters are necessary to set up a private or public
222 * RSA key.
223 *
224 * \note The imported parameters are copied and need not be preserved
225 * for the lifetime of the RSA context being set up.
226 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000227 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000228 * \param N The RSA modulus. This may be \c NULL.
229 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
230 * \param P The first prime factor of \p N. This may be \c NULL.
231 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
232 * \param Q The second prime factor of \p N. This may be \c NULL.
233 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
234 * \param D The private exponent. This may be \c NULL.
235 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
236 * \param E The public exponent. This may be \c NULL.
237 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100239 * \return \c 0 on success.
240 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100241 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100242int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100243 unsigned char const *N, size_t N_len,
244 unsigned char const *P, size_t P_len,
245 unsigned char const *Q, size_t Q_len,
246 unsigned char const *D, size_t D_len,
247 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100248
249/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000250 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100251 * a set of imported core parameters.
252 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000253 * To setup an RSA public key, precisely \p N and \p E
254 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100255 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000256 * To setup an RSA private key, sufficient information must
257 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100258 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000259 * The default implementation supports the following:
260 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
261 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
262 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100263 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000264 * If this function runs successfully, it guarantees that
265 * the RSA context can be used for RSA operations without
266 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100267 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100268 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000269 * for the imported parameters. In particular, parameters that
270 * are not needed by the implementation might be silently
271 * discarded and left unchecked. To check the consistency
272 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100273 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100274 * \param ctx The initialized RSA context holding imported parameters.
275 *
276 * \return \c 0 on success.
277 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
278 * failed.
279 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100280 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100281int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100282
283/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000284 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100285 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000286 * If this function runs successfully, the non-NULL buffers
287 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
288 * written, with additional unused space filled leading by
289 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100290 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000291 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300292 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000293 * <li>An alternative RSA implementation is in use, which
294 * stores the key externally, and either cannot or should
295 * not export it into RAM.</li>
296 * <li>A SW or HW implementation might not support a certain
297 * deduction. For example, \p P, \p Q from \p N, \p D,
298 * and \p E if the former are not part of the
299 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100300 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000301 * If the function fails due to an unsupported operation,
302 * the RSA context stays intact and remains usable.
303 *
304 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000305 * \param N The MPI to hold the RSA modulus.
306 * This may be \c NULL if this field need not be exported.
307 * \param P The MPI to hold the first prime factor of \p N.
308 * This may be \c NULL if this field need not be exported.
309 * \param Q The MPI to hold the second prime factor of \p N.
310 * This may be \c NULL if this field need not be exported.
311 * \param D The MPI to hold the private exponent.
312 * This may be \c NULL if this field need not be exported.
313 * \param E The MPI to hold the public exponent.
314 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000315 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100316 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300317 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000318 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100319 * functionality or because of security policies.
320 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100321 *
322 */
323int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
324 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
325 mbedtls_mpi *D, mbedtls_mpi *E );
326
327/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000328 * \brief This function exports core parameters of an RSA key
329 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100330 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000331 * If this function runs successfully, the non-NULL buffers
332 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
333 * written, with additional unused space filled leading by
334 * zero Bytes.
335 *
336 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300337 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000338 * <li>An alternative RSA implementation is in use, which
339 * stores the key externally, and either cannot or should
340 * not export it into RAM.</li>
341 * <li>A SW or HW implementation might not support a certain
342 * deduction. For example, \p P, \p Q from \p N, \p D,
343 * and \p E if the former are not part of the
344 * implementation.</li></ul>
345 * If the function fails due to an unsupported operation,
346 * the RSA context stays intact and remains usable.
347 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100348 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100349 * buffer pointers are NULL.
350 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000351 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000352 * \param N The Byte array to store the RSA modulus,
353 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000354 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000355 * \param P The Byte array to hold the first prime factor of \p N,
356 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000357 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000358 * \param Q The Byte array to hold the second prime factor of \p N,
359 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000360 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000361 * \param D The Byte array to hold the private exponent,
362 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000363 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000364 * \param E The Byte array to hold the public exponent,
365 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000366 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100367 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100368 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300369 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000370 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100371 * functionality or because of security policies.
372 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100373 */
374int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
375 unsigned char *N, size_t N_len,
376 unsigned char *P, size_t P_len,
377 unsigned char *Q, size_t Q_len,
378 unsigned char *D, size_t D_len,
379 unsigned char *E, size_t E_len );
380
381/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000382 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100383 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100384 * \note Alternative RSA implementations not using CRT-parameters
385 * internally can implement this function based on
386 * mbedtls_rsa_deduce_opt().
387 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000388 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000389 * \param DP The MPI to hold \c D modulo `P-1`,
390 * or \c NULL if it need not be exported.
391 * \param DQ The MPI to hold \c D modulo `Q-1`,
392 * or \c NULL if it need not be exported.
393 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
394 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100395 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100396 * \return \c 0 on success.
397 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100398 *
399 */
400int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
401 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
402
Paul Bakker5121ce52009-01-03 21:22:43 +0000403/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000404 * \brief This function sets padding for an already initialized RSA
405 * context. See mbedtls_rsa_init() for details.
Paul Bakker5121ce52009-01-03 21:22:43 +0000406 *
Hanno Becker9a467772018-12-13 09:54:59 +0000407 * \param ctx The initialized RSA context to be configured.
408 * \param padding The padding mode to use. This must be either
409 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Rose Zadik042e97f2018-01-26 16:35:10 +0000410 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Paul Bakker40e46942009-01-03 21:51:57 +0000411 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100412void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000413 int hash_id );
Paul Bakker21eb2802010-08-16 11:10:02 +0000414
Paul Bakkera3d195c2011-11-27 21:07:34 +0000415/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000416 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100417 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000418 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100419 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000420 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100421 *
422 */
423size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
424
425/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000426 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000428 * \note mbedtls_rsa_init() must be called before this function,
429 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 *
Hanno Becker9a467772018-12-13 09:54:59 +0000431 * \param ctx The initialized RSA context used to hold the key.
432 * \param f_rng The RNG function to be used for key generation.
433 * This must not be \c NULL.
434 * \param p_rng The RNG context to be passed to \p f_rng.
435 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100436 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000437 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000438 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100439 *
440 * \return \c 0 on success.
441 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100444 int (*f_rng)(void *, unsigned char *, size_t),
445 void *p_rng,
446 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000447
448/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000449 * \brief This function checks if a context contains at least an RSA
450 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000452 * If the function runs successfully, it is guaranteed that
453 * enough information is present to perform an RSA public key
454 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 *
Hanno Becker9a467772018-12-13 09:54:59 +0000456 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000457 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100458 * \return \c 0 on success.
459 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100460 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
464/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000465 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100466 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000467 *
Hanno Becker68767a62017-10-17 10:13:31 +0100468 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000469 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100470 * on the given context, but that the various parameters are
471 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000472 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100473 *
474 * \warning This function should catch accidental misconfigurations
475 * like swapping of parameters, but it cannot establish full
476 * trust in neither the quality nor the consistency of the key
477 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000478 * <ul><li>Consistency: Imported parameters that are irrelevant
479 * for the implementation might be silently dropped. If dropped,
480 * the current function does not have access to them,
481 * and therefore cannot check them. See mbedtls_rsa_complete().
482 * If you want to check the consistency of the entire
483 * content of an PKCS1-encoded RSA private key, for example, you
484 * should use mbedtls_rsa_validate_params() before setting
485 * up the RSA context.
486 * Additionally, if the implementation performs empirical checks,
487 * these checks substantiate but do not guarantee consistency.</li>
488 * <li>Quality: This function is not expected to perform
489 * extended quality assessments like checking that the prime
490 * factors are safe. Additionally, it is the responsibility of the
491 * user to ensure the trustworthiness of the source of his RSA
492 * parameters, which goes beyond what is effectively checkable
493 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100494 *
Hanno Becker9a467772018-12-13 09:54:59 +0000495 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100496 *
497 * \return \c 0 on success.
498 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000503 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100504 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000505 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100506 *
Hanno Becker9a467772018-12-13 09:54:59 +0000507 * \param pub The initialized RSA context holding the public key.
508 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000509 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100510 * \return \c 0 on success.
511 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100512 */
Hanno Becker98838b02017-10-02 13:16:10 +0100513int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
514 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100515
516/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000517 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 *
Hanno Becker9a467772018-12-13 09:54:59 +0000519 * \param ctx The initialized RSA context to use.
520 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000521 * of length \c ctx->len Bytes. For example, \c 256 Bytes
522 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000523 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000524 * of length \c ctx->len Bytes. For example, \c 256 Bytes
525 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000526 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000527 * \note This function does not handle message padding.
528 *
529 * \note Make sure to set \p input[0] = 0 or ensure that
530 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100532 * \return \c 0 on success.
533 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000536 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 unsigned char *output );
538
539/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000540 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000541 *
Hanno Becker24120612017-10-26 11:53:35 +0100542 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100543 *
544 * \note If blinding is used, both the base of exponentation
Hanno Becker24120612017-10-26 11:53:35 +0100545 * and the exponent are blinded, providing protection
546 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100547 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100548 * \warning It is deprecated and a security risk to not provide
549 * a PRNG here and thereby prevent the use of blinding.
550 * Future versions of the library may enforce the presence
551 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100552 *
Hanno Becker9a467772018-12-13 09:54:59 +0000553 * \param ctx The initialized RSA context to use.
554 * \param f_rng The RNG function, used for blinding. It is discouraged
555 * and deprecated to pass \c NULL here, in which case
556 * blinding will be omitted.
557 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
558 * if \p f_rng is \c NULL or if \p f_rng doesn't need a context.
559 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000560 * of length \c ctx->len Bytes. For example, \c 256 Bytes
561 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000562 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000563 * of length \c ctx->len Bytes. For example, \c 256 Bytes
564 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100565 *
566 * \return \c 0 on success.
567 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
568 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200571 int (*f_rng)(void *, unsigned char *, size_t),
572 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000573 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 unsigned char *output );
575
576/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000577 * \brief This function adds the message padding, then performs an RSA
578 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000580 * It is the generic wrapper for performing a PKCS#1 encryption
581 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000582 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100583 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000584 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
585 * are likely to remove the \p mode argument and have it
586 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100587 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100588 * \note Alternative implementations of RSA need not support
589 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300590 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100591 *
Hanno Becker9a467772018-12-13 09:54:59 +0000592 * \param ctx The initialized RSA context to use.
Hanno Becker974ca0d2018-12-18 18:03:24 +0000593 * \param f_rng The RNG to use. It is mandatory for PKCS#1 v2.1 padding
594 * encoding, and for PKCS#1 v1.5 padding encoding when used
595 * with \p mode set to #MBEDTLS_RSA_PUBLIC. For PKCS#1 v1.5
596 * padding encoding and \p mode set to #MBEDTLS_RSA_PRIVATE,
597 * it is used for blinding and should be provided in this
598 * case; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000599 * \param p_rng The RNG context to be passed to \p f_rng. May be
600 * \c NULL if \p f_rng is \c NULL or if \p f_rng doesn't
601 * need a context argument.
602 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000603 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000604 * \param ilen The length of the plaintext in Bytes.
605 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000606 * buffer of size \p ilen Bytes. It may be \c NULL if
607 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000608 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000609 * of length \c ctx->len Bytes. For example, \c 256 Bytes
610 * for an 2048-bit RSA modulus.
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 Bakker5121ce52009-01-03 21:22:43 +0000614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000616 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000617 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000618 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000619 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 unsigned char *output );
621
622/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000623 * \brief This function performs a PKCS#1 v1.5 encryption operation
624 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100625 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100626 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000627 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
628 * are likely to remove the \p mode argument and have it
629 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100630 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100631 * \note Alternative implementations of RSA need not support
632 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300633 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100634 *
Hanno Becker9a467772018-12-13 09:54:59 +0000635 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000636 * \param f_rng The RNG function to use. It is needed for padding generation
637 * if \p mode is #MBEDTLS_RSA_PUBLIC. If \p mode is
638 * #MBEDTLS_RSA_PRIVATE (discouraged), it is used for
639 * blinding and should be provided; see mbedtls_rsa_private().
Hanno Becker9a467772018-12-13 09:54:59 +0000640 * \param p_rng The RNG context to be passed to \p f_rng. This may
641 * be \c NULL if \p f_rng is \c NULL or if \p f_rng
642 * doesn't need a context argument.
643 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000644 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000645 * \param ilen The length of the plaintext in Bytes.
646 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000647 * buffer of size \p ilen Bytes. It may be \c NULL if
648 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000649 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000650 * of length \c ctx->len Bytes. For example, \c 256 Bytes
651 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100652 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100653 * \return \c 0 on success.
654 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100655 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100657 int (*f_rng)(void *, unsigned char *, size_t),
658 void *p_rng,
659 int mode, size_t ilen,
660 const unsigned char *input,
661 unsigned char *output );
662
663/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000664 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
665 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100666 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100667 * \note The output buffer must be as large as the size
668 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
669 *
670 * \deprecated It is deprecated and discouraged to call this function
671 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
672 * are likely to remove the \p mode argument and have it
673 * implicitly set to #MBEDTLS_RSA_PUBLIC.
674 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100675 * \note Alternative implementations of RSA need not support
676 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300677 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100678 *
Hanno Becker9a467772018-12-13 09:54:59 +0000679 * \param ctx The initnialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000680 * \param f_rng The RNG function to use. This is needed for padding
681 * generation and must be provided.
Hanno Becker9a467772018-12-13 09:54:59 +0000682 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000683 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000684 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000685 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000686 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000687 * This must be a readable buffer of length \p label_len
688 * Bytes. It may be \c NULL if \p label_len is \c 0.
689 * \param label_len The length of the label in Bytes.
690 * \param ilen The length of the plaintext buffer \p input in Bytes.
691 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000692 * buffer of size \p ilen Bytes. It may be \c NULL if
693 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000694 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000695 * of length \c ctx->len Bytes. For example, \c 256 Bytes
696 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100697 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100698 * \return \c 0 on success.
699 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100702 int (*f_rng)(void *, unsigned char *, size_t),
703 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100704 int mode,
705 const unsigned char *label, size_t label_len,
706 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100707 const unsigned char *input,
708 unsigned char *output );
709
710/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000711 * \brief This function performs an RSA operation, then removes the
712 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000713 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000714 * It is the generic wrapper for performing a PKCS#1 decryption
715 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000716 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100717 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000718 * as large as the size \p ctx->len of \p ctx->N (for example,
719 * 128 Bytes if RSA-1024 is used) to be able to hold an
720 * arbitrary decrypted message. If it is not large enough to
721 * hold the decryption of the particular ciphertext provided,
722 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100723 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100724 * \deprecated It is deprecated and discouraged to call this function
725 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
726 * are likely to remove the \p mode argument and have it
727 * implicitly set to #MBEDTLS_RSA_PRIVATE.
728 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100729 * \note Alternative implementations of RSA need not support
730 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300731 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100732 *
Hanno Becker9a467772018-12-13 09:54:59 +0000733 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000734 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
735 * this is used for blinding and should be provided; see
736 * mbedtls_rsa_private() for more. If \p mode is
737 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000738 * \param p_rng The RNG context to be passed to \p f_rng. This may be
739 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
740 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000741 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000742 * \param olen The address at which to store the length of
743 * the plaintext. This must not be \c NULL.
744 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000745 * of length \c ctx->len Bytes. For example, \c 256 Bytes
746 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000747 * \param output The buffer used to hold the plaintext. This must
748 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000749 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100750 *
751 * \return \c 0 on success.
752 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000753 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200755 int (*f_rng)(void *, unsigned char *, size_t),
756 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000757 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000758 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000759 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000760 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000761
762/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000763 * \brief This function performs a PKCS#1 v1.5 decryption
764 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100765 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100766 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000767 * as large as the size \p ctx->len of \p ctx->N, for example,
768 * 128 Bytes if RSA-1024 is used, to be able to hold an
769 * arbitrary decrypted message. If it is not large enough to
770 * hold the decryption of the particular ciphertext provided,
771 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100772 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100773 * \deprecated It is deprecated and discouraged to call this function
774 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
775 * are likely to remove the \p mode argument and have it
776 * implicitly set to #MBEDTLS_RSA_PRIVATE.
777 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100778 * \note Alternative implementations of RSA need not support
779 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300780 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100781 *
Hanno Becker9a467772018-12-13 09:54:59 +0000782 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000783 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
784 * this is used for blinding and should be provided; see
785 * mbedtls_rsa_private() for more. If \p mode is
786 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000787 * \param p_rng The RNG context to be passed to \p f_rng. This may be
788 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
789 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000790 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000791 * \param olen The address at which to store the length of
792 * the plaintext. This must not be \c NULL.
793 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000794 * of length \c ctx->len Bytes. For example, \c 256 Bytes
795 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000796 * \param output The buffer used to hold the plaintext. This must
797 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000798 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100799 *
800 * \return \c 0 on success.
801 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
802 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100803 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200805 int (*f_rng)(void *, unsigned char *, size_t),
806 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100807 int mode, size_t *olen,
808 const unsigned char *input,
809 unsigned char *output,
810 size_t output_max_len );
811
812/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100813 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
814 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100815 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100816 * \note The output buffer length \c output_max_len should be
817 * as large as the size \p ctx->len of \p ctx->N, for
818 * example, 128 Bytes if RSA-1024 is used, to be able to
819 * hold an arbitrary decrypted message. If it is not
820 * large enough to hold the decryption of the particular
821 * ciphertext provided, the function returns
822 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100823 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100824 * \deprecated It is deprecated and discouraged to call this function
825 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
826 * are likely to remove the \p mode argument and have it
827 * implicitly set to #MBEDTLS_RSA_PRIVATE.
828 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100829 * \note Alternative implementations of RSA need not support
830 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300831 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100832 *
Hanno Becker9a467772018-12-13 09:54:59 +0000833 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000834 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
835 * this is used for blinding and should be provided; see
836 * mbedtls_rsa_private() for more. If \p mode is
837 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000838 * \param p_rng The RNG context to be passed to \p f_rng. This may be
839 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
840 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000841 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100842 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000843 * This must be a readable buffer of length \p label_len
844 * Bytes. It may be \c NULL if \p label_len is \c 0.
845 * \param label_len The length of the label in Bytes.
846 * \param olen The address at which to store the length of
847 * the plaintext. This must not be \c NULL.
848 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000849 * of length \c ctx->len Bytes. For example, \c 256 Bytes
850 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000851 * \param output The buffer used to hold the plaintext. This must
852 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000853 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100854 *
855 * \return \c 0 on success.
856 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100857 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200859 int (*f_rng)(void *, unsigned char *, size_t),
860 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100861 int mode,
862 const unsigned char *label, size_t label_len,
863 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100864 const unsigned char *input,
865 unsigned char *output,
866 size_t output_max_len );
867
868/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000869 * \brief This function performs a private RSA operation to sign
870 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000871 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000872 * It is the generic wrapper for performing a PKCS#1
873 * signature using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000874 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000875 * \note The \p sig buffer must be as large as the size
876 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000877 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000878 * \note For PKCS#1 v2.1 encoding, see comments on
879 * mbedtls_rsa_rsassa_pss_sign() for details on
880 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100881 *
882 * \deprecated It is deprecated and discouraged to call this function
883 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
884 * are likely to remove the \p mode argument and have it
885 * implicitly set to #MBEDTLS_RSA_PRIVATE.
886 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100887 * \note Alternative implementations of RSA need not support
888 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300889 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100890 *
Hanno Becker9a467772018-12-13 09:54:59 +0000891 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000892 * \param f_rng The RNG function to use. If the padding mode is PKCS#1 v2.1,
893 * this must be provided. If the padding mode is PKCS#1 v1.5 and
894 * \p mode is #MBEDTLS_RSA_PRIVATE, it is used for blinding
895 * and should be provided; see mbedtls_rsa_private() for more
896 * more. It is ignored otherwise.
Hanno Becker9a467772018-12-13 09:54:59 +0000897 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
898 * if \p f_rng is \c NULL or doesn't need a context argument.
899 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000900 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100901 * \param md_alg The message-digest algorithm used to hash the original data.
902 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000903 * \param hashlen The length of the message digest.
904 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
905 * \param hash The buffer holding the message digest or raw data.
906 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
907 * buffer of length \p hashlen Bytes. If \p md_alg is not
908 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
909 * the size of the hash corresponding to \p md_alg.
910 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000911 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100912 * for an 2048-bit RSA modulus. A buffer length of
913 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100914 *
915 * \return \c 0 if the signing operation was successful.
916 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000917 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000919 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000920 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000921 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000923 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000924 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000925 unsigned char *sig );
926
927/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000928 * \brief This function performs a PKCS#1 v1.5 signature
929 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100930 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100931 * \deprecated It is deprecated and discouraged to call this function
932 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
933 * are likely to remove the \p mode argument and have it
934 * implicitly set to #MBEDTLS_RSA_PRIVATE.
935 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100936 * \note Alternative implementations of RSA need not support
937 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300938 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100939 *
Hanno Becker9a467772018-12-13 09:54:59 +0000940 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000941 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
942 * this is used for blinding and should be provided; see
943 * mbedtls_rsa_private() for more. If \p mode is
944 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000945 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
946 * if \p f_rng is \c NULL or doesn't need a context argument.
947 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000948 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000949 * \param md_alg The message-digest algorithm used to hash the original data.
950 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000951 * \param hashlen The length of the message digest.
952 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
953 * \param hash The buffer holding the message digest or raw data.
954 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
955 * buffer of length \p hashlen Bytes. If \p md_alg is not
956 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
957 * the size of the hash corresponding to \p md_alg.
958 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000959 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100960 * for an 2048-bit RSA modulus. A buffer length of
961 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100962 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100963 * \return \c 0 if the signing operation was successful.
964 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100965 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200967 int (*f_rng)(void *, unsigned char *, size_t),
968 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100969 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100971 unsigned int hashlen,
972 const unsigned char *hash,
973 unsigned char *sig );
974
975/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000976 * \brief This function performs a PKCS#1 v2.1 PSS signature
977 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100978 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000979 * \note The \p hash_id in the RSA context is the one used for the
980 * encoding. \p md_alg in the function call is the type of hash
981 * that is encoded. According to <em>RFC-3447: Public-Key
982 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
983 * Specifications</em> it is advised to keep both hashes the
984 * same.
Rose Zadike8b5b992018-03-27 12:19:47 +0100985 *
Jaeden Amero3725bb22018-09-07 19:12:36 +0100986 * \note This function always uses the maximum possible salt size,
987 * up to the length of the payload hash. This choice of salt
988 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
989 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
990 * minimum salt size which is the hash size minus 2 bytes. If
991 * this minimum size is too large given the key size (the salt
992 * size, plus the hash size, plus 2 bytes must be no more than
993 * the key size in bytes), this function returns
994 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
995 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100996 * \deprecated It is deprecated and discouraged to call this function
997 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
998 * are likely to remove the \p mode argument and have it
999 * implicitly set to #MBEDTLS_RSA_PRIVATE.
1000 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001001 * \note Alternative implementations of RSA need not support
1002 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001003 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001004 *
Hanno Becker9a467772018-12-13 09:54:59 +00001005 * \param ctx The initialized RSA context to use.
1006 * \param f_rng The RNG function. It must not be \c NULL.
1007 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
1008 * if \p f_rng doesn't need a context argument.
1009 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001010 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001011 * \param md_alg The message-digest algorithm used to hash the original data.
1012 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001013 * \param hashlen The length of the message digest.
1014 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
1015 * \param hash The buffer holding the message digest or raw data.
1016 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1017 * buffer of length \p hashlen Bytes. If \p md_alg is not
1018 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1019 * the size of the hash corresponding to \p md_alg.
1020 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +00001021 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +01001022 * for an 2048-bit RSA modulus. A buffer length of
1023 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +01001024 *
1025 * \return \c 0 if the signing operation was successful.
1026 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001029 int (*f_rng)(void *, unsigned char *, size_t),
1030 void *p_rng,
1031 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001033 unsigned int hashlen,
1034 const unsigned char *hash,
1035 unsigned char *sig );
1036
1037/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001038 * \brief This function performs a public RSA operation and checks
1039 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +00001040 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001041 * This is the generic wrapper for performing a PKCS#1
1042 * verification using the mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001044 * \note For PKCS#1 v2.1 encoding, see comments on
1045 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
1046 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +01001047 *
1048 * \deprecated It is deprecated and discouraged to call this function
1049 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1050 * are likely to remove the \p mode argument and have it
1051 * set to #MBEDTLS_RSA_PUBLIC.
1052 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001053 * \note Alternative implementations of RSA need not support
1054 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001055 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001056 *
Hanno Becker9a467772018-12-13 09:54:59 +00001057 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001058 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1059 * this is used for blinding and should be provided; see
1060 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001061 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1062 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1063 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001064 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001065 * \param md_alg The message-digest algorithm used to hash the original data.
1066 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001067 * \param hashlen The length of the message digest.
1068 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1069 * \param hash The buffer holding the message digest or raw data.
1070 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1071 * buffer of length \p hashlen Bytes. If \p md_alg is not
1072 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1073 * the size of the hash corresponding to \p md_alg.
1074 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001075 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1076 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001077 *
1078 * \return \c 0 if the verify operation was successful.
1079 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001082 int (*f_rng)(void *, unsigned char *, size_t),
1083 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001084 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001086 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001087 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001088 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +00001089
1090/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001091 * \brief This function performs a PKCS#1 v1.5 verification
1092 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001093 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001094 * \deprecated It is deprecated and discouraged to call this function
1095 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1096 * are likely to remove the \p mode argument and have it
1097 * set to #MBEDTLS_RSA_PUBLIC.
1098 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001099 * \note Alternative implementations of RSA need not support
1100 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001101 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001102 *
Hanno Becker9a467772018-12-13 09:54:59 +00001103 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001104 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1105 * this is used for blinding and should be provided; see
1106 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001107 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1108 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1109 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001110 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +00001111 * \param md_alg The message-digest algorithm used to hash the original data.
1112 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001113 * \param hashlen The length of the message digest.
1114 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1115 * \param hash The buffer holding the message digest or raw data.
1116 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1117 * buffer of length \p hashlen Bytes. If \p md_alg is not
1118 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1119 * the size of the hash corresponding to \p md_alg.
1120 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001121 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1122 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001123 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001124 * \return \c 0 if the verify operation was successful.
1125 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001128 int (*f_rng)(void *, unsigned char *, size_t),
1129 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001130 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001132 unsigned int hashlen,
1133 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001134 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001135
1136/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001137 * \brief This function performs a PKCS#1 v2.1 PSS verification
1138 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001139 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001140 * The hash function for the MGF mask generating function
1141 * is that specified in the RSA context.
Paul Bakkerb3869132013-02-28 17:21:01 +01001142 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001143 * \note The \p hash_id in the RSA context is the one used for the
1144 * verification. \p md_alg in the function call is the type of
1145 * hash that is verified. According to <em>RFC-3447: Public-Key
1146 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1147 * Specifications</em> it is advised to keep both hashes the
1148 * same. If \p hash_id in the RSA context is unset,
1149 * the \p md_alg from the function call is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001150 *
1151 * \deprecated It is deprecated and discouraged to call this function
1152 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1153 * are likely to remove the \p mode argument and have it
1154 * implicitly set to #MBEDTLS_RSA_PUBLIC.
1155 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001156 * \note Alternative implementations of RSA need not support
1157 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001158 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001159 *
Hanno Becker9a467772018-12-13 09:54:59 +00001160 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001161 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1162 * this is used for blinding and should be provided; see
1163 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001164 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1165 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1166 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001167 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001168 * \param md_alg The message-digest algorithm used to hash the original data.
1169 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001170 * \param hashlen The length of the message digest.
1171 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1172 * \param hash The buffer holding the message digest or raw data.
1173 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1174 * buffer of length \p hashlen Bytes. If \p md_alg is not
1175 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1176 * the size of the hash corresponding to \p md_alg.
1177 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001178 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1179 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001180 *
1181 * \return \c 0 if the verify operation was successful.
1182 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001183 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001185 int (*f_rng)(void *, unsigned char *, size_t),
1186 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001187 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001189 unsigned int hashlen,
1190 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001191 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001192
1193/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001194 * \brief This function performs a PKCS#1 v2.1 PSS verification
1195 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001196 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001197 * The hash function for the MGF mask generating function
1198 * is that specified in \p mgf1_hash_id.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001199 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001200 * \note The \p sig buffer must be as large as the size
1201 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001202 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001203 * \note The \p hash_id in the RSA context is ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001204 *
Hanno Becker9a467772018-12-13 09:54:59 +00001205 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001206 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1207 * this is used for blinding and should be provided; see
1208 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001209 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1210 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1211 * \param mode The mode of operation. This must be either
1212 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +01001213 * \param md_alg The message-digest algorithm used to hash the original data.
1214 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001215 * \param hashlen The length of the message digest.
1216 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1217 * \param hash The buffer holding the message digest or raw data.
1218 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1219 * buffer of length \p hashlen Bytes. If \p md_alg is not
1220 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1221 * the size of the hash corresponding to \p md_alg.
1222 * \param mgf1_hash_id The message digest used for mask generation.
1223 * \param expected_salt_len The length of the salt used in padding. Use
1224 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1225 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001226 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1227 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001228 *
1229 * \return \c 0 if the verify operation was successful.
1230 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001233 int (*f_rng)(void *, unsigned char *, size_t),
1234 void *p_rng,
1235 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001237 unsigned int hashlen,
1238 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001240 int expected_salt_len,
1241 const unsigned char *sig );
1242
1243/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001244 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001245 *
Hanno Becker9a467772018-12-13 09:54:59 +00001246 * \param dst The destination context. This must be initialized.
1247 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001248 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001249 * \return \c 0 on success.
1250 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001253
1254/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001255 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001256 *
Hanno Becker9a467772018-12-13 09:54:59 +00001257 * \param ctx The RSA context to free. May be \c NULL, in which case
1258 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001259 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001262
Ron Eldorfa8f6352017-06-20 15:48:46 +03001263#if defined(MBEDTLS_SELF_TEST)
1264
Paul Bakker5121ce52009-01-03 21:22:43 +00001265/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001266 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001267 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001268 * \return \c 0 on success.
1269 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001272
Ron Eldorfa8f6352017-06-20 15:48:46 +03001273#endif /* MBEDTLS_SELF_TEST */
1274
Paul Bakker5121ce52009-01-03 21:22:43 +00001275#ifdef __cplusplus
1276}
1277#endif
1278
Paul Bakker5121ce52009-01-03 21:22:43 +00001279#endif /* rsa.h */