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