blob: eeb846e285943ef7398b99ae92f5ca8e49afc094 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Rose Zadik21e29262018-04-17 14:08:56 +01004 * \brief This file provides an API for the RSA public-key cryptosystem.
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
Rose Zadike8b5b992018-03-27 12:19:47 +01006 * The RSA public-key cryptosystem is defined in <em>Public-Key
7 * Cryptography Standards (PKCS) #1 v1.5: RSA Encryption</em>
Darryl Green11999bb2018-03-13 15:22:58 +00008 * and <em>Public-Key Cryptography Standards (PKCS) #1 v2.1:
Rose Zadike8b5b992018-03-27 12:19:47 +01009 * RSA Cryptography Specifications</em>.
Rose Zadik042e97f2018-01-26 16:35:10 +000010 *
Darryl Greena40a1012018-01-05 15:33:17 +000011 */
12/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020013 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020014 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#ifndef MBEDTLS_RSA_H
29#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010032#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#endif
Paul Bakkered27a042013-04-18 22:46:23 +020036
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010037#include "mbedtls/bignum.h"
38#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_THREADING_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010041#include "mbedtls/threading.h"
Paul Bakkerc9965dc2013-09-29 14:58:17 +020042#endif
43
Paul Bakker13e2dfe2009-07-28 07:18:38 +000044/*
45 * RSA Error codes
46 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
48#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
49#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Rose Zadik042e97f2018-01-26 16:35:10 +000050#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the validity check of the library. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
52#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
53#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
54#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
55#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030056
Paul Bakker5121ce52009-01-03 21:22:43 +000057/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020058 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000059 */
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Rose Zadike8b5b992018-03-27 12:19:47 +010061#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
62#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Rose Zadik042e97f2018-01-26 16:35:10 +000064#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
65#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020068
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020069/*
70 * The above constants may be used even if the RSA module is compile out,
71 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
72 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010073
Paul Bakker407a0da2013-06-27 14:29:21 +020074#ifdef __cplusplus
75extern "C" {
76#endif
77
Ron Eldor4e6d55d2018-02-07 16:36:15 +020078#if !defined(MBEDTLS_RSA_ALT)
79// Regular implementation
80//
81
Paul Bakker5121ce52009-01-03 21:22:43 +000082/**
Rose Zadik042e97f2018-01-26 16:35:10 +000083 * \brief The RSA context structure.
Hanno Becker5063cd22017-09-29 11:49:12 +010084 *
85 * \note Direct manipulation of the members of this structure
Rose Zadik042e97f2018-01-26 16:35:10 +000086 * is deprecated. All manipulation should instead be done through
87 * the public interface functions.
Paul Bakker5121ce52009-01-03 21:22:43 +000088 */
Dawid Drozd428cc522018-07-24 10:02:47 +020089typedef struct mbedtls_rsa_context
Paul Bakker5121ce52009-01-03 21:22:43 +000090{
Gilles Peskine4337a9c2021-02-09 18:59:42 +010091 int ver; /*!< Reserved for internal purposes.
92 * Do not set this field in application
93 * code. Its meaning might change without
94 * notice. */
Rose Zadik042e97f2018-01-26 16:35:10 +000095 size_t len; /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Rose Zadike8b5b992018-03-27 12:19:47 +010097 mbedtls_mpi N; /*!< The public modulus. */
98 mbedtls_mpi E; /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Rose Zadike8b5b992018-03-27 12:19:47 +0100100 mbedtls_mpi D; /*!< The private exponent. */
101 mbedtls_mpi P; /*!< The first prime factor. */
102 mbedtls_mpi Q; /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100103
Rose Zadikf2ec2882018-04-17 10:27:25 +0100104 mbedtls_mpi DP; /*!< <code>D % (P - 1)</code>. */
105 mbedtls_mpi DQ; /*!< <code>D % (Q - 1)</code>. */
106 mbedtls_mpi QP; /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Rose Zadikf2ec2882018-04-17 10:27:25 +0100108 mbedtls_mpi RN; /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
Rose Zadikf2ec2882018-04-17 10:27:25 +0100110 mbedtls_mpi RP; /*!< cached <code>R^2 mod P</code>. */
111 mbedtls_mpi RQ; /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200112
Rose Zadike8b5b992018-03-27 12:19:47 +0100113 mbedtls_mpi Vi; /*!< The cached blinding value. */
114 mbedtls_mpi Vf; /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000115
Rose Zadik042e97f2018-01-26 16:35:10 +0000116 int padding; /*!< Selects padding mode:
117 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
118 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
119 int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
120 as specified in md.h for use in the MGF
121 mask generating function used in the
122 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#if defined(MBEDTLS_THREADING_C)
Gilles Peskine4337a9c2021-02-09 18:59:42 +0100124 /* Invariant: the mutex is initialized iff ver != 0. */
Rose Zadik042e97f2018-01-26 16:35:10 +0000125 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200126#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000127}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200130#else /* MBEDTLS_RSA_ALT */
131#include "rsa_alt.h"
132#endif /* MBEDTLS_RSA_ALT */
133
Paul Bakker5121ce52009-01-03 21:22:43 +0000134/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000135 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000137 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000138 * encryption scheme and the RSASSA-PSS signature scheme.
139 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000140 * \note The \p hash_id parameter is ignored when using
141 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200142 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000143 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200144 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000145 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100146 * a default value, which can be overridden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000147 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200148 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000149 * \note The hash selected in \p hash_id is always used for OEAP
150 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100151 * making signatures, but can be overridden for verifying them.
152 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100153 *
Hanno Becker9a467772018-12-13 09:54:59 +0000154 * \param ctx The RSA context to initialize. This must not be \c NULL.
155 * \param padding The padding mode to use. This must be either
156 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Hanno Becker385ce912018-12-13 18:33:12 +0000157 * \param hash_id The hash identifier of ::mbedtls_md_type_t type, if
Hanno Becker9a467772018-12-13 09:54:59 +0000158 * \p padding is #MBEDTLS_RSA_PKCS_V21. It is unused
159 * otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100162 int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000163 int hash_id );
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
165/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000166 * \brief This function imports a set of core parameters into an
167 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100168 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100169 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000170 * imports, if the parameters are not simultaneously present.
171 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100172 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000173 * by a call to mbedtls_rsa_complete(), which checks and
174 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100175 * public or private RSA key.
176 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000177 * \note See mbedtls_rsa_complete() for more information on which
178 * parameters are necessary to set up a private or public
179 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100180 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100181 * \note The imported parameters are copied and need not be preserved
182 * for the lifetime of the RSA context being set up.
183 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100184 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000185 * \param N The RSA modulus. This may be \c NULL.
186 * \param P The first prime factor of \p N. This may be \c NULL.
187 * \param Q The second prime factor of \p N. This may be \c NULL.
188 * \param D The private exponent. This may be \c NULL.
189 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100190 *
191 * \return \c 0 on success.
192 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100193 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100194int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
195 const mbedtls_mpi *N,
196 const mbedtls_mpi *P, const mbedtls_mpi *Q,
197 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100198
199/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000200 * \brief This function imports core RSA parameters, in raw big-endian
201 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100203 * \note This function can be called multiple times for successive
204 * imports, if the parameters are not simultaneously present.
205 *
206 * Any sequence of calls to this function should be followed
207 * by a call to mbedtls_rsa_complete(), which checks and
208 * completes the provided information to a ready-for-use
209 * public or private RSA key.
210 *
211 * \note See mbedtls_rsa_complete() for more information on which
212 * parameters are necessary to set up a private or public
213 * RSA key.
214 *
215 * \note The imported parameters are copied and need not be preserved
216 * for the lifetime of the RSA context being set up.
217 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000218 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000219 * \param N The RSA modulus. This may be \c NULL.
220 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
221 * \param P The first prime factor of \p N. This may be \c NULL.
222 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
223 * \param Q The second prime factor of \p N. This may be \c NULL.
224 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
225 * \param D The private exponent. This may be \c NULL.
226 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
227 * \param E The public exponent. This may be \c NULL.
228 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100230 * \return \c 0 on success.
231 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100232 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100233int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100234 unsigned char const *N, size_t N_len,
235 unsigned char const *P, size_t P_len,
236 unsigned char const *Q, size_t Q_len,
237 unsigned char const *D, size_t D_len,
238 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100239
240/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000241 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100242 * a set of imported core parameters.
243 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000244 * To setup an RSA public key, precisely \p N and \p E
245 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100246 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000247 * To setup an RSA private key, sufficient information must
248 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100249 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000250 * The default implementation supports the following:
251 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
252 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
253 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100254 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000255 * If this function runs successfully, it guarantees that
256 * the RSA context can be used for RSA operations without
257 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100258 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100259 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000260 * for the imported parameters. In particular, parameters that
261 * are not needed by the implementation might be silently
262 * discarded and left unchecked. To check the consistency
263 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100264 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100265 * \param ctx The initialized RSA context holding imported parameters.
266 *
267 * \return \c 0 on success.
268 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
269 * failed.
270 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100271 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100272int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100273
274/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000275 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100276 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000277 * If this function runs successfully, the non-NULL buffers
278 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
279 * written, with additional unused space filled leading by
280 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100281 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000282 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300283 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000284 * <li>An alternative RSA implementation is in use, which
285 * stores the key externally, and either cannot or should
286 * not export it into RAM.</li>
287 * <li>A SW or HW implementation might not support a certain
288 * deduction. For example, \p P, \p Q from \p N, \p D,
289 * and \p E if the former are not part of the
290 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100291 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000292 * If the function fails due to an unsupported operation,
293 * the RSA context stays intact and remains usable.
294 *
295 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000296 * \param N The MPI to hold the RSA modulus.
297 * This may be \c NULL if this field need not be exported.
298 * \param P The MPI to hold the first prime factor of \p N.
299 * This may be \c NULL if this field need not be exported.
300 * \param Q The MPI to hold the second prime factor of \p N.
301 * This may be \c NULL if this field need not be exported.
302 * \param D The MPI to hold the private exponent.
303 * This may be \c NULL if this field need not be exported.
304 * \param E The MPI to hold the public exponent.
305 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000306 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100307 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300308 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000309 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100310 * functionality or because of security policies.
311 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100312 *
313 */
314int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
315 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
316 mbedtls_mpi *D, mbedtls_mpi *E );
317
318/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000319 * \brief This function exports core parameters of an RSA key
320 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100321 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000322 * If this function runs successfully, the non-NULL buffers
323 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
324 * written, with additional unused space filled leading by
325 * zero Bytes.
326 *
327 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300328 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000329 * <li>An alternative RSA implementation is in use, which
330 * stores the key externally, and either cannot or should
331 * not export it into RAM.</li>
332 * <li>A SW or HW implementation might not support a certain
333 * deduction. For example, \p P, \p Q from \p N, \p D,
334 * and \p E if the former are not part of the
335 * implementation.</li></ul>
336 * If the function fails due to an unsupported operation,
337 * the RSA context stays intact and remains usable.
338 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100339 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100340 * buffer pointers are NULL.
341 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000342 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000343 * \param N The Byte array to store the RSA modulus,
344 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000345 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000346 * \param P The Byte array to hold the first prime factor of \p N,
347 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000348 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000349 * \param Q The Byte array to hold the second prime factor of \p N,
350 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000351 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000352 * \param D The Byte array to hold the private exponent,
353 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000354 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000355 * \param E The Byte array to hold the public exponent,
356 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000357 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100358 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100359 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300360 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000361 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100362 * functionality or because of security policies.
363 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100364 */
365int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
366 unsigned char *N, size_t N_len,
367 unsigned char *P, size_t P_len,
368 unsigned char *Q, size_t Q_len,
369 unsigned char *D, size_t D_len,
370 unsigned char *E, size_t E_len );
371
372/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000373 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100374 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100375 * \note Alternative RSA implementations not using CRT-parameters
376 * internally can implement this function based on
377 * mbedtls_rsa_deduce_opt().
378 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000379 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000380 * \param DP The MPI to hold \c D modulo `P-1`,
381 * or \c NULL if it need not be exported.
382 * \param DQ The MPI to hold \c D modulo `Q-1`,
383 * or \c NULL if it need not be exported.
384 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
385 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100386 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100387 * \return \c 0 on success.
388 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100389 *
390 */
391int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
392 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
393
Paul Bakker5121ce52009-01-03 21:22:43 +0000394/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000395 * \brief This function sets padding for an already initialized RSA
396 * context. See mbedtls_rsa_init() for details.
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 *
Hanno Becker9a467772018-12-13 09:54:59 +0000398 * \param ctx The initialized RSA context to be configured.
399 * \param padding The padding mode to use. This must be either
400 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Rose Zadik042e97f2018-01-26 16:35:10 +0000401 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Ronald Cronea7631b2021-06-03 18:51:59 +0200402 * #MBEDTLS_MD_NONE is accepted by this function but may be
403 * not suitable for some operations.
404 *
405 * \return \c 0 on success.
406 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING failure:
407 * \p padding or \p hash_id is invalid.
Paul Bakker40e46942009-01-03 21:51:57 +0000408 */
Ronald Cronea7631b2021-06-03 18:51:59 +0200409int mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
410 mbedtls_md_type_t hash_id );
Paul Bakker21eb2802010-08-16 11:10:02 +0000411
Paul Bakkera3d195c2011-11-27 21:07:34 +0000412/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000413 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100414 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000415 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100416 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000417 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100418 *
419 */
420size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
421
422/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000423 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000425 * \note mbedtls_rsa_init() must be called before this function,
426 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 *
Hanno Becker9a467772018-12-13 09:54:59 +0000428 * \param ctx The initialized RSA context used to hold the key.
429 * \param f_rng The RNG function to be used for key generation.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100430 * This is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000431 * \param p_rng The RNG context to be passed to \p f_rng.
432 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100433 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000434 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000435 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100436 *
437 * \return \c 0 on success.
438 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100441 int (*f_rng)(void *, unsigned char *, size_t),
442 void *p_rng,
443 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000444
445/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000446 * \brief This function checks if a context contains at least an RSA
447 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000448 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000449 * If the function runs successfully, it is guaranteed that
450 * enough information is present to perform an RSA public key
451 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 *
Hanno Becker9a467772018-12-13 09:54:59 +0000453 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000454 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100455 * \return \c 0 on success.
456 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100457 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000462 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100463 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000464 *
Hanno Becker68767a62017-10-17 10:13:31 +0100465 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000466 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100467 * on the given context, but that the various parameters are
468 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000469 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100470 *
471 * \warning This function should catch accidental misconfigurations
472 * like swapping of parameters, but it cannot establish full
473 * trust in neither the quality nor the consistency of the key
474 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000475 * <ul><li>Consistency: Imported parameters that are irrelevant
476 * for the implementation might be silently dropped. If dropped,
477 * the current function does not have access to them,
478 * and therefore cannot check them. See mbedtls_rsa_complete().
479 * If you want to check the consistency of the entire
480 * content of an PKCS1-encoded RSA private key, for example, you
481 * should use mbedtls_rsa_validate_params() before setting
482 * up the RSA context.
483 * Additionally, if the implementation performs empirical checks,
484 * these checks substantiate but do not guarantee consistency.</li>
485 * <li>Quality: This function is not expected to perform
486 * extended quality assessments like checking that the prime
487 * factors are safe. Additionally, it is the responsibility of the
488 * user to ensure the trustworthiness of the source of his RSA
489 * parameters, which goes beyond what is effectively checkable
490 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100491 *
Hanno Becker9a467772018-12-13 09:54:59 +0000492 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100493 *
494 * \return \c 0 on success.
495 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000498
499/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000500 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100501 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000502 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100503 *
Hanno Becker9a467772018-12-13 09:54:59 +0000504 * \param pub The initialized RSA context holding the public key.
505 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000506 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100507 * \return \c 0 on success.
508 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100509 */
Hanno Becker98838b02017-10-02 13:16:10 +0100510int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
511 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100512
513/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000514 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 *
Hanno Becker9a467772018-12-13 09:54:59 +0000516 * \param ctx The initialized RSA context to use.
517 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000518 * of length \c ctx->len Bytes. For example, \c 256 Bytes
519 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000520 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000521 * of length \c ctx->len Bytes. For example, \c 256 Bytes
522 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000523 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000524 * \note This function does not handle message padding.
525 *
526 * \note Make sure to set \p input[0] = 0 or ensure that
527 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100529 * \return \c 0 on success.
530 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000533 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 unsigned char *output );
535
536/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000537 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 *
Hanno Becker24120612017-10-26 11:53:35 +0100539 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100540 *
541 * \note If blinding is used, both the base of exponentation
Hanno Becker24120612017-10-26 11:53:35 +0100542 * and the exponent are blinded, providing protection
543 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100544 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100545 * \warning It is deprecated and a security risk to not provide
546 * a PRNG here and thereby prevent the use of blinding.
547 * Future versions of the library may enforce the presence
548 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100549 *
Hanno Becker9a467772018-12-13 09:54:59 +0000550 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100551 * \param f_rng The RNG function, used for blinding. It is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000552 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
Thomas Daubney03412782021-05-20 15:31:17 +0100553 * if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000554 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000555 * of length \c ctx->len Bytes. For example, \c 256 Bytes
556 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000557 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000558 * of length \c ctx->len Bytes. For example, \c 256 Bytes
559 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100560 *
561 * \return \c 0 on success.
562 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
563 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200566 int (*f_rng)(void *, unsigned char *, size_t),
567 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000568 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 unsigned char *output );
570
571/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000572 * \brief This function adds the message padding, then performs an RSA
573 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000575 * It is the generic wrapper for performing a PKCS#1 encryption
Thomas Daubney21772772021-05-13 17:30:32 +0100576 * operation.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100577 *
Hanno Becker9a467772018-12-13 09:54:59 +0000578 * \param ctx The initialized RSA context to use.
Thomas Daubneyf54c5c52021-05-21 17:00:30 +0100579 * \param f_rng The RNG to use. It is used for padding generation
Thomas Daubney2c65db92021-05-21 10:58:28 +0100580 * and it is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000581 * \param p_rng The RNG context to be passed to \p f_rng. May be
Thomas Daubney03412782021-05-20 15:31:17 +0100582 * \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000583 * \param ilen The length of the plaintext in Bytes.
584 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000585 * buffer of size \p ilen Bytes. It may be \c NULL if
586 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000587 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000588 * of length \c ctx->len Bytes. For example, \c 256 Bytes
589 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100590 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100591 * \return \c 0 on success.
592 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000595 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000596 void *p_rng,
Thomas Daubney21772772021-05-13 17:30:32 +0100597 size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000598 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 unsigned char *output );
600
601/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000602 * \brief This function performs a PKCS#1 v1.5 encryption operation
603 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100604 *
Hanno Becker9a467772018-12-13 09:54:59 +0000605 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100606 * \param f_rng The RNG function to use. It is mandatory and used for
607 * padding generation.
Hanno Becker9a467772018-12-13 09:54:59 +0000608 * \param p_rng The RNG context to be passed to \p f_rng. This may
Thomas Daubney03412782021-05-20 15:31:17 +0100609 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000610 * \param ilen The length of the plaintext in Bytes.
611 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000612 * buffer of size \p ilen Bytes. It may be \c NULL if
613 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000614 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000615 * of length \c ctx->len Bytes. For example, \c 256 Bytes
616 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100617 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100618 * \return \c 0 on success.
619 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100622 int (*f_rng)(void *, unsigned char *, size_t),
623 void *p_rng,
Thomas Daubney53e4ac62021-05-13 18:26:49 +0100624 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100625 const unsigned char *input,
626 unsigned char *output );
627
628/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000629 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
630 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100631 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100632 * \note The output buffer must be as large as the size
633 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
634 *
Hanno Becker9a467772018-12-13 09:54:59 +0000635 * \param ctx The initnialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000636 * \param f_rng The RNG function to use. This is needed for padding
Thomas Daubney2c65db92021-05-21 10:58:28 +0100637 * generation and is mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000638 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000639 * be \c NULL if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000640 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000641 * This must be a readable buffer of length \p label_len
642 * Bytes. It may be \c NULL if \p label_len is \c 0.
643 * \param label_len The length of the label in Bytes.
644 * \param ilen The length of the plaintext buffer \p input in Bytes.
645 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000646 * buffer of size \p ilen Bytes. It may be \c NULL if
647 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000648 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000649 * of length \c ctx->len Bytes. For example, \c 256 Bytes
650 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100651 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100652 * \return \c 0 on success.
653 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100654 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100656 int (*f_rng)(void *, unsigned char *, size_t),
657 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100658 const unsigned char *label, size_t label_len,
659 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100660 const unsigned char *input,
661 unsigned char *output );
662
663/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000664 * \brief This function performs an RSA operation, then removes the
665 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000667 * It is the generic wrapper for performing a PKCS#1 decryption
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100668 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100670 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000671 * as large as the size \p ctx->len of \p ctx->N (for example,
672 * 128 Bytes if RSA-1024 is used) to be able to hold an
673 * arbitrary decrypted message. If it is not large enough to
674 * hold the decryption of the particular ciphertext provided,
675 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100676 *
Hanno Becker9a467772018-12-13 09:54:59 +0000677 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100678 * \param f_rng The RNG function. This is used for blinding and is
679 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000680 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100681 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000682 * \param olen The address at which to store the length of
683 * the plaintext. This must not be \c NULL.
684 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000685 * of length \c ctx->len Bytes. For example, \c 256 Bytes
686 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000687 * \param output The buffer used to hold the plaintext. This must
688 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000689 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100690 *
691 * \return \c 0 on success.
692 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200695 int (*f_rng)(void *, unsigned char *, size_t),
696 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100697 size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000698 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000699 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000700 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000701
702/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000703 * \brief This function performs a PKCS#1 v1.5 decryption
704 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100705 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100706 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000707 * as large as the size \p ctx->len of \p ctx->N, for example,
708 * 128 Bytes if RSA-1024 is used, to be able to hold an
709 * arbitrary decrypted message. If it is not large enough to
710 * hold the decryption of the particular ciphertext provided,
711 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100712 *
Hanno Becker9a467772018-12-13 09:54:59 +0000713 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100714 * \param f_rng The RNG function. This is used for blinding and is
715 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000716 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100717 * \c NULL if \p f_rng doesn't need a context.
Hanno Becker9a467772018-12-13 09:54:59 +0000718 * \param olen The address at which to store the length of
719 * the plaintext. This must not be \c NULL.
720 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000721 * of length \c ctx->len Bytes. For example, \c 256 Bytes
722 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000723 * \param output The buffer used to hold the plaintext. This must
724 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000725 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100726 *
727 * \return \c 0 on success.
728 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
729 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100730 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200732 int (*f_rng)(void *, unsigned char *, size_t),
733 void *p_rng,
Thomas Daubney34733082021-05-12 09:24:29 +0100734 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100735 const unsigned char *input,
736 unsigned char *output,
737 size_t output_max_len );
738
739/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100740 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
741 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100742 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100743 * \note The output buffer length \c output_max_len should be
744 * as large as the size \p ctx->len of \p ctx->N, for
745 * example, 128 Bytes if RSA-1024 is used, to be able to
746 * hold an arbitrary decrypted message. If it is not
747 * large enough to hold the decryption of the particular
748 * ciphertext provided, the function returns
749 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100750 *
Hanno Becker9a467772018-12-13 09:54:59 +0000751 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100752 * \param f_rng The RNG function. This is used for blinding and is
753 * mandatory.
Hanno Becker9a467772018-12-13 09:54:59 +0000754 * \param p_rng The RNG context to be passed to \p f_rng. This may be
Thomas Daubney03412782021-05-20 15:31:17 +0100755 * \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100756 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000757 * This must be a readable buffer of length \p label_len
758 * Bytes. It may be \c NULL if \p label_len is \c 0.
759 * \param label_len The length of the label in Bytes.
760 * \param olen The address at which to store the length of
761 * the plaintext. This must not be \c NULL.
762 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000763 * of length \c ctx->len Bytes. For example, \c 256 Bytes
764 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000765 * \param output The buffer used to hold the plaintext. This must
766 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000767 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100768 *
769 * \return \c 0 on success.
770 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200773 int (*f_rng)(void *, unsigned char *, size_t),
774 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100775 const unsigned char *label, size_t label_len,
776 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100777 const unsigned char *input,
778 unsigned char *output,
779 size_t output_max_len );
780
781/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000782 * \brief This function performs a private RSA operation to sign
783 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000784 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000785 * It is the generic wrapper for performing a PKCS#1
Thomas Daubney140184d2021-05-18 16:04:07 +0100786 * signature.
Paul Bakker5121ce52009-01-03 21:22:43 +0000787 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000788 * \note The \p sig buffer must be as large as the size
789 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000790 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000791 * \note For PKCS#1 v2.1 encoding, see comments on
792 * mbedtls_rsa_rsassa_pss_sign() for details on
793 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +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 to use. This is mandatory and
797 * must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000798 * \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 +0100799 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100800 * \param md_alg The message-digest algorithm used to hash the original data.
801 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000802 * \param hashlen The length of the message digest.
803 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
804 * \param hash The buffer holding the message digest or raw data.
805 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
806 * buffer of length \p hashlen Bytes. If \p md_alg is not
807 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
808 * the size of the hash corresponding to \p md_alg.
809 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000810 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100811 * for an 2048-bit RSA modulus. A buffer length of
812 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100813 *
814 * \return \c 0 if the signing operation was successful.
815 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000818 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000819 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000821 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000822 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000823 unsigned char *sig );
824
825/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000826 * \brief This function performs a PKCS#1 v1.5 signature
827 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100828 *
Hanno Becker9a467772018-12-13 09:54:59 +0000829 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100830 * \param f_rng The RNG function. This is used for blinding and is
831 * mandatory; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000832 * \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 +0100833 * if \p f_rng doesn't need a context argument.
Rose Zadik042e97f2018-01-26 16:35:10 +0000834 * \param md_alg The message-digest algorithm used to hash the original data.
835 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000836 * \param hashlen The length of the message digest.
837 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
838 * \param hash The buffer holding the message digest or raw data.
839 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
840 * buffer of length \p hashlen Bytes. If \p md_alg is not
841 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
842 * the size of the hash corresponding to \p md_alg.
843 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000844 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100845 * for an 2048-bit RSA modulus. A buffer length of
846 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Paul Bakkerb3869132013-02-28 17:21:01 +0100847 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100848 * \return \c 0 if the signing operation was successful.
849 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100850 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200852 int (*f_rng)(void *, unsigned char *, size_t),
853 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100855 unsigned int hashlen,
856 const unsigned char *hash,
857 unsigned char *sig );
858
859/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000860 * \brief This function performs a PKCS#1 v2.1 PSS signature
861 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100862 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000863 * \note The \p hash_id in the RSA context is the one used for the
864 * encoding. \p md_alg in the function call is the type of hash
865 * that is encoded. According to <em>RFC-3447: Public-Key
866 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
867 * Specifications</em> it is advised to keep both hashes the
868 * same.
Rose Zadike8b5b992018-03-27 12:19:47 +0100869 *
Cédric Meuter010ddc22020-04-25 09:24:11 +0200870 * \note This function enforces that the provided salt length complies
871 * with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 v2.2) §9.1.1
872 * step 3. The constraint is that the hash length plus the salt
873 * length plus 2 bytes must be at most the key length. If this
874 * constraint is not met, this function returns
Jaeden Amero3725bb22018-09-07 19:12:36 +0100875 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
876 *
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. It is mandatory and must not be \c NULL.
Hanno Becker9a467772018-12-13 09:54:59 +0000879 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
880 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100881 * \param md_alg The message-digest algorithm used to hash the original data.
882 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000883 * \param hashlen The length of the message digest.
884 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
885 * \param hash The buffer holding the message digest or raw data.
886 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
887 * buffer of length \p hashlen Bytes. If \p md_alg is not
888 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
889 * the size of the hash corresponding to \p md_alg.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200890 * \param saltlen The length of the salt that should be used.
Cédric Meuter010ddc22020-04-25 09:24:11 +0200891 * If passed #MBEDTLS_RSA_SALT_LEN_ANY, the function will use
892 * the largest possible salt length up to the hash length,
893 * which is the largest permitted by some standards including
894 * FIPS 186-4 §5.5.
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200895 * \param sig The buffer to hold the signature. This must be a writable
896 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
897 * for an 2048-bit RSA modulus. A buffer length of
898 * #MBEDTLS_MPI_MAX_SIZE is always safe.
899 *
900 * \return \c 0 if the signing operation was successful.
901 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
902 */
903int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
904 int (*f_rng)(void *, unsigned char *, size_t),
905 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +0200906 mbedtls_md_type_t md_alg,
907 unsigned int hashlen,
908 const unsigned char *hash,
909 int saltlen,
910 unsigned char *sig );
911
912/**
913 * \brief This function performs a PKCS#1 v2.1 PSS signature
914 * operation (RSASSA-PSS-SIGN).
915 *
916 * \note The \p hash_id in the RSA context is the one used for the
917 * encoding. \p md_alg in the function call is the type of hash
918 * that is encoded. According to <em>RFC-3447: Public-Key
919 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
920 * Specifications</em> it is advised to keep both hashes the
921 * same.
922 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200923 * \note This function always uses the maximum possible salt size,
924 * up to the length of the payload hash. This choice of salt
Paul Bakkerb3869132013-02-28 17:21:01 +0100925 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
926 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
Rose Zadike8b5b992018-03-27 12:19:47 +0100927 * minimum salt size which is the hash size minus 2 bytes. If
928 * this minimum size is too large given the key size (the salt
929 * size, plus the hash size, plus 2 bytes must be no more than
930 * the key size in bytes), this function returns
931 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
932 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100933 * \param ctx The initialized RSA context to use.
Thomas Daubney2c65db92021-05-21 10:58:28 +0100934 * \param f_rng The RNG function. It is mandatory and must not be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100935 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
936 * if \p f_rng doesn't need a context argument.
Rose Zadike8b5b992018-03-27 12:19:47 +0100937 * \param md_alg The message-digest algorithm used to hash the original data.
938 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000939 * \param hashlen The length of the message digest.
940 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
941 * \param hash The buffer holding the message digest or raw data.
942 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
943 * buffer of length \p hashlen Bytes. If \p md_alg is not
944 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
945 * the size of the hash corresponding to \p md_alg.
946 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000947 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
Gilles Peskine73a1f372019-11-08 18:39:22 +0100948 * for an 2048-bit RSA modulus. A buffer length of
949 * #MBEDTLS_MPI_MAX_SIZE is always safe.
Rose Zadike8b5b992018-03-27 12:19:47 +0100950 *
951 * \return \c 0 if the signing operation was successful.
952 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100953 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100955 int (*f_rng)(void *, unsigned char *, size_t),
956 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100958 unsigned int hashlen,
959 const unsigned char *hash,
960 unsigned char *sig );
961
962/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000963 * \brief This function performs a public RSA operation and checks
964 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +0000965 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000966 * This is the generic wrapper for performing a PKCS#1
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100967 * verification.
Paul Bakker5121ce52009-01-03 21:22:43 +0000968 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000969 * \note For PKCS#1 v2.1 encoding, see comments on
970 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
971 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100972 *
Hanno Becker9a467772018-12-13 09:54:59 +0000973 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +0100974 * \param md_alg The message-digest algorithm used to hash the original data.
975 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000976 * \param hashlen The length of the message digest.
977 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
978 * \param hash The buffer holding the message digest or raw data.
979 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
980 * buffer of length \p hashlen Bytes. If \p md_alg is not
981 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
982 * the size of the hash corresponding to \p md_alg.
983 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +0000984 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
985 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100986 *
987 * \return \c 0 if the verify operation was successful.
988 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000992 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000993 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200994 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000995
996/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000997 * \brief This function performs a PKCS#1 v1.5 verification
998 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +0100999 *
Hanno Becker9a467772018-12-13 09:54:59 +00001000 * \param ctx The initialized RSA public key context to use.
Rose Zadik042e97f2018-01-26 16:35:10 +00001001 * \param md_alg The message-digest algorithm used to hash the original data.
1002 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001003 * \param hashlen The length of the message digest.
1004 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1005 * \param hash The buffer holding the message digest or raw data.
1006 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1007 * buffer of length \p hashlen Bytes. If \p md_alg is not
1008 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1009 * the size of the hash corresponding to \p md_alg.
1010 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001011 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1012 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001013 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001014 * \return \c 0 if the verify operation was successful.
1015 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001019 unsigned int hashlen,
1020 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001021 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001022
1023/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001024 * \brief This function performs a PKCS#1 v2.1 PSS verification
1025 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001026 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001027 * The hash function for the MGF mask generating function
1028 * is that specified in the RSA context.
Paul Bakkerb3869132013-02-28 17:21:01 +01001029 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001030 * \note The \p hash_id in the RSA context is the one used for the
1031 * verification. \p md_alg in the function call is the type of
1032 * hash that is verified. According to <em>RFC-3447: Public-Key
1033 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1034 * Specifications</em> it is advised to keep both hashes the
1035 * same. If \p hash_id in the RSA context is unset,
1036 * the \p md_alg from the function call is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001037 *
Hanno Becker9a467772018-12-13 09:54:59 +00001038 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001039 * \param md_alg The message-digest algorithm used to hash the original data.
1040 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001041 * \param hashlen The length of the message digest.
1042 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1043 * \param hash The buffer holding the message digest or raw data.
1044 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1045 * buffer of length \p hashlen Bytes. If \p md_alg is not
1046 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1047 * the size of the hash corresponding to \p md_alg.
1048 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001049 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1050 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001051 *
1052 * \return \c 0 if the verify operation was successful.
1053 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001057 unsigned int hashlen,
1058 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001059 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001060
1061/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001062 * \brief This function performs a PKCS#1 v2.1 PSS verification
1063 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001064 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001065 * The hash function for the MGF mask generating function
1066 * is that specified in \p mgf1_hash_id.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001067 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001068 * \note The \p sig buffer must be as large as the size
1069 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001070 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001071 * \note The \p hash_id in the RSA context is ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001072 *
Hanno Becker9a467772018-12-13 09:54:59 +00001073 * \param ctx The initialized RSA public key context to use.
Rose Zadike8b5b992018-03-27 12:19:47 +01001074 * \param md_alg The message-digest algorithm used to hash the original data.
1075 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001076 * \param hashlen The length of the message digest.
1077 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1078 * \param hash The buffer holding the message digest or raw data.
1079 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1080 * buffer of length \p hashlen Bytes. If \p md_alg is not
1081 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1082 * the size of the hash corresponding to \p md_alg.
1083 * \param mgf1_hash_id The message digest used for mask generation.
1084 * \param expected_salt_len The length of the salt used in padding. Use
1085 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1086 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001087 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1088 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001089 *
1090 * \return \c 0 if the verify operation was successful.
1091 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001092 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001095 unsigned int hashlen,
1096 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001098 int expected_salt_len,
1099 const unsigned char *sig );
1100
1101/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001102 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001103 *
Hanno Becker9a467772018-12-13 09:54:59 +00001104 * \param dst The destination context. This must be initialized.
1105 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001106 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001107 * \return \c 0 on success.
1108 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001111
1112/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001113 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001114 *
Hanno Becker9a467772018-12-13 09:54:59 +00001115 * \param ctx The RSA context to free. May be \c NULL, in which case
1116 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001117 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001120
Ron Eldorfa8f6352017-06-20 15:48:46 +03001121#if defined(MBEDTLS_SELF_TEST)
1122
Paul Bakker5121ce52009-01-03 21:22:43 +00001123/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001124 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001125 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001126 * \return \c 0 on success.
1127 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001130
Ron Eldorfa8f6352017-06-20 15:48:46 +03001131#endif /* MBEDTLS_SELF_TEST */
1132
Paul Bakker5121ce52009-01-03 21:22:43 +00001133#ifdef __cplusplus
1134}
1135#endif
1136
Paul Bakker5121ce52009-01-03 21:22:43 +00001137#endif /* rsa.h */