2 * Copyright (c) 2006 - 2016 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
40 #define HC_DEPRECATED_CRYPTO
45 #include <evp-hcrypto.h>
50 #include <evp-pkcs11.h>
51 #include <evp-openssl.h>
53 #include <krb5-types.h>
55 #ifndef HCRYPTO_DEF_PROVIDER
57 # define HCRYPTO_DEF_PROVIDER cc
59 # define HCRYPTO_DEF_PROVIDER pkcs11_hcrypto
60 # elif HAVE_HCRYPTO_W_OPENSSL
61 # define HCRYPTO_DEF_PROVIDER ossl
63 # define HCRYPTO_DEF_PROVIDER hcrypto
67 #define HC_CONCAT4(x,y,z,aa) x ## y ## z ## aa
70 #define EVP_DEF_OP(_prov,_op) HC_CONCAT4(EVP_,_prov,_,_op)()
73 * @page page_evp EVP - generic crypto interface
75 * See the library functions here: @ref hcrypto_evp
77 * @section evp_cipher EVP Cipher
79 * The use of EVP_CipherInit_ex() and EVP_Cipher() is pretty easy to
80 * understand forward, then EVP_CipherUpdate() and
81 * EVP_CipherFinal_ex() really needs an example to explain @ref
82 * example_evp_cipher.c .
84 * @example example_evp_cipher.c
86 * This is an example how to use EVP_CipherInit_ex(),
87 * EVP_CipherUpdate() and EVP_CipherFinal_ex().
90 struct hc_EVP_MD_CTX
{
98 * Return the output size of the message digest function.
100 * @param md the evp message
102 * @return size output size of the message digest function.
104 * @ingroup hcrypto_evp
108 EVP_MD_size(const EVP_MD
*md
)
110 return md
->hash_size
;
114 * Return the blocksize of the message digest function.
116 * @param md the evp message
118 * @return size size of the message digest block size
120 * @ingroup hcrypto_evp
124 EVP_MD_block_size(const EVP_MD
*md
)
126 return md
->block_size
;
130 * Allocate a messsage digest context object. Free with
131 * EVP_MD_CTX_destroy().
133 * @return a newly allocated message digest context object.
135 * @ingroup hcrypto_evp
139 EVP_MD_CTX_create(void)
141 return calloc(1, sizeof(EVP_MD_CTX
));
145 * Initiate a messsage digest context object. Deallocate with
146 * EVP_MD_CTX_cleanup(). Please use EVP_MD_CTX_create() instead.
148 * @param ctx variable to initiate.
150 * @ingroup hcrypto_evp
154 EVP_MD_CTX_init(EVP_MD_CTX
*ctx
) HC_DEPRECATED
156 memset(ctx
, 0, sizeof(*ctx
));
160 * Free a messsage digest context object.
162 * @param ctx context to free.
164 * @ingroup hcrypto_evp
168 EVP_MD_CTX_destroy(EVP_MD_CTX
*ctx
)
170 EVP_MD_CTX_cleanup(ctx
);
175 * Free the resources used by the EVP_MD context.
177 * @param ctx the context to free the resources from.
179 * @return 1 on success.
181 * @ingroup hcrypto_evp
185 EVP_MD_CTX_cleanup(EVP_MD_CTX
*ctx
) HC_DEPRECATED
187 if (ctx
->md
&& ctx
->md
->cleanup
) {
188 int ret
= (ctx
->md
->cleanup
)(ctx
->ptr
);
191 } else if (ctx
->md
) {
192 memset(ctx
->ptr
, 0, ctx
->md
->ctx_size
);
197 memset(ctx
, 0, sizeof(*ctx
));
202 * Get the EVP_MD use for a specified context.
204 * @param ctx the EVP_MD context to get the EVP_MD for.
206 * @return the EVP_MD used for the context.
208 * @ingroup hcrypto_evp
212 EVP_MD_CTX_md(EVP_MD_CTX
*ctx
)
218 * Return the output size of the message digest function.
220 * @param ctx the evp message digest context
222 * @return size output size of the message digest function.
224 * @ingroup hcrypto_evp
228 EVP_MD_CTX_size(EVP_MD_CTX
*ctx
)
230 return EVP_MD_size(ctx
->md
);
234 * Return the blocksize of the message digest function.
236 * @param ctx the evp message digest context
238 * @return size size of the message digest block size
240 * @ingroup hcrypto_evp
244 EVP_MD_CTX_block_size(EVP_MD_CTX
*ctx
)
246 return EVP_MD_block_size(ctx
->md
);
250 * Init a EVP_MD_CTX for use a specific message digest and engine.
252 * @param ctx the message digest context to init.
253 * @param md the message digest to use.
254 * @param engine the engine to use, NULL to use the default engine.
256 * @return 1 on success.
258 * @ingroup hcrypto_evp
262 EVP_DigestInit_ex(EVP_MD_CTX
*ctx
, const EVP_MD
*md
, ENGINE
*engine
)
264 if (ctx
->md
!= md
|| ctx
->engine
!= engine
) {
265 EVP_MD_CTX_cleanup(ctx
);
267 ctx
->engine
= engine
;
271 ctx
->ptr
= calloc(1, md
->ctx_size
);
272 if (ctx
->ptr
== NULL
)
277 return (ctx
->md
->init
)(ctx
->ptr
);
281 * Update the digest with some data.
283 * @param ctx the context to update
284 * @param data the data to update the context with
285 * @param size length of data
287 * @return 1 on success.
289 * @ingroup hcrypto_evp
293 EVP_DigestUpdate(EVP_MD_CTX
*ctx
, const void *data
, size_t size
)
295 (ctx
->md
->update
)(ctx
->ptr
, data
, size
);
300 * Complete the message digest.
302 * @param ctx the context to complete.
303 * @param hash the output of the message digest function. At least
305 * @param size the output size of hash.
307 * @return 1 on success.
309 * @ingroup hcrypto_evp
313 EVP_DigestFinal_ex(EVP_MD_CTX
*ctx
, void *hash
, unsigned int *size
)
315 (ctx
->md
->final
)(hash
, ctx
->ptr
);
317 *size
= ctx
->md
->hash_size
;
322 * Do the whole EVP_MD_CTX_create(), EVP_DigestInit_ex(),
323 * EVP_DigestUpdate(), EVP_DigestFinal_ex(), EVP_MD_CTX_destroy()
326 * @param data the data to update the context with
327 * @param dsize length of data
328 * @param hash output data of at least EVP_MD_size() length.
329 * @param hsize output length of hash.
330 * @param md message digest to use
331 * @param engine engine to use, NULL for default engine.
333 * @return 1 on success.
335 * @ingroup hcrypto_evp
339 EVP_Digest(const void *data
, size_t dsize
, void *hash
, unsigned int *hsize
,
340 const EVP_MD
*md
, ENGINE
*engine
)
345 ctx
= EVP_MD_CTX_create();
348 ret
= EVP_DigestInit_ex(ctx
, md
, engine
);
350 EVP_MD_CTX_destroy(ctx
);
353 ret
= EVP_DigestUpdate(ctx
, data
, dsize
);
355 EVP_MD_CTX_destroy(ctx
);
358 ret
= EVP_DigestFinal_ex(ctx
, hash
, hsize
);
359 EVP_MD_CTX_destroy(ctx
);
364 * The message digest SHA256
366 * @return the message digest type.
368 * @ingroup hcrypto_evp
375 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, sha256
);
379 * The message digest SHA384
381 * @return the message digest type.
383 * @ingroup hcrypto_evp
390 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, sha384
);
394 * The message digest SHA512
396 * @return the message digest type.
398 * @ingroup hcrypto_evp
405 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, sha512
);
409 * The message digest SHA1
411 * @return the message digest type.
413 * @ingroup hcrypto_evp
420 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, sha1
);
424 * The message digest SHA1
426 * @return the message digest type.
428 * @ingroup hcrypto_evp
432 EVP_sha(void) HC_DEPRECATED
440 * The message digest MD5
442 * @return the message digest type.
444 * @ingroup hcrypto_evp
448 EVP_md5(void) HC_DEPRECATED_CRYPTO
451 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, md5
);
455 * The message digest MD4
457 * @return the message digest type.
459 * @ingroup hcrypto_evp
463 EVP_md4(void) HC_DEPRECATED_CRYPTO
466 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, md4
);
470 * The message digest MD2
472 * @return the message digest type.
474 * @ingroup hcrypto_evp
478 EVP_md2(void) HC_DEPRECATED_CRYPTO
481 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, md2
);
493 null_Update (void *m
, const void * data
, size_t size
)
497 null_Final(void *res
, void *m
)
502 * The null message digest
504 * @return the message digest type.
506 * @ingroup hcrypto_evp
512 static const struct hc_evp_md null
= {
516 (hc_evp_md_init
)null_Init
,
517 (hc_evp_md_update
)null_Update
,
518 (hc_evp_md_final
)null_Final
,
525 * Return the block size of the cipher.
527 * @param c cipher to get the block size from.
529 * @return the block size of the cipher.
531 * @ingroup hcrypto_evp
535 EVP_CIPHER_block_size(const EVP_CIPHER
*c
)
537 return c
->block_size
;
541 * Return the key size of the cipher.
543 * @param c cipher to get the key size from.
545 * @return the key size of the cipher.
547 * @ingroup hcrypto_evp
551 EVP_CIPHER_key_length(const EVP_CIPHER
*c
)
557 * Return the IV size of the cipher.
559 * @param c cipher to get the IV size from.
561 * @return the IV size of the cipher.
563 * @ingroup hcrypto_evp
567 EVP_CIPHER_iv_length(const EVP_CIPHER
*c
)
573 * Initiate a EVP_CIPHER_CTX context. Clean up with
574 * EVP_CIPHER_CTX_cleanup().
576 * @param c the cipher initiate.
578 * @ingroup hcrypto_evp
582 EVP_CIPHER_CTX_init(EVP_CIPHER_CTX
*c
)
584 memset(c
, 0, sizeof(*c
));
588 * Clean up the EVP_CIPHER_CTX context.
590 * @param c the cipher to clean up.
592 * @return 1 on success.
594 * @ingroup hcrypto_evp
598 EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX
*c
)
600 if (c
->cipher
&& c
->cipher
->cleanup
) {
601 int ret
= c
->cipher
->cleanup(c
);
605 if (c
->cipher_data
) {
606 memset(c
->cipher_data
, 0, c
->cipher
->ctx_size
);
607 free(c
->cipher_data
);
608 c
->cipher_data
= NULL
;
614 * If the cipher type supports it, change the key length
616 * @param c the cipher context to change the key length for
617 * @param length new key length
619 * @return 1 on success.
621 * @ingroup hcrypto_evp
625 EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX
*c
, int length
)
627 if ((c
->cipher
->flags
& EVP_CIPH_VARIABLE_LENGTH
) && length
> 0) {
636 EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX
*c
, int pad
)
643 * Return the EVP_CIPHER for a EVP_CIPHER_CTX context.
645 * @param ctx the context to get the cipher type from.
647 * @return the EVP_CIPHER pointer.
649 * @ingroup hcrypto_evp
653 EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX
*ctx
)
659 * Return the block size of the cipher context.
661 * @param ctx cipher context to get the block size from.
663 * @return the block size of the cipher context.
665 * @ingroup hcrypto_evp
669 EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX
*ctx
)
671 return EVP_CIPHER_block_size(ctx
->cipher
);
675 * Return the key size of the cipher context.
677 * @param ctx cipher context to get the key size from.
679 * @return the key size of the cipher context.
681 * @ingroup hcrypto_evp
685 EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX
*ctx
)
687 return EVP_CIPHER_key_length(ctx
->cipher
);
691 * Return the IV size of the cipher context.
693 * @param ctx cipher context to get the IV size from.
695 * @return the IV size of the cipher context.
697 * @ingroup hcrypto_evp
701 EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX
*ctx
)
703 return EVP_CIPHER_iv_length(ctx
->cipher
);
707 * Get the flags for an EVP_CIPHER_CTX context.
709 * @param ctx the EVP_CIPHER_CTX to get the flags from
711 * @return the flags for an EVP_CIPHER_CTX.
713 * @ingroup hcrypto_evp
717 EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX
*ctx
)
719 return ctx
->cipher
->flags
;
723 * Get the mode for an EVP_CIPHER_CTX context.
725 * @param ctx the EVP_CIPHER_CTX to get the mode from
727 * @return the mode for an EVP_CIPHER_CTX.
729 * @ingroup hcrypto_evp
733 EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX
*ctx
)
735 return EVP_CIPHER_CTX_flags(ctx
) & EVP_CIPH_MODE
;
739 * Get the app data for an EVP_CIPHER_CTX context.
741 * @param ctx the EVP_CIPHER_CTX to get the app data from
743 * @return the app data for an EVP_CIPHER_CTX.
745 * @ingroup hcrypto_evp
749 EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX
*ctx
)
751 return ctx
->app_data
;
755 * Set the app data for an EVP_CIPHER_CTX context.
757 * @param ctx the EVP_CIPHER_CTX to set the app data for
758 * @param data the app data to set for an EVP_CIPHER_CTX.
760 * @ingroup hcrypto_evp
764 EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX
*ctx
, void *data
)
766 ctx
->app_data
= data
;
770 * Initiate the EVP_CIPHER_CTX context to encrypt or decrypt data.
771 * Clean up with EVP_CIPHER_CTX_cleanup().
773 * @param ctx context to initiate
774 * @param c cipher to use.
775 * @param engine crypto engine to use, NULL to select default.
776 * @param key the crypto key to use, NULL will use the previous value.
777 * @param iv the IV to use, NULL will use the previous value.
778 * @param encp non zero will encrypt, -1 use the previous value.
780 * @return 1 on success.
782 * @ingroup hcrypto_evp
786 EVP_CipherInit_ex(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*c
, ENGINE
*engine
,
787 const void *key
, const void *iv
, int encp
)
794 ctx
->encrypt
= (encp
? 1 : 0);
796 if (c
&& (c
!= ctx
->cipher
)) {
797 EVP_CIPHER_CTX_cleanup(ctx
);
799 ctx
->key_len
= c
->key_len
;
801 ctx
->cipher_data
= calloc(1, c
->ctx_size
);
802 if (ctx
->cipher_data
== NULL
&& c
->ctx_size
!= 0)
805 /* assume block size is a multiple of 2 */
806 ctx
->block_mask
= EVP_CIPHER_block_size(c
) - 1;
808 if ((ctx
->cipher
->flags
& EVP_CIPH_CTRL_INIT
) &&
809 !EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_INIT
, 0, NULL
))
812 } else if (ctx
->cipher
== NULL
) {
813 /* reuse of cipher, but not any cipher ever set! */
817 switch (EVP_CIPHER_CTX_mode(ctx
)) {
818 case EVP_CIPH_CBC_MODE
:
820 assert(EVP_CIPHER_CTX_iv_length(ctx
) <= sizeof(ctx
->iv
));
823 memcpy(ctx
->oiv
, iv
, EVP_CIPHER_CTX_iv_length(ctx
));
824 memcpy(ctx
->iv
, ctx
->oiv
, EVP_CIPHER_CTX_iv_length(ctx
));
827 case EVP_CIPH_STREAM_CIPHER
:
829 case EVP_CIPH_CFB8_MODE
:
831 memcpy(ctx
->iv
, iv
, EVP_CIPHER_CTX_iv_length(ctx
));
838 if (key
|| (ctx
->cipher
->flags
& EVP_CIPH_ALWAYS_CALL_INIT
))
839 return ctx
->cipher
->init(ctx
, key
, iv
, encp
);
845 * Encipher/decipher partial data
847 * @param ctx the cipher context.
848 * @param out output data from the operation.
849 * @param outlen output length
850 * @param in input data to the operation.
851 * @param inlen length of data.
853 * The output buffer length should at least be EVP_CIPHER_block_size()
854 * byte longer then the input length.
856 * See @ref evp_cipher for an example how to use this function.
858 * @return 1 on success.
860 * @ingroup hcrypto_evp
864 EVP_CipherUpdate(EVP_CIPHER_CTX
*ctx
, void *out
, int *outlen
,
865 void *in
, size_t inlen
)
867 int ret
, left
, blocksize
;
872 * If there in no spare bytes in the left from last Update and the
873 * input length is on the block boundery, the EVP_CipherUpdate()
874 * function can take a shortcut (and preformance gain) and
875 * directly encrypt the data, otherwise we hav to fix it up and
876 * store extra it the EVP_CIPHER_CTX.
878 if (ctx
->buf_len
== 0 && (inlen
& ctx
->block_mask
) == 0) {
879 ret
= (*ctx
->cipher
->do_cipher
)(ctx
, out
, in
, inlen
);
888 blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
889 left
= blocksize
- ctx
->buf_len
;
894 /* if total buffer is smaller then input, store locally */
896 memcpy(ctx
->buf
+ ctx
->buf_len
, in
, inlen
);
897 ctx
->buf_len
+= inlen
;
901 /* fill in local buffer and encrypt */
902 memcpy(ctx
->buf
+ ctx
->buf_len
, in
, left
);
903 ret
= (*ctx
->cipher
->do_cipher
)(ctx
, out
, ctx
->buf
, blocksize
);
904 memset(ctx
->buf
, 0, blocksize
);
908 *outlen
+= blocksize
;
910 in
= ((unsigned char *)in
) + left
;
911 out
= ((unsigned char *)out
) + blocksize
;
916 ctx
->buf_len
= (inlen
& ctx
->block_mask
);
917 inlen
&= ~ctx
->block_mask
;
919 ret
= (*ctx
->cipher
->do_cipher
)(ctx
, out
, in
, inlen
);
925 in
= ((unsigned char *)in
) + inlen
;
926 memcpy(ctx
->buf
, in
, ctx
->buf_len
);
933 * Encipher/decipher final data
935 * @param ctx the cipher context.
936 * @param out output data from the operation.
937 * @param outlen output length
939 * The input length needs to be at least EVP_CIPHER_block_size() bytes
942 * See @ref evp_cipher for an example how to use this function.
944 * @return 1 on success.
946 * @ingroup hcrypto_evp
950 EVP_CipherFinal_ex(EVP_CIPHER_CTX
*ctx
, void *out
, int *outlen
)
955 int ret
, left
, blocksize
;
957 blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
959 left
= blocksize
- ctx
->buf_len
;
962 /* zero fill local buffer */
963 memset(ctx
->buf
+ ctx
->buf_len
, 0, left
);
964 ret
= (*ctx
->cipher
->do_cipher
)(ctx
, out
, ctx
->buf
, blocksize
);
965 memset(ctx
->buf
, 0, blocksize
);
969 *outlen
+= blocksize
;
976 * Encipher/decipher data
978 * @param ctx the cipher context.
979 * @param out out data from the operation.
980 * @param in in data to the operation.
981 * @param size length of data.
983 * @return 1 on success.
987 EVP_Cipher(EVP_CIPHER_CTX
*ctx
, void *out
, const void *in
,size_t size
)
989 return ctx
->cipher
->do_cipher(ctx
, out
, in
, size
);
997 enc_null_init(EVP_CIPHER_CTX
*ctx
,
998 const unsigned char * key
,
999 const unsigned char * iv
,
1006 enc_null_do_cipher(EVP_CIPHER_CTX
*ctx
,
1008 const unsigned char *in
,
1011 memmove(out
, in
, size
);
1016 enc_null_cleanup(EVP_CIPHER_CTX
*ctx
)
1022 * The NULL cipher type, does no encryption/decryption.
1024 * @return the null EVP_CIPHER pointer.
1026 * @ingroup hcrypto_evp
1032 static const EVP_CIPHER enc_null
= {
1051 * The RC2 cipher type
1053 * @return the RC2 EVP_CIPHER pointer.
1055 * @ingroup hcrypto_evp
1062 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, rc2_cbc
);
1066 * The RC2 cipher type
1068 * @return the RC2 EVP_CIPHER pointer.
1070 * @ingroup hcrypto_evp
1074 EVP_rc2_40_cbc(void)
1077 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, rc2_40_cbc
);
1081 * The RC2 cipher type
1083 * @return the RC2 EVP_CIPHER pointer.
1085 * @ingroup hcrypto_evp
1089 EVP_rc2_64_cbc(void)
1092 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, rc2_64_cbc
);
1096 * The RC4 cipher type
1098 * @return the RC4 EVP_CIPHER pointer.
1100 * @ingroup hcrypto_evp
1107 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, rc4
);
1111 * The RC4-40 cipher type
1113 * @return the RC4-40 EVP_CIPHER pointer.
1115 * @ingroup hcrypto_evp
1122 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, rc4_40
);
1126 * The DES cipher type
1128 * @return the DES-CBC EVP_CIPHER pointer.
1130 * @ingroup hcrypto_evp
1137 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, des_cbc
);
1141 * The triple DES cipher type
1143 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
1145 * @ingroup hcrypto_evp
1149 EVP_des_ede3_cbc(void)
1152 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, des_ede3_cbc
);
1156 * The AES-128 cipher type
1158 * @return the AES-128 EVP_CIPHER pointer.
1160 * @ingroup hcrypto_evp
1164 EVP_aes_128_cbc(void)
1167 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, aes_128_cbc
);
1171 * The AES-192 cipher type
1173 * @return the AES-192 EVP_CIPHER pointer.
1175 * @ingroup hcrypto_evp
1179 EVP_aes_192_cbc(void)
1182 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, aes_192_cbc
);
1186 * The AES-256 cipher type
1188 * @return the AES-256 EVP_CIPHER pointer.
1190 * @ingroup hcrypto_evp
1194 EVP_aes_256_cbc(void)
1197 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, aes_256_cbc
);
1201 * The AES-128 cipher type
1203 * @return the AES-128 EVP_CIPHER pointer.
1205 * @ingroup hcrypto_evp
1209 EVP_aes_128_cfb8(void)
1212 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, aes_128_cfb8
);
1216 * The AES-192 cipher type
1218 * @return the AES-192 EVP_CIPHER pointer.
1220 * @ingroup hcrypto_evp
1224 EVP_aes_192_cfb8(void)
1227 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, aes_192_cfb8
);
1231 * The AES-256 cipher type
1233 * @return the AES-256 EVP_CIPHER pointer.
1235 * @ingroup hcrypto_evp
1239 EVP_aes_256_cfb8(void)
1242 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, aes_256_cfb8
);
1246 * The Camellia-128 cipher type
1248 * @return the Camellia-128 EVP_CIPHER pointer.
1250 * @ingroup hcrypto_evp
1254 EVP_camellia_128_cbc(void)
1257 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, camellia_128_cbc
);
1261 * The Camellia-198 cipher type
1263 * @return the Camellia-198 EVP_CIPHER pointer.
1265 * @ingroup hcrypto_evp
1269 EVP_camellia_192_cbc(void)
1272 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, camellia_192_cbc
);
1276 * The Camellia-256 cipher type
1278 * @return the Camellia-256 EVP_CIPHER pointer.
1280 * @ingroup hcrypto_evp
1284 EVP_camellia_256_cbc(void)
1287 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, camellia_256_cbc
);
1294 static const struct cipher_name
{
1296 const EVP_CIPHER
*(*func
)(void);
1298 { "des-ede3-cbc", EVP_des_ede3_cbc
},
1299 { "aes-128-cbc", EVP_aes_128_cbc
},
1300 { "aes-192-cbc", EVP_aes_192_cbc
},
1301 { "aes-256-cbc", EVP_aes_256_cbc
},
1302 { "aes-128-cfb8", EVP_aes_128_cfb8
},
1303 { "aes-192-cfb8", EVP_aes_192_cfb8
},
1304 { "aes-256-cfb8", EVP_aes_256_cfb8
},
1305 { "camellia-128-cbc", EVP_camellia_128_cbc
},
1306 { "camellia-192-cbc", EVP_camellia_192_cbc
},
1307 { "camellia-256-cbc", EVP_camellia_256_cbc
}
1311 * Get the cipher type using their name.
1313 * @param name the name of the cipher.
1315 * @return the selected EVP_CIPHER pointer or NULL if not found.
1317 * @ingroup hcrypto_evp
1321 EVP_get_cipherbyname(const char *name
)
1324 for (i
= 0; i
< sizeof(cipher_name
)/sizeof(cipher_name
[0]); i
++) {
1325 if (strcasecmp(cipher_name
[i
].name
, name
) == 0)
1326 return (*cipher_name
[i
].func
)();
1337 #define min(a,b) (((a)>(b))?(b):(a))
1341 * Provides a legancy string to key function, used in PEM files.
1343 * New protocols should use new string to key functions like NIST
1344 * SP56-800A or PKCS#5 v2.0 (see PKCS5_PBKDF2_HMAC_SHA1()).
1346 * @param type type of cipher to use
1347 * @param md message digest to use
1348 * @param salt salt salt string, should be an binary 8 byte buffer.
1349 * @param data the password/input key string.
1350 * @param datalen length of data parameter.
1351 * @param count iteration counter.
1352 * @param keydata output keydata, needs to of the size EVP_CIPHER_key_length().
1353 * @param ivdata output ivdata, needs to of the size EVP_CIPHER_block_size().
1355 * @return the size of derived key.
1357 * @ingroup hcrypto_evp
1361 EVP_BytesToKey(const EVP_CIPHER
*type
,
1364 const void *data
, size_t datalen
,
1369 unsigned int ivlen
, keylen
;
1371 unsigned int mds
= 0, i
;
1372 unsigned char *key
= keydata
;
1373 unsigned char *iv
= ivdata
;
1377 keylen
= EVP_CIPHER_key_length(type
);
1378 ivlen
= EVP_CIPHER_iv_length(type
);
1383 buf
= malloc(EVP_MD_size(md
));
1387 EVP_MD_CTX_init(&c
);
1391 EVP_DigestInit_ex(&c
, md
, NULL
);
1393 EVP_DigestUpdate(&c
, buf
, mds
);
1395 EVP_DigestUpdate(&c
,data
,datalen
);
1397 #define PKCS5_SALT_LEN 8
1400 EVP_DigestUpdate(&c
, salt
, PKCS5_SALT_LEN
);
1402 EVP_DigestFinal_ex(&c
, buf
, &mds
);
1403 assert(mds
== EVP_MD_size(md
));
1405 for (i
= 1; i
< count
; i
++) {
1406 EVP_DigestInit_ex(&c
, md
, NULL
);
1407 EVP_DigestUpdate(&c
, buf
, mds
);
1408 EVP_DigestFinal_ex(&c
, buf
, &mds
);
1409 assert(mds
== EVP_MD_size(md
));
1414 size_t sz
= min(keylen
, mds
);
1416 memcpy(key
, buf
, sz
);
1422 if (ivlen
&& mds
> i
) {
1423 size_t sz
= min(ivlen
, (mds
- i
));
1425 memcpy(iv
, &buf
[i
], sz
);
1430 if (keylen
== 0 && ivlen
== 0)
1434 EVP_MD_CTX_cleanup(&c
);
1437 return EVP_CIPHER_key_length(type
);
1441 * Generate a random key for the specificed EVP_CIPHER.
1443 * @param ctx EVP_CIPHER_CTX type to build the key for.
1444 * @param key return key, must be at least EVP_CIPHER_key_length() byte long.
1446 * @return 1 for success, 0 for failure.
1448 * @ingroup hcrypto_core
1452 EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX
*ctx
, void *key
)
1454 if (ctx
->cipher
->flags
& EVP_CIPH_RAND_KEY
)
1455 return EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_RAND_KEY
, 0, key
);
1456 if (RAND_bytes(key
, ctx
->key_len
) != 1)
1462 * Perform a operation on a ctx
1464 * @param ctx context to perform operation on.
1465 * @param type type of operation.
1466 * @param arg argument to operation.
1467 * @param data addition data to operation.
1469 * @return 1 for success, 0 for failure.
1471 * @ingroup hcrypto_core
1475 EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX
*ctx
, int type
, int arg
, void *data
)
1477 if (ctx
->cipher
== NULL
|| ctx
->cipher
->ctrl
== NULL
)
1479 return (*ctx
->cipher
->ctrl
)(ctx
, type
, arg
, data
);
1483 * Add all algorithms to the crypto core.
1485 * @ingroup hcrypto_core
1489 OpenSSL_add_all_algorithms(void)
1495 * Add all algorithms to the crypto core using configuration file.
1497 * @ingroup hcrypto_core
1501 OpenSSL_add_all_algorithms_conf(void)
1507 * Add all algorithms to the crypto core, but don't use the
1508 * configuration file.
1510 * @ingroup hcrypto_core
1514 OpenSSL_add_all_algorithms_noconf(void)