blob: 04175eb4f83b5406d03155a40616094e4ce1d17d [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
Hanno Beckera3ebec22017-08-23 14:06:24 +010092 * \param P Pointer to MPI holding first prime factor of N on success
93 * \param Q Pointer to MPI holding second prime factor of N on success
94 *
Hanno Beckered203612017-09-29 13:34:25 +010095 * \return
96 * - 0 if successful. In this case, P and Q constitute a
Hanno Beckerba5b7552017-10-02 09:55:49 +010097 * factorization of N.
98 * - A non-zero error code otherwise.
Hanno Beckera3ebec22017-08-23 14:06:24 +010099 *
Hanno Becker1b831fe2017-10-02 12:24:50 +0100100 * \note It is neither checked that P, Q are prime nor that
101 * D, E are modular inverses wrt. P-1 and Q-1. For that,
102 * use the helper function \c mbedtls_rsa_validate_params.
103 *
Hanno Beckera3ebec22017-08-23 14:06:24 +0100104 */
Hanno Becker0f65e0c2017-10-03 14:39:16 +0100105int mbedtls_rsa_deduce_primes( mbedtls_mpi const *N, mbedtls_mpi const *D,
Hanno Becker68b4d582017-10-10 16:39:10 +0100106 mbedtls_mpi const *E,
107 mbedtls_mpi *P, mbedtls_mpi *Q );
Hanno Beckera3ebec22017-08-23 14:06:24 +0100108
109/**
110 * \brief Compute RSA private exponent from
111 * prime moduli and public key.
112 *
113 * \note This is a 'static' helper function not operating on
114 * an RSA context. Alternative implementations need not
115 * overwrite it.
116 *
117 * \param P First prime factor of RSA modulus
118 * \param Q Second prime factor of RSA modulus
119 * \param E RSA public exponent
120 * \param D Pointer to MPI holding the private exponent on success.
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 Becker1b831fe2017-10-02 12:24:50 +0100127 * \note This function does not check whether P and Q are primes.
128 *
Hanno Beckera3ebec22017-08-23 14:06:24 +0100129 */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100130int mbedtls_rsa_deduce_private_exponent( mbedtls_mpi const *P,
131 mbedtls_mpi const *Q,
132 mbedtls_mpi const *E,
133 mbedtls_mpi *D );
Hanno Beckera3ebec22017-08-23 14:06:24 +0100134
135
136/**
137 * \brief Generate RSA-CRT parameters
138 *
139 * \note This is a 'static' helper function not operating on
140 * an RSA context. Alternative implementations need not
141 * overwrite it.
142 *
143 * \param P First prime factor of N
144 * \param Q Second prime factor of N
145 * \param D RSA private exponent
146 * \param DP Output variable for D modulo P-1
147 * \param DQ Output variable for D modulo Q-1
148 * \param QP Output variable for the modular inverse of Q modulo P.
149 *
150 * \return 0 on success, non-zero error code otherwise.
151 *
Hanno Becker1b831fe2017-10-02 12:24:50 +0100152 * \note This function does not check whether P, Q are
153 * prime and whether D is a valid private exponent.
154 *
Hanno Beckera3ebec22017-08-23 14:06:24 +0100155 */
156int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
157 const mbedtls_mpi *D, mbedtls_mpi *DP,
158 mbedtls_mpi *DQ, mbedtls_mpi *QP );
159
160
161/**
162 * \brief Check validity of core RSA parameters
163 *
164 * \note This is a 'static' helper function not operating on
165 * an RSA context. Alternative implementations need not
166 * overwrite it.
167 *
168 * \param N RSA modulus N = PQ
169 * \param P First prime factor of N
170 * \param Q Second prime factor of N
171 * \param D RSA private exponent
172 * \param E RSA public exponent
Hanno Becker56bae952017-09-29 15:33:10 +0100173 * \param f_rng PRNG to be used for primality check, or NULL
Hanno Beckera3ebec22017-08-23 14:06:24 +0100174 * \param p_rng PRNG context for f_rng, or NULL
175 *
Hanno Beckered203612017-09-29 13:34:25 +0100176 * \return
Hanno Beckerb5beaa82017-10-02 13:01:43 +0100177 * - 0 if the following conditions are satisfied
178 * if all relevant parameters are provided:
179 * - P prime if f_rng != NULL
180 * - Q prime if f_rng != NULL
181 * - 1 < N = PQ
182 * - 1 < D, E < N
Hanno Beckered203612017-09-29 13:34:25 +0100183 * - D and E are modular inverses modulo P-1 and Q-1
Hanno Becker750e8b42017-08-25 07:54:27 +0100184 * - A non-zero error code otherwise.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100185 *
186 * \note The function can be used with a restricted set of arguments
187 * to perform specific checks only. E.g., calling it with
188 * (-,P,-,-,-) and a PRNG amounts to a primality check for P.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100189 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100190int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
191 const mbedtls_mpi *Q, const mbedtls_mpi *D,
192 const mbedtls_mpi *E,
193 int (*f_rng)(void *, unsigned char *, size_t),
194 void *p_rng );
Hanno Beckera3ebec22017-08-23 14:06:24 +0100195
196/**
Hanno Beckerd3637992017-08-25 07:55:03 +0100197 * \brief Check validity of RSA CRT parameters
198 *
199 * \note This is a 'static' helper function not operating on
200 * an RSA context. Alternative implementations need not
201 * overwrite it.
202 *
203 * \param P First prime factor of RSA modulus
204 * \param Q Second prime factor of RSA modulus
205 * \param D RSA private exponent
206 * \param DP MPI to check for D modulo P-1
207 * \param DQ MPI to check for D modulo P-1
208 * \param QP MPI to check for the modular inverse of Q modulo P.
209 *
Hanno Beckered203612017-09-29 13:34:25 +0100210 * \return
211 * - 0 if the following conditions are satisfied:
212 * - D = DP mod P-1 if P, D, DP != NULL
213 * - Q = DQ mod P-1 if P, D, DQ != NULL
214 * - QP = Q^-1 mod P if P, Q, QP != NULL
215 * - \c MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if check failed,
216 * potentially including \c MBEDTLS_ERR_MPI_XXX if some
Hanno Beckerd3637992017-08-25 07:55:03 +0100217 * MPI calculations failed.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100218 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if insufficient
Hanno Beckerd3637992017-08-25 07:55:03 +0100219 * data was provided to check DP, DQ or QP.
220 *
221 * \note The function can be used with a restricted set of arguments
222 * to perform specific checks only. E.g., calling it with the
223 * parameters (P, -, D, DP, -, -) will check DP = D mod P-1.
224 */
225int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
226 const mbedtls_mpi *D, const mbedtls_mpi *DP,
227 const mbedtls_mpi *DQ, const mbedtls_mpi *QP );
228
229/**
Hanno Beckera3ebec22017-08-23 14:06:24 +0100230 * Implementation of RSA interface
231 */
232
Hanno Beckerab377312017-08-23 16:24:51 +0100233#if !defined(MBEDTLS_RSA_ALT)
234
Hanno Beckera3ebec22017-08-23 14:06:24 +0100235/**
Hanno Becker5063cd22017-09-29 11:49:12 +0100236 * \brief RSA context structure
237 *
238 * \note Direct manipulation of the members of this structure
239 * is deprecated and will no longer be supported starting
240 * from the next major release. All manipulation should instead
241 * be done through the public interface functions.
242 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 */
244typedef struct
245{
246 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +0000247 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +0000248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_mpi N; /*!< public modulus */
250 mbedtls_mpi E; /*!< public exponent */
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_mpi D; /*!< private exponent */
253 mbedtls_mpi P; /*!< 1st prime factor */
254 mbedtls_mpi Q; /*!< 2nd prime factor */
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 DP; /*!< D % (P - 1) */
258 mbedtls_mpi DQ; /*!< D % (Q - 1) */
259 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Hanno Becker1a59e792017-08-23 07:41:10 +0100260#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_mpi RN; /*!< cached R^2 mod N */
Hanno Becker1a59e792017-08-23 07:41:10 +0100263
264#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_mpi RP; /*!< cached R^2 mod P */
266 mbedtls_mpi RQ; /*!< cached R^2 mod Q */
Hanno Becker1a59e792017-08-23 07:41:10 +0100267#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_mpi Vi; /*!< cached blinding value */
270 mbedtls_mpi Vf; /*!< cached un-blinding value */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200271
Hanno Becker4b2f6912017-09-29 13:34:55 +0100272 int padding; /*!< \c MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
273 \c MBEDTLS_RSA_PKCS_v21 for OAEP/PSS */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
275 specified in the mbedtls_md.h header file
Paul Bakker9dcc3222011-03-08 14:16:06 +0000276 for the EME-OAEP and EMSA-PSS
277 encoding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278#if defined(MBEDTLS_THREADING_C)
279 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200280#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000281}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000283
Hanno Beckerab377312017-08-23 16:24:51 +0100284#else
285
286#include "rsa_alt.h"
287
288#endif /* MBEDTLS_RSA_ALT */
289
Paul Bakker5121ce52009-01-03 21:22:43 +0000290/**
291 * \brief Initialize an RSA context
292 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100293 * Note: Set padding to \c MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000294 * encryption scheme and the RSASSA-PSS signature scheme.
295 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000296 * \param ctx RSA context to be initialized
Hanno Becker4b2f6912017-09-29 13:34:55 +0100297 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
298 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 *
300 * \note The hash_id parameter is actually ignored
Hanno Becker4b2f6912017-09-29 13:34:55 +0100301 * when using \c MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200302 *
303 * \note Choice of padding mode is strictly enforced for private key
304 * operations, since there might be security concerns in
305 * mixing padding modes. For public key operations it's merely
306 * a default value, which can be overriden by calling specific
307 * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
308 *
309 * \note The chosen hash is always used for OEAP encryption.
310 * For PSS signatures, it's always used for making signatures,
311 * but can be overriden (and always is, if set to
Hanno Becker4b2f6912017-09-29 13:34:55 +0100312 * \c MBEDTLS_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100315 int padding,
316 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100318/**
319 * \brief Import a set of core parameters into an RSA context
320 *
321 * \param ctx Initialized RSA context to store parameters
322 * \param N RSA modulus, or NULL
323 * \param P First prime factor of N, or NULL
324 * \param Q Second prime factor of N, or NULL
325 * \param D Private exponent, or NULL
326 * \param E Public exponent, or NULL
327 *
328 * \note This function can be called multiple times for successive
329 * imports if the parameters are not simultaneously present.
330 * Any sequence of calls to this function should be followed
331 * by a call to \c mbedtls_rsa_complete which will check
332 * and complete the provided information to a ready-for-use
333 * public or private RSA key.
334 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100335 * \note The imported parameters are copied and need not be preserved
336 * for the lifetime of the RSA context being set up.
337 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100338 * \return 0 if successful, non-zero error code on failure.
339 */
340int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
341 const mbedtls_mpi *N,
342 const mbedtls_mpi *P, const mbedtls_mpi *Q,
343 const mbedtls_mpi *D, const mbedtls_mpi *E );
344
345/**
346 * \brief Import core RSA parameters in raw big-endian
347 * binary format into an RSA context
348 *
349 * \param ctx Initialized RSA context to store parameters
350 * \param N RSA modulus, or NULL
351 * \param N_len Byte length of N, ignored if N == NULL
352 * \param P First prime factor of N, or NULL
353 * \param P_len Byte length of P, ignored if P == NULL
354 * \param Q Second prime factor of N, or NULL
355 * \param Q_len Byte length of Q, ignored if Q == NULL
356 * \param D Private exponent, or NULL
357 * \param D_len Byte length of D, ignored if D == NULL
358 * \param E Public exponent, or NULL
359 * \param E_len Byte length of E, ignored if E == NULL
360 *
361 * \note This function can be called multiple times for successive
362 * imports if the parameters are not simultaneously present.
363 * Any sequence of calls to this function should be followed
364 * by a call to \c mbedtls_rsa_complete which will check
365 * and complete the provided information to a ready-for-use
366 * public or private RSA key.
367 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100368 * \note The imported parameters are copied and need not be preserved
369 * for the lifetime of the RSA context being set up.
370 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100371 * \return 0 if successful, non-zero error code on failure.
372 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100373int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100374 unsigned char const *N, size_t N_len,
375 unsigned char const *P, size_t P_len,
376 unsigned char const *Q, size_t Q_len,
377 unsigned char const *D, size_t D_len,
378 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100379
380/**
381 * \brief Attempt to complete an RSA context from
382 * a set of imported core parameters.
383 *
384 * \param ctx Initialized RSA context to store parameters
Hanno Becker43a08d02017-10-02 13:16:35 +0100385 * \param f_rng RNG function, or NULL
386 * \param p_rng RNG parameter, or NULL
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100387 *
Hanno Beckered203612017-09-29 13:34:25 +0100388 * \note
389 * - To setup an RSA public key, precisely N and E
390 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100391 *
Hanno Beckered203612017-09-29 13:34:25 +0100392 * - To setup an RSA private key, enough information must be
Hanno Becker98838b02017-10-02 13:16:10 +0100393 * present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100394 *
Hanno Beckered203612017-09-29 13:34:25 +0100395 * The default implementation supports the following:
396 * - Derive P, Q from N, D, E
397 * - Derive N, D from P, Q, E.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100398 *
Hanno Beckered203612017-09-29 13:34:25 +0100399 * - Alternative implementations need not support these
400 * and may return \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA instead.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100401 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100402 * \note The PRNG is used for the probabilistic algorithm
403 * used in the derivation of P, Q from N, D, E. If it
404 * not present, a deterministic heuristic is used.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100405 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100406 * \return
407 * - 0 if successful. In this case, it is guaranteed
408 * the functions \c mbedtls_rsa_check_pubkey resp.
409 * \c mbedtls_rsa_check_privkey pass in case of a
410 * public resp. private key.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100411 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100412 * derivations failed.
Hanno Becker43a08d02017-10-02 13:16:35 +0100413 *
414 * \warning Implementations are *not* obliged to perform exhaustive
415 * validation of the imported parameters!
416 * In particular, parameters that are not needed by the
417 * implementation may be silently discarded and left unchecked.
418 * If the user mistrusts the given key material, he should
419 * employ other means for verification like the helper functions
420 * \c mbedtls_rsa_validate_params, \c mbedtls_rsa_validate_crt.
421 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100422 */
423int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
424 int (*f_rng)(void *, unsigned char *, size_t),
425 void *p_rng );
426
427/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100428 * \brief Export core parameters of an RSA key
429 *
430 * \param ctx Initialized RSA context
431 * \param N MPI to hold the RSA modulus, or NULL
432 * \param P MPI to hold the first prime factor of N, or NULL
433 * \param Q MPI to hold the second prime factor of N, or NULL
434 * \param D MPI to hold the private exponent, or NULL
435 * \param E MPI to hold the public exponent, or NULL
436 *
Hanno Beckered203612017-09-29 13:34:25 +0100437 * \return
438 * - 0 if successful. In this case, the non-NULL buffers
439 * pointed to by N, P, Q, D, E are fully written, with
440 * additional unused space filled leading by 0-bytes.
441 * - Non-zero return code otherwise. In particular, if
442 * exporting the requested parameters
443 * cannot be done because of a lack of functionality
444 * or because of security policies, the error code
445 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
446 * In this case, the RSA context stays intact and can
447 * be continued to be used.
Hanno Becker91c194d2017-09-29 12:50:12 +0100448 *
Hanno Beckered203612017-09-29 13:34:25 +0100449 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100450 * would be the following: Firstly, it might be that an
451 * alternative RSA implementation is in use which stores
452 * the key externally, and which either cannot or should not
453 * export it into RAM. Alternatively, an implementation
454 * (regardless of SW or HW) might not support deducing e.g.
455 * P, Q from N, D, E if the former are not part of the
456 * implementation.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100457 *
458 */
459int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
460 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
461 mbedtls_mpi *D, mbedtls_mpi *E );
462
463/**
464 * \brief Export core parameters of an RSA key
465 * in raw big-endian binary format
466 *
467 * \param ctx Initialized RSA context
468 * \param N Byte array to store the RSA modulus, or NULL
469 * \param N_len Size of buffer for modulus
470 * \param P Byte array to hold the first prime factor of N, or NULL
471 * \param P_len Size of buffer for first prime factor
472 * \param Q Byte array to hold the second prime factor of N, or NULL
473 * \param Q_len Size of buffer for second prime factor
474 * \param D Byte array to hold the private exponent, or NULL
475 * \param D_len Size of buffer for private exponent
476 * \param E Byte array to hold the public exponent, or NULL
477 * \param E_len Size of buffer for public exponent
478 *
479 * \note The length fields are ignored if the corresponding
480 * buffer pointers are NULL.
481 *
Hanno Beckered203612017-09-29 13:34:25 +0100482 * \return
483 * - 0 if successful. In this case, the non-NULL buffers
484 * pointed to by N, P, Q, D, E are fully written, with
485 * additional unused space filled leading by 0-bytes.
486 * - Non-zero return code otherwise. In particular, if
487 * exporting the requested parameters
488 * cannot be done because of a lack of functionality
489 * or because of security policies, the error code
490 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
491 * In this case, the RSA context stays intact and can
492 * be continued to be used.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100493 *
Hanno Beckered203612017-09-29 13:34:25 +0100494 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100495 * would be the following: Firstly, it might be that an
496 * alternative RSA implementation is in use which stores
497 * the key externally, and which either cannot or should not
498 * export it into RAM. Alternatively, an implementation
499 * (regardless of SW or HW) might not support deducing e.g.
500 * P, Q from N, D, E if the former are not part of the
501 * implementation.
502 *
503 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100504 */
505int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
506 unsigned char *N, size_t N_len,
507 unsigned char *P, size_t P_len,
508 unsigned char *Q, size_t Q_len,
509 unsigned char *D, size_t D_len,
510 unsigned char *E, size_t E_len );
511
512/**
513 * \brief Export CRT parameters of a private RSA key
514 *
515 * \param ctx Initialized RSA context
516 * \param DP MPI to hold D modulo P-1, or NULL
517 * \param DQ MPI to hold D modulo Q-1, or NULL
518 * \param QP MPI to hold modular inverse of Q modulo P, or NULL
519 *
520 * \return 0 if successful, non-zero error code otherwise.
521 *
522 * \note Alternative RSA implementations not using CRT-parameters
523 * internally can implement this function using based on
524 * \c mbedtls_rsa_deduce_opt.
525 *
526 */
527int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
528 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
529
Paul Bakker5121ce52009-01-03 21:22:43 +0000530/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100531 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100533 *
534 * \param ctx RSA context to be set
Hanno Becker4b2f6912017-09-29 13:34:55 +0100535 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
536 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100537 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100538void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
539 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100540
541/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100542 * \brief Get length of RSA modulus in bytes
543 *
544 * \param ctx Initialized RSA context
545 *
546 * \return Length of RSA modulus, in bytes.
547 *
548 */
549size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
550
551/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 * \brief Generate an RSA keypair
553 *
554 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000555 * \param f_rng RNG function
556 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 * \param nbits size of the public key in bits
558 * \param exponent public exponent (e.g., 65537)
559 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000561 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100563 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100566 int (*f_rng)(void *, unsigned char *, size_t),
567 void *p_rng,
568 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
570/**
Hanno Becker43a08d02017-10-02 13:16:35 +0100571 * \brief Check if a context contains (at least) an RSA public key
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 *
573 * \param ctx RSA context to be checked
574 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100575 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
576 * On success, it is guaranteed that enough information is
577 * present to perform an RSA public key operation
578 * \c mbedtls_rsa_public.
579 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
583/**
Hanno Becker43a08d02017-10-02 13:16:35 +0100584 * \brief Check if a context contains an RSA private key
585 * and perform basic sanity checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 *
587 * \param ctx RSA context to be checked
588 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100589 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
590 * On success, it is guaranteed that enough information is
591 * present to perform RSA private and public key operations.
592 *
593 * \warning This function is *not* obliged to perform an exhaustive
594 * sanity check what would guarantee the internal parameters
595 * to match and \c mbedtls_rsa_private and \c mbedtls_rsa_public
596 * to be mutually inverse to each other.
597 * The reason is that for minimal non-CRT implementations
598 * using only N, D, E, for example, checking the validity
599 * would be computationally expensive.
600 * Users mistrusting their key material should use other
601 * means for verification; see the documentation of
602 * \c mbedtls_rsa_complete.
603 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
607/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100608 * \brief Check a public-private RSA key pair.
609 * Check each of the contexts, and make sure they match.
610 *
611 * \param pub RSA context holding the public key
612 * \param prv RSA context holding the private key
613 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100614 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100615 */
Hanno Becker98838b02017-10-02 13:16:10 +0100616int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
617 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100618
619/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 * \brief Do an RSA public key operation
621 *
622 * \param ctx RSA context
623 * \param input input buffer
624 * \param output output buffer
625 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100626 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000627 *
628 * \note This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800629 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000630 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000631 *
632 * \note The input and output buffers must be large
633 * enough (eg. 128 bytes if RSA-1024 is used).
634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000636 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 unsigned char *output );
638
639/**
640 * \brief Do an RSA private key operation
641 *
642 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200643 * \param f_rng RNG function (Needed for blinding)
644 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 * \param input input buffer
646 * \param output output buffer
647 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100648 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 *
650 * \note The input and output buffers must be large
651 * enough (eg. 128 bytes if RSA-1024 is used).
652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200654 int (*f_rng)(void *, unsigned char *, size_t),
655 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000656 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000657 unsigned char *output );
658
659/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100660 * \brief Generic wrapper to perform a PKCS#1 encryption using the
661 * mode from the context. Add the message padding, then do an
662 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 *
664 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200665 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100666 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000667 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100668 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000669 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 * \param input buffer holding the data to be encrypted
671 * \param output buffer that will hold the ciphertext
672 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100673 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000674 *
675 * \note The output buffer must be as large as the size
676 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000679 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000680 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000681 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000682 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 unsigned char *output );
684
685/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100686 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
687 *
688 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100689 * \param f_rng RNG function (Needed for padding and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100690 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100691 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100692 * \param ilen contains the plaintext length
693 * \param input buffer holding the data to be encrypted
694 * \param output buffer that will hold the ciphertext
695 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100696 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100697 *
698 * \note The output buffer must be as large as the size
699 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100702 int (*f_rng)(void *, unsigned char *, size_t),
703 void *p_rng,
704 int mode, size_t ilen,
705 const unsigned char *input,
706 unsigned char *output );
707
708/**
709 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
710 *
711 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200712 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100713 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100714 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100715 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100716 * \param label buffer holding the custom label to use
717 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100718 * \param ilen contains the plaintext length
719 * \param input buffer holding the data to be encrypted
720 * \param output buffer that will hold the ciphertext
721 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100722 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100723 *
724 * \note The output buffer must be as large as the size
725 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100728 int (*f_rng)(void *, unsigned char *, size_t),
729 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100730 int mode,
731 const unsigned char *label, size_t label_len,
732 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100733 const unsigned char *input,
734 unsigned char *output );
735
736/**
737 * \brief Generic wrapper to perform a PKCS#1 decryption using the
738 * mode from the context. Do an RSA operation, then remove
739 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000740 *
741 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100742 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200743 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100744 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000745 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000746 * \param input buffer holding the encrypted data
747 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000748 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000749 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100750 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000751 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100752 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100753 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100754 * if RSA-1024 is used) to be able to hold an arbitrary
755 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100756 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100757 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100758 *
759 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100760 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000761 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200763 int (*f_rng)(void *, unsigned char *, size_t),
764 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000765 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000766 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000767 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000768 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
770/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100771 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
772 *
773 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100774 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200775 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100776 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100777 * \param olen will contain the plaintext length
778 * \param input buffer holding the encrypted data
779 * \param output buffer that will hold the plaintext
780 * \param output_max_len maximum length of the output buffer
781 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100782 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100783 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100784 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100785 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100786 * if RSA-1024 is used) to be able to hold an arbitrary
787 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100788 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100789 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100790 *
791 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100792 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100793 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200795 int (*f_rng)(void *, unsigned char *, size_t),
796 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100797 int mode, size_t *olen,
798 const unsigned char *input,
799 unsigned char *output,
800 size_t output_max_len );
801
802/**
803 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
804 *
805 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100806 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200807 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100808 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100809 * \param label buffer holding the custom label to use
810 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100811 * \param olen will contain the plaintext length
812 * \param input buffer holding the encrypted data
813 * \param output buffer that will hold the plaintext
814 * \param output_max_len maximum length of the output buffer
815 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100816 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100817 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100818 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100819 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100820 * if RSA-1024 is used) to be able to hold an arbitrary
821 * decrypted message. If it is not large enough to hold
Hanno Becker8fd55482017-08-23 14:07:48 +0100822 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100823 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100824 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100825 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100826 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200829 int (*f_rng)(void *, unsigned char *, size_t),
830 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100831 int mode,
832 const unsigned char *label, size_t label_len,
833 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100834 const unsigned char *input,
835 unsigned char *output,
836 size_t output_max_len );
837
838/**
839 * \brief Generic wrapper to perform a PKCS#1 signature using the
840 * mode from the context. Do a private RSA operation to sign
841 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000842 *
843 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200844 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100845 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000846 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100847 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
848 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for
849 * signing raw data)
850 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000851 * \param hash buffer holding the message digest
852 * \param sig buffer that will hold the ciphertext
853 *
854 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100855 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000856 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100857 * \note The \c sig buffer must be as large as the size
858 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000859 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200860 * \note In case of PKCS#1 v2.1 encoding, see comments on
Hanno Becker4b2f6912017-09-29 13:34:55 +0100861 * \note \c mbedtls_rsa_rsassa_pss_sign() for details on
862 * \c md_alg and \c hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000863 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000865 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000866 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000867 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000869 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000870 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000871 unsigned char *sig );
872
873/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100874 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
875 *
876 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100877 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200878 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100879 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
880 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
881 * for signing raw data)
882 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100883 * \param hash buffer holding the message digest
884 * \param sig buffer that will hold the ciphertext
885 *
886 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100887 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100888 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100889 * \note The \c sig buffer must be as large as the size
890 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100891 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200893 int (*f_rng)(void *, unsigned char *, size_t),
894 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100895 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100897 unsigned int hashlen,
898 const unsigned char *hash,
899 unsigned char *sig );
900
901/**
902 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
903 *
904 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200905 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100906 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100907 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100908 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
909 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
910 * for signing raw data)
911 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100912 * \param hash buffer holding the message digest
913 * \param sig buffer that will hold the ciphertext
914 *
915 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100916 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100917 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100918 * \note The \c sig buffer must be as large as the size
919 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100920 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100921 * \note The \c hash_id in the RSA context is the one used for the
922 * encoding. \c md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100923 * that is encoded. According to RFC 3447 it is advised to
924 * keep both hashes the same.
925 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100927 int (*f_rng)(void *, unsigned char *, size_t),
928 void *p_rng,
929 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100931 unsigned int hashlen,
932 const unsigned char *hash,
933 unsigned char *sig );
934
935/**
936 * \brief Generic wrapper to perform a PKCS#1 verification using the
937 * mode from the context. Do a public RSA operation and check
938 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000939 *
940 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100941 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200942 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100943 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
944 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
945 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000946 * \param hash buffer holding the message digest
947 * \param sig buffer holding the ciphertext
948 *
949 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100950 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000951 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100952 * \note The \c sig buffer must be as large as the size
953 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000954 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200955 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000957 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200959 int (*f_rng)(void *, unsigned char *, size_t),
960 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000961 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000963 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000964 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200965 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000966
967/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100968 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
969 *
970 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100971 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200972 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100973 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
974 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
975 * for signing raw data)
976 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100977 * \param hash buffer holding the message digest
978 * \param sig buffer holding the ciphertext
979 *
980 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100981 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100982 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100983 * \note The \c sig buffer must be as large as the size
984 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100985 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200987 int (*f_rng)(void *, unsigned char *, size_t),
988 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100989 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100991 unsigned int hashlen,
992 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200993 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100994
995/**
996 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200997 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +0100998 *
999 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +01001000 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +02001001 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +01001002 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
1003 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
1004 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +01001005 * \param hash buffer holding the message digest
1006 * \param sig buffer holding the ciphertext
1007 *
1008 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001009 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +01001010 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001011 * \note The \c sig buffer must be as large as the size
1012 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +01001013 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001014 * \note The \c hash_id in the RSA context is the one used for the
1015 * verification. \c md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001016 * hash that is verified. According to RFC 3447 it is advised to
Hanno Becker4b2f6912017-09-29 13:34:55 +01001017 * keep both hashes the same. If \c hash_id in the RSA context is
1018 * unset, the \c md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001019 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001021 int (*f_rng)(void *, unsigned char *, size_t),
1022 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001023 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001025 unsigned int hashlen,
1026 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001027 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001028
1029/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001030 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
1031 * (This is the version with "full" options.)
1032 *
1033 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +01001034 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001035 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +01001036 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
1037 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
1038 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001039 * \param hash buffer holding the message digest
1040 * \param mgf1_hash_id message digest used for mask generation
1041 * \param expected_salt_len Length of the salt used in padding, use
Hanno Becker4b2f6912017-09-29 13:34:55 +01001042 * \c MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001043 * \param sig buffer holding the ciphertext
1044 *
1045 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001046 * or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001047 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001048 * \note The \c sig buffer must be as large as the size
1049 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001050 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001051 * \note The \c hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001052 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001054 int (*f_rng)(void *, unsigned char *, size_t),
1055 void *p_rng,
1056 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001058 unsigned int hashlen,
1059 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001061 int expected_salt_len,
1062 const unsigned char *sig );
1063
1064/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001065 * \brief Copy the components of an RSA context
1066 *
1067 * \param dst Destination context
1068 * \param src Source context
1069 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001070 * \return 0 on success,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001071 * \c MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001074
1075/**
Paul Bakker5121ce52009-01-03 21:22:43 +00001076 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001077 *
1078 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +00001079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
1082/**
1083 * \brief Checkup routine
1084 *
1085 * \return 0 if successful, or 1 if the test failed
1086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001088
1089#ifdef __cplusplus
1090}
1091#endif
1092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093#endif /* MBEDTLS_RSA_C */
Paul Bakkered27a042013-04-18 22:46:23 +02001094
Paul Bakker5121ce52009-01-03 21:22:43 +00001095#endif /* rsa.h */