blob: 8aefdb66034dfb3f3f3971685b7f3fba0866cf60 [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
82& and a pair of private and public key.
83 *
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
189 * - A non-zero error code otherwise. In this case, the values
190 * of N, P, Q, D, E are undefined.
191 *
192 * \note The function can be used with a restricted set of arguments
193 * to perform specific checks only. E.g., calling it with
194 * (-,P,-,-,-) and a PRNG amounts to a primality check for P.
195 *
196 * \note The input MPI's are deliberately not declared as constant
197 * and may therefore be used for in-place calculations by
198 * the implementation. In particular, their values can be
199 * corrupted when the function fails. If the user cannot
200 * tolerate this, he has to make copies of the MPI's prior
201 * to calling this function. See \c mbedtls_mpi_copy for this.
202 */
203int mbedtls_rsa_check_params( mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
204 mbedtls_mpi *D, mbedtls_mpi *E,
205 int (*f_rng)(void *, unsigned char *, size_t),
206 void *p_rng );
207
208/**
209 * Implementation of RSA interface
210 */
211
212/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 * \brief RSA context structure
214 */
215typedef struct
216{
217 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +0000218 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_mpi N; /*!< public modulus */
221 mbedtls_mpi E; /*!< public exponent */
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 mbedtls_mpi D; /*!< private exponent */
224 mbedtls_mpi P; /*!< 1st prime factor */
225 mbedtls_mpi Q; /*!< 2nd prime factor */
Hanno Becker1a59e792017-08-23 07:41:10 +0100226
227#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_mpi DP; /*!< D % (P - 1) */
229 mbedtls_mpi DQ; /*!< D % (Q - 1) */
230 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Hanno Becker1a59e792017-08-23 07:41:10 +0100231#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_mpi RN; /*!< cached R^2 mod N */
Hanno Becker1a59e792017-08-23 07:41:10 +0100234
235#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_mpi RP; /*!< cached R^2 mod P */
237 mbedtls_mpi RQ; /*!< cached R^2 mod Q */
Hanno Becker1a59e792017-08-23 07:41:10 +0100238#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_mpi Vi; /*!< cached blinding value */
241 mbedtls_mpi Vf; /*!< cached un-blinding value */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 int padding; /*!< MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
Hanno Becker8fd55482017-08-23 14:07:48 +0100244 MBEDTLS_RSA_PKCS_v21 for OAEP/PSS */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
246 specified in the mbedtls_md.h header file
Paul Bakker9dcc3222011-03-08 14:16:06 +0000247 for the EME-OAEP and EMSA-PSS
248 encoding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#if defined(MBEDTLS_THREADING_C)
250 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200251#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000252}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
Paul Bakker5121ce52009-01-03 21:22:43 +0000255/**
256 * \brief Initialize an RSA context
257 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 * Note: Set padding to MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000259 * encryption scheme and the RSASSA-PSS signature scheme.
260 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000261 * \param ctx RSA context to be initialized
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 * \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21
263 * \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 *
265 * \note The hash_id parameter is actually ignored
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 * when using MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200267 *
268 * \note Choice of padding mode is strictly enforced for private key
269 * operations, since there might be security concerns in
270 * mixing padding modes. For public key operations it's merely
271 * a default value, which can be overriden by calling specific
272 * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
273 *
274 * \note The chosen hash is always used for OEAP encryption.
275 * For PSS signatures, it's always used for making signatures,
276 * but can be overriden (and always is, if set to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 * MBEDTLS_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100280 int padding,
281 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000282
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100283
284/**
285 * \brief Import a set of core parameters into an RSA context
286 *
287 * \param ctx Initialized RSA context to store parameters
288 * \param N RSA modulus, or NULL
289 * \param P First prime factor of N, or NULL
290 * \param Q Second prime factor of N, or NULL
291 * \param D Private exponent, or NULL
292 * \param E Public exponent, or NULL
293 *
294 * \note This function can be called multiple times for successive
295 * imports if the parameters are not simultaneously present.
296 * Any sequence of calls to this function should be followed
297 * by a call to \c mbedtls_rsa_complete which will check
298 * and complete the provided information to a ready-for-use
299 * public or private RSA key.
300 *
301 * \return 0 if successful, non-zero error code on failure.
302 */
303int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
304 const mbedtls_mpi *N,
305 const mbedtls_mpi *P, const mbedtls_mpi *Q,
306 const mbedtls_mpi *D, const mbedtls_mpi *E );
307
308/**
309 * \brief Import core RSA parameters in raw big-endian
310 * binary format into an RSA context
311 *
312 * \param ctx Initialized RSA context to store parameters
313 * \param N RSA modulus, or NULL
314 * \param N_len Byte length of N, ignored if N == NULL
315 * \param P First prime factor of N, or NULL
316 * \param P_len Byte length of P, ignored if P == NULL
317 * \param Q Second prime factor of N, or NULL
318 * \param Q_len Byte length of Q, ignored if Q == NULL
319 * \param D Private exponent, or NULL
320 * \param D_len Byte length of D, ignored if D == NULL
321 * \param E Public exponent, or NULL
322 * \param E_len Byte length of E, ignored if E == NULL
323 *
324 * \note This function can be called multiple times for successive
325 * imports if the parameters are not simultaneously present.
326 * Any sequence of calls to this function should be followed
327 * by a call to \c mbedtls_rsa_complete which will check
328 * and complete the provided information to a ready-for-use
329 * public or private RSA key.
330 *
331 * \return 0 if successful, non-zero error code on failure.
332 */
333
334int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
335 unsigned char *N, size_t N_len,
336 unsigned char *P, size_t P_len,
337 unsigned char *Q, size_t Q_len,
338 unsigned char *D, size_t D_len,
339 unsigned char *E, size_t E_len );
340
341/**
342 * \brief Attempt to complete an RSA context from
343 * a set of imported core parameters.
344 *
345 * \param ctx Initialized RSA context to store parameters
346 * \param f_rng RNG function,
347 * \param p_rng RNG parameter
348 *
349 * To setup an RSA public key, precisely N and E
350 * must have been imported.
351 *
352 * To setup an RSA private key, enough information must be
353 * present for the other parameters to be efficiently derivable.
354 *
355 * The default implementation supports the following:
356 * (a) Derive P, Q from N, D, E
357 * (b) Derive N, D from P, Q, E.
358 *
359 * Alternative implementations need not support these
360 * and may return MBEDTLS_ERR_RSA_BAD_INPUT_DATA instead.
361 *
362 * \note The PRNG is used for probabilistic algorithms
363 * like the derivation of P, Q from N, D, E, as
364 * well as primality checks.
365 *
366 * \return - 0 if successful. In this case, all core parameters
367 * as well as other internally needed parameters have
368 * been generated, and it is guaranteed that they are
369 * sane in the sense of \c mbedtls_rsa_check_params
370 * (with primality of P, Q checked if a PRNG is given).
371 * - MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted
372 * derivations failed.
373 */
374int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
375 int (*f_rng)(void *, unsigned char *, size_t),
376 void *p_rng );
377
378/**
379 * \brief Check if CRT-parameters match core parameters
380 *
381 * \param ctx Complete RSA private key context
382 * \param DP Private exponent modulo P-1, or NULL
383 * \param DQ Private exponent modulo Q-1, or NULL
384 * \param QP Modular inverse of Q modulo P, or NULL
385 *
386 * \return 0 if successful, testifying that the non-NULL optional
387 * parameters provided are in accordance with the core
388 * RSA parameters. Non-zero error code otherwise.
389 *
390 * \note This function performs in-place computations on the
391 * parameters DP, DQ and QP. If modification cannot be
392 * tolerated, you should make copies with mbedtls_mpi_copy
393 * before calling this function.
394 *
395 */
396int mbedtls_rsa_check_crt( mbedtls_rsa_context *ctx,
397 mbedtls_mpi *DP,
398 mbedtls_mpi *DQ,
399 mbedtls_mpi *QP );
400
401/**
402 * \brief Export core parameters of an RSA key
403 *
404 * \param ctx Initialized RSA context
405 * \param N MPI to hold the RSA modulus, or NULL
406 * \param P MPI to hold the first prime factor of N, or NULL
407 * \param Q MPI to hold the second prime factor of N, or NULL
408 * \param D MPI to hold the private exponent, or NULL
409 * \param E MPI to hold the public exponent, or NULL
410 *
411 * \return 0 if successful, non-zero error code otherwise.
412 *
413 */
414int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
415 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
416 mbedtls_mpi *D, mbedtls_mpi *E );
417
418/**
419 * \brief Export core parameters of an RSA key
420 * in raw big-endian binary format
421 *
422 * \param ctx Initialized RSA context
423 * \param N Byte array to store the RSA modulus, or NULL
424 * \param N_len Size of buffer for modulus
425 * \param P Byte array to hold the first prime factor of N, or NULL
426 * \param P_len Size of buffer for first prime factor
427 * \param Q Byte array to hold the second prime factor of N, or NULL
428 * \param Q_len Size of buffer for second prime factor
429 * \param D Byte array to hold the private exponent, or NULL
430 * \param D_len Size of buffer for private exponent
431 * \param E Byte array to hold the public exponent, or NULL
432 * \param E_len Size of buffer for public exponent
433 *
434 * \note The length fields are ignored if the corresponding
435 * buffer pointers are NULL.
436 *
437 * \return 0 if successful. In this case, the non-NULL buffers
438 * pointed to by N, P, Q, D, E are fully written, with
439 * additional unused space filled leading by 0-bytes.
440 *
441 */
442int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
443 unsigned char *N, size_t N_len,
444 unsigned char *P, size_t P_len,
445 unsigned char *Q, size_t Q_len,
446 unsigned char *D, size_t D_len,
447 unsigned char *E, size_t E_len );
448
449/**
450 * \brief Export CRT parameters of a private RSA key
451 *
452 * \param ctx Initialized RSA context
453 * \param DP MPI to hold D modulo P-1, or NULL
454 * \param DQ MPI to hold D modulo Q-1, or NULL
455 * \param QP MPI to hold modular inverse of Q modulo P, or NULL
456 *
457 * \return 0 if successful, non-zero error code otherwise.
458 *
459 * \note Alternative RSA implementations not using CRT-parameters
460 * internally can implement this function using based on
461 * \c mbedtls_rsa_deduce_opt.
462 *
463 */
464int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
465 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
466
Paul Bakker5121ce52009-01-03 21:22:43 +0000467/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100468 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100470 *
471 * \param ctx RSA context to be set
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 * \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21
473 * \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100474 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100475void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
476 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100477
478/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100479 * \brief Get length of RSA modulus in bytes
480 *
481 * \param ctx Initialized RSA context
482 *
483 * \return Length of RSA modulus, in bytes.
484 *
485 */
486size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
487
488/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000489 * \brief Generate an RSA keypair
490 *
491 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000492 * \param f_rng RNG function
493 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000494 * \param nbits size of the public key in bits
495 * \param exponent public exponent (e.g., 65537)
496 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000498 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100503 int (*f_rng)(void *, unsigned char *, size_t),
504 void *p_rng,
505 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
507/**
Hanno Becker8fd55482017-08-23 14:07:48 +0100508 * \brief Check if a context contains an RSA public key
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 *
510 * \param ctx RSA context to be checked
511 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
516/**
Hanno Becker8fd55482017-08-23 14:07:48 +0100517 * \brief Check if a context contains a complete
518 * and valid RSA private key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 *
520 * \param ctx RSA context to be checked
521 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
526/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100527 * \brief Check a public-private RSA key pair.
528 * Check each of the contexts, and make sure they match.
529 *
530 * \param pub RSA context holding the public key
531 * \param prv RSA context holding the private key
532 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100536
537/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 * \brief Do an RSA public key operation
539 *
540 * \param ctx RSA context
541 * \param input input buffer
542 * \param output output buffer
543 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 *
546 * \note This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800547 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000548 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 *
550 * \note The input and output buffers must be large
551 * enough (eg. 128 bytes if RSA-1024 is used).
552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000554 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000555 unsigned char *output );
556
557/**
558 * \brief Do an RSA private key operation
559 *
560 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200561 * \param f_rng RNG function (Needed for blinding)
562 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 * \param input input buffer
564 * \param output output buffer
565 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 *
568 * \note The input and output buffers must be large
569 * enough (eg. 128 bytes if RSA-1024 is used).
570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200572 int (*f_rng)(void *, unsigned char *, size_t),
573 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000574 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 unsigned char *output );
576
577/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100578 * \brief Generic wrapper to perform a PKCS#1 encryption using the
579 * mode from the context. Add the message padding, then do an
580 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 *
582 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200583 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 * and MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000585 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000587 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 * \param input buffer holding the data to be encrypted
589 * \param output buffer that will hold the ciphertext
590 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000592 *
593 * \note The output buffer must be as large as the size
594 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000597 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000598 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000599 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000600 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000601 unsigned char *output );
602
603/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100604 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
605 *
606 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 * \param f_rng RNG function (Needed for padding and MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100608 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100610 * \param ilen contains the plaintext length
611 * \param input buffer holding the data to be encrypted
612 * \param output buffer that will hold the ciphertext
613 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100615 *
616 * \note The output buffer must be as large as the size
617 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100620 int (*f_rng)(void *, unsigned char *, size_t),
621 void *p_rng,
622 int mode, size_t ilen,
623 const unsigned char *input,
624 unsigned char *output );
625
626/**
627 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
628 *
629 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200630 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 * and MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100632 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100634 * \param label buffer holding the custom label to use
635 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100636 * \param ilen contains the plaintext length
637 * \param input buffer holding the data to be encrypted
638 * \param output buffer that will hold the ciphertext
639 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100641 *
642 * \note The output buffer must be as large as the size
643 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100646 int (*f_rng)(void *, unsigned char *, size_t),
647 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100648 int mode,
649 const unsigned char *label, size_t label_len,
650 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100651 const unsigned char *input,
652 unsigned char *output );
653
654/**
655 * \brief Generic wrapper to perform a PKCS#1 decryption using the
656 * mode from the context. Do an RSA operation, then remove
657 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000658 *
659 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200661 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000663 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000664 * \param input buffer holding the encrypted data
665 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000666 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100670 * \note The output buffer length \c output_max_len should be
671 * as large as the size ctx->len of ctx->N (eg. 128 bytes
672 * if RSA-1024 is used) to be able to hold an arbitrary
673 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100674 * the decryption of the particular ciphertext provided,
Hanno Becker248ae6d2017-05-04 11:27:39 +0100675 * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
676 *
677 * \note The input buffer must be as large as the size
678 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200681 int (*f_rng)(void *, unsigned char *, size_t),
682 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000683 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000684 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000685 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000686 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000687
688/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100689 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
690 *
691 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200693 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100695 * \param olen will contain the plaintext length
696 * \param input buffer holding the encrypted data
697 * \param output buffer that will hold the plaintext
698 * \param output_max_len maximum length of the output buffer
699 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100701 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100702 * \note The output buffer length \c output_max_len should be
703 * as large as the size ctx->len of ctx->N (eg. 128 bytes
704 * if RSA-1024 is used) to be able to hold an arbitrary
705 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100706 * the decryption of the particular ciphertext provided,
Hanno Becker248ae6d2017-05-04 11:27:39 +0100707 * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
708 *
709 * \note The input buffer must be as large as the size
710 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200713 int (*f_rng)(void *, unsigned char *, size_t),
714 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100715 int mode, size_t *olen,
716 const unsigned char *input,
717 unsigned char *output,
718 size_t output_max_len );
719
720/**
721 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
722 *
723 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200725 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100727 * \param label buffer holding the custom label to use
728 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100729 * \param olen will contain the plaintext length
730 * \param input buffer holding the encrypted data
731 * \param output buffer that will hold the plaintext
732 * \param output_max_len maximum length of the output buffer
733 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100735 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100736 * \note The output buffer length \c output_max_len should be
737 * as large as the size ctx->len of ctx->N (eg. 128 bytes
738 * if RSA-1024 is used) to be able to hold an arbitrary
739 * decrypted message. If it is not large enough to hold
Hanno Becker8fd55482017-08-23 14:07:48 +0100740 * the decryption of the particular ciphertext provided,
Hanno Becker248ae6d2017-05-04 11:27:39 +0100741 * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
742 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100743 * \note The input buffer must be as large as the size
Hanno Becker248ae6d2017-05-04 11:27:39 +0100744 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100745 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200747 int (*f_rng)(void *, unsigned char *, size_t),
748 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100749 int mode,
750 const unsigned char *label, size_t label_len,
751 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100752 const unsigned char *input,
753 unsigned char *output,
754 size_t output_max_len );
755
756/**
757 * \brief Generic wrapper to perform a PKCS#1 signature using the
758 * mode from the context. Do a private RSA operation to sign
759 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000760 *
761 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200762 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 * MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000764 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
766 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
767 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000768 * \param hash buffer holding the message digest
769 * \param sig buffer that will hold the ciphertext
770 *
771 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000773 *
774 * \note The "sig" buffer must be as large as the size
775 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000776 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200777 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 * \note \c mbedtls_rsa_rsassa_pss_sign() for details on md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000779 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000781 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000782 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000783 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000785 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000786 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000787 unsigned char *sig );
788
789/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100790 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
791 *
792 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200794 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
796 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
797 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100798 * \param hash buffer holding the message digest
799 * \param sig buffer that will hold the ciphertext
800 *
801 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100803 *
804 * \note The "sig" buffer must be as large as the size
805 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
806 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200808 int (*f_rng)(void *, unsigned char *, size_t),
809 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100810 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100812 unsigned int hashlen,
813 const unsigned char *hash,
814 unsigned char *sig );
815
816/**
817 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
818 *
819 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200820 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 * MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100822 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
824 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
825 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100826 * \param hash buffer holding the message digest
827 * \param sig buffer that will hold the ciphertext
828 *
829 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100831 *
832 * \note The "sig" buffer must be as large as the size
833 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
834 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200835 * \note The hash_id in the RSA context is the one used for the
836 * encoding. md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100837 * that is encoded. According to RFC 3447 it is advised to
838 * keep both hashes the same.
839 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100841 int (*f_rng)(void *, unsigned char *, size_t),
842 void *p_rng,
843 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 Generic wrapper to perform a PKCS#1 verification using the
851 * mode from the context. Do a public RSA operation and check
852 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000853 *
854 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200856 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
858 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
859 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000860 * \param hash buffer holding the message digest
861 * \param sig buffer holding the ciphertext
862 *
863 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000865 *
866 * \note The "sig" buffer must be as large as the size
867 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000868 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200869 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200873 int (*f_rng)(void *, unsigned char *, size_t),
874 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000875 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000877 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000878 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200879 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000880
881/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100882 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
883 *
884 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200886 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
888 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
889 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100890 * \param hash buffer holding the message digest
891 * \param sig buffer holding the ciphertext
892 *
893 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100895 *
896 * \note The "sig" buffer must be as large as the size
897 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
898 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200900 int (*f_rng)(void *, unsigned char *, size_t),
901 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100902 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100904 unsigned int hashlen,
905 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200906 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100907
908/**
909 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200910 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +0100911 *
912 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200914 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
916 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
917 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100918 * \param hash buffer holding the message digest
919 * \param sig buffer holding the ciphertext
920 *
921 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100923 *
924 * \note The "sig" buffer must be as large as the size
925 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
926 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200927 * \note The hash_id in the RSA context is the one used for the
928 * verification. md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200929 * hash that is verified. According to RFC 3447 it is advised to
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200930 * keep both hashes the same. If hash_id in the RSA context is
931 * unset, the md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100932 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200934 int (*f_rng)(void *, unsigned char *, size_t),
935 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100936 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100938 unsigned int hashlen,
939 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200940 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100941
942/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200943 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
944 * (This is the version with "full" options.)
945 *
946 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200948 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
950 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
951 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200952 * \param hash buffer holding the message digest
953 * \param mgf1_hash_id message digest used for mask generation
954 * \param expected_salt_len Length of the salt used in padding, use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 * MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200956 * \param sig buffer holding the ciphertext
957 *
958 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 * or an MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200960 *
961 * \note The "sig" buffer must be as large as the size
962 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
963 *
964 * \note The hash_id in the RSA context is ignored.
965 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200967 int (*f_rng)(void *, unsigned char *, size_t),
968 void *p_rng,
969 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200971 unsigned int hashlen,
972 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200974 int expected_salt_len,
975 const unsigned char *sig );
976
977/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200978 * \brief Copy the components of an RSA context
979 *
980 * \param dst Destination context
981 * \param src Source context
982 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200983 * \return 0 on success,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200984 * MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200985 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200987
988/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000989 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000990 *
991 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000994
995/**
996 * \brief Checkup routine
997 *
998 * \return 0 if successful, or 1 if the test failed
999 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
1002#ifdef __cplusplus
1003}
1004#endif
1005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006#endif /* MBEDTLS_RSA_C */
Paul Bakkered27a042013-04-18 22:46:23 +02001007
Paul Bakker5121ce52009-01-03 21:22:43 +00001008#endif /* rsa.h */