blob: 04488774901e118fd5b1cccc1e6895f225d85328 [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 Becker0f65e0c2017-10-03 14:39:16 +0100107int mbedtls_rsa_deduce_primes( mbedtls_mpi const *N, mbedtls_mpi const *D,
Hanno Beckerba5b7552017-10-02 09:55:49 +0100108 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 Becker8ba6ce42017-10-03 14:36:26 +0100132int mbedtls_rsa_deduce_private_exponent( mbedtls_mpi const *P,
133 mbedtls_mpi const *Q,
134 mbedtls_mpi const *E,
135 mbedtls_mpi *D );
Hanno Beckera3ebec22017-08-23 14:06:24 +0100136
137
138/**
139 * \brief Generate RSA-CRT parameters
140 *
141 * \note This is a 'static' helper function not operating on
142 * an RSA context. Alternative implementations need not
143 * overwrite it.
144 *
145 * \param P First prime factor of N
146 * \param Q Second prime factor of N
147 * \param D RSA private exponent
148 * \param DP Output variable for D modulo P-1
149 * \param DQ Output variable for D modulo Q-1
150 * \param QP Output variable for the modular inverse of Q modulo P.
151 *
152 * \return 0 on success, non-zero error code otherwise.
153 *
Hanno Becker1b831fe2017-10-02 12:24:50 +0100154 * \note This function does not check whether P, Q are
155 * prime and whether D is a valid private exponent.
156 *
Hanno Beckera3ebec22017-08-23 14:06:24 +0100157 */
158int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
159 const mbedtls_mpi *D, mbedtls_mpi *DP,
160 mbedtls_mpi *DQ, mbedtls_mpi *QP );
161
162
163/**
164 * \brief Check validity of core RSA parameters
165 *
166 * \note This is a 'static' helper function not operating on
167 * an RSA context. Alternative implementations need not
168 * overwrite it.
169 *
170 * \param N RSA modulus N = PQ
171 * \param P First prime factor of N
172 * \param Q Second prime factor of N
173 * \param D RSA private exponent
174 * \param E RSA public exponent
Hanno Becker56bae952017-09-29 15:33:10 +0100175 * \param f_rng PRNG to be used for primality check, or NULL
Hanno Beckera3ebec22017-08-23 14:06:24 +0100176 * \param p_rng PRNG context for f_rng, or NULL
177 *
Hanno Beckered203612017-09-29 13:34:25 +0100178 * \return
Hanno Beckerb5beaa82017-10-02 13:01:43 +0100179 * - 0 if the following conditions are satisfied
180 * if all relevant parameters are provided:
181 * - P prime if f_rng != NULL
182 * - Q prime if f_rng != NULL
183 * - 1 < N = PQ
184 * - 1 < D, E < N
Hanno Beckered203612017-09-29 13:34:25 +0100185 * - D and E are modular inverses modulo P-1 and Q-1
Hanno Becker750e8b42017-08-25 07:54:27 +0100186 * - A non-zero error code otherwise.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100187 *
188 * \note The function can be used with a restricted set of arguments
189 * to perform specific checks only. E.g., calling it with
190 * (-,P,-,-,-) and a PRNG amounts to a primality check for P.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100191 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100192int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
193 const mbedtls_mpi *Q, const mbedtls_mpi *D,
194 const mbedtls_mpi *E,
195 int (*f_rng)(void *, unsigned char *, size_t),
196 void *p_rng );
Hanno Beckera3ebec22017-08-23 14:06:24 +0100197
198/**
Hanno Beckerd3637992017-08-25 07:55:03 +0100199 * \brief Check validity of RSA CRT parameters
200 *
201 * \note This is a 'static' helper function not operating on
202 * an RSA context. Alternative implementations need not
203 * overwrite it.
204 *
205 * \param P First prime factor of RSA modulus
206 * \param Q Second prime factor of RSA modulus
207 * \param D RSA private exponent
208 * \param DP MPI to check for D modulo P-1
209 * \param DQ MPI to check for D modulo P-1
210 * \param QP MPI to check for the modular inverse of Q modulo P.
211 *
Hanno Beckered203612017-09-29 13:34:25 +0100212 * \return
213 * - 0 if the following conditions are satisfied:
214 * - D = DP mod P-1 if P, D, DP != NULL
215 * - Q = DQ mod P-1 if P, D, DQ != NULL
216 * - QP = Q^-1 mod P if P, Q, QP != NULL
217 * - \c MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if check failed,
218 * potentially including \c MBEDTLS_ERR_MPI_XXX if some
Hanno Beckerd3637992017-08-25 07:55:03 +0100219 * MPI calculations failed.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100220 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if insufficient
Hanno Beckerd3637992017-08-25 07:55:03 +0100221 * data was provided to check DP, DQ or QP.
222 *
223 * \note The function can be used with a restricted set of arguments
224 * to perform specific checks only. E.g., calling it with the
225 * parameters (P, -, D, DP, -, -) will check DP = D mod P-1.
226 */
227int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
228 const mbedtls_mpi *D, const mbedtls_mpi *DP,
229 const mbedtls_mpi *DQ, const mbedtls_mpi *QP );
230
231/**
Hanno Beckera3ebec22017-08-23 14:06:24 +0100232 * Implementation of RSA interface
233 */
234
Hanno Beckerab377312017-08-23 16:24:51 +0100235#if !defined(MBEDTLS_RSA_ALT)
236
Hanno Beckera3ebec22017-08-23 14:06:24 +0100237/**
Hanno Becker5063cd22017-09-29 11:49:12 +0100238 * \brief RSA context structure
239 *
240 * \note Direct manipulation of the members of this structure
241 * is deprecated and will no longer be supported starting
242 * from the next major release. All manipulation should instead
243 * be done through the public interface functions.
244 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 */
246typedef struct
247{
248 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +0000249 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_mpi N; /*!< public modulus */
252 mbedtls_mpi E; /*!< public exponent */
Paul Bakker5121ce52009-01-03 21:22:43 +0000253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_mpi D; /*!< private exponent */
255 mbedtls_mpi P; /*!< 1st prime factor */
256 mbedtls_mpi Q; /*!< 2nd prime factor */
Hanno Becker1a59e792017-08-23 07:41:10 +0100257
258#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 mbedtls_mpi DP; /*!< D % (P - 1) */
260 mbedtls_mpi DQ; /*!< D % (Q - 1) */
261 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Hanno Becker1a59e792017-08-23 07:41:10 +0100262#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 mbedtls_mpi RN; /*!< cached R^2 mod N */
Hanno Becker1a59e792017-08-23 07:41:10 +0100265
266#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_mpi RP; /*!< cached R^2 mod P */
268 mbedtls_mpi RQ; /*!< cached R^2 mod Q */
Hanno Becker1a59e792017-08-23 07:41:10 +0100269#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_mpi Vi; /*!< cached blinding value */
272 mbedtls_mpi Vf; /*!< cached un-blinding value */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200273
Hanno Becker4b2f6912017-09-29 13:34:55 +0100274 int padding; /*!< \c MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
275 \c MBEDTLS_RSA_PKCS_v21 for OAEP/PSS */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
277 specified in the mbedtls_md.h header file
Paul Bakker9dcc3222011-03-08 14:16:06 +0000278 for the EME-OAEP and EMSA-PSS
279 encoding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#if defined(MBEDTLS_THREADING_C)
281 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200282#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000283}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000285
Hanno Beckerab377312017-08-23 16:24:51 +0100286#else
287
288#include "rsa_alt.h"
289
290#endif /* MBEDTLS_RSA_ALT */
291
Paul Bakker5121ce52009-01-03 21:22:43 +0000292/**
293 * \brief Initialize an RSA context
294 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100295 * Note: Set padding to \c MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000296 * encryption scheme and the RSASSA-PSS signature scheme.
297 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 * \param ctx RSA context to be initialized
Hanno Becker4b2f6912017-09-29 13:34:55 +0100299 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
300 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 *
302 * \note The hash_id parameter is actually ignored
Hanno Becker4b2f6912017-09-29 13:34:55 +0100303 * when using \c MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200304 *
305 * \note Choice of padding mode is strictly enforced for private key
306 * operations, since there might be security concerns in
307 * mixing padding modes. For public key operations it's merely
308 * a default value, which can be overriden by calling specific
309 * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
310 *
311 * \note The chosen hash is always used for OEAP encryption.
312 * For PSS signatures, it's always used for making signatures,
313 * but can be overriden (and always is, if set to
Hanno Becker4b2f6912017-09-29 13:34:55 +0100314 * \c MBEDTLS_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100317 int padding,
318 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100320/**
321 * \brief Import a set of core parameters into an RSA context
322 *
323 * \param ctx Initialized RSA context to store parameters
324 * \param N RSA modulus, or NULL
325 * \param P First prime factor of N, or NULL
326 * \param Q Second prime factor of N, or NULL
327 * \param D Private exponent, or NULL
328 * \param E Public exponent, or NULL
329 *
330 * \note This function can be called multiple times for successive
331 * imports if the parameters are not simultaneously present.
332 * Any sequence of calls to this function should be followed
333 * by a call to \c mbedtls_rsa_complete which will check
334 * and complete the provided information to a ready-for-use
335 * public or private RSA key.
336 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100337 * \note The imported parameters are copied and need not be preserved
338 * for the lifetime of the RSA context being set up.
339 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100340 * \return 0 if successful, non-zero error code on failure.
341 */
342int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
343 const mbedtls_mpi *N,
344 const mbedtls_mpi *P, const mbedtls_mpi *Q,
345 const mbedtls_mpi *D, const mbedtls_mpi *E );
346
347/**
348 * \brief Import core RSA parameters in raw big-endian
349 * binary format into an RSA context
350 *
351 * \param ctx Initialized RSA context to store parameters
352 * \param N RSA modulus, or NULL
353 * \param N_len Byte length of N, ignored if N == NULL
354 * \param P First prime factor of N, or NULL
355 * \param P_len Byte length of P, ignored if P == NULL
356 * \param Q Second prime factor of N, or NULL
357 * \param Q_len Byte length of Q, ignored if Q == NULL
358 * \param D Private exponent, or NULL
359 * \param D_len Byte length of D, ignored if D == NULL
360 * \param E Public exponent, or NULL
361 * \param E_len Byte length of E, ignored if E == NULL
362 *
363 * \note This function can be called multiple times for successive
364 * imports if the parameters are not simultaneously present.
365 * Any sequence of calls to this function should be followed
366 * by a call to \c mbedtls_rsa_complete which will check
367 * and complete the provided information to a ready-for-use
368 * public or private RSA key.
369 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100370 * \note The imported parameters are copied and need not be preserved
371 * for the lifetime of the RSA context being set up.
372 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100373 * \return 0 if successful, non-zero error code on failure.
374 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100375int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100376 unsigned char const *N, size_t N_len,
377 unsigned char const *P, size_t P_len,
378 unsigned char const *Q, size_t Q_len,
379 unsigned char const *D, size_t D_len,
380 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100381
382/**
383 * \brief Attempt to complete an RSA context from
384 * a set of imported core parameters.
385 *
386 * \param ctx Initialized RSA context to store parameters
Hanno Becker43a08d02017-10-02 13:16:35 +0100387 * \param f_rng RNG function, or NULL
388 * \param p_rng RNG parameter, or NULL
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100389 *
Hanno Beckered203612017-09-29 13:34:25 +0100390 * \note
391 * - To setup an RSA public key, precisely N and E
392 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100393 *
Hanno Beckered203612017-09-29 13:34:25 +0100394 * - To setup an RSA private key, enough information must be
Hanno Becker98838b02017-10-02 13:16:10 +0100395 * present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100396 *
Hanno Beckered203612017-09-29 13:34:25 +0100397 * The default implementation supports the following:
398 * - Derive P, Q from N, D, E
399 * - Derive N, D from P, Q, E.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100400 *
Hanno Beckered203612017-09-29 13:34:25 +0100401 * - Alternative implementations need not support these
402 * and may return \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA instead.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100403 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100404 * \note The PRNG is used for the probabilistic algorithm
405 * used in the derivation of P, Q from N, D, E. If it
406 * not present, a deterministic heuristic is used.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100407 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100408 * \return
409 * - 0 if successful. In this case, it is guaranteed
410 * the functions \c mbedtls_rsa_check_pubkey resp.
411 * \c mbedtls_rsa_check_privkey pass in case of a
412 * public resp. private key.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100413 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100414 * derivations failed.
Hanno Becker43a08d02017-10-02 13:16:35 +0100415 *
416 * \warning Implementations are *not* obliged to perform exhaustive
417 * validation of the imported parameters!
418 * In particular, parameters that are not needed by the
419 * implementation may be silently discarded and left unchecked.
420 * If the user mistrusts the given key material, he should
421 * employ other means for verification like the helper functions
422 * \c mbedtls_rsa_validate_params, \c mbedtls_rsa_validate_crt.
423 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100424 */
425int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
426 int (*f_rng)(void *, unsigned char *, size_t),
427 void *p_rng );
428
429/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100430 * \brief Export core parameters of an RSA key
431 *
432 * \param ctx Initialized RSA context
433 * \param N MPI to hold the RSA modulus, or NULL
434 * \param P MPI to hold the first prime factor of N, or NULL
435 * \param Q MPI to hold the second prime factor of N, or NULL
436 * \param D MPI to hold the private exponent, or NULL
437 * \param E MPI to hold the public exponent, or NULL
438 *
Hanno Beckered203612017-09-29 13:34:25 +0100439 * \return
440 * - 0 if successful. In this case, the non-NULL buffers
441 * pointed to by N, P, Q, D, E are fully written, with
442 * additional unused space filled leading by 0-bytes.
443 * - Non-zero return code otherwise. In particular, if
444 * exporting the requested parameters
445 * cannot be done because of a lack of functionality
446 * or because of security policies, the error code
447 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
448 * In this case, the RSA context stays intact and can
449 * be continued to be used.
Hanno Becker91c194d2017-09-29 12:50:12 +0100450 *
Hanno Beckered203612017-09-29 13:34:25 +0100451 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100452 * would be the following: Firstly, it might be that an
453 * alternative RSA implementation is in use which stores
454 * the key externally, and which either cannot or should not
455 * export it into RAM. Alternatively, an implementation
456 * (regardless of SW or HW) might not support deducing e.g.
457 * P, Q from N, D, E if the former are not part of the
458 * implementation.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100459 *
460 */
461int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
462 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
463 mbedtls_mpi *D, mbedtls_mpi *E );
464
465/**
466 * \brief Export core parameters of an RSA key
467 * in raw big-endian binary format
468 *
469 * \param ctx Initialized RSA context
470 * \param N Byte array to store the RSA modulus, or NULL
471 * \param N_len Size of buffer for modulus
472 * \param P Byte array to hold the first prime factor of N, or NULL
473 * \param P_len Size of buffer for first prime factor
474 * \param Q Byte array to hold the second prime factor of N, or NULL
475 * \param Q_len Size of buffer for second prime factor
476 * \param D Byte array to hold the private exponent, or NULL
477 * \param D_len Size of buffer for private exponent
478 * \param E Byte array to hold the public exponent, or NULL
479 * \param E_len Size of buffer for public exponent
480 *
481 * \note The length fields are ignored if the corresponding
482 * buffer pointers are NULL.
483 *
Hanno Beckered203612017-09-29 13:34:25 +0100484 * \return
485 * - 0 if successful. In this case, the non-NULL buffers
486 * pointed to by N, P, Q, D, E are fully written, with
487 * additional unused space filled leading by 0-bytes.
488 * - Non-zero return code otherwise. In particular, if
489 * exporting the requested parameters
490 * cannot be done because of a lack of functionality
491 * or because of security policies, the error code
492 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
493 * In this case, the RSA context stays intact and can
494 * be continued to be used.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100495 *
Hanno Beckered203612017-09-29 13:34:25 +0100496 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100497 * would be the following: Firstly, it might be that an
498 * alternative RSA implementation is in use which stores
499 * the key externally, and which either cannot or should not
500 * export it into RAM. Alternatively, an implementation
501 * (regardless of SW or HW) might not support deducing e.g.
502 * P, Q from N, D, E if the former are not part of the
503 * implementation.
504 *
505 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100506 */
507int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
508 unsigned char *N, size_t N_len,
509 unsigned char *P, size_t P_len,
510 unsigned char *Q, size_t Q_len,
511 unsigned char *D, size_t D_len,
512 unsigned char *E, size_t E_len );
513
514/**
515 * \brief Export CRT parameters of a private RSA key
516 *
517 * \param ctx Initialized RSA context
518 * \param DP MPI to hold D modulo P-1, or NULL
519 * \param DQ MPI to hold D modulo Q-1, or NULL
520 * \param QP MPI to hold modular inverse of Q modulo P, or NULL
521 *
522 * \return 0 if successful, non-zero error code otherwise.
523 *
524 * \note Alternative RSA implementations not using CRT-parameters
525 * internally can implement this function using based on
526 * \c mbedtls_rsa_deduce_opt.
527 *
528 */
529int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
530 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
531
Paul Bakker5121ce52009-01-03 21:22:43 +0000532/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100533 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100535 *
536 * \param ctx RSA context to be set
Hanno Becker4b2f6912017-09-29 13:34:55 +0100537 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
538 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100539 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100540void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
541 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100542
543/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100544 * \brief Get length of RSA modulus in bytes
545 *
546 * \param ctx Initialized RSA context
547 *
548 * \return Length of RSA modulus, in bytes.
549 *
550 */
551size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
552
553/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000554 * \brief Generate an RSA keypair
555 *
556 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000557 * \param f_rng RNG function
558 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 * \param nbits size of the public key in bits
560 * \param exponent public exponent (e.g., 65537)
561 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000563 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100565 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100568 int (*f_rng)(void *, unsigned char *, size_t),
569 void *p_rng,
570 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
572/**
Hanno Becker43a08d02017-10-02 13:16:35 +0100573 * \brief Check if a context contains (at least) an RSA public key
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 *
575 * \param ctx RSA context to be checked
576 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100577 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
578 * On success, it is guaranteed that enough information is
579 * present to perform an RSA public key operation
580 * \c mbedtls_rsa_public.
581 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000582 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
585/**
Hanno Becker43a08d02017-10-02 13:16:35 +0100586 * \brief Check if a context contains an RSA private key
587 * and perform basic sanity checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 *
589 * \param ctx RSA context to be checked
590 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100591 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
592 * On success, it is guaranteed that enough information is
593 * present to perform RSA private and public key operations.
594 *
595 * \warning This function is *not* obliged to perform an exhaustive
596 * sanity check what would guarantee the internal parameters
597 * to match and \c mbedtls_rsa_private and \c mbedtls_rsa_public
598 * to be mutually inverse to each other.
599 * The reason is that for minimal non-CRT implementations
600 * using only N, D, E, for example, checking the validity
601 * would be computationally expensive.
602 * Users mistrusting their key material should use other
603 * means for verification; see the documentation of
604 * \c mbedtls_rsa_complete.
605 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000608
609/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100610 * \brief Check a public-private RSA key pair.
611 * Check each of the contexts, and make sure they match.
612 *
613 * \param pub RSA context holding the public key
614 * \param prv RSA context holding the private key
615 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100616 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100617 */
Hanno Becker98838b02017-10-02 13:16:10 +0100618int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
619 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100620
621/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000622 * \brief Do an RSA public key operation
623 *
624 * \param ctx RSA context
625 * \param input input buffer
626 * \param output output buffer
627 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100628 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000629 *
630 * \note This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800631 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000632 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000633 *
634 * \note The input and output buffers must be large
635 * enough (eg. 128 bytes if RSA-1024 is used).
636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000638 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 unsigned char *output );
640
641/**
642 * \brief Do an RSA private key operation
643 *
644 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200645 * \param f_rng RNG function (Needed for blinding)
646 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 * \param input input buffer
648 * \param output output buffer
649 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100650 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000651 *
652 * \note The input and output buffers must be large
653 * enough (eg. 128 bytes if RSA-1024 is used).
654 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200656 int (*f_rng)(void *, unsigned char *, size_t),
657 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000658 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000659 unsigned char *output );
660
661/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100662 * \brief Generic wrapper to perform a PKCS#1 encryption using the
663 * mode from the context. Add the message padding, then do an
664 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 *
666 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200667 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100668 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000669 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100670 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000671 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 * \param input buffer holding the data to be encrypted
673 * \param output buffer that will hold the ciphertext
674 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100675 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 *
677 * \note The output buffer must be as large as the size
678 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000681 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000682 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000683 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000684 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 unsigned char *output );
686
687/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100688 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
689 *
690 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100691 * \param f_rng RNG function (Needed for padding and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100692 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100693 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100694 * \param ilen contains the plaintext length
695 * \param input buffer holding the data to be encrypted
696 * \param output buffer that will hold the ciphertext
697 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100698 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100699 *
700 * \note The output buffer must be as large as the size
701 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100704 int (*f_rng)(void *, unsigned char *, size_t),
705 void *p_rng,
706 int mode, size_t ilen,
707 const unsigned char *input,
708 unsigned char *output );
709
710/**
711 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
712 *
713 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200714 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100715 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100716 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100717 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100718 * \param label buffer holding the custom label to use
719 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100720 * \param ilen contains the plaintext length
721 * \param input buffer holding the data to be encrypted
722 * \param output buffer that will hold the ciphertext
723 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100724 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100725 *
726 * \note The output buffer must be as large as the size
727 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100730 int (*f_rng)(void *, unsigned char *, size_t),
731 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100732 int mode,
733 const unsigned char *label, size_t label_len,
734 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100735 const unsigned char *input,
736 unsigned char *output );
737
738/**
739 * \brief Generic wrapper to perform a PKCS#1 decryption using the
740 * mode from the context. Do an RSA operation, then remove
741 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 *
743 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100744 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200745 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100746 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000747 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000748 * \param input buffer holding the encrypted data
749 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000750 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000751 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100752 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000753 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100754 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100755 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100756 * if RSA-1024 is used) to be able to hold an arbitrary
757 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100758 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100759 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100760 *
761 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100762 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200765 int (*f_rng)(void *, unsigned char *, size_t),
766 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000767 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000768 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000769 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000770 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000771
772/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100773 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
774 *
775 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100776 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200777 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100778 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100779 * \param olen will contain the plaintext length
780 * \param input buffer holding the encrypted data
781 * \param output buffer that will hold the plaintext
782 * \param output_max_len maximum length of the output buffer
783 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100784 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100785 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100786 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100787 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100788 * if RSA-1024 is used) to be able to hold an arbitrary
789 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100790 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100791 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100792 *
793 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100794 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100795 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200797 int (*f_rng)(void *, unsigned char *, size_t),
798 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100799 int mode, size_t *olen,
800 const unsigned char *input,
801 unsigned char *output,
802 size_t output_max_len );
803
804/**
805 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
806 *
807 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100808 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200809 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100810 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100811 * \param label buffer holding the custom label to use
812 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100813 * \param olen will contain the plaintext length
814 * \param input buffer holding the encrypted data
815 * \param output buffer that will hold the plaintext
816 * \param output_max_len maximum length of the output buffer
817 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100818 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100819 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100820 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100821 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100822 * if RSA-1024 is used) to be able to hold an arbitrary
823 * decrypted message. If it is not large enough to hold
Hanno Becker8fd55482017-08-23 14:07:48 +0100824 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100825 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100826 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100827 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100828 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100829 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200831 int (*f_rng)(void *, unsigned char *, size_t),
832 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100833 int mode,
834 const unsigned char *label, size_t label_len,
835 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100836 const unsigned char *input,
837 unsigned char *output,
838 size_t output_max_len );
839
840/**
841 * \brief Generic wrapper to perform a PKCS#1 signature using the
842 * mode from the context. Do a private RSA operation to sign
843 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000844 *
845 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200846 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100847 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000848 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100849 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
850 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for
851 * signing raw data)
852 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000853 * \param hash buffer holding the message digest
854 * \param sig buffer that will hold the ciphertext
855 *
856 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100857 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000858 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100859 * \note The \c sig buffer must be as large as the size
860 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000861 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200862 * \note In case of PKCS#1 v2.1 encoding, see comments on
Hanno Becker4b2f6912017-09-29 13:34:55 +0100863 * \note \c mbedtls_rsa_rsassa_pss_sign() for details on
864 * \c md_alg and \c hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000867 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000868 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000871 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000872 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000873 unsigned char *sig );
874
875/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100876 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
877 *
878 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100879 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200880 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100881 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
882 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
883 * for signing raw data)
884 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100885 * \param hash buffer holding the message digest
886 * \param sig buffer that will hold the ciphertext
887 *
888 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100889 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100890 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100891 * \note The \c sig buffer must be as large as the size
892 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100893 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200895 int (*f_rng)(void *, unsigned char *, size_t),
896 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100897 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100899 unsigned int hashlen,
900 const unsigned char *hash,
901 unsigned char *sig );
902
903/**
904 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
905 *
906 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200907 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100908 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100909 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100910 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
911 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
912 * for signing raw data)
913 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100914 * \param hash buffer holding the message digest
915 * \param sig buffer that will hold the ciphertext
916 *
917 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100918 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100919 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100920 * \note The \c sig buffer must be as large as the size
921 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100922 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100923 * \note The \c hash_id in the RSA context is the one used for the
924 * encoding. \c md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100925 * that is encoded. According to RFC 3447 it is advised to
926 * keep both hashes the same.
927 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100929 int (*f_rng)(void *, unsigned char *, size_t),
930 void *p_rng,
931 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100933 unsigned int hashlen,
934 const unsigned char *hash,
935 unsigned char *sig );
936
937/**
938 * \brief Generic wrapper to perform a PKCS#1 verification using the
939 * mode from the context. Do a public RSA operation and check
940 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000941 *
942 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100943 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200944 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100945 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
946 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
947 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000948 * \param hash buffer holding the message digest
949 * \param sig buffer holding the ciphertext
950 *
951 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100952 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000953 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100954 * \note The \c sig buffer must be as large as the size
955 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000956 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200957 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000959 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200961 int (*f_rng)(void *, unsigned char *, size_t),
962 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000963 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000965 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000966 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200967 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000968
969/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100970 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
971 *
972 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100973 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200974 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100975 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
976 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
977 * for signing raw data)
978 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100979 * \param hash buffer holding the message digest
980 * \param sig buffer holding the ciphertext
981 *
982 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100983 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100984 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100985 * \note The \c sig buffer must be as large as the size
986 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100987 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200989 int (*f_rng)(void *, unsigned char *, size_t),
990 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100991 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100993 unsigned int hashlen,
994 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200995 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100996
997/**
998 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200999 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +01001000 *
1001 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +01001002 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +02001003 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +01001004 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
1005 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
1006 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +01001007 * \param hash buffer holding the message digest
1008 * \param sig buffer holding the ciphertext
1009 *
1010 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001011 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +01001012 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001013 * \note The \c sig buffer must be as large as the size
1014 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +01001015 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001016 * \note The \c hash_id in the RSA context is the one used for the
1017 * verification. \c md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001018 * hash that is verified. According to RFC 3447 it is advised to
Hanno Becker4b2f6912017-09-29 13:34:55 +01001019 * keep both hashes the same. If \c hash_id in the RSA context is
1020 * unset, the \c md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001023 int (*f_rng)(void *, unsigned char *, size_t),
1024 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001025 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001027 unsigned int hashlen,
1028 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001029 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001030
1031/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001032 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
1033 * (This is the version with "full" options.)
1034 *
1035 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +01001036 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001037 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +01001038 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
1039 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
1040 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001041 * \param hash buffer holding the message digest
1042 * \param mgf1_hash_id message digest used for mask generation
1043 * \param expected_salt_len Length of the salt used in padding, use
Hanno Becker4b2f6912017-09-29 13:34:55 +01001044 * \c MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001045 * \param sig buffer holding the ciphertext
1046 *
1047 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001048 * or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001049 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001050 * \note The \c sig buffer must be as large as the size
1051 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001052 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001053 * \note The \c hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001056 int (*f_rng)(void *, unsigned char *, size_t),
1057 void *p_rng,
1058 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001060 unsigned int hashlen,
1061 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001063 int expected_salt_len,
1064 const unsigned char *sig );
1065
1066/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001067 * \brief Copy the components of an RSA context
1068 *
1069 * \param dst Destination context
1070 * \param src Source context
1071 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001072 * \return 0 on success,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001073 * \c MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001076
1077/**
Paul Bakker5121ce52009-01-03 21:22:43 +00001078 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001079 *
1080 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001083
1084/**
1085 * \brief Checkup routine
1086 *
1087 * \return 0 if successful, or 1 if the test failed
1088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
1091#ifdef __cplusplus
1092}
1093#endif
1094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095#endif /* MBEDTLS_RSA_C */
Paul Bakkered27a042013-04-18 22:46:23 +02001096
Paul Bakker5121ce52009-01-03 21:22:43 +00001097#endif /* rsa.h */