blob: 14cdef8d5caa64627d22f507154be5d1f0a10463 [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
Hanno Becker56bae952017-09-29 15:33:10 +0100183 * \param f_rng PRNG to be used for primality check, or NULL
Hanno Beckera3ebec22017-08-23 14:06:24 +0100184 * \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 * \brief Import a set of core parameters into an RSA context
329 *
330 * \param ctx Initialized RSA context to store parameters
331 * \param N RSA modulus, or NULL
332 * \param P First prime factor of N, or NULL
333 * \param Q Second prime factor of N, or NULL
334 * \param D Private exponent, or NULL
335 * \param E Public exponent, or NULL
336 *
337 * \note This function can be called multiple times for successive
338 * imports if the parameters are not simultaneously present.
339 * Any sequence of calls to this function should be followed
340 * by a call to \c mbedtls_rsa_complete which will check
341 * and complete the provided information to a ready-for-use
342 * public or private RSA key.
343 *
344 * \return 0 if successful, non-zero error code on failure.
345 */
346int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
347 const mbedtls_mpi *N,
348 const mbedtls_mpi *P, const mbedtls_mpi *Q,
349 const mbedtls_mpi *D, const mbedtls_mpi *E );
350
351/**
352 * \brief Import core RSA parameters in raw big-endian
353 * binary format into an RSA context
354 *
355 * \param ctx Initialized RSA context to store parameters
356 * \param N RSA modulus, or NULL
357 * \param N_len Byte length of N, ignored if N == NULL
358 * \param P First prime factor of N, or NULL
359 * \param P_len Byte length of P, ignored if P == NULL
360 * \param Q Second prime factor of N, or NULL
361 * \param Q_len Byte length of Q, ignored if Q == NULL
362 * \param D Private exponent, or NULL
363 * \param D_len Byte length of D, ignored if D == NULL
364 * \param E Public exponent, or NULL
365 * \param E_len Byte length of E, ignored if E == NULL
366 *
367 * \note This function can be called multiple times for successive
368 * imports if the parameters are not simultaneously present.
369 * Any sequence of calls to this function should be followed
370 * by a call to \c mbedtls_rsa_complete which will check
371 * and complete the provided information to a ready-for-use
372 * public or private RSA key.
373 *
374 * \return 0 if successful, non-zero error code on failure.
375 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100376int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
377 unsigned char *N, size_t N_len,
378 unsigned char *P, size_t P_len,
379 unsigned char *Q, size_t Q_len,
380 unsigned char *D, size_t D_len,
381 unsigned char *E, size_t E_len );
382
383/**
384 * \brief Attempt to complete an RSA context from
385 * a set of imported core parameters.
386 *
387 * \param ctx Initialized RSA context to store parameters
388 * \param f_rng RNG function,
389 * \param p_rng RNG parameter
390 *
Hanno Beckered203612017-09-29 13:34:25 +0100391 * \note
392 * - To setup an RSA public key, precisely N and E
393 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100394 *
Hanno Beckered203612017-09-29 13:34:25 +0100395 * - To setup an RSA private key, enough information must be
396 * present for the other parameters to be efficiently derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100397 *
Hanno Beckered203612017-09-29 13:34:25 +0100398 * The default implementation supports the following:
399 * - Derive P, Q from N, D, E
400 * - Derive N, D from P, Q, E.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100401 *
Hanno Beckered203612017-09-29 13:34:25 +0100402 * - Alternative implementations need not support these
403 * and may return \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA instead.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100404 *
405 * \note The PRNG is used for probabilistic algorithms
406 * like the derivation of P, Q from N, D, E, as
407 * well as primality checks.
408 *
Hanno Becker603b8c62017-08-25 11:03:07 +0100409 * \return - 0 if successful. In this case, all imported core
410 * parameters are guaranteed to be sane, the RSA context
411 * has been fully setup and is ready for use.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100412 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100413 * derivations failed.
414 */
415int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
416 int (*f_rng)(void *, unsigned char *, size_t),
417 void *p_rng );
418
419/**
420 * \brief Check if CRT-parameters match core parameters
421 *
422 * \param ctx Complete RSA private key context
423 * \param DP Private exponent modulo P-1, or NULL
424 * \param DQ Private exponent modulo Q-1, or NULL
425 * \param QP Modular inverse of Q modulo P, or NULL
426 *
427 * \return 0 if successful, testifying that the non-NULL optional
428 * parameters provided are in accordance with the core
429 * RSA parameters. Non-zero error code otherwise.
430 *
431 * \note This function performs in-place computations on the
432 * parameters DP, DQ and QP. If modification cannot be
433 * tolerated, you should make copies with mbedtls_mpi_copy
434 * before calling this function.
435 *
436 */
Hanno Beckerd3637992017-08-25 07:55:03 +0100437int mbedtls_rsa_check_crt( const mbedtls_rsa_context *ctx,
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100438 mbedtls_mpi *DP,
439 mbedtls_mpi *DQ,
440 mbedtls_mpi *QP );
441
442/**
443 * \brief Export core parameters of an RSA key
444 *
445 * \param ctx Initialized RSA context
446 * \param N MPI to hold the RSA modulus, or NULL
447 * \param P MPI to hold the first prime factor of N, or NULL
448 * \param Q MPI to hold the second prime factor of N, or NULL
449 * \param D MPI to hold the private exponent, or NULL
450 * \param E MPI to hold the public exponent, or NULL
451 *
Hanno Beckered203612017-09-29 13:34:25 +0100452 * \return
453 * - 0 if successful. In this case, the non-NULL buffers
454 * pointed to by N, P, Q, D, E are fully written, with
455 * additional unused space filled leading by 0-bytes.
456 * - Non-zero return code otherwise. In particular, if
457 * exporting the requested parameters
458 * cannot be done because of a lack of functionality
459 * or because of security policies, the error code
460 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
461 * In this case, the RSA context stays intact and can
462 * be continued to be used.
Hanno Becker91c194d2017-09-29 12:50:12 +0100463 *
Hanno Beckered203612017-09-29 13:34:25 +0100464 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100465 * would be the following: Firstly, it might be that an
466 * alternative RSA implementation is in use which stores
467 * the key externally, and which either cannot or should not
468 * export it into RAM. Alternatively, an implementation
469 * (regardless of SW or HW) might not support deducing e.g.
470 * P, Q from N, D, E if the former are not part of the
471 * implementation.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100472 *
473 */
474int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
475 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
476 mbedtls_mpi *D, mbedtls_mpi *E );
477
478/**
479 * \brief Export core parameters of an RSA key
480 * in raw big-endian binary format
481 *
482 * \param ctx Initialized RSA context
483 * \param N Byte array to store the RSA modulus, or NULL
484 * \param N_len Size of buffer for modulus
485 * \param P Byte array to hold the first prime factor of N, or NULL
486 * \param P_len Size of buffer for first prime factor
487 * \param Q Byte array to hold the second prime factor of N, or NULL
488 * \param Q_len Size of buffer for second prime factor
489 * \param D Byte array to hold the private exponent, or NULL
490 * \param D_len Size of buffer for private exponent
491 * \param E Byte array to hold the public exponent, or NULL
492 * \param E_len Size of buffer for public exponent
493 *
494 * \note The length fields are ignored if the corresponding
495 * buffer pointers are NULL.
496 *
Hanno Beckered203612017-09-29 13:34:25 +0100497 * \return
498 * - 0 if successful. In this case, the non-NULL buffers
499 * pointed to by N, P, Q, D, E are fully written, with
500 * additional unused space filled leading by 0-bytes.
501 * - Non-zero return code otherwise. In particular, if
502 * exporting the requested parameters
503 * cannot be done because of a lack of functionality
504 * or because of security policies, the error code
505 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
506 * In this case, the RSA context stays intact and can
507 * be continued to be used.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100508 *
Hanno Beckered203612017-09-29 13:34:25 +0100509 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100510 * would be the following: Firstly, it might be that an
511 * alternative RSA implementation is in use which stores
512 * the key externally, and which either cannot or should not
513 * export it into RAM. Alternatively, an implementation
514 * (regardless of SW or HW) might not support deducing e.g.
515 * P, Q from N, D, E if the former are not part of the
516 * implementation.
517 *
518 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100519 */
520int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
521 unsigned char *N, size_t N_len,
522 unsigned char *P, size_t P_len,
523 unsigned char *Q, size_t Q_len,
524 unsigned char *D, size_t D_len,
525 unsigned char *E, size_t E_len );
526
527/**
528 * \brief Export CRT parameters of a private RSA key
529 *
530 * \param ctx Initialized RSA context
531 * \param DP MPI to hold D modulo P-1, or NULL
532 * \param DQ MPI to hold D modulo Q-1, or NULL
533 * \param QP MPI to hold modular inverse of Q modulo P, or NULL
534 *
535 * \return 0 if successful, non-zero error code otherwise.
536 *
537 * \note Alternative RSA implementations not using CRT-parameters
538 * internally can implement this function using based on
539 * \c mbedtls_rsa_deduce_opt.
540 *
541 */
542int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
543 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
544
Paul Bakker5121ce52009-01-03 21:22:43 +0000545/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100546 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100548 *
549 * \param ctx RSA context to be set
Hanno Becker4b2f6912017-09-29 13:34:55 +0100550 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
551 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100552 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100553void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
554 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100555
556/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100557 * \brief Get length of RSA modulus in bytes
558 *
559 * \param ctx Initialized RSA context
560 *
561 * \return Length of RSA modulus, in bytes.
562 *
563 */
564size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
565
566/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 * \brief Generate an RSA keypair
568 *
569 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000570 * \param f_rng RNG function
571 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 * \param nbits size of the public key in bits
573 * \param exponent public exponent (e.g., 65537)
574 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000576 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100578 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100581 int (*f_rng)(void *, unsigned char *, size_t),
582 void *p_rng,
583 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
585/**
Hanno Becker8fd55482017-08-23 14:07:48 +0100586 * \brief Check if a context contains an RSA public 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_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000593
594/**
Hanno Becker8fd55482017-08-23 14:07:48 +0100595 * \brief Check if a context contains a complete
596 * and valid RSA private key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000597 *
598 * \param ctx RSA context to be checked
599 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100600 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000603
604/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100605 * \brief Check a public-private RSA key pair.
606 * Check each of the contexts, and make sure they match.
607 *
608 * \param pub RSA context holding the public key
609 * \param prv RSA context holding the private key
610 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100611 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100614
615/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000616 * \brief Do an RSA public key operation
617 *
618 * \param ctx RSA context
619 * \param input input buffer
620 * \param output output buffer
621 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100622 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 *
624 * \note This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800625 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000626 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000627 *
628 * \note The input and output buffers must be large
629 * enough (eg. 128 bytes if RSA-1024 is used).
630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000632 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000633 unsigned char *output );
634
635/**
636 * \brief Do an RSA private key operation
637 *
638 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200639 * \param f_rng RNG function (Needed for blinding)
640 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 * \param input input buffer
642 * \param output output buffer
643 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100644 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 *
646 * \note The input and output buffers must be large
647 * enough (eg. 128 bytes if RSA-1024 is used).
648 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200650 int (*f_rng)(void *, unsigned char *, size_t),
651 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000652 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 unsigned char *output );
654
655/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100656 * \brief Generic wrapper to perform a PKCS#1 encryption using the
657 * mode from the context. Add the message padding, then do an
658 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000659 *
660 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200661 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100662 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000663 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100664 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000665 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 * \param input buffer holding the data to be encrypted
667 * \param output buffer that will hold the ciphertext
668 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100669 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 *
671 * \note The output buffer must be as large as the size
672 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000675 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000676 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000677 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000678 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000679 unsigned char *output );
680
681/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100682 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
683 *
684 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100685 * \param f_rng RNG function (Needed for padding and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100686 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100687 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100688 * \param ilen contains the plaintext length
689 * \param input buffer holding the data to be encrypted
690 * \param output buffer that will hold the ciphertext
691 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100692 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100693 *
694 * \note The output buffer must be as large as the size
695 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100698 int (*f_rng)(void *, unsigned char *, size_t),
699 void *p_rng,
700 int mode, size_t ilen,
701 const unsigned char *input,
702 unsigned char *output );
703
704/**
705 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
706 *
707 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200708 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100709 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100710 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100711 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100712 * \param label buffer holding the custom label to use
713 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100714 * \param ilen contains the plaintext length
715 * \param input buffer holding the data to be encrypted
716 * \param output buffer that will hold the ciphertext
717 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100718 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100719 *
720 * \note The output buffer must be as large as the size
721 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100724 int (*f_rng)(void *, unsigned char *, size_t),
725 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100726 int mode,
727 const unsigned char *label, size_t label_len,
728 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100729 const unsigned char *input,
730 unsigned char *output );
731
732/**
733 * \brief Generic wrapper to perform a PKCS#1 decryption using the
734 * mode from the context. Do an RSA operation, then remove
735 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000736 *
737 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100738 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200739 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100740 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000741 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 * \param input buffer holding the encrypted data
743 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000744 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000745 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100746 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000747 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100748 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100749 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100750 * if RSA-1024 is used) to be able to hold an arbitrary
751 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100752 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100753 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100754 *
755 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100756 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200759 int (*f_rng)(void *, unsigned char *, size_t),
760 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000761 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000762 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000763 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000764 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
766/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100767 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
768 *
769 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100770 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200771 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100772 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100773 * \param olen will contain the plaintext length
774 * \param input buffer holding the encrypted data
775 * \param output buffer that will hold the plaintext
776 * \param output_max_len maximum length of the output buffer
777 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100778 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100779 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100780 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100781 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100782 * if RSA-1024 is used) to be able to hold an arbitrary
783 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100784 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100785 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100786 *
787 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100788 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100789 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200791 int (*f_rng)(void *, unsigned char *, size_t),
792 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100793 int mode, size_t *olen,
794 const unsigned char *input,
795 unsigned char *output,
796 size_t output_max_len );
797
798/**
799 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
800 *
801 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100802 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200803 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100804 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100805 * \param label buffer holding the custom label to use
806 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100807 * \param olen will contain the plaintext length
808 * \param input buffer holding the encrypted data
809 * \param output buffer that will hold the plaintext
810 * \param output_max_len maximum length of the output buffer
811 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100812 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100813 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100814 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100815 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100816 * if RSA-1024 is used) to be able to hold an arbitrary
817 * decrypted message. If it is not large enough to hold
Hanno Becker8fd55482017-08-23 14:07:48 +0100818 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100819 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100820 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100821 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100822 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100823 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200825 int (*f_rng)(void *, unsigned char *, size_t),
826 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100827 int mode,
828 const unsigned char *label, size_t label_len,
829 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100830 const unsigned char *input,
831 unsigned char *output,
832 size_t output_max_len );
833
834/**
835 * \brief Generic wrapper to perform a PKCS#1 signature using the
836 * mode from the context. Do a private RSA operation to sign
837 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000838 *
839 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200840 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100841 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000842 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100843 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
844 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for
845 * signing raw data)
846 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000847 * \param hash buffer holding the message digest
848 * \param sig buffer that will hold the ciphertext
849 *
850 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100851 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000852 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100853 * \note The \c sig buffer must be as large as the size
854 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000855 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200856 * \note In case of PKCS#1 v2.1 encoding, see comments on
Hanno Becker4b2f6912017-09-29 13:34:55 +0100857 * \note \c mbedtls_rsa_rsassa_pss_sign() for details on
858 * \c md_alg and \c hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000861 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000862 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000863 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000865 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000866 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000867 unsigned char *sig );
868
869/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100870 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
871 *
872 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100873 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200874 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100875 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
876 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
877 * for signing raw data)
878 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100879 * \param hash buffer holding the message digest
880 * \param sig buffer that will hold the ciphertext
881 *
882 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100883 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100884 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100885 * \note The \c sig buffer must be as large as the size
886 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100887 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200889 int (*f_rng)(void *, unsigned char *, size_t),
890 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100891 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100893 unsigned int hashlen,
894 const unsigned char *hash,
895 unsigned char *sig );
896
897/**
898 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
899 *
900 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200901 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100902 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100903 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100904 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
905 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
906 * for signing raw data)
907 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100908 * \param hash buffer holding the message digest
909 * \param sig buffer that will hold the ciphertext
910 *
911 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100912 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100913 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100914 * \note The \c sig buffer must be as large as the size
915 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100916 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100917 * \note The \c hash_id in the RSA context is the one used for the
918 * encoding. \c md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100919 * that is encoded. According to RFC 3447 it is advised to
920 * keep both hashes the same.
921 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100923 int (*f_rng)(void *, unsigned char *, size_t),
924 void *p_rng,
925 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100927 unsigned int hashlen,
928 const unsigned char *hash,
929 unsigned char *sig );
930
931/**
932 * \brief Generic wrapper to perform a PKCS#1 verification using the
933 * mode from the context. Do a public RSA operation and check
934 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000935 *
936 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100937 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200938 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100939 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
940 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
941 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000942 * \param hash buffer holding the message digest
943 * \param sig buffer holding the ciphertext
944 *
945 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100946 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000947 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100948 * \note The \c sig buffer must be as large as the size
949 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000950 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200951 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000953 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200955 int (*f_rng)(void *, unsigned char *, size_t),
956 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000957 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000959 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000960 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200961 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000962
963/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100964 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
965 *
966 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100967 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200968 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100969 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
970 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
971 * for signing raw data)
972 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100973 * \param hash buffer holding the message digest
974 * \param sig buffer holding the ciphertext
975 *
976 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100977 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100978 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100979 * \note The \c sig buffer must be as large as the size
980 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100981 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200983 int (*f_rng)(void *, unsigned char *, size_t),
984 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100985 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100987 unsigned int hashlen,
988 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200989 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100990
991/**
992 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200993 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +0100994 *
995 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100996 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200997 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100998 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
999 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
1000 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +01001001 * \param hash buffer holding the message digest
1002 * \param sig buffer holding the ciphertext
1003 *
1004 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001005 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +01001006 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001007 * \note The \c sig buffer must be as large as the size
1008 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +01001009 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001010 * \note The \c hash_id in the RSA context is the one used for the
1011 * verification. \c md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001012 * hash that is verified. According to RFC 3447 it is advised to
Hanno Becker4b2f6912017-09-29 13:34:55 +01001013 * keep both hashes the same. If \c hash_id in the RSA context is
1014 * unset, the \c md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001017 int (*f_rng)(void *, unsigned char *, size_t),
1018 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001019 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001021 unsigned int hashlen,
1022 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001023 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001024
1025/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001026 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
1027 * (This is the version with "full" options.)
1028 *
1029 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +01001030 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001031 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +01001032 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
1033 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
1034 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001035 * \param hash buffer holding the message digest
1036 * \param mgf1_hash_id message digest used for mask generation
1037 * \param expected_salt_len Length of the salt used in padding, use
Hanno Becker4b2f6912017-09-29 13:34:55 +01001038 * \c MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001039 * \param sig buffer holding the ciphertext
1040 *
1041 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001042 * or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001043 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001044 * \note The \c sig buffer must be as large as the size
1045 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001046 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001047 * \note The \c hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001050 int (*f_rng)(void *, unsigned char *, size_t),
1051 void *p_rng,
1052 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001054 unsigned int hashlen,
1055 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001057 int expected_salt_len,
1058 const unsigned char *sig );
1059
1060/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001061 * \brief Copy the components of an RSA context
1062 *
1063 * \param dst Destination context
1064 * \param src Source context
1065 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001066 * \return 0 on success,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001067 * \c MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001070
1071/**
Paul Bakker5121ce52009-01-03 21:22:43 +00001072 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001073 *
1074 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +00001075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001077
1078/**
1079 * \brief Checkup routine
1080 *
1081 * \return 0 if successful, or 1 if the test failed
1082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001084
1085#ifdef __cplusplus
1086}
1087#endif
1088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089#endif /* MBEDTLS_RSA_C */
Paul Bakkered27a042013-04-18 22:46:23 +02001090
Paul Bakker5121ce52009-01-03 21:22:43 +00001091#endif /* rsa.h */