blob: 5c2d875c2d6dfe7e399ce9f8aa534ed899d53f4b [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 *
Simon Butcher573bb962017-07-19 01:58:47 +0100339 * \note The output buffer length \c output_max_len should be
340 * as large as the size ctx->len of ctx->N (eg. 128 bytes
341 * if RSA-1024 is used) to be able to hold an arbitrary
342 * decrypted message. If it is not large enough to hold
343 * the decryption of the particular ciphertext provided,
344 * the function will return POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE.
345 *
346 * \note The input buffer must be as large as the size
347 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 */
349int rsa_pkcs1_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200350 int (*f_rng)(void *, unsigned char *, size_t),
351 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000352 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000353 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000354 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000355 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000356
357/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100358 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200359 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakkerb3869132013-02-28 17:21:01 +0100360 *
361 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200362 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200363 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100364 * \param mode RSA_PUBLIC or RSA_PRIVATE
365 * \param olen will contain the plaintext length
366 * \param input buffer holding the encrypted data
367 * \param output buffer that will hold the plaintext
368 * \param output_max_len maximum length of the output buffer
369 *
370 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
371 *
Simon Butcher573bb962017-07-19 01:58:47 +0100372 * \note The output buffer length \c output_max_len should be
373 * as large as the size ctx->len of ctx->N (eg. 128 bytes
374 * if RSA-1024 is used) to be able to hold an arbitrary
375 * decrypted message. If it is not large enough to hold
376 * the decryption of the particular ciphertext provided,
377 * the function will return POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE.
378 *
379 * \note The input buffer must be as large as the size
380 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Hanno Becker1af21bf2017-05-11 16:33:02 +0100381 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100382 */
383int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200384 int (*f_rng)(void *, unsigned char *, size_t),
385 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100386 int mode, size_t *olen,
387 const unsigned char *input,
388 unsigned char *output,
389 size_t output_max_len );
390
391/**
392 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200393 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakkerb3869132013-02-28 17:21:01 +0100394 *
395 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200396 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200397 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100398 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100399 * \param label buffer holding the custom label to use
400 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100401 * \param olen will contain the plaintext length
402 * \param input buffer holding the encrypted data
403 * \param output buffer that will hold the plaintext
404 * \param output_max_len maximum length of the output buffer
405 *
406 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
407 *
Simon Butcher573bb962017-07-19 01:58:47 +0100408 * \note The output buffer length \c output_max_len should be
409 * as large as the size ctx->len of ctx->N (eg. 128 bytes
410 * if RSA-1024 is used) to be able to hold an arbitrary
411 * decrypted message. If it is not large enough to hold
412 * the decryption of the particular ciphertext provided,
413 * the function will return POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE.
414 *
Hanno Becker1af21bf2017-05-11 16:33:02 +0100415 * \note The input buffer must be as large as the size
Simon Butcher573bb962017-07-19 01:58:47 +0100416 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Hanno Becker1af21bf2017-05-11 16:33:02 +0100417 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100418 */
419int rsa_rsaes_oaep_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200420 int (*f_rng)(void *, unsigned char *, size_t),
421 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100422 int mode,
423 const unsigned char *label, size_t label_len,
424 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100425 const unsigned char *input,
426 unsigned char *output,
427 size_t output_max_len );
428
429/**
430 * \brief Generic wrapper to perform a PKCS#1 signature using the
431 * mode from the context. Do a private RSA operation to sign
432 * a message digest
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200433 * (Thread-safe if POLARSSL_THREADING_C is enabled)
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
437 * RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000438 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200440 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
441 * \param hashlen message digest length (for POLARSSL_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,
Paul Bakker40e46942009-01-03 21:51:57 +0000446 * or an POLARSSL_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
452 * \note \c rsa_rsassa_pss_sign() for details on md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 */
454int rsa_pkcs1_sign( 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,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200458 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
Paul Bakkerf451bac2013-08-30 15:37:02 +0200467 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200468 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100469 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200470 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
471 * \param hashlen message digest length (for POLARSSL_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,
476 * or an POLARSSL_ERR_RSA_XXX error code
477 *
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 */
481int rsa_rsassa_pkcs1_v15_sign( 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,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200485 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)
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200492 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakkerb3869132013-02-28 17:21:01 +0100493 *
494 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200495 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
496 * RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100497 * \param p_rng RNG parameter
498 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200499 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
500 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100501 * \param hash buffer holding the message digest
502 * \param sig buffer that will hold the ciphertext
503 *
504 * \return 0 if the signing operation was successful,
505 * or an POLARSSL_ERR_RSA_XXX error code
506 *
507 * \note The "sig" buffer must be as large as the size
508 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
509 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200510 * \note The hash_id in the RSA context is the one used for the
511 * encoding. md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100512 * that is encoded. According to RFC 3447 it is advised to
513 * keep both hashes the same.
514 */
515int rsa_rsassa_pss_sign( rsa_context *ctx,
516 int (*f_rng)(void *, unsigned char *, size_t),
517 void *p_rng,
518 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200519 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100520 unsigned int hashlen,
521 const unsigned char *hash,
522 unsigned char *sig );
523
524/**
525 * \brief Generic wrapper to perform a PKCS#1 verification using the
526 * mode from the context. Do a public RSA operation and check
527 * the message digest
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200528 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 *
530 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200531 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200532 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200534 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
535 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 * \param hash buffer holding the message digest
537 * \param sig buffer holding the ciphertext
538 *
539 * \return 0 if the verify operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000540 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000541 *
542 * \note The "sig" buffer must be as large as the size
543 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000544 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200545 * \note In case of PKCS#1 v2.1 encoding, see comments on
546 * \c rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 */
548int rsa_pkcs1_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200549 int (*f_rng)(void *, unsigned char *, size_t),
550 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200552 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000553 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000554 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200555 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
557/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100558 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200559 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakkerb3869132013-02-28 17:21:01 +0100560 *
561 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200562 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200563 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100564 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200565 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
566 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100567 * \param hash buffer holding the message digest
568 * \param sig buffer holding the ciphertext
569 *
570 * \return 0 if the verify operation was successful,
571 * or an POLARSSL_ERR_RSA_XXX error code
572 *
573 * \note The "sig" buffer must be as large as the size
574 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
575 */
576int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200577 int (*f_rng)(void *, unsigned char *, size_t),
578 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100579 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200580 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100581 unsigned int hashlen,
582 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200583 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100584
585/**
586 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200587 * (This is the "simple" version.)
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200588 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakkerb3869132013-02-28 17:21:01 +0100589 *
590 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200591 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200592 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100593 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200594 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
595 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100596 * \param hash buffer holding the message digest
597 * \param sig buffer holding the ciphertext
598 *
599 * \return 0 if the verify operation was successful,
600 * or an POLARSSL_ERR_RSA_XXX error code
601 *
602 * \note The "sig" buffer must be as large as the size
603 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
604 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200605 * \note The hash_id in the RSA context is the one used for the
606 * verification. md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200607 * hash that is verified. According to RFC 3447 it is advised to
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200608 * keep both hashes the same. If hash_id in the RSA context is
609 * unset, the md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100610 */
611int rsa_rsassa_pss_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200612 int (*f_rng)(void *, unsigned char *, size_t),
613 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100614 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200615 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100616 unsigned int hashlen,
617 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200618 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100619
620/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200621 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
622 * (This is the version with "full" options.)
Manuel Pégourié-Gonnard55745462015-07-03 17:51:10 +0200623 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200624 *
625 * \param ctx points to an RSA public key
626 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
627 * \param p_rng RNG parameter
628 * \param mode RSA_PUBLIC or RSA_PRIVATE
629 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
630 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
631 * \param hash buffer holding the message digest
632 * \param mgf1_hash_id message digest used for mask generation
633 * \param expected_salt_len Length of the salt used in padding, use
634 * RSA_SALT_LEN_ANY to accept any salt length
635 * \param sig buffer holding the ciphertext
636 *
637 * \return 0 if the verify operation was successful,
638 * or an POLARSSL_ERR_RSA_XXX error code
639 *
640 * \note The "sig" buffer must be as large as the size
641 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
642 *
643 * \note The hash_id in the RSA context is ignored.
644 */
645int rsa_rsassa_pss_verify_ext( rsa_context *ctx,
646 int (*f_rng)(void *, unsigned char *, size_t),
647 void *p_rng,
648 int mode,
649 md_type_t md_alg,
650 unsigned int hashlen,
651 const unsigned char *hash,
652 md_type_t mgf1_hash_id,
653 int expected_salt_len,
654 const unsigned char *sig );
655
656/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200657 * \brief Copy the components of an RSA context
658 *
659 * \param dst Destination context
660 * \param src Source context
661 *
662 * \return O on success,
663 * POLARSSL_ERR_MPI_MALLOC_FAILED on memory allocation failure
664 */
665int rsa_copy( rsa_context *dst, const rsa_context *src );
666
667/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000669 *
670 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 */
672void rsa_free( rsa_context *ctx );
673
674/**
675 * \brief Checkup routine
676 *
677 * \return 0 if successful, or 1 if the test failed
678 */
679int rsa_self_test( int verbose );
680
681#ifdef __cplusplus
682}
683#endif
684
Paul Bakkered27a042013-04-18 22:46:23 +0200685#endif /* POLARSSL_RSA_C */
686
Paul Bakker5121ce52009-01-03 21:22:43 +0000687#endif /* rsa.h */