blob: 4e85ca6ccb8c24541d90d1ddcd2b620af8340191 [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
Paul Bakker9dcc3222011-03-08 14:16:06 +000092 int padding; /*!< RSA_PKCS_V15 for 1.5 padding and
93 RSA_PKCS_v21 for OAEP/PSS */
94 int hash_id; /*!< Hash identifier of md_type_t as
95 specified in the md.h header file
96 for the EME-OAEP and EMSA-PSS
97 encoding */
Paul Bakker5121ce52009-01-03 21:22:43 +000098}
99rsa_context;
100
Paul Bakker5121ce52009-01-03 21:22:43 +0000101/**
102 * \brief Initialize an RSA context
103 *
Paul Bakker9a736322012-11-14 12:39:52 +0000104 * Note: Set padding to RSA_PKCS_V21 for the RSAES-OAEP
105 * encryption scheme and the RSASSA-PSS signature scheme.
106 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 * \param ctx RSA context to be initialized
108 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
109 * \param hash_id RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 *
111 * \note The hash_id parameter is actually ignored
112 * when using RSA_PKCS_V15 padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 */
114void rsa_init( rsa_context *ctx,
115 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000116 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
118/**
119 * \brief Generate an RSA keypair
120 *
121 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000122 * \param f_rng RNG function
123 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 * \param nbits size of the public key in bits
125 * \param exponent public exponent (e.g., 65537)
126 *
127 * \note rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000128 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 *
Paul Bakker40e46942009-01-03 21:51:57 +0000130 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 */
Paul Bakker21eb2802010-08-16 11:10:02 +0000132int rsa_gen_key( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000133 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000134 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000135 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
137/**
138 * \brief Check a public RSA key
139 *
140 * \param ctx RSA context to be checked
141 *
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 Bakkerff60ee62010-03-16 21:09:09 +0000144int rsa_check_pubkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
146/**
147 * \brief Check a private RSA key
148 *
149 * \param ctx RSA context to be checked
150 *
Paul Bakker40e46942009-01-03 21:51:57 +0000151 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000153int rsa_check_privkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
155/**
156 * \brief Do an RSA public key operation
157 *
158 * \param ctx RSA context
159 * \param input input buffer
160 * \param output output buffer
161 *
Paul Bakker40e46942009-01-03 21:51:57 +0000162 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 *
164 * \note This function does NOT take care of message
Paul Bakker619467a2009-03-28 23:26:51 +0000165 * padding. Also, be sure to set input[0] = 0 or assure that
166 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 *
168 * \note The input and output buffers must be large
169 * enough (eg. 128 bytes if RSA-1024 is used).
170 */
171int rsa_public( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000172 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 unsigned char *output );
174
175/**
176 * \brief Do an RSA private key operation
177 *
178 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200179 * \param f_rng RNG function (Needed for blinding)
180 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 * \param input input buffer
182 * \param output output buffer
183 *
Paul Bakker40e46942009-01-03 21:51:57 +0000184 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 *
186 * \note The input and output buffers must be large
187 * enough (eg. 128 bytes if RSA-1024 is used).
188 */
189int rsa_private( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200190 int (*f_rng)(void *, unsigned char *, size_t),
191 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000192 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 unsigned char *output );
194
195/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100196 * \brief Generic wrapper to perform a PKCS#1 encryption using the
197 * mode from the context. Add the message padding, then do an
198 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 *
200 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200201 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
202 * and RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000203 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000205 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 * \param input buffer holding the data to be encrypted
207 * \param output buffer that will hold the ciphertext
208 *
Paul Bakker40e46942009-01-03 21:51:57 +0000209 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 *
211 * \note The output buffer must be as large as the size
212 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
213 */
214int rsa_pkcs1_encrypt( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000215 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000216 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000217 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000218 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 unsigned char *output );
220
221/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100222 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
223 *
224 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200225 * \param f_rng RNG function (Needed for padding and RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100226 * \param p_rng RNG parameter
227 * \param mode RSA_PUBLIC or RSA_PRIVATE
228 * \param ilen contains the plaintext length
229 * \param input buffer holding the data to be encrypted
230 * \param output buffer that will hold the ciphertext
231 *
232 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
233 *
234 * \note The output buffer must be as large as the size
235 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
236 */
237int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx,
238 int (*f_rng)(void *, unsigned char *, size_t),
239 void *p_rng,
240 int mode, size_t ilen,
241 const unsigned char *input,
242 unsigned char *output );
243
244/**
245 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
246 *
247 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200248 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
249 * and RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100250 * \param p_rng RNG parameter
251 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100252 * \param label buffer holding the custom label to use
253 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100254 * \param ilen contains the plaintext length
255 * \param input buffer holding the data to be encrypted
256 * \param output buffer that will hold the ciphertext
257 *
258 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
259 *
260 * \note The output buffer must be as large as the size
261 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
262 */
263int rsa_rsaes_oaep_encrypt( rsa_context *ctx,
264 int (*f_rng)(void *, unsigned char *, size_t),
265 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100266 int mode,
267 const unsigned char *label, size_t label_len,
268 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100269 const unsigned char *input,
270 unsigned char *output );
271
272/**
273 * \brief Generic wrapper to perform a PKCS#1 decryption using the
274 * mode from the context. Do an RSA operation, then remove
275 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000276 *
277 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200278 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200279 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000280 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000281 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 * \param input buffer holding the encrypted data
283 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000284 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 *
Paul Bakker40e46942009-01-03 21:51:57 +0000286 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 *
288 * \note The output buffer must be as large as the size
Paul Bakker060c5682009-01-12 21:48:39 +0000289 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
290 * an error is thrown.
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 */
292int rsa_pkcs1_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200293 int (*f_rng)(void *, unsigned char *, size_t),
294 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000295 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000296 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000297 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000298 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100301 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
302 *
303 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200304 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200305 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100306 * \param mode RSA_PUBLIC or RSA_PRIVATE
307 * \param olen will contain the plaintext length
308 * \param input buffer holding the encrypted data
309 * \param output buffer that will hold the plaintext
310 * \param output_max_len maximum length of the output buffer
311 *
312 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
313 *
314 * \note The output buffer must be as large as the size
315 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
316 * an error is thrown.
317 */
318int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200319 int (*f_rng)(void *, unsigned char *, size_t),
320 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100321 int mode, size_t *olen,
322 const unsigned char *input,
323 unsigned char *output,
324 size_t output_max_len );
325
326/**
327 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
328 *
329 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200330 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200331 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100332 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100333 * \param label buffer holding the custom label to use
334 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100335 * \param olen will contain the plaintext length
336 * \param input buffer holding the encrypted data
337 * \param output buffer that will hold the plaintext
338 * \param output_max_len maximum length of the output buffer
339 *
340 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
341 *
342 * \note The output buffer must be as large as the size
343 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
344 * an error is thrown.
345 */
346int rsa_rsaes_oaep_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200347 int (*f_rng)(void *, unsigned char *, size_t),
348 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100349 int mode,
350 const unsigned char *label, size_t label_len,
351 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100352 const unsigned char *input,
353 unsigned char *output,
354 size_t output_max_len );
355
356/**
357 * \brief Generic wrapper to perform a PKCS#1 signature using the
358 * mode from the context. Do a private RSA operation to sign
359 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 *
361 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200362 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
363 * RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000364 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000365 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200366 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
367 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000368 * \param hash buffer holding the message digest
369 * \param sig buffer that will hold the ciphertext
370 *
371 * \return 0 if the signing operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000372 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000373 *
374 * \note The "sig" buffer must be as large as the size
375 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000376 *
377 * \note In case of PKCS#1 v2.1 encoding keep in mind that
378 * the hash_id in the RSA context is the one used for the
379 * encoding. hash_id in the function call is the type of hash
380 * that is encoded. According to RFC 3447 it is advised to
381 * keep both hashes the same.
Paul Bakker5121ce52009-01-03 21:22:43 +0000382 */
383int rsa_pkcs1_sign( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000384 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000385 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000386 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200387 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000388 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000389 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 unsigned char *sig );
391
392/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100393 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
394 *
395 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200396 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200397 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100398 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200399 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
400 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100401 * \param hash buffer holding the message digest
402 * \param sig buffer that will hold the ciphertext
403 *
404 * \return 0 if the signing operation was successful,
405 * or an POLARSSL_ERR_RSA_XXX error code
406 *
407 * \note The "sig" buffer must be as large as the size
408 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
409 */
410int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200411 int (*f_rng)(void *, unsigned char *, size_t),
412 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100413 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200414 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100415 unsigned int hashlen,
416 const unsigned char *hash,
417 unsigned char *sig );
418
419/**
420 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
421 *
422 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200423 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
424 * RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100425 * \param p_rng RNG parameter
426 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200427 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
428 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100429 * \param hash buffer holding the message digest
430 * \param sig buffer that will hold the ciphertext
431 *
432 * \return 0 if the signing operation was successful,
433 * or an POLARSSL_ERR_RSA_XXX error code
434 *
435 * \note The "sig" buffer must be as large as the size
436 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
437 *
438 * \note In case of PKCS#1 v2.1 encoding keep in mind that
439 * the hash_id in the RSA context is the one used for the
440 * encoding. hash_id in the function call is the type of hash
441 * that is encoded. According to RFC 3447 it is advised to
442 * keep both hashes the same.
443 */
444int rsa_rsassa_pss_sign( rsa_context *ctx,
445 int (*f_rng)(void *, unsigned char *, size_t),
446 void *p_rng,
447 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200448 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100449 unsigned int hashlen,
450 const unsigned char *hash,
451 unsigned char *sig );
452
453/**
454 * \brief Generic wrapper to perform a PKCS#1 verification using the
455 * mode from the context. Do a public RSA operation and check
456 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 *
458 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200459 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200460 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200462 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
463 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000464 * \param hash buffer holding the message digest
465 * \param sig buffer holding the ciphertext
466 *
467 * \return 0 if the verify operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000468 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 *
470 * \note The "sig" buffer must be as large as the size
471 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000472 *
473 * \note In case of PKCS#1 v2.1 encoding keep in mind that
474 * the hash_id in the RSA context is the one used for the
475 * verification. hash_id in the function call is the type of hash
476 * that is verified. According to RFC 3447 it is advised to
477 * keep both hashes the same.
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 */
479int rsa_pkcs1_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200480 int (*f_rng)(void *, unsigned char *, size_t),
481 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000482 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200483 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000484 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000485 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200486 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000487
488/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100489 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
490 *
491 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200492 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200493 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100494 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200495 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
496 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100497 * \param hash buffer holding the message digest
498 * \param sig buffer holding the ciphertext
499 *
500 * \return 0 if the verify operation was successful,
501 * or an POLARSSL_ERR_RSA_XXX error code
502 *
503 * \note The "sig" buffer must be as large as the size
504 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
505 */
506int rsa_rsassa_pkcs1_v15_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 Bakkerb3869132013-02-28 17:21:01 +0100509 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200510 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100511 unsigned int hashlen,
512 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200513 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100514
515/**
516 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
517 * \brief Do a public RSA and check the message digest
518 *
519 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200520 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200521 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100522 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200523 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
524 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100525 * \param hash buffer holding the message digest
526 * \param sig buffer holding the ciphertext
527 *
528 * \return 0 if the verify operation was successful,
529 * or an POLARSSL_ERR_RSA_XXX error code
530 *
531 * \note The "sig" buffer must be as large as the size
532 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
533 *
534 * \note In case of PKCS#1 v2.1 encoding keep in mind that
535 * the hash_id in the RSA context is the one used for the
536 * verification. hash_id in the function call is the type of hash
537 * that is verified. According to RFC 3447 it is advised to
538 * keep both hashes the same.
539 */
540int rsa_rsassa_pss_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200541 int (*f_rng)(void *, unsigned char *, size_t),
542 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100543 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200544 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100545 unsigned int hashlen,
546 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200547 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100548
549/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200550 * \brief Copy the components of an RSA context
551 *
552 * \param dst Destination context
553 * \param src Source context
554 *
555 * \return O on success,
556 * POLARSSL_ERR_MPI_MALLOC_FAILED on memory allocation failure
557 */
558int rsa_copy( rsa_context *dst, const rsa_context *src );
559
560/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000562 *
563 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 */
565void rsa_free( rsa_context *ctx );
566
567/**
568 * \brief Checkup routine
569 *
570 * \return 0 if successful, or 1 if the test failed
571 */
572int rsa_self_test( int verbose );
573
574#ifdef __cplusplus
575}
576#endif
577
Paul Bakkered27a042013-04-18 22:46:23 +0200578#endif /* POLARSSL_RSA_C */
579
Paul Bakker5121ce52009-01-03 21:22:43 +0000580#endif /* rsa.h */