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