blob: 082fbef2e3b493e6d7b0f82232e32fcf511e5294 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief The RSA public-key cryptosystem
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_RSA_H
24#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020027#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakkered27a042013-04-18 22:46:23 +020031
Paul Bakker314052f2011-08-15 09:07:52 +000032#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020033#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_THREADING_C)
Paul Bakkerc9965dc2013-09-29 14:58:17 +020036#include "threading.h"
37#endif
38
Paul Bakker13e2dfe2009-07-28 07:18:38 +000039/*
40 * RSA Error codes
41 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
43#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
44#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +020045#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the library's validity check. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
47#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
48#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
49#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
50#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Paul Bakker5121ce52009-01-03 21:22:43 +000051
52/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020053 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#define MBEDTLS_RSA_PUBLIC 0
56#define MBEDTLS_RSA_PRIVATE 1
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define MBEDTLS_RSA_PKCS_V15 0
59#define MBEDTLS_RSA_PKCS_V21 1
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#define MBEDTLS_RSA_SIGN 1
62#define MBEDTLS_RSA_CRYPT 2
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020065
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020066/*
67 * The above constants may be used even if the RSA module is compile out,
68 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
69 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020071
Paul Bakker407a0da2013-06-27 14:29:21 +020072#ifdef __cplusplus
73extern "C" {
74#endif
75
Paul Bakker5121ce52009-01-03 21:22:43 +000076/**
77 * \brief RSA context structure
78 */
79typedef struct
80{
81 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +000082 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +000083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 mbedtls_mpi N; /*!< public modulus */
85 mbedtls_mpi E; /*!< public exponent */
Paul Bakker5121ce52009-01-03 21:22:43 +000086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_mpi D; /*!< private exponent */
88 mbedtls_mpi P; /*!< 1st prime factor */
89 mbedtls_mpi Q; /*!< 2nd prime factor */
90 mbedtls_mpi DP; /*!< D % (P - 1) */
91 mbedtls_mpi DQ; /*!< D % (Q - 1) */
92 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Paul Bakker5121ce52009-01-03 21:22:43 +000093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_mpi RN; /*!< cached R^2 mod N */
95 mbedtls_mpi RP; /*!< cached R^2 mod P */
96 mbedtls_mpi RQ; /*!< cached R^2 mod Q */
Paul Bakker5121ce52009-01-03 21:22:43 +000097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_mpi Vi; /*!< cached blinding value */
99 mbedtls_mpi Vf; /*!< cached un-blinding value */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 int padding; /*!< MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
Paul Bakker9dcc3222011-03-08 14:16:06 +0000102 RSA_PKCS_v21 for OAEP/PSS */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
104 specified in the mbedtls_md.h header file
Paul Bakker9dcc3222011-03-08 14:16:06 +0000105 for the EME-OAEP and EMSA-PSS
106 encoding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#if defined(MBEDTLS_THREADING_C)
108 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200109#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
Paul Bakker5121ce52009-01-03 21:22:43 +0000113/**
114 * \brief Initialize an RSA context
115 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 * Note: Set padding to MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000117 * encryption scheme and the RSASSA-PSS signature scheme.
118 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 * \param ctx RSA context to be initialized
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 * \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21
121 * \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 *
123 * \note The hash_id parameter is actually ignored
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 * when using MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200125 *
126 * \note Choice of padding mode is strictly enforced for private key
127 * operations, since there might be security concerns in
128 * mixing padding modes. For public key operations it's merely
129 * a default value, which can be overriden by calling specific
130 * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
131 *
132 * \note The chosen hash is always used for OEAP encryption.
133 * For PSS signatures, it's always used for making signatures,
134 * but can be overriden (and always is, if set to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 * MBEDTLS_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 int padding,
Paul Bakker42099c32014-01-27 11:45:49 +0100139 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100142 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100144 *
145 * \param ctx RSA context to be set
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 * \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21
147 * \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100150
151/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 * \brief Generate an RSA keypair
153 *
154 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000155 * \param f_rng RNG function
156 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 * \param nbits size of the public key in bits
158 * \param exponent public exponent (e.g., 65537)
159 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000161 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000166 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000167 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000168 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
170/**
171 * \brief Check a public RSA key
172 *
173 * \param ctx RSA context to be checked
174 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
179/**
180 * \brief Check a private RSA key
181 *
182 * \param ctx RSA context to be checked
183 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
188/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100189 * \brief Check a public-private RSA key pair.
190 * Check each of the contexts, and make sure they match.
191 *
192 * \param pub RSA context holding the public key
193 * \param prv RSA context holding the private key
194 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100198
199/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 * \brief Do an RSA public key operation
201 *
202 * \param ctx RSA context
203 * \param input input buffer
204 * \param output output buffer
205 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 *
208 * \note This function does NOT take care of message
Brian J Murraye7f8dc32016-11-06 04:45:15 -0800209 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000210 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 *
212 * \note The input and output buffers must be large
213 * enough (eg. 128 bytes if RSA-1024 is used).
214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000216 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 unsigned char *output );
218
219/**
220 * \brief Do an RSA private key operation
221 *
222 * \param ctx RSA context
Hanno Becker9d5785b2017-11-06 15:06:25 +0000223 * \param f_rng RNG function (used for blinding)
Paul Bakker548957d2013-08-30 10:30:02 +0200224 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 * \param input input buffer
226 * \param output output buffer
227 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 *
230 * \note The input and output buffers must be large
231 * enough (eg. 128 bytes if RSA-1024 is used).
Hanno Becker9d5785b2017-11-06 15:06:25 +0000232 *
233 * \note Blinding is used if and only if a PRNG is provided.
234 *
235 * \note If blinding is used, both the base of exponentation
236 * and the exponent are blinded, providing protection
237 * against some side-channel attacks.
238 *
239 * \warning It is deprecated and a security risk to not provide
240 * a PRNG here and thereby prevent the use of blinding.
241 * Future versions of the library may enforce the presence
242 * of a PRNG.
243 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200246 int (*f_rng)(void *, unsigned char *, size_t),
247 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000248 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 unsigned char *output );
250
251/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100252 * \brief Generic wrapper to perform a PKCS#1 encryption using the
253 * mode from the context. Add the message padding, then do an
254 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 *
256 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200257 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 * and MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000259 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000261 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 * \param input buffer holding the data to be encrypted
263 * \param output buffer that will hold the ciphertext
264 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 *
267 * \note The output buffer must be as large as the size
268 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000271 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000272 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000273 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000274 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 unsigned char *output );
276
277/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100278 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
279 *
280 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 * \param f_rng RNG function (Needed for padding and MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100282 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100284 * \param ilen contains the plaintext length
285 * \param input buffer holding the data to be encrypted
286 * \param output buffer that will hold the ciphertext
287 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100289 *
290 * \note The output buffer must be as large as the size
291 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100294 int (*f_rng)(void *, unsigned char *, size_t),
295 void *p_rng,
296 int mode, size_t ilen,
297 const unsigned char *input,
298 unsigned char *output );
299
300/**
301 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
302 *
303 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200304 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 * and MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100306 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100308 * \param label buffer holding the custom label to use
309 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100310 * \param ilen contains the plaintext length
311 * \param input buffer holding the data to be encrypted
312 * \param output buffer that will hold the ciphertext
313 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100315 *
316 * \note The output buffer must be as large as the size
317 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100320 int (*f_rng)(void *, unsigned char *, size_t),
321 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100322 int mode,
323 const unsigned char *label, size_t label_len,
324 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100325 const unsigned char *input,
326 unsigned char *output );
327
328/**
329 * \brief Generic wrapper to perform a PKCS#1 decryption using the
330 * mode from the context. Do an RSA operation, then remove
331 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 *
333 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200335 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000337 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 * \param input buffer holding the encrypted data
339 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000340 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000341 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 *
Hanno Beckercea8b532017-05-04 11:27:39 +0100344 * \note The output buffer length \c output_max_len should be
345 * as large as the size ctx->len of ctx->N (eg. 128 bytes
346 * if RSA-1024 is used) to be able to hold an arbitrary
347 * decrypted message. If it is not large enough to hold
Simon Butcherad761c42018-06-22 11:22:44 +0100348 * the decryption of the particular ciphertext provided,
Hanno Beckercea8b532017-05-04 11:27:39 +0100349 * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
350 *
351 * \note The input buffer must be as large as the size
352 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200355 int (*f_rng)(void *, unsigned char *, size_t),
356 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000357 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000358 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000359 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000360 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000361
362/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100363 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
364 *
365 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200367 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100369 * \param olen will contain the plaintext length
370 * \param input buffer holding the encrypted data
371 * \param output buffer that will hold the plaintext
372 * \param output_max_len maximum length of the output buffer
373 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100375 *
Hanno Beckercea8b532017-05-04 11:27:39 +0100376 * \note The output buffer length \c output_max_len should be
377 * as large as the size ctx->len of ctx->N (eg. 128 bytes
378 * if RSA-1024 is used) to be able to hold an arbitrary
379 * decrypted message. If it is not large enough to hold
Simon Butcherad761c42018-06-22 11:22:44 +0100380 * the decryption of the particular ciphertext provided,
Hanno Beckercea8b532017-05-04 11:27:39 +0100381 * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
382 *
383 * \note The input buffer must be as large as the size
384 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200387 int (*f_rng)(void *, unsigned char *, size_t),
388 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100389 int mode, size_t *olen,
390 const unsigned char *input,
391 unsigned char *output,
392 size_t output_max_len );
393
394/**
395 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
396 *
397 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200399 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100401 * \param label buffer holding the custom label to use
402 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100403 * \param olen will contain the plaintext length
404 * \param input buffer holding the encrypted data
405 * \param output buffer that will hold the plaintext
406 * \param output_max_len maximum length of the output buffer
407 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100409 *
Hanno Beckercea8b532017-05-04 11:27:39 +0100410 * \note The output buffer length \c output_max_len should be
411 * as large as the size ctx->len of ctx->N (eg. 128 bytes
412 * if RSA-1024 is used) to be able to hold an arbitrary
413 * decrypted message. If it is not large enough to hold
Simon Butcherad761c42018-06-22 11:22:44 +0100414 * the decryption of the particular ciphertext provided,
Hanno Beckercea8b532017-05-04 11:27:39 +0100415 * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
416 *
Simon Butcherad761c42018-06-22 11:22:44 +0100417 * \note The input buffer must be as large as the size
Hanno Beckercea8b532017-05-04 11:27:39 +0100418 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200421 int (*f_rng)(void *, unsigned char *, size_t),
422 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100423 int mode,
424 const unsigned char *label, size_t label_len,
425 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100426 const unsigned char *input,
427 unsigned char *output,
428 size_t output_max_len );
429
430/**
431 * \brief Generic wrapper to perform a PKCS#1 signature using the
432 * mode from the context. Do a private RSA operation to sign
433 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 *
435 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200436 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 * MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000438 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
440 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
441 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000442 * \param hash buffer holding the message digest
443 * \param sig buffer that will hold the ciphertext
444 *
445 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000447 *
448 * \note The "sig" buffer must be as large as the size
449 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000450 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200451 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 * \note \c mbedtls_rsa_rsassa_pss_sign() for details on md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000455 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000456 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000459 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000460 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 unsigned char *sig );
462
463/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100464 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
465 *
466 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200468 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
470 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
471 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100472 * \param hash buffer holding the message digest
473 * \param sig buffer that will hold the ciphertext
474 *
475 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100477 *
478 * \note The "sig" buffer must be as large as the size
479 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200482 int (*f_rng)(void *, unsigned char *, size_t),
483 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100484 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100486 unsigned int hashlen,
487 const unsigned char *hash,
488 unsigned char *sig );
489
490/**
491 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
492 *
493 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200494 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 * MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100496 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
498 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
499 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100500 * \param hash buffer holding the message digest
501 * \param sig buffer that will hold the ciphertext
502 *
503 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100505 *
506 * \note The "sig" buffer must be as large as the size
507 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
508 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200509 * \note The hash_id in the RSA context is the one used for the
510 * encoding. md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100511 * that is encoded. According to RFC 3447 it is advised to
512 * keep both hashes the same.
513 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100515 int (*f_rng)(void *, unsigned char *, size_t),
516 void *p_rng,
517 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100519 unsigned int hashlen,
520 const unsigned char *hash,
521 unsigned char *sig );
522
523/**
524 * \brief Generic wrapper to perform a PKCS#1 verification using the
525 * mode from the context. Do a public RSA operation and check
526 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000527 *
528 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200530 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
532 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
533 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 * \param hash buffer holding the message digest
535 * \param sig buffer holding the ciphertext
536 *
537 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000539 *
540 * \note The "sig" buffer must be as large as the size
541 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000542 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200543 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200547 int (*f_rng)(void *, unsigned char *, size_t),
548 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000551 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000552 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200553 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
Hanno Beckerb46e7332018-10-25 14:37:35 +0100555#if defined(MBEDTLS_ASN1_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000556/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100557 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
558 *
559 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200561 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
563 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
564 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100565 * \param hash buffer holding the message digest
566 * \param sig buffer holding the ciphertext
567 *
568 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100570 *
571 * \note The "sig" buffer must be as large as the size
572 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200575 int (*f_rng)(void *, unsigned char *, size_t),
576 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100577 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100579 unsigned int hashlen,
580 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200581 const unsigned char *sig );
Hanno Beckerb46e7332018-10-25 14:37:35 +0100582#endif /* MBEDTLS_ASN1_PARSE_C */
Paul Bakkerb3869132013-02-28 17:21:01 +0100583
584/**
585 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200586 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +0100587 *
588 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200590 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
592 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
593 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100594 * \param hash buffer holding the message digest
595 * \param sig buffer holding the ciphertext
596 *
597 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100599 *
600 * \note The "sig" buffer must be as large as the size
601 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
602 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200603 * \note The hash_id in the RSA context is the one used for the
604 * verification. md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200605 * hash that is verified. According to RFC 3447 it is advised to
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200606 * keep both hashes the same. If hash_id in the RSA context is
607 * unset, the md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200610 int (*f_rng)(void *, unsigned char *, size_t),
611 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100612 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100614 unsigned int hashlen,
615 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200616 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100617
618/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200619 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
620 * (This is the version with "full" options.)
621 *
622 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200624 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
626 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
627 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200628 * \param hash buffer holding the message digest
629 * \param mgf1_hash_id message digest used for mask generation
630 * \param expected_salt_len Length of the salt used in padding, use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 * MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200632 * \param sig buffer holding the ciphertext
633 *
634 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 * or an MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200636 *
637 * \note The "sig" buffer must be as large as the size
638 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
639 *
640 * \note The hash_id in the RSA context is ignored.
641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200643 int (*f_rng)(void *, unsigned char *, size_t),
644 void *p_rng,
645 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200647 unsigned int hashlen,
648 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200650 int expected_salt_len,
651 const unsigned char *sig );
652
653/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200654 * \brief Copy the components of an RSA context
655 *
656 * \param dst Destination context
657 * \param src Source context
658 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200659 * \return 0 on success,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200660 * MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200661 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200663
664/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000666 *
667 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000670
671/**
672 * \brief Checkup routine
673 *
674 * \return 0 if successful, or 1 if the test failed
675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000677
678#ifdef __cplusplus
679}
680#endif
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682#endif /* MBEDTLS_RSA_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200683
Paul Bakker5121ce52009-01-03 21:22:43 +0000684#endif /* rsa.h */