source: trunk/include/polarssl/rsa.h @ 1219

Revision 1219, 13.4 KB checked in by paul, 2 months ago (diff)
  • Added alternative for SHA1 signature structure to check for (without NULL)
Line 
1/**
2 * \file rsa.h
3 *
4 * \brief The RSA public-key cryptosystem
5 *
6 *  Copyright (C) 2006-2010, Brainspark B.V.
7 *
8 *  This file is part of PolarSSL (http://www.polarssl.org)
9 *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
11 *  All rights reserved.
12 *
13 *  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.
26 */
27#ifndef POLARSSL_RSA_H
28#define POLARSSL_RSA_H
29
30#include "bignum.h"
31
32/*
33 * RSA Error codes
34 */
35#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. */
44
45/*
46 * PKCS#1 constants
47 */
48#define SIG_RSA_RAW     0
49#define SIG_RSA_MD2     2
50#define SIG_RSA_MD4     3
51#define SIG_RSA_MD5     4
52#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
57
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
67#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"
71
72#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"
75
76#define OID_ISO_MEMBER_BODIES           "\x2a"
77#define OID_ISO_IDENTIFIED_ORG          "\x2b"
78
79/*
80 * ISO Member bodies OID parts
81 */
82#define OID_COUNTRY_US                  "\x86\x48"
83#define OID_RSA_DATA_SECURITY           "\x86\xf7\x0d"
84
85/*
86 * ISO Identified organization OID parts
87 */
88#define OID_OIW_SECSIG_SHA1             "\x0e\x03\x02\x1a"
89
90/*
91 * DigestInfo ::= SEQUENCE {
92 *   digestAlgorithm DigestAlgorithmIdentifier,
93 *   digest Digest }
94 *
95 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
96 *
97 * Digest ::= OCTET STRING
98 */
99#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"              \
107)
108
109#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"                    \
115      ASN1_STR_OCTET_STRING "\x14"
116
117#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
124#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"                    \
130      ASN1_STR_OCTET_STRING "\x00"
131
132/**
133 * \brief          RSA context structure
134 */
135typedef struct
136{
137    int ver;                    /*!<  always 0          */
138    size_t len;                 /*!<  size(N) in chars  */
139
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
154    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                          */
160}
161rsa_context;
162
163#ifdef __cplusplus
164extern "C" {
165#endif
166
167/**
168 * \brief          Initialize an RSA context
169 *
170 * \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
173 *
174 * \note           The hash_id parameter is actually ignored
175 *                 when using RSA_PKCS_V15 padding.
176 */
177void rsa_init( rsa_context *ctx,
178               int padding,
179               int hash_id);
180
181/**
182 * \brief          Generate an RSA keypair
183 *
184 * \param ctx      RSA context that will hold the key
185 * \param f_rng    RNG function
186 * \param p_rng    RNG parameter
187 * \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
191 *                 the RSA context.
192 *
193 * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code
194 */
195int rsa_gen_key( rsa_context *ctx,
196                 int (*f_rng)(void *, unsigned char *, size_t),
197                 void *p_rng,
198                 unsigned int nbits, int exponent );
199
200/**
201 * \brief          Check a public RSA key
202 *
203 * \param ctx      RSA context to be checked
204 *
205 * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code
206 */
207int rsa_check_pubkey( const rsa_context *ctx );
208
209/**
210 * \brief          Check a private RSA key
211 *
212 * \param ctx      RSA context to be checked
213 *
214 * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code
215 */
216int rsa_check_privkey( const rsa_context *ctx );
217
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 *
225 * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code
226 *
227 * \note           This function does NOT take care of message
228 *                 padding. Also, be sure to set input[0] = 0 or assure that
229 *                 input is smaller than N.
230 *
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,
235                const unsigned char *input,
236                unsigned char *output );
237
238/**
239 * \brief          Do an RSA private key operation
240 *
241 * \param ctx      RSA context
242 * \param input    input buffer
243 * \param output   output buffer
244 *
245 * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code
246 *
247 * \note           The input and output buffers must be large
248 *                 enough (eg. 128 bytes if RSA-1024 is used).
249 */
250int rsa_private( rsa_context *ctx,
251                 const unsigned char *input,
252                 unsigned char *output );
253
254/**
255 * \brief          Add the message padding, then do an RSA operation
256 *
257 * \param ctx      RSA context
258 * \param f_rng    RNG function (Needed for padding and PKCS#1 v2.1 encoding)
259 * \param p_rng    RNG parameter
260 * \param mode     RSA_PUBLIC or RSA_PRIVATE
261 * \param ilen     contains the plaintext length
262 * \param input    buffer holding the data to be encrypted
263 * \param output   buffer that will hold the ciphertext
264 *
265 * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code
266 *
267 * \note           The output buffer must be as large as the size
268 *                 of ctx->N (eg. 128 bytes if RSA-1024 is used).
269 */
270int rsa_pkcs1_encrypt( rsa_context *ctx,
271                       int (*f_rng)(void *, unsigned char *, size_t),
272                       void *p_rng,
273                       int mode, size_t ilen,
274                       const unsigned char *input,
275                       unsigned char *output );
276
277/**
278 * \brief          Do an RSA operation, then remove the message padding
279 *
280 * \param ctx      RSA context
281 * \param mode     RSA_PUBLIC or RSA_PRIVATE
282 * \param olen     will contain the plaintext length
283 * \param input    buffer holding the encrypted data
284 * \param output   buffer that will hold the plaintext
285 * \param output_max_len    maximum length of the output buffer
286 *
287 * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code
288 *
289 * \note           The output buffer must be as large as the size
290 *                 of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
291 *                 an error is thrown.
292 */
293int rsa_pkcs1_decrypt( rsa_context *ctx,
294                       int mode, size_t *olen,
295                       const unsigned char *input,
296                       unsigned char *output,
297                       size_t output_max_len );
298
299/**
300 * \brief          Do a private RSA to sign a message digest
301 *
302 * \param ctx      RSA context
303 * \param f_rng    RNG function (Needed for PKCS#1 v2.1 encoding)
304 * \param p_rng    RNG parameter
305 * \param mode     RSA_PUBLIC or RSA_PRIVATE
306 * \param hash_id  SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
307 * \param hashlen  message digest length (for SIG_RSA_RAW only)
308 * \param hash     buffer holding the message digest
309 * \param sig      buffer that will hold the ciphertext
310 *
311 * \return         0 if the signing operation was successful,
312 *                 or an POLARSSL_ERR_RSA_XXX error code
313 *
314 * \note           The "sig" buffer must be as large as the size
315 *                 of ctx->N (eg. 128 bytes if RSA-1024 is used).
316 *
317 * \note           In case of PKCS#1 v2.1 encoding keep in mind that
318 *                 the hash_id in the RSA context is the one used for the
319 *                 encoding. hash_id in the function call is the type of hash
320 *                 that is encoded. According to RFC 3447 it is advised to
321 *                 keep both hashes the same.
322 */
323int rsa_pkcs1_sign( rsa_context *ctx,
324                    int (*f_rng)(void *, unsigned char *, size_t),
325                    void *p_rng,
326                    int mode,
327                    int hash_id,
328                    unsigned int hashlen,
329                    const unsigned char *hash,
330                    unsigned char *sig );
331
332/**
333 * \brief          Do a public RSA and check the message digest
334 *
335 * \param ctx      points to an RSA public key
336 * \param mode     RSA_PUBLIC or RSA_PRIVATE
337 * \param hash_id  SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
338 * \param hashlen  message digest length (for SIG_RSA_RAW only)
339 * \param hash     buffer holding the message digest
340 * \param sig      buffer holding the ciphertext
341 *
342 * \return         0 if the verify operation was successful,
343 *                 or an POLARSSL_ERR_RSA_XXX error code
344 *
345 * \note           The "sig" buffer must be as large as the size
346 *                 of ctx->N (eg. 128 bytes if RSA-1024 is used).
347 *
348 * \note           In case of PKCS#1 v2.1 encoding keep in mind that
349 *                 the hash_id in the RSA context is the one used for the
350 *                 verification. hash_id in the function call is the type of hash
351 *                 that is verified. According to RFC 3447 it is advised to
352 *                 keep both hashes the same.
353 */
354int rsa_pkcs1_verify( rsa_context *ctx,
355                      int mode,
356                      int hash_id,
357                      unsigned int hashlen,
358                      const unsigned char *hash,
359                      unsigned char *sig );
360
361/**
362 * \brief          Free the components of an RSA key
363 *
364 * \param ctx      RSA Context to free
365 */
366void rsa_free( rsa_context *ctx );
367
368/**
369 * \brief          Checkup routine
370 *
371 * \return         0 if successful, or 1 if the test failed
372 */
373int rsa_self_test( int verbose );
374
375#ifdef __cplusplus
376}
377#endif
378
379#endif /* rsa.h */
Note: See TracBrowser for help on using the repository browser.

What are you looking for?