blob: 366502a85b3d2727431a7f97a6c8e30b11cd7188 [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 */
226 mbedtls_mpi DP; /*!< D % (P - 1) */
227 mbedtls_mpi DQ; /*!< D % (Q - 1) */
228 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_mpi RN; /*!< cached R^2 mod N */
231 mbedtls_mpi RP; /*!< cached R^2 mod P */
232 mbedtls_mpi RQ; /*!< cached R^2 mod Q */
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_mpi Vi; /*!< cached blinding value */
235 mbedtls_mpi Vf; /*!< cached un-blinding value */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 int padding; /*!< MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
Hanno Becker8fd55482017-08-23 14:07:48 +0100238 MBEDTLS_RSA_PKCS_v21 for OAEP/PSS */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
240 specified in the mbedtls_md.h header file
Paul Bakker9dcc3222011-03-08 14:16:06 +0000241 for the EME-OAEP and EMSA-PSS
242 encoding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_THREADING_C)
244 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200245#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000246}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000248
Paul Bakker5121ce52009-01-03 21:22:43 +0000249/**
250 * \brief Initialize an RSA context
251 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 * Note: Set padding to MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000253 * encryption scheme and the RSASSA-PSS signature scheme.
254 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 * \param ctx RSA context to be initialized
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 * \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21
257 * \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 *
259 * \note The hash_id parameter is actually ignored
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 * when using MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200261 *
262 * \note Choice of padding mode is strictly enforced for private key
263 * operations, since there might be security concerns in
264 * mixing padding modes. For public key operations it's merely
265 * a default value, which can be overriden by calling specific
266 * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
267 *
268 * \note The chosen hash is always used for OEAP encryption.
269 * For PSS signatures, it's always used for making signatures,
270 * but can be overriden (and always is, if set to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 * MBEDTLS_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100274 int padding,
275 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000276
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100277
278/**
279 * \brief Import a set of core parameters into an RSA context
280 *
281 * \param ctx Initialized RSA context to store parameters
282 * \param N RSA modulus, or NULL
283 * \param P First prime factor of N, or NULL
284 * \param Q Second prime factor of N, or NULL
285 * \param D Private exponent, or NULL
286 * \param E Public exponent, or NULL
287 *
288 * \note This function can be called multiple times for successive
289 * imports if the parameters are not simultaneously present.
290 * Any sequence of calls to this function should be followed
291 * by a call to \c mbedtls_rsa_complete which will check
292 * and complete the provided information to a ready-for-use
293 * public or private RSA key.
294 *
295 * \return 0 if successful, non-zero error code on failure.
296 */
297int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
298 const mbedtls_mpi *N,
299 const mbedtls_mpi *P, const mbedtls_mpi *Q,
300 const mbedtls_mpi *D, const mbedtls_mpi *E );
301
302/**
303 * \brief Import core RSA parameters in raw big-endian
304 * binary format into an RSA context
305 *
306 * \param ctx Initialized RSA context to store parameters
307 * \param N RSA modulus, or NULL
308 * \param N_len Byte length of N, ignored if N == NULL
309 * \param P First prime factor of N, or NULL
310 * \param P_len Byte length of P, ignored if P == NULL
311 * \param Q Second prime factor of N, or NULL
312 * \param Q_len Byte length of Q, ignored if Q == NULL
313 * \param D Private exponent, or NULL
314 * \param D_len Byte length of D, ignored if D == NULL
315 * \param E Public exponent, or NULL
316 * \param E_len Byte length of E, ignored if E == NULL
317 *
318 * \note This function can be called multiple times for successive
319 * imports if the parameters are not simultaneously present.
320 * Any sequence of calls to this function should be followed
321 * by a call to \c mbedtls_rsa_complete which will check
322 * and complete the provided information to a ready-for-use
323 * public or private RSA key.
324 *
325 * \return 0 if successful, non-zero error code on failure.
326 */
327
328int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
329 unsigned char *N, size_t N_len,
330 unsigned char *P, size_t P_len,
331 unsigned char *Q, size_t Q_len,
332 unsigned char *D, size_t D_len,
333 unsigned char *E, size_t E_len );
334
335/**
336 * \brief Attempt to complete an RSA context from
337 * a set of imported core parameters.
338 *
339 * \param ctx Initialized RSA context to store parameters
340 * \param f_rng RNG function,
341 * \param p_rng RNG parameter
342 *
343 * To setup an RSA public key, precisely N and E
344 * must have been imported.
345 *
346 * To setup an RSA private key, enough information must be
347 * present for the other parameters to be efficiently derivable.
348 *
349 * The default implementation supports the following:
350 * (a) Derive P, Q from N, D, E
351 * (b) Derive N, D from P, Q, E.
352 *
353 * Alternative implementations need not support these
354 * and may return MBEDTLS_ERR_RSA_BAD_INPUT_DATA instead.
355 *
356 * \note The PRNG is used for probabilistic algorithms
357 * like the derivation of P, Q from N, D, E, as
358 * well as primality checks.
359 *
360 * \return - 0 if successful. In this case, all core parameters
361 * as well as other internally needed parameters have
362 * been generated, and it is guaranteed that they are
363 * sane in the sense of \c mbedtls_rsa_check_params
364 * (with primality of P, Q checked if a PRNG is given).
365 * - MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted
366 * derivations failed.
367 */
368int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
369 int (*f_rng)(void *, unsigned char *, size_t),
370 void *p_rng );
371
372/**
373 * \brief Check if CRT-parameters match core parameters
374 *
375 * \param ctx Complete RSA private key context
376 * \param DP Private exponent modulo P-1, or NULL
377 * \param DQ Private exponent modulo Q-1, or NULL
378 * \param QP Modular inverse of Q modulo P, or NULL
379 *
380 * \return 0 if successful, testifying that the non-NULL optional
381 * parameters provided are in accordance with the core
382 * RSA parameters. Non-zero error code otherwise.
383 *
384 * \note This function performs in-place computations on the
385 * parameters DP, DQ and QP. If modification cannot be
386 * tolerated, you should make copies with mbedtls_mpi_copy
387 * before calling this function.
388 *
389 */
390int mbedtls_rsa_check_crt( mbedtls_rsa_context *ctx,
391 mbedtls_mpi *DP,
392 mbedtls_mpi *DQ,
393 mbedtls_mpi *QP );
394
395/**
396 * \brief Export core parameters of an RSA key
397 *
398 * \param ctx Initialized RSA context
399 * \param N MPI to hold the RSA modulus, or NULL
400 * \param P MPI to hold the first prime factor of N, or NULL
401 * \param Q MPI to hold the second prime factor of N, or NULL
402 * \param D MPI to hold the private exponent, or NULL
403 * \param E MPI to hold the public exponent, or NULL
404 *
405 * \return 0 if successful, non-zero error code otherwise.
406 *
407 */
408int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
409 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
410 mbedtls_mpi *D, mbedtls_mpi *E );
411
412/**
413 * \brief Export core parameters of an RSA key
414 * in raw big-endian binary format
415 *
416 * \param ctx Initialized RSA context
417 * \param N Byte array to store the RSA modulus, or NULL
418 * \param N_len Size of buffer for modulus
419 * \param P Byte array to hold the first prime factor of N, or NULL
420 * \param P_len Size of buffer for first prime factor
421 * \param Q Byte array to hold the second prime factor of N, or NULL
422 * \param Q_len Size of buffer for second prime factor
423 * \param D Byte array to hold the private exponent, or NULL
424 * \param D_len Size of buffer for private exponent
425 * \param E Byte array to hold the public exponent, or NULL
426 * \param E_len Size of buffer for public exponent
427 *
428 * \note The length fields are ignored if the corresponding
429 * buffer pointers are NULL.
430 *
431 * \return 0 if successful. In this case, the non-NULL buffers
432 * pointed to by N, P, Q, D, E are fully written, with
433 * additional unused space filled leading by 0-bytes.
434 *
435 */
436int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
437 unsigned char *N, size_t N_len,
438 unsigned char *P, size_t P_len,
439 unsigned char *Q, size_t Q_len,
440 unsigned char *D, size_t D_len,
441 unsigned char *E, size_t E_len );
442
443/**
444 * \brief Export CRT parameters of a private RSA key
445 *
446 * \param ctx Initialized RSA context
447 * \param DP MPI to hold D modulo P-1, or NULL
448 * \param DQ MPI to hold D modulo Q-1, or NULL
449 * \param QP MPI to hold modular inverse of Q modulo P, or NULL
450 *
451 * \return 0 if successful, non-zero error code otherwise.
452 *
453 * \note Alternative RSA implementations not using CRT-parameters
454 * internally can implement this function using based on
455 * \c mbedtls_rsa_deduce_opt.
456 *
457 */
458int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
459 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
460
Paul Bakker5121ce52009-01-03 21:22:43 +0000461/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100462 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100464 *
465 * \param ctx RSA context to be set
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 * \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21
467 * \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100468 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100469void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
470 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100471
472/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100473 * \brief Get length of RSA modulus in bytes
474 *
475 * \param ctx Initialized RSA context
476 *
477 * \return Length of RSA modulus, in bytes.
478 *
479 */
480size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
481
482/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 * \brief Generate an RSA keypair
484 *
485 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000486 * \param f_rng RNG function
487 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 * \param nbits size of the public key in bits
489 * \param exponent public exponent (e.g., 65537)
490 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000492 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100497 int (*f_rng)(void *, unsigned char *, size_t),
498 void *p_rng,
499 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
501/**
Hanno Becker8fd55482017-08-23 14:07:48 +0100502 * \brief Check if a context contains an RSA public key
Paul Bakker5121ce52009-01-03 21:22:43 +0000503 *
504 * \param ctx RSA context to be checked
505 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
510/**
Hanno Becker8fd55482017-08-23 14:07:48 +0100511 * \brief Check if a context contains a complete
512 * and valid RSA private key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 *
514 * \param ctx RSA context to be checked
515 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
520/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100521 * \brief Check a public-private RSA key pair.
522 * Check each of the contexts, and make sure they match.
523 *
524 * \param pub RSA context holding the public key
525 * \param prv RSA context holding the private key
526 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100530
531/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 * \brief Do an RSA public key operation
533 *
534 * \param ctx RSA context
535 * \param input input buffer
536 * \param output output buffer
537 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000539 *
540 * \note This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800541 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000542 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000543 *
544 * \note The input and output buffers must be large
545 * enough (eg. 128 bytes if RSA-1024 is used).
546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000548 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 unsigned char *output );
550
551/**
552 * \brief Do an RSA private key operation
553 *
554 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200555 * \param f_rng RNG function (Needed for blinding)
556 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 * \param input input buffer
558 * \param output output buffer
559 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 *
562 * \note The input and output buffers must be large
563 * enough (eg. 128 bytes if RSA-1024 is used).
564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200566 int (*f_rng)(void *, unsigned char *, size_t),
567 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000568 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 unsigned char *output );
570
571/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100572 * \brief Generic wrapper to perform a PKCS#1 encryption using the
573 * mode from the context. Add the message padding, then do an
574 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 *
576 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200577 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 * and MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000579 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000581 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000582 * \param input buffer holding the data to be encrypted
583 * \param output buffer that will hold the ciphertext
584 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 *
587 * \note The output buffer must be as large as the size
588 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000591 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000592 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000593 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000594 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 unsigned char *output );
596
597/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100598 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
599 *
600 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 * \param f_rng RNG function (Needed for padding and MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100602 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100604 * \param ilen contains the plaintext length
605 * \param input buffer holding the data to be encrypted
606 * \param output buffer that will hold the ciphertext
607 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100609 *
610 * \note The output buffer must be as large as the size
611 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100614 int (*f_rng)(void *, unsigned char *, size_t),
615 void *p_rng,
616 int mode, size_t ilen,
617 const unsigned char *input,
618 unsigned char *output );
619
620/**
621 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
622 *
623 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200624 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 * and MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100626 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100628 * \param label buffer holding the custom label to use
629 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100630 * \param ilen contains the plaintext length
631 * \param input buffer holding the data to be encrypted
632 * \param output buffer that will hold the ciphertext
633 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100635 *
636 * \note The output buffer must be as large as the size
637 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100640 int (*f_rng)(void *, unsigned char *, size_t),
641 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100642 int mode,
643 const unsigned char *label, size_t label_len,
644 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100645 const unsigned char *input,
646 unsigned char *output );
647
648/**
649 * \brief Generic wrapper to perform a PKCS#1 decryption using the
650 * mode from the context. Do an RSA operation, then remove
651 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000652 *
653 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200655 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000657 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000658 * \param input buffer holding the encrypted data
659 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000660 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100664 * \note The output buffer length \c output_max_len should be
665 * as large as the size ctx->len of ctx->N (eg. 128 bytes
666 * if RSA-1024 is used) to be able to hold an arbitrary
667 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100668 * the decryption of the particular ciphertext provided,
Hanno Becker248ae6d2017-05-04 11:27:39 +0100669 * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
670 *
671 * \note The input buffer must be as large as the size
672 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200675 int (*f_rng)(void *, unsigned char *, size_t),
676 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000677 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000678 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000679 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000680 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000681
682/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100683 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
684 *
685 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200687 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100689 * \param olen will contain the plaintext length
690 * \param input buffer holding the encrypted data
691 * \param output buffer that will hold the plaintext
692 * \param output_max_len maximum length of the output buffer
693 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100695 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100696 * \note The output buffer length \c output_max_len should be
697 * as large as the size ctx->len of ctx->N (eg. 128 bytes
698 * if RSA-1024 is used) to be able to hold an arbitrary
699 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100700 * the decryption of the particular ciphertext provided,
Hanno Becker248ae6d2017-05-04 11:27:39 +0100701 * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
702 *
703 * \note The input buffer must be as large as the size
704 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200707 int (*f_rng)(void *, unsigned char *, size_t),
708 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100709 int mode, size_t *olen,
710 const unsigned char *input,
711 unsigned char *output,
712 size_t output_max_len );
713
714/**
715 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
716 *
717 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200719 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100721 * \param label buffer holding the custom label to use
722 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100723 * \param olen will contain the plaintext length
724 * \param input buffer holding the encrypted data
725 * \param output buffer that will hold the plaintext
726 * \param output_max_len maximum length of the output buffer
727 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100729 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100730 * \note The output buffer length \c output_max_len should be
731 * as large as the size ctx->len of ctx->N (eg. 128 bytes
732 * if RSA-1024 is used) to be able to hold an arbitrary
733 * decrypted message. If it is not large enough to hold
Hanno Becker8fd55482017-08-23 14:07:48 +0100734 * the decryption of the particular ciphertext provided,
Hanno Becker248ae6d2017-05-04 11:27:39 +0100735 * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
736 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100737 * \note The input buffer must be as large as the size
Hanno Becker248ae6d2017-05-04 11:27:39 +0100738 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200741 int (*f_rng)(void *, unsigned char *, size_t),
742 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100743 int mode,
744 const unsigned char *label, size_t label_len,
745 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100746 const unsigned char *input,
747 unsigned char *output,
748 size_t output_max_len );
749
750/**
751 * \brief Generic wrapper to perform a PKCS#1 signature using the
752 * mode from the context. Do a private RSA operation to sign
753 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000754 *
755 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200756 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 * MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000758 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
760 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
761 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000762 * \param hash buffer holding the message digest
763 * \param sig buffer that will hold the ciphertext
764 *
765 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000767 *
768 * \note The "sig" buffer must be as large as the size
769 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000770 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200771 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 * \note \c mbedtls_rsa_rsassa_pss_sign() for details on md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000773 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000775 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000776 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000777 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000779 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000780 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000781 unsigned char *sig );
782
783/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100784 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
785 *
786 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200788 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
790 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
791 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100792 * \param hash buffer holding the message digest
793 * \param sig buffer that will hold the ciphertext
794 *
795 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100797 *
798 * \note The "sig" buffer must be as large as the size
799 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200802 int (*f_rng)(void *, unsigned char *, size_t),
803 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100804 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100806 unsigned int hashlen,
807 const unsigned char *hash,
808 unsigned char *sig );
809
810/**
811 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
812 *
813 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200814 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 * MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100816 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
818 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
819 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100820 * \param hash buffer holding the message digest
821 * \param sig buffer that will hold the ciphertext
822 *
823 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100825 *
826 * \note The "sig" buffer must be as large as the size
827 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
828 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200829 * \note The hash_id in the RSA context is the one used for the
830 * encoding. md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100831 * that is encoded. According to RFC 3447 it is advised to
832 * keep both hashes the same.
833 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100835 int (*f_rng)(void *, unsigned char *, size_t),
836 void *p_rng,
837 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100839 unsigned int hashlen,
840 const unsigned char *hash,
841 unsigned char *sig );
842
843/**
844 * \brief Generic wrapper to perform a PKCS#1 verification using the
845 * mode from the context. Do a public RSA operation and check
846 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000847 *
848 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200850 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
852 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
853 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000854 * \param hash buffer holding the message digest
855 * \param sig buffer holding the ciphertext
856 *
857 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000859 *
860 * \note The "sig" buffer must be as large as the size
861 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000862 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200863 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200867 int (*f_rng)(void *, unsigned char *, size_t),
868 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000871 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000872 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200873 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000874
875/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100876 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
877 *
878 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200880 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
882 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
883 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100884 * \param hash buffer holding the message digest
885 * \param sig buffer holding the ciphertext
886 *
887 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100889 *
890 * \note The "sig" buffer must be as large as the size
891 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200894 int (*f_rng)(void *, unsigned char *, size_t),
895 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100896 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100898 unsigned int hashlen,
899 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200900 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100901
902/**
903 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200904 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +0100905 *
906 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200908 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
910 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
911 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100912 * \param hash buffer holding the message digest
913 * \param sig buffer holding the ciphertext
914 *
915 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100917 *
918 * \note The "sig" buffer must be as large as the size
919 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
920 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200921 * \note The hash_id in the RSA context is the one used for the
922 * verification. md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200923 * hash that is verified. According to RFC 3447 it is advised to
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200924 * keep both hashes the same. If hash_id in the RSA context is
925 * unset, the md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100926 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200928 int (*f_rng)(void *, unsigned char *, size_t),
929 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100930 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100932 unsigned int hashlen,
933 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200934 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100935
936/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200937 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
938 * (This is the version with "full" options.)
939 *
940 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200942 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
944 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
945 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200946 * \param hash buffer holding the message digest
947 * \param mgf1_hash_id message digest used for mask generation
948 * \param expected_salt_len Length of the salt used in padding, use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 * MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200950 * \param sig buffer holding the ciphertext
951 *
952 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 * or an MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200954 *
955 * \note The "sig" buffer must be as large as the size
956 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
957 *
958 * \note The hash_id in the RSA context is ignored.
959 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200961 int (*f_rng)(void *, unsigned char *, size_t),
962 void *p_rng,
963 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200965 unsigned int hashlen,
966 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200968 int expected_salt_len,
969 const unsigned char *sig );
970
971/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200972 * \brief Copy the components of an RSA context
973 *
974 * \param dst Destination context
975 * \param src Source context
976 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200977 * \return 0 on success,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200978 * MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200981
982/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000983 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000984 *
985 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000986 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000988
989/**
990 * \brief Checkup routine
991 *
992 * \return 0 if successful, or 1 if the test failed
993 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000995
996#ifdef __cplusplus
997}
998#endif
999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000#endif /* MBEDTLS_RSA_C */
Paul Bakkered27a042013-04-18 22:46:23 +02001001
Paul Bakker5121ce52009-01-03 21:22:43 +00001002#endif /* rsa.h */