blob: 90da0f980432f1e3df228b2d0724adfb5da1be96 [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)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010038#include "mbedtls/threading.h"
Paul Bakkerc9965dc2013-09-29 14:58:17 +020039#endif
40
Przemyslaw Stekielb6a66502021-12-09 11:11:54 +010041#if defined(PSA_CRYPTO_DRIVER_TEST)
42#include <psa/crypto_driver_common.h>
43#include <psa/crypto.h>
44
45typedef struct {
46 /* If non-null, on success, copy this to the output. */
47 void *forced_output;
48 size_t forced_output_length;
49 /* If not PSA_SUCCESS, return this error code instead of processing the
50 * function call. */
51 psa_status_t forced_status;
52 /* Count the amount of times one of the rsa driver functions is called. */
53 unsigned long hits;
54} mbedtls_test_driver_rsa_hooks_t;
55
56#define MBEDTLS_TEST_DRIVER_RSA_INIT { NULL, 0, PSA_SUCCESS, 0 }
57
58static inline mbedtls_test_driver_rsa_hooks_t
59 mbedtls_test_driver_rsa_hooks_init( void )
60{
61 const mbedtls_test_driver_rsa_hooks_t v = MBEDTLS_TEST_DRIVER_RSA_INIT;
62 return( v );
63}
64
65extern mbedtls_test_driver_rsa_hooks_t mbedtls_test_driver_rsa_hooks;
66
67psa_status_t mbedtls_test_transparent_asymmetric_encrypt(
68 const psa_key_attributes_t *attributes,
69 const uint8_t *key_buffer,
70 size_t key_buffer_size,
71 psa_algorithm_t alg,
72 const uint8_t *input,
73 size_t input_length,
74 const uint8_t *salt,
75 size_t salt_length,
76 uint8_t *output,
77 size_t output_size,
78 size_t *output_length );
79
80psa_status_t mbedtls_test_opaque_asymmetric_encrypt(
81 const psa_key_attributes_t *attributes,
82 const uint8_t *key,
83 size_t key_length,
84 psa_algorithm_t alg,
85 const uint8_t *input,
86 size_t input_length,
87 const uint8_t *salt,
88 size_t salt_length,
89 uint8_t *output,
90 size_t output_size,
91 size_t *output_length);
92
93#endif /* PSA_CRYPTO_DRIVER_TEST */
94
Paul Bakker13e2dfe2009-07-28 07:18:38 +000095/*
96 * RSA Error codes
97 */
Gilles Peskined2971572021-07-26 18:48:10 +020098/** Bad input parameters to function. */
99#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080
100/** Input data contains invalid padding and is rejected. */
101#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100
102/** Something failed during generation of a key. */
103#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180
104/** Key failed to pass the validity check of the library. */
105#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200
106/** The public key operation failed. */
107#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280
108/** The private key operation failed. */
109#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300
110/** The PKCS#1 verification failed. */
111#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380
112/** The output buffer for decryption is not large enough. */
113#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400
114/** The random generator failed to generate non-zeros. */
115#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480
Ron Eldor9924bdc2018-10-04 10:59:13 +0300116
Paul Bakker5121ce52009-01-03 21:22:43 +0000117/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200118 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
Rose Zadike8b5b992018-03-27 12:19:47 +0100121#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
122#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
Rose Zadik042e97f2018-01-26 16:35:10 +0000124#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
125#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200128
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +0200129/*
130 * The above constants may be used even if the RSA module is compile out,
131 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
132 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +0100133
Paul Bakker407a0da2013-06-27 14:29:21 +0200134#ifdef __cplusplus
135extern "C" {
136#endif
137
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200138#if !defined(MBEDTLS_RSA_ALT)
139// Regular implementation
140//
141
Paul Bakker5121ce52009-01-03 21:22:43 +0000142/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000143 * \brief The RSA context structure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200145typedef struct mbedtls_rsa_context
Paul Bakker5121ce52009-01-03 21:22:43 +0000146{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200147 int MBEDTLS_PRIVATE(ver); /*!< Reserved for internal purposes.
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100148 * Do not set this field in application
149 * code. Its meaning might change without
150 * notice. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200151 size_t MBEDTLS_PRIVATE(len); /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200153 mbedtls_mpi MBEDTLS_PRIVATE(N); /*!< The public modulus. */
154 mbedtls_mpi MBEDTLS_PRIVATE(E); /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200156 mbedtls_mpi MBEDTLS_PRIVATE(D); /*!< The private exponent. */
157 mbedtls_mpi MBEDTLS_PRIVATE(P); /*!< The first prime factor. */
158 mbedtls_mpi MBEDTLS_PRIVATE(Q); /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100159
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200160 mbedtls_mpi MBEDTLS_PRIVATE(DP); /*!< <code>D % (P - 1)</code>. */
161 mbedtls_mpi MBEDTLS_PRIVATE(DQ); /*!< <code>D % (Q - 1)</code>. */
162 mbedtls_mpi MBEDTLS_PRIVATE(QP); /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200164 mbedtls_mpi MBEDTLS_PRIVATE(RN); /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200166 mbedtls_mpi MBEDTLS_PRIVATE(RP); /*!< cached <code>R^2 mod P</code>. */
167 mbedtls_mpi MBEDTLS_PRIVATE(RQ); /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200168
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200169 mbedtls_mpi MBEDTLS_PRIVATE(Vi); /*!< The cached blinding value. */
170 mbedtls_mpi MBEDTLS_PRIVATE(Vf); /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000171
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200172 int MBEDTLS_PRIVATE(padding); /*!< Selects padding mode:
Rose Zadik042e97f2018-01-26 16:35:10 +0000173 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
174 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200175 int MBEDTLS_PRIVATE(hash_id); /*!< Hash identifier of mbedtls_md_type_t type,
Rose Zadik042e97f2018-01-26 16:35:10 +0000176 as specified in md.h for use in the MGF
177 mask generating function used in the
178 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100180 /* Invariant: the mutex is initialized iff ver != 0. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200181 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex); /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200182#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000183}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200186#else /* MBEDTLS_RSA_ALT */
187#include "rsa_alt.h"
188#endif /* MBEDTLS_RSA_ALT */
189
Paul Bakker5121ce52009-01-03 21:22:43 +0000190/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000191 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200193 * \note This function initializes the padding and the hash
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200194 * identifier to respectively #MBEDTLS_RSA_PKCS_V15 and
195 * #MBEDTLS_MD_NONE. See mbedtls_rsa_set_padding() for more
196 * information about those parameters.
Ronald Cronc1905a12021-06-05 11:11:14 +0200197 *
198 * \param ctx The RSA context to initialize. This must not be \c NULL.
199 */
200void mbedtls_rsa_init( mbedtls_rsa_context *ctx );
201
202/**
203 * \brief This function sets padding for an already initialized RSA
204 * context.
205 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000206 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000207 * encryption scheme and the RSASSA-PSS signature scheme.
208 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000209 * \note The \p hash_id parameter is ignored when using
210 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200211 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200212 * \note The choice of padding mode is strictly enforced for private
213 * key operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000214 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100215 * a default value, which can be overridden by calling specific
Ronald Cronc1905a12021-06-05 11:11:14 +0200216 * \c mbedtls_rsa_rsaes_xxx or \c mbedtls_rsa_rsassa_xxx
217 * functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200218 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000219 * \note The hash selected in \p hash_id is always used for OEAP
220 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100221 * making signatures, but can be overridden for verifying them.
222 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100223 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200224 * \param ctx The initialized RSA context to be configured.
Hanno Becker9a467772018-12-13 09:54:59 +0000225 * \param padding The padding mode to use. This must be either
226 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200227 * \param hash_id The hash identifier for PSS or OAEP, if \p padding is
228 * #MBEDTLS_RSA_PKCS_V21. #MBEDTLS_MD_NONE is accepted by this
229 * function but may be not suitable for some operations.
230 * Ignored if \p padding is #MBEDTLS_RSA_PKCS_V15.
Ronald Cronc1905a12021-06-05 11:11:14 +0200231 *
232 * \return \c 0 on success.
233 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING failure:
234 * \p padding or \p hash_id is invalid.
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 */
Ronald Cronc1905a12021-06-05 11:11:14 +0200236int mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
237 mbedtls_md_type_t hash_id );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
239/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000240 * \brief This function imports a set of core parameters into an
241 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100242 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100243 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000244 * imports, if the parameters are not simultaneously present.
245 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100246 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000247 * by a call to mbedtls_rsa_complete(), which checks and
248 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100249 * public or private RSA key.
250 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000251 * \note See mbedtls_rsa_complete() for more information on which
252 * parameters are necessary to set up a private or public
253 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100254 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100255 * \note The imported parameters are copied and need not be preserved
256 * for the lifetime of the RSA context being set up.
257 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100258 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000259 * \param N The RSA modulus. This may be \c NULL.
260 * \param P The first prime factor of \p N. This may be \c NULL.
261 * \param Q The second prime factor of \p N. This may be \c NULL.
262 * \param D The private exponent. This may be \c NULL.
263 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100264 *
265 * \return \c 0 on success.
266 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100267 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100268int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
269 const mbedtls_mpi *N,
270 const mbedtls_mpi *P, const mbedtls_mpi *Q,
271 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100272
273/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000274 * \brief This function imports core RSA parameters, in raw big-endian
275 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000276 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100277 * \note This function can be called multiple times for successive
278 * imports, if the parameters are not simultaneously present.
279 *
280 * Any sequence of calls to this function should be followed
281 * by a call to mbedtls_rsa_complete(), which checks and
282 * completes the provided information to a ready-for-use
283 * public or private RSA key.
284 *
285 * \note See mbedtls_rsa_complete() for more information on which
286 * parameters are necessary to set up a private or public
287 * RSA key.
288 *
289 * \note The imported parameters are copied and need not be preserved
290 * for the lifetime of the RSA context being set up.
291 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000292 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000293 * \param N The RSA modulus. This may be \c NULL.
294 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
295 * \param P The first prime factor of \p N. This may be \c NULL.
296 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
297 * \param Q The second prime factor of \p N. This may be \c NULL.
298 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
299 * \param D The private exponent. This may be \c NULL.
300 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
301 * \param E The public exponent. This may be \c NULL.
302 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100304 * \return \c 0 on success.
305 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100306 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100307int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100308 unsigned char const *N, size_t N_len,
309 unsigned char const *P, size_t P_len,
310 unsigned char const *Q, size_t Q_len,
311 unsigned char const *D, size_t D_len,
312 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100313
314/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000315 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100316 * a set of imported core parameters.
317 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000318 * To setup an RSA public key, precisely \p N and \p E
319 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100320 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000321 * To setup an RSA private key, sufficient information must
322 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100323 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000324 * The default implementation supports the following:
325 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
326 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
327 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100328 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000329 * If this function runs successfully, it guarantees that
330 * the RSA context can be used for RSA operations without
331 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100332 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100333 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000334 * for the imported parameters. In particular, parameters that
335 * are not needed by the implementation might be silently
336 * discarded and left unchecked. To check the consistency
337 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100338 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100339 * \param ctx The initialized RSA context holding imported parameters.
340 *
341 * \return \c 0 on success.
342 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
343 * failed.
344 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100345 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100346int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100347
348/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000349 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100350 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000351 * If this function runs successfully, the non-NULL buffers
352 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
353 * written, with additional unused space filled leading by
354 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100355 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000356 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300357 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000358 * <li>An alternative RSA implementation is in use, which
359 * stores the key externally, and either cannot or should
360 * not export it into RAM.</li>
361 * <li>A SW or HW implementation might not support a certain
362 * deduction. For example, \p P, \p Q from \p N, \p D,
363 * and \p E if the former are not part of the
364 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100365 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000366 * If the function fails due to an unsupported operation,
367 * the RSA context stays intact and remains usable.
368 *
369 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000370 * \param N The MPI to hold the RSA modulus.
371 * This may be \c NULL if this field need not be exported.
372 * \param P The MPI to hold the first prime factor of \p N.
373 * This may be \c NULL if this field need not be exported.
374 * \param Q The MPI to hold the second prime factor of \p N.
375 * This may be \c NULL if this field need not be exported.
376 * \param D The MPI to hold the private exponent.
377 * This may be \c NULL if this field need not be exported.
378 * \param E The MPI to hold the public exponent.
379 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000380 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100381 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300382 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000383 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100384 * functionality or because of security policies.
385 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100386 *
387 */
388int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
389 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
390 mbedtls_mpi *D, mbedtls_mpi *E );
391
392/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000393 * \brief This function exports core parameters of an RSA key
394 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100395 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000396 * If this function runs successfully, the non-NULL buffers
397 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
398 * written, with additional unused space filled leading by
399 * zero Bytes.
400 *
401 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300402 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000403 * <li>An alternative RSA implementation is in use, which
404 * stores the key externally, and either cannot or should
405 * not export it into RAM.</li>
406 * <li>A SW or HW implementation might not support a certain
407 * deduction. For example, \p P, \p Q from \p N, \p D,
408 * and \p E if the former are not part of the
409 * implementation.</li></ul>
410 * If the function fails due to an unsupported operation,
411 * the RSA context stays intact and remains usable.
412 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100413 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100414 * buffer pointers are NULL.
415 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000416 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000417 * \param N The Byte array to store the RSA modulus,
418 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000419 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000420 * \param P The Byte array to hold the first prime factor of \p N,
421 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000422 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000423 * \param Q The Byte array to hold the second prime factor of \p N,
424 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000425 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000426 * \param D The Byte array to hold the private exponent,
427 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000428 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000429 * \param E The Byte array to hold the public exponent,
430 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000431 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100432 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100433 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300434 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000435 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100436 * functionality or because of security policies.
437 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100438 */
439int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
440 unsigned char *N, size_t N_len,
441 unsigned char *P, size_t P_len,
442 unsigned char *Q, size_t Q_len,
443 unsigned char *D, size_t D_len,
444 unsigned char *E, size_t E_len );
445
446/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000447 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100448 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100449 * \note Alternative RSA implementations not using CRT-parameters
450 * internally can implement this function based on
451 * mbedtls_rsa_deduce_opt().
452 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000453 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000454 * \param DP The MPI to hold \c D modulo `P-1`,
455 * or \c NULL if it need not be exported.
456 * \param DQ The MPI to hold \c D modulo `Q-1`,
457 * or \c NULL if it need not be exported.
458 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
459 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100460 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100461 * \return \c 0 on success.
462 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100463 *
464 */
465int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
466 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
467
Paul Bakker5121ce52009-01-03 21:22:43 +0000468/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000469 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100470 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000471 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100472 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000473 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100474 *
475 */
476size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
477
478/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000479 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000481 * \note mbedtls_rsa_init() must be called before this function,
482 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 *
Hanno Becker9a467772018-12-13 09:54:59 +0000484 * \param ctx The initialized RSA context used to hold the key.
485 * \param f_rng The RNG function to be used for key generation.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100486 * This is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000487 * \param p_rng The RNG context to be passed to \p f_rng.
488 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100489 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000490 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000491 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100492 *
493 * \return \c 0 on success.
494 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100497 int (*f_rng)(void *, unsigned char *, size_t),
498 void *p_rng,
499 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
501/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000502 * \brief This function checks if a context contains at least an RSA
503 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000505 * If the function runs successfully, it is guaranteed that
506 * enough information is present to perform an RSA public key
507 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 *
Hanno Becker9a467772018-12-13 09:54:59 +0000509 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000510 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100511 * \return \c 0 on success.
512 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100513 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
517/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000518 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100519 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 *
Hanno Becker68767a62017-10-17 10:13:31 +0100521 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000522 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100523 * on the given context, but that the various parameters are
524 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000525 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100526 *
527 * \warning This function should catch accidental misconfigurations
528 * like swapping of parameters, but it cannot establish full
529 * trust in neither the quality nor the consistency of the key
530 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000531 * <ul><li>Consistency: Imported parameters that are irrelevant
532 * for the implementation might be silently dropped. If dropped,
533 * the current function does not have access to them,
534 * and therefore cannot check them. See mbedtls_rsa_complete().
535 * If you want to check the consistency of the entire
536 * content of an PKCS1-encoded RSA private key, for example, you
537 * should use mbedtls_rsa_validate_params() before setting
538 * up the RSA context.
539 * Additionally, if the implementation performs empirical checks,
540 * these checks substantiate but do not guarantee consistency.</li>
541 * <li>Quality: This function is not expected to perform
542 * extended quality assessments like checking that the prime
543 * factors are safe. Additionally, it is the responsibility of the
544 * user to ensure the trustworthiness of the source of his RSA
545 * parameters, which goes beyond what is effectively checkable
546 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100547 *
Hanno Becker9a467772018-12-13 09:54:59 +0000548 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100549 *
550 * \return \c 0 on success.
551 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
555/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000556 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100557 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000558 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100559 *
Hanno Becker9a467772018-12-13 09:54:59 +0000560 * \param pub The initialized RSA context holding the public key.
561 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000562 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100563 * \return \c 0 on success.
564 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100565 */
Hanno Becker98838b02017-10-02 13:16:10 +0100566int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
567 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100568
569/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000570 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000571 *
Hanno Becker9a467772018-12-13 09:54:59 +0000572 * \param ctx The initialized RSA context to use.
573 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000574 * of length \c ctx->len Bytes. For example, \c 256 Bytes
575 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000576 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000577 * of length \c ctx->len Bytes. For example, \c 256 Bytes
578 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000579 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000580 * \note This function does not handle message padding.
581 *
582 * \note Make sure to set \p input[0] = 0 or ensure that
583 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100585 * \return \c 0 on success.
586 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000589 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 unsigned char *output );
591
592/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000593 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 *
Hanno Becker24120612017-10-26 11:53:35 +0100595 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100596 *
597 * \note If blinding is used, both the base of exponentation
Hanno Becker24120612017-10-26 11:53:35 +0100598 * and the exponent are blinded, providing protection
599 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100600 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100601 * \warning It is deprecated and a security risk to not provide
602 * a PRNG here and thereby prevent the use of blinding.
603 * Future versions of the library may enforce the presence
604 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100605 *
Hanno Becker9a467772018-12-13 09:54:59 +0000606 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100607 * \param f_rng The RNG function, used for blinding. It is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000608 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
Thomas Daubney03412782021-05-20 15:31:17 +0100609 * if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000610 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000611 * of length \c ctx->len Bytes. For example, \c 256 Bytes
612 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000613 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000614 * of length \c ctx->len Bytes. For example, \c 256 Bytes
615 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100616 *
617 * \return \c 0 on success.
618 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
619 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200622 int (*f_rng)(void *, unsigned char *, size_t),
623 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000624 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000625 unsigned char *output );
626
627/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000628 * \brief This function adds the message padding, then performs an RSA
629 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000631 * It is the generic wrapper for performing a PKCS#1 encryption
Thomas Daubney21772772021-05-13 17:30:32 +0100632 * operation.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100633 *
Hanno Becker9a467772018-12-13 09:54:59 +0000634 * \param ctx The initialized RSA context to use.
Thomas Daubneyf54c5c52021-05-21 17:00:30 +0100635 * \param f_rng The RNG to use. It is used for padding generation
Thomas Daubney2c65db92021-05-21 10:58:28 +0100636 * and it is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000637 * \param p_rng The RNG context to be passed to \p f_rng. May be
Thomas Daubney03412782021-05-20 15:31:17 +0100638 * \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000639 * \param ilen The length of the plaintext in Bytes.
640 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000641 * buffer of size \p ilen Bytes. It may be \c NULL if
642 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000643 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000644 * of length \c ctx->len Bytes. For example, \c 256 Bytes
645 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100646 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100647 * \return \c 0 on success.
648 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000651 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000652 void *p_rng,
Thomas Daubney21772772021-05-13 17:30:32 +0100653 size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000654 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 unsigned char *output );
656
657/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000658 * \brief This function performs a PKCS#1 v1.5 encryption operation
659 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100660 *
Hanno Becker9a467772018-12-13 09:54:59 +0000661 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100662 * \param f_rng The RNG function to use. It is mandatory and used for
663 * padding generation.
Hanno Becker9a467772018-12-13 09:54:59 +0000664 * \param p_rng The RNG context to be passed to \p f_rng. This may
Thomas Daubney03412782021-05-20 15:31:17 +0100665 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000666 * \param ilen The length of the plaintext in Bytes.
667 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000668 * buffer of size \p ilen Bytes. It may be \c NULL if
669 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000670 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000671 * of length \c ctx->len Bytes. For example, \c 256 Bytes
672 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100673 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100674 * \return \c 0 on success.
675 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100676 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100678 int (*f_rng)(void *, unsigned char *, size_t),
679 void *p_rng,
Thomas Daubney53e4ac62021-05-13 18:26:49 +0100680 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100681 const unsigned char *input,
682 unsigned char *output );
683
684/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000685 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
686 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100687 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100688 * \note The output buffer must be as large as the size
689 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
690 *
Hanno Becker9a467772018-12-13 09:54:59 +0000691 * \param ctx The initnialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000692 * \param f_rng The RNG function to use. This is needed for padding
Thomas Daubney2c65db92021-05-21 10:58:28 +0100693 * generation and is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000694 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000695 * be \c NULL if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000696 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000697 * This must be a readable buffer of length \p label_len
698 * Bytes. It may be \c NULL if \p label_len is \c 0.
699 * \param label_len The length of the label in Bytes.
700 * \param ilen The length of the plaintext buffer \p input in Bytes.
701 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000702 * buffer of size \p ilen Bytes. It may be \c NULL if
703 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000704 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000705 * of length \c ctx->len Bytes. For example, \c 256 Bytes
706 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100707 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100708 * \return \c 0 on success.
709 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100710 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100712 int (*f_rng)(void *, unsigned char *, size_t),
713 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100714 const unsigned char *label, size_t label_len,
715 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100716 const unsigned char *input,
717 unsigned char *output );
718
719/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000720 * \brief This function performs an RSA operation, then removes the
721 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000722 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000723 * It is the generic wrapper for performing a PKCS#1 decryption
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100724 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000725 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100726 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000727 * as large as the size \p ctx->len of \p ctx->N (for example,
728 * 128 Bytes if RSA-1024 is used) to be able to hold an
729 * arbitrary decrypted message. If it is not large enough to
730 * hold the decryption of the particular ciphertext provided,
731 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100732 *
Hanno Becker9a467772018-12-13 09:54:59 +0000733 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100734 * \param f_rng The RNG function. This is used for blinding and is
735 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000736 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100737 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000738 * \param olen The address at which to store the length of
739 * the plaintext. This must not be \c NULL.
740 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000741 * of length \c ctx->len Bytes. For example, \c 256 Bytes
742 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000743 * \param output The buffer used to hold the plaintext. This must
744 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000745 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100746 *
747 * \return \c 0 on success.
748 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200751 int (*f_rng)(void *, unsigned char *, size_t),
752 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100753 size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000754 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000755 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000756 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000757
758/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000759 * \brief This function performs a PKCS#1 v1.5 decryption
760 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100761 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100762 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000763 * as large as the size \p ctx->len of \p ctx->N, for example,
764 * 128 Bytes if RSA-1024 is used, to be able to hold an
765 * arbitrary decrypted message. If it is not large enough to
766 * hold the decryption of the particular ciphertext provided,
767 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100768 *
Hanno Becker9a467772018-12-13 09:54:59 +0000769 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100770 * \param f_rng The RNG function. This is used for blinding and is
771 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000772 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100773 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000774 * \param olen The address at which to store the length of
775 * the plaintext. This must not be \c NULL.
776 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000777 * of length \c ctx->len Bytes. For example, \c 256 Bytes
778 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000779 * \param output The buffer used to hold the plaintext. This must
780 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000781 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100782 *
783 * \return \c 0 on success.
784 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
785 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200788 int (*f_rng)(void *, unsigned char *, size_t),
789 void *p_rng,
Thomas Daubney34733082021-05-12 09:24:29 +0100790 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100791 const unsigned char *input,
792 unsigned char *output,
793 size_t output_max_len );
794
795/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100796 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
797 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100798 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100799 * \note The output buffer length \c output_max_len should be
800 * as large as the size \p ctx->len of \p ctx->N, for
801 * example, 128 Bytes if RSA-1024 is used, to be able to
802 * hold an arbitrary decrypted message. If it is not
803 * large enough to hold the decryption of the particular
804 * ciphertext provided, the function returns
805 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100806 *
Hanno Becker9a467772018-12-13 09:54:59 +0000807 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100808 * \param f_rng The RNG function. This is used for blinding and is
809 * mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000810 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100811 * \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100812 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000813 * This must be a readable buffer of length \p label_len
814 * Bytes. It may be \c NULL if \p label_len is \c 0.
815 * \param label_len The length of the label in Bytes.
816 * \param olen The address at which to store the length of
817 * the plaintext. This must not be \c NULL.
818 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000819 * of length \c ctx->len Bytes. For example, \c 256 Bytes
820 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000821 * \param output The buffer used to hold the plaintext. This must
822 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000823 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100824 *
825 * \return \c 0 on success.
826 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200829 int (*f_rng)(void *, unsigned char *, size_t),
830 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100831 const unsigned char *label, size_t label_len,
832 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100833 const unsigned char *input,
834 unsigned char *output,
835 size_t output_max_len );
836
837/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000838 * \brief This function performs a private RSA operation to sign
839 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000840 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000841 * It is the generic wrapper for performing a PKCS#1
Thomas Daubney140184d2021-05-18 16:04:07 +0100842 * signature.
Paul Bakker5121ce52009-01-03 21:22:43 +0000843 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000844 * \note The \p sig buffer must be as large as the size
845 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000846 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000847 * \note For PKCS#1 v2.1 encoding, see comments on
848 * mbedtls_rsa_rsassa_pss_sign() for details on
849 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100850 *
Hanno Becker9a467772018-12-13 09:54:59 +0000851 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100852 * \param f_rng The RNG function to use. This is mandatory and
853 * must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000854 * \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 +0100855 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100856 * \param md_alg The message-digest algorithm used to hash the original data.
857 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200858 * \param hashlen The length of the message digest or raw data in Bytes.
859 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
860 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000861 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200862 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000863 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000864 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100865 * for an 2048-bit RSA modulus. A buffer length of
866 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100867 *
868 * \return \c 0 if the signing operation was successful.
869 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000872 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000873 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000875 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000876 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000877 unsigned char *sig );
878
879/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000880 * \brief This function performs a PKCS#1 v1.5 signature
881 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100882 *
Hanno Becker9a467772018-12-13 09:54:59 +0000883 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100884 * \param f_rng The RNG function. This is used for blinding and is
885 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000886 * \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 +0100887 * if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000888 * \param md_alg The message-digest algorithm used to hash the original data.
889 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200890 * \param hashlen The length of the message digest or raw data in Bytes.
891 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
892 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000893 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200894 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000895 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000896 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100897 * for an 2048-bit RSA modulus. A buffer length of
898 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100899 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100900 * \return \c 0 if the signing operation was successful.
901 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200904 int (*f_rng)(void *, unsigned char *, size_t),
905 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100907 unsigned int hashlen,
908 const unsigned char *hash,
909 unsigned char *sig );
910
911/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000912 * \brief This function performs a PKCS#1 v2.1 PSS signature
913 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100914 *
Janos Follathb7953322021-04-01 14:44:17 +0100915 * \note The \c hash_id set in \p ctx by calling
916 * mbedtls_rsa_set_padding() selects the hash used for the
917 * encoding operation and for the mask generation function
918 * (MGF1). For more details on the encoding operation and the
919 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +0000920 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100921 * Specifications</em>.
Rose Zadike8b5b992018-03-27 12:19:47 +0100922 *
Cédric Meuter010ddc22020-04-25 09:24:11 +0200923 * \note This function enforces that the provided salt length complies
924 * with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 v2.2) §9.1.1
925 * step 3. The constraint is that the hash length plus the salt
926 * length plus 2 bytes must be at most the key length. If this
927 * constraint is not met, this function returns
Jaeden Amero3725bb22018-09-07 19:12:36 +0100928 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
929 *
Hanno Becker9a467772018-12-13 09:54:59 +0000930 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100931 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000932 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
933 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100934 * \param md_alg The message-digest algorithm used to hash the original data.
935 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200936 * \param hashlen The length of the message digest or raw data in Bytes.
937 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
938 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000939 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200940 * This must be a readable buffer of at least \p hashlen Bytes.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200941 * \param saltlen The length of the salt that should be used.
Cédric Meuter010ddc22020-04-25 09:24:11 +0200942 * If passed #MBEDTLS_RSA_SALT_LEN_ANY, the function will use
943 * the largest possible salt length up to the hash length,
944 * which is the largest permitted by some standards including
945 * FIPS 186-4 §5.5.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200946 * \param sig The buffer to hold the signature. This must be a writable
947 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
948 * for an 2048-bit RSA modulus. A buffer length of
949 * #MBEDTLS_MPI_MAX_SIZE is always safe.
950 *
951 * \return \c 0 if the signing operation was successful.
952 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
953 */
954int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
955 int (*f_rng)(void *, unsigned char *, size_t),
956 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200957 mbedtls_md_type_t md_alg,
958 unsigned int hashlen,
959 const unsigned char *hash,
960 int saltlen,
961 unsigned char *sig );
962
963/**
964 * \brief This function performs a PKCS#1 v2.1 PSS signature
965 * operation (RSASSA-PSS-SIGN).
966 *
Janos Follathb7953322021-04-01 14:44:17 +0100967 * \note The \c hash_id set in \p ctx by calling
968 * mbedtls_rsa_set_padding() selects the hash used for the
969 * encoding operation and for the mask generation function
970 * (MGF1). For more details on the encoding operation and the
971 * mask generation function, consult <em>RFC-3447: Public-Key
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200972 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100973 * Specifications</em>.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200974 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000975 * \note This function always uses the maximum possible salt size,
976 * up to the length of the payload hash. This choice of salt
977 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
978 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
Rose Zadike8b5b992018-03-27 12:19:47 +0100979 * minimum salt size which is the hash size minus 2 bytes. If
980 * this minimum size is too large given the key size (the salt
981 * size, plus the hash size, plus 2 bytes must be no more than
982 * the key size in bytes), this function returns
983 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
984 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100985 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100986 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100987 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
988 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100989 * \param md_alg The message-digest algorithm used to hash the original data.
990 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200991 * \param hashlen The length of the message digest or raw data in Bytes.
992 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
993 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000994 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200995 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000996 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000997 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100998 * for an 2048-bit RSA modulus. A buffer length of
999 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +01001000 *
1001 * \return \c 0 if the signing operation was successful.
1002 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001003 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001005 int (*f_rng)(void *, unsigned char *, size_t),
1006 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001008 unsigned int hashlen,
1009 const unsigned char *hash,
1010 unsigned char *sig );
1011
1012/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001013 * \brief This function performs a public RSA operation and checks
1014 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +00001015 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001016 * This is the generic wrapper for performing a PKCS#1
Thomas Daubney68d9cbc2021-05-18 18:45:09 +01001017 * verification.
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001019 * \note For PKCS#1 v2.1 encoding, see comments on
1020 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
1021 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +01001022 *
Hanno Becker9a467772018-12-13 09:54:59 +00001023 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001024 * \param md_alg The message-digest algorithm used to hash the original data.
1025 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001026 * \param hashlen The length of the message digest or raw data in Bytes.
1027 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1028 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001029 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001030 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001031 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001032 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1033 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001034 *
1035 * \return \c 0 if the verify operation was successful.
1036 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001037 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001040 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001041 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001042 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +00001043
1044/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001045 * \brief This function performs a PKCS#1 v1.5 verification
1046 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001047 *
Hanno Becker9a467772018-12-13 09:54:59 +00001048 * \param ctx The initialized RSA public key context to use.
Rose Zadik042e97f2018-01-26 16:35:10 +00001049 * \param md_alg The message-digest algorithm used to hash the original data.
1050 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001051 * \param hashlen The length of the message digest or raw data in Bytes.
1052 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1053 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001054 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001055 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001056 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001057 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1058 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001059 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001060 * \return \c 0 if the verify operation was successful.
1061 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001062 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001065 unsigned int hashlen,
1066 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001067 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001068
1069/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001070 * \brief This function performs a PKCS#1 v2.1 PSS verification
1071 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001072 *
Janos Follathb7953322021-04-01 14:44:17 +01001073 * \note The \c hash_id set in \p ctx by calling
1074 * mbedtls_rsa_set_padding() selects the hash used for the
1075 * encoding operation and for the mask generation function
1076 * (MGF1). For more details on the encoding operation and the
1077 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +00001078 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +01001079 * Specifications</em>. If the \c hash_id set in \p ctx by
1080 * mbedtls_rsa_set_padding() is #MBEDTLS_MD_NONE, the \p md_alg
1081 * parameter is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001082 *
Hanno Becker9a467772018-12-13 09:54:59 +00001083 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001084 * \param md_alg The message-digest algorithm used to hash the original data.
1085 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001086 * \param hashlen The length of the message digest or raw data in Bytes.
1087 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1088 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001089 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001090 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001091 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001092 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1093 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001094 *
1095 * \return \c 0 if the verify operation was successful.
1096 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001100 unsigned int hashlen,
1101 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001102 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001103
1104/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001105 * \brief This function performs a PKCS#1 v2.1 PSS verification
1106 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001107 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001108 * \note The \p sig buffer must be as large as the size
1109 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001110 *
Janos Follathb7953322021-04-01 14:44:17 +01001111 * \note The \c hash_id set in \p ctx by mbedtls_rsa_set_padding() is
1112 * ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001113 *
Hanno Becker9a467772018-12-13 09:54:59 +00001114 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001115 * \param md_alg The message-digest algorithm used to hash the original data.
1116 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001117 * \param hashlen The length of the message digest or raw data in Bytes.
1118 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1119 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001120 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001121 * This must be a readable buffer of at least \p hashlen Bytes.
Janos Follathb7953322021-04-01 14:44:17 +01001122 * \param mgf1_hash_id The message digest algorithm used for the
1123 * verification operation and the mask generation
1124 * function (MGF1). For more details on the encoding
1125 * operation and the mask generation function, consult
1126 * <em>RFC-3447: Public-Key Cryptography Standards
1127 * (PKCS) #1 v2.1: RSA Cryptography
1128 * Specifications</em>.
Hanno Becker9a467772018-12-13 09:54:59 +00001129 * \param expected_salt_len The length of the salt used in padding. Use
1130 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1131 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001132 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1133 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001134 *
1135 * \return \c 0 if the verify operation was successful.
1136 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001140 unsigned int hashlen,
1141 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001143 int expected_salt_len,
1144 const unsigned char *sig );
1145
1146/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001147 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001148 *
Hanno Becker9a467772018-12-13 09:54:59 +00001149 * \param dst The destination context. This must be initialized.
1150 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001151 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001152 * \return \c 0 on success.
1153 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001156
1157/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001158 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001159 *
Hanno Becker9a467772018-12-13 09:54:59 +00001160 * \param ctx The RSA context to free. May be \c NULL, in which case
1161 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001162 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001165
Ron Eldorfa8f6352017-06-20 15:48:46 +03001166#if defined(MBEDTLS_SELF_TEST)
1167
Paul Bakker5121ce52009-01-03 21:22:43 +00001168/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001169 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001171 * \return \c 0 on success.
1172 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001175
Ron Eldorfa8f6352017-06-20 15:48:46 +03001176#endif /* MBEDTLS_SELF_TEST */
1177
Paul Bakker5121ce52009-01-03 21:22:43 +00001178#ifdef __cplusplus
1179}
1180#endif
1181
Paul Bakker5121ce52009-01-03 21:22:43 +00001182#endif /* rsa.h */