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 SHA384
366 * @return the message digest type.
368 * @ingroup hcrypto_evp
375 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, sha384
);
379 * The message digest SHA512
381 * @return the message digest type.
383 * @ingroup hcrypto_evp
390 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, sha512
);
394 * The message digest SHA1
396 * @return the message digest type.
398 * @ingroup hcrypto_evp
405 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, sha1
);
409 * The message digest SHA1
411 * @return the message digest type.
413 * @ingroup hcrypto_evp
417 EVP_sha(void) HC_DEPRECATED
425 * The message digest MD5
427 * @return the message digest type.
429 * @ingroup hcrypto_evp
433 EVP_md5(void) HC_DEPRECATED_CRYPTO
436 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, md5
);
440 * The message digest MD4
442 * @return the message digest type.
444 * @ingroup hcrypto_evp
448 EVP_md4(void) HC_DEPRECATED_CRYPTO
451 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, md4
);
455 * The message digest MD2
457 * @return the message digest type.
459 * @ingroup hcrypto_evp
463 EVP_md2(void) HC_DEPRECATED_CRYPTO
466 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, md2
);
478 null_Update (void *m
, const void * data
, size_t size
)
482 null_Final(void *res
, void *m
)
487 * The null message digest
489 * @return the message digest type.
491 * @ingroup hcrypto_evp
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
,
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
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
536 EVP_CIPHER_key_length(const EVP_CIPHER
*c
)
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
552 EVP_CIPHER_iv_length(const EVP_CIPHER
*c
)
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
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
;
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) {
618 EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX
*c
, int pad
)
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
635 EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX
*ctx
)
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
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
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
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
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
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
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
)
776 ctx
->encrypt
= (encp
? 1 : 0);
778 if (c
&& (c
!= ctx
->cipher
)) {
779 EVP_CIPHER_CTX_cleanup(ctx
);
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)
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! */
795 switch (EVP_CIPHER_CTX_mode(ctx
)) {
796 case EVP_CIPH_CBC_MODE
:
798 assert(EVP_CIPHER_CTX_iv_length(ctx
) <= sizeof(ctx
->iv
));
801 memcpy(ctx
->oiv
, iv
, EVP_CIPHER_CTX_iv_length(ctx
));
802 memcpy(ctx
->iv
, ctx
->oiv
, EVP_CIPHER_CTX_iv_length(ctx
));
805 case EVP_CIPH_STREAM_CIPHER
:
807 case EVP_CIPH_CFB8_MODE
:
809 memcpy(ctx
->iv
, iv
, EVP_CIPHER_CTX_iv_length(ctx
));
816 if (key
|| (ctx
->cipher
->flags
& EVP_CIPH_ALWAYS_CALL_INIT
))
817 ctx
->cipher
->init(ctx
, key
, iv
, encp
);
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
;
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
);
866 blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
867 left
= blocksize
- ctx
->buf_len
;
872 /* if total buffer is smaller then input, store locally */
874 memcpy(ctx
->buf
+ ctx
->buf_len
, in
, inlen
);
875 ctx
->buf_len
+= inlen
;
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
);
886 *outlen
+= blocksize
;
888 in
= ((unsigned char *)in
) + left
;
889 out
= ((unsigned char *)out
) + blocksize
;
894 ctx
->buf_len
= (inlen
& ctx
->block_mask
);
895 inlen
&= ~ctx
->block_mask
;
897 ret
= (*ctx
->cipher
->do_cipher
)(ctx
, out
, in
, inlen
);
903 in
= ((unsigned char *)in
) + inlen
;
904 memcpy(ctx
->buf
, in
, ctx
->buf_len
);
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
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
)
933 int ret
, left
, blocksize
;
935 blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
937 left
= blocksize
- ctx
->buf_len
;
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
);
947 *outlen
+= blocksize
;
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
);
975 enc_null_init(EVP_CIPHER_CTX
*ctx
,
976 const unsigned char * key
,
977 const unsigned char * iv
,
984 enc_null_do_cipher(EVP_CIPHER_CTX
*ctx
,
986 const unsigned char *in
,
989 memmove(out
, in
, size
);
994 enc_null_cleanup(EVP_CIPHER_CTX
*ctx
)
1000 * The NULL cipher type, does no encryption/decryption.
1002 * @return the null EVP_CIPHER pointer.
1004 * @ingroup hcrypto_evp
1010 static const EVP_CIPHER enc_null
= {
1029 * The RC2 cipher type
1031 * @return the RC2 EVP_CIPHER pointer.
1033 * @ingroup hcrypto_evp
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
1052 EVP_rc2_40_cbc(void)
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
1067 EVP_rc2_64_cbc(void)
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
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
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
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
1127 EVP_des_ede3_cbc(void)
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
1142 EVP_aes_128_cbc(void)
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
1157 EVP_aes_192_cbc(void)
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
1172 EVP_aes_256_cbc(void)
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
1187 EVP_aes_128_cfb8(void)
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
1202 EVP_aes_192_cfb8(void)
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
1217 EVP_aes_256_cfb8(void)
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
1232 EVP_camellia_128_cbc(void)
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
1247 EVP_camellia_192_cbc(void)
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
1262 EVP_camellia_256_cbc(void)
1265 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, camellia_256_cbc
);
1272 static const struct cipher_name
{
1274 const EVP_CIPHER
*(*func
)(void);
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
1299 EVP_get_cipherbyname(const char *name
)
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
)();
1315 #define min(a,b) (((a)>(b))?(b):(a))
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
,
1342 const void *data
, size_t datalen
,
1347 unsigned int ivlen
, keylen
;
1349 unsigned int mds
= 0, i
;
1350 unsigned char *key
= keydata
;
1351 unsigned char *iv
= ivdata
;
1355 keylen
= EVP_CIPHER_key_length(type
);
1356 ivlen
= EVP_CIPHER_iv_length(type
);
1361 buf
= malloc(EVP_MD_size(md
));
1365 EVP_MD_CTX_init(&c
);
1369 EVP_DigestInit_ex(&c
, md
, NULL
);
1371 EVP_DigestUpdate(&c
, buf
, mds
);
1373 EVP_DigestUpdate(&c
,data
,datalen
);
1375 #define PKCS5_SALT_LEN 8
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
));
1392 size_t sz
= min(keylen
, mds
);
1394 memcpy(key
, buf
, sz
);
1400 if (ivlen
&& mds
> i
) {
1401 size_t sz
= min(ivlen
, (mds
- i
));
1403 memcpy(iv
, &buf
[i
], sz
);
1408 if (keylen
== 0 && ivlen
== 0)
1412 EVP_MD_CTX_cleanup(&c
);
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)
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
)
1457 return (*ctx
->cipher
->ctrl
)(ctx
, type
, arg
, data
);
1461 * Add all algorithms to the crypto core.
1463 * @ingroup hcrypto_core
1467 OpenSSL_add_all_algorithms(void)
1473 * Add all algorithms to the crypto core using configuration file.
1475 * @ingroup hcrypto_core
1479 OpenSSL_add_all_algorithms_conf(void)
1485 * Add all algorithms to the crypto core, but don't use the
1486 * configuration file.
1488 * @ingroup hcrypto_core
1492 OpenSSL_add_all_algorithms_noconf(void)