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>
52 #include <krb5-types.h>
55 #ifndef HCRYPTO_DEF_PROVIDER
56 #define HCRYPTO_DEF_PROVIDER hcrypto
59 #define HC_CONCAT4(x,y,z,aa) x ## y ## z ## aa
62 #define EVP_DEF_OP(_prov,_op) HC_CONCAT4(EVP_,_prov,_,_op)()
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
{
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
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
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
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
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
160 EVP_MD_CTX_destroy(EVP_MD_CTX
*ctx
)
162 EVP_MD_CTX_cleanup(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
);
183 } else if (ctx
->md
) {
184 memset(ctx
->ptr
, 0, ctx
->md
->ctx_size
);
189 memset(ctx
, 0, sizeof(*ctx
));
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
204 EVP_MD_CTX_md(EVP_MD_CTX
*ctx
)
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
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
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
);
259 ctx
->engine
= engine
;
261 ctx
->ptr
= calloc(1, md
->ctx_size
);
262 if (ctx
->ptr
== NULL
)
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
);
288 * Complete the message digest.
290 * @param ctx the context to complete.
291 * @param hash the output of the message digest function. At least
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
);
305 *size
= ctx
->md
->hash_size
;
310 * Do the whole EVP_MD_CTX_create(), EVP_DigestInit_ex(),
311 * EVP_DigestUpdate(), EVP_DigestFinal_ex(), EVP_MD_CTX_destroy()
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
)
333 ctx
= EVP_MD_CTX_create();
336 ret
= EVP_DigestInit_ex(ctx
, md
, engine
);
338 EVP_MD_CTX_destroy(ctx
);
341 ret
= EVP_DigestUpdate(ctx
, data
, dsize
);
343 EVP_MD_CTX_destroy(ctx
);
346 ret
= EVP_DigestFinal_ex(ctx
, hash
, hsize
);
347 EVP_MD_CTX_destroy(ctx
);
352 * The message digest SHA256
354 * @return the message digest type.
356 * @ingroup hcrypto_evp
363 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, sha256
);
367 * The message digest SHA384
369 * @return the message digest type.
371 * @ingroup hcrypto_evp
378 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, sha384
);
382 * The message digest SHA512
384 * @return the message digest type.
386 * @ingroup hcrypto_evp
393 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, sha512
);
397 * The message digest SHA1
399 * @return the message digest type.
401 * @ingroup hcrypto_evp
408 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, sha1
);
412 * The message digest SHA1
414 * @return the message digest type.
416 * @ingroup hcrypto_evp
420 EVP_sha(void) HC_DEPRECATED
428 * The message digest MD5
430 * @return the message digest type.
432 * @ingroup hcrypto_evp
436 EVP_md5(void) HC_DEPRECATED_CRYPTO
439 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, md5
);
443 * The message digest MD4
445 * @return the message digest type.
447 * @ingroup hcrypto_evp
451 EVP_md4(void) HC_DEPRECATED_CRYPTO
454 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, md4
);
458 * The message digest MD2
460 * @return the message digest type.
462 * @ingroup hcrypto_evp
466 EVP_md2(void) HC_DEPRECATED_CRYPTO
469 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, md2
);
481 null_Update (void *m
, const void * data
, size_t size
)
485 null_Final(void *res
, void *m
)
490 * The null message digest
492 * @return the message digest type.
494 * @ingroup hcrypto_evp
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
,
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
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
539 EVP_CIPHER_key_length(const EVP_CIPHER
*c
)
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
555 EVP_CIPHER_iv_length(const EVP_CIPHER
*c
)
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
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
);
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
;
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) {
624 EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX
*c
, int pad
)
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
641 EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX
*ctx
)
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
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
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
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
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
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
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
)
782 ctx
->encrypt
= (encp
? 1 : 0);
784 if (c
&& (c
!= ctx
->cipher
)) {
785 EVP_CIPHER_CTX_cleanup(ctx
);
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)
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! */
801 switch (EVP_CIPHER_CTX_mode(ctx
)) {
802 case EVP_CIPH_CBC_MODE
:
804 assert(EVP_CIPHER_CTX_iv_length(ctx
) <= sizeof(ctx
->iv
));
807 memcpy(ctx
->oiv
, iv
, EVP_CIPHER_CTX_iv_length(ctx
));
808 memcpy(ctx
->iv
, ctx
->oiv
, EVP_CIPHER_CTX_iv_length(ctx
));
811 case EVP_CIPH_STREAM_CIPHER
:
813 case EVP_CIPH_CFB8_MODE
:
815 memcpy(ctx
->iv
, iv
, EVP_CIPHER_CTX_iv_length(ctx
));
822 if (key
|| (ctx
->cipher
->flags
& EVP_CIPH_ALWAYS_CALL_INIT
))
823 return ctx
->cipher
->init(ctx
, key
, iv
, encp
);
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
;
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
);
872 blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
873 left
= blocksize
- ctx
->buf_len
;
878 /* if total buffer is smaller then input, store locally */
880 memcpy(ctx
->buf
+ ctx
->buf_len
, in
, inlen
);
881 ctx
->buf_len
+= inlen
;
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
);
892 *outlen
+= blocksize
;
894 in
= ((unsigned char *)in
) + left
;
895 out
= ((unsigned char *)out
) + blocksize
;
900 ctx
->buf_len
= (inlen
& ctx
->block_mask
);
901 inlen
&= ~ctx
->block_mask
;
903 ret
= (*ctx
->cipher
->do_cipher
)(ctx
, out
, in
, inlen
);
909 in
= ((unsigned char *)in
) + inlen
;
910 memcpy(ctx
->buf
, in
, ctx
->buf_len
);
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
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
)
939 int ret
, left
, blocksize
;
941 blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
943 left
= blocksize
- ctx
->buf_len
;
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
);
953 *outlen
+= blocksize
;
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
);
981 enc_null_init(EVP_CIPHER_CTX
*ctx
,
982 const unsigned char * key
,
983 const unsigned char * iv
,
990 enc_null_do_cipher(EVP_CIPHER_CTX
*ctx
,
992 const unsigned char *in
,
995 memmove(out
, in
, size
);
1000 enc_null_cleanup(EVP_CIPHER_CTX
*ctx
)
1006 * The NULL cipher type, does no encryption/decryption.
1008 * @return the null EVP_CIPHER pointer.
1010 * @ingroup hcrypto_evp
1016 static const EVP_CIPHER enc_null
= {
1035 * The RC2 cipher type
1037 * @return the RC2 EVP_CIPHER pointer.
1039 * @ingroup hcrypto_evp
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
1058 EVP_rc2_40_cbc(void)
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
1073 EVP_rc2_64_cbc(void)
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
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
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
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
1133 EVP_des_ede3_cbc(void)
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
1148 EVP_aes_128_cbc(void)
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
1163 EVP_aes_192_cbc(void)
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
1178 EVP_aes_256_cbc(void)
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
1193 EVP_aes_128_cfb8(void)
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
1208 EVP_aes_192_cfb8(void)
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
1223 EVP_aes_256_cfb8(void)
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
1238 EVP_camellia_128_cbc(void)
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
1253 EVP_camellia_192_cbc(void)
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
1268 EVP_camellia_256_cbc(void)
1271 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, camellia_256_cbc
);
1278 static const struct cipher_name
{
1280 const EVP_CIPHER
*(*func
)(void);
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
1305 EVP_get_cipherbyname(const char *name
)
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
)();
1321 #define min(a,b) (((a)>(b))?(b):(a))
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
,
1348 const void *data
, size_t datalen
,
1353 unsigned int ivlen
, keylen
;
1355 unsigned int mds
= 0, i
;
1356 unsigned char *key
= keydata
;
1357 unsigned char *iv
= ivdata
;
1361 keylen
= EVP_CIPHER_key_length(type
);
1362 ivlen
= EVP_CIPHER_iv_length(type
);
1367 buf
= malloc(EVP_MD_size(md
));
1371 EVP_MD_CTX_init(&c
);
1375 EVP_DigestInit_ex(&c
, md
, NULL
);
1377 EVP_DigestUpdate(&c
, buf
, mds
);
1379 EVP_DigestUpdate(&c
,data
,datalen
);
1381 #define PKCS5_SALT_LEN 8
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
));
1398 size_t sz
= min(keylen
, mds
);
1400 memcpy(key
, buf
, sz
);
1406 if (ivlen
&& mds
> i
) {
1407 size_t sz
= min(ivlen
, (mds
- i
));
1409 memcpy(iv
, &buf
[i
], sz
);
1414 if (keylen
== 0 && ivlen
== 0)
1418 EVP_MD_CTX_cleanup(&c
);
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)
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
)
1463 return (*ctx
->cipher
->ctrl
)(ctx
, type
, arg
, data
);
1467 * Add all algorithms to the crypto core.
1469 * @ingroup hcrypto_core
1473 OpenSSL_add_all_algorithms(void)
1479 * Add all algorithms to the crypto core using configuration file.
1481 * @ingroup hcrypto_core
1485 OpenSSL_add_all_algorithms_conf(void)
1491 * Add all algorithms to the crypto core, but don't use the
1492 * configuration file.
1494 * @ingroup hcrypto_core
1498 OpenSSL_add_all_algorithms_noconf(void)