blob: e7b61915986314ee069a4951bc06ed915317fa5a [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 Bakkerc9965dc2013-09-29 14:58:17 +020035#if defined(POLARSSL_THREADING_C)
36#include "threading.h"
37#endif
38
Paul Bakker13e2dfe2009-07-28 07:18:38 +000039/*
40 * RSA Error codes
41 */
Paul Bakker9d781402011-05-09 16:17:09 +000042#define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
43#define POLARSSL_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
44#define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
45#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the libraries validity check. */
46#define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
47#define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
48#define POLARSSL_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
49#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
50#define POLARSSL_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Paul Bakker5121ce52009-01-03 21:22:43 +000051
52/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020053 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000054 */
Paul Bakker5121ce52009-01-03 21:22:43 +000055#define RSA_PUBLIC 0
56#define RSA_PRIVATE 1
57
58#define RSA_PKCS_V15 0
59#define RSA_PKCS_V21 1
60
61#define RSA_SIGN 1
62#define RSA_CRYPT 2
63
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020064/*
65 * The above constants may be used even if the RSA module is compile out,
66 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
67 */
68#if defined(POLARSSL_RSA_C)
69
Paul Bakker407a0da2013-06-27 14:29:21 +020070#ifdef __cplusplus
71extern "C" {
72#endif
73
Paul Bakker5121ce52009-01-03 21:22:43 +000074/**
75 * \brief RSA context structure
76 */
77typedef struct
78{
79 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +000080 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +000081
82 mpi N; /*!< public modulus */
83 mpi E; /*!< public exponent */
84
85 mpi D; /*!< private exponent */
86 mpi P; /*!< 1st prime factor */
87 mpi Q; /*!< 2nd prime factor */
88 mpi DP; /*!< D % (P - 1) */
89 mpi DQ; /*!< D % (Q - 1) */
90 mpi QP; /*!< 1 / (Q % P) */
91
92 mpi RN; /*!< cached R^2 mod N */
93 mpi RP; /*!< cached R^2 mod P */
94 mpi RQ; /*!< cached R^2 mod Q */
95
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +020096#if !defined(POLARSSL_RSA_NO_CRT)
97 mpi Vi; /*!< cached blinding value */
98 mpi Vf; /*!< cached un-blinding value */
99#endif
100
Paul Bakker9dcc3222011-03-08 14:16:06 +0000101 int padding; /*!< RSA_PKCS_V15 for 1.5 padding and
102 RSA_PKCS_v21 for OAEP/PSS */
103 int hash_id; /*!< Hash identifier of md_type_t as
104 specified in the md.h header file
105 for the EME-OAEP and EMSA-PSS
106 encoding */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200107#if defined(POLARSSL_THREADING_C)
108 threading_mutex_t mutex; /*!< Thread-safety mutex */
109#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000110}
111rsa_context;
112
Paul Bakker5121ce52009-01-03 21:22:43 +0000113/**
114 * \brief Initialize an RSA context
115 *
Paul Bakker9a736322012-11-14 12:39:52 +0000116 * Note: Set padding to RSA_PKCS_V21 for the RSAES-OAEP
117 * encryption scheme and the RSASSA-PSS signature scheme.
118 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 * \param ctx RSA context to be initialized
120 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
121 * \param hash_id RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 *
123 * \note The hash_id parameter is actually ignored
124 * when using RSA_PKCS_V15 padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 */
126void rsa_init( rsa_context *ctx,
127 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000128 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
130/**
131 * \brief Generate an RSA keypair
132 *
133 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000134 * \param f_rng RNG function
135 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 * \param nbits size of the public key in bits
137 * \param exponent public exponent (e.g., 65537)
138 *
139 * \note rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000140 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 *
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 Bakker21eb2802010-08-16 11:10:02 +0000144int rsa_gen_key( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000145 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000146 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000147 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
149/**
150 * \brief Check a public RSA key
151 *
152 * \param ctx RSA context to be checked
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 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000156int rsa_check_pubkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
158/**
159 * \brief Check a private RSA key
160 *
161 * \param ctx RSA context to be checked
162 *
Paul Bakker40e46942009-01-03 21:51:57 +0000163 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000165int rsa_check_privkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
167/**
168 * \brief Do an RSA public 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 This function does NOT take care of message
Paul Bakker619467a2009-03-28 23:26:51 +0000177 * padding. Also, be sure to set input[0] = 0 or assure that
178 * input is smaller than N.
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_public( 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/**
188 * \brief Do an RSA private key operation
189 *
190 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200191 * \param f_rng RNG function (Needed for blinding)
192 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 * \param input input buffer
194 * \param output output buffer
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 input and output buffers must be large
199 * enough (eg. 128 bytes if RSA-1024 is used).
200 */
201int rsa_private( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200202 int (*f_rng)(void *, unsigned char *, size_t),
203 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000204 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 unsigned char *output );
206
207/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100208 * \brief Generic wrapper to perform a PKCS#1 encryption using the
209 * mode from the context. Add the message padding, then do an
210 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 *
212 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200213 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
214 * and RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000215 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000217 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 * \param input buffer holding the data to be encrypted
219 * \param output buffer that will hold the ciphertext
220 *
Paul Bakker40e46942009-01-03 21:51:57 +0000221 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 *
223 * \note The output buffer must be as large as the size
224 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
225 */
226int rsa_pkcs1_encrypt( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000227 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000228 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000229 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000230 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 unsigned char *output );
232
233/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100234 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
235 *
236 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200237 * \param f_rng RNG function (Needed for padding and RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100238 * \param p_rng RNG parameter
239 * \param mode RSA_PUBLIC or RSA_PRIVATE
240 * \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_pkcs1_v15_encrypt( rsa_context *ctx,
250 int (*f_rng)(void *, unsigned char *, size_t),
251 void *p_rng,
252 int mode, size_t ilen,
253 const unsigned char *input,
254 unsigned char *output );
255
256/**
257 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
258 *
259 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200260 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
261 * and RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100262 * \param p_rng RNG parameter
263 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100264 * \param label buffer holding the custom label to use
265 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100266 * \param ilen contains the plaintext length
267 * \param input buffer holding the data to be encrypted
268 * \param output buffer that will hold the ciphertext
269 *
270 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
271 *
272 * \note The output buffer must be as large as the size
273 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
274 */
275int rsa_rsaes_oaep_encrypt( rsa_context *ctx,
276 int (*f_rng)(void *, unsigned char *, size_t),
277 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100278 int mode,
279 const unsigned char *label, size_t label_len,
280 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100281 const unsigned char *input,
282 unsigned char *output );
283
284/**
285 * \brief Generic wrapper to perform a PKCS#1 decryption using the
286 * mode from the context. Do an RSA operation, then remove
287 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 *
289 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200290 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200291 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000293 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 * \param input buffer holding the encrypted data
295 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000296 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 *
Paul Bakker40e46942009-01-03 21:51:57 +0000298 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 *
300 * \note The output buffer must be as large as the size
Paul Bakker060c5682009-01-12 21:48:39 +0000301 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
302 * an error is thrown.
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 */
304int rsa_pkcs1_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200305 int (*f_rng)(void *, unsigned char *, size_t),
306 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000307 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000308 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000309 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000310 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
312/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100313 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
314 *
315 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200316 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200317 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100318 * \param mode RSA_PUBLIC or RSA_PRIVATE
319 * \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_pkcs1_v15_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200331 int (*f_rng)(void *, unsigned char *, size_t),
332 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100333 int mode, size_t *olen,
334 const unsigned char *input,
335 unsigned char *output,
336 size_t output_max_len );
337
338/**
339 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
340 *
341 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200342 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200343 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100344 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100345 * \param label buffer holding the custom label to use
346 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100347 * \param olen will contain the plaintext length
348 * \param input buffer holding the encrypted data
349 * \param output buffer that will hold the plaintext
350 * \param output_max_len maximum length of the output buffer
351 *
352 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
353 *
354 * \note The output buffer must be as large as the size
355 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
356 * an error is thrown.
357 */
358int rsa_rsaes_oaep_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200359 int (*f_rng)(void *, unsigned char *, size_t),
360 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100361 int mode,
362 const unsigned char *label, size_t label_len,
363 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100364 const unsigned char *input,
365 unsigned char *output,
366 size_t output_max_len );
367
368/**
369 * \brief Generic wrapper to perform a PKCS#1 signature using the
370 * mode from the context. Do a private RSA operation to sign
371 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000372 *
373 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200374 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
375 * RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000376 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 * \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 Bakker5121ce52009-01-03 21:22:43 +0000380 * \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,
Paul Bakker40e46942009-01-03 21:51:57 +0000384 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 *
386 * \note The "sig" buffer must be as large as the size
387 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000388 *
389 * \note In case of PKCS#1 v2.1 encoding keep in mind that
390 * the hash_id in the RSA context is the one used for the
391 * encoding. hash_id in the function call is the type of hash
392 * that is encoded. According to RFC 3447 it is advised to
393 * keep both hashes the same.
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 */
395int rsa_pkcs1_sign( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000396 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000397 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200399 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000400 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000401 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 unsigned char *sig );
403
404/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100405 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
406 *
407 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200408 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200409 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100410 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200411 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
412 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100413 * \param hash buffer holding the message digest
414 * \param sig buffer that will hold the ciphertext
415 *
416 * \return 0 if the signing operation was successful,
417 * or an POLARSSL_ERR_RSA_XXX error code
418 *
419 * \note The "sig" buffer must be as large as the size
420 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
421 */
422int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200423 int (*f_rng)(void *, unsigned char *, size_t),
424 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100425 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200426 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100427 unsigned int hashlen,
428 const unsigned char *hash,
429 unsigned char *sig );
430
431/**
432 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
433 *
434 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200435 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
436 * RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100437 * \param p_rng RNG parameter
438 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200439 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
440 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100441 * \param hash buffer holding the message digest
442 * \param sig buffer that will hold the ciphertext
443 *
444 * \return 0 if the signing operation was successful,
445 * or an POLARSSL_ERR_RSA_XXX error code
446 *
447 * \note The "sig" buffer must be as large as the size
448 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
449 *
450 * \note In case of PKCS#1 v2.1 encoding keep in mind that
451 * the hash_id in the RSA context is the one used for the
452 * encoding. hash_id in the function call is the type of hash
453 * that is encoded. According to RFC 3447 it is advised to
454 * keep both hashes the same.
455 */
456int rsa_rsassa_pss_sign( rsa_context *ctx,
457 int (*f_rng)(void *, unsigned char *, size_t),
458 void *p_rng,
459 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200460 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100461 unsigned int hashlen,
462 const unsigned char *hash,
463 unsigned char *sig );
464
465/**
466 * \brief Generic wrapper to perform a PKCS#1 verification using the
467 * mode from the context. Do a public RSA operation and check
468 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 *
470 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200471 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200472 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000473 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200474 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
475 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 * \param hash buffer holding the message digest
477 * \param sig buffer holding the ciphertext
478 *
479 * \return 0 if the verify operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000480 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 *
482 * \note The "sig" buffer must be as large as the size
483 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000484 *
485 * \note In case of PKCS#1 v2.1 encoding keep in mind that
486 * the hash_id in the RSA context is the one used for the
487 * verification. hash_id in the function call is the type of hash
488 * that is verified. According to RFC 3447 it is advised to
489 * keep both hashes the same.
Paul Bakker5121ce52009-01-03 21:22:43 +0000490 */
491int rsa_pkcs1_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200492 int (*f_rng)(void *, unsigned char *, size_t),
493 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000494 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200495 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000496 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000497 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200498 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100501 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
502 *
503 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200504 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200505 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100506 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200507 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
508 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100509 * \param hash buffer holding the message digest
510 * \param sig buffer holding the ciphertext
511 *
512 * \return 0 if the verify operation was successful,
513 * or an POLARSSL_ERR_RSA_XXX error code
514 *
515 * \note The "sig" buffer must be as large as the size
516 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
517 */
518int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200519 int (*f_rng)(void *, unsigned char *, size_t),
520 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100521 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200522 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100523 unsigned int hashlen,
524 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200525 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100526
527/**
528 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
529 * \brief Do a public RSA and check the message digest
530 *
531 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200532 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200533 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100534 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200535 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
536 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100537 * \param hash buffer holding the message digest
538 * \param sig buffer holding the ciphertext
539 *
540 * \return 0 if the verify operation was successful,
541 * or an POLARSSL_ERR_RSA_XXX error code
542 *
543 * \note The "sig" buffer must be as large as the size
544 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
545 *
546 * \note In case of PKCS#1 v2.1 encoding keep in mind that
547 * the hash_id in the RSA context is the one used for the
548 * verification. hash_id in the function call is the type of hash
549 * that is verified. According to RFC 3447 it is advised to
550 * keep both hashes the same.
551 */
552int rsa_rsassa_pss_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200553 int (*f_rng)(void *, unsigned char *, size_t),
554 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100555 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200556 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100557 unsigned int hashlen,
558 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200559 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100560
561/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200562 * \brief Copy the components of an RSA context
563 *
564 * \param dst Destination context
565 * \param src Source context
566 *
567 * \return O on success,
568 * POLARSSL_ERR_MPI_MALLOC_FAILED on memory allocation failure
569 */
570int rsa_copy( rsa_context *dst, const rsa_context *src );
571
572/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000573 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000574 *
575 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 */
577void rsa_free( rsa_context *ctx );
578
579/**
580 * \brief Checkup routine
581 *
582 * \return 0 if successful, or 1 if the test failed
583 */
584int rsa_self_test( int verbose );
585
586#ifdef __cplusplus
587}
588#endif
589
Paul Bakkered27a042013-04-18 22:46:23 +0200590#endif /* POLARSSL_RSA_C */
591
Paul Bakker5121ce52009-01-03 21:22:43 +0000592#endif /* rsa.h */