blob: 653354855bce79553c7a0ef25c9c49f65d8a6272 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief The RSA public-key cryptosystem
5 *
Paul Bakker84f12b72010-07-18 10:13:04 +00006 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_RSA_H
28#define POLARSSL_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker314052f2011-08-15 09:07:52 +000030#include "bignum.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker13e2dfe2009-07-28 07:18:38 +000032/*
33 * RSA Error codes
34 */
Paul Bakker9d781402011-05-09 16:17:09 +000035#define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
36#define POLARSSL_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
37#define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
38#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the libraries validity check. */
39#define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
40#define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
41#define POLARSSL_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
42#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
43#define POLARSSL_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Paul Bakker5121ce52009-01-03 21:22:43 +000044
45/*
46 * PKCS#1 constants
47 */
Paul Bakkerfc22c442009-07-19 20:36:27 +000048#define SIG_RSA_RAW 0
49#define SIG_RSA_MD2 2
50#define SIG_RSA_MD4 3
51#define SIG_RSA_MD5 4
Paul Bakker23986e52011-04-24 08:57:21 +000052#define SIG_RSA_SHA1 5
53#define SIG_RSA_SHA224 14
54#define SIG_RSA_SHA256 11
55#define SIG_RSA_SHA384 12
56#define SIG_RSA_SHA512 13
Paul Bakker5121ce52009-01-03 21:22:43 +000057
58#define RSA_PUBLIC 0
59#define RSA_PRIVATE 1
60
61#define RSA_PKCS_V15 0
62#define RSA_PKCS_V21 1
63
64#define RSA_SIGN 1
65#define RSA_CRYPT 2
66
Paul Bakker23986e52011-04-24 08:57:21 +000067#define ASN1_STR_CONSTRUCTED_SEQUENCE "\x30"
68#define ASN1_STR_NULL "\x05"
69#define ASN1_STR_OID "\x06"
70#define ASN1_STR_OCTET_STRING "\x04"
Paul Bakker4593aea2009-02-09 22:32:35 +000071
Paul Bakker23986e52011-04-24 08:57:21 +000072#define OID_DIGEST_ALG_MDX "\x2A\x86\x48\x86\xF7\x0D\x02\x00"
73#define OID_HASH_ALG_SHA1 "\x2b\x0e\x03\x02\x1a"
74#define OID_HASH_ALG_SHA2X "\x60\x86\x48\x01\x65\x03\x04\x02\x00"
Paul Bakker4593aea2009-02-09 22:32:35 +000075
Paul Bakker23986e52011-04-24 08:57:21 +000076#define OID_ISO_MEMBER_BODIES "\x2a"
77#define OID_ISO_IDENTIFIED_ORG "\x2b"
Paul Bakker4593aea2009-02-09 22:32:35 +000078
79/*
80 * ISO Member bodies OID parts
81 */
Paul Bakker23986e52011-04-24 08:57:21 +000082#define OID_COUNTRY_US "\x86\x48"
83#define OID_RSA_DATA_SECURITY "\x86\xf7\x0d"
Paul Bakker4593aea2009-02-09 22:32:35 +000084
85/*
86 * ISO Identified organization OID parts
87 */
Paul Bakker23986e52011-04-24 08:57:21 +000088#define OID_OIW_SECSIG_SHA1 "\x0e\x03\x02\x1a"
Paul Bakker4593aea2009-02-09 22:32:35 +000089
Paul Bakker5121ce52009-01-03 21:22:43 +000090/*
91 * DigestInfo ::= SEQUENCE {
92 * digestAlgorithm DigestAlgorithmIdentifier,
93 * digest Digest }
94 *
95 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
96 *
97 * Digest ::= OCTET STRING
98 */
Paul Bakker23986e52011-04-24 08:57:21 +000099#define ASN1_HASH_MDX \
100( \
101 ASN1_STR_CONSTRUCTED_SEQUENCE "\x20" \
102 ASN1_STR_CONSTRUCTED_SEQUENCE "\x0C" \
103 ASN1_STR_OID "\x08" \
104 OID_DIGEST_ALG_MDX \
105 ASN1_STR_NULL "\x00" \
106 ASN1_STR_OCTET_STRING "\x10" \
Paul Bakker4593aea2009-02-09 22:32:35 +0000107)
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
Paul Bakker23986e52011-04-24 08:57:21 +0000109#define ASN1_HASH_SHA1 \
110 ASN1_STR_CONSTRUCTED_SEQUENCE "\x21" \
111 ASN1_STR_CONSTRUCTED_SEQUENCE "\x09" \
112 ASN1_STR_OID "\x05" \
113 OID_HASH_ALG_SHA1 \
114 ASN1_STR_NULL "\x00" \
Paul Bakker4593aea2009-02-09 22:32:35 +0000115 ASN1_STR_OCTET_STRING "\x14"
116
Paul Bakker56a76842012-03-22 15:31:27 +0000117#define ASN1_HASH_SHA1_ALT \
118 ASN1_STR_CONSTRUCTED_SEQUENCE "\x1F" \
119 ASN1_STR_CONSTRUCTED_SEQUENCE "\x07" \
120 ASN1_STR_OID "\x05" \
121 OID_HASH_ALG_SHA1 \
122 ASN1_STR_OCTET_STRING "\x14"
123
Paul Bakker23986e52011-04-24 08:57:21 +0000124#define ASN1_HASH_SHA2X \
125 ASN1_STR_CONSTRUCTED_SEQUENCE "\x11" \
126 ASN1_STR_CONSTRUCTED_SEQUENCE "\x0d" \
127 ASN1_STR_OID "\x09" \
128 OID_HASH_ALG_SHA2X \
129 ASN1_STR_NULL "\x00" \
Paul Bakker4593aea2009-02-09 22:32:35 +0000130 ASN1_STR_OCTET_STRING "\x00"
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
132/**
133 * \brief RSA context structure
134 */
135typedef struct
136{
137 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +0000138 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140 mpi N; /*!< public modulus */
141 mpi E; /*!< public exponent */
142
143 mpi D; /*!< private exponent */
144 mpi P; /*!< 1st prime factor */
145 mpi Q; /*!< 2nd prime factor */
146 mpi DP; /*!< D % (P - 1) */
147 mpi DQ; /*!< D % (Q - 1) */
148 mpi QP; /*!< 1 / (Q % P) */
149
150 mpi RN; /*!< cached R^2 mod N */
151 mpi RP; /*!< cached R^2 mod P */
152 mpi RQ; /*!< cached R^2 mod Q */
153
Paul Bakker9dcc3222011-03-08 14:16:06 +0000154 int padding; /*!< RSA_PKCS_V15 for 1.5 padding and
155 RSA_PKCS_v21 for OAEP/PSS */
156 int hash_id; /*!< Hash identifier of md_type_t as
157 specified in the md.h header file
158 for the EME-OAEP and EMSA-PSS
159 encoding */
Paul Bakker5121ce52009-01-03 21:22:43 +0000160}
161rsa_context;
162
163#ifdef __cplusplus
164extern "C" {
165#endif
166
167/**
168 * \brief Initialize an RSA context
169 *
Paul Bakker9a736322012-11-14 12:39:52 +0000170 * Note: Set padding to RSA_PKCS_V21 for the RSAES-OAEP
171 * encryption scheme and the RSASSA-PSS signature scheme.
172 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 * \param ctx RSA context to be initialized
174 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
175 * \param hash_id RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 *
177 * \note The hash_id parameter is actually ignored
178 * when using RSA_PKCS_V15 padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 */
180void rsa_init( rsa_context *ctx,
181 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000182 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
184/**
185 * \brief Generate an RSA keypair
186 *
187 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000188 * \param f_rng RNG function
189 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 * \param nbits size of the public key in bits
191 * \param exponent public exponent (e.g., 65537)
192 *
193 * \note rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000194 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 *
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 */
Paul Bakker21eb2802010-08-16 11:10:02 +0000198int rsa_gen_key( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000199 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000200 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000201 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
203/**
204 * \brief Check a public RSA key
205 *
206 * \param ctx RSA context to be checked
207 *
Paul Bakker40e46942009-01-03 21:51:57 +0000208 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000210int rsa_check_pubkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
212/**
213 * \brief Check a private RSA key
214 *
215 * \param ctx RSA context to be checked
216 *
Paul Bakker40e46942009-01-03 21:51:57 +0000217 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000219int rsa_check_privkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
221/**
222 * \brief Do an RSA public key operation
223 *
224 * \param ctx RSA context
225 * \param input input buffer
226 * \param output output buffer
227 *
Paul Bakker40e46942009-01-03 21:51:57 +0000228 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 *
230 * \note This function does NOT take care of message
Paul Bakker619467a2009-03-28 23:26:51 +0000231 * padding. Also, be sure to set input[0] = 0 or assure that
232 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 *
234 * \note The input and output buffers must be large
235 * enough (eg. 128 bytes if RSA-1024 is used).
236 */
237int rsa_public( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000238 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 unsigned char *output );
240
241/**
242 * \brief Do an RSA private key operation
243 *
244 * \param ctx RSA context
Paul Bakker43f97992013-09-23 11:23:31 +0200245 * \param f_rng RNG function (Needed for blinding)
246 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 * \param input input buffer
248 * \param output output buffer
249 *
Paul Bakker40e46942009-01-03 21:51:57 +0000250 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 *
252 * \note The input and output buffers must be large
253 * enough (eg. 128 bytes if RSA-1024 is used).
254 */
255int rsa_private( rsa_context *ctx,
Paul Bakker43f97992013-09-23 11:23:31 +0200256 int (*f_rng)(void *, unsigned char *, size_t),
257 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000258 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 unsigned char *output );
260
261/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100262 * \brief Generic wrapper to perform a PKCS#1 encryption using the
263 * mode from the context. Add the message padding, then do an
264 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 *
266 * \param ctx RSA context
Paul Bakker43f97992013-09-23 11:23:31 +0200267 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
268 * and RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000269 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000271 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 * \param input buffer holding the data to be encrypted
273 * \param output buffer that will hold the ciphertext
274 *
Paul Bakker40e46942009-01-03 21:51:57 +0000275 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000276 *
277 * \note The output buffer must be as large as the size
278 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
279 */
280int rsa_pkcs1_encrypt( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000281 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000282 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000283 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000284 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 unsigned char *output );
286
287/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100288 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
289 *
290 * \param ctx RSA context
Paul Bakker43f97992013-09-23 11:23:31 +0200291 * \param f_rng RNG function (Needed for padding and RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100292 * \param p_rng RNG parameter
293 * \param mode RSA_PUBLIC or RSA_PRIVATE
294 * \param ilen contains the plaintext length
295 * \param input buffer holding the data to be encrypted
296 * \param output buffer that will hold the ciphertext
297 *
298 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
299 *
300 * \note The output buffer must be as large as the size
301 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
302 */
303int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx,
304 int (*f_rng)(void *, unsigned char *, size_t),
305 void *p_rng,
306 int mode, size_t ilen,
307 const unsigned char *input,
308 unsigned char *output );
309
310/**
311 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
312 *
313 * \param ctx RSA context
Paul Bakker43f97992013-09-23 11:23:31 +0200314 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
315 * and RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100316 * \param p_rng RNG parameter
317 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100318 * \param label buffer holding the custom label to use
319 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100320 * \param ilen contains the plaintext length
321 * \param input buffer holding the data to be encrypted
322 * \param output buffer that will hold the ciphertext
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).
328 */
329int rsa_rsaes_oaep_encrypt( rsa_context *ctx,
330 int (*f_rng)(void *, unsigned char *, size_t),
331 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100332 int mode,
333 const unsigned char *label, size_t label_len,
334 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100335 const unsigned char *input,
336 unsigned char *output );
337
338/**
339 * \brief Generic wrapper to perform a PKCS#1 decryption using the
340 * mode from the context. Do an RSA operation, then remove
341 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 *
343 * \param ctx RSA context
Paul Bakker43f97992013-09-23 11:23:31 +0200344 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
345 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000346 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000347 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 * \param input buffer holding the encrypted data
349 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000350 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000351 *
Paul Bakker40e46942009-01-03 21:51:57 +0000352 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000353 *
354 * \note The output buffer must be as large as the size
Paul Bakker060c5682009-01-12 21:48:39 +0000355 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
356 * an error is thrown.
Paul Bakker5121ce52009-01-03 21:22:43 +0000357 */
358int rsa_pkcs1_decrypt( rsa_context *ctx,
Paul Bakker43f97992013-09-23 11:23:31 +0200359 int (*f_rng)(void *, unsigned char *, size_t),
360 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000361 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000362 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000363 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000364 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000365
366/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100367 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
368 *
369 * \param ctx RSA context
Paul Bakker43f97992013-09-23 11:23:31 +0200370 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
371 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100372 * \param mode RSA_PUBLIC or RSA_PRIVATE
373 * \param olen will contain the plaintext length
374 * \param input buffer holding the encrypted data
375 * \param output buffer that will hold the plaintext
376 * \param output_max_len maximum length of the output buffer
377 *
378 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
379 *
380 * \note The output buffer must be as large as the size
381 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
382 * an error is thrown.
383 */
384int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx,
Paul Bakker43f97992013-09-23 11:23:31 +0200385 int (*f_rng)(void *, unsigned char *, size_t),
386 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100387 int mode, size_t *olen,
388 const unsigned char *input,
389 unsigned char *output,
390 size_t output_max_len );
391
392/**
393 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
394 *
395 * \param ctx RSA context
Paul Bakker43f97992013-09-23 11:23:31 +0200396 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
397 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100398 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100399 * \param label buffer holding the custom label to use
400 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100401 * \param olen will contain the plaintext length
402 * \param input buffer holding the encrypted data
403 * \param output buffer that will hold the plaintext
404 * \param output_max_len maximum length of the output buffer
405 *
406 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
407 *
408 * \note The output buffer must be as large as the size
409 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
410 * an error is thrown.
411 */
412int rsa_rsaes_oaep_decrypt( rsa_context *ctx,
Paul Bakker43f97992013-09-23 11:23:31 +0200413 int (*f_rng)(void *, unsigned char *, size_t),
414 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100415 int mode,
416 const unsigned char *label, size_t label_len,
417 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100418 const unsigned char *input,
419 unsigned char *output,
420 size_t output_max_len );
421
422/**
423 * \brief Generic wrapper to perform a PKCS#1 signature using the
424 * mode from the context. Do a private RSA operation to sign
425 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000426 *
427 * \param ctx RSA context
Paul Bakker43f97992013-09-23 11:23:31 +0200428 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
429 * RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000430 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000431 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerfc22c442009-07-19 20:36:27 +0000432 * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
433 * \param hashlen message digest length (for SIG_RSA_RAW only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 * \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,
Paul Bakker40e46942009-01-03 21:51:57 +0000438 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 *
440 * \note The "sig" buffer must be as large as the size
441 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000442 *
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.
Paul Bakker5121ce52009-01-03 21:22:43 +0000448 */
449int rsa_pkcs1_sign( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000450 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000451 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 int mode,
453 int hash_id,
Paul Bakker23986e52011-04-24 08:57:21 +0000454 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000455 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 unsigned char *sig );
457
458/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100459 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
460 *
461 * \param ctx RSA context
Paul Bakker43f97992013-09-23 11:23:31 +0200462 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
463 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100464 * \param mode RSA_PUBLIC or RSA_PRIVATE
465 * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
466 * \param hashlen message digest length (for SIG_RSA_RAW only)
467 * \param hash buffer holding the message digest
468 * \param sig buffer that will hold the ciphertext
469 *
470 * \return 0 if the signing operation was successful,
471 * or an POLARSSL_ERR_RSA_XXX error code
472 *
473 * \note The "sig" buffer must be as large as the size
474 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
475 */
476int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx,
Paul Bakker43f97992013-09-23 11:23:31 +0200477 int (*f_rng)(void *, unsigned char *, size_t),
478 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100479 int mode,
480 int hash_id,
481 unsigned int hashlen,
482 const unsigned char *hash,
483 unsigned char *sig );
484
485/**
486 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
487 *
488 * \param ctx RSA context
Paul Bakker43f97992013-09-23 11:23:31 +0200489 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
490 * RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100491 * \param p_rng RNG parameter
492 * \param mode RSA_PUBLIC or RSA_PRIVATE
493 * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
494 * \param hashlen message digest length (for SIG_RSA_RAW only)
495 * \param hash buffer holding the message digest
496 * \param sig buffer that will hold the ciphertext
497 *
498 * \return 0 if the signing operation was successful,
499 * or an POLARSSL_ERR_RSA_XXX error code
500 *
501 * \note The "sig" buffer must be as large as the size
502 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
503 *
504 * \note In case of PKCS#1 v2.1 encoding keep in mind that
505 * the hash_id in the RSA context is the one used for the
506 * encoding. hash_id in the function call is the type of hash
507 * that is encoded. According to RFC 3447 it is advised to
508 * keep both hashes the same.
509 */
510int rsa_rsassa_pss_sign( rsa_context *ctx,
511 int (*f_rng)(void *, unsigned char *, size_t),
512 void *p_rng,
513 int mode,
514 int hash_id,
515 unsigned int hashlen,
516 const unsigned char *hash,
517 unsigned char *sig );
518
519/**
520 * \brief Generic wrapper to perform a PKCS#1 verification using the
521 * mode from the context. Do a public RSA operation and check
522 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 *
524 * \param ctx points to an RSA public key
Paul Bakker43f97992013-09-23 11:23:31 +0200525 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
526 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000527 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerb924f042010-07-18 08:49:19 +0000528 * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
Paul Bakkerfc22c442009-07-19 20:36:27 +0000529 * \param hashlen message digest length (for SIG_RSA_RAW only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000530 * \param hash buffer holding the message digest
531 * \param sig buffer holding the ciphertext
532 *
533 * \return 0 if the verify operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000534 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 *
536 * \note The "sig" buffer must be as large as the size
537 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000538 *
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.
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 */
545int rsa_pkcs1_verify( rsa_context *ctx,
Paul Bakker43f97992013-09-23 11:23:31 +0200546 int (*f_rng)(void *, unsigned char *, size_t),
547 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000548 int mode,
549 int hash_id,
Paul Bakker23986e52011-04-24 08:57:21 +0000550 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000551 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 unsigned char *sig );
553
554/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100555 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
556 *
557 * \param ctx points to an RSA public key
Paul Bakker43f97992013-09-23 11:23:31 +0200558 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
559 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100560 * \param mode RSA_PUBLIC or RSA_PRIVATE
561 * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
562 * \param hashlen message digest length (for SIG_RSA_RAW only)
563 * \param hash buffer holding the message digest
564 * \param sig buffer holding the ciphertext
565 *
566 * \return 0 if the verify operation was successful,
567 * or an POLARSSL_ERR_RSA_XXX error code
568 *
569 * \note The "sig" buffer must be as large as the size
570 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
571 */
572int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx,
Paul Bakker43f97992013-09-23 11:23:31 +0200573 int (*f_rng)(void *, unsigned char *, size_t),
574 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100575 int mode,
576 int hash_id,
577 unsigned int hashlen,
578 const unsigned char *hash,
579 unsigned char *sig );
580
581/**
582 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Paul Bakkerb3869132013-02-28 17:21:01 +0100583 *
584 * \param ctx points to an RSA public key
Paul Bakker43f97992013-09-23 11:23:31 +0200585 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
586 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100587 * \param mode RSA_PUBLIC or RSA_PRIVATE
588 * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
589 * \param hashlen message digest length (for SIG_RSA_RAW only)
590 * \param hash buffer holding the message digest
591 * \param sig buffer holding the ciphertext
592 *
593 * \return 0 if the verify operation was successful,
594 * or an POLARSSL_ERR_RSA_XXX error code
595 *
596 * \note The "sig" buffer must be as large as the size
597 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
598 *
599 * \note In case of PKCS#1 v2.1 encoding keep in mind that
600 * the hash_id in the RSA context is the one used for the
601 * verification. hash_id in the function call is the type of hash
602 * that is verified. According to RFC 3447 it is advised to
603 * keep both hashes the same.
604 */
605int rsa_rsassa_pss_verify( rsa_context *ctx,
Paul Bakker43f97992013-09-23 11:23:31 +0200606 int (*f_rng)(void *, unsigned char *, size_t),
607 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100608 int mode,
609 int hash_id,
610 unsigned int hashlen,
611 const unsigned char *hash,
612 unsigned char *sig );
613
614/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000615 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000616 *
617 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 */
619void rsa_free( rsa_context *ctx );
620
621/**
622 * \brief Checkup routine
623 *
624 * \return 0 if successful, or 1 if the test failed
625 */
626int rsa_self_test( int verbose );
627
628#ifdef __cplusplus
629}
630#endif
631
632#endif /* rsa.h */