Ensure DER form of hxtool ca random serial numbers
[heimdal.git] / lib / hcrypto / evp.c
blobc564353e4232edd9dc52f907a3e42847a37b162f
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>
50 #include <evp-w32.h>
52 #include <krb5-types.h>
53 #include <roken.h>
55 #ifndef HCRYPTO_DEF_PROVIDER
56 #define HCRYPTO_DEF_PROVIDER hcrypto
57 #endif
59 #define HC_CONCAT4(x,y,z,aa) x ## y ## z ## aa
62 #define EVP_DEF_OP(_prov,_op) HC_CONCAT4(EVP_,_prov,_,_op)()
64 /**
65 * @page page_evp EVP - generic crypto interface
67 * See the library functions here: @ref hcrypto_evp
69 * @section evp_cipher EVP Cipher
71 * The use of EVP_CipherInit_ex() and EVP_Cipher() is pretty easy to
72 * understand forward, then EVP_CipherUpdate() and
73 * EVP_CipherFinal_ex() really needs an example to explain @ref
74 * example_evp_cipher.c .
76 * @example example_evp_cipher.c
78 * This is an example how to use EVP_CipherInit_ex(),
79 * EVP_CipherUpdate() and EVP_CipherFinal_ex().
82 struct hc_EVP_MD_CTX {
83 const EVP_MD *md;
84 ENGINE *engine;
85 void *ptr;
89 /**
90 * Return the output size of the message digest function.
92 * @param md the evp message
94 * @return size output size of the message digest function.
96 * @ingroup hcrypto_evp
99 size_t
100 EVP_MD_size(const EVP_MD *md)
102 return md->hash_size;
106 * Return the blocksize of the message digest function.
108 * @param md the evp message
110 * @return size size of the message digest block size
112 * @ingroup hcrypto_evp
115 size_t
116 EVP_MD_block_size(const EVP_MD *md)
118 return md->block_size;
122 * Allocate a messsage digest context object. Free with
123 * EVP_MD_CTX_destroy().
125 * @return a newly allocated message digest context object.
127 * @ingroup hcrypto_evp
130 EVP_MD_CTX *
131 EVP_MD_CTX_create(void)
133 return calloc(1, sizeof(EVP_MD_CTX));
137 * Initiate a messsage digest context object. Deallocate with
138 * EVP_MD_CTX_cleanup(). Please use EVP_MD_CTX_create() instead.
140 * @param ctx variable to initiate.
142 * @ingroup hcrypto_evp
145 void
146 EVP_MD_CTX_init(EVP_MD_CTX *ctx) HC_DEPRECATED
148 memset(ctx, 0, sizeof(*ctx));
152 * Free a messsage digest context object.
154 * @param ctx context to free.
156 * @ingroup hcrypto_evp
159 void
160 EVP_MD_CTX_destroy(EVP_MD_CTX *ctx)
162 EVP_MD_CTX_cleanup(ctx);
163 free(ctx);
167 * Free the resources used by the EVP_MD context.
169 * @param ctx the context to free the resources from.
171 * @return 1 on success.
173 * @ingroup hcrypto_evp
177 EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) HC_DEPRECATED
179 if (ctx->md && ctx->md->cleanup) {
180 int ret = (ctx->md->cleanup)(ctx);
181 if (!ret)
182 return ret;
183 } else if (ctx->md) {
184 memset(ctx->ptr, 0, ctx->md->ctx_size);
186 ctx->md = NULL;
187 ctx->engine = NULL;
188 free(ctx->ptr);
189 memset(ctx, 0, sizeof(*ctx));
190 return 1;
194 * Get the EVP_MD use for a specified context.
196 * @param ctx the EVP_MD context to get the EVP_MD for.
198 * @return the EVP_MD used for the context.
200 * @ingroup hcrypto_evp
203 const EVP_MD *
204 EVP_MD_CTX_md(EVP_MD_CTX *ctx)
206 return ctx->md;
210 * Return the output size of the message digest function.
212 * @param ctx the evp message digest context
214 * @return size output size of the message digest function.
216 * @ingroup hcrypto_evp
219 size_t
220 EVP_MD_CTX_size(EVP_MD_CTX *ctx)
222 return EVP_MD_size(ctx->md);
226 * Return the blocksize of the message digest function.
228 * @param ctx the evp message digest context
230 * @return size size of the message digest block size
232 * @ingroup hcrypto_evp
235 size_t
236 EVP_MD_CTX_block_size(EVP_MD_CTX *ctx)
238 return EVP_MD_block_size(ctx->md);
242 * Init a EVP_MD_CTX for use a specific message digest and engine.
244 * @param ctx the message digest context to init.
245 * @param md the message digest to use.
246 * @param engine the engine to use, NULL to use the default engine.
248 * @return 1 on success.
250 * @ingroup hcrypto_evp
254 EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *md, ENGINE *engine)
256 if (ctx->md != md || ctx->engine != engine) {
257 EVP_MD_CTX_cleanup(ctx);
258 ctx->md = md;
259 ctx->engine = engine;
261 ctx->ptr = calloc(1, md->ctx_size);
262 if (ctx->ptr == NULL)
263 return 0;
265 return (ctx->md->init)(ctx->ptr);
269 * Update the digest with some data.
271 * @param ctx the context to update
272 * @param data the data to update the context with
273 * @param size length of data
275 * @return 1 on success.
277 * @ingroup hcrypto_evp
281 EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t size)
283 (ctx->md->update)(ctx->ptr, data, size);
284 return 1;
288 * Complete the message digest.
290 * @param ctx the context to complete.
291 * @param hash the output of the message digest function. At least
292 * EVP_MD_size().
293 * @param size the output size of hash.
295 * @return 1 on success.
297 * @ingroup hcrypto_evp
301 EVP_DigestFinal_ex(EVP_MD_CTX *ctx, void *hash, unsigned int *size)
303 (ctx->md->final)(hash, ctx->ptr);
304 if (size)
305 *size = ctx->md->hash_size;
306 return 1;
310 * Do the whole EVP_MD_CTX_create(), EVP_DigestInit_ex(),
311 * EVP_DigestUpdate(), EVP_DigestFinal_ex(), EVP_MD_CTX_destroy()
312 * dance in one call.
314 * @param data the data to update the context with
315 * @param dsize length of data
316 * @param hash output data of at least EVP_MD_size() length.
317 * @param hsize output length of hash.
318 * @param md message digest to use
319 * @param engine engine to use, NULL for default engine.
321 * @return 1 on success.
323 * @ingroup hcrypto_evp
327 EVP_Digest(const void *data, size_t dsize, void *hash, unsigned int *hsize,
328 const EVP_MD *md, ENGINE *engine)
330 EVP_MD_CTX *ctx;
331 int ret;
333 ctx = EVP_MD_CTX_create();
334 if (ctx == NULL)
335 return 0;
336 ret = EVP_DigestInit_ex(ctx, md, engine);
337 if (ret != 1) {
338 EVP_MD_CTX_destroy(ctx);
339 return ret;
341 ret = EVP_DigestUpdate(ctx, data, dsize);
342 if (ret != 1) {
343 EVP_MD_CTX_destroy(ctx);
344 return ret;
346 ret = EVP_DigestFinal_ex(ctx, hash, hsize);
347 EVP_MD_CTX_destroy(ctx);
348 return ret;
352 * The message digest SHA256
354 * @return the message digest type.
356 * @ingroup hcrypto_evp
359 const EVP_MD *
360 EVP_sha256(void)
362 hcrypto_validate();
363 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha256);
367 * The message digest SHA384
369 * @return the message digest type.
371 * @ingroup hcrypto_evp
374 const EVP_MD *
375 EVP_sha384(void)
377 hcrypto_validate();
378 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha384);
382 * The message digest SHA512
384 * @return the message digest type.
386 * @ingroup hcrypto_evp
389 const EVP_MD *
390 EVP_sha512(void)
392 hcrypto_validate();
393 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha512);
397 * The message digest SHA1
399 * @return the message digest type.
401 * @ingroup hcrypto_evp
404 const EVP_MD *
405 EVP_sha1(void)
407 hcrypto_validate();
408 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha1);
412 * The message digest SHA1
414 * @return the message digest type.
416 * @ingroup hcrypto_evp
419 const EVP_MD *
420 EVP_sha(void) HC_DEPRECATED
423 hcrypto_validate();
424 return EVP_sha1();
428 * The message digest MD5
430 * @return the message digest type.
432 * @ingroup hcrypto_evp
435 const EVP_MD *
436 EVP_md5(void) HC_DEPRECATED_CRYPTO
438 hcrypto_validate();
439 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, md5);
443 * The message digest MD4
445 * @return the message digest type.
447 * @ingroup hcrypto_evp
450 const EVP_MD *
451 EVP_md4(void) HC_DEPRECATED_CRYPTO
453 hcrypto_validate();
454 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, md4);
458 * The message digest MD2
460 * @return the message digest type.
462 * @ingroup hcrypto_evp
465 const EVP_MD *
466 EVP_md2(void) HC_DEPRECATED_CRYPTO
468 hcrypto_validate();
469 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, md2);
476 static void
477 null_Init (void *m)
480 static void
481 null_Update (void *m, const void * data, size_t size)
484 static void
485 null_Final(void *res, void *m)
490 * The null message digest
492 * @return the message digest type.
494 * @ingroup hcrypto_evp
497 const EVP_MD *
498 EVP_md_null(void)
500 static const struct hc_evp_md null = {
504 (hc_evp_md_init)null_Init,
505 (hc_evp_md_update)null_Update,
506 (hc_evp_md_final)null_Final,
507 NULL
509 return &null;
513 * Return the block size of the cipher.
515 * @param c cipher to get the block size from.
517 * @return the block size of the cipher.
519 * @ingroup hcrypto_evp
522 size_t
523 EVP_CIPHER_block_size(const EVP_CIPHER *c)
525 return c->block_size;
529 * Return the key size of the cipher.
531 * @param c cipher to get the key size from.
533 * @return the key size of the cipher.
535 * @ingroup hcrypto_evp
538 size_t
539 EVP_CIPHER_key_length(const EVP_CIPHER *c)
541 return c->key_len;
545 * Return the IV size of the cipher.
547 * @param c cipher to get the IV size from.
549 * @return the IV size of the cipher.
551 * @ingroup hcrypto_evp
554 size_t
555 EVP_CIPHER_iv_length(const EVP_CIPHER *c)
557 return c->iv_len;
561 * Initiate a EVP_CIPHER_CTX context. Clean up with
562 * EVP_CIPHER_CTX_cleanup().
564 * @param c the cipher initiate.
566 * @ingroup hcrypto_evp
569 void
570 EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *c)
572 memset(c, 0, sizeof(*c));
576 * Clean up the EVP_CIPHER_CTX context.
578 * @param c the cipher to clean up.
580 * @return 1 on success.
582 * @ingroup hcrypto_evp
586 EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
588 if (c->cipher && c->cipher->cleanup) {
589 int ret = c->cipher->cleanup(c);
590 if (!ret)
591 return ret;
593 if (c->cipher_data) {
594 memset(c->cipher_data, 0, c->cipher->ctx_size);
595 free(c->cipher_data);
596 c->cipher_data = NULL;
598 return 1;
602 * If the cipher type supports it, change the key length
604 * @param c the cipher context to change the key length for
605 * @param length new key length
607 * @return 1 on success.
609 * @ingroup hcrypto_evp
613 EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int length)
615 if ((c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH) && length > 0) {
616 c->key_len = length;
617 return 1;
619 return 0;
622 #if 0
624 EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *c, int pad)
626 return 0;
628 #endif
631 * Return the EVP_CIPHER for a EVP_CIPHER_CTX context.
633 * @param ctx the context to get the cipher type from.
635 * @return the EVP_CIPHER pointer.
637 * @ingroup hcrypto_evp
640 const EVP_CIPHER *
641 EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX *ctx)
643 return ctx->cipher;
647 * Return the block size of the cipher context.
649 * @param ctx cipher context to get the block size from.
651 * @return the block size of the cipher context.
653 * @ingroup hcrypto_evp
656 size_t
657 EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
659 return EVP_CIPHER_block_size(ctx->cipher);
663 * Return the key size of the cipher context.
665 * @param ctx cipher context to get the key size from.
667 * @return the key size of the cipher context.
669 * @ingroup hcrypto_evp
672 size_t
673 EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
675 return EVP_CIPHER_key_length(ctx->cipher);
679 * Return the IV size of the cipher context.
681 * @param ctx cipher context to get the IV size from.
683 * @return the IV size of the cipher context.
685 * @ingroup hcrypto_evp
688 size_t
689 EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
691 return EVP_CIPHER_iv_length(ctx->cipher);
695 * Get the flags for an EVP_CIPHER_CTX context.
697 * @param ctx the EVP_CIPHER_CTX to get the flags from
699 * @return the flags for an EVP_CIPHER_CTX.
701 * @ingroup hcrypto_evp
704 unsigned long
705 EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx)
707 return ctx->cipher->flags;
711 * Get the mode for an EVP_CIPHER_CTX context.
713 * @param ctx the EVP_CIPHER_CTX to get the mode from
715 * @return the mode for an EVP_CIPHER_CTX.
717 * @ingroup hcrypto_evp
721 EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx)
723 return EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_MODE;
727 * Get the app data for an EVP_CIPHER_CTX context.
729 * @param ctx the EVP_CIPHER_CTX to get the app data from
731 * @return the app data for an EVP_CIPHER_CTX.
733 * @ingroup hcrypto_evp
736 void *
737 EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX *ctx)
739 return ctx->app_data;
743 * Set the app data for an EVP_CIPHER_CTX context.
745 * @param ctx the EVP_CIPHER_CTX to set the app data for
746 * @param data the app data to set for an EVP_CIPHER_CTX.
748 * @ingroup hcrypto_evp
751 void
752 EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
754 ctx->app_data = data;
758 * Initiate the EVP_CIPHER_CTX context to encrypt or decrypt data.
759 * Clean up with EVP_CIPHER_CTX_cleanup().
761 * @param ctx context to initiate
762 * @param c cipher to use.
763 * @param engine crypto engine to use, NULL to select default.
764 * @param key the crypto key to use, NULL will use the previous value.
765 * @param iv the IV to use, NULL will use the previous value.
766 * @param encp non zero will encrypt, -1 use the previous value.
768 * @return 1 on success.
770 * @ingroup hcrypto_evp
774 EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *c, ENGINE *engine,
775 const void *key, const void *iv, int encp)
777 ctx->buf_len = 0;
779 if (encp == -1)
780 encp = ctx->encrypt;
781 else
782 ctx->encrypt = (encp ? 1 : 0);
784 if (c && (c != ctx->cipher)) {
785 EVP_CIPHER_CTX_cleanup(ctx);
786 ctx->cipher = c;
787 ctx->key_len = c->key_len;
789 ctx->cipher_data = calloc(1, c->ctx_size);
790 if (ctx->cipher_data == NULL && c->ctx_size != 0)
791 return 0;
793 /* assume block size is a multiple of 2 */
794 ctx->block_mask = EVP_CIPHER_block_size(c) - 1;
796 } else if (ctx->cipher == NULL) {
797 /* reuse of cipher, but not any cipher ever set! */
798 return 0;
801 switch (EVP_CIPHER_CTX_mode(ctx)) {
802 case EVP_CIPH_CBC_MODE:
804 assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv));
806 if (iv)
807 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
808 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
809 break;
811 case EVP_CIPH_STREAM_CIPHER:
812 break;
813 case EVP_CIPH_CFB8_MODE:
814 if (iv)
815 memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
816 break;
818 default:
819 return 0;
822 if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT))
823 return ctx->cipher->init(ctx, key, iv, encp);
825 return 1;
829 * Encipher/decipher partial data
831 * @param ctx the cipher context.
832 * @param out output data from the operation.
833 * @param outlen output length
834 * @param in input data to the operation.
835 * @param inlen length of data.
837 * The output buffer length should at least be EVP_CIPHER_block_size()
838 * byte longer then the input length.
840 * See @ref evp_cipher for an example how to use this function.
842 * @return 1 on success.
844 * @ingroup hcrypto_evp
848 EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, void *out, int *outlen,
849 void *in, size_t inlen)
851 int ret, left, blocksize;
853 *outlen = 0;
856 * If there in no spare bytes in the left from last Update and the
857 * input length is on the block boundery, the EVP_CipherUpdate()
858 * function can take a shortcut (and preformance gain) and
859 * directly encrypt the data, otherwise we hav to fix it up and
860 * store extra it the EVP_CIPHER_CTX.
862 if (ctx->buf_len == 0 && (inlen & ctx->block_mask) == 0) {
863 ret = (*ctx->cipher->do_cipher)(ctx, out, in, inlen);
864 if (ret == 1)
865 *outlen = inlen;
866 else
867 *outlen = 0;
868 return ret;
872 blocksize = EVP_CIPHER_CTX_block_size(ctx);
873 left = blocksize - ctx->buf_len;
874 assert(left > 0);
876 if (ctx->buf_len) {
878 /* if total buffer is smaller then input, store locally */
879 if (inlen < left) {
880 memcpy(ctx->buf + ctx->buf_len, in, inlen);
881 ctx->buf_len += inlen;
882 return 1;
885 /* fill in local buffer and encrypt */
886 memcpy(ctx->buf + ctx->buf_len, in, left);
887 ret = (*ctx->cipher->do_cipher)(ctx, out, ctx->buf, blocksize);
888 memset(ctx->buf, 0, blocksize);
889 if (ret != 1)
890 return ret;
892 *outlen += blocksize;
893 inlen -= left;
894 in = ((unsigned char *)in) + left;
895 out = ((unsigned char *)out) + blocksize;
896 ctx->buf_len = 0;
899 if (inlen) {
900 ctx->buf_len = (inlen & ctx->block_mask);
901 inlen &= ~ctx->block_mask;
903 ret = (*ctx->cipher->do_cipher)(ctx, out, in, inlen);
904 if (ret != 1)
905 return ret;
907 *outlen += inlen;
909 in = ((unsigned char *)in) + inlen;
910 memcpy(ctx->buf, in, ctx->buf_len);
913 return 1;
917 * Encipher/decipher final data
919 * @param ctx the cipher context.
920 * @param out output data from the operation.
921 * @param outlen output length
923 * The input length needs to be at least EVP_CIPHER_block_size() bytes
924 * long.
926 * See @ref evp_cipher for an example how to use this function.
928 * @return 1 on success.
930 * @ingroup hcrypto_evp
934 EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, void *out, int *outlen)
936 *outlen = 0;
938 if (ctx->buf_len) {
939 int ret, left, blocksize;
941 blocksize = EVP_CIPHER_CTX_block_size(ctx);
943 left = blocksize - ctx->buf_len;
944 assert(left > 0);
946 /* zero fill local buffer */
947 memset(ctx->buf + ctx->buf_len, 0, left);
948 ret = (*ctx->cipher->do_cipher)(ctx, out, ctx->buf, blocksize);
949 memset(ctx->buf, 0, blocksize);
950 if (ret != 1)
951 return ret;
953 *outlen += blocksize;
956 return 1;
960 * Encipher/decipher data
962 * @param ctx the cipher context.
963 * @param out out data from the operation.
964 * @param in in data to the operation.
965 * @param size length of data.
967 * @return 1 on success.
971 EVP_Cipher(EVP_CIPHER_CTX *ctx, void *out, const void *in,size_t size)
973 return ctx->cipher->do_cipher(ctx, out, in, size);
980 static int
981 enc_null_init(EVP_CIPHER_CTX *ctx,
982 const unsigned char * key,
983 const unsigned char * iv,
984 int encp)
986 return 1;
989 static int
990 enc_null_do_cipher(EVP_CIPHER_CTX *ctx,
991 unsigned char *out,
992 const unsigned char *in,
993 unsigned int size)
995 memmove(out, in, size);
996 return 1;
999 static int
1000 enc_null_cleanup(EVP_CIPHER_CTX *ctx)
1002 return 1;
1006 * The NULL cipher type, does no encryption/decryption.
1008 * @return the null EVP_CIPHER pointer.
1010 * @ingroup hcrypto_evp
1013 const EVP_CIPHER *
1014 EVP_enc_null(void)
1016 static const EVP_CIPHER enc_null = {
1021 EVP_CIPH_CBC_MODE,
1022 enc_null_init,
1023 enc_null_do_cipher,
1024 enc_null_cleanup,
1026 NULL,
1027 NULL,
1028 NULL,
1029 NULL
1031 return &enc_null;
1035 * The RC2 cipher type
1037 * @return the RC2 EVP_CIPHER pointer.
1039 * @ingroup hcrypto_evp
1042 const EVP_CIPHER *
1043 EVP_rc2_cbc(void)
1045 hcrypto_validate();
1046 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_cbc);
1050 * The RC2 cipher type
1052 * @return the RC2 EVP_CIPHER pointer.
1054 * @ingroup hcrypto_evp
1057 const EVP_CIPHER *
1058 EVP_rc2_40_cbc(void)
1060 hcrypto_validate();
1061 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_40_cbc);
1065 * The RC2 cipher type
1067 * @return the RC2 EVP_CIPHER pointer.
1069 * @ingroup hcrypto_evp
1072 const EVP_CIPHER *
1073 EVP_rc2_64_cbc(void)
1075 hcrypto_validate();
1076 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_64_cbc);
1080 * The RC4 cipher type
1082 * @return the RC4 EVP_CIPHER pointer.
1084 * @ingroup hcrypto_evp
1087 const EVP_CIPHER *
1088 EVP_rc4(void)
1090 hcrypto_validate();
1091 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc4);
1095 * The RC4-40 cipher type
1097 * @return the RC4-40 EVP_CIPHER pointer.
1099 * @ingroup hcrypto_evp
1102 const EVP_CIPHER *
1103 EVP_rc4_40(void)
1105 hcrypto_validate();
1106 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc4_40);
1110 * The DES cipher type
1112 * @return the DES-CBC EVP_CIPHER pointer.
1114 * @ingroup hcrypto_evp
1117 const EVP_CIPHER *
1118 EVP_des_cbc(void)
1120 hcrypto_validate();
1121 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, des_cbc);
1125 * The tripple DES cipher type
1127 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
1129 * @ingroup hcrypto_evp
1132 const EVP_CIPHER *
1133 EVP_des_ede3_cbc(void)
1135 hcrypto_validate();
1136 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, des_ede3_cbc);
1140 * The AES-128 cipher type
1142 * @return the AES-128 EVP_CIPHER pointer.
1144 * @ingroup hcrypto_evp
1147 const EVP_CIPHER *
1148 EVP_aes_128_cbc(void)
1150 hcrypto_validate();
1151 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_128_cbc);
1155 * The AES-192 cipher type
1157 * @return the AES-192 EVP_CIPHER pointer.
1159 * @ingroup hcrypto_evp
1162 const EVP_CIPHER *
1163 EVP_aes_192_cbc(void)
1165 hcrypto_validate();
1166 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_192_cbc);
1170 * The AES-256 cipher type
1172 * @return the AES-256 EVP_CIPHER pointer.
1174 * @ingroup hcrypto_evp
1177 const EVP_CIPHER *
1178 EVP_aes_256_cbc(void)
1180 hcrypto_validate();
1181 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_256_cbc);
1185 * The AES-128 cipher type
1187 * @return the AES-128 EVP_CIPHER pointer.
1189 * @ingroup hcrypto_evp
1192 const EVP_CIPHER *
1193 EVP_aes_128_cfb8(void)
1195 hcrypto_validate();
1196 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_128_cfb8);
1200 * The AES-192 cipher type
1202 * @return the AES-192 EVP_CIPHER pointer.
1204 * @ingroup hcrypto_evp
1207 const EVP_CIPHER *
1208 EVP_aes_192_cfb8(void)
1210 hcrypto_validate();
1211 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_192_cfb8);
1215 * The AES-256 cipher type
1217 * @return the AES-256 EVP_CIPHER pointer.
1219 * @ingroup hcrypto_evp
1222 const EVP_CIPHER *
1223 EVP_aes_256_cfb8(void)
1225 hcrypto_validate();
1226 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_256_cfb8);
1230 * The Camellia-128 cipher type
1232 * @return the Camellia-128 EVP_CIPHER pointer.
1234 * @ingroup hcrypto_evp
1237 const EVP_CIPHER *
1238 EVP_camellia_128_cbc(void)
1240 hcrypto_validate();
1241 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_128_cbc);
1245 * The Camellia-198 cipher type
1247 * @return the Camellia-198 EVP_CIPHER pointer.
1249 * @ingroup hcrypto_evp
1252 const EVP_CIPHER *
1253 EVP_camellia_192_cbc(void)
1255 hcrypto_validate();
1256 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_192_cbc);
1260 * The Camellia-256 cipher type
1262 * @return the Camellia-256 EVP_CIPHER pointer.
1264 * @ingroup hcrypto_evp
1267 const EVP_CIPHER *
1268 EVP_camellia_256_cbc(void)
1270 hcrypto_validate();
1271 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_256_cbc);
1278 static const struct cipher_name {
1279 const char *name;
1280 const EVP_CIPHER *(*func)(void);
1281 } cipher_name[] = {
1282 { "des-ede3-cbc", EVP_des_ede3_cbc },
1283 { "aes-128-cbc", EVP_aes_128_cbc },
1284 { "aes-192-cbc", EVP_aes_192_cbc },
1285 { "aes-256-cbc", EVP_aes_256_cbc },
1286 { "aes-128-cfb8", EVP_aes_128_cfb8 },
1287 { "aes-192-cfb8", EVP_aes_192_cfb8 },
1288 { "aes-256-cfb8", EVP_aes_256_cfb8 },
1289 { "camellia-128-cbc", EVP_camellia_128_cbc },
1290 { "camellia-192-cbc", EVP_camellia_192_cbc },
1291 { "camellia-256-cbc", EVP_camellia_256_cbc }
1295 * Get the cipher type using their name.
1297 * @param name the name of the cipher.
1299 * @return the selected EVP_CIPHER pointer or NULL if not found.
1301 * @ingroup hcrypto_evp
1304 const EVP_CIPHER *
1305 EVP_get_cipherbyname(const char *name)
1307 int i;
1308 for (i = 0; i < sizeof(cipher_name)/sizeof(cipher_name[0]); i++) {
1309 if (strcasecmp(cipher_name[i].name, name) == 0)
1310 return (*cipher_name[i].func)();
1312 return NULL;
1320 #ifndef min
1321 #define min(a,b) (((a)>(b))?(b):(a))
1322 #endif
1325 * Provides a legancy string to key function, used in PEM files.
1327 * New protocols should use new string to key functions like NIST
1328 * SP56-800A or PKCS#5 v2.0 (see PKCS5_PBKDF2_HMAC_SHA1()).
1330 * @param type type of cipher to use
1331 * @param md message digest to use
1332 * @param salt salt salt string, should be an binary 8 byte buffer.
1333 * @param data the password/input key string.
1334 * @param datalen length of data parameter.
1335 * @param count iteration counter.
1336 * @param keydata output keydata, needs to of the size EVP_CIPHER_key_length().
1337 * @param ivdata output ivdata, needs to of the size EVP_CIPHER_block_size().
1339 * @return the size of derived key.
1341 * @ingroup hcrypto_evp
1345 EVP_BytesToKey(const EVP_CIPHER *type,
1346 const EVP_MD *md,
1347 const void *salt,
1348 const void *data, size_t datalen,
1349 unsigned int count,
1350 void *keydata,
1351 void *ivdata)
1353 unsigned int ivlen, keylen;
1354 int first = 0;
1355 unsigned int mds = 0, i;
1356 unsigned char *key = keydata;
1357 unsigned char *iv = ivdata;
1358 unsigned char *buf;
1359 EVP_MD_CTX c;
1361 keylen = EVP_CIPHER_key_length(type);
1362 ivlen = EVP_CIPHER_iv_length(type);
1364 if (data == NULL)
1365 return keylen;
1367 buf = malloc(EVP_MD_size(md));
1368 if (buf == NULL)
1369 return -1;
1371 EVP_MD_CTX_init(&c);
1373 first = 1;
1374 while (1) {
1375 EVP_DigestInit_ex(&c, md, NULL);
1376 if (!first)
1377 EVP_DigestUpdate(&c, buf, mds);
1378 first = 0;
1379 EVP_DigestUpdate(&c,data,datalen);
1381 #define PKCS5_SALT_LEN 8
1383 if (salt)
1384 EVP_DigestUpdate(&c, salt, PKCS5_SALT_LEN);
1386 EVP_DigestFinal_ex(&c, buf, &mds);
1387 assert(mds == EVP_MD_size(md));
1389 for (i = 1; i < count; i++) {
1390 EVP_DigestInit_ex(&c, md, NULL);
1391 EVP_DigestUpdate(&c, buf, mds);
1392 EVP_DigestFinal_ex(&c, buf, &mds);
1393 assert(mds == EVP_MD_size(md));
1396 i = 0;
1397 if (keylen) {
1398 size_t sz = min(keylen, mds);
1399 if (key) {
1400 memcpy(key, buf, sz);
1401 key += sz;
1403 keylen -= sz;
1404 i += sz;
1406 if (ivlen && mds > i) {
1407 size_t sz = min(ivlen, (mds - i));
1408 if (iv) {
1409 memcpy(iv, &buf[i], sz);
1410 iv += sz;
1412 ivlen -= sz;
1414 if (keylen == 0 && ivlen == 0)
1415 break;
1418 EVP_MD_CTX_cleanup(&c);
1419 free(buf);
1421 return EVP_CIPHER_key_length(type);
1425 * Generate a random key for the specificed EVP_CIPHER.
1427 * @param ctx EVP_CIPHER_CTX type to build the key for.
1428 * @param key return key, must be at least EVP_CIPHER_key_length() byte long.
1430 * @return 1 for success, 0 for failure.
1432 * @ingroup hcrypto_core
1436 EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, void *key)
1438 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
1439 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
1440 if (RAND_bytes(key, ctx->key_len) != 1)
1441 return 0;
1442 return 1;
1446 * Perform a operation on a ctx
1448 * @param ctx context to perform operation on.
1449 * @param type type of operation.
1450 * @param arg argument to operation.
1451 * @param data addition data to operation.
1453 * @return 1 for success, 0 for failure.
1455 * @ingroup hcrypto_core
1459 EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *data)
1461 if (ctx->cipher == NULL || ctx->cipher->ctrl == NULL)
1462 return 0;
1463 return (*ctx->cipher->ctrl)(ctx, type, arg, data);
1467 * Add all algorithms to the crypto core.
1469 * @ingroup hcrypto_core
1472 void
1473 OpenSSL_add_all_algorithms(void)
1475 return;
1479 * Add all algorithms to the crypto core using configuration file.
1481 * @ingroup hcrypto_core
1484 void
1485 OpenSSL_add_all_algorithms_conf(void)
1487 return;
1491 * Add all algorithms to the crypto core, but don't use the
1492 * configuration file.
1494 * @ingroup hcrypto_core
1497 void
1498 OpenSSL_add_all_algorithms_noconf(void)
1500 return;