blob: b272b76a5cf51b182343a047a33316b3c77b0eaf [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 Beckera3ebec22017-08-23 14:06:24 +010099 * factorization of N, and it is guaranteed that D and E
100 * are indeed modular inverses modulo P-1 and modulo Q-1.
101 * The values of N, D and E are unchanged. It is checked
102 * that P, Q are prime if a PRNG is provided.
103 * - A non-zero error code otherwise. In this case, the values
104 * of N, D, E are undefined.
105 *
106 * \note The input MPI's are deliberately not declared as constant
107 * and may therefore be used for in-place calculations by
108 * the implementation. In particular, their values can be
109 * corrupted when the function fails. If the user cannot
110 * tolerate this, he has to make copies of the MPI's prior
111 * to calling this function. See \c mbedtls_mpi_copy for this.
112 */
113int mbedtls_rsa_deduce_moduli( mbedtls_mpi *N, mbedtls_mpi *D, mbedtls_mpi *E,
114 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
115 mbedtls_mpi *P, mbedtls_mpi *Q );
116
117/**
118 * \brief Compute RSA private exponent from
119 * prime moduli and public key.
120 *
121 * \note This is a 'static' helper function not operating on
122 * an RSA context. Alternative implementations need not
123 * overwrite it.
124 *
125 * \param P First prime factor of RSA modulus
126 * \param Q Second prime factor of RSA modulus
127 * \param E RSA public exponent
128 * \param D Pointer to MPI holding the private exponent on success.
129 *
130 * \note This function does not check whether P and Q are primes.
131 *
Hanno Beckered203612017-09-29 13:34:25 +0100132 * \return
133 * - 0 if successful. In this case, D is set to a simultaneous
Hanno Beckera3ebec22017-08-23 14:06:24 +0100134 * modular inverse of E modulo both P-1 and Q-1.
135 * - A non-zero error code otherwise. In this case, the values
136 * of P, Q, E are undefined.
137 *
138 * \note The input MPI's are deliberately not declared as constant
139 * and may therefore be used for in-place calculations by
140 * the implementation. In particular, their values can be
141 * corrupted when the function fails. If the user cannot
142 * tolerate this, he has to make copies of the MPI's prior
143 * to calling this function. See \c mbedtls_mpi_copy for this.
144 */
145int mbedtls_rsa_deduce_private( mbedtls_mpi *P, mbedtls_mpi *Q, mbedtls_mpi *E,
146 mbedtls_mpi *D );
147
148
149/**
150 * \brief Generate RSA-CRT parameters
151 *
152 * \note This is a 'static' helper function not operating on
153 * an RSA context. Alternative implementations need not
154 * overwrite it.
155 *
156 * \param P First prime factor of N
157 * \param Q Second prime factor of N
158 * \param D RSA private exponent
159 * \param DP Output variable for D modulo P-1
160 * \param DQ Output variable for D modulo Q-1
161 * \param QP Output variable for the modular inverse of Q modulo P.
162 *
163 * \return 0 on success, non-zero error code otherwise.
164 *
165 */
166int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
167 const mbedtls_mpi *D, mbedtls_mpi *DP,
168 mbedtls_mpi *DQ, mbedtls_mpi *QP );
169
170
171/**
172 * \brief Check validity of core RSA parameters
173 *
174 * \note This is a 'static' helper function not operating on
175 * an RSA context. Alternative implementations need not
176 * overwrite it.
177 *
178 * \param N RSA modulus N = PQ
179 * \param P First prime factor of N
180 * \param Q Second prime factor of N
181 * \param D RSA private exponent
182 * \param E RSA public exponent
183 * \param f_rng PRNG to be used for randomization, or NULL
184 * \param p_rng PRNG context for f_rng, or NULL
185 *
Hanno Beckered203612017-09-29 13:34:25 +0100186 * \return
187 * - 0 if the following conditions are satisfied:
188 * - N = PQ if N,P,Q != NULL
189 * - D and E are modular inverses modulo P-1 and Q-1
190 * if D,E,P,Q != NULL
191 * - P prime if f_rng, P != NULL
192 * - Q prime if f_rng, Q != NULL
Hanno Becker750e8b42017-08-25 07:54:27 +0100193 * - A non-zero error code otherwise.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100194 *
195 * \note The function can be used with a restricted set of arguments
196 * to perform specific checks only. E.g., calling it with
197 * (-,P,-,-,-) and a PRNG amounts to a primality check for P.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100198 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100199int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
200 const mbedtls_mpi *Q, const mbedtls_mpi *D,
201 const mbedtls_mpi *E,
202 int (*f_rng)(void *, unsigned char *, size_t),
203 void *p_rng );
Hanno Beckera3ebec22017-08-23 14:06:24 +0100204
205/**
Hanno Beckerd3637992017-08-25 07:55:03 +0100206 * \brief Check validity of RSA CRT parameters
207 *
208 * \note This is a 'static' helper function not operating on
209 * an RSA context. Alternative implementations need not
210 * overwrite it.
211 *
212 * \param P First prime factor of RSA modulus
213 * \param Q Second prime factor of RSA modulus
214 * \param D RSA private exponent
215 * \param DP MPI to check for D modulo P-1
216 * \param DQ MPI to check for D modulo P-1
217 * \param QP MPI to check for the modular inverse of Q modulo P.
218 *
Hanno Beckered203612017-09-29 13:34:25 +0100219 * \return
220 * - 0 if the following conditions are satisfied:
221 * - D = DP mod P-1 if P, D, DP != NULL
222 * - Q = DQ mod P-1 if P, D, DQ != NULL
223 * - QP = Q^-1 mod P if P, Q, QP != NULL
224 * - \c MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if check failed,
225 * potentially including \c MBEDTLS_ERR_MPI_XXX if some
Hanno Beckerd3637992017-08-25 07:55:03 +0100226 * MPI calculations failed.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100227 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if insufficient
Hanno Beckerd3637992017-08-25 07:55:03 +0100228 * data was provided to check DP, DQ or QP.
229 *
230 * \note The function can be used with a restricted set of arguments
231 * to perform specific checks only. E.g., calling it with the
232 * parameters (P, -, D, DP, -, -) will check DP = D mod P-1.
233 */
234int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
235 const mbedtls_mpi *D, const mbedtls_mpi *DP,
236 const mbedtls_mpi *DQ, const mbedtls_mpi *QP );
237
238/**
Hanno Beckera3ebec22017-08-23 14:06:24 +0100239 * Implementation of RSA interface
240 */
241
Hanno Beckerab377312017-08-23 16:24:51 +0100242#if !defined(MBEDTLS_RSA_ALT)
243
Hanno Beckera3ebec22017-08-23 14:06:24 +0100244/**
Hanno Becker5063cd22017-09-29 11:49:12 +0100245 * \brief RSA context structure
246 *
247 * \note Direct manipulation of the members of this structure
248 * is deprecated and will no longer be supported starting
249 * from the next major release. All manipulation should instead
250 * be done through the public interface functions.
251 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 */
253typedef struct
254{
255 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +0000256 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +0000257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 mbedtls_mpi N; /*!< public modulus */
259 mbedtls_mpi E; /*!< public exponent */
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_mpi D; /*!< private exponent */
262 mbedtls_mpi P; /*!< 1st prime factor */
263 mbedtls_mpi Q; /*!< 2nd prime factor */
Hanno Becker1a59e792017-08-23 07:41:10 +0100264
265#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 mbedtls_mpi DP; /*!< D % (P - 1) */
267 mbedtls_mpi DQ; /*!< D % (Q - 1) */
268 mbedtls_mpi QP; /*!< 1 / (Q % P) */
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 RN; /*!< cached R^2 mod N */
Hanno Becker1a59e792017-08-23 07:41:10 +0100272
273#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 mbedtls_mpi RP; /*!< cached R^2 mod P */
275 mbedtls_mpi RQ; /*!< cached R^2 mod Q */
Hanno Becker1a59e792017-08-23 07:41:10 +0100276#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_mpi Vi; /*!< cached blinding value */
279 mbedtls_mpi Vf; /*!< cached un-blinding value */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200280
Hanno Becker4b2f6912017-09-29 13:34:55 +0100281 int padding; /*!< \c MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
282 \c MBEDTLS_RSA_PKCS_v21 for OAEP/PSS */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
284 specified in the mbedtls_md.h header file
Paul Bakker9dcc3222011-03-08 14:16:06 +0000285 for the EME-OAEP and EMSA-PSS
286 encoding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287#if defined(MBEDTLS_THREADING_C)
288 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200289#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000290}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
Hanno Beckerab377312017-08-23 16:24:51 +0100293#else
294
295#include "rsa_alt.h"
296
297#endif /* MBEDTLS_RSA_ALT */
298
Paul Bakker5121ce52009-01-03 21:22:43 +0000299/**
300 * \brief Initialize an RSA context
301 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100302 * Note: Set padding to \c MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000303 * encryption scheme and the RSASSA-PSS signature scheme.
304 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 * \param ctx RSA context to be initialized
Hanno Becker4b2f6912017-09-29 13:34:55 +0100306 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
307 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000308 *
309 * \note The hash_id parameter is actually ignored
Hanno Becker4b2f6912017-09-29 13:34:55 +0100310 * when using \c MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200311 *
312 * \note Choice of padding mode is strictly enforced for private key
313 * operations, since there might be security concerns in
314 * mixing padding modes. For public key operations it's merely
315 * a default value, which can be overriden by calling specific
316 * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
317 *
318 * \note The chosen hash is always used for OEAP encryption.
319 * For PSS signatures, it's always used for making signatures,
320 * but can be overriden (and always is, if set to
Hanno Becker4b2f6912017-09-29 13:34:55 +0100321 * \c MBEDTLS_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100324 int padding,
325 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000326
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100327
328/**
329 * \brief Import a set of core parameters into an RSA context
330 *
331 * \param ctx Initialized RSA context to store parameters
332 * \param N RSA modulus, or NULL
333 * \param P First prime factor of N, or NULL
334 * \param Q Second prime factor of N, or NULL
335 * \param D Private exponent, or NULL
336 * \param E Public exponent, or NULL
337 *
338 * \note This function can be called multiple times for successive
339 * imports if the parameters are not simultaneously present.
340 * Any sequence of calls to this function should be followed
341 * by a call to \c mbedtls_rsa_complete which will check
342 * and complete the provided information to a ready-for-use
343 * public or private RSA key.
344 *
345 * \return 0 if successful, non-zero error code on failure.
346 */
347int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
348 const mbedtls_mpi *N,
349 const mbedtls_mpi *P, const mbedtls_mpi *Q,
350 const mbedtls_mpi *D, const mbedtls_mpi *E );
351
352/**
353 * \brief Import core RSA parameters in raw big-endian
354 * binary format into an RSA context
355 *
356 * \param ctx Initialized RSA context to store parameters
357 * \param N RSA modulus, or NULL
358 * \param N_len Byte length of N, ignored if N == NULL
359 * \param P First prime factor of N, or NULL
360 * \param P_len Byte length of P, ignored if P == NULL
361 * \param Q Second prime factor of N, or NULL
362 * \param Q_len Byte length of Q, ignored if Q == NULL
363 * \param D Private exponent, or NULL
364 * \param D_len Byte length of D, ignored if D == NULL
365 * \param E Public exponent, or NULL
366 * \param E_len Byte length of E, ignored if E == NULL
367 *
368 * \note This function can be called multiple times for successive
369 * imports if the parameters are not simultaneously present.
370 * Any sequence of calls to this function should be followed
371 * by a call to \c mbedtls_rsa_complete which will check
372 * and complete the provided information to a ready-for-use
373 * public or private RSA key.
374 *
375 * \return 0 if successful, non-zero error code on failure.
376 */
377
378int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
379 unsigned char *N, size_t N_len,
380 unsigned char *P, size_t P_len,
381 unsigned char *Q, size_t Q_len,
382 unsigned char *D, size_t D_len,
383 unsigned char *E, size_t E_len );
384
385/**
386 * \brief Attempt to complete an RSA context from
387 * a set of imported core parameters.
388 *
389 * \param ctx Initialized RSA context to store parameters
390 * \param f_rng RNG function,
391 * \param p_rng RNG parameter
392 *
Hanno Beckered203612017-09-29 13:34:25 +0100393 * \note
394 * - To setup an RSA public key, precisely N and E
395 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100396 *
Hanno Beckered203612017-09-29 13:34:25 +0100397 * - To setup an RSA private key, enough information must be
398 * present for the other parameters to be efficiently derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100399 *
Hanno Beckered203612017-09-29 13:34:25 +0100400 * The default implementation supports the following:
401 * - Derive P, Q from N, D, E
402 * - Derive N, D from P, Q, E.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100403 *
Hanno Beckered203612017-09-29 13:34:25 +0100404 * - Alternative implementations need not support these
405 * and may return \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA instead.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100406 *
407 * \note The PRNG is used for probabilistic algorithms
408 * like the derivation of P, Q from N, D, E, as
409 * well as primality checks.
410 *
Hanno Becker603b8c62017-08-25 11:03:07 +0100411 * \return - 0 if successful. In this case, all imported core
412 * parameters are guaranteed to be sane, the RSA context
413 * has been fully setup and is ready for use.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100414 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100415 * derivations failed.
416 */
417int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
418 int (*f_rng)(void *, unsigned char *, size_t),
419 void *p_rng );
420
421/**
422 * \brief Check if CRT-parameters match core parameters
423 *
424 * \param ctx Complete RSA private key context
425 * \param DP Private exponent modulo P-1, or NULL
426 * \param DQ Private exponent modulo Q-1, or NULL
427 * \param QP Modular inverse of Q modulo P, or NULL
428 *
429 * \return 0 if successful, testifying that the non-NULL optional
430 * parameters provided are in accordance with the core
431 * RSA parameters. Non-zero error code otherwise.
432 *
433 * \note This function performs in-place computations on the
434 * parameters DP, DQ and QP. If modification cannot be
435 * tolerated, you should make copies with mbedtls_mpi_copy
436 * before calling this function.
437 *
438 */
Hanno Beckerd3637992017-08-25 07:55:03 +0100439int mbedtls_rsa_check_crt( const mbedtls_rsa_context *ctx,
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100440 mbedtls_mpi *DP,
441 mbedtls_mpi *DQ,
442 mbedtls_mpi *QP );
443
444/**
445 * \brief Export core parameters of an RSA key
446 *
447 * \param ctx Initialized RSA context
448 * \param N MPI to hold the RSA modulus, or NULL
449 * \param P MPI to hold the first prime factor of N, or NULL
450 * \param Q MPI to hold the second prime factor of N, or NULL
451 * \param D MPI to hold the private exponent, or NULL
452 * \param E MPI to hold the public exponent, or NULL
453 *
Hanno Beckered203612017-09-29 13:34:25 +0100454 * \return
455 * - 0 if successful. In this case, the non-NULL buffers
456 * pointed to by N, P, Q, D, E are fully written, with
457 * additional unused space filled leading by 0-bytes.
458 * - Non-zero return code otherwise. In particular, if
459 * exporting the requested parameters
460 * cannot be done because of a lack of functionality
461 * or because of security policies, the error code
462 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
463 * In this case, the RSA context stays intact and can
464 * be continued to be used.
Hanno Becker91c194d2017-09-29 12:50:12 +0100465 *
Hanno Beckered203612017-09-29 13:34:25 +0100466 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100467 * would be the following: Firstly, it might be that an
468 * alternative RSA implementation is in use which stores
469 * the key externally, and which either cannot or should not
470 * export it into RAM. Alternatively, an implementation
471 * (regardless of SW or HW) might not support deducing e.g.
472 * P, Q from N, D, E if the former are not part of the
473 * implementation.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100474 *
475 */
476int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
477 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
478 mbedtls_mpi *D, mbedtls_mpi *E );
479
480/**
481 * \brief Export core parameters of an RSA key
482 * in raw big-endian binary format
483 *
484 * \param ctx Initialized RSA context
485 * \param N Byte array to store the RSA modulus, or NULL
486 * \param N_len Size of buffer for modulus
487 * \param P Byte array to hold the first prime factor of N, or NULL
488 * \param P_len Size of buffer for first prime factor
489 * \param Q Byte array to hold the second prime factor of N, or NULL
490 * \param Q_len Size of buffer for second prime factor
491 * \param D Byte array to hold the private exponent, or NULL
492 * \param D_len Size of buffer for private exponent
493 * \param E Byte array to hold the public exponent, or NULL
494 * \param E_len Size of buffer for public exponent
495 *
496 * \note The length fields are ignored if the corresponding
497 * buffer pointers are NULL.
498 *
Hanno Beckered203612017-09-29 13:34:25 +0100499 * \return
500 * - 0 if successful. In this case, the non-NULL buffers
501 * pointed to by N, P, Q, D, E are fully written, with
502 * additional unused space filled leading by 0-bytes.
503 * - Non-zero return code otherwise. In particular, if
504 * exporting the requested parameters
505 * cannot be done because of a lack of functionality
506 * or because of security policies, the error code
507 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
508 * In this case, the RSA context stays intact and can
509 * be continued to be used.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100510 *
Hanno Beckered203612017-09-29 13:34:25 +0100511 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100512 * would be the following: Firstly, it might be that an
513 * alternative RSA implementation is in use which stores
514 * the key externally, and which either cannot or should not
515 * export it into RAM. Alternatively, an implementation
516 * (regardless of SW or HW) might not support deducing e.g.
517 * P, Q from N, D, E if the former are not part of the
518 * implementation.
519 *
520 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100521 */
522int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
523 unsigned char *N, size_t N_len,
524 unsigned char *P, size_t P_len,
525 unsigned char *Q, size_t Q_len,
526 unsigned char *D, size_t D_len,
527 unsigned char *E, size_t E_len );
528
529/**
530 * \brief Export CRT parameters of a private RSA key
531 *
532 * \param ctx Initialized RSA context
533 * \param DP MPI to hold D modulo P-1, or NULL
534 * \param DQ MPI to hold D modulo Q-1, or NULL
535 * \param QP MPI to hold modular inverse of Q modulo P, or NULL
536 *
537 * \return 0 if successful, non-zero error code otherwise.
538 *
539 * \note Alternative RSA implementations not using CRT-parameters
540 * internally can implement this function using based on
541 * \c mbedtls_rsa_deduce_opt.
542 *
543 */
544int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
545 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
546
Paul Bakker5121ce52009-01-03 21:22:43 +0000547/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100548 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100550 *
551 * \param ctx RSA context to be set
Hanno Becker4b2f6912017-09-29 13:34:55 +0100552 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
553 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100554 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100555void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
556 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100557
558/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100559 * \brief Get length of RSA modulus in bytes
560 *
561 * \param ctx Initialized RSA context
562 *
563 * \return Length of RSA modulus, in bytes.
564 *
565 */
566size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
567
568/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 * \brief Generate an RSA keypair
570 *
571 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000572 * \param f_rng RNG function
573 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 * \param nbits size of the public key in bits
575 * \param exponent public exponent (e.g., 65537)
576 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000578 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 *
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_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100583 int (*f_rng)(void *, unsigned char *, size_t),
584 void *p_rng,
585 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
587/**
Hanno Becker8fd55482017-08-23 14:07:48 +0100588 * \brief Check if a context contains an RSA public key
Paul Bakker5121ce52009-01-03 21:22:43 +0000589 *
590 * \param ctx RSA context to be checked
591 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100592 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000595
596/**
Hanno Becker8fd55482017-08-23 14:07:48 +0100597 * \brief Check if a context contains a complete
598 * and valid RSA private key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 *
600 * \param ctx RSA context to be checked
601 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100602 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000605
606/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100607 * \brief Check a public-private RSA key pair.
608 * Check each of the contexts, and make sure they match.
609 *
610 * \param pub RSA context holding the public key
611 * \param prv RSA context holding the private key
612 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100613 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100616
617/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 * \brief Do an RSA public key operation
619 *
620 * \param ctx RSA context
621 * \param input input buffer
622 * \param output output buffer
623 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100624 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000625 *
626 * \note This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800627 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000628 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000629 *
630 * \note The input and output buffers must be large
631 * enough (eg. 128 bytes if RSA-1024 is used).
632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000634 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 unsigned char *output );
636
637/**
638 * \brief Do an RSA private key operation
639 *
640 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200641 * \param f_rng RNG function (Needed for blinding)
642 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000643 * \param input input buffer
644 * \param output output buffer
645 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100646 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 *
648 * \note The input and output buffers must be large
649 * enough (eg. 128 bytes if RSA-1024 is used).
650 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200652 int (*f_rng)(void *, unsigned char *, size_t),
653 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000654 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 unsigned char *output );
656
657/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100658 * \brief Generic wrapper to perform a PKCS#1 encryption using the
659 * mode from the context. Add the message padding, then do an
660 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 *
662 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200663 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100664 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000665 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100666 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000667 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 * \param input buffer holding the data to be encrypted
669 * \param output buffer that will hold the ciphertext
670 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100671 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 *
673 * \note The output buffer must be as large as the size
674 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000677 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000678 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000679 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000680 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 unsigned char *output );
682
683/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100684 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
685 *
686 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100687 * \param f_rng RNG function (Needed for padding and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100688 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100689 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100690 * \param ilen contains the plaintext length
691 * \param input buffer holding the data to be encrypted
692 * \param output buffer that will hold the ciphertext
693 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100694 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100695 *
696 * \note The output buffer must be as large as the size
697 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
698 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100700 int (*f_rng)(void *, unsigned char *, size_t),
701 void *p_rng,
702 int mode, size_t ilen,
703 const unsigned char *input,
704 unsigned char *output );
705
706/**
707 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
708 *
709 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200710 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100711 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100712 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100713 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100714 * \param label buffer holding the custom label to use
715 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100716 * \param ilen contains the plaintext length
717 * \param input buffer holding the data to be encrypted
718 * \param output buffer that will hold the ciphertext
719 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100720 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100721 *
722 * \note The output buffer must be as large as the size
723 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100726 int (*f_rng)(void *, unsigned char *, size_t),
727 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100728 int mode,
729 const unsigned char *label, size_t label_len,
730 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100731 const unsigned char *input,
732 unsigned char *output );
733
734/**
735 * \brief Generic wrapper to perform a PKCS#1 decryption using the
736 * mode from the context. Do an RSA operation, then remove
737 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000738 *
739 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100740 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200741 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100742 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000743 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000744 * \param input buffer holding the encrypted data
745 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000746 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000747 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100748 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000749 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100750 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100751 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100752 * if RSA-1024 is used) to be able to hold an arbitrary
753 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100754 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100755 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100756 *
757 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100758 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000759 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200761 int (*f_rng)(void *, unsigned char *, size_t),
762 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000763 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000764 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000765 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000766 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000767
768/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100769 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
770 *
771 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100772 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200773 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100774 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100775 * \param olen will contain the plaintext length
776 * \param input buffer holding the encrypted data
777 * \param output buffer that will hold the plaintext
778 * \param output_max_len maximum length of the output buffer
779 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100780 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100781 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100782 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100783 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100784 * if RSA-1024 is used) to be able to hold an arbitrary
785 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100786 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100787 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100788 *
789 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100790 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100791 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200793 int (*f_rng)(void *, unsigned char *, size_t),
794 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100795 int mode, size_t *olen,
796 const unsigned char *input,
797 unsigned char *output,
798 size_t output_max_len );
799
800/**
801 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
802 *
803 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100804 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200805 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100806 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100807 * \param label buffer holding the custom label to use
808 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100809 * \param olen will contain the plaintext length
810 * \param input buffer holding the encrypted data
811 * \param output buffer that will hold the plaintext
812 * \param output_max_len maximum length of the output buffer
813 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100814 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100815 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100816 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100817 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100818 * if RSA-1024 is used) to be able to hold an arbitrary
819 * decrypted message. If it is not large enough to hold
Hanno Becker8fd55482017-08-23 14:07:48 +0100820 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100821 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100822 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100823 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100824 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200827 int (*f_rng)(void *, unsigned char *, size_t),
828 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100829 int mode,
830 const unsigned char *label, size_t label_len,
831 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100832 const unsigned char *input,
833 unsigned char *output,
834 size_t output_max_len );
835
836/**
837 * \brief Generic wrapper to perform a PKCS#1 signature using the
838 * mode from the context. Do a private RSA operation to sign
839 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000840 *
841 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200842 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100843 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000844 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100845 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
846 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for
847 * signing raw data)
848 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000849 * \param hash buffer holding the message digest
850 * \param sig buffer that will hold the ciphertext
851 *
852 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100853 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000854 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100855 * \note The \c sig buffer must be as large as the size
856 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000857 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200858 * \note In case of PKCS#1 v2.1 encoding, see comments on
Hanno Becker4b2f6912017-09-29 13:34:55 +0100859 * \note \c mbedtls_rsa_rsassa_pss_sign() for details on
860 * \c md_alg and \c hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000863 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000864 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000865 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000867 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000868 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 unsigned char *sig );
870
871/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100872 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
873 *
874 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100875 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200876 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100877 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
878 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
879 * for signing raw data)
880 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100881 * \param hash buffer holding the message digest
882 * \param sig buffer that will hold the ciphertext
883 *
884 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100885 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100886 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100887 * \note The \c sig buffer must be as large as the size
888 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200891 int (*f_rng)(void *, unsigned char *, size_t),
892 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100893 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100895 unsigned int hashlen,
896 const unsigned char *hash,
897 unsigned char *sig );
898
899/**
900 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
901 *
902 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200903 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100904 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100905 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100906 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
907 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
908 * for signing raw data)
909 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100910 * \param hash buffer holding the message digest
911 * \param sig buffer that will hold the ciphertext
912 *
913 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100914 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100915 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100916 * \note The \c sig buffer must be as large as the size
917 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100918 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100919 * \note The \c hash_id in the RSA context is the one used for the
920 * encoding. \c md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100921 * that is encoded. According to RFC 3447 it is advised to
922 * keep both hashes the same.
923 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100925 int (*f_rng)(void *, unsigned char *, size_t),
926 void *p_rng,
927 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100929 unsigned int hashlen,
930 const unsigned char *hash,
931 unsigned char *sig );
932
933/**
934 * \brief Generic wrapper to perform a PKCS#1 verification using the
935 * mode from the context. Do a public RSA operation and check
936 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000937 *
938 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100939 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200940 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100941 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
942 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
943 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000944 * \param hash buffer holding the message digest
945 * \param sig buffer holding the ciphertext
946 *
947 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100948 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000949 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100950 * \note The \c sig buffer must be as large as the size
951 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000952 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200953 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200957 int (*f_rng)(void *, unsigned char *, size_t),
958 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000959 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000961 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000962 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200963 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000964
965/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100966 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
967 *
968 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100969 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200970 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100971 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
972 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
973 * for signing raw data)
974 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100975 * \param hash buffer holding the message digest
976 * \param sig buffer holding the ciphertext
977 *
978 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100979 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100980 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100981 * \note The \c sig buffer must be as large as the size
982 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100983 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200985 int (*f_rng)(void *, unsigned char *, size_t),
986 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100987 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100989 unsigned int hashlen,
990 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200991 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100992
993/**
994 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200995 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +0100996 *
997 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100998 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200999 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +01001000 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
1001 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
1002 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +01001003 * \param hash buffer holding the message digest
1004 * \param sig buffer holding the ciphertext
1005 *
1006 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001007 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +01001008 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001009 * \note The \c sig buffer must be as large as the size
1010 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +01001011 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001012 * \note The \c hash_id in the RSA context is the one used for the
1013 * verification. \c md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001014 * hash that is verified. According to RFC 3447 it is advised to
Hanno Becker4b2f6912017-09-29 13:34:55 +01001015 * keep both hashes the same. If \c hash_id in the RSA context is
1016 * unset, the \c md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001017 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001019 int (*f_rng)(void *, unsigned char *, size_t),
1020 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001021 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001023 unsigned int hashlen,
1024 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001025 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001026
1027/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001028 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
1029 * (This is the version with "full" options.)
1030 *
1031 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +01001032 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001033 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +01001034 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
1035 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
1036 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001037 * \param hash buffer holding the message digest
1038 * \param mgf1_hash_id message digest used for mask generation
1039 * \param expected_salt_len Length of the salt used in padding, use
Hanno Becker4b2f6912017-09-29 13:34:55 +01001040 * \c MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001041 * \param sig buffer holding the ciphertext
1042 *
1043 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001044 * or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001045 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001046 * \note The \c sig buffer must be as large as the size
1047 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001048 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001049 * \note The \c hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001052 int (*f_rng)(void *, unsigned char *, size_t),
1053 void *p_rng,
1054 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001056 unsigned int hashlen,
1057 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001059 int expected_salt_len,
1060 const unsigned char *sig );
1061
1062/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001063 * \brief Copy the components of an RSA context
1064 *
1065 * \param dst Destination context
1066 * \param src Source context
1067 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001068 * \return 0 on success,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001069 * \c MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001070 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001072
1073/**
Paul Bakker5121ce52009-01-03 21:22:43 +00001074 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001075 *
1076 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +00001077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001079
1080/**
1081 * \brief Checkup routine
1082 *
1083 * \return 0 if successful, or 1 if the test failed
1084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001086
1087#ifdef __cplusplus
1088}
1089#endif
1090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091#endif /* MBEDTLS_RSA_C */
Paul Bakkered27a042013-04-18 22:46:23 +02001092
Paul Bakker5121ce52009-01-03 21:22:43 +00001093#endif /* rsa.h */