blob: 8e073cd64bb761ec0cb9dc36a4a38ec441e76de5 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Rose Zadik21e29262018-04-17 14:08:56 +01004 * \brief This file provides an API for the RSA public-key cryptosystem.
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
Rose Zadike8b5b992018-03-27 12:19:47 +01006 * The RSA public-key cryptosystem is defined in <em>Public-Key
7 * Cryptography Standards (PKCS) #1 v1.5: RSA Encryption</em>
Darryl Green11999bb2018-03-13 15:22:58 +00008 * and <em>Public-Key Cryptography Standards (PKCS) #1 v2.1:
Rose Zadike8b5b992018-03-27 12:19:47 +01009 * RSA Cryptography Specifications</em>.
Rose Zadik042e97f2018-01-26 16:35:10 +000010 *
Darryl Greena40a1012018-01-05 15:33:17 +000011 */
12/*
Rose Zadik042e97f2018-01-26 16:35:10 +000013 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020014 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000027 *
Rose Zadik042e97f2018-01-26 16:35:10 +000028 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#ifndef MBEDTLS_RSA_H
31#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Amero203fdf72019-07-04 20:01:14 +010034#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020037#endif
Paul Bakkered27a042013-04-18 22:46:23 +020038
Jaeden Amero203fdf72019-07-04 20:01:14 +010039#include "mbedtls/bignum.h"
40#include "mbedtls/md.h"
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +010041#include "mbedtls/platform_util.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_THREADING_C)
Jaeden Amero203fdf72019-07-04 20:01:14 +010044#include "mbedtls/threading.h"
Paul Bakkerc9965dc2013-09-29 14:58:17 +020045#endif
46
Paul Bakker13e2dfe2009-07-28 07:18:38 +000047/*
48 * RSA Error codes
49 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
51#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
52#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Rose Zadik042e97f2018-01-26 16:35:10 +000053#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 +020054#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
55#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
56#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
57#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
58#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030059
60/* MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is deprecated and should not be used.
61 */
Rose Zadik042e97f2018-01-26 16:35:10 +000062#define MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION -0x4500 /**< The implementation does not offer the requested operation, for example, because of security violations or lack of functionality. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030063
64/* MBEDTLS_ERR_RSA_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010065#define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580 /**< RSA hardware accelerator failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000066
67/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020068 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000069 */
Rose Zadik042e97f2018-01-26 16:35:10 +000070#define MBEDTLS_RSA_PUBLIC 0 /**< Request private key operation. */
71#define MBEDTLS_RSA_PRIVATE 1 /**< Request public key operation. */
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Rose Zadike8b5b992018-03-27 12:19:47 +010073#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
74#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
Paul Bakker5121ce52009-01-03 21:22:43 +000075
Rose Zadik042e97f2018-01-26 16:35:10 +000076#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
77#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
Paul Bakker5121ce52009-01-03 21:22:43 +000078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020080
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020081/*
82 * The above constants may be used even if the RSA module is compile out,
83 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
84 */
Hanno Beckerd22b78b2017-10-12 11:42:17 +010085
Paul Bakker407a0da2013-06-27 14:29:21 +020086#ifdef __cplusplus
87extern "C" {
88#endif
89
Ron Eldor4e6d55d2018-02-07 16:36:15 +020090#if !defined(MBEDTLS_RSA_ALT)
91// Regular implementation
92//
93
Paul Bakker5121ce52009-01-03 21:22:43 +000094/**
Rose Zadik042e97f2018-01-26 16:35:10 +000095 * \brief The RSA context structure.
Hanno Becker5063cd22017-09-29 11:49:12 +010096 *
97 * \note Direct manipulation of the members of this structure
Rose Zadik042e97f2018-01-26 16:35:10 +000098 * is deprecated. All manipulation should instead be done through
99 * the public interface functions.
Paul Bakker5121ce52009-01-03 21:22:43 +0000100 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200101typedef struct mbedtls_rsa_context
Paul Bakker5121ce52009-01-03 21:22:43 +0000102{
Rose Zadik042e97f2018-01-26 16:35:10 +0000103 int ver; /*!< Always 0.*/
104 size_t len; /*!< The size of \p N in Bytes. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Rose Zadike8b5b992018-03-27 12:19:47 +0100106 mbedtls_mpi N; /*!< The public modulus. */
107 mbedtls_mpi E; /*!< The public exponent. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
Rose Zadike8b5b992018-03-27 12:19:47 +0100109 mbedtls_mpi D; /*!< The private exponent. */
110 mbedtls_mpi P; /*!< The first prime factor. */
111 mbedtls_mpi Q; /*!< The second prime factor. */
Hanno Becker1a59e792017-08-23 07:41:10 +0100112
Rose Zadikf2ec2882018-04-17 10:27:25 +0100113 mbedtls_mpi DP; /*!< <code>D % (P - 1)</code>. */
114 mbedtls_mpi DQ; /*!< <code>D % (Q - 1)</code>. */
115 mbedtls_mpi QP; /*!< <code>1 / (Q % P)</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
Rose Zadikf2ec2882018-04-17 10:27:25 +0100117 mbedtls_mpi RN; /*!< cached <code>R^2 mod N</code>. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
Rose Zadikf2ec2882018-04-17 10:27:25 +0100119 mbedtls_mpi RP; /*!< cached <code>R^2 mod P</code>. */
120 mbedtls_mpi RQ; /*!< cached <code>R^2 mod Q</code>. */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200121
Rose Zadike8b5b992018-03-27 12:19:47 +0100122 mbedtls_mpi Vi; /*!< The cached blinding value. */
123 mbedtls_mpi Vf; /*!< The cached un-blinding value. */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000124
Rose Zadik042e97f2018-01-26 16:35:10 +0000125 int padding; /*!< Selects padding mode:
126 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
127 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
128 int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
129 as specified in md.h for use in the MGF
130 mask generating function used in the
131 EME-OAEP and EMSA-PSS encodings. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#if defined(MBEDTLS_THREADING_C)
Rose Zadik042e97f2018-01-26 16:35:10 +0000133 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200134#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000135}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200138#else /* MBEDTLS_RSA_ALT */
139#include "rsa_alt.h"
140#endif /* MBEDTLS_RSA_ALT */
141
Paul Bakker5121ce52009-01-03 21:22:43 +0000142/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000143 * \brief This function initializes an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000145 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000146 * encryption scheme and the RSASSA-PSS signature scheme.
147 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000148 * \note The \p hash_id parameter is ignored when using
149 * #MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200150 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000151 * \note The choice of padding mode is strictly enforced for private key
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200152 * operations, since there might be security concerns in
Rose Zadik042e97f2018-01-26 16:35:10 +0000153 * mixing padding modes. For public key operations it is
Antonin Décimo36e89b52019-01-23 15:24:37 +0100154 * a default value, which can be overridden by calling specific
Rose Zadik042e97f2018-01-26 16:35:10 +0000155 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200156 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000157 * \note The hash selected in \p hash_id is always used for OEAP
158 * encryption. For PSS signatures, it is always used for
Antonin Décimo36e89b52019-01-23 15:24:37 +0100159 * making signatures, but can be overridden for verifying them.
160 * If set to #MBEDTLS_MD_NONE, it is always overridden.
Rose Zadike8b5b992018-03-27 12:19:47 +0100161 *
Hanno Becker9a467772018-12-13 09:54:59 +0000162 * \param ctx The RSA context to initialize. This must not be \c NULL.
163 * \param padding The padding mode to use. This must be either
164 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Hanno Becker385ce912018-12-13 18:33:12 +0000165 * \param hash_id The hash identifier of ::mbedtls_md_type_t type, if
Hanno Becker9a467772018-12-13 09:54:59 +0000166 * \p padding is #MBEDTLS_RSA_PKCS_V21. It is unused
167 * otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100169MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100171 int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000172 int hash_id );
Paul Bakker5121ce52009-01-03 21:22:43 +0000173
174/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000175 * \brief This function imports a set of core parameters into an
176 * RSA context.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100177 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100178 * \note This function can be called multiple times for successive
Rose Zadik042e97f2018-01-26 16:35:10 +0000179 * imports, if the parameters are not simultaneously present.
180 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100181 * Any sequence of calls to this function should be followed
Rose Zadik042e97f2018-01-26 16:35:10 +0000182 * by a call to mbedtls_rsa_complete(), which checks and
183 * completes the provided information to a ready-for-use
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100184 * public or private RSA key.
185 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000186 * \note See mbedtls_rsa_complete() for more information on which
187 * parameters are necessary to set up a private or public
188 * RSA key.
Hanno Becker33195552017-10-25 17:04:10 +0100189 *
Hanno Becker5178dca2017-10-03 14:29:37 +0100190 * \note The imported parameters are copied and need not be preserved
191 * for the lifetime of the RSA context being set up.
192 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100193 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000194 * \param N The RSA modulus. This may be \c NULL.
195 * \param P The first prime factor of \p N. This may be \c NULL.
196 * \param Q The second prime factor of \p N. This may be \c NULL.
197 * \param D The private exponent. This may be \c NULL.
198 * \param E The public exponent. This may be \c NULL.
Rose Zadike8b5b992018-03-27 12:19:47 +0100199 *
200 * \return \c 0 on success.
201 * \return A non-zero error code on failure.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100202 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100203MBEDTLS_DEPRECATED
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100204int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
205 const mbedtls_mpi *N,
206 const mbedtls_mpi *P, const mbedtls_mpi *Q,
207 const mbedtls_mpi *D, const mbedtls_mpi *E );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100208
209/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000210 * \brief This function imports core RSA parameters, in raw big-endian
211 * binary format, into an RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100213 * \note This function can be called multiple times for successive
214 * imports, if the parameters are not simultaneously present.
215 *
216 * Any sequence of calls to this function should be followed
217 * by a call to mbedtls_rsa_complete(), which checks and
218 * completes the provided information to a ready-for-use
219 * public or private RSA key.
220 *
221 * \note See mbedtls_rsa_complete() for more information on which
222 * parameters are necessary to set up a private or public
223 * RSA key.
224 *
225 * \note The imported parameters are copied and need not be preserved
226 * for the lifetime of the RSA context being set up.
227 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000228 * \param ctx The initialized RSA context to store the parameters in.
Hanno Becker9a467772018-12-13 09:54:59 +0000229 * \param N The RSA modulus. This may be \c NULL.
230 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
231 * \param P The first prime factor of \p N. This may be \c NULL.
232 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
233 * \param Q The second prime factor of \p N. This may be \c NULL.
234 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
235 * \param D The private exponent. This may be \c NULL.
236 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
237 * \param E The public exponent. This may be \c NULL.
238 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100240 * \return \c 0 on success.
241 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100242 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100243MBEDTLS_DEPRECATED
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100244int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100245 unsigned char const *N, size_t N_len,
246 unsigned char const *P, size_t P_len,
247 unsigned char const *Q, size_t Q_len,
248 unsigned char const *D, size_t D_len,
249 unsigned char const *E, size_t E_len );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100250
251/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000252 * \brief This function completes an RSA context from
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100253 * a set of imported core parameters.
254 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000255 * To setup an RSA public key, precisely \p N and \p E
256 * must have been imported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100257 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000258 * To setup an RSA private key, sufficient information must
259 * be present for the other parameters to be derivable.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100260 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000261 * The default implementation supports the following:
262 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
263 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
264 * Alternative implementations need not support these.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100265 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000266 * If this function runs successfully, it guarantees that
267 * the RSA context can be used for RSA operations without
268 * the risk of failure or crash.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100269 *
Hanno Becker1e801f52017-10-10 16:44:47 +0100270 * \warning This function need not perform consistency checks
Rose Zadik042e97f2018-01-26 16:35:10 +0000271 * for the imported parameters. In particular, parameters that
272 * are not needed by the implementation might be silently
273 * discarded and left unchecked. To check the consistency
274 * of the key material, see mbedtls_rsa_check_privkey().
Hanno Becker43a08d02017-10-02 13:16:35 +0100275 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100276 * \param ctx The initialized RSA context holding imported parameters.
277 *
278 * \return \c 0 on success.
279 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
280 * failed.
281 *
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100282 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100283MBEDTLS_DEPRECATED
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100284int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100285
286/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000287 * \brief This function exports the core parameters of an RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100288 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000289 * If this function runs successfully, the non-NULL buffers
290 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
291 * written, with additional unused space filled leading by
292 * zero Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100293 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000294 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300295 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000296 * <li>An alternative RSA implementation is in use, which
297 * stores the key externally, and either cannot or should
298 * not export it into RAM.</li>
299 * <li>A SW or HW implementation might not support a certain
300 * deduction. For example, \p P, \p Q from \p N, \p D,
301 * and \p E if the former are not part of the
302 * implementation.</li></ul>
Hanno Becker91c194d2017-09-29 12:50:12 +0100303 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000304 * If the function fails due to an unsupported operation,
305 * the RSA context stays intact and remains usable.
306 *
307 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000308 * \param N The MPI to hold the RSA modulus.
309 * This may be \c NULL if this field need not be exported.
310 * \param P The MPI to hold the first prime factor of \p N.
311 * This may be \c NULL if this field need not be exported.
312 * \param Q The MPI to hold the second prime factor of \p N.
313 * This may be \c NULL if this field need not be exported.
314 * \param D The MPI to hold the private exponent.
315 * This may be \c NULL if this field need not be exported.
316 * \param E The MPI to hold the public exponent.
317 * This may be \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000318 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100319 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300320 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000321 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100322 * functionality or because of security policies.
323 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100324 *
325 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100326MBEDTLS_DEPRECATED
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100327int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
328 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
329 mbedtls_mpi *D, mbedtls_mpi *E );
330
331/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000332 * \brief This function exports core parameters of an RSA key
333 * in raw big-endian binary format.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100334 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000335 * If this function runs successfully, the non-NULL buffers
336 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
337 * written, with additional unused space filled leading by
338 * zero Bytes.
339 *
340 * Possible reasons for returning
Ron Eldor9924bdc2018-10-04 10:59:13 +0300341 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
Rose Zadik042e97f2018-01-26 16:35:10 +0000342 * <li>An alternative RSA implementation is in use, which
343 * stores the key externally, and either cannot or should
344 * not export it into RAM.</li>
345 * <li>A SW or HW implementation might not support a certain
346 * deduction. For example, \p P, \p Q from \p N, \p D,
347 * and \p E if the former are not part of the
348 * implementation.</li></ul>
349 * If the function fails due to an unsupported operation,
350 * the RSA context stays intact and remains usable.
351 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100352 * \note The length parameters are ignored if the corresponding
Rose Zadike8b5b992018-03-27 12:19:47 +0100353 * buffer pointers are NULL.
354 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000355 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000356 * \param N The Byte array to store the RSA modulus,
357 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000358 * \param N_len The size of the buffer for the modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000359 * \param P The Byte array to hold the first prime factor of \p N,
360 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000361 * \param P_len The size of the buffer for the first prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000362 * \param Q The Byte array to hold the second prime factor of \p N,
363 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000364 * \param Q_len The size of the buffer for the second prime factor.
Hanno Becker9a467772018-12-13 09:54:59 +0000365 * \param D The Byte array to hold the private exponent,
366 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000367 * \param D_len The size of the buffer for the private exponent.
Hanno Becker9a467772018-12-13 09:54:59 +0000368 * \param E The Byte array to hold the public exponent,
369 * or \c NULL if this field need not be exported.
Rose Zadik042e97f2018-01-26 16:35:10 +0000370 * \param E_len The size of the buffer for the public exponent.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100371 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100372 * \return \c 0 on success.
Ron Eldor9924bdc2018-10-04 10:59:13 +0300373 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
Rose Zadik042e97f2018-01-26 16:35:10 +0000374 * requested parameters cannot be done due to missing
Rose Zadike8b5b992018-03-27 12:19:47 +0100375 * functionality or because of security policies.
376 * \return A non-zero return code on any other failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100377 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100378MBEDTLS_DEPRECATED
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100379int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
380 unsigned char *N, size_t N_len,
381 unsigned char *P, size_t P_len,
382 unsigned char *Q, size_t Q_len,
383 unsigned char *D, size_t D_len,
384 unsigned char *E, size_t E_len );
385
386/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000387 * \brief This function exports CRT parameters of a private RSA key.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100388 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100389 * \note Alternative RSA implementations not using CRT-parameters
390 * internally can implement this function based on
391 * mbedtls_rsa_deduce_opt().
392 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000393 * \param ctx The initialized RSA context.
Hanno Becker9a467772018-12-13 09:54:59 +0000394 * \param DP The MPI to hold \c D modulo `P-1`,
395 * or \c NULL if it need not be exported.
396 * \param DQ The MPI to hold \c D modulo `Q-1`,
397 * or \c NULL if it need not be exported.
398 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
399 * or \c NULL if it need not be exported.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100400 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100401 * \return \c 0 on success.
402 * \return A non-zero error code on failure.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100403 *
404 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100405MBEDTLS_DEPRECATED
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100406int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
407 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
408
Paul Bakker5121ce52009-01-03 21:22:43 +0000409/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000410 * \brief This function sets padding for an already initialized RSA
411 * context. See mbedtls_rsa_init() for details.
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 *
Hanno Becker9a467772018-12-13 09:54:59 +0000413 * \param ctx The initialized RSA context to be configured.
414 * \param padding The padding mode to use. This must be either
415 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
Rose Zadik042e97f2018-01-26 16:35:10 +0000416 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
Paul Bakker40e46942009-01-03 21:51:57 +0000417 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100418MBEDTLS_DEPRECATED
Hanno Becker8fd55482017-08-23 14:07:48 +0100419void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
Hanno Becker9a467772018-12-13 09:54:59 +0000420 int hash_id );
Paul Bakker21eb2802010-08-16 11:10:02 +0000421
Paul Bakkera3d195c2011-11-27 21:07:34 +0000422/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000423 * \brief This function retrieves the length of RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100424 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000425 * \param ctx The initialized RSA context.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100426 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000427 * \return The length of the RSA modulus in Bytes.
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100428 *
429 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100430MBEDTLS_DEPRECATED
Hanno Beckercbb59bc2017-08-23 14:11:08 +0100431size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
432
433/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000434 * \brief This function generates an RSA keypair.
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000436 * \note mbedtls_rsa_init() must be called before this function,
437 * to set up the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 *
Hanno Becker9a467772018-12-13 09:54:59 +0000439 * \param ctx The initialized RSA context used to hold the key.
440 * \param f_rng The RNG function to be used for key generation.
441 * This must not be \c NULL.
442 * \param p_rng The RNG context to be passed to \p f_rng.
443 * This may be \c NULL if \p f_rng doesn't need a context.
Rose Zadike8b5b992018-03-27 12:19:47 +0100444 * \param nbits The size of the public key in bits.
Hanno Becker9a467772018-12-13 09:54:59 +0000445 * \param exponent The public exponent to use. For example, \c 65537.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000446 * This must be odd and greater than \c 1.
Rose Zadike8b5b992018-03-27 12:19:47 +0100447 *
448 * \return \c 0 on success.
449 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000450 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100451MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Hanno Becker8fd55482017-08-23 14:07:48 +0100453 int (*f_rng)(void *, unsigned char *, size_t),
454 void *p_rng,
455 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000456
457/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000458 * \brief This function checks if a context contains at least an RSA
459 * public key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000461 * If the function runs successfully, it is guaranteed that
462 * enough information is present to perform an RSA public key
463 * operation using mbedtls_rsa_public().
Paul Bakker5121ce52009-01-03 21:22:43 +0000464 *
Hanno Becker9a467772018-12-13 09:54:59 +0000465 * \param ctx The initialized RSA context to check.
Rose Zadik042e97f2018-01-26 16:35:10 +0000466 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100467 * \return \c 0 on success.
468 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Hanno Becker43a08d02017-10-02 13:16:35 +0100469 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100471MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
474/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000475 * \brief This function checks if a context contains an RSA private key
Hanno Becker1e801f52017-10-10 16:44:47 +0100476 * and perform basic consistency checks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 *
Hanno Becker68767a62017-10-17 10:13:31 +0100478 * \note The consistency checks performed by this function not only
Rose Zadik042e97f2018-01-26 16:35:10 +0000479 * ensure that mbedtls_rsa_private() can be called successfully
Hanno Becker68767a62017-10-17 10:13:31 +0100480 * on the given context, but that the various parameters are
481 * mutually consistent with high probability, in the sense that
Rose Zadik042e97f2018-01-26 16:35:10 +0000482 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
Hanno Becker1e801f52017-10-10 16:44:47 +0100483 *
484 * \warning This function should catch accidental misconfigurations
485 * like swapping of parameters, but it cannot establish full
486 * trust in neither the quality nor the consistency of the key
487 * material that was used to setup the given RSA context:
Rose Zadik042e97f2018-01-26 16:35:10 +0000488 * <ul><li>Consistency: Imported parameters that are irrelevant
489 * for the implementation might be silently dropped. If dropped,
490 * the current function does not have access to them,
491 * and therefore cannot check them. See mbedtls_rsa_complete().
492 * If you want to check the consistency of the entire
493 * content of an PKCS1-encoded RSA private key, for example, you
494 * should use mbedtls_rsa_validate_params() before setting
495 * up the RSA context.
496 * Additionally, if the implementation performs empirical checks,
497 * these checks substantiate but do not guarantee consistency.</li>
498 * <li>Quality: This function is not expected to perform
499 * extended quality assessments like checking that the prime
500 * factors are safe. Additionally, it is the responsibility of the
501 * user to ensure the trustworthiness of the source of his RSA
502 * parameters, which goes beyond what is effectively checkable
503 * by the library.</li></ul>
Rose Zadike8b5b992018-03-27 12:19:47 +0100504 *
Hanno Becker9a467772018-12-13 09:54:59 +0000505 * \param ctx The initialized RSA context to check.
Rose Zadike8b5b992018-03-27 12:19:47 +0100506 *
507 * \return \c 0 on success.
508 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100510MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
513/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000514 * \brief This function checks a public-private RSA key pair.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100515 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000516 * It checks each of the contexts, and makes sure they match.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100517 *
Hanno Becker9a467772018-12-13 09:54:59 +0000518 * \param pub The initialized RSA context holding the public key.
519 * \param prv The initialized RSA context holding the private key.
Rose Zadik042e97f2018-01-26 16:35:10 +0000520 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100521 * \return \c 0 on success.
522 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100523 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100524MBEDTLS_DEPRECATED
Hanno Becker98838b02017-10-02 13:16:10 +0100525int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
526 const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100527
528/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000529 * \brief This function performs an RSA public key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000530 *
Hanno Becker9a467772018-12-13 09:54:59 +0000531 * \param ctx The initialized RSA context to use.
532 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000533 * of length \c ctx->len Bytes. For example, \c 256 Bytes
534 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000535 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000536 * of length \c ctx->len Bytes. For example, \c 256 Bytes
537 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000538 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000539 * \note This function does not handle message padding.
540 *
541 * \note Make sure to set \p input[0] = 0 or ensure that
542 * input is smaller than \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000543 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100544 * \return \c 0 on success.
545 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100547MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000549 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 unsigned char *output );
551
552/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000553 * \brief This function performs an RSA private key operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000554 *
Hanno Becker24120612017-10-26 11:53:35 +0100555 * \note Blinding is used if and only if a PRNG is provided.
Hanno Becker88ec2382017-05-03 13:51:16 +0100556 *
557 * \note If blinding is used, both the base of exponentation
Hanno Becker24120612017-10-26 11:53:35 +0100558 * and the exponent are blinded, providing protection
559 * against some side-channel attacks.
Hanno Becker88ec2382017-05-03 13:51:16 +0100560 *
Hanno Becker4e1be392017-10-02 15:56:48 +0100561 * \warning It is deprecated and a security risk to not provide
562 * a PRNG here and thereby prevent the use of blinding.
563 * Future versions of the library may enforce the presence
564 * of a PRNG.
Hanno Becker88ec2382017-05-03 13:51:16 +0100565 *
Hanno Becker9a467772018-12-13 09:54:59 +0000566 * \param ctx The initialized RSA context to use.
567 * \param f_rng The RNG function, used for blinding. It is discouraged
568 * and deprecated to pass \c NULL here, in which case
569 * blinding will be omitted.
570 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
571 * if \p f_rng is \c NULL or if \p f_rng doesn't need a context.
572 * \param input The input buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000573 * of length \c ctx->len Bytes. For example, \c 256 Bytes
574 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000575 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000576 * of length \c ctx->len Bytes. For example, \c 256 Bytes
577 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100578 *
579 * \return \c 0 on success.
580 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
581 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000582 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100583MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200585 int (*f_rng)(void *, unsigned char *, size_t),
586 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000587 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 unsigned char *output );
589
590/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000591 * \brief This function adds the message padding, then performs an RSA
592 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000594 * It is the generic wrapper for performing a PKCS#1 encryption
595 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000596 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100597 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000598 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
599 * are likely to remove the \p mode argument and have it
600 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100601 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100602 * \note Alternative implementations of RSA need not support
603 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300604 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100605 *
Hanno Becker9a467772018-12-13 09:54:59 +0000606 * \param ctx The initialized RSA context to use.
Hanno Becker974ca0d2018-12-18 18:03:24 +0000607 * \param f_rng The RNG to use. It is mandatory for PKCS#1 v2.1 padding
608 * encoding, and for PKCS#1 v1.5 padding encoding when used
609 * with \p mode set to #MBEDTLS_RSA_PUBLIC. For PKCS#1 v1.5
610 * padding encoding and \p mode set to #MBEDTLS_RSA_PRIVATE,
611 * it is used for blinding and should be provided in this
612 * case; see mbedtls_rsa_private() for more.
Hanno Becker9a467772018-12-13 09:54:59 +0000613 * \param p_rng The RNG context to be passed to \p f_rng. May be
614 * \c NULL if \p f_rng is \c NULL or if \p f_rng doesn't
615 * need a context argument.
616 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000617 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000618 * \param ilen The length of the plaintext in Bytes.
619 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000620 * buffer of size \p ilen Bytes. It may be \c NULL if
621 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000622 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000623 * of length \c ctx->len Bytes. For example, \c 256 Bytes
624 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100625 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100626 * \return \c 0 on success.
627 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000628 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100629MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000631 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000632 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000633 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000634 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 unsigned char *output );
636
637/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000638 * \brief This function performs a PKCS#1 v1.5 encryption operation
639 * (RSAES-PKCS1-v1_5-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100640 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100641 * \deprecated It is deprecated and discouraged to call this function
Rose Zadik042e97f2018-01-26 16:35:10 +0000642 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
643 * are likely to remove the \p mode argument and have it
644 * implicitly set to #MBEDTLS_RSA_PUBLIC.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100645 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100646 * \note Alternative implementations of RSA need not support
647 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300648 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100649 *
Hanno Becker9a467772018-12-13 09:54:59 +0000650 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000651 * \param f_rng The RNG function to use. It is needed for padding generation
652 * if \p mode is #MBEDTLS_RSA_PUBLIC. If \p mode is
653 * #MBEDTLS_RSA_PRIVATE (discouraged), it is used for
654 * blinding and should be provided; see mbedtls_rsa_private().
Hanno Becker9a467772018-12-13 09:54:59 +0000655 * \param p_rng The RNG context to be passed to \p f_rng. This may
656 * be \c NULL if \p f_rng is \c NULL or if \p f_rng
657 * doesn't need a context argument.
658 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000659 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000660 * \param ilen The length of the plaintext in Bytes.
661 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000662 * buffer of size \p ilen Bytes. It may be \c NULL if
663 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000664 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000665 * of length \c ctx->len Bytes. For example, \c 256 Bytes
666 * for an 2048-bit RSA modulus.
Hanno Becker3cdc7112017-10-05 10:09:31 +0100667 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100668 * \return \c 0 on success.
669 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100670 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100671MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100673 int (*f_rng)(void *, unsigned char *, size_t),
674 void *p_rng,
675 int mode, size_t ilen,
676 const unsigned char *input,
677 unsigned char *output );
678
679/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000680 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
681 * operation (RSAES-OAEP-ENCRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100682 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100683 * \note The output buffer must be as large as the size
684 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
685 *
686 * \deprecated It is deprecated and discouraged to call this function
687 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
688 * are likely to remove the \p mode argument and have it
689 * implicitly set to #MBEDTLS_RSA_PUBLIC.
690 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100691 * \note Alternative implementations of RSA need not support
692 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300693 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100694 *
Hanno Becker9a467772018-12-13 09:54:59 +0000695 * \param ctx The initnialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000696 * \param f_rng The RNG function to use. This is needed for padding
697 * generation and must be provided.
Hanno Becker9a467772018-12-13 09:54:59 +0000698 * \param p_rng The RNG context to be passed to \p f_rng. This may
Hanno Beckera9020f22018-12-18 14:45:45 +0000699 * be \c NULL if \p f_rng doesn't need a context argument.
Hanno Becker9a467772018-12-13 09:54:59 +0000700 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000701 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000702 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000703 * This must be a readable buffer of length \p label_len
704 * Bytes. It may be \c NULL if \p label_len is \c 0.
705 * \param label_len The length of the label in Bytes.
706 * \param ilen The length of the plaintext buffer \p input in Bytes.
707 * \param input The input data to encrypt. This must be a readable
Jaeden Amerofb236732019-02-08 13:11:59 +0000708 * buffer of size \p ilen Bytes. It may be \c NULL if
709 * `ilen == 0`.
Hanno Becker9a467772018-12-13 09:54:59 +0000710 * \param output The output buffer. This must be a writable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000711 * of length \c ctx->len Bytes. For example, \c 256 Bytes
712 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100713 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100714 * \return \c 0 on success.
715 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100716 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100717MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100719 int (*f_rng)(void *, unsigned char *, size_t),
720 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100721 int mode,
722 const unsigned char *label, size_t label_len,
723 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100724 const unsigned char *input,
725 unsigned char *output );
726
727/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000728 * \brief This function performs an RSA operation, then removes the
729 * message padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000730 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000731 * It is the generic wrapper for performing a PKCS#1 decryption
732 * operation using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100734 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000735 * as large as the size \p ctx->len of \p ctx->N (for example,
736 * 128 Bytes if RSA-1024 is used) to be able to hold an
737 * arbitrary decrypted message. If it is not large enough to
738 * hold the decryption of the particular ciphertext provided,
739 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100740 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100741 * \deprecated It is deprecated and discouraged to call this function
742 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
743 * are likely to remove the \p mode argument and have it
744 * implicitly set to #MBEDTLS_RSA_PRIVATE.
745 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100746 * \note Alternative implementations of RSA need not support
747 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300748 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100749 *
Hanno Becker9a467772018-12-13 09:54:59 +0000750 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000751 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
752 * this is used for blinding and should be provided; see
753 * mbedtls_rsa_private() for more. If \p mode is
754 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000755 * \param p_rng The RNG context to be passed to \p f_rng. This may be
756 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
757 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000758 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000759 * \param olen The address at which to store the length of
760 * the plaintext. This must not be \c NULL.
761 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000762 * of length \c ctx->len Bytes. For example, \c 256 Bytes
763 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000764 * \param output The buffer used to hold the plaintext. This must
765 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000766 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100767 *
768 * \return \c 0 on success.
769 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000770 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100771MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200773 int (*f_rng)(void *, unsigned char *, size_t),
774 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000775 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000776 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000777 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000778 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000779
780/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000781 * \brief This function performs a PKCS#1 v1.5 decryption
782 * operation (RSAES-PKCS1-v1_5-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100783 *
Hanno Becker248ae6d2017-05-04 11:27:39 +0100784 * \note The output buffer length \c output_max_len should be
Rose Zadik042e97f2018-01-26 16:35:10 +0000785 * as large as the size \p ctx->len of \p ctx->N, for example,
786 * 128 Bytes if RSA-1024 is used, to be able to hold an
787 * arbitrary decrypted message. If it is not large enough to
788 * hold the decryption of the particular ciphertext provided,
789 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Hanno Becker248ae6d2017-05-04 11:27:39 +0100790 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100791 * \deprecated It is deprecated and discouraged to call this function
792 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
793 * are likely to remove the \p mode argument and have it
794 * implicitly set to #MBEDTLS_RSA_PRIVATE.
795 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100796 * \note Alternative implementations of RSA need not support
797 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300798 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100799 *
Hanno Becker9a467772018-12-13 09:54:59 +0000800 * \param ctx The initialized RSA context to use.
Hanno Becker5bdfca92018-12-18 13:59:28 +0000801 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
802 * this is used for blinding and should be provided; see
803 * mbedtls_rsa_private() for more. If \p mode is
804 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000805 * \param p_rng The RNG context to be passed to \p f_rng. This may be
806 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
807 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000808 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Hanno Becker9a467772018-12-13 09:54:59 +0000809 * \param olen The address at which to store the length of
810 * the plaintext. This must not be \c NULL.
811 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000812 * of length \c ctx->len Bytes. For example, \c 256 Bytes
813 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000814 * \param output The buffer used to hold the plaintext. This must
815 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000816 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100817 *
818 * \return \c 0 on success.
819 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
820 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100821 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100822MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200824 int (*f_rng)(void *, unsigned char *, size_t),
825 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100826 int mode, size_t *olen,
827 const unsigned char *input,
828 unsigned char *output,
829 size_t output_max_len );
830
831/**
Rose Zadike8b5b992018-03-27 12:19:47 +0100832 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
833 * operation (RSAES-OAEP-DECRYPT).
Paul Bakkerb3869132013-02-28 17:21:01 +0100834 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100835 * \note The output buffer length \c output_max_len should be
836 * as large as the size \p ctx->len of \p ctx->N, for
837 * example, 128 Bytes if RSA-1024 is used, to be able to
838 * hold an arbitrary decrypted message. If it is not
839 * large enough to hold the decryption of the particular
840 * ciphertext provided, the function returns
841 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
Paul Bakkerb3869132013-02-28 17:21:01 +0100842 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100843 * \deprecated It is deprecated and discouraged to call this function
844 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
845 * are likely to remove the \p mode argument and have it
846 * implicitly set to #MBEDTLS_RSA_PRIVATE.
847 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100848 * \note Alternative implementations of RSA need not support
849 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300850 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100851 *
Hanno Becker9a467772018-12-13 09:54:59 +0000852 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000853 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
854 * this is used for blinding and should be provided; see
855 * mbedtls_rsa_private() for more. If \p mode is
856 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000857 * \param p_rng The RNG context to be passed to \p f_rng. This may be
858 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
859 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000860 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100861 * \param label The buffer holding the custom label to use.
Hanno Becker9a467772018-12-13 09:54:59 +0000862 * This must be a readable buffer of length \p label_len
863 * Bytes. It may be \c NULL if \p label_len is \c 0.
864 * \param label_len The length of the label in Bytes.
865 * \param olen The address at which to store the length of
866 * the plaintext. This must not be \c NULL.
867 * \param input The ciphertext buffer. This must be a readable buffer
Hanno Becker385ce912018-12-13 18:33:12 +0000868 * of length \c ctx->len Bytes. For example, \c 256 Bytes
869 * for an 2048-bit RSA modulus.
Hanno Becker9a467772018-12-13 09:54:59 +0000870 * \param output The buffer used to hold the plaintext. This must
871 * be a writable buffer of length \p output_max_len Bytes.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000872 * \param output_max_len The length in Bytes of the output buffer \p output.
Rose Zadike8b5b992018-03-27 12:19:47 +0100873 *
874 * \return \c 0 on success.
875 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100876 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100877MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200879 int (*f_rng)(void *, unsigned char *, size_t),
880 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100881 int mode,
882 const unsigned char *label, size_t label_len,
883 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100884 const unsigned char *input,
885 unsigned char *output,
886 size_t output_max_len );
887
888/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000889 * \brief This function performs a private RSA operation to sign
890 * a message digest using PKCS#1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000891 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000892 * It is the generic wrapper for performing a PKCS#1
893 * signature using the \p mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000894 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000895 * \note The \p sig buffer must be as large as the size
896 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000897 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000898 * \note For PKCS#1 v2.1 encoding, see comments on
899 * mbedtls_rsa_rsassa_pss_sign() for details on
900 * \p md_alg and \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +0100901 *
902 * \deprecated It is deprecated and discouraged to call this function
903 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
904 * are likely to remove the \p mode argument and have it
905 * implicitly set to #MBEDTLS_RSA_PRIVATE.
906 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100907 * \note Alternative implementations of RSA need not support
908 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300909 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100910 *
Hanno Becker9a467772018-12-13 09:54:59 +0000911 * \param ctx The initialized RSA context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +0000912 * \param f_rng The RNG function to use. If the padding mode is PKCS#1 v2.1,
913 * this must be provided. If the padding mode is PKCS#1 v1.5 and
914 * \p mode is #MBEDTLS_RSA_PRIVATE, it is used for blinding
915 * and should be provided; see mbedtls_rsa_private() for more
916 * more. It is ignored otherwise.
Hanno Becker9a467772018-12-13 09:54:59 +0000917 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
918 * if \p f_rng is \c NULL or doesn't need a context argument.
919 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000920 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +0100921 * \param md_alg The message-digest algorithm used to hash the original data.
922 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000923 * \param hashlen The length of the message digest.
924 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
925 * \param hash The buffer holding the message digest or raw data.
926 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
927 * buffer of length \p hashlen Bytes. If \p md_alg is not
928 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
929 * the size of the hash corresponding to \p md_alg.
930 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000931 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
932 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +0100933 *
934 * \return \c 0 if the signing operation was successful.
935 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000936 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100937MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000939 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000940 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000941 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000943 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000944 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000945 unsigned char *sig );
946
947/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000948 * \brief This function performs a PKCS#1 v1.5 signature
949 * operation (RSASSA-PKCS1-v1_5-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100950 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100951 * \deprecated It is deprecated and discouraged to call this function
952 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
953 * are likely to remove the \p mode argument and have it
954 * implicitly set to #MBEDTLS_RSA_PRIVATE.
955 *
Rose Zadikf2ec2882018-04-17 10:27:25 +0100956 * \note Alternative implementations of RSA need not support
957 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +0300958 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +0100959 *
Hanno Becker9a467772018-12-13 09:54:59 +0000960 * \param ctx The initialized RSA context to use.
Hanno Beckerf66f2942018-12-18 13:30:08 +0000961 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
962 * this is used for blinding and should be provided; see
963 * mbedtls_rsa_private() for more. If \p mode is
964 * #MBEDTLS_RSA_PUBLIC, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +0000965 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
966 * if \p f_rng is \c NULL or doesn't need a context argument.
967 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +0000968 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +0000969 * \param md_alg The message-digest algorithm used to hash the original data.
970 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +0000971 * \param hashlen The length of the message digest.
972 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
973 * \param hash The buffer holding the message digest or raw data.
974 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
975 * buffer of length \p hashlen Bytes. If \p md_alg is not
976 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
977 * the size of the hash corresponding to \p md_alg.
978 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +0000979 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
980 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +0100981 *
Rose Zadike8b5b992018-03-27 12:19:47 +0100982 * \return \c 0 if the signing operation was successful.
983 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +0100984 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100985MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986int mbedtls_rsa_rsassa_pkcs1_v15_sign( 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,
993 unsigned char *sig );
994
995/**
Rose Zadik042e97f2018-01-26 16:35:10 +0000996 * \brief This function performs a PKCS#1 v2.1 PSS signature
997 * operation (RSASSA-PSS-SIGN).
Paul Bakkerb3869132013-02-28 17:21:01 +0100998 *
Rose Zadik042e97f2018-01-26 16:35:10 +0000999 * \note The \p hash_id in the RSA context is the one used for the
1000 * encoding. \p md_alg in the function call is the type of hash
1001 * that is encoded. According to <em>RFC-3447: Public-Key
1002 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1003 * Specifications</em> it is advised to keep both hashes the
1004 * same.
Rose Zadike8b5b992018-03-27 12:19:47 +01001005 *
Jaeden Amero3725bb22018-09-07 19:12:36 +01001006 * \note This function always uses the maximum possible salt size,
1007 * up to the length of the payload hash. This choice of salt
1008 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
1009 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
1010 * minimum salt size which is the hash size minus 2 bytes. If
1011 * this minimum size is too large given the key size (the salt
1012 * size, plus the hash size, plus 2 bytes must be no more than
1013 * the key size in bytes), this function returns
1014 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
1015 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001016 * \deprecated It is deprecated and discouraged to call this function
1017 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
1018 * are likely to remove the \p mode argument and have it
1019 * implicitly set to #MBEDTLS_RSA_PRIVATE.
1020 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001021 * \note Alternative implementations of RSA need not support
1022 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001023 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001024 *
Hanno Becker9a467772018-12-13 09:54:59 +00001025 * \param ctx The initialized RSA context to use.
1026 * \param f_rng The RNG function. It must not be \c NULL.
1027 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
1028 * if \p f_rng doesn't need a context argument.
1029 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001030 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001031 * \param md_alg The message-digest algorithm used to hash the original data.
1032 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001033 * \param hashlen The length of the message digest.
1034 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
1035 * \param hash The buffer holding the message digest or raw data.
1036 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1037 * buffer of length \p hashlen Bytes. If \p md_alg is not
1038 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1039 * the size of the hash corresponding to \p md_alg.
1040 * \param sig The buffer to hold the signature. This must be a writable
Hanno Becker385ce912018-12-13 18:33:12 +00001041 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1042 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001043 *
1044 * \return \c 0 if the signing operation was successful.
1045 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001046 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +01001047MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001049 int (*f_rng)(void *, unsigned char *, size_t),
1050 void *p_rng,
1051 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001053 unsigned int hashlen,
1054 const unsigned char *hash,
1055 unsigned char *sig );
1056
1057/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001058 * \brief This function performs a public RSA operation and checks
1059 * the message digest.
Paul Bakker5121ce52009-01-03 21:22:43 +00001060 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001061 * This is the generic wrapper for performing a PKCS#1
1062 * verification using the mode from the context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001063 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001064 * \note For PKCS#1 v2.1 encoding, see comments on
1065 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
1066 * \p hash_id.
Rose Zadike8b5b992018-03-27 12:19:47 +01001067 *
1068 * \deprecated It is deprecated and discouraged to call this function
1069 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1070 * are likely to remove the \p mode argument and have it
1071 * set to #MBEDTLS_RSA_PUBLIC.
1072 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001073 * \note Alternative implementations of RSA need not support
1074 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001075 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001076 *
Hanno Becker9a467772018-12-13 09:54:59 +00001077 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001078 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1079 * this is used for blinding and should be provided; see
1080 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001081 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1082 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1083 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001084 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001085 * \param md_alg The message-digest algorithm used to hash the original data.
1086 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001087 * \param hashlen The length of the message digest.
1088 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1089 * \param hash The buffer holding the message digest or raw data.
1090 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1091 * buffer of length \p hashlen Bytes. If \p md_alg is not
1092 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1093 * the size of the hash corresponding to \p md_alg.
1094 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001095 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1096 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001097 *
1098 * \return \c 0 if the verify operation was successful.
1099 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +01001101MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001103 int (*f_rng)(void *, unsigned char *, size_t),
1104 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001105 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001107 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001108 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001109 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +00001110
1111/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001112 * \brief This function performs a PKCS#1 v1.5 verification
1113 * operation (RSASSA-PKCS1-v1_5-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001114 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001115 * \deprecated It is deprecated and discouraged to call this function
1116 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1117 * are likely to remove the \p mode argument and have it
1118 * set to #MBEDTLS_RSA_PUBLIC.
1119 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001120 * \note Alternative implementations of RSA need not support
1121 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001122 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001123 *
Hanno Becker9a467772018-12-13 09:54:59 +00001124 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001125 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1126 * this is used for blinding and should be provided; see
1127 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001128 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1129 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1130 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001131 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadik042e97f2018-01-26 16:35:10 +00001132 * \param md_alg The message-digest algorithm used to hash the original data.
1133 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001134 * \param hashlen The length of the message digest.
1135 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1136 * \param hash The buffer holding the message digest or raw data.
1137 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1138 * buffer of length \p hashlen Bytes. If \p md_alg is not
1139 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1140 * the size of the hash corresponding to \p md_alg.
1141 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001142 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1143 * for an 2048-bit RSA modulus.
Paul Bakkerb3869132013-02-28 17:21:01 +01001144 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001145 * \return \c 0 if the verify operation was successful.
1146 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001147 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +01001148MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001150 int (*f_rng)(void *, unsigned char *, size_t),
1151 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001152 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001154 unsigned int hashlen,
1155 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001156 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001157
1158/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001159 * \brief This function performs a PKCS#1 v2.1 PSS verification
1160 * operation (RSASSA-PSS-VERIFY).
Paul Bakkerb3869132013-02-28 17:21:01 +01001161 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001162 * The hash function for the MGF mask generating function
1163 * is that specified in the RSA context.
Paul Bakkerb3869132013-02-28 17:21:01 +01001164 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001165 * \note The \p hash_id in the RSA context is the one used for the
1166 * verification. \p md_alg in the function call is the type of
1167 * hash that is verified. According to <em>RFC-3447: Public-Key
1168 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1169 * Specifications</em> it is advised to keep both hashes the
1170 * same. If \p hash_id in the RSA context is unset,
1171 * the \p md_alg from the function call is used.
Rose Zadike8b5b992018-03-27 12:19:47 +01001172 *
1173 * \deprecated It is deprecated and discouraged to call this function
1174 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1175 * are likely to remove the \p mode argument and have it
1176 * implicitly set to #MBEDTLS_RSA_PUBLIC.
1177 *
Rose Zadikf2ec2882018-04-17 10:27:25 +01001178 * \note Alternative implementations of RSA need not support
1179 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
Ron Eldor9924bdc2018-10-04 10:59:13 +03001180 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
Rose Zadikf2ec2882018-04-17 10:27:25 +01001181 *
Hanno Becker9a467772018-12-13 09:54:59 +00001182 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001183 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1184 * this is used for blinding and should be provided; see
1185 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001186 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1187 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1188 * \param mode The mode of operation. This must be either
Hanno Becker385ce912018-12-13 18:33:12 +00001189 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
Rose Zadike8b5b992018-03-27 12:19:47 +01001190 * \param md_alg The message-digest algorithm used to hash the original data.
1191 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001192 * \param hashlen The length of the message digest.
1193 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1194 * \param hash The buffer holding the message digest or raw data.
1195 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1196 * buffer of length \p hashlen Bytes. If \p md_alg is not
1197 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1198 * the size of the hash corresponding to \p md_alg.
1199 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001200 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1201 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001202 *
1203 * \return \c 0 if the verify operation was successful.
1204 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Paul Bakkerb3869132013-02-28 17:21:01 +01001205 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +01001206MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001208 int (*f_rng)(void *, unsigned char *, size_t),
1209 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001210 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001212 unsigned int hashlen,
1213 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001214 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001215
1216/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001217 * \brief This function performs a PKCS#1 v2.1 PSS verification
1218 * operation (RSASSA-PSS-VERIFY).
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001219 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001220 * The hash function for the MGF mask generating function
1221 * is that specified in \p mgf1_hash_id.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001222 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001223 * \note The \p sig buffer must be as large as the size
1224 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001225 *
Rose Zadik042e97f2018-01-26 16:35:10 +00001226 * \note The \p hash_id in the RSA context is ignored.
Rose Zadike8b5b992018-03-27 12:19:47 +01001227 *
Hanno Becker9a467772018-12-13 09:54:59 +00001228 * \param ctx The initialized RSA public key context to use.
Hanno Beckera9020f22018-12-18 14:45:45 +00001229 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1230 * this is used for blinding and should be provided; see
1231 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
Hanno Becker9a467772018-12-13 09:54:59 +00001232 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1233 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1234 * \param mode The mode of operation. This must be either
1235 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
Rose Zadike8b5b992018-03-27 12:19:47 +01001236 * \param md_alg The message-digest algorithm used to hash the original data.
1237 * Use #MBEDTLS_MD_NONE for signing raw data.
Hanno Becker9a467772018-12-13 09:54:59 +00001238 * \param hashlen The length of the message digest.
1239 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1240 * \param hash The buffer holding the message digest or raw data.
1241 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1242 * buffer of length \p hashlen Bytes. If \p md_alg is not
1243 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1244 * the size of the hash corresponding to \p md_alg.
1245 * \param mgf1_hash_id The message digest used for mask generation.
1246 * \param expected_salt_len The length of the salt used in padding. Use
1247 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1248 * \param sig The buffer holding the signature. This must be a readable
Hanno Becker385ce912018-12-13 18:33:12 +00001249 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1250 * for an 2048-bit RSA modulus.
Rose Zadike8b5b992018-03-27 12:19:47 +01001251 *
1252 * \return \c 0 if the verify operation was successful.
1253 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001254 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +01001255MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001257 int (*f_rng)(void *, unsigned char *, size_t),
1258 void *p_rng,
1259 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001261 unsigned int hashlen,
1262 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001264 int expected_salt_len,
1265 const unsigned char *sig );
1266
1267/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001268 * \brief This function copies the components of an RSA context.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001269 *
Hanno Becker9a467772018-12-13 09:54:59 +00001270 * \param dst The destination context. This must be initialized.
1271 * \param src The source context. This must be initialized.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001272 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001273 * \return \c 0 on success.
1274 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001275 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +01001276MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001278
1279/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001280 * \brief This function frees the components of an RSA key.
Paul Bakker13e2dfe2009-07-28 07:18:38 +00001281 *
Hanno Becker9a467772018-12-13 09:54:59 +00001282 * \param ctx The RSA context to free. May be \c NULL, in which case
1283 * this function is a no-op. If it is not \c NULL, it must
Hanno Becker385ce912018-12-13 18:33:12 +00001284 * point to an initialized RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +00001285 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +01001286MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00001288
Ron Eldorfa8f6352017-06-20 15:48:46 +03001289#if defined(MBEDTLS_SELF_TEST)
1290
Paul Bakker5121ce52009-01-03 21:22:43 +00001291/**
Rose Zadik042e97f2018-01-26 16:35:10 +00001292 * \brief The RSA checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 *
Rose Zadike8b5b992018-03-27 12:19:47 +01001294 * \return \c 0 on success.
1295 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +00001296 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +01001297MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
Ron Eldorfa8f6352017-06-20 15:48:46 +03001300#endif /* MBEDTLS_SELF_TEST */
1301
Paul Bakker5121ce52009-01-03 21:22:43 +00001302#ifdef __cplusplus
1303}
1304#endif
1305
Paul Bakker5121ce52009-01-03 21:22:43 +00001306#endif /* rsa.h */