blob: 8342eecb650784824bb5dcc9494f3f6db4933446 [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 Bakker84f12b72010-07-18 10:13:04 +00006 * Copyright (C) 2006-2010, 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 Bakker314052f2011-08-15 09:07:52 +000030#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020031#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Paul Bakker13e2dfe2009-07-28 07:18:38 +000033/*
34 * RSA Error codes
35 */
Paul Bakker9d781402011-05-09 16:17:09 +000036#define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
37#define POLARSSL_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
38#define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
39#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the libraries validity check. */
40#define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
41#define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
42#define POLARSSL_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
43#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
44#define POLARSSL_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Paul Bakker5121ce52009-01-03 21:22:43 +000045
46/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020047 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000048 */
Paul Bakker5121ce52009-01-03 21:22:43 +000049#define RSA_PUBLIC 0
50#define RSA_PRIVATE 1
51
52#define RSA_PKCS_V15 0
53#define RSA_PKCS_V21 1
54
55#define RSA_SIGN 1
56#define RSA_CRYPT 2
57
Paul Bakker5121ce52009-01-03 21:22:43 +000058/**
59 * \brief RSA context structure
60 */
61typedef struct
62{
63 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +000064 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +000065
66 mpi N; /*!< public modulus */
67 mpi E; /*!< public exponent */
68
69 mpi D; /*!< private exponent */
70 mpi P; /*!< 1st prime factor */
71 mpi Q; /*!< 2nd prime factor */
72 mpi DP; /*!< D % (P - 1) */
73 mpi DQ; /*!< D % (Q - 1) */
74 mpi QP; /*!< 1 / (Q % P) */
75
76 mpi RN; /*!< cached R^2 mod N */
77 mpi RP; /*!< cached R^2 mod P */
78 mpi RQ; /*!< cached R^2 mod Q */
79
Paul Bakker9dcc3222011-03-08 14:16:06 +000080 int padding; /*!< RSA_PKCS_V15 for 1.5 padding and
81 RSA_PKCS_v21 for OAEP/PSS */
82 int hash_id; /*!< Hash identifier of md_type_t as
83 specified in the md.h header file
84 for the EME-OAEP and EMSA-PSS
85 encoding */
Paul Bakker5121ce52009-01-03 21:22:43 +000086}
87rsa_context;
88
89#ifdef __cplusplus
90extern "C" {
91#endif
92
93/**
94 * \brief Initialize an RSA context
95 *
Paul Bakker9a736322012-11-14 12:39:52 +000096 * Note: Set padding to RSA_PKCS_V21 for the RSAES-OAEP
97 * encryption scheme and the RSASSA-PSS signature scheme.
98 *
Paul Bakker5121ce52009-01-03 21:22:43 +000099 * \param ctx RSA context to be initialized
100 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
101 * \param hash_id RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 *
103 * \note The hash_id parameter is actually ignored
104 * when using RSA_PKCS_V15 padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 */
106void rsa_init( rsa_context *ctx,
107 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000108 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
110/**
111 * \brief Generate an RSA keypair
112 *
113 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000114 * \param f_rng RNG function
115 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 * \param nbits size of the public key in bits
117 * \param exponent public exponent (e.g., 65537)
118 *
119 * \note rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000120 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 *
Paul Bakker40e46942009-01-03 21:51:57 +0000122 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 */
Paul Bakker21eb2802010-08-16 11:10:02 +0000124int rsa_gen_key( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000125 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000126 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000127 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
129/**
130 * \brief Check a public RSA key
131 *
132 * \param ctx RSA context to be checked
133 *
Paul Bakker40e46942009-01-03 21:51:57 +0000134 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000136int rsa_check_pubkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
138/**
139 * \brief Check a private RSA key
140 *
141 * \param ctx RSA context to be checked
142 *
Paul Bakker40e46942009-01-03 21:51:57 +0000143 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000145int rsa_check_privkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147/**
148 * \brief Do an RSA public key operation
149 *
150 * \param ctx RSA context
151 * \param input input buffer
152 * \param output output buffer
153 *
Paul Bakker40e46942009-01-03 21:51:57 +0000154 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 *
156 * \note This function does NOT take care of message
Paul Bakker619467a2009-03-28 23:26:51 +0000157 * padding. Also, be sure to set input[0] = 0 or assure that
158 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 *
160 * \note The input and output buffers must be large
161 * enough (eg. 128 bytes if RSA-1024 is used).
162 */
163int rsa_public( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000164 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 unsigned char *output );
166
167/**
168 * \brief Do an RSA private key operation
169 *
170 * \param ctx RSA context
171 * \param input input buffer
172 * \param output output buffer
173 *
Paul Bakker40e46942009-01-03 21:51:57 +0000174 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 *
176 * \note The input and output buffers must be large
177 * enough (eg. 128 bytes if RSA-1024 is used).
178 */
179int rsa_private( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000180 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 unsigned char *output );
182
183/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100184 * \brief Generic wrapper to perform a PKCS#1 encryption using the
185 * mode from the context. Add the message padding, then do an
186 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 *
188 * \param ctx RSA context
Paul Bakker9dcc3222011-03-08 14:16:06 +0000189 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding)
Paul Bakker21eb2802010-08-16 11:10:02 +0000190 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000192 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 * \param input buffer holding the data to be encrypted
194 * \param output buffer that will hold the ciphertext
195 *
Paul Bakker40e46942009-01-03 21:51:57 +0000196 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 *
198 * \note The output buffer must be as large as the size
199 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
200 */
201int rsa_pkcs1_encrypt( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000202 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000203 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000204 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000205 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 unsigned char *output );
207
208/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100209 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
210 *
211 * \param ctx RSA context
212 * \param f_rng RNG function (Needed for padding)
213 * \param p_rng RNG parameter
214 * \param mode RSA_PUBLIC or RSA_PRIVATE
215 * \param ilen contains the plaintext length
216 * \param input buffer holding the data to be encrypted
217 * \param output buffer that will hold the ciphertext
218 *
219 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
220 *
221 * \note The output buffer must be as large as the size
222 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
223 */
224int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx,
225 int (*f_rng)(void *, unsigned char *, size_t),
226 void *p_rng,
227 int mode, size_t ilen,
228 const unsigned char *input,
229 unsigned char *output );
230
231/**
232 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
233 *
234 * \param ctx RSA context
235 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding)
236 * \param p_rng RNG parameter
237 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100238 * \param label buffer holding the custom label to use
239 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100240 * \param ilen contains the plaintext length
241 * \param input buffer holding the data to be encrypted
242 * \param output buffer that will hold the ciphertext
243 *
244 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
245 *
246 * \note The output buffer must be as large as the size
247 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
248 */
249int rsa_rsaes_oaep_encrypt( rsa_context *ctx,
250 int (*f_rng)(void *, unsigned char *, size_t),
251 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100252 int mode,
253 const unsigned char *label, size_t label_len,
254 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100255 const unsigned char *input,
256 unsigned char *output );
257
258/**
259 * \brief Generic wrapper to perform a PKCS#1 decryption using the
260 * mode from the context. Do an RSA operation, then remove
261 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 *
263 * \param ctx RSA context
264 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000265 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 * \param input buffer holding the encrypted data
267 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000268 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 *
Paul Bakker40e46942009-01-03 21:51:57 +0000270 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 *
272 * \note The output buffer must be as large as the size
Paul Bakker060c5682009-01-12 21:48:39 +0000273 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
274 * an error is thrown.
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 */
276int rsa_pkcs1_decrypt( rsa_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000277 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000278 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000279 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000280 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000281
282/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100283 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
284 *
285 * \param ctx RSA context
286 * \param mode RSA_PUBLIC or RSA_PRIVATE
287 * \param olen will contain the plaintext length
288 * \param input buffer holding the encrypted data
289 * \param output buffer that will hold the plaintext
290 * \param output_max_len maximum length of the output buffer
291 *
292 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
293 *
294 * \note The output buffer must be as large as the size
295 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
296 * an error is thrown.
297 */
298int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx,
299 int mode, size_t *olen,
300 const unsigned char *input,
301 unsigned char *output,
302 size_t output_max_len );
303
304/**
305 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
306 *
307 * \param ctx RSA context
308 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100309 * \param label buffer holding the custom label to use
310 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100311 * \param olen will contain the plaintext length
312 * \param input buffer holding the encrypted data
313 * \param output buffer that will hold the plaintext
314 * \param output_max_len maximum length of the output buffer
315 *
316 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
317 *
318 * \note The output buffer must be as large as the size
319 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
320 * an error is thrown.
321 */
322int rsa_rsaes_oaep_decrypt( rsa_context *ctx,
Paul Bakkera43231c2013-02-28 17:33:49 +0100323 int mode,
324 const unsigned char *label, size_t label_len,
325 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100326 const unsigned char *input,
327 unsigned char *output,
328 size_t output_max_len );
329
330/**
331 * \brief Generic wrapper to perform a PKCS#1 signature using the
332 * mode from the context. Do a private RSA operation to sign
333 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 *
335 * \param ctx RSA context
Paul Bakker9dcc3222011-03-08 14:16:06 +0000336 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding)
337 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200339 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
340 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000341 * \param hash buffer holding the message digest
342 * \param sig buffer that will hold the ciphertext
343 *
344 * \return 0 if the signing operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000345 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000346 *
347 * \note The "sig" buffer must be as large as the size
348 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000349 *
350 * \note In case of PKCS#1 v2.1 encoding keep in mind that
351 * the hash_id in the RSA context is the one used for the
352 * encoding. hash_id in the function call is the type of hash
353 * that is encoded. According to RFC 3447 it is advised to
354 * keep both hashes the same.
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 */
356int rsa_pkcs1_sign( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000357 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000358 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000359 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200360 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000361 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000362 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 unsigned char *sig );
364
365/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100366 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
367 *
368 * \param ctx RSA context
369 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200370 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
371 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100372 * \param hash buffer holding the message digest
373 * \param sig buffer that will hold the ciphertext
374 *
375 * \return 0 if the signing operation was successful,
376 * or an POLARSSL_ERR_RSA_XXX error code
377 *
378 * \note The "sig" buffer must be as large as the size
379 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
380 */
381int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx,
382 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200383 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100384 unsigned int hashlen,
385 const unsigned char *hash,
386 unsigned char *sig );
387
388/**
389 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
390 *
391 * \param ctx RSA context
392 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding)
393 * \param p_rng RNG parameter
394 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200395 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
396 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100397 * \param hash buffer holding the message digest
398 * \param sig buffer that will hold the ciphertext
399 *
400 * \return 0 if the signing operation was successful,
401 * or an POLARSSL_ERR_RSA_XXX error code
402 *
403 * \note The "sig" buffer must be as large as the size
404 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
405 *
406 * \note In case of PKCS#1 v2.1 encoding keep in mind that
407 * the hash_id in the RSA context is the one used for the
408 * encoding. hash_id in the function call is the type of hash
409 * that is encoded. According to RFC 3447 it is advised to
410 * keep both hashes the same.
411 */
412int rsa_rsassa_pss_sign( rsa_context *ctx,
413 int (*f_rng)(void *, unsigned char *, size_t),
414 void *p_rng,
415 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200416 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100417 unsigned int hashlen,
418 const unsigned char *hash,
419 unsigned char *sig );
420
421/**
422 * \brief Generic wrapper to perform a PKCS#1 verification using the
423 * mode from the context. Do a public RSA operation and check
424 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 *
426 * \param ctx points to an RSA public key
427 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200428 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
429 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 * \param hash buffer holding the message digest
431 * \param sig buffer holding the ciphertext
432 *
433 * \return 0 if the verify operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000434 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 *
436 * \note The "sig" buffer must be as large as the size
437 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000438 *
439 * \note In case of PKCS#1 v2.1 encoding keep in mind that
440 * the hash_id in the RSA context is the one used for the
441 * verification. hash_id in the function call is the type of hash
442 * that is verified. According to RFC 3447 it is advised to
443 * keep both hashes the same.
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 */
445int rsa_pkcs1_verify( rsa_context *ctx,
446 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200447 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000448 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000449 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000450 unsigned char *sig );
451
452/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100453 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
454 *
455 * \param ctx points to an RSA public key
456 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200457 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
458 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100459 * \param hash buffer holding the message digest
460 * \param sig buffer holding the ciphertext
461 *
462 * \return 0 if the verify operation was successful,
463 * or an POLARSSL_ERR_RSA_XXX error code
464 *
465 * \note The "sig" buffer must be as large as the size
466 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
467 */
468int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx,
469 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200470 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100471 unsigned int hashlen,
472 const unsigned char *hash,
473 unsigned char *sig );
474
475/**
476 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
477 * \brief Do a public RSA and check the message digest
478 *
479 * \param ctx points to an RSA public key
480 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200481 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
482 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100483 * \param hash buffer holding the message digest
484 * \param sig buffer holding the ciphertext
485 *
486 * \return 0 if the verify operation was successful,
487 * or an POLARSSL_ERR_RSA_XXX error code
488 *
489 * \note The "sig" buffer must be as large as the size
490 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
491 *
492 * \note In case of PKCS#1 v2.1 encoding keep in mind that
493 * the hash_id in the RSA context is the one used for the
494 * verification. hash_id in the function call is the type of hash
495 * that is verified. According to RFC 3447 it is advised to
496 * keep both hashes the same.
497 */
498int rsa_rsassa_pss_verify( rsa_context *ctx,
499 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200500 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100501 unsigned int hashlen,
502 const unsigned char *hash,
503 unsigned char *sig );
504
505/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000507 *
508 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 */
510void rsa_free( rsa_context *ctx );
511
512/**
513 * \brief Checkup routine
514 *
515 * \return 0 if successful, or 1 if the test failed
516 */
517int rsa_self_test( int verbose );
518
519#ifdef __cplusplus
520}
521#endif
522
523#endif /* rsa.h */