more doxygen
[heimdal.git] / lib / hcrypto / evp.c
blob0dc55314325c82e3559a8e351a63c491776982b6
1 /*
2 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
38 RCSID("$Id$");
40 #include <sys/types.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <assert.h>
46 #include <evp.h>
48 #include <krb5-types.h>
50 #include <aes.h>
51 #include "camellia.h"
52 #include <des.h>
53 #include <sha.h>
54 #include <rc2.h>
55 #include <rc4.h>
56 #include <md2.h>
57 #include <md4.h>
58 #include <md5.h>
60 /**
61 * @page page_evp EVP - generic crypto interface
63 * See the library functions here: @ref hcrypto_evp
67 typedef int (*evp_md_init)(EVP_MD_CTX *);
68 typedef int (*evp_md_update)(EVP_MD_CTX *,const void *, size_t);
69 typedef int (*evp_md_final)(void *, EVP_MD_CTX *);
70 typedef int (*evp_md_cleanup)(EVP_MD_CTX *);
72 struct hc_evp_md {
73 int hash_size;
74 int block_size;
75 int ctx_size;
76 evp_md_init init;
77 evp_md_update update;
78 evp_md_final final;
79 evp_md_cleanup cleanup;
82 /**
83 * Return the output size of the message digest function.
85 * @param md the evp message
87 * @return size output size of the message digest function.
89 * @ingroup hcrypto_evp
92 size_t
93 EVP_MD_size(const EVP_MD *md)
95 return md->hash_size;
98 /**
99 * Return the blocksize of the message digest function.
101 * @param md the evp message
103 * @return size size of the message digest block size
105 * @ingroup hcrypto_evp
108 size_t
109 EVP_MD_block_size(const EVP_MD *md)
111 return md->block_size;
115 * Allocate a messsage digest context object. Free with
116 * EVP_MD_CTX_destroy().
118 * @return a newly allocated message digest context object.
120 * @ingroup hcrypto_evp
123 EVP_MD_CTX *
124 EVP_MD_CTX_create(void)
126 return calloc(1, sizeof(EVP_MD_CTX));
130 * Initiate a messsage digest context object. Deallocate with
131 * EVP_MD_CTX_cleanup(). Please use EVP_MD_CTX_create() instead.
133 * @param ctx variable to initiate.
135 * @ingroup hcrypto_evp
138 void
139 EVP_MD_CTX_init(EVP_MD_CTX *ctx)
141 memset(ctx, 0, sizeof(*ctx));
145 * Free a messsage digest context object.
147 * @param ctx context to free.
149 * @ingroup hcrypto_evp
152 void
153 EVP_MD_CTX_destroy(EVP_MD_CTX *ctx)
155 EVP_MD_CTX_cleanup(ctx);
156 free(ctx);
160 * Free the resources used by the EVP_MD context.
162 * @param ctx the context to free the resources from.
164 * @return 1 on success.
166 * @ingroup hcrypto_evp
170 EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
172 if (ctx->md && ctx->md->cleanup)
173 (ctx->md->cleanup)(ctx);
174 ctx->md = NULL;
175 ctx->engine = NULL;
176 free(ctx->ptr);
177 memset(ctx, 0, sizeof(*ctx));
178 return 1;
182 * Get the EVP_MD use for a specified context.
184 * @param ctx the EVP_MD context to get the EVP_MD for.
186 * @return the EVP_MD used for the context.
188 * @ingroup hcrypto_evp
191 const EVP_MD *
192 EVP_MD_CTX_md(EVP_MD_CTX *ctx)
194 return ctx->md;
198 * Return the output size of the message digest function.
200 * @param ctx the evp message digest context
202 * @return size output size of the message digest function.
204 * @ingroup hcrypto_evp
207 size_t
208 EVP_MD_CTX_size(EVP_MD_CTX *ctx)
210 return EVP_MD_size(ctx->md);
214 * Return the blocksize of the message digest function.
216 * @param ctx the evp message digest context
218 * @return size size of the message digest block size
220 * @ingroup hcrypto_evp
223 size_t
224 EVP_MD_CTX_block_size(EVP_MD_CTX *ctx)
226 return EVP_MD_block_size(ctx->md);
230 * Init a EVP_MD_CTX for use a specific message digest and engine.
232 * @param ctx the message digest context to init.
233 * @param md the message digest to use.
234 * @param engine the engine to use, NULL to use the default engine.
236 * @return 1 on success.
238 * @ingroup hcrypto_evp
242 EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *md, ENGINE *engine)
244 if (ctx->md != md || ctx->engine != engine) {
245 EVP_MD_CTX_cleanup(ctx);
246 ctx->md = md;
247 ctx->engine = engine;
249 ctx->ptr = calloc(1, md->ctx_size);
250 if (ctx->ptr == NULL)
251 return 0;
253 (ctx->md->init)(ctx->ptr);
254 return 1;
258 EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t size)
260 (ctx->md->update)(ctx->ptr, data, size);
261 return 1;
265 EVP_DigestFinal_ex(EVP_MD_CTX *ctx, void *hash, unsigned int *size)
267 (ctx->md->final)(hash, ctx->ptr);
268 if (size)
269 *size = ctx->md->hash_size;
270 return 1;
274 EVP_Digest(const void *data, size_t dsize, void *hash, unsigned int *hsize,
275 const EVP_MD *md, ENGINE *engine)
277 EVP_MD_CTX *ctx;
278 int ret;
280 ctx = EVP_MD_CTX_create();
281 if (ctx == NULL)
282 return 0;
283 ret = EVP_DigestInit_ex(ctx, md, engine);
284 if (ret != 1)
285 return ret;
286 ret = EVP_DigestUpdate(ctx, data, dsize);
287 if (ret != 1)
288 return ret;
289 ret = EVP_DigestFinal_ex(ctx, hash, hsize);
290 if (ret != 1)
291 return ret;
292 EVP_MD_CTX_destroy(ctx);
293 return 1;
300 const EVP_MD *
301 EVP_sha256(void)
303 static const struct hc_evp_md sha256 = {
306 sizeof(SHA256_CTX),
307 (evp_md_init)SHA256_Init,
308 (evp_md_update)SHA256_Update,
309 (evp_md_final)SHA256_Final,
310 NULL
312 return &sha256;
315 static const struct hc_evp_md sha1 = {
318 sizeof(SHA_CTX),
319 (evp_md_init)SHA1_Init,
320 (evp_md_update)SHA1_Update,
321 (evp_md_final)SHA1_Final,
322 NULL
325 const EVP_MD *
326 EVP_sha1(void)
328 return &sha1;
331 const EVP_MD *
332 EVP_sha(void)
334 return &sha1;
337 const EVP_MD *
338 EVP_md5(void)
340 static const struct hc_evp_md md5 = {
343 sizeof(MD5_CTX),
344 (evp_md_init)MD5_Init,
345 (evp_md_update)MD5_Update,
346 (evp_md_final)MD5_Final,
347 NULL
349 return &md5;
352 const EVP_MD *
353 EVP_md4(void)
355 static const struct hc_evp_md md4 = {
358 sizeof(MD4_CTX),
359 (evp_md_init)MD4_Init,
360 (evp_md_update)MD4_Update,
361 (evp_md_final)MD4_Final,
362 NULL
364 return &md4;
367 const EVP_MD *
368 EVP_md2(void)
370 static const struct hc_evp_md md2 = {
373 sizeof(MD2_CTX),
374 (evp_md_init)MD2_Init,
375 (evp_md_update)MD2_Update,
376 (evp_md_final)MD2_Final,
377 NULL
379 return &md2;
386 static void
387 null_Init (void *m)
390 static void
391 null_Update (void *m, const void * data, size_t size)
394 static void
395 null_Final(void *res, struct md5 *m)
399 const EVP_MD *
400 EVP_md_null(void)
402 static const struct hc_evp_md null = {
406 (evp_md_init)null_Init,
407 (evp_md_update)null_Update,
408 (evp_md_final)null_Final,
409 NULL
411 return &null;
414 #if 0
415 void EVP_MD_CTX_init(EVP_MD_CTX *ctx);
416 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
417 int EVP_DigestFinal(EVP_MD_CTX *ctx,unsigned char *md,unsigned int *s);
418 int EVP_SignFinal(EVP_MD_CTX *, void *, size_t *, EVP_PKEY *);
419 int EVP_VerifyFinal(EVP_MD_CTX *, const void *, size_t, EVP_PKEY *);
420 #endif
426 size_t
427 EVP_CIPHER_block_size(const EVP_CIPHER *c)
429 return c->block_size;
432 size_t
433 EVP_CIPHER_key_length(const EVP_CIPHER *c)
435 return c->key_len;
438 size_t
439 EVP_CIPHER_iv_length(const EVP_CIPHER *c)
441 return c->iv_len;
444 void
445 EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *c)
447 memset(c, 0, sizeof(*c));
451 EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
453 if (c->cipher && c->cipher->cleanup)
454 c->cipher->cleanup(c);
455 if (c->cipher_data) {
456 free(c->cipher_data);
457 c->cipher_data = NULL;
459 return 1;
462 #if 0
464 EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int length)
466 return 0;
470 EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *c, int pad)
472 return 0;
474 #endif
476 const EVP_CIPHER *
477 EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX *ctx)
479 return ctx->cipher;
482 size_t
483 EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
485 return EVP_CIPHER_block_size(ctx->cipher);
488 size_t
489 EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
491 return EVP_CIPHER_key_length(ctx->cipher);
494 size_t
495 EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
497 return EVP_CIPHER_iv_length(ctx->cipher);
500 unsigned long
501 EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx)
503 return ctx->cipher->flags;
507 EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx)
509 return EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_MODE;
512 void *
513 EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX *ctx)
515 return ctx->app_data;
518 void
519 EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
521 ctx->app_data = data;
525 EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *c, ENGINE *engine,
526 const void *key, const void *iv, int encp)
528 if (encp == -1)
529 encp = ctx->encrypt;
530 else
531 ctx->encrypt = (encp ? 1 : 0);
533 if (c && (c != ctx->cipher)) {
534 EVP_CIPHER_CTX_cleanup(ctx);
535 ctx->cipher = c;
536 ctx->key_len = c->key_len;
538 ctx->cipher_data = malloc(c->ctx_size);
539 if (ctx->cipher_data == NULL && c->ctx_size != 0)
540 return 0;
542 } else if (ctx->cipher == NULL) {
543 /* reuse of cipher, but not any cipher ever set! */
544 return 0;
547 switch (EVP_CIPHER_CTX_flags(ctx)) {
548 case EVP_CIPH_CBC_MODE:
550 assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv));
552 if (iv)
553 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
554 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
555 break;
556 default:
557 return 0;
560 if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT))
561 ctx->cipher->init(ctx, key, iv, encp);
563 return 1;
567 EVP_Cipher(EVP_CIPHER_CTX *ctx, void *out, const void *in,size_t size)
569 return ctx->cipher->do_cipher(ctx, out, in, size);
576 static int
577 enc_null_init(EVP_CIPHER_CTX *ctx,
578 const unsigned char * key,
579 const unsigned char * iv,
580 int encp)
582 return 1;
585 static int
586 enc_null_do_cipher(EVP_CIPHER_CTX *ctx,
587 unsigned char *out,
588 const unsigned char *in,
589 unsigned int size)
591 memmove(out, in, size);
592 return 1;
595 static int
596 enc_null_cleanup(EVP_CIPHER_CTX *ctx)
598 return 1;
601 const EVP_CIPHER *
602 EVP_enc_null(void)
604 static const EVP_CIPHER enc_null = {
609 EVP_CIPH_CBC_MODE,
610 enc_null_init,
611 enc_null_do_cipher,
612 enc_null_cleanup,
614 NULL,
615 NULL,
616 NULL,
617 NULL
619 return &enc_null;
626 struct rc2_cbc {
627 unsigned int maximum_effective_key;
628 RC2_KEY key;
631 static int
632 rc2_init(EVP_CIPHER_CTX *ctx,
633 const unsigned char * key,
634 const unsigned char * iv,
635 int encp)
637 struct rc2_cbc *k = ctx->cipher_data;
638 k->maximum_effective_key = EVP_CIPHER_CTX_key_length(ctx) * 8;
639 RC2_set_key(&k->key,
640 EVP_CIPHER_CTX_key_length(ctx),
641 key,
642 k->maximum_effective_key);
643 return 1;
646 static int
647 rc2_do_cipher(EVP_CIPHER_CTX *ctx,
648 unsigned char *out,
649 const unsigned char *in,
650 unsigned int size)
652 struct rc2_cbc *k = ctx->cipher_data;
653 RC2_cbc_encrypt(in, out, size, &k->key, ctx->iv, ctx->encrypt);
654 return 1;
657 static int
658 rc2_cleanup(EVP_CIPHER_CTX *ctx)
660 memset(ctx->cipher_data, 0, sizeof(struct rc2_cbc));
661 return 1;
665 const EVP_CIPHER *
666 EVP_rc2_cbc(void)
668 static const EVP_CIPHER rc2_cbc = {
670 RC2_BLOCK_SIZE,
671 RC2_KEY_LENGTH,
672 RC2_BLOCK_SIZE,
673 EVP_CIPH_CBC_MODE,
674 rc2_init,
675 rc2_do_cipher,
676 rc2_cleanup,
677 sizeof(struct rc2_cbc),
678 NULL,
679 NULL,
680 NULL,
681 NULL
683 return &rc2_cbc;
686 const EVP_CIPHER *
687 EVP_rc2_40_cbc(void)
689 static const EVP_CIPHER rc2_40_cbc = {
691 RC2_BLOCK_SIZE,
693 RC2_BLOCK_SIZE,
694 EVP_CIPH_CBC_MODE,
695 rc2_init,
696 rc2_do_cipher,
697 rc2_cleanup,
698 sizeof(struct rc2_cbc),
699 NULL,
700 NULL,
701 NULL,
702 NULL
704 return &rc2_40_cbc;
707 const EVP_CIPHER *
708 EVP_rc2_64_cbc(void)
710 static const EVP_CIPHER rc2_64_cbc = {
712 RC2_BLOCK_SIZE,
714 RC2_BLOCK_SIZE,
715 EVP_CIPH_CBC_MODE,
716 rc2_init,
717 rc2_do_cipher,
718 rc2_cleanup,
719 sizeof(struct rc2_cbc),
720 NULL,
721 NULL,
722 NULL,
723 NULL
725 return &rc2_64_cbc;
732 const EVP_CIPHER *
733 EVP_rc4(void)
735 printf("evp rc4\n");
736 abort();
737 return NULL;
740 const EVP_CIPHER *
741 EVP_rc4_40(void)
743 printf("evp rc4_40\n");
744 abort();
745 return NULL;
752 struct des_ede3_cbc {
753 DES_key_schedule ks[3];
756 static int
757 des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
758 const unsigned char * key,
759 const unsigned char * iv,
760 int encp)
762 struct des_ede3_cbc *k = ctx->cipher_data;
764 DES_key_sched((DES_cblock *)(key), &k->ks[0]);
765 DES_key_sched((DES_cblock *)(key + 8), &k->ks[1]);
766 DES_key_sched((DES_cblock *)(key + 16), &k->ks[2]);
768 return 1;
771 static int
772 des_ede3_cbc_do_cipher(EVP_CIPHER_CTX *ctx,
773 unsigned char *out,
774 const unsigned char *in,
775 unsigned int size)
777 struct des_ede3_cbc *k = ctx->cipher_data;
778 DES_ede3_cbc_encrypt(in, out, size,
779 &k->ks[0], &k->ks[1], &k->ks[2],
780 (DES_cblock *)ctx->iv, ctx->encrypt);
781 return 1;
784 static int
785 des_ede3_cbc_cleanup(EVP_CIPHER_CTX *ctx)
787 memset(ctx->cipher_data, 0, sizeof(struct des_ede3_cbc));
788 return 1;
791 const EVP_CIPHER *
792 EVP_des_ede3_cbc(void)
794 static const EVP_CIPHER des_ede3_cbc = {
799 EVP_CIPH_CBC_MODE,
800 des_ede3_cbc_init,
801 des_ede3_cbc_do_cipher,
802 des_ede3_cbc_cleanup,
803 sizeof(struct des_ede3_cbc),
804 NULL,
805 NULL,
806 NULL,
807 NULL
809 return &des_ede3_cbc;
816 static int
817 aes_init(EVP_CIPHER_CTX *ctx,
818 const unsigned char * key,
819 const unsigned char * iv,
820 int encp)
822 AES_KEY *k = ctx->cipher_data;
823 if (ctx->encrypt)
824 AES_set_encrypt_key(key, ctx->cipher->key_len * 8, k);
825 else
826 AES_set_decrypt_key(key, ctx->cipher->key_len * 8, k);
827 return 1;
830 static int
831 aes_do_cipher(EVP_CIPHER_CTX *ctx,
832 unsigned char *out,
833 const unsigned char *in,
834 unsigned int size)
836 AES_KEY *k = ctx->cipher_data;
837 AES_cbc_encrypt(in, out, size, k, ctx->iv, ctx->encrypt);
838 return 1;
841 static int
842 aes_cleanup(EVP_CIPHER_CTX *ctx)
844 memset(ctx->cipher_data, 0, sizeof(AES_KEY));
845 return 1;
848 const EVP_CIPHER *
849 EVP_aes_128_cbc(void)
851 static const EVP_CIPHER aes_128_cbc = {
856 EVP_CIPH_CBC_MODE,
857 aes_init,
858 aes_do_cipher,
859 aes_cleanup,
860 sizeof(AES_KEY),
861 NULL,
862 NULL,
863 NULL,
864 NULL
866 return &aes_128_cbc;
869 const EVP_CIPHER *
870 EVP_aes_192_cbc(void)
872 static const EVP_CIPHER aes_192_cbc = {
877 EVP_CIPH_CBC_MODE,
878 aes_init,
879 aes_do_cipher,
880 aes_cleanup,
881 sizeof(AES_KEY),
882 NULL,
883 NULL,
884 NULL,
885 NULL
887 return &aes_192_cbc;
891 const EVP_CIPHER *
892 EVP_aes_256_cbc(void)
894 static const EVP_CIPHER aes_256_cbc = {
899 EVP_CIPH_CBC_MODE,
900 aes_init,
901 aes_do_cipher,
902 aes_cleanup,
903 sizeof(AES_KEY),
904 NULL,
905 NULL,
906 NULL,
907 NULL
909 return &aes_256_cbc;
912 static int
913 camellia_init(EVP_CIPHER_CTX *ctx,
914 const unsigned char * key,
915 const unsigned char * iv,
916 int encp)
918 CAMELLIA_KEY *k = ctx->cipher_data;
919 k->bits = ctx->cipher->key_len * 8;
920 CAMELLIA_set_key(key, ctx->cipher->key_len * 8, k);
921 return 1;
924 static int
925 camellia_do_cipher(EVP_CIPHER_CTX *ctx,
926 unsigned char *out,
927 const unsigned char *in,
928 unsigned int size)
930 CAMELLIA_KEY *k = ctx->cipher_data;
931 CAMELLIA_cbc_encrypt(in, out, size, k, ctx->iv, ctx->encrypt);
932 return 1;
935 static int
936 camellia_cleanup(EVP_CIPHER_CTX *ctx)
938 memset(ctx->cipher_data, 0, sizeof(CAMELLIA_KEY));
939 return 1;
942 const EVP_CIPHER *
943 EVP_camellia_128_cbc(void)
945 static const EVP_CIPHER cipher = {
950 EVP_CIPH_CBC_MODE,
951 camellia_init,
952 camellia_do_cipher,
953 camellia_cleanup,
954 sizeof(CAMELLIA_KEY),
955 NULL,
956 NULL,
957 NULL,
958 NULL
960 return &cipher;
963 const EVP_CIPHER *
964 EVP_camellia_192_cbc(void)
966 static const EVP_CIPHER cipher = {
971 EVP_CIPH_CBC_MODE,
972 camellia_init,
973 camellia_do_cipher,
974 camellia_cleanup,
975 sizeof(CAMELLIA_KEY),
976 NULL,
977 NULL,
978 NULL,
979 NULL
981 return &cipher;
984 const EVP_CIPHER *
985 EVP_camellia_256_cbc(void)
987 static const EVP_CIPHER cipher = {
992 EVP_CIPH_CBC_MODE,
993 camellia_init,
994 camellia_do_cipher,
995 camellia_cleanup,
996 sizeof(CAMELLIA_KEY),
997 NULL,
998 NULL,
999 NULL,
1000 NULL
1002 return &cipher;
1009 static const struct cipher_name {
1010 const char *name;
1011 const EVP_CIPHER *(*func)(void);
1012 } cipher_name[] = {
1013 { "des-ede3-cbc", EVP_des_ede3_cbc },
1014 { "aes-128-cbc", EVP_aes_128_cbc },
1015 { "aes-192-cbc", EVP_aes_192_cbc },
1016 { "aes-256-cbc", EVP_aes_256_cbc },
1017 { "camellia-128-cbc", EVP_camellia_128_cbc },
1018 { "camellia-192-cbc", EVP_camellia_192_cbc },
1019 { "camellia-256-cbc", EVP_camellia_256_cbc }
1023 const EVP_CIPHER *
1024 EVP_get_cipherbyname(const char *name)
1026 int i;
1027 for (i = 0; i < sizeof(cipher_name)/sizeof(cipher_name[0]); i++) {
1028 if (strcasecmp(cipher_name[i].name, name) == 0)
1029 return (*cipher_name[i].func)();
1031 return NULL;
1039 #ifndef min
1040 #define min(a,b) (((a)>(b))?(b):(a))
1041 #endif
1044 EVP_BytesToKey(const EVP_CIPHER *type,
1045 const EVP_MD *md,
1046 const void *salt,
1047 const void *data, size_t datalen,
1048 unsigned int count,
1049 void *keydata,
1050 void *ivdata)
1052 int ivlen, keylen, first = 0;
1053 unsigned int mds = 0, i;
1054 unsigned char *key = keydata;
1055 unsigned char *iv = ivdata;
1056 unsigned char *buf;
1057 EVP_MD_CTX c;
1059 keylen = EVP_CIPHER_key_length(type);
1060 ivlen = EVP_CIPHER_iv_length(type);
1062 if (data == NULL)
1063 return keylen;
1065 buf = malloc(EVP_MD_size(md));
1066 if (buf == NULL)
1067 return -1;
1069 EVP_MD_CTX_init(&c);
1071 first = 1;
1072 while (1) {
1073 EVP_DigestInit_ex(&c, md, NULL);
1074 if (!first)
1075 EVP_DigestUpdate(&c, buf, mds);
1076 first = 0;
1077 EVP_DigestUpdate(&c,data,datalen);
1079 #define PKCS5_SALT_LEN 8
1081 if (salt)
1082 EVP_DigestUpdate(&c, salt, PKCS5_SALT_LEN);
1084 EVP_DigestFinal_ex(&c, buf, &mds);
1085 assert(mds == EVP_MD_size(md));
1087 for (i = 1; i < count; i++) {
1088 EVP_DigestInit_ex(&c, md, NULL);
1089 EVP_DigestUpdate(&c, buf, mds);
1090 EVP_DigestFinal_ex(&c, buf, &mds);
1091 assert(mds == EVP_MD_size(md));
1094 i = 0;
1095 if (keylen) {
1096 size_t sz = min(keylen, mds);
1097 if (key) {
1098 memcpy(key, buf, sz);
1099 key += sz;
1101 keylen -= sz;
1102 i += sz;
1104 if (ivlen && mds > i) {
1105 size_t sz = min(ivlen, (mds - i));
1106 if (iv) {
1107 memcpy(iv, &buf[i], sz);
1108 iv += sz;
1110 ivlen -= sz;
1112 if (keylen == 0 && ivlen == 0)
1113 break;
1116 EVP_MD_CTX_cleanup(&c);
1117 free(buf);
1119 return EVP_CIPHER_key_length(type);
1126 void
1127 OpenSSL_add_all_algorithms(void)
1129 return;
1132 void
1133 OpenSSL_add_all_algorithms_conf(void)
1135 return;
1138 void
1139 OpenSSL_add_all_algorithms_noconf(void)
1141 return;