blob: 90cfd552a527cda7fa7fa6863c6e5f34ab35b71b [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 Bakker407a0da2013-06-27 14:29:21 +02006 * Copyright (C) 2006-2013, 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
Paul Bakkered27a042013-04-18 22:46:23 +020030#include "config.h"
31
Paul Bakker314052f2011-08-15 09:07:52 +000032#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020033#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Paul Bakker13e2dfe2009-07-28 07:18:38 +000035/*
36 * RSA Error codes
37 */
Paul Bakker9d781402011-05-09 16:17:09 +000038#define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
39#define POLARSSL_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
40#define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
41#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the libraries validity check. */
42#define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
43#define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
44#define POLARSSL_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
45#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
46#define POLARSSL_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Paul Bakker5121ce52009-01-03 21:22:43 +000047
48/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020049 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000050 */
Paul Bakker5121ce52009-01-03 21:22:43 +000051#define RSA_PUBLIC 0
52#define RSA_PRIVATE 1
53
54#define RSA_PKCS_V15 0
55#define RSA_PKCS_V21 1
56
57#define RSA_SIGN 1
58#define RSA_CRYPT 2
59
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020060/*
61 * The above constants may be used even if the RSA module is compile out,
62 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
63 */
64#if defined(POLARSSL_RSA_C)
65
Paul Bakker407a0da2013-06-27 14:29:21 +020066#ifdef __cplusplus
67extern "C" {
68#endif
69
Paul Bakker5121ce52009-01-03 21:22:43 +000070/**
71 * \brief RSA context structure
72 */
73typedef struct
74{
75 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +000076 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +000077
78 mpi N; /*!< public modulus */
79 mpi E; /*!< public exponent */
80
81 mpi D; /*!< private exponent */
82 mpi P; /*!< 1st prime factor */
83 mpi Q; /*!< 2nd prime factor */
84 mpi DP; /*!< D % (P - 1) */
85 mpi DQ; /*!< D % (Q - 1) */
86 mpi QP; /*!< 1 / (Q % P) */
87
88 mpi RN; /*!< cached R^2 mod N */
89 mpi RP; /*!< cached R^2 mod P */
90 mpi RQ; /*!< cached R^2 mod Q */
91
Paul Bakker9dcc3222011-03-08 14:16:06 +000092 int padding; /*!< RSA_PKCS_V15 for 1.5 padding and
93 RSA_PKCS_v21 for OAEP/PSS */
94 int hash_id; /*!< Hash identifier of md_type_t as
95 specified in the md.h header file
96 for the EME-OAEP and EMSA-PSS
97 encoding */
Paul Bakker5121ce52009-01-03 21:22:43 +000098}
99rsa_context;
100
Paul Bakker5121ce52009-01-03 21:22:43 +0000101/**
102 * \brief Initialize an RSA context
103 *
Paul Bakker9a736322012-11-14 12:39:52 +0000104 * Note: Set padding to RSA_PKCS_V21 for the RSAES-OAEP
105 * encryption scheme and the RSASSA-PSS signature scheme.
106 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 * \param ctx RSA context to be initialized
108 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
109 * \param hash_id RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 *
111 * \note The hash_id parameter is actually ignored
112 * when using RSA_PKCS_V15 padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 */
114void rsa_init( rsa_context *ctx,
115 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000116 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
118/**
119 * \brief Generate an RSA keypair
120 *
121 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000122 * \param f_rng RNG function
123 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 * \param nbits size of the public key in bits
125 * \param exponent public exponent (e.g., 65537)
126 *
127 * \note rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000128 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 *
Paul Bakker40e46942009-01-03 21:51:57 +0000130 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 */
Paul Bakker21eb2802010-08-16 11:10:02 +0000132int rsa_gen_key( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000133 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000134 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000135 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
137/**
138 * \brief Check a public RSA key
139 *
140 * \param ctx RSA context to be checked
141 *
Paul Bakker40e46942009-01-03 21:51:57 +0000142 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000144int rsa_check_pubkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
146/**
147 * \brief Check a private RSA key
148 *
149 * \param ctx RSA context to be checked
150 *
Paul Bakker40e46942009-01-03 21:51:57 +0000151 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000153int rsa_check_privkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
155/**
156 * \brief Do an RSA public key operation
157 *
158 * \param ctx RSA context
159 * \param input input buffer
160 * \param output output buffer
161 *
Paul Bakker40e46942009-01-03 21:51:57 +0000162 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 *
164 * \note This function does NOT take care of message
Paul Bakker619467a2009-03-28 23:26:51 +0000165 * padding. Also, be sure to set input[0] = 0 or assure that
166 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 *
168 * \note The input and output buffers must be large
169 * enough (eg. 128 bytes if RSA-1024 is used).
170 */
171int rsa_public( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000172 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 unsigned char *output );
174
175/**
176 * \brief Do an RSA private key operation
177 *
178 * \param ctx RSA context
179 * \param input input buffer
180 * \param output output buffer
181 *
Paul Bakker40e46942009-01-03 21:51:57 +0000182 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 *
184 * \note The input and output buffers must be large
185 * enough (eg. 128 bytes if RSA-1024 is used).
186 */
187int rsa_private( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000188 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 unsigned char *output );
190
191/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100192 * \brief Generic wrapper to perform a PKCS#1 encryption using the
193 * mode from the context. Add the message padding, then do an
194 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 *
196 * \param ctx RSA context
Paul Bakker9dcc3222011-03-08 14:16:06 +0000197 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding)
Paul Bakker21eb2802010-08-16 11:10:02 +0000198 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000200 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 * \param input buffer holding the data to be encrypted
202 * \param output buffer that will hold the ciphertext
203 *
Paul Bakker40e46942009-01-03 21:51:57 +0000204 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 *
206 * \note The output buffer must be as large as the size
207 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
208 */
209int rsa_pkcs1_encrypt( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000210 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000211 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000212 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000213 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 unsigned char *output );
215
216/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100217 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
218 *
219 * \param ctx RSA context
220 * \param f_rng RNG function (Needed for padding)
221 * \param p_rng RNG parameter
222 * \param mode RSA_PUBLIC or RSA_PRIVATE
223 * \param ilen contains the plaintext length
224 * \param input buffer holding the data to be encrypted
225 * \param output buffer that will hold the ciphertext
226 *
227 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
228 *
229 * \note The output buffer must be as large as the size
230 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
231 */
232int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx,
233 int (*f_rng)(void *, unsigned char *, size_t),
234 void *p_rng,
235 int mode, size_t ilen,
236 const unsigned char *input,
237 unsigned char *output );
238
239/**
240 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
241 *
242 * \param ctx RSA context
243 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding)
244 * \param p_rng RNG parameter
245 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100246 * \param label buffer holding the custom label to use
247 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100248 * \param ilen contains the plaintext length
249 * \param input buffer holding the data to be encrypted
250 * \param output buffer that will hold the ciphertext
251 *
252 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
253 *
254 * \note The output buffer must be as large as the size
255 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
256 */
257int rsa_rsaes_oaep_encrypt( rsa_context *ctx,
258 int (*f_rng)(void *, unsigned char *, size_t),
259 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100260 int mode,
261 const unsigned char *label, size_t label_len,
262 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100263 const unsigned char *input,
264 unsigned char *output );
265
266/**
267 * \brief Generic wrapper to perform a PKCS#1 decryption using the
268 * mode from the context. Do an RSA operation, then remove
269 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 *
271 * \param ctx RSA context
272 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000273 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 * \param input buffer holding the encrypted data
275 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000276 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 *
Paul Bakker40e46942009-01-03 21:51:57 +0000278 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 *
280 * \note The output buffer must be as large as the size
Paul Bakker060c5682009-01-12 21:48:39 +0000281 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
282 * an error is thrown.
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 */
284int rsa_pkcs1_decrypt( rsa_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000285 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000286 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000287 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000288 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
290/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100291 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
292 *
293 * \param ctx RSA context
294 * \param mode RSA_PUBLIC or RSA_PRIVATE
295 * \param olen will contain the plaintext length
296 * \param input buffer holding the encrypted data
297 * \param output buffer that will hold the plaintext
298 * \param output_max_len maximum length of the output buffer
299 *
300 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
301 *
302 * \note The output buffer must be as large as the size
303 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
304 * an error is thrown.
305 */
306int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx,
307 int mode, size_t *olen,
308 const unsigned char *input,
309 unsigned char *output,
310 size_t output_max_len );
311
312/**
313 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
314 *
315 * \param ctx RSA context
316 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100317 * \param label buffer holding the custom label to use
318 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100319 * \param olen will contain the plaintext length
320 * \param input buffer holding the encrypted data
321 * \param output buffer that will hold the plaintext
322 * \param output_max_len maximum length of the output buffer
323 *
324 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
325 *
326 * \note The output buffer must be as large as the size
327 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
328 * an error is thrown.
329 */
330int rsa_rsaes_oaep_decrypt( rsa_context *ctx,
Paul Bakkera43231c2013-02-28 17:33:49 +0100331 int mode,
332 const unsigned char *label, size_t label_len,
333 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100334 const unsigned char *input,
335 unsigned char *output,
336 size_t output_max_len );
337
338/**
339 * \brief Generic wrapper to perform a PKCS#1 signature using the
340 * mode from the context. Do a private RSA operation to sign
341 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 *
343 * \param ctx RSA context
Paul Bakker9dcc3222011-03-08 14:16:06 +0000344 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding)
345 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000346 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200347 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
348 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000349 * \param hash buffer holding the message digest
350 * \param sig buffer that will hold the ciphertext
351 *
352 * \return 0 if the signing operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000353 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 *
355 * \note The "sig" buffer must be as large as the size
356 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000357 *
358 * \note In case of PKCS#1 v2.1 encoding keep in mind that
359 * the hash_id in the RSA context is the one used for the
360 * encoding. hash_id in the function call is the type of hash
361 * that is encoded. According to RFC 3447 it is advised to
362 * keep both hashes the same.
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 */
364int rsa_pkcs1_sign( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000365 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000366 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000367 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200368 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000369 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000370 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000371 unsigned char *sig );
372
373/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100374 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
375 *
376 * \param ctx RSA context
377 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200378 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
379 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100380 * \param hash buffer holding the message digest
381 * \param sig buffer that will hold the ciphertext
382 *
383 * \return 0 if the signing operation was successful,
384 * or an POLARSSL_ERR_RSA_XXX error code
385 *
386 * \note The "sig" buffer must be as large as the size
387 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
388 */
389int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx,
390 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200391 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100392 unsigned int hashlen,
393 const unsigned char *hash,
394 unsigned char *sig );
395
396/**
397 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
398 *
399 * \param ctx RSA context
400 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding)
401 * \param p_rng RNG parameter
402 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200403 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
404 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100405 * \param hash buffer holding the message digest
406 * \param sig buffer that will hold the ciphertext
407 *
408 * \return 0 if the signing operation was successful,
409 * or an POLARSSL_ERR_RSA_XXX error code
410 *
411 * \note The "sig" buffer must be as large as the size
412 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
413 *
414 * \note In case of PKCS#1 v2.1 encoding keep in mind that
415 * the hash_id in the RSA context is the one used for the
416 * encoding. hash_id in the function call is the type of hash
417 * that is encoded. According to RFC 3447 it is advised to
418 * keep both hashes the same.
419 */
420int rsa_rsassa_pss_sign( rsa_context *ctx,
421 int (*f_rng)(void *, unsigned char *, size_t),
422 void *p_rng,
423 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200424 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100425 unsigned int hashlen,
426 const unsigned char *hash,
427 unsigned char *sig );
428
429/**
430 * \brief Generic wrapper to perform a PKCS#1 verification using the
431 * mode from the context. Do a public RSA operation and check
432 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 *
434 * \param ctx points to an RSA public key
435 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200436 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
437 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 * \param hash buffer holding the message digest
439 * \param sig buffer holding the ciphertext
440 *
441 * \return 0 if the verify operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000442 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000443 *
444 * \note The "sig" buffer must be as large as the size
445 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000446 *
447 * \note In case of PKCS#1 v2.1 encoding keep in mind that
448 * the hash_id in the RSA context is the one used for the
449 * verification. hash_id in the function call is the type of hash
450 * that is verified. According to RFC 3447 it is advised to
451 * keep both hashes the same.
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 */
453int rsa_pkcs1_verify( rsa_context *ctx,
454 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200455 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000456 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000457 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200458 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000459
460/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100461 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
462 *
463 * \param ctx points to an RSA public key
464 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200465 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
466 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100467 * \param hash buffer holding the message digest
468 * \param sig buffer holding the ciphertext
469 *
470 * \return 0 if the verify operation was successful,
471 * or an POLARSSL_ERR_RSA_XXX error code
472 *
473 * \note The "sig" buffer must be as large as the size
474 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
475 */
476int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx,
477 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200478 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100479 unsigned int hashlen,
480 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200481 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100482
483/**
484 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
485 * \brief Do a public RSA and check the message digest
486 *
487 * \param ctx points to an RSA public key
488 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200489 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
490 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100491 * \param hash buffer holding the message digest
492 * \param sig buffer holding the ciphertext
493 *
494 * \return 0 if the verify operation was successful,
495 * or an POLARSSL_ERR_RSA_XXX error code
496 *
497 * \note The "sig" buffer must be as large as the size
498 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
499 *
500 * \note In case of PKCS#1 v2.1 encoding keep in mind that
501 * the hash_id in the RSA context is the one used for the
502 * verification. hash_id in the function call is the type of hash
503 * that is verified. According to RFC 3447 it is advised to
504 * keep both hashes the same.
505 */
506int rsa_rsassa_pss_verify( rsa_context *ctx,
507 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200508 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100509 unsigned int hashlen,
510 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200511 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100512
513/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200514 * \brief Copy the components of an RSA context
515 *
516 * \param dst Destination context
517 * \param src Source context
518 *
519 * \return O on success,
520 * POLARSSL_ERR_MPI_MALLOC_FAILED on memory allocation failure
521 */
522int rsa_copy( rsa_context *dst, const rsa_context *src );
523
524/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000525 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000526 *
527 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 */
529void rsa_free( rsa_context *ctx );
530
531/**
532 * \brief Checkup routine
533 *
534 * \return 0 if successful, or 1 if the test failed
535 */
536int rsa_self_test( int verbose );
537
538#ifdef __cplusplus
539}
540#endif
541
Paul Bakkered27a042013-04-18 22:46:23 +0200542#endif /* POLARSSL_RSA_C */
543
Paul Bakker5121ce52009-01-03 21:22:43 +0000544#endif /* rsa.h */