1 /* $NetBSD: evp.c,v 1.1.1.2 2014/04/24 12:45:30 pettai Exp $ */
4 * Copyright (c) 2006 - 2008 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #define HC_DEPRECATED_CRYPTO
43 #include <sys/types.h>
50 #include <evp-hcrypto.h>
53 #include <krb5/krb5-types.h>
54 #include <krb5/roken.h>
56 #ifndef HCRYPTO_DEF_PROVIDER
57 #define HCRYPTO_DEF_PROVIDER hcrypto
60 #define HC_CONCAT4(x,y,z,aa) x ## y ## z ## aa
63 #define EVP_DEF_OP(_prov,_op) HC_CONCAT4(EVP_,_prov,_,_op)()
66 * @page page_evp EVP - generic crypto interface
68 * See the library functions here: @ref hcrypto_evp
70 * @section evp_cipher EVP Cipher
72 * The use of EVP_CipherInit_ex() and EVP_Cipher() is pretty easy to
73 * understand forward, then EVP_CipherUpdate() and
74 * EVP_CipherFinal_ex() really needs an example to explain @ref
75 * example_evp_cipher.c .
77 * @example example_evp_cipher.c
79 * This is an example how to use EVP_CipherInit_ex(),
80 * EVP_CipherUpdate() and EVP_CipherFinal_ex().
83 struct hc_EVP_MD_CTX
{
91 * Return the output size of the message digest function.
93 * @param md the evp message
95 * @return size output size of the message digest function.
97 * @ingroup hcrypto_evp
101 EVP_MD_size(const EVP_MD
*md
)
103 return md
->hash_size
;
107 * Return the blocksize of the message digest function.
109 * @param md the evp message
111 * @return size size of the message digest block size
113 * @ingroup hcrypto_evp
117 EVP_MD_block_size(const EVP_MD
*md
)
119 return md
->block_size
;
123 * Allocate a messsage digest context object. Free with
124 * EVP_MD_CTX_destroy().
126 * @return a newly allocated message digest context object.
128 * @ingroup hcrypto_evp
132 EVP_MD_CTX_create(void)
134 return calloc(1, sizeof(EVP_MD_CTX
));
138 * Initiate a messsage digest context object. Deallocate with
139 * EVP_MD_CTX_cleanup(). Please use EVP_MD_CTX_create() instead.
141 * @param ctx variable to initiate.
143 * @ingroup hcrypto_evp
147 EVP_MD_CTX_init(EVP_MD_CTX
*ctx
) HC_DEPRECATED
149 memset(ctx
, 0, sizeof(*ctx
));
153 * Free a messsage digest context object.
155 * @param ctx context to free.
157 * @ingroup hcrypto_evp
161 EVP_MD_CTX_destroy(EVP_MD_CTX
*ctx
)
163 EVP_MD_CTX_cleanup(ctx
);
168 * Free the resources used by the EVP_MD context.
170 * @param ctx the context to free the resources from.
172 * @return 1 on success.
174 * @ingroup hcrypto_evp
178 EVP_MD_CTX_cleanup(EVP_MD_CTX
*ctx
) HC_DEPRECATED
180 if (ctx
->md
&& ctx
->md
->cleanup
)
181 (ctx
->md
->cleanup
)(ctx
);
183 memset(ctx
->ptr
, 0, ctx
->md
->ctx_size
);
187 memset(ctx
, 0, sizeof(*ctx
));
192 * Get the EVP_MD use for a specified context.
194 * @param ctx the EVP_MD context to get the EVP_MD for.
196 * @return the EVP_MD used for the context.
198 * @ingroup hcrypto_evp
202 EVP_MD_CTX_md(EVP_MD_CTX
*ctx
)
208 * Return the output size of the message digest function.
210 * @param ctx the evp message digest context
212 * @return size output size of the message digest function.
214 * @ingroup hcrypto_evp
218 EVP_MD_CTX_size(EVP_MD_CTX
*ctx
)
220 return EVP_MD_size(ctx
->md
);
224 * Return the blocksize of the message digest function.
226 * @param ctx the evp message digest context
228 * @return size size of the message digest block size
230 * @ingroup hcrypto_evp
234 EVP_MD_CTX_block_size(EVP_MD_CTX
*ctx
)
236 return EVP_MD_block_size(ctx
->md
);
240 * Init a EVP_MD_CTX for use a specific message digest and engine.
242 * @param ctx the message digest context to init.
243 * @param md the message digest to use.
244 * @param engine the engine to use, NULL to use the default engine.
246 * @return 1 on success.
248 * @ingroup hcrypto_evp
252 EVP_DigestInit_ex(EVP_MD_CTX
*ctx
, const EVP_MD
*md
, ENGINE
*engine
)
254 if (ctx
->md
!= md
|| ctx
->engine
!= engine
) {
255 EVP_MD_CTX_cleanup(ctx
);
257 ctx
->engine
= engine
;
259 ctx
->ptr
= calloc(1, md
->ctx_size
);
260 if (ctx
->ptr
== NULL
)
263 (ctx
->md
->init
)(ctx
->ptr
);
268 * Update the digest with some data.
270 * @param ctx the context to update
271 * @param data the data to update the context with
272 * @param size length of data
274 * @return 1 on success.
276 * @ingroup hcrypto_evp
280 EVP_DigestUpdate(EVP_MD_CTX
*ctx
, const void *data
, size_t size
)
282 (ctx
->md
->update
)(ctx
->ptr
, data
, size
);
287 * Complete the message digest.
289 * @param ctx the context to complete.
290 * @param hash the output of the message digest function. At least
292 * @param size the output size of hash.
294 * @return 1 on success.
296 * @ingroup hcrypto_evp
300 EVP_DigestFinal_ex(EVP_MD_CTX
*ctx
, void *hash
, unsigned int *size
)
302 (ctx
->md
->final
)(hash
, ctx
->ptr
);
304 *size
= ctx
->md
->hash_size
;
309 * Do the whole EVP_MD_CTX_create(), EVP_DigestInit_ex(),
310 * EVP_DigestUpdate(), EVP_DigestFinal_ex(), EVP_MD_CTX_destroy()
313 * @param data the data to update the context with
314 * @param dsize length of data
315 * @param hash output data of at least EVP_MD_size() length.
316 * @param hsize output length of hash.
317 * @param md message digest to use
318 * @param engine engine to use, NULL for default engine.
320 * @return 1 on success.
322 * @ingroup hcrypto_evp
326 EVP_Digest(const void *data
, size_t dsize
, void *hash
, unsigned int *hsize
,
327 const EVP_MD
*md
, ENGINE
*engine
)
332 ctx
= EVP_MD_CTX_create();
335 ret
= EVP_DigestInit_ex(ctx
, md
, engine
);
337 EVP_MD_CTX_destroy(ctx
);
340 ret
= EVP_DigestUpdate(ctx
, data
, dsize
);
342 EVP_MD_CTX_destroy(ctx
);
345 ret
= EVP_DigestFinal_ex(ctx
, hash
, hsize
);
346 EVP_MD_CTX_destroy(ctx
);
351 * The message digest SHA256
353 * @return the message digest type.
355 * @ingroup hcrypto_evp
362 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, sha256
);
366 * The message digest SHA384
368 * @return the message digest type.
370 * @ingroup hcrypto_evp
377 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, sha384
);
381 * The message digest SHA512
383 * @return the message digest type.
385 * @ingroup hcrypto_evp
392 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, sha512
);
396 * The message digest SHA1
398 * @return the message digest type.
400 * @ingroup hcrypto_evp
407 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, sha1
);
411 * The message digest SHA1
413 * @return the message digest type.
415 * @ingroup hcrypto_evp
419 EVP_sha(void) HC_DEPRECATED
427 * The message digest MD5
429 * @return the message digest type.
431 * @ingroup hcrypto_evp
435 EVP_md5(void) HC_DEPRECATED_CRYPTO
438 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, md5
);
442 * The message digest MD4
444 * @return the message digest type.
446 * @ingroup hcrypto_evp
450 EVP_md4(void) HC_DEPRECATED_CRYPTO
453 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, md4
);
457 * The message digest MD2
459 * @return the message digest type.
461 * @ingroup hcrypto_evp
465 EVP_md2(void) HC_DEPRECATED_CRYPTO
468 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, md2
);
480 null_Update (void *m
, const void * data
, size_t size
)
484 null_Final(void *res
, void *m
)
489 * The null message digest
491 * @return the message digest type.
493 * @ingroup hcrypto_evp
499 static const struct hc_evp_md null
= {
503 (hc_evp_md_init
)null_Init
,
504 (hc_evp_md_update
)null_Update
,
505 (hc_evp_md_final
)null_Final
,
512 * Return the block size of the cipher.
514 * @param c cipher to get the block size from.
516 * @return the block size of the cipher.
518 * @ingroup hcrypto_evp
522 EVP_CIPHER_block_size(const EVP_CIPHER
*c
)
524 return c
->block_size
;
528 * Return the key size of the cipher.
530 * @param c cipher to get the key size from.
532 * @return the key size of the cipher.
534 * @ingroup hcrypto_evp
538 EVP_CIPHER_key_length(const EVP_CIPHER
*c
)
544 * Return the IV size of the cipher.
546 * @param c cipher to get the IV size from.
548 * @return the IV size of the cipher.
550 * @ingroup hcrypto_evp
554 EVP_CIPHER_iv_length(const EVP_CIPHER
*c
)
560 * Initiate a EVP_CIPHER_CTX context. Clean up with
561 * EVP_CIPHER_CTX_cleanup().
563 * @param c the cipher initiate.
565 * @ingroup hcrypto_evp
569 EVP_CIPHER_CTX_init(EVP_CIPHER_CTX
*c
)
571 memset(c
, 0, sizeof(*c
));
575 * Clean up the EVP_CIPHER_CTX context.
577 * @param c the cipher to clean up.
579 * @return 1 on success.
581 * @ingroup hcrypto_evp
585 EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX
*c
)
587 if (c
->cipher
&& c
->cipher
->cleanup
)
588 c
->cipher
->cleanup(c
);
589 if (c
->cipher_data
) {
590 memset(c
->cipher_data
, 0, c
->cipher
->ctx_size
);
591 free(c
->cipher_data
);
592 c
->cipher_data
= NULL
;
598 * If the cipher type supports it, change the key length
600 * @param c the cipher context to change the key length for
601 * @param length new key length
603 * @return 1 on success.
605 * @ingroup hcrypto_evp
609 EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX
*c
, int length
)
611 if ((c
->cipher
->flags
& EVP_CIPH_VARIABLE_LENGTH
) && length
> 0) {
620 EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX
*c
, int pad
)
627 * Return the EVP_CIPHER for a EVP_CIPHER_CTX context.
629 * @param ctx the context to get the cipher type from.
631 * @return the EVP_CIPHER pointer.
633 * @ingroup hcrypto_evp
637 EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX
*ctx
)
643 * Return the block size of the cipher context.
645 * @param ctx cipher context to get the block size from.
647 * @return the block size of the cipher context.
649 * @ingroup hcrypto_evp
653 EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX
*ctx
)
655 return EVP_CIPHER_block_size(ctx
->cipher
);
659 * Return the key size of the cipher context.
661 * @param ctx cipher context to get the key size from.
663 * @return the key size of the cipher context.
665 * @ingroup hcrypto_evp
669 EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX
*ctx
)
671 return EVP_CIPHER_key_length(ctx
->cipher
);
675 * Return the IV size of the cipher context.
677 * @param ctx cipher context to get the IV size from.
679 * @return the IV size of the cipher context.
681 * @ingroup hcrypto_evp
685 EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX
*ctx
)
687 return EVP_CIPHER_iv_length(ctx
->cipher
);
691 * Get the flags for an EVP_CIPHER_CTX context.
693 * @param ctx the EVP_CIPHER_CTX to get the flags from
695 * @return the flags for an EVP_CIPHER_CTX.
697 * @ingroup hcrypto_evp
701 EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX
*ctx
)
703 return ctx
->cipher
->flags
;
707 * Get the mode for an EVP_CIPHER_CTX context.
709 * @param ctx the EVP_CIPHER_CTX to get the mode from
711 * @return the mode for an EVP_CIPHER_CTX.
713 * @ingroup hcrypto_evp
717 EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX
*ctx
)
719 return EVP_CIPHER_CTX_flags(ctx
) & EVP_CIPH_MODE
;
723 * Get the app data for an EVP_CIPHER_CTX context.
725 * @param ctx the EVP_CIPHER_CTX to get the app data from
727 * @return the app data for an EVP_CIPHER_CTX.
729 * @ingroup hcrypto_evp
733 EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX
*ctx
)
735 return ctx
->app_data
;
739 * Set the app data for an EVP_CIPHER_CTX context.
741 * @param ctx the EVP_CIPHER_CTX to set the app data for
742 * @param data the app data to set for an EVP_CIPHER_CTX.
744 * @ingroup hcrypto_evp
748 EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX
*ctx
, void *data
)
750 ctx
->app_data
= data
;
754 * Initiate the EVP_CIPHER_CTX context to encrypt or decrypt data.
755 * Clean up with EVP_CIPHER_CTX_cleanup().
757 * @param ctx context to initiate
758 * @param c cipher to use.
759 * @param engine crypto engine to use, NULL to select default.
760 * @param key the crypto key to use, NULL will use the previous value.
761 * @param iv the IV to use, NULL will use the previous value.
762 * @param encp non zero will encrypt, -1 use the previous value.
764 * @return 1 on success.
766 * @ingroup hcrypto_evp
770 EVP_CipherInit_ex(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*c
, ENGINE
*engine
,
771 const void *key
, const void *iv
, int encp
)
778 ctx
->encrypt
= (encp
? 1 : 0);
780 if (c
&& (c
!= ctx
->cipher
)) {
781 EVP_CIPHER_CTX_cleanup(ctx
);
783 ctx
->key_len
= c
->key_len
;
785 ctx
->cipher_data
= calloc(1, c
->ctx_size
);
786 if (ctx
->cipher_data
== NULL
&& c
->ctx_size
!= 0)
789 /* assume block size is a multiple of 2 */
790 ctx
->block_mask
= EVP_CIPHER_block_size(c
) - 1;
792 } else if (ctx
->cipher
== NULL
) {
793 /* reuse of cipher, but not any cipher ever set! */
797 switch (EVP_CIPHER_CTX_mode(ctx
)) {
798 case EVP_CIPH_CBC_MODE
:
800 assert(EVP_CIPHER_CTX_iv_length(ctx
) <= sizeof(ctx
->iv
));
803 memcpy(ctx
->oiv
, iv
, EVP_CIPHER_CTX_iv_length(ctx
));
804 memcpy(ctx
->iv
, ctx
->oiv
, EVP_CIPHER_CTX_iv_length(ctx
));
807 case EVP_CIPH_STREAM_CIPHER
:
809 case EVP_CIPH_CFB8_MODE
:
811 memcpy(ctx
->iv
, iv
, EVP_CIPHER_CTX_iv_length(ctx
));
818 if (key
|| (ctx
->cipher
->flags
& EVP_CIPH_ALWAYS_CALL_INIT
))
819 ctx
->cipher
->init(ctx
, key
, iv
, encp
);
825 * Encipher/decipher partial data
827 * @param ctx the cipher context.
828 * @param out output data from the operation.
829 * @param outlen output length
830 * @param in input data to the operation.
831 * @param inlen length of data.
833 * The output buffer length should at least be EVP_CIPHER_block_size()
834 * byte longer then the input length.
836 * See @ref evp_cipher for an example how to use this function.
838 * @return 1 on success.
840 * @ingroup hcrypto_evp
844 EVP_CipherUpdate(EVP_CIPHER_CTX
*ctx
, void *out
, int *outlen
,
845 void *in
, size_t inlen
)
847 int ret
, left
, blocksize
;
852 * If there in no spare bytes in the left from last Update and the
853 * input length is on the block boundery, the EVP_CipherUpdate()
854 * function can take a shortcut (and preformance gain) and
855 * directly encrypt the data, otherwise we hav to fix it up and
856 * store extra it the EVP_CIPHER_CTX.
858 if (ctx
->buf_len
== 0 && (inlen
& ctx
->block_mask
) == 0) {
859 ret
= (*ctx
->cipher
->do_cipher
)(ctx
, out
, in
, inlen
);
868 blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
869 left
= blocksize
- ctx
->buf_len
;
874 /* if total buffer is smaller then input, store locally */
876 memcpy(ctx
->buf
+ ctx
->buf_len
, in
, inlen
);
877 ctx
->buf_len
+= inlen
;
881 /* fill in local buffer and encrypt */
882 memcpy(ctx
->buf
+ ctx
->buf_len
, in
, left
);
883 ret
= (*ctx
->cipher
->do_cipher
)(ctx
, out
, ctx
->buf
, blocksize
);
884 memset(ctx
->buf
, 0, blocksize
);
888 *outlen
+= blocksize
;
890 in
= ((unsigned char *)in
) + left
;
891 out
= ((unsigned char *)out
) + blocksize
;
896 ctx
->buf_len
= (inlen
& ctx
->block_mask
);
897 inlen
&= ~ctx
->block_mask
;
899 ret
= (*ctx
->cipher
->do_cipher
)(ctx
, out
, in
, inlen
);
905 in
= ((unsigned char *)in
) + inlen
;
906 memcpy(ctx
->buf
, in
, ctx
->buf_len
);
913 * Encipher/decipher final data
915 * @param ctx the cipher context.
916 * @param out output data from the operation.
917 * @param outlen output length
919 * The input length needs to be at least EVP_CIPHER_block_size() bytes
922 * See @ref evp_cipher for an example how to use this function.
924 * @return 1 on success.
926 * @ingroup hcrypto_evp
930 EVP_CipherFinal_ex(EVP_CIPHER_CTX
*ctx
, void *out
, int *outlen
)
935 int ret
, left
, blocksize
;
937 blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
939 left
= blocksize
- ctx
->buf_len
;
942 /* zero fill local buffer */
943 memset(ctx
->buf
+ ctx
->buf_len
, 0, left
);
944 ret
= (*ctx
->cipher
->do_cipher
)(ctx
, out
, ctx
->buf
, blocksize
);
945 memset(ctx
->buf
, 0, blocksize
);
949 *outlen
+= blocksize
;
956 * Encipher/decipher data
958 * @param ctx the cipher context.
959 * @param out out data from the operation.
960 * @param in in data to the operation.
961 * @param size length of data.
963 * @return 1 on success.
967 EVP_Cipher(EVP_CIPHER_CTX
*ctx
, void *out
, const void *in
,size_t size
)
969 return ctx
->cipher
->do_cipher(ctx
, out
, in
, size
);
977 enc_null_init(EVP_CIPHER_CTX
*ctx
,
978 const unsigned char * key
,
979 const unsigned char * iv
,
986 enc_null_do_cipher(EVP_CIPHER_CTX
*ctx
,
988 const unsigned char *in
,
991 memmove(out
, in
, size
);
996 enc_null_cleanup(EVP_CIPHER_CTX
*ctx
)
1002 * The NULL cipher type, does no encryption/decryption.
1004 * @return the null EVP_CIPHER pointer.
1006 * @ingroup hcrypto_evp
1012 static const EVP_CIPHER enc_null
= {
1031 * The RC2 cipher type
1033 * @return the RC2 EVP_CIPHER pointer.
1035 * @ingroup hcrypto_evp
1042 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, rc2_cbc
);
1046 * The RC2 cipher type
1048 * @return the RC2 EVP_CIPHER pointer.
1050 * @ingroup hcrypto_evp
1054 EVP_rc2_40_cbc(void)
1057 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, rc2_40_cbc
);
1061 * The RC2 cipher type
1063 * @return the RC2 EVP_CIPHER pointer.
1065 * @ingroup hcrypto_evp
1069 EVP_rc2_64_cbc(void)
1072 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, rc2_64_cbc
);
1076 * The RC4 cipher type
1078 * @return the RC4 EVP_CIPHER pointer.
1080 * @ingroup hcrypto_evp
1087 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, rc4
);
1091 * The RC4-40 cipher type
1093 * @return the RC4-40 EVP_CIPHER pointer.
1095 * @ingroup hcrypto_evp
1102 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, rc4_40
);
1106 * The DES cipher type
1108 * @return the DES-CBC EVP_CIPHER pointer.
1110 * @ingroup hcrypto_evp
1117 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, des_cbc
);
1121 * The tripple DES cipher type
1123 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
1125 * @ingroup hcrypto_evp
1129 EVP_des_ede3_cbc(void)
1132 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, des_ede3_cbc
);
1136 * The AES-128 cipher type
1138 * @return the AES-128 EVP_CIPHER pointer.
1140 * @ingroup hcrypto_evp
1144 EVP_aes_128_cbc(void)
1147 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, aes_128_cbc
);
1151 * The AES-192 cipher type
1153 * @return the AES-192 EVP_CIPHER pointer.
1155 * @ingroup hcrypto_evp
1159 EVP_aes_192_cbc(void)
1162 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, aes_192_cbc
);
1166 * The AES-256 cipher type
1168 * @return the AES-256 EVP_CIPHER pointer.
1170 * @ingroup hcrypto_evp
1174 EVP_aes_256_cbc(void)
1177 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, aes_256_cbc
);
1181 * The AES-128 cipher type
1183 * @return the AES-128 EVP_CIPHER pointer.
1185 * @ingroup hcrypto_evp
1189 EVP_aes_128_cfb8(void)
1192 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, aes_128_cfb8
);
1196 * The AES-192 cipher type
1198 * @return the AES-192 EVP_CIPHER pointer.
1200 * @ingroup hcrypto_evp
1204 EVP_aes_192_cfb8(void)
1207 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, aes_192_cfb8
);
1211 * The AES-256 cipher type
1213 * @return the AES-256 EVP_CIPHER pointer.
1215 * @ingroup hcrypto_evp
1219 EVP_aes_256_cfb8(void)
1222 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, aes_256_cfb8
);
1226 * The Camellia-128 cipher type
1228 * @return the Camellia-128 EVP_CIPHER pointer.
1230 * @ingroup hcrypto_evp
1234 EVP_camellia_128_cbc(void)
1237 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, camellia_128_cbc
);
1241 * The Camellia-198 cipher type
1243 * @return the Camellia-198 EVP_CIPHER pointer.
1245 * @ingroup hcrypto_evp
1249 EVP_camellia_192_cbc(void)
1252 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, camellia_192_cbc
);
1256 * The Camellia-256 cipher type
1258 * @return the Camellia-256 EVP_CIPHER pointer.
1260 * @ingroup hcrypto_evp
1264 EVP_camellia_256_cbc(void)
1267 return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER
, camellia_256_cbc
);
1274 static const struct cipher_name
{
1276 const EVP_CIPHER
*(*func
)(void);
1278 { "des-ede3-cbc", EVP_des_ede3_cbc
},
1279 { "aes-128-cbc", EVP_aes_128_cbc
},
1280 { "aes-192-cbc", EVP_aes_192_cbc
},
1281 { "aes-256-cbc", EVP_aes_256_cbc
},
1282 { "aes-128-cfb8", EVP_aes_128_cfb8
},
1283 { "aes-192-cfb8", EVP_aes_192_cfb8
},
1284 { "aes-256-cfb8", EVP_aes_256_cfb8
},
1285 { "camellia-128-cbc", EVP_camellia_128_cbc
},
1286 { "camellia-192-cbc", EVP_camellia_192_cbc
},
1287 { "camellia-256-cbc", EVP_camellia_256_cbc
}
1291 * Get the cipher type using their name.
1293 * @param name the name of the cipher.
1295 * @return the selected EVP_CIPHER pointer or NULL if not found.
1297 * @ingroup hcrypto_evp
1301 EVP_get_cipherbyname(const char *name
)
1304 for (i
= 0; i
< sizeof(cipher_name
)/sizeof(cipher_name
[0]); i
++) {
1305 if (strcasecmp(cipher_name
[i
].name
, name
) == 0)
1306 return (*cipher_name
[i
].func
)();
1317 #define min(a,b) (((a)>(b))?(b):(a))
1321 * Provides a legancy string to key function, used in PEM files.
1323 * New protocols should use new string to key functions like NIST
1324 * SP56-800A or PKCS#5 v2.0 (see PKCS5_PBKDF2_HMAC_SHA1()).
1326 * @param type type of cipher to use
1327 * @param md message digest to use
1328 * @param salt salt salt string, should be an binary 8 byte buffer.
1329 * @param data the password/input key string.
1330 * @param datalen length of data parameter.
1331 * @param count iteration counter.
1332 * @param keydata output keydata, needs to of the size EVP_CIPHER_key_length().
1333 * @param ivdata output ivdata, needs to of the size EVP_CIPHER_block_size().
1335 * @return the size of derived key.
1337 * @ingroup hcrypto_evp
1341 EVP_BytesToKey(const EVP_CIPHER
*type
,
1344 const void *data
, size_t datalen
,
1349 unsigned int ivlen
, keylen
;
1351 unsigned int mds
= 0, i
;
1352 unsigned char *key
= keydata
;
1353 unsigned char *iv
= ivdata
;
1357 keylen
= EVP_CIPHER_key_length(type
);
1358 ivlen
= EVP_CIPHER_iv_length(type
);
1363 buf
= malloc(EVP_MD_size(md
));
1367 EVP_MD_CTX_init(&c
);
1371 EVP_DigestInit_ex(&c
, md
, NULL
);
1373 EVP_DigestUpdate(&c
, buf
, mds
);
1375 EVP_DigestUpdate(&c
,data
,datalen
);
1377 #define PKCS5_SALT_LEN 8
1380 EVP_DigestUpdate(&c
, salt
, PKCS5_SALT_LEN
);
1382 EVP_DigestFinal_ex(&c
, buf
, &mds
);
1383 assert(mds
== EVP_MD_size(md
));
1385 for (i
= 1; i
< count
; i
++) {
1386 EVP_DigestInit_ex(&c
, md
, NULL
);
1387 EVP_DigestUpdate(&c
, buf
, mds
);
1388 EVP_DigestFinal_ex(&c
, buf
, &mds
);
1389 assert(mds
== EVP_MD_size(md
));
1394 size_t sz
= min(keylen
, mds
);
1396 memcpy(key
, buf
, sz
);
1402 if (ivlen
&& mds
> i
) {
1403 size_t sz
= min(ivlen
, (mds
- i
));
1405 memcpy(iv
, &buf
[i
], sz
);
1410 if (keylen
== 0 && ivlen
== 0)
1414 EVP_MD_CTX_cleanup(&c
);
1417 return EVP_CIPHER_key_length(type
);
1421 * Generate a random key for the specificed EVP_CIPHER.
1423 * @param ctx EVP_CIPHER_CTX type to build the key for.
1424 * @param key return key, must be at least EVP_CIPHER_key_length() byte long.
1426 * @return 1 for success, 0 for failure.
1428 * @ingroup hcrypto_core
1432 EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX
*ctx
, void *key
)
1434 if (ctx
->cipher
->flags
& EVP_CIPH_RAND_KEY
)
1435 return EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_RAND_KEY
, 0, key
);
1436 if (RAND_bytes(key
, ctx
->key_len
) != 1)
1442 * Perform a operation on a ctx
1444 * @param ctx context to perform operation on.
1445 * @param type type of operation.
1446 * @param arg argument to operation.
1447 * @param data addition data to operation.
1449 * @return 1 for success, 0 for failure.
1451 * @ingroup hcrypto_core
1455 EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX
*ctx
, int type
, int arg
, void *data
)
1457 if (ctx
->cipher
== NULL
|| ctx
->cipher
->ctrl
== NULL
)
1459 return (*ctx
->cipher
->ctrl
)(ctx
, type
, arg
, data
);
1463 * Add all algorithms to the crypto core.
1465 * @ingroup hcrypto_core
1469 OpenSSL_add_all_algorithms(void)
1475 * Add all algorithms to the crypto core using configuration file.
1477 * @ingroup hcrypto_core
1481 OpenSSL_add_all_algorithms_conf(void)
1487 * Add all algorithms to the crypto core, but don't use the
1488 * configuration file.
1490 * @ingroup hcrypto_core
1494 OpenSSL_add_all_algorithms_noconf(void)