blob: 0c649073e0c17f7d1be01e9d9e0323948ef2d1be [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
Hanno Becker1e801f52017-10-10 16:44:47 +0100408 * that the RSA context can be used for RSA operations
409 * without the risk of failure or crash.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100410 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100411 * derivations failed.
Hanno Becker43a08d02017-10-02 13:16:35 +0100412 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100413 * \warning This function need not perform consistency checks
414 * for the imported parameters! In particular, parameters that
415 * are not needed by the implementation may be silently discarded
416 * and left unchecked. For the purpose of checking the consistency
417 * of the key material, see \c mbedtls_rsa_check_privkey.
Hanno Becker43a08d02017-10-02 13:16:35 +0100418 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100419 */
420int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
421 int (*f_rng)(void *, unsigned char *, size_t),
422 void *p_rng );
423
424/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100425 * \brief Export core parameters of an RSA key
426 *
427 * \param ctx Initialized RSA context
428 * \param N MPI to hold the RSA modulus, or NULL
429 * \param P MPI to hold the first prime factor of N, or NULL
430 * \param Q MPI to hold the second prime factor of N, or NULL
431 * \param D MPI to hold the private exponent, or NULL
432 * \param E MPI to hold the public exponent, or NULL
433 *
Hanno Beckered203612017-09-29 13:34:25 +0100434 * \return
435 * - 0 if successful. In this case, the non-NULL buffers
436 * pointed to by N, P, Q, D, E are fully written, with
437 * additional unused space filled leading by 0-bytes.
438 * - Non-zero return code otherwise. In particular, if
439 * exporting the requested parameters
440 * cannot be done because of a lack of functionality
441 * or because of security policies, the error code
442 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
443 * In this case, the RSA context stays intact and can
444 * be continued to be used.
Hanno Becker91c194d2017-09-29 12:50:12 +0100445 *
Hanno Beckered203612017-09-29 13:34:25 +0100446 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100447 * would be the following: Firstly, it might be that an
448 * alternative RSA implementation is in use which stores
449 * the key externally, and which either cannot or should not
450 * export it into RAM. Alternatively, an implementation
451 * (regardless of SW or HW) might not support deducing e.g.
452 * P, Q from N, D, E if the former are not part of the
453 * implementation.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100454 *
455 */
456int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
457 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
458 mbedtls_mpi *D, mbedtls_mpi *E );
459
460/**
461 * \brief Export core parameters of an RSA key
462 * in raw big-endian binary format
463 *
464 * \param ctx Initialized RSA context
465 * \param N Byte array to store the RSA modulus, or NULL
466 * \param N_len Size of buffer for modulus
467 * \param P Byte array to hold the first prime factor of N, or NULL
468 * \param P_len Size of buffer for first prime factor
469 * \param Q Byte array to hold the second prime factor of N, or NULL
470 * \param Q_len Size of buffer for second prime factor
471 * \param D Byte array to hold the private exponent, or NULL
472 * \param D_len Size of buffer for private exponent
473 * \param E Byte array to hold the public exponent, or NULL
474 * \param E_len Size of buffer for public exponent
475 *
476 * \note The length fields are ignored if the corresponding
477 * buffer pointers are NULL.
478 *
Hanno Beckered203612017-09-29 13:34:25 +0100479 * \return
480 * - 0 if successful. In this case, the non-NULL buffers
481 * pointed to by N, P, Q, D, E are fully written, with
482 * additional unused space filled leading by 0-bytes.
483 * - Non-zero return code otherwise. In particular, if
484 * exporting the requested parameters
485 * cannot be done because of a lack of functionality
486 * or because of security policies, the error code
487 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
488 * In this case, the RSA context stays intact and can
489 * be continued to be used.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100490 *
Hanno Beckered203612017-09-29 13:34:25 +0100491 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100492 * would be the following: Firstly, it might be that an
493 * alternative RSA implementation is in use which stores
494 * the key externally, and which either cannot or should not
495 * export it into RAM. Alternatively, an implementation
496 * (regardless of SW or HW) might not support deducing e.g.
497 * P, Q from N, D, E if the former are not part of the
498 * implementation.
499 *
500 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100501 */
502int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
503 unsigned char *N, size_t N_len,
504 unsigned char *P, size_t P_len,
505 unsigned char *Q, size_t Q_len,
506 unsigned char *D, size_t D_len,
507 unsigned char *E, size_t E_len );
508
509/**
510 * \brief Export CRT parameters of a private RSA key
511 *
512 * \param ctx Initialized RSA context
513 * \param DP MPI to hold D modulo P-1, or NULL
514 * \param DQ MPI to hold D modulo Q-1, or NULL
515 * \param QP MPI to hold modular inverse of Q modulo P, or NULL
516 *
517 * \return 0 if successful, non-zero error code otherwise.
518 *
519 * \note Alternative RSA implementations not using CRT-parameters
520 * internally can implement this function using based on
521 * \c mbedtls_rsa_deduce_opt.
522 *
523 */
524int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
525 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
526
Paul Bakker5121ce52009-01-03 21:22:43 +0000527/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100528 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100530 *
531 * \param ctx RSA context to be set
Hanno Becker4b2f6912017-09-29 13:34:55 +0100532 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
533 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100534 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100535void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
536 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100537
538/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100539 * \brief Get length of RSA modulus in bytes
540 *
541 * \param ctx Initialized RSA context
542 *
543 * \return Length of RSA modulus, in bytes.
544 *
545 */
546size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
547
548/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 * \brief Generate an RSA keypair
550 *
551 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000552 * \param f_rng RNG function
553 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000554 * \param nbits size of the public key in bits
555 * \param exponent public exponent (e.g., 65537)
556 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000558 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100560 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100563 int (*f_rng)(void *, unsigned char *, size_t),
564 void *p_rng,
565 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000566
567/**
Hanno Becker43a08d02017-10-02 13:16:35 +0100568 * \brief Check if a context contains (at least) an RSA public key
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 *
570 * \param ctx RSA context to be checked
571 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100572 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
573 * On success, it is guaranteed that enough information is
574 * present to perform an RSA public key operation
575 * \c mbedtls_rsa_public.
576 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
580/**
Hanno Becker1e801f52017-10-10 16:44:47 +0100581 * \brief Check if a context contains an RSA private key
582 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000583 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100584 * \param ctx RSA context to be checked
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100586 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
Hanno Becker43a08d02017-10-02 13:16:35 +0100587 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100588 * \note This function performs checks substantiating
589 * the consistency of the key material used to setup
590 * the RSA context. In case of implementations saving
591 * all core RSA parameters, this might mean a consistency
592 * check in the sense of \c mbedtls_rsa_validate_params,
593 * while other implementations might perform an empirical
594 * check consisting of an encryption-decryption pair.
595 *
596 * \warning This function should catch accidental misconfigurations
597 * like swapping of parameters, but it cannot establish full
598 * trust in neither the quality nor the consistency of the key
599 * material that was used to setup the given RSA context:
600 * - Regarding consistency, note (see \c mbedtls_rsa_complete)
601 * that imported parameters irrelevant for the implementation
602 * might be silently dropped, in which case the present
603 * function doesn't have access to and hence cannot check them.
604 * If the user desires to check the consistency of the entire
605 * content of, say, an PKCS1-encoded RSA private key, he
606 * should use \c mbedtls_rsa_validate_params before setting
607 * up the RSA context.
608 * Further, if the implementation performs empirical checks,
609 * these checks will substantiate but not guarantee consistency.
610 * - Regarding quality, this function is not expected to perform
611 * extended quality assessments like checking that the prime
612 * factors are safe. Further, it is the user's responsibility to
613 * ensure trustworthiness of the source of his RSA parameters,
614 * a question going beyond what's effectively checkable
615 * by the library.
Hanno Becker43a08d02017-10-02 13:16:35 +0100616 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
620/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100621 * \brief Check a public-private RSA key pair.
622 * Check each of the contexts, and make sure they match.
623 *
624 * \param pub RSA context holding the public key
625 * \param prv RSA context holding the private key
626 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100627 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100628 */
Hanno Becker98838b02017-10-02 13:16:10 +0100629int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
630 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100631
632/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000633 * \brief Do an RSA public key operation
634 *
635 * \param ctx RSA context
636 * \param input input buffer
637 * \param output output buffer
638 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100639 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000640 *
641 * \note This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800642 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000643 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 *
645 * \note The input and output buffers must be large
646 * enough (eg. 128 bytes if RSA-1024 is used).
647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000649 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000650 unsigned char *output );
651
652/**
653 * \brief Do an RSA private key operation
654 *
655 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200656 * \param f_rng RNG function (Needed for blinding)
657 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000658 * \param input input buffer
659 * \param output output buffer
660 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100661 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000662 *
663 * \note The input and output buffers must be large
664 * enough (eg. 128 bytes if RSA-1024 is used).
665 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200667 int (*f_rng)(void *, unsigned char *, size_t),
668 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000669 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 unsigned char *output );
671
672/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100673 * \brief Generic wrapper to perform a PKCS#1 encryption using the
674 * mode from the context. Add the message padding, then do an
675 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 *
677 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200678 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100679 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000680 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100681 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000682 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 * \param input buffer holding the data to be encrypted
684 * \param output buffer that will hold the ciphertext
685 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100686 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000687 *
688 * \note The output buffer must be as large as the size
689 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
690 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000692 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000693 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000694 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000695 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 unsigned char *output );
697
698/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100699 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
700 *
701 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100702 * \param f_rng RNG function (Needed for padding and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100703 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100704 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100705 * \param ilen contains the plaintext length
706 * \param input buffer holding the data to be encrypted
707 * \param output buffer that will hold the ciphertext
708 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100709 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100710 *
711 * \note The output buffer must be as large as the size
712 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100715 int (*f_rng)(void *, unsigned char *, size_t),
716 void *p_rng,
717 int mode, size_t ilen,
718 const unsigned char *input,
719 unsigned char *output );
720
721/**
722 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
723 *
724 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200725 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100726 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100727 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100728 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100729 * \param label buffer holding the custom label to use
730 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100731 * \param ilen contains the plaintext length
732 * \param input buffer holding the data to be encrypted
733 * \param output buffer that will hold the ciphertext
734 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100735 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100736 *
737 * \note The output buffer must be as large as the size
738 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100741 int (*f_rng)(void *, unsigned char *, size_t),
742 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100743 int mode,
744 const unsigned char *label, size_t label_len,
745 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100746 const unsigned char *input,
747 unsigned char *output );
748
749/**
750 * \brief Generic wrapper to perform a PKCS#1 decryption using the
751 * mode from the context. Do an RSA operation, then remove
752 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000753 *
754 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100755 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200756 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100757 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000758 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000759 * \param input buffer holding the encrypted data
760 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000761 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000762 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100763 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000764 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100765 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100766 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100767 * if RSA-1024 is used) to be able to hold an arbitrary
768 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100769 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100770 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100771 *
772 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100773 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000774 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200776 int (*f_rng)(void *, unsigned char *, size_t),
777 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000778 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000779 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000780 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000781 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000782
783/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100784 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
785 *
786 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100787 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200788 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100789 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
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 Beckercbb59bc2017-08-23 14:11:08 +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 *
804 * \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_pkcs1_v15_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 Bakkerb3869132013-02-28 17:21:01 +0100810 int mode, size_t *olen,
811 const unsigned char *input,
812 unsigned char *output,
813 size_t output_max_len );
814
815/**
816 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
817 *
818 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100819 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200820 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100821 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100822 * \param label buffer holding the custom label to use
823 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100824 * \param olen will contain the plaintext length
825 * \param input buffer holding the encrypted data
826 * \param output buffer that will hold the plaintext
827 * \param output_max_len maximum length of the output buffer
828 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100829 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100830 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100831 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100832 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100833 * if RSA-1024 is used) to be able to hold an arbitrary
834 * decrypted message. If it is not large enough to hold
Hanno Becker8fd55482017-08-23 14:07:48 +0100835 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100836 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100837 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100838 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100839 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100840 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200842 int (*f_rng)(void *, unsigned char *, size_t),
843 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100844 int mode,
845 const unsigned char *label, size_t label_len,
846 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100847 const unsigned char *input,
848 unsigned char *output,
849 size_t output_max_len );
850
851/**
852 * \brief Generic wrapper to perform a PKCS#1 signature using the
853 * mode from the context. Do a private RSA operation to sign
854 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000855 *
856 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200857 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100858 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000859 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100860 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
861 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for
862 * signing raw data)
863 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000864 * \param hash buffer holding the message digest
865 * \param sig buffer that will hold the ciphertext
866 *
867 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100868 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100870 * \note The \c sig buffer must be as large as the size
871 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000872 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200873 * \note In case of PKCS#1 v2.1 encoding, see comments on
Hanno Becker4b2f6912017-09-29 13:34:55 +0100874 * \note \c mbedtls_rsa_rsassa_pss_sign() for details on
875 * \c md_alg and \c hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000876 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000878 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000879 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000880 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000882 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000883 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000884 unsigned char *sig );
885
886/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100887 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
888 *
889 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100890 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200891 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100892 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
893 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
894 * for signing raw data)
895 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100896 * \param hash buffer holding the message digest
897 * \param sig buffer that will hold the ciphertext
898 *
899 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100900 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100901 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100902 * \note The \c sig buffer must be as large as the size
903 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100904 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200906 int (*f_rng)(void *, unsigned char *, size_t),
907 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100908 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 Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
916 *
917 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200918 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100919 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100920 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100921 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
922 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
923 * for signing raw data)
924 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100925 * \param hash buffer holding the message digest
926 * \param sig buffer that will hold the ciphertext
927 *
928 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100929 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100930 *
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 Bakkerb3869132013-02-28 17:21:01 +0100933 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100934 * \note The \c hash_id in the RSA context is the one used for the
935 * encoding. \c md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100936 * that is encoded. According to RFC 3447 it is advised to
937 * keep both hashes the same.
938 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100940 int (*f_rng)(void *, unsigned char *, size_t),
941 void *p_rng,
942 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100944 unsigned int hashlen,
945 const unsigned char *hash,
946 unsigned char *sig );
947
948/**
949 * \brief Generic wrapper to perform a PKCS#1 verification using the
950 * mode from the context. Do a public RSA operation and check
951 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000952 *
953 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100954 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200955 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100956 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
957 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
958 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000959 * \param hash buffer holding the message digest
960 * \param sig buffer holding the ciphertext
961 *
962 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100963 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000964 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100965 * \note The \c sig buffer must be as large as the size
966 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000967 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200968 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000970 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200972 int (*f_rng)(void *, unsigned char *, size_t),
973 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000974 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000976 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000977 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200978 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000979
980/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100981 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
982 *
983 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100984 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200985 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100986 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
987 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
988 * for signing raw data)
989 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100990 * \param hash buffer holding the message digest
991 * \param sig buffer holding the ciphertext
992 *
993 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100994 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100995 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100996 * \note The \c sig buffer must be as large as the size
997 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100998 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999int mbedtls_rsa_rsassa_pkcs1_v15_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/**
1009 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001010 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +01001011 *
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)
Paul Bakker548957d2013-08-30 10:30:02 +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)
Paul Bakkerb3869132013-02-28 17:21:01 +01001018 * \param hash buffer holding the message digest
1019 * \param sig buffer holding the ciphertext
1020 *
1021 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001022 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +01001023 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001024 * \note The \c sig buffer must be as large as the size
1025 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +01001026 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001027 * \note The \c hash_id in the RSA context is the one used for the
1028 * verification. \c md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001029 * hash that is verified. According to RFC 3447 it is advised to
Hanno Becker4b2f6912017-09-29 13:34:55 +01001030 * keep both hashes the same. If \c hash_id in the RSA context is
1031 * unset, the \c md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001032 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001034 int (*f_rng)(void *, unsigned char *, size_t),
1035 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001036 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001038 unsigned int hashlen,
1039 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001040 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001041
1042/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001043 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
1044 * (This is the version with "full" options.)
1045 *
1046 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +01001047 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001048 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +01001049 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
1050 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
1051 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001052 * \param hash buffer holding the message digest
1053 * \param mgf1_hash_id message digest used for mask generation
1054 * \param expected_salt_len Length of the salt used in padding, use
Hanno Becker4b2f6912017-09-29 13:34:55 +01001055 * \c MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001056 * \param sig buffer holding the ciphertext
1057 *
1058 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001059 * or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001060 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001061 * \note The \c sig buffer must be as large as the size
1062 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001063 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001064 * \note The \c hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001065 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001067 int (*f_rng)(void *, unsigned char *, size_t),
1068 void *p_rng,
1069 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001071 unsigned int hashlen,
1072 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001074 int expected_salt_len,
1075 const unsigned char *sig );
1076
1077/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001078 * \brief Copy the components of an RSA context
1079 *
1080 * \param dst Destination context
1081 * \param src Source context
1082 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001083 * \return 0 on success,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001084 * \c MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001085 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001087
1088/**
Paul Bakker5121ce52009-01-03 21:22:43 +00001089 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001090 *
1091 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +00001092 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001094
1095/**
1096 * \brief Checkup routine
1097 *
1098 * \return 0 if successful, or 1 if the test failed
1099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001101
1102#ifdef __cplusplus
1103}
1104#endif
1105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106#endif /* MBEDTLS_RSA_C */
Paul Bakkered27a042013-04-18 22:46:23 +02001107
Paul Bakker5121ce52009-01-03 21:22:43 +00001108#endif /* rsa.h */