blob: 734c779c1e382b1e6f96fe47a6442288703eb84b [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. */
Paul Bakker5121ce52009-01-03 21:22:43 +000051
52/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020053 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#define MBEDTLS_RSA_PUBLIC 0
56#define MBEDTLS_RSA_PRIVATE 1
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define MBEDTLS_RSA_PKCS_V15 0
59#define MBEDTLS_RSA_PKCS_V21 1
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#define MBEDTLS_RSA_SIGN 1
62#define MBEDTLS_RSA_CRYPT 2
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020065
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020066/*
67 * The above constants may be used even if the RSA module is compile out,
68 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
69 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020071
Paul Bakker407a0da2013-06-27 14:29:21 +020072#ifdef __cplusplus
73extern "C" {
74#endif
75
Paul Bakker5121ce52009-01-03 21:22:43 +000076/**
Hanno Beckera3ebec22017-08-23 14:06:24 +010077 * Helper functions for RSA-related operations on MPI's.
78 */
79
80/**
81 * \brief Compute RSA prime moduli P, Q from public modulus N=PQ
Hanno Beckerb0c5edc2017-08-23 22:16:10 +010082 * and a pair of private and public key.
Hanno Beckera3ebec22017-08-23 14:06:24 +010083 *
84 * \note This is a 'static' helper function not operating on
85 * an RSA context. Alternative implementations need not
86 * overwrite it.
87 *
88 * \param N RSA modulus N = PQ, with P, Q to be found
89 * \param D RSA private exponent
90 * \param E RSA public exponent
91 * \param f_rng PRNG to be used for randomization, or NULL
92 * \param p_rng PRNG context for f_rng, or NULL
93 * \param P Pointer to MPI holding first prime factor of N on success
94 * \param Q Pointer to MPI holding second prime factor of N on success
95 *
96 * \return - 0 if successful. In this case, P and Q constitute a
97 * factorization of N, and it is guaranteed that D and E
98 * are indeed modular inverses modulo P-1 and modulo Q-1.
99 * The values of N, D and E are unchanged. It is checked
100 * that P, Q are prime if a PRNG is provided.
101 * - A non-zero error code otherwise. In this case, the values
102 * of N, D, E are undefined.
103 *
104 * \note The input MPI's are deliberately not declared as constant
105 * and may therefore be used for in-place calculations by
106 * the implementation. In particular, their values can be
107 * corrupted when the function fails. If the user cannot
108 * tolerate this, he has to make copies of the MPI's prior
109 * to calling this function. See \c mbedtls_mpi_copy for this.
110 */
111int mbedtls_rsa_deduce_moduli( mbedtls_mpi *N, mbedtls_mpi *D, mbedtls_mpi *E,
112 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
113 mbedtls_mpi *P, mbedtls_mpi *Q );
114
115/**
116 * \brief Compute RSA private exponent from
117 * prime moduli and public key.
118 *
119 * \note This is a 'static' helper function not operating on
120 * an RSA context. Alternative implementations need not
121 * overwrite it.
122 *
123 * \param P First prime factor of RSA modulus
124 * \param Q Second prime factor of RSA modulus
125 * \param E RSA public exponent
126 * \param D Pointer to MPI holding the private exponent on success.
127 *
128 * \note This function does not check whether P and Q are primes.
129 *
130 * \return - 0 if successful. In this case, D is set to a simultaneous
131 * modular inverse of E modulo both P-1 and Q-1.
132 * - A non-zero error code otherwise. In this case, the values
133 * of P, Q, E are undefined.
134 *
135 * \note The input MPI's are deliberately not declared as constant
136 * and may therefore be used for in-place calculations by
137 * the implementation. In particular, their values can be
138 * corrupted when the function fails. If the user cannot
139 * tolerate this, he has to make copies of the MPI's prior
140 * to calling this function. See \c mbedtls_mpi_copy for this.
141 */
142int mbedtls_rsa_deduce_private( mbedtls_mpi *P, mbedtls_mpi *Q, mbedtls_mpi *E,
143 mbedtls_mpi *D );
144
145
146/**
147 * \brief Generate RSA-CRT parameters
148 *
149 * \note This is a 'static' helper function not operating on
150 * an RSA context. Alternative implementations need not
151 * overwrite it.
152 *
153 * \param P First prime factor of N
154 * \param Q Second prime factor of N
155 * \param D RSA private exponent
156 * \param DP Output variable for D modulo P-1
157 * \param DQ Output variable for D modulo Q-1
158 * \param QP Output variable for the modular inverse of Q modulo P.
159 *
160 * \return 0 on success, non-zero error code otherwise.
161 *
162 */
163int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
164 const mbedtls_mpi *D, mbedtls_mpi *DP,
165 mbedtls_mpi *DQ, mbedtls_mpi *QP );
166
167
168/**
169 * \brief Check validity of core RSA parameters
170 *
171 * \note This is a 'static' helper function not operating on
172 * an RSA context. Alternative implementations need not
173 * overwrite it.
174 *
175 * \param N RSA modulus N = PQ
176 * \param P First prime factor of N
177 * \param Q Second prime factor of N
178 * \param D RSA private exponent
179 * \param E RSA public exponent
180 * \param f_rng PRNG to be used for randomization, or NULL
181 * \param p_rng PRNG context for f_rng, or NULL
182 *
183 * \return - 0 if the following conditions are satisfied:
184 * - N = PQ if N,P,Q != NULL
185 * - D and E are modular inverses modulo P-1 and Q-1
186 * if D,E,P,Q != NULL
187 * - P prime if f_rng, P != NULL
188 * - Q prime if f_rng, Q != NULL
Hanno Becker750e8b42017-08-25 07:54:27 +0100189 * - A non-zero error code otherwise.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100190 *
191 * \note The function can be used with a restricted set of arguments
192 * to perform specific checks only. E.g., calling it with
193 * (-,P,-,-,-) and a PRNG amounts to a primality check for P.
Hanno Beckera3ebec22017-08-23 14:06:24 +0100194 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100195int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
196 const mbedtls_mpi *Q, const mbedtls_mpi *D,
197 const mbedtls_mpi *E,
198 int (*f_rng)(void *, unsigned char *, size_t),
199 void *p_rng );
Hanno Beckera3ebec22017-08-23 14:06:24 +0100200
201/**
Hanno Beckerd3637992017-08-25 07:55:03 +0100202 * \brief Check validity of RSA CRT parameters
203 *
204 * \note This is a 'static' helper function not operating on
205 * an RSA context. Alternative implementations need not
206 * overwrite it.
207 *
208 * \param P First prime factor of RSA modulus
209 * \param Q Second prime factor of RSA modulus
210 * \param D RSA private exponent
211 * \param DP MPI to check for D modulo P-1
212 * \param DQ MPI to check for D modulo P-1
213 * \param QP MPI to check for the modular inverse of Q modulo P.
214 *
215 * \return - 0 if the following conditions are satisfied:
216 * - D = DP mod P-1 if P, D, DP != NULL
217 * - Q = DQ mod P-1 if P, D, DQ != NULL
218 * - QP = Q^-1 mod P if P, Q, QP != NULL
219 * - MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if check failed,
220 * potentially including MBEDTLS_ERR_MPI_XXX if some
221 * MPI calculations failed.
222 * - MBEDTLS_ERR_RSA_BAD_INPUT_DATA if insufficient
223 * data was provided to check DP, DQ or QP.
224 *
225 * \note The function can be used with a restricted set of arguments
226 * to perform specific checks only. E.g., calling it with the
227 * parameters (P, -, D, DP, -, -) will check DP = D mod P-1.
228 */
229int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
230 const mbedtls_mpi *D, const mbedtls_mpi *DP,
231 const mbedtls_mpi *DQ, const mbedtls_mpi *QP );
232
233/**
Hanno Beckera3ebec22017-08-23 14:06:24 +0100234 * Implementation of RSA interface
235 */
236
Hanno Beckerab377312017-08-23 16:24:51 +0100237#if !defined(MBEDTLS_RSA_ALT)
238
Hanno Beckera3ebec22017-08-23 14:06:24 +0100239/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 * \brief RSA context structure
241 */
242typedef struct
243{
244 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +0000245 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +0000246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_mpi N; /*!< public modulus */
248 mbedtls_mpi E; /*!< public exponent */
Paul Bakker5121ce52009-01-03 21:22:43 +0000249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_mpi D; /*!< private exponent */
251 mbedtls_mpi P; /*!< 1st prime factor */
252 mbedtls_mpi Q; /*!< 2nd prime factor */
Hanno Becker1a59e792017-08-23 07:41:10 +0100253
254#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_mpi DP; /*!< D % (P - 1) */
256 mbedtls_mpi DQ; /*!< D % (Q - 1) */
257 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Hanno Becker1a59e792017-08-23 07:41:10 +0100258#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_mpi RN; /*!< cached R^2 mod N */
Hanno Becker1a59e792017-08-23 07:41:10 +0100261
262#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 mbedtls_mpi RP; /*!< cached R^2 mod P */
264 mbedtls_mpi RQ; /*!< cached R^2 mod Q */
Hanno Becker1a59e792017-08-23 07:41:10 +0100265#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_mpi Vi; /*!< cached blinding value */
268 mbedtls_mpi Vf; /*!< cached un-blinding value */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 int padding; /*!< MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
Hanno Becker8fd55482017-08-23 14:07:48 +0100271 MBEDTLS_RSA_PKCS_v21 for OAEP/PSS */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
273 specified in the mbedtls_md.h header file
Paul Bakker9dcc3222011-03-08 14:16:06 +0000274 for the EME-OAEP and EMSA-PSS
275 encoding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276#if defined(MBEDTLS_THREADING_C)
277 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200278#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000279}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000281
Hanno Beckerab377312017-08-23 16:24:51 +0100282#else
283
284#include "rsa_alt.h"
285
286#endif /* MBEDTLS_RSA_ALT */
287
Paul Bakker5121ce52009-01-03 21:22:43 +0000288/**
289 * \brief Initialize an RSA context
290 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 * Note: Set padding to MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000292 * encryption scheme and the RSASSA-PSS signature scheme.
293 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 * \param ctx RSA context to be initialized
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 * \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21
296 * \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 *
298 * \note The hash_id parameter is actually ignored
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 * when using MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200300 *
301 * \note Choice of padding mode is strictly enforced for private key
302 * operations, since there might be security concerns in
303 * mixing padding modes. For public key operations it's merely
304 * a default value, which can be overriden by calling specific
305 * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
306 *
307 * \note The chosen hash is always used for OEAP encryption.
308 * For PSS signatures, it's always used for making signatures,
309 * but can be overriden (and always is, if set to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 * MBEDTLS_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100313 int padding,
314 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000315
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100316
317/**
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 */
366
367int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
368 unsigned char *N, size_t N_len,
369 unsigned char *P, size_t P_len,
370 unsigned char *Q, size_t Q_len,
371 unsigned char *D, size_t D_len,
372 unsigned char *E, size_t E_len );
373
374/**
375 * \brief Attempt to complete an RSA context from
376 * a set of imported core parameters.
377 *
378 * \param ctx Initialized RSA context to store parameters
379 * \param f_rng RNG function,
380 * \param p_rng RNG parameter
381 *
382 * To setup an RSA public key, precisely N and E
383 * must have been imported.
384 *
385 * To setup an RSA private key, enough information must be
386 * present for the other parameters to be efficiently derivable.
387 *
388 * The default implementation supports the following:
389 * (a) Derive P, Q from N, D, E
390 * (b) Derive N, D from P, Q, E.
391 *
392 * Alternative implementations need not support these
393 * and may return MBEDTLS_ERR_RSA_BAD_INPUT_DATA instead.
394 *
395 * \note The PRNG is used for probabilistic algorithms
396 * like the derivation of P, Q from N, D, E, as
397 * well as primality checks.
398 *
399 * \return - 0 if successful. In this case, all core parameters
400 * as well as other internally needed parameters have
401 * been generated, and it is guaranteed that they are
Hanno Becker750e8b42017-08-25 07:54:27 +0100402 * sane in the sense of \c mbedtls_rsa_validate_params
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100403 * (with primality of P, Q checked if a PRNG is given).
404 * - MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted
405 * derivations failed.
406 */
407int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
408 int (*f_rng)(void *, unsigned char *, size_t),
409 void *p_rng );
410
411/**
412 * \brief Check if CRT-parameters match core parameters
413 *
414 * \param ctx Complete RSA private key context
415 * \param DP Private exponent modulo P-1, or NULL
416 * \param DQ Private exponent modulo Q-1, or NULL
417 * \param QP Modular inverse of Q modulo P, or NULL
418 *
419 * \return 0 if successful, testifying that the non-NULL optional
420 * parameters provided are in accordance with the core
421 * RSA parameters. Non-zero error code otherwise.
422 *
423 * \note This function performs in-place computations on the
424 * parameters DP, DQ and QP. If modification cannot be
425 * tolerated, you should make copies with mbedtls_mpi_copy
426 * before calling this function.
427 *
428 */
Hanno Beckerd3637992017-08-25 07:55:03 +0100429int mbedtls_rsa_check_crt( const mbedtls_rsa_context *ctx,
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100430 mbedtls_mpi *DP,
431 mbedtls_mpi *DQ,
432 mbedtls_mpi *QP );
433
434/**
435 * \brief Export core parameters of an RSA key
436 *
437 * \param ctx Initialized RSA context
438 * \param N MPI to hold the RSA modulus, or NULL
439 * \param P MPI to hold the first prime factor of N, or NULL
440 * \param Q MPI to hold the second prime factor of N, or NULL
441 * \param D MPI to hold the private exponent, or NULL
442 * \param E MPI to hold the public exponent, or NULL
443 *
444 * \return 0 if successful, non-zero error code otherwise.
445 *
446 */
447int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
448 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
449 mbedtls_mpi *D, mbedtls_mpi *E );
450
451/**
452 * \brief Export core parameters of an RSA key
453 * in raw big-endian binary format
454 *
455 * \param ctx Initialized RSA context
456 * \param N Byte array to store the RSA modulus, or NULL
457 * \param N_len Size of buffer for modulus
458 * \param P Byte array to hold the first prime factor of N, or NULL
459 * \param P_len Size of buffer for first prime factor
460 * \param Q Byte array to hold the second prime factor of N, or NULL
461 * \param Q_len Size of buffer for second prime factor
462 * \param D Byte array to hold the private exponent, or NULL
463 * \param D_len Size of buffer for private exponent
464 * \param E Byte array to hold the public exponent, or NULL
465 * \param E_len Size of buffer for public exponent
466 *
467 * \note The length fields are ignored if the corresponding
468 * buffer pointers are NULL.
469 *
470 * \return 0 if successful. In this case, the non-NULL buffers
471 * pointed to by N, P, Q, D, E are fully written, with
472 * additional unused space filled leading by 0-bytes.
473 *
474 */
475int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
476 unsigned char *N, size_t N_len,
477 unsigned char *P, size_t P_len,
478 unsigned char *Q, size_t Q_len,
479 unsigned char *D, size_t D_len,
480 unsigned char *E, size_t E_len );
481
482/**
483 * \brief Export CRT parameters of a private RSA key
484 *
485 * \param ctx Initialized RSA context
486 * \param DP MPI to hold D modulo P-1, or NULL
487 * \param DQ MPI to hold D modulo Q-1, or NULL
488 * \param QP MPI to hold modular inverse of Q modulo P, or NULL
489 *
490 * \return 0 if successful, non-zero error code otherwise.
491 *
492 * \note Alternative RSA implementations not using CRT-parameters
493 * internally can implement this function using based on
494 * \c mbedtls_rsa_deduce_opt.
495 *
496 */
497int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
498 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
499
Paul Bakker5121ce52009-01-03 21:22:43 +0000500/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100501 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100503 *
504 * \param ctx RSA context to be set
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 * \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21
506 * \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100507 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100508void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
509 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100510
511/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100512 * \brief Get length of RSA modulus in bytes
513 *
514 * \param ctx Initialized RSA context
515 *
516 * \return Length of RSA modulus, in bytes.
517 *
518 */
519size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
520
521/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 * \brief Generate an RSA keypair
523 *
524 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000525 * \param f_rng RNG function
526 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000527 * \param nbits size of the public key in bits
528 * \param exponent public exponent (e.g., 65537)
529 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000531 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100536 int (*f_rng)(void *, unsigned char *, size_t),
537 void *p_rng,
538 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
540/**
Hanno Becker8fd55482017-08-23 14:07:48 +0100541 * \brief Check if a context contains an RSA public key
Paul Bakker5121ce52009-01-03 21:22:43 +0000542 *
543 * \param ctx RSA context to be checked
544 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549/**
Hanno Becker8fd55482017-08-23 14:07:48 +0100550 * \brief Check if a context contains a complete
551 * and valid RSA private key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 *
553 * \param ctx RSA context to be checked
554 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
559/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100560 * \brief Check a public-private RSA key pair.
561 * Check each of the contexts, and make sure they match.
562 *
563 * \param pub RSA context holding the public key
564 * \param prv RSA context holding the private key
565 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100569
570/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000571 * \brief Do an RSA public key operation
572 *
573 * \param ctx RSA context
574 * \param input input buffer
575 * \param output output buffer
576 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 *
579 * \note This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800580 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000581 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000582 *
583 * \note The input and output buffers must be large
584 * enough (eg. 128 bytes if RSA-1024 is used).
585 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000587 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 unsigned char *output );
589
590/**
591 * \brief Do an RSA private key operation
592 *
593 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200594 * \param f_rng RNG function (Needed for blinding)
595 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000596 * \param input input buffer
597 * \param output output buffer
598 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000600 *
601 * \note The input and output buffers must be large
602 * enough (eg. 128 bytes if RSA-1024 is used).
603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200605 int (*f_rng)(void *, unsigned char *, size_t),
606 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000607 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000608 unsigned char *output );
609
610/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100611 * \brief Generic wrapper to perform a PKCS#1 encryption using the
612 * mode from the context. Add the message padding, then do an
613 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 *
615 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200616 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 * and MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000618 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000620 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000621 * \param input buffer holding the data to be encrypted
622 * \param output buffer that will hold the ciphertext
623 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000625 *
626 * \note The output buffer must be as large as the size
627 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000630 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000631 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000632 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000633 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000634 unsigned char *output );
635
636/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100637 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
638 *
639 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 * \param f_rng RNG function (Needed for padding and MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100641 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100643 * \param ilen contains the plaintext length
644 * \param input buffer holding the data to be encrypted
645 * \param output buffer that will hold the ciphertext
646 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100648 *
649 * \note The output buffer must be as large as the size
650 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
651 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100653 int (*f_rng)(void *, unsigned char *, size_t),
654 void *p_rng,
655 int mode, size_t ilen,
656 const unsigned char *input,
657 unsigned char *output );
658
659/**
660 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
661 *
662 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200663 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 * and MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100665 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100667 * \param label buffer holding the custom label to use
668 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100669 * \param ilen contains the plaintext length
670 * \param input buffer holding the data to be encrypted
671 * \param output buffer that will hold the ciphertext
672 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100674 *
675 * \note The output buffer must be as large as the size
676 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100679 int (*f_rng)(void *, unsigned char *, size_t),
680 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100681 int mode,
682 const unsigned char *label, size_t label_len,
683 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100684 const unsigned char *input,
685 unsigned char *output );
686
687/**
688 * \brief Generic wrapper to perform a PKCS#1 decryption using the
689 * mode from the context. Do an RSA operation, then remove
690 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000691 *
692 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200694 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000696 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000697 * \param input buffer holding the encrypted data
698 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000699 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000702 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100703 * \note The output buffer length \c output_max_len should be
704 * as large as the size ctx->len of ctx->N (eg. 128 bytes
705 * if RSA-1024 is used) to be able to hold an arbitrary
706 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100707 * the decryption of the particular ciphertext provided,
Hanno Becker248ae6d2017-05-04 11:27:39 +0100708 * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
709 *
710 * \note The input buffer must be as large as the size
711 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200714 int (*f_rng)(void *, unsigned char *, size_t),
715 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000716 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000717 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000718 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000719 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000720
721/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100722 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
723 *
724 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200726 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100728 * \param olen will contain the plaintext length
729 * \param input buffer holding the encrypted data
730 * \param output buffer that will hold the plaintext
731 * \param output_max_len maximum length of the output buffer
732 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100734 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100735 * \note The output buffer length \c output_max_len should be
736 * as large as the size ctx->len of ctx->N (eg. 128 bytes
737 * if RSA-1024 is used) to be able to hold an arbitrary
738 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100739 * the decryption of the particular ciphertext provided,
Hanno Becker248ae6d2017-05-04 11:27:39 +0100740 * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
741 *
742 * \note The input buffer must be as large as the size
743 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100744 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200746 int (*f_rng)(void *, unsigned char *, size_t),
747 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100748 int mode, size_t *olen,
749 const unsigned char *input,
750 unsigned char *output,
751 size_t output_max_len );
752
753/**
754 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
755 *
756 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200758 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100760 * \param label buffer holding the custom label to use
761 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100762 * \param olen will contain the plaintext length
763 * \param input buffer holding the encrypted data
764 * \param output buffer that will hold the plaintext
765 * \param output_max_len maximum length of the output buffer
766 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100768 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100769 * \note The output buffer length \c output_max_len should be
770 * as large as the size ctx->len of ctx->N (eg. 128 bytes
771 * if RSA-1024 is used) to be able to hold an arbitrary
772 * decrypted message. If it is not large enough to hold
Hanno Becker8fd55482017-08-23 14:07:48 +0100773 * the decryption of the particular ciphertext provided,
Hanno Becker248ae6d2017-05-04 11:27:39 +0100774 * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
775 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100776 * \note The input buffer must be as large as the size
Hanno Becker248ae6d2017-05-04 11:27:39 +0100777 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100778 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200780 int (*f_rng)(void *, unsigned char *, size_t),
781 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100782 int mode,
783 const unsigned char *label, size_t label_len,
784 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100785 const unsigned char *input,
786 unsigned char *output,
787 size_t output_max_len );
788
789/**
790 * \brief Generic wrapper to perform a PKCS#1 signature using the
791 * mode from the context. Do a private RSA operation to sign
792 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000793 *
794 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200795 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 * MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000797 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
799 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
800 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000801 * \param hash buffer holding the message digest
802 * \param sig buffer that will hold the ciphertext
803 *
804 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000806 *
807 * \note The "sig" buffer must be as large as the size
808 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000809 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200810 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 * \note \c mbedtls_rsa_rsassa_pss_sign() for details on md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000812 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000814 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000815 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000816 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000818 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000819 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000820 unsigned char *sig );
821
822/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100823 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
824 *
825 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200827 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
829 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
830 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100831 * \param hash buffer holding the message digest
832 * \param sig buffer that will hold the ciphertext
833 *
834 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100836 *
837 * \note The "sig" buffer must be as large as the size
838 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
839 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200841 int (*f_rng)(void *, unsigned char *, size_t),
842 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100843 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100845 unsigned int hashlen,
846 const unsigned char *hash,
847 unsigned char *sig );
848
849/**
850 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
851 *
852 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200853 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 * MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100855 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
857 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
858 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100859 * \param hash buffer holding the message digest
860 * \param sig buffer that will hold the ciphertext
861 *
862 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100864 *
865 * \note The "sig" buffer must be as large as the size
866 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
867 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200868 * \note The hash_id in the RSA context is the one used for the
869 * encoding. md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100870 * that is encoded. According to RFC 3447 it is advised to
871 * keep both hashes the same.
872 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100874 int (*f_rng)(void *, unsigned char *, size_t),
875 void *p_rng,
876 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100878 unsigned int hashlen,
879 const unsigned char *hash,
880 unsigned char *sig );
881
882/**
883 * \brief Generic wrapper to perform a PKCS#1 verification using the
884 * mode from the context. Do a public RSA operation and check
885 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000886 *
887 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200889 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
891 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
892 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000893 * \param hash buffer holding the message digest
894 * \param sig buffer holding the ciphertext
895 *
896 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000898 *
899 * \note The "sig" buffer must be as large as the size
900 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000901 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200902 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000904 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200906 int (*f_rng)(void *, unsigned char *, size_t),
907 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000908 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000910 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000911 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200912 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
914/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100915 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
916 *
917 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200919 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
921 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
922 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100923 * \param hash buffer holding the message digest
924 * \param sig buffer holding the ciphertext
925 *
926 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100928 *
929 * \note The "sig" buffer must be as large as the size
930 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
931 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200933 int (*f_rng)(void *, unsigned char *, size_t),
934 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100935 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100937 unsigned int hashlen,
938 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200939 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100940
941/**
942 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200943 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +0100944 *
945 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200947 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
949 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
950 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100951 * \param hash buffer holding the message digest
952 * \param sig buffer holding the ciphertext
953 *
954 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100956 *
957 * \note The "sig" buffer must be as large as the size
958 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
959 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200960 * \note The hash_id in the RSA context is the one used for the
961 * verification. md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200962 * hash that is verified. According to RFC 3447 it is advised to
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200963 * keep both hashes the same. If hash_id in the RSA context is
964 * unset, the md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100965 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200967 int (*f_rng)(void *, unsigned char *, size_t),
968 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100969 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100971 unsigned int hashlen,
972 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200973 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100974
975/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200976 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
977 * (This is the version with "full" options.)
978 *
979 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200981 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
983 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
984 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200985 * \param hash buffer holding the message digest
986 * \param mgf1_hash_id message digest used for mask generation
987 * \param expected_salt_len Length of the salt used in padding, use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 * MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200989 * \param sig buffer holding the ciphertext
990 *
991 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 * or an MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200993 *
994 * \note The "sig" buffer must be as large as the size
995 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
996 *
997 * \note The hash_id in the RSA context is ignored.
998 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001000 int (*f_rng)(void *, unsigned char *, size_t),
1001 void *p_rng,
1002 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001004 unsigned int hashlen,
1005 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001007 int expected_salt_len,
1008 const unsigned char *sig );
1009
1010/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001011 * \brief Copy the components of an RSA context
1012 *
1013 * \param dst Destination context
1014 * \param src Source context
1015 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001016 * \return 0 on success,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001017 * MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001018 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001020
1021/**
Paul Bakker5121ce52009-01-03 21:22:43 +00001022 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001023 *
1024 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +00001025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001027
1028/**
1029 * \brief Checkup routine
1030 *
1031 * \return 0 if successful, or 1 if the test failed
1032 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001034
1035#ifdef __cplusplus
1036}
1037#endif
1038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039#endif /* MBEDTLS_RSA_C */
Paul Bakkered27a042013-04-18 22:46:23 +02001040
Paul Bakker5121ce52009-01-03 21:22:43 +00001041#endif /* rsa.h */