blob: 6ede7cb1cbc8c58afe27aaf30132100df41cd2f5 [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.
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
Rose Zadik042e97f2018-01-26 16:35:10 +00006 * 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
Paul Bakker407a0da2013-06-27 14:29:21 +020079#ifdef __cplusplus
80extern "C" {
81#endif
82
Ron Eldor4e6d55d2018-02-07 16:36:15 +020083#if !defined(MBEDTLS_RSA_ALT)
84// Regular implementation
85//
86
Paul Bakker5121ce52009-01-03 21:22:43 +000087/**
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 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
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 */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200114
Rose Zadik042e97f2018-01-26 16:35:10 +0000115 mbedtls_mpi Vi; /*!< The cached blinding value. */
116 mbedtls_mpi Vf; /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000117
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
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200131#else /* MBEDTLS_RSA_ALT */
132#include "rsa_alt.h"
133#endif /* MBEDTLS_RSA_ALT */
134
Paul Bakker5121ce52009-01-03 21:22:43 +0000135/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000136 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000138 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000139 * encryption scheme and the RSASSA-PSS signature scheme.
140 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000141 * \param ctx The RSA context to initialize.
142 * \param padding Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or
143 * #MBEDTLS_RSA_PKCS_V21.
144 * \param hash_id The hash identifier of #mbedtls_md_type_t type, if
145 * \p padding is #MBEDTLS_RSA_PKCS_V21.
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 *
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.
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100162 int padding,
163 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
165/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000166 * \brief This function imports a set of core parameters into an
167 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100168 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000169 * \param ctx The initialized RSA context to store the parameters in.
170 * \param N The RSA modulus, or NULL.
171 * \param P The first prime factor of \p N, or NULL.
172 * \param Q The second prime factor of \p N, or NULL.
173 * \param D The private exponent, or NULL.
174 * \param E The public exponent, or NULL.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100175 *
176 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000177 * imports, if the parameters are not simultaneously present.
178 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100179 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000180 * by a call to mbedtls_rsa_complete(), which checks and
181 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100182 * public or private RSA key.
183 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000184 * \note See mbedtls_rsa_complete() for more information on which
185 * parameters are necessary to set up a private or public
186 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100187 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100188 * \note The imported parameters are copied and need not be preserved
189 * for the lifetime of the RSA context being set up.
190 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000191 * \return \c 0 on success, or a non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100192 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100193int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
194 const mbedtls_mpi *N,
195 const mbedtls_mpi *P, const mbedtls_mpi *Q,
196 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100197
198/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000199 * \brief This function imports core RSA parameters, in raw big-endian
200 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000202 * \param ctx The initialized RSA context to store the parameters in.
203 * \param N The RSA modulus, or NULL.
204 * \param N_len The Byte length of \p N, ignored if \p N == NULL.
205 * \param P The first prime factor of \p N, or NULL.
206 * \param P_len The Byte length of \p P, ignored if \p P == NULL.
207 * \param Q The second prime factor of \p N, or NULL.
208 * \param Q_len The Byte length of \p Q, ignored if \p Q == NULL.
209 * \param D The private exponent, or NULL.
210 * \param D_len The Byte length of \p D, ignored if \p D == NULL.
211 * \param E The public exponent, or NULL.
212 * \param E_len The Byte length of \p E, ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100214 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000215 * imports, if the parameters are not simultaneously present.
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100217 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000218 * by a call to mbedtls_rsa_complete(), which checks and
219 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100220 * public or private RSA key.
221 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000222 * \note See mbedtls_rsa_complete() for more information on which
223 * parameters are necessary to set up a private or public
224 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100225 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100226 * \note The imported parameters are copied and need not be preserved
227 * for the lifetime of the RSA context being set up.
228 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000229 * \return \c 0 on success, or a non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100230 */
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100231int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100232 unsigned char const *N, size_t N_len,
233 unsigned char const *P, size_t P_len,
234 unsigned char const *Q, size_t Q_len,
235 unsigned char const *D, size_t D_len,
236 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100237
238/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000239 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100240 * a set of imported core parameters.
241 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000242 * To setup an RSA public key, precisely \p N and \p E
243 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100244 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000245 * To setup an RSA private key, sufficient information must
246 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100247 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000248 * The default implementation supports the following:
249 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
250 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
251 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100252 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000253 * If this function runs successfully, it guarantees that
254 * the RSA context can be used for RSA operations without
255 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100256 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000257 * \param ctx The initialized RSA context holding imported parameters.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100258 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000259 * \return \c 0 on success, or #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the
260 * attempted derivations failed.
Hanno Becker43a08d02017-10-02 13:16:35 +0100261 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100262 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000263 * for the imported parameters. In particular, parameters that
264 * are not needed by the implementation might be silently
265 * discarded and left unchecked. To check the consistency
266 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100267 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100268 */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100269int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100270
271/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000272 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100273 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000274 * If this function runs successfully, the non-NULL buffers
275 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
276 * written, with additional unused space filled leading by
277 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100278 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000279 * Possible reasons for returning
280 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:<ul>
281 * <li>An alternative RSA implementation is in use, which
282 * stores the key externally, and either cannot or should
283 * not export it into RAM.</li>
284 * <li>A SW or HW implementation might not support a certain
285 * deduction. For example, \p P, \p Q from \p N, \p D,
286 * and \p E if the former are not part of the
287 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100288 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000289 * If the function fails due to an unsupported operation,
290 * the RSA context stays intact and remains usable.
291 *
292 * \param ctx The initialized RSA context.
293 * \param N The MPI to hold the RSA modulus, or NULL.
294 * \param P The MPI to hold the first prime factor of \p N, or NULL.
295 * \param Q The MPI to hold the second prime factor of \p N, or NULL.
296 * \param D The MPI to hold the private exponent, or NULL.
297 * \param E The MPI to hold the public exponent, or NULL.
298 *
299 * \return \c 0 on success,
300 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION if exporting the
301 * requested parameters cannot be done due to missing
302 * functionality or because of security policies,
303 * or a non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100304 *
305 */
306int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
307 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
308 mbedtls_mpi *D, mbedtls_mpi *E );
309
310/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000311 * \brief This function exports core parameters of an RSA key
312 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100313 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000314 * If this function runs successfully, the non-NULL buffers
315 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
316 * written, with additional unused space filled leading by
317 * zero Bytes.
318 *
319 * Possible reasons for returning
320 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:<ul>
321 * <li>An alternative RSA implementation is in use, which
322 * stores the key externally, and either cannot or should
323 * not export it into RAM.</li>
324 * <li>A SW or HW implementation might not support a certain
325 * deduction. For example, \p P, \p Q from \p N, \p D,
326 * and \p E if the former are not part of the
327 * implementation.</li></ul>
328 * If the function fails due to an unsupported operation,
329 * the RSA context stays intact and remains usable.
330 *
331 * \param ctx The initialized RSA context.
332 * \param N The Byte array to store the RSA modulus, or NULL.
333 * \param N_len The size of the buffer for the modulus.
334 * \param P The Byte array to hold the first prime factor of \p N, or
335 * NULL.
336 * \param P_len The size of the buffer for the first prime factor.
337 * \param Q The Byte array to hold the second prime factor of \p N, or
338 NULL.
339 * \param Q_len The size of the buffer for the second prime factor.
340 * \param D The Byte array to hold the private exponent, or NULL.
341 * \param D_len The size of the buffer for the private exponent.
342 * \param E The Byte array to hold the public exponent, or NULL.
343 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100344 *
345 * \note The length fields are ignored if the corresponding
346 * buffer pointers are NULL.
347 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000348 * \return \c 0 on success,
349 * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION if exporting the
350 * requested parameters cannot be done due to missing
351 * functionality or because of security policies,
352 * or a non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100353 */
354int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
355 unsigned char *N, size_t N_len,
356 unsigned char *P, size_t P_len,
357 unsigned char *Q, size_t Q_len,
358 unsigned char *D, size_t D_len,
359 unsigned char *E, size_t E_len );
360
361/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000362 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100363 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000364 * \param ctx The initialized RSA context.
365 * \param DP The MPI to hold D modulo P-1, or NULL.
366 * \param DQ The MPI to hold D modulo Q-1, or NULL.
367 * \param QP The MPI to hold modular inverse of Q modulo P, or NULL.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100368 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000369 * \return \c 0 on success, non-zero error code otherwise.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100370 *
371 * \note Alternative RSA implementations not using CRT-parameters
Rose Zadik042e97f2018-01-26 16:35:10 +0000372 * internally can implement this function based on
373 * mbedtls_rsa_deduce_opt().
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100374 *
375 */
376int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
377 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
378
Paul Bakker5121ce52009-01-03 21:22:43 +0000379/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000380 * \brief This function sets padding for an already initialized RSA
381 * context. See mbedtls_rsa_init() for details.
Paul Bakker5121ce52009-01-03 21:22:43 +0000382 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000383 * \param ctx The RSA context to be set.
384 * \param padding Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or
385 * #MBEDTLS_RSA_PKCS_V21.
386 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Paul Bakker40e46942009-01-03 21:51:57 +0000387 */
Hanno Becker8fd55482017-08-23 14:07:48 +0100388void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
389 int hash_id);
Paul Bakker21eb2802010-08-16 11:10:02 +0000390
Paul Bakkera3d195c2011-11-27 21:07:34 +0000391/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000392 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100393 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000394 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100395 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000396 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100397 *
398 */
399size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
400
401/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000402 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000403 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000404 * \param ctx The RSA context used to hold the key.
405 * \param f_rng The RNG function.
406 * \param p_rng The RNG parameter.
407 * \param nbits The size of the public key in bits.
408 * \param exponent The public exponent. For example, 65537.
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000410 * \note mbedtls_rsa_init() must be called before this function,
411 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000413 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
414 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100417 int (*f_rng)(void *, unsigned char *, size_t),
418 void *p_rng,
419 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000420
421/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000422 * \brief This function checks if a context contains at least an RSA
423 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000425 * If the function runs successfully, it is guaranteed that
426 * enough information is present to perform an RSA public key
427 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000429 * \param ctx The RSA context to check.
430 *
431 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
432 * on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100433 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000436
437/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000438 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100439 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000440 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000441 * \param ctx The RSA context to check.
Paul Bakker5121ce52009-01-03 21:22:43 +0000442 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000443 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code on
444 * failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100445 *
Hanno Becker68767a62017-10-17 10:13:31 +0100446 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000447 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100448 * on the given context, but that the various parameters are
449 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000450 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100451 *
452 * \warning This function should catch accidental misconfigurations
453 * like swapping of parameters, but it cannot establish full
454 * trust in neither the quality nor the consistency of the key
455 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000456 * <ul><li>Consistency: Imported parameters that are irrelevant
457 * for the implementation might be silently dropped. If dropped,
458 * the current function does not have access to them,
459 * and therefore cannot check them. See mbedtls_rsa_complete().
460 * If you want to check the consistency of the entire
461 * content of an PKCS1-encoded RSA private key, for example, you
462 * should use mbedtls_rsa_validate_params() before setting
463 * up the RSA context.
464 * Additionally, if the implementation performs empirical checks,
465 * these checks substantiate but do not guarantee consistency.</li>
466 * <li>Quality: This function is not expected to perform
467 * extended quality assessments like checking that the prime
468 * factors are safe. Additionally, it is the responsibility of the
469 * user to ensure the trustworthiness of the source of his RSA
470 * parameters, which goes beyond what is effectively checkable
471 * by the library.</li></ul>
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000474
475/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000476 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100477 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000478 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100479 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000480 * \param pub The RSA context holding the public key.
481 * \param prv The RSA context holding the private key.
482 *
483 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
484 * on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100485 */
Hanno Becker98838b02017-10-02 13:16:10 +0100486int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
487 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100488
489/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000490 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000491 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000492 * \param ctx The RSA context.
493 * \param input The input buffer.
494 * \param output The output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000496 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
497 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000499 * \note This function does not handle message padding.
500 *
501 * \note Make sure to set \p input[0] = 0 or ensure that
502 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000503 *
504 * \note The input and output buffers must be large
Rose Zadik042e97f2018-01-26 16:35:10 +0000505 * enough. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000508 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 unsigned char *output );
510
511/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000512 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000514 * \param ctx The RSA context.
515 * \param f_rng The RNG function. Needed for blinding.
516 * \param p_rng The RNG parameter.
517 * \param input The input buffer.
518 * \param output The output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000520 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
521 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 *
523 * \note The input and output buffers must be large
Rose Zadik042e97f2018-01-26 16:35:10 +0000524 * enough. For example, 128 Bytes if RSA-1024 is used.
Hanno Becker88ec2382017-05-03 13:51:16 +0100525 *
Hanno Becker24120612017-10-26 11:53:35 +0100526 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100527 *
528 * \note If blinding is used, both the base of exponentation
Hanno Becker24120612017-10-26 11:53:35 +0100529 * and the exponent are blinded, providing protection
530 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100531 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100532 * \warning It is deprecated and a security risk to not provide
533 * a PRNG here and thereby prevent the use of blinding.
534 * Future versions of the library may enforce the presence
535 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100536 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200539 int (*f_rng)(void *, unsigned char *, size_t),
540 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000541 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000542 unsigned char *output );
543
544/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000545 * \brief This function adds the message padding, then performs an RSA
546 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000548 * It is the generic wrapper for performing a PKCS#1 encryption
549 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000552 * \param ctx The RSA context.
553 * \param f_rng The RNG function. Needed for padding, PKCS#1 v2.1
554 * encoding, and #MBEDTLS_RSA_PRIVATE.
555 * \param p_rng The RNG parameter.
556 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
557 * \param ilen The length of the plaintext.
558 * \param input The buffer holding the data to encrypt.
559 * \param output The buffer used to hold the ciphertext.
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100561 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000562 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
563 * are likely to remove the \p mode argument and have it
564 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100565 *
566 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000567 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
568 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100569 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000570 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
571 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000573 * \note The input and output buffers must be as large as the size
574 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000577 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000578 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000579 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000580 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 unsigned char *output );
582
583/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000584 * \brief This function performs a PKCS#1 v1.5 encryption operation
585 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100586 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000587 * \param ctx The RSA context.
588 * \param f_rng The RNG function. Needed for padding and
589 * #MBEDTLS_RSA_PRIVATE.
590 * \param p_rng The RNG parameter.
591 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
592 * \param ilen The length of the plaintext.
593 * \param input The buffer holding the data to encrypt.
594 * \param output The buffer used to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100595 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100596 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000597 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
598 * are likely to remove the \p mode argument and have it
599 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100600 *
601 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000602 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
603 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100604 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000605 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
606 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100607 *
608 * \note The output buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000609 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100612 int (*f_rng)(void *, unsigned char *, size_t),
613 void *p_rng,
614 int mode, size_t ilen,
615 const unsigned char *input,
616 unsigned char *output );
617
618/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000619 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
620 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100621 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000622 * \param ctx The RSA context.
623 * \param f_rng The RNG function. Needed for padding and PKCS#1 v2.1
624 * encoding and #MBEDTLS_RSA_PRIVATE.
625 * \param p_rng The RNG parameter.
626 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
627 * \param label The buffer holding the custom label to use.
628 * \param label_len The length of the label.
629 * \param ilen The length of the plaintext.
630 * \param input The buffer holding the data to encrypt.
631 * \param output The buffer used to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100632 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100633 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000634 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
635 * are likely to remove the \p mode argument and have it
636 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100637 *
638 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000639 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
640 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100641 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000642 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
643 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100644 *
645 * \note The output buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000646 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100649 int (*f_rng)(void *, unsigned char *, size_t),
650 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100651 int mode,
652 const unsigned char *label, size_t label_len,
653 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100654 const unsigned char *input,
655 unsigned char *output );
656
657/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000658 * \brief This function performs an RSA operation, then removes the
659 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000660 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000661 * It is the generic wrapper for performing a PKCS#1 decryption
662 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000664 * \param ctx The RSA context.
665 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
666 * \param p_rng The RNG parameter.
667 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
668 * \param olen The length of the plaintext.
669 * \param input The buffer holding the encrypted data.
670 * \param output The buffer used to hold the plaintext.
671 * \param output_max_len The maximum length of the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100673 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000674 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
675 * are likely to remove the \p mode argument and have it
676 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100677 *
678 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000679 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
680 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100681 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000682 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
683 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000684 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100685 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000686 * as large as the size \p ctx->len of \p ctx->N (for example,
687 * 128 Bytes if RSA-1024 is used) to be able to hold an
688 * arbitrary decrypted message. If it is not large enough to
689 * hold the decryption of the particular ciphertext provided,
690 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100691 *
692 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000693 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker5121ce52009-01-03 21:22:43 +0000694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200696 int (*f_rng)(void *, unsigned char *, size_t),
697 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000698 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000699 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000700 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000701 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000702
703/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000704 * \brief This function performs a PKCS#1 v1.5 decryption
705 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100706 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000707 * \param ctx The RSA context.
708 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
709 * \param p_rng The RNG parameter.
710 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
711 * \param olen The length of the plaintext.
712 * \param input The buffer holding the encrypted data.
713 * \param output The buffer to hold the plaintext.
714 * \param output_max_len The maximum length of the output buffer.
Paul Bakkerb3869132013-02-28 17:21:01 +0100715 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100716 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000717 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
718 * are likely to remove the \p mode argument and have it
719 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100720 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100721 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000722 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
723 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100724 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000725 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
726 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100727 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100728 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000729 * as large as the size \p ctx->len of \p ctx->N, for example,
730 * 128 Bytes if RSA-1024 is used, to be able to hold an
731 * arbitrary decrypted message. If it is not large enough to
732 * hold the decryption of the particular ciphertext provided,
733 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100734 *
735 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000736 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200739 int (*f_rng)(void *, unsigned char *, size_t),
740 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100741 int mode, size_t *olen,
742 const unsigned char *input,
743 unsigned char *output,
744 size_t output_max_len );
745
746/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000747 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
748 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100749 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000750 * \param ctx The RSA context.
751 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
752 * \param p_rng The RNG parameter.
753 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
754 * \param label The buffer holding the custom label to use.
755 * \param label_len The length of the label.
756 * \param olen The length of the plaintext.
757 * \param input The buffer holding the encrypted data.
758 * \param output The buffer to hold the plaintext.
759 * \param output_max_len The maximum length of the output buffer.
Paul Bakkerb3869132013-02-28 17:21:01 +0100760 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100761 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000762 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
763 * are likely to remove the \p mode argument and have it
764 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100765 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100766 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000767 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
768 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100769 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000770 * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
771 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100772 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100773 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000774 * as large as the size \p ctx->len of \p ctx->N, for
775 * example, 128 Bytes if RSA-1024 is used, to be able to
776 * hold an arbitrary decrypted message. If it is not
777 * large enough to hold the decryption of the particular
778 * ciphertext provided, the function returns
779 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100780 *
Hanno Becker8fd55482017-08-23 14:07:48 +0100781 * \note The input buffer must be as large as the size
Rose Zadik042e97f2018-01-26 16:35:10 +0000782 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200785 int (*f_rng)(void *, unsigned char *, size_t),
786 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100787 int mode,
788 const unsigned char *label, size_t label_len,
789 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100790 const unsigned char *input,
791 unsigned char *output,
792 size_t output_max_len );
793
794/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000795 * \brief This function performs a private RSA operation to sign
796 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000797 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000798 * It is the generic wrapper for performing a PKCS#1
799 * signature using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000800 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000801 * \param ctx The RSA context.
802 * \param f_rng The RNG function. Needed for PKCS#1 v2.1 encoding and for
803 * #MBEDTLS_RSA_PRIVATE.
804 * \param p_rng The RNG parameter.
805 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
806 * \param md_alg The message-digest algorithm used to hash the original data.
807 * Use #MBEDTLS_MD_NONE for signing raw data.
808 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
809 * \param hash The buffer holding the message digest.
810 * \param sig The buffer to hold the ciphertext.
Paul Bakker5121ce52009-01-03 21:22:43 +0000811 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100812 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000813 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
814 * are likely to remove the \p mode argument and have it
815 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000816 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100817 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000818 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
819 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100820 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000821 * \return \c 0 if the signing operation was successful,
822 * or an \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000823 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000824 * \note The \p sig buffer must be as large as the size
825 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000826 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000827 * \note For PKCS#1 v2.1 encoding, see comments on
828 * mbedtls_rsa_rsassa_pss_sign() for details on
829 * \p md_alg and \p hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000830 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000832 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000833 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000834 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000836 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000837 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000838 unsigned char *sig );
839
840/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000841 * \brief This function performs a PKCS#1 v1.5 signature
842 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100843 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000844 * \param ctx The RSA context.
845 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
846 * \param p_rng The RNG parameter.
847 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
848 * \param md_alg The message-digest algorithm used to hash the original data.
849 * Use #MBEDTLS_MD_NONE for signing raw data.
850 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
851 * \param hash The buffer holding the message digest.
852 * \param sig The buffer to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100853 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100854 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000855 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
856 * are likely to remove the \p mode argument and have it
857 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100858 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100859 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000860 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
861 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100862 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000863 * \return \c 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100864 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +0000865 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100866 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000867 * \note The \p sig buffer must be as large as the size
868 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100869 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200871 int (*f_rng)(void *, unsigned char *, size_t),
872 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100873 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100875 unsigned int hashlen,
876 const unsigned char *hash,
877 unsigned char *sig );
878
879/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000880 * \brief This function performs a PKCS#1 v2.1 PSS signature
881 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100882 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000883 * \param ctx The RSA context.
884 * \param f_rng The RNG function. Needed for PKCS#1 v2.1 encoding and for
885 * #MBEDTLS_RSA_PRIVATE.
886 * \param p_rng The RNG parameter.
887 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
888 * \param md_alg The message-digest algorithm used to hash the original data.
889 * Use #MBEDTLS_MD_NONE for signing raw data.
890 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
891 * \param hash The buffer holding the message digest.
892 * \param sig The buffer to hold the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100893 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100894 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000895 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
896 * are likely to remove the \p mode argument and have it
897 * implicitly set to #MBEDTLS_RSA_PRIVATE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100898 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100899 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000900 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
901 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Paul Bakkerb3869132013-02-28 17:21:01 +0100902 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000903 * \return \c 0 if the signing operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100904 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +0000905 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100906 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000907 * \note The \p sig buffer must be as large as the size
908 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100909 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000910 * \note The \p hash_id in the RSA context is the one used for the
911 * encoding. \p md_alg in the function call is the type of hash
912 * that is encoded. According to <em>RFC-3447: Public-Key
913 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
914 * Specifications</em> it is advised to keep both hashes the
915 * same.
Paul Bakkerb3869132013-02-28 17:21:01 +0100916 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100918 int (*f_rng)(void *, unsigned char *, size_t),
919 void *p_rng,
920 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100922 unsigned int hashlen,
923 const unsigned char *hash,
924 unsigned char *sig );
925
926/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000927 * \brief This function performs a public RSA operation and checks
928 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000930 * This is the generic wrapper for performing a PKCS#1
931 * verification using the mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000932 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000933 * \param ctx The RSA public key context.
934 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
935 * \param p_rng The RNG parameter.
936 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
937 * \param md_alg The message-digest algorithm used to hash the original data.
938 * Use #MBEDTLS_MD_NONE for signing raw data.
939 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
940 * \param hash The buffer holding the message digest.
941 * \param sig The buffer holding the ciphertext.
Paul Bakker5121ce52009-01-03 21:22:43 +0000942 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100943 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000944 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
945 * are likely to remove the \p mode argument and have it
946 * set to #MBEDTLS_RSA_PUBLIC.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000947 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100948 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000949 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
950 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100951 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000952 * \return \c 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100953 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +0000954 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000956 * \note The \p sig buffer must be as large as the size
957 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000958 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000959 * \note For PKCS#1 v2.1 encoding, see comments on
960 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
961 * \p hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200964 int (*f_rng)(void *, unsigned char *, size_t),
965 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000966 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000968 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000969 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200970 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000971
972/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000973 * \brief This function performs a PKCS#1 v1.5 verification
974 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +0100975 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000976 * \param ctx The RSA public key context.
977 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
978 * \param p_rng The RNG parameter.
979 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
980 * \param md_alg The message-digest algorithm used to hash the original data.
981 * Use #MBEDTLS_MD_NONE for signing raw data.
982 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
983 * \param hash The buffer holding the message digest.
984 * \param sig The buffer holding the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +0100985 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100986 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000987 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
988 * are likely to remove the \p mode argument and have it
989 * set to #MBEDTLS_RSA_PUBLIC.
Paul Bakkerb3869132013-02-28 17:21:01 +0100990 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100991 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +0000992 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
993 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100994 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000995 * \return \c 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +0100996 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +0000997 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100998 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000999 * \note The \p sig buffer must be as large as the size
1000 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001001 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001003 int (*f_rng)(void *, unsigned char *, size_t),
1004 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001005 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001007 unsigned int hashlen,
1008 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001009 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001010
1011/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001012 * \brief This function performs a PKCS#1 v2.1 PSS verification
1013 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001014 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001015 * The hash function for the MGF mask generating function
1016 * is that specified in the RSA context.
Paul Bakkerb3869132013-02-28 17:21:01 +01001017 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001018 * \param ctx The RSA public key context.
1019 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
1020 * \param p_rng The RNG parameter.
1021 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1022 * \param md_alg The message-digest algorithm used to hash the original data.
1023 * Use #MBEDTLS_MD_NONE for signing raw data.
1024 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
1025 * \param hash The buffer holding the message digest.
1026 * \param sig The buffer holding the ciphertext.
Paul Bakkerb3869132013-02-28 17:21:01 +01001027 *
Hanno Becker3cdc7112017-10-05 10:09:31 +01001028 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +00001029 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1030 * are likely to remove the \p mode argument and have it
1031 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Paul Bakkerb3869132013-02-28 17:21:01 +01001032 *
Hanno Becker3cdc7112017-10-05 10:09:31 +01001033 * \note Alternative implementations of RSA need not support
Rose Zadik042e97f2018-01-26 16:35:10 +00001034 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
1035 * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
Hanno Becker3cdc7112017-10-05 10:09:31 +01001036 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001037 * \return \c 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001038 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +00001039 * on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001040 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001041 * \note The \p sig buffer must be as large as the size
1042 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001043 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001044 * \note The \p hash_id in the RSA context is the one used for the
1045 * verification. \p md_alg in the function call is the type of
1046 * hash that is verified. According to <em>RFC-3447: Public-Key
1047 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1048 * Specifications</em> it is advised to keep both hashes the
1049 * same. If \p hash_id in the RSA context is unset,
1050 * the \p md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +01001051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001053 int (*f_rng)(void *, unsigned char *, size_t),
1054 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001055 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001057 unsigned int hashlen,
1058 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001059 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001060
1061/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001062 * \brief This function performs a PKCS#1 v2.1 PSS verification
1063 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001064 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001065 * The hash function for the MGF mask generating function
1066 * is that specified in \p mgf1_hash_id.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001067 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001068 * \param ctx The RSA public key context.
1069 * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
1070 * \param p_rng The RNG parameter.
1071 * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1072 * \param md_alg The message-digest algorithm used to hash the original data.
1073 * Use #MBEDTLS_MD_NONE for signing raw data.
1074 * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
1075 * \param hash The buffer holding the message digest.
1076 * \param mgf1_hash_id The message digest used for mask generation.
1077 * \param expected_salt_len The length of the salt used in padding. Use
1078 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1079 * \param sig The buffer holding the ciphertext.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001080 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001081 * \return \c 0 if the verify operation was successful,
Hanno Becker4b2f6912017-09-29 13:34:55 +01001082 * or an \c MBEDTLS_ERR_RSA_XXX error code
Rose Zadik042e97f2018-01-26 16:35:10 +00001083 * on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001084 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001085 * \note The \p sig buffer must be as large as the size
1086 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001087 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001088 * \note The \p hash_id in the RSA context is ignored.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001091 int (*f_rng)(void *, unsigned char *, size_t),
1092 void *p_rng,
1093 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001095 unsigned int hashlen,
1096 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001098 int expected_salt_len,
1099 const unsigned char *sig );
1100
1101/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001102 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001103 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001104 * \param dst The destination context.
1105 * \param src The source context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001106 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001107 * \return \c 0 on success,
1108 * #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001111
1112/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001113 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001114 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001115 * \param ctx The RSA Context to free.
Paul Bakker5121ce52009-01-03 21:22:43 +00001116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
1119/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001120 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001121 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001122 * \return \c 0 on success, or \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001125
1126#ifdef __cplusplus
1127}
1128#endif
1129
Paul Bakker5121ce52009-01-03 21:22:43 +00001130#endif /* rsa.h */