blob: fb2f77f94f0ac599b40ac5eada1d6fc16ffff2dc [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Rose Zadik042e97f2018-01-26 16:35:10 +00004 * \brief The RSA public-key cryptosystem.
5 *
6 * For more information, see <em>Public-Key Cryptography Standards (PKCS)
7 * #1 v1.5: RSA Encryption</em> and <em>Public-Key Cryptography Standards
8 * (PKCS) #1 v2.1: RSA Cryptography Specifications</em>.
9 *
Darryl Greena40a1012018-01-05 15:33:17 +000010 */
11/*
Rose Zadik042e97f2018-01-26 16:35:10 +000012 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020013 * SPDX-License-Identifier: Apache-2.0
14 *
15 * Licensed under the Apache License, Version 2.0 (the "License"); you may
16 * not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
18 *
19 * http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
23 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000026 *
Rose Zadik042e97f2018-01-26 16:35:10 +000027 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000028 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#ifndef MBEDTLS_RSA_H
30#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020033#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#endif
Paul Bakkered27a042013-04-18 22:46:23 +020037
Paul Bakker314052f2011-08-15 09:07:52 +000038#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020039#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_THREADING_C)
Paul Bakkerc9965dc2013-09-29 14:58:17 +020042#include "threading.h"
43#endif
44
Paul Bakker13e2dfe2009-07-28 07:18:38 +000045/*
46 * RSA Error codes
47 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
49#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
50#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Rose Zadik042e97f2018-01-26 16:35:10 +000051#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 +020052#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
53#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
54#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
55#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
56#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Rose Zadik042e97f2018-01-26 16:35:10 +000057#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. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010058#define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580 /**< RSA hardware accelerator failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000059
60/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020061 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000062 */
Rose Zadik042e97f2018-01-26 16:35:10 +000063#define MBEDTLS_RSA_PUBLIC 0 /**< Request private key operation. */
64#define MBEDTLS_RSA_PRIVATE 1 /**< Request public key operation. */
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Rose Zadik042e97f2018-01-26 16:35:10 +000066#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS-1 v1.5 encoding. */
67#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS-1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000068
Rose Zadik042e97f2018-01-26 16:35:10 +000069#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
70#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020073
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020074/*
75 * The above constants may be used even if the RSA module is compile out,
76 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
77 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010078
79#if !defined(MBEDTLS_RSA_ALT)
80// Regular implementation
81//
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020082
Paul Bakker407a0da2013-06-27 14:29:21 +020083#ifdef __cplusplus
84extern "C" {
85#endif
86
Hanno Beckera3ebec22017-08-23 14:06:24 +010087/**
Rose Zadik042e97f2018-01-26 16:35:10 +000088 * \brief The RSA context structure.
Hanno Becker5063cd22017-09-29 11:49:12 +010089 *
90 * \note Direct manipulation of the members of this structure
Rose Zadik042e97f2018-01-26 16:35:10 +000091 * is deprecated. All manipulation should instead be done through
92 * the public interface functions.
Paul Bakker5121ce52009-01-03 21:22:43 +000093 */
94typedef struct
95{
Rose Zadik042e97f2018-01-26 16:35:10 +000096 int ver; /*!< Always 0.*/
97 size_t len; /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +000098
Rose Zadik042e97f2018-01-26 16:35:10 +000099 mbedtls_mpi N; /*!< The public modulus. */
100 mbedtls_mpi E; /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
Rose Zadik042e97f2018-01-26 16:35:10 +0000102 mbedtls_mpi D; /*!< The private exponent. */
103 mbedtls_mpi P; /*!< The first prime factor. */
104 mbedtls_mpi Q; /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100105
Rose Zadik042e97f2018-01-26 16:35:10 +0000106 mbedtls_mpi DP; /*!< \p D % (P - 1) */
107 mbedtls_mpi DQ; /*!< \p D % (Q - 1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
Rose Zadik042e97f2018-01-26 16:35:10 +0000110 mbedtls_mpi RN; /*!< cached R^2 mod \p N */
Hanno Becker1a59e792017-08-23 07:41:10 +0100111
Rose Zadik042e97f2018-01-26 16:35:10 +0000112 mbedtls_mpi RP; /*!< cached R^2 mod \p P */
113 mbedtls_mpi RQ; /*!< cached R^2 mod \p Q */
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Rose Zadik042e97f2018-01-26 16:35:10 +0000115 mbedtls_mpi Vi; /*!< The cached blinding value. */
116 mbedtls_mpi Vf; /*!< The cached un-blinding value. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200117
Rose Zadik042e97f2018-01-26 16:35:10 +0000118 int padding; /*!< Selects padding mode:
119 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
120 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
121 int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
122 as specified in md.h for use in the MGF
123 mask generating function used in the
124 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#if defined(MBEDTLS_THREADING_C)
Rose Zadik042e97f2018-01-26 16:35:10 +0000126 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200127#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000128}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
Paul Bakker5121ce52009-01-03 21:22:43 +0000131/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000132 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000134 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000135 * encryption scheme and the RSASSA-PSS signature scheme.
136 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000137 * \param ctx The RSA context to initialize.
138 * \param padding Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or
139 * #MBEDTLS_RSA_PKCS_V21.
140 * \param hash_id The hash identifier of #mbedtls_md_type_t type, if
141 * \p padding is #MBEDTLS_RSA_PKCS_V21.
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000143 * \note The \p hash_id parameter is ignored when using
144 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200145 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000146 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200147 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000148 * mixing padding modes. For public key operations it is
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200149 * a default value, which can be overriden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000150 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200151 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000152 * \note The hash selected in \p hash_id is always used for OEAP
153 * encryption. For PSS signatures, it is always used for
154 * making signatures, but can be overriden for verifying them.
155 * If set to #MBEDTLS_MD_NONE, it is always overriden.
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100158 int padding,
159 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100161/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000162 * \brief This function imports a set of core parameters into an
163 * RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100164 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000165 * \param ctx The initialized RSA context to store the parameters in.
166 * \param N The RSA modulus, or NULL.
167 * \param P The first prime factor of \p N, or NULL.
168 * \param Q The second prime factor of \p N, or NULL.
169 * \param D The private exponent, or NULL.
170 * \param E The public exponent, or NULL.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100171 *
172 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000173 * imports, if the parameters are not simultaneously present.
174 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100175 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000176 * by a call to mbedtls_rsa_complete(), which checks and
177 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100178 * public or private RSA key.
179 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000180 * \note See mbedtls_rsa_complete() for more information on which
181 * parameters are necessary to set up a private or public
182 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100183 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100184 * \note The imported parameters are copied and need not be preserved
185 * for the lifetime of the RSA context being set up.
186 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000187 * \return \c 0 on success, or a non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100188 */
189int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
190 const mbedtls_mpi *N,
191 const mbedtls_mpi *P, const mbedtls_mpi *Q,
192 const mbedtls_mpi *D, const mbedtls_mpi *E );
193
194/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000195 * \brief This function imports core RSA parameters, in raw big-endian
196 * binary format, into an RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100197 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000198 * \param ctx The initialized RSA context to store the parameters in.
199 * \param N The RSA modulus, or NULL.
200 * \param N_len The Byte length of \p N, ignored if \p N == NULL.
201 * \param P The first prime factor of \p N, or NULL.
202 * \param P_len The Byte length of \p P, ignored if \p P == NULL.
203 * \param Q The second prime factor of \p N, or NULL.
204 * \param Q_len The Byte length of \p Q, ignored if \p Q == NULL.
205 * \param D The private exponent, or NULL.
206 * \param D_len The Byte length of \p D, ignored if \p D == NULL.
207 * \param E The public exponent, or NULL.
208 * \param E_len The Byte length of \p E, ignored if \p E == NULL.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100209 *
210 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000211 * imports, if the parameters are not simultaneously present.
212 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100213 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000214 * by a call to mbedtls_rsa_complete(), which checks and
215 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100216 * public or private RSA key.
217 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000218 * \note See mbedtls_rsa_complete() for more information on which
219 * parameters are necessary to set up a private or public
220 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100221 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100222 * \note The imported parameters are copied and need not be preserved
223 * for the lifetime of the RSA context being set up.
224 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000225 * \return \c 0 on success, or a non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100226 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100227int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100228 unsigned char const *N, size_t N_len,
229 unsigned char const *P, size_t P_len,
230 unsigned char const *Q, size_t Q_len,
231 unsigned char const *D, size_t D_len,
232 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100233
234/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000235 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100236 * a set of imported core parameters.
237 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000238 * To setup an RSA public key, precisely \p N and \p E
239 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100240 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000241 * To setup an RSA private key, sufficient information must
242 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100243 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000244 * The default implementation supports the following:
245 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
246 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
247 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100248 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000249 * If this function runs successfully, it guarantees that
250 * the RSA context can be used for RSA operations without
251 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100252 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000253 * \param ctx The initialized RSA context holding imported parameters.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100254 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000255 * \return \c 0 on success, or #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the
256 * attempted derivations failed.
Hanno Becker43a08d02017-10-02 13:16:35 +0100257 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100258 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000259 * for the imported parameters. In particular, parameters that
260 * are not needed by the implementation might be silently
261 * discarded and left unchecked. To check the consistency
262 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100263 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100264 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100265int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100266
267/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000268 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100269 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000270 * If this function runs successfully, the non-NULL buffers
271 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
272 * written, with additional unused space filled leading by
273 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100274 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000275 * Possible reasons for returning
276 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:<ul>
277 * <li>An alternative RSA implementation is in use, which
278 * stores the key externally, and either cannot or should
279 * not export it into RAM.</li>
280 * <li>A SW or HW implementation might not support a certain
281 * deduction. For example, \p P, \p Q from \p N, \p D,
282 * and \p E if the former are not part of the
283 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100284 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000285 * If the function fails due to an unsupported operation,
286 * the RSA context stays intact and remains usable.
287 *
288 * \param ctx The initialized RSA context.
289 * \param N The MPI to hold the RSA modulus, or NULL.
290 * \param P The MPI to hold the first prime factor of \p N, or NULL.
291 * \param Q The MPI to hold the second prime factor of \p N, or NULL.
292 * \param D The MPI to hold the private exponent, or NULL.
293 * \param E The MPI to hold the public exponent, or NULL.
294 *
295 * \return \c 0 on success,
296 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION if exporting the
297 * requested parameters cannot be done due to missing
298 * functionality or because of security policies,
299 * or a non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100300 *
301 */
302int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
303 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
304 mbedtls_mpi *D, mbedtls_mpi *E );
305
306/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000307 * \brief This function exports core parameters of an RSA key
308 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100309 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000310 * If this function runs successfully, the non-NULL buffers
311 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
312 * written, with additional unused space filled leading by
313 * zero Bytes.
314 *
315 * Possible reasons for returning
316 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:<ul>
317 * <li>An alternative RSA implementation is in use, which
318 * stores the key externally, and either cannot or should
319 * not export it into RAM.</li>
320 * <li>A SW or HW implementation might not support a certain
321 * deduction. For example, \p P, \p Q from \p N, \p D,
322 * and \p E if the former are not part of the
323 * implementation.</li></ul>
324 * If the function fails due to an unsupported operation,
325 * the RSA context stays intact and remains usable.
326 *
327 * \param ctx The initialized RSA context.
328 * \param N The Byte array to store the RSA modulus, or NULL.
329 * \param N_len The size of the buffer for the modulus.
330 * \param P The Byte array to hold the first prime factor of \p N, or
331 * NULL.
332 * \param P_len The size of the buffer for the first prime factor.
333 * \param Q The Byte array to hold the second prime factor of \p N, or
334 NULL.
335 * \param Q_len The size of the buffer for the second prime factor.
336 * \param D The Byte array to hold the private exponent, or NULL.
337 * \param D_len The size of the buffer for the private exponent.
338 * \param E The Byte array to hold the public exponent, or NULL.
339 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100340 *
341 * \note The length fields are ignored if the corresponding
342 * buffer pointers are NULL.
343 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000344 * \return \c 0 on success,
345 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION if exporting the
346 * requested parameters cannot be done due to missing
347 * functionality or because of security policies,
348 * or a non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100349 */
350int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
351 unsigned char *N, size_t N_len,
352 unsigned char *P, size_t P_len,
353 unsigned char *Q, size_t Q_len,
354 unsigned char *D, size_t D_len,
355 unsigned char *E, size_t E_len );
356
357/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000358 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100359 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000360 * \param ctx The initialized RSA context.
361 * \param DP The MPI to hold D modulo P-1, or NULL.
362 * \param DQ The MPI to hold D modulo Q-1, or NULL.
363 * \param QP The MPI to hold modular inverse of Q modulo P, or NULL.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100364 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000365 * \return \c 0 on success, non-zero error code otherwise.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100366 *
367 * \note Alternative RSA implementations not using CRT-parameters
Rose Zadik042e97f2018-01-26 16:35:10 +0000368 * internally can implement this function based on
369 * mbedtls_rsa_deduce_opt().
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100370 *
371 */
372int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
373 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
374
Paul Bakker5121ce52009-01-03 21:22:43 +0000375/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000376 * \brief This function sets padding for an already initialized RSA
377 * context. See mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100378 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000379 * \param ctx The RSA context to be set.
380 * \param padding Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or
381 * #MBEDTLS_RSA_PKCS_V21.
382 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100383 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100384void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
385 int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100386
387/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000388 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100389 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000390 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100391 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000392 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100393 *
394 */
395size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
396
397/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000398 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000399 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000400 * \param ctx The RSA context used to hold the key.
401 * \param f_rng The RNG function.
402 * \param p_rng The RNG parameter.
403 * \param nbits The size of the public key in bits.
404 * \param exponent The public exponent. For example, 65537.
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000406 * \note mbedtls_rsa_init() must be called before this function,
407 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000409 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
410 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100413 int (*f_rng)(void *, unsigned char *, size_t),
414 void *p_rng,
415 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000416
417/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000418 * \brief This function checks if a context contains at least an RSA
419 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000421 * If the function runs successfully, it is guaranteed that
422 * enough information is present to perform an RSA public key
423 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000425 * \param ctx The RSA context to check.
426 *
427 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
428 * on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100429 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000432
433/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000434 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100435 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000436 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000437 * \param ctx The RSA context to check.
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000439 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code on
440 * failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100441 *
Hanno Becker68767a62017-10-17 10:13:31 +0100442 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000443 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100444 * on the given context, but that the various parameters are
445 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000446 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100447 *
448 * \warning This function should catch accidental misconfigurations
449 * like swapping of parameters, but it cannot establish full
450 * trust in neither the quality nor the consistency of the key
451 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000452 * <ul><li>Consistency: Imported parameters that are irrelevant
453 * for the implementation might be silently dropped. If dropped,
454 * the current function does not have access to them,
455 * and therefore cannot check them. See mbedtls_rsa_complete().
456 * If you want to check the consistency of the entire
457 * content of an PKCS1-encoded RSA private key, for example, you
458 * should use mbedtls_rsa_validate_params() before setting
459 * up the RSA context.
460 * Additionally, if the implementation performs empirical checks,
461 * these checks substantiate but do not guarantee consistency.</li>
462 * <li>Quality: This function is not expected to perform
463 * extended quality assessments like checking that the prime
464 * factors are safe. Additionally, it is the responsibility of the
465 * user to ensure the trustworthiness of the source of his RSA
466 * parameters, which goes beyond what is effectively checkable
467 * by the library.</li></ul>
Paul Bakker5121ce52009-01-03 21:22:43 +0000468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
471/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000472 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100473 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000474 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100475 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000476 * \param pub The RSA context holding the public key.
477 * \param prv The RSA context holding the private key.
478 *
479 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
480 * on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100481 */
Hanno Becker98838b02017-10-02 13:16:10 +0100482int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
483 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100484
485/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000486 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000488 * \param ctx The RSA context.
489 * \param input The input buffer.
490 * \param output The output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000491 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000492 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
493 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000494 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000495 * \note This function does not handle message padding.
496 *
497 * \note Make sure to set \p input[0] = 0 or ensure that
498 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 *
500 * \note The input and output buffers must be large
Rose Zadik042e97f2018-01-26 16:35:10 +0000501 * enough. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker5121ce52009-01-03 21:22:43 +0000502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000504 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000505 unsigned char *output );
506
507/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000508 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000510 * \param ctx The RSA context.
511 * \param f_rng The RNG function. Needed for blinding.
512 * \param p_rng The RNG parameter.
513 * \param input The input buffer.
514 * \param output The output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000516 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
517 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 *
519 * \note The input and output buffers must be large
Rose Zadik042e97f2018-01-26 16:35:10 +0000520 * enough. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200523 int (*f_rng)(void *, unsigned char *, size_t),
524 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000525 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 unsigned char *output );
527
528/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000529 * \brief This function adds the message padding, then performs an RSA
530 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000532 * It is the generic wrapper for performing a PKCS#1 encryption
533 * operation using the \p mode from the context.
534 *
535 *
536 * \param ctx The RSA context.
537 * \param f_rng The RNG function. Needed for padding, PKCS#1 v2.1
538 * encoding, and #MBEDTLS_RSA_PRIVATE.
539 * \param p_rng The RNG parameter.
540 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
541 * \param ilen The length of the plaintext.
542 * \param input The buffer holding the data to encrypt.
543 * \param output The buffer used to hold the ciphertext.
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100545 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000546 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
547 * are likely to remove the \p mode argument and have it
548 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100549 *
550 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000551 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
552 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100553 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000554 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
555 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000557 * \note The input and output buffers must be as large as the size
558 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000561 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000562 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000563 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000564 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 unsigned char *output );
566
567/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000568 * \brief This function performs a PKCS#1 v1.5 encryption operation
569 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100570 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000571 * \param ctx The RSA context.
572 * \param f_rng The RNG function. Needed for padding and
573 * #MBEDTLS_RSA_PRIVATE.
574 * \param p_rng The RNG parameter.
575 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
576 * \param ilen The length of the plaintext.
577 * \param input The buffer holding the data to encrypt.
578 * \param output The buffer used to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100579 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100580 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000581 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
582 * are likely to remove the \p mode argument and have it
583 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100584 *
585 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000586 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
587 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100588 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000589 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
590 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100591 *
592 * \note The output buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000593 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100596 int (*f_rng)(void *, unsigned char *, size_t),
597 void *p_rng,
598 int mode, size_t ilen,
599 const unsigned char *input,
600 unsigned char *output );
601
602/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000603 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
604 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100605 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000606 * \param ctx The RSA context.
607 * \param f_rng The RNG function. Needed for padding and PKCS#1 v2.1
608 * encoding and #MBEDTLS_RSA_PRIVATE.
609 * \param p_rng The RNG parameter.
610 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
611 * \param label The buffer holding the custom label to use.
612 * \param label_len The length of the label.
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.
Paul Bakkerb3869132013-02-28 17:21:01 +0100616 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100617 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000618 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
619 * are likely to remove the \p mode argument and have it
620 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100621 *
622 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000623 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
624 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100625 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000626 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
627 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100628 *
629 * \note The output buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000630 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100633 int (*f_rng)(void *, unsigned char *, size_t),
634 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100635 int mode,
636 const unsigned char *label, size_t label_len,
637 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100638 const unsigned char *input,
639 unsigned char *output );
640
641/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000642 * \brief This function performs an RSA operation, then removes the
643 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000645 * It is the generic wrapper for performing a PKCS#1 decryption
646 * operation using the \p mode from the context.
647 *
648 * \param ctx The RSA context.
649 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
650 * \param p_rng The RNG parameter.
651 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
652 * \param olen The length of the plaintext.
653 * \param input The buffer holding the encrypted data.
654 * \param output The buffer used to hold the plaintext.
655 * \param output_max_len The maximum length of the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000656 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100657 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000658 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
659 * are likely to remove the \p mode argument and have it
660 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100661 *
662 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000663 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
664 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100665 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000666 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
667 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100669 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000670 * as large as the size \p ctx->len of \p ctx->N (for example,
671 * 128 Bytes if RSA-1024 is used) to be able to hold an
672 * arbitrary decrypted message. If it is not large enough to
673 * hold the decryption of the particular ciphertext provided,
674 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100675 *
676 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000677 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker5121ce52009-01-03 21:22:43 +0000678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200680 int (*f_rng)(void *, unsigned char *, size_t),
681 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000682 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000683 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000684 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000685 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000686
687/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000688 * \brief This function performs a PKCS#1 v1.5 decryption
689 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100690 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000691 * \param ctx The RSA context.
692 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
693 * \param p_rng The RNG parameter.
694 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
695 * \param olen The length of the plaintext.
696 * \param input The buffer holding the encrypted data.
697 * \param output The buffer to hold the plaintext.
698 * \param output_max_len The maximum length of the output buffer.
Paul Bakkerb3869132013-02-28 17:21:01 +0100699 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100700 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000701 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
702 * are likely to remove the \p mode argument and have it
703 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100704 *
705 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000706 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
707 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100708 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000709 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
710 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100711 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100712 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000713 * as large as the size \p ctx->len of \p ctx->N, for example,
714 * 128 Bytes if RSA-1024 is used, to be able to hold an
715 * arbitrary decrypted message. If it is not large enough to
716 * hold the decryption of the particular ciphertext provided,
717 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100718 *
719 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000720 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200723 int (*f_rng)(void *, unsigned char *, size_t),
724 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100725 int mode, size_t *olen,
726 const unsigned char *input,
727 unsigned char *output,
728 size_t output_max_len );
729
730/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000731 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
732 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100733 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000734 * \param ctx The RSA context.
735 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
736 * \param p_rng The RNG parameter.
737 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
738 * \param label The buffer holding the custom label to use.
739 * \param label_len The length of the label.
740 * \param olen The length of the plaintext.
741 * \param input The buffer holding the encrypted data.
742 * \param output The buffer to hold the plaintext.
743 * \param output_max_len The maximum length of the output buffer.
Paul Bakkerb3869132013-02-28 17:21:01 +0100744 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100745 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000746 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
747 * are likely to remove the \p mode argument and have it
748 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100749 *
750 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000751 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
752 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100753 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000754 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
755 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100756 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100757 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000758 * as large as the size \p ctx->len of \p ctx->N, for
759 * example, 128 Bytes if RSA-1024 is used, to be able to
760 * hold an arbitrary decrypted message. If it is not
761 * large enough to hold the decryption of the particular
762 * ciphertext provided, the function returns
763 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100764 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100765 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000766 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200769 int (*f_rng)(void *, unsigned char *, size_t),
770 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100771 int mode,
772 const unsigned char *label, size_t label_len,
773 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100774 const unsigned char *input,
775 unsigned char *output,
776 size_t output_max_len );
777
778/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000779 * \brief This function performs a private RSA operation to sign
780 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000781 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000782 * It is the generic wrapper for performing a PKCS#1
783 * signature using the \p mode from the context.
784 *
785 * \param ctx The RSA context.
786 * \param f_rng The RNG function. Needed for PKCS#1 v2.1 encoding and for
787 * #MBEDTLS_RSA_PRIVATE.
788 * \param p_rng The RNG parameter.
789 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
790 * \param md_alg The message-digest algorithm used to hash the original data.
791 * Use #MBEDTLS_MD_NONE for signing raw data.
792 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
793 * \param hash The buffer holding the message digest.
794 * \param sig The buffer to hold the ciphertext.
Paul Bakker5121ce52009-01-03 21:22:43 +0000795 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100796 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000797 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
798 * are likely to remove the \p mode argument and have it
799 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100800 *
801 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000802 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
803 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100804 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000805 * \return \c 0 if the signing operation was successful,
806 * or an \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000807 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000808 * \note The \p sig buffer must be as large as the size
809 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000810 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000811 * \note For PKCS#1 v2.1 encoding, see comments on
812 * mbedtls_rsa_rsassa_pss_sign() for details on
813 * \p md_alg and \p hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000814 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000816 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000817 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000818 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000820 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000821 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000822 unsigned char *sig );
823
824/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000825 * \brief This function performs a PKCS#1 v1.5 signature
826 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100827 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000828 * \param ctx The RSA context.
829 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
830 * \param p_rng The RNG parameter.
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.
Paul Bakkerb3869132013-02-28 17:21:01 +0100837 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100838 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000839 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
840 * are likely to remove the \p mode argument and have it
841 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100842 *
843 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000844 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
845 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100846 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000847 * \return \c 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100848 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +0000849 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100850 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000851 * \note The \p sig buffer must be as large as the size
852 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100853 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200855 int (*f_rng)(void *, unsigned char *, size_t),
856 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100857 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100859 unsigned int hashlen,
860 const unsigned char *hash,
861 unsigned char *sig );
862
863/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000864 * \brief This function performs a PKCS#1 v2.1 PSS signature
865 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100866 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000867 * \param ctx The RSA context.
868 * \param f_rng The RNG function. Needed for PKCS#1 v2.1 encoding and for
869 * #MBEDTLS_RSA_PRIVATE.
870 * \param p_rng The RNG parameter.
871 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
872 * \param md_alg The message-digest algorithm used to hash the original data.
873 * Use #MBEDTLS_MD_NONE for signing raw data.
874 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
875 * \param hash The buffer holding the message digest.
876 * \param sig The buffer to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100877 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100878 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000879 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
880 * are likely to remove the \p mode argument and have it
881 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100882 *
883 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000884 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
885 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100886 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000887 * \return \c 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100888 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +0000889 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100890 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000891 * \note The \p sig buffer must be as large as the size
892 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100893 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000894 * \note The \p hash_id in the RSA context is the one used for the
895 * encoding. \p md_alg in the function call is the type of hash
896 * that is encoded. According to <em>RFC-3447: Public-Key
897 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
898 * Specifications</em> it is advised to keep both hashes the
899 * same.
Paul Bakkerb3869132013-02-28 17:21:01 +0100900 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100902 int (*f_rng)(void *, unsigned char *, size_t),
903 void *p_rng,
904 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100906 unsigned int hashlen,
907 const unsigned char *hash,
908 unsigned char *sig );
909
910/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000911 * \brief This function performs a public RSA operation and checks
912 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +0000913 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000914 * This is the generic wrapper for performing a PKCS#1
915 * verification using the mode from the context.
916 *
917 * \param ctx The RSA public key context.
918 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
919 * \param p_rng The RNG parameter.
920 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
921 * \param md_alg The message-digest algorithm used to hash the original data.
922 * Use #MBEDTLS_MD_NONE for signing raw data.
923 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
924 * \param hash The buffer holding the message digest.
925 * \param sig The buffer holding the ciphertext.
Paul Bakker5121ce52009-01-03 21:22:43 +0000926 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100927 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000928 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
929 * are likely to remove the \p mode argument and have it
930 * set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100931 *
932 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000933 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
934 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100935 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000936 * \return \c 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100937 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +0000938 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000939 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000940 * \note The \p sig buffer must be as large as the size
941 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000942 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000943 * \note For PKCS#1 v2.1 encoding, see comments on
944 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
945 * \p hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000946 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200948 int (*f_rng)(void *, unsigned char *, size_t),
949 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000950 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000952 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000953 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200954 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000955
956/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000957 * \brief This function performs a PKCS#1 v1.5 verification
958 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +0100959 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000960 * \param ctx The RSA public key context.
961 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
962 * \param p_rng The RNG parameter.
963 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
964 * \param md_alg The message-digest algorithm used to hash the original data.
965 * Use #MBEDTLS_MD_NONE for signing raw data.
966 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
967 * \param hash The buffer holding the message digest.
968 * \param sig The buffer holding the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100969 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100970 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000971 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
972 * are likely to remove the \p mode argument and have it
973 * set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100974 *
975 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000976 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
977 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100978 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000979 * \return \c 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100980 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +0000981 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100982 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000983 * \note The \p sig buffer must be as large as the size
984 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100985 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200987 int (*f_rng)(void *, unsigned char *, size_t),
988 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100989 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100991 unsigned int hashlen,
992 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200993 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100994
995/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000996 * \brief This function performs a PKCS#1 v2.1 PSS verification
997 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +0100998 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000999 * The hash function for the MGF mask generating function
1000 * is that specified in the RSA context.
1001 *
1002 * \param ctx The RSA public key context.
1003 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
1004 * \param p_rng The RNG parameter.
1005 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1006 * \param md_alg The message-digest algorithm used to hash the original data.
1007 * Use #MBEDTLS_MD_NONE for signing raw data.
1008 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
1009 * \param hash The buffer holding the message digest.
1010 * \param sig The buffer holding the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +01001011 *
Hanno Becker3cdc7112017-10-05 10:09:31 +01001012 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +00001013 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1014 * are likely to remove the \p mode argument and have it
1015 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +01001016 *
1017 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +00001018 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
1019 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +01001020 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001021 * \return \c 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001022 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +00001023 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001024 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001025 * \note The \p sig buffer must be as large as the size
1026 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001027 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001028 * \note The \p hash_id in the RSA context is the one used for the
1029 * verification. \p md_alg in the function call is the type of
1030 * hash that is verified. According to <em>RFC-3447: Public-Key
1031 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1032 * Specifications</em> it is advised to keep both hashes the
1033 * same. If \p hash_id in the RSA context is unset,
1034 * the \p md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001035 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001037 int (*f_rng)(void *, unsigned char *, size_t),
1038 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001039 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001041 unsigned int hashlen,
1042 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001043 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001044
1045/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001046 * \brief This function performs a PKCS#1 v2.1 PSS verification
1047 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001048 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001049 * The hash function for the MGF mask generating function
1050 * is that specified in \p mgf1_hash_id.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001051 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001052 * \param ctx The RSA public key context.
1053 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
1054 * \param p_rng The RNG parameter.
1055 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1056 * \param md_alg The message-digest algorithm used to hash the original data.
1057 * Use #MBEDTLS_MD_NONE for signing raw data.
1058 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
1059 * \param hash The buffer holding the message digest.
1060 * \param mgf1_hash_id The message digest used for mask generation.
1061 * \param expected_salt_len The length of the salt used in padding. Use
1062 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1063 * \param sig The buffer holding the ciphertext.
1064 *
1065 * \return \c 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001066 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +00001067 * on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001068 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001069 * \note The \p sig buffer must be as large as the size
1070 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001071 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001072 * \note The \p hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001073 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001075 int (*f_rng)(void *, unsigned char *, size_t),
1076 void *p_rng,
1077 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001079 unsigned int hashlen,
1080 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001082 int expected_salt_len,
1083 const unsigned char *sig );
1084
1085/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001086 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001087 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001088 * \param dst The destination context.
1089 * \param src The source context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001090 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001091 * \return \c 0 on success,
1092 * #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001095
1096/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001097 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001098 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001099 * \param ctx The RSA Context to free.
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001102
Hanno Beckerd22b78b2017-10-12 11:42:17 +01001103#ifdef __cplusplus
1104}
1105#endif
1106
1107#else /* MBEDTLS_RSA_ALT */
1108#include "rsa_alt.h"
1109#endif /* MBEDTLS_RSA_ALT */
1110
1111#ifdef __cplusplus
1112extern "C" {
1113#endif
1114
Paul Bakker5121ce52009-01-03 21:22:43 +00001115/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001116 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001118 * \return \c 0 on success, or \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001119 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001121
1122#ifdef __cplusplus
1123}
1124#endif
1125
Paul Bakker5121ce52009-01-03 21:22:43 +00001126#endif /* rsa.h */