1 /* $Cambridge: exim/src/src/pdkim/rsa.h,v 1.1.2.3 2009/03/17 21:11:56 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 \
128 "\x30\x31\x30\x0d\x06\x09\x60\x86\x48" \
129 "\x01\x65\x03\x04\x02\x01\x05\x00\x04" \
133 * \brief RSA context structure
137 int ver; /*!< always 0 */
138 int len; /*!< size(N) in chars */
140 mpi N; /*!< public modulus */
141 mpi E; /*!< public exponent */
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) */
150 mpi RN; /*!< cached R^2 mod N */
151 mpi RP; /*!< cached R^2 mod P */
152 mpi RQ; /*!< cached R^2 mod Q */
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 */
166 * \brief Initialize an RSA context
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
174 * \note The hash_id parameter is actually ignored
175 * when using RSA_PKCS_V15 padding.
177 * \note Currently (xyssl-0.8), RSA_PKCS_V21 padding
180 void rsa_init( rsa_context *ctx,
183 int (*f_rng)(void *),
187 * \brief Generate an RSA keypair
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)
193 * \note rsa_init() must be called beforehand to setup
194 * the RSA context (especially f_rng and p_rng).
196 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
198 int rsa_gen_key( rsa_context *ctx, int nbits, int exponent );
201 * \brief Check a public RSA key
203 * \param ctx RSA context to be checked
205 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
207 int rsa_check_pubkey( rsa_context *ctx );
210 * \brief Check a private RSA key
212 * \param ctx RSA context to be checked
214 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
216 int rsa_check_privkey( rsa_context *ctx );
219 * \brief Do an RSA public key operation
221 * \param ctx RSA context
222 * \param input input buffer
223 * \param output output buffer
225 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
227 * \note This function does NOT take care of message
228 * padding. Also, be sure to set input[0] = 0.
230 * \note The input and output buffers must be large
231 * enough (eg. 128 bytes if RSA-1024 is used).
233 int rsa_public( rsa_context *ctx,
234 unsigned char *input,
235 unsigned char *output );
238 * \brief Do an RSA private key operation
240 * \param ctx RSA context
241 * \param input input buffer
242 * \param output output buffer
244 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
246 * \note The input and output buffers must be large
247 * enough (eg. 128 bytes if RSA-1024 is used).
249 int rsa_private( rsa_context *ctx,
250 unsigned char *input,
251 unsigned char *output );
254 * \brief Add the message padding, then do an RSA operation
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
262 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
264 * \note The output buffer must be as large as the size
265 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
267 int rsa_pkcs1_encrypt( rsa_context *ctx,
269 unsigned char *input,
270 unsigned char *output );
273 * \brief Do an RSA operation, then remove the message padding
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
282 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
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.
288 int rsa_pkcs1_decrypt( rsa_context *ctx,
290 unsigned char *input,
291 unsigned char *output,
295 * \brief Do a private RSA to sign a message digest
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
304 * \return 0 if the signing operation was successful,
305 * or an POLARSSL_ERR_RSA_XXX error code
307 * \note The "sig" buffer must be as large as the size
308 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
310 int rsa_pkcs1_sign( rsa_context *ctx,
315 unsigned char *sig );
318 * \brief Do a public RSA and check the message digest
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
327 * \return 0 if the verify operation was successful,
328 * or an POLARSSL_ERR_RSA_XXX error code
330 * \note The "sig" buffer must be as large as the size
331 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
333 int rsa_pkcs1_verify( rsa_context *ctx,
338 unsigned char *sig );
341 * \brief Free the components of an RSA key
343 void rsa_free( rsa_context *ctx );
345 int rsa_parse_public_key( rsa_context *rsa, unsigned char *buf, int buflen );
347 int rsa_parse_key( rsa_context *rsa, unsigned char *buf, int buflen,
348 unsigned char *pwd, int pwdlen );