No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / hcrypto / evp.c
blob79d79df4282624f1637de0260faf6197ce982803
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("$Heimdal: evp.c 22379 2007-12-29 11:13:26Z lha $"
39 "$NetBSD$");
41 #include <sys/types.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <assert.h>
47 #include <evp.h>
49 #include <krb5-types.h>
51 #include <aes.h>
52 #include "camellia.h"
53 #include <des.h>
54 #include <sha.h>
55 #include <rc2.h>
56 #include <rc4.h>
57 #include <md2.h>
58 #include <md4.h>
59 #include <md5.h>
61 /**
62 * @page page_evp EVP - generic crypto interface
64 * See the library functions here: @ref hcrypto_evp
68 typedef int (*evp_md_init)(EVP_MD_CTX *);
69 typedef int (*evp_md_update)(EVP_MD_CTX *,const void *, size_t);
70 typedef int (*evp_md_final)(void *, EVP_MD_CTX *);
71 typedef int (*evp_md_cleanup)(EVP_MD_CTX *);
73 struct hc_evp_md {
74 int hash_size;
75 int block_size;
76 int ctx_size;
77 evp_md_init init;
78 evp_md_update update;
79 evp_md_final final;
80 evp_md_cleanup cleanup;
83 /**
84 * Return the output size of the message digest function.
86 * @param md the evp message
88 * @return size output size of the message digest function.
90 * @ingroup hcrypto_evp
93 size_t
94 EVP_MD_size(const EVP_MD *md)
96 return md->hash_size;
99 /**
100 * Return the blocksize of the message digest function.
102 * @param md the evp message
104 * @return size size of the message digest block size
106 * @ingroup hcrypto_evp
109 size_t
110 EVP_MD_block_size(const EVP_MD *md)
112 return md->block_size;
116 * Allocate a messsage digest context object. Free with
117 * EVP_MD_CTX_destroy().
119 * @return a newly allocated message digest context object.
121 * @ingroup hcrypto_evp
124 EVP_MD_CTX *
125 EVP_MD_CTX_create(void)
127 return calloc(1, sizeof(EVP_MD_CTX));
131 * Initiate a messsage digest context object. Deallocate with
132 * EVP_MD_CTX_cleanup(). Please use EVP_MD_CTX_create() instead.
134 * @param ctx variable to initiate.
136 * @ingroup hcrypto_evp
139 void
140 EVP_MD_CTX_init(EVP_MD_CTX *ctx)
142 memset(ctx, 0, sizeof(*ctx));
146 * Free a messsage digest context object.
148 * @param ctx context to free.
150 * @ingroup hcrypto_evp
153 void
154 EVP_MD_CTX_destroy(EVP_MD_CTX *ctx)
156 EVP_MD_CTX_cleanup(ctx);
157 free(ctx);
161 * Free the resources used by the EVP_MD context.
163 * @param ctx the context to free the resources from.
165 * @return 1 on success.
167 * @ingroup hcrypto_evp
171 EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
173 if (ctx->md && ctx->md->cleanup)
174 (ctx->md->cleanup)(ctx);
175 ctx->md = NULL;
176 ctx->engine = NULL;
177 free(ctx->ptr);
178 memset(ctx, 0, sizeof(*ctx));
179 return 1;
183 * Get the EVP_MD use for a specified context.
185 * @param ctx the EVP_MD context to get the EVP_MD for.
187 * @return the EVP_MD used for the context.
189 * @ingroup hcrypto_evp
192 const EVP_MD *
193 EVP_MD_CTX_md(EVP_MD_CTX *ctx)
195 return ctx->md;
199 * Return the output size of the message digest function.
201 * @param ctx the evp message digest context
203 * @return size output size of the message digest function.
205 * @ingroup hcrypto_evp
208 size_t
209 EVP_MD_CTX_size(EVP_MD_CTX *ctx)
211 return EVP_MD_size(ctx->md);
215 * Return the blocksize of the message digest function.
217 * @param ctx the evp message digest context
219 * @return size size of the message digest block size
221 * @ingroup hcrypto_evp
224 size_t
225 EVP_MD_CTX_block_size(EVP_MD_CTX *ctx)
227 return EVP_MD_block_size(ctx->md);
231 * Init a EVP_MD_CTX for use a specific message digest and engine.
233 * @param ctx the message digest context to init.
234 * @param md the message digest to use.
235 * @param engine the engine to use, NULL to use the default engine.
237 * @return 1 on success.
239 * @ingroup hcrypto_evp
243 EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *md, ENGINE *engine)
245 if (ctx->md != md || ctx->engine != engine) {
246 EVP_MD_CTX_cleanup(ctx);
247 ctx->md = md;
248 ctx->engine = engine;
250 ctx->ptr = calloc(1, md->ctx_size);
251 if (ctx->ptr == NULL)
252 return 0;
254 (ctx->md->init)(ctx->ptr);
255 return 1;
259 * Update the digest with some data.
261 * @param ctx the context to update
262 * @param data the data to update the context with
263 * @param size length of data
265 * @return 1 on success.
267 * @ingroup hcrypto_evp
271 EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t size)
273 (ctx->md->update)(ctx->ptr, data, size);
274 return 1;
278 * Complete the message digest.
280 * @param ctx the context to complete.
281 * @param hash the output of the message digest function. At least
282 * EVP_MD_size().
283 * @param size the output size of hash.
285 * @return 1 on success.
287 * @ingroup hcrypto_evp
291 EVP_DigestFinal_ex(EVP_MD_CTX *ctx, void *hash, unsigned int *size)
293 (ctx->md->final)(hash, ctx->ptr);
294 if (size)
295 *size = ctx->md->hash_size;
296 return 1;
300 * Do the whole EVP_MD_CTX_create(), EVP_DigestInit_ex(),
301 * EVP_DigestUpdate(), EVP_DigestFinal_ex(), EVP_MD_CTX_destroy()
302 * dance in one call.
304 * @param data the data to update the context with
305 * @param dsize length of data
306 * @param hash output data of at least EVP_MD_size() length.
307 * @param hsize output length of hash.
308 * @param md message digest to use
309 * @param engine engine to use, NULL for default engine.
311 * @return 1 on success.
313 * @ingroup hcrypto_evp
317 EVP_Digest(const void *data, size_t dsize, void *hash, unsigned int *hsize,
318 const EVP_MD *md, ENGINE *engine)
320 EVP_MD_CTX *ctx;
321 int ret;
323 ctx = EVP_MD_CTX_create();
324 if (ctx == NULL)
325 return 0;
326 ret = EVP_DigestInit_ex(ctx, md, engine);
327 if (ret != 1) {
328 EVP_MD_CTX_destroy(ctx);
329 return ret;
331 ret = EVP_DigestUpdate(ctx, data, dsize);
332 if (ret != 1) {
333 EVP_MD_CTX_destroy(ctx);
334 return ret;
336 ret = EVP_DigestFinal_ex(ctx, hash, hsize);
337 EVP_MD_CTX_destroy(ctx);
338 return ret;
342 * The message digest SHA256
344 * @return the message digest type.
346 * @ingroup hcrypto_evp
349 const EVP_MD *
350 EVP_sha256(void)
352 static const struct hc_evp_md sha256 = {
355 sizeof(SHA256_CTX),
356 (evp_md_init)SHA256_Init,
357 (evp_md_update)SHA256_Update,
358 (evp_md_final)SHA256_Final,
359 NULL
361 return &sha256;
364 static const struct hc_evp_md sha1 = {
367 sizeof(SHA_CTX),
368 (evp_md_init)SHA1_Init,
369 (evp_md_update)SHA1_Update,
370 (evp_md_final)SHA1_Final,
371 NULL
375 * The message digest SHA1
377 * @return the message digest type.
379 * @ingroup hcrypto_evp
382 const EVP_MD *
383 EVP_sha1(void)
385 return &sha1;
389 * The message digest SHA1
391 * @return the message digest type.
393 * @ingroup hcrypto_evp
396 const EVP_MD *
397 EVP_sha(void)
399 return &sha1;
403 * The message digest MD5
405 * @return the message digest type.
407 * @ingroup hcrypto_evp
410 const EVP_MD *
411 EVP_md5(void)
413 static const struct hc_evp_md md5 = {
416 sizeof(MD5_CTX),
417 (evp_md_init)MD5_Init,
418 (evp_md_update)MD5_Update,
419 (evp_md_final)MD5_Final,
420 NULL
422 return &md5;
426 * The message digest MD4
428 * @return the message digest type.
430 * @ingroup hcrypto_evp
433 const EVP_MD *
434 EVP_md4(void)
436 static const struct hc_evp_md md4 = {
439 sizeof(MD4_CTX),
440 (evp_md_init)MD4_Init,
441 (evp_md_update)MD4_Update,
442 (evp_md_final)MD4_Final,
443 NULL
445 return &md4;
449 * The message digest MD2
451 * @return the message digest type.
453 * @ingroup hcrypto_evp
456 const EVP_MD *
457 EVP_md2(void)
459 static const struct hc_evp_md md2 = {
462 sizeof(MD2_CTX),
463 (evp_md_init)MD2_Init,
464 (evp_md_update)MD2_Update,
465 (evp_md_final)MD2_Final,
466 NULL
468 return &md2;
475 static void
476 null_Init (void *m)
479 static void
480 null_Update (void *m, const void * data, size_t size)
483 static void
484 null_Final(void *res, void *m)
489 * The null message digest
491 * @return the message digest type.
493 * @ingroup hcrypto_evp
496 const EVP_MD *
497 EVP_md_null(void)
499 static const struct hc_evp_md null = {
503 (evp_md_init)null_Init,
504 (evp_md_update)null_Update,
505 (evp_md_final)null_Final,
506 NULL
508 return &null;
511 #if 0
512 void EVP_MD_CTX_init(EVP_MD_CTX *ctx);
513 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
514 int EVP_DigestFinal(EVP_MD_CTX *ctx,unsigned char *md,unsigned int *s);
515 int EVP_SignFinal(EVP_MD_CTX *, void *, size_t *, EVP_PKEY *);
516 int EVP_VerifyFinal(EVP_MD_CTX *, const void *, size_t, EVP_PKEY *);
517 #endif
520 * Return the block size of the cipher.
522 * @param c cipher to get the block size from.
524 * @return the block size of the cipher.
526 * @ingroup hcrypto_evp
529 size_t
530 EVP_CIPHER_block_size(const EVP_CIPHER *c)
532 return c->block_size;
536 * Return the key size of the cipher.
538 * @param c cipher to get the key size from.
540 * @return the key size of the cipher.
542 * @ingroup hcrypto_evp
545 size_t
546 EVP_CIPHER_key_length(const EVP_CIPHER *c)
548 return c->key_len;
552 * Return the IV size of the cipher.
554 * @param c cipher to get the IV size from.
556 * @return the IV size of the cipher.
558 * @ingroup hcrypto_evp
561 size_t
562 EVP_CIPHER_iv_length(const EVP_CIPHER *c)
564 return c->iv_len;
568 * Initiate a EVP_CIPHER_CTX context. Clean up with
569 * EVP_CIPHER_CTX_cleanup().
571 * @param c the cipher initiate.
573 * @ingroup hcrypto_evp
576 void
577 EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *c)
579 memset(c, 0, sizeof(*c));
583 * Clean up the EVP_CIPHER_CTX context.
585 * @param c the cipher to clean up.
587 * @return 1 on success.
589 * @ingroup hcrypto_evp
593 EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
595 if (c->cipher && c->cipher->cleanup)
596 c->cipher->cleanup(c);
597 if (c->cipher_data) {
598 free(c->cipher_data);
599 c->cipher_data = NULL;
601 return 1;
604 #if 0
606 EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int length)
608 return 0;
612 EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *c, int pad)
614 return 0;
616 #endif
619 * Return the EVP_CIPHER for a EVP_CIPHER_CTX context.
621 * @param ctx the context to get the cipher type from.
623 * @return the EVP_CIPHER pointer.
625 * @ingroup hcrypto_evp
628 const EVP_CIPHER *
629 EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX *ctx)
631 return ctx->cipher;
635 * Return the block size of the cipher context.
637 * @param ctx cipher context to get the block size from.
639 * @return the block size of the cipher context.
641 * @ingroup hcrypto_evp
644 size_t
645 EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
647 return EVP_CIPHER_block_size(ctx->cipher);
651 * Return the key size of the cipher context.
653 * @param ctx cipher context to get the key size from.
655 * @return the key size of the cipher context.
657 * @ingroup hcrypto_evp
660 size_t
661 EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
663 return EVP_CIPHER_key_length(ctx->cipher);
667 * Return the IV size of the cipher context.
669 * @param ctx cipher context to get the IV size from.
671 * @return the IV size of the cipher context.
673 * @ingroup hcrypto_evp
676 size_t
677 EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
679 return EVP_CIPHER_iv_length(ctx->cipher);
683 * Get the flags for an EVP_CIPHER_CTX context.
685 * @param ctx the EVP_CIPHER_CTX to get the flags from
687 * @return the flags for an EVP_CIPHER_CTX.
689 * @ingroup hcrypto_evp
692 unsigned long
693 EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx)
695 return ctx->cipher->flags;
699 * Get the mode for an EVP_CIPHER_CTX context.
701 * @param ctx the EVP_CIPHER_CTX to get the mode from
703 * @return the mode for an EVP_CIPHER_CTX.
705 * @ingroup hcrypto_evp
709 EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx)
711 return EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_MODE;
715 * Get the app data for an EVP_CIPHER_CTX context.
717 * @param ctx the EVP_CIPHER_CTX to get the app data from
719 * @return the app data for an EVP_CIPHER_CTX.
721 * @ingroup hcrypto_evp
724 void *
725 EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX *ctx)
727 return ctx->app_data;
731 * Set the app data for an EVP_CIPHER_CTX context.
733 * @param ctx the EVP_CIPHER_CTX to set the app data for
734 * @param data the app data to set for an EVP_CIPHER_CTX.
736 * @ingroup hcrypto_evp
739 void
740 EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
742 ctx->app_data = data;
746 * Initiate the EVP_CIPHER_CTX context to encrypt or decrypt data.
747 * Clean up with EVP_CIPHER_CTX_cleanup().
749 * @param ctx context to initiate
750 * @param c cipher to use.
751 * @param engine crypto engine to use, NULL to select default.
752 * @param key the crypto key to use, NULL will use the previous value.
753 * @param iv the IV to use, NULL will use the previous value.
754 * @param encp non zero will encrypt, -1 use the previous value.
756 * @return 1 on success.
758 * @ingroup hcrypto_evp
762 EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *c, ENGINE *engine,
763 const void *key, const void *iv, int encp)
765 if (encp == -1)
766 encp = ctx->encrypt;
767 else
768 ctx->encrypt = (encp ? 1 : 0);
770 if (c && (c != ctx->cipher)) {
771 EVP_CIPHER_CTX_cleanup(ctx);
772 ctx->cipher = c;
773 ctx->key_len = c->key_len;
775 ctx->cipher_data = malloc(c->ctx_size);
776 if (ctx->cipher_data == NULL && c->ctx_size != 0)
777 return 0;
779 } else if (ctx->cipher == NULL) {
780 /* reuse of cipher, but not any cipher ever set! */
781 return 0;
784 switch (EVP_CIPHER_CTX_flags(ctx)) {
785 case EVP_CIPH_CBC_MODE:
787 assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv));
789 if (iv)
790 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
791 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
792 break;
793 default:
794 return 0;
797 if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT))
798 ctx->cipher->init(ctx, key, iv, encp);
800 return 1;
804 * Encypher/decypher data
806 * @param ctx the cipher context.
807 * @param out out data from the operation.
808 * @param in in data to the operation.
809 * @param size length of data.
811 * @return 1 on success.
815 EVP_Cipher(EVP_CIPHER_CTX *ctx, void *out, const void *in,size_t size)
817 return ctx->cipher->do_cipher(ctx, out, in, size);
824 static int
825 enc_null_init(EVP_CIPHER_CTX *ctx,
826 const unsigned char * key,
827 const unsigned char * iv,
828 int encp)
830 return 1;
833 static int
834 enc_null_do_cipher(EVP_CIPHER_CTX *ctx,
835 unsigned char *out,
836 const unsigned char *in,
837 unsigned int size)
839 memmove(out, in, size);
840 return 1;
843 static int
844 enc_null_cleanup(EVP_CIPHER_CTX *ctx)
846 return 1;
850 * The NULL cipher type, does no encryption/decryption.
852 * @return the null EVP_CIPHER pointer.
854 * @ingroup hcrypto_evp
857 const EVP_CIPHER *
858 EVP_enc_null(void)
860 static const EVP_CIPHER enc_null = {
865 EVP_CIPH_CBC_MODE,
866 enc_null_init,
867 enc_null_do_cipher,
868 enc_null_cleanup,
870 NULL,
871 NULL,
872 NULL,
873 NULL
875 return &enc_null;
882 struct rc2_cbc {
883 unsigned int maximum_effective_key;
884 RC2_KEY key;
887 static int
888 rc2_init(EVP_CIPHER_CTX *ctx,
889 const unsigned char * key,
890 const unsigned char * iv,
891 int encp)
893 struct rc2_cbc *k = ctx->cipher_data;
894 k->maximum_effective_key = EVP_CIPHER_CTX_key_length(ctx) * 8;
895 RC2_set_key(&k->key,
896 EVP_CIPHER_CTX_key_length(ctx),
897 key,
898 k->maximum_effective_key);
899 return 1;
902 static int
903 rc2_do_cipher(EVP_CIPHER_CTX *ctx,
904 unsigned char *out,
905 const unsigned char *in,
906 unsigned int size)
908 struct rc2_cbc *k = ctx->cipher_data;
909 RC2_cbc_encrypt(in, out, size, &k->key, ctx->iv, ctx->encrypt);
910 return 1;
913 static int
914 rc2_cleanup(EVP_CIPHER_CTX *ctx)
916 memset(ctx->cipher_data, 0, sizeof(struct rc2_cbc));
917 return 1;
921 * The RC2 cipher type
923 * @return the RC2 EVP_CIPHER pointer.
925 * @ingroup hcrypto_evp
928 const EVP_CIPHER *
929 EVP_rc2_cbc(void)
931 static const EVP_CIPHER rc2_cbc = {
933 RC2_BLOCK_SIZE,
934 RC2_KEY_LENGTH,
935 RC2_BLOCK_SIZE,
936 EVP_CIPH_CBC_MODE,
937 rc2_init,
938 rc2_do_cipher,
939 rc2_cleanup,
940 sizeof(struct rc2_cbc),
941 NULL,
942 NULL,
943 NULL,
944 NULL
946 return &rc2_cbc;
950 * The RC2-40 cipher type
952 * @return the RC2-40 EVP_CIPHER pointer.
954 * @ingroup hcrypto_evp
957 const EVP_CIPHER *
958 EVP_rc2_40_cbc(void)
960 static const EVP_CIPHER rc2_40_cbc = {
962 RC2_BLOCK_SIZE,
964 RC2_BLOCK_SIZE,
965 EVP_CIPH_CBC_MODE,
966 rc2_init,
967 rc2_do_cipher,
968 rc2_cleanup,
969 sizeof(struct rc2_cbc),
970 NULL,
971 NULL,
972 NULL,
973 NULL
975 return &rc2_40_cbc;
979 * The RC2-64 cipher type
981 * @return the RC2-64 EVP_CIPHER pointer.
983 * @ingroup hcrypto_evp
986 const EVP_CIPHER *
987 EVP_rc2_64_cbc(void)
989 static const EVP_CIPHER rc2_64_cbc = {
991 RC2_BLOCK_SIZE,
993 RC2_BLOCK_SIZE,
994 EVP_CIPH_CBC_MODE,
995 rc2_init,
996 rc2_do_cipher,
997 rc2_cleanup,
998 sizeof(struct rc2_cbc),
999 NULL,
1000 NULL,
1001 NULL,
1002 NULL
1004 return &rc2_64_cbc;
1008 * The RC4 cipher type
1010 * @return the RC4 EVP_CIPHER pointer.
1012 * @ingroup hcrypto_evp
1015 const EVP_CIPHER *
1016 EVP_rc4(void)
1018 printf("evp rc4\n");
1019 abort();
1020 return NULL;
1024 * The RC4-40 cipher type
1026 * @return the RC4-40 EVP_CIPHER pointer.
1028 * @ingroup hcrypto_evp
1031 const EVP_CIPHER *
1032 EVP_rc4_40(void)
1034 printf("evp rc4_40\n");
1035 abort();
1036 return NULL;
1043 struct des_ede3_cbc {
1044 DES_key_schedule ks[3];
1047 static int
1048 des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
1049 const unsigned char * key,
1050 const unsigned char * iv,
1051 int encp)
1053 struct des_ede3_cbc *k = ctx->cipher_data;
1055 DES_key_sched((DES_cblock *)(key), &k->ks[0]);
1056 DES_key_sched((DES_cblock *)(key + 8), &k->ks[1]);
1057 DES_key_sched((DES_cblock *)(key + 16), &k->ks[2]);
1059 return 1;
1062 static int
1063 des_ede3_cbc_do_cipher(EVP_CIPHER_CTX *ctx,
1064 unsigned char *out,
1065 const unsigned char *in,
1066 unsigned int size)
1068 struct des_ede3_cbc *k = ctx->cipher_data;
1069 DES_ede3_cbc_encrypt(in, out, size,
1070 &k->ks[0], &k->ks[1], &k->ks[2],
1071 (DES_cblock *)ctx->iv, ctx->encrypt);
1072 return 1;
1075 static int
1076 des_ede3_cbc_cleanup(EVP_CIPHER_CTX *ctx)
1078 memset(ctx->cipher_data, 0, sizeof(struct des_ede3_cbc));
1079 return 1;
1083 * The tripple DES cipher type
1085 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
1087 * @ingroup hcrypto_evp
1090 const EVP_CIPHER *
1091 EVP_des_ede3_cbc(void)
1093 static const EVP_CIPHER des_ede3_cbc = {
1098 EVP_CIPH_CBC_MODE,
1099 des_ede3_cbc_init,
1100 des_ede3_cbc_do_cipher,
1101 des_ede3_cbc_cleanup,
1102 sizeof(struct des_ede3_cbc),
1103 NULL,
1104 NULL,
1105 NULL,
1106 NULL
1108 return &des_ede3_cbc;
1115 static int
1116 aes_init(EVP_CIPHER_CTX *ctx,
1117 const unsigned char * key,
1118 const unsigned char * iv,
1119 int encp)
1121 AES_KEY *k = ctx->cipher_data;
1122 if (ctx->encrypt)
1123 AES_set_encrypt_key(key, ctx->cipher->key_len * 8, k);
1124 else
1125 AES_set_decrypt_key(key, ctx->cipher->key_len * 8, k);
1126 return 1;
1129 static int
1130 aes_do_cipher(EVP_CIPHER_CTX *ctx,
1131 unsigned char *out,
1132 const unsigned char *in,
1133 unsigned int size)
1135 AES_KEY *k = ctx->cipher_data;
1136 AES_cbc_encrypt(in, out, size, k, ctx->iv, ctx->encrypt);
1137 return 1;
1140 static int
1141 aes_cleanup(EVP_CIPHER_CTX *ctx)
1143 memset(ctx->cipher_data, 0, sizeof(AES_KEY));
1144 return 1;
1148 * The AES-128 cipher type
1150 * @return the AES-128 EVP_CIPHER pointer.
1152 * @ingroup hcrypto_evp
1155 const EVP_CIPHER *
1156 EVP_aes_128_cbc(void)
1158 static const EVP_CIPHER aes_128_cbc = {
1163 EVP_CIPH_CBC_MODE,
1164 aes_init,
1165 aes_do_cipher,
1166 aes_cleanup,
1167 sizeof(AES_KEY),
1168 NULL,
1169 NULL,
1170 NULL,
1171 NULL
1173 return &aes_128_cbc;
1177 * The AES-192 cipher type
1179 * @return the AES-192 EVP_CIPHER pointer.
1181 * @ingroup hcrypto_evp
1184 const EVP_CIPHER *
1185 EVP_aes_192_cbc(void)
1187 static const EVP_CIPHER aes_192_cbc = {
1192 EVP_CIPH_CBC_MODE,
1193 aes_init,
1194 aes_do_cipher,
1195 aes_cleanup,
1196 sizeof(AES_KEY),
1197 NULL,
1198 NULL,
1199 NULL,
1200 NULL
1202 return &aes_192_cbc;
1206 * The AES-256 cipher type
1208 * @return the AES-256 EVP_CIPHER pointer.
1210 * @ingroup hcrypto_evp
1213 const EVP_CIPHER *
1214 EVP_aes_256_cbc(void)
1216 static const EVP_CIPHER aes_256_cbc = {
1221 EVP_CIPH_CBC_MODE,
1222 aes_init,
1223 aes_do_cipher,
1224 aes_cleanup,
1225 sizeof(AES_KEY),
1226 NULL,
1227 NULL,
1228 NULL,
1229 NULL
1231 return &aes_256_cbc;
1234 static int
1235 camellia_init(EVP_CIPHER_CTX *ctx,
1236 const unsigned char * key,
1237 const unsigned char * iv,
1238 int encp)
1240 CAMELLIA_KEY *k = ctx->cipher_data;
1241 k->bits = ctx->cipher->key_len * 8;
1242 CAMELLIA_set_key(key, ctx->cipher->key_len * 8, k);
1243 return 1;
1246 static int
1247 camellia_do_cipher(EVP_CIPHER_CTX *ctx,
1248 unsigned char *out,
1249 const unsigned char *in,
1250 unsigned int size)
1252 CAMELLIA_KEY *k = ctx->cipher_data;
1253 CAMELLIA_cbc_encrypt(in, out, size, k, ctx->iv, ctx->encrypt);
1254 return 1;
1257 static int
1258 camellia_cleanup(EVP_CIPHER_CTX *ctx)
1260 memset(ctx->cipher_data, 0, sizeof(CAMELLIA_KEY));
1261 return 1;
1265 * The Camellia-128 cipher type
1267 * @return the Camellia-128 EVP_CIPHER pointer.
1269 * @ingroup hcrypto_evp
1272 const EVP_CIPHER *
1273 EVP_camellia_128_cbc(void)
1275 static const EVP_CIPHER cipher = {
1280 EVP_CIPH_CBC_MODE,
1281 camellia_init,
1282 camellia_do_cipher,
1283 camellia_cleanup,
1284 sizeof(CAMELLIA_KEY),
1285 NULL,
1286 NULL,
1287 NULL,
1288 NULL
1290 return &cipher;
1294 * The Camellia-198 cipher type
1296 * @return the Camellia-198 EVP_CIPHER pointer.
1298 * @ingroup hcrypto_evp
1301 const EVP_CIPHER *
1302 EVP_camellia_192_cbc(void)
1304 static const EVP_CIPHER cipher = {
1309 EVP_CIPH_CBC_MODE,
1310 camellia_init,
1311 camellia_do_cipher,
1312 camellia_cleanup,
1313 sizeof(CAMELLIA_KEY),
1314 NULL,
1315 NULL,
1316 NULL,
1317 NULL
1319 return &cipher;
1323 * The Camellia-256 cipher type
1325 * @return the Camellia-256 EVP_CIPHER pointer.
1327 * @ingroup hcrypto_evp
1330 const EVP_CIPHER *
1331 EVP_camellia_256_cbc(void)
1333 static const EVP_CIPHER cipher = {
1338 EVP_CIPH_CBC_MODE,
1339 camellia_init,
1340 camellia_do_cipher,
1341 camellia_cleanup,
1342 sizeof(CAMELLIA_KEY),
1343 NULL,
1344 NULL,
1345 NULL,
1346 NULL
1348 return &cipher;
1355 static const struct cipher_name {
1356 const char *name;
1357 const EVP_CIPHER *(*func)(void);
1358 } cipher_name[] = {
1359 { "des-ede3-cbc", EVP_des_ede3_cbc },
1360 { "aes-128-cbc", EVP_aes_128_cbc },
1361 { "aes-192-cbc", EVP_aes_192_cbc },
1362 { "aes-256-cbc", EVP_aes_256_cbc },
1363 { "camellia-128-cbc", EVP_camellia_128_cbc },
1364 { "camellia-192-cbc", EVP_camellia_192_cbc },
1365 { "camellia-256-cbc", EVP_camellia_256_cbc }
1369 * Get the cipher type using their name.
1371 * @param name the name of the cipher.
1373 * @return the selected EVP_CIPHER pointer or NULL if not found.
1375 * @ingroup hcrypto_evp
1378 const EVP_CIPHER *
1379 EVP_get_cipherbyname(const char *name)
1381 int i;
1382 for (i = 0; i < sizeof(cipher_name)/sizeof(cipher_name[0]); i++) {
1383 if (strcasecmp(cipher_name[i].name, name) == 0)
1384 return (*cipher_name[i].func)();
1386 return NULL;
1394 #ifndef min
1395 #define min(a,b) (((a)>(b))?(b):(a))
1396 #endif
1399 * Provides a legancy string to key function, used in PEM files.
1401 * New protocols should use new string to key functions like NIST
1402 * SP56-800A or PKCS#5 v2.0 (see PKCS5_PBKDF2_HMAC_SHA1()).
1404 * @param type type of cipher to use
1405 * @param md message digest to use
1406 * @param salt salt salt string, should be an binary 8 byte buffer.
1407 * @param data the password/input key string.
1408 * @param datalen length of data parameter.
1409 * @param count iteration counter.
1410 * @param keydata output keydata, needs to of the size EVP_CIPHER_key_length().
1411 * @param ivdata output ivdata, needs to of the size EVP_CIPHER_block_size().
1413 * @return the size of derived key.
1415 * @ingroup hcrypto_evp
1419 EVP_BytesToKey(const EVP_CIPHER *type,
1420 const EVP_MD *md,
1421 const void *salt,
1422 const void *data, size_t datalen,
1423 unsigned int count,
1424 void *keydata,
1425 void *ivdata)
1427 int ivlen, keylen, first = 0;
1428 unsigned int mds = 0, i;
1429 unsigned char *key = keydata;
1430 unsigned char *iv = ivdata;
1431 unsigned char *buf;
1432 EVP_MD_CTX c;
1434 keylen = EVP_CIPHER_key_length(type);
1435 ivlen = EVP_CIPHER_iv_length(type);
1437 if (data == NULL)
1438 return keylen;
1440 buf = malloc(EVP_MD_size(md));
1441 if (buf == NULL)
1442 return -1;
1444 EVP_MD_CTX_init(&c);
1446 first = 1;
1447 while (1) {
1448 EVP_DigestInit_ex(&c, md, NULL);
1449 if (!first)
1450 EVP_DigestUpdate(&c, buf, mds);
1451 first = 0;
1452 EVP_DigestUpdate(&c,data,datalen);
1454 #define PKCS5_SALT_LEN 8
1456 if (salt)
1457 EVP_DigestUpdate(&c, salt, PKCS5_SALT_LEN);
1459 EVP_DigestFinal_ex(&c, buf, &mds);
1460 assert(mds == EVP_MD_size(md));
1462 for (i = 1; i < count; i++) {
1463 EVP_DigestInit_ex(&c, md, NULL);
1464 EVP_DigestUpdate(&c, buf, mds);
1465 EVP_DigestFinal_ex(&c, buf, &mds);
1466 assert(mds == EVP_MD_size(md));
1469 i = 0;
1470 if (keylen) {
1471 size_t sz = min(keylen, mds);
1472 if (key) {
1473 memcpy(key, buf, sz);
1474 key += sz;
1476 keylen -= sz;
1477 i += sz;
1479 if (ivlen && mds > i) {
1480 size_t sz = min(ivlen, (mds - i));
1481 if (iv) {
1482 memcpy(iv, &buf[i], sz);
1483 iv += sz;
1485 ivlen -= sz;
1487 if (keylen == 0 && ivlen == 0)
1488 break;
1491 EVP_MD_CTX_cleanup(&c);
1492 free(buf);
1494 return EVP_CIPHER_key_length(type);
1498 * Add all algorithms to the crypto core.
1500 * @ingroup hcrypto_core
1503 void
1504 OpenSSL_add_all_algorithms(void)
1506 return;
1510 * Add all algorithms to the crypto core using configuration file.
1512 * @ingroup hcrypto_core
1515 void
1516 OpenSSL_add_all_algorithms_conf(void)
1518 return;
1522 * Add all algorithms to the crypto core, but don't use the
1523 * configuration file.
1525 * @ingroup hcrypto_core
1528 void
1529 OpenSSL_add_all_algorithms_noconf(void)
1531 return;