Import of old SSLeay release: SSLeay 0.8.1b
[openssl.git] / crypto / crypto.c
1 /* crypto/crypto.c */
2 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 /* If you are happy to use the assmbler version of bn/bn_mulw.c, define
60  * BN_ASM */
61 #ifndef BN_ASM
62 #undef BN_ASM
63 #endif
64
65 #ifndef DES_ASM
66 #undef DES_ASM
67 #endif
68
69 #ifndef BF_ASM
70 #undef BF_ASM
71 #endif
72
73 /* The following defines are only to break the compiles into chunks.
74  * If you wish to not compile some sections, use the 'NO_XXX' macros
75  */
76 #ifndef CRYPTO_SUBSET
77 /* Define all subset symbols. */
78 #define CRYPTO_LIB_SUBSET
79 #define CRYPTO_ASN1_SUBSET
80 #define CRYPTO_BN_SUBSET
81 #define CRYPTO_BUFFER_SUBSET
82 #define CRYPTO_BIO_SUBSET
83 #define CRYPTO_CONF_SUBSET
84 #define CRYPTO_DES_SUBSET
85 #define CRYPTO_DH_SUBSET
86 #define CRYPTO_DSA_SUBSET
87 #define CRYPTO_ERROR_SUBSET
88 #define CRYPTO_EVP_SUBSET
89 #define CRYPTO_IDEA_SUBSET
90 #define CRYPTO_LHASH_SUBSET
91 #define CRYPTO_MD_SUBSET
92 #define CRYPTO_MDC2_SUBSET
93 #define CRYPTO_METH_SUBSET
94 #define CRYPTO_OBJECTS_SUBSET
95 #define CRYPTO_PEM_SUBSET
96 #define CRYPTO_RAND_SUBSET
97 #define CRYPTO_RC_SUBSET
98 #define CRYPTO_BF_SUBSET
99 #define CRYPTO_RSA_SUBSET
100 #define CRYPTO_SHA_SUBSET
101 #define CRYPTO_SHA1_SUBSET
102 #define CRYPTO_STACK_SUBSET
103 #define CRYPTO_TXT_DB_SUBSET
104 #define CRYPTO_X509_SUBSET
105 #define CRYPTO_PKCS7_SUBSET
106 #endif
107
108 #include <stdio.h>
109 #include <stdlib.h>
110 #include <string.h>
111
112 #define USE_SOCKETS
113 #include "../e_os.h"
114
115 #include "buffer.h"
116 #include "bio.h"
117 #include "stack.h"
118 #include "lhash.h"
119
120 #include "err.h"
121
122 #include "bn.h"
123 #include "evp.h"
124
125 #include "rand.h"
126 #include "conf.h"
127 #include "txt_db.h"
128
129 #include "x509.h"
130 #include "pkcs7.h"
131 #include "pem.h"
132 #include "asn1.h"
133 #include "objects.h"
134
135 #ifdef CRYPTO_LIB_SUBSET
136 #include "cryptlib.c"
137 #include "mem.c"
138 #include "cversion.c"
139 #endif
140
141 #ifdef CRYPTO_ASN1_SUBSET
142 #include "asn1/a_meth.c"
143 #include "asn1/a_bitstr.c"
144 #include "asn1/a_d2i_fp.c"
145 #include "asn1/a_dup.c"
146 #include "asn1/a_hdr.c"
147 #include "asn1/a_i2d_fp.c"
148 #include "asn1/a_int.c"
149 #include "asn1/a_bool.c"
150 #include "asn1/a_bytes.c"
151 #include "asn1/a_object.c"
152 #include "asn1/a_octet.c"
153 #include "asn1/a_print.c"
154 #include "asn1/a_set.c"
155 #include "asn1/a_sign.c"
156 #include "asn1/a_type.c"
157 #include "asn1/a_utctm.c"
158 #include "asn1/a_verify.c"
159 #include "asn1/a_digest.c"
160 #include "asn1/asn1_err.c"
161 #include "asn1/asn1_lib.c"
162 #include "asn1/asn1_par.c"
163 #ifndef NO_DH
164 #include "asn1/d2i_dhp.c"
165 #include "asn1/i2d_dhp.c"
166 #endif
167 #ifndef NO_DSA
168 #include "asn1/d2i_dsap.c"
169 #include "asn1/i2d_dsap.c"
170 #include "asn1/d2i_s_pr.c"
171 #include "asn1/i2d_s_pr.c"
172 #include "asn1/d2i_s_pu.c"
173 #include "asn1/i2d_s_pu.c"
174 #endif
175 #ifndef NO_RSA
176 #include "asn1/d2i_r_pr.c"
177 #include "asn1/i2d_r_pr.c"
178 #include "asn1/d2i_r_pu.c"
179 #include "asn1/i2d_r_pu.c"
180 #include "asn1/n_pkey.c"
181 #endif
182 #include "asn1/d2i_pr.c"
183 #include "asn1/d2i_pu.c"
184 #include "asn1/i2d_pr.c"
185 #include "asn1/i2d_pu.c"
186 #include "asn1/f_int.c"
187 #include "asn1/f_string.c"
188 #include "asn1/p7_dgst.c"
189 #include "asn1/p7_enc.c"
190 #include "asn1/p7_enc_c.c"
191 #include "asn1/p7_evp.c"
192 #include "asn1/p7_i_s.c"
193 #include "asn1/p7_lib.c"
194 #include "asn1/p7_recip.c"
195 #include "asn1/p7_s_e.c"
196 #include "asn1/p7_signd.c"
197 #include "asn1/p7_signi.c"
198 #include "asn1/t_pkey.c"
199 #include "asn1/t_req.c"
200 #include "asn1/t_x509.c"
201 #include "asn1/x_algor.c"
202 #include "asn1/x_attrib.c"
203 #include "asn1/x_exten.c"
204 #include "asn1/x_cinf.c"
205 #include "asn1/x_crl.c"
206 #include "asn1/x_info.c"
207 #include "asn1/x_name.c"
208 #include "asn1/x_pkey.c"
209 #include "asn1/x_pubkey.c"
210 #include "asn1/x_req.c"
211 #include "asn1/x_sig.c"
212 #include "asn1/x_spki.c"
213 #include "asn1/x_val.c"
214 #include "asn1/x_x509.c"
215 #endif
216 #ifdef CRYPTO_BN_SUBSET
217 #include "bn/bn_add.c"
218 #include "bn/bn_div.c"
219 #include "bn/bn_exp.c"
220 #include "bn/bn_mont.c"
221 #include "bn/bn_recp.c"
222 #include "bn/bn_gcd.c"
223 #include "bn/bn_lib.c"
224 #include "bn/bn_mod.c"
225 #include "bn/bn_mul.c"
226 #ifndef BN_ASM
227 #include "bn/bn_mulw.c"
228 #endif
229 #include "bn/bn_prime.c"
230 #include "bn/bn_rand.c"
231 #include "bn/bn_shift.c"
232 #include "bn/bn_sqr.c"
233 #include "bn/bn_sub.c"
234 #include "bn/bn_word.c"
235 #include "bn/bn_print.c"
236 #include "bn/bn_err.c"
237 #endif
238 #ifdef CRYPTO_BIO_SUBSET
239 #include "bio/bf_buff.c"
240 #include "bio/bf_null.c"
241 #include "bio/bf_nbio.c"
242 #include "bio/bio_cb.c"
243 #include "bio/bio_lib.c"
244 #include "bio/bss_fd.c"
245 #include "bio/bss_file.c"
246 #include "bio/bss_mem.c"
247 #include "bio/bss_null.c"
248 #ifdef VMS
249 #include "bio/bss_rtcp.c"
250 #endif
251 #include "bio/bss_sock.c"
252 #include "bio/bss_conn.c"
253 #include "bio/bss_acpt.c"
254 #include "bio/b_sock.c"
255 #include "bio/b_print.c"
256 #include "bio/b_dump.c"
257 #include "bio/bio_err.c"
258 #endif
259 #ifdef CRYPTO_BUFFER_SUBSET
260 #include "buffer/buf_err.c"
261 #include "buffer/buffer.c"
262 #endif
263 #ifdef CRYPTO_CONF_SUBSET
264 #include "conf/conf.c"
265 #include "conf/conf_err.c"
266 #endif
267 #ifdef CRYPTO_DES_SUBSET
268 #include "des/read_pwd.c"
269 #ifndef NO_DES
270 #ifndef DES_ASM
271 #include "des/fcrypt_b.c"
272 #include "des/des_enc.c"
273 #endif
274 #include "des/ncbc_enc.c"
275 #include "des/cbc3_enc.c"
276 #include "des/cbc_cksm.c"
277 #include "des/xcbc_enc.c"
278 #include "des/cbc_enc.c"
279 #include "des/ede_enc.c"
280 #include "des/cfb64ede.c"
281 #include "des/cfb64enc.c"
282 #include "des/cfb_enc.c"
283 #include "des/ecb3_enc.c"
284 #include "des/ecb_enc.c"
285 #include "des/enc_read.c"
286 #include "des/enc_writ.c"
287 #include "des/fcrypt.c"
288 #include "des/ofb64ede.c"
289 #include "des/ofb64enc.c"
290 #include "des/ofb_enc.c"
291 #include "des/pcbc_enc.c"
292 #include "des/qud_cksm.c"
293 #include "des/rand_key.c"
294 #include "des/read2pwd.c"
295 #include "des/rpc_enc.c"
296 #include "des/set_key.c"
297 #include "des/str2key.c"
298 #include "des/supp.c"
299 #endif
300 #endif
301 #ifdef CRYPTO_DH_SUBSET
302 #ifndef NO_DH
303 #include "dh/dh_check.c"
304 #include "dh/dh_err.c"
305 #include "dh/dh_gen.c"
306 #include "dh/dh_key.c"
307 #include "dh/dh_lib.c"
308 #endif
309 #endif
310 #ifdef CRYPTO_DSA_SUBSET
311 #ifndef NO_DSA
312 #include "dsa/dsa_gen.c"
313 #include "dsa/dsa_key.c"
314 #include "dsa/dsa_lib.c"
315 #include "dsa/dsa_sign.c"
316 #include "dsa/dsa_vrf.c"
317 #include "dsa/dsa_err.c"
318 #endif
319 #endif
320 #ifdef CRYPTO_ERROR_SUBSET
321 #include "err/err.c"
322 #include "err/err_all.c"
323 #include "err/err_prn.c"
324 #endif
325 #ifdef CRYPTO_EVP_SUBSET
326 #include "evp/bio_md.c"
327 #include "evp/bio_b64.c"
328 #include "evp/bio_enc.c"
329 #include "evp/c_all.c"
330 #include "evp/digest.c"
331 #ifndef NO_DES
332 #include "evp/e_cbc_3d.c"
333 #include "evp/e_cfb_3d.c"
334 #include "evp/e_ecb_3d.c"
335 #include "evp/e_ofb_3d.c"
336 #include "evp/e_cbc_d.c"
337 #include "evp/e_cfb_d.c"
338 #include "evp/e_xcbc_d.c"
339 #include "evp/e_ecb_d.c"
340 #include "evp/e_ofb_d.c"
341 #endif
342 #ifndef NO_IDEA
343 #include "evp/e_cbc_i.c"
344 #include "evp/e_cfb_i.c"
345 #include "evp/e_ecb_i.c"
346 #include "evp/e_ofb_i.c"
347 #endif
348 #ifndef NO_RC2
349 #include "evp/e_cbc_r2.c"
350 #include "evp/e_cfb_r2.c"
351 #include "evp/e_ecb_r2.c"
352 #include "evp/e_ofb_r2.c"
353 #endif
354 #ifndef NO_BLOWFISH
355 #include "evp/e_cbc_bf.c"
356 #include "evp/e_cfb_bf.c"
357 #include "evp/e_ecb_bf.c"
358 #include "evp/e_ofb_bf.c"
359 #endif
360 #ifndef NO_RC4
361 #include "evp/e_rc4.c"
362 #endif
363 #include "evp/names.c"
364 #include "evp/e_null.c"
365 #include "evp/encode.c"
366 #include "evp/evp_enc.c"
367 #include "evp/evp_err.c"
368 #include "evp/evp_key.c"
369 #include "evp/m_null.c"
370 #include "evp/p_lib.c"
371 #ifndef NO_RSA
372 #include "evp/p_open.c"
373 #include "evp/p_seal.c"
374 #endif
375 #include "evp/p_sign.c"
376 #include "evp/p_verify.c"
377 #endif
378 #ifdef CRYPTO_IDEA_SUBSET
379 #ifndef NO_IDEA
380 #include "idea/i_cbc.c"
381 #include "idea/i_cfb64.c"
382 #include "idea/i_ecb.c"
383 #include "idea/i_ofb64.c"
384 #include "idea/i_skey.c"
385 #endif
386 #endif
387 #ifdef CRYPTO_BF_SUBSET
388 #ifndef NO_BLOWFISH
389 #include "bf/bf_cfb64.c"
390 #include "bf/bf_ecb.c"
391 #ifndef BF_ASM
392 #include "bf/bf_enc.c"
393 #endif
394 #include "bf/bf_cbc.c"
395 #include "bf/bf_ofb64.c"
396 #include "bf/bf_skey.c"
397 #endif
398 #endif
399 #ifdef CRYPTO_LHASH_SUBSET
400 #include "lhash/lh_stats.c"
401 #include "lhash/lhash.c"
402 #endif
403 #ifdef CRYPTO_MD_SUBSET
404 #ifndef NO_MD2
405 #include "md/md2_dgst.c"
406 #include "md/md2_one.c"
407 #include "evp/m_md2.c"
408 #endif
409 #ifndef NO_MD5
410 #include "md/md5_dgst.c"
411 #include "md/md5_one.c"
412 #include "evp/m_md5.c"
413 #endif
414 #endif
415 #ifdef CRYPTO_MDC2_SUBSET
416 #ifndef NO_MDC2
417 #include "mdc2/mdc2dgst.c"
418 #include "mdc2/mdc2_one.c"
419 #include "evp/m_mdc2.c"
420 #endif
421 #endif
422 #ifdef CRYPTO_OBJECTS_SUBSET
423 #include "objects/obj_dat.c"
424 #include "objects/obj_err.c"
425 #include "objects/obj_lib.c"
426 #endif
427 #ifdef CRYPTO_PEM_SUBSET
428 #include "pem/pem_err.c"
429 #include "pem/pem_info.c"
430 #include "pem/pem_lib.c"
431 #include "pem/pem_all.c"
432 #ifndef NO_RSA
433 #include "pem/pem_seal.c"
434 #include "pem/pem_sign.c"
435 #endif
436 #endif
437 #ifdef CRYPTO_RAND_SUBSET
438 #include "rand/md_rand.c"
439 #include "rand/randfile.c"
440 #endif
441 #ifdef CRYPTO_RC_SUBSET
442 #ifndef NO_RC4
443 #include "rc2/rc2_cbc.c"
444 #include "rc2/rc2_ecb.c"
445 #include "rc2/rc2_skey.c"
446 #include "rc2/rc2cfb64.c"
447 #include "rc2/rc2ofb64.c"
448 #include "rc4/rc4_enc.c"
449 #endif
450 #endif
451 #ifdef CRYPTO_RSA_SUBSET
452 #ifndef NO_RSA
453 #include "rsa/rsa_enc.c"
454 #include "rsa/rsa_err.c"
455 #include "rsa/rsa_gen.c"
456 #include "rsa/rsa_lib.c"
457 #include "rsa/rsa_sign.c"
458 #include "rsa/rsa_saos.c"
459 #endif
460 #endif
461 #ifdef CRYPTO_SHA1_SUBSET
462 #ifndef NO_SHA1
463 #include "sha/sha1_one.c"
464 #include "sha/sha1dgst.c"
465 #include "evp/m_dss1.c"
466 #include "evp/m_sha1.c"
467 #endif
468 #endif
469 #ifdef CRYPTO_SHA_SUBSET
470 #ifndef NO_SHA
471 #include "evp/m_dss.c"
472 #include "sha/sha_dgst.c"
473 #include "sha/sha_one.c"
474 #include "evp/m_sha.c"
475 #endif
476 #endif
477 #ifdef CRYPTO_STACK_SUBSET
478 #include "stack/stack.c"
479 #endif
480 #ifdef CRYPTO_TXT_DB_SUBSET
481 #include "txt_db/txt_db.c"
482 #endif
483 #ifdef CRYPTO_X509_SUBSET
484 #include "x509/x509_cmp.c"
485 #include "x509/x509_d2.c"
486 #include "x509/x509_def.c"
487 #include "x509/x509_err.c"
488 #include "x509/x509_ext.c"
489 #include "x509/x509_lu.c"
490 #include "x509/x509_obj.c"
491 #include "x509/x509_r2x.c"
492 #include "x509/x509_req.c"
493 #include "x509/x509_set.c"
494 #include "x509/x509_v3.c"
495 #include "x509/x509_vfy.c"
496 #include "x509/x509name.c"
497 #include "x509/x509pack.c"
498 #include "x509/x509rset.c"
499 #include "x509/x509type.c"
500 #include "x509/x_all.c"
501 #include "x509/x509_txt.c"
502 #include "x509/by_dir.c"
503 #include "x509/by_file.c"
504 #include "x509/v3_net.c"
505 #include "x509/v3_x509.c"
506 #endif
507 #endif
508 #ifdef CRYPTO_PKCS7_SUBSET /* I have an excplicit removal of 7 lines */
509 #include "pkcs7/pk7_lib.c"
510 #include "pkcs7/pkcs7err.c"
511 #endif
512