blob: 02de1766393b6f5f3b8241d841cc9229a886faed [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
Przemyslaw Stekiel71284ea2021-12-13 09:00:52 +010093psa_status_t mbedtls_test_transparent_asymmetric_decrypt(
94 const psa_key_attributes_t *attributes,
95 const uint8_t *key_buffer,
96 size_t key_buffer_size,
97 psa_algorithm_t alg,
98 const uint8_t *input,
99 size_t input_length,
100 const uint8_t *salt,
101 size_t salt_length,
102 uint8_t *output,
103 size_t output_size,
104 size_t *output_length );
105
106psa_status_t mbedtls_test_opaque_asymmetric_decrypt(
107 const psa_key_attributes_t *attributes,
108 const uint8_t *key,
109 size_t key_length,
110 psa_algorithm_t alg,
111 const uint8_t *input,
112 size_t input_length,
113 const uint8_t *salt,
114 size_t salt_length,
115 uint8_t *output,
116 size_t output_size,
117 size_t *output_length);
118
Przemyslaw Stekielb6a66502021-12-09 11:11:54 +0100119#endif /* PSA_CRYPTO_DRIVER_TEST */
120
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000121/*
122 * RSA Error codes
123 */
Gilles Peskined2971572021-07-26 18:48:10 +0200124/** Bad input parameters to function. */
125#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080
126/** Input data contains invalid padding and is rejected. */
127#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100
128/** Something failed during generation of a key. */
129#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180
130/** Key failed to pass the validity check of the library. */
131#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200
132/** The public key operation failed. */
133#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280
134/** The private key operation failed. */
135#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300
136/** The PKCS#1 verification failed. */
137#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380
138/** The output buffer for decryption is not large enough. */
139#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400
140/** The random generator failed to generate non-zeros. */
141#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480
Ron Eldor9924bdc2018-10-04 10:59:13 +0300142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200144 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
Rose Zadike8b5b992018-03-27 12:19:47 +0100147#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
148#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
Rose Zadik042e97f2018-01-26 16:35:10 +0000150#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
151#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200154
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +0200155/*
156 * The above constants may be used even if the RSA module is compile out,
157 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
158 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +0100159
Paul Bakker407a0da2013-06-27 14:29:21 +0200160#ifdef __cplusplus
161extern "C" {
162#endif
163
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200164#if !defined(MBEDTLS_RSA_ALT)
165// Regular implementation
166//
167
Paul Bakker5121ce52009-01-03 21:22:43 +0000168/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000169 * \brief The RSA context structure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200171typedef struct mbedtls_rsa_context
Paul Bakker5121ce52009-01-03 21:22:43 +0000172{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200173 int MBEDTLS_PRIVATE(ver); /*!< Reserved for internal purposes.
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100174 * Do not set this field in application
175 * code. Its meaning might change without
176 * notice. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200177 size_t MBEDTLS_PRIVATE(len); /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200179 mbedtls_mpi MBEDTLS_PRIVATE(N); /*!< The public modulus. */
180 mbedtls_mpi MBEDTLS_PRIVATE(E); /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200182 mbedtls_mpi MBEDTLS_PRIVATE(D); /*!< The private exponent. */
183 mbedtls_mpi MBEDTLS_PRIVATE(P); /*!< The first prime factor. */
184 mbedtls_mpi MBEDTLS_PRIVATE(Q); /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100185
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200186 mbedtls_mpi MBEDTLS_PRIVATE(DP); /*!< <code>D % (P - 1)</code>. */
187 mbedtls_mpi MBEDTLS_PRIVATE(DQ); /*!< <code>D % (Q - 1)</code>. */
188 mbedtls_mpi MBEDTLS_PRIVATE(QP); /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200190 mbedtls_mpi MBEDTLS_PRIVATE(RN); /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200192 mbedtls_mpi MBEDTLS_PRIVATE(RP); /*!< cached <code>R^2 mod P</code>. */
193 mbedtls_mpi MBEDTLS_PRIVATE(RQ); /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200194
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200195 mbedtls_mpi MBEDTLS_PRIVATE(Vi); /*!< The cached blinding value. */
196 mbedtls_mpi MBEDTLS_PRIVATE(Vf); /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000197
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200198 int MBEDTLS_PRIVATE(padding); /*!< Selects padding mode:
Rose Zadik042e97f2018-01-26 16:35:10 +0000199 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
200 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200201 int MBEDTLS_PRIVATE(hash_id); /*!< Hash identifier of mbedtls_md_type_t type,
Rose Zadik042e97f2018-01-26 16:35:10 +0000202 as specified in md.h for use in the MGF
203 mask generating function used in the
204 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100206 /* Invariant: the mutex is initialized iff ver != 0. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200207 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex); /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200208#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000209}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200212#else /* MBEDTLS_RSA_ALT */
213#include "rsa_alt.h"
214#endif /* MBEDTLS_RSA_ALT */
215
Paul Bakker5121ce52009-01-03 21:22:43 +0000216/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000217 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200219 * \note This function initializes the padding and the hash
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200220 * identifier to respectively #MBEDTLS_RSA_PKCS_V15 and
221 * #MBEDTLS_MD_NONE. See mbedtls_rsa_set_padding() for more
222 * information about those parameters.
Ronald Cronc1905a12021-06-05 11:11:14 +0200223 *
224 * \param ctx The RSA context to initialize. This must not be \c NULL.
225 */
226void mbedtls_rsa_init( mbedtls_rsa_context *ctx );
227
228/**
229 * \brief This function sets padding for an already initialized RSA
230 * context.
231 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000232 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000233 * encryption scheme and the RSASSA-PSS signature scheme.
234 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000235 * \note The \p hash_id parameter is ignored when using
236 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200237 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200238 * \note The choice of padding mode is strictly enforced for private
239 * key operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000240 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100241 * a default value, which can be overridden by calling specific
Ronald Cronc1905a12021-06-05 11:11:14 +0200242 * \c mbedtls_rsa_rsaes_xxx or \c mbedtls_rsa_rsassa_xxx
243 * functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200244 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000245 * \note The hash selected in \p hash_id is always used for OEAP
246 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100247 * making signatures, but can be overridden for verifying them.
248 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100249 *
Ronald Cronc1905a12021-06-05 11:11:14 +0200250 * \param ctx The initialized RSA context to be configured.
Hanno Becker9a467772018-12-13 09:54:59 +0000251 * \param padding The padding mode to use. This must be either
252 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Ronald Crond2cfa3e2021-06-08 09:09:04 +0200253 * \param hash_id The hash identifier for PSS or OAEP, if \p padding is
254 * #MBEDTLS_RSA_PKCS_V21. #MBEDTLS_MD_NONE is accepted by this
255 * function but may be not suitable for some operations.
256 * Ignored if \p padding is #MBEDTLS_RSA_PKCS_V15.
Ronald Cronc1905a12021-06-05 11:11:14 +0200257 *
258 * \return \c 0 on success.
259 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING failure:
260 * \p padding or \p hash_id is invalid.
Paul Bakker5121ce52009-01-03 21:22:43 +0000261 */
Ronald Cronc1905a12021-06-05 11:11:14 +0200262int mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
263 mbedtls_md_type_t hash_id );
Paul Bakker5121ce52009-01-03 21:22:43 +0000264
265/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000266 * \brief This function imports a set of core parameters into an
267 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100268 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100269 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000270 * imports, if the parameters are not simultaneously present.
271 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100272 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000273 * by a call to mbedtls_rsa_complete(), which checks and
274 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100275 * public or private RSA key.
276 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000277 * \note See mbedtls_rsa_complete() for more information on which
278 * parameters are necessary to set up a private or public
279 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100280 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100281 * \note The imported parameters are copied and need not be preserved
282 * for the lifetime of the RSA context being set up.
283 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100284 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000285 * \param N The RSA modulus. This may be \c NULL.
286 * \param P The first prime factor of \p N. This may be \c NULL.
287 * \param Q The second prime factor of \p N. This may be \c NULL.
288 * \param D The private exponent. This may be \c NULL.
289 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100290 *
291 * \return \c 0 on success.
292 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100293 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100294int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
295 const mbedtls_mpi *N,
296 const mbedtls_mpi *P, const mbedtls_mpi *Q,
297 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100298
299/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000300 * \brief This function imports core RSA parameters, in raw big-endian
301 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100303 * \note This function can be called multiple times for successive
304 * imports, if the parameters are not simultaneously present.
305 *
306 * Any sequence of calls to this function should be followed
307 * by a call to mbedtls_rsa_complete(), which checks and
308 * completes the provided information to a ready-for-use
309 * public or private RSA key.
310 *
311 * \note See mbedtls_rsa_complete() for more information on which
312 * parameters are necessary to set up a private or public
313 * RSA key.
314 *
315 * \note The imported parameters are copied and need not be preserved
316 * for the lifetime of the RSA context being set up.
317 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000318 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000319 * \param N The RSA modulus. This may be \c NULL.
320 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
321 * \param P The first prime factor of \p N. This may be \c NULL.
322 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
323 * \param Q The second prime factor of \p N. This may be \c NULL.
324 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
325 * \param D The private exponent. This may be \c NULL.
326 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
327 * \param E The public exponent. This may be \c NULL.
328 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100330 * \return \c 0 on success.
331 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100332 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100333int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100334 unsigned char const *N, size_t N_len,
335 unsigned char const *P, size_t P_len,
336 unsigned char const *Q, size_t Q_len,
337 unsigned char const *D, size_t D_len,
338 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100339
340/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000341 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100342 * a set of imported core parameters.
343 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000344 * To setup an RSA public key, precisely \p N and \p E
345 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100346 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000347 * To setup an RSA private key, sufficient information must
348 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100349 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000350 * The default implementation supports the following:
351 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
352 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
353 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100354 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000355 * If this function runs successfully, it guarantees that
356 * the RSA context can be used for RSA operations without
357 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100358 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100359 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000360 * for the imported parameters. In particular, parameters that
361 * are not needed by the implementation might be silently
362 * discarded and left unchecked. To check the consistency
363 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100364 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100365 * \param ctx The initialized RSA context holding imported parameters.
366 *
367 * \return \c 0 on success.
368 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
369 * failed.
370 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100371 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100372int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100373
374/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000375 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100376 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000377 * If this function runs successfully, the non-NULL buffers
378 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
379 * written, with additional unused space filled leading by
380 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100381 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000382 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300383 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000384 * <li>An alternative RSA implementation is in use, which
385 * stores the key externally, and either cannot or should
386 * not export it into RAM.</li>
387 * <li>A SW or HW implementation might not support a certain
388 * deduction. For example, \p P, \p Q from \p N, \p D,
389 * and \p E if the former are not part of the
390 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100391 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000392 * If the function fails due to an unsupported operation,
393 * the RSA context stays intact and remains usable.
394 *
395 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000396 * \param N The MPI to hold the RSA modulus.
397 * This may be \c NULL if this field need not be exported.
398 * \param P The MPI to hold the first prime factor of \p N.
399 * This may be \c NULL if this field need not be exported.
400 * \param Q The MPI to hold the second prime factor of \p N.
401 * This may be \c NULL if this field need not be exported.
402 * \param D The MPI to hold the private exponent.
403 * This may be \c NULL if this field need not be exported.
404 * \param E The MPI to hold the public exponent.
405 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000406 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100407 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300408 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000409 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100410 * functionality or because of security policies.
411 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100412 *
413 */
414int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
415 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
416 mbedtls_mpi *D, mbedtls_mpi *E );
417
418/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000419 * \brief This function exports core parameters of an RSA key
420 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100421 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000422 * If this function runs successfully, the non-NULL buffers
423 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
424 * written, with additional unused space filled leading by
425 * zero Bytes.
426 *
427 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300428 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000429 * <li>An alternative RSA implementation is in use, which
430 * stores the key externally, and either cannot or should
431 * not export it into RAM.</li>
432 * <li>A SW or HW implementation might not support a certain
433 * deduction. For example, \p P, \p Q from \p N, \p D,
434 * and \p E if the former are not part of the
435 * implementation.</li></ul>
436 * If the function fails due to an unsupported operation,
437 * the RSA context stays intact and remains usable.
438 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100439 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100440 * buffer pointers are NULL.
441 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000442 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000443 * \param N The Byte array to store the RSA modulus,
444 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000445 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000446 * \param P The Byte array to hold the first prime factor of \p N,
447 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000448 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000449 * \param Q The Byte array to hold the second prime factor of \p N,
450 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000451 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000452 * \param D The Byte array to hold the private exponent,
453 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000454 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000455 * \param E The Byte array to hold the public exponent,
456 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000457 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100458 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100459 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300460 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000461 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100462 * functionality or because of security policies.
463 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100464 */
465int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
466 unsigned char *N, size_t N_len,
467 unsigned char *P, size_t P_len,
468 unsigned char *Q, size_t Q_len,
469 unsigned char *D, size_t D_len,
470 unsigned char *E, size_t E_len );
471
472/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000473 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100474 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100475 * \note Alternative RSA implementations not using CRT-parameters
476 * internally can implement this function based on
477 * mbedtls_rsa_deduce_opt().
478 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000479 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000480 * \param DP The MPI to hold \c D modulo `P-1`,
481 * or \c NULL if it need not be exported.
482 * \param DQ The MPI to hold \c D modulo `Q-1`,
483 * or \c NULL if it need not be exported.
484 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
485 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100486 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100487 * \return \c 0 on success.
488 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100489 *
490 */
491int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
492 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
493
Paul Bakker5121ce52009-01-03 21:22:43 +0000494/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000495 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100496 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000497 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100498 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000499 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100500 *
501 */
502size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
503
504/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000505 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000507 * \note mbedtls_rsa_init() must be called before this function,
508 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 *
Hanno Becker9a467772018-12-13 09:54:59 +0000510 * \param ctx The initialized RSA context used to hold the key.
511 * \param f_rng The RNG function to be used for key generation.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100512 * This is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000513 * \param p_rng The RNG context to be passed to \p f_rng.
514 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100515 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000516 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000517 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100518 *
519 * \return \c 0 on success.
520 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100523 int (*f_rng)(void *, unsigned char *, size_t),
524 void *p_rng,
525 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000528 * \brief This function checks if a context contains at least an RSA
529 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000530 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000531 * If the function runs successfully, it is guaranteed that
532 * enough information is present to perform an RSA public key
533 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 *
Hanno Becker9a467772018-12-13 09:54:59 +0000535 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000536 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100537 * \return \c 0 on success.
538 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100539 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
543/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000544 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100545 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 *
Hanno Becker68767a62017-10-17 10:13:31 +0100547 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000548 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100549 * on the given context, but that the various parameters are
550 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000551 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100552 *
553 * \warning This function should catch accidental misconfigurations
554 * like swapping of parameters, but it cannot establish full
555 * trust in neither the quality nor the consistency of the key
556 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000557 * <ul><li>Consistency: Imported parameters that are irrelevant
558 * for the implementation might be silently dropped. If dropped,
559 * the current function does not have access to them,
560 * and therefore cannot check them. See mbedtls_rsa_complete().
561 * If you want to check the consistency of the entire
562 * content of an PKCS1-encoded RSA private key, for example, you
563 * should use mbedtls_rsa_validate_params() before setting
564 * up the RSA context.
565 * Additionally, if the implementation performs empirical checks,
566 * these checks substantiate but do not guarantee consistency.</li>
567 * <li>Quality: This function is not expected to perform
568 * extended quality assessments like checking that the prime
569 * factors are safe. Additionally, it is the responsibility of the
570 * user to ensure the trustworthiness of the source of his RSA
571 * parameters, which goes beyond what is effectively checkable
572 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100573 *
Hanno Becker9a467772018-12-13 09:54:59 +0000574 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100575 *
576 * \return \c 0 on success.
577 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000582 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100583 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000584 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100585 *
Hanno Becker9a467772018-12-13 09:54:59 +0000586 * \param pub The initialized RSA context holding the public key.
587 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000588 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100589 * \return \c 0 on success.
590 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100591 */
Hanno Becker98838b02017-10-02 13:16:10 +0100592int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
593 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100594
595/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000596 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000597 *
Hanno Becker9a467772018-12-13 09:54:59 +0000598 * \param ctx The initialized RSA context to use.
599 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000600 * of length \c ctx->len Bytes. For example, \c 256 Bytes
601 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000602 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000603 * of length \c ctx->len Bytes. For example, \c 256 Bytes
604 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000605 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000606 * \note This function does not handle message padding.
607 *
608 * \note Make sure to set \p input[0] = 0 or ensure that
609 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100611 * \return \c 0 on success.
612 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000615 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000616 unsigned char *output );
617
618/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000619 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 *
Hanno Becker24120612017-10-26 11:53:35 +0100621 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100622 *
623 * \note If blinding is used, both the base of exponentation
Hanno Becker24120612017-10-26 11:53:35 +0100624 * and the exponent are blinded, providing protection
625 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100626 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100627 * \warning It is deprecated and a security risk to not provide
628 * a PRNG here and thereby prevent the use of blinding.
629 * Future versions of the library may enforce the presence
630 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100631 *
Hanno Becker9a467772018-12-13 09:54:59 +0000632 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100633 * \param f_rng The RNG function, used for blinding. It is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000634 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
Thomas Daubney03412782021-05-20 15:31:17 +0100635 * if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000636 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000637 * of length \c ctx->len Bytes. For example, \c 256 Bytes
638 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000639 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000640 * of length \c ctx->len Bytes. For example, \c 256 Bytes
641 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100642 *
643 * \return \c 0 on success.
644 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
645 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200648 int (*f_rng)(void *, unsigned char *, size_t),
649 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000650 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000651 unsigned char *output );
652
653/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000654 * \brief This function adds the message padding, then performs an RSA
655 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000656 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000657 * It is the generic wrapper for performing a PKCS#1 encryption
Thomas Daubney21772772021-05-13 17:30:32 +0100658 * operation.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100659 *
Hanno Becker9a467772018-12-13 09:54:59 +0000660 * \param ctx The initialized RSA context to use.
Thomas Daubneyf54c5c52021-05-21 17:00:30 +0100661 * \param f_rng The RNG to use. It is used for padding generation
Thomas Daubney2c65db92021-05-21 10:58:28 +0100662 * and it is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000663 * \param p_rng The RNG context to be passed to \p f_rng. May be
Thomas Daubney03412782021-05-20 15:31:17 +0100664 * \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000665 * \param ilen The length of the plaintext in Bytes.
666 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000667 * buffer of size \p ilen Bytes. It may be \c NULL if
668 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000669 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000670 * of length \c ctx->len Bytes. For example, \c 256 Bytes
671 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100672 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100673 * \return \c 0 on success.
674 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000677 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000678 void *p_rng,
Thomas Daubney21772772021-05-13 17:30:32 +0100679 size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000680 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 unsigned char *output );
682
683/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000684 * \brief This function performs a PKCS#1 v1.5 encryption operation
685 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100686 *
Hanno Becker9a467772018-12-13 09:54:59 +0000687 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100688 * \param f_rng The RNG function to use. It is mandatory and used for
689 * padding generation.
Hanno Becker9a467772018-12-13 09:54:59 +0000690 * \param p_rng The RNG context to be passed to \p f_rng. This may
Thomas Daubney03412782021-05-20 15:31:17 +0100691 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000692 * \param ilen The length of the plaintext in Bytes.
693 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000694 * buffer of size \p ilen Bytes. It may be \c NULL if
695 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000696 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000697 * of length \c ctx->len Bytes. For example, \c 256 Bytes
698 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100699 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100700 * \return \c 0 on success.
701 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100704 int (*f_rng)(void *, unsigned char *, size_t),
705 void *p_rng,
Thomas Daubney53e4ac62021-05-13 18:26:49 +0100706 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100707 const unsigned char *input,
708 unsigned char *output );
709
710/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000711 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
712 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100713 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100714 * \note The output buffer must be as large as the size
715 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
716 *
Hanno Becker9a467772018-12-13 09:54:59 +0000717 * \param ctx The initnialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000718 * \param f_rng The RNG function to use. This is needed for padding
Thomas Daubney2c65db92021-05-21 10:58:28 +0100719 * generation and is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000720 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000721 * be \c NULL if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000722 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000723 * This must be a readable buffer of length \p label_len
724 * Bytes. It may be \c NULL if \p label_len is \c 0.
725 * \param label_len The length of the label in Bytes.
726 * \param ilen The length of the plaintext buffer \p input in Bytes.
727 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000728 * buffer of size \p ilen Bytes. It may be \c NULL if
729 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000730 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000731 * of length \c ctx->len Bytes. For example, \c 256 Bytes
732 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100733 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100734 * \return \c 0 on success.
735 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100738 int (*f_rng)(void *, unsigned char *, size_t),
739 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100740 const unsigned char *label, size_t label_len,
741 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100742 const unsigned char *input,
743 unsigned char *output );
744
745/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000746 * \brief This function performs an RSA operation, then removes the
747 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000748 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000749 * It is the generic wrapper for performing a PKCS#1 decryption
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100750 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000751 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100752 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000753 * as large as the size \p ctx->len of \p ctx->N (for example,
754 * 128 Bytes if RSA-1024 is used) to be able to hold an
755 * arbitrary decrypted message. If it is not large enough to
756 * hold the decryption of the particular ciphertext provided,
757 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100758 *
Hanno Becker9a467772018-12-13 09:54:59 +0000759 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100760 * \param f_rng The RNG function. This is used for blinding and is
761 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000762 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100763 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000764 * \param olen The address at which to store the length of
765 * the plaintext. This must not be \c NULL.
766 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000767 * of length \c ctx->len Bytes. For example, \c 256 Bytes
768 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000769 * \param output The buffer used to hold the plaintext. This must
770 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000771 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100772 *
773 * \return \c 0 on success.
774 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200777 int (*f_rng)(void *, unsigned char *, size_t),
778 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100779 size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000780 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000781 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000782 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000783
784/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000785 * \brief This function performs a PKCS#1 v1.5 decryption
786 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100787 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100788 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000789 * as large as the size \p ctx->len of \p ctx->N, for example,
790 * 128 Bytes if RSA-1024 is used, to be able to hold an
791 * arbitrary decrypted message. If it is not large enough to
792 * hold the decryption of the particular ciphertext provided,
793 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100794 *
Hanno Becker9a467772018-12-13 09:54:59 +0000795 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100796 * \param f_rng The RNG function. This is used for blinding and is
797 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000798 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100799 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000800 * \param olen The address at which to store the length of
801 * the plaintext. This must not be \c NULL.
802 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000803 * of length \c ctx->len Bytes. For example, \c 256 Bytes
804 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000805 * \param output The buffer used to hold the plaintext. This must
806 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000807 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100808 *
809 * \return \c 0 on success.
810 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
811 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100812 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200814 int (*f_rng)(void *, unsigned char *, size_t),
815 void *p_rng,
Thomas Daubney34733082021-05-12 09:24:29 +0100816 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100817 const unsigned char *input,
818 unsigned char *output,
819 size_t output_max_len );
820
821/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100822 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
823 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100824 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100825 * \note The output buffer length \c output_max_len should be
826 * as large as the size \p ctx->len of \p ctx->N, for
827 * example, 128 Bytes if RSA-1024 is used, to be able to
828 * hold an arbitrary decrypted message. If it is not
829 * large enough to hold the decryption of the particular
830 * ciphertext provided, the function returns
831 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100832 *
Hanno Becker9a467772018-12-13 09:54:59 +0000833 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100834 * \param f_rng The RNG function. This is used for blinding and is
835 * mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000836 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100837 * \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100838 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000839 * This must be a readable buffer of length \p label_len
840 * Bytes. It may be \c NULL if \p label_len is \c 0.
841 * \param label_len The length of the label in Bytes.
842 * \param olen The address at which to store the length of
843 * the plaintext. This must not be \c NULL.
844 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000845 * of length \c ctx->len Bytes. For example, \c 256 Bytes
846 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000847 * \param output The buffer used to hold the plaintext. This must
848 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000849 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100850 *
851 * \return \c 0 on success.
852 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100853 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200855 int (*f_rng)(void *, unsigned char *, size_t),
856 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100857 const unsigned char *label, size_t label_len,
858 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100859 const unsigned char *input,
860 unsigned char *output,
861 size_t output_max_len );
862
863/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000864 * \brief This function performs a private RSA operation to sign
865 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000866 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000867 * It is the generic wrapper for performing a PKCS#1
Thomas Daubney140184d2021-05-18 16:04:07 +0100868 * signature.
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000870 * \note The \p sig buffer must be as large as the size
871 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000872 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000873 * \note For PKCS#1 v2.1 encoding, see comments on
874 * mbedtls_rsa_rsassa_pss_sign() for details on
875 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100876 *
Hanno Becker9a467772018-12-13 09:54:59 +0000877 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100878 * \param f_rng The RNG function to use. This is mandatory and
879 * must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000880 * \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 +0100881 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100882 * \param md_alg The message-digest algorithm used to hash the original data.
883 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200884 * \param hashlen The length of the message digest or raw data in Bytes.
885 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
886 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000887 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200888 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000889 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000890 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100891 * for an 2048-bit RSA modulus. A buffer length of
892 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100893 *
894 * \return \c 0 if the signing operation was successful.
895 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000898 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000899 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000901 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000902 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000903 unsigned char *sig );
904
905/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000906 * \brief This function performs a PKCS#1 v1.5 signature
907 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100908 *
Hanno Becker9a467772018-12-13 09:54:59 +0000909 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100910 * \param f_rng The RNG function. This is used for blinding and is
911 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000912 * \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 +0100913 * if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000914 * \param md_alg The message-digest algorithm used to hash the original data.
915 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200916 * \param hashlen The length of the message digest or raw data in Bytes.
917 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
918 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000919 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200920 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +0000921 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000922 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100923 * for an 2048-bit RSA modulus. A buffer length of
924 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100925 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100926 * \return \c 0 if the signing operation was successful.
927 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100928 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200930 int (*f_rng)(void *, unsigned char *, size_t),
931 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100933 unsigned int hashlen,
934 const unsigned char *hash,
935 unsigned char *sig );
936
937/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000938 * \brief This function performs a PKCS#1 v2.1 PSS signature
939 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100940 *
Janos Follathb7953322021-04-01 14:44:17 +0100941 * \note The \c hash_id set in \p ctx by calling
942 * mbedtls_rsa_set_padding() selects the hash used for the
943 * encoding operation and for the mask generation function
944 * (MGF1). For more details on the encoding operation and the
945 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +0000946 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100947 * Specifications</em>.
Rose Zadike8b5b992018-03-27 12:19:47 +0100948 *
Cédric Meuter010ddc22020-04-25 09:24:11 +0200949 * \note This function enforces that the provided salt length complies
950 * with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 v2.2) §9.1.1
951 * step 3. The constraint is that the hash length plus the salt
952 * length plus 2 bytes must be at most the key length. If this
953 * constraint is not met, this function returns
Jaeden Amero3725bb22018-09-07 19:12:36 +0100954 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
955 *
Hanno Becker9a467772018-12-13 09:54:59 +0000956 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100957 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000958 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
959 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100960 * \param md_alg The message-digest algorithm used to hash the original data.
961 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200962 * \param hashlen The length of the message digest or raw data in Bytes.
963 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
964 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +0000965 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +0200966 * This must be a readable buffer of at least \p hashlen Bytes.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200967 * \param saltlen The length of the salt that should be used.
Cédric Meuter010ddc22020-04-25 09:24:11 +0200968 * If passed #MBEDTLS_RSA_SALT_LEN_ANY, the function will use
969 * the largest possible salt length up to the hash length,
970 * which is the largest permitted by some standards including
971 * FIPS 186-4 §5.5.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200972 * \param sig The buffer to hold the signature. This must be a writable
973 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
974 * for an 2048-bit RSA modulus. A buffer length of
975 * #MBEDTLS_MPI_MAX_SIZE is always safe.
976 *
977 * \return \c 0 if the signing operation was successful.
978 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
979 */
980int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
981 int (*f_rng)(void *, unsigned char *, size_t),
982 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200983 mbedtls_md_type_t md_alg,
984 unsigned int hashlen,
985 const unsigned char *hash,
986 int saltlen,
987 unsigned char *sig );
988
989/**
990 * \brief This function performs a PKCS#1 v2.1 PSS signature
991 * operation (RSASSA-PSS-SIGN).
992 *
Janos Follathb7953322021-04-01 14:44:17 +0100993 * \note The \c hash_id set in \p ctx by calling
994 * mbedtls_rsa_set_padding() selects the hash used for the
995 * encoding operation and for the mask generation function
996 * (MGF1). For more details on the encoding operation and the
997 * mask generation function, consult <em>RFC-3447: Public-Key
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200998 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +0100999 * Specifications</em>.
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001000 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001001 * \note This function always uses the maximum possible salt size,
1002 * up to the length of the payload hash. This choice of salt
1003 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
1004 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
Rose Zadike8b5b992018-03-27 12:19:47 +01001005 * minimum salt size which is the hash size minus 2 bytes. If
1006 * this minimum size is too large given the key size (the salt
1007 * size, plus the hash size, plus 2 bytes must be no more than
1008 * the key size in bytes), this function returns
1009 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
1010 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001011 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +01001012 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +01001013 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
1014 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +01001015 * \param md_alg The message-digest algorithm used to hash the original data.
1016 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001017 * \param hashlen The length of the message digest or raw data in Bytes.
1018 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1019 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001020 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001021 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001022 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +00001023 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +01001024 * for an 2048-bit RSA modulus. A buffer length of
1025 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +01001026 *
1027 * \return \c 0 if the signing operation was successful.
1028 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001031 int (*f_rng)(void *, unsigned char *, size_t),
1032 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001034 unsigned int hashlen,
1035 const unsigned char *hash,
1036 unsigned char *sig );
1037
1038/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001039 * \brief This function performs a public RSA operation and checks
1040 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +00001041 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001042 * This is the generic wrapper for performing a PKCS#1
Thomas Daubney68d9cbc2021-05-18 18:45:09 +01001043 * verification.
Paul Bakker5121ce52009-01-03 21:22:43 +00001044 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001045 * \note For PKCS#1 v2.1 encoding, see comments on
1046 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
1047 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +01001048 *
Hanno Becker9a467772018-12-13 09:54:59 +00001049 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001050 * \param md_alg The message-digest algorithm used to hash the original data.
1051 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001052 * \param hashlen The length of the message digest or raw data in Bytes.
1053 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1054 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001055 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001056 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001057 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001058 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1059 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001060 *
1061 * \return \c 0 if the verify operation was successful.
1062 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001063 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001066 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001067 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001068 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +00001069
1070/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001071 * \brief This function performs a PKCS#1 v1.5 verification
1072 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001073 *
Hanno Becker9a467772018-12-13 09:54:59 +00001074 * \param ctx The initialized RSA public key context to use.
Rose Zadik042e97f2018-01-26 16:35:10 +00001075 * \param md_alg The message-digest algorithm used to hash the original data.
1076 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001077 * \param hashlen The length of the message digest or raw data in Bytes.
1078 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1079 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001080 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001081 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001082 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001083 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1084 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001085 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001086 * \return \c 0 if the verify operation was successful.
1087 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001091 unsigned int hashlen,
1092 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001093 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001094
1095/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001096 * \brief This function performs a PKCS#1 v2.1 PSS verification
1097 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001098 *
Janos Follathb7953322021-04-01 14:44:17 +01001099 * \note The \c hash_id set in \p ctx by calling
1100 * mbedtls_rsa_set_padding() selects the hash used for the
1101 * encoding operation and for the mask generation function
1102 * (MGF1). For more details on the encoding operation and the
1103 * mask generation function, consult <em>RFC-3447: Public-Key
Rose Zadik042e97f2018-01-26 16:35:10 +00001104 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
Janos Follathb7953322021-04-01 14:44:17 +01001105 * Specifications</em>. If the \c hash_id set in \p ctx by
1106 * mbedtls_rsa_set_padding() is #MBEDTLS_MD_NONE, the \p md_alg
1107 * parameter is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001108 *
Hanno Becker9a467772018-12-13 09:54:59 +00001109 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001110 * \param md_alg The message-digest algorithm used to hash the original data.
1111 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001112 * \param hashlen The length of the message digest or raw data in Bytes.
1113 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1114 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001115 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001116 * This must be a readable buffer of at least \p hashlen Bytes.
Hanno Becker9a467772018-12-13 09:54:59 +00001117 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001118 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1119 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001120 *
1121 * \return \c 0 if the verify operation was successful.
1122 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001126 unsigned int hashlen,
1127 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001128 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001129
1130/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001131 * \brief This function performs a PKCS#1 v2.1 PSS verification
1132 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001133 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001134 * \note The \p sig buffer must be as large as the size
1135 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001136 *
Janos Follathb7953322021-04-01 14:44:17 +01001137 * \note The \c hash_id set in \p ctx by mbedtls_rsa_set_padding() is
1138 * ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001139 *
Hanno Becker9a467772018-12-13 09:54:59 +00001140 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001141 * \param md_alg The message-digest algorithm used to hash the original data.
1142 * Use #MBEDTLS_MD_NONE for signing raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001143 * \param hashlen The length of the message digest or raw data in Bytes.
1144 * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the
1145 * output length of the corresponding hash algorithm.
Hanno Becker9a467772018-12-13 09:54:59 +00001146 * \param hash The buffer holding the message digest or raw data.
Manuel Pégourié-Gonnarde7885e52021-06-22 12:29:27 +02001147 * This must be a readable buffer of at least \p hashlen Bytes.
Janos Follathb7953322021-04-01 14:44:17 +01001148 * \param mgf1_hash_id The message digest algorithm used for the
1149 * verification operation and the mask generation
1150 * function (MGF1). For more details on the encoding
1151 * operation and the mask generation function, consult
1152 * <em>RFC-3447: Public-Key Cryptography Standards
1153 * (PKCS) #1 v2.1: RSA Cryptography
1154 * Specifications</em>.
Hanno Becker9a467772018-12-13 09:54:59 +00001155 * \param expected_salt_len The length of the salt used in padding. Use
1156 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1157 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001158 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1159 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001160 *
1161 * \return \c 0 if the verify operation was successful.
1162 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001166 unsigned int hashlen,
1167 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001169 int expected_salt_len,
1170 const unsigned char *sig );
1171
1172/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001173 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001174 *
Hanno Becker9a467772018-12-13 09:54:59 +00001175 * \param dst The destination context. This must be initialized.
1176 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001177 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001178 * \return \c 0 on success.
1179 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001182
1183/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001184 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001185 *
Hanno Becker9a467772018-12-13 09:54:59 +00001186 * \param ctx The RSA context to free. May be \c NULL, in which case
1187 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001188 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001191
Ron Eldorfa8f6352017-06-20 15:48:46 +03001192#if defined(MBEDTLS_SELF_TEST)
1193
Paul Bakker5121ce52009-01-03 21:22:43 +00001194/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001195 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001197 * \return \c 0 on success.
1198 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001201
Ron Eldorfa8f6352017-06-20 15:48:46 +03001202#endif /* MBEDTLS_SELF_TEST */
1203
Paul Bakker5121ce52009-01-03 21:22:43 +00001204#ifdef __cplusplus
1205}
1206#endif
1207
Paul Bakker5121ce52009-01-03 21:22:43 +00001208#endif /* rsa.h */