blob: d3347fc0318e6c06b7042cdaea78a98168c65fa9 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief The RSA public-key cryptosystem
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_RSA_H
24#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020027#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakkered27a042013-04-18 22:46:23 +020031
Paul Bakker314052f2011-08-15 09:07:52 +000032#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020033#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_THREADING_C)
Paul Bakkerc9965dc2013-09-29 14:58:17 +020036#include "threading.h"
37#endif
38
Paul Bakker13e2dfe2009-07-28 07:18:38 +000039/*
40 * RSA Error codes
41 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
43#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
44#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +020045#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the library's validity check. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
47#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
48#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
49#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
50#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Hanno Becker91c194d2017-09-29 12:50:12 +010051#define MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED -0x4500 /**< The requested parameter export is not possible/allowed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000052
53/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020054 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define MBEDTLS_RSA_PUBLIC 0
57#define MBEDTLS_RSA_PRIVATE 1
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define MBEDTLS_RSA_PKCS_V15 0
60#define MBEDTLS_RSA_PKCS_V21 1
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define MBEDTLS_RSA_SIGN 1
63#define MBEDTLS_RSA_CRYPT 2
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020066
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020067/*
68 * The above constants may be used even if the RSA module is compile out,
69 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
70 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020072
Paul Bakker407a0da2013-06-27 14:29:21 +020073#ifdef __cplusplus
74extern "C" {
75#endif
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/**
Hanno Beckera3ebec22017-08-23 14:06:24 +010078 * Helper functions for RSA-related operations on MPI's.
79 */
80
81/**
82 * \brief Compute RSA prime moduli P, Q from public modulus N=PQ
Hanno Beckerb0c5edc2017-08-23 22:16:10 +010083 * and a pair of private and public key.
Hanno Beckera3ebec22017-08-23 14:06:24 +010084 *
85 * \note This is a 'static' helper function not operating on
86 * an RSA context. Alternative implementations need not
87 * overwrite it.
88 *
89 * \param N RSA modulus N = PQ, with P, Q to be found
90 * \param D RSA private exponent
91 * \param E RSA public exponent
92 * \param f_rng PRNG to be used for randomization, or NULL
93 * \param p_rng PRNG context for f_rng, or NULL
94 * \param P Pointer to MPI holding first prime factor of N on success
95 * \param Q Pointer to MPI holding second prime factor of N on success
96 *
Hanno Beckered203612017-09-29 13:34:25 +010097 * \return
98 * - 0 if successful. In this case, P and Q constitute a
Hanno Beckerba5b7552017-10-02 09:55:49 +010099 * factorization of N.
100 * - A non-zero error code otherwise.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100101 *
Hanno Becker1b831fe2017-10-02 12:24:50 +0100102 * \note It is neither checked that P, Q are prime nor that
103 * D, E are modular inverses wrt. P-1 and Q-1. For that,
104 * use the helper function \c mbedtls_rsa_validate_params.
105 *
Hanno Beckera3ebec22017-08-23 14:06:24 +0100106 */
Hanno Beckerba5b7552017-10-02 09:55:49 +0100107int mbedtls_rsa_deduce_moduli( mbedtls_mpi const *N, mbedtls_mpi const *D,
108 mbedtls_mpi const *E, int (*f_rng)(void *, unsigned char *, size_t),
109 void *p_rng, mbedtls_mpi *P, mbedtls_mpi *Q );
Hanno Beckera3ebec22017-08-23 14:06:24 +0100110
111/**
112 * \brief Compute RSA private exponent from
113 * prime moduli and public key.
114 *
115 * \note This is a 'static' helper function not operating on
116 * an RSA context. Alternative implementations need not
117 * overwrite it.
118 *
119 * \param P First prime factor of RSA modulus
120 * \param Q Second prime factor of RSA modulus
121 * \param E RSA public exponent
122 * \param D Pointer to MPI holding the private exponent on success.
123 *
Hanno Beckered203612017-09-29 13:34:25 +0100124 * \return
125 * - 0 if successful. In this case, D is set to a simultaneous
Hanno Beckera3ebec22017-08-23 14:06:24 +0100126 * modular inverse of E modulo both P-1 and Q-1.
Hanno Beckerbdefff12017-10-02 09:57:50 +0100127 * - A non-zero error code otherwise.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100128 *
Hanno Becker1b831fe2017-10-02 12:24:50 +0100129 * \note This function does not check whether P and Q are primes.
130 *
Hanno Beckera3ebec22017-08-23 14:06:24 +0100131 */
Hanno Beckerbdefff12017-10-02 09:57:50 +0100132int mbedtls_rsa_deduce_private( mbedtls_mpi const *P, mbedtls_mpi const *Q,
133 mbedtls_mpi const *E, mbedtls_mpi *D );
Hanno Beckera3ebec22017-08-23 14:06:24 +0100134
135
136/**
137 * \brief Generate RSA-CRT parameters
138 *
139 * \note This is a 'static' helper function not operating on
140 * an RSA context. Alternative implementations need not
141 * overwrite it.
142 *
143 * \param P First prime factor of N
144 * \param Q Second prime factor of N
145 * \param D RSA private exponent
146 * \param DP Output variable for D modulo P-1
147 * \param DQ Output variable for D modulo Q-1
148 * \param QP Output variable for the modular inverse of Q modulo P.
149 *
150 * \return 0 on success, non-zero error code otherwise.
151 *
Hanno Becker1b831fe2017-10-02 12:24:50 +0100152 * \note This function does not check whether P, Q are
153 * prime and whether D is a valid private exponent.
154 *
Hanno Beckera3ebec22017-08-23 14:06:24 +0100155 */
156int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
157 const mbedtls_mpi *D, mbedtls_mpi *DP,
158 mbedtls_mpi *DQ, mbedtls_mpi *QP );
159
160
161/**
162 * \brief Check validity of core RSA parameters
163 *
164 * \note This is a 'static' helper function not operating on
165 * an RSA context. Alternative implementations need not
166 * overwrite it.
167 *
168 * \param N RSA modulus N = PQ
169 * \param P First prime factor of N
170 * \param Q Second prime factor of N
171 * \param D RSA private exponent
172 * \param E RSA public exponent
Hanno Becker56bae952017-09-29 15:33:10 +0100173 * \param f_rng PRNG to be used for primality check, or NULL
Hanno Beckera3ebec22017-08-23 14:06:24 +0100174 * \param p_rng PRNG context for f_rng, or NULL
175 *
Hanno Beckered203612017-09-29 13:34:25 +0100176 * \return
177 * - 0 if the following conditions are satisfied:
178 * - N = PQ if N,P,Q != NULL
179 * - D and E are modular inverses modulo P-1 and Q-1
180 * if D,E,P,Q != NULL
181 * - P prime if f_rng, P != NULL
182 * - Q prime if f_rng, Q != NULL
Hanno Becker750e8b42017-08-25 07:54:27 +0100183 * - A non-zero error code otherwise.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100184 *
185 * \note The function can be used with a restricted set of arguments
186 * to perform specific checks only. E.g., calling it with
187 * (-,P,-,-,-) and a PRNG amounts to a primality check for P.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100188 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100189int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
190 const mbedtls_mpi *Q, const mbedtls_mpi *D,
191 const mbedtls_mpi *E,
192 int (*f_rng)(void *, unsigned char *, size_t),
193 void *p_rng );
Hanno Beckera3ebec22017-08-23 14:06:24 +0100194
195/**
Hanno Beckerd3637992017-08-25 07:55:03 +0100196 * \brief Check validity of RSA CRT parameters
197 *
198 * \note This is a 'static' helper function not operating on
199 * an RSA context. Alternative implementations need not
200 * overwrite it.
201 *
202 * \param P First prime factor of RSA modulus
203 * \param Q Second prime factor of RSA modulus
204 * \param D RSA private exponent
205 * \param DP MPI to check for D modulo P-1
206 * \param DQ MPI to check for D modulo P-1
207 * \param QP MPI to check for the modular inverse of Q modulo P.
208 *
Hanno Beckered203612017-09-29 13:34:25 +0100209 * \return
210 * - 0 if the following conditions are satisfied:
211 * - D = DP mod P-1 if P, D, DP != NULL
212 * - Q = DQ mod P-1 if P, D, DQ != NULL
213 * - QP = Q^-1 mod P if P, Q, QP != NULL
214 * - \c MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if check failed,
215 * potentially including \c MBEDTLS_ERR_MPI_XXX if some
Hanno Beckerd3637992017-08-25 07:55:03 +0100216 * MPI calculations failed.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100217 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if insufficient
Hanno Beckerd3637992017-08-25 07:55:03 +0100218 * data was provided to check DP, DQ or QP.
219 *
220 * \note The function can be used with a restricted set of arguments
221 * to perform specific checks only. E.g., calling it with the
222 * parameters (P, -, D, DP, -, -) will check DP = D mod P-1.
223 */
224int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
225 const mbedtls_mpi *D, const mbedtls_mpi *DP,
226 const mbedtls_mpi *DQ, const mbedtls_mpi *QP );
227
228/**
Hanno Beckera3ebec22017-08-23 14:06:24 +0100229 * Implementation of RSA interface
230 */
231
Hanno Beckerab377312017-08-23 16:24:51 +0100232#if !defined(MBEDTLS_RSA_ALT)
233
Hanno Beckera3ebec22017-08-23 14:06:24 +0100234/**
Hanno Becker5063cd22017-09-29 11:49:12 +0100235 * \brief RSA context structure
236 *
237 * \note Direct manipulation of the members of this structure
238 * is deprecated and will no longer be supported starting
239 * from the next major release. All manipulation should instead
240 * be done through the public interface functions.
241 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 */
243typedef struct
244{
245 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +0000246 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_mpi N; /*!< public modulus */
249 mbedtls_mpi E; /*!< public exponent */
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_mpi D; /*!< private exponent */
252 mbedtls_mpi P; /*!< 1st prime factor */
253 mbedtls_mpi Q; /*!< 2nd prime factor */
Hanno Becker1a59e792017-08-23 07:41:10 +0100254
255#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_mpi DP; /*!< D % (P - 1) */
257 mbedtls_mpi DQ; /*!< D % (Q - 1) */
258 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Hanno Becker1a59e792017-08-23 07:41:10 +0100259#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_mpi RN; /*!< cached R^2 mod N */
Hanno Becker1a59e792017-08-23 07:41:10 +0100262
263#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 mbedtls_mpi RP; /*!< cached R^2 mod P */
265 mbedtls_mpi RQ; /*!< cached R^2 mod Q */
Hanno Becker1a59e792017-08-23 07:41:10 +0100266#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_mpi Vi; /*!< cached blinding value */
269 mbedtls_mpi Vf; /*!< cached un-blinding value */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200270
Hanno Becker4b2f6912017-09-29 13:34:55 +0100271 int padding; /*!< \c MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
272 \c MBEDTLS_RSA_PKCS_v21 for OAEP/PSS */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
274 specified in the mbedtls_md.h header file
Paul Bakker9dcc3222011-03-08 14:16:06 +0000275 for the EME-OAEP and EMSA-PSS
276 encoding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#if defined(MBEDTLS_THREADING_C)
278 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200279#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000280}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000282
Hanno Beckerab377312017-08-23 16:24:51 +0100283#else
284
285#include "rsa_alt.h"
286
287#endif /* MBEDTLS_RSA_ALT */
288
Paul Bakker5121ce52009-01-03 21:22:43 +0000289/**
290 * \brief Initialize an RSA context
291 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100292 * Note: Set padding to \c MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000293 * encryption scheme and the RSASSA-PSS signature scheme.
294 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 * \param ctx RSA context to be initialized
Hanno Becker4b2f6912017-09-29 13:34:55 +0100296 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
297 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 *
299 * \note The hash_id parameter is actually ignored
Hanno Becker4b2f6912017-09-29 13:34:55 +0100300 * when using \c MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200301 *
302 * \note Choice of padding mode is strictly enforced for private key
303 * operations, since there might be security concerns in
304 * mixing padding modes. For public key operations it's merely
305 * a default value, which can be overriden by calling specific
306 * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
307 *
308 * \note The chosen hash is always used for OEAP encryption.
309 * For PSS signatures, it's always used for making signatures,
310 * but can be overriden (and always is, if set to
Hanno Becker4b2f6912017-09-29 13:34:55 +0100311 * \c MBEDTLS_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100314 int padding,
315 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000316
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100317/**
318 * \brief Import a set of core parameters into an RSA context
319 *
320 * \param ctx Initialized RSA context to store parameters
321 * \param N RSA modulus, or NULL
322 * \param P First prime factor of N, or NULL
323 * \param Q Second prime factor of N, or NULL
324 * \param D Private exponent, or NULL
325 * \param E Public exponent, or NULL
326 *
327 * \note This function can be called multiple times for successive
328 * imports if the parameters are not simultaneously present.
329 * Any sequence of calls to this function should be followed
330 * by a call to \c mbedtls_rsa_complete which will check
331 * and complete the provided information to a ready-for-use
332 * public or private RSA key.
333 *
334 * \return 0 if successful, non-zero error code on failure.
335 */
336int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
337 const mbedtls_mpi *N,
338 const mbedtls_mpi *P, const mbedtls_mpi *Q,
339 const mbedtls_mpi *D, const mbedtls_mpi *E );
340
341/**
342 * \brief Import core RSA parameters in raw big-endian
343 * binary format into an RSA context
344 *
345 * \param ctx Initialized RSA context to store parameters
346 * \param N RSA modulus, or NULL
347 * \param N_len Byte length of N, ignored if N == NULL
348 * \param P First prime factor of N, or NULL
349 * \param P_len Byte length of P, ignored if P == NULL
350 * \param Q Second prime factor of N, or NULL
351 * \param Q_len Byte length of Q, ignored if Q == NULL
352 * \param D Private exponent, or NULL
353 * \param D_len Byte length of D, ignored if D == NULL
354 * \param E Public exponent, or NULL
355 * \param E_len Byte length of E, ignored if E == NULL
356 *
357 * \note This function can be called multiple times for successive
358 * imports if the parameters are not simultaneously present.
359 * Any sequence of calls to this function should be followed
360 * by a call to \c mbedtls_rsa_complete which will check
361 * and complete the provided information to a ready-for-use
362 * public or private RSA key.
363 *
364 * \return 0 if successful, non-zero error code on failure.
365 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100366int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100367 unsigned char const *N, size_t N_len,
368 unsigned char const *P, size_t P_len,
369 unsigned char const *Q, size_t Q_len,
370 unsigned char const *D, size_t D_len,
371 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100372
373/**
374 * \brief Attempt to complete an RSA context from
375 * a set of imported core parameters.
376 *
377 * \param ctx Initialized RSA context to store parameters
Hanno Becker43a08d02017-10-02 13:16:35 +0100378 * \param f_rng RNG function, or NULL
379 * \param p_rng RNG parameter, or NULL
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100380 *
Hanno Beckered203612017-09-29 13:34:25 +0100381 * \note
382 * - To setup an RSA public key, precisely N and E
383 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100384 *
Hanno Beckered203612017-09-29 13:34:25 +0100385 * - To setup an RSA private key, enough information must be
Hanno Becker98838b02017-10-02 13:16:10 +0100386 * present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100387 *
Hanno Beckered203612017-09-29 13:34:25 +0100388 * The default implementation supports the following:
389 * - Derive P, Q from N, D, E
390 * - Derive N, D from P, Q, E.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100391 *
Hanno Beckered203612017-09-29 13:34:25 +0100392 * - Alternative implementations need not support these
393 * and may return \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA instead.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100394 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100395 * \note The PRNG is used for the probabilistic algorithm
396 * used in the derivation of P, Q from N, D, E. If it
397 * not present, a deterministic heuristic is used.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100398 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100399 * \return
400 * - 0 if successful. In this case, it is guaranteed
401 * the functions \c mbedtls_rsa_check_pubkey resp.
402 * \c mbedtls_rsa_check_privkey pass in case of a
403 * public resp. private key.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100404 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100405 * derivations failed.
Hanno Becker43a08d02017-10-02 13:16:35 +0100406 *
407 * \warning Implementations are *not* obliged to perform exhaustive
408 * validation of the imported parameters!
409 * In particular, parameters that are not needed by the
410 * implementation may be silently discarded and left unchecked.
411 * If the user mistrusts the given key material, he should
412 * employ other means for verification like the helper functions
413 * \c mbedtls_rsa_validate_params, \c mbedtls_rsa_validate_crt.
414 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100415 */
416int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
417 int (*f_rng)(void *, unsigned char *, size_t),
418 void *p_rng );
419
420/**
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 */
Hanno Becker98838b02017-10-02 13:16:10 +0100632int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
633 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100634
635/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000636 * \brief Do an RSA public key operation
637 *
638 * \param ctx RSA context
639 * \param input input buffer
640 * \param output output buffer
641 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100642 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000643 *
644 * \note This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800645 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000646 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 *
648 * \note The input and output buffers must be large
649 * enough (eg. 128 bytes if RSA-1024 is used).
650 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000652 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 unsigned char *output );
654
655/**
656 * \brief Do an RSA private key operation
657 *
658 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200659 * \param f_rng RNG function (Needed for blinding)
660 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 * \param input input buffer
662 * \param output output buffer
663 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100664 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 *
666 * \note The input and output buffers must be large
667 * enough (eg. 128 bytes if RSA-1024 is used).
668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200670 int (*f_rng)(void *, unsigned char *, size_t),
671 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000672 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000673 unsigned char *output );
674
675/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100676 * \brief Generic wrapper to perform a PKCS#1 encryption using the
677 * mode from the context. Add the message padding, then do an
678 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000679 *
680 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200681 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100682 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000683 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100684 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000685 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000686 * \param input buffer holding the data to be encrypted
687 * \param output buffer that will hold the ciphertext
688 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100689 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000690 *
691 * \note The output buffer must be as large as the size
692 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000695 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000696 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000697 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000698 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000699 unsigned char *output );
700
701/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100702 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
703 *
704 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100705 * \param f_rng RNG function (Needed for padding and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100706 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100707 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100708 * \param ilen contains the plaintext length
709 * \param input buffer holding the data to be encrypted
710 * \param output buffer that will hold the ciphertext
711 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100712 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100713 *
714 * \note The output buffer must be as large as the size
715 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100718 int (*f_rng)(void *, unsigned char *, size_t),
719 void *p_rng,
720 int mode, size_t ilen,
721 const unsigned char *input,
722 unsigned char *output );
723
724/**
725 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
726 *
727 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200728 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100729 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100730 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100731 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100732 * \param label buffer holding the custom label to use
733 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100734 * \param ilen contains the plaintext length
735 * \param input buffer holding the data to be encrypted
736 * \param output buffer that will hold the ciphertext
737 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100738 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100739 *
740 * \note The output buffer must be as large as the size
741 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
742 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100744 int (*f_rng)(void *, unsigned char *, size_t),
745 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100746 int mode,
747 const unsigned char *label, size_t label_len,
748 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100749 const unsigned char *input,
750 unsigned char *output );
751
752/**
753 * \brief Generic wrapper to perform a PKCS#1 decryption using the
754 * mode from the context. Do an RSA operation, then remove
755 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000756 *
757 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100758 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200759 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100760 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000761 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000762 * \param input buffer holding the encrypted data
763 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000764 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000765 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100766 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000767 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100768 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100769 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100770 * if RSA-1024 is used) to be able to hold an arbitrary
771 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100772 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100773 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100774 *
775 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100776 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000777 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200779 int (*f_rng)(void *, unsigned char *, size_t),
780 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000781 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000782 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000783 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000784 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000785
786/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100787 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
788 *
789 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100790 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200791 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100792 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100793 * \param olen will contain the plaintext length
794 * \param input buffer holding the encrypted data
795 * \param output buffer that will hold the plaintext
796 * \param output_max_len maximum length of the output buffer
797 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100798 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100799 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100800 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100801 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100802 * if RSA-1024 is used) to be able to hold an arbitrary
803 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100804 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100805 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100806 *
807 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100808 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200811 int (*f_rng)(void *, unsigned char *, size_t),
812 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100813 int mode, size_t *olen,
814 const unsigned char *input,
815 unsigned char *output,
816 size_t output_max_len );
817
818/**
819 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
820 *
821 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100822 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200823 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100824 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100825 * \param label buffer holding the custom label to use
826 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100827 * \param olen will contain the plaintext length
828 * \param input buffer holding the encrypted data
829 * \param output buffer that will hold the plaintext
830 * \param output_max_len maximum length of the output buffer
831 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100832 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100833 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100834 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100835 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100836 * if RSA-1024 is used) to be able to hold an arbitrary
837 * decrypted message. If it is not large enough to hold
Hanno Becker8fd55482017-08-23 14:07:48 +0100838 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100839 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100840 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100841 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100842 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200845 int (*f_rng)(void *, unsigned char *, size_t),
846 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100847 int mode,
848 const unsigned char *label, size_t label_len,
849 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100850 const unsigned char *input,
851 unsigned char *output,
852 size_t output_max_len );
853
854/**
855 * \brief Generic wrapper to perform a PKCS#1 signature using the
856 * mode from the context. Do a private RSA operation to sign
857 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000858 *
859 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200860 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100861 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000862 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100863 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
864 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for
865 * signing raw data)
866 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000867 * \param hash buffer holding the message digest
868 * \param sig buffer that will hold the ciphertext
869 *
870 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100871 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000872 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100873 * \note The \c sig buffer must be as large as the size
874 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000875 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200876 * \note In case of PKCS#1 v2.1 encoding, see comments on
Hanno Becker4b2f6912017-09-29 13:34:55 +0100877 * \note \c mbedtls_rsa_rsassa_pss_sign() for details on
878 * \c md_alg and \c hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000879 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000881 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000882 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000883 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000885 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000886 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000887 unsigned char *sig );
888
889/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100890 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
891 *
892 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100893 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200894 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100895 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
896 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
897 * for signing raw data)
898 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100899 * \param hash buffer holding the message digest
900 * \param sig buffer that will hold the ciphertext
901 *
902 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100903 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100904 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100905 * \note The \c sig buffer must be as large as the size
906 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200909 int (*f_rng)(void *, unsigned char *, size_t),
910 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100911 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100913 unsigned int hashlen,
914 const unsigned char *hash,
915 unsigned char *sig );
916
917/**
918 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
919 *
920 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200921 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100922 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100923 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100924 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
925 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
926 * for signing raw data)
927 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100928 * \param hash buffer holding the message digest
929 * \param sig buffer that will hold the ciphertext
930 *
931 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100932 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100933 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100934 * \note The \c sig buffer must be as large as the size
935 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100936 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100937 * \note The \c hash_id in the RSA context is the one used for the
938 * encoding. \c md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100939 * that is encoded. According to RFC 3447 it is advised to
940 * keep both hashes the same.
941 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100943 int (*f_rng)(void *, unsigned char *, size_t),
944 void *p_rng,
945 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100947 unsigned int hashlen,
948 const unsigned char *hash,
949 unsigned char *sig );
950
951/**
952 * \brief Generic wrapper to perform a PKCS#1 verification using the
953 * mode from the context. Do a public RSA operation and check
954 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 *
956 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100957 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200958 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100959 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
960 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
961 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000962 * \param hash buffer holding the message digest
963 * \param sig buffer holding the ciphertext
964 *
965 * \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 Bakker5121ce52009-01-03 21:22:43 +0000967 *
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 Bakker9dcc3222011-03-08 14:16:06 +0000970 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200971 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200975 int (*f_rng)(void *, unsigned char *, size_t),
976 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000977 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000979 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000980 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200981 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000982
983/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100984 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
985 *
986 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100987 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200988 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100989 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
990 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
991 * for signing raw data)
992 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100993 * \param hash buffer holding the message digest
994 * \param sig buffer holding the ciphertext
995 *
996 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100997 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100998 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100999 * \note The \c sig buffer must be as large as the size
1000 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +01001001 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001003 int (*f_rng)(void *, unsigned char *, size_t),
1004 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001005 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001007 unsigned int hashlen,
1008 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001009 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001010
1011/**
1012 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001013 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +01001014 *
1015 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +01001016 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +02001017 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +01001018 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
1019 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
1020 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +01001021 * \param hash buffer holding the message digest
1022 * \param sig buffer holding the ciphertext
1023 *
1024 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001025 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +01001026 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001027 * \note The \c sig buffer must be as large as the size
1028 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +01001029 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001030 * \note The \c hash_id in the RSA context is the one used for the
1031 * verification. \c md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001032 * hash that is verified. According to RFC 3447 it is advised to
Hanno Becker4b2f6912017-09-29 13:34:55 +01001033 * keep both hashes the same. If \c hash_id in the RSA context is
1034 * unset, the \c md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001035 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001037 int (*f_rng)(void *, unsigned char *, size_t),
1038 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001039 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001041 unsigned int hashlen,
1042 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001043 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001044
1045/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001046 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
1047 * (This is the version with "full" options.)
1048 *
1049 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +01001050 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001051 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +01001052 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
1053 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
1054 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001055 * \param hash buffer holding the message digest
1056 * \param mgf1_hash_id message digest used for mask generation
1057 * \param expected_salt_len Length of the salt used in padding, use
Hanno Becker4b2f6912017-09-29 13:34:55 +01001058 * \c MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001059 * \param sig buffer holding the ciphertext
1060 *
1061 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001062 * or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001063 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001064 * \note The \c sig buffer must be as large as the size
1065 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001066 *
Hanno Becker4b2f6912017-09-29 13:34:55 +01001067 * \note The \c hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001070 int (*f_rng)(void *, unsigned char *, size_t),
1071 void *p_rng,
1072 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001074 unsigned int hashlen,
1075 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001077 int expected_salt_len,
1078 const unsigned char *sig );
1079
1080/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001081 * \brief Copy the components of an RSA context
1082 *
1083 * \param dst Destination context
1084 * \param src Source context
1085 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001086 * \return 0 on success,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001087 * \c MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001090
1091/**
Paul Bakker5121ce52009-01-03 21:22:43 +00001092 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001093 *
1094 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +00001095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001097
1098/**
1099 * \brief Checkup routine
1100 *
1101 * \return 0 if successful, or 1 if the test failed
1102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001104
1105#ifdef __cplusplus
1106}
1107#endif
1108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109#endif /* MBEDTLS_RSA_C */
Paul Bakkered27a042013-04-18 22:46:23 +02001110
Paul Bakker5121ce52009-01-03 21:22:43 +00001111#endif /* rsa.h */