blob: df0ade80c20186b338982e9be9fc228bbee10c74 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief The RSA public-key cryptosystem
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_RSA_H
24#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020027#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakkered27a042013-04-18 22:46:23 +020031
Paul Bakker314052f2011-08-15 09:07:52 +000032#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020033#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_THREADING_C)
Paul Bakkerc9965dc2013-09-29 14:58:17 +020036#include "threading.h"
37#endif
38
Paul Bakker13e2dfe2009-07-28 07:18:38 +000039/*
40 * RSA Error codes
41 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
43#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
44#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +020045#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the library's validity check. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
47#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
48#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
49#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
50#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Hanno Becker91c194d2017-09-29 12:50:12 +010051#define MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED -0x4500 /**< The requested parameter export is not possible/allowed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000052
53/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020054 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define MBEDTLS_RSA_PUBLIC 0
57#define MBEDTLS_RSA_PRIVATE 1
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define MBEDTLS_RSA_PKCS_V15 0
60#define MBEDTLS_RSA_PKCS_V21 1
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define MBEDTLS_RSA_SIGN 1
63#define MBEDTLS_RSA_CRYPT 2
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020066
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020067/*
68 * The above constants may be used even if the RSA module is compile out,
69 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
70 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020072
Paul Bakker407a0da2013-06-27 14:29:21 +020073#ifdef __cplusplus
74extern "C" {
75#endif
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/**
Hanno Beckera3ebec22017-08-23 14:06:24 +010078 * Helper functions for RSA-related operations on MPI's.
79 */
80
81/**
82 * \brief Compute RSA prime moduli P, Q from public modulus N=PQ
Hanno Beckerb0c5edc2017-08-23 22:16:10 +010083 * and a pair of private and public key.
Hanno Beckera3ebec22017-08-23 14:06:24 +010084 *
85 * \note This is a 'static' helper function not operating on
86 * an RSA context. Alternative implementations need not
87 * overwrite it.
88 *
89 * \param N RSA modulus N = PQ, with P, Q to be found
90 * \param D RSA private exponent
91 * \param E RSA public exponent
92 * \param f_rng PRNG to be used for randomization, or NULL
93 * \param p_rng PRNG context for f_rng, or NULL
94 * \param P Pointer to MPI holding first prime factor of N on success
95 * \param Q Pointer to MPI holding second prime factor of N on success
96 *
Hanno Beckered203612017-09-29 13:34:25 +010097 * \return
98 * - 0 if successful. In this case, P and Q constitute a
Hanno Beckerba5b7552017-10-02 09:55:49 +010099 * factorization of N.
100 * - A non-zero error code otherwise.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100101 *
Hanno Becker1b831fe2017-10-02 12:24:50 +0100102 * \note It is neither checked that P, Q are prime nor that
103 * D, E are modular inverses wrt. P-1 and Q-1. For that,
104 * use the helper function \c mbedtls_rsa_validate_params.
105 *
Hanno Beckera3ebec22017-08-23 14:06:24 +0100106 */
Hanno Beckerba5b7552017-10-02 09:55:49 +0100107int mbedtls_rsa_deduce_moduli( mbedtls_mpi const *N, mbedtls_mpi const *D,
108 mbedtls_mpi const *E, int (*f_rng)(void *, unsigned char *, size_t),
109 void *p_rng, mbedtls_mpi *P, mbedtls_mpi *Q );
Hanno Beckera3ebec22017-08-23 14:06:24 +0100110
111/**
112 * \brief Compute RSA private exponent from
113 * prime moduli and public key.
114 *
115 * \note This is a 'static' helper function not operating on
116 * an RSA context. Alternative implementations need not
117 * overwrite it.
118 *
119 * \param P First prime factor of RSA modulus
120 * \param Q Second prime factor of RSA modulus
121 * \param E RSA public exponent
122 * \param D Pointer to MPI holding the private exponent on success.
123 *
Hanno Beckered203612017-09-29 13:34:25 +0100124 * \return
125 * - 0 if successful. In this case, D is set to a simultaneous
Hanno Beckera3ebec22017-08-23 14:06:24 +0100126 * modular inverse of E modulo both P-1 and Q-1.
Hanno Beckerbdefff12017-10-02 09:57:50 +0100127 * - A non-zero error code otherwise.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100128 *
Hanno Becker1b831fe2017-10-02 12:24:50 +0100129 * \note This function does not check whether P and Q are primes.
130 *
Hanno Beckera3ebec22017-08-23 14:06:24 +0100131 */
Hanno Beckerbdefff12017-10-02 09:57:50 +0100132int mbedtls_rsa_deduce_private( mbedtls_mpi const *P, mbedtls_mpi const *Q,
133 mbedtls_mpi const *E, mbedtls_mpi *D );
Hanno Beckera3ebec22017-08-23 14:06:24 +0100134
135
136/**
137 * \brief Generate RSA-CRT parameters
138 *
139 * \note This is a 'static' helper function not operating on
140 * an RSA context. Alternative implementations need not
141 * overwrite it.
142 *
143 * \param P First prime factor of N
144 * \param Q Second prime factor of N
145 * \param D RSA private exponent
146 * \param DP Output variable for D modulo P-1
147 * \param DQ Output variable for D modulo Q-1
148 * \param QP Output variable for the modular inverse of Q modulo P.
149 *
150 * \return 0 on success, non-zero error code otherwise.
151 *
Hanno Becker1b831fe2017-10-02 12:24:50 +0100152 * \note This function does not check whether P, Q are
153 * prime and whether D is a valid private exponent.
154 *
Hanno Beckera3ebec22017-08-23 14:06:24 +0100155 */
156int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
157 const mbedtls_mpi *D, mbedtls_mpi *DP,
158 mbedtls_mpi *DQ, mbedtls_mpi *QP );
159
160
161/**
162 * \brief Check validity of core RSA parameters
163 *
164 * \note This is a 'static' helper function not operating on
165 * an RSA context. Alternative implementations need not
166 * overwrite it.
167 *
168 * \param N RSA modulus N = PQ
169 * \param P First prime factor of N
170 * \param Q Second prime factor of N
171 * \param D RSA private exponent
172 * \param E RSA public exponent
Hanno Becker56bae952017-09-29 15:33:10 +0100173 * \param f_rng PRNG to be used for primality check, or NULL
Hanno Beckera3ebec22017-08-23 14:06:24 +0100174 * \param p_rng PRNG context for f_rng, or NULL
175 *
Hanno Beckered203612017-09-29 13:34:25 +0100176 * \return
177 * - 0 if the following conditions are satisfied:
178 * - N = PQ if N,P,Q != NULL
179 * - D and E are modular inverses modulo P-1 and Q-1
180 * if D,E,P,Q != NULL
181 * - P prime if f_rng, P != NULL
182 * - Q prime if f_rng, Q != NULL
Hanno Becker750e8b42017-08-25 07:54:27 +0100183 * - A non-zero error code otherwise.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100184 *
185 * \note The function can be used with a restricted set of arguments
186 * to perform specific checks only. E.g., calling it with
187 * (-,P,-,-,-) and a PRNG amounts to a primality check for P.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100188 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100189int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
190 const mbedtls_mpi *Q, const mbedtls_mpi *D,
191 const mbedtls_mpi *E,
192 int (*f_rng)(void *, unsigned char *, size_t),
193 void *p_rng );
Hanno Beckera3ebec22017-08-23 14:06:24 +0100194
195/**
Hanno Beckerd3637992017-08-25 07:55:03 +0100196 * \brief Check validity of RSA CRT parameters
197 *
198 * \note This is a 'static' helper function not operating on
199 * an RSA context. Alternative implementations need not
200 * overwrite it.
201 *
202 * \param P First prime factor of RSA modulus
203 * \param Q Second prime factor of RSA modulus
204 * \param D RSA private exponent
205 * \param DP MPI to check for D modulo P-1
206 * \param DQ MPI to check for D modulo P-1
207 * \param QP MPI to check for the modular inverse of Q modulo P.
208 *
Hanno Beckered203612017-09-29 13:34:25 +0100209 * \return
210 * - 0 if the following conditions are satisfied:
211 * - D = DP mod P-1 if P, D, DP != NULL
212 * - Q = DQ mod P-1 if P, D, DQ != NULL
213 * - QP = Q^-1 mod P if P, Q, QP != NULL
214 * - \c MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if check failed,
215 * potentially including \c MBEDTLS_ERR_MPI_XXX if some
Hanno Beckerd3637992017-08-25 07:55:03 +0100216 * MPI calculations failed.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100217 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if insufficient
Hanno Beckerd3637992017-08-25 07:55:03 +0100218 * data was provided to check DP, DQ or QP.
219 *
220 * \note The function can be used with a restricted set of arguments
221 * to perform specific checks only. E.g., calling it with the
222 * parameters (P, -, D, DP, -, -) will check DP = D mod P-1.
223 */
224int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
225 const mbedtls_mpi *D, const mbedtls_mpi *DP,
226 const mbedtls_mpi *DQ, const mbedtls_mpi *QP );
227
228/**
Hanno Beckera3ebec22017-08-23 14:06:24 +0100229 * Implementation of RSA interface
230 */
231
Hanno Beckerab377312017-08-23 16:24:51 +0100232#if !defined(MBEDTLS_RSA_ALT)
233
Hanno Beckera3ebec22017-08-23 14:06:24 +0100234/**
Hanno Becker5063cd22017-09-29 11:49:12 +0100235 * \brief RSA context structure
236 *
237 * \note Direct manipulation of the members of this structure
238 * is deprecated and will no longer be supported starting
239 * from the next major release. All manipulation should instead
240 * be done through the public interface functions.
241 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 */
243typedef struct
244{
245 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +0000246 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_mpi N; /*!< public modulus */
249 mbedtls_mpi E; /*!< public exponent */
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_mpi D; /*!< private exponent */
252 mbedtls_mpi P; /*!< 1st prime factor */
253 mbedtls_mpi Q; /*!< 2nd prime factor */
Hanno Becker1a59e792017-08-23 07:41:10 +0100254
255#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_mpi DP; /*!< D % (P - 1) */
257 mbedtls_mpi DQ; /*!< D % (Q - 1) */
258 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Hanno Becker1a59e792017-08-23 07:41:10 +0100259#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_mpi RN; /*!< cached R^2 mod N */
Hanno Becker1a59e792017-08-23 07:41:10 +0100262
263#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 mbedtls_mpi RP; /*!< cached R^2 mod P */
265 mbedtls_mpi RQ; /*!< cached R^2 mod Q */
Hanno Becker1a59e792017-08-23 07:41:10 +0100266#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_mpi Vi; /*!< cached blinding value */
269 mbedtls_mpi Vf; /*!< cached un-blinding value */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200270
Hanno Becker4b2f6912017-09-29 13:34:55 +0100271 int padding; /*!< \c MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
272 \c MBEDTLS_RSA_PKCS_v21 for OAEP/PSS */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
274 specified in the mbedtls_md.h header file
Paul Bakker9dcc3222011-03-08 14:16:06 +0000275 for the EME-OAEP and EMSA-PSS
276 encoding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#if defined(MBEDTLS_THREADING_C)
278 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200279#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000280}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000282
Hanno Beckerab377312017-08-23 16:24:51 +0100283#else
284
285#include "rsa_alt.h"
286
287#endif /* MBEDTLS_RSA_ALT */
288
Paul Bakker5121ce52009-01-03 21:22:43 +0000289/**
290 * \brief Initialize an RSA context
291 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100292 * Note: Set padding to \c MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000293 * encryption scheme and the RSASSA-PSS signature scheme.
294 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 * \param ctx RSA context to be initialized
Hanno Becker4b2f6912017-09-29 13:34:55 +0100296 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
297 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 *
299 * \note The hash_id parameter is actually ignored
Hanno Becker4b2f6912017-09-29 13:34:55 +0100300 * when using \c MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200301 *
302 * \note Choice of padding mode is strictly enforced for private key
303 * operations, since there might be security concerns in
304 * mixing padding modes. For public key operations it's merely
305 * a default value, which can be overriden by calling specific
306 * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
307 *
308 * \note The chosen hash is always used for OEAP encryption.
309 * For PSS signatures, it's always used for making signatures,
310 * but can be overriden (and always is, if set to
Hanno Becker4b2f6912017-09-29 13:34:55 +0100311 * \c MBEDTLS_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100314 int padding,
315 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000316
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100317/**
318 * \brief Import a set of core parameters into an RSA context
319 *
320 * \param ctx Initialized RSA context to store parameters
321 * \param N RSA modulus, or NULL
322 * \param P First prime factor of N, or NULL
323 * \param Q Second prime factor of N, or NULL
324 * \param D Private exponent, or NULL
325 * \param E Public exponent, or NULL
326 *
327 * \note This function can be called multiple times for successive
328 * imports if the parameters are not simultaneously present.
329 * Any sequence of calls to this function should be followed
330 * by a call to \c mbedtls_rsa_complete which will check
331 * and complete the provided information to a ready-for-use
332 * public or private RSA key.
333 *
334 * \return 0 if successful, non-zero error code on failure.
335 */
336int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
337 const mbedtls_mpi *N,
338 const mbedtls_mpi *P, const mbedtls_mpi *Q,
339 const mbedtls_mpi *D, const mbedtls_mpi *E );
340
341/**
342 * \brief Import core RSA parameters in raw big-endian
343 * binary format into an RSA context
344 *
345 * \param ctx Initialized RSA context to store parameters
346 * \param N RSA modulus, or NULL
347 * \param N_len Byte length of N, ignored if N == NULL
348 * \param P First prime factor of N, or NULL
349 * \param P_len Byte length of P, ignored if P == NULL
350 * \param Q Second prime factor of N, or NULL
351 * \param Q_len Byte length of Q, ignored if Q == NULL
352 * \param D Private exponent, or NULL
353 * \param D_len Byte length of D, ignored if D == NULL
354 * \param E Public exponent, or NULL
355 * \param E_len Byte length of E, ignored if E == NULL
356 *
357 * \note This function can be called multiple times for successive
358 * imports if the parameters are not simultaneously present.
359 * Any sequence of calls to this function should be followed
360 * by a call to \c mbedtls_rsa_complete which will check
361 * and complete the provided information to a ready-for-use
362 * public or private RSA key.
363 *
364 * \return 0 if successful, non-zero error code on failure.
365 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100366int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100367 unsigned char const *N, size_t N_len,
368 unsigned char const *P, size_t P_len,
369 unsigned char const *Q, size_t Q_len,
370 unsigned char const *D, size_t D_len,
371 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100372
373/**
374 * \brief Attempt to complete an RSA context from
375 * a set of imported core parameters.
376 *
377 * \param ctx Initialized RSA context to store parameters
Hanno Becker43a08d02017-10-02 13:16:35 +0100378 * \param f_rng RNG function, or NULL
379 * \param p_rng RNG parameter, or NULL
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100380 *
Hanno Beckered203612017-09-29 13:34:25 +0100381 * \note
382 * - To setup an RSA public key, precisely N and E
383 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100384 *
Hanno Beckered203612017-09-29 13:34:25 +0100385 * - To setup an RSA private key, enough information must be
Hanno Becker98838b02017-10-02 13:16:10 +0100386 * present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100387 *
Hanno Beckered203612017-09-29 13:34:25 +0100388 * The default implementation supports the following:
389 * - Derive P, Q from N, D, E
390 * - Derive N, D from P, Q, E.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100391 *
Hanno Beckered203612017-09-29 13:34:25 +0100392 * - Alternative implementations need not support these
393 * and may return \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA instead.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100394 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100395 * \note The PRNG is used for the probabilistic algorithm
396 * used in the derivation of P, Q from N, D, E. If it
397 * not present, a deterministic heuristic is used.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100398 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100399 * \return
400 * - 0 if successful. In this case, it is guaranteed
401 * the functions \c mbedtls_rsa_check_pubkey resp.
402 * \c mbedtls_rsa_check_privkey pass in case of a
403 * public resp. private key.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100404 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100405 * derivations failed.
Hanno Becker43a08d02017-10-02 13:16:35 +0100406 *
407 * \warning Implementations are *not* obliged to perform exhaustive
408 * validation of the imported parameters!
409 * In particular, parameters that are not needed by the
410 * implementation may be silently discarded and left unchecked.
411 * If the user mistrusts the given key material, he should
412 * employ other means for verification like the helper functions
413 * \c mbedtls_rsa_validate_params, \c mbedtls_rsa_validate_crt.
414 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100415 */
416int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
417 int (*f_rng)(void *, unsigned char *, size_t),
418 void *p_rng );
419
420/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100421 * \brief Export core parameters of an RSA key
422 *
423 * \param ctx Initialized RSA context
424 * \param N MPI to hold the RSA modulus, or NULL
425 * \param P MPI to hold the first prime factor of N, or NULL
426 * \param Q MPI to hold the second prime factor of N, or NULL
427 * \param D MPI to hold the private exponent, or NULL
428 * \param E MPI to hold the public exponent, or NULL
429 *
Hanno Beckered203612017-09-29 13:34:25 +0100430 * \return
431 * - 0 if successful. In this case, the non-NULL buffers
432 * pointed to by N, P, Q, D, E are fully written, with
433 * additional unused space filled leading by 0-bytes.
434 * - Non-zero return code otherwise. In particular, if
435 * exporting the requested parameters
436 * cannot be done because of a lack of functionality
437 * or because of security policies, the error code
438 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
439 * In this case, the RSA context stays intact and can
440 * be continued to be used.
Hanno Becker91c194d2017-09-29 12:50:12 +0100441 *
Hanno Beckered203612017-09-29 13:34:25 +0100442 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100443 * would be the following: Firstly, it might be that an
444 * alternative RSA implementation is in use which stores
445 * the key externally, and which either cannot or should not
446 * export it into RAM. Alternatively, an implementation
447 * (regardless of SW or HW) might not support deducing e.g.
448 * P, Q from N, D, E if the former are not part of the
449 * implementation.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100450 *
451 */
452int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
453 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
454 mbedtls_mpi *D, mbedtls_mpi *E );
455
456/**
457 * \brief Export core parameters of an RSA key
458 * in raw big-endian binary format
459 *
460 * \param ctx Initialized RSA context
461 * \param N Byte array to store the RSA modulus, or NULL
462 * \param N_len Size of buffer for modulus
463 * \param P Byte array to hold the first prime factor of N, or NULL
464 * \param P_len Size of buffer for first prime factor
465 * \param Q Byte array to hold the second prime factor of N, or NULL
466 * \param Q_len Size of buffer for second prime factor
467 * \param D Byte array to hold the private exponent, or NULL
468 * \param D_len Size of buffer for private exponent
469 * \param E Byte array to hold the public exponent, or NULL
470 * \param E_len Size of buffer for public exponent
471 *
472 * \note The length fields are ignored if the corresponding
473 * buffer pointers are NULL.
474 *
Hanno Beckered203612017-09-29 13:34:25 +0100475 * \return
476 * - 0 if successful. In this case, the non-NULL buffers
477 * pointed to by N, P, Q, D, E are fully written, with
478 * additional unused space filled leading by 0-bytes.
479 * - Non-zero return code otherwise. In particular, if
480 * exporting the requested parameters
481 * cannot be done because of a lack of functionality
482 * or because of security policies, the error code
483 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
484 * In this case, the RSA context stays intact and can
485 * be continued to be used.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100486 *
Hanno Beckered203612017-09-29 13:34:25 +0100487 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100488 * would be the following: Firstly, it might be that an
489 * alternative RSA implementation is in use which stores
490 * the key externally, and which either cannot or should not
491 * export it into RAM. Alternatively, an implementation
492 * (regardless of SW or HW) might not support deducing e.g.
493 * P, Q from N, D, E if the former are not part of the
494 * implementation.
495 *
496 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100497 */
498int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
499 unsigned char *N, size_t N_len,
500 unsigned char *P, size_t P_len,
501 unsigned char *Q, size_t Q_len,
502 unsigned char *D, size_t D_len,
503 unsigned char *E, size_t E_len );
504
505/**
506 * \brief Export CRT parameters of a private RSA key
507 *
508 * \param ctx Initialized RSA context
509 * \param DP MPI to hold D modulo P-1, or NULL
510 * \param DQ MPI to hold D modulo Q-1, or NULL
511 * \param QP MPI to hold modular inverse of Q modulo P, or NULL
512 *
513 * \return 0 if successful, non-zero error code otherwise.
514 *
515 * \note Alternative RSA implementations not using CRT-parameters
516 * internally can implement this function using based on
517 * \c mbedtls_rsa_deduce_opt.
518 *
519 */
520int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
521 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
522
Paul Bakker5121ce52009-01-03 21:22:43 +0000523/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100524 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100526 *
527 * \param ctx RSA context to be set
Hanno Becker4b2f6912017-09-29 13:34:55 +0100528 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
529 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100530 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100531void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
532 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100533
534/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100535 * \brief Get length of RSA modulus in bytes
536 *
537 * \param ctx Initialized RSA context
538 *
539 * \return Length of RSA modulus, in bytes.
540 *
541 */
542size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
543
544/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 * \brief Generate an RSA keypair
546 *
547 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000548 * \param f_rng RNG function
549 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 * \param nbits size of the public key in bits
551 * \param exponent public exponent (e.g., 65537)
552 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000554 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000555 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100556 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100559 int (*f_rng)(void *, unsigned char *, size_t),
560 void *p_rng,
561 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000562
563/**
Hanno Becker43a08d02017-10-02 13:16:35 +0100564 * \brief Check if a context contains (at least) an RSA public key
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 *
566 * \param ctx RSA context to be checked
567 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100568 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
569 * On success, it is guaranteed that enough information is
570 * present to perform an RSA public key operation
571 * \c mbedtls_rsa_public.
572 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
576/**
Hanno Becker43a08d02017-10-02 13:16:35 +0100577 * \brief Check if a context contains an RSA private key
578 * and perform basic sanity checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 *
580 * \param ctx RSA context to be checked
581 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100582 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
583 * On success, it is guaranteed that enough information is
584 * present to perform RSA private and public key operations.
585 *
586 * \warning This function is *not* obliged to perform an exhaustive
587 * sanity check what would guarantee the internal parameters
588 * to match and \c mbedtls_rsa_private and \c mbedtls_rsa_public
589 * to be mutually inverse to each other.
590 * The reason is that for minimal non-CRT implementations
591 * using only N, D, E, for example, checking the validity
592 * would be computationally expensive.
593 * Users mistrusting their key material should use other
594 * means for verification; see the documentation of
595 * \c mbedtls_rsa_complete.
596 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000597 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000599
600/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100601 * \brief Check a public-private RSA key pair.
602 * Check each of the contexts, and make sure they match.
603 *
604 * \param pub RSA context holding the public key
605 * \param prv RSA context holding the private key
606 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100607 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100608 */
Hanno Becker98838b02017-10-02 13:16:10 +0100609int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
610 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100611
612/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 * \brief Do an RSA public key operation
614 *
615 * \param ctx RSA context
616 * \param input input buffer
617 * \param output output buffer
618 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100619 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 *
621 * \note This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800622 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000623 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000624 *
625 * \note The input and output buffers must be large
626 * enough (eg. 128 bytes if RSA-1024 is used).
627 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000629 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 unsigned char *output );
631
632/**
633 * \brief Do an RSA private key operation
634 *
635 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200636 * \param f_rng RNG function (Needed for blinding)
637 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000638 * \param input input buffer
639 * \param output output buffer
640 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100641 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 *
643 * \note The input and output buffers must be large
644 * enough (eg. 128 bytes if RSA-1024 is used).
645 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200647 int (*f_rng)(void *, unsigned char *, size_t),
648 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000649 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000650 unsigned char *output );
651
652/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100653 * \brief Generic wrapper to perform a PKCS#1 encryption using the
654 * mode from the context. Add the message padding, then do an
655 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000656 *
657 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200658 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100659 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000660 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100661 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000662 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 * \param input buffer holding the data to be encrypted
664 * \param output buffer that will hold the ciphertext
665 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100666 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 *
668 * \note The output buffer must be as large as the size
669 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
670 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000672 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000673 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000674 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000675 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 unsigned char *output );
677
678/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100679 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
680 *
681 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100682 * \param f_rng RNG function (Needed for padding and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100683 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100684 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100685 * \param ilen contains the plaintext length
686 * \param input buffer holding the data to be encrypted
687 * \param output buffer that will hold the ciphertext
688 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100689 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100690 *
691 * \note The output buffer must be as large as the size
692 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100695 int (*f_rng)(void *, unsigned char *, size_t),
696 void *p_rng,
697 int mode, size_t ilen,
698 const unsigned char *input,
699 unsigned char *output );
700
701/**
702 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
703 *
704 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200705 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100706 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100707 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100708 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100709 * \param label buffer holding the custom label to use
710 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100711 * \param ilen contains the plaintext length
712 * \param input buffer holding the data to be encrypted
713 * \param output buffer that will hold the ciphertext
714 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100715 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100716 *
717 * \note The output buffer must be as large as the size
718 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100721 int (*f_rng)(void *, unsigned char *, size_t),
722 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100723 int mode,
724 const unsigned char *label, size_t label_len,
725 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100726 const unsigned char *input,
727 unsigned char *output );
728
729/**
730 * \brief Generic wrapper to perform a PKCS#1 decryption using the
731 * mode from the context. Do an RSA operation, then remove
732 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 *
734 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100735 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200736 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100737 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000738 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000739 * \param input buffer holding the encrypted data
740 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000741 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100743 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000744 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100745 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100746 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100747 * if RSA-1024 is used) to be able to hold an arbitrary
748 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100749 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100750 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100751 *
752 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100753 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000754 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200756 int (*f_rng)(void *, unsigned char *, size_t),
757 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000758 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000759 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000760 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000761 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000762
763/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100764 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
765 *
766 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100767 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200768 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100769 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100770 * \param olen will contain the plaintext length
771 * \param input buffer holding the encrypted data
772 * \param output buffer that will hold the plaintext
773 * \param output_max_len maximum length of the output buffer
774 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100775 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100776 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100777 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100778 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100779 * if RSA-1024 is used) to be able to hold an arbitrary
780 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100781 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100782 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100783 *
784 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100785 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200788 int (*f_rng)(void *, unsigned char *, size_t),
789 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100790 int mode, size_t *olen,
791 const unsigned char *input,
792 unsigned char *output,
793 size_t output_max_len );
794
795/**
796 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
797 *
798 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100799 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200800 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100801 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100802 * \param label buffer holding the custom label to use
803 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100804 * \param olen will contain the plaintext length
805 * \param input buffer holding the encrypted data
806 * \param output buffer that will hold the plaintext
807 * \param output_max_len maximum length of the output buffer
808 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100809 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100810 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100811 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100812 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100813 * if RSA-1024 is used) to be able to hold an arbitrary
814 * decrypted message. If it is not large enough to hold
Hanno Becker8fd55482017-08-23 14:07:48 +0100815 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100816 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100817 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100818 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100819 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200822 int (*f_rng)(void *, unsigned char *, size_t),
823 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100824 int mode,
825 const unsigned char *label, size_t label_len,
826 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100827 const unsigned char *input,
828 unsigned char *output,
829 size_t output_max_len );
830
831/**
832 * \brief Generic wrapper to perform a PKCS#1 signature using the
833 * mode from the context. Do a private RSA operation to sign
834 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000835 *
836 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200837 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100838 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000839 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100840 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
841 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for
842 * signing raw data)
843 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000844 * \param hash buffer holding the message digest
845 * \param sig buffer that will hold the ciphertext
846 *
847 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100848 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000849 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100850 * \note The \c sig buffer must be as large as the size
851 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000852 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200853 * \note In case of PKCS#1 v2.1 encoding, see comments on
Hanno Becker4b2f6912017-09-29 13:34:55 +0100854 * \note \c mbedtls_rsa_rsassa_pss_sign() for details on
855 * \c md_alg and \c hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000856 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000858 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000859 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000860 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000862 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000863 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000864 unsigned char *sig );
865
866/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100867 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
868 *
869 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100870 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200871 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100872 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
873 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
874 * for signing raw data)
875 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100876 * \param hash buffer holding the message digest
877 * \param sig buffer that will hold the ciphertext
878 *
879 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100880 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100881 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100882 * \note The \c sig buffer must be as large as the size
883 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100884 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200886 int (*f_rng)(void *, unsigned char *, size_t),
887 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100888 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100890 unsigned int hashlen,
891 const unsigned char *hash,
892 unsigned char *sig );
893
894/**
895 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
896 *
897 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200898 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100899 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100900 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100901 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
902 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
903 * for signing raw data)
904 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100905 * \param hash buffer holding the message digest
906 * \param sig buffer that will hold the ciphertext
907 *
908 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100909 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100910 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100911 * \note The \c sig buffer must be as large as the size
912 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100913 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100914 * \note The \c hash_id in the RSA context is the one used for the
915 * encoding. \c md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100916 * that is encoded. According to RFC 3447 it is advised to
917 * keep both hashes the same.
918 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100920 int (*f_rng)(void *, unsigned char *, size_t),
921 void *p_rng,
922 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100924 unsigned int hashlen,
925 const unsigned char *hash,
926 unsigned char *sig );
927
928/**
929 * \brief Generic wrapper to perform a PKCS#1 verification using the
930 * mode from the context. Do a public RSA operation and check
931 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000932 *
933 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100934 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200935 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100936 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
937 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
938 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000939 * \param hash buffer holding the message digest
940 * \param sig buffer holding the ciphertext
941 *
942 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100943 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000944 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100945 * \note The \c sig buffer must be as large as the size
946 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000947 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200948 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000950 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200952 int (*f_rng)(void *, unsigned char *, size_t),
953 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000954 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000956 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000957 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200958 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000959
960/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100961 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
962 *
963 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100964 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200965 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100966 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
967 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
968 * for signing raw data)
969 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100970 * \param hash buffer holding the message digest
971 * \param sig buffer holding the ciphertext
972 *
973 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100974 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100975 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100976 * \note The \c sig buffer must be as large as the size
977 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100978 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200980 int (*f_rng)(void *, unsigned char *, size_t),
981 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100982 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100984 unsigned int hashlen,
985 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200986 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100987
988/**
989 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200990 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +0100991 *
992 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100993 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200994 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100995 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
996 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
997 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100998 * \param hash buffer holding the message digest
999 * \param sig buffer holding the ciphertext
1000 *
1001 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001002 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +01001003 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001004 * \note The \c sig buffer must be as large as the size
1005 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +01001006 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001007 * \note The \c hash_id in the RSA context is the one used for the
1008 * verification. \c md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001009 * hash that is verified. According to RFC 3447 it is advised to
Hanno Becker4b2f6912017-09-29 13:34:55 +01001010 * keep both hashes the same. If \c hash_id in the RSA context is
1011 * unset, the \c md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001014 int (*f_rng)(void *, unsigned char *, size_t),
1015 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001016 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001018 unsigned int hashlen,
1019 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001020 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001021
1022/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001023 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
1024 * (This is the version with "full" options.)
1025 *
1026 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +01001027 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001028 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +01001029 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
1030 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
1031 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001032 * \param hash buffer holding the message digest
1033 * \param mgf1_hash_id message digest used for mask generation
1034 * \param expected_salt_len Length of the salt used in padding, use
Hanno Becker4b2f6912017-09-29 13:34:55 +01001035 * \c MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001036 * \param sig buffer holding the ciphertext
1037 *
1038 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001039 * or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001040 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001041 * \note The \c sig buffer must be as large as the size
1042 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001043 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001044 * \note The \c hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001045 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001047 int (*f_rng)(void *, unsigned char *, size_t),
1048 void *p_rng,
1049 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001051 unsigned int hashlen,
1052 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001054 int expected_salt_len,
1055 const unsigned char *sig );
1056
1057/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001058 * \brief Copy the components of an RSA context
1059 *
1060 * \param dst Destination context
1061 * \param src Source context
1062 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001063 * \return 0 on success,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001064 * \c MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001065 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001067
1068/**
Paul Bakker5121ce52009-01-03 21:22:43 +00001069 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001070 *
1071 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +00001072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
1075/**
1076 * \brief Checkup routine
1077 *
1078 * \return 0 if successful, or 1 if the test failed
1079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
1082#ifdef __cplusplus
1083}
1084#endif
1085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086#endif /* MBEDTLS_RSA_C */
Paul Bakkered27a042013-04-18 22:46:23 +02001087
Paul Bakker5121ce52009-01-03 21:22:43 +00001088#endif /* rsa.h */