blob: c06c7d5050c49f6cbe9ed7ea42248d4e904f6d91 [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 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_RSA_H
28#define POLARSSL_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020031#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Paul Bakkered27a042013-04-18 22:46:23 +020035
Paul Bakker314052f2011-08-15 09:07:52 +000036#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020037#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakkerc9965dc2013-09-29 14:58:17 +020039#if defined(POLARSSL_THREADING_C)
40#include "threading.h"
41#endif
42
Paul Bakker13e2dfe2009-07-28 07:18:38 +000043/*
44 * RSA Error codes
45 */
Paul Bakker9d781402011-05-09 16:17:09 +000046#define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
47#define POLARSSL_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
48#define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
49#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the libraries validity check. */
50#define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
51#define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
52#define POLARSSL_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
53#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
54#define POLARSSL_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Paul Bakker5121ce52009-01-03 21:22:43 +000055
56/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020057 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000058 */
Paul Bakker5121ce52009-01-03 21:22:43 +000059#define RSA_PUBLIC 0
60#define RSA_PRIVATE 1
61
62#define RSA_PKCS_V15 0
63#define RSA_PKCS_V21 1
64
65#define RSA_SIGN 1
66#define RSA_CRYPT 2
67
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020068#define RSA_SALT_LEN_ANY -1
69
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020070/*
71 * The above constants may be used even if the RSA module is compile out,
72 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
73 */
74#if defined(POLARSSL_RSA_C)
75
Paul Bakker407a0da2013-06-27 14:29:21 +020076#ifdef __cplusplus
77extern "C" {
78#endif
79
Paul Bakker5121ce52009-01-03 21:22:43 +000080/**
81 * \brief RSA context structure
82 */
83typedef struct
84{
85 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +000086 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +000087
88 mpi N; /*!< public modulus */
89 mpi E; /*!< public exponent */
90
91 mpi D; /*!< private exponent */
92 mpi P; /*!< 1st prime factor */
93 mpi Q; /*!< 2nd prime factor */
94 mpi DP; /*!< D % (P - 1) */
95 mpi DQ; /*!< D % (Q - 1) */
96 mpi QP; /*!< 1 / (Q % P) */
97
98 mpi RN; /*!< cached R^2 mod N */
99 mpi RP; /*!< cached R^2 mod P */
100 mpi RQ; /*!< cached R^2 mod Q */
101
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200102#if !defined(POLARSSL_RSA_NO_CRT)
103 mpi Vi; /*!< cached blinding value */
104 mpi Vf; /*!< cached un-blinding value */
105#endif
106
Paul Bakker9dcc3222011-03-08 14:16:06 +0000107 int padding; /*!< RSA_PKCS_V15 for 1.5 padding and
108 RSA_PKCS_v21 for OAEP/PSS */
109 int hash_id; /*!< Hash identifier of md_type_t as
110 specified in the md.h header file
111 for the EME-OAEP and EMSA-PSS
112 encoding */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200113#if defined(POLARSSL_THREADING_C)
114 threading_mutex_t mutex; /*!< Thread-safety mutex */
115#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000116}
117rsa_context;
118
Paul Bakker5121ce52009-01-03 21:22:43 +0000119/**
120 * \brief Initialize an RSA context
121 *
Paul Bakker9a736322012-11-14 12:39:52 +0000122 * Note: Set padding to RSA_PKCS_V21 for the RSAES-OAEP
123 * encryption scheme and the RSASSA-PSS signature scheme.
124 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 * \param ctx RSA context to be initialized
126 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
127 * \param hash_id RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 *
129 * \note The hash_id parameter is actually ignored
130 * when using RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200131 *
132 * \note Choice of padding mode is strictly enforced for private key
133 * operations, since there might be security concerns in
134 * mixing padding modes. For public key operations it's merely
135 * a default value, which can be overriden by calling specific
136 * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
137 *
138 * \note The chosen hash is always used for OEAP encryption.
139 * For PSS signatures, it's always used for making signatures,
140 * but can be overriden (and always is, if set to
141 * POLARSSL_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 */
143void rsa_init( rsa_context *ctx,
144 int padding,
Paul Bakker42099c32014-01-27 11:45:49 +0100145 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100148 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200149 * See \c rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100150 *
151 * \param ctx RSA context to be set
152 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
153 * \param hash_id RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100154 */
155void rsa_set_padding( rsa_context *ctx, int padding, int hash_id);
156
157/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 * \brief Generate an RSA keypair
159 *
160 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000161 * \param f_rng RNG function
162 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 * \param nbits size of the public key in bits
164 * \param exponent public exponent (e.g., 65537)
165 *
166 * \note rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000167 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 *
Paul Bakker40e46942009-01-03 21:51:57 +0000169 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 */
Paul Bakker21eb2802010-08-16 11:10:02 +0000171int rsa_gen_key( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000172 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000173 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000174 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000175
176/**
177 * \brief Check a public RSA key
178 *
179 * \param ctx RSA context to be checked
180 *
Paul Bakker40e46942009-01-03 21:51:57 +0000181 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000183int rsa_check_pubkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
185/**
186 * \brief Check a private RSA key
187 *
188 * \param ctx RSA context to be checked
189 *
Paul Bakker40e46942009-01-03 21:51:57 +0000190 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000192int rsa_check_privkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
194/**
195 * \brief Do an RSA public key operation
196 *
197 * \param ctx RSA context
198 * \param input input buffer
199 * \param output output buffer
200 *
Paul Bakker40e46942009-01-03 21:51:57 +0000201 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 *
203 * \note This function does NOT take care of message
Paul Bakker619467a2009-03-28 23:26:51 +0000204 * padding. Also, be sure to set input[0] = 0 or assure that
205 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 *
207 * \note The input and output buffers must be large
208 * enough (eg. 128 bytes if RSA-1024 is used).
209 */
210int rsa_public( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000211 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 unsigned char *output );
213
214/**
215 * \brief Do an RSA private key operation
216 *
217 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200218 * \param f_rng RNG function (Needed for blinding)
219 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 * \param input input buffer
221 * \param output output buffer
222 *
Paul Bakker40e46942009-01-03 21:51:57 +0000223 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 *
225 * \note The input and output buffers must be large
226 * enough (eg. 128 bytes if RSA-1024 is used).
227 */
228int rsa_private( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200229 int (*f_rng)(void *, unsigned char *, size_t),
230 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000231 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 unsigned char *output );
233
234/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100235 * \brief Generic wrapper to perform a PKCS#1 encryption using the
236 * mode from the context. Add the message padding, then do an
237 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 *
239 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200240 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
241 * and RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000242 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000244 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 * \param input buffer holding the data to be encrypted
246 * \param output buffer that will hold the ciphertext
247 *
Paul Bakker40e46942009-01-03 21:51:57 +0000248 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 *
250 * \note The output buffer must be as large as the size
251 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
252 */
253int rsa_pkcs1_encrypt( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000254 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000255 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000256 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000257 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 unsigned char *output );
259
260/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100261 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
262 *
263 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200264 * \param f_rng RNG function (Needed for padding and RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100265 * \param p_rng RNG parameter
266 * \param mode RSA_PUBLIC or RSA_PRIVATE
267 * \param ilen contains the plaintext length
268 * \param input buffer holding the data to be encrypted
269 * \param output buffer that will hold the ciphertext
270 *
271 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
272 *
273 * \note The output buffer must be as large as the size
274 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
275 */
276int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx,
277 int (*f_rng)(void *, unsigned char *, size_t),
278 void *p_rng,
279 int mode, size_t ilen,
280 const unsigned char *input,
281 unsigned char *output );
282
283/**
284 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
285 *
286 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200287 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
288 * and RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100289 * \param p_rng RNG parameter
290 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100291 * \param label buffer holding the custom label to use
292 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100293 * \param ilen contains the plaintext length
294 * \param input buffer holding the data to be encrypted
295 * \param output buffer that will hold the ciphertext
296 *
297 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
298 *
299 * \note The output buffer must be as large as the size
300 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
301 */
302int rsa_rsaes_oaep_encrypt( rsa_context *ctx,
303 int (*f_rng)(void *, unsigned char *, size_t),
304 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100305 int mode,
306 const unsigned char *label, size_t label_len,
307 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100308 const unsigned char *input,
309 unsigned char *output );
310
311/**
312 * \brief Generic wrapper to perform a PKCS#1 decryption using the
313 * mode from the context. Do an RSA operation, then remove
314 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 *
316 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200317 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200318 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000320 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 * \param input buffer holding the encrypted data
322 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000323 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 *
Paul Bakker40e46942009-01-03 21:51:57 +0000325 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 *
327 * \note The output buffer must be as large as the size
Paul Bakker060c5682009-01-12 21:48:39 +0000328 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
329 * an error is thrown.
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 */
331int rsa_pkcs1_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200332 int (*f_rng)(void *, unsigned char *, size_t),
333 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000334 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000335 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000336 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000337 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000338
339/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100340 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
341 *
342 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200343 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200344 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100345 * \param mode RSA_PUBLIC or RSA_PRIVATE
346 * \param olen will contain the plaintext length
347 * \param input buffer holding the encrypted data
348 * \param output buffer that will hold the plaintext
349 * \param output_max_len maximum length of the output buffer
350 *
351 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
352 *
353 * \note The output buffer must be as large as the size
354 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
355 * an error is thrown.
356 */
357int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200358 int (*f_rng)(void *, unsigned char *, size_t),
359 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100360 int mode, size_t *olen,
361 const unsigned char *input,
362 unsigned char *output,
363 size_t output_max_len );
364
365/**
366 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
367 *
368 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200369 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200370 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100371 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100372 * \param label buffer holding the custom label to use
373 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100374 * \param olen will contain the plaintext length
375 * \param input buffer holding the encrypted data
376 * \param output buffer that will hold the plaintext
377 * \param output_max_len maximum length of the output buffer
378 *
379 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
380 *
381 * \note The output buffer must be as large as the size
382 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
383 * an error is thrown.
384 */
385int rsa_rsaes_oaep_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200386 int (*f_rng)(void *, unsigned char *, size_t),
387 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100388 int mode,
389 const unsigned char *label, size_t label_len,
390 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100391 const unsigned char *input,
392 unsigned char *output,
393 size_t output_max_len );
394
395/**
396 * \brief Generic wrapper to perform a PKCS#1 signature using the
397 * mode from the context. Do a private RSA operation to sign
398 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000399 *
400 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200401 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
402 * RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000403 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000404 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200405 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
406 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 * \param hash buffer holding the message digest
408 * \param sig buffer that will hold the ciphertext
409 *
410 * \return 0 if the signing operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000411 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 *
413 * \note The "sig" buffer must be as large as the size
414 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000415 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200416 * \note In case of PKCS#1 v2.1 encoding, see comments on
417 * \note \c rsa_rsassa_pss_sign() for details on md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 */
419int rsa_pkcs1_sign( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000420 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000421 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200423 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000424 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000425 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000426 unsigned char *sig );
427
428/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100429 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
430 *
431 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200432 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200433 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100434 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200435 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
436 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100437 * \param hash buffer holding the message digest
438 * \param sig buffer that will hold the ciphertext
439 *
440 * \return 0 if the signing operation was successful,
441 * or an POLARSSL_ERR_RSA_XXX error code
442 *
443 * \note The "sig" buffer must be as large as the size
444 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
445 */
446int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200447 int (*f_rng)(void *, unsigned char *, size_t),
448 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100449 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200450 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100451 unsigned int hashlen,
452 const unsigned char *hash,
453 unsigned char *sig );
454
455/**
456 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
457 *
458 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200459 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
460 * RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100461 * \param p_rng RNG parameter
462 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200463 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
464 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100465 * \param hash buffer holding the message digest
466 * \param sig buffer that will hold the ciphertext
467 *
468 * \return 0 if the signing operation was successful,
469 * or an POLARSSL_ERR_RSA_XXX error code
470 *
471 * \note The "sig" buffer must be as large as the size
472 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
473 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200474 * \note The hash_id in the RSA context is the one used for the
475 * encoding. md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100476 * that is encoded. According to RFC 3447 it is advised to
477 * keep both hashes the same.
478 */
479int rsa_rsassa_pss_sign( rsa_context *ctx,
480 int (*f_rng)(void *, unsigned char *, size_t),
481 void *p_rng,
482 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200483 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100484 unsigned int hashlen,
485 const unsigned char *hash,
486 unsigned char *sig );
487
488/**
489 * \brief Generic wrapper to perform a PKCS#1 verification using the
490 * mode from the context. Do a public RSA operation and check
491 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000492 *
493 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200494 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200495 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000496 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200497 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
498 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 * \param hash buffer holding the message digest
500 * \param sig buffer holding the ciphertext
501 *
502 * \return 0 if the verify operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000503 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 *
505 * \note The "sig" buffer must be as large as the size
506 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000507 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200508 * \note In case of PKCS#1 v2.1 encoding, see comments on
509 * \c rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 */
511int rsa_pkcs1_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200512 int (*f_rng)(void *, unsigned char *, size_t),
513 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200515 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000516 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000517 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200518 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
520/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100521 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
522 *
523 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200524 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200525 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100526 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200527 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
528 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100529 * \param hash buffer holding the message digest
530 * \param sig buffer holding the ciphertext
531 *
532 * \return 0 if the verify operation was successful,
533 * or an POLARSSL_ERR_RSA_XXX error code
534 *
535 * \note The "sig" buffer must be as large as the size
536 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
537 */
538int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200539 int (*f_rng)(void *, unsigned char *, size_t),
540 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100541 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200542 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100543 unsigned int hashlen,
544 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200545 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100546
547/**
548 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200549 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +0100550 *
551 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200552 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200553 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100554 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200555 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
556 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100557 * \param hash buffer holding the message digest
558 * \param sig buffer holding the ciphertext
559 *
560 * \return 0 if the verify operation was successful,
561 * or an POLARSSL_ERR_RSA_XXX error code
562 *
563 * \note The "sig" buffer must be as large as the size
564 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
565 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200566 * \note The hash_id in the RSA context is the one used for the
567 * verification. md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200568 * hash that is verified. According to RFC 3447 it is advised to
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200569 * keep both hashes the same. If hash_id in the RSA context is
570 * unset, the md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100571 */
572int rsa_rsassa_pss_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200573 int (*f_rng)(void *, unsigned char *, size_t),
574 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100575 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200576 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100577 unsigned int hashlen,
578 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200579 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100580
581/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200582 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
583 * (This is the version with "full" options.)
584 *
585 * \param ctx points to an RSA public key
586 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
587 * \param p_rng RNG parameter
588 * \param mode RSA_PUBLIC or RSA_PRIVATE
589 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
590 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
591 * \param hash buffer holding the message digest
592 * \param mgf1_hash_id message digest used for mask generation
593 * \param expected_salt_len Length of the salt used in padding, use
594 * RSA_SALT_LEN_ANY to accept any salt length
595 * \param sig buffer holding the ciphertext
596 *
597 * \return 0 if the verify operation was successful,
598 * or an POLARSSL_ERR_RSA_XXX error code
599 *
600 * \note The "sig" buffer must be as large as the size
601 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
602 *
603 * \note The hash_id in the RSA context is ignored.
604 */
605int rsa_rsassa_pss_verify_ext( rsa_context *ctx,
606 int (*f_rng)(void *, unsigned char *, size_t),
607 void *p_rng,
608 int mode,
609 md_type_t md_alg,
610 unsigned int hashlen,
611 const unsigned char *hash,
612 md_type_t mgf1_hash_id,
613 int expected_salt_len,
614 const unsigned char *sig );
615
616/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200617 * \brief Copy the components of an RSA context
618 *
619 * \param dst Destination context
620 * \param src Source context
621 *
622 * \return O on success,
623 * POLARSSL_ERR_MPI_MALLOC_FAILED on memory allocation failure
624 */
625int rsa_copy( rsa_context *dst, const rsa_context *src );
626
627/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000628 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000629 *
630 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000631 */
632void rsa_free( rsa_context *ctx );
633
634/**
635 * \brief Checkup routine
636 *
637 * \return 0 if successful, or 1 if the test failed
638 */
639int rsa_self_test( int verbose );
640
641#ifdef __cplusplus
642}
643#endif
644
Paul Bakkered27a042013-04-18 22:46:23 +0200645#endif /* POLARSSL_RSA_C */
646
Paul Bakker5121ce52009-01-03 21:22:43 +0000647#endif /* rsa.h */