blob: d8c8341c9a8944e537878227a6a3229e602f2043 [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 Bakker42099c32014-01-27 11:45:49 +0100128 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
130/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100131 * \brief Set padding for an already initialized RSA context
132 *
133 * Note: Set padding to RSA_PKCS_V21 for the RSAES-OAEP
134 * encryption scheme and the RSASSA-PSS signature scheme.
135 *
136 * \param ctx RSA context to be set
137 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
138 * \param hash_id RSA_PKCS_V21 hash identifier
139 *
140 * \note The hash_id parameter is actually ignored
141 * when using RSA_PKCS_V15 padding.
142 */
143void rsa_set_padding( rsa_context *ctx, int padding, int hash_id);
144
145/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 * \brief Generate an RSA keypair
147 *
148 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000149 * \param f_rng RNG function
150 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 * \param nbits size of the public key in bits
152 * \param exponent public exponent (e.g., 65537)
153 *
154 * \note rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000155 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 *
Paul Bakker40e46942009-01-03 21:51:57 +0000157 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 */
Paul Bakker21eb2802010-08-16 11:10:02 +0000159int rsa_gen_key( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000160 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000161 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000162 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
164/**
165 * \brief Check a public RSA key
166 *
167 * \param ctx RSA context to be checked
168 *
Paul Bakker40e46942009-01-03 21:51:57 +0000169 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000171int rsa_check_pubkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
173/**
174 * \brief Check a private RSA key
175 *
176 * \param ctx RSA context to be checked
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 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000180int rsa_check_privkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
182/**
183 * \brief Do an RSA public key operation
184 *
185 * \param ctx RSA context
186 * \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 This function does NOT take care of message
Paul Bakker619467a2009-03-28 23:26:51 +0000192 * padding. Also, be sure to set input[0] = 0 or assure that
193 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 *
195 * \note The input and output buffers must be large
196 * enough (eg. 128 bytes if RSA-1024 is used).
197 */
198int rsa_public( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000199 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 unsigned char *output );
201
202/**
203 * \brief Do an RSA private key operation
204 *
205 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200206 * \param f_rng RNG function (Needed for blinding)
207 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 * \param input input buffer
209 * \param output output buffer
210 *
Paul Bakker40e46942009-01-03 21:51:57 +0000211 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 *
213 * \note The input and output buffers must be large
214 * enough (eg. 128 bytes if RSA-1024 is used).
215 */
216int rsa_private( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200217 int (*f_rng)(void *, unsigned char *, size_t),
218 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000219 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 unsigned char *output );
221
222/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100223 * \brief Generic wrapper to perform a PKCS#1 encryption using the
224 * mode from the context. Add the message padding, then do an
225 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 *
227 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200228 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
229 * and RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000230 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000232 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 * \param input buffer holding the data to be encrypted
234 * \param output buffer that will hold the ciphertext
235 *
Paul Bakker40e46942009-01-03 21:51:57 +0000236 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 *
238 * \note The output buffer must be as large as the size
239 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
240 */
241int rsa_pkcs1_encrypt( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000242 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000243 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000244 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000245 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 unsigned char *output );
247
248/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100249 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
250 *
251 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200252 * \param f_rng RNG function (Needed for padding and RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100253 * \param p_rng RNG parameter
254 * \param mode RSA_PUBLIC or RSA_PRIVATE
255 * \param ilen contains the plaintext length
256 * \param input buffer holding the data to be encrypted
257 * \param output buffer that will hold the ciphertext
258 *
259 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
260 *
261 * \note The output buffer must be as large as the size
262 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
263 */
264int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx,
265 int (*f_rng)(void *, unsigned char *, size_t),
266 void *p_rng,
267 int mode, size_t ilen,
268 const unsigned char *input,
269 unsigned char *output );
270
271/**
272 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
273 *
274 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200275 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
276 * and RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100277 * \param p_rng RNG parameter
278 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100279 * \param label buffer holding the custom label to use
280 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100281 * \param ilen contains the plaintext length
282 * \param input buffer holding the data to be encrypted
283 * \param output buffer that will hold the ciphertext
284 *
285 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
286 *
287 * \note The output buffer must be as large as the size
288 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
289 */
290int rsa_rsaes_oaep_encrypt( rsa_context *ctx,
291 int (*f_rng)(void *, unsigned char *, size_t),
292 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100293 int mode,
294 const unsigned char *label, size_t label_len,
295 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100296 const unsigned char *input,
297 unsigned char *output );
298
299/**
300 * \brief Generic wrapper to perform a PKCS#1 decryption using the
301 * mode from the context. Do an RSA operation, then remove
302 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 *
304 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200305 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200306 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000308 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 * \param input buffer holding the encrypted data
310 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000311 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 *
Paul Bakker40e46942009-01-03 21:51:57 +0000313 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 *
315 * \note The output buffer must be as large as the size
Paul Bakker060c5682009-01-12 21:48:39 +0000316 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
317 * an error is thrown.
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 */
319int rsa_pkcs1_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200320 int (*f_rng)(void *, unsigned char *, size_t),
321 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000322 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000323 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000324 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000325 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000326
327/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100328 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
329 *
330 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200331 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200332 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100333 * \param mode RSA_PUBLIC or RSA_PRIVATE
334 * \param olen will contain the plaintext length
335 * \param input buffer holding the encrypted data
336 * \param output buffer that will hold the plaintext
337 * \param output_max_len maximum length of the output buffer
338 *
339 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
340 *
341 * \note The output buffer must be as large as the size
342 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
343 * an error is thrown.
344 */
345int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200346 int (*f_rng)(void *, unsigned char *, size_t),
347 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100348 int mode, size_t *olen,
349 const unsigned char *input,
350 unsigned char *output,
351 size_t output_max_len );
352
353/**
354 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
355 *
356 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200357 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200358 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100359 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100360 * \param label buffer holding the custom label to use
361 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100362 * \param olen will contain the plaintext length
363 * \param input buffer holding the encrypted data
364 * \param output buffer that will hold the plaintext
365 * \param output_max_len maximum length of the output buffer
366 *
367 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
368 *
369 * \note The output buffer must be as large as the size
370 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
371 * an error is thrown.
372 */
373int rsa_rsaes_oaep_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200374 int (*f_rng)(void *, unsigned char *, size_t),
375 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100376 int mode,
377 const unsigned char *label, size_t label_len,
378 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100379 const unsigned char *input,
380 unsigned char *output,
381 size_t output_max_len );
382
383/**
384 * \brief Generic wrapper to perform a PKCS#1 signature using the
385 * mode from the context. Do a private RSA operation to sign
386 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 *
388 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200389 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
390 * RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000391 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000392 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200393 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
394 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 * \param hash buffer holding the message digest
396 * \param sig buffer that will hold the ciphertext
397 *
398 * \return 0 if the signing operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000399 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000400 *
401 * \note The "sig" buffer must be as large as the size
402 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000403 *
404 * \note In case of PKCS#1 v2.1 encoding keep in mind that
405 * the hash_id in the RSA context is the one used for the
406 * encoding. hash_id in the function call is the type of hash
407 * that is encoded. According to RFC 3447 it is advised to
408 * keep both hashes the same.
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 */
410int rsa_pkcs1_sign( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000411 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000412 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200414 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000415 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000416 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000417 unsigned char *sig );
418
419/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100420 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
421 *
422 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200423 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200424 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100425 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200426 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
427 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100428 * \param hash buffer holding the message digest
429 * \param sig buffer that will hold the ciphertext
430 *
431 * \return 0 if the signing operation was successful,
432 * or an POLARSSL_ERR_RSA_XXX error code
433 *
434 * \note The "sig" buffer must be as large as the size
435 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
436 */
437int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200438 int (*f_rng)(void *, unsigned char *, size_t),
439 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100440 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200441 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100442 unsigned int hashlen,
443 const unsigned char *hash,
444 unsigned char *sig );
445
446/**
447 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
448 *
449 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200450 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
451 * RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100452 * \param p_rng RNG parameter
453 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200454 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
455 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100456 * \param hash buffer holding the message digest
457 * \param sig buffer that will hold the ciphertext
458 *
459 * \return 0 if the signing operation was successful,
460 * or an POLARSSL_ERR_RSA_XXX error code
461 *
462 * \note The "sig" buffer must be as large as the size
463 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
464 *
465 * \note In case of PKCS#1 v2.1 encoding keep in mind that
466 * the hash_id in the RSA context is the one used for the
467 * encoding. hash_id in the function call is the type of hash
468 * that is encoded. According to RFC 3447 it is advised to
469 * keep both hashes the same.
470 */
471int rsa_rsassa_pss_sign( rsa_context *ctx,
472 int (*f_rng)(void *, unsigned char *, size_t),
473 void *p_rng,
474 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200475 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100476 unsigned int hashlen,
477 const unsigned char *hash,
478 unsigned char *sig );
479
480/**
481 * \brief Generic wrapper to perform a PKCS#1 verification using the
482 * mode from the context. Do a public RSA operation and check
483 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000484 *
485 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200486 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200487 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200489 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
490 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000491 * \param hash buffer holding the message digest
492 * \param sig buffer holding the ciphertext
493 *
494 * \return 0 if the verify operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000495 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000496 *
497 * \note The "sig" buffer must be as large as the size
498 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000499 *
500 * \note In case of PKCS#1 v2.1 encoding keep in mind that
501 * the hash_id in the RSA context is the one used for the
502 * verification. hash_id in the function call is the type of hash
503 * that is verified. According to RFC 3447 it is advised to
504 * keep both hashes the same.
Paul Bakker5121ce52009-01-03 21:22:43 +0000505 */
506int rsa_pkcs1_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200507 int (*f_rng)(void *, unsigned char *, size_t),
508 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200510 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000511 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000512 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200513 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000514
515/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100516 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
517 *
518 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200519 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200520 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100521 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200522 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
523 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100524 * \param hash buffer holding the message digest
525 * \param sig buffer holding the ciphertext
526 *
527 * \return 0 if the verify operation was successful,
528 * or an POLARSSL_ERR_RSA_XXX error code
529 *
530 * \note The "sig" buffer must be as large as the size
531 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
532 */
533int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200534 int (*f_rng)(void *, unsigned char *, size_t),
535 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100536 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200537 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100538 unsigned int hashlen,
539 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200540 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100541
542/**
543 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Paul Bakkerb3869132013-02-28 17:21:01 +0100544 *
545 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200546 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200547 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100548 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200549 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
550 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100551 * \param hash buffer holding the message digest
552 * \param sig buffer holding the ciphertext
553 *
554 * \return 0 if the verify operation was successful,
555 * or an POLARSSL_ERR_RSA_XXX error code
556 *
557 * \note The "sig" buffer must be as large as the size
558 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
559 *
560 * \note In case of PKCS#1 v2.1 encoding keep in mind that
561 * the hash_id in the RSA context is the one used for the
562 * verification. hash_id in the function call is the type of hash
563 * that is verified. According to RFC 3447 it is advised to
564 * keep both hashes the same.
565 */
566int rsa_rsassa_pss_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200567 int (*f_rng)(void *, unsigned char *, size_t),
568 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100569 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200570 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100571 unsigned int hashlen,
572 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200573 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100574
575/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200576 * \brief Copy the components of an RSA context
577 *
578 * \param dst Destination context
579 * \param src Source context
580 *
581 * \return O on success,
582 * POLARSSL_ERR_MPI_MALLOC_FAILED on memory allocation failure
583 */
584int rsa_copy( rsa_context *dst, const rsa_context *src );
585
586/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000587 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000588 *
589 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 */
591void rsa_free( rsa_context *ctx );
592
593/**
594 * \brief Checkup routine
595 *
596 * \return 0 if successful, or 1 if the test failed
597 */
598int rsa_self_test( int verbose );
599
600#ifdef __cplusplus
601}
602#endif
603
Paul Bakkered27a042013-04-18 22:46:23 +0200604#endif /* POLARSSL_RSA_C */
605
Paul Bakker5121ce52009-01-03 21:22:43 +0000606#endif /* rsa.h */