blob: 15b047cf1d2c8cdb148a4c85f752b101c9306ca2 [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 Bakker8e831ed2009-01-03 21:24:11 +000030#include "polarssl/bignum.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker13e2dfe2009-07-28 07:18:38 +000032/*
33 * RSA Error codes
34 */
Paul Bakker3391b122009-07-28 20:11:54 +000035#define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x0400
36#define POLARSSL_ERR_RSA_INVALID_PADDING -0x0410
37#define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x0420
38#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x0430
39#define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x0440
40#define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x0450
41#define POLARSSL_ERR_RSA_VERIFY_FAILED -0x0460
42#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x0470
Paul Bakkerb572adf2010-07-18 08:29:32 +000043#define POLARSSL_ERR_RSA_RNG_FAILED -0x0480
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 Bakker4593aea2009-02-09 22:32:35 +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 Bakker4593aea2009-02-09 22:32:35 +000067#define ASN1_STR_CONSTRUCTED_SEQUENCE "\x30"
Paul Bakker13e2dfe2009-07-28 07:18:38 +000068#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 Bakker13e2dfe2009-07-28 07:18:38 +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 Bakker13e2dfe2009-07-28 07:18:38 +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 Bakker13e2dfe2009-07-28 07:18:38 +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 Bakker13e2dfe2009-07-28 07:18:38 +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 Bakker13e2dfe2009-07-28 07:18:38 +000099#define ASN1_HASH_MDX \
100( \
Paul Bakker4593aea2009-02-09 22:32:35 +0000101 ASN1_STR_CONSTRUCTED_SEQUENCE "\x20" \
102 ASN1_STR_CONSTRUCTED_SEQUENCE "\x0C" \
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000103 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 Bakker13e2dfe2009-07-28 07:18:38 +0000109#define ASN1_HASH_SHA1 \
Paul Bakker4593aea2009-02-09 22:32:35 +0000110 ASN1_STR_CONSTRUCTED_SEQUENCE "\x21" \
111 ASN1_STR_CONSTRUCTED_SEQUENCE "\x09" \
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000112 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 Bakker13e2dfe2009-07-28 07:18:38 +0000117#define ASN1_HASH_SHA2X \
Paul Bakker4593aea2009-02-09 22:32:35 +0000118 ASN1_STR_CONSTRUCTED_SEQUENCE "\x11" \
119 ASN1_STR_CONSTRUCTED_SEQUENCE "\x0d" \
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000120 ASN1_STR_OID "\x09" \
121 OID_HASH_ALG_SHA2X \
122 ASN1_STR_NULL "\x00" \
Paul Bakker4593aea2009-02-09 22:32:35 +0000123 ASN1_STR_OCTET_STRING "\x00"
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
125/**
126 * \brief RSA context structure
127 */
128typedef struct
129{
130 int ver; /*!< always 0 */
131 int len; /*!< size(N) in chars */
132
133 mpi N; /*!< public modulus */
134 mpi E; /*!< public exponent */
135
136 mpi D; /*!< private exponent */
137 mpi P; /*!< 1st prime factor */
138 mpi Q; /*!< 2nd prime factor */
139 mpi DP; /*!< D % (P - 1) */
140 mpi DQ; /*!< D % (Q - 1) */
141 mpi QP; /*!< 1 / (Q % P) */
142
143 mpi RN; /*!< cached R^2 mod N */
144 mpi RP; /*!< cached R^2 mod P */
145 mpi RQ; /*!< cached R^2 mod Q */
146
147 int padding; /*!< 1.5 or OAEP/PSS */
148 int hash_id; /*!< hash identifier */
Paul Bakker5121ce52009-01-03 21:22:43 +0000149}
150rsa_context;
151
152#ifdef __cplusplus
153extern "C" {
154#endif
155
156/**
157 * \brief Initialize an RSA context
158 *
159 * \param ctx RSA context to be initialized
160 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
161 * \param hash_id RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 *
163 * \note The hash_id parameter is actually ignored
164 * when using RSA_PKCS_V15 padding.
165 *
Paul Bakkerc38b2f52010-07-18 10:06:45 +0000166 * \note Currently, RSA_PKCS_V21 padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 * is not supported.
168 */
169void rsa_init( rsa_context *ctx,
170 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000171 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
173/**
174 * \brief Generate an RSA keypair
175 *
176 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000177 * \param f_rng RNG function
178 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 * \param nbits size of the public key in bits
180 * \param exponent public exponent (e.g., 65537)
181 *
182 * \note rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000183 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 *
Paul Bakker40e46942009-01-03 21:51:57 +0000185 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 */
Paul Bakker21eb2802010-08-16 11:10:02 +0000187int rsa_gen_key( rsa_context *ctx,
188 int (*f_rng)(void *),
189 void *p_rng,
190 int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
192/**
193 * \brief Check a public RSA key
194 *
195 * \param ctx RSA context to be checked
196 *
Paul Bakker40e46942009-01-03 21:51:57 +0000197 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000199int rsa_check_pubkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
201/**
202 * \brief Check a private RSA key
203 *
204 * \param ctx RSA context to be checked
205 *
Paul Bakker40e46942009-01-03 21:51:57 +0000206 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000208int rsa_check_privkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
210/**
211 * \brief Do an RSA public key operation
212 *
213 * \param ctx RSA context
214 * \param input input buffer
215 * \param output output buffer
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 *
219 * \note This function does NOT take care of message
Paul Bakker619467a2009-03-28 23:26:51 +0000220 * padding. Also, be sure to set input[0] = 0 or assure that
221 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 *
223 * \note The input and output buffers must be large
224 * enough (eg. 128 bytes if RSA-1024 is used).
225 */
226int rsa_public( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000227 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 unsigned char *output );
229
230/**
231 * \brief Do an RSA private key operation
232 *
233 * \param ctx RSA context
234 * \param input input buffer
235 * \param output output buffer
236 *
Paul Bakker40e46942009-01-03 21:51:57 +0000237 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 *
239 * \note The input and output buffers must be large
240 * enough (eg. 128 bytes if RSA-1024 is used).
241 */
242int rsa_private( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000243 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 unsigned char *output );
245
246/**
247 * \brief Add the message padding, then do an RSA operation
248 *
249 * \param ctx RSA context
Paul Bakker21eb2802010-08-16 11:10:02 +0000250 * \param f_rng RNG function
251 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000253 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 * \param input buffer holding the data to be encrypted
255 * \param output buffer that will hold the ciphertext
256 *
Paul Bakker40e46942009-01-03 21:51:57 +0000257 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 *
259 * \note The output buffer must be as large as the size
260 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
261 */
262int rsa_pkcs1_encrypt( rsa_context *ctx,
Paul Bakker21eb2802010-08-16 11:10:02 +0000263 int (*f_rng)(void *),
264 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 int mode, int ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000266 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 unsigned char *output );
268
269/**
270 * \brief Do an RSA operation, then remove the message padding
271 *
272 * \param ctx RSA context
273 * \param mode RSA_PUBLIC or RSA_PRIVATE
274 * \param input buffer holding the encrypted data
275 * \param output buffer that will hold the plaintext
276 * \param olen will contain the plaintext length
Paul Bakker060c5682009-01-12 21:48:39 +0000277 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 *
Paul Bakker40e46942009-01-03 21:51:57 +0000279 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000280 *
281 * \note The output buffer must be as large as the size
Paul Bakker060c5682009-01-12 21:48:39 +0000282 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
283 * an error is thrown.
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 */
285int rsa_pkcs1_decrypt( rsa_context *ctx,
286 int mode, int *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000287 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000288 unsigned char *output,
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000289 int output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000290
291/**
292 * \brief Do a private RSA to sign a message digest
293 *
294 * \param ctx RSA context
295 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerfc22c442009-07-19 20:36:27 +0000296 * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
297 * \param hashlen message digest length (for SIG_RSA_RAW only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 * \param hash buffer holding the message digest
299 * \param sig buffer that will hold the ciphertext
300 *
301 * \return 0 if the signing operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000302 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 *
304 * \note The "sig" buffer must be as large as the size
305 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
306 */
307int rsa_pkcs1_sign( rsa_context *ctx,
308 int mode,
309 int hash_id,
310 int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000311 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 unsigned char *sig );
313
314/**
315 * \brief Do a public RSA and check the message digest
316 *
317 * \param ctx points to an RSA public key
318 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerb924f042010-07-18 08:49:19 +0000319 * \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 +0000320 * \param hashlen message digest length (for SIG_RSA_RAW only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 * \param hash buffer holding the message digest
322 * \param sig buffer holding the ciphertext
323 *
324 * \return 0 if the verify operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000325 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 *
327 * \note The "sig" buffer must be as large as the size
328 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
329 */
330int rsa_pkcs1_verify( rsa_context *ctx,
331 int mode,
332 int hash_id,
333 int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000334 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000335 unsigned char *sig );
336
337/**
338 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000339 *
340 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000341 */
342void rsa_free( rsa_context *ctx );
343
344/**
345 * \brief Checkup routine
346 *
347 * \return 0 if successful, or 1 if the test failed
348 */
349int rsa_self_test( int verbose );
350
351#ifdef __cplusplus
352}
353#endif
354
355#endif /* rsa.h */