blob: a7e8a3320d7caaae6ee7a8465b04eaaedd9bcc19 [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 Beckera3ebec22017-08-23 14:06:24 +0100102 */
Hanno Beckerba5b7552017-10-02 09:55:49 +0100103int mbedtls_rsa_deduce_moduli( mbedtls_mpi const *N, mbedtls_mpi const *D,
104 mbedtls_mpi const *E, int (*f_rng)(void *, unsigned char *, size_t),
105 void *p_rng, mbedtls_mpi *P, mbedtls_mpi *Q );
Hanno Beckera3ebec22017-08-23 14:06:24 +0100106
107/**
108 * \brief Compute RSA private exponent from
109 * prime moduli and public key.
110 *
111 * \note This is a 'static' helper function not operating on
112 * an RSA context. Alternative implementations need not
113 * overwrite it.
114 *
115 * \param P First prime factor of RSA modulus
116 * \param Q Second prime factor of RSA modulus
117 * \param E RSA public exponent
118 * \param D Pointer to MPI holding the private exponent on success.
119 *
120 * \note This function does not check whether P and Q are primes.
121 *
Hanno Beckered203612017-09-29 13:34:25 +0100122 * \return
123 * - 0 if successful. In this case, D is set to a simultaneous
Hanno Beckera3ebec22017-08-23 14:06:24 +0100124 * modular inverse of E modulo both P-1 and Q-1.
125 * - A non-zero error code otherwise. In this case, the values
126 * of P, Q, E are undefined.
127 *
128 * \note The input MPI's are deliberately not declared as constant
129 * and may therefore be used for in-place calculations by
130 * the implementation. In particular, their values can be
131 * corrupted when the function fails. If the user cannot
132 * tolerate this, he has to make copies of the MPI's prior
133 * to calling this function. See \c mbedtls_mpi_copy for this.
134 */
135int mbedtls_rsa_deduce_private( mbedtls_mpi *P, mbedtls_mpi *Q, mbedtls_mpi *E,
136 mbedtls_mpi *D );
137
138
139/**
140 * \brief Generate RSA-CRT parameters
141 *
142 * \note This is a 'static' helper function not operating on
143 * an RSA context. Alternative implementations need not
144 * overwrite it.
145 *
146 * \param P First prime factor of N
147 * \param Q Second prime factor of N
148 * \param D RSA private exponent
149 * \param DP Output variable for D modulo P-1
150 * \param DQ Output variable for D modulo Q-1
151 * \param QP Output variable for the modular inverse of Q modulo P.
152 *
153 * \return 0 on success, non-zero error code otherwise.
154 *
155 */
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
177 * - 0 if the following conditions are satisfied:
178 * - N = PQ if N,P,Q != NULL
179 * - D and E are modular inverses modulo P-1 and Q-1
180 * if D,E,P,Q != NULL
181 * - P prime if f_rng, P != NULL
182 * - Q prime if f_rng, Q != NULL
Hanno Becker750e8b42017-08-25 07:54:27 +0100183 * - A non-zero error code otherwise.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100184 *
185 * \note The function can be used with a restricted set of arguments
186 * to perform specific checks only. E.g., calling it with
187 * (-,P,-,-,-) and a PRNG amounts to a primality check for P.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100188 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100189int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
190 const mbedtls_mpi *Q, const mbedtls_mpi *D,
191 const mbedtls_mpi *E,
192 int (*f_rng)(void *, unsigned char *, size_t),
193 void *p_rng );
Hanno Beckera3ebec22017-08-23 14:06:24 +0100194
195/**
Hanno Beckerd3637992017-08-25 07:55:03 +0100196 * \brief Check validity of RSA CRT parameters
197 *
198 * \note This is a 'static' helper function not operating on
199 * an RSA context. Alternative implementations need not
200 * overwrite it.
201 *
202 * \param P First prime factor of RSA modulus
203 * \param Q Second prime factor of RSA modulus
204 * \param D RSA private exponent
205 * \param DP MPI to check for D modulo P-1
206 * \param DQ MPI to check for D modulo P-1
207 * \param QP MPI to check for the modular inverse of Q modulo P.
208 *
Hanno Beckered203612017-09-29 13:34:25 +0100209 * \return
210 * - 0 if the following conditions are satisfied:
211 * - D = DP mod P-1 if P, D, DP != NULL
212 * - Q = DQ mod P-1 if P, D, DQ != NULL
213 * - QP = Q^-1 mod P if P, Q, QP != NULL
214 * - \c MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if check failed,
215 * potentially including \c MBEDTLS_ERR_MPI_XXX if some
Hanno Beckerd3637992017-08-25 07:55:03 +0100216 * MPI calculations failed.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100217 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if insufficient
Hanno Beckerd3637992017-08-25 07:55:03 +0100218 * data was provided to check DP, DQ or QP.
219 *
220 * \note The function can be used with a restricted set of arguments
221 * to perform specific checks only. E.g., calling it with the
222 * parameters (P, -, D, DP, -, -) will check DP = D mod P-1.
223 */
224int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
225 const mbedtls_mpi *D, const mbedtls_mpi *DP,
226 const mbedtls_mpi *DQ, const mbedtls_mpi *QP );
227
228/**
Hanno Beckera3ebec22017-08-23 14:06:24 +0100229 * Implementation of RSA interface
230 */
231
Hanno Beckerab377312017-08-23 16:24:51 +0100232#if !defined(MBEDTLS_RSA_ALT)
233
Hanno Beckera3ebec22017-08-23 14:06:24 +0100234/**
Hanno Becker5063cd22017-09-29 11:49:12 +0100235 * \brief RSA context structure
236 *
237 * \note Direct manipulation of the members of this structure
238 * is deprecated and will no longer be supported starting
239 * from the next major release. All manipulation should instead
240 * be done through the public interface functions.
241 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 */
243typedef struct
244{
245 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +0000246 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_mpi N; /*!< public modulus */
249 mbedtls_mpi E; /*!< public exponent */
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_mpi D; /*!< private exponent */
252 mbedtls_mpi P; /*!< 1st prime factor */
253 mbedtls_mpi Q; /*!< 2nd prime factor */
Hanno Becker1a59e792017-08-23 07:41:10 +0100254
255#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_mpi DP; /*!< D % (P - 1) */
257 mbedtls_mpi DQ; /*!< D % (Q - 1) */
258 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Hanno Becker1a59e792017-08-23 07:41:10 +0100259#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_mpi RN; /*!< cached R^2 mod N */
Hanno Becker1a59e792017-08-23 07:41:10 +0100262
263#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 mbedtls_mpi RP; /*!< cached R^2 mod P */
265 mbedtls_mpi RQ; /*!< cached R^2 mod Q */
Hanno Becker1a59e792017-08-23 07:41:10 +0100266#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_mpi Vi; /*!< cached blinding value */
269 mbedtls_mpi Vf; /*!< cached un-blinding value */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200270
Hanno Becker4b2f6912017-09-29 13:34:55 +0100271 int padding; /*!< \c MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
272 \c MBEDTLS_RSA_PKCS_v21 for OAEP/PSS */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
274 specified in the mbedtls_md.h header file
Paul Bakker9dcc3222011-03-08 14:16:06 +0000275 for the EME-OAEP and EMSA-PSS
276 encoding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#if defined(MBEDTLS_THREADING_C)
278 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200279#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000280}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000282
Hanno Beckerab377312017-08-23 16:24:51 +0100283#else
284
285#include "rsa_alt.h"
286
287#endif /* MBEDTLS_RSA_ALT */
288
Paul Bakker5121ce52009-01-03 21:22:43 +0000289/**
290 * \brief Initialize an RSA context
291 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100292 * Note: Set padding to \c MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000293 * encryption scheme and the RSASSA-PSS signature scheme.
294 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 * \param ctx RSA context to be initialized
Hanno Becker4b2f6912017-09-29 13:34:55 +0100296 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
297 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 *
299 * \note The hash_id parameter is actually ignored
Hanno Becker4b2f6912017-09-29 13:34:55 +0100300 * when using \c MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200301 *
302 * \note Choice of padding mode is strictly enforced for private key
303 * operations, since there might be security concerns in
304 * mixing padding modes. For public key operations it's merely
305 * a default value, which can be overriden by calling specific
306 * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
307 *
308 * \note The chosen hash is always used for OEAP encryption.
309 * For PSS signatures, it's always used for making signatures,
310 * but can be overriden (and always is, if set to
Hanno Becker4b2f6912017-09-29 13:34:55 +0100311 * \c MBEDTLS_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100314 int padding,
315 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000316
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100317/**
318 * \brief Import a set of core parameters into an RSA context
319 *
320 * \param ctx Initialized RSA context to store parameters
321 * \param N RSA modulus, or NULL
322 * \param P First prime factor of N, or NULL
323 * \param Q Second prime factor of N, or NULL
324 * \param D Private exponent, or NULL
325 * \param E Public exponent, or NULL
326 *
327 * \note This function can be called multiple times for successive
328 * imports if the parameters are not simultaneously present.
329 * Any sequence of calls to this function should be followed
330 * by a call to \c mbedtls_rsa_complete which will check
331 * and complete the provided information to a ready-for-use
332 * public or private RSA key.
333 *
334 * \return 0 if successful, non-zero error code on failure.
335 */
336int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
337 const mbedtls_mpi *N,
338 const mbedtls_mpi *P, const mbedtls_mpi *Q,
339 const mbedtls_mpi *D, const mbedtls_mpi *E );
340
341/**
342 * \brief Import core RSA parameters in raw big-endian
343 * binary format into an RSA context
344 *
345 * \param ctx Initialized RSA context to store parameters
346 * \param N RSA modulus, or NULL
347 * \param N_len Byte length of N, ignored if N == NULL
348 * \param P First prime factor of N, or NULL
349 * \param P_len Byte length of P, ignored if P == NULL
350 * \param Q Second prime factor of N, or NULL
351 * \param Q_len Byte length of Q, ignored if Q == NULL
352 * \param D Private exponent, or NULL
353 * \param D_len Byte length of D, ignored if D == NULL
354 * \param E Public exponent, or NULL
355 * \param E_len Byte length of E, ignored if E == NULL
356 *
357 * \note This function can be called multiple times for successive
358 * imports if the parameters are not simultaneously present.
359 * Any sequence of calls to this function should be followed
360 * by a call to \c mbedtls_rsa_complete which will check
361 * and complete the provided information to a ready-for-use
362 * public or private RSA key.
363 *
364 * \return 0 if successful, non-zero error code on failure.
365 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100366int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
367 unsigned char *N, size_t N_len,
368 unsigned char *P, size_t P_len,
369 unsigned char *Q, size_t Q_len,
370 unsigned char *D, size_t D_len,
371 unsigned char *E, size_t E_len );
372
373/**
374 * \brief Attempt to complete an RSA context from
375 * a set of imported core parameters.
376 *
377 * \param ctx Initialized RSA context to store parameters
378 * \param f_rng RNG function,
379 * \param p_rng RNG parameter
380 *
Hanno Beckered203612017-09-29 13:34:25 +0100381 * \note
382 * - To setup an RSA public key, precisely N and E
383 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100384 *
Hanno Beckered203612017-09-29 13:34:25 +0100385 * - To setup an RSA private key, enough information must be
386 * present for the other parameters to be efficiently derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100387 *
Hanno Beckered203612017-09-29 13:34:25 +0100388 * The default implementation supports the following:
389 * - Derive P, Q from N, D, E
390 * - Derive N, D from P, Q, E.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100391 *
Hanno Beckered203612017-09-29 13:34:25 +0100392 * - Alternative implementations need not support these
393 * and may return \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA instead.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100394 *
395 * \note The PRNG is used for probabilistic algorithms
396 * like the derivation of P, Q from N, D, E, as
397 * well as primality checks.
398 *
Hanno Becker603b8c62017-08-25 11:03:07 +0100399 * \return - 0 if successful. In this case, all imported core
400 * parameters are guaranteed to be sane, the RSA context
401 * has been fully setup and is ready for use.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100402 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100403 * derivations failed.
404 */
405int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
406 int (*f_rng)(void *, unsigned char *, size_t),
407 void *p_rng );
408
409/**
410 * \brief Check if CRT-parameters match core parameters
411 *
412 * \param ctx Complete RSA private key context
413 * \param DP Private exponent modulo P-1, or NULL
414 * \param DQ Private exponent modulo Q-1, or NULL
415 * \param QP Modular inverse of Q modulo P, or NULL
416 *
417 * \return 0 if successful, testifying that the non-NULL optional
418 * parameters provided are in accordance with the core
419 * RSA parameters. Non-zero error code otherwise.
420 *
421 * \note This function performs in-place computations on the
422 * parameters DP, DQ and QP. If modification cannot be
423 * tolerated, you should make copies with mbedtls_mpi_copy
424 * before calling this function.
425 *
426 */
Hanno Beckerd3637992017-08-25 07:55:03 +0100427int mbedtls_rsa_check_crt( const mbedtls_rsa_context *ctx,
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100428 mbedtls_mpi *DP,
429 mbedtls_mpi *DQ,
430 mbedtls_mpi *QP );
431
432/**
433 * \brief Export core parameters of an RSA key
434 *
435 * \param ctx Initialized RSA context
436 * \param N MPI to hold the RSA modulus, or NULL
437 * \param P MPI to hold the first prime factor of N, or NULL
438 * \param Q MPI to hold the second prime factor of N, or NULL
439 * \param D MPI to hold the private exponent, or NULL
440 * \param E MPI to hold the public exponent, or NULL
441 *
Hanno Beckered203612017-09-29 13:34:25 +0100442 * \return
443 * - 0 if successful. In this case, the non-NULL buffers
444 * pointed to by N, P, Q, D, E are fully written, with
445 * additional unused space filled leading by 0-bytes.
446 * - Non-zero return code otherwise. In particular, if
447 * exporting the requested parameters
448 * cannot be done because of a lack of functionality
449 * or because of security policies, the error code
450 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
451 * In this case, the RSA context stays intact and can
452 * be continued to be used.
Hanno Becker91c194d2017-09-29 12:50:12 +0100453 *
Hanno Beckered203612017-09-29 13:34:25 +0100454 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100455 * would be the following: Firstly, it might be that an
456 * alternative RSA implementation is in use which stores
457 * the key externally, and which either cannot or should not
458 * export it into RAM. Alternatively, an implementation
459 * (regardless of SW or HW) might not support deducing e.g.
460 * P, Q from N, D, E if the former are not part of the
461 * implementation.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100462 *
463 */
464int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
465 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
466 mbedtls_mpi *D, mbedtls_mpi *E );
467
468/**
469 * \brief Export core parameters of an RSA key
470 * in raw big-endian binary format
471 *
472 * \param ctx Initialized RSA context
473 * \param N Byte array to store the RSA modulus, or NULL
474 * \param N_len Size of buffer for modulus
475 * \param P Byte array to hold the first prime factor of N, or NULL
476 * \param P_len Size of buffer for first prime factor
477 * \param Q Byte array to hold the second prime factor of N, or NULL
478 * \param Q_len Size of buffer for second prime factor
479 * \param D Byte array to hold the private exponent, or NULL
480 * \param D_len Size of buffer for private exponent
481 * \param E Byte array to hold the public exponent, or NULL
482 * \param E_len Size of buffer for public exponent
483 *
484 * \note The length fields are ignored if the corresponding
485 * buffer pointers are NULL.
486 *
Hanno Beckered203612017-09-29 13:34:25 +0100487 * \return
488 * - 0 if successful. In this case, the non-NULL buffers
489 * pointed to by N, P, Q, D, E are fully written, with
490 * additional unused space filled leading by 0-bytes.
491 * - Non-zero return code otherwise. In particular, if
492 * exporting the requested parameters
493 * cannot be done because of a lack of functionality
494 * or because of security policies, the error code
495 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
496 * In this case, the RSA context stays intact and can
497 * be continued to be used.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100498 *
Hanno Beckered203612017-09-29 13:34:25 +0100499 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100500 * would be the following: Firstly, it might be that an
501 * alternative RSA implementation is in use which stores
502 * the key externally, and which either cannot or should not
503 * export it into RAM. Alternatively, an implementation
504 * (regardless of SW or HW) might not support deducing e.g.
505 * P, Q from N, D, E if the former are not part of the
506 * implementation.
507 *
508 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100509 */
510int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
511 unsigned char *N, size_t N_len,
512 unsigned char *P, size_t P_len,
513 unsigned char *Q, size_t Q_len,
514 unsigned char *D, size_t D_len,
515 unsigned char *E, size_t E_len );
516
517/**
518 * \brief Export CRT parameters of a private RSA key
519 *
520 * \param ctx Initialized RSA context
521 * \param DP MPI to hold D modulo P-1, or NULL
522 * \param DQ MPI to hold D modulo Q-1, or NULL
523 * \param QP MPI to hold modular inverse of Q modulo P, or NULL
524 *
525 * \return 0 if successful, non-zero error code otherwise.
526 *
527 * \note Alternative RSA implementations not using CRT-parameters
528 * internally can implement this function using based on
529 * \c mbedtls_rsa_deduce_opt.
530 *
531 */
532int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
533 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
534
Paul Bakker5121ce52009-01-03 21:22:43 +0000535/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100536 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100538 *
539 * \param ctx RSA context to be set
Hanno Becker4b2f6912017-09-29 13:34:55 +0100540 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
541 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100542 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100543void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
544 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100545
546/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100547 * \brief Get length of RSA modulus in bytes
548 *
549 * \param ctx Initialized RSA context
550 *
551 * \return Length of RSA modulus, in bytes.
552 *
553 */
554size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
555
556/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 * \brief Generate an RSA keypair
558 *
559 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000560 * \param f_rng RNG function
561 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 * \param nbits size of the public key in bits
563 * \param exponent public exponent (e.g., 65537)
564 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000566 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100568 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100571 int (*f_rng)(void *, unsigned char *, size_t),
572 void *p_rng,
573 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
575/**
Hanno Becker8fd55482017-08-23 14:07:48 +0100576 * \brief Check if a context contains an RSA public key
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 *
578 * \param ctx RSA context to be checked
579 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100580 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
584/**
Hanno Becker8fd55482017-08-23 14:07:48 +0100585 * \brief Check if a context contains a complete
586 * and valid RSA private key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000587 *
588 * \param ctx RSA context to be checked
589 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100590 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000593
594/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100595 * \brief Check a public-private RSA key pair.
596 * Check each of the contexts, and make sure they match.
597 *
598 * \param pub RSA context holding the public key
599 * \param prv RSA context holding the private key
600 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100601 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100604
605/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000606 * \brief Do an RSA public key operation
607 *
608 * \param ctx RSA context
609 * \param input input buffer
610 * \param output output buffer
611 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100612 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 *
614 * \note This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800615 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000616 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 *
618 * \note The input and output buffers must be large
619 * enough (eg. 128 bytes if RSA-1024 is used).
620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000622 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 unsigned char *output );
624
625/**
626 * \brief Do an RSA private key operation
627 *
628 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200629 * \param f_rng RNG function (Needed for blinding)
630 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000631 * \param input input buffer
632 * \param output output buffer
633 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100634 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 *
636 * \note The input and output buffers must be large
637 * enough (eg. 128 bytes if RSA-1024 is used).
638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200640 int (*f_rng)(void *, unsigned char *, size_t),
641 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000642 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000643 unsigned char *output );
644
645/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100646 * \brief Generic wrapper to perform a PKCS#1 encryption using the
647 * mode from the context. Add the message padding, then do an
648 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 *
650 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200651 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100652 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000653 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100654 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000655 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000656 * \param input buffer holding the data to be encrypted
657 * \param output buffer that will hold the ciphertext
658 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100659 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000660 *
661 * \note The output buffer must be as large as the size
662 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000665 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000666 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000667 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000668 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 unsigned char *output );
670
671/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100672 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
673 *
674 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100675 * \param f_rng RNG function (Needed for padding and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100676 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100677 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100678 * \param ilen contains the plaintext length
679 * \param input buffer holding the data to be encrypted
680 * \param output buffer that will hold the ciphertext
681 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100682 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100683 *
684 * \note The output buffer must be as large as the size
685 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100688 int (*f_rng)(void *, unsigned char *, size_t),
689 void *p_rng,
690 int mode, size_t ilen,
691 const unsigned char *input,
692 unsigned char *output );
693
694/**
695 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
696 *
697 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200698 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100699 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100700 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100701 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100702 * \param label buffer holding the custom label to use
703 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100704 * \param ilen contains the plaintext length
705 * \param input buffer holding the data to be encrypted
706 * \param output buffer that will hold the ciphertext
707 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100708 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100709 *
710 * \note The output buffer must be as large as the size
711 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100714 int (*f_rng)(void *, unsigned char *, size_t),
715 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100716 int mode,
717 const unsigned char *label, size_t label_len,
718 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100719 const unsigned char *input,
720 unsigned char *output );
721
722/**
723 * \brief Generic wrapper to perform a PKCS#1 decryption using the
724 * mode from the context. Do an RSA operation, then remove
725 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000726 *
727 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100728 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200729 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100730 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000731 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000732 * \param input buffer holding the encrypted data
733 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000734 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000735 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100736 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000737 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100738 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100739 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100740 * if RSA-1024 is used) to be able to hold an arbitrary
741 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100742 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100743 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100744 *
745 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100746 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200749 int (*f_rng)(void *, unsigned char *, size_t),
750 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000751 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000752 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000753 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000754 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
756/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100757 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
758 *
759 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100760 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200761 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100762 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100763 * \param olen will contain the plaintext length
764 * \param input buffer holding the encrypted data
765 * \param output buffer that will hold the plaintext
766 * \param output_max_len maximum length of the output buffer
767 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100768 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100769 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100770 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100771 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100772 * if RSA-1024 is used) to be able to hold an arbitrary
773 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100774 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100775 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100776 *
777 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100778 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100779 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200781 int (*f_rng)(void *, unsigned char *, size_t),
782 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100783 int mode, size_t *olen,
784 const unsigned char *input,
785 unsigned char *output,
786 size_t output_max_len );
787
788/**
789 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
790 *
791 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100792 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200793 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100794 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100795 * \param label buffer holding the custom label to use
796 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100797 * \param olen will contain the plaintext length
798 * \param input buffer holding the encrypted data
799 * \param output buffer that will hold the plaintext
800 * \param output_max_len maximum length of the output buffer
801 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100802 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100803 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100804 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100805 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100806 * if RSA-1024 is used) to be able to hold an arbitrary
807 * decrypted message. If it is not large enough to hold
Hanno Becker8fd55482017-08-23 14:07:48 +0100808 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100809 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100810 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100811 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100812 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100813 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200815 int (*f_rng)(void *, unsigned char *, size_t),
816 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100817 int mode,
818 const unsigned char *label, size_t label_len,
819 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100820 const unsigned char *input,
821 unsigned char *output,
822 size_t output_max_len );
823
824/**
825 * \brief Generic wrapper to perform a PKCS#1 signature using the
826 * mode from the context. Do a private RSA operation to sign
827 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000828 *
829 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200830 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100831 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000832 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100833 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
834 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for
835 * signing raw data)
836 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000837 * \param hash buffer holding the message digest
838 * \param sig buffer that will hold the ciphertext
839 *
840 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100841 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000842 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100843 * \note The \c sig buffer must be as large as the size
844 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000845 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200846 * \note In case of PKCS#1 v2.1 encoding, see comments on
Hanno Becker4b2f6912017-09-29 13:34:55 +0100847 * \note \c mbedtls_rsa_rsassa_pss_sign() for details on
848 * \c md_alg and \c hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000851 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000852 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000853 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000855 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000856 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000857 unsigned char *sig );
858
859/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100860 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
861 *
862 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100863 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200864 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100865 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
866 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
867 * for signing raw data)
868 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100869 * \param hash buffer holding the message digest
870 * \param sig buffer that will hold the ciphertext
871 *
872 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100873 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100874 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100875 * \note The \c sig buffer must be as large as the size
876 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100877 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200879 int (*f_rng)(void *, unsigned char *, size_t),
880 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100881 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100883 unsigned int hashlen,
884 const unsigned char *hash,
885 unsigned char *sig );
886
887/**
888 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
889 *
890 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200891 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100892 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100893 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100894 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
895 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
896 * for signing raw data)
897 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100898 * \param hash buffer holding the message digest
899 * \param sig buffer that will hold the ciphertext
900 *
901 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100902 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100903 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100904 * \note The \c sig buffer must be as large as the size
905 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100906 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100907 * \note The \c hash_id in the RSA context is the one used for the
908 * encoding. \c md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100909 * that is encoded. According to RFC 3447 it is advised to
910 * keep both hashes the same.
911 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100913 int (*f_rng)(void *, unsigned char *, size_t),
914 void *p_rng,
915 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100917 unsigned int hashlen,
918 const unsigned char *hash,
919 unsigned char *sig );
920
921/**
922 * \brief Generic wrapper to perform a PKCS#1 verification using the
923 * mode from the context. Do a public RSA operation and check
924 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000925 *
926 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100927 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200928 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100929 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
930 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
931 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000932 * \param hash buffer holding the message digest
933 * \param sig buffer holding the ciphertext
934 *
935 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100936 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000937 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100938 * \note The \c sig buffer must be as large as the size
939 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000940 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200941 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000943 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200945 int (*f_rng)(void *, unsigned char *, size_t),
946 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000947 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000949 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000950 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200951 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000952
953/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100954 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
955 *
956 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100957 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200958 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100959 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
960 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
961 * for signing raw data)
962 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100963 * \param hash buffer holding the message digest
964 * \param sig buffer holding the ciphertext
965 *
966 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100967 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100968 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100969 * \note The \c sig buffer must be as large as the size
970 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100971 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200973 int (*f_rng)(void *, unsigned char *, size_t),
974 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100975 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100977 unsigned int hashlen,
978 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200979 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100980
981/**
982 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200983 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +0100984 *
985 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100986 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200987 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100988 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
989 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
990 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100991 * \param hash buffer holding the message digest
992 * \param sig buffer holding the ciphertext
993 *
994 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100995 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100996 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100997 * \note The \c sig buffer must be as large as the size
998 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100999 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001000 * \note The \c hash_id in the RSA context is the one used for the
1001 * verification. \c md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001002 * hash that is verified. According to RFC 3447 it is advised to
Hanno Becker4b2f6912017-09-29 13:34:55 +01001003 * keep both hashes the same. If \c hash_id in the RSA context is
1004 * unset, the \c md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001005 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001007 int (*f_rng)(void *, unsigned char *, size_t),
1008 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001009 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001011 unsigned int hashlen,
1012 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001013 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001014
1015/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001016 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
1017 * (This is the version with "full" options.)
1018 *
1019 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +01001020 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001021 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +01001022 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
1023 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
1024 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001025 * \param hash buffer holding the message digest
1026 * \param mgf1_hash_id message digest used for mask generation
1027 * \param expected_salt_len Length of the salt used in padding, use
Hanno Becker4b2f6912017-09-29 13:34:55 +01001028 * \c MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001029 * \param sig buffer holding the ciphertext
1030 *
1031 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001032 * or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001033 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001034 * \note The \c sig buffer must be as large as the size
1035 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001036 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001037 * \note The \c hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001038 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001040 int (*f_rng)(void *, unsigned char *, size_t),
1041 void *p_rng,
1042 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001044 unsigned int hashlen,
1045 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001047 int expected_salt_len,
1048 const unsigned char *sig );
1049
1050/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001051 * \brief Copy the components of an RSA context
1052 *
1053 * \param dst Destination context
1054 * \param src Source context
1055 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001056 * \return 0 on success,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001057 * \c MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001060
1061/**
Paul Bakker5121ce52009-01-03 21:22:43 +00001062 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001063 *
1064 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001067
1068/**
1069 * \brief Checkup routine
1070 *
1071 * \return 0 if successful, or 1 if the test failed
1072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
1075#ifdef __cplusplus
1076}
1077#endif
1078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079#endif /* MBEDTLS_RSA_C */
Paul Bakkered27a042013-04-18 22:46:23 +02001080
Paul Bakker5121ce52009-01-03 21:22:43 +00001081#endif /* rsa.h */