blob: 46daac55f314e70a357542b9680a91dc4a9517ac [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief The RSA public-key cryptosystem
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_RSA_H
24#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020027#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakkered27a042013-04-18 22:46:23 +020031
Paul Bakker314052f2011-08-15 09:07:52 +000032#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020033#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_THREADING_C)
Paul Bakkerc9965dc2013-09-29 14:58:17 +020036#include "threading.h"
37#endif
38
Paul Bakker13e2dfe2009-07-28 07:18:38 +000039/*
40 * RSA Error codes
41 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
43#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
44#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +020045#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the library's validity check. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
47#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
48#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
49#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
50#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Hanno Becker91c194d2017-09-29 12:50:12 +010051#define MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED -0x4500 /**< The requested parameter export is not possible/allowed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000052
53/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020054 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define MBEDTLS_RSA_PUBLIC 0
57#define MBEDTLS_RSA_PRIVATE 1
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define MBEDTLS_RSA_PKCS_V15 0
60#define MBEDTLS_RSA_PKCS_V21 1
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define MBEDTLS_RSA_SIGN 1
63#define MBEDTLS_RSA_CRYPT 2
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020066
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020067/*
68 * The above constants may be used even if the RSA module is compile out,
69 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
70 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020072
Paul Bakker407a0da2013-06-27 14:29:21 +020073#ifdef __cplusplus
74extern "C" {
75#endif
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/**
Hanno Beckera3ebec22017-08-23 14:06:24 +010078 * Helper functions for RSA-related operations on MPI's.
79 */
80
81/**
82 * \brief Compute RSA prime moduli P, Q from public modulus N=PQ
Hanno Beckerb0c5edc2017-08-23 22:16:10 +010083 * and a pair of private and public key.
Hanno Beckera3ebec22017-08-23 14:06:24 +010084 *
85 * \note This is a 'static' helper function not operating on
86 * an RSA context. Alternative implementations need not
87 * overwrite it.
88 *
89 * \param N RSA modulus N = PQ, with P, Q to be found
90 * \param D RSA private exponent
91 * \param E RSA public exponent
92 * \param f_rng PRNG to be used for randomization, or NULL
93 * \param p_rng PRNG context for f_rng, or NULL
94 * \param P Pointer to MPI holding first prime factor of N on success
95 * \param Q Pointer to MPI holding second prime factor of N on success
96 *
Hanno Beckered203612017-09-29 13:34:25 +010097 * \return
98 * - 0 if successful. In this case, P and Q constitute a
Hanno Beckerba5b7552017-10-02 09:55:49 +010099 * factorization of N.
100 * - A non-zero error code otherwise.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100101 *
Hanno Becker1b831fe2017-10-02 12:24:50 +0100102 * \note It is neither checked that P, Q are prime nor that
103 * D, E are modular inverses wrt. P-1 and Q-1. For that,
104 * use the helper function \c mbedtls_rsa_validate_params.
105 *
Hanno Beckera3ebec22017-08-23 14:06:24 +0100106 */
Hanno Beckerba5b7552017-10-02 09:55:49 +0100107int mbedtls_rsa_deduce_moduli( mbedtls_mpi const *N, mbedtls_mpi const *D,
108 mbedtls_mpi const *E, int (*f_rng)(void *, unsigned char *, size_t),
109 void *p_rng, mbedtls_mpi *P, mbedtls_mpi *Q );
Hanno Beckera3ebec22017-08-23 14:06:24 +0100110
111/**
112 * \brief Compute RSA private exponent from
113 * prime moduli and public key.
114 *
115 * \note This is a 'static' helper function not operating on
116 * an RSA context. Alternative implementations need not
117 * overwrite it.
118 *
119 * \param P First prime factor of RSA modulus
120 * \param Q Second prime factor of RSA modulus
121 * \param E RSA public exponent
122 * \param D Pointer to MPI holding the private exponent on success.
123 *
Hanno Beckered203612017-09-29 13:34:25 +0100124 * \return
125 * - 0 if successful. In this case, D is set to a simultaneous
Hanno Beckera3ebec22017-08-23 14:06:24 +0100126 * modular inverse of E modulo both P-1 and Q-1.
Hanno Beckerbdefff12017-10-02 09:57:50 +0100127 * - A non-zero error code otherwise.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100128 *
Hanno Becker1b831fe2017-10-02 12:24:50 +0100129 * \note This function does not check whether P and Q are primes.
130 *
Hanno Beckera3ebec22017-08-23 14:06:24 +0100131 */
Hanno Beckerbdefff12017-10-02 09:57:50 +0100132int mbedtls_rsa_deduce_private( mbedtls_mpi const *P, mbedtls_mpi const *Q,
133 mbedtls_mpi const *E, 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 *
335 * \return 0 if successful, non-zero error code on failure.
336 */
337int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
338 const mbedtls_mpi *N,
339 const mbedtls_mpi *P, const mbedtls_mpi *Q,
340 const mbedtls_mpi *D, const mbedtls_mpi *E );
341
342/**
343 * \brief Import core RSA parameters in raw big-endian
344 * binary format into an RSA context
345 *
346 * \param ctx Initialized RSA context to store parameters
347 * \param N RSA modulus, or NULL
348 * \param N_len Byte length of N, ignored if N == NULL
349 * \param P First prime factor of N, or NULL
350 * \param P_len Byte length of P, ignored if P == NULL
351 * \param Q Second prime factor of N, or NULL
352 * \param Q_len Byte length of Q, ignored if Q == NULL
353 * \param D Private exponent, or NULL
354 * \param D_len Byte length of D, ignored if D == NULL
355 * \param E Public exponent, or NULL
356 * \param E_len Byte length of E, ignored if E == NULL
357 *
358 * \note This function can be called multiple times for successive
359 * imports if the parameters are not simultaneously present.
360 * Any sequence of calls to this function should be followed
361 * by a call to \c mbedtls_rsa_complete which will check
362 * and complete the provided information to a ready-for-use
363 * public or private RSA key.
364 *
365 * \return 0 if successful, non-zero error code on failure.
366 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100367int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100368 unsigned char const *N, size_t N_len,
369 unsigned char const *P, size_t P_len,
370 unsigned char const *Q, size_t Q_len,
371 unsigned char const *D, size_t D_len,
372 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100373
374/**
375 * \brief Attempt to complete an RSA context from
376 * a set of imported core parameters.
377 *
378 * \param ctx Initialized RSA context to store parameters
Hanno Becker43a08d02017-10-02 13:16:35 +0100379 * \param f_rng RNG function, or NULL
380 * \param p_rng RNG parameter, or NULL
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100381 *
Hanno Beckered203612017-09-29 13:34:25 +0100382 * \note
383 * - To setup an RSA public key, precisely N and E
384 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100385 *
Hanno Beckered203612017-09-29 13:34:25 +0100386 * - To setup an RSA private key, enough information must be
Hanno Becker98838b02017-10-02 13:16:10 +0100387 * present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100388 *
Hanno Beckered203612017-09-29 13:34:25 +0100389 * The default implementation supports the following:
390 * - Derive P, Q from N, D, E
391 * - Derive N, D from P, Q, E.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100392 *
Hanno Beckered203612017-09-29 13:34:25 +0100393 * - Alternative implementations need not support these
394 * and may return \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA instead.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100395 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100396 * \note The PRNG is used for the probabilistic algorithm
397 * used in the derivation of P, Q from N, D, E. If it
398 * not present, a deterministic heuristic is used.
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
402 * the functions \c mbedtls_rsa_check_pubkey resp.
403 * \c mbedtls_rsa_check_privkey pass in case of a
404 * public resp. private key.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100405 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100406 * derivations failed.
Hanno Becker43a08d02017-10-02 13:16:35 +0100407 *
408 * \warning Implementations are *not* obliged to perform exhaustive
409 * validation of the imported parameters!
410 * In particular, parameters that are not needed by the
411 * implementation may be silently discarded and left unchecked.
412 * If the user mistrusts the given key material, he should
413 * employ other means for verification like the helper functions
414 * \c mbedtls_rsa_validate_params, \c mbedtls_rsa_validate_crt.
415 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100416 */
417int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
418 int (*f_rng)(void *, unsigned char *, size_t),
419 void *p_rng );
420
421/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100422 * \brief Export core parameters of an RSA key
423 *
424 * \param ctx Initialized RSA context
425 * \param N MPI to hold the RSA modulus, or NULL
426 * \param P MPI to hold the first prime factor of N, or NULL
427 * \param Q MPI to hold the second prime factor of N, or NULL
428 * \param D MPI to hold the private exponent, or NULL
429 * \param E MPI to hold the public exponent, or NULL
430 *
Hanno Beckered203612017-09-29 13:34:25 +0100431 * \return
432 * - 0 if successful. In this case, the non-NULL buffers
433 * pointed to by N, P, Q, D, E are fully written, with
434 * additional unused space filled leading by 0-bytes.
435 * - Non-zero return code otherwise. In particular, if
436 * exporting the requested parameters
437 * cannot be done because of a lack of functionality
438 * or because of security policies, the error code
439 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
440 * In this case, the RSA context stays intact and can
441 * be continued to be used.
Hanno Becker91c194d2017-09-29 12:50:12 +0100442 *
Hanno Beckered203612017-09-29 13:34:25 +0100443 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100444 * would be the following: Firstly, it might be that an
445 * alternative RSA implementation is in use which stores
446 * the key externally, and which either cannot or should not
447 * export it into RAM. Alternatively, an implementation
448 * (regardless of SW or HW) might not support deducing e.g.
449 * P, Q from N, D, E if the former are not part of the
450 * implementation.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100451 *
452 */
453int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
454 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
455 mbedtls_mpi *D, mbedtls_mpi *E );
456
457/**
458 * \brief Export core parameters of an RSA key
459 * in raw big-endian binary format
460 *
461 * \param ctx Initialized RSA context
462 * \param N Byte array to store the RSA modulus, or NULL
463 * \param N_len Size of buffer for modulus
464 * \param P Byte array to hold the first prime factor of N, or NULL
465 * \param P_len Size of buffer for first prime factor
466 * \param Q Byte array to hold the second prime factor of N, or NULL
467 * \param Q_len Size of buffer for second prime factor
468 * \param D Byte array to hold the private exponent, or NULL
469 * \param D_len Size of buffer for private exponent
470 * \param E Byte array to hold the public exponent, or NULL
471 * \param E_len Size of buffer for public exponent
472 *
473 * \note The length fields are ignored if the corresponding
474 * buffer pointers are NULL.
475 *
Hanno Beckered203612017-09-29 13:34:25 +0100476 * \return
477 * - 0 if successful. In this case, the non-NULL buffers
478 * pointed to by N, P, Q, D, E are fully written, with
479 * additional unused space filled leading by 0-bytes.
480 * - Non-zero return code otherwise. In particular, if
481 * exporting the requested parameters
482 * cannot be done because of a lack of functionality
483 * or because of security policies, the error code
484 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
485 * In this case, the RSA context stays intact and can
486 * be continued to be used.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100487 *
Hanno Beckered203612017-09-29 13:34:25 +0100488 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100489 * would be the following: Firstly, it might be that an
490 * alternative RSA implementation is in use which stores
491 * the key externally, and which either cannot or should not
492 * export it into RAM. Alternatively, an implementation
493 * (regardless of SW or HW) might not support deducing e.g.
494 * P, Q from N, D, E if the former are not part of the
495 * implementation.
496 *
497 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100498 */
499int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
500 unsigned char *N, size_t N_len,
501 unsigned char *P, size_t P_len,
502 unsigned char *Q, size_t Q_len,
503 unsigned char *D, size_t D_len,
504 unsigned char *E, size_t E_len );
505
506/**
507 * \brief Export CRT parameters of a private RSA key
508 *
509 * \param ctx Initialized RSA context
510 * \param DP MPI to hold D modulo P-1, or NULL
511 * \param DQ MPI to hold D modulo Q-1, or NULL
512 * \param QP MPI to hold modular inverse of Q modulo P, or NULL
513 *
514 * \return 0 if successful, non-zero error code otherwise.
515 *
516 * \note Alternative RSA implementations not using CRT-parameters
517 * internally can implement this function using based on
518 * \c mbedtls_rsa_deduce_opt.
519 *
520 */
521int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
522 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
523
Paul Bakker5121ce52009-01-03 21:22:43 +0000524/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100525 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100527 *
528 * \param ctx RSA context to be set
Hanno Becker4b2f6912017-09-29 13:34:55 +0100529 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
530 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100531 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100532void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
533 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100534
535/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100536 * \brief Get length of RSA modulus in bytes
537 *
538 * \param ctx Initialized RSA context
539 *
540 * \return Length of RSA modulus, in bytes.
541 *
542 */
543size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
544
545/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 * \brief Generate an RSA keypair
547 *
548 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000549 * \param f_rng RNG function
550 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 * \param nbits size of the public key in bits
552 * \param exponent public exponent (e.g., 65537)
553 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000555 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100557 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100560 int (*f_rng)(void *, unsigned char *, size_t),
561 void *p_rng,
562 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
564/**
Hanno Becker43a08d02017-10-02 13:16:35 +0100565 * \brief Check if a context contains (at least) an RSA public key
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 *
567 * \param ctx RSA context to be checked
568 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100569 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
570 * On success, it is guaranteed that enough information is
571 * present to perform an RSA public key operation
572 * \c mbedtls_rsa_public.
573 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
577/**
Hanno Becker43a08d02017-10-02 13:16:35 +0100578 * \brief Check if a context contains an RSA private key
579 * and perform basic sanity checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 *
581 * \param ctx RSA context to be checked
582 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100583 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
584 * On success, it is guaranteed that enough information is
585 * present to perform RSA private and public key operations.
586 *
587 * \warning This function is *not* obliged to perform an exhaustive
588 * sanity check what would guarantee the internal parameters
589 * to match and \c mbedtls_rsa_private and \c mbedtls_rsa_public
590 * to be mutually inverse to each other.
591 * The reason is that for minimal non-CRT implementations
592 * using only N, D, E, for example, checking the validity
593 * would be computationally expensive.
594 * Users mistrusting their key material should use other
595 * means for verification; see the documentation of
596 * \c mbedtls_rsa_complete.
597 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
601/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100602 * \brief Check a public-private RSA key pair.
603 * Check each of the contexts, and make sure they match.
604 *
605 * \param pub RSA context holding the public key
606 * \param prv RSA context holding the private key
607 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100608 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100609 */
Hanno Becker98838b02017-10-02 13:16:10 +0100610int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
611 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100612
613/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 * \brief Do an RSA public key operation
615 *
616 * \param ctx RSA context
617 * \param input input buffer
618 * \param output output buffer
619 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100620 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000621 *
622 * \note This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800623 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000624 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000625 *
626 * \note The input and output buffers must be large
627 * enough (eg. 128 bytes if RSA-1024 is used).
628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000630 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000631 unsigned char *output );
632
633/**
634 * \brief Do an RSA private key operation
635 *
636 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200637 * \param f_rng RNG function (Needed for blinding)
638 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 * \param input input buffer
640 * \param output output buffer
641 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100642 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000643 *
644 * \note The input and output buffers must be large
645 * enough (eg. 128 bytes if RSA-1024 is used).
646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200648 int (*f_rng)(void *, unsigned char *, size_t),
649 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000650 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000651 unsigned char *output );
652
653/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100654 * \brief Generic wrapper to perform a PKCS#1 encryption using the
655 * mode from the context. Add the message padding, then do an
656 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000657 *
658 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200659 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100660 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000661 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100662 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000663 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000664 * \param input buffer holding the data to be encrypted
665 * \param output buffer that will hold the ciphertext
666 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100667 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 *
669 * \note The output buffer must be as large as the size
670 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
671 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000673 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000674 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000675 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000676 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000677 unsigned char *output );
678
679/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100680 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
681 *
682 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100683 * \param f_rng RNG function (Needed for padding and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100684 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100685 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100686 * \param ilen contains the plaintext length
687 * \param input buffer holding the data to be encrypted
688 * \param output buffer that will hold the ciphertext
689 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100690 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100691 *
692 * \note The output buffer must be as large as the size
693 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100696 int (*f_rng)(void *, unsigned char *, size_t),
697 void *p_rng,
698 int mode, size_t ilen,
699 const unsigned char *input,
700 unsigned char *output );
701
702/**
703 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
704 *
705 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200706 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100707 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100708 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100709 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100710 * \param label buffer holding the custom label to use
711 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100712 * \param ilen contains the plaintext length
713 * \param input buffer holding the data to be encrypted
714 * \param output buffer that will hold the ciphertext
715 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100716 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100717 *
718 * \note The output buffer must be as large as the size
719 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
720 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100722 int (*f_rng)(void *, unsigned char *, size_t),
723 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100724 int mode,
725 const unsigned char *label, size_t label_len,
726 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100727 const unsigned char *input,
728 unsigned char *output );
729
730/**
731 * \brief Generic wrapper to perform a PKCS#1 decryption using the
732 * mode from the context. Do an RSA operation, then remove
733 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000734 *
735 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100736 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200737 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100738 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000739 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000740 * \param input buffer holding the encrypted data
741 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000742 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000743 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100744 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000745 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100746 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100747 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100748 * if RSA-1024 is used) to be able to hold an arbitrary
749 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100750 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100751 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100752 *
753 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100754 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000755 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200757 int (*f_rng)(void *, unsigned char *, size_t),
758 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000759 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000760 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000761 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000762 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000763
764/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100765 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
766 *
767 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100768 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200769 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100770 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100771 * \param olen will contain the plaintext length
772 * \param input buffer holding the encrypted data
773 * \param output buffer that will hold the plaintext
774 * \param output_max_len maximum length of the output buffer
775 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100776 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100777 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100778 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100779 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100780 * if RSA-1024 is used) to be able to hold an arbitrary
781 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100782 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100783 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100784 *
785 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100786 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200789 int (*f_rng)(void *, unsigned char *, size_t),
790 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100791 int mode, size_t *olen,
792 const unsigned char *input,
793 unsigned char *output,
794 size_t output_max_len );
795
796/**
797 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
798 *
799 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100800 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200801 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100802 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100803 * \param label buffer holding the custom label to use
804 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100805 * \param olen will contain the plaintext length
806 * \param input buffer holding the encrypted data
807 * \param output buffer that will hold the plaintext
808 * \param output_max_len maximum length of the output buffer
809 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100810 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100811 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100812 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100813 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100814 * if RSA-1024 is used) to be able to hold an arbitrary
815 * decrypted message. If it is not large enough to hold
Hanno Becker8fd55482017-08-23 14:07:48 +0100816 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100817 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100818 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100819 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100820 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200823 int (*f_rng)(void *, unsigned char *, size_t),
824 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100825 int mode,
826 const unsigned char *label, size_t label_len,
827 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100828 const unsigned char *input,
829 unsigned char *output,
830 size_t output_max_len );
831
832/**
833 * \brief Generic wrapper to perform a PKCS#1 signature using the
834 * mode from the context. Do a private RSA operation to sign
835 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000836 *
837 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200838 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100839 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000840 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100841 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
842 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for
843 * signing raw data)
844 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000845 * \param hash buffer holding the message digest
846 * \param sig buffer that will hold the ciphertext
847 *
848 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100849 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000850 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100851 * \note The \c sig buffer must be as large as the size
852 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000853 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200854 * \note In case of PKCS#1 v2.1 encoding, see comments on
Hanno Becker4b2f6912017-09-29 13:34:55 +0100855 * \note \c mbedtls_rsa_rsassa_pss_sign() for details on
856 * \c md_alg and \c hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000857 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000859 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000860 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000863 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000864 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000865 unsigned char *sig );
866
867/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100868 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
869 *
870 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100871 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200872 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100873 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
874 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
875 * for signing raw data)
876 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100877 * \param hash buffer holding the message digest
878 * \param sig buffer that will hold the ciphertext
879 *
880 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100881 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100882 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100883 * \note The \c sig buffer must be as large as the size
884 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100885 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200887 int (*f_rng)(void *, unsigned char *, size_t),
888 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100889 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100891 unsigned int hashlen,
892 const unsigned char *hash,
893 unsigned char *sig );
894
895/**
896 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
897 *
898 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200899 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100900 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100901 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100902 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
903 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
904 * for signing raw data)
905 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100906 * \param hash buffer holding the message digest
907 * \param sig buffer that will hold the ciphertext
908 *
909 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100910 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100911 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100912 * \note The \c sig buffer must be as large as the size
913 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100914 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100915 * \note The \c hash_id in the RSA context is the one used for the
916 * encoding. \c md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100917 * that is encoded. According to RFC 3447 it is advised to
918 * keep both hashes the same.
919 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100921 int (*f_rng)(void *, unsigned char *, size_t),
922 void *p_rng,
923 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100925 unsigned int hashlen,
926 const unsigned char *hash,
927 unsigned char *sig );
928
929/**
930 * \brief Generic wrapper to perform a PKCS#1 verification using the
931 * mode from the context. Do a public RSA operation and check
932 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000933 *
934 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100935 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200936 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100937 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
938 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
939 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000940 * \param hash buffer holding the message digest
941 * \param sig buffer holding the ciphertext
942 *
943 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100944 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000945 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100946 * \note The \c sig buffer must be as large as the size
947 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000948 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200949 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000951 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200953 int (*f_rng)(void *, unsigned char *, size_t),
954 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000957 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000958 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200959 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000960
961/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100962 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
963 *
964 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100965 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200966 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100967 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
968 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
969 * for signing raw data)
970 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100971 * \param hash buffer holding the message digest
972 * \param sig buffer holding the ciphertext
973 *
974 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100975 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100976 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100977 * \note The \c sig buffer must be as large as the size
978 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200981 int (*f_rng)(void *, unsigned char *, size_t),
982 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100983 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100985 unsigned int hashlen,
986 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200987 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100988
989/**
990 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200991 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +0100992 *
993 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100994 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200995 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100996 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
997 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
998 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100999 * \param hash buffer holding the message digest
1000 * \param sig buffer holding the ciphertext
1001 *
1002 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001003 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +01001004 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001005 * \note The \c sig buffer must be as large as the size
1006 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +01001007 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001008 * \note The \c hash_id in the RSA context is the one used for the
1009 * verification. \c md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001010 * hash that is verified. According to RFC 3447 it is advised to
Hanno Becker4b2f6912017-09-29 13:34:55 +01001011 * keep both hashes the same. If \c hash_id in the RSA context is
1012 * unset, the \c md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001015 int (*f_rng)(void *, unsigned char *, size_t),
1016 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001017 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001019 unsigned int hashlen,
1020 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001021 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001022
1023/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001024 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
1025 * (This is the version with "full" options.)
1026 *
1027 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +01001028 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001029 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +01001030 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
1031 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
1032 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001033 * \param hash buffer holding the message digest
1034 * \param mgf1_hash_id message digest used for mask generation
1035 * \param expected_salt_len Length of the salt used in padding, use
Hanno Becker4b2f6912017-09-29 13:34:55 +01001036 * \c MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001037 * \param sig buffer holding the ciphertext
1038 *
1039 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001040 * or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001041 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001042 * \note The \c sig buffer must be as large as the size
1043 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001044 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001045 * \note The \c hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001046 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001048 int (*f_rng)(void *, unsigned char *, size_t),
1049 void *p_rng,
1050 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001052 unsigned int hashlen,
1053 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001055 int expected_salt_len,
1056 const unsigned char *sig );
1057
1058/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001059 * \brief Copy the components of an RSA context
1060 *
1061 * \param dst Destination context
1062 * \param src Source context
1063 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001064 * \return 0 on success,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001065 * \c MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001068
1069/**
Paul Bakker5121ce52009-01-03 21:22:43 +00001070 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001071 *
1072 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +00001073 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001075
1076/**
1077 * \brief Checkup routine
1078 *
1079 * \return 0 if successful, or 1 if the test failed
1080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001082
1083#ifdef __cplusplus
1084}
1085#endif
1086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087#endif /* MBEDTLS_RSA_C */
Paul Bakkered27a042013-04-18 22:46:23 +02001088
Paul Bakker5121ce52009-01-03 21:22:43 +00001089#endif /* rsa.h */