1 /* $Cambridge: exim/src/src/pdkim/rsa.h,v 1.1.2.2 2009/03/17 12:57:37 tom Exp $ */
5 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
7 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
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.
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.
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.
23 #ifndef POLARSSL_RSA_H
24 #define POLARSSL_RSA_H
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
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
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
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
78 #define ASN1_UTF8_STRING 0x0C
79 #define ASN1_SEQUENCE 0x10
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
102 #define RSA_PRIVATE 1
104 #define RSA_PKCS_V15 0
105 #define RSA_PKCS_V21 1
111 * DigestInfo ::= SEQUENCE {
112 * digestAlgorithm DigestAlgorithmIdentifier,
115 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
117 * Digest ::= OCTET STRING
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"
123 #define ASN1_HASH_SHA1 \
124 "\x30\x21\x30\x09\x06\x05\x2B\x0E\x03" \
125 "\x02\x1A\x05\x00\x04\x14"
127 #define ASN1_HASH_SHA256 \
129 "\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02" \
134 * \brief RSA context structure
138 int ver; /*!< always 0 */
139 int len; /*!< size(N) in chars */
141 mpi N; /*!< public modulus */
142 mpi E; /*!< public exponent */
144 mpi D; /*!< private exponent */
145 mpi P; /*!< 1st prime factor */
146 mpi Q; /*!< 2nd prime factor */
147 mpi DP; /*!< D % (P - 1) */
148 mpi DQ; /*!< D % (Q - 1) */
149 mpi QP; /*!< 1 / (Q % P) */
151 mpi RN; /*!< cached R^2 mod N */
152 mpi RP; /*!< cached R^2 mod P */
153 mpi RQ; /*!< cached R^2 mod Q */
155 int padding; /*!< 1.5 or OAEP/PSS */
156 int hash_id; /*!< hash identifier */
157 int (*f_rng)(void *); /*!< RNG function */
158 void *p_rng; /*!< RNG parameter */
167 * \brief Initialize an RSA context
169 * \param ctx RSA context to be initialized
170 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
171 * \param hash_id RSA_PKCS_V21 hash identifier
172 * \param f_rng RNG function
173 * \param p_rng RNG parameter
175 * \note The hash_id parameter is actually ignored
176 * when using RSA_PKCS_V15 padding.
178 * \note Currently (xyssl-0.8), RSA_PKCS_V21 padding
181 void rsa_init( rsa_context *ctx,
184 int (*f_rng)(void *),
188 * \brief Generate an RSA keypair
190 * \param ctx RSA context that will hold the key
191 * \param nbits size of the public key in bits
192 * \param exponent public exponent (e.g., 65537)
194 * \note rsa_init() must be called beforehand to setup
195 * the RSA context (especially f_rng and p_rng).
197 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
199 int rsa_gen_key( rsa_context *ctx, int nbits, int exponent );
202 * \brief Check a public RSA key
204 * \param ctx RSA context to be checked
206 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
208 int rsa_check_pubkey( rsa_context *ctx );
211 * \brief Check a private RSA key
213 * \param ctx RSA context to be checked
215 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
217 int rsa_check_privkey( rsa_context *ctx );
220 * \brief Do an RSA public key operation
222 * \param ctx RSA context
223 * \param input input buffer
224 * \param output output buffer
226 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
228 * \note This function does NOT take care of message
229 * padding. Also, be sure to set input[0] = 0.
231 * \note The input and output buffers must be large
232 * enough (eg. 128 bytes if RSA-1024 is used).
234 int rsa_public( rsa_context *ctx,
235 unsigned char *input,
236 unsigned char *output );
239 * \brief Do an RSA private key operation
241 * \param ctx RSA context
242 * \param input input buffer
243 * \param output output buffer
245 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
247 * \note The input and output buffers must be large
248 * enough (eg. 128 bytes if RSA-1024 is used).
250 int rsa_private( rsa_context *ctx,
251 unsigned char *input,
252 unsigned char *output );
255 * \brief Add the message padding, then do an RSA operation
257 * \param ctx RSA context
258 * \param mode RSA_PUBLIC or RSA_PRIVATE
259 * \param ilen contains the the plaintext length
260 * \param input buffer holding the data to be encrypted
261 * \param output buffer that will hold the ciphertext
263 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
265 * \note The output buffer must be as large as the size
266 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
268 int rsa_pkcs1_encrypt( rsa_context *ctx,
270 unsigned char *input,
271 unsigned char *output );
274 * \brief Do an RSA operation, then remove the message padding
276 * \param ctx RSA context
277 * \param mode RSA_PUBLIC or RSA_PRIVATE
278 * \param input buffer holding the encrypted data
279 * \param output buffer that will hold the plaintext
280 * \param olen will contain the plaintext length
281 * \param output_max_len maximum length of the output buffer
283 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
285 * \note The output buffer must be as large as the size
286 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
287 * an error is thrown.
289 int rsa_pkcs1_decrypt( rsa_context *ctx,
291 unsigned char *input,
292 unsigned char *output,
296 * \brief Do a private RSA to sign a message digest
298 * \param ctx RSA context
299 * \param mode RSA_PUBLIC or RSA_PRIVATE
300 * \param hash_id RSA_RAW, RSA_MD{2,4,5} or RSA_SHA{1,256}
301 * \param hashlen message digest length (for RSA_RAW only)
302 * \param hash buffer holding the message digest
303 * \param sig buffer that will hold the ciphertext
305 * \return 0 if the signing operation was successful,
306 * or an POLARSSL_ERR_RSA_XXX error code
308 * \note The "sig" buffer must be as large as the size
309 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
311 int rsa_pkcs1_sign( rsa_context *ctx,
316 unsigned char *sig );
319 * \brief Do a public RSA and check the message digest
321 * \param ctx points to an RSA public key
322 * \param mode RSA_PUBLIC or RSA_PRIVATE
323 * \param hash_id RSA_RAW, RSA_MD{2,4,5} or RSA_SHA{1,256}
324 * \param hashlen message digest length (for RSA_RAW only)
325 * \param hash buffer holding the message digest
326 * \param sig buffer holding the ciphertext
328 * \return 0 if the verify operation was successful,
329 * or an POLARSSL_ERR_RSA_XXX error code
331 * \note The "sig" buffer must be as large as the size
332 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
334 int rsa_pkcs1_verify( rsa_context *ctx,
339 unsigned char *sig );
342 * \brief Free the components of an RSA key
344 void rsa_free( rsa_context *ctx );
346 int rsa_parse_public_key( rsa_context *rsa, unsigned char *buf, int buflen );
348 int rsa_parse_key( rsa_context *rsa, unsigned char *buf, int buflen,
349 unsigned char *pwd, int pwdlen );