blob: 71fbff2d80c9c0ae0e20dee8090c9485b5794c77 [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
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +020092#if !defined(POLARSSL_RSA_NO_CRT)
93 mpi Vi; /*!< cached blinding value */
94 mpi Vf; /*!< cached un-blinding value */
95#endif
96
Paul Bakker9dcc3222011-03-08 14:16:06 +000097 int padding; /*!< RSA_PKCS_V15 for 1.5 padding and
98 RSA_PKCS_v21 for OAEP/PSS */
99 int hash_id; /*!< Hash identifier of md_type_t as
100 specified in the md.h header file
101 for the EME-OAEP and EMSA-PSS
102 encoding */
Paul Bakker5121ce52009-01-03 21:22:43 +0000103}
104rsa_context;
105
Paul Bakker5121ce52009-01-03 21:22:43 +0000106/**
107 * \brief Initialize an RSA context
108 *
Paul Bakker9a736322012-11-14 12:39:52 +0000109 * Note: Set padding to RSA_PKCS_V21 for the RSAES-OAEP
110 * encryption scheme and the RSASSA-PSS signature scheme.
111 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 * \param ctx RSA context to be initialized
113 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
114 * \param hash_id RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 *
116 * \note The hash_id parameter is actually ignored
117 * when using RSA_PKCS_V15 padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 */
119void rsa_init( rsa_context *ctx,
120 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000121 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
123/**
124 * \brief Generate an RSA keypair
125 *
126 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000127 * \param f_rng RNG function
128 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 * \param nbits size of the public key in bits
130 * \param exponent public exponent (e.g., 65537)
131 *
132 * \note rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000133 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 *
Paul Bakker40e46942009-01-03 21:51:57 +0000135 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 */
Paul Bakker21eb2802010-08-16 11:10:02 +0000137int rsa_gen_key( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000138 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000139 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000140 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
142/**
143 * \brief Check a public 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_pubkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151/**
152 * \brief Check a private RSA key
153 *
154 * \param ctx RSA context to be checked
155 *
Paul Bakker40e46942009-01-03 21:51:57 +0000156 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000158int rsa_check_privkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
160/**
161 * \brief Do an RSA public key operation
162 *
163 * \param ctx RSA context
164 * \param input input buffer
165 * \param output output buffer
166 *
Paul Bakker40e46942009-01-03 21:51:57 +0000167 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 *
169 * \note This function does NOT take care of message
Paul Bakker619467a2009-03-28 23:26:51 +0000170 * padding. Also, be sure to set input[0] = 0 or assure that
171 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 *
173 * \note The input and output buffers must be large
174 * enough (eg. 128 bytes if RSA-1024 is used).
175 */
176int rsa_public( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000177 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 unsigned char *output );
179
180/**
181 * \brief Do an RSA private key operation
182 *
183 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200184 * \param f_rng RNG function (Needed for blinding)
185 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 * \param input input buffer
187 * \param output output buffer
188 *
Paul Bakker40e46942009-01-03 21:51:57 +0000189 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 *
191 * \note The input and output buffers must be large
192 * enough (eg. 128 bytes if RSA-1024 is used).
193 */
194int rsa_private( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200195 int (*f_rng)(void *, unsigned char *, size_t),
196 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000197 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 unsigned char *output );
199
200/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100201 * \brief Generic wrapper to perform a PKCS#1 encryption using the
202 * mode from the context. Add the message padding, then do an
203 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 *
205 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200206 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
207 * and RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000208 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000210 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 * \param input buffer holding the data to be encrypted
212 * \param output buffer that will hold the ciphertext
213 *
Paul Bakker40e46942009-01-03 21:51:57 +0000214 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 *
216 * \note The output buffer must be as large as the size
217 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
218 */
219int rsa_pkcs1_encrypt( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000220 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000221 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000222 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000223 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 unsigned char *output );
225
226/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100227 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
228 *
229 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200230 * \param f_rng RNG function (Needed for padding and RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100231 * \param p_rng RNG parameter
232 * \param mode RSA_PUBLIC or RSA_PRIVATE
233 * \param ilen contains the plaintext length
234 * \param input buffer holding the data to be encrypted
235 * \param output buffer that will hold the ciphertext
236 *
237 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
238 *
239 * \note The output buffer must be as large as the size
240 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
241 */
242int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx,
243 int (*f_rng)(void *, unsigned char *, size_t),
244 void *p_rng,
245 int mode, size_t ilen,
246 const unsigned char *input,
247 unsigned char *output );
248
249/**
250 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
251 *
252 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200253 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
254 * and RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100255 * \param p_rng RNG parameter
256 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100257 * \param label buffer holding the custom label to use
258 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100259 * \param ilen contains the plaintext length
260 * \param input buffer holding the data to be encrypted
261 * \param output buffer that will hold the ciphertext
262 *
263 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
264 *
265 * \note The output buffer must be as large as the size
266 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
267 */
268int rsa_rsaes_oaep_encrypt( rsa_context *ctx,
269 int (*f_rng)(void *, unsigned char *, size_t),
270 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100271 int mode,
272 const unsigned char *label, size_t label_len,
273 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100274 const unsigned char *input,
275 unsigned char *output );
276
277/**
278 * \brief Generic wrapper to perform a PKCS#1 decryption using the
279 * mode from the context. Do an RSA operation, then remove
280 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 *
282 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200283 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200284 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000286 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 * \param input buffer holding the encrypted data
288 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000289 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 *
Paul Bakker40e46942009-01-03 21:51:57 +0000291 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 *
293 * \note The output buffer must be as large as the size
Paul Bakker060c5682009-01-12 21:48:39 +0000294 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
295 * an error is thrown.
Paul Bakker5121ce52009-01-03 21:22:43 +0000296 */
297int rsa_pkcs1_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200298 int (*f_rng)(void *, unsigned char *, size_t),
299 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000300 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000301 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000302 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000303 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304
305/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100306 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
307 *
308 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200309 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200310 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100311 * \param mode RSA_PUBLIC or RSA_PRIVATE
312 * \param olen will contain the plaintext length
313 * \param input buffer holding the encrypted data
314 * \param output buffer that will hold the plaintext
315 * \param output_max_len maximum length of the output buffer
316 *
317 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
318 *
319 * \note The output buffer must be as large as the size
320 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
321 * an error is thrown.
322 */
323int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200324 int (*f_rng)(void *, unsigned char *, size_t),
325 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100326 int mode, size_t *olen,
327 const unsigned char *input,
328 unsigned char *output,
329 size_t output_max_len );
330
331/**
332 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
333 *
334 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200335 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200336 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100337 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100338 * \param label buffer holding the custom label to use
339 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100340 * \param olen will contain the plaintext length
341 * \param input buffer holding the encrypted data
342 * \param output buffer that will hold the plaintext
343 * \param output_max_len maximum length of the output buffer
344 *
345 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
346 *
347 * \note The output buffer must be as large as the size
348 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
349 * an error is thrown.
350 */
351int rsa_rsaes_oaep_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200352 int (*f_rng)(void *, unsigned char *, size_t),
353 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100354 int mode,
355 const unsigned char *label, size_t label_len,
356 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100357 const unsigned char *input,
358 unsigned char *output,
359 size_t output_max_len );
360
361/**
362 * \brief Generic wrapper to perform a PKCS#1 signature using the
363 * mode from the context. Do a private RSA operation to sign
364 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000365 *
366 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200367 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
368 * RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000369 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000370 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200371 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
372 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000373 * \param hash buffer holding the message digest
374 * \param sig buffer that will hold the ciphertext
375 *
376 * \return 0 if the signing operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000377 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 *
379 * \note The "sig" buffer must be as large as the size
380 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000381 *
382 * \note In case of PKCS#1 v2.1 encoding keep in mind that
383 * the hash_id in the RSA context is the one used for the
384 * encoding. hash_id in the function call is the type of hash
385 * that is encoded. According to RFC 3447 it is advised to
386 * keep both hashes the same.
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 */
388int rsa_pkcs1_sign( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000389 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000390 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200392 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000393 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000394 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 unsigned char *sig );
396
397/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100398 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
399 *
400 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200401 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200402 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100403 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200404 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
405 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100406 * \param hash buffer holding the message digest
407 * \param sig buffer that will hold the ciphertext
408 *
409 * \return 0 if the signing operation was successful,
410 * or an POLARSSL_ERR_RSA_XXX error code
411 *
412 * \note The "sig" buffer must be as large as the size
413 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
414 */
415int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200416 int (*f_rng)(void *, unsigned char *, size_t),
417 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100418 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200419 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100420 unsigned int hashlen,
421 const unsigned char *hash,
422 unsigned char *sig );
423
424/**
425 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
426 *
427 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200428 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
429 * RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100430 * \param p_rng RNG parameter
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 Bakkerb3869132013-02-28 17:21:01 +0100434 * \param hash buffer holding the message digest
435 * \param sig buffer that will hold the ciphertext
436 *
437 * \return 0 if the signing operation was successful,
438 * or an POLARSSL_ERR_RSA_XXX error code
439 *
440 * \note The "sig" buffer must be as large as the size
441 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
442 *
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 * encoding. hash_id in the function call is the type of hash
446 * that is encoded. According to RFC 3447 it is advised to
447 * keep both hashes the same.
448 */
449int rsa_rsassa_pss_sign( rsa_context *ctx,
450 int (*f_rng)(void *, unsigned char *, size_t),
451 void *p_rng,
452 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200453 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100454 unsigned int hashlen,
455 const unsigned char *hash,
456 unsigned char *sig );
457
458/**
459 * \brief Generic wrapper to perform a PKCS#1 verification using the
460 * mode from the context. Do a public RSA operation and check
461 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000462 *
463 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200464 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200465 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200467 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
468 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 * \param hash buffer holding the message digest
470 * \param sig buffer holding the ciphertext
471 *
472 * \return 0 if the verify operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000473 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000474 *
475 * \note The "sig" buffer must be as large as the size
476 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000477 *
478 * \note In case of PKCS#1 v2.1 encoding keep in mind that
479 * the hash_id in the RSA context is the one used for the
480 * verification. hash_id in the function call is the type of hash
481 * that is verified. According to RFC 3447 it is advised to
482 * keep both hashes the same.
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 */
484int rsa_pkcs1_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200485 int (*f_rng)(void *, unsigned char *, size_t),
486 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200488 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000489 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000490 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200491 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
493/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100494 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
495 *
496 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200497 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200498 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100499 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200500 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
501 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100502 * \param hash buffer holding the message digest
503 * \param sig buffer holding the ciphertext
504 *
505 * \return 0 if the verify operation was successful,
506 * or an POLARSSL_ERR_RSA_XXX error code
507 *
508 * \note The "sig" buffer must be as large as the size
509 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
510 */
511int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200512 int (*f_rng)(void *, unsigned char *, size_t),
513 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100514 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200515 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100516 unsigned int hashlen,
517 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200518 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100519
520/**
521 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
522 * \brief Do a public RSA and check the message digest
523 *
524 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200525 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200526 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100527 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200528 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
529 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100530 * \param hash buffer holding the message digest
531 * \param sig buffer holding the ciphertext
532 *
533 * \return 0 if the verify operation was successful,
534 * or an POLARSSL_ERR_RSA_XXX error code
535 *
536 * \note The "sig" buffer must be as large as the size
537 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
538 *
539 * \note In case of PKCS#1 v2.1 encoding keep in mind that
540 * the hash_id in the RSA context is the one used for the
541 * verification. hash_id in the function call is the type of hash
542 * that is verified. According to RFC 3447 it is advised to
543 * keep both hashes the same.
544 */
545int rsa_rsassa_pss_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200546 int (*f_rng)(void *, unsigned char *, size_t),
547 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100548 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200549 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100550 unsigned int hashlen,
551 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200552 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100553
554/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200555 * \brief Copy the components of an RSA context
556 *
557 * \param dst Destination context
558 * \param src Source context
559 *
560 * \return O on success,
561 * POLARSSL_ERR_MPI_MALLOC_FAILED on memory allocation failure
562 */
563int rsa_copy( rsa_context *dst, const rsa_context *src );
564
565/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000567 *
568 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 */
570void rsa_free( rsa_context *ctx );
571
572/**
573 * \brief Checkup routine
574 *
575 * \return 0 if successful, or 1 if the test failed
576 */
577int rsa_self_test( int verbose );
578
579#ifdef __cplusplus
580}
581#endif
582
Paul Bakkered27a042013-04-18 22:46:23 +0200583#endif /* POLARSSL_RSA_C */
584
Paul Bakker5121ce52009-01-03 21:22:43 +0000585#endif /* rsa.h */