s3:include: change smb_request->vuid to uint64_t
[Samba/gebeck_regimport.git] / source4 / heimdal / lib / hcrypto / evp.c
blob75eefc49312e3acbf241e2eb591939280c9239bc
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 SHA384
366 * @return the message digest type.
368 * @ingroup hcrypto_evp
371 const EVP_MD *
372 EVP_sha384(void)
374 hcrypto_validate();
375 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha384);
379 * The message digest SHA512
381 * @return the message digest type.
383 * @ingroup hcrypto_evp
386 const EVP_MD *
387 EVP_sha512(void)
389 hcrypto_validate();
390 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha512);
394 * The message digest SHA1
396 * @return the message digest type.
398 * @ingroup hcrypto_evp
401 const EVP_MD *
402 EVP_sha1(void)
404 hcrypto_validate();
405 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha1);
409 * The message digest SHA1
411 * @return the message digest type.
413 * @ingroup hcrypto_evp
416 const EVP_MD *
417 EVP_sha(void) HC_DEPRECATED
420 hcrypto_validate();
421 return EVP_sha1();
425 * The message digest MD5
427 * @return the message digest type.
429 * @ingroup hcrypto_evp
432 const EVP_MD *
433 EVP_md5(void) HC_DEPRECATED_CRYPTO
435 hcrypto_validate();
436 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, md5);
440 * The message digest MD4
442 * @return the message digest type.
444 * @ingroup hcrypto_evp
447 const EVP_MD *
448 EVP_md4(void) HC_DEPRECATED_CRYPTO
450 hcrypto_validate();
451 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, md4);
455 * The message digest MD2
457 * @return the message digest type.
459 * @ingroup hcrypto_evp
462 const EVP_MD *
463 EVP_md2(void) HC_DEPRECATED_CRYPTO
465 hcrypto_validate();
466 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, md2);
473 static void
474 null_Init (void *m)
477 static void
478 null_Update (void *m, const void * data, size_t size)
481 static void
482 null_Final(void *res, void *m)
487 * The null message digest
489 * @return the message digest type.
491 * @ingroup hcrypto_evp
494 const EVP_MD *
495 EVP_md_null(void)
497 static const struct hc_evp_md null = {
501 (hc_evp_md_init)null_Init,
502 (hc_evp_md_update)null_Update,
503 (hc_evp_md_final)null_Final,
504 NULL
506 return &null;
510 * Return the block size of the cipher.
512 * @param c cipher to get the block size from.
514 * @return the block size of the cipher.
516 * @ingroup hcrypto_evp
519 size_t
520 EVP_CIPHER_block_size(const EVP_CIPHER *c)
522 return c->block_size;
526 * Return the key size of the cipher.
528 * @param c cipher to get the key size from.
530 * @return the key size of the cipher.
532 * @ingroup hcrypto_evp
535 size_t
536 EVP_CIPHER_key_length(const EVP_CIPHER *c)
538 return c->key_len;
542 * Return the IV size of the cipher.
544 * @param c cipher to get the IV size from.
546 * @return the IV size of the cipher.
548 * @ingroup hcrypto_evp
551 size_t
552 EVP_CIPHER_iv_length(const EVP_CIPHER *c)
554 return c->iv_len;
558 * Initiate a EVP_CIPHER_CTX context. Clean up with
559 * EVP_CIPHER_CTX_cleanup().
561 * @param c the cipher initiate.
563 * @ingroup hcrypto_evp
566 void
567 EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *c)
569 memset(c, 0, sizeof(*c));
573 * Clean up the EVP_CIPHER_CTX context.
575 * @param c the cipher to clean up.
577 * @return 1 on success.
579 * @ingroup hcrypto_evp
583 EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
585 if (c->cipher && c->cipher->cleanup)
586 c->cipher->cleanup(c);
587 if (c->cipher_data) {
588 memset(c->cipher_data, 0, c->cipher->ctx_size);
589 free(c->cipher_data);
590 c->cipher_data = NULL;
592 return 1;
596 * If the cipher type supports it, change the key length
598 * @param c the cipher context to change the key length for
599 * @param length new key length
601 * @return 1 on success.
603 * @ingroup hcrypto_evp
607 EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int length)
609 if ((c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH) && length > 0) {
610 c->key_len = length;
611 return 1;
613 return 0;
616 #if 0
618 EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *c, int pad)
620 return 0;
622 #endif
625 * Return the EVP_CIPHER for a EVP_CIPHER_CTX context.
627 * @param ctx the context to get the cipher type from.
629 * @return the EVP_CIPHER pointer.
631 * @ingroup hcrypto_evp
634 const EVP_CIPHER *
635 EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX *ctx)
637 return ctx->cipher;
641 * Return the block size of the cipher context.
643 * @param ctx cipher context to get the block size from.
645 * @return the block size of the cipher context.
647 * @ingroup hcrypto_evp
650 size_t
651 EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
653 return EVP_CIPHER_block_size(ctx->cipher);
657 * Return the key size of the cipher context.
659 * @param ctx cipher context to get the key size from.
661 * @return the key size of the cipher context.
663 * @ingroup hcrypto_evp
666 size_t
667 EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
669 return EVP_CIPHER_key_length(ctx->cipher);
673 * Return the IV size of the cipher context.
675 * @param ctx cipher context to get the IV size from.
677 * @return the IV size of the cipher context.
679 * @ingroup hcrypto_evp
682 size_t
683 EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
685 return EVP_CIPHER_iv_length(ctx->cipher);
689 * Get the flags for an EVP_CIPHER_CTX context.
691 * @param ctx the EVP_CIPHER_CTX to get the flags from
693 * @return the flags for an EVP_CIPHER_CTX.
695 * @ingroup hcrypto_evp
698 unsigned long
699 EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx)
701 return ctx->cipher->flags;
705 * Get the mode for an EVP_CIPHER_CTX context.
707 * @param ctx the EVP_CIPHER_CTX to get the mode from
709 * @return the mode for an EVP_CIPHER_CTX.
711 * @ingroup hcrypto_evp
715 EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx)
717 return EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_MODE;
721 * Get the app data for an EVP_CIPHER_CTX context.
723 * @param ctx the EVP_CIPHER_CTX to get the app data from
725 * @return the app data for an EVP_CIPHER_CTX.
727 * @ingroup hcrypto_evp
730 void *
731 EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX *ctx)
733 return ctx->app_data;
737 * Set the app data for an EVP_CIPHER_CTX context.
739 * @param ctx the EVP_CIPHER_CTX to set the app data for
740 * @param data the app data to set for an EVP_CIPHER_CTX.
742 * @ingroup hcrypto_evp
745 void
746 EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
748 ctx->app_data = data;
752 * Initiate the EVP_CIPHER_CTX context to encrypt or decrypt data.
753 * Clean up with EVP_CIPHER_CTX_cleanup().
755 * @param ctx context to initiate
756 * @param c cipher to use.
757 * @param engine crypto engine to use, NULL to select default.
758 * @param key the crypto key to use, NULL will use the previous value.
759 * @param iv the IV to use, NULL will use the previous value.
760 * @param encp non zero will encrypt, -1 use the previous value.
762 * @return 1 on success.
764 * @ingroup hcrypto_evp
768 EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *c, ENGINE *engine,
769 const void *key, const void *iv, int encp)
771 ctx->buf_len = 0;
773 if (encp == -1)
774 encp = ctx->encrypt;
775 else
776 ctx->encrypt = (encp ? 1 : 0);
778 if (c && (c != ctx->cipher)) {
779 EVP_CIPHER_CTX_cleanup(ctx);
780 ctx->cipher = c;
781 ctx->key_len = c->key_len;
783 ctx->cipher_data = calloc(1, c->ctx_size);
784 if (ctx->cipher_data == NULL && c->ctx_size != 0)
785 return 0;
787 /* assume block size is a multiple of 2 */
788 ctx->block_mask = EVP_CIPHER_block_size(c) - 1;
790 } else if (ctx->cipher == NULL) {
791 /* reuse of cipher, but not any cipher ever set! */
792 return 0;
795 switch (EVP_CIPHER_CTX_mode(ctx)) {
796 case EVP_CIPH_CBC_MODE:
798 assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv));
800 if (iv)
801 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
802 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
803 break;
805 case EVP_CIPH_STREAM_CIPHER:
806 break;
807 case EVP_CIPH_CFB8_MODE:
808 if (iv)
809 memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
810 break;
812 default:
813 return 0;
816 if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT))
817 ctx->cipher->init(ctx, key, iv, encp);
819 return 1;
823 * Encipher/decipher partial data
825 * @param ctx the cipher context.
826 * @param out output data from the operation.
827 * @param outlen output length
828 * @param in input data to the operation.
829 * @param inlen length of data.
831 * The output buffer length should at least be EVP_CIPHER_block_size()
832 * byte longer then the input length.
834 * See @ref evp_cipher for an example how to use this function.
836 * @return 1 on success.
838 * @ingroup hcrypto_evp
842 EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, void *out, int *outlen,
843 void *in, size_t inlen)
845 int ret, left, blocksize;
847 *outlen = 0;
850 * If there in no spare bytes in the left from last Update and the
851 * input length is on the block boundery, the EVP_CipherUpdate()
852 * function can take a shortcut (and preformance gain) and
853 * directly encrypt the data, otherwise we hav to fix it up and
854 * store extra it the EVP_CIPHER_CTX.
856 if (ctx->buf_len == 0 && (inlen & ctx->block_mask) == 0) {
857 ret = (*ctx->cipher->do_cipher)(ctx, out, in, inlen);
858 if (ret == 1)
859 *outlen = inlen;
860 else
861 *outlen = 0;
862 return ret;
866 blocksize = EVP_CIPHER_CTX_block_size(ctx);
867 left = blocksize - ctx->buf_len;
868 assert(left > 0);
870 if (ctx->buf_len) {
872 /* if total buffer is smaller then input, store locally */
873 if (inlen < left) {
874 memcpy(ctx->buf + ctx->buf_len, in, inlen);
875 ctx->buf_len += inlen;
876 return 1;
879 /* fill in local buffer and encrypt */
880 memcpy(ctx->buf + ctx->buf_len, in, left);
881 ret = (*ctx->cipher->do_cipher)(ctx, out, ctx->buf, blocksize);
882 memset(ctx->buf, 0, blocksize);
883 if (ret != 1)
884 return ret;
886 *outlen += blocksize;
887 inlen -= left;
888 in = ((unsigned char *)in) + left;
889 out = ((unsigned char *)out) + blocksize;
890 ctx->buf_len = 0;
893 if (inlen) {
894 ctx->buf_len = (inlen & ctx->block_mask);
895 inlen &= ~ctx->block_mask;
897 ret = (*ctx->cipher->do_cipher)(ctx, out, in, inlen);
898 if (ret != 1)
899 return ret;
901 *outlen += inlen;
903 in = ((unsigned char *)in) + inlen;
904 memcpy(ctx->buf, in, ctx->buf_len);
907 return 1;
911 * Encipher/decipher final data
913 * @param ctx the cipher context.
914 * @param out output data from the operation.
915 * @param outlen output length
917 * The input length needs to be at least EVP_CIPHER_block_size() bytes
918 * long.
920 * See @ref evp_cipher for an example how to use this function.
922 * @return 1 on success.
924 * @ingroup hcrypto_evp
928 EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, void *out, int *outlen)
930 *outlen = 0;
932 if (ctx->buf_len) {
933 int ret, left, blocksize;
935 blocksize = EVP_CIPHER_CTX_block_size(ctx);
937 left = blocksize - ctx->buf_len;
938 assert(left > 0);
940 /* zero fill local buffer */
941 memset(ctx->buf + ctx->buf_len, 0, left);
942 ret = (*ctx->cipher->do_cipher)(ctx, out, ctx->buf, blocksize);
943 memset(ctx->buf, 0, blocksize);
944 if (ret != 1)
945 return ret;
947 *outlen += blocksize;
950 return 1;
954 * Encipher/decipher data
956 * @param ctx the cipher context.
957 * @param out out data from the operation.
958 * @param in in data to the operation.
959 * @param size length of data.
961 * @return 1 on success.
965 EVP_Cipher(EVP_CIPHER_CTX *ctx, void *out, const void *in,size_t size)
967 return ctx->cipher->do_cipher(ctx, out, in, size);
974 static int
975 enc_null_init(EVP_CIPHER_CTX *ctx,
976 const unsigned char * key,
977 const unsigned char * iv,
978 int encp)
980 return 1;
983 static int
984 enc_null_do_cipher(EVP_CIPHER_CTX *ctx,
985 unsigned char *out,
986 const unsigned char *in,
987 unsigned int size)
989 memmove(out, in, size);
990 return 1;
993 static int
994 enc_null_cleanup(EVP_CIPHER_CTX *ctx)
996 return 1;
1000 * The NULL cipher type, does no encryption/decryption.
1002 * @return the null EVP_CIPHER pointer.
1004 * @ingroup hcrypto_evp
1007 const EVP_CIPHER *
1008 EVP_enc_null(void)
1010 static const EVP_CIPHER enc_null = {
1015 EVP_CIPH_CBC_MODE,
1016 enc_null_init,
1017 enc_null_do_cipher,
1018 enc_null_cleanup,
1020 NULL,
1021 NULL,
1022 NULL,
1023 NULL
1025 return &enc_null;
1029 * The RC2 cipher type
1031 * @return the RC2 EVP_CIPHER pointer.
1033 * @ingroup hcrypto_evp
1036 const EVP_CIPHER *
1037 EVP_rc2_cbc(void)
1039 hcrypto_validate();
1040 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_cbc);
1044 * The RC2 cipher type
1046 * @return the RC2 EVP_CIPHER pointer.
1048 * @ingroup hcrypto_evp
1051 const EVP_CIPHER *
1052 EVP_rc2_40_cbc(void)
1054 hcrypto_validate();
1055 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_40_cbc);
1059 * The RC2 cipher type
1061 * @return the RC2 EVP_CIPHER pointer.
1063 * @ingroup hcrypto_evp
1066 const EVP_CIPHER *
1067 EVP_rc2_64_cbc(void)
1069 hcrypto_validate();
1070 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_64_cbc);
1074 * The RC4 cipher type
1076 * @return the RC4 EVP_CIPHER pointer.
1078 * @ingroup hcrypto_evp
1081 const EVP_CIPHER *
1082 EVP_rc4(void)
1084 hcrypto_validate();
1085 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc4);
1089 * The RC4-40 cipher type
1091 * @return the RC4-40 EVP_CIPHER pointer.
1093 * @ingroup hcrypto_evp
1096 const EVP_CIPHER *
1097 EVP_rc4_40(void)
1099 hcrypto_validate();
1100 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc4_40);
1104 * The DES cipher type
1106 * @return the DES-CBC EVP_CIPHER pointer.
1108 * @ingroup hcrypto_evp
1111 const EVP_CIPHER *
1112 EVP_des_cbc(void)
1114 hcrypto_validate();
1115 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, des_cbc);
1119 * The tripple DES cipher type
1121 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
1123 * @ingroup hcrypto_evp
1126 const EVP_CIPHER *
1127 EVP_des_ede3_cbc(void)
1129 hcrypto_validate();
1130 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, des_ede3_cbc);
1134 * The AES-128 cipher type
1136 * @return the AES-128 EVP_CIPHER pointer.
1138 * @ingroup hcrypto_evp
1141 const EVP_CIPHER *
1142 EVP_aes_128_cbc(void)
1144 hcrypto_validate();
1145 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_128_cbc);
1149 * The AES-192 cipher type
1151 * @return the AES-192 EVP_CIPHER pointer.
1153 * @ingroup hcrypto_evp
1156 const EVP_CIPHER *
1157 EVP_aes_192_cbc(void)
1159 hcrypto_validate();
1160 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_192_cbc);
1164 * The AES-256 cipher type
1166 * @return the AES-256 EVP_CIPHER pointer.
1168 * @ingroup hcrypto_evp
1171 const EVP_CIPHER *
1172 EVP_aes_256_cbc(void)
1174 hcrypto_validate();
1175 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_256_cbc);
1179 * The AES-128 cipher type
1181 * @return the AES-128 EVP_CIPHER pointer.
1183 * @ingroup hcrypto_evp
1186 const EVP_CIPHER *
1187 EVP_aes_128_cfb8(void)
1189 hcrypto_validate();
1190 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_128_cfb8);
1194 * The AES-192 cipher type
1196 * @return the AES-192 EVP_CIPHER pointer.
1198 * @ingroup hcrypto_evp
1201 const EVP_CIPHER *
1202 EVP_aes_192_cfb8(void)
1204 hcrypto_validate();
1205 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_192_cfb8);
1209 * The AES-256 cipher type
1211 * @return the AES-256 EVP_CIPHER pointer.
1213 * @ingroup hcrypto_evp
1216 const EVP_CIPHER *
1217 EVP_aes_256_cfb8(void)
1219 hcrypto_validate();
1220 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_256_cfb8);
1224 * The Camellia-128 cipher type
1226 * @return the Camellia-128 EVP_CIPHER pointer.
1228 * @ingroup hcrypto_evp
1231 const EVP_CIPHER *
1232 EVP_camellia_128_cbc(void)
1234 hcrypto_validate();
1235 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_128_cbc);
1239 * The Camellia-198 cipher type
1241 * @return the Camellia-198 EVP_CIPHER pointer.
1243 * @ingroup hcrypto_evp
1246 const EVP_CIPHER *
1247 EVP_camellia_192_cbc(void)
1249 hcrypto_validate();
1250 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_192_cbc);
1254 * The Camellia-256 cipher type
1256 * @return the Camellia-256 EVP_CIPHER pointer.
1258 * @ingroup hcrypto_evp
1261 const EVP_CIPHER *
1262 EVP_camellia_256_cbc(void)
1264 hcrypto_validate();
1265 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_256_cbc);
1272 static const struct cipher_name {
1273 const char *name;
1274 const EVP_CIPHER *(*func)(void);
1275 } cipher_name[] = {
1276 { "des-ede3-cbc", EVP_des_ede3_cbc },
1277 { "aes-128-cbc", EVP_aes_128_cbc },
1278 { "aes-192-cbc", EVP_aes_192_cbc },
1279 { "aes-256-cbc", EVP_aes_256_cbc },
1280 { "aes-128-cfb8", EVP_aes_128_cfb8 },
1281 { "aes-192-cfb8", EVP_aes_192_cfb8 },
1282 { "aes-256-cfb8", EVP_aes_256_cfb8 },
1283 { "camellia-128-cbc", EVP_camellia_128_cbc },
1284 { "camellia-192-cbc", EVP_camellia_192_cbc },
1285 { "camellia-256-cbc", EVP_camellia_256_cbc }
1289 * Get the cipher type using their name.
1291 * @param name the name of the cipher.
1293 * @return the selected EVP_CIPHER pointer or NULL if not found.
1295 * @ingroup hcrypto_evp
1298 const EVP_CIPHER *
1299 EVP_get_cipherbyname(const char *name)
1301 int i;
1302 for (i = 0; i < sizeof(cipher_name)/sizeof(cipher_name[0]); i++) {
1303 if (strcasecmp(cipher_name[i].name, name) == 0)
1304 return (*cipher_name[i].func)();
1306 return NULL;
1314 #ifndef min
1315 #define min(a,b) (((a)>(b))?(b):(a))
1316 #endif
1319 * Provides a legancy string to key function, used in PEM files.
1321 * New protocols should use new string to key functions like NIST
1322 * SP56-800A or PKCS#5 v2.0 (see PKCS5_PBKDF2_HMAC_SHA1()).
1324 * @param type type of cipher to use
1325 * @param md message digest to use
1326 * @param salt salt salt string, should be an binary 8 byte buffer.
1327 * @param data the password/input key string.
1328 * @param datalen length of data parameter.
1329 * @param count iteration counter.
1330 * @param keydata output keydata, needs to of the size EVP_CIPHER_key_length().
1331 * @param ivdata output ivdata, needs to of the size EVP_CIPHER_block_size().
1333 * @return the size of derived key.
1335 * @ingroup hcrypto_evp
1339 EVP_BytesToKey(const EVP_CIPHER *type,
1340 const EVP_MD *md,
1341 const void *salt,
1342 const void *data, size_t datalen,
1343 unsigned int count,
1344 void *keydata,
1345 void *ivdata)
1347 unsigned int ivlen, keylen;
1348 int first = 0;
1349 unsigned int mds = 0, i;
1350 unsigned char *key = keydata;
1351 unsigned char *iv = ivdata;
1352 unsigned char *buf;
1353 EVP_MD_CTX c;
1355 keylen = EVP_CIPHER_key_length(type);
1356 ivlen = EVP_CIPHER_iv_length(type);
1358 if (data == NULL)
1359 return keylen;
1361 buf = malloc(EVP_MD_size(md));
1362 if (buf == NULL)
1363 return -1;
1365 EVP_MD_CTX_init(&c);
1367 first = 1;
1368 while (1) {
1369 EVP_DigestInit_ex(&c, md, NULL);
1370 if (!first)
1371 EVP_DigestUpdate(&c, buf, mds);
1372 first = 0;
1373 EVP_DigestUpdate(&c,data,datalen);
1375 #define PKCS5_SALT_LEN 8
1377 if (salt)
1378 EVP_DigestUpdate(&c, salt, PKCS5_SALT_LEN);
1380 EVP_DigestFinal_ex(&c, buf, &mds);
1381 assert(mds == EVP_MD_size(md));
1383 for (i = 1; i < count; i++) {
1384 EVP_DigestInit_ex(&c, md, NULL);
1385 EVP_DigestUpdate(&c, buf, mds);
1386 EVP_DigestFinal_ex(&c, buf, &mds);
1387 assert(mds == EVP_MD_size(md));
1390 i = 0;
1391 if (keylen) {
1392 size_t sz = min(keylen, mds);
1393 if (key) {
1394 memcpy(key, buf, sz);
1395 key += sz;
1397 keylen -= sz;
1398 i += sz;
1400 if (ivlen && mds > i) {
1401 size_t sz = min(ivlen, (mds - i));
1402 if (iv) {
1403 memcpy(iv, &buf[i], sz);
1404 iv += sz;
1406 ivlen -= sz;
1408 if (keylen == 0 && ivlen == 0)
1409 break;
1412 EVP_MD_CTX_cleanup(&c);
1413 free(buf);
1415 return EVP_CIPHER_key_length(type);
1419 * Generate a random key for the specificed EVP_CIPHER.
1421 * @param ctx EVP_CIPHER_CTX type to build the key for.
1422 * @param key return key, must be at least EVP_CIPHER_key_length() byte long.
1424 * @return 1 for success, 0 for failure.
1426 * @ingroup hcrypto_core
1430 EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, void *key)
1432 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
1433 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
1434 if (RAND_bytes(key, ctx->key_len) != 1)
1435 return 0;
1436 return 1;
1440 * Perform a operation on a ctx
1442 * @param ctx context to perform operation on.
1443 * @param type type of operation.
1444 * @param arg argument to operation.
1445 * @param data addition data to operation.
1447 * @return 1 for success, 0 for failure.
1449 * @ingroup hcrypto_core
1453 EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *data)
1455 if (ctx->cipher == NULL || ctx->cipher->ctrl == NULL)
1456 return 0;
1457 return (*ctx->cipher->ctrl)(ctx, type, arg, data);
1461 * Add all algorithms to the crypto core.
1463 * @ingroup hcrypto_core
1466 void
1467 OpenSSL_add_all_algorithms(void)
1469 return;
1473 * Add all algorithms to the crypto core using configuration file.
1475 * @ingroup hcrypto_core
1478 void
1479 OpenSSL_add_all_algorithms_conf(void)
1481 return;
1485 * Add all algorithms to the crypto core, but don't use the
1486 * configuration file.
1488 * @ingroup hcrypto_core
1491 void
1492 OpenSSL_add_all_algorithms_noconf(void)
1494 return;