blob: 2e8e40cc8729bb8a1b26f859056482f91fb5f8c6 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
5 *
Paul Bakker785a9ee2009-01-25 14:15:10 +00006 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000021 */
Paul Bakker40e46942009-01-03 21:51:57 +000022#ifndef POLARSSL_RSA_H
23#define POLARSSL_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000024
Paul Bakker8e831ed2009-01-03 21:24:11 +000025#include "polarssl/bignum.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Paul Bakker13e2dfe2009-07-28 07:18:38 +000027/*
28 * RSA Error codes
29 */
Paul Bakkerb5bf1762009-07-19 20:28:35 +000030#define POLARSSL_ERR_RSA_BAD_INPUT_DATA 0x0400
31#define POLARSSL_ERR_RSA_INVALID_PADDING 0x0410
32#define POLARSSL_ERR_RSA_KEY_GEN_FAILED 0x0420
33#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED 0x0430
34#define POLARSSL_ERR_RSA_PUBLIC_FAILED 0x0440
35#define POLARSSL_ERR_RSA_PRIVATE_FAILED 0x0450
36#define POLARSSL_ERR_RSA_VERIFY_FAILED 0x0460
Paul Bakker38e2b482009-07-19 20:41:06 +000037#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE 0x0470
Paul Bakker5121ce52009-01-03 21:22:43 +000038
39/*
40 * PKCS#1 constants
41 */
Paul Bakkerfc22c442009-07-19 20:36:27 +000042#define SIG_RSA_RAW 0
43#define SIG_RSA_MD2 2
44#define SIG_RSA_MD4 3
45#define SIG_RSA_MD5 4
Paul Bakker4593aea2009-02-09 22:32:35 +000046#define SIG_RSA_SHA1 5
47#define SIG_RSA_SHA224 14
48#define SIG_RSA_SHA256 11
49#define SIG_RSA_SHA384 12
50#define SIG_RSA_SHA512 13
Paul Bakker5121ce52009-01-03 21:22:43 +000051
52#define RSA_PUBLIC 0
53#define RSA_PRIVATE 1
54
55#define RSA_PKCS_V15 0
56#define RSA_PKCS_V21 1
57
58#define RSA_SIGN 1
59#define RSA_CRYPT 2
60
Paul Bakker4593aea2009-02-09 22:32:35 +000061#define ASN1_STR_CONSTRUCTED_SEQUENCE "\x30"
Paul Bakker13e2dfe2009-07-28 07:18:38 +000062#define ASN1_STR_NULL "\x05"
63#define ASN1_STR_OID "\x06"
64#define ASN1_STR_OCTET_STRING "\x04"
Paul Bakker4593aea2009-02-09 22:32:35 +000065
Paul Bakker13e2dfe2009-07-28 07:18:38 +000066#define OID_DIGEST_ALG_MDX "\x2A\x86\x48\x86\xF7\x0D\x02\x00"
67#define OID_HASH_ALG_SHA1 "\x2b\x0e\x03\x02\x1a"
68#define OID_HASH_ALG_SHA2X "\x60\x86\x48\x01\x65\x03\x04\x02\x00"
Paul Bakker4593aea2009-02-09 22:32:35 +000069
Paul Bakker13e2dfe2009-07-28 07:18:38 +000070#define OID_ISO_MEMBER_BODIES "\x2a"
71#define OID_ISO_IDENTIFIED_ORG "\x2b"
Paul Bakker4593aea2009-02-09 22:32:35 +000072
73/*
74 * ISO Member bodies OID parts
75 */
Paul Bakker13e2dfe2009-07-28 07:18:38 +000076#define OID_COUNTRY_US "\x86\x48"
77#define OID_RSA_DATA_SECURITY "\x86\xf7\x0d"
Paul Bakker4593aea2009-02-09 22:32:35 +000078
79/*
80 * ISO Identified organization OID parts
81 */
Paul Bakker13e2dfe2009-07-28 07:18:38 +000082#define OID_OIW_SECSIG_SHA1 "\x0e\x03\x02\x1a"
Paul Bakker4593aea2009-02-09 22:32:35 +000083
Paul Bakker5121ce52009-01-03 21:22:43 +000084/*
85 * DigestInfo ::= SEQUENCE {
86 * digestAlgorithm DigestAlgorithmIdentifier,
87 * digest Digest }
88 *
89 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
90 *
91 * Digest ::= OCTET STRING
92 */
Paul Bakker13e2dfe2009-07-28 07:18:38 +000093#define ASN1_HASH_MDX \
94( \
Paul Bakker4593aea2009-02-09 22:32:35 +000095 ASN1_STR_CONSTRUCTED_SEQUENCE "\x20" \
96 ASN1_STR_CONSTRUCTED_SEQUENCE "\x0C" \
Paul Bakker13e2dfe2009-07-28 07:18:38 +000097 ASN1_STR_OID "\x08" \
98 OID_DIGEST_ALG_MDX \
99 ASN1_STR_NULL "\x00" \
100 ASN1_STR_OCTET_STRING "\x10" \
Paul Bakker4593aea2009-02-09 22:32:35 +0000101)
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000103#define ASN1_HASH_SHA1 \
Paul Bakker4593aea2009-02-09 22:32:35 +0000104 ASN1_STR_CONSTRUCTED_SEQUENCE "\x21" \
105 ASN1_STR_CONSTRUCTED_SEQUENCE "\x09" \
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000106 ASN1_STR_OID "\x05" \
107 OID_HASH_ALG_SHA1 \
108 ASN1_STR_NULL "\x00" \
Paul Bakker4593aea2009-02-09 22:32:35 +0000109 ASN1_STR_OCTET_STRING "\x14"
110
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000111#define ASN1_HASH_SHA2X \
Paul Bakker4593aea2009-02-09 22:32:35 +0000112 ASN1_STR_CONSTRUCTED_SEQUENCE "\x11" \
113 ASN1_STR_CONSTRUCTED_SEQUENCE "\x0d" \
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000114 ASN1_STR_OID "\x09" \
115 OID_HASH_ALG_SHA2X \
116 ASN1_STR_NULL "\x00" \
Paul Bakker4593aea2009-02-09 22:32:35 +0000117 ASN1_STR_OCTET_STRING "\x00"
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
119/**
120 * \brief RSA context structure
121 */
122typedef struct
123{
124 int ver; /*!< always 0 */
125 int len; /*!< size(N) in chars */
126
127 mpi N; /*!< public modulus */
128 mpi E; /*!< public exponent */
129
130 mpi D; /*!< private exponent */
131 mpi P; /*!< 1st prime factor */
132 mpi Q; /*!< 2nd prime factor */
133 mpi DP; /*!< D % (P - 1) */
134 mpi DQ; /*!< D % (Q - 1) */
135 mpi QP; /*!< 1 / (Q % P) */
136
137 mpi RN; /*!< cached R^2 mod N */
138 mpi RP; /*!< cached R^2 mod P */
139 mpi RQ; /*!< cached R^2 mod Q */
140
141 int padding; /*!< 1.5 or OAEP/PSS */
142 int hash_id; /*!< hash identifier */
143 int (*f_rng)(void *); /*!< RNG function */
144 void *p_rng; /*!< RNG parameter */
145}
146rsa_context;
147
148#ifdef __cplusplus
149extern "C" {
150#endif
151
152/**
153 * \brief Initialize an RSA context
154 *
155 * \param ctx RSA context to be initialized
156 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
157 * \param hash_id RSA_PKCS_V21 hash identifier
158 * \param f_rng RNG function
159 * \param p_rng RNG parameter
160 *
161 * \note The hash_id parameter is actually ignored
162 * when using RSA_PKCS_V15 padding.
163 *
164 * \note Currently (xyssl-0.8), RSA_PKCS_V21 padding
165 * is not supported.
166 */
167void rsa_init( rsa_context *ctx,
168 int padding,
169 int hash_id,
170 int (*f_rng)(void *),
171 void *p_rng );
172
173/**
174 * \brief Generate an RSA keypair
175 *
176 * \param ctx RSA context that will hold the key
177 * \param nbits size of the public key in bits
178 * \param exponent public exponent (e.g., 65537)
179 *
180 * \note rsa_init() must be called beforehand to setup
181 * the RSA context (especially f_rng and p_rng).
182 *
Paul Bakker40e46942009-01-03 21:51:57 +0000183 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 */
185int rsa_gen_key( rsa_context *ctx, int nbits, int exponent );
186
187/**
188 * \brief Check a public RSA key
189 *
190 * \param ctx RSA context to be checked
191 *
Paul Bakker40e46942009-01-03 21:51:57 +0000192 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 */
194int rsa_check_pubkey( rsa_context *ctx );
195
196/**
197 * \brief Check a private RSA key
198 *
199 * \param ctx RSA context to be checked
200 *
Paul Bakker40e46942009-01-03 21:51:57 +0000201 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 */
203int rsa_check_privkey( rsa_context *ctx );
204
205/**
206 * \brief Do an RSA public key operation
207 *
208 * \param ctx RSA context
209 * \param input input buffer
210 * \param output output buffer
211 *
Paul Bakker40e46942009-01-03 21:51:57 +0000212 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 *
214 * \note This function does NOT take care of message
Paul Bakker619467a2009-03-28 23:26:51 +0000215 * padding. Also, be sure to set input[0] = 0 or assure that
216 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 *
218 * \note The input and output buffers must be large
219 * enough (eg. 128 bytes if RSA-1024 is used).
220 */
221int rsa_public( rsa_context *ctx,
222 unsigned char *input,
223 unsigned char *output );
224
225/**
226 * \brief Do an RSA private key operation
227 *
228 * \param ctx RSA context
229 * \param input input buffer
230 * \param output output buffer
231 *
Paul Bakker40e46942009-01-03 21:51:57 +0000232 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
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_private( rsa_context *ctx,
238 unsigned char *input,
239 unsigned char *output );
240
241/**
242 * \brief Add the message padding, then do an RSA operation
243 *
244 * \param ctx RSA context
245 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000246 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 * \param input buffer holding the data to be encrypted
248 * \param output buffer that will hold the ciphertext
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 output buffer must be as large as the size
253 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
254 */
255int rsa_pkcs1_encrypt( rsa_context *ctx,
256 int mode, int ilen,
257 unsigned char *input,
258 unsigned char *output );
259
260/**
261 * \brief Do an RSA operation, then remove the message padding
262 *
263 * \param ctx RSA context
264 * \param mode RSA_PUBLIC or RSA_PRIVATE
265 * \param input buffer holding the encrypted data
266 * \param output buffer that will hold the plaintext
267 * \param olen will contain the plaintext length
Paul Bakker060c5682009-01-12 21:48:39 +0000268 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 *
Paul Bakker40e46942009-01-03 21:51:57 +0000270 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 *
272 * \note The output buffer must be as large as the size
Paul Bakker060c5682009-01-12 21:48:39 +0000273 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
274 * an error is thrown.
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 */
276int rsa_pkcs1_decrypt( rsa_context *ctx,
277 int mode, int *olen,
278 unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000279 unsigned char *output,
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000280 int output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000281
282/**
283 * \brief Do a private RSA to sign a message digest
284 *
285 * \param ctx RSA context
286 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerfc22c442009-07-19 20:36:27 +0000287 * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
288 * \param hashlen message digest length (for SIG_RSA_RAW only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000289 * \param hash buffer holding the message digest
290 * \param sig buffer that will hold the ciphertext
291 *
292 * \return 0 if the signing operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000293 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 *
295 * \note The "sig" buffer must be as large as the size
296 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
297 */
298int rsa_pkcs1_sign( rsa_context *ctx,
299 int mode,
300 int hash_id,
301 int hashlen,
302 unsigned char *hash,
303 unsigned char *sig );
304
305/**
306 * \brief Do a public RSA and check the message digest
307 *
308 * \param ctx points to an RSA public key
309 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerfc22c442009-07-19 20:36:27 +0000310 * \param hash_id SIG_RSA_RAW, RSA_MD{2,4,5} or RSA_SHA{1,256}
311 * \param hashlen message digest length (for SIG_RSA_RAW only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 * \param hash buffer holding the message digest
313 * \param sig buffer holding the ciphertext
314 *
315 * \return 0 if the verify operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000316 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 *
318 * \note The "sig" buffer must be as large as the size
319 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
320 */
321int rsa_pkcs1_verify( rsa_context *ctx,
322 int mode,
323 int hash_id,
324 int hashlen,
325 unsigned char *hash,
326 unsigned char *sig );
327
328/**
329 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000330 *
331 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 */
333void rsa_free( rsa_context *ctx );
334
335/**
336 * \brief Checkup routine
337 *
338 * \return 0 if successful, or 1 if the test failed
339 */
340int rsa_self_test( int verbose );
341
342#ifdef __cplusplus
343}
344#endif
345
346#endif /* rsa.h */