blob: c85e6c81d8771563b672ba66e6e0aa59a6ea58e6 [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 Beckercbb59bc2017-08-23 14:11:08 +0100385 *
Hanno Beckered203612017-09-29 13:34:25 +0100386 * \note
387 * - To setup an RSA public key, precisely N and E
388 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100389 *
Hanno Beckered203612017-09-29 13:34:25 +0100390 * - To setup an RSA private key, enough information must be
Hanno Becker98838b02017-10-02 13:16:10 +0100391 * present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100392 *
Hanno Beckered203612017-09-29 13:34:25 +0100393 * The default implementation supports the following:
394 * - Derive P, Q from N, D, E
395 * - Derive N, D from P, Q, E.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100396 *
Hanno Beckered203612017-09-29 13:34:25 +0100397 * - Alternative implementations need not support these
398 * and may return \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA instead.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100399 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100400 * \return
401 * - 0 if successful. In this case, it is guaranteed
Hanno Becker1e801f52017-10-10 16:44:47 +0100402 * that the RSA context can be used for RSA operations
403 * without the risk of failure or crash.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100404 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100405 * derivations failed.
Hanno Becker43a08d02017-10-02 13:16:35 +0100406 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100407 * \warning This function need not perform consistency checks
408 * for the imported parameters! In particular, parameters that
409 * are not needed by the implementation may be silently discarded
410 * and left unchecked. For the purpose of checking the consistency
411 * of the key material, see \c mbedtls_rsa_check_privkey.
Hanno Becker43a08d02017-10-02 13:16:35 +0100412 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100413 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100414int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100415
416/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100417 * \brief Export core parameters of an RSA key
418 *
419 * \param ctx Initialized RSA context
420 * \param N MPI to hold the RSA modulus, or NULL
421 * \param P MPI to hold the first prime factor of N, or NULL
422 * \param Q MPI to hold the second prime factor of N, or NULL
423 * \param D MPI to hold the private exponent, or NULL
424 * \param E MPI to hold the public exponent, or NULL
425 *
Hanno Beckered203612017-09-29 13:34:25 +0100426 * \return
427 * - 0 if successful. In this case, the non-NULL buffers
428 * pointed to by N, P, Q, D, E are fully written, with
429 * additional unused space filled leading by 0-bytes.
430 * - Non-zero return code otherwise. In particular, if
431 * exporting the requested parameters
432 * cannot be done because of a lack of functionality
433 * or because of security policies, the error code
434 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
435 * In this case, the RSA context stays intact and can
436 * be continued to be used.
Hanno Becker91c194d2017-09-29 12:50:12 +0100437 *
Hanno Beckered203612017-09-29 13:34:25 +0100438 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100439 * would be the following: Firstly, it might be that an
440 * alternative RSA implementation is in use which stores
441 * the key externally, and which either cannot or should not
442 * export it into RAM. Alternatively, an implementation
443 * (regardless of SW or HW) might not support deducing e.g.
444 * P, Q from N, D, E if the former are not part of the
445 * implementation.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100446 *
447 */
448int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
449 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
450 mbedtls_mpi *D, mbedtls_mpi *E );
451
452/**
453 * \brief Export core parameters of an RSA key
454 * in raw big-endian binary format
455 *
456 * \param ctx Initialized RSA context
457 * \param N Byte array to store the RSA modulus, or NULL
458 * \param N_len Size of buffer for modulus
459 * \param P Byte array to hold the first prime factor of N, or NULL
460 * \param P_len Size of buffer for first prime factor
461 * \param Q Byte array to hold the second prime factor of N, or NULL
462 * \param Q_len Size of buffer for second prime factor
463 * \param D Byte array to hold the private exponent, or NULL
464 * \param D_len Size of buffer for private exponent
465 * \param E Byte array to hold the public exponent, or NULL
466 * \param E_len Size of buffer for public exponent
467 *
468 * \note The length fields are ignored if the corresponding
469 * buffer pointers are NULL.
470 *
Hanno Beckered203612017-09-29 13:34:25 +0100471 * \return
472 * - 0 if successful. In this case, the non-NULL buffers
473 * pointed to by N, P, Q, D, E are fully written, with
474 * additional unused space filled leading by 0-bytes.
475 * - Non-zero return code otherwise. In particular, if
476 * exporting the requested parameters
477 * cannot be done because of a lack of functionality
478 * or because of security policies, the error code
479 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
480 * In this case, the RSA context stays intact and can
481 * be continued to be used.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100482 *
Hanno Beckered203612017-09-29 13:34:25 +0100483 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100484 * would be the following: Firstly, it might be that an
485 * alternative RSA implementation is in use which stores
486 * the key externally, and which either cannot or should not
487 * export it into RAM. Alternatively, an implementation
488 * (regardless of SW or HW) might not support deducing e.g.
489 * P, Q from N, D, E if the former are not part of the
490 * implementation.
491 *
492 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100493 */
494int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
495 unsigned char *N, size_t N_len,
496 unsigned char *P, size_t P_len,
497 unsigned char *Q, size_t Q_len,
498 unsigned char *D, size_t D_len,
499 unsigned char *E, size_t E_len );
500
501/**
502 * \brief Export CRT parameters of a private RSA key
503 *
504 * \param ctx Initialized RSA context
505 * \param DP MPI to hold D modulo P-1, or NULL
506 * \param DQ MPI to hold D modulo Q-1, or NULL
507 * \param QP MPI to hold modular inverse of Q modulo P, or NULL
508 *
509 * \return 0 if successful, non-zero error code otherwise.
510 *
511 * \note Alternative RSA implementations not using CRT-parameters
512 * internally can implement this function using based on
513 * \c mbedtls_rsa_deduce_opt.
514 *
515 */
516int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
517 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
518
Paul Bakker5121ce52009-01-03 21:22:43 +0000519/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100520 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100522 *
523 * \param ctx RSA context to be set
Hanno Becker4b2f6912017-09-29 13:34:55 +0100524 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
525 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100526 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100527void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
528 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100529
530/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100531 * \brief Get length of RSA modulus in bytes
532 *
533 * \param ctx Initialized RSA context
534 *
535 * \return Length of RSA modulus, in bytes.
536 *
537 */
538size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
539
540/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000541 * \brief Generate an RSA keypair
542 *
543 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000544 * \param f_rng RNG function
545 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 * \param nbits size of the public key in bits
547 * \param exponent public exponent (e.g., 65537)
548 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000550 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100552 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100555 int (*f_rng)(void *, unsigned char *, size_t),
556 void *p_rng,
557 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
559/**
Hanno Becker43a08d02017-10-02 13:16:35 +0100560 * \brief Check if a context contains (at least) an RSA public key
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 *
562 * \param ctx RSA context to be checked
563 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100564 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
565 * On success, it is guaranteed that enough information is
566 * present to perform an RSA public key operation
567 * \c mbedtls_rsa_public.
568 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
572/**
Hanno Becker1e801f52017-10-10 16:44:47 +0100573 * \brief Check if a context contains an RSA private key
574 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100576 * \param ctx RSA context to be checked
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100578 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
Hanno Becker43a08d02017-10-02 13:16:35 +0100579 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100580 * \note This function performs checks substantiating
581 * the consistency of the key material used to setup
582 * the RSA context. In case of implementations saving
583 * all core RSA parameters, this might mean a consistency
584 * check in the sense of \c mbedtls_rsa_validate_params,
585 * while other implementations might perform an empirical
586 * check consisting of an encryption-decryption pair.
587 *
588 * \warning This function should catch accidental misconfigurations
589 * like swapping of parameters, but it cannot establish full
590 * trust in neither the quality nor the consistency of the key
591 * material that was used to setup the given RSA context:
592 * - Regarding consistency, note (see \c mbedtls_rsa_complete)
593 * that imported parameters irrelevant for the implementation
594 * might be silently dropped, in which case the present
595 * function doesn't have access to and hence cannot check them.
596 * If the user desires to check the consistency of the entire
597 * content of, say, an PKCS1-encoded RSA private key, he
598 * should use \c mbedtls_rsa_validate_params before setting
599 * up the RSA context.
600 * Further, if the implementation performs empirical checks,
601 * these checks will substantiate but not guarantee consistency.
602 * - Regarding quality, this function is not expected to perform
603 * extended quality assessments like checking that the prime
604 * factors are safe. Further, it is the user's responsibility to
605 * ensure trustworthiness of the source of his RSA parameters,
606 * a question going beyond what's effectively checkable
607 * by the library.
Hanno Becker43a08d02017-10-02 13:16:35 +0100608 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000609 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000611
612/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100613 * \brief Check a public-private RSA key pair.
614 * Check each of the contexts, and make sure they match.
615 *
616 * \param pub RSA context holding the public key
617 * \param prv RSA context holding the private key
618 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100619 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100620 */
Hanno Becker98838b02017-10-02 13:16:10 +0100621int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
622 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100623
624/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000625 * \brief Do an RSA public key operation
626 *
627 * \param ctx RSA context
628 * \param input input buffer
629 * \param output output buffer
630 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100631 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000632 *
633 * \note This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800634 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000635 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000636 *
637 * \note The input and output buffers must be large
638 * enough (eg. 128 bytes if RSA-1024 is used).
639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000641 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 unsigned char *output );
643
644/**
645 * \brief Do an RSA private key operation
646 *
647 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200648 * \param f_rng RNG function (Needed for blinding)
649 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000650 * \param input input buffer
651 * \param output output buffer
652 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100653 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 *
655 * \note The input and output buffers must be large
656 * enough (eg. 128 bytes if RSA-1024 is used).
657 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200659 int (*f_rng)(void *, unsigned char *, size_t),
660 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000661 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000662 unsigned char *output );
663
664/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100665 * \brief Generic wrapper to perform a PKCS#1 encryption using the
666 * mode from the context. Add the message padding, then do an
667 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 *
669 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200670 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100671 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000672 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100673 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000674 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000675 * \param input buffer holding the data to be encrypted
676 * \param output buffer that will hold the ciphertext
677 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100678 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000679 *
680 * \note The output buffer must be as large as the size
681 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000684 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000685 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000686 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000687 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000688 unsigned char *output );
689
690/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100691 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
692 *
693 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100694 * \param f_rng RNG function (Needed for padding and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100695 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100696 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100697 * \param ilen contains the plaintext length
698 * \param input buffer holding the data to be encrypted
699 * \param output buffer that will hold the ciphertext
700 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100701 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100702 *
703 * \note The output buffer must be as large as the size
704 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100707 int (*f_rng)(void *, unsigned char *, size_t),
708 void *p_rng,
709 int mode, size_t ilen,
710 const unsigned char *input,
711 unsigned char *output );
712
713/**
714 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
715 *
716 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200717 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100718 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100719 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100720 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100721 * \param label buffer holding the custom label to use
722 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100723 * \param ilen contains the plaintext length
724 * \param input buffer holding the data to be encrypted
725 * \param output buffer that will hold the ciphertext
726 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100727 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100728 *
729 * \note The output buffer must be as large as the size
730 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100733 int (*f_rng)(void *, unsigned char *, size_t),
734 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100735 int mode,
736 const unsigned char *label, size_t label_len,
737 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100738 const unsigned char *input,
739 unsigned char *output );
740
741/**
742 * \brief Generic wrapper to perform a PKCS#1 decryption using the
743 * mode from the context. Do an RSA operation, then remove
744 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000745 *
746 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100747 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200748 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100749 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000750 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000751 * \param input buffer holding the encrypted data
752 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000753 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000754 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100755 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000756 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100757 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100758 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100759 * if RSA-1024 is used) to be able to hold an arbitrary
760 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100761 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100762 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100763 *
764 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100765 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000766 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200768 int (*f_rng)(void *, unsigned char *, size_t),
769 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000770 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000771 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000772 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000773 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000774
775/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100776 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
777 *
778 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100779 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200780 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100781 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100782 * \param olen will contain the plaintext length
783 * \param input buffer holding the encrypted data
784 * \param output buffer that will hold the plaintext
785 * \param output_max_len maximum length of the output buffer
786 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100787 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100788 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100789 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100790 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100791 * if RSA-1024 is used) to be able to hold an arbitrary
792 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100793 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100794 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100795 *
796 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100797 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100798 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200800 int (*f_rng)(void *, unsigned char *, size_t),
801 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100802 int mode, size_t *olen,
803 const unsigned char *input,
804 unsigned char *output,
805 size_t output_max_len );
806
807/**
808 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
809 *
810 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100811 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200812 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100813 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100814 * \param label buffer holding the custom label to use
815 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100816 * \param olen will contain the plaintext length
817 * \param input buffer holding the encrypted data
818 * \param output buffer that will hold the plaintext
819 * \param output_max_len maximum length of the output buffer
820 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100821 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100822 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100823 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100824 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100825 * if RSA-1024 is used) to be able to hold an arbitrary
826 * decrypted message. If it is not large enough to hold
Hanno Becker8fd55482017-08-23 14:07:48 +0100827 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100828 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100829 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100830 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100831 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100832 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200834 int (*f_rng)(void *, unsigned char *, size_t),
835 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100836 int mode,
837 const unsigned char *label, size_t label_len,
838 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100839 const unsigned char *input,
840 unsigned char *output,
841 size_t output_max_len );
842
843/**
844 * \brief Generic wrapper to perform a PKCS#1 signature using the
845 * mode from the context. Do a private RSA operation to sign
846 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000847 *
848 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200849 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100850 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000851 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100852 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
853 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for
854 * signing raw data)
855 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000856 * \param hash buffer holding the message digest
857 * \param sig buffer that will hold the ciphertext
858 *
859 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100860 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100862 * \note The \c sig buffer must be as large as the size
863 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000864 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200865 * \note In case of PKCS#1 v2.1 encoding, see comments on
Hanno Becker4b2f6912017-09-29 13:34:55 +0100866 * \note \c mbedtls_rsa_rsassa_pss_sign() for details on
867 * \c md_alg and \c hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000868 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000870 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000871 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000872 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000874 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000875 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000876 unsigned char *sig );
877
878/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100879 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
880 *
881 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100882 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200883 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100884 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
885 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
886 * for signing raw data)
887 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100888 * \param hash buffer holding the message digest
889 * \param sig buffer that will hold the ciphertext
890 *
891 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100892 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100893 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100894 * \note The \c sig buffer must be as large as the size
895 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200898 int (*f_rng)(void *, unsigned char *, size_t),
899 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100900 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100902 unsigned int hashlen,
903 const unsigned char *hash,
904 unsigned char *sig );
905
906/**
907 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
908 *
909 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200910 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100911 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100912 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100913 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
914 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
915 * for signing raw data)
916 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100917 * \param hash buffer holding the message digest
918 * \param sig buffer that will hold the ciphertext
919 *
920 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100921 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100922 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100923 * \note The \c sig buffer must be as large as the size
924 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100925 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100926 * \note The \c hash_id in the RSA context is the one used for the
927 * encoding. \c md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100928 * that is encoded. According to RFC 3447 it is advised to
929 * keep both hashes the same.
930 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100932 int (*f_rng)(void *, unsigned char *, size_t),
933 void *p_rng,
934 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100936 unsigned int hashlen,
937 const unsigned char *hash,
938 unsigned char *sig );
939
940/**
941 * \brief Generic wrapper to perform a PKCS#1 verification using the
942 * mode from the context. Do a public RSA operation and check
943 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000944 *
945 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100946 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200947 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100948 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
949 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
950 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000951 * \param hash buffer holding the message digest
952 * \param sig buffer holding the ciphertext
953 *
954 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100955 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000956 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100957 * \note The \c sig buffer must be as large as the size
958 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000959 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200960 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200964 int (*f_rng)(void *, unsigned char *, size_t),
965 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000966 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000968 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000969 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200970 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000971
972/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100973 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
974 *
975 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100976 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200977 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100978 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
979 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
980 * for signing raw data)
981 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100982 * \param hash buffer holding the message digest
983 * \param sig buffer holding the ciphertext
984 *
985 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100986 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100987 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100988 * \note The \c sig buffer must be as large as the size
989 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100990 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200992 int (*f_rng)(void *, unsigned char *, size_t),
993 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100994 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100996 unsigned int hashlen,
997 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200998 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100999
1000/**
1001 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001002 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +01001003 *
1004 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +01001005 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +02001006 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +01001007 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
1008 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
1009 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +01001010 * \param hash buffer holding the message digest
1011 * \param sig buffer holding the ciphertext
1012 *
1013 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001014 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +01001015 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001016 * \note The \c sig buffer must be as large as the size
1017 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +01001018 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001019 * \note The \c hash_id in the RSA context is the one used for the
1020 * verification. \c md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001021 * hash that is verified. According to RFC 3447 it is advised to
Hanno Becker4b2f6912017-09-29 13:34:55 +01001022 * keep both hashes the same. If \c hash_id in the RSA context is
1023 * unset, the \c md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001026 int (*f_rng)(void *, unsigned char *, size_t),
1027 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001028 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001030 unsigned int hashlen,
1031 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001032 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001033
1034/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001035 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
1036 * (This is the version with "full" options.)
1037 *
1038 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +01001039 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001040 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +01001041 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
1042 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
1043 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001044 * \param hash buffer holding the message digest
1045 * \param mgf1_hash_id message digest used for mask generation
1046 * \param expected_salt_len Length of the salt used in padding, use
Hanno Becker4b2f6912017-09-29 13:34:55 +01001047 * \c MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001048 * \param sig buffer holding the ciphertext
1049 *
1050 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001051 * or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001052 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001053 * \note The \c sig buffer must be as large as the size
1054 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001055 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001056 * \note The \c hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001059 int (*f_rng)(void *, unsigned char *, size_t),
1060 void *p_rng,
1061 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001063 unsigned int hashlen,
1064 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001066 int expected_salt_len,
1067 const unsigned char *sig );
1068
1069/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001070 * \brief Copy the components of an RSA context
1071 *
1072 * \param dst Destination context
1073 * \param src Source context
1074 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001075 * \return 0 on success,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001076 * \c MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001079
1080/**
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001082 *
1083 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +00001084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001086
1087/**
1088 * \brief Checkup routine
1089 *
1090 * \return 0 if successful, or 1 if the test failed
1091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001093
1094#ifdef __cplusplus
1095}
1096#endif
1097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098#endif /* MBEDTLS_RSA_C */
Paul Bakkered27a042013-04-18 22:46:23 +02001099
Paul Bakker5121ce52009-01-03 21:22:43 +00001100#endif /* rsa.h */