Merge lorikeet-heimdal -r 787 into Samba4 tree.
[samba.git] / source4 / heimdal / lib / hcrypto / evp.c
1 /*
2  * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright 
14  *    notice, this list of conditions and the following disclaimer in the 
15  *    documentation and/or other materials provided with the distribution. 
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors 
18  *    may be used to endorse or promote products derived from this software 
19  *    without specific prior written permission. 
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
31  * SUCH DAMAGE. 
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 RCSID("$Id: evp.c 22379 2007-12-29 11:13:26Z lha $");
39
40 #include <sys/types.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <assert.h>
45
46 #include <evp.h>
47
48 #include <krb5-types.h>
49
50 #include <aes.h>
51 #include "camellia.h"
52 #include <des.h>
53 #include <sha.h>
54 #include <rc2.h>
55 #include <rc4.h>
56 #include <md2.h>
57 #include <md4.h>
58 #include <md5.h>
59
60 /**
61  * @page page_evp EVP - generic crypto interface
62  *
63  * See the library functions here: @ref hcrypto_evp
64  */
65
66
67 typedef int (*evp_md_init)(EVP_MD_CTX *);
68 typedef int (*evp_md_update)(EVP_MD_CTX *,const void *, size_t);
69 typedef int (*evp_md_final)(void *, EVP_MD_CTX *);
70 typedef int (*evp_md_cleanup)(EVP_MD_CTX *);
71
72 struct hc_evp_md {
73     int hash_size;
74     int block_size;
75     int ctx_size;
76     evp_md_init init;
77     evp_md_update update;
78     evp_md_final final;
79     evp_md_cleanup cleanup;
80 };
81
82 /**
83  * Return the output size of the message digest function.
84  *
85  * @param md the evp message
86  *
87  * @return size output size of the message digest function.
88  *
89  * @ingroup hcrypto_evp
90  */
91
92 size_t
93 EVP_MD_size(const EVP_MD *md)
94 {
95     return md->hash_size;
96 }
97
98 /**
99  * Return the blocksize of the message digest function.
100  *
101  * @param md the evp message
102  *
103  * @return size size of the message digest block size
104  *
105  * @ingroup hcrypto_evp
106  */
107
108 size_t
109 EVP_MD_block_size(const EVP_MD *md)
110 {
111     return md->block_size;
112 }
113
114 /**
115  * Allocate a messsage digest context object. Free with
116  * EVP_MD_CTX_destroy().
117  *
118  * @return a newly allocated message digest context object.
119  *
120  * @ingroup hcrypto_evp
121  */
122
123 EVP_MD_CTX *
124 EVP_MD_CTX_create(void)
125 {
126     return calloc(1, sizeof(EVP_MD_CTX));
127 }
128
129 /**
130  * Initiate a messsage digest context object. Deallocate with
131  * EVP_MD_CTX_cleanup(). Please use EVP_MD_CTX_create() instead.
132  *
133  * @param ctx variable to initiate.
134  *
135  * @ingroup hcrypto_evp
136  */
137
138 void
139 EVP_MD_CTX_init(EVP_MD_CTX *ctx)
140 {
141     memset(ctx, 0, sizeof(*ctx));
142 }
143
144 /**
145  * Free a messsage digest context object.
146  *
147  * @param ctx context to free.
148  *
149  * @ingroup hcrypto_evp
150  */
151
152 void
153 EVP_MD_CTX_destroy(EVP_MD_CTX *ctx)
154 {
155     EVP_MD_CTX_cleanup(ctx);
156     free(ctx);
157 }
158
159 /**
160  * Free the resources used by the EVP_MD context.
161  *
162  * @param ctx the context to free the resources from.
163  *
164  * @return 1 on success.
165  *
166  * @ingroup hcrypto_evp
167  */
168
169 int
170 EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
171 {
172     if (ctx->md && ctx->md->cleanup)
173         (ctx->md->cleanup)(ctx);
174     ctx->md = NULL;
175     ctx->engine = NULL;
176     free(ctx->ptr);
177     memset(ctx, 0, sizeof(*ctx));
178     return 1;
179 }
180
181 /**
182  * Get the EVP_MD use for a specified context.
183  *
184  * @param ctx the EVP_MD context to get the EVP_MD for.
185  *
186  * @return the EVP_MD used for the context.
187  *
188  * @ingroup hcrypto_evp
189  */
190
191 const EVP_MD *
192 EVP_MD_CTX_md(EVP_MD_CTX *ctx)
193 {
194     return ctx->md;
195 }
196
197 /**
198  * Return the output size of the message digest function.
199  *
200  * @param ctx the evp message digest context
201  *
202  * @return size output size of the message digest function.
203  *
204  * @ingroup hcrypto_evp
205  */
206
207 size_t
208 EVP_MD_CTX_size(EVP_MD_CTX *ctx)
209 {
210     return EVP_MD_size(ctx->md);
211 }
212
213 /**
214  * Return the blocksize of the message digest function.
215  *
216  * @param ctx the evp message digest context
217  *
218  * @return size size of the message digest block size
219  *
220  * @ingroup hcrypto_evp
221  */
222
223 size_t
224 EVP_MD_CTX_block_size(EVP_MD_CTX *ctx)
225 {
226     return EVP_MD_block_size(ctx->md);
227 }
228
229 /**
230  * Init a EVP_MD_CTX for use a specific message digest and engine.
231  *
232  * @param ctx the message digest context to init.
233  * @param md the message digest to use.
234  * @param engine the engine to use, NULL to use the default engine.
235  *
236  * @return 1 on success.
237  *
238  * @ingroup hcrypto_evp
239  */
240
241 int
242 EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *md, ENGINE *engine)
243 {
244     if (ctx->md != md || ctx->engine != engine) {
245         EVP_MD_CTX_cleanup(ctx);
246         ctx->md = md;
247         ctx->engine = engine;
248
249         ctx->ptr = calloc(1, md->ctx_size);
250         if (ctx->ptr == NULL)
251             return 0;
252     }
253     (ctx->md->init)(ctx->ptr);
254     return 1;
255 }
256
257 /**
258  * Update the digest with some data.
259  *
260  * @param ctx the context to update
261  * @param data the data to update the context with
262  * @param size length of data
263  *
264  * @return 1 on success.
265  *
266  * @ingroup hcrypto_evp
267  */
268
269 int
270 EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t size)
271 {
272     (ctx->md->update)(ctx->ptr, data, size);
273     return 1;
274 }
275
276 /**
277  * Complete the message digest.
278  *
279  * @param ctx the context to complete.
280  * @param hash the output of the message digest function. At least
281  * EVP_MD_size().
282  * @param size the output size of hash.
283  *
284  * @return 1 on success.
285  *
286  * @ingroup hcrypto_evp
287  */
288
289 int
290 EVP_DigestFinal_ex(EVP_MD_CTX *ctx, void *hash, unsigned int *size)
291 {
292     (ctx->md->final)(hash, ctx->ptr);
293     if (size)
294         *size = ctx->md->hash_size;
295     return 1;
296 }
297
298 /**
299  * Do the whole EVP_MD_CTX_create(), EVP_DigestInit_ex(),
300  * EVP_DigestUpdate(), EVP_DigestFinal_ex(), EVP_MD_CTX_destroy()
301  * dance in one call.
302  *
303  * @param data the data to update the context with
304  * @param dsize length of data
305  * @param hash output data of at least EVP_MD_size() length.
306  * @param hsize output length of hash.
307  * @param md message digest to use
308  * @param engine engine to use, NULL for default engine.
309  *
310  * @return 1 on success.
311  *
312  * @ingroup hcrypto_evp
313  */
314
315 int
316 EVP_Digest(const void *data, size_t dsize, void *hash, unsigned int *hsize, 
317            const EVP_MD *md, ENGINE *engine)
318 {
319     EVP_MD_CTX *ctx;
320     int ret;
321
322     ctx = EVP_MD_CTX_create();
323     if (ctx == NULL)
324         return 0;
325     ret = EVP_DigestInit_ex(ctx, md, engine);
326     if (ret != 1) {
327         EVP_MD_CTX_destroy(ctx);
328         return ret;
329     }
330     ret = EVP_DigestUpdate(ctx, data, dsize);
331     if (ret != 1) {
332         EVP_MD_CTX_destroy(ctx);
333         return ret;
334     }
335     ret = EVP_DigestFinal_ex(ctx, hash, hsize);
336     EVP_MD_CTX_destroy(ctx);
337     return ret;
338 }
339
340 /**
341  * The message digest SHA256
342  *
343  * @return the message digest type.
344  *
345  * @ingroup hcrypto_evp
346  */
347
348 const EVP_MD *
349 EVP_sha256(void)
350 {
351     static const struct hc_evp_md sha256 = {
352         32,
353         64,
354         sizeof(SHA256_CTX),
355         (evp_md_init)SHA256_Init,
356         (evp_md_update)SHA256_Update,
357         (evp_md_final)SHA256_Final,
358         NULL
359     };
360     return &sha256;
361 }
362
363 static const struct hc_evp_md sha1 = {
364     20,
365     64,
366     sizeof(SHA_CTX),
367     (evp_md_init)SHA1_Init,
368     (evp_md_update)SHA1_Update,
369     (evp_md_final)SHA1_Final,
370     NULL
371 };
372
373 /**
374  * The message digest SHA1
375  *
376  * @return the message digest type.
377  *
378  * @ingroup hcrypto_evp
379  */
380
381 const EVP_MD *
382 EVP_sha1(void)
383 {
384     return &sha1;
385 }
386
387 /**
388  * The message digest SHA1
389  *
390  * @return the message digest type.
391  *
392  * @ingroup hcrypto_evp
393  */
394
395 const EVP_MD *
396 EVP_sha(void)
397 {
398     return &sha1;
399 }
400
401 /**
402  * The message digest MD5
403  *
404  * @return the message digest type.
405  *
406  * @ingroup hcrypto_evp
407  */
408
409 const EVP_MD *
410 EVP_md5(void)
411 {
412     static const struct hc_evp_md md5 = {
413         16,
414         64,
415         sizeof(MD5_CTX),
416         (evp_md_init)MD5_Init,
417         (evp_md_update)MD5_Update,
418         (evp_md_final)MD5_Final,
419         NULL
420     };
421     return &md5;
422 }
423
424 /**
425  * The message digest MD4
426  *
427  * @return the message digest type.
428  *
429  * @ingroup hcrypto_evp
430  */
431
432 const EVP_MD *
433 EVP_md4(void)
434 {
435     static const struct hc_evp_md md4 = {
436         16,
437         64,
438         sizeof(MD4_CTX),
439         (evp_md_init)MD4_Init,
440         (evp_md_update)MD4_Update,
441         (evp_md_final)MD4_Final,
442         NULL
443     };
444     return &md4;
445 }
446
447 /**
448  * The message digest MD2
449  *
450  * @return the message digest type.
451  *
452  * @ingroup hcrypto_evp
453  */
454
455 const EVP_MD *
456 EVP_md2(void)
457 {
458     static const struct hc_evp_md md2 = {
459         16,
460         16,
461         sizeof(MD2_CTX),
462         (evp_md_init)MD2_Init,
463         (evp_md_update)MD2_Update,
464         (evp_md_final)MD2_Final,
465         NULL
466     };
467     return &md2;
468 }
469
470 /*
471  *
472  */
473
474 static void
475 null_Init (void *m)
476 {
477 }
478 static void
479 null_Update (void *m, const void * data, size_t size)
480 {
481 }
482 static void
483 null_Final(void *res, void *m)
484 {
485 }
486
487 /**
488  * The null message digest
489  *
490  * @return the message digest type.
491  *
492  * @ingroup hcrypto_evp
493  */
494
495 const EVP_MD *
496 EVP_md_null(void)
497 {
498     static const struct hc_evp_md null = {
499         0,
500         0,
501         0,
502         (evp_md_init)null_Init,
503         (evp_md_update)null_Update,
504         (evp_md_final)null_Final,
505         NULL
506     };
507     return &null;
508 }
509
510 #if 0
511 void    EVP_MD_CTX_init(EVP_MD_CTX *ctx);
512 int     EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
513 int     EVP_DigestFinal(EVP_MD_CTX *ctx,unsigned char *md,unsigned int *s);
514 int     EVP_SignFinal(EVP_MD_CTX *, void *, size_t *, EVP_PKEY *);
515 int     EVP_VerifyFinal(EVP_MD_CTX *, const void *, size_t, EVP_PKEY *);
516 #endif
517
518 /**
519  * Return the block size of the cipher.
520  *
521  * @param c cipher to get the block size from.
522  *
523  * @return the block size of the cipher.
524  *
525  * @ingroup hcrypto_evp
526  */
527
528 size_t
529 EVP_CIPHER_block_size(const EVP_CIPHER *c)
530 {
531     return c->block_size;
532 }
533
534 /**
535  * Return the key size of the cipher.
536  *
537  * @param c cipher to get the key size from.
538  *
539  * @return the key size of the cipher.
540  *
541  * @ingroup hcrypto_evp
542  */
543
544 size_t
545 EVP_CIPHER_key_length(const EVP_CIPHER *c)
546 {
547     return c->key_len;
548 }
549
550 /**
551  * Return the IV size of the cipher.
552  *
553  * @param c cipher to get the IV size from.
554  *
555  * @return the IV size of the cipher.
556  *
557  * @ingroup hcrypto_evp
558  */
559
560 size_t
561 EVP_CIPHER_iv_length(const EVP_CIPHER *c)
562 {
563     return c->iv_len;
564 }
565
566 /**
567  * Initiate a EVP_CIPHER_CTX context. Clean up with
568  * EVP_CIPHER_CTX_cleanup().
569  *
570  * @param c the cipher initiate.
571  *
572  * @ingroup hcrypto_evp
573  */
574
575 void
576 EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *c)
577 {
578     memset(c, 0, sizeof(*c));
579 }
580
581 /**
582  * Clean up the EVP_CIPHER_CTX context.
583  *
584  * @param c the cipher to clean up.
585  *
586  * @return 1 on success.
587  *
588  * @ingroup hcrypto_evp
589  */
590
591 int
592 EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
593 {
594     if (c->cipher && c->cipher->cleanup)
595         c->cipher->cleanup(c);
596     if (c->cipher_data) {
597         free(c->cipher_data);
598         c->cipher_data = NULL;
599     }
600     return 1;
601 }
602
603 #if 0
604 int
605 EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int length)
606 {
607     return 0;
608 }
609
610 int
611 EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *c, int pad)
612 {
613     return 0;
614 }
615 #endif
616
617 /**
618  * Return the EVP_CIPHER for a EVP_CIPHER_CTX context.
619  *
620  * @param ctx the context to get the cipher type from.
621  *
622  * @return the EVP_CIPHER pointer.
623  *
624  * @ingroup hcrypto_evp
625  */
626
627 const EVP_CIPHER *
628 EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX *ctx)
629 {
630     return ctx->cipher;
631 }
632
633 /**
634  * Return the block size of the cipher context.
635  *
636  * @param ctx cipher context to get the block size from.
637  *
638  * @return the block size of the cipher context.
639  *
640  * @ingroup hcrypto_evp
641  */
642
643 size_t
644 EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
645 {
646     return EVP_CIPHER_block_size(ctx->cipher);
647 }
648
649 /**
650  * Return the key size of the cipher context.
651  *
652  * @param ctx cipher context to get the key size from.
653  *
654  * @return the key size of the cipher context.
655  *
656  * @ingroup hcrypto_evp
657  */
658
659 size_t
660 EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
661 {
662     return EVP_CIPHER_key_length(ctx->cipher);
663 }
664
665 /**
666  * Return the IV size of the cipher context.
667  *
668  * @param ctx cipher context to get the IV size from.
669  *
670  * @return the IV size of the cipher context.
671  *
672  * @ingroup hcrypto_evp
673  */
674
675 size_t
676 EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
677 {
678     return EVP_CIPHER_iv_length(ctx->cipher);
679 }
680
681 /**
682  * Get the flags for an EVP_CIPHER_CTX context.
683  *
684  * @param ctx the EVP_CIPHER_CTX to get the flags from
685  *
686  * @return the flags for an EVP_CIPHER_CTX.
687  *
688  * @ingroup hcrypto_evp
689  */
690
691 unsigned long
692 EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx)
693 {
694     return ctx->cipher->flags;
695 }
696
697 /**
698  * Get the mode for an EVP_CIPHER_CTX context.
699  *
700  * @param ctx the EVP_CIPHER_CTX to get the mode from
701  *
702  * @return the mode for an EVP_CIPHER_CTX.
703  *
704  * @ingroup hcrypto_evp
705  */
706
707 int
708 EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx)
709 {
710     return EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_MODE;
711 }
712
713 /**
714  * Get the app data for an EVP_CIPHER_CTX context.
715  *
716  * @param ctx the EVP_CIPHER_CTX to get the app data from
717  *
718  * @return the app data for an EVP_CIPHER_CTX.
719  *
720  * @ingroup hcrypto_evp
721  */
722
723 void *
724 EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX *ctx)
725 {
726     return ctx->app_data;
727 }
728
729 /**
730  * Set the app data for an EVP_CIPHER_CTX context.
731  *
732  * @param ctx the EVP_CIPHER_CTX to set the app data for
733  * @param data the app data to set for an EVP_CIPHER_CTX.
734  *
735  * @ingroup hcrypto_evp
736  */
737
738 void
739 EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
740 {
741     ctx->app_data = data;
742 }
743
744 /**
745  * Initiate the EVP_CIPHER_CTX context to encrypt or decrypt data.
746  * Clean up with EVP_CIPHER_CTX_cleanup().
747  *
748  * @param ctx context to initiate
749  * @param c cipher to use.
750  * @param engine crypto engine to use, NULL to select default.
751  * @param key the crypto key to use, NULL will use the previous value.
752  * @param iv the IV to use, NULL will use the previous value.
753  * @param encp non zero will encrypt, -1 use the previous value.
754  *
755  * @return 1 on success.
756  *
757  * @ingroup hcrypto_evp
758  */
759
760 int
761 EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *c, ENGINE *engine,
762                   const void *key, const void *iv, int encp)
763 {
764     if (encp == -1)
765         encp = ctx->encrypt;
766     else
767         ctx->encrypt = (encp ? 1 : 0);
768
769     if (c && (c != ctx->cipher)) {
770         EVP_CIPHER_CTX_cleanup(ctx);
771         ctx->cipher = c;
772         ctx->key_len = c->key_len;
773
774         ctx->cipher_data = malloc(c->ctx_size);
775         if (ctx->cipher_data == NULL && c->ctx_size != 0)
776             return 0;
777
778     } else if (ctx->cipher == NULL) {
779         /* reuse of cipher, but not any cipher ever set! */
780         return 0;
781     }
782
783     switch (EVP_CIPHER_CTX_flags(ctx)) {
784     case EVP_CIPH_CBC_MODE:
785
786         assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv));
787
788         if (iv)
789             memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
790         memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
791         break;
792     default:
793         return 0;
794     }
795
796     if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT))
797         ctx->cipher->init(ctx, key, iv, encp);
798
799     return 1;
800 }
801
802 /**
803  * Encypher/decypher data
804  *
805  * @param ctx the cipher context.
806  * @param out out data from the operation.
807  * @param in in data to the operation.
808  * @param size length of data.
809  *
810  * @return 1 on success.
811  */
812
813 int
814 EVP_Cipher(EVP_CIPHER_CTX *ctx, void *out, const void *in,size_t size)
815 {
816     return ctx->cipher->do_cipher(ctx, out, in, size);
817 }
818
819 /*
820  *
821  */
822
823 static int
824 enc_null_init(EVP_CIPHER_CTX *ctx,
825                   const unsigned char * key,
826                   const unsigned char * iv,
827                   int encp)
828 {
829     return 1;
830 }
831
832 static int
833 enc_null_do_cipher(EVP_CIPHER_CTX *ctx,
834               unsigned char *out,
835               const unsigned char *in,
836               unsigned int size)
837 {
838     memmove(out, in, size);
839     return 1;
840 }
841
842 static int
843 enc_null_cleanup(EVP_CIPHER_CTX *ctx)
844 {
845     return 1;
846 }
847
848 /**
849  * The NULL cipher type, does no encryption/decryption.
850  *
851  * @return the null EVP_CIPHER pointer.
852  *
853  * @ingroup hcrypto_evp
854  */
855
856 const EVP_CIPHER *
857 EVP_enc_null(void)
858 {
859     static const EVP_CIPHER enc_null = {
860         0,
861         0,
862         0,
863         0,
864         EVP_CIPH_CBC_MODE,
865         enc_null_init,
866         enc_null_do_cipher,
867         enc_null_cleanup,
868         0,
869         NULL,
870         NULL,
871         NULL,
872         NULL
873     };
874     return &enc_null;
875 }
876
877 /*
878  *
879  */
880
881 struct rc2_cbc {
882     unsigned int maximum_effective_key;
883     RC2_KEY key;
884 };
885
886 static int
887 rc2_init(EVP_CIPHER_CTX *ctx,
888          const unsigned char * key,
889          const unsigned char * iv,
890          int encp)
891 {
892     struct rc2_cbc *k = ctx->cipher_data;
893     k->maximum_effective_key = EVP_CIPHER_CTX_key_length(ctx) * 8;
894     RC2_set_key(&k->key,
895                 EVP_CIPHER_CTX_key_length(ctx),
896                 key,
897                 k->maximum_effective_key);
898     return 1;
899 }
900
901 static int
902 rc2_do_cipher(EVP_CIPHER_CTX *ctx,
903               unsigned char *out,
904               const unsigned char *in,
905               unsigned int size)
906 {
907     struct rc2_cbc *k = ctx->cipher_data;
908     RC2_cbc_encrypt(in, out, size, &k->key, ctx->iv, ctx->encrypt);
909     return 1;
910 }
911
912 static int
913 rc2_cleanup(EVP_CIPHER_CTX *ctx)
914 {
915     memset(ctx->cipher_data, 0, sizeof(struct rc2_cbc));
916     return 1;
917 }
918
919 /**
920  * The RC2 cipher type
921  *
922  * @return the RC2 EVP_CIPHER pointer.
923  *
924  * @ingroup hcrypto_evp
925  */
926
927 const EVP_CIPHER *
928 EVP_rc2_cbc(void)
929 {
930     static const EVP_CIPHER rc2_cbc = {
931         0,
932         RC2_BLOCK_SIZE,
933         RC2_KEY_LENGTH,
934         RC2_BLOCK_SIZE,
935         EVP_CIPH_CBC_MODE,
936         rc2_init,
937         rc2_do_cipher,
938         rc2_cleanup,
939         sizeof(struct rc2_cbc),
940         NULL,
941         NULL,
942         NULL,
943         NULL
944     };
945     return &rc2_cbc;
946 }
947
948 /**
949  * The RC2-40 cipher type
950  *
951  * @return the RC2-40 EVP_CIPHER pointer.
952  *
953  * @ingroup hcrypto_evp
954  */
955
956 const EVP_CIPHER *
957 EVP_rc2_40_cbc(void)
958 {
959     static const EVP_CIPHER rc2_40_cbc = {
960         0,
961         RC2_BLOCK_SIZE,
962         5,
963         RC2_BLOCK_SIZE,
964         EVP_CIPH_CBC_MODE,
965         rc2_init,
966         rc2_do_cipher,
967         rc2_cleanup,
968         sizeof(struct rc2_cbc),
969         NULL,
970         NULL,
971         NULL,
972         NULL
973     };
974     return &rc2_40_cbc;
975 }
976
977 /**
978  * The RC2-64 cipher type
979  *
980  * @return the RC2-64 EVP_CIPHER pointer.
981  *
982  * @ingroup hcrypto_evp
983  */
984
985 const EVP_CIPHER *
986 EVP_rc2_64_cbc(void)
987 {
988     static const EVP_CIPHER rc2_64_cbc = {
989         0,
990         RC2_BLOCK_SIZE,
991         8,
992         RC2_BLOCK_SIZE,
993         EVP_CIPH_CBC_MODE,
994         rc2_init,
995         rc2_do_cipher,
996         rc2_cleanup,
997         sizeof(struct rc2_cbc),
998         NULL,
999         NULL,
1000         NULL,
1001         NULL
1002     };
1003     return &rc2_64_cbc;
1004 }
1005
1006 /**
1007  * The RC4 cipher type
1008  *
1009  * @return the RC4 EVP_CIPHER pointer.
1010  *
1011  * @ingroup hcrypto_evp
1012  */
1013
1014 const EVP_CIPHER *
1015 EVP_rc4(void)
1016 {
1017     printf("evp rc4\n");
1018     abort();
1019     return NULL;
1020 }
1021
1022 /**
1023  * The RC4-40 cipher type
1024  *
1025  * @return the RC4-40 EVP_CIPHER pointer.
1026  *
1027  * @ingroup hcrypto_evp
1028  */
1029
1030 const EVP_CIPHER *
1031 EVP_rc4_40(void)
1032 {
1033     printf("evp rc4_40\n");
1034     abort();
1035     return NULL;
1036 }
1037
1038 /*
1039  *
1040  */
1041
1042 struct des_ede3_cbc {
1043     DES_key_schedule ks[3];
1044 };
1045
1046 static int
1047 des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
1048                   const unsigned char * key,
1049                   const unsigned char * iv,
1050                   int encp)
1051 {
1052     struct des_ede3_cbc *k = ctx->cipher_data;
1053
1054     DES_key_sched((DES_cblock *)(key), &k->ks[0]);
1055     DES_key_sched((DES_cblock *)(key + 8), &k->ks[1]);
1056     DES_key_sched((DES_cblock *)(key + 16), &k->ks[2]);
1057
1058     return 1;
1059 }
1060
1061 static int
1062 des_ede3_cbc_do_cipher(EVP_CIPHER_CTX *ctx,
1063                        unsigned char *out,
1064                        const unsigned char *in,
1065                        unsigned int size)
1066 {
1067     struct des_ede3_cbc *k = ctx->cipher_data;
1068     DES_ede3_cbc_encrypt(in, out, size,
1069                          &k->ks[0], &k->ks[1], &k->ks[2],
1070                          (DES_cblock *)ctx->iv, ctx->encrypt);
1071     return 1;
1072 }
1073
1074 static int
1075 des_ede3_cbc_cleanup(EVP_CIPHER_CTX *ctx)
1076 {
1077     memset(ctx->cipher_data, 0, sizeof(struct des_ede3_cbc));
1078     return 1;
1079 }
1080
1081 /**
1082  * The tripple DES cipher type
1083  *
1084  * @return the DES-EDE3-CBC EVP_CIPHER pointer.
1085  *
1086  * @ingroup hcrypto_evp
1087  */
1088
1089 const EVP_CIPHER *
1090 EVP_des_ede3_cbc(void)
1091 {
1092     static const EVP_CIPHER des_ede3_cbc = {
1093         0,
1094         8,
1095         24,
1096         8,
1097         EVP_CIPH_CBC_MODE,
1098         des_ede3_cbc_init,
1099         des_ede3_cbc_do_cipher,
1100         des_ede3_cbc_cleanup,
1101         sizeof(struct des_ede3_cbc),
1102         NULL,
1103         NULL,
1104         NULL,
1105         NULL
1106     };
1107     return &des_ede3_cbc;
1108 }
1109
1110 /*
1111  *
1112  */
1113
1114 static int
1115 aes_init(EVP_CIPHER_CTX *ctx,
1116          const unsigned char * key,
1117          const unsigned char * iv,
1118          int encp)
1119 {
1120     AES_KEY *k = ctx->cipher_data;
1121     if (ctx->encrypt)
1122         AES_set_encrypt_key(key, ctx->cipher->key_len * 8, k);
1123     else
1124         AES_set_decrypt_key(key, ctx->cipher->key_len * 8, k);
1125     return 1;
1126 }
1127
1128 static int
1129 aes_do_cipher(EVP_CIPHER_CTX *ctx,
1130               unsigned char *out,
1131               const unsigned char *in,
1132               unsigned int size)
1133 {
1134     AES_KEY *k = ctx->cipher_data;
1135     AES_cbc_encrypt(in, out, size, k, ctx->iv, ctx->encrypt);
1136     return 1;
1137 }
1138
1139 static int
1140 aes_cleanup(EVP_CIPHER_CTX *ctx)
1141 {
1142     memset(ctx->cipher_data, 0, sizeof(AES_KEY));
1143     return 1;
1144 }
1145
1146 /**
1147  * The AES-128 cipher type
1148  *
1149  * @return the AES-128 EVP_CIPHER pointer.
1150  *
1151  * @ingroup hcrypto_evp
1152  */
1153
1154 const EVP_CIPHER *
1155 EVP_aes_128_cbc(void)
1156 {
1157     static const EVP_CIPHER aes_128_cbc = {
1158         0,
1159         16,
1160         16,
1161         16,
1162         EVP_CIPH_CBC_MODE,
1163         aes_init,
1164         aes_do_cipher,
1165         aes_cleanup,
1166         sizeof(AES_KEY),
1167         NULL,
1168         NULL,
1169         NULL,
1170         NULL
1171     };
1172     return &aes_128_cbc;
1173 }
1174
1175 /**
1176  * The AES-192 cipher type
1177  *
1178  * @return the AES-192 EVP_CIPHER pointer.
1179  *
1180  * @ingroup hcrypto_evp
1181  */
1182
1183 const EVP_CIPHER *
1184 EVP_aes_192_cbc(void)
1185 {
1186     static const EVP_CIPHER aes_192_cbc = {
1187         0,
1188         16,
1189         24,
1190         16,
1191         EVP_CIPH_CBC_MODE,
1192         aes_init,
1193         aes_do_cipher,
1194         aes_cleanup,
1195         sizeof(AES_KEY),
1196         NULL,
1197         NULL,
1198         NULL,
1199         NULL
1200     };
1201     return &aes_192_cbc;
1202 }
1203
1204 /**
1205  * The AES-256 cipher type
1206  *
1207  * @return the AES-256 EVP_CIPHER pointer.
1208  *
1209  * @ingroup hcrypto_evp
1210  */
1211
1212 const EVP_CIPHER *
1213 EVP_aes_256_cbc(void)
1214 {
1215     static const EVP_CIPHER aes_256_cbc = {
1216         0,
1217         16,
1218         32,
1219         16,
1220         EVP_CIPH_CBC_MODE,
1221         aes_init,
1222         aes_do_cipher,
1223         aes_cleanup,
1224         sizeof(AES_KEY),
1225         NULL,
1226         NULL,
1227         NULL,
1228         NULL
1229     };
1230     return &aes_256_cbc;
1231 }
1232
1233 static int
1234 camellia_init(EVP_CIPHER_CTX *ctx,
1235          const unsigned char * key,
1236          const unsigned char * iv,
1237          int encp)
1238 {
1239     CAMELLIA_KEY *k = ctx->cipher_data;
1240     k->bits = ctx->cipher->key_len * 8;
1241     CAMELLIA_set_key(key, ctx->cipher->key_len * 8, k);
1242     return 1;
1243 }
1244
1245 static int
1246 camellia_do_cipher(EVP_CIPHER_CTX *ctx,
1247               unsigned char *out,
1248               const unsigned char *in,
1249               unsigned int size)
1250 {
1251     CAMELLIA_KEY *k = ctx->cipher_data;
1252     CAMELLIA_cbc_encrypt(in, out, size, k, ctx->iv, ctx->encrypt);
1253     return 1;
1254 }
1255
1256 static int
1257 camellia_cleanup(EVP_CIPHER_CTX *ctx)
1258 {
1259     memset(ctx->cipher_data, 0, sizeof(CAMELLIA_KEY));
1260     return 1;
1261 }
1262
1263 /**
1264  * The Camellia-128 cipher type
1265  *
1266  * @return the Camellia-128 EVP_CIPHER pointer.
1267  *
1268  * @ingroup hcrypto_evp
1269  */
1270
1271 const EVP_CIPHER *
1272 EVP_camellia_128_cbc(void)
1273 {
1274     static const EVP_CIPHER cipher = {
1275         0,
1276         16,
1277         16,
1278         16,
1279         EVP_CIPH_CBC_MODE,
1280         camellia_init,
1281         camellia_do_cipher,
1282         camellia_cleanup,
1283         sizeof(CAMELLIA_KEY),
1284         NULL,
1285         NULL,
1286         NULL,
1287         NULL
1288     };
1289     return &cipher;
1290 }
1291
1292 /**
1293  * The Camellia-198 cipher type
1294  *
1295  * @return the Camellia-198 EVP_CIPHER pointer.
1296  *
1297  * @ingroup hcrypto_evp
1298  */
1299
1300 const EVP_CIPHER *
1301 EVP_camellia_192_cbc(void)
1302 {
1303     static const EVP_CIPHER cipher = {
1304         0,
1305         16,
1306         24,
1307         16,
1308         EVP_CIPH_CBC_MODE,
1309         camellia_init,
1310         camellia_do_cipher,
1311         camellia_cleanup,
1312         sizeof(CAMELLIA_KEY),
1313         NULL,
1314         NULL,
1315         NULL,
1316         NULL
1317     };
1318     return &cipher;
1319 }
1320
1321 /**
1322  * The Camellia-256 cipher type
1323  *
1324  * @return the Camellia-256 EVP_CIPHER pointer.
1325  *
1326  * @ingroup hcrypto_evp
1327  */
1328
1329 const EVP_CIPHER *
1330 EVP_camellia_256_cbc(void)
1331 {
1332     static const EVP_CIPHER cipher = {
1333         0,
1334         16,
1335         32,
1336         16,
1337         EVP_CIPH_CBC_MODE,
1338         camellia_init,
1339         camellia_do_cipher,
1340         camellia_cleanup,
1341         sizeof(CAMELLIA_KEY),
1342         NULL,
1343         NULL,
1344         NULL,
1345         NULL
1346     };
1347     return &cipher;
1348 }
1349
1350 /*
1351  *
1352  */
1353
1354 static const struct cipher_name {
1355     const char *name;
1356     const EVP_CIPHER *(*func)(void);
1357 } cipher_name[] = {
1358     { "des-ede3-cbc", EVP_des_ede3_cbc },
1359     { "aes-128-cbc", EVP_aes_128_cbc },
1360     { "aes-192-cbc", EVP_aes_192_cbc },
1361     { "aes-256-cbc", EVP_aes_256_cbc },
1362     { "camellia-128-cbc", EVP_camellia_128_cbc },
1363     { "camellia-192-cbc", EVP_camellia_192_cbc },
1364     { "camellia-256-cbc", EVP_camellia_256_cbc }
1365 };
1366
1367 /**
1368  * Get the cipher type using their name.
1369  *
1370  * @param name the name of the cipher.
1371  *
1372  * @return the selected EVP_CIPHER pointer or NULL if not found.
1373  *
1374  * @ingroup hcrypto_evp
1375  */
1376
1377 const EVP_CIPHER *
1378 EVP_get_cipherbyname(const char *name)
1379 {
1380     int i;
1381     for (i = 0; i < sizeof(cipher_name)/sizeof(cipher_name[0]); i++) {
1382         if (strcasecmp(cipher_name[i].name, name) == 0)
1383             return (*cipher_name[i].func)();
1384     }
1385     return NULL;
1386 }
1387
1388
1389 /*
1390  *
1391  */
1392
1393 #ifndef min
1394 #define min(a,b) (((a)>(b))?(b):(a))
1395 #endif
1396
1397 /**
1398  * Provides a legancy string to key function, used in PEM files.
1399  *
1400  * New protocols should use new string to key functions like NIST
1401  * SP56-800A or PKCS#5 v2.0 (see PKCS5_PBKDF2_HMAC_SHA1()).
1402  *
1403  * @param type type of cipher to use
1404  * @param md message digest to use
1405  * @param salt salt salt string, should be an binary 8 byte buffer.
1406  * @param data the password/input key string.
1407  * @param datalen length of data parameter.
1408  * @param count iteration counter.
1409  * @param keydata output keydata, needs to of the size EVP_CIPHER_key_length().
1410  * @param ivdata output ivdata, needs to of the size EVP_CIPHER_block_size().
1411  *
1412  * @return the size of derived key.
1413  *
1414  * @ingroup hcrypto_evp
1415  */
1416
1417 int
1418 EVP_BytesToKey(const EVP_CIPHER *type,
1419                const EVP_MD *md, 
1420                const void *salt,
1421                const void *data, size_t datalen,
1422                unsigned int count,
1423                void *keydata,
1424                void *ivdata)
1425 {
1426     int ivlen, keylen, first = 0;
1427     unsigned int mds = 0, i;
1428     unsigned char *key = keydata;
1429     unsigned char *iv = ivdata;
1430     unsigned char *buf;
1431     EVP_MD_CTX c;
1432
1433     keylen = EVP_CIPHER_key_length(type);
1434     ivlen = EVP_CIPHER_iv_length(type);
1435
1436     if (data == NULL)
1437         return keylen;
1438
1439     buf = malloc(EVP_MD_size(md));
1440     if (buf == NULL)
1441         return -1;
1442
1443     EVP_MD_CTX_init(&c);
1444
1445     first = 1;
1446     while (1) {
1447         EVP_DigestInit_ex(&c, md, NULL);
1448         if (!first)
1449             EVP_DigestUpdate(&c, buf, mds);
1450         first = 0;
1451         EVP_DigestUpdate(&c,data,datalen);
1452
1453 #define PKCS5_SALT_LEN 8
1454
1455         if (salt)
1456             EVP_DigestUpdate(&c, salt, PKCS5_SALT_LEN);
1457
1458         EVP_DigestFinal_ex(&c, buf, &mds);
1459         assert(mds == EVP_MD_size(md));
1460
1461         for (i = 1; i < count; i++) {
1462             EVP_DigestInit_ex(&c, md, NULL);
1463             EVP_DigestUpdate(&c, buf, mds);
1464             EVP_DigestFinal_ex(&c, buf, &mds);
1465             assert(mds == EVP_MD_size(md));
1466         }
1467
1468         i = 0;
1469         if (keylen) {
1470             size_t sz = min(keylen, mds);
1471             if (key) {
1472                 memcpy(key, buf, sz);
1473                 key += sz;
1474             }
1475             keylen -= sz;
1476             i += sz;
1477         }
1478         if (ivlen && mds > i) {
1479             size_t sz = min(ivlen, (mds - i));
1480             if (iv) {
1481                 memcpy(iv, &buf[i], sz);
1482                 iv += sz;
1483             }
1484             ivlen -= sz;
1485         }
1486         if (keylen == 0 && ivlen == 0)
1487             break;
1488     }
1489
1490     EVP_MD_CTX_cleanup(&c);
1491     free(buf);
1492
1493     return EVP_CIPHER_key_length(type);
1494 }
1495
1496 /**
1497  * Add all algorithms to the crypto core.
1498  *
1499  * @ingroup hcrypto_core
1500  */
1501
1502 void
1503 OpenSSL_add_all_algorithms(void)
1504 {
1505     return;
1506 }
1507
1508 /**
1509  * Add all algorithms to the crypto core using configuration file.
1510  *
1511  * @ingroup hcrypto_core
1512  */
1513
1514 void
1515 OpenSSL_add_all_algorithms_conf(void)
1516 {
1517     return;
1518 }
1519
1520 /**
1521  * Add all algorithms to the crypto core, but don't use the
1522  * configuration file.
1523  *
1524  * @ingroup hcrypto_core
1525  */
1526
1527 void
1528 OpenSSL_add_all_algorithms_noconf(void)
1529 {
1530     return;
1531 }