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