blob: 9ff667ed4d18ed0ee19f5074eca26e180f1d13ef [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é-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00009 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_RSA_H
25#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakkered27a042013-04-18 22:46:23 +020032
Paul Bakker314052f2011-08-15 09:07:52 +000033#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020034#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_THREADING_C)
Paul Bakkerc9965dc2013-09-29 14:58:17 +020037#include "threading.h"
38#endif
39
Paul Bakker13e2dfe2009-07-28 07:18:38 +000040/*
41 * RSA Error codes
42 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
44#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
45#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +020046#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 +020047#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
48#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
49#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
50#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
51#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Paul Bakker5121ce52009-01-03 21:22:43 +000052
53/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020054 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define MBEDTLS_RSA_PUBLIC 0
57#define MBEDTLS_RSA_PRIVATE 1
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define MBEDTLS_RSA_PKCS_V15 0
60#define MBEDTLS_RSA_PKCS_V21 1
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define MBEDTLS_RSA_SIGN 1
63#define MBEDTLS_RSA_CRYPT 2
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020066
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020067/*
68 * The above constants may be used even if the RSA module is compile out,
69 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
70 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020072
Paul Bakker407a0da2013-06-27 14:29:21 +020073#ifdef __cplusplus
74extern "C" {
75#endif
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/**
78 * \brief RSA context structure
79 */
80typedef struct
81{
82 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +000083 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +000084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 mbedtls_mpi N; /*!< public modulus */
86 mbedtls_mpi E; /*!< public exponent */
Paul Bakker5121ce52009-01-03 21:22:43 +000087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_mpi D; /*!< private exponent */
89 mbedtls_mpi P; /*!< 1st prime factor */
90 mbedtls_mpi Q; /*!< 2nd prime factor */
91 mbedtls_mpi DP; /*!< D % (P - 1) */
92 mbedtls_mpi DQ; /*!< D % (Q - 1) */
93 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Paul Bakker5121ce52009-01-03 21:22:43 +000094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_mpi RN; /*!< cached R^2 mod N */
96 mbedtls_mpi RP; /*!< cached R^2 mod P */
97 mbedtls_mpi RQ; /*!< cached R^2 mod Q */
Paul Bakker5121ce52009-01-03 21:22:43 +000098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_mpi Vi; /*!< cached blinding value */
100 mbedtls_mpi Vf; /*!< cached un-blinding value */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 int padding; /*!< MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
Paul Bakker9dcc3222011-03-08 14:16:06 +0000103 RSA_PKCS_v21 for OAEP/PSS */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
105 specified in the mbedtls_md.h header file
Paul Bakker9dcc3222011-03-08 14:16:06 +0000106 for the EME-OAEP and EMSA-PSS
107 encoding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108#if defined(MBEDTLS_THREADING_C)
109 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200110#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000111}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
Paul Bakker5121ce52009-01-03 21:22:43 +0000114/**
115 * \brief Initialize an RSA context
116 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 * Note: Set padding to MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000118 * encryption scheme and the RSASSA-PSS signature scheme.
119 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 * \param ctx RSA context to be initialized
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 * \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21
122 * \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 *
124 * \note The hash_id parameter is actually ignored
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 * when using MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200126 *
127 * \note Choice of padding mode is strictly enforced for private key
128 * operations, since there might be security concerns in
129 * mixing padding modes. For public key operations it's merely
130 * a default value, which can be overriden by calling specific
131 * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
132 *
133 * \note The chosen hash is always used for OEAP encryption.
134 * For PSS signatures, it's always used for making signatures,
135 * but can be overriden (and always is, if set to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 * MBEDTLS_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 int padding,
Paul Bakker42099c32014-01-27 11:45:49 +0100140 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
142/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100143 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100145 *
146 * \param ctx RSA context to be set
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 * \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21
148 * \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100151
152/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 * \brief Generate an RSA keypair
154 *
155 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000156 * \param f_rng RNG function
157 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 * \param nbits size of the public key in bits
159 * \param exponent public exponent (e.g., 65537)
160 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000162 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000167 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000168 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000169 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
171/**
172 * \brief Check a public RSA key
173 *
174 * \param ctx RSA context to be checked
175 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
180/**
181 * \brief Check a private RSA key
182 *
183 * \param ctx RSA context to be checked
184 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
189/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100190 * \brief Check a public-private RSA key pair.
191 * Check each of the contexts, and make sure they match.
192 *
193 * \param pub RSA context holding the public key
194 * \param prv RSA context holding the private key
195 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100199
200/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 * \brief Do an RSA public key operation
202 *
203 * \param ctx RSA context
204 * \param input input buffer
205 * \param output output buffer
206 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 *
209 * \note This function does NOT take care of message
Paul Bakker619467a2009-03-28 23:26:51 +0000210 * padding. Also, be sure to set input[0] = 0 or assure that
211 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 *
213 * \note The input and output buffers must be large
214 * enough (eg. 128 bytes if RSA-1024 is used).
215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000217 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 unsigned char *output );
219
220/**
221 * \brief Do an RSA private key operation
222 *
223 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200224 * \param f_rng RNG function (Needed for blinding)
225 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 * \param input input buffer
227 * \param output output buffer
228 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 *
231 * \note The input and output buffers must be large
232 * enough (eg. 128 bytes if RSA-1024 is used).
233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200235 int (*f_rng)(void *, unsigned char *, size_t),
236 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000237 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 unsigned char *output );
239
240/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100241 * \brief Generic wrapper to perform a PKCS#1 encryption using the
242 * mode from the context. Add the message padding, then do an
243 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 *
245 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200246 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 * and MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000248 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000250 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 * \param input buffer holding the data to be encrypted
252 * \param output buffer that will hold the ciphertext
253 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 *
256 * \note The output buffer must be as large as the size
257 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000260 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000261 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000262 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000263 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 unsigned char *output );
265
266/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100267 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
268 *
269 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 * \param f_rng RNG function (Needed for padding and MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100271 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100273 * \param ilen contains the plaintext length
274 * \param input buffer holding the data to be encrypted
275 * \param output buffer that will hold the ciphertext
276 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100278 *
279 * \note The output buffer must be as large as the size
280 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100283 int (*f_rng)(void *, unsigned char *, size_t),
284 void *p_rng,
285 int mode, size_t ilen,
286 const unsigned char *input,
287 unsigned char *output );
288
289/**
290 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
291 *
292 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200293 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 * and MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100295 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100297 * \param label buffer holding the custom label to use
298 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100299 * \param ilen contains the plaintext length
300 * \param input buffer holding the data to be encrypted
301 * \param output buffer that will hold the ciphertext
302 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100304 *
305 * \note The output buffer must be as large as the size
306 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100309 int (*f_rng)(void *, unsigned char *, size_t),
310 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100311 int mode,
312 const unsigned char *label, size_t label_len,
313 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100314 const unsigned char *input,
315 unsigned char *output );
316
317/**
318 * \brief Generic wrapper to perform a PKCS#1 decryption using the
319 * mode from the context. Do an RSA operation, then remove
320 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 *
322 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200324 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000326 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 * \param input buffer holding the encrypted data
328 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000329 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 *
333 * \note The output buffer must be as large as the size
Paul Bakker060c5682009-01-12 21:48:39 +0000334 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
335 * an error is thrown.
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200338 int (*f_rng)(void *, unsigned char *, size_t),
339 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000340 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000341 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000342 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000343 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000344
345/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100346 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
347 *
348 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200350 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100352 * \param olen will contain the plaintext length
353 * \param input buffer holding the encrypted data
354 * \param output buffer that will hold the plaintext
355 * \param output_max_len maximum length of the output buffer
356 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100358 *
359 * \note The output buffer must be as large as the size
360 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
361 * an error is thrown.
362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200364 int (*f_rng)(void *, unsigned char *, size_t),
365 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100366 int mode, size_t *olen,
367 const unsigned char *input,
368 unsigned char *output,
369 size_t output_max_len );
370
371/**
372 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
373 *
374 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200376 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100378 * \param label buffer holding the custom label to use
379 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100380 * \param olen will contain the plaintext length
381 * \param input buffer holding the encrypted data
382 * \param output buffer that will hold the plaintext
383 * \param output_max_len maximum length of the output buffer
384 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100386 *
387 * \note The output buffer must be as large as the size
388 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
389 * an error is thrown.
390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200392 int (*f_rng)(void *, unsigned char *, size_t),
393 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100394 int mode,
395 const unsigned char *label, size_t label_len,
396 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100397 const unsigned char *input,
398 unsigned char *output,
399 size_t output_max_len );
400
401/**
402 * \brief Generic wrapper to perform a PKCS#1 signature using the
403 * mode from the context. Do a private RSA operation to sign
404 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 *
406 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200407 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 * MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000409 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
411 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
412 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 * \param hash buffer holding the message digest
414 * \param sig buffer that will hold the ciphertext
415 *
416 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 *
419 * \note The "sig" buffer must be as large as the size
420 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000421 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200422 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 * \note \c mbedtls_rsa_rsassa_pss_sign() for details on md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000426 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000427 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000430 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000431 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000432 unsigned char *sig );
433
434/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100435 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
436 *
437 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200439 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
441 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
442 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100443 * \param hash buffer holding the message digest
444 * \param sig buffer that will hold the ciphertext
445 *
446 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100448 *
449 * \note The "sig" buffer must be as large as the size
450 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
451 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200453 int (*f_rng)(void *, unsigned char *, size_t),
454 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100455 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100457 unsigned int hashlen,
458 const unsigned char *hash,
459 unsigned char *sig );
460
461/**
462 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
463 *
464 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200465 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 * MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100467 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
469 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
470 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100471 * \param hash buffer holding the message digest
472 * \param sig buffer that will hold the ciphertext
473 *
474 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100476 *
477 * \note The "sig" buffer must be as large as the size
478 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
479 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200480 * \note The hash_id in the RSA context is the one used for the
481 * encoding. md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100482 * that is encoded. According to RFC 3447 it is advised to
483 * keep both hashes the same.
484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100486 int (*f_rng)(void *, unsigned char *, size_t),
487 void *p_rng,
488 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100490 unsigned int hashlen,
491 const unsigned char *hash,
492 unsigned char *sig );
493
494/**
495 * \brief Generic wrapper to perform a PKCS#1 verification using the
496 * mode from the context. Do a public RSA operation and check
497 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 *
499 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200501 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
503 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
504 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000505 * \param hash buffer holding the message digest
506 * \param sig buffer holding the ciphertext
507 *
508 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 *
511 * \note The "sig" buffer must be as large as the size
512 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000513 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200514 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200518 int (*f_rng)(void *, unsigned char *, size_t),
519 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000522 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000523 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200524 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
526/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100527 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
528 *
529 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200531 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
533 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
534 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100535 * \param hash buffer holding the message digest
536 * \param sig buffer holding the ciphertext
537 *
538 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100540 *
541 * \note The "sig" buffer must be as large as the size
542 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
543 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200545 int (*f_rng)(void *, unsigned char *, size_t),
546 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100547 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100549 unsigned int hashlen,
550 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200551 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100552
553/**
554 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200555 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +0100556 *
557 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200559 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
561 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
562 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100563 * \param hash buffer holding the message digest
564 * \param sig buffer holding the ciphertext
565 *
566 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100568 *
569 * \note The "sig" buffer must be as large as the size
570 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
571 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200572 * \note The hash_id in the RSA context is the one used for the
573 * verification. md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200574 * hash that is verified. According to RFC 3447 it is advised to
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200575 * keep both hashes the same. If hash_id in the RSA context is
576 * unset, the md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200579 int (*f_rng)(void *, unsigned char *, size_t),
580 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100581 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100583 unsigned int hashlen,
584 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200585 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100586
587/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200588 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
589 * (This is the version with "full" options.)
590 *
591 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200593 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
595 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
596 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200597 * \param hash buffer holding the message digest
598 * \param mgf1_hash_id message digest used for mask generation
599 * \param expected_salt_len Length of the salt used in padding, use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 * MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200601 * \param sig buffer holding the ciphertext
602 *
603 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 * or an MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200605 *
606 * \note The "sig" buffer must be as large as the size
607 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
608 *
609 * \note The hash_id in the RSA context is ignored.
610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200612 int (*f_rng)(void *, unsigned char *, size_t),
613 void *p_rng,
614 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200616 unsigned int hashlen,
617 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200619 int expected_salt_len,
620 const unsigned char *sig );
621
622/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200623 * \brief Copy the components of an RSA context
624 *
625 * \param dst Destination context
626 * \param src Source context
627 *
628 * \return O on success,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 * MBEDTLS_ERR_MPI_MALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200632
633/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000634 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000635 *
636 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000639
640/**
641 * \brief Checkup routine
642 *
643 * \return 0 if successful, or 1 if the test failed
644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
647#ifdef __cplusplus
648}
649#endif
650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#endif /* MBEDTLS_RSA_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200652
Paul Bakker5121ce52009-01-03 21:22:43 +0000653#endif /* rsa.h */