blob: e45520f9a89200843c933d8f465f37fd7e1d10df [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 Becker91c194d2017-09-29 12:50:12 +010051#define MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED -0x4500 /**< The requested parameter export is not possible/allowed. */
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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020072
Paul Bakker407a0da2013-06-27 14:29:21 +020073#ifdef __cplusplus
74extern "C" {
75#endif
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/**
Hanno Beckera3ebec22017-08-23 14:06:24 +010078 * Helper functions for RSA-related operations on MPI's.
79 */
80
81/**
82 * \brief Compute RSA prime moduli P, Q from public modulus N=PQ
Hanno Beckerb0c5edc2017-08-23 22:16:10 +010083 * and a pair of private and public key.
Hanno Beckera3ebec22017-08-23 14:06:24 +010084 *
85 * \note This is a 'static' helper function not operating on
86 * an RSA context. Alternative implementations need not
87 * overwrite it.
88 *
89 * \param N RSA modulus N = PQ, with P, Q to be found
90 * \param D RSA private exponent
91 * \param E RSA public exponent
92 * \param f_rng PRNG to be used for randomization, or NULL
93 * \param p_rng PRNG context for f_rng, or NULL
94 * \param P Pointer to MPI holding first prime factor of N on success
95 * \param Q Pointer to MPI holding second prime factor of N on success
96 *
Hanno Beckered203612017-09-29 13:34:25 +010097 * \return
98 * - 0 if successful. In this case, P and Q constitute a
Hanno Beckerba5b7552017-10-02 09:55:49 +010099 * factorization of N.
100 * - A non-zero error code otherwise.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100101 *
Hanno Beckera3ebec22017-08-23 14:06:24 +0100102 */
Hanno Beckerba5b7552017-10-02 09:55:49 +0100103int mbedtls_rsa_deduce_moduli( mbedtls_mpi const *N, mbedtls_mpi const *D,
104 mbedtls_mpi const *E, int (*f_rng)(void *, unsigned char *, size_t),
105 void *p_rng, mbedtls_mpi *P, mbedtls_mpi *Q );
Hanno Beckera3ebec22017-08-23 14:06:24 +0100106
107/**
108 * \brief Compute RSA private exponent from
109 * prime moduli and public key.
110 *
111 * \note This is a 'static' helper function not operating on
112 * an RSA context. Alternative implementations need not
113 * overwrite it.
114 *
115 * \param P First prime factor of RSA modulus
116 * \param Q Second prime factor of RSA modulus
117 * \param E RSA public exponent
118 * \param D Pointer to MPI holding the private exponent on success.
119 *
120 * \note This function does not check whether P and Q are primes.
121 *
Hanno Beckered203612017-09-29 13:34:25 +0100122 * \return
123 * - 0 if successful. In this case, D is set to a simultaneous
Hanno Beckera3ebec22017-08-23 14:06:24 +0100124 * modular inverse of E modulo both P-1 and Q-1.
Hanno Beckerbdefff12017-10-02 09:57:50 +0100125 * - A non-zero error code otherwise.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100126 *
Hanno Beckera3ebec22017-08-23 14:06:24 +0100127 */
Hanno Beckerbdefff12017-10-02 09:57:50 +0100128int mbedtls_rsa_deduce_private( mbedtls_mpi const *P, mbedtls_mpi const *Q,
129 mbedtls_mpi const *E, mbedtls_mpi *D );
Hanno Beckera3ebec22017-08-23 14:06:24 +0100130
131
132/**
133 * \brief Generate RSA-CRT parameters
134 *
135 * \note This is a 'static' helper function not operating on
136 * an RSA context. Alternative implementations need not
137 * overwrite it.
138 *
139 * \param P First prime factor of N
140 * \param Q Second prime factor of N
141 * \param D RSA private exponent
142 * \param DP Output variable for D modulo P-1
143 * \param DQ Output variable for D modulo Q-1
144 * \param QP Output variable for the modular inverse of Q modulo P.
145 *
146 * \return 0 on success, non-zero error code otherwise.
147 *
148 */
149int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
150 const mbedtls_mpi *D, mbedtls_mpi *DP,
151 mbedtls_mpi *DQ, mbedtls_mpi *QP );
152
153
154/**
155 * \brief Check validity of core RSA parameters
156 *
157 * \note This is a 'static' helper function not operating on
158 * an RSA context. Alternative implementations need not
159 * overwrite it.
160 *
161 * \param N RSA modulus N = PQ
162 * \param P First prime factor of N
163 * \param Q Second prime factor of N
164 * \param D RSA private exponent
165 * \param E RSA public exponent
Hanno Becker56bae952017-09-29 15:33:10 +0100166 * \param f_rng PRNG to be used for primality check, or NULL
Hanno Beckera3ebec22017-08-23 14:06:24 +0100167 * \param p_rng PRNG context for f_rng, or NULL
168 *
Hanno Beckered203612017-09-29 13:34:25 +0100169 * \return
170 * - 0 if the following conditions are satisfied:
171 * - N = PQ if N,P,Q != NULL
172 * - D and E are modular inverses modulo P-1 and Q-1
173 * if D,E,P,Q != NULL
174 * - P prime if f_rng, P != NULL
175 * - Q prime if f_rng, Q != NULL
Hanno Becker750e8b42017-08-25 07:54:27 +0100176 * - A non-zero error code otherwise.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100177 *
178 * \note The function can be used with a restricted set of arguments
179 * to perform specific checks only. E.g., calling it with
180 * (-,P,-,-,-) and a PRNG amounts to a primality check for P.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100181 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100182int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
183 const mbedtls_mpi *Q, const mbedtls_mpi *D,
184 const mbedtls_mpi *E,
185 int (*f_rng)(void *, unsigned char *, size_t),
186 void *p_rng );
Hanno Beckera3ebec22017-08-23 14:06:24 +0100187
188/**
Hanno Beckerd3637992017-08-25 07:55:03 +0100189 * \brief Check validity of RSA CRT parameters
190 *
191 * \note This is a 'static' helper function not operating on
192 * an RSA context. Alternative implementations need not
193 * overwrite it.
194 *
195 * \param P First prime factor of RSA modulus
196 * \param Q Second prime factor of RSA modulus
197 * \param D RSA private exponent
198 * \param DP MPI to check for D modulo P-1
199 * \param DQ MPI to check for D modulo P-1
200 * \param QP MPI to check for the modular inverse of Q modulo P.
201 *
Hanno Beckered203612017-09-29 13:34:25 +0100202 * \return
203 * - 0 if the following conditions are satisfied:
204 * - D = DP mod P-1 if P, D, DP != NULL
205 * - Q = DQ mod P-1 if P, D, DQ != NULL
206 * - QP = Q^-1 mod P if P, Q, QP != NULL
207 * - \c MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if check failed,
208 * potentially including \c MBEDTLS_ERR_MPI_XXX if some
Hanno Beckerd3637992017-08-25 07:55:03 +0100209 * MPI calculations failed.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100210 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if insufficient
Hanno Beckerd3637992017-08-25 07:55:03 +0100211 * data was provided to check DP, DQ or QP.
212 *
213 * \note The function can be used with a restricted set of arguments
214 * to perform specific checks only. E.g., calling it with the
215 * parameters (P, -, D, DP, -, -) will check DP = D mod P-1.
216 */
217int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
218 const mbedtls_mpi *D, const mbedtls_mpi *DP,
219 const mbedtls_mpi *DQ, const mbedtls_mpi *QP );
220
221/**
Hanno Beckera3ebec22017-08-23 14:06:24 +0100222 * Implementation of RSA interface
223 */
224
Hanno Beckerab377312017-08-23 16:24:51 +0100225#if !defined(MBEDTLS_RSA_ALT)
226
Hanno Beckera3ebec22017-08-23 14:06:24 +0100227/**
Hanno Becker5063cd22017-09-29 11:49:12 +0100228 * \brief RSA context structure
229 *
230 * \note Direct manipulation of the members of this structure
231 * is deprecated and will no longer be supported starting
232 * from the next major release. All manipulation should instead
233 * be done through the public interface functions.
234 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 */
236typedef struct
237{
238 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +0000239 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_mpi N; /*!< public modulus */
242 mbedtls_mpi E; /*!< public exponent */
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_mpi D; /*!< private exponent */
245 mbedtls_mpi P; /*!< 1st prime factor */
246 mbedtls_mpi Q; /*!< 2nd prime factor */
Hanno Becker1a59e792017-08-23 07:41:10 +0100247
248#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_mpi DP; /*!< D % (P - 1) */
250 mbedtls_mpi DQ; /*!< D % (Q - 1) */
251 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Hanno Becker1a59e792017-08-23 07:41:10 +0100252#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_mpi RN; /*!< cached R^2 mod N */
Hanno Becker1a59e792017-08-23 07:41:10 +0100255
256#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_mpi RP; /*!< cached R^2 mod P */
258 mbedtls_mpi RQ; /*!< cached R^2 mod Q */
Hanno Becker1a59e792017-08-23 07:41:10 +0100259#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_mpi Vi; /*!< cached blinding value */
262 mbedtls_mpi Vf; /*!< cached un-blinding value */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200263
Hanno Becker4b2f6912017-09-29 13:34:55 +0100264 int padding; /*!< \c MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
265 \c MBEDTLS_RSA_PKCS_v21 for OAEP/PSS */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
267 specified in the mbedtls_md.h header file
Paul Bakker9dcc3222011-03-08 14:16:06 +0000268 for the EME-OAEP and EMSA-PSS
269 encoding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270#if defined(MBEDTLS_THREADING_C)
271 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200272#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000273}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000275
Hanno Beckerab377312017-08-23 16:24:51 +0100276#else
277
278#include "rsa_alt.h"
279
280#endif /* MBEDTLS_RSA_ALT */
281
Paul Bakker5121ce52009-01-03 21:22:43 +0000282/**
283 * \brief Initialize an RSA context
284 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100285 * Note: Set padding to \c MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000286 * encryption scheme and the RSASSA-PSS signature scheme.
287 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 * \param ctx RSA context to be initialized
Hanno Becker4b2f6912017-09-29 13:34:55 +0100289 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
290 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 *
292 * \note The hash_id parameter is actually ignored
Hanno Becker4b2f6912017-09-29 13:34:55 +0100293 * when using \c MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200294 *
295 * \note Choice of padding mode is strictly enforced for private key
296 * operations, since there might be security concerns in
297 * mixing padding modes. For public key operations it's merely
298 * a default value, which can be overriden by calling specific
299 * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
300 *
301 * \note The chosen hash is always used for OEAP encryption.
302 * For PSS signatures, it's always used for making signatures,
303 * but can be overriden (and always is, if set to
Hanno Becker4b2f6912017-09-29 13:34:55 +0100304 * \c MBEDTLS_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100307 int padding,
308 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000309
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100310/**
311 * \brief Import a set of core parameters into an RSA context
312 *
313 * \param ctx Initialized RSA context to store parameters
314 * \param N RSA modulus, or NULL
315 * \param P First prime factor of N, or NULL
316 * \param Q Second prime factor of N, or NULL
317 * \param D Private exponent, or NULL
318 * \param E Public exponent, or NULL
319 *
320 * \note This function can be called multiple times for successive
321 * imports if the parameters are not simultaneously present.
322 * Any sequence of calls to this function should be followed
323 * by a call to \c mbedtls_rsa_complete which will check
324 * and complete the provided information to a ready-for-use
325 * public or private RSA key.
326 *
327 * \return 0 if successful, non-zero error code on failure.
328 */
329int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
330 const mbedtls_mpi *N,
331 const mbedtls_mpi *P, const mbedtls_mpi *Q,
332 const mbedtls_mpi *D, const mbedtls_mpi *E );
333
334/**
335 * \brief Import core RSA parameters in raw big-endian
336 * binary format into an RSA context
337 *
338 * \param ctx Initialized RSA context to store parameters
339 * \param N RSA modulus, or NULL
340 * \param N_len Byte length of N, ignored if N == NULL
341 * \param P First prime factor of N, or NULL
342 * \param P_len Byte length of P, ignored if P == NULL
343 * \param Q Second prime factor of N, or NULL
344 * \param Q_len Byte length of Q, ignored if Q == NULL
345 * \param D Private exponent, or NULL
346 * \param D_len Byte length of D, ignored if D == NULL
347 * \param E Public exponent, or NULL
348 * \param E_len Byte length of E, ignored if E == NULL
349 *
350 * \note This function can be called multiple times for successive
351 * imports if the parameters are not simultaneously present.
352 * Any sequence of calls to this function should be followed
353 * by a call to \c mbedtls_rsa_complete which will check
354 * and complete the provided information to a ready-for-use
355 * public or private RSA key.
356 *
357 * \return 0 if successful, non-zero error code on failure.
358 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100359int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
360 unsigned char *N, size_t N_len,
361 unsigned char *P, size_t P_len,
362 unsigned char *Q, size_t Q_len,
363 unsigned char *D, size_t D_len,
364 unsigned char *E, size_t E_len );
365
366/**
367 * \brief Attempt to complete an RSA context from
368 * a set of imported core parameters.
369 *
370 * \param ctx Initialized RSA context to store parameters
371 * \param f_rng RNG function,
372 * \param p_rng RNG parameter
373 *
Hanno Beckered203612017-09-29 13:34:25 +0100374 * \note
375 * - To setup an RSA public key, precisely N and E
376 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100377 *
Hanno Beckered203612017-09-29 13:34:25 +0100378 * - To setup an RSA private key, enough information must be
379 * present for the other parameters to be efficiently derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100380 *
Hanno Beckered203612017-09-29 13:34:25 +0100381 * The default implementation supports the following:
382 * - Derive P, Q from N, D, E
383 * - Derive N, D from P, Q, E.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100384 *
Hanno Beckered203612017-09-29 13:34:25 +0100385 * - Alternative implementations need not support these
386 * and may return \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA instead.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100387 *
388 * \note The PRNG is used for probabilistic algorithms
389 * like the derivation of P, Q from N, D, E, as
390 * well as primality checks.
391 *
Hanno Becker603b8c62017-08-25 11:03:07 +0100392 * \return - 0 if successful. In this case, all imported core
393 * parameters are guaranteed to be sane, the RSA context
394 * has been fully setup and is ready for use.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100395 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100396 * derivations failed.
397 */
398int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
399 int (*f_rng)(void *, unsigned char *, size_t),
400 void *p_rng );
401
402/**
403 * \brief Check if CRT-parameters match core parameters
404 *
405 * \param ctx Complete RSA private key context
406 * \param DP Private exponent modulo P-1, or NULL
407 * \param DQ Private exponent modulo Q-1, or NULL
408 * \param QP Modular inverse of Q modulo P, or NULL
409 *
410 * \return 0 if successful, testifying that the non-NULL optional
411 * parameters provided are in accordance with the core
412 * RSA parameters. Non-zero error code otherwise.
413 *
414 * \note This function performs in-place computations on the
415 * parameters DP, DQ and QP. If modification cannot be
416 * tolerated, you should make copies with mbedtls_mpi_copy
417 * before calling this function.
418 *
419 */
Hanno Beckerd3637992017-08-25 07:55:03 +0100420int mbedtls_rsa_check_crt( const mbedtls_rsa_context *ctx,
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100421 mbedtls_mpi *DP,
422 mbedtls_mpi *DQ,
423 mbedtls_mpi *QP );
424
425/**
426 * \brief Export core parameters of an RSA key
427 *
428 * \param ctx Initialized RSA context
429 * \param N MPI to hold the RSA modulus, or NULL
430 * \param P MPI to hold the first prime factor of N, or NULL
431 * \param Q MPI to hold the second prime factor of N, or NULL
432 * \param D MPI to hold the private exponent, or NULL
433 * \param E MPI to hold the public exponent, or NULL
434 *
Hanno Beckered203612017-09-29 13:34:25 +0100435 * \return
436 * - 0 if successful. In this case, the non-NULL buffers
437 * pointed to by N, P, Q, D, E are fully written, with
438 * additional unused space filled leading by 0-bytes.
439 * - Non-zero return code otherwise. In particular, if
440 * exporting the requested parameters
441 * cannot be done because of a lack of functionality
442 * or because of security policies, the error code
443 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
444 * In this case, the RSA context stays intact and can
445 * be continued to be used.
Hanno Becker91c194d2017-09-29 12:50:12 +0100446 *
Hanno Beckered203612017-09-29 13:34:25 +0100447 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100448 * would be the following: Firstly, it might be that an
449 * alternative RSA implementation is in use which stores
450 * the key externally, and which either cannot or should not
451 * export it into RAM. Alternatively, an implementation
452 * (regardless of SW or HW) might not support deducing e.g.
453 * P, Q from N, D, E if the former are not part of the
454 * implementation.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100455 *
456 */
457int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
458 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
459 mbedtls_mpi *D, mbedtls_mpi *E );
460
461/**
462 * \brief Export core parameters of an RSA key
463 * in raw big-endian binary format
464 *
465 * \param ctx Initialized RSA context
466 * \param N Byte array to store the RSA modulus, or NULL
467 * \param N_len Size of buffer for modulus
468 * \param P Byte array to hold the first prime factor of N, or NULL
469 * \param P_len Size of buffer for first prime factor
470 * \param Q Byte array to hold the second prime factor of N, or NULL
471 * \param Q_len Size of buffer for second prime factor
472 * \param D Byte array to hold the private exponent, or NULL
473 * \param D_len Size of buffer for private exponent
474 * \param E Byte array to hold the public exponent, or NULL
475 * \param E_len Size of buffer for public exponent
476 *
477 * \note The length fields are ignored if the corresponding
478 * buffer pointers are NULL.
479 *
Hanno Beckered203612017-09-29 13:34:25 +0100480 * \return
481 * - 0 if successful. In this case, the non-NULL buffers
482 * pointed to by N, P, Q, D, E are fully written, with
483 * additional unused space filled leading by 0-bytes.
484 * - Non-zero return code otherwise. In particular, if
485 * exporting the requested parameters
486 * cannot be done because of a lack of functionality
487 * or because of security policies, the error code
488 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
489 * In this case, the RSA context stays intact and can
490 * be continued to be used.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100491 *
Hanno Beckered203612017-09-29 13:34:25 +0100492 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100493 * would be the following: Firstly, it might be that an
494 * alternative RSA implementation is in use which stores
495 * the key externally, and which either cannot or should not
496 * export it into RAM. Alternatively, an implementation
497 * (regardless of SW or HW) might not support deducing e.g.
498 * P, Q from N, D, E if the former are not part of the
499 * implementation.
500 *
501 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100502 */
503int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
504 unsigned char *N, size_t N_len,
505 unsigned char *P, size_t P_len,
506 unsigned char *Q, size_t Q_len,
507 unsigned char *D, size_t D_len,
508 unsigned char *E, size_t E_len );
509
510/**
511 * \brief Export CRT parameters of a private RSA key
512 *
513 * \param ctx Initialized RSA context
514 * \param DP MPI to hold D modulo P-1, or NULL
515 * \param DQ MPI to hold D modulo Q-1, or NULL
516 * \param QP MPI to hold modular inverse of Q modulo P, or NULL
517 *
518 * \return 0 if successful, non-zero error code otherwise.
519 *
520 * \note Alternative RSA implementations not using CRT-parameters
521 * internally can implement this function using based on
522 * \c mbedtls_rsa_deduce_opt.
523 *
524 */
525int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
526 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
527
Paul Bakker5121ce52009-01-03 21:22:43 +0000528/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100529 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100531 *
532 * \param ctx RSA context to be set
Hanno Becker4b2f6912017-09-29 13:34:55 +0100533 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
534 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100535 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100536void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
537 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100538
539/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100540 * \brief Get length of RSA modulus in bytes
541 *
542 * \param ctx Initialized RSA context
543 *
544 * \return Length of RSA modulus, in bytes.
545 *
546 */
547size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
548
549/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 * \brief Generate an RSA keypair
551 *
552 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000553 * \param f_rng RNG function
554 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000555 * \param nbits size of the public key in bits
556 * \param exponent public exponent (e.g., 65537)
557 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000559 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100561 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100564 int (*f_rng)(void *, unsigned char *, size_t),
565 void *p_rng,
566 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
568/**
Hanno Becker8fd55482017-08-23 14:07:48 +0100569 * \brief Check if a context contains an RSA public key
Paul Bakker5121ce52009-01-03 21:22:43 +0000570 *
571 * \param ctx RSA context to be checked
572 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100573 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
577/**
Hanno Becker8fd55482017-08-23 14:07:48 +0100578 * \brief Check if a context contains a complete
579 * and valid RSA private key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 *
581 * \param ctx RSA context to be checked
582 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100583 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
587/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100588 * \brief Check a public-private RSA key pair.
589 * Check each of the contexts, and make sure they match.
590 *
591 * \param pub RSA context holding the public key
592 * \param prv RSA context holding the private key
593 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100594 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100597
598/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 * \brief Do an RSA public key operation
600 *
601 * \param ctx RSA context
602 * \param input input buffer
603 * \param output output buffer
604 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100605 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000606 *
607 * \note This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800608 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000609 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 *
611 * \note The input and output buffers must be large
612 * enough (eg. 128 bytes if RSA-1024 is used).
613 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000615 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000616 unsigned char *output );
617
618/**
619 * \brief Do an RSA private key operation
620 *
621 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200622 * \param f_rng RNG function (Needed for blinding)
623 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000624 * \param input input buffer
625 * \param output output buffer
626 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100627 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000628 *
629 * \note The input and output buffers must be large
630 * enough (eg. 128 bytes if RSA-1024 is used).
631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200633 int (*f_rng)(void *, unsigned char *, size_t),
634 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000635 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000636 unsigned char *output );
637
638/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100639 * \brief Generic wrapper to perform a PKCS#1 encryption using the
640 * mode from the context. Add the message padding, then do an
641 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 *
643 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200644 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100645 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000646 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100647 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000648 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 * \param input buffer holding the data to be encrypted
650 * \param output buffer that will hold the ciphertext
651 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100652 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 *
654 * \note The output buffer must be as large as the size
655 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000658 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000659 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000660 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000661 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000662 unsigned char *output );
663
664/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100665 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
666 *
667 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100668 * \param f_rng RNG function (Needed for padding and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100669 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100670 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100671 * \param ilen contains the plaintext length
672 * \param input buffer holding the data to be encrypted
673 * \param output buffer that will hold the ciphertext
674 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100675 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100676 *
677 * \note The output buffer must be as large as the size
678 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100681 int (*f_rng)(void *, unsigned char *, size_t),
682 void *p_rng,
683 int mode, size_t ilen,
684 const unsigned char *input,
685 unsigned char *output );
686
687/**
688 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
689 *
690 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200691 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100692 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100693 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100694 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100695 * \param label buffer holding the custom label to use
696 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100697 * \param ilen contains the plaintext length
698 * \param input buffer holding the data to be encrypted
699 * \param output buffer that will hold the ciphertext
700 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100701 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100702 *
703 * \note The output buffer must be as large as the size
704 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100707 int (*f_rng)(void *, unsigned char *, size_t),
708 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100709 int mode,
710 const unsigned char *label, size_t label_len,
711 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100712 const unsigned char *input,
713 unsigned char *output );
714
715/**
716 * \brief Generic wrapper to perform a PKCS#1 decryption using the
717 * mode from the context. Do an RSA operation, then remove
718 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000719 *
720 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100721 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200722 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100723 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000724 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000725 * \param input buffer holding the encrypted data
726 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000727 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000728 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100729 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000730 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100731 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100732 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100733 * if RSA-1024 is used) to be able to hold an arbitrary
734 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100735 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100736 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100737 *
738 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100739 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000740 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200742 int (*f_rng)(void *, unsigned char *, size_t),
743 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000744 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000745 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000746 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000747 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000748
749/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100750 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
751 *
752 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100753 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200754 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100755 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100756 * \param olen will contain the plaintext length
757 * \param input buffer holding the encrypted data
758 * \param output buffer that will hold the plaintext
759 * \param output_max_len maximum length of the output buffer
760 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100761 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100762 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100763 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100764 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100765 * if RSA-1024 is used) to be able to hold an arbitrary
766 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100767 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100768 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100769 *
770 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100771 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100772 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200774 int (*f_rng)(void *, unsigned char *, size_t),
775 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100776 int mode, size_t *olen,
777 const unsigned char *input,
778 unsigned char *output,
779 size_t output_max_len );
780
781/**
782 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
783 *
784 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100785 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200786 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100787 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100788 * \param label buffer holding the custom label to use
789 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100790 * \param olen will contain the plaintext length
791 * \param input buffer holding the encrypted data
792 * \param output buffer that will hold the plaintext
793 * \param output_max_len maximum length of the output buffer
794 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100795 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100796 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100797 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100798 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100799 * if RSA-1024 is used) to be able to hold an arbitrary
800 * decrypted message. If it is not large enough to hold
Hanno Becker8fd55482017-08-23 14:07:48 +0100801 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100802 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100803 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100804 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100805 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100806 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200808 int (*f_rng)(void *, unsigned char *, size_t),
809 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100810 int mode,
811 const unsigned char *label, size_t label_len,
812 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100813 const unsigned char *input,
814 unsigned char *output,
815 size_t output_max_len );
816
817/**
818 * \brief Generic wrapper to perform a PKCS#1 signature using the
819 * mode from the context. Do a private RSA operation to sign
820 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000821 *
822 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200823 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100824 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000825 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100826 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
827 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for
828 * signing raw data)
829 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000830 * \param hash buffer holding the message digest
831 * \param sig buffer that will hold the ciphertext
832 *
833 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100834 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000835 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100836 * \note The \c sig buffer must be as large as the size
837 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000838 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200839 * \note In case of PKCS#1 v2.1 encoding, see comments on
Hanno Becker4b2f6912017-09-29 13:34:55 +0100840 * \note \c mbedtls_rsa_rsassa_pss_sign() for details on
841 * \c md_alg and \c hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000844 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000845 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000846 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000848 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000849 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000850 unsigned char *sig );
851
852/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100853 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
854 *
855 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100856 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200857 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100858 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
859 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
860 * for signing raw data)
861 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100862 * \param hash buffer holding the message digest
863 * \param sig buffer that will hold the ciphertext
864 *
865 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100866 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100867 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100868 * \note The \c sig buffer must be as large as the size
869 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200872 int (*f_rng)(void *, unsigned char *, size_t),
873 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100874 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100876 unsigned int hashlen,
877 const unsigned char *hash,
878 unsigned char *sig );
879
880/**
881 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
882 *
883 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200884 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100885 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100886 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100887 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
888 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
889 * for signing raw data)
890 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100891 * \param hash buffer holding the message digest
892 * \param sig buffer that will hold the ciphertext
893 *
894 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100895 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100896 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100897 * \note The \c sig buffer must be as large as the size
898 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100899 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100900 * \note The \c hash_id in the RSA context is the one used for the
901 * encoding. \c md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100902 * that is encoded. According to RFC 3447 it is advised to
903 * keep both hashes the same.
904 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100906 int (*f_rng)(void *, unsigned char *, size_t),
907 void *p_rng,
908 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100910 unsigned int hashlen,
911 const unsigned char *hash,
912 unsigned char *sig );
913
914/**
915 * \brief Generic wrapper to perform a PKCS#1 verification using the
916 * mode from the context. Do a public RSA operation and check
917 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000918 *
919 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100920 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200921 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100922 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
923 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
924 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000925 * \param hash buffer holding the message digest
926 * \param sig buffer holding the ciphertext
927 *
928 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100929 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000930 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100931 * \note The \c sig buffer must be as large as the size
932 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000933 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200934 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000936 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200938 int (*f_rng)(void *, unsigned char *, size_t),
939 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000940 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000942 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000943 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200944 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000945
946/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100947 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
948 *
949 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100950 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200951 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100952 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
953 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
954 * for signing raw data)
955 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100956 * \param hash buffer holding the message digest
957 * \param sig buffer holding the ciphertext
958 *
959 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100960 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100961 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100962 * \note The \c sig buffer must be as large as the size
963 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100964 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200966 int (*f_rng)(void *, unsigned char *, size_t),
967 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100968 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100970 unsigned int hashlen,
971 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200972 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100973
974/**
975 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200976 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +0100977 *
978 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100979 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200980 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100981 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
982 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
983 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100984 * \param hash buffer holding the message digest
985 * \param sig buffer holding the ciphertext
986 *
987 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100988 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100989 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100990 * \note The \c sig buffer must be as large as the size
991 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100992 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100993 * \note The \c hash_id in the RSA context is the one used for the
994 * verification. \c md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200995 * hash that is verified. According to RFC 3447 it is advised to
Hanno Becker4b2f6912017-09-29 13:34:55 +0100996 * keep both hashes the same. If \c hash_id in the RSA context is
997 * unset, the \c md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100998 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001000 int (*f_rng)(void *, unsigned char *, size_t),
1001 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001002 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001004 unsigned int hashlen,
1005 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001006 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001007
1008/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001009 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
1010 * (This is the version with "full" options.)
1011 *
1012 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +01001013 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001014 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +01001015 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
1016 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
1017 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001018 * \param hash buffer holding the message digest
1019 * \param mgf1_hash_id message digest used for mask generation
1020 * \param expected_salt_len Length of the salt used in padding, use
Hanno Becker4b2f6912017-09-29 13:34:55 +01001021 * \c MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001022 * \param sig buffer holding the ciphertext
1023 *
1024 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001025 * or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001026 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001027 * \note The \c sig buffer must be as large as the size
1028 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001029 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001030 * \note The \c hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001033 int (*f_rng)(void *, unsigned char *, size_t),
1034 void *p_rng,
1035 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001037 unsigned int hashlen,
1038 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001040 int expected_salt_len,
1041 const unsigned char *sig );
1042
1043/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001044 * \brief Copy the components of an RSA context
1045 *
1046 * \param dst Destination context
1047 * \param src Source context
1048 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001049 * \return 0 on success,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001050 * \c MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001053
1054/**
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001056 *
1057 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +00001058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001060
1061/**
1062 * \brief Checkup routine
1063 *
1064 * \return 0 if successful, or 1 if the test failed
1065 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001067
1068#ifdef __cplusplus
1069}
1070#endif
1071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#endif /* MBEDTLS_RSA_C */
Paul Bakkered27a042013-04-18 22:46:23 +02001073
Paul Bakker5121ce52009-01-03 21:22:43 +00001074#endif /* rsa.h */