blob: 9fb28101d8d310ffd5f5ab24930e2825342bd4c9 [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
Mateusz Starzyk846f0212021-05-19 19:44:07 +020030#include "mbedtls/private_access.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Bence Szépkútic662b362021-05-27 11:25:03 +020032#include "mbedtls/build_info.h"
Paul Bakkered27a042013-04-18 22:46:23 +020033
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010034#include "mbedtls/bignum.h"
35#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_THREADING_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020038# include "mbedtls/threading.h"
Paul Bakkerc9965dc2013-09-29 14:58:17 +020039#endif
40
Paul Bakker13e2dfe2009-07-28 07:18:38 +000041/*
42 * RSA Error codes
43 */
Gilles Peskined2971572021-07-26 18:48:10 +020044/** Bad input parameters to function. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020045#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080
Gilles Peskined2971572021-07-26 18:48:10 +020046/** Input data contains invalid padding and is rejected. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020047#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100
Gilles Peskined2971572021-07-26 18:48:10 +020048/** Something failed during generation of a key. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020049#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180
Gilles Peskined2971572021-07-26 18:48:10 +020050/** Key failed to pass the validity check of the library. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020051#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200
Gilles Peskined2971572021-07-26 18:48:10 +020052/** The public key operation failed. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020053#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280
Gilles Peskined2971572021-07-26 18:48:10 +020054/** The private key operation failed. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020055#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300
Gilles Peskined2971572021-07-26 18:48:10 +020056/** The PKCS#1 verification failed. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020057#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380
Gilles Peskined2971572021-07-26 18:48:10 +020058/** The output buffer for decryption is not large enough. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020059#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400
Gilles Peskined2971572021-07-26 18:48:10 +020060/** The random generator failed to generate non-zeros. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020061#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480
Ron Eldor9924bdc2018-10-04 10:59:13 +030062
Paul Bakker5121ce52009-01-03 21:22:43 +000063/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020064 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000065 */
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Mateusz Starzyk16fec332021-07-22 16:43:35 +020067/** Use PKCS#1 v1.5 encoding. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020068#define MBEDTLS_RSA_PKCS_V15 0
Mateusz Starzyk16fec332021-07-22 16:43:35 +020069/** Use PKCS#1 v2.1 encoding. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020070#define MBEDTLS_RSA_PKCS_V21 1
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Mateusz Starzyk16fec332021-07-22 16:43:35 +020072/** Identifier for RSA signature operations. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020073#define MBEDTLS_RSA_SIGN 1
Mateusz Starzyk16fec332021-07-22 16:43:35 +020074/** Identifier for RSA encryption and decryption operations. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020075#define MBEDTLS_RSA_CRYPT 2
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020077#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020078
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020079/*
80 * The above constants may be used even if the RSA module is compile out,
81 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
82 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010083
Paul Bakker407a0da2013-06-27 14:29:21 +020084#ifdef __cplusplus
85extern "C" {
86#endif
87
Ron Eldor4e6d55d2018-02-07 16:36:15 +020088#if !defined(MBEDTLS_RSA_ALT)
89// Regular implementation
90//
91
Paul Bakker5121ce52009-01-03 21:22:43 +000092/**
Rose Zadik042e97f2018-01-26 16:35:10 +000093 * \brief The RSA context structure.
Paul Bakker5121ce52009-01-03 21:22:43 +000094 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020095typedef struct mbedtls_rsa_context {
96 int MBEDTLS_PRIVATE(ver); /*!< Reserved for internal purposes.
97 * Do not set this field in application
98 * code. Its meaning might change without
99 * notice. */
100 size_t MBEDTLS_PRIVATE(len); /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200102 mbedtls_mpi MBEDTLS_PRIVATE(N); /*!< The public modulus. */
103 mbedtls_mpi MBEDTLS_PRIVATE(E); /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200105 mbedtls_mpi MBEDTLS_PRIVATE(D); /*!< The private exponent. */
106 mbedtls_mpi MBEDTLS_PRIVATE(P); /*!< The first prime factor. */
107 mbedtls_mpi MBEDTLS_PRIVATE(Q); /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100108
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200109 mbedtls_mpi MBEDTLS_PRIVATE(DP); /*!< <code>D % (P - 1)</code>. */
110 mbedtls_mpi MBEDTLS_PRIVATE(DQ); /*!< <code>D % (Q - 1)</code>. */
111 mbedtls_mpi MBEDTLS_PRIVATE(QP); /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200113 mbedtls_mpi MBEDTLS_PRIVATE(RN); /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200115 mbedtls_mpi MBEDTLS_PRIVATE(RP); /*!< cached <code>R^2 mod P</code>. */
116 mbedtls_mpi MBEDTLS_PRIVATE(RQ); /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200117
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200118 mbedtls_mpi MBEDTLS_PRIVATE(Vi); /*!< The cached blinding value. */
119 mbedtls_mpi MBEDTLS_PRIVATE(Vf); /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000120
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200121 int MBEDTLS_PRIVATE(padding); /*!< Selects padding mode:
122 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
123 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
124 int MBEDTLS_PRIVATE(hash_id); /*!< Hash identifier of mbedtls_md_type_t
125 type, as specified in md.h for use in the MGF mask
126 generating function used in the EME-OAEP and EMSA-PSS
127 encodings. */
128# if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100129 /* Invariant: the mutex is initialized iff ver != 0. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200130 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex); /*!< Thread-safety mutex.
131 */
132# endif
133} mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200135#else /* MBEDTLS_RSA_ALT */
136# include "rsa_alt.h"
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200137#endif /* MBEDTLS_RSA_ALT */
138
Paul Bakker5121ce52009-01-03 21:22:43 +0000139/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000140 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200142 * \note This function initializes the padding and the hash
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200143 * identifier to respectively #MBEDTLS_RSA_PKCS_V15 and
144 * #MBEDTLS_MD_NONE. See mbedtls_rsa_set_padding() for more
145 * information about those parameters.
Ronald Cronc1905a12021-06-05 11:11:14 +0200146 *
147 * \param ctx The RSA context to initialize. This must not be \c NULL.
148 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200149void mbedtls_rsa_init(mbedtls_rsa_context *ctx);
Ronald Cronc1905a12021-06-05 11:11:14 +0200150
151/**
152 * \brief This function sets padding for an already initialized RSA
153 * context.
154 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000155 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000156 * encryption scheme and the RSASSA-PSS signature scheme.
157 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000158 * \note The \p hash_id parameter is ignored when using
159 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200160 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200161 * \note The choice of padding mode is strictly enforced for private
162 * key operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000163 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100164 * a default value, which can be overridden by calling specific
Ronald Cronc1905a12021-06-05 11:11:14 +0200165 * \c mbedtls_rsa_rsaes_xxx or \c mbedtls_rsa_rsassa_xxx
166 * functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200167 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000168 * \note The hash selected in \p hash_id is always used for OEAP
169 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100170 * making signatures, but can be overridden for verifying them.
171 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100172 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200173 * \param ctx The initialized RSA context to be configured.
Hanno Becker9a467772018-12-13 09:54:59 +0000174 * \param padding The padding mode to use. This must be either
175 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200176 * \param hash_id The hash identifier for PSS or OAEP, if \p padding is
177 * #MBEDTLS_RSA_PKCS_V21. #MBEDTLS_MD_NONE is accepted by this
178 * function but may be not suitable for some operations.
179 * Ignored if \p padding is #MBEDTLS_RSA_PKCS_V15.
Ronald Cronc1905a12021-06-05 11:11:14 +0200180 *
181 * \return \c 0 on success.
182 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING failure:
183 * \p padding or \p hash_id is invalid.
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200185int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx,
186 int padding,
187 mbedtls_md_type_t hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
189/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000190 * \brief This function imports a set of core parameters into an
191 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100192 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100193 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000194 * imports, if the parameters are not simultaneously present.
195 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100196 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000197 * by a call to mbedtls_rsa_complete(), which checks and
198 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100199 * public or private RSA key.
200 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000201 * \note See mbedtls_rsa_complete() for more information on which
202 * parameters are necessary to set up a private or public
203 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100204 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100205 * \note The imported parameters are copied and need not be preserved
206 * for the lifetime of the RSA context being set up.
207 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100208 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000209 * \param N The RSA modulus. This may be \c NULL.
210 * \param P The first prime factor of \p N. This may be \c NULL.
211 * \param Q The second prime factor of \p N. This may be \c NULL.
212 * \param D The private exponent. This may be \c NULL.
213 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100214 *
215 * \return \c 0 on success.
216 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100217 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200218int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
219 const mbedtls_mpi *N,
220 const mbedtls_mpi *P,
221 const mbedtls_mpi *Q,
222 const mbedtls_mpi *D,
223 const mbedtls_mpi *E);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100224
225/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000226 * \brief This function imports core RSA parameters, in raw big-endian
227 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100229 * \note This function can be called multiple times for successive
230 * imports, if the parameters are not simultaneously present.
231 *
232 * Any sequence of calls to this function should be followed
233 * by a call to mbedtls_rsa_complete(), which checks and
234 * completes the provided information to a ready-for-use
235 * public or private RSA key.
236 *
237 * \note See mbedtls_rsa_complete() for more information on which
238 * parameters are necessary to set up a private or public
239 * RSA key.
240 *
241 * \note The imported parameters are copied and need not be preserved
242 * for the lifetime of the RSA context being set up.
243 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000244 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000245 * \param N The RSA modulus. This may be \c NULL.
246 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
247 * \param P The first prime factor of \p N. This may be \c NULL.
248 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
249 * \param Q The second prime factor of \p N. This may be \c NULL.
250 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
251 * \param D The private exponent. This may be \c NULL.
252 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
253 * \param E The public exponent. This may be \c NULL.
254 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100256 * \return \c 0 on success.
257 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100258 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200259int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
260 unsigned char const *N,
261 size_t N_len,
262 unsigned char const *P,
263 size_t P_len,
264 unsigned char const *Q,
265 size_t Q_len,
266 unsigned char const *D,
267 size_t D_len,
268 unsigned char const *E,
269 size_t E_len);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100270
271/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000272 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100273 * a set of imported core parameters.
274 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000275 * To setup an RSA public key, precisely \p N and \p E
276 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100277 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000278 * To setup an RSA private key, sufficient information must
279 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100280 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000281 * The default implementation supports the following:
282 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
283 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
284 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100285 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000286 * If this function runs successfully, it guarantees that
287 * the RSA context can be used for RSA operations without
288 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100289 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100290 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000291 * for the imported parameters. In particular, parameters that
292 * are not needed by the implementation might be silently
293 * discarded and left unchecked. To check the consistency
294 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100295 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100296 * \param ctx The initialized RSA context holding imported parameters.
297 *
298 * \return \c 0 on success.
299 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
300 * failed.
301 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100302 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200303int mbedtls_rsa_complete(mbedtls_rsa_context *ctx);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100304
305/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000306 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100307 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000308 * If this function runs successfully, the non-NULL buffers
309 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
310 * written, with additional unused space filled leading by
311 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100312 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000313 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300314 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000315 * <li>An alternative RSA implementation is in use, which
316 * stores the key externally, and either cannot or should
317 * not export it into RAM.</li>
318 * <li>A SW or HW implementation might not support a certain
319 * deduction. For example, \p P, \p Q from \p N, \p D,
320 * and \p E if the former are not part of the
321 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100322 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000323 * If the function fails due to an unsupported operation,
324 * the RSA context stays intact and remains usable.
325 *
326 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000327 * \param N The MPI to hold the RSA modulus.
328 * This may be \c NULL if this field need not be exported.
329 * \param P The MPI to hold the first prime factor of \p N.
330 * This may be \c NULL if this field need not be exported.
331 * \param Q The MPI to hold the second prime factor of \p N.
332 * This may be \c NULL if this field need not be exported.
333 * \param D The MPI to hold the private exponent.
334 * This may be \c NULL if this field need not be exported.
335 * \param E The MPI to hold the public exponent.
336 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000337 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100338 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300339 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000340 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100341 * functionality or because of security policies.
342 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100343 *
344 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200345int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
346 mbedtls_mpi *N,
347 mbedtls_mpi *P,
348 mbedtls_mpi *Q,
349 mbedtls_mpi *D,
350 mbedtls_mpi *E);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100351
352/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000353 * \brief This function exports core parameters of an RSA key
354 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100355 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000356 * If this function runs successfully, the non-NULL buffers
357 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
358 * written, with additional unused space filled leading by
359 * zero Bytes.
360 *
361 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300362 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000363 * <li>An alternative RSA implementation is in use, which
364 * stores the key externally, and either cannot or should
365 * not export it into RAM.</li>
366 * <li>A SW or HW implementation might not support a certain
367 * deduction. For example, \p P, \p Q from \p N, \p D,
368 * and \p E if the former are not part of the
369 * implementation.</li></ul>
370 * If the function fails due to an unsupported operation,
371 * the RSA context stays intact and remains usable.
372 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100373 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100374 * buffer pointers are NULL.
375 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000376 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000377 * \param N The Byte array to store the RSA modulus,
378 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000379 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000380 * \param P The Byte array to hold the first prime factor of \p N,
381 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000382 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000383 * \param Q The Byte array to hold the second prime factor of \p N,
384 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000385 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000386 * \param D The Byte array to hold the private exponent,
387 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000388 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000389 * \param E The Byte array to hold the public exponent,
390 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000391 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100392 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100393 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300394 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000395 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100396 * functionality or because of security policies.
397 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100398 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200399int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
400 unsigned char *N,
401 size_t N_len,
402 unsigned char *P,
403 size_t P_len,
404 unsigned char *Q,
405 size_t Q_len,
406 unsigned char *D,
407 size_t D_len,
408 unsigned char *E,
409 size_t E_len);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100410
411/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000412 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100413 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100414 * \note Alternative RSA implementations not using CRT-parameters
415 * internally can implement this function based on
416 * mbedtls_rsa_deduce_opt().
417 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000418 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000419 * \param DP The MPI to hold \c D modulo `P-1`,
420 * or \c NULL if it need not be exported.
421 * \param DQ The MPI to hold \c D modulo `Q-1`,
422 * or \c NULL if it need not be exported.
423 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
424 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100425 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100426 * \return \c 0 on success.
427 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100428 *
429 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200430int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
431 mbedtls_mpi *DP,
432 mbedtls_mpi *DQ,
433 mbedtls_mpi *QP);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100434
Paul Bakker5121ce52009-01-03 21:22:43 +0000435/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000436 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100437 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000438 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100439 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000440 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100441 *
442 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200443size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx);
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100444
445/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000446 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000447 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000448 * \note mbedtls_rsa_init() must be called before this function,
449 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000450 *
Hanno Becker9a467772018-12-13 09:54:59 +0000451 * \param ctx The initialized RSA context used to hold the key.
452 * \param f_rng The RNG function to be used for key generation.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100453 * This is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000454 * \param p_rng The RNG context to be passed to \p f_rng.
455 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100456 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000457 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000458 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100459 *
460 * \return \c 0 on success.
461 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000462 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200463int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
464 int (*f_rng)(void *, unsigned char *, size_t),
465 void *p_rng,
466 unsigned int nbits,
467 int exponent);
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
469/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000470 * \brief This function checks if a context contains at least an RSA
471 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000473 * If the function runs successfully, it is guaranteed that
474 * enough information is present to perform an RSA public key
475 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 *
Hanno Becker9a467772018-12-13 09:54:59 +0000477 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000478 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100479 * \return \c 0 on success.
480 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100481 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000482 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200483int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
485/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000486 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100487 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 *
Hanno Becker68767a62017-10-17 10:13:31 +0100489 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000490 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100491 * on the given context, but that the various parameters are
492 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000493 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100494 *
495 * \warning This function should catch accidental misconfigurations
496 * like swapping of parameters, but it cannot establish full
497 * trust in neither the quality nor the consistency of the key
498 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000499 * <ul><li>Consistency: Imported parameters that are irrelevant
500 * for the implementation might be silently dropped. If dropped,
501 * the current function does not have access to them,
502 * and therefore cannot check them. See mbedtls_rsa_complete().
503 * If you want to check the consistency of the entire
504 * content of an PKCS1-encoded RSA private key, for example, you
505 * should use mbedtls_rsa_validate_params() before setting
506 * up the RSA context.
507 * Additionally, if the implementation performs empirical checks,
508 * these checks substantiate but do not guarantee consistency.</li>
509 * <li>Quality: This function is not expected to perform
510 * extended quality assessments like checking that the prime
511 * factors are safe. Additionally, it is the responsibility of the
512 * user to ensure the trustworthiness of the source of his RSA
513 * parameters, which goes beyond what is effectively checkable
514 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100515 *
Hanno Becker9a467772018-12-13 09:54:59 +0000516 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100517 *
518 * \return \c 0 on success.
519 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200521int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000522
523/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000524 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100525 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000526 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100527 *
Hanno Becker9a467772018-12-13 09:54:59 +0000528 * \param pub The initialized RSA context holding the public key.
529 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000530 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100531 * \return \c 0 on success.
532 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100533 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200534int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
535 const mbedtls_rsa_context *prv);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100536
537/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000538 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000539 *
Hanno Becker9a467772018-12-13 09:54:59 +0000540 * \param ctx The initialized RSA context to use.
541 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000542 * of length \c ctx->len Bytes. For example, \c 256 Bytes
543 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000544 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000545 * of length \c ctx->len Bytes. For example, \c 256 Bytes
546 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000547 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000548 * \note This function does not handle message padding.
549 *
550 * \note Make sure to set \p input[0] = 0 or ensure that
551 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100553 * \return \c 0 on success.
554 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000555 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200556int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
557 const unsigned char *input,
558 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
560/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000561 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 *
Hanno Becker24120612017-10-26 11:53:35 +0100563 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100564 *
565 * \note If blinding is used, both the base of exponentation
Hanno Becker24120612017-10-26 11:53:35 +0100566 * and the exponent are blinded, providing protection
567 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100568 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100569 * \warning It is deprecated and a security risk to not provide
570 * a PRNG here and thereby prevent the use of blinding.
571 * Future versions of the library may enforce the presence
572 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100573 *
Hanno Becker9a467772018-12-13 09:54:59 +0000574 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100575 * \param f_rng The RNG function, used for blinding. It is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000576 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
Thomas Daubney03412782021-05-20 15:31:17 +0100577 * if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000578 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000579 * of length \c ctx->len Bytes. For example, \c 256 Bytes
580 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000581 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000582 * of length \c ctx->len Bytes. For example, \c 256 Bytes
583 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100584 *
585 * \return \c 0 on success.
586 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
587 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200589int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
590 int (*f_rng)(void *, unsigned char *, size_t),
591 void *p_rng,
592 const unsigned char *input,
593 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
595/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000596 * \brief This function adds the message padding, then performs an RSA
597 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000598 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000599 * It is the generic wrapper for performing a PKCS#1 encryption
Thomas Daubney21772772021-05-13 17:30:32 +0100600 * operation.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100601 *
Hanno Becker9a467772018-12-13 09:54:59 +0000602 * \param ctx The initialized RSA context to use.
Thomas Daubneyf54c5c52021-05-21 17:00:30 +0100603 * \param f_rng The RNG to use. It is used for padding generation
Thomas Daubney2c65db92021-05-21 10:58:28 +0100604 * and it is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000605 * \param p_rng The RNG context to be passed to \p f_rng. May be
Thomas Daubney03412782021-05-20 15:31:17 +0100606 * \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000607 * \param ilen The length of the plaintext in Bytes.
608 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000609 * buffer of size \p ilen Bytes. It may be \c NULL if
610 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000611 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000612 * of length \c ctx->len Bytes. For example, \c 256 Bytes
613 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100614 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100615 * \return \c 0 on success.
616 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200618int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
619 int (*f_rng)(void *, unsigned char *, size_t),
620 void *p_rng,
621 size_t ilen,
622 const unsigned char *input,
623 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000624
625/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000626 * \brief This function performs a PKCS#1 v1.5 encryption operation
627 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100628 *
Hanno Becker9a467772018-12-13 09:54:59 +0000629 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100630 * \param f_rng The RNG function to use. It is mandatory and used for
631 * padding generation.
Hanno Becker9a467772018-12-13 09:54:59 +0000632 * \param p_rng The RNG context to be passed to \p f_rng. This may
Thomas Daubney03412782021-05-20 15:31:17 +0100633 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000634 * \param ilen The length of the plaintext in Bytes.
635 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000636 * buffer of size \p ilen Bytes. It may be \c NULL if
637 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000638 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000639 * of length \c ctx->len Bytes. For example, \c 256 Bytes
640 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100641 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100642 * \return \c 0 on success.
643 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100644 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200645int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
646 int (*f_rng)(void *,
647 unsigned char *,
648 size_t),
649 void *p_rng,
650 size_t ilen,
651 const unsigned char *input,
652 unsigned char *output);
Paul Bakkerb3869132013-02-28 17:21:01 +0100653
654/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000655 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
656 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100657 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100658 * \note The output buffer must be as large as the size
659 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
660 *
Hanno Becker9a467772018-12-13 09:54:59 +0000661 * \param ctx The initnialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000662 * \param f_rng The RNG function to use. This is needed for padding
Thomas Daubney2c65db92021-05-21 10:58:28 +0100663 * generation and is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000664 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000665 * be \c NULL if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000666 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000667 * This must be a readable buffer of length \p label_len
668 * Bytes. It may be \c NULL if \p label_len is \c 0.
669 * \param label_len The length of the label in Bytes.
670 * \param ilen The length of the plaintext buffer \p input in Bytes.
671 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000672 * buffer of size \p ilen Bytes. It may be \c NULL if
673 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000674 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000675 * of length \c ctx->len Bytes. For example, \c 256 Bytes
676 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100677 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100678 * \return \c 0 on success.
679 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100680 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200681int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
682 int (*f_rng)(void *, unsigned char *, size_t),
683 void *p_rng,
684 const unsigned char *label,
685 size_t label_len,
686 size_t ilen,
687 const unsigned char *input,
688 unsigned char *output);
Paul Bakkerb3869132013-02-28 17:21:01 +0100689
690/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000691 * \brief This function performs an RSA operation, then removes the
692 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000694 * It is the generic wrapper for performing a PKCS#1 decryption
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100695 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100697 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000698 * as large as the size \p ctx->len of \p ctx->N (for example,
699 * 128 Bytes if RSA-1024 is used) to be able to hold an
700 * arbitrary decrypted message. If it is not large enough to
701 * hold the decryption of the particular ciphertext provided,
702 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100703 *
Hanno Becker9a467772018-12-13 09:54:59 +0000704 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100705 * \param f_rng The RNG function. This is used for blinding and is
706 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000707 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100708 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000709 * \param olen The address at which to store the length of
710 * the plaintext. This must not be \c NULL.
711 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000712 * of length \c ctx->len Bytes. For example, \c 256 Bytes
713 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000714 * \param output The buffer used to hold the plaintext. This must
715 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000716 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100717 *
718 * \return \c 0 on success.
719 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000720 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200721int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
722 int (*f_rng)(void *, unsigned char *, size_t),
723 void *p_rng,
724 size_t *olen,
725 const unsigned char *input,
726 unsigned char *output,
727 size_t output_max_len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000728
729/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000730 * \brief This function performs a PKCS#1 v1.5 decryption
731 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100732 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100733 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000734 * as large as the size \p ctx->len of \p ctx->N, for example,
735 * 128 Bytes if RSA-1024 is used, to be able to hold an
736 * arbitrary decrypted message. If it is not large enough to
737 * hold the decryption of the particular ciphertext provided,
738 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100739 *
Hanno Becker9a467772018-12-13 09:54:59 +0000740 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100741 * \param f_rng The RNG function. This is used for blinding and is
742 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000743 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100744 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000745 * \param olen The address at which to store the length of
746 * the plaintext. This must not be \c NULL.
747 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000748 * of length \c ctx->len Bytes. For example, \c 256 Bytes
749 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000750 * \param output The buffer used to hold the plaintext. This must
751 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000752 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100753 *
754 * \return \c 0 on success.
755 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
756 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100757 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200758int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
759 int (*f_rng)(void *,
760 unsigned char *,
761 size_t),
762 void *p_rng,
763 size_t *olen,
764 const unsigned char *input,
765 unsigned char *output,
766 size_t output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +0100767
768/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100769 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
770 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100771 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100772 * \note The output buffer length \c output_max_len should be
773 * as large as the size \p ctx->len of \p ctx->N, for
774 * example, 128 Bytes if RSA-1024 is used, to be able to
775 * hold an arbitrary decrypted message. If it is not
776 * large enough to hold the decryption of the particular
777 * ciphertext provided, the function returns
778 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100779 *
Hanno Becker9a467772018-12-13 09:54:59 +0000780 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100781 * \param f_rng The RNG function. This is used for blinding and is
782 * mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000783 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100784 * \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100785 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000786 * This must be a readable buffer of length \p label_len
787 * Bytes. It may be \c NULL if \p label_len is \c 0.
788 * \param label_len The length of the label in Bytes.
789 * \param olen The address at which to store the length of
790 * the plaintext. This must not be \c NULL.
791 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000792 * of length \c ctx->len Bytes. For example, \c 256 Bytes
793 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000794 * \param output The buffer used to hold the plaintext. This must
795 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000796 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100797 *
798 * \return \c 0 on success.
799 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100800 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200801int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
802 int (*f_rng)(void *, unsigned char *, size_t),
803 void *p_rng,
804 const unsigned char *label,
805 size_t label_len,
806 size_t *olen,
807 const unsigned char *input,
808 unsigned char *output,
809 size_t output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +0100810
811/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000812 * \brief This function performs a private RSA operation to sign
813 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000814 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000815 * It is the generic wrapper for performing a PKCS#1
Thomas Daubney140184d2021-05-18 16:04:07 +0100816 * signature.
Paul Bakker5121ce52009-01-03 21:22:43 +0000817 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000818 * \note The \p sig buffer must be as large as the size
819 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000820 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000821 * \note For PKCS#1 v2.1 encoding, see comments on
822 * mbedtls_rsa_rsassa_pss_sign() for details on
823 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100824 *
Hanno Becker9a467772018-12-13 09:54:59 +0000825 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100826 * \param f_rng The RNG function to use. This is mandatory and
827 * must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000828 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
Thomas Daubney03412782021-05-20 15:31:17 +0100829 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100830 * \param md_alg The message-digest algorithm used to hash the original data.
831 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200832 * \param hashlen The length of the message digest or raw data in Bytes.
833 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
834 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000835 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200836 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000837 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000838 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100839 * for an 2048-bit RSA modulus. A buffer length of
840 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100841 *
842 * \return \c 0 if the signing operation was successful.
843 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000844 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200845int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
846 int (*f_rng)(void *, unsigned char *, size_t),
847 void *p_rng,
848 mbedtls_md_type_t md_alg,
849 unsigned int hashlen,
850 const unsigned char *hash,
851 unsigned char *sig);
Paul Bakker5121ce52009-01-03 21:22:43 +0000852
853/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000854 * \brief This function performs a PKCS#1 v1.5 signature
855 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100856 *
Hanno Becker9a467772018-12-13 09:54:59 +0000857 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100858 * \param f_rng The RNG function. This is used for blinding and is
859 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000860 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
Thomas Daubney2c65db92021-05-21 10:58:28 +0100861 * if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000862 * \param md_alg The message-digest algorithm used to hash the original data.
863 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200864 * \param hashlen The length of the message digest or raw data in Bytes.
865 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
866 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000867 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200868 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000869 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000870 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100871 * for an 2048-bit RSA modulus. A buffer length of
872 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100873 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100874 * \return \c 0 if the signing operation was successful.
875 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100876 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200877int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
878 int (*f_rng)(void *,
879 unsigned char *,
880 size_t),
881 void *p_rng,
882 mbedtls_md_type_t md_alg,
883 unsigned int hashlen,
884 const unsigned char *hash,
885 unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +0100886
887/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000888 * \brief This function performs a PKCS#1 v2.1 PSS signature
889 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100890 *
Janos Follathb7953322021-04-01 14:44:17 +0100891 * \note The \c hash_id set in \p ctx by calling
892 * mbedtls_rsa_set_padding() selects the hash used for the
893 * encoding operation and for the mask generation function
894 * (MGF1). For more details on the encoding operation and the
895 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +0000896 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100897 * Specifications</em>.
Rose Zadike8b5b992018-03-27 12:19:47 +0100898 *
Cédric Meuter010ddc22020-04-25 09:24:11 +0200899 * \note This function enforces that the provided salt length complies
900 * with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 v2.2) §9.1.1
901 * step 3. The constraint is that the hash length plus the salt
902 * length plus 2 bytes must be at most the key length. If this
903 * constraint is not met, this function returns
Jaeden Amero3725bb22018-09-07 19:12:36 +0100904 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
905 *
Hanno Becker9a467772018-12-13 09:54:59 +0000906 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100907 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000908 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
909 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100910 * \param md_alg The message-digest algorithm used to hash the original data.
911 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200912 * \param hashlen The length of the message digest or raw data in Bytes.
913 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
914 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000915 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200916 * This must be a readable buffer of at least \p hashlen Bytes.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200917 * \param saltlen The length of the salt that should be used.
Cédric Meuter010ddc22020-04-25 09:24:11 +0200918 * If passed #MBEDTLS_RSA_SALT_LEN_ANY, the function will use
919 * the largest possible salt length up to the hash length,
920 * which is the largest permitted by some standards including
921 * FIPS 186-4 §5.5.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200922 * \param sig The buffer to hold the signature. This must be a writable
923 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
924 * for an 2048-bit RSA modulus. A buffer length of
925 * #MBEDTLS_MPI_MAX_SIZE is always safe.
926 *
927 * \return \c 0 if the signing operation was successful.
928 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
929 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200930int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
931 int (*f_rng)(void *,
932 unsigned char *,
933 size_t),
934 void *p_rng,
935 mbedtls_md_type_t md_alg,
936 unsigned int hashlen,
937 const unsigned char *hash,
938 int saltlen,
939 unsigned char *sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200940
941/**
942 * \brief This function performs a PKCS#1 v2.1 PSS signature
943 * operation (RSASSA-PSS-SIGN).
944 *
Janos Follathb7953322021-04-01 14:44:17 +0100945 * \note The \c hash_id set in \p ctx by calling
946 * mbedtls_rsa_set_padding() selects the hash used for the
947 * encoding operation and for the mask generation function
948 * (MGF1). For more details on the encoding operation and the
949 * mask generation function, consult <em>RFC-3447: Public-Key
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200950 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100951 * Specifications</em>.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200952 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000953 * \note This function always uses the maximum possible salt size,
954 * up to the length of the payload hash. This choice of salt
955 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
956 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
Rose Zadike8b5b992018-03-27 12:19:47 +0100957 * minimum salt size which is the hash size minus 2 bytes. If
958 * this minimum size is too large given the key size (the salt
959 * size, plus the hash size, plus 2 bytes must be no more than
960 * the key size in bytes), this function returns
961 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
962 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100963 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100964 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100965 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
966 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100967 * \param md_alg The message-digest algorithm used to hash the original data.
968 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200969 * \param hashlen The length of the message digest or raw data in Bytes.
970 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
971 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000972 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200973 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000974 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000975 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100976 * for an 2048-bit RSA modulus. A buffer length of
977 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100978 *
979 * \return \c 0 if the signing operation was successful.
980 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100981 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200982int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
983 int (*f_rng)(void *, unsigned char *, size_t),
984 void *p_rng,
985 mbedtls_md_type_t md_alg,
986 unsigned int hashlen,
987 const unsigned char *hash,
988 unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +0100989
990/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000991 * \brief This function performs a public RSA operation and checks
992 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +0000993 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000994 * This is the generic wrapper for performing a PKCS#1
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100995 * verification.
Paul Bakker5121ce52009-01-03 21:22:43 +0000996 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000997 * \note For PKCS#1 v2.1 encoding, see comments on
998 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
999 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +01001000 *
Hanno Becker9a467772018-12-13 09:54:59 +00001001 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001002 * \param md_alg The message-digest algorithm used to hash the original data.
1003 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001004 * \param hashlen The length of the message digest or raw data in Bytes.
1005 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1006 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001007 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001008 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001009 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001010 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1011 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001012 *
1013 * \return \c 0 if the verify operation was successful.
1014 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001015 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001016int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
1017 mbedtls_md_type_t md_alg,
1018 unsigned int hashlen,
1019 const unsigned char *hash,
1020 const unsigned char *sig);
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
1022/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001023 * \brief This function performs a PKCS#1 v1.5 verification
1024 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001025 *
Hanno Becker9a467772018-12-13 09:54:59 +00001026 * \param ctx The initialized RSA public key context to use.
Rose Zadik042e97f2018-01-26 16:35:10 +00001027 * \param md_alg The message-digest algorithm used to hash the original data.
1028 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001029 * \param hashlen The length of the message digest or raw data in Bytes.
1030 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1031 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001032 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001033 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001034 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001035 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1036 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001037 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001038 * \return \c 0 if the verify operation was successful.
1039 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001040 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001041int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
1042 mbedtls_md_type_t md_alg,
1043 unsigned int hashlen,
1044 const unsigned char *hash,
1045 const unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001046
1047/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001048 * \brief This function performs a PKCS#1 v2.1 PSS verification
1049 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001050 *
Janos Follathb7953322021-04-01 14:44:17 +01001051 * \note The \c hash_id set in \p ctx by calling
1052 * mbedtls_rsa_set_padding() selects the hash used for the
1053 * encoding operation and for the mask generation function
1054 * (MGF1). For more details on the encoding operation and the
1055 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +00001056 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +01001057 * Specifications</em>. If the \c hash_id set in \p ctx by
1058 * mbedtls_rsa_set_padding() is #MBEDTLS_MD_NONE, the \p md_alg
1059 * parameter is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001060 *
Hanno Becker9a467772018-12-13 09:54:59 +00001061 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001062 * \param md_alg The message-digest algorithm used to hash the original data.
1063 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001064 * \param hashlen The length of the message digest or raw data in Bytes.
1065 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1066 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001067 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001068 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001069 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001070 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1071 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001072 *
1073 * \return \c 0 if the verify operation was successful.
1074 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001075 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001076int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
1077 mbedtls_md_type_t md_alg,
1078 unsigned int hashlen,
1079 const unsigned char *hash,
1080 const unsigned char *sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001081
1082/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001083 * \brief This function performs a PKCS#1 v2.1 PSS verification
1084 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001085 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001086 * \note The \p sig buffer must be as large as the size
1087 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001088 *
Janos Follathb7953322021-04-01 14:44:17 +01001089 * \note The \c hash_id set in \p ctx by mbedtls_rsa_set_padding() is
1090 * ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001091 *
Hanno Becker9a467772018-12-13 09:54:59 +00001092 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001093 * \param md_alg The message-digest algorithm used to hash the original data.
1094 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001095 * \param hashlen The length of the message digest or raw data in Bytes.
1096 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1097 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001098 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001099 * This must be a readable buffer of at least \p hashlen Bytes.
Janos Follathb7953322021-04-01 14:44:17 +01001100 * \param mgf1_hash_id The message digest algorithm used for the
1101 * verification operation and the mask generation
1102 * function (MGF1). For more details on the encoding
1103 * operation and the mask generation function, consult
1104 * <em>RFC-3447: Public-Key Cryptography Standards
1105 * (PKCS) #1 v2.1: RSA Cryptography
1106 * Specifications</em>.
Hanno Becker9a467772018-12-13 09:54:59 +00001107 * \param expected_salt_len The length of the salt used in padding. Use
1108 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1109 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001110 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1111 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001112 *
1113 * \return \c 0 if the verify operation was successful.
1114 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001115 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001116int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
1117 mbedtls_md_type_t md_alg,
1118 unsigned int hashlen,
1119 const unsigned char *hash,
1120 mbedtls_md_type_t mgf1_hash_id,
1121 int expected_salt_len,
1122 const unsigned char *sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001123
1124/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001125 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001126 *
Hanno Becker9a467772018-12-13 09:54:59 +00001127 * \param dst The destination context. This must be initialized.
1128 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001129 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001130 * \return \c 0 on success.
1131 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001132 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001133int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src);
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001134
1135/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001136 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001137 *
Hanno Becker9a467772018-12-13 09:54:59 +00001138 * \param ctx The RSA context to free. May be \c NULL, in which case
1139 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001140 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001141 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001142void mbedtls_rsa_free(mbedtls_rsa_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00001143
Ron Eldorfa8f6352017-06-20 15:48:46 +03001144#if defined(MBEDTLS_SELF_TEST)
1145
Paul Bakker5121ce52009-01-03 21:22:43 +00001146/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001147 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001148 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001149 * \return \c 0 on success.
1150 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001151 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001152int mbedtls_rsa_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
Ron Eldorfa8f6352017-06-20 15:48:46 +03001154#endif /* MBEDTLS_SELF_TEST */
1155
Paul Bakker5121ce52009-01-03 21:22:43 +00001156#ifdef __cplusplus
1157}
1158#endif
1159
Paul Bakker5121ce52009-01-03 21:22:43 +00001160#endif /* rsa.h */