ca3b63e32beec65e343c05ae5e1648a1c1102c56
[users/jgh/exim.git] / src / src / pdkim / rsa.h
1 /* $Cambridge: exim/src/src/pdkim/rsa.h,v 1.1.2.3 2009/03/17 21:11:56 tom Exp $ */
2 /**
3  * \file rsa.h
4  *
5  *  Based on XySSL: Copyright (C) 2006-2008  Christophe Devine
6  *
7  *  Copyright (C) 2009  Paul Bakker <polarssl_maintainer at polarssl dot org>
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 #ifndef POLARSSL_RSA_H
24 #define POLARSSL_RSA_H
25
26 #include "bignum.h"
27
28 #define POLARSSL_ERR_RSA_BAD_INPUT_DATA                    -0x0400
29 #define POLARSSL_ERR_RSA_INVALID_PADDING                   -0x0410
30 #define POLARSSL_ERR_RSA_KEY_GEN_FAILED                    -0x0420
31 #define POLARSSL_ERR_RSA_KEY_CHECK_FAILED                  -0x0430
32 #define POLARSSL_ERR_RSA_PUBLIC_FAILED                     -0x0440
33 #define POLARSSL_ERR_RSA_PRIVATE_FAILED                    -0x0450
34 #define POLARSSL_ERR_RSA_VERIFY_FAILED                     -0x0460
35 #define POLARSSL_ERR_RSA_OUTPUT_TO_LARGE                   -0x0470
36
37 #define POLARSSL_ERR_ASN1_OUT_OF_DATA                      -0x0014
38 #define POLARSSL_ERR_ASN1_UNEXPECTED_TAG                   -0x0016
39 #define POLARSSL_ERR_ASN1_INVALID_LENGTH                   -0x0018
40 #define POLARSSL_ERR_ASN1_LENGTH_MISMATCH                  -0x001A
41 #define POLARSSL_ERR_ASN1_INVALID_DATA                     -0x001C
42
43 #define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE              -0x0020
44 #define POLARSSL_ERR_X509_CERT_INVALID_PEM                 -0x0040
45 #define POLARSSL_ERR_X509_CERT_INVALID_FORMAT              -0x0060
46 #define POLARSSL_ERR_X509_CERT_INVALID_VERSION             -0x0080
47 #define POLARSSL_ERR_X509_CERT_INVALID_SERIAL              -0x00A0
48 #define POLARSSL_ERR_X509_CERT_INVALID_ALG                 -0x00C0
49 #define POLARSSL_ERR_X509_CERT_INVALID_NAME                -0x00E0
50 #define POLARSSL_ERR_X509_CERT_INVALID_DATE                -0x0100
51 #define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY              -0x0120
52 #define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE           -0x0140
53 #define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS          -0x0160
54 #define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION             -0x0180
55 #define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG             -0x01A0
56 #define POLARSSL_ERR_X509_CERT_UNKNOWN_PK_ALG              -0x01C0
57 #define POLARSSL_ERR_X509_CERT_SIG_MISMATCH                -0x01E0
58 #define POLARSSL_ERR_X509_CERT_VERIFY_FAILED               -0x0200
59 #define POLARSSL_ERR_X509_KEY_INVALID_PEM                  -0x0220
60 #define POLARSSL_ERR_X509_KEY_INVALID_VERSION              -0x0240
61 #define POLARSSL_ERR_X509_KEY_INVALID_FORMAT               -0x0260
62 #define POLARSSL_ERR_X509_KEY_INVALID_ENC_IV               -0x0280
63 #define POLARSSL_ERR_X509_KEY_UNKNOWN_ENC_ALG              -0x02A0
64 #define POLARSSL_ERR_X509_KEY_PASSWORD_REQUIRED            -0x02C0
65 #define POLARSSL_ERR_X509_KEY_PASSWORD_MISMATCH            -0x02E0
66 #define POLARSSL_ERR_X509_POINT_ERROR                      -0x0300
67 #define POLARSSL_ERR_X509_VALUE_TO_LENGTH                  -0x0320
68
69 /*
70  * DER constants
71  */
72 #define ASN1_BOOLEAN                 0x01
73 #define ASN1_INTEGER                 0x02
74 #define ASN1_BIT_STRING              0x03
75 #define ASN1_OCTET_STRING            0x04
76 #define ASN1_NULL                    0x05
77 #define ASN1_OID                     0x06
78 #define ASN1_UTF8_STRING             0x0C
79 #define ASN1_SEQUENCE                0x10
80 #define ASN1_SET                     0x11
81 #define ASN1_PRINTABLE_STRING        0x13
82 #define ASN1_T61_STRING              0x14
83 #define ASN1_IA5_STRING              0x16
84 #define ASN1_UTC_TIME                0x17
85 #define ASN1_UNIVERSAL_STRING        0x1C
86 #define ASN1_BMP_STRING              0x1E
87 #define ASN1_PRIMITIVE               0x00
88 #define ASN1_CONSTRUCTED             0x20
89 #define ASN1_CONTEXT_SPECIFIC        0x80
90
91 /*
92  * PKCS#1 constants
93  */
94 #define RSA_RAW         0
95 #define RSA_MD2         2
96 #define RSA_MD4         3
97 #define RSA_MD5         4
98 #define RSA_SHA1        5
99 #define RSA_SHA256      6
100
101 #define RSA_PUBLIC      0
102 #define RSA_PRIVATE     1
103
104 #define RSA_PKCS_V15    0
105 #define RSA_PKCS_V21    1
106
107 #define RSA_SIGN        1
108 #define RSA_CRYPT       2
109
110 /*
111  * DigestInfo ::= SEQUENCE {
112  *   digestAlgorithm DigestAlgorithmIdentifier,
113  *   digest Digest }
114  *
115  * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
116  *
117  * Digest ::= OCTET STRING
118  */
119 #define ASN1_HASH_MDX                       \
120     "\x30\x20\x30\x0C\x06\x08\x2A\x86\x48"  \
121     "\x86\xF7\x0D\x02\x00\x05\x00\x04\x10"
122
123 #define ASN1_HASH_SHA1                      \
124     "\x30\x21\x30\x09\x06\x05\x2B\x0E\x03"  \
125     "\x02\x1A\x05\x00\x04\x14"
126
127 #define ASN1_HASH_SHA256                    \
128     "\x30\x31\x30\x0d\x06\x09\x60\x86\x48"  \
129     "\x01\x65\x03\x04\x02\x01\x05\x00\x04"  \
130     "\x20"
131
132 /**
133  * \brief          RSA context structure
134  */
135 typedef struct
136 {
137     int ver;                    /*!<  always 0          */
138     int 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;                /*!<  1.5 or OAEP/PSS   */
155     int hash_id;                /*!<  hash identifier   */
156     int (*f_rng)(void *);       /*!<  RNG function      */
157     void *p_rng;                /*!<  RNG parameter     */
158 }
159 rsa_context;
160
161 #ifdef __cplusplus
162 extern "C" {
163 #endif
164
165 /**
166  * \brief          Initialize an RSA context
167  *
168  * \param ctx      RSA context to be initialized
169  * \param padding  RSA_PKCS_V15 or RSA_PKCS_V21
170  * \param hash_id  RSA_PKCS_V21 hash identifier
171  * \param f_rng    RNG function
172  * \param p_rng    RNG parameter
173  *
174  * \note           The hash_id parameter is actually ignored
175  *                 when using RSA_PKCS_V15 padding.
176  *
177  * \note           Currently (xyssl-0.8), RSA_PKCS_V21 padding
178  *                 is not supported.
179  */
180 void rsa_init( rsa_context *ctx,
181                int padding,
182                int hash_id,
183                int (*f_rng)(void *),
184                void *p_rng );
185
186 /**
187  * \brief          Generate an RSA keypair
188  *
189  * \param ctx      RSA context that will hold the key
190  * \param nbits    size of the public key in bits
191  * \param exponent public exponent (e.g., 65537)
192  *
193  * \note           rsa_init() must be called beforehand to setup
194  *                 the RSA context (especially f_rng and p_rng).
195  *
196  * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code
197  */
198 int rsa_gen_key( rsa_context *ctx, 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  */
207 int rsa_check_pubkey( 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  */
216 int rsa_check_privkey( 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.
229  *
230  * \note           The input and output buffers must be large
231  *                 enough (eg. 128 bytes if RSA-1024 is used).
232  */
233 int rsa_public( rsa_context *ctx,
234                 unsigned char *input,
235                 unsigned char *output );
236
237 /**
238  * \brief          Do an RSA private key operation
239  *
240  * \param ctx      RSA context
241  * \param input    input buffer
242  * \param output   output buffer
243  *
244  * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code
245  *
246  * \note           The input and output buffers must be large
247  *                 enough (eg. 128 bytes if RSA-1024 is used).
248  */
249 int rsa_private( rsa_context *ctx,
250                  unsigned char *input,
251                  unsigned char *output );
252
253 /**
254  * \brief          Add the message padding, then do an RSA operation
255  *
256  * \param ctx      RSA context
257  * \param mode     RSA_PUBLIC or RSA_PRIVATE
258  * \param ilen     contains the the plaintext length
259  * \param input    buffer holding the data to be encrypted
260  * \param output   buffer that will hold the ciphertext
261  *
262  * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code
263  *
264  * \note           The output buffer must be as large as the size
265  *                 of ctx->N (eg. 128 bytes if RSA-1024 is used).
266  */
267 int rsa_pkcs1_encrypt( rsa_context *ctx,
268                        int mode, int  ilen,
269                        unsigned char *input,
270                        unsigned char *output );
271
272 /**
273  * \brief          Do an RSA operation, then remove the message padding
274  *
275  * \param ctx      RSA context
276  * \param mode     RSA_PUBLIC or RSA_PRIVATE
277  * \param input    buffer holding the encrypted data
278  * \param output   buffer that will hold the plaintext
279  * \param olen     will contain the plaintext length
280  * \param output_max_len    maximum length of the output buffer
281  *
282  * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code
283  *
284  * \note           The output buffer must be as large as the size
285  *                 of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
286  *                 an error is thrown.
287  */
288 int rsa_pkcs1_decrypt( rsa_context *ctx,
289                        int mode, int *olen,
290                        unsigned char *input,
291                        unsigned char *output,
292                int output_max_len);
293
294 /**
295  * \brief          Do a private RSA to sign a message digest
296  *
297  * \param ctx      RSA context
298  * \param mode     RSA_PUBLIC or RSA_PRIVATE
299  * \param hash_id  RSA_RAW, RSA_MD{2,4,5} or RSA_SHA{1,256}
300  * \param hashlen  message digest length (for RSA_RAW only)
301  * \param hash     buffer holding the message digest
302  * \param sig      buffer that will hold the ciphertext
303  *
304  * \return         0 if the signing operation was successful,
305  *                 or an POLARSSL_ERR_RSA_XXX error code
306  *
307  * \note           The "sig" buffer must be as large as the size
308  *                 of ctx->N (eg. 128 bytes if RSA-1024 is used).
309  */
310 int rsa_pkcs1_sign( rsa_context *ctx,
311                     int mode,
312                     int hash_id,
313                     int hashlen,
314                     unsigned char *hash,
315                     unsigned char *sig );
316
317 /**
318  * \brief          Do a public RSA and check the message digest
319  *
320  * \param ctx      points to an RSA public key
321  * \param mode     RSA_PUBLIC or RSA_PRIVATE
322  * \param hash_id  RSA_RAW, RSA_MD{2,4,5} or RSA_SHA{1,256}
323  * \param hashlen  message digest length (for RSA_RAW only)
324  * \param hash     buffer holding the message digest
325  * \param sig      buffer holding the ciphertext
326  *
327  * \return         0 if the verify operation was successful,
328  *                 or an POLARSSL_ERR_RSA_XXX error code
329  *
330  * \note           The "sig" buffer must be as large as the size
331  *                 of ctx->N (eg. 128 bytes if RSA-1024 is used).
332  */
333 int rsa_pkcs1_verify( rsa_context *ctx,
334                       int mode,
335                       int hash_id,
336                       int hashlen,
337                       unsigned char *hash,
338                       unsigned char *sig );
339
340 /**
341  * \brief          Free the components of an RSA key
342  */
343 void rsa_free( rsa_context *ctx );
344
345 int rsa_parse_public_key( rsa_context *rsa, unsigned char *buf, int buflen );
346
347 int rsa_parse_key( rsa_context *rsa, unsigned char *buf, int buflen,
348                                      unsigned char *pwd, int pwdlen );
349
350 #ifdef __cplusplus
351 }
352 #endif
353
354 #endif /* rsa.h */