blob: 406a317d4ffd33a47af92369f4611296f1835e9f [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Rose Zadik21e29262018-04-17 14:08:56 +01004 * \brief This file provides an API for the RSA public-key cryptosystem.
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
Rose Zadike8b5b992018-03-27 12:19:47 +01006 * The RSA public-key cryptosystem is defined in <em>Public-Key
7 * Cryptography Standards (PKCS) #1 v1.5: RSA Encryption</em>
Darryl Green11999bb2018-03-13 15:22:58 +00008 * and <em>Public-Key Cryptography Standards (PKCS) #1 v2.1:
Rose Zadike8b5b992018-03-27 12:19:47 +01009 * RSA Cryptography Specifications</em>.
Rose Zadik042e97f2018-01-26 16:35:10 +000010 *
Darryl Greena40a1012018-01-05 15:33:17 +000011 */
12/*
Rose Zadik042e97f2018-01-26 16:35:10 +000013 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020014 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000027 *
Rose Zadik042e97f2018-01-26 16:35:10 +000028 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#ifndef MBEDTLS_RSA_H
31#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020034#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020037#endif
Paul Bakkered27a042013-04-18 22:46:23 +020038
Paul Bakker314052f2011-08-15 09:07:52 +000039#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020040#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_THREADING_C)
Paul Bakkerc9965dc2013-09-29 14:58:17 +020043#include "threading.h"
44#endif
45
Paul Bakker13e2dfe2009-07-28 07:18:38 +000046/*
47 * RSA Error codes
48 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
50#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
51#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Rose Zadik042e97f2018-01-26 16:35:10 +000052#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the validity check of the library. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
54#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
55#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
56#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
57#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030058
59/* MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is deprecated and should not be used.
60 */
Rose Zadik042e97f2018-01-26 16:35:10 +000061#define MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION -0x4500 /**< The implementation does not offer the requested operation, for example, because of security violations or lack of functionality. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030062
63/* MBEDTLS_ERR_RSA_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010064#define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580 /**< RSA hardware accelerator failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000065
66/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020067 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000068 */
Rose Zadik042e97f2018-01-26 16:35:10 +000069#define MBEDTLS_RSA_PUBLIC 0 /**< Request private key operation. */
70#define MBEDTLS_RSA_PRIVATE 1 /**< Request public key operation. */
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Rose Zadike8b5b992018-03-27 12:19:47 +010072#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
73#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Rose Zadik042e97f2018-01-26 16:35:10 +000075#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
76#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +000077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020079
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020080/*
81 * The above constants may be used even if the RSA module is compile out,
82 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
83 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010084
Paul Bakker407a0da2013-06-27 14:29:21 +020085#ifdef __cplusplus
86extern "C" {
87#endif
88
Ron Eldor4e6d55d2018-02-07 16:36:15 +020089#if !defined(MBEDTLS_RSA_ALT)
90// Regular implementation
91//
92
Paul Bakker5121ce52009-01-03 21:22:43 +000093/**
Rose Zadik042e97f2018-01-26 16:35:10 +000094 * \brief The RSA context structure.
Hanno Becker5063cd22017-09-29 11:49:12 +010095 *
96 * \note Direct manipulation of the members of this structure
Rose Zadik042e97f2018-01-26 16:35:10 +000097 * is deprecated. All manipulation should instead be done through
98 * the public interface functions.
Paul Bakker5121ce52009-01-03 21:22:43 +000099 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200100typedef struct mbedtls_rsa_context
Paul Bakker5121ce52009-01-03 21:22:43 +0000101{
Rose Zadik042e97f2018-01-26 16:35:10 +0000102 int ver; /*!< Always 0.*/
103 size_t len; /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Rose Zadike8b5b992018-03-27 12:19:47 +0100105 mbedtls_mpi N; /*!< The public modulus. */
106 mbedtls_mpi E; /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Rose Zadike8b5b992018-03-27 12:19:47 +0100108 mbedtls_mpi D; /*!< The private exponent. */
109 mbedtls_mpi P; /*!< The first prime factor. */
110 mbedtls_mpi Q; /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100111
Rose Zadikf2ec2882018-04-17 10:27:25 +0100112 mbedtls_mpi DP; /*!< <code>D % (P - 1)</code>. */
113 mbedtls_mpi DQ; /*!< <code>D % (Q - 1)</code>. */
114 mbedtls_mpi QP; /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
Rose Zadikf2ec2882018-04-17 10:27:25 +0100116 mbedtls_mpi RN; /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
Rose Zadikf2ec2882018-04-17 10:27:25 +0100118 mbedtls_mpi RP; /*!< cached <code>R^2 mod P</code>. */
119 mbedtls_mpi RQ; /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200120
Rose Zadike8b5b992018-03-27 12:19:47 +0100121 mbedtls_mpi Vi; /*!< The cached blinding value. */
122 mbedtls_mpi Vf; /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000123
Rose Zadik042e97f2018-01-26 16:35:10 +0000124 int padding; /*!< Selects padding mode:
125 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
126 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
127 int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
128 as specified in md.h for use in the MGF
129 mask generating function used in the
130 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_THREADING_C)
Rose Zadik042e97f2018-01-26 16:35:10 +0000132 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200133#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200137#else /* MBEDTLS_RSA_ALT */
138#include "rsa_alt.h"
139#endif /* MBEDTLS_RSA_ALT */
140
Paul Bakker5121ce52009-01-03 21:22:43 +0000141/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000142 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000144 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000145 * encryption scheme and the RSASSA-PSS signature scheme.
146 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000147 * \note The \p hash_id parameter is ignored when using
148 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200149 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000150 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200151 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000152 * mixing padding modes. For public key operations it is
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200153 * a default value, which can be overriden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000154 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200155 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000156 * \note The hash selected in \p hash_id is always used for OEAP
157 * encryption. For PSS signatures, it is always used for
158 * making signatures, but can be overriden for verifying them.
159 * If set to #MBEDTLS_MD_NONE, it is always overriden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100160 *
161 * \param ctx The RSA context to initialize.
162 * \param padding Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or
163 * #MBEDTLS_RSA_PKCS_V21.
164 * \param hash_id The hash identifier of #mbedtls_md_type_t type, if
165 * \p padding is #MBEDTLS_RSA_PKCS_V21.
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100168 int padding,
169 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
171/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000172 * \brief This function imports a set of core parameters into an
173 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100174 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100175 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000176 * imports, if the parameters are not simultaneously present.
177 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100178 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000179 * by a call to mbedtls_rsa_complete(), which checks and
180 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100181 * public or private RSA key.
182 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000183 * \note See mbedtls_rsa_complete() for more information on which
184 * parameters are necessary to set up a private or public
185 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100186 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100187 * \note The imported parameters are copied and need not be preserved
188 * for the lifetime of the RSA context being set up.
189 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100190 * \param ctx The initialized RSA context to store the parameters in.
191 * \param N The RSA modulus, or NULL.
192 * \param P The first prime factor of \p N, or NULL.
193 * \param Q The second prime factor of \p N, or NULL.
194 * \param D The private exponent, or NULL.
195 * \param E The public exponent, or NULL.
196 *
197 * \return \c 0 on success.
198 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100199 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100200int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
201 const mbedtls_mpi *N,
202 const mbedtls_mpi *P, const mbedtls_mpi *Q,
203 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100204
205/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000206 * \brief This function imports core RSA parameters, in raw big-endian
207 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100209 * \note This function can be called multiple times for successive
210 * imports, if the parameters are not simultaneously present.
211 *
212 * Any sequence of calls to this function should be followed
213 * by a call to mbedtls_rsa_complete(), which checks and
214 * completes the provided information to a ready-for-use
215 * public or private RSA key.
216 *
217 * \note See mbedtls_rsa_complete() for more information on which
218 * parameters are necessary to set up a private or public
219 * RSA key.
220 *
221 * \note The imported parameters are copied and need not be preserved
222 * for the lifetime of the RSA context being set up.
223 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000224 * \param ctx The initialized RSA context to store the parameters in.
225 * \param N The RSA modulus, or NULL.
226 * \param N_len The Byte length of \p N, ignored if \p N == NULL.
227 * \param P The first prime factor of \p N, or NULL.
228 * \param P_len The Byte length of \p P, ignored if \p P == NULL.
229 * \param Q The second prime factor of \p N, or NULL.
230 * \param Q_len The Byte length of \p Q, ignored if \p Q == NULL.
231 * \param D The private exponent, or NULL.
232 * \param D_len The Byte length of \p D, ignored if \p D == NULL.
233 * \param E The public exponent, or NULL.
234 * \param E_len The Byte length of \p E, ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100236 * \return \c 0 on success.
237 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100238 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100239int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100240 unsigned char const *N, size_t N_len,
241 unsigned char const *P, size_t P_len,
242 unsigned char const *Q, size_t Q_len,
243 unsigned char const *D, size_t D_len,
244 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100245
246/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000247 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100248 * a set of imported core parameters.
249 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000250 * To setup an RSA public key, precisely \p N and \p E
251 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100252 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000253 * To setup an RSA private key, sufficient information must
254 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100255 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000256 * The default implementation supports the following:
257 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
258 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
259 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100260 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000261 * If this function runs successfully, it guarantees that
262 * the RSA context can be used for RSA operations without
263 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100264 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100265 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000266 * for the imported parameters. In particular, parameters that
267 * are not needed by the implementation might be silently
268 * discarded and left unchecked. To check the consistency
269 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100270 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100271 * \param ctx The initialized RSA context holding imported parameters.
272 *
273 * \return \c 0 on success.
274 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
275 * failed.
276 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100277 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100278int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100279
280/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000281 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100282 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000283 * If this function runs successfully, the non-NULL buffers
284 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
285 * written, with additional unused space filled leading by
286 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100287 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000288 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300289 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000290 * <li>An alternative RSA implementation is in use, which
291 * stores the key externally, and either cannot or should
292 * not export it into RAM.</li>
293 * <li>A SW or HW implementation might not support a certain
294 * deduction. For example, \p P, \p Q from \p N, \p D,
295 * and \p E if the former are not part of the
296 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100297 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000298 * If the function fails due to an unsupported operation,
299 * the RSA context stays intact and remains usable.
300 *
301 * \param ctx The initialized RSA context.
302 * \param N The MPI to hold the RSA modulus, or NULL.
303 * \param P The MPI to hold the first prime factor of \p N, or NULL.
304 * \param Q The MPI to hold the second prime factor of \p N, or NULL.
305 * \param D The MPI to hold the private exponent, or NULL.
306 * \param E The MPI to hold the public exponent, or NULL.
307 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100308 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300309 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000310 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100311 * functionality or because of security policies.
312 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100313 *
314 */
315int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
316 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
317 mbedtls_mpi *D, mbedtls_mpi *E );
318
319/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000320 * \brief This function exports core parameters of an RSA key
321 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100322 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000323 * If this function runs successfully, the non-NULL buffers
324 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
325 * written, with additional unused space filled leading by
326 * zero Bytes.
327 *
328 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300329 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000330 * <li>An alternative RSA implementation is in use, which
331 * stores the key externally, and either cannot or should
332 * not export it into RAM.</li>
333 * <li>A SW or HW implementation might not support a certain
334 * deduction. For example, \p P, \p Q from \p N, \p D,
335 * and \p E if the former are not part of the
336 * implementation.</li></ul>
337 * If the function fails due to an unsupported operation,
338 * the RSA context stays intact and remains usable.
339 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100340 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100341 * buffer pointers are NULL.
342 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000343 * \param ctx The initialized RSA context.
344 * \param N The Byte array to store the RSA modulus, or NULL.
345 * \param N_len The size of the buffer for the modulus.
346 * \param P The Byte array to hold the first prime factor of \p N, or
347 * NULL.
348 * \param P_len The size of the buffer for the first prime factor.
349 * \param Q The Byte array to hold the second prime factor of \p N, or
Rose Zadikf2ec2882018-04-17 10:27:25 +0100350 * NULL.
Rose Zadik042e97f2018-01-26 16:35:10 +0000351 * \param Q_len The size of the buffer for the second prime factor.
352 * \param D The Byte array to hold the private exponent, or NULL.
353 * \param D_len The size of the buffer for the private exponent.
354 * \param E The Byte array to hold the public exponent, or NULL.
355 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100356 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100357 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300358 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000359 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100360 * functionality or because of security policies.
361 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100362 */
363int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
364 unsigned char *N, size_t N_len,
365 unsigned char *P, size_t P_len,
366 unsigned char *Q, size_t Q_len,
367 unsigned char *D, size_t D_len,
368 unsigned char *E, size_t E_len );
369
370/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000371 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100372 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100373 * \note Alternative RSA implementations not using CRT-parameters
374 * internally can implement this function based on
375 * mbedtls_rsa_deduce_opt().
376 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000377 * \param ctx The initialized RSA context.
378 * \param DP The MPI to hold D modulo P-1, or NULL.
379 * \param DQ The MPI to hold D modulo Q-1, or NULL.
380 * \param QP The MPI to hold modular inverse of Q modulo P, or NULL.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100381 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100382 * \return \c 0 on success.
383 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100384 *
385 */
386int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
387 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
388
Paul Bakker5121ce52009-01-03 21:22:43 +0000389/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000390 * \brief This function sets padding for an already initialized RSA
391 * context. See mbedtls_rsa_init() for details.
Paul Bakker5121ce52009-01-03 21:22:43 +0000392 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000393 * \param ctx The RSA context to be set.
394 * \param padding Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or
395 * #MBEDTLS_RSA_PKCS_V21.
396 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Paul Bakker40e46942009-01-03 21:51:57 +0000397 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100398void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
399 int hash_id);
Paul Bakker21eb2802010-08-16 11:10:02 +0000400
Paul Bakkera3d195c2011-11-27 21:07:34 +0000401/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000402 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100403 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000404 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100405 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000406 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100407 *
408 */
409size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
410
411/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000412 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000414 * \note mbedtls_rsa_init() must be called before this function,
415 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000416 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100417 * \param ctx The RSA context used to hold the key.
418 * \param f_rng The RNG function.
419 * \param p_rng The RNG context.
420 * \param nbits The size of the public key in bits.
421 * \param exponent The public exponent. For example, 65537.
422 *
423 * \return \c 0 on success.
424 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100427 int (*f_rng)(void *, unsigned char *, size_t),
428 void *p_rng,
429 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000430
431/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000432 * \brief This function checks if a context contains at least an RSA
433 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000435 * If the function runs successfully, it is guaranteed that
436 * enough information is present to perform an RSA public key
437 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000439 * \param ctx The RSA context to check.
440 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100441 * \return \c 0 on success.
442 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100443 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
447/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000448 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100449 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000450 *
Hanno Becker68767a62017-10-17 10:13:31 +0100451 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000452 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100453 * on the given context, but that the various parameters are
454 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000455 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100456 *
457 * \warning This function should catch accidental misconfigurations
458 * like swapping of parameters, but it cannot establish full
459 * trust in neither the quality nor the consistency of the key
460 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000461 * <ul><li>Consistency: Imported parameters that are irrelevant
462 * for the implementation might be silently dropped. If dropped,
463 * the current function does not have access to them,
464 * and therefore cannot check them. See mbedtls_rsa_complete().
465 * If you want to check the consistency of the entire
466 * content of an PKCS1-encoded RSA private key, for example, you
467 * should use mbedtls_rsa_validate_params() before setting
468 * up the RSA context.
469 * Additionally, if the implementation performs empirical checks,
470 * these checks substantiate but do not guarantee consistency.</li>
471 * <li>Quality: This function is not expected to perform
472 * extended quality assessments like checking that the prime
473 * factors are safe. Additionally, it is the responsibility of the
474 * user to ensure the trustworthiness of the source of his RSA
475 * parameters, which goes beyond what is effectively checkable
476 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100477 *
478 * \param ctx The RSA context to check.
479 *
480 * \return \c 0 on success.
481 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
485/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000486 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100487 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000488 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100489 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000490 * \param pub The RSA context holding the public key.
491 * \param prv The RSA context holding the private key.
492 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100493 * \return \c 0 on success.
494 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100495 */
Hanno Becker98838b02017-10-02 13:16:10 +0100496int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
497 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100498
499/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000500 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000502 * \note This function does not handle message padding.
503 *
504 * \note Make sure to set \p input[0] = 0 or ensure that
505 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 *
507 * \note The input and output buffers must be large
Rose Zadik042e97f2018-01-26 16:35:10 +0000508 * enough. For example, 128 Bytes if RSA-1024 is used.
Rose Zadike8b5b992018-03-27 12:19:47 +0100509 *
510 * \param ctx The RSA context.
511 * \param input The input buffer.
512 * \param output The output buffer.
513 *
514 * \return \c 0 on success.
515 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000518 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 unsigned char *output );
520
521/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000522 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 * \note The input and output buffers must be large
Rose Zadik042e97f2018-01-26 16:35:10 +0000525 * enough. For example, 128 Bytes if RSA-1024 is used.
Hanno Becker88ec2382017-05-03 13:51:16 +0100526 *
Hanno Becker24120612017-10-26 11:53:35 +0100527 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100528 *
529 * \note If blinding is used, both the base of exponentation
Hanno Becker24120612017-10-26 11:53:35 +0100530 * and the exponent are blinded, providing protection
531 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100532 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100533 * \warning It is deprecated and a security risk to not provide
534 * a PRNG here and thereby prevent the use of blinding.
535 * Future versions of the library may enforce the presence
536 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100537 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100538 * \param ctx The RSA context.
539 * \param f_rng The RNG function. Needed for blinding.
540 * \param p_rng The RNG context.
541 * \param input The input buffer.
542 * \param output The output buffer.
543 *
544 * \return \c 0 on success.
545 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
546 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200549 int (*f_rng)(void *, unsigned char *, size_t),
550 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000551 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 unsigned char *output );
553
554/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000555 * \brief This function adds the message padding, then performs an RSA
556 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000558 * It is the generic wrapper for performing a PKCS#1 encryption
559 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100561 * \note The input and output buffers must be as large as the size
562 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100564 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000565 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
566 * are likely to remove the \p mode argument and have it
567 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100568 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100569 * \note Alternative implementations of RSA need not support
570 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300571 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100572 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100573 * \param ctx The RSA context.
574 * \param f_rng The RNG function. Needed for padding, PKCS#1 v2.1
575 * encoding, and #MBEDTLS_RSA_PRIVATE.
576 * \param p_rng The RNG context.
577 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
578 * \param ilen The length of the plaintext.
579 * \param input The buffer holding the data to encrypt.
580 * \param output The buffer used to hold the ciphertext.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100581 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100582 * \return \c 0 on success.
583 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000586 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000587 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000588 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000589 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 unsigned char *output );
591
592/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000593 * \brief This function performs a PKCS#1 v1.5 encryption operation
594 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100595 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100596 * \note The output buffer must be as large as the size
597 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100598 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100599 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000600 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
601 * are likely to remove the \p mode argument and have it
602 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100603 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100604 * \note Alternative implementations of RSA need not support
605 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300606 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100607 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100608 * \param ctx The RSA context.
609 * \param f_rng The RNG function. Needed for padding and
610 * #MBEDTLS_RSA_PRIVATE.
611 * \param p_rng The RNG context.
612 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
613 * \param ilen The length of the plaintext.
614 * \param input The buffer holding the data to encrypt.
615 * \param output The buffer used to hold the ciphertext.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100616 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100617 * \return \c 0 on success.
618 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100621 int (*f_rng)(void *, unsigned char *, size_t),
622 void *p_rng,
623 int mode, size_t ilen,
624 const unsigned char *input,
625 unsigned char *output );
626
627/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000628 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
629 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100630 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100631 * \note The output buffer must be as large as the size
632 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
633 *
634 * \deprecated It is deprecated and discouraged to call this function
635 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
636 * are likely to remove the \p mode argument and have it
637 * implicitly set to #MBEDTLS_RSA_PUBLIC.
638 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100639 * \note Alternative implementations of RSA need not support
640 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300641 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100642 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000643 * \param ctx The RSA context.
644 * \param f_rng The RNG function. Needed for padding and PKCS#1 v2.1
645 * encoding and #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +0100646 * \param p_rng The RNG context.
Rose Zadik042e97f2018-01-26 16:35:10 +0000647 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
648 * \param label The buffer holding the custom label to use.
649 * \param label_len The length of the label.
650 * \param ilen The length of the plaintext.
651 * \param input The buffer holding the data to encrypt.
652 * \param output The buffer used to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100653 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100654 * \return \c 0 on success.
655 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100658 int (*f_rng)(void *, unsigned char *, size_t),
659 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100660 int mode,
661 const unsigned char *label, size_t label_len,
662 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100663 const unsigned char *input,
664 unsigned char *output );
665
666/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000667 * \brief This function performs an RSA operation, then removes the
668 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000670 * It is the generic wrapper for performing a PKCS#1 decryption
671 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100673 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000674 * as large as the size \p ctx->len of \p ctx->N (for example,
675 * 128 Bytes if RSA-1024 is used) to be able to hold an
676 * arbitrary decrypted message. If it is not large enough to
677 * hold the decryption of the particular ciphertext provided,
678 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100679 *
680 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000681 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Rose Zadike8b5b992018-03-27 12:19:47 +0100682 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100683 * \deprecated It is deprecated and discouraged to call this function
684 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
685 * are likely to remove the \p mode argument and have it
686 * implicitly set to #MBEDTLS_RSA_PRIVATE.
687 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100688 * \note Alternative implementations of RSA need not support
689 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300690 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100691 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100692 * \param ctx The RSA context.
693 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
694 * \param p_rng The RNG context.
695 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
696 * \param olen The length of the plaintext.
697 * \param input The buffer holding the encrypted data.
698 * \param output The buffer used to hold the plaintext.
699 * \param output_max_len The maximum length of the output buffer.
700 *
701 * \return \c 0 on success.
702 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200705 int (*f_rng)(void *, unsigned char *, size_t),
706 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000707 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000708 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000709 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000710 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000711
712/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000713 * \brief This function performs a PKCS#1 v1.5 decryption
714 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100715 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100716 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000717 * as large as the size \p ctx->len of \p ctx->N, for example,
718 * 128 Bytes if RSA-1024 is used, to be able to hold an
719 * arbitrary decrypted message. If it is not large enough to
720 * hold the decryption of the particular ciphertext provided,
721 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100722 *
723 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000724 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Rose Zadike8b5b992018-03-27 12:19:47 +0100725 *
726 * \deprecated It is deprecated and discouraged to call this function
727 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
728 * are likely to remove the \p mode argument and have it
729 * implicitly set to #MBEDTLS_RSA_PRIVATE.
730 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100731 * \note Alternative implementations of RSA need not support
732 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300733 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100734 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100735 * \param ctx The RSA context.
736 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
737 * \param p_rng The RNG context.
738 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
739 * \param olen The length of the plaintext.
740 * \param input The buffer holding the encrypted data.
741 * \param output The buffer to hold the plaintext.
742 * \param output_max_len The maximum length of the output buffer.
743 *
744 * \return \c 0 on success.
745 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
746 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200749 int (*f_rng)(void *, unsigned char *, size_t),
750 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100751 int mode, size_t *olen,
752 const unsigned char *input,
753 unsigned char *output,
754 size_t output_max_len );
755
756/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100757 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
758 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100759 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100760 * \note The output buffer length \c output_max_len should be
761 * as large as the size \p ctx->len of \p ctx->N, for
762 * example, 128 Bytes if RSA-1024 is used, to be able to
763 * hold an arbitrary decrypted message. If it is not
764 * large enough to hold the decryption of the particular
765 * ciphertext provided, the function returns
766 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100767 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100768 * \note The input buffer must be as large as the size
769 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Rose Zadike8b5b992018-03-27 12:19:47 +0100770 *
771 * \deprecated It is deprecated and discouraged to call this function
772 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
773 * are likely to remove the \p mode argument and have it
774 * implicitly set to #MBEDTLS_RSA_PRIVATE.
775 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100776 * \note Alternative implementations of RSA need not support
777 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300778 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100779 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100780 * \param ctx The RSA context.
781 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
782 * \param p_rng The RNG context.
783 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
784 * \param label The buffer holding the custom label to use.
785 * \param label_len The length of the label.
786 * \param olen The length of the plaintext.
787 * \param input The buffer holding the encrypted data.
788 * \param output The buffer to hold the plaintext.
Darryl Green11999bb2018-03-13 15:22:58 +0000789 * \param output_max_len The maximum length of the output buffer.
Rose Zadike8b5b992018-03-27 12:19:47 +0100790 *
791 * \return \c 0 on success.
792 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100793 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200795 int (*f_rng)(void *, unsigned char *, size_t),
796 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100797 int mode,
798 const unsigned char *label, size_t label_len,
799 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100800 const unsigned char *input,
801 unsigned char *output,
802 size_t output_max_len );
803
804/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000805 * \brief This function performs a private RSA operation to sign
806 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000807 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000808 * It is the generic wrapper for performing a PKCS#1
809 * signature using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000810 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000811 * \note The \p sig buffer must be as large as the size
812 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000813 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000814 * \note For PKCS#1 v2.1 encoding, see comments on
815 * mbedtls_rsa_rsassa_pss_sign() for details on
816 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100817 *
818 * \deprecated It is deprecated and discouraged to call this function
819 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
820 * are likely to remove the \p mode argument and have it
821 * implicitly set to #MBEDTLS_RSA_PRIVATE.
822 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100823 * \note Alternative implementations of RSA need not support
824 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300825 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100826 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100827 * \param ctx The RSA context.
828 * \param f_rng The RNG function. Needed for PKCS#1 v2.1 encoding and for
829 * #MBEDTLS_RSA_PRIVATE.
830 * \param p_rng The RNG context.
831 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
832 * \param md_alg The message-digest algorithm used to hash the original data.
833 * Use #MBEDTLS_MD_NONE for signing raw data.
834 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
835 * \param hash The buffer holding the message digest.
836 * \param sig The buffer to hold the ciphertext.
837 *
838 * \return \c 0 if the signing operation was successful.
839 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000840 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000842 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000843 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000844 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000846 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000847 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000848 unsigned char *sig );
849
850/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000851 * \brief This function performs a PKCS#1 v1.5 signature
852 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100853 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100854 * \note The \p sig buffer must be as large as the size
855 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
856 *
857 * \deprecated It is deprecated and discouraged to call this function
858 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
859 * are likely to remove the \p mode argument and have it
860 * implicitly set to #MBEDTLS_RSA_PRIVATE.
861 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100862 * \note Alternative implementations of RSA need not support
863 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300864 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100865 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000866 * \param ctx The RSA context.
867 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +0100868 * \param p_rng The RNG context.
Rose Zadik042e97f2018-01-26 16:35:10 +0000869 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
870 * \param md_alg The message-digest algorithm used to hash the original data.
871 * Use #MBEDTLS_MD_NONE for signing raw data.
872 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
873 * \param hash The buffer holding the message digest.
874 * \param sig The buffer to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100875 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100876 * \return \c 0 if the signing operation was successful.
877 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200880 int (*f_rng)(void *, unsigned char *, size_t),
881 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100882 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100884 unsigned int hashlen,
885 const unsigned char *hash,
886 unsigned char *sig );
887
888/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000889 * \brief This function performs a PKCS#1 v2.1 PSS signature
890 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100891 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000892 * \note The \p sig buffer must be as large as the size
893 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100894 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000895 * \note The \p hash_id in the RSA context is the one used for the
896 * encoding. \p md_alg in the function call is the type of hash
897 * that is encoded. According to <em>RFC-3447: Public-Key
898 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
899 * Specifications</em> it is advised to keep both hashes the
900 * same.
Rose Zadike8b5b992018-03-27 12:19:47 +0100901 *
Jaeden Amero3725bb22018-09-07 19:12:36 +0100902 * \note This function always uses the maximum possible salt size,
903 * up to the length of the payload hash. This choice of salt
904 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
905 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
906 * minimum salt size which is the hash size minus 2 bytes. If
907 * this minimum size is too large given the key size (the salt
908 * size, plus the hash size, plus 2 bytes must be no more than
909 * the key size in bytes), this function returns
910 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
911 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100912 * \deprecated It is deprecated and discouraged to call this function
913 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
914 * are likely to remove the \p mode argument and have it
915 * implicitly set to #MBEDTLS_RSA_PRIVATE.
916 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100917 * \note Alternative implementations of RSA need not support
918 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300919 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100920 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100921 * \param ctx The RSA context.
922 * \param f_rng The RNG function. Needed for PKCS#1 v2.1 encoding and for
923 * #MBEDTLS_RSA_PRIVATE.
924 * \param p_rng The RNG context.
925 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
926 * \param md_alg The message-digest algorithm used to hash the original data.
927 * Use #MBEDTLS_MD_NONE for signing raw data.
928 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
929 * \param hash The buffer holding the message digest.
930 * \param sig The buffer to hold the ciphertext.
931 *
932 * \return \c 0 if the signing operation was successful.
933 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100934 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100936 int (*f_rng)(void *, unsigned char *, size_t),
937 void *p_rng,
938 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100940 unsigned int hashlen,
941 const unsigned char *hash,
942 unsigned char *sig );
943
944/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000945 * \brief This function performs a public RSA operation and checks
946 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +0000947 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000948 * This is the generic wrapper for performing a PKCS#1
949 * verification using the mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000950 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000951 * \note The \p sig buffer must be as large as the size
952 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000953 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000954 * \note For PKCS#1 v2.1 encoding, see comments on
955 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
956 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100957 *
958 * \deprecated It is deprecated and discouraged to call this function
959 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
960 * are likely to remove the \p mode argument and have it
961 * set to #MBEDTLS_RSA_PUBLIC.
962 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100963 * \note Alternative implementations of RSA need not support
964 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300965 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100966 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100967 * \param ctx The RSA public key context.
968 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
969 * \param p_rng The RNG context.
970 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
971 * \param md_alg The message-digest algorithm used to hash the original data.
972 * Use #MBEDTLS_MD_NONE for signing raw data.
973 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
974 * \param hash The buffer holding the message digest.
975 * \param sig The buffer holding the ciphertext.
976 *
977 * \return \c 0 if the verify operation was successful.
978 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200981 int (*f_rng)(void *, unsigned char *, size_t),
982 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000983 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000985 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000986 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200987 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000988
989/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000990 * \brief This function performs a PKCS#1 v1.5 verification
991 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +0100992 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100993 * \note The \p sig buffer must be as large as the size
994 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
995 *
996 * \deprecated It is deprecated and discouraged to call this function
997 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
998 * are likely to remove the \p mode argument and have it
999 * set to #MBEDTLS_RSA_PUBLIC.
1000 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001001 * \note Alternative implementations of RSA need not support
1002 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001003 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001004 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001005 * \param ctx The RSA public key context.
1006 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +01001007 * \param p_rng The RNG context.
Rose Zadik042e97f2018-01-26 16:35:10 +00001008 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1009 * \param md_alg The message-digest algorithm used to hash the original data.
1010 * Use #MBEDTLS_MD_NONE for signing raw data.
1011 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
1012 * \param hash The buffer holding the message digest.
1013 * \param sig The buffer holding the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +01001014 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001015 * \return \c 0 if the verify operation was successful.
1016 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001017 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001019 int (*f_rng)(void *, unsigned char *, size_t),
1020 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001021 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001023 unsigned int hashlen,
1024 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001025 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001026
1027/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001028 * \brief This function performs a PKCS#1 v2.1 PSS verification
1029 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001030 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001031 * The hash function for the MGF mask generating function
1032 * is that specified in the RSA context.
Paul Bakkerb3869132013-02-28 17:21:01 +01001033 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001034 * \note The \p sig buffer must be as large as the size
1035 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001036 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001037 * \note The \p hash_id in the RSA context is the one used for the
1038 * verification. \p md_alg in the function call is the type of
1039 * hash that is verified. According to <em>RFC-3447: Public-Key
1040 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1041 * Specifications</em> it is advised to keep both hashes the
1042 * same. If \p hash_id in the RSA context is unset,
1043 * the \p md_alg from the function call is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001044 *
1045 * \deprecated It is deprecated and discouraged to call this function
1046 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1047 * are likely to remove the \p mode argument and have it
1048 * implicitly set to #MBEDTLS_RSA_PUBLIC.
1049 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001050 * \note Alternative implementations of RSA need not support
1051 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001052 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001053 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001054 * \param ctx The RSA public key context.
1055 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
1056 * \param p_rng The RNG context.
1057 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1058 * \param md_alg The message-digest algorithm used to hash the original data.
1059 * Use #MBEDTLS_MD_NONE for signing raw data.
1060 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
1061 * \param hash The buffer holding the message digest.
1062 * \param sig The buffer holding the ciphertext.
1063 *
1064 * \return \c 0 if the verify operation was successful.
1065 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001068 int (*f_rng)(void *, unsigned char *, size_t),
1069 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001070 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001072 unsigned int hashlen,
1073 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001074 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001075
1076/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001077 * \brief This function performs a PKCS#1 v2.1 PSS verification
1078 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001079 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001080 * The hash function for the MGF mask generating function
1081 * is that specified in \p mgf1_hash_id.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001082 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001083 * \note The \p sig buffer must be as large as the size
1084 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001085 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001086 * \note The \p hash_id in the RSA context is ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001087 *
1088 * \param ctx The RSA public key context.
1089 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
1090 * \param p_rng The RNG context.
1091 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1092 * \param md_alg The message-digest algorithm used to hash the original data.
1093 * Use #MBEDTLS_MD_NONE for signing raw data.
1094 * \param hashlen The length of the message digest. Only used if \p md_alg is
1095 * #MBEDTLS_MD_NONE.
1096 * \param hash The buffer holding the message digest.
1097 * \param mgf1_hash_id The message digest used for mask generation.
1098 * \param expected_salt_len The length of the salt used in padding. Use
1099 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1100 * \param sig The buffer holding the ciphertext.
1101 *
1102 * \return \c 0 if the verify operation was successful.
1103 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001106 int (*f_rng)(void *, unsigned char *, size_t),
1107 void *p_rng,
1108 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001110 unsigned int hashlen,
1111 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001113 int expected_salt_len,
1114 const unsigned char *sig );
1115
1116/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001117 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001118 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001119 * \param dst The destination context.
1120 * \param src The source context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001121 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001122 * \return \c 0 on success.
1123 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001126
1127/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001128 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001129 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001130 * \param ctx The RSA Context to free.
Paul Bakker5121ce52009-01-03 21:22:43 +00001131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001133
1134/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001135 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001137 * \return \c 0 on success.
1138 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001141
1142#ifdef __cplusplus
1143}
1144#endif
1145
Paul Bakker5121ce52009-01-03 21:22:43 +00001146#endif /* rsa.h */