blob: d7503ac83136fb13cfebbc9f8bce8bd73d5c0d01 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief The RSA public-key cryptosystem
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_RSA_H
24#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020027#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakkered27a042013-04-18 22:46:23 +020031
Paul Bakker314052f2011-08-15 09:07:52 +000032#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020033#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_THREADING_C)
Paul Bakkerc9965dc2013-09-29 14:58:17 +020036#include "threading.h"
37#endif
38
Paul Bakker13e2dfe2009-07-28 07:18:38 +000039/*
40 * RSA Error codes
41 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
43#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
44#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +020045#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the library's validity check. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
47#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
48#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
49#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
50#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Hanno Becker1434a362017-12-13 11:24:49 +000051#define MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION -0x4500 /**< The implementation doesn't offer the requested operation, e.g. because of security violations or lack of functionality */
Paul Bakker5121ce52009-01-03 21:22:43 +000052
53/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020054 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define MBEDTLS_RSA_PUBLIC 0
57#define MBEDTLS_RSA_PRIVATE 1
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define MBEDTLS_RSA_PKCS_V15 0
60#define MBEDTLS_RSA_PKCS_V21 1
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define MBEDTLS_RSA_SIGN 1
63#define MBEDTLS_RSA_CRYPT 2
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020066
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020067/*
68 * The above constants may be used even if the RSA module is compile out,
69 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
70 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010071
72#if !defined(MBEDTLS_RSA_ALT)
73// Regular implementation
74//
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020075
Paul Bakker407a0da2013-06-27 14:29:21 +020076#ifdef __cplusplus
77extern "C" {
78#endif
79
Hanno Beckera3ebec22017-08-23 14:06:24 +010080/**
Hanno Becker5063cd22017-09-29 11:49:12 +010081 * \brief RSA context structure
82 *
83 * \note Direct manipulation of the members of this structure
84 * is deprecated and will no longer be supported starting
85 * from the next major release. All manipulation should instead
86 * be done through the public interface functions.
87 *
Paul Bakker5121ce52009-01-03 21:22:43 +000088 */
89typedef struct
90{
91 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +000092 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +000093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_mpi N; /*!< public modulus */
95 mbedtls_mpi E; /*!< public exponent */
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_mpi D; /*!< private exponent */
98 mbedtls_mpi P; /*!< 1st prime factor */
99 mbedtls_mpi Q; /*!< 2nd prime factor */
Hanno Becker1a59e792017-08-23 07:41:10 +0100100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_mpi DP; /*!< D % (P - 1) */
102 mbedtls_mpi DQ; /*!< D % (Q - 1) */
103 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_mpi RN; /*!< cached R^2 mod N */
Hanno Becker1a59e792017-08-23 07:41:10 +0100106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_mpi RP; /*!< cached R^2 mod P */
108 mbedtls_mpi RQ; /*!< cached R^2 mod Q */
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_mpi Vi; /*!< cached blinding value */
111 mbedtls_mpi Vf; /*!< cached un-blinding value */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200112
Hanno Becker4b2f6912017-09-29 13:34:55 +0100113 int padding; /*!< \c MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
114 \c MBEDTLS_RSA_PKCS_v21 for OAEP/PSS */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
116 specified in the mbedtls_md.h header file
Paul Bakker9dcc3222011-03-08 14:16:06 +0000117 for the EME-OAEP and EMSA-PSS
118 encoding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119#if defined(MBEDTLS_THREADING_C)
120 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200121#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000122}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
Paul Bakker5121ce52009-01-03 21:22:43 +0000125/**
126 * \brief Initialize an RSA context
127 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100128 * Note: Set padding to \c MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000129 * encryption scheme and the RSASSA-PSS signature scheme.
130 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 * \param ctx RSA context to be initialized
Hanno Becker4b2f6912017-09-29 13:34:55 +0100132 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
133 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 *
135 * \note The hash_id parameter is actually ignored
Hanno Becker4b2f6912017-09-29 13:34:55 +0100136 * when using \c MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200137 *
138 * \note Choice of padding mode is strictly enforced for private key
139 * operations, since there might be security concerns in
140 * mixing padding modes. For public key operations it's merely
141 * a default value, which can be overriden by calling specific
142 * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
143 *
144 * \note The chosen hash is always used for OEAP encryption.
145 * For PSS signatures, it's always used for making signatures,
146 * but can be overriden (and always is, if set to
Hanno Becker4b2f6912017-09-29 13:34:55 +0100147 * \c MBEDTLS_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100150 int padding,
151 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100153/**
154 * \brief Import a set of core parameters into an RSA context
155 *
156 * \param ctx Initialized RSA context to store parameters
157 * \param N RSA modulus, or NULL
158 * \param P First prime factor of N, or NULL
159 * \param Q Second prime factor of N, or NULL
160 * \param D Private exponent, or NULL
161 * \param E Public exponent, or NULL
162 *
163 * \note This function can be called multiple times for successive
164 * imports if the parameters are not simultaneously present.
165 * Any sequence of calls to this function should be followed
166 * by a call to \c mbedtls_rsa_complete which will check
167 * and complete the provided information to a ready-for-use
168 * public or private RSA key.
169 *
Hanno Becker33195552017-10-25 17:04:10 +0100170 * \note See the documentation of \c mbedtls_rsa_complete for more
171 * information on which parameters are necessary to setup
172 * a private or public RSA key.
173 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100174 * \note The imported parameters are copied and need not be preserved
175 * for the lifetime of the RSA context being set up.
176 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100177 * \return 0 if successful, non-zero error code on failure.
178 */
179int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
180 const mbedtls_mpi *N,
181 const mbedtls_mpi *P, const mbedtls_mpi *Q,
182 const mbedtls_mpi *D, const mbedtls_mpi *E );
183
184/**
185 * \brief Import core RSA parameters in raw big-endian
186 * binary format into an RSA context
187 *
188 * \param ctx Initialized RSA context to store parameters
189 * \param N RSA modulus, or NULL
190 * \param N_len Byte length of N, ignored if N == NULL
191 * \param P First prime factor of N, or NULL
192 * \param P_len Byte length of P, ignored if P == NULL
193 * \param Q Second prime factor of N, or NULL
194 * \param Q_len Byte length of Q, ignored if Q == NULL
195 * \param D Private exponent, or NULL
196 * \param D_len Byte length of D, ignored if D == NULL
197 * \param E Public exponent, or NULL
198 * \param E_len Byte length of E, ignored if E == NULL
199 *
200 * \note This function can be called multiple times for successive
201 * imports if the parameters are not simultaneously present.
202 * Any sequence of calls to this function should be followed
203 * by a call to \c mbedtls_rsa_complete which will check
204 * and complete the provided information to a ready-for-use
205 * public or private RSA key.
206 *
Hanno Becker33195552017-10-25 17:04:10 +0100207 * \note See the documentation of \c mbedtls_rsa_complete for more
208 * information on which parameters are necessary to setup
209 * a private or public RSA key.
210 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100211 * \note The imported parameters are copied and need not be preserved
212 * for the lifetime of the RSA context being set up.
213 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100214 * \return 0 if successful, non-zero error code on failure.
215 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100216int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100217 unsigned char const *N, size_t N_len,
218 unsigned char const *P, size_t P_len,
219 unsigned char const *Q, size_t Q_len,
220 unsigned char const *D, size_t D_len,
221 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100222
223/**
224 * \brief Attempt to complete an RSA context from
225 * a set of imported core parameters.
226 *
227 * \param ctx Initialized RSA context to store parameters
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100228 *
Hanno Beckered203612017-09-29 13:34:25 +0100229 * \note
230 * - To setup an RSA public key, precisely N and E
231 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100232 *
Hanno Beckered203612017-09-29 13:34:25 +0100233 * - To setup an RSA private key, enough information must be
Hanno Becker98838b02017-10-02 13:16:10 +0100234 * present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100235 *
Hanno Beckered203612017-09-29 13:34:25 +0100236 * The default implementation supports the following:
237 * - Derive P, Q from N, D, E
238 * - Derive N, D from P, Q, E.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100239 *
Hanno Beckered203612017-09-29 13:34:25 +0100240 * - Alternative implementations need not support these
241 * and may return \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA instead.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100242 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100243 * \return
244 * - 0 if successful. In this case, it is guaranteed
Hanno Becker1e801f52017-10-10 16:44:47 +0100245 * that the RSA context can be used for RSA operations
246 * without the risk of failure or crash.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100247 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100248 * derivations failed.
Hanno Becker43a08d02017-10-02 13:16:35 +0100249 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100250 * \warning This function need not perform consistency checks
251 * for the imported parameters! In particular, parameters that
252 * are not needed by the implementation may be silently discarded
253 * and left unchecked. For the purpose of checking the consistency
254 * of the key material, see \c mbedtls_rsa_check_privkey.
Hanno Becker43a08d02017-10-02 13:16:35 +0100255 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100256 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100257int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100258
259/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100260 * \brief Export core parameters of an RSA key
261 *
262 * \param ctx Initialized RSA context
263 * \param N MPI to hold the RSA modulus, or NULL
264 * \param P MPI to hold the first prime factor of N, or NULL
265 * \param Q MPI to hold the second prime factor of N, or NULL
266 * \param D MPI to hold the private exponent, or NULL
267 * \param E MPI to hold the public exponent, or NULL
268 *
Hanno Beckered203612017-09-29 13:34:25 +0100269 * \return
270 * - 0 if successful. In this case, the non-NULL buffers
271 * pointed to by N, P, Q, D, E are fully written, with
272 * additional unused space filled leading by 0-bytes.
273 * - Non-zero return code otherwise. In particular, if
274 * exporting the requested parameters
275 * cannot be done because of a lack of functionality
276 * or because of security policies, the error code
Hanno Beckera47023e2017-12-22 17:08:03 +0000277 * \c MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is returned.
Hanno Beckered203612017-09-29 13:34:25 +0100278 * In this case, the RSA context stays intact and can
279 * be continued to be used.
Hanno Becker91c194d2017-09-29 12:50:12 +0100280 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000281 * \note Reasons for returning \c MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION
Hanno Becker91c194d2017-09-29 12:50:12 +0100282 * would be the following: Firstly, it might be that an
283 * alternative RSA implementation is in use which stores
284 * the key externally, and which either cannot or should not
285 * export it into RAM. Alternatively, an implementation
286 * (regardless of SW or HW) might not support deducing e.g.
287 * P, Q from N, D, E if the former are not part of the
288 * implementation.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100289 *
290 */
291int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
292 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
293 mbedtls_mpi *D, mbedtls_mpi *E );
294
295/**
296 * \brief Export core parameters of an RSA key
297 * in raw big-endian binary format
298 *
299 * \param ctx Initialized RSA context
300 * \param N Byte array to store the RSA modulus, or NULL
301 * \param N_len Size of buffer for modulus
302 * \param P Byte array to hold the first prime factor of N, or NULL
303 * \param P_len Size of buffer for first prime factor
304 * \param Q Byte array to hold the second prime factor of N, or NULL
305 * \param Q_len Size of buffer for second prime factor
306 * \param D Byte array to hold the private exponent, or NULL
307 * \param D_len Size of buffer for private exponent
308 * \param E Byte array to hold the public exponent, or NULL
309 * \param E_len Size of buffer for public exponent
310 *
311 * \note The length fields are ignored if the corresponding
312 * buffer pointers are NULL.
313 *
Hanno Beckered203612017-09-29 13:34:25 +0100314 * \return
315 * - 0 if successful. In this case, the non-NULL buffers
316 * pointed to by N, P, Q, D, E are fully written, with
317 * additional unused space filled leading by 0-bytes.
318 * - Non-zero return code otherwise. In particular, if
319 * exporting the requested parameters
320 * cannot be done because of a lack of functionality
321 * or because of security policies, the error code
Hanno Beckera47023e2017-12-22 17:08:03 +0000322 * \c MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is returned.
Hanno Beckered203612017-09-29 13:34:25 +0100323 * In this case, the RSA context stays intact and can
324 * be continued to be used.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100325 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000326 * \note Reasons for returning \c MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION
Hanno Becker91c194d2017-09-29 12:50:12 +0100327 * would be the following: Firstly, it might be that an
328 * alternative RSA implementation is in use which stores
329 * the key externally, and which either cannot or should not
330 * export it into RAM. Alternatively, an implementation
331 * (regardless of SW or HW) might not support deducing e.g.
332 * P, Q from N, D, E if the former are not part of the
333 * implementation.
334 *
335 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100336 */
337int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
338 unsigned char *N, size_t N_len,
339 unsigned char *P, size_t P_len,
340 unsigned char *Q, size_t Q_len,
341 unsigned char *D, size_t D_len,
342 unsigned char *E, size_t E_len );
343
344/**
345 * \brief Export CRT parameters of a private RSA key
346 *
347 * \param ctx Initialized RSA context
348 * \param DP MPI to hold D modulo P-1, or NULL
349 * \param DQ MPI to hold D modulo Q-1, or NULL
350 * \param QP MPI to hold modular inverse of Q modulo P, or NULL
351 *
352 * \return 0 if successful, non-zero error code otherwise.
353 *
354 * \note Alternative RSA implementations not using CRT-parameters
355 * internally can implement this function using based on
356 * \c mbedtls_rsa_deduce_opt.
357 *
358 */
359int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
360 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
361
Paul Bakker5121ce52009-01-03 21:22:43 +0000362/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100363 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100365 *
366 * \param ctx RSA context to be set
Hanno Becker4b2f6912017-09-29 13:34:55 +0100367 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
368 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100369 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100370void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
371 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100372
373/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100374 * \brief Get length of RSA modulus in bytes
375 *
376 * \param ctx Initialized RSA context
377 *
378 * \return Length of RSA modulus, in bytes.
379 *
380 */
381size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
382
383/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000384 * \brief Generate an RSA keypair
385 *
386 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000387 * \param f_rng RNG function
388 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000389 * \param nbits size of the public key in bits
390 * \param exponent public exponent (e.g., 65537)
391 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000393 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100395 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100398 int (*f_rng)(void *, unsigned char *, size_t),
399 void *p_rng,
400 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000401
402/**
Hanno Becker43a08d02017-10-02 13:16:35 +0100403 * \brief Check if a context contains (at least) an RSA public key
Paul Bakker5121ce52009-01-03 21:22:43 +0000404 *
405 * \param ctx RSA context to be checked
406 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100407 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
408 * On success, it is guaranteed that enough information is
409 * present to perform an RSA public key operation
410 * \c mbedtls_rsa_public.
411 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000414
415/**
Hanno Becker1e801f52017-10-10 16:44:47 +0100416 * \brief Check if a context contains an RSA private key
417 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100419 * \param ctx RSA context to be checked
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100421 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
Hanno Becker43a08d02017-10-02 13:16:35 +0100422 *
Hanno Becker68767a62017-10-17 10:13:31 +0100423 * \note The consistency checks performed by this function not only
424 * ensure that \c mbedtls_rsa_private can be called successfully
425 * on the given context, but that the various parameters are
426 * mutually consistent with high probability, in the sense that
427 * \c mbedtls_rsa_public and \c mbedtls_rsa_private are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100428 *
429 * \warning This function should catch accidental misconfigurations
430 * like swapping of parameters, but it cannot establish full
431 * trust in neither the quality nor the consistency of the key
432 * material that was used to setup the given RSA context:
433 * - Regarding consistency, note (see \c mbedtls_rsa_complete)
434 * that imported parameters irrelevant for the implementation
435 * might be silently dropped, in which case the present
436 * function doesn't have access to and hence cannot check them.
Hanno Beckerfc8fbfa2017-10-17 10:31:15 +0100437 * If you want to check the consistency of the entire
438 * content of, say, an PKCS1-encoded RSA private key, you
Hanno Becker1e801f52017-10-10 16:44:47 +0100439 * should use \c mbedtls_rsa_validate_params before setting
440 * up the RSA context.
441 * Further, if the implementation performs empirical checks,
442 * these checks will substantiate but not guarantee consistency.
443 * - Regarding quality, this function is not expected to perform
444 * extended quality assessments like checking that the prime
445 * factors are safe. Further, it is the user's responsibility to
446 * ensure trustworthiness of the source of his RSA parameters,
447 * a question going beyond what's effectively checkable
448 * by the library.
Hanno Becker43a08d02017-10-02 13:16:35 +0100449 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000452
453/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100454 * \brief Check a public-private RSA key pair.
455 * Check each of the contexts, and make sure they match.
456 *
457 * \param pub RSA context holding the public key
458 * \param prv RSA context holding the private key
459 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100460 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100461 */
Hanno Becker98838b02017-10-02 13:16:10 +0100462int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
463 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100464
465/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 * \brief Do an RSA public key operation
467 *
468 * \param ctx RSA context
469 * \param input input buffer
470 * \param output output buffer
471 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100472 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000473 *
474 * \note This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800475 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000476 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 *
478 * \note The input and output buffers must be large
479 * enough (eg. 128 bytes if RSA-1024 is used).
480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000482 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 unsigned char *output );
484
485/**
486 * \brief Do an RSA private key operation
487 *
488 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200489 * \param f_rng RNG function (Needed for blinding)
490 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000491 * \param input input buffer
492 * \param output output buffer
493 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100494 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 *
496 * \note The input and output buffers must be large
497 * enough (eg. 128 bytes if RSA-1024 is used).
498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200500 int (*f_rng)(void *, unsigned char *, size_t),
501 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000502 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000503 unsigned char *output );
504
505/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100506 * \brief Generic wrapper to perform a PKCS#1 encryption using the
507 * mode from the context. Add the message padding, then do an
508 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 *
510 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200511 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100512 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000513 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100514 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000515 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 * \param input buffer holding the data to be encrypted
517 * \param output buffer that will hold the ciphertext
518 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100519 * \deprecated It is deprecated and discouraged to call this function
520 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
521 * are likely to remove the mode argument and have it implicitly
522 * set to MBEDTLS_RSA_PUBLIC.
523 *
524 * \note Alternative implementations of RSA need not support
525 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
526 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
527 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000528 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 *
530 * \note The output buffer must be as large as the size
531 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
532 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000534 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000535 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000536 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000537 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 unsigned char *output );
539
540/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100541 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
542 *
543 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100544 * \param f_rng RNG function (Needed for padding and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100545 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100546 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100547 * \param ilen contains the plaintext length
548 * \param input buffer holding the data to be encrypted
549 * \param output buffer that will hold the ciphertext
550 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100551 * \deprecated It is deprecated and discouraged to call this function
552 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
553 * are likely to remove the mode argument and have it implicitly
554 * set to MBEDTLS_RSA_PUBLIC.
555 *
556 * \note Alternative implementations of RSA need not support
557 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
558 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
559 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000560 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100561 *
562 * \note The output buffer must be as large as the size
563 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100566 int (*f_rng)(void *, unsigned char *, size_t),
567 void *p_rng,
568 int mode, size_t ilen,
569 const unsigned char *input,
570 unsigned char *output );
571
572/**
573 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
574 *
575 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200576 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100577 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100578 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100579 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100580 * \param label buffer holding the custom label to use
581 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100582 * \param ilen contains the plaintext length
583 * \param input buffer holding the data to be encrypted
584 * \param output buffer that will hold the ciphertext
585 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100586 * \deprecated It is deprecated and discouraged to call this function
587 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
588 * are likely to remove the mode argument and have it implicitly
589 * set to MBEDTLS_RSA_PUBLIC.
590 *
591 * \note Alternative implementations of RSA need not support
592 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
593 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
594 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000595 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100596 *
597 * \note The output buffer must be as large as the size
598 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
599 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100601 int (*f_rng)(void *, unsigned char *, size_t),
602 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100603 int mode,
604 const unsigned char *label, size_t label_len,
605 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100606 const unsigned char *input,
607 unsigned char *output );
608
609/**
610 * \brief Generic wrapper to perform a PKCS#1 decryption using the
611 * mode from the context. Do an RSA operation, then remove
612 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 *
614 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100615 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200616 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100617 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000618 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000619 * \param input buffer holding the encrypted data
620 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000621 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000622 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100623 * \deprecated It is deprecated and discouraged to call this function
624 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
625 * are likely to remove the mode argument and have it implicitly
626 * set to MBEDTLS_RSA_PRIVATE.
627 *
628 * \note Alternative implementations of RSA need not support
629 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
630 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
631 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000632 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000633 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100634 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100635 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100636 * if RSA-1024 is used) to be able to hold an arbitrary
637 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100638 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100639 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100640 *
641 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100642 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200645 int (*f_rng)(void *, unsigned char *, size_t),
646 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000647 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000648 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000649 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000650 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000651
652/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100653 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
654 *
655 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100656 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200657 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100658 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100659 * \param olen will contain the plaintext length
660 * \param input buffer holding the encrypted data
661 * \param output buffer that will hold the plaintext
662 * \param output_max_len maximum length of the output buffer
663 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100664 * \deprecated It is deprecated and discouraged to call this function
665 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
666 * are likely to remove the mode argument and have it implicitly
667 * set to MBEDTLS_RSA_PRIVATE.
668 *
669 * \note Alternative implementations of RSA need not support
670 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
671 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
672 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000673 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100674 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100675 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100676 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100677 * if RSA-1024 is used) to be able to hold an arbitrary
678 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100679 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100680 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100681 *
682 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100683 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100684 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200686 int (*f_rng)(void *, unsigned char *, size_t),
687 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100688 int mode, size_t *olen,
689 const unsigned char *input,
690 unsigned char *output,
691 size_t output_max_len );
692
693/**
694 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
695 *
696 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100697 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200698 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100699 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100700 * \param label buffer holding the custom label to use
701 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100702 * \param olen will contain the plaintext length
703 * \param input buffer holding the encrypted data
704 * \param output buffer that will hold the plaintext
705 * \param output_max_len maximum length of the output buffer
706 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100707 * \deprecated It is deprecated and discouraged to call this function
708 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
709 * are likely to remove the mode argument and have it implicitly
710 * set to MBEDTLS_RSA_PRIVATE.
711 *
712 * \note Alternative implementations of RSA need not support
713 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
714 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
715 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000716 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100717 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100718 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100719 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100720 * if RSA-1024 is used) to be able to hold an arbitrary
721 * decrypted message. If it is not large enough to hold
Hanno Becker8fd55482017-08-23 14:07:48 +0100722 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100723 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100724 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100725 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100726 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Hanno Becker32297e82017-12-22 10:24:32 +0000727 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200730 int (*f_rng)(void *, unsigned char *, size_t),
731 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100732 int mode,
733 const unsigned char *label, size_t label_len,
734 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/**
740 * \brief Generic wrapper to perform a PKCS#1 signature using the
741 * mode from the context. Do a private RSA operation to sign
742 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000743 *
744 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200745 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100746 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000747 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100748 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
749 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for
750 * signing raw data)
751 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000752 * \param hash buffer holding the message digest
753 * \param sig buffer that will hold the ciphertext
754 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100755 * \deprecated It is deprecated and discouraged to call this function
756 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
757 * are likely to remove the mode argument and have it implicitly
758 * set to MBEDTLS_RSA_PRIVATE.
759 *
760 * \note Alternative implementations of RSA need not support
761 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
762 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
763 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000764 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100765 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000766 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100767 * \note The \c sig buffer must be as large as the size
768 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000769 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200770 * \note In case of PKCS#1 v2.1 encoding, see comments on
Hanno Becker32297e82017-12-22 10:24:32 +0000771 * \c mbedtls_rsa_rsassa_pss_sign() for details on
Hanno Becker4b2f6912017-09-29 13:34:55 +0100772 * \c md_alg and \c hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000773 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000775 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000776 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000777 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000779 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000780 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000781 unsigned char *sig );
782
783/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100784 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
785 *
786 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100787 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200788 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100789 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
790 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
791 * for signing raw data)
792 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100793 * \param hash buffer holding the message digest
794 * \param sig buffer that will hold the ciphertext
795 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100796 * \deprecated It is deprecated and discouraged to call this function
797 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
798 * are likely to remove the mode argument and have it implicitly
799 * set to MBEDTLS_RSA_PRIVATE.
800 *
801 * \note Alternative implementations of RSA need not support
802 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
803 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
804 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100805 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100806 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100807 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100808 * \note The \c sig buffer must be as large as the size
809 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100810 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200812 int (*f_rng)(void *, unsigned char *, size_t),
813 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100814 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100816 unsigned int hashlen,
817 const unsigned char *hash,
818 unsigned char *sig );
819
820/**
821 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
822 *
823 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200824 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100825 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100826 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100827 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
828 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
829 * for signing raw data)
830 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100831 * \param hash buffer holding the message digest
832 * \param sig buffer that will hold the ciphertext
833 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100834 * \deprecated It is deprecated and discouraged to call this function
835 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
836 * are likely to remove the mode argument and have it implicitly
837 * set to MBEDTLS_RSA_PRIVATE.
838 *
839 * \note Alternative implementations of RSA need not support
840 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
841 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
842 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100843 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100844 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100845 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100846 * \note The \c sig buffer must be as large as the size
847 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100848 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100849 * \note The \c hash_id in the RSA context is the one used for the
850 * encoding. \c md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100851 * that is encoded. According to RFC 3447 it is advised to
852 * keep both hashes the same.
853 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100855 int (*f_rng)(void *, unsigned char *, size_t),
856 void *p_rng,
857 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100859 unsigned int hashlen,
860 const unsigned char *hash,
861 unsigned char *sig );
862
863/**
864 * \brief Generic wrapper to perform a PKCS#1 verification using the
865 * mode from the context. Do a public RSA operation and check
866 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000867 *
868 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100869 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200870 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100871 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
872 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
873 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000874 * \param hash buffer holding the message digest
875 * \param sig buffer holding the ciphertext
876 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100877 * \deprecated It is deprecated and discouraged to call this function
878 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
879 * are likely to remove the mode argument and have it implicitly
880 * set to MBEDTLS_RSA_PUBLIC.
881 *
882 * \note Alternative implementations of RSA need not support
883 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
884 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
885 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000886 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100887 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000888 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100889 * \note The \c sig buffer must be as large as the size
890 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000891 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200892 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000894 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200896 int (*f_rng)(void *, unsigned char *, size_t),
897 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000898 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000900 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000901 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200902 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000903
904/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100905 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
906 *
907 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100908 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200909 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100910 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
911 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
912 * for signing raw data)
913 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100914 * \param hash buffer holding the message digest
915 * \param sig buffer holding the ciphertext
916 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100917 * \deprecated It is deprecated and discouraged to call this function
918 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
919 * are likely to remove the mode argument and have it implicitly
920 * set to MBEDTLS_RSA_PUBLIC.
921 *
922 * \note Alternative implementations of RSA need not support
923 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
924 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
925 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100926 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100927 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100928 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100929 * \note The \c sig buffer must be as large as the size
930 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100931 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200933 int (*f_rng)(void *, unsigned char *, size_t),
934 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100935 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100937 unsigned int hashlen,
938 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200939 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100940
941/**
942 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200943 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +0100944 *
945 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100946 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200947 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100948 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
949 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
950 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100951 * \param hash buffer holding the message digest
952 * \param sig buffer holding the ciphertext
953 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100954 * \deprecated It is deprecated and discouraged to call this function
955 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
956 * are likely to remove the mode argument and have it implicitly
957 * set to MBEDTLS_RSA_PUBLIC.
958 *
959 * \note Alternative implementations of RSA need not support
960 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
961 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
962 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100963 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100964 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100965 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100966 * \note The \c sig buffer must be as large as the size
967 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100968 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100969 * \note The \c hash_id in the RSA context is the one used for the
970 * verification. \c md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200971 * hash that is verified. According to RFC 3447 it is advised to
Hanno Becker4b2f6912017-09-29 13:34:55 +0100972 * keep both hashes the same. If \c hash_id in the RSA context is
973 * unset, the \c md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200976 int (*f_rng)(void *, unsigned char *, size_t),
977 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100978 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100980 unsigned int hashlen,
981 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200982 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100983
984/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200985 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
986 * (This is the version with "full" options.)
987 *
988 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100989 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200990 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100991 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
992 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
993 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200994 * \param hash buffer holding the message digest
995 * \param mgf1_hash_id message digest used for mask generation
996 * \param expected_salt_len Length of the salt used in padding, use
Hanno Becker4b2f6912017-09-29 13:34:55 +0100997 * \c MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200998 * \param sig buffer holding the ciphertext
999 *
1000 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001001 * or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001002 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001003 * \note The \c sig buffer must be as large as the size
1004 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001005 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001006 * \note The \c hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001007 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001009 int (*f_rng)(void *, unsigned char *, size_t),
1010 void *p_rng,
1011 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001013 unsigned int hashlen,
1014 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001016 int expected_salt_len,
1017 const unsigned char *sig );
1018
1019/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001020 * \brief Copy the components of an RSA context
1021 *
1022 * \param dst Destination context
1023 * \param src Source context
1024 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001025 * \return 0 on success,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001026 * \c MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001029
1030/**
Paul Bakker5121ce52009-01-03 21:22:43 +00001031 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001032 *
1033 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +00001034 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001036
Hanno Beckerd22b78b2017-10-12 11:42:17 +01001037#ifdef __cplusplus
1038}
1039#endif
1040
1041#else /* MBEDTLS_RSA_ALT */
1042#include "rsa_alt.h"
1043#endif /* MBEDTLS_RSA_ALT */
1044
1045#ifdef __cplusplus
1046extern "C" {
1047#endif
1048
Paul Bakker5121ce52009-01-03 21:22:43 +00001049/**
1050 * \brief Checkup routine
1051 *
1052 * \return 0 if successful, or 1 if the test failed
1053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001055
1056#ifdef __cplusplus
1057}
1058#endif
1059
Paul Bakker5121ce52009-01-03 21:22:43 +00001060#endif /* rsa.h */