blob: a4a4716830be70bbac62a20ada4135d90020dc60 [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
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000021 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_RSA_H
25#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakkered27a042013-04-18 22:46:23 +020032
Paul Bakker314052f2011-08-15 09:07:52 +000033#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020034#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_THREADING_C)
Paul Bakkerc9965dc2013-09-29 14:58:17 +020037#include "threading.h"
38#endif
39
Paul Bakker13e2dfe2009-07-28 07:18:38 +000040/*
41 * RSA Error codes
42 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
44#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
45#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +020046#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 +020047#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
48#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
49#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
50#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
51#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Hanno Becker1434a362017-12-13 11:24:49 +000052#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 +000053
54/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020055 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000056 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define MBEDTLS_RSA_PUBLIC 0
58#define MBEDTLS_RSA_PRIVATE 1
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#define MBEDTLS_RSA_PKCS_V15 0
61#define MBEDTLS_RSA_PKCS_V21 1
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#define MBEDTLS_RSA_SIGN 1
64#define MBEDTLS_RSA_CRYPT 2
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020067
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020068/*
69 * The above constants may be used even if the RSA module is compile out,
70 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
71 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010072
73#if !defined(MBEDTLS_RSA_ALT)
74// Regular implementation
75//
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020076
Paul Bakker407a0da2013-06-27 14:29:21 +020077#ifdef __cplusplus
78extern "C" {
79#endif
80
Hanno Beckera3ebec22017-08-23 14:06:24 +010081/**
Hanno Becker5063cd22017-09-29 11:49:12 +010082 * \brief RSA context structure
83 *
84 * \note Direct manipulation of the members of this structure
85 * is deprecated and will no longer be supported starting
86 * from the next major release. All manipulation should instead
87 * be done through the public interface functions.
88 *
Paul Bakker5121ce52009-01-03 21:22:43 +000089 */
90typedef struct
91{
92 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +000093 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +000094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_mpi N; /*!< public modulus */
96 mbedtls_mpi E; /*!< public exponent */
Paul Bakker5121ce52009-01-03 21:22:43 +000097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_mpi D; /*!< private exponent */
99 mbedtls_mpi P; /*!< 1st prime factor */
100 mbedtls_mpi Q; /*!< 2nd prime factor */
Hanno Becker1a59e792017-08-23 07:41:10 +0100101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_mpi DP; /*!< D % (P - 1) */
103 mbedtls_mpi DQ; /*!< D % (Q - 1) */
104 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 mbedtls_mpi RN; /*!< cached R^2 mod N */
Hanno Becker1a59e792017-08-23 07:41:10 +0100107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 mbedtls_mpi RP; /*!< cached R^2 mod P */
109 mbedtls_mpi RQ; /*!< cached R^2 mod Q */
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_mpi Vi; /*!< cached blinding value */
112 mbedtls_mpi Vf; /*!< cached un-blinding value */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200113
Hanno Becker4b2f6912017-09-29 13:34:55 +0100114 int padding; /*!< \c MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
115 \c MBEDTLS_RSA_PKCS_v21 for OAEP/PSS */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
117 specified in the mbedtls_md.h header file
Paul Bakker9dcc3222011-03-08 14:16:06 +0000118 for the EME-OAEP and EMSA-PSS
119 encoding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#if defined(MBEDTLS_THREADING_C)
121 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200122#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000123}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
Paul Bakker5121ce52009-01-03 21:22:43 +0000126/**
127 * \brief Initialize an RSA context
128 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100129 * Note: Set padding to \c MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000130 * encryption scheme and the RSASSA-PSS signature scheme.
131 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 * \param ctx RSA context to be initialized
Hanno Becker4b2f6912017-09-29 13:34:55 +0100133 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
134 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 *
136 * \note The hash_id parameter is actually ignored
Hanno Becker4b2f6912017-09-29 13:34:55 +0100137 * when using \c MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200138 *
139 * \note Choice of padding mode is strictly enforced for private key
140 * operations, since there might be security concerns in
141 * mixing padding modes. For public key operations it's merely
142 * a default value, which can be overriden by calling specific
143 * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
144 *
145 * \note The chosen hash is always used for OEAP encryption.
146 * For PSS signatures, it's always used for making signatures,
147 * but can be overriden (and always is, if set to
Hanno Becker4b2f6912017-09-29 13:34:55 +0100148 * \c MBEDTLS_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100151 int padding,
152 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100154/**
155 * \brief Import a set of core parameters into an RSA context
156 *
157 * \param ctx Initialized RSA context to store parameters
158 * \param N RSA modulus, or NULL
159 * \param P First prime factor of N, or NULL
160 * \param Q Second prime factor of N, or NULL
161 * \param D Private exponent, or NULL
162 * \param E Public exponent, or NULL
163 *
164 * \note This function can be called multiple times for successive
165 * imports if the parameters are not simultaneously present.
166 * Any sequence of calls to this function should be followed
167 * by a call to \c mbedtls_rsa_complete which will check
168 * and complete the provided information to a ready-for-use
169 * public or private RSA key.
170 *
Hanno Becker33195552017-10-25 17:04:10 +0100171 * \note See the documentation of \c mbedtls_rsa_complete for more
172 * information on which parameters are necessary to setup
173 * a private or public RSA key.
174 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100175 * \note The imported parameters are copied and need not be preserved
176 * for the lifetime of the RSA context being set up.
177 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100178 * \return 0 if successful, non-zero error code on failure.
179 */
180int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
181 const mbedtls_mpi *N,
182 const mbedtls_mpi *P, const mbedtls_mpi *Q,
183 const mbedtls_mpi *D, const mbedtls_mpi *E );
184
185/**
186 * \brief Import core RSA parameters in raw big-endian
187 * binary format into an RSA context
188 *
189 * \param ctx Initialized RSA context to store parameters
190 * \param N RSA modulus, or NULL
191 * \param N_len Byte length of N, ignored if N == NULL
192 * \param P First prime factor of N, or NULL
193 * \param P_len Byte length of P, ignored if P == NULL
194 * \param Q Second prime factor of N, or NULL
195 * \param Q_len Byte length of Q, ignored if Q == NULL
196 * \param D Private exponent, or NULL
197 * \param D_len Byte length of D, ignored if D == NULL
198 * \param E Public exponent, or NULL
199 * \param E_len Byte length of E, ignored if E == NULL
200 *
201 * \note This function can be called multiple times for successive
202 * imports if the parameters are not simultaneously present.
203 * Any sequence of calls to this function should be followed
204 * by a call to \c mbedtls_rsa_complete which will check
205 * and complete the provided information to a ready-for-use
206 * public or private RSA key.
207 *
Hanno Becker33195552017-10-25 17:04:10 +0100208 * \note See the documentation of \c mbedtls_rsa_complete for more
209 * information on which parameters are necessary to setup
210 * a private or public RSA key.
211 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100212 * \note The imported parameters are copied and need not be preserved
213 * for the lifetime of the RSA context being set up.
214 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100215 * \return 0 if successful, non-zero error code on failure.
216 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100217int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100218 unsigned char const *N, size_t N_len,
219 unsigned char const *P, size_t P_len,
220 unsigned char const *Q, size_t Q_len,
221 unsigned char const *D, size_t D_len,
222 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100223
224/**
225 * \brief Attempt to complete an RSA context from
226 * a set of imported core parameters.
227 *
228 * \param ctx Initialized RSA context to store parameters
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100229 *
Hanno Beckered203612017-09-29 13:34:25 +0100230 * \note
231 * - To setup an RSA public key, precisely N and E
232 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100233 *
Hanno Beckered203612017-09-29 13:34:25 +0100234 * - To setup an RSA private key, enough information must be
Hanno Becker98838b02017-10-02 13:16:10 +0100235 * present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100236 *
Hanno Beckered203612017-09-29 13:34:25 +0100237 * The default implementation supports the following:
238 * - Derive P, Q from N, D, E
239 * - Derive N, D from P, Q, E.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100240 *
Hanno Beckered203612017-09-29 13:34:25 +0100241 * - Alternative implementations need not support these
242 * and may return \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA instead.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100243 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100244 * \return
245 * - 0 if successful. In this case, it is guaranteed
Hanno Becker1e801f52017-10-10 16:44:47 +0100246 * that the RSA context can be used for RSA operations
247 * without the risk of failure or crash.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100248 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100249 * derivations failed.
Hanno Becker43a08d02017-10-02 13:16:35 +0100250 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100251 * \warning This function need not perform consistency checks
252 * for the imported parameters! In particular, parameters that
253 * are not needed by the implementation may be silently discarded
254 * and left unchecked. For the purpose of checking the consistency
255 * of the key material, see \c mbedtls_rsa_check_privkey.
Hanno Becker43a08d02017-10-02 13:16:35 +0100256 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100257 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100258int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100259
260/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100261 * \brief Export core parameters of an RSA key
262 *
263 * \param ctx Initialized RSA context
264 * \param N MPI to hold the RSA modulus, or NULL
265 * \param P MPI to hold the first prime factor of N, or NULL
266 * \param Q MPI to hold the second prime factor of N, or NULL
267 * \param D MPI to hold the private exponent, or NULL
268 * \param E MPI to hold the public exponent, or NULL
269 *
Hanno Beckered203612017-09-29 13:34:25 +0100270 * \return
271 * - 0 if successful. In this case, the non-NULL buffers
272 * pointed to by N, P, Q, D, E are fully written, with
273 * additional unused space filled leading by 0-bytes.
274 * - Non-zero return code otherwise. In particular, if
275 * exporting the requested parameters
276 * cannot be done because of a lack of functionality
277 * or because of security policies, the error code
Hanno Beckera47023e2017-12-22 17:08:03 +0000278 * \c MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is returned.
Hanno Beckered203612017-09-29 13:34:25 +0100279 * In this case, the RSA context stays intact and can
280 * be continued to be used.
Hanno Becker91c194d2017-09-29 12:50:12 +0100281 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000282 * \note Reasons for returning \c MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION
Hanno Becker91c194d2017-09-29 12:50:12 +0100283 * would be the following: Firstly, it might be that an
284 * alternative RSA implementation is in use which stores
285 * the key externally, and which either cannot or should not
286 * export it into RAM. Alternatively, an implementation
287 * (regardless of SW or HW) might not support deducing e.g.
288 * P, Q from N, D, E if the former are not part of the
289 * implementation.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100290 *
291 */
292int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
293 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
294 mbedtls_mpi *D, mbedtls_mpi *E );
295
296/**
297 * \brief Export core parameters of an RSA key
298 * in raw big-endian binary format
299 *
300 * \param ctx Initialized RSA context
301 * \param N Byte array to store the RSA modulus, or NULL
302 * \param N_len Size of buffer for modulus
303 * \param P Byte array to hold the first prime factor of N, or NULL
304 * \param P_len Size of buffer for first prime factor
305 * \param Q Byte array to hold the second prime factor of N, or NULL
306 * \param Q_len Size of buffer for second prime factor
307 * \param D Byte array to hold the private exponent, or NULL
308 * \param D_len Size of buffer for private exponent
309 * \param E Byte array to hold the public exponent, or NULL
310 * \param E_len Size of buffer for public exponent
311 *
312 * \note The length fields are ignored if the corresponding
313 * buffer pointers are NULL.
314 *
Hanno Beckered203612017-09-29 13:34:25 +0100315 * \return
316 * - 0 if successful. In this case, the non-NULL buffers
317 * pointed to by N, P, Q, D, E are fully written, with
318 * additional unused space filled leading by 0-bytes.
319 * - Non-zero return code otherwise. In particular, if
320 * exporting the requested parameters
321 * cannot be done because of a lack of functionality
322 * or because of security policies, the error code
Hanno Beckera47023e2017-12-22 17:08:03 +0000323 * \c MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is returned.
Hanno Beckered203612017-09-29 13:34:25 +0100324 * In this case, the RSA context stays intact and can
325 * be continued to be used.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100326 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000327 * \note Reasons for returning \c MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION
Hanno Becker91c194d2017-09-29 12:50:12 +0100328 * would be the following: Firstly, it might be that an
329 * alternative RSA implementation is in use which stores
330 * the key externally, and which either cannot or should not
331 * export it into RAM. Alternatively, an implementation
332 * (regardless of SW or HW) might not support deducing e.g.
333 * P, Q from N, D, E if the former are not part of the
334 * implementation.
335 *
336 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100337 */
338int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
339 unsigned char *N, size_t N_len,
340 unsigned char *P, size_t P_len,
341 unsigned char *Q, size_t Q_len,
342 unsigned char *D, size_t D_len,
343 unsigned char *E, size_t E_len );
344
345/**
346 * \brief Export CRT parameters of a private RSA key
347 *
348 * \param ctx Initialized RSA context
349 * \param DP MPI to hold D modulo P-1, or NULL
350 * \param DQ MPI to hold D modulo Q-1, or NULL
351 * \param QP MPI to hold modular inverse of Q modulo P, or NULL
352 *
353 * \return 0 if successful, non-zero error code otherwise.
354 *
355 * \note Alternative RSA implementations not using CRT-parameters
356 * internally can implement this function using based on
357 * \c mbedtls_rsa_deduce_opt.
358 *
359 */
360int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
361 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
362
Paul Bakker5121ce52009-01-03 21:22:43 +0000363/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100364 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100366 *
367 * \param ctx RSA context to be set
Hanno Becker4b2f6912017-09-29 13:34:55 +0100368 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
369 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100370 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100371void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
372 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100373
374/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100375 * \brief Get length of RSA modulus in bytes
376 *
377 * \param ctx Initialized RSA context
378 *
379 * \return Length of RSA modulus, in bytes.
380 *
381 */
382size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
383
384/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 * \brief Generate an RSA keypair
386 *
387 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000388 * \param f_rng RNG function
389 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 * \param nbits size of the public key in bits
391 * \param exponent public exponent (e.g., 65537)
392 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000394 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100396 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100399 int (*f_rng)(void *, unsigned char *, size_t),
400 void *p_rng,
401 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000402
403/**
Hanno Becker43a08d02017-10-02 13:16:35 +0100404 * \brief Check if a context contains (at least) an RSA public key
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 *
406 * \param ctx RSA context to be checked
407 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100408 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
409 * On success, it is guaranteed that enough information is
410 * present to perform an RSA public key operation
411 * \c mbedtls_rsa_public.
412 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000415
416/**
Hanno Becker1e801f52017-10-10 16:44:47 +0100417 * \brief Check if a context contains an RSA private key
418 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000419 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100420 * \param ctx RSA context to be checked
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100422 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
Hanno Becker43a08d02017-10-02 13:16:35 +0100423 *
Hanno Becker68767a62017-10-17 10:13:31 +0100424 * \note The consistency checks performed by this function not only
425 * ensure that \c mbedtls_rsa_private can be called successfully
426 * on the given context, but that the various parameters are
427 * mutually consistent with high probability, in the sense that
428 * \c mbedtls_rsa_public and \c mbedtls_rsa_private are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100429 *
430 * \warning This function should catch accidental misconfigurations
431 * like swapping of parameters, but it cannot establish full
432 * trust in neither the quality nor the consistency of the key
433 * material that was used to setup the given RSA context:
434 * - Regarding consistency, note (see \c mbedtls_rsa_complete)
435 * that imported parameters irrelevant for the implementation
436 * might be silently dropped, in which case the present
437 * function doesn't have access to and hence cannot check them.
Hanno Beckerfc8fbfa2017-10-17 10:31:15 +0100438 * If you want to check the consistency of the entire
439 * content of, say, an PKCS1-encoded RSA private key, you
Hanno Becker1e801f52017-10-10 16:44:47 +0100440 * should use \c mbedtls_rsa_validate_params before setting
441 * up the RSA context.
442 * Further, if the implementation performs empirical checks,
443 * these checks will substantiate but not guarantee consistency.
444 * - Regarding quality, this function is not expected to perform
445 * extended quality assessments like checking that the prime
446 * factors are safe. Further, it is the user's responsibility to
447 * ensure trustworthiness of the source of his RSA parameters,
448 * a question going beyond what's effectively checkable
449 * by the library.
Hanno Becker43a08d02017-10-02 13:16:35 +0100450 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000453
454/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100455 * \brief Check a public-private RSA key pair.
456 * Check each of the contexts, and make sure they match.
457 *
458 * \param pub RSA context holding the public key
459 * \param prv RSA context holding the private key
460 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100461 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100462 */
Hanno Becker98838b02017-10-02 13:16:10 +0100463int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
464 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100465
466/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000467 * \brief Do an RSA public key operation
468 *
469 * \param ctx RSA context
470 * \param input input buffer
471 * \param output output buffer
472 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100473 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000474 *
475 * \note This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800476 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000477 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 *
479 * \note The input and output buffers must be large
480 * enough (eg. 128 bytes if RSA-1024 is used).
481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000483 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000484 unsigned char *output );
485
486/**
487 * \brief Do an RSA private key operation
488 *
489 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200490 * \param f_rng RNG function (Needed for blinding)
491 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000492 * \param input input buffer
493 * \param output output buffer
494 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100495 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000496 *
497 * \note The input and output buffers must be large
498 * enough (eg. 128 bytes if RSA-1024 is used).
499 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200501 int (*f_rng)(void *, unsigned char *, size_t),
502 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000503 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 unsigned char *output );
505
506/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100507 * \brief Generic wrapper to perform a PKCS#1 encryption using the
508 * mode from the context. Add the message padding, then do an
509 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 *
511 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200512 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100513 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000514 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100515 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000516 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 * \param input buffer holding the data to be encrypted
518 * \param output buffer that will hold the ciphertext
519 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100520 * \deprecated It is deprecated and discouraged to call this function
521 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
522 * are likely to remove the mode argument and have it implicitly
523 * set to MBEDTLS_RSA_PUBLIC.
524 *
525 * \note Alternative implementations of RSA need not support
526 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
527 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
528 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000529 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000530 *
531 * \note The output buffer must be as large as the size
532 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000535 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000536 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000537 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000538 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000539 unsigned char *output );
540
541/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100542 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
543 *
544 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100545 * \param f_rng RNG function (Needed for padding and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100546 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100547 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100548 * \param ilen contains the plaintext length
549 * \param input buffer holding the data to be encrypted
550 * \param output buffer that will hold the ciphertext
551 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100552 * \deprecated It is deprecated and discouraged to call this function
553 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
554 * are likely to remove the mode argument and have it implicitly
555 * set to MBEDTLS_RSA_PUBLIC.
556 *
557 * \note Alternative implementations of RSA need not support
558 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
559 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
560 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000561 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100562 *
563 * \note The output buffer must be as large as the size
564 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100567 int (*f_rng)(void *, unsigned char *, size_t),
568 void *p_rng,
569 int mode, size_t ilen,
570 const unsigned char *input,
571 unsigned char *output );
572
573/**
574 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
575 *
576 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200577 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100578 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100579 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100580 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100581 * \param label buffer holding the custom label to use
582 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100583 * \param ilen contains the plaintext length
584 * \param input buffer holding the data to be encrypted
585 * \param output buffer that will hold the ciphertext
586 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100587 * \deprecated It is deprecated and discouraged to call this function
588 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
589 * are likely to remove the mode argument and have it implicitly
590 * set to MBEDTLS_RSA_PUBLIC.
591 *
592 * \note Alternative implementations of RSA need not support
593 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
594 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
595 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000596 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100597 *
598 * \note The output buffer must be as large as the size
599 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100602 int (*f_rng)(void *, unsigned char *, size_t),
603 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100604 int mode,
605 const unsigned char *label, size_t label_len,
606 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100607 const unsigned char *input,
608 unsigned char *output );
609
610/**
611 * \brief Generic wrapper to perform a PKCS#1 decryption using the
612 * mode from the context. Do an RSA operation, then remove
613 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 *
615 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100616 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200617 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100618 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000619 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 * \param input buffer holding the encrypted data
621 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000622 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100624 * \deprecated It is deprecated and discouraged to call this function
625 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
626 * are likely to remove the mode argument and have it implicitly
627 * set to MBEDTLS_RSA_PRIVATE.
628 *
629 * \note Alternative implementations of RSA need not support
630 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
631 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
632 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000633 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000634 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100635 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100636 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100637 * if RSA-1024 is used) to be able to hold an arbitrary
638 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100639 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100640 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100641 *
642 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100643 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200646 int (*f_rng)(void *, unsigned char *, size_t),
647 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000648 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000649 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000650 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000651 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
653/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100654 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
655 *
656 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100657 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200658 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100659 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100660 * \param olen will contain the plaintext length
661 * \param input buffer holding the encrypted data
662 * \param output buffer that will hold the plaintext
663 * \param output_max_len maximum length of the output buffer
664 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100665 * \deprecated It is deprecated and discouraged to call this function
666 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
667 * are likely to remove the mode argument and have it implicitly
668 * set to MBEDTLS_RSA_PRIVATE.
669 *
670 * \note Alternative implementations of RSA need not support
671 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
672 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
673 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000674 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100675 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100676 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100677 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100678 * if RSA-1024 is used) to be able to hold an arbitrary
679 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100680 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100681 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100682 *
683 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100684 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200687 int (*f_rng)(void *, unsigned char *, size_t),
688 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100689 int mode, size_t *olen,
690 const unsigned char *input,
691 unsigned char *output,
692 size_t output_max_len );
693
694/**
695 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
696 *
697 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100698 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200699 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100700 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100701 * \param label buffer holding the custom label to use
702 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100703 * \param olen will contain the plaintext length
704 * \param input buffer holding the encrypted data
705 * \param output buffer that will hold the plaintext
706 * \param output_max_len maximum length of the output buffer
707 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100708 * \deprecated It is deprecated and discouraged to call this function
709 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
710 * are likely to remove the mode argument and have it implicitly
711 * set to MBEDTLS_RSA_PRIVATE.
712 *
713 * \note Alternative implementations of RSA need not support
714 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
715 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
716 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000717 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100718 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100719 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100720 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100721 * if RSA-1024 is used) to be able to hold an arbitrary
722 * decrypted message. If it is not large enough to hold
Hanno Becker8fd55482017-08-23 14:07:48 +0100723 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100724 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100725 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100726 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100727 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Hanno Becker32297e82017-12-22 10:24:32 +0000728 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200731 int (*f_rng)(void *, unsigned char *, size_t),
732 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100733 int mode,
734 const unsigned char *label, size_t label_len,
735 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100736 const unsigned char *input,
737 unsigned char *output,
738 size_t output_max_len );
739
740/**
741 * \brief Generic wrapper to perform a PKCS#1 signature using the
742 * mode from the context. Do a private RSA operation to sign
743 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000744 *
745 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200746 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100747 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000748 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100749 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
750 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for
751 * signing raw data)
752 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000753 * \param hash buffer holding the message digest
754 * \param sig buffer that will hold the ciphertext
755 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100756 * \deprecated It is deprecated and discouraged to call this function
757 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
758 * are likely to remove the mode argument and have it implicitly
759 * set to MBEDTLS_RSA_PRIVATE.
760 *
761 * \note Alternative implementations of RSA need not support
762 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
763 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
764 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000765 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100766 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000767 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100768 * \note The \c sig buffer must be as large as the size
769 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000770 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200771 * \note In case of PKCS#1 v2.1 encoding, see comments on
Hanno Becker32297e82017-12-22 10:24:32 +0000772 * \c mbedtls_rsa_rsassa_pss_sign() for details on
Hanno Becker4b2f6912017-09-29 13:34:55 +0100773 * \c md_alg and \c hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000774 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000776 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000777 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000778 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000780 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000781 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000782 unsigned char *sig );
783
784/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100785 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
786 *
787 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100788 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200789 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100790 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
791 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
792 * for signing raw data)
793 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100794 * \param hash buffer holding the message digest
795 * \param sig buffer that will hold the ciphertext
796 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100797 * \deprecated It is deprecated and discouraged to call this function
798 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
799 * are likely to remove the mode argument and have it implicitly
800 * set to MBEDTLS_RSA_PRIVATE.
801 *
802 * \note Alternative implementations of RSA need not support
803 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
804 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
805 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100806 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100807 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100808 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100809 * \note The \c sig buffer must be as large as the size
810 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200813 int (*f_rng)(void *, unsigned char *, size_t),
814 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100815 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100817 unsigned int hashlen,
818 const unsigned char *hash,
819 unsigned char *sig );
820
821/**
822 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
823 *
824 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200825 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100826 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100827 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100828 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
829 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
830 * for signing raw data)
831 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100832 * \param hash buffer holding the message digest
833 * \param sig buffer that will hold the ciphertext
834 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100835 * \deprecated It is deprecated and discouraged to call this function
836 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
837 * are likely to remove the mode argument and have it implicitly
838 * set to MBEDTLS_RSA_PRIVATE.
839 *
840 * \note Alternative implementations of RSA need not support
841 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
842 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
843 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100844 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100845 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100846 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100847 * \note The \c sig buffer must be as large as the size
848 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100849 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100850 * \note The \c hash_id in the RSA context is the one used for the
851 * encoding. \c md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100852 * that is encoded. According to RFC 3447 it is advised to
853 * keep both hashes the same.
854 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100856 int (*f_rng)(void *, unsigned char *, size_t),
857 void *p_rng,
858 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100860 unsigned int hashlen,
861 const unsigned char *hash,
862 unsigned char *sig );
863
864/**
865 * \brief Generic wrapper to perform a PKCS#1 verification using the
866 * mode from the context. Do a public RSA operation and check
867 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000868 *
869 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100870 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200871 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100872 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
873 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
874 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000875 * \param hash buffer holding the message digest
876 * \param sig buffer holding the ciphertext
877 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100878 * \deprecated It is deprecated and discouraged to call this function
879 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
880 * are likely to remove the mode argument and have it implicitly
881 * set to MBEDTLS_RSA_PUBLIC.
882 *
883 * \note Alternative implementations of RSA need not support
884 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
885 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
886 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000887 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100888 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000889 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100890 * \note The \c sig buffer must be as large as the size
891 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000892 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200893 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000895 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200897 int (*f_rng)(void *, unsigned char *, size_t),
898 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000899 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000901 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000902 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200903 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000904
905/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100906 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
907 *
908 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100909 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200910 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100911 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
912 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
913 * for signing raw data)
914 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100915 * \param hash buffer holding the message digest
916 * \param sig buffer holding the ciphertext
917 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100918 * \deprecated It is deprecated and discouraged to call this function
919 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
920 * are likely to remove the mode argument and have it implicitly
921 * set to MBEDTLS_RSA_PUBLIC.
922 *
923 * \note Alternative implementations of RSA need not support
924 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
925 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
926 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100927 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100928 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100929 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100930 * \note The \c sig buffer must be as large as the size
931 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100932 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200934 int (*f_rng)(void *, unsigned char *, size_t),
935 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100936 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100938 unsigned int hashlen,
939 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200940 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100941
942/**
943 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200944 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +0100945 *
946 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100947 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200948 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100949 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
950 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
951 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100952 * \param hash buffer holding the message digest
953 * \param sig buffer holding the ciphertext
954 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100955 * \deprecated It is deprecated and discouraged to call this function
956 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
957 * are likely to remove the mode argument and have it implicitly
958 * set to MBEDTLS_RSA_PUBLIC.
959 *
960 * \note Alternative implementations of RSA need not support
961 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
962 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
963 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100964 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100965 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100966 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100967 * \note The \c sig buffer must be as large as the size
968 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100969 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100970 * \note The \c hash_id in the RSA context is the one used for the
971 * verification. \c md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200972 * hash that is verified. According to RFC 3447 it is advised to
Hanno Becker4b2f6912017-09-29 13:34:55 +0100973 * keep both hashes the same. If \c hash_id in the RSA context is
974 * unset, the \c md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100975 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200977 int (*f_rng)(void *, unsigned char *, size_t),
978 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100979 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100981 unsigned int hashlen,
982 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200983 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100984
985/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200986 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
987 * (This is the version with "full" options.)
988 *
989 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100990 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200991 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100992 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
993 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
994 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200995 * \param hash buffer holding the message digest
996 * \param mgf1_hash_id message digest used for mask generation
997 * \param expected_salt_len Length of the salt used in padding, use
Hanno Becker4b2f6912017-09-29 13:34:55 +0100998 * \c MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200999 * \param sig buffer holding the ciphertext
1000 *
1001 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001002 * or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001003 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001004 * \note The \c sig buffer must be as large as the size
1005 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001006 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001007 * \note The \c hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001010 int (*f_rng)(void *, unsigned char *, size_t),
1011 void *p_rng,
1012 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001014 unsigned int hashlen,
1015 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001017 int expected_salt_len,
1018 const unsigned char *sig );
1019
1020/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001021 * \brief Copy the components of an RSA context
1022 *
1023 * \param dst Destination context
1024 * \param src Source context
1025 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001026 * \return 0 on success,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001027 * \c MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001028 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001030
1031/**
Paul Bakker5121ce52009-01-03 21:22:43 +00001032 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001033 *
1034 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
Hanno Beckerd22b78b2017-10-12 11:42:17 +01001038#ifdef __cplusplus
1039}
1040#endif
1041
1042#else /* MBEDTLS_RSA_ALT */
1043#include "rsa_alt.h"
1044#endif /* MBEDTLS_RSA_ALT */
1045
1046#ifdef __cplusplus
1047extern "C" {
1048#endif
1049
Paul Bakker5121ce52009-01-03 21:22:43 +00001050/**
1051 * \brief Checkup routine
1052 *
1053 * \return 0 if successful, or 1 if the test failed
1054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001056
1057#ifdef __cplusplus
1058}
1059#endif
1060
Paul Bakker5121ce52009-01-03 21:22:43 +00001061#endif /* rsa.h */