blob: 17f1e267b8d41435d3e884496f696553908df69e [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 Bakker530927b2015-02-13 14:24:10 +01006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00008 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00009 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Paul Bakker40e46942009-01-03 21:51:57 +000024#ifndef POLARSSL_RSA_H
25#define POLARSSL_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Paul Bakker314052f2011-08-15 09:07:52 +000027#include "bignum.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Paul Bakker13e2dfe2009-07-28 07:18:38 +000029/*
30 * RSA Error codes
31 */
Paul Bakker9d781402011-05-09 16:17:09 +000032#define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
33#define POLARSSL_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
34#define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
35#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the libraries validity check. */
36#define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
37#define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
38#define POLARSSL_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
39#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
40#define POLARSSL_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Paul Bakker5121ce52009-01-03 21:22:43 +000041
42/*
43 * PKCS#1 constants
44 */
Paul Bakkerfc22c442009-07-19 20:36:27 +000045#define SIG_RSA_RAW 0
46#define SIG_RSA_MD2 2
47#define SIG_RSA_MD4 3
48#define SIG_RSA_MD5 4
Paul Bakker23986e52011-04-24 08:57:21 +000049#define SIG_RSA_SHA1 5
50#define SIG_RSA_SHA224 14
51#define SIG_RSA_SHA256 11
52#define SIG_RSA_SHA384 12
53#define SIG_RSA_SHA512 13
Paul Bakker5121ce52009-01-03 21:22:43 +000054
55#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
Paul Bakker23986e52011-04-24 08:57:21 +000064#define ASN1_STR_CONSTRUCTED_SEQUENCE "\x30"
65#define ASN1_STR_NULL "\x05"
66#define ASN1_STR_OID "\x06"
67#define ASN1_STR_OCTET_STRING "\x04"
Paul Bakker4593aea2009-02-09 22:32:35 +000068
Paul Bakker23986e52011-04-24 08:57:21 +000069#define OID_DIGEST_ALG_MDX "\x2A\x86\x48\x86\xF7\x0D\x02\x00"
70#define OID_HASH_ALG_SHA1 "\x2b\x0e\x03\x02\x1a"
71#define OID_HASH_ALG_SHA2X "\x60\x86\x48\x01\x65\x03\x04\x02\x00"
Paul Bakker4593aea2009-02-09 22:32:35 +000072
Paul Bakker23986e52011-04-24 08:57:21 +000073#define OID_ISO_MEMBER_BODIES "\x2a"
74#define OID_ISO_IDENTIFIED_ORG "\x2b"
Paul Bakker4593aea2009-02-09 22:32:35 +000075
76/*
77 * ISO Member bodies OID parts
78 */
Paul Bakker23986e52011-04-24 08:57:21 +000079#define OID_COUNTRY_US "\x86\x48"
80#define OID_RSA_DATA_SECURITY "\x86\xf7\x0d"
Paul Bakker4593aea2009-02-09 22:32:35 +000081
82/*
83 * ISO Identified organization OID parts
84 */
Paul Bakker23986e52011-04-24 08:57:21 +000085#define OID_OIW_SECSIG_SHA1 "\x0e\x03\x02\x1a"
Paul Bakker4593aea2009-02-09 22:32:35 +000086
Paul Bakker5121ce52009-01-03 21:22:43 +000087/*
88 * DigestInfo ::= SEQUENCE {
89 * digestAlgorithm DigestAlgorithmIdentifier,
90 * digest Digest }
91 *
92 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
93 *
94 * Digest ::= OCTET STRING
95 */
Paul Bakker23986e52011-04-24 08:57:21 +000096#define ASN1_HASH_MDX \
97( \
98 ASN1_STR_CONSTRUCTED_SEQUENCE "\x20" \
99 ASN1_STR_CONSTRUCTED_SEQUENCE "\x0C" \
100 ASN1_STR_OID "\x08" \
101 OID_DIGEST_ALG_MDX \
102 ASN1_STR_NULL "\x00" \
103 ASN1_STR_OCTET_STRING "\x10" \
Paul Bakker4593aea2009-02-09 22:32:35 +0000104)
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Paul Bakker23986e52011-04-24 08:57:21 +0000106#define ASN1_HASH_SHA1 \
107 ASN1_STR_CONSTRUCTED_SEQUENCE "\x21" \
108 ASN1_STR_CONSTRUCTED_SEQUENCE "\x09" \
109 ASN1_STR_OID "\x05" \
110 OID_HASH_ALG_SHA1 \
111 ASN1_STR_NULL "\x00" \
Paul Bakker4593aea2009-02-09 22:32:35 +0000112 ASN1_STR_OCTET_STRING "\x14"
113
Paul Bakker56a76842012-03-22 15:31:27 +0000114#define ASN1_HASH_SHA1_ALT \
115 ASN1_STR_CONSTRUCTED_SEQUENCE "\x1F" \
116 ASN1_STR_CONSTRUCTED_SEQUENCE "\x07" \
117 ASN1_STR_OID "\x05" \
118 OID_HASH_ALG_SHA1 \
119 ASN1_STR_OCTET_STRING "\x14"
120
Paul Bakker23986e52011-04-24 08:57:21 +0000121#define ASN1_HASH_SHA2X \
122 ASN1_STR_CONSTRUCTED_SEQUENCE "\x11" \
123 ASN1_STR_CONSTRUCTED_SEQUENCE "\x0d" \
124 ASN1_STR_OID "\x09" \
125 OID_HASH_ALG_SHA2X \
126 ASN1_STR_NULL "\x00" \
Paul Bakker4593aea2009-02-09 22:32:35 +0000127 ASN1_STR_OCTET_STRING "\x00"
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
129/**
130 * \brief RSA context structure
131 */
132typedef struct
133{
134 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +0000135 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
137 mpi N; /*!< public modulus */
138 mpi E; /*!< public exponent */
139
140 mpi D; /*!< private exponent */
141 mpi P; /*!< 1st prime factor */
142 mpi Q; /*!< 2nd prime factor */
143 mpi DP; /*!< D % (P - 1) */
144 mpi DQ; /*!< D % (Q - 1) */
145 mpi QP; /*!< 1 / (Q % P) */
146
147 mpi RN; /*!< cached R^2 mod N */
148 mpi RP; /*!< cached R^2 mod P */
149 mpi RQ; /*!< cached R^2 mod Q */
150
Paul Bakker9dcc3222011-03-08 14:16:06 +0000151 int padding; /*!< RSA_PKCS_V15 for 1.5 padding and
152 RSA_PKCS_v21 for OAEP/PSS */
153 int hash_id; /*!< Hash identifier of md_type_t as
154 specified in the md.h header file
155 for the EME-OAEP and EMSA-PSS
156 encoding */
Paul Bakker5121ce52009-01-03 21:22:43 +0000157}
158rsa_context;
159
160#ifdef __cplusplus
161extern "C" {
162#endif
163
164/**
165 * \brief Initialize an RSA context
166 *
Paul Bakker9a736322012-11-14 12:39:52 +0000167 * Note: Set padding to RSA_PKCS_V21 for the RSAES-OAEP
168 * encryption scheme and the RSASSA-PSS signature scheme.
169 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 * \param ctx RSA context to be initialized
171 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
172 * \param hash_id RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 *
174 * \note The hash_id parameter is actually ignored
175 * when using RSA_PKCS_V15 padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 */
177void rsa_init( rsa_context *ctx,
178 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000179 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000180
181/**
182 * \brief Generate an RSA keypair
183 *
184 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000185 * \param f_rng RNG function
186 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 * \param nbits size of the public key in bits
188 * \param exponent public exponent (e.g., 65537)
189 *
190 * \note rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000191 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 *
Paul Bakker40e46942009-01-03 21:51:57 +0000193 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 */
Paul Bakker21eb2802010-08-16 11:10:02 +0000195int rsa_gen_key( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000196 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000197 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000198 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
200/**
201 * \brief Check a public RSA key
202 *
203 * \param ctx RSA context to be checked
204 *
Paul Bakker40e46942009-01-03 21:51:57 +0000205 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000207int rsa_check_pubkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000208
209/**
210 * \brief Check a private RSA key
211 *
212 * \param ctx RSA context to be checked
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 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000216int rsa_check_privkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
218/**
219 * \brief Do an RSA public key operation
220 *
221 * \param ctx RSA context
222 * \param input input buffer
223 * \param output output buffer
224 *
Paul Bakker40e46942009-01-03 21:51:57 +0000225 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 *
227 * \note This function does NOT take care of message
Paul Bakker619467a2009-03-28 23:26:51 +0000228 * padding. Also, be sure to set input[0] = 0 or assure that
229 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 *
231 * \note The input and output buffers must be large
232 * enough (eg. 128 bytes if RSA-1024 is used).
233 */
234int rsa_public( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000235 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 unsigned char *output );
237
238/**
239 * \brief Do an RSA private key operation
240 *
241 * \param ctx RSA context
Paul Bakker43f97992013-09-23 11:23:31 +0200242 * \param f_rng RNG function (Needed for blinding)
243 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 * \param input input buffer
245 * \param output output buffer
246 *
Paul Bakker40e46942009-01-03 21:51:57 +0000247 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 *
249 * \note The input and output buffers must be large
250 * enough (eg. 128 bytes if RSA-1024 is used).
251 */
252int rsa_private( rsa_context *ctx,
Paul Bakker43f97992013-09-23 11:23:31 +0200253 int (*f_rng)(void *, unsigned char *, size_t),
254 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000255 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000256 unsigned char *output );
257
258/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100259 * \brief Generic wrapper to perform a PKCS#1 encryption using the
260 * mode from the context. Add the message padding, then do an
261 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 *
263 * \param ctx RSA context
Paul Bakker43f97992013-09-23 11:23:31 +0200264 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
265 * and RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000266 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000268 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 * \param input buffer holding the data to be encrypted
270 * \param output buffer that will hold the ciphertext
271 *
Paul Bakker40e46942009-01-03 21:51:57 +0000272 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 *
274 * \note The output buffer must be as large as the size
275 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
276 */
277int rsa_pkcs1_encrypt( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000278 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000279 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000280 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000281 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 unsigned char *output );
283
284/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100285 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
286 *
287 * \param ctx RSA context
Paul Bakker43f97992013-09-23 11:23:31 +0200288 * \param f_rng RNG function (Needed for padding and RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100289 * \param p_rng RNG parameter
290 * \param mode RSA_PUBLIC or RSA_PRIVATE
291 * \param ilen contains the plaintext length
292 * \param input buffer holding the data to be encrypted
293 * \param output buffer that will hold the ciphertext
294 *
295 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
296 *
297 * \note The output buffer must be as large as the size
298 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
299 */
300int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx,
301 int (*f_rng)(void *, unsigned char *, size_t),
302 void *p_rng,
303 int mode, size_t ilen,
304 const unsigned char *input,
305 unsigned char *output );
306
307/**
308 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
309 *
310 * \param ctx RSA context
Paul Bakker43f97992013-09-23 11:23:31 +0200311 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
312 * and RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100313 * \param p_rng RNG parameter
314 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100315 * \param label buffer holding the custom label to use
316 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100317 * \param ilen contains the plaintext length
318 * \param input buffer holding the data to be encrypted
319 * \param output buffer that will hold the ciphertext
320 *
321 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
322 *
323 * \note The output buffer must be as large as the size
324 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
325 */
326int rsa_rsaes_oaep_encrypt( rsa_context *ctx,
327 int (*f_rng)(void *, unsigned char *, size_t),
328 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100329 int mode,
330 const unsigned char *label, size_t label_len,
331 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100332 const unsigned char *input,
333 unsigned char *output );
334
335/**
336 * \brief Generic wrapper to perform a PKCS#1 decryption using the
337 * mode from the context. Do an RSA operation, then remove
338 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 *
340 * \param ctx RSA context
Paul Bakker43f97992013-09-23 11:23:31 +0200341 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
342 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000344 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 * \param input buffer holding the encrypted data
346 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000347 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 *
Paul Bakker40e46942009-01-03 21:51:57 +0000349 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 *
351 * \note The output buffer must be as large as the size
Paul Bakker060c5682009-01-12 21:48:39 +0000352 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
353 * an error is thrown.
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 */
355int rsa_pkcs1_decrypt( rsa_context *ctx,
Paul Bakker43f97992013-09-23 11:23:31 +0200356 int (*f_rng)(void *, unsigned char *, size_t),
357 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000358 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000359 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000360 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000361 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000362
363/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100364 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
365 *
366 * \param ctx RSA context
Paul Bakker43f97992013-09-23 11:23:31 +0200367 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
368 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100369 * \param mode RSA_PUBLIC or RSA_PRIVATE
370 * \param olen will contain the plaintext length
371 * \param input buffer holding the encrypted data
372 * \param output buffer that will hold the plaintext
373 * \param output_max_len maximum length of the output buffer
374 *
375 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
376 *
377 * \note The output buffer must be as large as the size
378 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
379 * an error is thrown.
380 */
381int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx,
Paul Bakker43f97992013-09-23 11:23:31 +0200382 int (*f_rng)(void *, unsigned char *, size_t),
383 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100384 int mode, size_t *olen,
385 const unsigned char *input,
386 unsigned char *output,
387 size_t output_max_len );
388
389/**
390 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
391 *
392 * \param ctx RSA context
Paul Bakker43f97992013-09-23 11:23:31 +0200393 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
394 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100395 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100396 * \param label buffer holding the custom label to use
397 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100398 * \param olen will contain the plaintext length
399 * \param input buffer holding the encrypted data
400 * \param output buffer that will hold the plaintext
401 * \param output_max_len maximum length of the output buffer
402 *
403 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
404 *
405 * \note The output buffer must be as large as the size
406 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
407 * an error is thrown.
408 */
409int rsa_rsaes_oaep_decrypt( rsa_context *ctx,
Paul Bakker43f97992013-09-23 11:23:31 +0200410 int (*f_rng)(void *, unsigned char *, size_t),
411 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100412 int mode,
413 const unsigned char *label, size_t label_len,
414 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100415 const unsigned char *input,
416 unsigned char *output,
417 size_t output_max_len );
418
419/**
420 * \brief Generic wrapper to perform a PKCS#1 signature using the
421 * mode from the context. Do a private RSA operation to sign
422 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000423 *
424 * \param ctx RSA context
Paul Bakker43f97992013-09-23 11:23:31 +0200425 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
426 * RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000427 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerfc22c442009-07-19 20:36:27 +0000429 * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
430 * \param hashlen message digest length (for SIG_RSA_RAW only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000431 * \param hash buffer holding the message digest
432 * \param sig buffer that will hold the ciphertext
433 *
434 * \return 0 if the signing operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000435 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000436 *
437 * \note The "sig" buffer must be as large as the size
438 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000439 *
440 * \note In case of PKCS#1 v2.1 encoding keep in mind that
441 * the hash_id in the RSA context is the one used for the
442 * encoding. hash_id in the function call is the type of hash
443 * that is encoded. According to RFC 3447 it is advised to
444 * keep both hashes the same.
Paul Bakker5121ce52009-01-03 21:22:43 +0000445 */
446int rsa_pkcs1_sign( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000447 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000448 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000449 int mode,
450 int hash_id,
Paul Bakker23986e52011-04-24 08:57:21 +0000451 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000452 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 unsigned char *sig );
454
455/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100456 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
457 *
458 * \param ctx RSA context
Paul Bakker43f97992013-09-23 11:23:31 +0200459 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
460 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100461 * \param mode RSA_PUBLIC or RSA_PRIVATE
462 * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
463 * \param hashlen message digest length (for SIG_RSA_RAW only)
464 * \param hash buffer holding the message digest
465 * \param sig buffer that will hold the ciphertext
466 *
467 * \return 0 if the signing operation was successful,
468 * or an POLARSSL_ERR_RSA_XXX error code
469 *
470 * \note The "sig" buffer must be as large as the size
471 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
472 */
473int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx,
Paul Bakker43f97992013-09-23 11:23:31 +0200474 int (*f_rng)(void *, unsigned char *, size_t),
475 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100476 int mode,
477 int hash_id,
478 unsigned int hashlen,
479 const unsigned char *hash,
480 unsigned char *sig );
481
482/**
483 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
484 *
485 * \param ctx RSA context
Paul Bakker43f97992013-09-23 11:23:31 +0200486 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
487 * RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100488 * \param p_rng RNG parameter
489 * \param mode RSA_PUBLIC or RSA_PRIVATE
490 * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
491 * \param hashlen message digest length (for SIG_RSA_RAW only)
492 * \param hash buffer holding the message digest
493 * \param sig buffer that will hold the ciphertext
494 *
495 * \return 0 if the signing operation was successful,
496 * or an POLARSSL_ERR_RSA_XXX error code
497 *
498 * \note The "sig" buffer must be as large as the size
499 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
500 *
501 * \note In case of PKCS#1 v2.1 encoding keep in mind that
502 * the hash_id in the RSA context is the one used for the
503 * encoding. hash_id in the function call is the type of hash
504 * that is encoded. According to RFC 3447 it is advised to
505 * keep both hashes the same.
506 */
507int rsa_rsassa_pss_sign( rsa_context *ctx,
508 int (*f_rng)(void *, unsigned char *, size_t),
509 void *p_rng,
510 int mode,
511 int hash_id,
512 unsigned int hashlen,
513 const unsigned char *hash,
514 unsigned char *sig );
515
516/**
517 * \brief Generic wrapper to perform a PKCS#1 verification using the
518 * mode from the context. Do a public RSA operation and check
519 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 *
521 * \param ctx points to an RSA public key
Paul Bakker43f97992013-09-23 11:23:31 +0200522 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
523 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerb924f042010-07-18 08:49:19 +0000525 * \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 +0000526 * \param hashlen message digest length (for SIG_RSA_RAW only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000527 * \param hash buffer holding the message digest
528 * \param sig buffer holding the ciphertext
529 *
530 * \return 0 if the verify operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000531 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 *
533 * \note The "sig" buffer must be as large as the size
534 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000535 *
536 * \note In case of PKCS#1 v2.1 encoding keep in mind that
537 * the hash_id in the RSA context is the one used for the
538 * verification. hash_id in the function call is the type of hash
539 * that is verified. According to RFC 3447 it is advised to
540 * keep both hashes the same.
Paul Bakker5121ce52009-01-03 21:22:43 +0000541 */
542int rsa_pkcs1_verify( rsa_context *ctx,
Paul Bakker43f97992013-09-23 11:23:31 +0200543 int (*f_rng)(void *, unsigned char *, size_t),
544 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 int mode,
546 int hash_id,
Paul Bakker23986e52011-04-24 08:57:21 +0000547 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000548 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 unsigned char *sig );
550
551/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100552 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
553 *
554 * \param ctx points to an RSA public key
Paul Bakker43f97992013-09-23 11:23:31 +0200555 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
556 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100557 * \param mode RSA_PUBLIC or RSA_PRIVATE
558 * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
559 * \param hashlen message digest length (for SIG_RSA_RAW only)
560 * \param hash buffer holding the message digest
561 * \param sig buffer holding the ciphertext
562 *
563 * \return 0 if the verify operation was successful,
564 * or an POLARSSL_ERR_RSA_XXX error code
565 *
566 * \note The "sig" buffer must be as large as the size
567 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
568 */
569int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx,
Paul Bakker43f97992013-09-23 11:23:31 +0200570 int (*f_rng)(void *, unsigned char *, size_t),
571 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100572 int mode,
573 int hash_id,
574 unsigned int hashlen,
575 const unsigned char *hash,
576 unsigned char *sig );
577
578/**
579 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Paul Bakkerb3869132013-02-28 17:21:01 +0100580 *
581 * \param ctx points to an RSA public key
Paul Bakker43f97992013-09-23 11:23:31 +0200582 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
583 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100584 * \param mode RSA_PUBLIC or RSA_PRIVATE
585 * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
586 * \param hashlen message digest length (for SIG_RSA_RAW only)
587 * \param hash buffer holding the message digest
588 * \param sig buffer holding the ciphertext
589 *
590 * \return 0 if the verify operation was successful,
591 * or an POLARSSL_ERR_RSA_XXX error code
592 *
593 * \note The "sig" buffer must be as large as the size
594 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
595 *
596 * \note In case of PKCS#1 v2.1 encoding keep in mind that
597 * the hash_id in the RSA context is the one used for the
598 * verification. hash_id in the function call is the type of hash
599 * that is verified. According to RFC 3447 it is advised to
600 * keep both hashes the same.
601 */
602int rsa_rsassa_pss_verify( rsa_context *ctx,
Paul Bakker43f97992013-09-23 11:23:31 +0200603 int (*f_rng)(void *, unsigned char *, size_t),
604 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100605 int mode,
606 int hash_id,
607 unsigned int hashlen,
608 const unsigned char *hash,
609 unsigned char *sig );
610
611/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000612 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000613 *
614 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000615 */
616void rsa_free( rsa_context *ctx );
617
618/**
619 * \brief Checkup routine
620 *
621 * \return 0 if successful, or 1 if the test failed
622 */
623int rsa_self_test( int verbose );
624
625#ifdef __cplusplus
626}
627#endif
628
629#endif /* rsa.h */