clean better
[heimdal.git] / lib / hcrypto / evp.c
blob5946e848d3dd5fb8940fb1b2ffce546c21129972
1 /*
2 * Copyright (c) 2006 - 2008 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 #define HC_DEPRECATED
39 #define HC_DEPRECATED_CRYPTO
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>
48 #include <evp-hcrypto.h>
49 #include <evp-cc.h>
51 #include <krb5-types.h>
52 #include <roken.h>
54 #ifndef HCRYPTO_DEF_PROVIDER
55 #define HCRYPTO_DEF_PROVIDER hcrypto
56 #endif
58 #define HC_CONCAT4(x,y,z,aa) x ## y ## z ## aa
61 #define EVP_DEF_OP(_prov,_op) HC_CONCAT4(EVP_,_prov,_,_op)()
63 /**
64 * @page page_evp EVP - generic crypto interface
66 * See the library functions here: @ref hcrypto_evp
68 * @section evp_cipher EVP Cipher
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 .
75 * @example example_evp_cipher.c
77 * This is an example how to use EVP_CipherInit_ex(),
78 * EVP_CipherUpdate() and EVP_CipherFinal_ex().
81 struct hc_EVP_MD_CTX {
82 const EVP_MD *md;
83 ENGINE *engine;
84 void *ptr;
88 /**
89 * Return the output size of the message digest function.
91 * @param md the evp message
93 * @return size output size of the message digest function.
95 * @ingroup hcrypto_evp
98 size_t
99 EVP_MD_size(const EVP_MD *md)
101 return md->hash_size;
105 * Return the blocksize of the message digest function.
107 * @param md the evp message
109 * @return size size of the message digest block size
111 * @ingroup hcrypto_evp
114 size_t
115 EVP_MD_block_size(const EVP_MD *md)
117 return md->block_size;
121 * Allocate a messsage digest context object. Free with
122 * EVP_MD_CTX_destroy().
124 * @return a newly allocated message digest context object.
126 * @ingroup hcrypto_evp
129 EVP_MD_CTX *
130 EVP_MD_CTX_create(void)
132 return calloc(1, sizeof(EVP_MD_CTX));
136 * Initiate a messsage digest context object. Deallocate with
137 * EVP_MD_CTX_cleanup(). Please use EVP_MD_CTX_create() instead.
139 * @param ctx variable to initiate.
141 * @ingroup hcrypto_evp
144 void
145 EVP_MD_CTX_init(EVP_MD_CTX *ctx) HC_DEPRECATED
147 memset(ctx, 0, sizeof(*ctx));
151 * Free a messsage digest context object.
153 * @param ctx context to free.
155 * @ingroup hcrypto_evp
158 void
159 EVP_MD_CTX_destroy(EVP_MD_CTX *ctx)
161 EVP_MD_CTX_cleanup(ctx);
162 free(ctx);
166 * Free the resources used by the EVP_MD context.
168 * @param ctx the context to free the resources from.
170 * @return 1 on success.
172 * @ingroup hcrypto_evp
176 EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) HC_DEPRECATED
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;
190 * Get the EVP_MD use for a specified context.
192 * @param ctx the EVP_MD context to get the EVP_MD for.
194 * @return the EVP_MD used for the context.
196 * @ingroup hcrypto_evp
199 const EVP_MD *
200 EVP_MD_CTX_md(EVP_MD_CTX *ctx)
202 return ctx->md;
206 * Return the output size of the message digest function.
208 * @param ctx the evp message digest context
210 * @return size output size of the message digest function.
212 * @ingroup hcrypto_evp
215 size_t
216 EVP_MD_CTX_size(EVP_MD_CTX *ctx)
218 return EVP_MD_size(ctx->md);
222 * Return the blocksize of the message digest function.
224 * @param ctx the evp message digest context
226 * @return size size of the message digest block size
228 * @ingroup hcrypto_evp
231 size_t
232 EVP_MD_CTX_block_size(EVP_MD_CTX *ctx)
234 return EVP_MD_block_size(ctx->md);
238 * Init a EVP_MD_CTX for use a specific message digest and engine.
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.
244 * @return 1 on success.
246 * @ingroup hcrypto_evp
250 EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *md, ENGINE *engine)
252 if (ctx->md != md || ctx->engine != engine) {
253 EVP_MD_CTX_cleanup(ctx);
254 ctx->md = md;
255 ctx->engine = engine;
257 ctx->ptr = calloc(1, md->ctx_size);
258 if (ctx->ptr == NULL)
259 return 0;
261 (ctx->md->init)(ctx->ptr);
262 return 1;
266 * Update the digest with some data.
268 * @param ctx the context to update
269 * @param data the data to update the context with
270 * @param size length of data
272 * @return 1 on success.
274 * @ingroup hcrypto_evp
278 EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t size)
280 (ctx->md->update)(ctx->ptr, data, size);
281 return 1;
285 * Complete the message digest.
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.
292 * @return 1 on success.
294 * @ingroup hcrypto_evp
298 EVP_DigestFinal_ex(EVP_MD_CTX *ctx, void *hash, unsigned int *size)
300 (ctx->md->final)(hash, ctx->ptr);
301 if (size)
302 *size = ctx->md->hash_size;
303 return 1;
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.
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.
318 * @return 1 on success.
320 * @ingroup hcrypto_evp
324 EVP_Digest(const void *data, size_t dsize, void *hash, unsigned int *hsize,
325 const EVP_MD *md, ENGINE *engine)
327 EVP_MD_CTX *ctx;
328 int ret;
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;
338 ret = EVP_DigestUpdate(ctx, data, dsize);
339 if (ret != 1) {
340 EVP_MD_CTX_destroy(ctx);
341 return ret;
343 ret = EVP_DigestFinal_ex(ctx, hash, hsize);
344 EVP_MD_CTX_destroy(ctx);
345 return ret;
349 * The message digest SHA256
351 * @return the message digest type.
353 * @ingroup hcrypto_evp
356 const EVP_MD *
357 EVP_sha256(void)
359 hcrypto_validate();
360 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha256);
364 * The message digest SHA1
366 * @return the message digest type.
368 * @ingroup hcrypto_evp
371 const EVP_MD *
372 EVP_sha1(void)
374 hcrypto_validate();
375 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha1);
379 * The message digest SHA1
381 * @return the message digest type.
383 * @ingroup hcrypto_evp
386 const EVP_MD *
387 EVP_sha(void) HC_DEPRECATED
390 hcrypto_validate();
391 return EVP_sha1();
395 * The message digest MD5
397 * @return the message digest type.
399 * @ingroup hcrypto_evp
402 const EVP_MD *
403 EVP_md5(void) HC_DEPRECATED_CRYPTO
405 hcrypto_validate();
406 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, md5);
410 * The message digest MD4
412 * @return the message digest type.
414 * @ingroup hcrypto_evp
417 const EVP_MD *
418 EVP_md4(void) HC_DEPRECATED_CRYPTO
420 hcrypto_validate();
421 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, md4);
425 * The message digest MD2
427 * @return the message digest type.
429 * @ingroup hcrypto_evp
432 const EVP_MD *
433 EVP_md2(void) HC_DEPRECATED_CRYPTO
435 hcrypto_validate();
436 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, md2);
443 static void
444 null_Init (void *m)
447 static void
448 null_Update (void *m, const void * data, size_t size)
451 static void
452 null_Final(void *res, void *m)
457 * The null message digest
459 * @return the message digest type.
461 * @ingroup hcrypto_evp
464 const EVP_MD *
465 EVP_md_null(void)
467 static const struct hc_evp_md null = {
471 (hc_evp_md_init)null_Init,
472 (hc_evp_md_update)null_Update,
473 (hc_evp_md_final)null_Final,
474 NULL
476 return &null;
480 * Return the block size of the cipher.
482 * @param c cipher to get the block size from.
484 * @return the block size of the cipher.
486 * @ingroup hcrypto_evp
489 size_t
490 EVP_CIPHER_block_size(const EVP_CIPHER *c)
492 return c->block_size;
496 * Return the key size of the cipher.
498 * @param c cipher to get the key size from.
500 * @return the key size of the cipher.
502 * @ingroup hcrypto_evp
505 size_t
506 EVP_CIPHER_key_length(const EVP_CIPHER *c)
508 return c->key_len;
512 * Return the IV size of the cipher.
514 * @param c cipher to get the IV size from.
516 * @return the IV size of the cipher.
518 * @ingroup hcrypto_evp
521 size_t
522 EVP_CIPHER_iv_length(const EVP_CIPHER *c)
524 return c->iv_len;
528 * Initiate a EVP_CIPHER_CTX context. Clean up with
529 * EVP_CIPHER_CTX_cleanup().
531 * @param c the cipher initiate.
533 * @ingroup hcrypto_evp
536 void
537 EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *c)
539 memset(c, 0, sizeof(*c));
543 * Clean up the EVP_CIPHER_CTX context.
545 * @param c the cipher to clean up.
547 * @return 1 on success.
549 * @ingroup hcrypto_evp
553 EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
555 if (c->cipher && c->cipher->cleanup)
556 c->cipher->cleanup(c);
557 if (c->cipher_data) {
558 memset(c->cipher_data, 0, c->cipher->ctx_size);
559 free(c->cipher_data);
560 c->cipher_data = NULL;
562 return 1;
566 * If the cipher type supports it, change the key length
568 * @param c the cipher context to change the key length for
569 * @param length new key length
571 * @return 1 on success.
573 * @ingroup hcrypto_evp
577 EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int length)
579 if ((c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH) && length > 0) {
580 c->key_len = length;
581 return 1;
583 return 0;
586 #if 0
588 EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *c, int pad)
590 return 0;
592 #endif
595 * Return the EVP_CIPHER for a EVP_CIPHER_CTX context.
597 * @param ctx the context to get the cipher type from.
599 * @return the EVP_CIPHER pointer.
601 * @ingroup hcrypto_evp
604 const EVP_CIPHER *
605 EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX *ctx)
607 return ctx->cipher;
611 * Return the block size of the cipher context.
613 * @param ctx cipher context to get the block size from.
615 * @return the block size of the cipher context.
617 * @ingroup hcrypto_evp
620 size_t
621 EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
623 return EVP_CIPHER_block_size(ctx->cipher);
627 * Return the key size of the cipher context.
629 * @param ctx cipher context to get the key size from.
631 * @return the key size of the cipher context.
633 * @ingroup hcrypto_evp
636 size_t
637 EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
639 return EVP_CIPHER_key_length(ctx->cipher);
643 * Return the IV size of the cipher context.
645 * @param ctx cipher context to get the IV size from.
647 * @return the IV size of the cipher context.
649 * @ingroup hcrypto_evp
652 size_t
653 EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
655 return EVP_CIPHER_iv_length(ctx->cipher);
659 * Get the flags for an EVP_CIPHER_CTX context.
661 * @param ctx the EVP_CIPHER_CTX to get the flags from
663 * @return the flags for an EVP_CIPHER_CTX.
665 * @ingroup hcrypto_evp
668 unsigned long
669 EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx)
671 return ctx->cipher->flags;
675 * Get the mode for an EVP_CIPHER_CTX context.
677 * @param ctx the EVP_CIPHER_CTX to get the mode from
679 * @return the mode for an EVP_CIPHER_CTX.
681 * @ingroup hcrypto_evp
685 EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx)
687 return EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_MODE;
691 * Get the app data for an EVP_CIPHER_CTX context.
693 * @param ctx the EVP_CIPHER_CTX to get the app data from
695 * @return the app data for an EVP_CIPHER_CTX.
697 * @ingroup hcrypto_evp
700 void *
701 EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX *ctx)
703 return ctx->app_data;
707 * Set the app data for an EVP_CIPHER_CTX context.
709 * @param ctx the EVP_CIPHER_CTX to set the app data for
710 * @param data the app data to set for an EVP_CIPHER_CTX.
712 * @ingroup hcrypto_evp
715 void
716 EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
718 ctx->app_data = data;
722 * Initiate the EVP_CIPHER_CTX context to encrypt or decrypt data.
723 * Clean up with EVP_CIPHER_CTX_cleanup().
725 * @param ctx context to initiate
726 * @param c cipher to use.
727 * @param engine crypto engine to use, NULL to select default.
728 * @param key the crypto key to use, NULL will use the previous value.
729 * @param iv the IV to use, NULL will use the previous value.
730 * @param encp non zero will encrypt, -1 use the previous value.
732 * @return 1 on success.
734 * @ingroup hcrypto_evp
738 EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *c, ENGINE *engine,
739 const void *key, const void *iv, int encp)
741 ctx->buf_len = 0;
743 if (encp == -1)
744 encp = ctx->encrypt;
745 else
746 ctx->encrypt = (encp ? 1 : 0);
748 if (c && (c != ctx->cipher)) {
749 EVP_CIPHER_CTX_cleanup(ctx);
750 ctx->cipher = c;
751 ctx->key_len = c->key_len;
753 ctx->cipher_data = calloc(1, c->ctx_size);
754 if (ctx->cipher_data == NULL && c->ctx_size != 0)
755 return 0;
757 /* assume block size is a multiple of 2 */
758 ctx->block_mask = EVP_CIPHER_block_size(c) - 1;
760 } else if (ctx->cipher == NULL) {
761 /* reuse of cipher, but not any cipher ever set! */
762 return 0;
765 switch (EVP_CIPHER_CTX_mode(ctx)) {
766 case EVP_CIPH_CBC_MODE:
768 assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv));
770 if (iv)
771 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
772 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
773 break;
775 case EVP_CIPH_STREAM_CIPHER:
776 break;
777 case EVP_CIPH_CFB8_MODE:
778 if (iv)
779 memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
780 break;
782 default:
783 return 0;
786 if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT))
787 ctx->cipher->init(ctx, key, iv, encp);
789 return 1;
793 * Encipher/decipher partial data
795 * @param ctx the cipher context.
796 * @param out output data from the operation.
797 * @param outlen output length
798 * @param in input data to the operation.
799 * @param inlen length of data.
801 * The output buffer length should at least be EVP_CIPHER_block_size()
802 * byte longer then the input length.
804 * See @ref evp_cipher for an example how to use this function.
806 * @return 1 on success.
808 * @ingroup hcrypto_evp
812 EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, void *out, int *outlen,
813 void *in, size_t inlen)
815 int ret, left, blocksize;
817 *outlen = 0;
820 * If there in no spare bytes in the left from last Update and the
821 * input length is on the block boundery, the EVP_CipherUpdate()
822 * function can take a shortcut (and preformance gain) and
823 * directly encrypt the data, otherwise we hav to fix it up and
824 * store extra it the EVP_CIPHER_CTX.
826 if (ctx->buf_len == 0 && (inlen & ctx->block_mask) == 0) {
827 ret = (*ctx->cipher->do_cipher)(ctx, out, in, inlen);
828 if (ret == 1)
829 *outlen = inlen;
830 else
831 *outlen = 0;
832 return ret;
836 blocksize = EVP_CIPHER_CTX_block_size(ctx);
837 left = blocksize - ctx->buf_len;
838 assert(left > 0);
840 if (ctx->buf_len) {
842 /* if total buffer is smaller then input, store locally */
843 if (inlen < left) {
844 memcpy(ctx->buf + ctx->buf_len, in, inlen);
845 ctx->buf_len += inlen;
846 return 1;
849 /* fill in local buffer and encrypt */
850 memcpy(ctx->buf + ctx->buf_len, in, left);
851 ret = (*ctx->cipher->do_cipher)(ctx, out, ctx->buf, blocksize);
852 memset(ctx->buf, 0, blocksize);
853 if (ret != 1)
854 return ret;
856 *outlen += blocksize;
857 inlen -= left;
858 in = ((unsigned char *)in) + left;
859 out = ((unsigned char *)out) + blocksize;
860 ctx->buf_len = 0;
863 if (inlen) {
864 ctx->buf_len = (inlen & ctx->block_mask);
865 inlen &= ~ctx->block_mask;
867 ret = (*ctx->cipher->do_cipher)(ctx, out, in, inlen);
868 if (ret != 1)
869 return ret;
871 *outlen += inlen;
873 in = ((unsigned char *)in) + inlen;
874 memcpy(ctx->buf, in, ctx->buf_len);
877 return 1;
881 * Encipher/decipher final data
883 * @param ctx the cipher context.
884 * @param out output data from the operation.
885 * @param outlen output length
887 * The input length needs to be at least EVP_CIPHER_block_size() bytes
888 * long.
890 * See @ref evp_cipher for an example how to use this function.
892 * @return 1 on success.
894 * @ingroup hcrypto_evp
898 EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, void *out, int *outlen)
900 *outlen = 0;
902 if (ctx->buf_len) {
903 int ret, left, blocksize;
905 blocksize = EVP_CIPHER_CTX_block_size(ctx);
907 left = blocksize - ctx->buf_len;
908 assert(left > 0);
910 /* zero fill local buffer */
911 memset(ctx->buf + ctx->buf_len, 0, left);
912 ret = (*ctx->cipher->do_cipher)(ctx, out, ctx->buf, blocksize);
913 memset(ctx->buf, 0, blocksize);
914 if (ret != 1)
915 return ret;
917 *outlen += blocksize;
920 return 1;
924 * Encipher/decipher data
926 * @param ctx the cipher context.
927 * @param out out data from the operation.
928 * @param in in data to the operation.
929 * @param size length of data.
931 * @return 1 on success.
935 EVP_Cipher(EVP_CIPHER_CTX *ctx, void *out, const void *in,size_t size)
937 return ctx->cipher->do_cipher(ctx, out, in, size);
944 static int
945 enc_null_init(EVP_CIPHER_CTX *ctx,
946 const unsigned char * key,
947 const unsigned char * iv,
948 int encp)
950 return 1;
953 static int
954 enc_null_do_cipher(EVP_CIPHER_CTX *ctx,
955 unsigned char *out,
956 const unsigned char *in,
957 unsigned int size)
959 memmove(out, in, size);
960 return 1;
963 static int
964 enc_null_cleanup(EVP_CIPHER_CTX *ctx)
966 return 1;
970 * The NULL cipher type, does no encryption/decryption.
972 * @return the null EVP_CIPHER pointer.
974 * @ingroup hcrypto_evp
977 const EVP_CIPHER *
978 EVP_enc_null(void)
980 static const EVP_CIPHER enc_null = {
985 EVP_CIPH_CBC_MODE,
986 enc_null_init,
987 enc_null_do_cipher,
988 enc_null_cleanup,
990 NULL,
991 NULL,
992 NULL,
993 NULL
995 return &enc_null;
999 * The RC2 cipher type
1001 * @return the RC2 EVP_CIPHER pointer.
1003 * @ingroup hcrypto_evp
1006 const EVP_CIPHER *
1007 EVP_rc2_cbc(void)
1009 hcrypto_validate();
1010 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_cbc);
1014 * The RC2 cipher type
1016 * @return the RC2 EVP_CIPHER pointer.
1018 * @ingroup hcrypto_evp
1021 const EVP_CIPHER *
1022 EVP_rc2_40_cbc(void)
1024 hcrypto_validate();
1025 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_40_cbc);
1029 * The RC2 cipher type
1031 * @return the RC2 EVP_CIPHER pointer.
1033 * @ingroup hcrypto_evp
1036 const EVP_CIPHER *
1037 EVP_rc2_64_cbc(void)
1039 hcrypto_validate();
1040 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_64_cbc);
1044 * The RC4 cipher type
1046 * @return the RC4 EVP_CIPHER pointer.
1048 * @ingroup hcrypto_evp
1051 const EVP_CIPHER *
1052 EVP_rc4(void)
1054 hcrypto_validate();
1055 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc4);
1059 * The RC4-40 cipher type
1061 * @return the RC4-40 EVP_CIPHER pointer.
1063 * @ingroup hcrypto_evp
1066 const EVP_CIPHER *
1067 EVP_rc4_40(void)
1069 hcrypto_validate();
1070 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc4_40);
1074 * The DES cipher type
1076 * @return the DES-CBC EVP_CIPHER pointer.
1078 * @ingroup hcrypto_evp
1081 const EVP_CIPHER *
1082 EVP_des_cbc(void)
1084 hcrypto_validate();
1085 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, des_cbc);
1089 * The tripple DES cipher type
1091 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
1093 * @ingroup hcrypto_evp
1096 const EVP_CIPHER *
1097 EVP_des_ede3_cbc(void)
1099 hcrypto_validate();
1100 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, des_ede3_cbc);
1104 * The AES-128 cipher type
1106 * @return the AES-128 EVP_CIPHER pointer.
1108 * @ingroup hcrypto_evp
1111 const EVP_CIPHER *
1112 EVP_aes_128_cbc(void)
1114 hcrypto_validate();
1115 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_128_cbc);
1119 * The AES-192 cipher type
1121 * @return the AES-192 EVP_CIPHER pointer.
1123 * @ingroup hcrypto_evp
1126 const EVP_CIPHER *
1127 EVP_aes_192_cbc(void)
1129 hcrypto_validate();
1130 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_192_cbc);
1134 * The AES-256 cipher type
1136 * @return the AES-256 EVP_CIPHER pointer.
1138 * @ingroup hcrypto_evp
1141 const EVP_CIPHER *
1142 EVP_aes_256_cbc(void)
1144 hcrypto_validate();
1145 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_256_cbc);
1149 * The AES-128 cipher type
1151 * @return the AES-128 EVP_CIPHER pointer.
1153 * @ingroup hcrypto_evp
1156 const EVP_CIPHER *
1157 EVP_aes_128_cfb8(void)
1159 hcrypto_validate();
1160 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_128_cfb8);
1164 * The AES-192 cipher type
1166 * @return the AES-192 EVP_CIPHER pointer.
1168 * @ingroup hcrypto_evp
1171 const EVP_CIPHER *
1172 EVP_aes_192_cfb8(void)
1174 hcrypto_validate();
1175 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_192_cfb8);
1179 * The AES-256 cipher type
1181 * @return the AES-256 EVP_CIPHER pointer.
1183 * @ingroup hcrypto_evp
1186 const EVP_CIPHER *
1187 EVP_aes_256_cfb8(void)
1189 hcrypto_validate();
1190 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_256_cfb8);
1194 * The Camellia-128 cipher type
1196 * @return the Camellia-128 EVP_CIPHER pointer.
1198 * @ingroup hcrypto_evp
1201 const EVP_CIPHER *
1202 EVP_camellia_128_cbc(void)
1204 hcrypto_validate();
1205 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_128_cbc);
1209 * The Camellia-198 cipher type
1211 * @return the Camellia-198 EVP_CIPHER pointer.
1213 * @ingroup hcrypto_evp
1216 const EVP_CIPHER *
1217 EVP_camellia_192_cbc(void)
1219 hcrypto_validate();
1220 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_192_cbc);
1224 * The Camellia-256 cipher type
1226 * @return the Camellia-256 EVP_CIPHER pointer.
1228 * @ingroup hcrypto_evp
1231 const EVP_CIPHER *
1232 EVP_camellia_256_cbc(void)
1234 hcrypto_validate();
1235 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_256_cbc);
1242 static const struct cipher_name {
1243 const char *name;
1244 const EVP_CIPHER *(*func)(void);
1245 } cipher_name[] = {
1246 { "des-ede3-cbc", EVP_des_ede3_cbc },
1247 { "aes-128-cbc", EVP_aes_128_cbc },
1248 { "aes-192-cbc", EVP_aes_192_cbc },
1249 { "aes-256-cbc", EVP_aes_256_cbc },
1250 { "aes-128-cfb8", EVP_aes_128_cfb8 },
1251 { "aes-192-cfb8", EVP_aes_192_cfb8 },
1252 { "aes-256-cfb8", EVP_aes_256_cfb8 },
1253 { "camellia-128-cbc", EVP_camellia_128_cbc },
1254 { "camellia-192-cbc", EVP_camellia_192_cbc },
1255 { "camellia-256-cbc", EVP_camellia_256_cbc }
1259 * Get the cipher type using their name.
1261 * @param name the name of the cipher.
1263 * @return the selected EVP_CIPHER pointer or NULL if not found.
1265 * @ingroup hcrypto_evp
1268 const EVP_CIPHER *
1269 EVP_get_cipherbyname(const char *name)
1271 int i;
1272 for (i = 0; i < sizeof(cipher_name)/sizeof(cipher_name[0]); i++) {
1273 if (strcasecmp(cipher_name[i].name, name) == 0)
1274 return (*cipher_name[i].func)();
1276 return NULL;
1284 #ifndef min
1285 #define min(a,b) (((a)>(b))?(b):(a))
1286 #endif
1289 * Provides a legancy string to key function, used in PEM files.
1291 * New protocols should use new string to key functions like NIST
1292 * SP56-800A or PKCS#5 v2.0 (see PKCS5_PBKDF2_HMAC_SHA1()).
1294 * @param type type of cipher to use
1295 * @param md message digest to use
1296 * @param salt salt salt string, should be an binary 8 byte buffer.
1297 * @param data the password/input key string.
1298 * @param datalen length of data parameter.
1299 * @param count iteration counter.
1300 * @param keydata output keydata, needs to of the size EVP_CIPHER_key_length().
1301 * @param ivdata output ivdata, needs to of the size EVP_CIPHER_block_size().
1303 * @return the size of derived key.
1305 * @ingroup hcrypto_evp
1309 EVP_BytesToKey(const EVP_CIPHER *type,
1310 const EVP_MD *md,
1311 const void *salt,
1312 const void *data, size_t datalen,
1313 unsigned int count,
1314 void *keydata,
1315 void *ivdata)
1317 int ivlen, keylen, first = 0;
1318 unsigned int mds = 0, i;
1319 unsigned char *key = keydata;
1320 unsigned char *iv = ivdata;
1321 unsigned char *buf;
1322 EVP_MD_CTX c;
1324 keylen = EVP_CIPHER_key_length(type);
1325 ivlen = EVP_CIPHER_iv_length(type);
1327 if (data == NULL)
1328 return keylen;
1330 buf = malloc(EVP_MD_size(md));
1331 if (buf == NULL)
1332 return -1;
1334 EVP_MD_CTX_init(&c);
1336 first = 1;
1337 while (1) {
1338 EVP_DigestInit_ex(&c, md, NULL);
1339 if (!first)
1340 EVP_DigestUpdate(&c, buf, mds);
1341 first = 0;
1342 EVP_DigestUpdate(&c,data,datalen);
1344 #define PKCS5_SALT_LEN 8
1346 if (salt)
1347 EVP_DigestUpdate(&c, salt, PKCS5_SALT_LEN);
1349 EVP_DigestFinal_ex(&c, buf, &mds);
1350 assert(mds == EVP_MD_size(md));
1352 for (i = 1; i < count; i++) {
1353 EVP_DigestInit_ex(&c, md, NULL);
1354 EVP_DigestUpdate(&c, buf, mds);
1355 EVP_DigestFinal_ex(&c, buf, &mds);
1356 assert(mds == EVP_MD_size(md));
1359 i = 0;
1360 if (keylen) {
1361 size_t sz = min(keylen, mds);
1362 if (key) {
1363 memcpy(key, buf, sz);
1364 key += sz;
1366 keylen -= sz;
1367 i += sz;
1369 if (ivlen && mds > i) {
1370 size_t sz = min(ivlen, (mds - i));
1371 if (iv) {
1372 memcpy(iv, &buf[i], sz);
1373 iv += sz;
1375 ivlen -= sz;
1377 if (keylen == 0 && ivlen == 0)
1378 break;
1381 EVP_MD_CTX_cleanup(&c);
1382 free(buf);
1384 return EVP_CIPHER_key_length(type);
1388 * Generate a random key for the specificed EVP_CIPHER.
1390 * @param ctx EVP_CIPHER_CTX type to build the key for.
1391 * @param key return key, must be at least EVP_CIPHER_key_length() byte long.
1393 * @return 1 for success, 0 for failure.
1395 * @ingroup hcrypto_core
1399 EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, void *key)
1401 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
1402 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
1403 if (RAND_bytes(key, ctx->key_len) != 1)
1404 return 0;
1405 return 1;
1409 * Perform a operation on a ctx
1411 * @param ctx context to perform operation on.
1412 * @param type type of operation.
1413 * @param arg argument to operation.
1414 * @param data addition data to operation.
1416 * @return 1 for success, 0 for failure.
1418 * @ingroup hcrypto_core
1422 EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *data)
1424 if (ctx->cipher == NULL || ctx->cipher->ctrl == NULL)
1425 return 0;
1426 return (*ctx->cipher->ctrl)(ctx, type, arg, data);
1430 * Add all algorithms to the crypto core.
1432 * @ingroup hcrypto_core
1435 void
1436 OpenSSL_add_all_algorithms(void)
1438 return;
1442 * Add all algorithms to the crypto core using configuration file.
1444 * @ingroup hcrypto_core
1447 void
1448 OpenSSL_add_all_algorithms_conf(void)
1450 return;
1454 * Add all algorithms to the crypto core, but don't use the
1455 * configuration file.
1457 * @ingroup hcrypto_core
1460 void
1461 OpenSSL_add_all_algorithms_noconf(void)
1463 return;