blob: 5e7fdca6bc9b2577c8269e0c139690e26babced2 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief The RSA public-key cryptosystem
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_RSA_H
24#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020027#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakkered27a042013-04-18 22:46:23 +020031
Paul Bakker314052f2011-08-15 09:07:52 +000032#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020033#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_THREADING_C)
Paul Bakkerc9965dc2013-09-29 14:58:17 +020036#include "threading.h"
37#endif
38
Paul Bakker13e2dfe2009-07-28 07:18:38 +000039/*
40 * RSA Error codes
41 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
43#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
44#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +020045#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the library's validity check. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
47#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
48#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
49#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
50#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Hanno Becker91c194d2017-09-29 12:50:12 +010051#define MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED -0x4500 /**< The requested parameter export is not possible/allowed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000052
53/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020054 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define MBEDTLS_RSA_PUBLIC 0
57#define MBEDTLS_RSA_PRIVATE 1
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define MBEDTLS_RSA_PKCS_V15 0
60#define MBEDTLS_RSA_PKCS_V21 1
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define MBEDTLS_RSA_SIGN 1
63#define MBEDTLS_RSA_CRYPT 2
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020066
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020067/*
68 * The above constants may be used even if the RSA module is compile out,
69 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
70 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010071
72#if !defined(MBEDTLS_RSA_ALT)
73// Regular implementation
74//
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020075
Paul Bakker407a0da2013-06-27 14:29:21 +020076#ifdef __cplusplus
77extern "C" {
78#endif
79
Hanno Beckera3ebec22017-08-23 14:06:24 +010080/**
Hanno Becker5063cd22017-09-29 11:49:12 +010081 * \brief RSA context structure
82 *
83 * \note Direct manipulation of the members of this structure
84 * is deprecated and will no longer be supported starting
85 * from the next major release. All manipulation should instead
86 * be done through the public interface functions.
87 *
Paul Bakker5121ce52009-01-03 21:22:43 +000088 */
89typedef struct
90{
91 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +000092 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +000093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_mpi N; /*!< public modulus */
95 mbedtls_mpi E; /*!< public exponent */
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_mpi D; /*!< private exponent */
98 mbedtls_mpi P; /*!< 1st prime factor */
99 mbedtls_mpi Q; /*!< 2nd prime factor */
Hanno Becker1a59e792017-08-23 07:41:10 +0100100
Hanno Becker08f055e2017-10-12 10:53:58 +0100101 /* DP,DQ,QP are not used in NO_CRT but temporarily kept for ABI
102 * compatibility. Will be removed on next ABI changing release. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_mpi DP; /*!< D % (P - 1) */
104 mbedtls_mpi DQ; /*!< D % (Q - 1) */
105 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_mpi RN; /*!< cached R^2 mod N */
Hanno Becker1a59e792017-08-23 07:41:10 +0100108
Hanno Becker08f055e2017-10-12 10:53:58 +0100109 /* RP, RQ are not used in NO_CRT but temporarily kept for ABI
110 * compatibility. Will be removed on next ABI changing release. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_mpi RP; /*!< cached R^2 mod P */
112 mbedtls_mpi RQ; /*!< cached R^2 mod Q */
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_mpi Vi; /*!< cached blinding value */
115 mbedtls_mpi Vf; /*!< cached un-blinding value */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200116
Hanno Becker4b2f6912017-09-29 13:34:55 +0100117 int padding; /*!< \c MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
118 \c MBEDTLS_RSA_PKCS_v21 for OAEP/PSS */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
120 specified in the mbedtls_md.h header file
Paul Bakker9dcc3222011-03-08 14:16:06 +0000121 for the EME-OAEP and EMSA-PSS
122 encoding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#if defined(MBEDTLS_THREADING_C)
124 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200125#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000126}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
Paul Bakker5121ce52009-01-03 21:22:43 +0000129/**
130 * \brief Initialize an RSA context
131 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100132 * Note: Set padding to \c MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000133 * encryption scheme and the RSASSA-PSS signature scheme.
134 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 * \param ctx RSA context to be initialized
Hanno Becker4b2f6912017-09-29 13:34:55 +0100136 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
137 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 *
139 * \note The hash_id parameter is actually ignored
Hanno Becker4b2f6912017-09-29 13:34:55 +0100140 * when using \c MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200141 *
142 * \note Choice of padding mode is strictly enforced for private key
143 * operations, since there might be security concerns in
144 * mixing padding modes. For public key operations it's merely
145 * a default value, which can be overriden by calling specific
146 * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
147 *
148 * \note The chosen hash is always used for OEAP encryption.
149 * For PSS signatures, it's always used for making signatures,
150 * but can be overriden (and always is, if set to
Hanno Becker4b2f6912017-09-29 13:34:55 +0100151 * \c MBEDTLS_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100154 int padding,
155 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100157/**
158 * \brief Import a set of core parameters into an RSA context
159 *
160 * \param ctx Initialized RSA context to store parameters
161 * \param N RSA modulus, or NULL
162 * \param P First prime factor of N, or NULL
163 * \param Q Second prime factor of N, or NULL
164 * \param D Private exponent, or NULL
165 * \param E Public exponent, or NULL
166 *
167 * \note This function can be called multiple times for successive
168 * imports if the parameters are not simultaneously present.
169 * Any sequence of calls to this function should be followed
170 * by a call to \c mbedtls_rsa_complete which will check
171 * and complete the provided information to a ready-for-use
172 * public or private RSA key.
173 *
Hanno Becker33195552017-10-25 17:04:10 +0100174 * \note See the documentation of \c mbedtls_rsa_complete for more
175 * information on which parameters are necessary to setup
176 * a private or public RSA key.
177 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100178 * \note The imported parameters are copied and need not be preserved
179 * for the lifetime of the RSA context being set up.
180 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100181 * \return 0 if successful, non-zero error code on failure.
182 */
183int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
184 const mbedtls_mpi *N,
185 const mbedtls_mpi *P, const mbedtls_mpi *Q,
186 const mbedtls_mpi *D, const mbedtls_mpi *E );
187
188/**
189 * \brief Import core RSA parameters in raw big-endian
190 * binary format into an RSA context
191 *
192 * \param ctx Initialized RSA context to store parameters
193 * \param N RSA modulus, or NULL
194 * \param N_len Byte length of N, ignored if N == NULL
195 * \param P First prime factor of N, or NULL
196 * \param P_len Byte length of P, ignored if P == NULL
197 * \param Q Second prime factor of N, or NULL
198 * \param Q_len Byte length of Q, ignored if Q == NULL
199 * \param D Private exponent, or NULL
200 * \param D_len Byte length of D, ignored if D == NULL
201 * \param E Public exponent, or NULL
202 * \param E_len Byte length of E, ignored if E == NULL
203 *
204 * \note This function can be called multiple times for successive
205 * imports if the parameters are not simultaneously present.
206 * Any sequence of calls to this function should be followed
207 * by a call to \c mbedtls_rsa_complete which will check
208 * and complete the provided information to a ready-for-use
209 * public or private RSA key.
210 *
Hanno Becker33195552017-10-25 17:04:10 +0100211 * \note See the documentation of \c mbedtls_rsa_complete for more
212 * information on which parameters are necessary to setup
213 * a private or public RSA key.
214 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100215 * \note The imported parameters are copied and need not be preserved
216 * for the lifetime of the RSA context being set up.
217 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100218 * \return 0 if successful, non-zero error code on failure.
219 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100220int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100221 unsigned char const *N, size_t N_len,
222 unsigned char const *P, size_t P_len,
223 unsigned char const *Q, size_t Q_len,
224 unsigned char const *D, size_t D_len,
225 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100226
227/**
228 * \brief Attempt to complete an RSA context from
229 * a set of imported core parameters.
230 *
231 * \param ctx Initialized RSA context to store parameters
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100232 *
Hanno Beckered203612017-09-29 13:34:25 +0100233 * \note
234 * - To setup an RSA public key, precisely N and E
235 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100236 *
Hanno Beckered203612017-09-29 13:34:25 +0100237 * - To setup an RSA private key, enough information must be
Hanno Becker98838b02017-10-02 13:16:10 +0100238 * present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100239 *
Hanno Beckered203612017-09-29 13:34:25 +0100240 * The default implementation supports the following:
241 * - Derive P, Q from N, D, E
242 * - Derive N, D from P, Q, E.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100243 *
Hanno Beckered203612017-09-29 13:34:25 +0100244 * - Alternative implementations need not support these
245 * and may return \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA instead.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100246 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100247 * \return
248 * - 0 if successful. In this case, it is guaranteed
Hanno Becker1e801f52017-10-10 16:44:47 +0100249 * that the RSA context can be used for RSA operations
250 * without the risk of failure or crash.
Hanno Becker4b2f6912017-09-29 13:34:55 +0100251 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100252 * derivations failed.
Hanno Becker43a08d02017-10-02 13:16:35 +0100253 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100254 * \warning This function need not perform consistency checks
255 * for the imported parameters! In particular, parameters that
256 * are not needed by the implementation may be silently discarded
257 * and left unchecked. For the purpose of checking the consistency
258 * of the key material, see \c mbedtls_rsa_check_privkey.
Hanno Becker43a08d02017-10-02 13:16:35 +0100259 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100260 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100261int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100262
263/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100264 * \brief Export core parameters of an RSA key
265 *
266 * \param ctx Initialized RSA context
267 * \param N MPI to hold the RSA modulus, or NULL
268 * \param P MPI to hold the first prime factor of N, or NULL
269 * \param Q MPI to hold the second prime factor of N, or NULL
270 * \param D MPI to hold the private exponent, or NULL
271 * \param E MPI to hold the public exponent, or NULL
272 *
Hanno Beckered203612017-09-29 13:34:25 +0100273 * \return
274 * - 0 if successful. In this case, the non-NULL buffers
275 * pointed to by N, P, Q, D, E are fully written, with
276 * additional unused space filled leading by 0-bytes.
277 * - Non-zero return code otherwise. In particular, if
278 * exporting the requested parameters
279 * cannot be done because of a lack of functionality
280 * or because of security policies, the error code
281 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
282 * In this case, the RSA context stays intact and can
283 * be continued to be used.
Hanno Becker91c194d2017-09-29 12:50:12 +0100284 *
Hanno Beckered203612017-09-29 13:34:25 +0100285 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100286 * would be the following: Firstly, it might be that an
287 * alternative RSA implementation is in use which stores
288 * the key externally, and which either cannot or should not
289 * export it into RAM. Alternatively, an implementation
290 * (regardless of SW or HW) might not support deducing e.g.
291 * P, Q from N, D, E if the former are not part of the
292 * implementation.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100293 *
294 */
295int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
296 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
297 mbedtls_mpi *D, mbedtls_mpi *E );
298
299/**
300 * \brief Export core parameters of an RSA key
301 * in raw big-endian binary format
302 *
303 * \param ctx Initialized RSA context
304 * \param N Byte array to store the RSA modulus, or NULL
305 * \param N_len Size of buffer for modulus
306 * \param P Byte array to hold the first prime factor of N, or NULL
307 * \param P_len Size of buffer for first prime factor
308 * \param Q Byte array to hold the second prime factor of N, or NULL
309 * \param Q_len Size of buffer for second prime factor
310 * \param D Byte array to hold the private exponent, or NULL
311 * \param D_len Size of buffer for private exponent
312 * \param E Byte array to hold the public exponent, or NULL
313 * \param E_len Size of buffer for public exponent
314 *
315 * \note The length fields are ignored if the corresponding
316 * buffer pointers are NULL.
317 *
Hanno Beckered203612017-09-29 13:34:25 +0100318 * \return
319 * - 0 if successful. In this case, the non-NULL buffers
320 * pointed to by N, P, Q, D, E are fully written, with
321 * additional unused space filled leading by 0-bytes.
322 * - Non-zero return code otherwise. In particular, if
323 * exporting the requested parameters
324 * cannot be done because of a lack of functionality
325 * or because of security policies, the error code
326 * \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED is returned.
327 * In this case, the RSA context stays intact and can
328 * be continued to be used.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100329 *
Hanno Beckered203612017-09-29 13:34:25 +0100330 * \note Reasons for returning \c MBEDTLS_ERR_RSA_EXPORT_UNSUPPORTED
Hanno Becker91c194d2017-09-29 12:50:12 +0100331 * would be the following: Firstly, it might be that an
332 * alternative RSA implementation is in use which stores
333 * the key externally, and which either cannot or should not
334 * export it into RAM. Alternatively, an implementation
335 * (regardless of SW or HW) might not support deducing e.g.
336 * P, Q from N, D, E if the former are not part of the
337 * implementation.
338 *
339 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100340 */
341int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
342 unsigned char *N, size_t N_len,
343 unsigned char *P, size_t P_len,
344 unsigned char *Q, size_t Q_len,
345 unsigned char *D, size_t D_len,
346 unsigned char *E, size_t E_len );
347
348/**
349 * \brief Export CRT parameters of a private RSA key
350 *
351 * \param ctx Initialized RSA context
352 * \param DP MPI to hold D modulo P-1, or NULL
353 * \param DQ MPI to hold D modulo Q-1, or NULL
354 * \param QP MPI to hold modular inverse of Q modulo P, or NULL
355 *
356 * \return 0 if successful, non-zero error code otherwise.
357 *
358 * \note Alternative RSA implementations not using CRT-parameters
359 * internally can implement this function using based on
360 * \c mbedtls_rsa_deduce_opt.
361 *
362 */
363int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
364 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
365
Paul Bakker5121ce52009-01-03 21:22:43 +0000366/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100367 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100369 *
370 * \param ctx RSA context to be set
Hanno Becker4b2f6912017-09-29 13:34:55 +0100371 * \param padding \c MBEDTLS_RSA_PKCS_V15 or \c MBEDTLS_RSA_PKCS_V21
372 * \param hash_id \c MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100373 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100374void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
375 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100376
377/**
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100378 * \brief Get length of RSA modulus in bytes
379 *
380 * \param ctx Initialized RSA context
381 *
382 * \return Length of RSA modulus, in bytes.
383 *
384 */
385size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
386
387/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 * \brief Generate an RSA keypair
389 *
390 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000391 * \param f_rng RNG function
392 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000393 * \param nbits size of the public key in bits
394 * \param exponent public exponent (e.g., 65537)
395 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000397 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100399 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100402 int (*f_rng)(void *, unsigned char *, size_t),
403 void *p_rng,
404 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
406/**
Hanno Becker43a08d02017-10-02 13:16:35 +0100407 * \brief Check if a context contains (at least) an RSA public key
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 *
409 * \param ctx RSA context to be checked
410 *
Hanno Becker43a08d02017-10-02 13:16:35 +0100411 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
412 * On success, it is guaranteed that enough information is
413 * present to perform an RSA public key operation
414 * \c mbedtls_rsa_public.
415 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000418
419/**
Hanno Becker1e801f52017-10-10 16:44:47 +0100420 * \brief Check if a context contains an RSA private key
421 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100423 * \param ctx RSA context to be checked
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100425 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code.
Hanno Becker43a08d02017-10-02 13:16:35 +0100426 *
Hanno Becker68767a62017-10-17 10:13:31 +0100427 * \note The consistency checks performed by this function not only
428 * ensure that \c mbedtls_rsa_private can be called successfully
429 * on the given context, but that the various parameters are
430 * mutually consistent with high probability, in the sense that
431 * \c mbedtls_rsa_public and \c mbedtls_rsa_private are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100432 *
433 * \warning This function should catch accidental misconfigurations
434 * like swapping of parameters, but it cannot establish full
435 * trust in neither the quality nor the consistency of the key
436 * material that was used to setup the given RSA context:
437 * - Regarding consistency, note (see \c mbedtls_rsa_complete)
438 * that imported parameters irrelevant for the implementation
439 * might be silently dropped, in which case the present
440 * function doesn't have access to and hence cannot check them.
Hanno Beckerfc8fbfa2017-10-17 10:31:15 +0100441 * If you want to check the consistency of the entire
442 * content of, say, an PKCS1-encoded RSA private key, you
Hanno Becker1e801f52017-10-10 16:44:47 +0100443 * should use \c mbedtls_rsa_validate_params before setting
444 * up the RSA context.
445 * Further, if the implementation performs empirical checks,
446 * these checks will substantiate but not guarantee consistency.
447 * - Regarding quality, this function is not expected to perform
448 * extended quality assessments like checking that the prime
449 * factors are safe. Further, it is the user's responsibility to
450 * ensure trustworthiness of the source of his RSA parameters,
451 * a question going beyond what's effectively checkable
452 * by the library.
Hanno Becker43a08d02017-10-02 13:16:35 +0100453 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000456
457/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100458 * \brief Check a public-private RSA key pair.
459 * Check each of the contexts, and make sure they match.
460 *
461 * \param pub RSA context holding the public key
462 * \param prv RSA context holding the private key
463 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100464 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100465 */
Hanno Becker98838b02017-10-02 13:16:10 +0100466int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
467 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100468
469/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 * \brief Do an RSA public key operation
471 *
472 * \param ctx RSA context
473 * \param input input buffer
474 * \param output output buffer
475 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100476 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 *
478 * \note This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800479 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000480 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 *
482 * \note The input and output buffers must be large
483 * enough (eg. 128 bytes if RSA-1024 is used).
484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000486 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 unsigned char *output );
488
489/**
490 * \brief Do an RSA private key operation
491 *
492 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200493 * \param f_rng RNG function (Needed for blinding)
494 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 * \param input input buffer
496 * \param output output buffer
497 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100498 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 *
500 * \note The input and output buffers must be large
501 * enough (eg. 128 bytes if RSA-1024 is used).
502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200504 int (*f_rng)(void *, unsigned char *, size_t),
505 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000506 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000507 unsigned char *output );
508
509/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100510 * \brief Generic wrapper to perform a PKCS#1 encryption using the
511 * mode from the context. Add the message padding, then do an
512 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 *
514 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200515 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100516 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000517 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100518 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000519 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 * \param input buffer holding the data to be encrypted
521 * \param output buffer that will hold the ciphertext
522 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100523 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 *
525 * \note The output buffer must be as large as the size
526 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
527 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000529 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000530 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000531 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000532 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 unsigned char *output );
534
535/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100536 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
537 *
538 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100539 * \param f_rng RNG function (Needed for padding and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100540 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100541 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100542 * \param ilen contains the plaintext length
543 * \param input buffer holding the data to be encrypted
544 * \param output buffer that will hold the ciphertext
545 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100546 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100547 *
548 * \note The output buffer must be as large as the size
549 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100552 int (*f_rng)(void *, unsigned char *, size_t),
553 void *p_rng,
554 int mode, size_t ilen,
555 const unsigned char *input,
556 unsigned char *output );
557
558/**
559 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
560 *
561 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200562 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Hanno Becker4b2f6912017-09-29 13:34:55 +0100563 * and \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100564 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100565 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100566 * \param label buffer holding the custom label to use
567 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100568 * \param ilen contains the plaintext length
569 * \param input buffer holding the data to be encrypted
570 * \param output buffer that will hold the ciphertext
571 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100572 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100573 *
574 * \note The output buffer must be as large as the size
575 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
576 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100578 int (*f_rng)(void *, unsigned char *, size_t),
579 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100580 int mode,
581 const unsigned char *label, size_t label_len,
582 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100583 const unsigned char *input,
584 unsigned char *output );
585
586/**
587 * \brief Generic wrapper to perform a PKCS#1 decryption using the
588 * mode from the context. Do an RSA operation, then remove
589 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 *
591 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100592 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200593 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100594 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000595 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000596 * \param input buffer holding the encrypted data
597 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000598 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100600 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000601 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100602 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100603 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100604 * if RSA-1024 is used) to be able to hold an arbitrary
605 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100606 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100607 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100608 *
609 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100610 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200613 int (*f_rng)(void *, unsigned char *, size_t),
614 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000615 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000616 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000617 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000618 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
620/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100621 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
622 *
623 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100624 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200625 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100626 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100627 * \param olen will contain the plaintext length
628 * \param input buffer holding the encrypted data
629 * \param output buffer that will hold the plaintext
630 * \param output_max_len maximum length of the output buffer
631 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100632 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100633 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100634 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100635 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100636 * if RSA-1024 is used) to be able to hold an arbitrary
637 * decrypted message. If it is not large enough to hold
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100638 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100639 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100640 *
641 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100642 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200645 int (*f_rng)(void *, unsigned char *, size_t),
646 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100647 int mode, size_t *olen,
648 const unsigned char *input,
649 unsigned char *output,
650 size_t output_max_len );
651
652/**
653 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
654 *
655 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100656 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200657 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100658 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100659 * \param label buffer holding the custom label to use
660 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100661 * \param olen will contain the plaintext length
662 * \param input buffer holding the encrypted data
663 * \param output buffer that will hold the plaintext
664 * \param output_max_len maximum length of the output buffer
665 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100666 * \return 0 if successful, or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100667 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100668 * \note The output buffer length \c output_max_len should be
Hanno Becker4b2f6912017-09-29 13:34:55 +0100669 * as large as the size \c ctx->len of \c ctx->N (eg. 128 bytes
Hanno Becker248ae6d2017-05-04 11:27:39 +0100670 * if RSA-1024 is used) to be able to hold an arbitrary
671 * decrypted message. If it is not large enough to hold
Hanno Becker8fd55482017-08-23 14:07:48 +0100672 * the decryption of the particular ciphertext provided,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100673 * the function will return \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100674 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100675 * \note The input buffer must be as large as the size
Hanno Becker4b2f6912017-09-29 13:34:55 +0100676 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200679 int (*f_rng)(void *, unsigned char *, size_t),
680 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100681 int mode,
682 const unsigned char *label, size_t label_len,
683 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100684 const unsigned char *input,
685 unsigned char *output,
686 size_t output_max_len );
687
688/**
689 * \brief Generic wrapper to perform a PKCS#1 signature using the
690 * mode from the context. Do a private RSA operation to sign
691 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000692 *
693 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200694 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100695 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000696 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100697 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
698 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for
699 * signing raw data)
700 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000701 * \param hash buffer holding the message digest
702 * \param sig buffer that will hold the ciphertext
703 *
704 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100705 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000706 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100707 * \note The \c sig buffer must be as large as the size
708 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000709 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200710 * \note In case of PKCS#1 v2.1 encoding, see comments on
Hanno Becker4b2f6912017-09-29 13:34:55 +0100711 * \note \c mbedtls_rsa_rsassa_pss_sign() for details on
712 * \c md_alg and \c hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000715 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000716 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000717 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000719 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000720 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000721 unsigned char *sig );
722
723/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100724 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
725 *
726 * \param ctx RSA context
Hanno Becker4b2f6912017-09-29 13:34:55 +0100727 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200728 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100729 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
730 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
731 * for signing raw data)
732 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100733 * \param hash buffer holding the message digest
734 * \param sig buffer that will hold the ciphertext
735 *
736 * \return 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100737 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100738 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100739 * \note The \c sig buffer must be as large as the size
740 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100741 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200743 int (*f_rng)(void *, unsigned char *, size_t),
744 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100745 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100747 unsigned int hashlen,
748 const unsigned char *hash,
749 unsigned char *sig );
750
751/**
752 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
753 *
754 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200755 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Hanno Becker4b2f6912017-09-29 13:34:55 +0100756 * \c MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100757 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100758 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
759 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
760 * for signing raw data)
761 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100762 * \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,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100766 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100767 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100768 * \note The \c sig buffer must be as large as the size
769 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100770 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100771 * \note The \c hash_id in the RSA context is the one used for the
772 * encoding. \c md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100773 * that is encoded. According to RFC 3447 it is advised to
774 * keep both hashes the same.
775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100777 int (*f_rng)(void *, unsigned char *, size_t),
778 void *p_rng,
779 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100781 unsigned int hashlen,
782 const unsigned char *hash,
783 unsigned char *sig );
784
785/**
786 * \brief Generic wrapper to perform a PKCS#1 verification using the
787 * mode from the context. Do a public RSA operation and check
788 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000789 *
790 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100791 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200792 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100793 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
794 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
795 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000796 * \param hash buffer holding the message digest
797 * \param sig buffer holding the ciphertext
798 *
799 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100800 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000801 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100802 * \note The \c sig buffer must be as large as the size
803 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000804 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200805 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000807 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200809 int (*f_rng)(void *, unsigned char *, size_t),
810 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000811 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000813 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000814 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200815 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000816
817/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100818 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
819 *
820 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100821 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200822 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100823 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
824 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE
825 * for signing raw data)
826 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100827 * \param hash buffer holding the message digest
828 * \param sig buffer holding the ciphertext
829 *
830 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100831 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100832 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100833 * \note The \c sig buffer must be as large as the size
834 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100835 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200837 int (*f_rng)(void *, unsigned char *, size_t),
838 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100839 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100841 unsigned int hashlen,
842 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200843 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100844
845/**
846 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200847 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +0100848 *
849 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100850 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200851 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100852 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
853 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
854 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100855 * \param hash buffer holding the message digest
856 * \param sig buffer holding the ciphertext
857 *
858 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100859 * or an \c MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100860 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100861 * \note The \c sig buffer must be as large as the size
862 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100863 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100864 * \note The \c hash_id in the RSA context is the one used for the
865 * verification. \c md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200866 * hash that is verified. According to RFC 3447 it is advised to
Hanno Becker4b2f6912017-09-29 13:34:55 +0100867 * keep both hashes the same. If \c hash_id in the RSA context is
868 * unset, the \c md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100869 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200871 int (*f_rng)(void *, unsigned char *, size_t),
872 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100873 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100875 unsigned int hashlen,
876 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200877 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100878
879/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200880 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
881 * (This is the version with "full" options.)
882 *
883 * \param ctx points to an RSA public key
Hanno Becker4b2f6912017-09-29 13:34:55 +0100884 * \param f_rng RNG function (Only needed for \c MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200885 * \param p_rng RNG parameter
Hanno Becker4b2f6912017-09-29 13:34:55 +0100886 * \param mode \c MBEDTLS_RSA_PUBLIC or \c MBEDTLS_RSA_PRIVATE
887 * \param md_alg a \c MBEDTLS_MD_XXX (use \c MBEDTLS_MD_NONE for signing raw data)
888 * \param hashlen message digest length (for \c MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200889 * \param hash buffer holding the message digest
890 * \param mgf1_hash_id message digest used for mask generation
891 * \param expected_salt_len Length of the salt used in padding, use
Hanno Becker4b2f6912017-09-29 13:34:55 +0100892 * \c MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200893 * \param sig buffer holding the ciphertext
894 *
895 * \return 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100896 * or an \c MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200897 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100898 * \note The \c sig buffer must be as large as the size
899 * of \c ctx->N (eg. 128 bytes if RSA-1024 is used).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200900 *
Hanno Becker4b2f6912017-09-29 13:34:55 +0100901 * \note The \c hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200904 int (*f_rng)(void *, unsigned char *, size_t),
905 void *p_rng,
906 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200908 unsigned int hashlen,
909 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200911 int expected_salt_len,
912 const unsigned char *sig );
913
914/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200915 * \brief Copy the components of an RSA context
916 *
917 * \param dst Destination context
918 * \param src Source context
919 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200920 * \return 0 on success,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100921 * \c MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200922 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200924
925/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000926 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000927 *
928 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
Hanno Beckerd22b78b2017-10-12 11:42:17 +0100932#ifdef __cplusplus
933}
934#endif
935
936#else /* MBEDTLS_RSA_ALT */
937#include "rsa_alt.h"
938#endif /* MBEDTLS_RSA_ALT */
939
940#ifdef __cplusplus
941extern "C" {
942#endif
943
Paul Bakker5121ce52009-01-03 21:22:43 +0000944/**
945 * \brief Checkup routine
946 *
947 * \return 0 if successful, or 1 if the test failed
948 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000950
951#ifdef __cplusplus
952}
953#endif
954
Paul Bakker5121ce52009-01-03 21:22:43 +0000955#endif /* rsa.h */