blob: 94e0b2888d5d52cefdd5b7c2cd4a1690dc3c97fa [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
386 * present for the other parameters to be efficiently derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100387 *
Hanno Beckered203612017-09-29 13:34:25 +0100388 * The default implementation supports the following:
389 * - Derive P, Q from N, D, E
390 * - Derive N, D from P, Q, E.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100391 *
Hanno Beckered203612017-09-29 13:34:25 +0100392 * - Alternative implementations need not support these
393 * and may return \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA instead.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100394 *
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/**
421 * \brief Check if CRT-parameters match core parameters
422 *
423 * \param ctx Complete RSA private key context
424 * \param DP Private exponent modulo P-1, or NULL
425 * \param DQ Private exponent modulo Q-1, or NULL
426 * \param QP Modular inverse of Q modulo P, or NULL
427 *
428 * \return 0 if successful, testifying that the non-NULL optional
429 * parameters provided are in accordance with the core
430 * RSA parameters. Non-zero error code otherwise.
431 *
432 * \note This function performs in-place computations on the
433 * parameters DP, DQ and QP. If modification cannot be
434 * tolerated, you should make copies with mbedtls_mpi_copy
435 * before calling this function.
436 *
437 */
Hanno Beckerd3637992017-08-25 07:55:03 +0100438int mbedtls_rsa_check_crt( const mbedtls_rsa_context *ctx,
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100439 mbedtls_mpi *DP,
440 mbedtls_mpi *DQ,
441 mbedtls_mpi *QP );
442
443/**
444 * \brief Export core parameters of an RSA key
445 *
446 * \param ctx Initialized RSA context
447 * \param N MPI to hold the RSA modulus, or NULL
448 * \param P MPI to hold the first prime factor of N, or NULL
449 * \param Q MPI to hold the second prime factor of N, or NULL
450 * \param D MPI to hold the private exponent, or NULL
451 * \param E MPI to hold the public exponent, or NULL
452 *
Hanno Beckered203612017-09-29 13:34:25 +0100453 * \return
454 * - 0 if successful. In this case, the non-NULL buffers
455 * pointed to by N, P, Q, D, E are fully written, with
456 * additional unused space filled leading by 0-bytes.
457 * - Non-zero return code otherwise. In particular, if
458 * exporting the requested parameters
459 * cannot be done because of a lack of functionality
460 * or because of security policies, the error code
461 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
462 * In this case, the RSA context stays intact and can
463 * be continued to be used.
Hanno Becker91c194d2017-09-29 12:50:12 +0100464 *
Hanno Beckered203612017-09-29 13:34:25 +0100465 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100466 * would be the following: Firstly, it might be that an
467 * alternative RSA implementation is in use which stores
468 * the key externally, and which either cannot or should not
469 * export it into RAM. Alternatively, an implementation
470 * (regardless of SW or HW) might not support deducing e.g.
471 * P, Q from N, D, E if the former are not part of the
472 * implementation.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100473 *
474 */
475int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
476 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
477 mbedtls_mpi *D, mbedtls_mpi *E );
478
479/**
480 * \brief Export core parameters of an RSA key
481 * in raw big-endian binary format
482 *
483 * \param ctx Initialized RSA context
484 * \param N Byte array to store the RSA modulus, or NULL
485 * \param N_len Size of buffer for modulus
486 * \param P Byte array to hold the first prime factor of N, or NULL
487 * \param P_len Size of buffer for first prime factor
488 * \param Q Byte array to hold the second prime factor of N, or NULL
489 * \param Q_len Size of buffer for second prime factor
490 * \param D Byte array to hold the private exponent, or NULL
491 * \param D_len Size of buffer for private exponent
492 * \param E Byte array to hold the public exponent, or NULL
493 * \param E_len Size of buffer for public exponent
494 *
495 * \note The length fields are ignored if the corresponding
496 * buffer pointers are NULL.
497 *
Hanno Beckered203612017-09-29 13:34:25 +0100498 * \return
499 * - 0 if successful. In this case, the non-NULL buffers
500 * pointed to by N, P, Q, D, E are fully written, with
501 * additional unused space filled leading by 0-bytes.
502 * - Non-zero return code otherwise. In particular, if
503 * exporting the requested parameters
504 * cannot be done because of a lack of functionality
505 * or because of security policies, the error code
506 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
507 * In this case, the RSA context stays intact and can
508 * be continued to be used.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100509 *
Hanno Beckered203612017-09-29 13:34:25 +0100510 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100511 * would be the following: Firstly, it might be that an
512 * alternative RSA implementation is in use which stores
513 * the key externally, and which either cannot or should not
514 * export it into RAM. Alternatively, an implementation
515 * (regardless of SW or HW) might not support deducing e.g.
516 * P, Q from N, D, E if the former are not part of the
517 * implementation.
518 *
519 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100520 */
521int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
522 unsigned char *N, size_t N_len,
523 unsigned char *P, size_t P_len,
524 unsigned char *Q, size_t Q_len,
525 unsigned char *D, size_t D_len,
526 unsigned char *E, size_t E_len );
527
528/**
529 * \brief Export CRT parameters of a private RSA key
530 *
531 * \param ctx Initialized RSA context
532 * \param DP MPI to hold D modulo P-1, or NULL
533 * \param DQ MPI to hold D modulo Q-1, or NULL
534 * \param QP MPI to hold modular inverse of Q modulo P, or NULL
535 *
536 * \return 0 if successful, non-zero error code otherwise.
537 *
538 * \note Alternative RSA implementations not using CRT-parameters
539 * internally can implement this function using based on
540 * \c mbedtls_rsa_deduce_opt.
541 *
542 */
543int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
544 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
545
Paul Bakker5121ce52009-01-03 21:22:43 +0000546/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100547 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100549 *
550 * \param ctx RSA context to be set
Hanno Becker4b2f6912017-09-29 13:34:55 +0100551 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
552 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100553 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100554void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
555 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100556
557/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100558 * \brief Get length of RSA modulus in bytes
559 *
560 * \param ctx Initialized RSA context
561 *
562 * \return Length of RSA modulus, in bytes.
563 *
564 */
565size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
566
567/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 * \brief Generate an RSA keypair
569 *
570 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000571 * \param f_rng RNG function
572 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000573 * \param nbits size of the public key in bits
574 * \param exponent public exponent (e.g., 65537)
575 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000577 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100579 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100582 int (*f_rng)(void *, unsigned char *, size_t),
583 void *p_rng,
584 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
586/**
Hanno Becker43a08d02017-10-02 13:16:35 +0100587 * \brief Check if a context contains (at least) an RSA public key
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 *
589 * \param ctx RSA context to be checked
590 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100591 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
592 * On success, it is guaranteed that enough information is
593 * present to perform an RSA public key operation
594 * \c mbedtls_rsa_public.
595 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599/**
Hanno Becker43a08d02017-10-02 13:16:35 +0100600 * \brief Check if a context contains an RSA private key
601 * and perform basic sanity checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000602 *
603 * \param ctx RSA context to be checked
604 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100605 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
606 * On success, it is guaranteed that enough information is
607 * present to perform RSA private and public key operations.
608 *
609 * \warning This function is *not* obliged to perform an exhaustive
610 * sanity check what would guarantee the internal parameters
611 * to match and \c mbedtls_rsa_private and \c mbedtls_rsa_public
612 * to be mutually inverse to each other.
613 * The reason is that for minimal non-CRT implementations
614 * using only N, D, E, for example, checking the validity
615 * would be computationally expensive.
616 * Users mistrusting their key material should use other
617 * means for verification; see the documentation of
618 * \c mbedtls_rsa_complete.
619 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
623/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100624 * \brief Check a public-private RSA key pair.
625 * Check each of the contexts, and make sure they match.
626 *
627 * \param pub RSA context holding the public key
628 * \param prv RSA context holding the private key
629 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100630 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100633
634/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 * \brief Do an RSA public key operation
636 *
637 * \param ctx RSA context
638 * \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 This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800644 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000645 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000646 *
647 * \note The input and output buffers must be large
648 * enough (eg. 128 bytes if RSA-1024 is used).
649 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000651 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000652 unsigned char *output );
653
654/**
655 * \brief Do an RSA private key operation
656 *
657 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200658 * \param f_rng RNG function (Needed for blinding)
659 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000660 * \param input input buffer
661 * \param output output buffer
662 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100663 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000664 *
665 * \note The input and output buffers must be large
666 * enough (eg. 128 bytes if RSA-1024 is used).
667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200669 int (*f_rng)(void *, unsigned char *, size_t),
670 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000671 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 unsigned char *output );
673
674/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100675 * \brief Generic wrapper to perform a PKCS#1 encryption using the
676 * mode from the context. Add the message padding, then do an
677 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000678 *
679 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200680 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100681 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000682 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100683 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000684 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 * \param input buffer holding the data to be encrypted
686 * \param output buffer that will hold the ciphertext
687 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100688 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000689 *
690 * \note The output buffer must be as large as the size
691 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
692 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000694 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000695 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000696 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000697 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 unsigned char *output );
699
700/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100701 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
702 *
703 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100704 * \param f_rng RNG function (Needed for padding and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100705 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100706 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100707 * \param ilen contains the plaintext length
708 * \param input buffer holding the data to be encrypted
709 * \param output buffer that will hold the ciphertext
710 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100711 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100712 *
713 * \note The output buffer must be as large as the size
714 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100717 int (*f_rng)(void *, unsigned char *, size_t),
718 void *p_rng,
719 int mode, size_t ilen,
720 const unsigned char *input,
721 unsigned char *output );
722
723/**
724 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
725 *
726 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200727 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100728 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100729 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100730 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100731 * \param label buffer holding the custom label to use
732 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100733 * \param ilen contains the plaintext length
734 * \param input buffer holding the data to be encrypted
735 * \param output buffer that will hold the ciphertext
736 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100737 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100738 *
739 * \note The output buffer must be as large as the size
740 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
741 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100743 int (*f_rng)(void *, unsigned char *, size_t),
744 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100745 int mode,
746 const unsigned char *label, size_t label_len,
747 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100748 const unsigned char *input,
749 unsigned char *output );
750
751/**
752 * \brief Generic wrapper to perform a PKCS#1 decryption using the
753 * mode from the context. Do an RSA operation, then remove
754 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000755 *
756 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100757 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200758 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100759 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000760 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000761 * \param input buffer holding the encrypted data
762 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000763 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000764 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100765 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000766 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100767 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100768 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100769 * if RSA-1024 is used) to be able to hold an arbitrary
770 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100771 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100772 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100773 *
774 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100775 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200778 int (*f_rng)(void *, unsigned char *, size_t),
779 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000780 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000781 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000782 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000783 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000784
785/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100786 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
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
Paul Bakkerb3869132013-02-28 17:21:01 +0100792 * \param olen will contain the plaintext length
793 * \param input buffer holding the encrypted data
794 * \param output buffer that will hold the plaintext
795 * \param output_max_len maximum length of the output buffer
796 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100797 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100798 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100799 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100800 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100801 * if RSA-1024 is used) to be able to hold an arbitrary
802 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100803 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100804 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100805 *
806 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100807 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100808 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200810 int (*f_rng)(void *, unsigned char *, size_t),
811 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100812 int mode, size_t *olen,
813 const unsigned char *input,
814 unsigned char *output,
815 size_t output_max_len );
816
817/**
818 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
819 *
820 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100821 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200822 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100823 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100824 * \param label buffer holding the custom label to use
825 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100826 * \param olen will contain the plaintext length
827 * \param input buffer holding the encrypted data
828 * \param output buffer that will hold the plaintext
829 * \param output_max_len maximum length of the output buffer
830 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100831 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100832 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100833 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100834 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100835 * if RSA-1024 is used) to be able to hold an arbitrary
836 * decrypted message. If it is not large enough to hold
Hanno Becker8fd55482017-08-23 14:07:48 +0100837 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100838 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100839 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100840 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100841 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200844 int (*f_rng)(void *, unsigned char *, size_t),
845 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100846 int mode,
847 const unsigned char *label, size_t label_len,
848 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100849 const unsigned char *input,
850 unsigned char *output,
851 size_t output_max_len );
852
853/**
854 * \brief Generic wrapper to perform a PKCS#1 signature using the
855 * mode from the context. Do a private RSA operation to sign
856 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000857 *
858 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200859 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100860 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000861 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100862 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
863 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for
864 * signing raw data)
865 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000866 * \param hash buffer holding the message digest
867 * \param sig buffer that will hold the ciphertext
868 *
869 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100870 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000871 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100872 * \note The \c sig buffer must be as large as the size
873 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000874 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200875 * \note In case of PKCS#1 v2.1 encoding, see comments on
Hanno Becker4b2f6912017-09-29 13:34:55 +0100876 * \note \c mbedtls_rsa_rsassa_pss_sign() for details on
877 * \c md_alg and \c hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000880 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000881 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000882 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000884 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000885 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000886 unsigned char *sig );
887
888/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100889 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
890 *
891 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100892 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200893 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100894 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
895 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
896 * for signing raw data)
897 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100898 * \param hash buffer holding the message digest
899 * \param sig buffer that will hold the ciphertext
900 *
901 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100902 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100903 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100904 * \note The \c sig buffer must be as large as the size
905 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100906 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200908 int (*f_rng)(void *, unsigned char *, size_t),
909 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100910 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100912 unsigned int hashlen,
913 const unsigned char *hash,
914 unsigned char *sig );
915
916/**
917 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
918 *
919 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200920 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100921 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100922 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100923 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
924 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
925 * for signing raw data)
926 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100927 * \param hash buffer holding the message digest
928 * \param sig buffer that will hold the ciphertext
929 *
930 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100931 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100932 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100933 * \note The \c sig buffer must be as large as the size
934 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100935 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100936 * \note The \c hash_id in the RSA context is the one used for the
937 * encoding. \c md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100938 * that is encoded. According to RFC 3447 it is advised to
939 * keep both hashes the same.
940 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100942 int (*f_rng)(void *, unsigned char *, size_t),
943 void *p_rng,
944 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100946 unsigned int hashlen,
947 const unsigned char *hash,
948 unsigned char *sig );
949
950/**
951 * \brief Generic wrapper to perform a PKCS#1 verification using the
952 * mode from the context. Do a public RSA operation and check
953 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000954 *
955 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100956 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200957 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100958 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
959 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
960 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000961 * \param hash buffer holding the message digest
962 * \param sig buffer holding the ciphertext
963 *
964 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100965 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000966 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100967 * \note The \c sig buffer must be as large as the size
968 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000969 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200970 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000972 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200974 int (*f_rng)(void *, unsigned char *, size_t),
975 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000976 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000978 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000979 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200980 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000981
982/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100983 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
984 *
985 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100986 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200987 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100988 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
989 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
990 * for signing raw data)
991 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100992 * \param hash buffer holding the message digest
993 * \param sig buffer holding the ciphertext
994 *
995 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100996 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100997 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100998 * \note The \c sig buffer must be as large as the size
999 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +01001000 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001002 int (*f_rng)(void *, unsigned char *, size_t),
1003 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001004 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001006 unsigned int hashlen,
1007 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001008 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001009
1010/**
1011 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001012 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +01001013 *
1014 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +01001015 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +02001016 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +01001017 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
1018 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
1019 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +01001020 * \param hash buffer holding the message digest
1021 * \param sig buffer holding the ciphertext
1022 *
1023 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001024 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +01001025 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001026 * \note The \c sig buffer must be as large as the size
1027 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +01001028 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001029 * \note The \c hash_id in the RSA context is the one used for the
1030 * verification. \c md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001031 * hash that is verified. According to RFC 3447 it is advised to
Hanno Becker4b2f6912017-09-29 13:34:55 +01001032 * keep both hashes the same. If \c hash_id in the RSA context is
1033 * unset, the \c md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001034 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001036 int (*f_rng)(void *, unsigned char *, size_t),
1037 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001038 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001040 unsigned int hashlen,
1041 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001042 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001043
1044/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001045 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
1046 * (This is the version with "full" options.)
1047 *
1048 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +01001049 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001050 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +01001051 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
1052 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
1053 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001054 * \param hash buffer holding the message digest
1055 * \param mgf1_hash_id message digest used for mask generation
1056 * \param expected_salt_len Length of the salt used in padding, use
Hanno Becker4b2f6912017-09-29 13:34:55 +01001057 * \c MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001058 * \param sig buffer holding the ciphertext
1059 *
1060 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001061 * or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001062 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001063 * \note The \c sig buffer must be as large as the size
1064 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001065 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001066 * \note The \c hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001069 int (*f_rng)(void *, unsigned char *, size_t),
1070 void *p_rng,
1071 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001073 unsigned int hashlen,
1074 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001076 int expected_salt_len,
1077 const unsigned char *sig );
1078
1079/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001080 * \brief Copy the components of an RSA context
1081 *
1082 * \param dst Destination context
1083 * \param src Source context
1084 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001085 * \return 0 on success,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001086 * \c MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001089
1090/**
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001092 *
1093 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +00001094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001096
1097/**
1098 * \brief Checkup routine
1099 *
1100 * \return 0 if successful, or 1 if the test failed
1101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001103
1104#ifdef __cplusplus
1105}
1106#endif
1107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108#endif /* MBEDTLS_RSA_C */
Paul Bakkered27a042013-04-18 22:46:23 +02001109
Paul Bakker5121ce52009-01-03 21:22:43 +00001110#endif /* rsa.h */