blob: e80bb4da08a2476b224cf38343038708577c5ecb [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 */
Paul Bakker40e46942009-01-03 21:51:57 +000024#ifndef POLARSSL_RSA_H
25#define POLARSSL_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#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
Paul Bakkerc9965dc2013-09-29 14:58:17 +020036#if defined(POLARSSL_THREADING_C)
37#include "threading.h"
38#endif
39
Paul Bakker13e2dfe2009-07-28 07:18:38 +000040/*
41 * RSA Error codes
42 */
Paul Bakker9d781402011-05-09 16:17:09 +000043#define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
44#define POLARSSL_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
45#define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Nicholas Wilsond0fa5cc2015-05-11 10:39:49 +010046#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the library's validity check. */
Paul Bakker9d781402011-05-09 16:17:09 +000047#define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
48#define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
49#define POLARSSL_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
50#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
51#define POLARSSL_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 */
Paul Bakker5121ce52009-01-03 21:22:43 +000056#define RSA_PUBLIC 0
57#define RSA_PRIVATE 1
58
59#define RSA_PKCS_V15 0
60#define RSA_PKCS_V21 1
61
62#define RSA_SIGN 1
63#define RSA_CRYPT 2
64
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020065#define RSA_SALT_LEN_ANY -1
66
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 */
71#if defined(POLARSSL_RSA_C)
72
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
85 mpi N; /*!< public modulus */
86 mpi E; /*!< public exponent */
87
88 mpi D; /*!< private exponent */
89 mpi P; /*!< 1st prime factor */
90 mpi Q; /*!< 2nd prime factor */
91 mpi DP; /*!< D % (P - 1) */
92 mpi DQ; /*!< D % (Q - 1) */
93 mpi QP; /*!< 1 / (Q % P) */
94
95 mpi RN; /*!< cached R^2 mod N */
96 mpi RP; /*!< cached R^2 mod P */
97 mpi RQ; /*!< cached R^2 mod Q */
98
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +020099 mpi Vi; /*!< cached blinding value */
100 mpi Vf; /*!< cached un-blinding value */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200101
Paul Bakker9dcc3222011-03-08 14:16:06 +0000102 int padding; /*!< RSA_PKCS_V15 for 1.5 padding and
103 RSA_PKCS_v21 for OAEP/PSS */
104 int hash_id; /*!< Hash identifier of md_type_t as
105 specified in the md.h header file
106 for the EME-OAEP and EMSA-PSS
107 encoding */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200108#if defined(POLARSSL_THREADING_C)
109 threading_mutex_t mutex; /*!< Thread-safety mutex */
110#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000111}
112rsa_context;
113
Paul Bakker5121ce52009-01-03 21:22:43 +0000114/**
115 * \brief Initialize an RSA context
116 *
Paul Bakker9a736322012-11-14 12:39:52 +0000117 * Note: Set padding to RSA_PKCS_V21 for the RSAES-OAEP
118 * encryption scheme and the RSASSA-PSS signature scheme.
119 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 * \param ctx RSA context to be initialized
121 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
122 * \param hash_id RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 *
124 * \note The hash_id parameter is actually ignored
125 * when using 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
136 * POLARSSL_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 */
138void rsa_init( rsa_context *ctx,
139 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é-Gonnarde6d1d822014-06-02 16:47:02 +0200144 * See \c rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100145 *
146 * \param ctx RSA context to be set
147 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
148 * \param hash_id RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100149 */
150void rsa_set_padding( rsa_context *ctx, int padding, int hash_id);
151
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 *
161 * \note 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 *
Paul Bakker40e46942009-01-03 21:51:57 +0000164 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 */
Paul Bakker21eb2802010-08-16 11:10:02 +0000166int rsa_gen_key( 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 *
Paul Bakker40e46942009-01-03 21:51:57 +0000176 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000178int rsa_check_pubkey( const 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 *
Paul Bakker40e46942009-01-03 21:51:57 +0000185 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000187int rsa_check_privkey( const 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 *
196 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
197 */
198int rsa_check_pub_priv( const rsa_context *pub, const rsa_context *prv );
199
200/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 * \brief Do an RSA public key operation
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200202 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 *
204 * \param ctx RSA context
205 * \param input input buffer
206 * \param output output buffer
207 *
Paul Bakker40e46942009-01-03 21:51:57 +0000208 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 *
210 * \note This function does NOT take care of message
Paul Bakker619467a2009-03-28 23:26:51 +0000211 * padding. Also, be sure to set input[0] = 0 or assure that
212 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 *
214 * \note The input and output buffers must be large
215 * enough (eg. 128 bytes if RSA-1024 is used).
216 */
217int rsa_public( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000218 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 unsigned char *output );
220
221/**
222 * \brief Do an RSA private key operation
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200223 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 *
225 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200226 * \param f_rng RNG function (Needed for blinding)
227 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 * \param input input buffer
229 * \param output output buffer
230 *
Paul Bakker40e46942009-01-03 21:51:57 +0000231 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 *
233 * \note The input and output buffers must be large
234 * enough (eg. 128 bytes if RSA-1024 is used).
235 */
236int rsa_private( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200237 int (*f_rng)(void *, unsigned char *, size_t),
238 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000239 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 unsigned char *output );
241
242/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100243 * \brief Generic wrapper to perform a PKCS#1 encryption using the
244 * mode from the context. Add the message padding, then do an
245 * RSA operation.
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200246 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 *
248 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200249 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
250 * and RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000251 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000253 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 * \param input buffer holding the data to be encrypted
255 * \param output buffer that will hold the ciphertext
256 *
Paul Bakker40e46942009-01-03 21:51:57 +0000257 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 *
259 * \note The output buffer must be as large as the size
260 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
261 */
262int rsa_pkcs1_encrypt( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000263 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000264 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000265 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000266 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 unsigned char *output );
268
269/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100270 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200271 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakkerb3869132013-02-28 17:21:01 +0100272 *
273 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200274 * \param f_rng RNG function (Needed for padding and RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100275 * \param p_rng RNG parameter
276 * \param mode RSA_PUBLIC or RSA_PRIVATE
277 * \param ilen contains the plaintext length
278 * \param input buffer holding the data to be encrypted
279 * \param output buffer that will hold the ciphertext
280 *
281 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
282 *
283 * \note The output buffer must be as large as the size
284 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
285 */
286int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx,
287 int (*f_rng)(void *, unsigned char *, size_t),
288 void *p_rng,
289 int mode, size_t ilen,
290 const unsigned char *input,
291 unsigned char *output );
292
293/**
294 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200295 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakkerb3869132013-02-28 17:21:01 +0100296 *
297 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200298 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
299 * and RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100300 * \param p_rng RNG parameter
301 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100302 * \param label buffer holding the custom label to use
303 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100304 * \param ilen contains the plaintext length
305 * \param input buffer holding the data to be encrypted
306 * \param output buffer that will hold the ciphertext
307 *
308 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
309 *
310 * \note The output buffer must be as large as the size
311 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
312 */
313int rsa_rsaes_oaep_encrypt( rsa_context *ctx,
314 int (*f_rng)(void *, unsigned char *, size_t),
315 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100316 int mode,
317 const unsigned char *label, size_t label_len,
318 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100319 const unsigned char *input,
320 unsigned char *output );
321
322/**
323 * \brief Generic wrapper to perform a PKCS#1 decryption using the
324 * mode from the context. Do an RSA operation, then remove
325 * the message padding
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200326 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 *
328 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200329 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200330 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000332 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 * \param input buffer holding the encrypted data
334 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000335 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 *
Paul Bakker40e46942009-01-03 21:51:57 +0000337 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 *
339 * \note The output buffer must be as large as the size
Paul Bakker060c5682009-01-12 21:48:39 +0000340 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
341 * an error is thrown.
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 */
343int rsa_pkcs1_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200344 int (*f_rng)(void *, unsigned char *, size_t),
345 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000346 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000347 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000348 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000349 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350
351/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100352 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200353 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakkerb3869132013-02-28 17:21:01 +0100354 *
355 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200356 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200357 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100358 * \param mode RSA_PUBLIC or RSA_PRIVATE
359 * \param olen will contain the plaintext length
360 * \param input buffer holding the encrypted data
361 * \param output buffer that will hold the plaintext
362 * \param output_max_len maximum length of the output buffer
363 *
364 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
365 *
366 * \note The output buffer must be as large as the size
367 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
368 * an error is thrown.
369 */
370int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200371 int (*f_rng)(void *, unsigned char *, size_t),
372 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100373 int mode, size_t *olen,
374 const unsigned char *input,
375 unsigned char *output,
376 size_t output_max_len );
377
378/**
379 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200380 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakkerb3869132013-02-28 17:21:01 +0100381 *
382 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200383 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200384 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100385 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100386 * \param label buffer holding the custom label to use
387 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100388 * \param olen will contain the plaintext length
389 * \param input buffer holding the encrypted data
390 * \param output buffer that will hold the plaintext
391 * \param output_max_len maximum length of the output buffer
392 *
393 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
394 *
395 * \note The output buffer must be as large as the size
396 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
397 * an error is thrown.
398 */
399int rsa_rsaes_oaep_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200400 int (*f_rng)(void *, unsigned char *, size_t),
401 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100402 int mode,
403 const unsigned char *label, size_t label_len,
404 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100405 const unsigned char *input,
406 unsigned char *output,
407 size_t output_max_len );
408
409/**
410 * \brief Generic wrapper to perform a PKCS#1 signature using the
411 * mode from the context. Do a private RSA operation to sign
412 * a message digest
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200413 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker5121ce52009-01-03 21:22:43 +0000414 *
415 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200416 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
417 * RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000418 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000419 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200420 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
421 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 * \param hash buffer holding the message digest
423 * \param sig buffer that will hold the ciphertext
424 *
425 * \return 0 if the signing operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000426 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 *
428 * \note The "sig" buffer must be as large as the size
429 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000430 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200431 * \note In case of PKCS#1 v2.1 encoding, see comments on
432 * \note \c rsa_rsassa_pss_sign() for details on md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 */
434int rsa_pkcs1_sign( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000435 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000436 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000437 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200438 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000439 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000440 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000441 unsigned char *sig );
442
443/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100444 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
445 *
446 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200447 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200448 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100449 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200450 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
451 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100452 * \param hash buffer holding the message digest
453 * \param sig buffer that will hold the ciphertext
454 *
455 * \return 0 if the signing operation was successful,
456 * or an POLARSSL_ERR_RSA_XXX error code
457 *
458 * \note The "sig" buffer must be as large as the size
459 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
460 */
461int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200462 int (*f_rng)(void *, unsigned char *, size_t),
463 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100464 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200465 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100466 unsigned int hashlen,
467 const unsigned char *hash,
468 unsigned char *sig );
469
470/**
471 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200472 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakkerb3869132013-02-28 17:21:01 +0100473 *
474 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200475 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
476 * RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100477 * \param p_rng RNG parameter
478 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200479 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
480 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100481 * \param hash buffer holding the message digest
482 * \param sig buffer that will hold the ciphertext
483 *
484 * \return 0 if the signing operation was successful,
485 * or an POLARSSL_ERR_RSA_XXX error code
486 *
487 * \note The "sig" buffer must be as large as the size
488 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
489 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200490 * \note The hash_id in the RSA context is the one used for the
491 * encoding. md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100492 * that is encoded. According to RFC 3447 it is advised to
493 * keep both hashes the same.
494 */
495int rsa_rsassa_pss_sign( rsa_context *ctx,
496 int (*f_rng)(void *, unsigned char *, size_t),
497 void *p_rng,
498 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200499 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100500 unsigned int hashlen,
501 const unsigned char *hash,
502 unsigned char *sig );
503
504/**
505 * \brief Generic wrapper to perform a PKCS#1 verification using the
506 * mode from the context. Do a public RSA operation and check
507 * the message digest
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200508 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 *
510 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200511 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200512 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200514 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
515 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 * \param hash buffer holding the message digest
517 * \param sig buffer holding the ciphertext
518 *
519 * \return 0 if the verify operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000520 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 *
522 * \note The "sig" buffer must be as large as the size
523 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000524 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200525 * \note In case of PKCS#1 v2.1 encoding, see comments on
526 * \c rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000527 */
528int rsa_pkcs1_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200529 int (*f_rng)(void *, unsigned char *, size_t),
530 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200532 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000533 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000534 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200535 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000536
537/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100538 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200539 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakkerb3869132013-02-28 17:21:01 +0100540 *
541 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200542 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200543 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100544 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200545 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
546 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100547 * \param hash buffer holding the message digest
548 * \param sig buffer holding the ciphertext
549 *
550 * \return 0 if the verify operation was successful,
551 * or an POLARSSL_ERR_RSA_XXX error code
552 *
553 * \note The "sig" buffer must be as large as the size
554 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
555 */
556int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200557 int (*f_rng)(void *, unsigned char *, size_t),
558 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100559 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200560 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100561 unsigned int hashlen,
562 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200563 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100564
565/**
566 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200567 * (This is the "simple" version.)
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200568 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakkerb3869132013-02-28 17:21:01 +0100569 *
570 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200571 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200572 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100573 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200574 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
575 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100576 * \param hash buffer holding the message digest
577 * \param sig buffer holding the ciphertext
578 *
579 * \return 0 if the verify operation was successful,
580 * or an POLARSSL_ERR_RSA_XXX error code
581 *
582 * \note The "sig" buffer must be as large as the size
583 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
584 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200585 * \note The hash_id in the RSA context is the one used for the
586 * verification. md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200587 * hash that is verified. According to RFC 3447 it is advised to
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200588 * keep both hashes the same. If hash_id in the RSA context is
589 * unset, the md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100590 */
591int rsa_rsassa_pss_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200592 int (*f_rng)(void *, unsigned char *, size_t),
593 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100594 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200595 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100596 unsigned int hashlen,
597 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200598 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100599
600/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200601 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
602 * (This is the version with "full" options.)
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200603 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200604 *
605 * \param ctx points to an RSA public key
606 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
607 * \param p_rng RNG parameter
608 * \param mode RSA_PUBLIC or RSA_PRIVATE
609 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
610 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
611 * \param hash buffer holding the message digest
612 * \param mgf1_hash_id message digest used for mask generation
613 * \param expected_salt_len Length of the salt used in padding, use
614 * RSA_SALT_LEN_ANY to accept any salt length
615 * \param sig buffer holding the ciphertext
616 *
617 * \return 0 if the verify operation was successful,
618 * or an POLARSSL_ERR_RSA_XXX error code
619 *
620 * \note The "sig" buffer must be as large as the size
621 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
622 *
623 * \note The hash_id in the RSA context is ignored.
624 */
625int rsa_rsassa_pss_verify_ext( rsa_context *ctx,
626 int (*f_rng)(void *, unsigned char *, size_t),
627 void *p_rng,
628 int mode,
629 md_type_t md_alg,
630 unsigned int hashlen,
631 const unsigned char *hash,
632 md_type_t mgf1_hash_id,
633 int expected_salt_len,
634 const unsigned char *sig );
635
636/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200637 * \brief Copy the components of an RSA context
638 *
639 * \param dst Destination context
640 * \param src Source context
641 *
642 * \return O on success,
643 * POLARSSL_ERR_MPI_MALLOC_FAILED on memory allocation failure
644 */
645int rsa_copy( rsa_context *dst, const rsa_context *src );
646
647/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000648 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000649 *
650 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000651 */
652void rsa_free( rsa_context *ctx );
653
654/**
655 * \brief Checkup routine
656 *
657 * \return 0 if successful, or 1 if the test failed
658 */
659int rsa_self_test( int verbose );
660
661#ifdef __cplusplus
662}
663#endif
664
Paul Bakkered27a042013-04-18 22:46:23 +0200665#endif /* POLARSSL_RSA_C */
666
Paul Bakker5121ce52009-01-03 21:22:43 +0000667#endif /* rsa.h */