blob: 752105822cdc4d3770b800b28877d4a4b5635ef1 [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
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000021 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_RSA_H
25#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakkered27a042013-04-18 22:46:23 +020032
Paul Bakker314052f2011-08-15 09:07:52 +000033#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020034#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_THREADING_C)
Paul Bakkerc9965dc2013-09-29 14:58:17 +020037#include "threading.h"
38#endif
39
Paul Bakker13e2dfe2009-07-28 07:18:38 +000040/*
41 * RSA Error codes
42 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
44#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
45#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +020046#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 +020047#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
48#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
49#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
50#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
51#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Hanno Becker1434a362017-12-13 11:24:49 +000052#define MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION -0x4500 /**< The implementation doesn't offer the requested operation, e.g. because of security violations or lack of functionality */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010053#define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580 /**< RSA hardware accelerator failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000054
55/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020056 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define MBEDTLS_RSA_PUBLIC 0
59#define MBEDTLS_RSA_PRIVATE 1
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#define MBEDTLS_RSA_PKCS_V15 0
62#define MBEDTLS_RSA_PKCS_V21 1
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define MBEDTLS_RSA_SIGN 1
65#define MBEDTLS_RSA_CRYPT 2
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020068
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020069/*
70 * The above constants may be used even if the RSA module is compile out,
71 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
72 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010073
74#if !defined(MBEDTLS_RSA_ALT)
75// Regular implementation
76//
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020077
Paul Bakker407a0da2013-06-27 14:29:21 +020078#ifdef __cplusplus
79extern "C" {
80#endif
81
Hanno Beckera3ebec22017-08-23 14:06:24 +010082/**
Hanno Becker5063cd22017-09-29 11:49:12 +010083 * \brief RSA context structure
84 *
85 * \note Direct manipulation of the members of this structure
86 * is deprecated and will no longer be supported starting
87 * from the next major release. All manipulation should instead
88 * be done through the public interface functions.
89 *
Paul Bakker5121ce52009-01-03 21:22:43 +000090 */
91typedef struct
92{
93 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +000094 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +000095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_mpi N; /*!< public modulus */
97 mbedtls_mpi E; /*!< public exponent */
Paul Bakker5121ce52009-01-03 21:22:43 +000098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_mpi D; /*!< private exponent */
100 mbedtls_mpi P; /*!< 1st prime factor */
101 mbedtls_mpi Q; /*!< 2nd prime factor */
Hanno Becker1a59e792017-08-23 07:41:10 +0100102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_mpi DP; /*!< D % (P - 1) */
104 mbedtls_mpi DQ; /*!< D % (Q - 1) */
105 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_mpi RN; /*!< cached R^2 mod N */
Hanno Becker1a59e792017-08-23 07:41:10 +0100108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_mpi RP; /*!< cached R^2 mod P */
110 mbedtls_mpi RQ; /*!< cached R^2 mod Q */
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_mpi Vi; /*!< cached blinding value */
113 mbedtls_mpi Vf; /*!< cached un-blinding value */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200114
Hanno Becker4b2f6912017-09-29 13:34:55 +0100115 int padding; /*!< \c MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
116 \c MBEDTLS_RSA_PKCS_v21 for OAEP/PSS */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
118 specified in the mbedtls_md.h header file
Paul Bakker9dcc3222011-03-08 14:16:06 +0000119 for the EME-OAEP and EMSA-PSS
120 encoding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121#if defined(MBEDTLS_THREADING_C)
122 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200123#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000124}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
Paul Bakker5121ce52009-01-03 21:22:43 +0000127/**
128 * \brief Initialize an RSA context
129 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100130 * Note: Set padding to \c MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000131 * encryption scheme and the RSASSA-PSS signature scheme.
132 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 * \param ctx RSA context to be initialized
Hanno Becker4b2f6912017-09-29 13:34:55 +0100134 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
135 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 *
137 * \note The hash_id parameter is actually ignored
Hanno Becker4b2f6912017-09-29 13:34:55 +0100138 * when using \c MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200139 *
140 * \note Choice of padding mode is strictly enforced for private key
141 * operations, since there might be security concerns in
142 * mixing padding modes. For public key operations it's merely
143 * a default value, which can be overriden by calling specific
144 * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
145 *
146 * \note The chosen hash is always used for OEAP encryption.
147 * For PSS signatures, it's always used for making signatures,
148 * but can be overriden (and always is, if set to
Hanno Becker4b2f6912017-09-29 13:34:55 +0100149 * \c MBEDTLS_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100152 int padding,
153 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100155/**
156 * \brief Import a set of core parameters into an RSA context
157 *
158 * \param ctx Initialized RSA context to store parameters
159 * \param N RSA modulus, or NULL
160 * \param P First prime factor of N, or NULL
161 * \param Q Second prime factor of N, or NULL
162 * \param D Private exponent, or NULL
163 * \param E Public exponent, or NULL
164 *
165 * \note This function can be called multiple times for successive
166 * imports if the parameters are not simultaneously present.
167 * Any sequence of calls to this function should be followed
168 * by a call to \c mbedtls_rsa_complete which will check
169 * and complete the provided information to a ready-for-use
170 * public or private RSA key.
171 *
Hanno Becker33195552017-10-25 17:04:10 +0100172 * \note See the documentation of \c mbedtls_rsa_complete for more
173 * information on which parameters are necessary to setup
174 * a private or public RSA key.
175 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100176 * \note The imported parameters are copied and need not be preserved
177 * for the lifetime of the RSA context being set up.
178 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100179 * \return 0 if successful, non-zero error code on failure.
180 */
181int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
182 const mbedtls_mpi *N,
183 const mbedtls_mpi *P, const mbedtls_mpi *Q,
184 const mbedtls_mpi *D, const mbedtls_mpi *E );
185
186/**
187 * \brief Import core RSA parameters in raw big-endian
188 * binary format into an RSA context
189 *
190 * \param ctx Initialized RSA context to store parameters
191 * \param N RSA modulus, or NULL
192 * \param N_len Byte length of N, ignored if N == NULL
193 * \param P First prime factor of N, or NULL
194 * \param P_len Byte length of P, ignored if P == NULL
195 * \param Q Second prime factor of N, or NULL
196 * \param Q_len Byte length of Q, ignored if Q == NULL
197 * \param D Private exponent, or NULL
198 * \param D_len Byte length of D, ignored if D == NULL
199 * \param E Public exponent, or NULL
200 * \param E_len Byte length of E, ignored if E == NULL
201 *
202 * \note This function can be called multiple times for successive
203 * imports if the parameters are not simultaneously present.
204 * Any sequence of calls to this function should be followed
205 * by a call to \c mbedtls_rsa_complete which will check
206 * and complete the provided information to a ready-for-use
207 * public or private RSA key.
208 *
Hanno Becker33195552017-10-25 17:04:10 +0100209 * \note See the documentation of \c mbedtls_rsa_complete for more
210 * information on which parameters are necessary to setup
211 * a private or public RSA key.
212 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100213 * \note The imported parameters are copied and need not be preserved
214 * for the lifetime of the RSA context being set up.
215 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100216 * \return 0 if successful, non-zero error code on failure.
217 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100218int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100219 unsigned char const *N, size_t N_len,
220 unsigned char const *P, size_t P_len,
221 unsigned char const *Q, size_t Q_len,
222 unsigned char const *D, size_t D_len,
223 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100224
225/**
226 * \brief Attempt to complete an RSA context from
227 * a set of imported core parameters.
228 *
229 * \param ctx Initialized RSA context to store parameters
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100230 *
Hanno Beckered203612017-09-29 13:34:25 +0100231 * \note
232 * - To setup an RSA public key, precisely N and E
233 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100234 *
Hanno Beckered203612017-09-29 13:34:25 +0100235 * - To setup an RSA private key, enough information must be
Hanno Becker98838b02017-10-02 13:16:10 +0100236 * present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100237 *
Hanno Beckered203612017-09-29 13:34:25 +0100238 * The default implementation supports the following:
239 * - Derive P, Q from N, D, E
240 * - Derive N, D from P, Q, E.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100241 *
Hanno Beckered203612017-09-29 13:34:25 +0100242 * - Alternative implementations need not support these
243 * and may return \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA instead.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100244 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100245 * \return
246 * - 0 if successful. In this case, it is guaranteed
Hanno Becker1e801f52017-10-10 16:44:47 +0100247 * that the RSA context can be used for RSA operations
248 * without the risk of failure or crash.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100249 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100250 * derivations failed.
Hanno Becker43a08d02017-10-02 13:16:35 +0100251 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100252 * \warning This function need not perform consistency checks
253 * for the imported parameters! In particular, parameters that
254 * are not needed by the implementation may be silently discarded
255 * and left unchecked. For the purpose of checking the consistency
256 * of the key material, see \c mbedtls_rsa_check_privkey.
Hanno Becker43a08d02017-10-02 13:16:35 +0100257 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100258 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100259int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100260
261/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100262 * \brief Export core parameters of an RSA key
263 *
264 * \param ctx Initialized RSA context
265 * \param N MPI to hold the RSA modulus, or NULL
266 * \param P MPI to hold the first prime factor of N, or NULL
267 * \param Q MPI to hold the second prime factor of N, or NULL
268 * \param D MPI to hold the private exponent, or NULL
269 * \param E MPI to hold the public exponent, or NULL
270 *
Hanno Beckered203612017-09-29 13:34:25 +0100271 * \return
272 * - 0 if successful. In this case, the non-NULL buffers
273 * pointed to by N, P, Q, D, E are fully written, with
274 * additional unused space filled leading by 0-bytes.
275 * - Non-zero return code otherwise. In particular, if
276 * exporting the requested parameters
277 * cannot be done because of a lack of functionality
278 * or because of security policies, the error code
Hanno Beckera47023e2017-12-22 17:08:03 +0000279 * \c MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is returned.
Hanno Beckered203612017-09-29 13:34:25 +0100280 * In this case, the RSA context stays intact and can
281 * be continued to be used.
Hanno Becker91c194d2017-09-29 12:50:12 +0100282 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000283 * \note Reasons for returning \c MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION
Hanno Becker91c194d2017-09-29 12:50:12 +0100284 * would be the following: Firstly, it might be that an
285 * alternative RSA implementation is in use which stores
286 * the key externally, and which either cannot or should not
287 * export it into RAM. Alternatively, an implementation
288 * (regardless of SW or HW) might not support deducing e.g.
289 * P, Q from N, D, E if the former are not part of the
290 * implementation.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100291 *
292 */
293int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
294 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
295 mbedtls_mpi *D, mbedtls_mpi *E );
296
297/**
298 * \brief Export core parameters of an RSA key
299 * in raw big-endian binary format
300 *
301 * \param ctx Initialized RSA context
302 * \param N Byte array to store the RSA modulus, or NULL
303 * \param N_len Size of buffer for modulus
304 * \param P Byte array to hold the first prime factor of N, or NULL
305 * \param P_len Size of buffer for first prime factor
306 * \param Q Byte array to hold the second prime factor of N, or NULL
307 * \param Q_len Size of buffer for second prime factor
308 * \param D Byte array to hold the private exponent, or NULL
309 * \param D_len Size of buffer for private exponent
310 * \param E Byte array to hold the public exponent, or NULL
311 * \param E_len Size of buffer for public exponent
312 *
313 * \note The length fields are ignored if the corresponding
314 * buffer pointers are NULL.
315 *
Hanno Beckered203612017-09-29 13:34:25 +0100316 * \return
317 * - 0 if successful. In this case, the non-NULL buffers
318 * pointed to by N, P, Q, D, E are fully written, with
319 * additional unused space filled leading by 0-bytes.
320 * - Non-zero return code otherwise. In particular, if
321 * exporting the requested parameters
322 * cannot be done because of a lack of functionality
323 * or because of security policies, the error code
Hanno Beckera47023e2017-12-22 17:08:03 +0000324 * \c MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is returned.
Hanno Beckered203612017-09-29 13:34:25 +0100325 * In this case, the RSA context stays intact and can
326 * be continued to be used.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100327 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000328 * \note Reasons for returning \c MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION
Hanno Becker91c194d2017-09-29 12:50:12 +0100329 * would be the following: Firstly, it might be that an
330 * alternative RSA implementation is in use which stores
331 * the key externally, and which either cannot or should not
332 * export it into RAM. Alternatively, an implementation
333 * (regardless of SW or HW) might not support deducing e.g.
334 * P, Q from N, D, E if the former are not part of the
335 * implementation.
336 *
337 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100338 */
339int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
340 unsigned char *N, size_t N_len,
341 unsigned char *P, size_t P_len,
342 unsigned char *Q, size_t Q_len,
343 unsigned char *D, size_t D_len,
344 unsigned char *E, size_t E_len );
345
346/**
347 * \brief Export CRT parameters of a private RSA key
348 *
349 * \param ctx Initialized RSA context
350 * \param DP MPI to hold D modulo P-1, or NULL
351 * \param DQ MPI to hold D modulo Q-1, or NULL
352 * \param QP MPI to hold modular inverse of Q modulo P, or NULL
353 *
354 * \return 0 if successful, non-zero error code otherwise.
355 *
356 * \note Alternative RSA implementations not using CRT-parameters
357 * internally can implement this function using based on
358 * \c mbedtls_rsa_deduce_opt.
359 *
360 */
361int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
362 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
363
Paul Bakker5121ce52009-01-03 21:22:43 +0000364/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100365 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100367 *
368 * \param ctx RSA context to be set
Hanno Becker4b2f6912017-09-29 13:34:55 +0100369 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
370 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100371 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100372void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
373 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100374
375/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100376 * \brief Get length of RSA modulus in bytes
377 *
378 * \param ctx Initialized RSA context
379 *
380 * \return Length of RSA modulus, in bytes.
381 *
382 */
383size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
384
385/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000386 * \brief Generate an RSA keypair
387 *
388 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000389 * \param f_rng RNG function
390 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 * \param nbits size of the public key in bits
392 * \param exponent public exponent (e.g., 65537)
393 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000395 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100397 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100400 int (*f_rng)(void *, unsigned char *, size_t),
401 void *p_rng,
402 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000403
404/**
Hanno Becker43a08d02017-10-02 13:16:35 +0100405 * \brief Check if a context contains (at least) an RSA public key
Paul Bakker5121ce52009-01-03 21:22:43 +0000406 *
407 * \param ctx RSA context to be checked
408 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100409 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
410 * On success, it is guaranteed that enough information is
411 * present to perform an RSA public key operation
412 * \c mbedtls_rsa_public.
413 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000416
417/**
Hanno Becker1e801f52017-10-10 16:44:47 +0100418 * \brief Check if a context contains an RSA private key
419 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100421 * \param ctx RSA context to be checked
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100423 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
Hanno Becker43a08d02017-10-02 13:16:35 +0100424 *
Hanno Becker68767a62017-10-17 10:13:31 +0100425 * \note The consistency checks performed by this function not only
426 * ensure that \c mbedtls_rsa_private can be called successfully
427 * on the given context, but that the various parameters are
428 * mutually consistent with high probability, in the sense that
429 * \c mbedtls_rsa_public and \c mbedtls_rsa_private are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100430 *
431 * \warning This function should catch accidental misconfigurations
432 * like swapping of parameters, but it cannot establish full
433 * trust in neither the quality nor the consistency of the key
434 * material that was used to setup the given RSA context:
435 * - Regarding consistency, note (see \c mbedtls_rsa_complete)
436 * that imported parameters irrelevant for the implementation
437 * might be silently dropped, in which case the present
438 * function doesn't have access to and hence cannot check them.
Hanno Beckerfc8fbfa2017-10-17 10:31:15 +0100439 * If you want to check the consistency of the entire
440 * content of, say, an PKCS1-encoded RSA private key, you
Hanno Becker1e801f52017-10-10 16:44:47 +0100441 * should use \c mbedtls_rsa_validate_params before setting
442 * up the RSA context.
443 * Further, if the implementation performs empirical checks,
444 * these checks will substantiate but not guarantee consistency.
445 * - Regarding quality, this function is not expected to perform
446 * extended quality assessments like checking that the prime
447 * factors are safe. Further, it is the user's responsibility to
448 * ensure trustworthiness of the source of his RSA parameters,
449 * a question going beyond what's effectively checkable
450 * by the library.
Hanno Becker43a08d02017-10-02 13:16:35 +0100451 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000454
455/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100456 * \brief Check a public-private RSA key pair.
457 * Check each of the contexts, and make sure they match.
458 *
459 * \param pub RSA context holding the public key
460 * \param prv RSA context holding the private key
461 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100462 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100463 */
Hanno Becker98838b02017-10-02 13:16:10 +0100464int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
465 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100466
467/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000468 * \brief Do an RSA public key operation
469 *
470 * \param ctx RSA context
471 * \param input input buffer
472 * \param output output buffer
473 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100474 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 *
476 * \note This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800477 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000478 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 *
480 * \note The input and output buffers must be large
481 * enough (eg. 128 bytes if RSA-1024 is used).
482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000484 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000485 unsigned char *output );
486
487/**
488 * \brief Do an RSA private key operation
489 *
490 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200491 * \param f_rng RNG function (Needed for blinding)
492 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 * \param input input buffer
494 * \param output output buffer
495 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100496 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 *
498 * \note The input and output buffers must be large
499 * enough (eg. 128 bytes if RSA-1024 is used).
500 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200502 int (*f_rng)(void *, unsigned char *, size_t),
503 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000504 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000505 unsigned char *output );
506
507/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100508 * \brief Generic wrapper to perform a PKCS#1 encryption using the
509 * mode from the context. Add the message padding, then do an
510 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 *
512 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200513 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100514 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000515 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100516 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000517 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 * \param input buffer holding the data to be encrypted
519 * \param output buffer that will hold the ciphertext
520 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100521 * \deprecated It is deprecated and discouraged to call this function
522 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
523 * are likely to remove the mode argument and have it implicitly
524 * set to MBEDTLS_RSA_PUBLIC.
525 *
526 * \note Alternative implementations of RSA need not support
527 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
528 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
529 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000530 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 *
532 * \note The output buffer must be as large as the size
533 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000536 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000537 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000538 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000539 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 unsigned char *output );
541
542/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100543 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
544 *
545 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100546 * \param f_rng RNG function (Needed for padding and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100547 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100548 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100549 * \param ilen contains the plaintext length
550 * \param input buffer holding the data to be encrypted
551 * \param output buffer that will hold the ciphertext
552 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100553 * \deprecated It is deprecated and discouraged to call this function
554 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
555 * are likely to remove the mode argument and have it implicitly
556 * set to MBEDTLS_RSA_PUBLIC.
557 *
558 * \note Alternative implementations of RSA need not support
559 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
560 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
561 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000562 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100563 *
564 * \note The output buffer must be as large as the size
565 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100568 int (*f_rng)(void *, unsigned char *, size_t),
569 void *p_rng,
570 int mode, size_t ilen,
571 const unsigned char *input,
572 unsigned char *output );
573
574/**
575 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
576 *
577 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200578 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100579 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100580 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100581 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100582 * \param label buffer holding the custom label to use
583 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100584 * \param ilen contains the plaintext length
585 * \param input buffer holding the data to be encrypted
586 * \param output buffer that will hold the ciphertext
587 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100588 * \deprecated It is deprecated and discouraged to call this function
589 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
590 * are likely to remove the mode argument and have it implicitly
591 * set to MBEDTLS_RSA_PUBLIC.
592 *
593 * \note Alternative implementations of RSA need not support
594 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
595 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
596 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000597 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100598 *
599 * \note The output buffer must be as large as the size
600 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100603 int (*f_rng)(void *, unsigned char *, size_t),
604 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100605 int mode,
606 const unsigned char *label, size_t label_len,
607 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100608 const unsigned char *input,
609 unsigned char *output );
610
611/**
612 * \brief Generic wrapper to perform a PKCS#1 decryption using the
613 * mode from the context. Do an RSA operation, then remove
614 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000615 *
616 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100617 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200618 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100619 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000620 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000621 * \param input buffer holding the encrypted data
622 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000623 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000624 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100625 * \deprecated It is deprecated and discouraged to call this function
626 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
627 * are likely to remove the mode argument and have it implicitly
628 * set to MBEDTLS_RSA_PRIVATE.
629 *
630 * \note Alternative implementations of RSA need not support
631 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
632 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
633 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000634 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100636 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100637 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100638 * if RSA-1024 is used) to be able to hold an arbitrary
639 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100640 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100641 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100642 *
643 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100644 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646int mbedtls_rsa_pkcs1_decrypt( 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 Bakker23986e52011-04-24 08:57:21 +0000649 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000650 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000651 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000652 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000653
654/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100655 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
656 *
657 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100658 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200659 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100660 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100661 * \param olen will contain the plaintext length
662 * \param input buffer holding the encrypted data
663 * \param output buffer that will hold the plaintext
664 * \param output_max_len maximum length of the output buffer
665 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100666 * \deprecated It is deprecated and discouraged to call this function
667 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
668 * are likely to remove the mode argument and have it implicitly
669 * set to MBEDTLS_RSA_PRIVATE.
670 *
671 * \note Alternative implementations of RSA need not support
672 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
673 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
674 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000675 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100676 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100677 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100678 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100679 * if RSA-1024 is used) to be able to hold an arbitrary
680 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100681 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100682 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100683 *
684 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100685 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200688 int (*f_rng)(void *, unsigned char *, size_t),
689 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100690 int mode, size_t *olen,
691 const unsigned char *input,
692 unsigned char *output,
693 size_t output_max_len );
694
695/**
696 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
697 *
698 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100699 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200700 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100701 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100702 * \param label buffer holding the custom label to use
703 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100704 * \param olen will contain the plaintext length
705 * \param input buffer holding the encrypted data
706 * \param output buffer that will hold the plaintext
707 * \param output_max_len maximum length of the output buffer
708 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100709 * \deprecated It is deprecated and discouraged to call this function
710 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
711 * are likely to remove the mode argument and have it implicitly
712 * set to MBEDTLS_RSA_PRIVATE.
713 *
714 * \note Alternative implementations of RSA need not support
715 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
716 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
717 *
Hanno Beckera47023e2017-12-22 17:08:03 +0000718 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100719 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100720 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100721 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100722 * if RSA-1024 is used) to be able to hold an arbitrary
723 * decrypted message. If it is not large enough to hold
Hanno Becker8fd55482017-08-23 14:07:48 +0100724 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100725 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100726 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100727 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100728 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Hanno Becker32297e82017-12-22 10:24:32 +0000729 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100730 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200732 int (*f_rng)(void *, unsigned char *, size_t),
733 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100734 int mode,
735 const unsigned char *label, size_t label_len,
736 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100737 const unsigned char *input,
738 unsigned char *output,
739 size_t output_max_len );
740
741/**
742 * \brief Generic wrapper to perform a PKCS#1 signature using the
743 * mode from the context. Do a private RSA operation to sign
744 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000745 *
746 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200747 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100748 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000749 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100750 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
751 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for
752 * signing raw data)
753 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000754 * \param hash buffer holding the message digest
755 * \param sig buffer that will hold the ciphertext
756 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100757 * \deprecated It is deprecated and discouraged to call this function
758 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
759 * are likely to remove the mode argument and have it implicitly
760 * set to MBEDTLS_RSA_PRIVATE.
761 *
762 * \note Alternative implementations of RSA need not support
763 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
764 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
765 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000766 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100767 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000768 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100769 * \note The \c sig buffer must be as large as the size
770 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000771 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200772 * \note In case of PKCS#1 v2.1 encoding, see comments on
Hanno Becker32297e82017-12-22 10:24:32 +0000773 * \c mbedtls_rsa_rsassa_pss_sign() for details on
Hanno Becker4b2f6912017-09-29 13:34:55 +0100774 * \c md_alg and \c hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000777 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000778 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000779 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000781 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000782 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000783 unsigned char *sig );
784
785/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100786 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
787 *
788 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100789 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200790 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100791 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
792 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
793 * for signing raw data)
794 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100795 * \param hash buffer holding the message digest
796 * \param sig buffer that will hold the ciphertext
797 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100798 * \deprecated It is deprecated and discouraged to call this function
799 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
800 * are likely to remove the mode argument and have it implicitly
801 * set to MBEDTLS_RSA_PRIVATE.
802 *
803 * \note Alternative implementations of RSA need not support
804 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
805 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
806 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100807 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100808 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100809 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100810 * \note The \c sig buffer must be as large as the size
811 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100812 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200814 int (*f_rng)(void *, unsigned char *, size_t),
815 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100816 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100818 unsigned int hashlen,
819 const unsigned char *hash,
820 unsigned char *sig );
821
822/**
823 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
824 *
825 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200826 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100827 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100828 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100829 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
830 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
831 * for signing raw data)
832 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100833 * \param hash buffer holding the message digest
834 * \param sig buffer that will hold the ciphertext
835 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100836 * \deprecated It is deprecated and discouraged to call this function
837 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
838 * are likely to remove the mode argument and have it implicitly
839 * set to MBEDTLS_RSA_PRIVATE.
840 *
841 * \note Alternative implementations of RSA need not support
842 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
843 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
844 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100845 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100846 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100847 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100848 * \note The \c sig buffer must be as large as the size
849 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100850 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100851 * \note The \c hash_id in the RSA context is the one used for the
852 * encoding. \c md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100853 * that is encoded. According to RFC 3447 it is advised to
854 * keep both hashes the same.
855 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100857 int (*f_rng)(void *, unsigned char *, size_t),
858 void *p_rng,
859 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100861 unsigned int hashlen,
862 const unsigned char *hash,
863 unsigned char *sig );
864
865/**
866 * \brief Generic wrapper to perform a PKCS#1 verification using the
867 * mode from the context. Do a public RSA operation and check
868 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 *
870 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100871 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200872 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100873 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
874 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
875 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000876 * \param hash buffer holding the message digest
877 * \param sig buffer holding the ciphertext
878 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100879 * \deprecated It is deprecated and discouraged to call this function
880 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
881 * are likely to remove the mode argument and have it implicitly
882 * set to MBEDTLS_RSA_PUBLIC.
883 *
884 * \note Alternative implementations of RSA need not support
885 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
886 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
887 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000888 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100889 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000890 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100891 * \note The \c sig buffer must be as large as the size
892 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000893 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200894 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200898 int (*f_rng)(void *, unsigned char *, size_t),
899 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000900 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000902 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000903 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200904 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000905
906/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100907 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
908 *
909 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100910 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200911 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100912 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
913 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
914 * for signing raw data)
915 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100916 * \param hash buffer holding the message digest
917 * \param sig buffer holding the ciphertext
918 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100919 * \deprecated It is deprecated and discouraged to call this function
920 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
921 * are likely to remove the mode argument and have it implicitly
922 * set to MBEDTLS_RSA_PUBLIC.
923 *
924 * \note Alternative implementations of RSA need not support
925 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
926 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
927 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100928 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100929 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100930 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100931 * \note The \c sig buffer must be as large as the size
932 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100933 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200935 int (*f_rng)(void *, unsigned char *, size_t),
936 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100937 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100939 unsigned int hashlen,
940 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200941 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100942
943/**
944 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200945 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +0100946 *
947 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100948 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200949 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100950 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
951 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
952 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100953 * \param hash buffer holding the message digest
954 * \param sig buffer holding the ciphertext
955 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100956 * \deprecated It is deprecated and discouraged to call this function
957 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
958 * are likely to remove the mode argument and have it implicitly
959 * set to MBEDTLS_RSA_PUBLIC.
960 *
961 * \note Alternative implementations of RSA need not support
962 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
963 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
964 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100965 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100966 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100967 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100968 * \note The \c sig buffer must be as large as the size
969 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100970 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100971 * \note The \c hash_id in the RSA context is the one used for the
972 * verification. \c md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200973 * hash that is verified. According to RFC 3447 it is advised to
Hanno Becker4b2f6912017-09-29 13:34:55 +0100974 * keep both hashes the same. If \c hash_id in the RSA context is
975 * unset, the \c md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100976 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200978 int (*f_rng)(void *, unsigned char *, size_t),
979 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100980 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100982 unsigned int hashlen,
983 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200984 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100985
986/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200987 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
988 * (This is the version with "full" options.)
989 *
990 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100991 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200992 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100993 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
994 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
995 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200996 * \param hash buffer holding the message digest
997 * \param mgf1_hash_id message digest used for mask generation
998 * \param expected_salt_len Length of the salt used in padding, use
Hanno Becker4b2f6912017-09-29 13:34:55 +0100999 * \c MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001000 * \param sig buffer holding the ciphertext
1001 *
1002 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001003 * or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001004 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001005 * \note The \c sig buffer must be as large as the size
1006 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001007 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001008 * \note The \c hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001011 int (*f_rng)(void *, unsigned char *, size_t),
1012 void *p_rng,
1013 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001015 unsigned int hashlen,
1016 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001018 int expected_salt_len,
1019 const unsigned char *sig );
1020
1021/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001022 * \brief Copy the components of an RSA context
1023 *
1024 * \param dst Destination context
1025 * \param src Source context
1026 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001027 * \return 0 on success,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001028 * \c MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001031
1032/**
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001034 *
1035 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +00001036 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001038
Hanno Beckerd22b78b2017-10-12 11:42:17 +01001039#ifdef __cplusplus
1040}
1041#endif
1042
1043#else /* MBEDTLS_RSA_ALT */
1044#include "rsa_alt.h"
1045#endif /* MBEDTLS_RSA_ALT */
1046
1047#ifdef __cplusplus
1048extern "C" {
1049#endif
1050
Paul Bakker5121ce52009-01-03 21:22:43 +00001051/**
1052 * \brief Checkup routine
1053 *
1054 * \return 0 if successful, or 1 if the test failed
1055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001057
1058#ifdef __cplusplus
1059}
1060#endif
1061
Paul Bakker5121ce52009-01-03 21:22:43 +00001062#endif /* rsa.h */