2 * Copyright (c) 2006 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
39 #define HC_DEPRECATED_CRYPTO
41 #include <sys/types.h>
48 #include <evp-hcrypto.h>
51 #include <krb5-types.h>
54 #ifndef HCRYPTO_DEF_PROVIDER
55 #define HCRYPTO_DEF_PROVIDER hcrypto
58 #define HC_CONCAT4(x,y,z,aa) x ## y ## z ## aa
61 #define EVP_DEF_OP(_prov,_op) HC_CONCAT4(EVP_,_prov,_,_op)()
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
{
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
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
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
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
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
159 EVP_MD_CTX_destroy(EVP_MD_CTX
*ctx
)
161 EVP_MD_CTX_cleanup(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
);
181 memset(ctx
->ptr
, 0, ctx
->md
->ctx_size
);
185 memset(ctx
, 0, sizeof(*ctx
));
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
200 EVP_MD_CTX_md(EVP_MD_CTX
*ctx
)
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
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
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
);
255 ctx
->engine
= engine
;
257 ctx
->ptr
= calloc(1, md
->ctx_size
);
258 if (ctx
->ptr
== NULL
)
261 (ctx
->md
->init
)(ctx
->ptr
);
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
);
285 * Complete the message digest.
287 * @param ctx the context to complete.
288 * @param hash the output of the message digest function. At least
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
);
302 *size
= ctx
->md
->hash_size
;
307 * Do the whole EVP_MD_CTX_create(), EVP_DigestInit_ex(),
308 * EVP_DigestUpdate(), EVP_DigestFinal_ex(), EVP_MD_CTX_destroy()
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
)
330 ctx
= EVP_MD_CTX_create();
333 ret
= EVP_DigestInit_ex(ctx
, md
, engine
);
335 EVP_MD_CTX_destroy(ctx
);
338 ret
= EVP_DigestUpdate(ctx
, data
, dsize
);
340 EVP_MD_CTX_destroy(ctx
);
343 ret
= EVP_DigestFinal_ex(ctx
, hash
, hsize
);
344 EVP_MD_CTX_destroy(ctx
);
349 * The message digest SHA256
351 * @return the message digest type.
353 * @ingroup hcrypto_evp
360 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, sha256
);
364 * The message digest SHA1
366 * @return the message digest type.
368 * @ingroup hcrypto_evp
375 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, sha1
);
379 * The message digest SHA1
381 * @return the message digest type.
383 * @ingroup hcrypto_evp
387 EVP_sha(void) HC_DEPRECATED
395 * The message digest MD5
397 * @return the message digest type.
399 * @ingroup hcrypto_evp
403 EVP_md5(void) HC_DEPRECATED_CRYPTO
406 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, md5
);
410 * The message digest MD4
412 * @return the message digest type.
414 * @ingroup hcrypto_evp
418 EVP_md4(void) HC_DEPRECATED_CRYPTO
421 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, md4
);
425 * The message digest MD2
427 * @return the message digest type.
429 * @ingroup hcrypto_evp
433 EVP_md2(void) HC_DEPRECATED_CRYPTO
436 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, md2
);
448 null_Update (void *m
, const void * data
, size_t size
)
452 null_Final(void *res
, void *m
)
457 * The null message digest
459 * @return the message digest type.
461 * @ingroup hcrypto_evp
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
,
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
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
506 EVP_CIPHER_key_length(const EVP_CIPHER
*c
)
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
522 EVP_CIPHER_iv_length(const EVP_CIPHER
*c
)
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
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
;
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) {
588 EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX
*c
, int pad
)
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
605 EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX
*ctx
)
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
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
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
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
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
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
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
)
746 ctx
->encrypt
= (encp
? 1 : 0);
748 if (c
&& (c
!= ctx
->cipher
)) {
749 EVP_CIPHER_CTX_cleanup(ctx
);
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)
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! */
765 switch (EVP_CIPHER_CTX_mode(ctx
)) {
766 case EVP_CIPH_CBC_MODE
:
768 assert(EVP_CIPHER_CTX_iv_length(ctx
) <= sizeof(ctx
->iv
));
771 memcpy(ctx
->oiv
, iv
, EVP_CIPHER_CTX_iv_length(ctx
));
772 memcpy(ctx
->iv
, ctx
->oiv
, EVP_CIPHER_CTX_iv_length(ctx
));
775 case EVP_CIPH_STREAM_CIPHER
:
777 case EVP_CIPH_CFB8_MODE
:
779 memcpy(ctx
->iv
, iv
, EVP_CIPHER_CTX_iv_length(ctx
));
786 if (key
|| (ctx
->cipher
->flags
& EVP_CIPH_ALWAYS_CALL_INIT
))
787 ctx
->cipher
->init(ctx
, key
, iv
, encp
);
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
;
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
);
836 blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
837 left
= blocksize
- ctx
->buf_len
;
842 /* if total buffer is smaller then input, store locally */
844 memcpy(ctx
->buf
+ ctx
->buf_len
, in
, inlen
);
845 ctx
->buf_len
+= inlen
;
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
);
856 *outlen
+= blocksize
;
858 in
= ((unsigned char *)in
) + left
;
859 out
= ((unsigned char *)out
) + blocksize
;
864 ctx
->buf_len
= (inlen
& ctx
->block_mask
);
865 inlen
&= ~ctx
->block_mask
;
867 ret
= (*ctx
->cipher
->do_cipher
)(ctx
, out
, in
, inlen
);
873 in
= ((unsigned char *)in
) + inlen
;
874 memcpy(ctx
->buf
, in
, ctx
->buf_len
);
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
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
)
903 int ret
, left
, blocksize
;
905 blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
907 left
= blocksize
- ctx
->buf_len
;
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
);
917 *outlen
+= blocksize
;
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
);
945 enc_null_init(EVP_CIPHER_CTX
*ctx
,
946 const unsigned char * key
,
947 const unsigned char * iv
,
954 enc_null_do_cipher(EVP_CIPHER_CTX
*ctx
,
956 const unsigned char *in
,
959 memmove(out
, in
, size
);
964 enc_null_cleanup(EVP_CIPHER_CTX
*ctx
)
970 * The NULL cipher type, does no encryption/decryption.
972 * @return the null EVP_CIPHER pointer.
974 * @ingroup hcrypto_evp
980 static const EVP_CIPHER enc_null
= {
999 * The RC2 cipher type
1001 * @return the RC2 EVP_CIPHER pointer.
1003 * @ingroup hcrypto_evp
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
1022 EVP_rc2_40_cbc(void)
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
1037 EVP_rc2_64_cbc(void)
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
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
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
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
1097 EVP_des_ede3_cbc(void)
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
1112 EVP_aes_128_cbc(void)
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
1127 EVP_aes_192_cbc(void)
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
1142 EVP_aes_256_cbc(void)
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
1157 EVP_aes_128_cfb8(void)
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
1172 EVP_aes_192_cfb8(void)
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
1187 EVP_aes_256_cfb8(void)
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
1202 EVP_camellia_128_cbc(void)
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
1217 EVP_camellia_192_cbc(void)
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
1232 EVP_camellia_256_cbc(void)
1235 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, camellia_256_cbc
);
1242 static const struct cipher_name
{
1244 const EVP_CIPHER
*(*func
)(void);
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
1269 EVP_get_cipherbyname(const char *name
)
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
)();
1285 #define min(a,b) (((a)>(b))?(b):(a))
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
,
1312 const void *data
, size_t datalen
,
1317 int ivlen
, keylen
, first
= 0;
1318 unsigned int mds
= 0, i
;
1319 unsigned char *key
= keydata
;
1320 unsigned char *iv
= ivdata
;
1324 keylen
= EVP_CIPHER_key_length(type
);
1325 ivlen
= EVP_CIPHER_iv_length(type
);
1330 buf
= malloc(EVP_MD_size(md
));
1334 EVP_MD_CTX_init(&c
);
1338 EVP_DigestInit_ex(&c
, md
, NULL
);
1340 EVP_DigestUpdate(&c
, buf
, mds
);
1342 EVP_DigestUpdate(&c
,data
,datalen
);
1344 #define PKCS5_SALT_LEN 8
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
));
1361 size_t sz
= min(keylen
, mds
);
1363 memcpy(key
, buf
, sz
);
1369 if (ivlen
&& mds
> i
) {
1370 size_t sz
= min(ivlen
, (mds
- i
));
1372 memcpy(iv
, &buf
[i
], sz
);
1377 if (keylen
== 0 && ivlen
== 0)
1381 EVP_MD_CTX_cleanup(&c
);
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)
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
)
1426 return (*ctx
->cipher
->ctrl
)(ctx
, type
, arg
, data
);
1430 * Add all algorithms to the crypto core.
1432 * @ingroup hcrypto_core
1436 OpenSSL_add_all_algorithms(void)
1442 * Add all algorithms to the crypto core using configuration file.
1444 * @ingroup hcrypto_core
1448 OpenSSL_add_all_algorithms_conf(void)
1454 * Add all algorithms to the crypto core, but don't use the
1455 * configuration file.
1457 * @ingroup hcrypto_core
1461 OpenSSL_add_all_algorithms_noconf(void)