blob: 8e52e7d1e948dacf6963e0e30826c95d3fdf5fbe [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
32#if defined(POLARSSL_RSA_C)
33
Paul Bakker314052f2011-08-15 09:07:52 +000034#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020035#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker13e2dfe2009-07-28 07:18:38 +000037/*
38 * RSA Error codes
39 */
Paul Bakker9d781402011-05-09 16:17:09 +000040#define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
41#define POLARSSL_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
42#define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
43#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the libraries validity check. */
44#define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
45#define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
46#define POLARSSL_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
47#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
48#define POLARSSL_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Paul Bakker5121ce52009-01-03 21:22:43 +000049
50/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020051 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000052 */
Paul Bakker5121ce52009-01-03 21:22:43 +000053#define RSA_PUBLIC 0
54#define RSA_PRIVATE 1
55
56#define RSA_PKCS_V15 0
57#define RSA_PKCS_V21 1
58
59#define RSA_SIGN 1
60#define RSA_CRYPT 2
61
Paul Bakker407a0da2013-06-27 14:29:21 +020062#ifdef __cplusplus
63extern "C" {
64#endif
65
Paul Bakker5121ce52009-01-03 21:22:43 +000066/**
67 * \brief RSA context structure
68 */
69typedef struct
70{
71 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +000072 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +000073
74 mpi N; /*!< public modulus */
75 mpi E; /*!< public exponent */
76
77 mpi D; /*!< private exponent */
78 mpi P; /*!< 1st prime factor */
79 mpi Q; /*!< 2nd prime factor */
80 mpi DP; /*!< D % (P - 1) */
81 mpi DQ; /*!< D % (Q - 1) */
82 mpi QP; /*!< 1 / (Q % P) */
83
84 mpi RN; /*!< cached R^2 mod N */
85 mpi RP; /*!< cached R^2 mod P */
86 mpi RQ; /*!< cached R^2 mod Q */
87
Paul Bakker9dcc3222011-03-08 14:16:06 +000088 int padding; /*!< RSA_PKCS_V15 for 1.5 padding and
89 RSA_PKCS_v21 for OAEP/PSS */
90 int hash_id; /*!< Hash identifier of md_type_t as
91 specified in the md.h header file
92 for the EME-OAEP and EMSA-PSS
93 encoding */
Paul Bakker5121ce52009-01-03 21:22:43 +000094}
95rsa_context;
96
Paul Bakker5121ce52009-01-03 21:22:43 +000097/**
98 * \brief Initialize an RSA context
99 *
Paul Bakker9a736322012-11-14 12:39:52 +0000100 * Note: Set padding to RSA_PKCS_V21 for the RSAES-OAEP
101 * encryption scheme and the RSASSA-PSS signature scheme.
102 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 * \param ctx RSA context to be initialized
104 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
105 * \param hash_id RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 *
107 * \note The hash_id parameter is actually ignored
108 * when using RSA_PKCS_V15 padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 */
110void rsa_init( rsa_context *ctx,
111 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000112 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
114/**
115 * \brief Generate an RSA keypair
116 *
117 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000118 * \param f_rng RNG function
119 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 * \param nbits size of the public key in bits
121 * \param exponent public exponent (e.g., 65537)
122 *
123 * \note rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000124 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 *
Paul Bakker40e46942009-01-03 21:51:57 +0000126 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 */
Paul Bakker21eb2802010-08-16 11:10:02 +0000128int rsa_gen_key( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000129 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000130 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000131 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
133/**
134 * \brief Check a public RSA key
135 *
136 * \param ctx RSA context to be checked
137 *
Paul Bakker40e46942009-01-03 21:51:57 +0000138 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000140int rsa_check_pubkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
142/**
143 * \brief Check a private RSA key
144 *
145 * \param ctx RSA context to be checked
146 *
Paul Bakker40e46942009-01-03 21:51:57 +0000147 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000149int rsa_check_privkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151/**
152 * \brief Do an RSA public key operation
153 *
154 * \param ctx RSA context
155 * \param input input buffer
156 * \param output output buffer
157 *
Paul Bakker40e46942009-01-03 21:51:57 +0000158 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 *
160 * \note This function does NOT take care of message
Paul Bakker619467a2009-03-28 23:26:51 +0000161 * padding. Also, be sure to set input[0] = 0 or assure that
162 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 *
164 * \note The input and output buffers must be large
165 * enough (eg. 128 bytes if RSA-1024 is used).
166 */
167int rsa_public( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000168 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 unsigned char *output );
170
171/**
172 * \brief Do an RSA private key operation
173 *
174 * \param ctx RSA context
175 * \param input input buffer
176 * \param output output buffer
177 *
Paul Bakker40e46942009-01-03 21:51:57 +0000178 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 *
180 * \note The input and output buffers must be large
181 * enough (eg. 128 bytes if RSA-1024 is used).
182 */
183int rsa_private( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000184 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 unsigned char *output );
186
187/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100188 * \brief Generic wrapper to perform a PKCS#1 encryption using the
189 * mode from the context. Add the message padding, then do an
190 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 *
192 * \param ctx RSA context
Paul Bakker9dcc3222011-03-08 14:16:06 +0000193 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding)
Paul Bakker21eb2802010-08-16 11:10:02 +0000194 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000196 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 * \param input buffer holding the data to be encrypted
198 * \param output buffer that will hold the ciphertext
199 *
Paul Bakker40e46942009-01-03 21:51:57 +0000200 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 *
202 * \note The output buffer must be as large as the size
203 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
204 */
205int rsa_pkcs1_encrypt( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000206 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000207 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000208 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000209 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 unsigned char *output );
211
212/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100213 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
214 *
215 * \param ctx RSA context
216 * \param f_rng RNG function (Needed for padding)
217 * \param p_rng RNG parameter
218 * \param mode RSA_PUBLIC or RSA_PRIVATE
219 * \param ilen contains the plaintext length
220 * \param input buffer holding the data to be encrypted
221 * \param output buffer that will hold the ciphertext
222 *
223 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
224 *
225 * \note The output buffer must be as large as the size
226 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
227 */
228int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx,
229 int (*f_rng)(void *, unsigned char *, size_t),
230 void *p_rng,
231 int mode, size_t ilen,
232 const unsigned char *input,
233 unsigned char *output );
234
235/**
236 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
237 *
238 * \param ctx RSA context
239 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding)
240 * \param p_rng RNG parameter
241 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100242 * \param label buffer holding the custom label to use
243 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100244 * \param ilen contains the plaintext length
245 * \param input buffer holding the data to be encrypted
246 * \param output buffer that will hold the ciphertext
247 *
248 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
249 *
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_rsaes_oaep_encrypt( rsa_context *ctx,
254 int (*f_rng)(void *, unsigned char *, size_t),
255 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100256 int mode,
257 const unsigned char *label, size_t label_len,
258 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100259 const unsigned char *input,
260 unsigned char *output );
261
262/**
263 * \brief Generic wrapper to perform a PKCS#1 decryption using the
264 * mode from the context. Do an RSA operation, then remove
265 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 *
267 * \param ctx RSA context
268 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000269 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 * \param input buffer holding the encrypted data
271 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000272 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 *
Paul Bakker40e46942009-01-03 21:51:57 +0000274 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 *
276 * \note The output buffer must be as large as the size
Paul Bakker060c5682009-01-12 21:48:39 +0000277 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
278 * an error is thrown.
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 */
280int rsa_pkcs1_decrypt( rsa_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000281 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000282 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000283 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000284 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000285
286/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100287 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
288 *
289 * \param ctx RSA context
290 * \param mode RSA_PUBLIC or RSA_PRIVATE
291 * \param olen will contain the plaintext length
292 * \param input buffer holding the encrypted data
293 * \param output buffer that will hold the plaintext
294 * \param output_max_len maximum length of the output buffer
295 *
296 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
297 *
298 * \note The output buffer must be as large as the size
299 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
300 * an error is thrown.
301 */
302int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx,
303 int mode, size_t *olen,
304 const unsigned char *input,
305 unsigned char *output,
306 size_t output_max_len );
307
308/**
309 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
310 *
311 * \param ctx RSA context
312 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100313 * \param label buffer holding the custom label to use
314 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100315 * \param olen will contain the plaintext length
316 * \param input buffer holding the encrypted data
317 * \param output buffer that will hold the plaintext
318 * \param output_max_len maximum length of the output buffer
319 *
320 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
321 *
322 * \note The output buffer must be as large as the size
323 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
324 * an error is thrown.
325 */
326int rsa_rsaes_oaep_decrypt( rsa_context *ctx,
Paul Bakkera43231c2013-02-28 17:33:49 +0100327 int mode,
328 const unsigned char *label, size_t label_len,
329 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100330 const unsigned char *input,
331 unsigned char *output,
332 size_t output_max_len );
333
334/**
335 * \brief Generic wrapper to perform a PKCS#1 signature using the
336 * mode from the context. Do a private RSA operation to sign
337 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 *
339 * \param ctx RSA context
Paul Bakker9dcc3222011-03-08 14:16:06 +0000340 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding)
341 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200343 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
344 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 * \param hash buffer holding the message digest
346 * \param sig buffer that will hold the ciphertext
347 *
348 * \return 0 if the signing operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000349 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 *
351 * \note The "sig" buffer must be as large as the size
352 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000353 *
354 * \note In case of PKCS#1 v2.1 encoding keep in mind that
355 * the hash_id in the RSA context is the one used for the
356 * encoding. hash_id in the function call is the type of hash
357 * that is encoded. According to RFC 3447 it is advised to
358 * keep both hashes the same.
Paul Bakker5121ce52009-01-03 21:22:43 +0000359 */
360int rsa_pkcs1_sign( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000361 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000362 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200364 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000365 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000366 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000367 unsigned char *sig );
368
369/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100370 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
371 *
372 * \param ctx RSA context
373 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200374 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
375 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100376 * \param hash buffer holding the message digest
377 * \param sig buffer that will hold the ciphertext
378 *
379 * \return 0 if the signing operation was successful,
380 * or an POLARSSL_ERR_RSA_XXX error code
381 *
382 * \note The "sig" buffer must be as large as the size
383 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
384 */
385int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx,
386 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200387 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100388 unsigned int hashlen,
389 const unsigned char *hash,
390 unsigned char *sig );
391
392/**
393 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
394 *
395 * \param ctx RSA context
396 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding)
397 * \param p_rng RNG parameter
398 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200399 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
400 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100401 * \param hash buffer holding the message digest
402 * \param sig buffer that will hold the ciphertext
403 *
404 * \return 0 if the signing operation was successful,
405 * or an POLARSSL_ERR_RSA_XXX error code
406 *
407 * \note The "sig" buffer must be as large as the size
408 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
409 *
410 * \note In case of PKCS#1 v2.1 encoding keep in mind that
411 * the hash_id in the RSA context is the one used for the
412 * encoding. hash_id in the function call is the type of hash
413 * that is encoded. According to RFC 3447 it is advised to
414 * keep both hashes the same.
415 */
416int rsa_rsassa_pss_sign( rsa_context *ctx,
417 int (*f_rng)(void *, unsigned char *, size_t),
418 void *p_rng,
419 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200420 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100421 unsigned int hashlen,
422 const unsigned char *hash,
423 unsigned char *sig );
424
425/**
426 * \brief Generic wrapper to perform a PKCS#1 verification using the
427 * mode from the context. Do a public RSA operation and check
428 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 *
430 * \param ctx points to an RSA public key
431 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200432 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
433 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 * \param hash buffer holding the message digest
435 * \param sig buffer holding the ciphertext
436 *
437 * \return 0 if the verify operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000438 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 *
440 * \note The "sig" buffer must be as large as the size
441 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000442 *
443 * \note In case of PKCS#1 v2.1 encoding keep in mind that
444 * the hash_id in the RSA context is the one used for the
445 * verification. hash_id in the function call is the type of hash
446 * that is verified. According to RFC 3447 it is advised to
447 * keep both hashes the same.
Paul Bakker5121ce52009-01-03 21:22:43 +0000448 */
449int rsa_pkcs1_verify( rsa_context *ctx,
450 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200451 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000452 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000453 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200454 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000455
456/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100457 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
458 *
459 * \param ctx points to an RSA public key
460 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200461 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
462 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100463 * \param hash buffer holding the message digest
464 * \param sig buffer holding the ciphertext
465 *
466 * \return 0 if the verify operation was successful,
467 * or an POLARSSL_ERR_RSA_XXX error code
468 *
469 * \note The "sig" buffer must be as large as the size
470 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
471 */
472int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx,
473 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200474 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100475 unsigned int hashlen,
476 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200477 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100478
479/**
480 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
481 * \brief Do a public RSA and check the message digest
482 *
483 * \param ctx points to an RSA public key
484 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200485 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
486 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100487 * \param hash buffer holding the message digest
488 * \param sig buffer holding the ciphertext
489 *
490 * \return 0 if the verify operation was successful,
491 * or an POLARSSL_ERR_RSA_XXX error code
492 *
493 * \note The "sig" buffer must be as large as the size
494 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
495 *
496 * \note In case of PKCS#1 v2.1 encoding keep in mind that
497 * the hash_id in the RSA context is the one used for the
498 * verification. hash_id in the function call is the type of hash
499 * that is verified. According to RFC 3447 it is advised to
500 * keep both hashes the same.
501 */
502int rsa_rsassa_pss_verify( rsa_context *ctx,
503 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200504 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100505 unsigned int hashlen,
506 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200507 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100508
509/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200510 * \brief Copy the components of an RSA context
511 *
512 * \param dst Destination context
513 * \param src Source context
514 *
515 * \return O on success,
516 * POLARSSL_ERR_MPI_MALLOC_FAILED on memory allocation failure
517 */
518int rsa_copy( rsa_context *dst, const rsa_context *src );
519
520/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000522 *
523 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 */
525void rsa_free( rsa_context *ctx );
526
527/**
528 * \brief Checkup routine
529 *
530 * \return 0 if successful, or 1 if the test failed
531 */
532int rsa_self_test( int verbose );
533
534#ifdef __cplusplus
535}
536#endif
537
Paul Bakkered27a042013-04-18 22:46:23 +0200538#endif /* POLARSSL_RSA_C */
539
Paul Bakker5121ce52009-01-03 21:22:43 +0000540#endif /* rsa.h */