blob: 4219cbed3100ae14d4ee415faf41a3cdbe407850 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakkerfc8c4362010-03-21 17:37:16 +00004 * Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00005 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
Paul Bakker40e46942009-01-03 21:51:57 +000021#ifndef POLARSSL_RSA_H
22#define POLARSSL_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000023
Paul Bakker8e831ed2009-01-03 21:24:11 +000024#include "polarssl/bignum.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Paul Bakker13e2dfe2009-07-28 07:18:38 +000026/*
27 * RSA Error codes
28 */
Paul Bakker3391b122009-07-28 20:11:54 +000029#define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x0400
30#define POLARSSL_ERR_RSA_INVALID_PADDING -0x0410
31#define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x0420
32#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x0430
33#define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x0440
34#define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x0450
35#define POLARSSL_ERR_RSA_VERIFY_FAILED -0x0460
36#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x0470
Paul Bakker5121ce52009-01-03 21:22:43 +000037
38/*
39 * PKCS#1 constants
40 */
Paul Bakkerfc22c442009-07-19 20:36:27 +000041#define SIG_RSA_RAW 0
42#define SIG_RSA_MD2 2
43#define SIG_RSA_MD4 3
44#define SIG_RSA_MD5 4
Paul Bakker4593aea2009-02-09 22:32:35 +000045#define SIG_RSA_SHA1 5
46#define SIG_RSA_SHA224 14
47#define SIG_RSA_SHA256 11
48#define SIG_RSA_SHA384 12
49#define SIG_RSA_SHA512 13
Paul Bakker5121ce52009-01-03 21:22:43 +000050
51#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
Paul Bakker4593aea2009-02-09 22:32:35 +000060#define ASN1_STR_CONSTRUCTED_SEQUENCE "\x30"
Paul Bakker13e2dfe2009-07-28 07:18:38 +000061#define ASN1_STR_NULL "\x05"
62#define ASN1_STR_OID "\x06"
63#define ASN1_STR_OCTET_STRING "\x04"
Paul Bakker4593aea2009-02-09 22:32:35 +000064
Paul Bakker13e2dfe2009-07-28 07:18:38 +000065#define OID_DIGEST_ALG_MDX "\x2A\x86\x48\x86\xF7\x0D\x02\x00"
66#define OID_HASH_ALG_SHA1 "\x2b\x0e\x03\x02\x1a"
67#define OID_HASH_ALG_SHA2X "\x60\x86\x48\x01\x65\x03\x04\x02\x00"
Paul Bakker4593aea2009-02-09 22:32:35 +000068
Paul Bakker13e2dfe2009-07-28 07:18:38 +000069#define OID_ISO_MEMBER_BODIES "\x2a"
70#define OID_ISO_IDENTIFIED_ORG "\x2b"
Paul Bakker4593aea2009-02-09 22:32:35 +000071
72/*
73 * ISO Member bodies OID parts
74 */
Paul Bakker13e2dfe2009-07-28 07:18:38 +000075#define OID_COUNTRY_US "\x86\x48"
76#define OID_RSA_DATA_SECURITY "\x86\xf7\x0d"
Paul Bakker4593aea2009-02-09 22:32:35 +000077
78/*
79 * ISO Identified organization OID parts
80 */
Paul Bakker13e2dfe2009-07-28 07:18:38 +000081#define OID_OIW_SECSIG_SHA1 "\x0e\x03\x02\x1a"
Paul Bakker4593aea2009-02-09 22:32:35 +000082
Paul Bakker5121ce52009-01-03 21:22:43 +000083/*
84 * DigestInfo ::= SEQUENCE {
85 * digestAlgorithm DigestAlgorithmIdentifier,
86 * digest Digest }
87 *
88 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
89 *
90 * Digest ::= OCTET STRING
91 */
Paul Bakker13e2dfe2009-07-28 07:18:38 +000092#define ASN1_HASH_MDX \
93( \
Paul Bakker4593aea2009-02-09 22:32:35 +000094 ASN1_STR_CONSTRUCTED_SEQUENCE "\x20" \
95 ASN1_STR_CONSTRUCTED_SEQUENCE "\x0C" \
Paul Bakker13e2dfe2009-07-28 07:18:38 +000096 ASN1_STR_OID "\x08" \
97 OID_DIGEST_ALG_MDX \
98 ASN1_STR_NULL "\x00" \
99 ASN1_STR_OCTET_STRING "\x10" \
Paul Bakker4593aea2009-02-09 22:32:35 +0000100)
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000102#define ASN1_HASH_SHA1 \
Paul Bakker4593aea2009-02-09 22:32:35 +0000103 ASN1_STR_CONSTRUCTED_SEQUENCE "\x21" \
104 ASN1_STR_CONSTRUCTED_SEQUENCE "\x09" \
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000105 ASN1_STR_OID "\x05" \
106 OID_HASH_ALG_SHA1 \
107 ASN1_STR_NULL "\x00" \
Paul Bakker4593aea2009-02-09 22:32:35 +0000108 ASN1_STR_OCTET_STRING "\x14"
109
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000110#define ASN1_HASH_SHA2X \
Paul Bakker4593aea2009-02-09 22:32:35 +0000111 ASN1_STR_CONSTRUCTED_SEQUENCE "\x11" \
112 ASN1_STR_CONSTRUCTED_SEQUENCE "\x0d" \
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000113 ASN1_STR_OID "\x09" \
114 OID_HASH_ALG_SHA2X \
115 ASN1_STR_NULL "\x00" \
Paul Bakker4593aea2009-02-09 22:32:35 +0000116 ASN1_STR_OCTET_STRING "\x00"
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
118/**
119 * \brief RSA context structure
120 */
121typedef struct
122{
123 int ver; /*!< always 0 */
124 int len; /*!< size(N) in chars */
125
126 mpi N; /*!< public modulus */
127 mpi E; /*!< public exponent */
128
129 mpi D; /*!< private exponent */
130 mpi P; /*!< 1st prime factor */
131 mpi Q; /*!< 2nd prime factor */
132 mpi DP; /*!< D % (P - 1) */
133 mpi DQ; /*!< D % (Q - 1) */
134 mpi QP; /*!< 1 / (Q % P) */
135
136 mpi RN; /*!< cached R^2 mod N */
137 mpi RP; /*!< cached R^2 mod P */
138 mpi RQ; /*!< cached R^2 mod Q */
139
140 int padding; /*!< 1.5 or OAEP/PSS */
141 int hash_id; /*!< hash identifier */
142 int (*f_rng)(void *); /*!< RNG function */
143 void *p_rng; /*!< RNG parameter */
144}
145rsa_context;
146
147#ifdef __cplusplus
148extern "C" {
149#endif
150
151/**
152 * \brief Initialize an RSA context
153 *
154 * \param ctx RSA context to be initialized
155 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
156 * \param hash_id RSA_PKCS_V21 hash identifier
157 * \param f_rng RNG function
158 * \param p_rng RNG parameter
159 *
160 * \note The hash_id parameter is actually ignored
161 * when using RSA_PKCS_V15 padding.
162 *
163 * \note Currently (xyssl-0.8), RSA_PKCS_V21 padding
164 * is not supported.
165 */
166void rsa_init( rsa_context *ctx,
167 int padding,
168 int hash_id,
169 int (*f_rng)(void *),
170 void *p_rng );
171
172/**
173 * \brief Generate an RSA keypair
174 *
175 * \param ctx RSA context that will hold the key
176 * \param nbits size of the public key in bits
177 * \param exponent public exponent (e.g., 65537)
178 *
179 * \note rsa_init() must be called beforehand to setup
180 * the RSA context (especially f_rng and p_rng).
181 *
Paul Bakker40e46942009-01-03 21:51:57 +0000182 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 */
184int rsa_gen_key( rsa_context *ctx, int nbits, int exponent );
185
186/**
187 * \brief Check a public RSA key
188 *
189 * \param ctx RSA context to be checked
190 *
Paul Bakker40e46942009-01-03 21:51:57 +0000191 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000193int rsa_check_pubkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
195/**
196 * \brief Check a private RSA key
197 *
198 * \param ctx RSA context to be checked
199 *
Paul Bakker40e46942009-01-03 21:51:57 +0000200 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000202int rsa_check_privkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000203
204/**
205 * \brief Do an RSA public key operation
206 *
207 * \param ctx RSA context
208 * \param input input buffer
209 * \param output output buffer
210 *
Paul Bakker40e46942009-01-03 21:51:57 +0000211 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 *
213 * \note This function does NOT take care of message
Paul Bakker619467a2009-03-28 23:26:51 +0000214 * padding. Also, be sure to set input[0] = 0 or assure that
215 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 *
217 * \note The input and output buffers must be large
218 * enough (eg. 128 bytes if RSA-1024 is used).
219 */
220int rsa_public( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000221 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 unsigned char *output );
223
224/**
225 * \brief Do an RSA private key operation
226 *
227 * \param ctx RSA context
228 * \param input input buffer
229 * \param output output buffer
230 *
Paul Bakker40e46942009-01-03 21:51:57 +0000231 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 *
233 * \note The input and output buffers must be large
234 * enough (eg. 128 bytes if RSA-1024 is used).
235 */
236int rsa_private( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000237 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 unsigned char *output );
239
240/**
241 * \brief Add the message padding, then do an RSA operation
242 *
243 * \param ctx RSA context
244 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000245 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 * \param input buffer holding the data to be encrypted
247 * \param output buffer that will hold the ciphertext
248 *
Paul Bakker40e46942009-01-03 21:51:57 +0000249 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 *
251 * \note The output buffer must be as large as the size
252 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
253 */
254int rsa_pkcs1_encrypt( rsa_context *ctx,
255 int mode, int ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000256 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000257 unsigned char *output );
258
259/**
260 * \brief Do an RSA operation, then remove the message padding
261 *
262 * \param ctx RSA context
263 * \param mode RSA_PUBLIC or RSA_PRIVATE
264 * \param input buffer holding the encrypted data
265 * \param output buffer that will hold the plaintext
266 * \param olen will contain the plaintext length
Paul Bakker060c5682009-01-12 21:48:39 +0000267 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 *
Paul Bakker40e46942009-01-03 21:51:57 +0000269 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 *
271 * \note The output buffer must be as large as the size
Paul Bakker060c5682009-01-12 21:48:39 +0000272 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
273 * an error is thrown.
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 */
275int rsa_pkcs1_decrypt( rsa_context *ctx,
276 int mode, int *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000277 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000278 unsigned char *output,
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000279 int output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000280
281/**
282 * \brief Do a private RSA to sign a message digest
283 *
284 * \param ctx RSA context
285 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerfc22c442009-07-19 20:36:27 +0000286 * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
287 * \param hashlen message digest length (for SIG_RSA_RAW only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 * \param hash buffer holding the message digest
289 * \param sig buffer that will hold the ciphertext
290 *
291 * \return 0 if the signing operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000292 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 *
294 * \note The "sig" buffer must be as large as the size
295 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
296 */
297int rsa_pkcs1_sign( rsa_context *ctx,
298 int mode,
299 int hash_id,
300 int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000301 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 unsigned char *sig );
303
304/**
305 * \brief Do a public RSA and check the message digest
306 *
307 * \param ctx points to an RSA public key
308 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerfc22c442009-07-19 20:36:27 +0000309 * \param hash_id SIG_RSA_RAW, RSA_MD{2,4,5} or RSA_SHA{1,256}
310 * \param hashlen message digest length (for SIG_RSA_RAW only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 * \param hash buffer holding the message digest
312 * \param sig buffer holding the ciphertext
313 *
314 * \return 0 if the verify operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000315 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 *
317 * \note The "sig" buffer must be as large as the size
318 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
319 */
320int rsa_pkcs1_verify( rsa_context *ctx,
321 int mode,
322 int hash_id,
323 int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000324 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 unsigned char *sig );
326
327/**
328 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000329 *
330 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 */
332void rsa_free( rsa_context *ctx );
333
334/**
335 * \brief Checkup routine
336 *
337 * \return 0 if successful, or 1 if the test failed
338 */
339int rsa_self_test( int verbose );
340
341#ifdef __cplusplus
342}
343#endif
344
345#endif /* rsa.h */