2 * Copyright (c) 2006 - 2007 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
38 __RCSID("$Heimdal: evp.c 22379 2007-12-29 11:13:26Z lha $"
41 #include <sys/types.h>
49 #include <krb5-types.h>
62 * @page page_evp EVP - generic crypto interface
64 * See the library functions here: @ref hcrypto_evp
68 typedef int (*evp_md_init
)(EVP_MD_CTX
*);
69 typedef int (*evp_md_update
)(EVP_MD_CTX
*,const void *, size_t);
70 typedef int (*evp_md_final
)(void *, EVP_MD_CTX
*);
71 typedef int (*evp_md_cleanup
)(EVP_MD_CTX
*);
80 evp_md_cleanup cleanup
;
84 * Return the output size of the message digest function.
86 * @param md the evp message
88 * @return size output size of the message digest function.
90 * @ingroup hcrypto_evp
94 EVP_MD_size(const EVP_MD
*md
)
100 * Return the blocksize of the message digest function.
102 * @param md the evp message
104 * @return size size of the message digest block size
106 * @ingroup hcrypto_evp
110 EVP_MD_block_size(const EVP_MD
*md
)
112 return md
->block_size
;
116 * Allocate a messsage digest context object. Free with
117 * EVP_MD_CTX_destroy().
119 * @return a newly allocated message digest context object.
121 * @ingroup hcrypto_evp
125 EVP_MD_CTX_create(void)
127 return calloc(1, sizeof(EVP_MD_CTX
));
131 * Initiate a messsage digest context object. Deallocate with
132 * EVP_MD_CTX_cleanup(). Please use EVP_MD_CTX_create() instead.
134 * @param ctx variable to initiate.
136 * @ingroup hcrypto_evp
140 EVP_MD_CTX_init(EVP_MD_CTX
*ctx
)
142 memset(ctx
, 0, sizeof(*ctx
));
146 * Free a messsage digest context object.
148 * @param ctx context to free.
150 * @ingroup hcrypto_evp
154 EVP_MD_CTX_destroy(EVP_MD_CTX
*ctx
)
156 EVP_MD_CTX_cleanup(ctx
);
161 * Free the resources used by the EVP_MD context.
163 * @param ctx the context to free the resources from.
165 * @return 1 on success.
167 * @ingroup hcrypto_evp
171 EVP_MD_CTX_cleanup(EVP_MD_CTX
*ctx
)
173 if (ctx
->md
&& ctx
->md
->cleanup
)
174 (ctx
->md
->cleanup
)(ctx
);
178 memset(ctx
, 0, sizeof(*ctx
));
183 * Get the EVP_MD use for a specified context.
185 * @param ctx the EVP_MD context to get the EVP_MD for.
187 * @return the EVP_MD used for the context.
189 * @ingroup hcrypto_evp
193 EVP_MD_CTX_md(EVP_MD_CTX
*ctx
)
199 * Return the output size of the message digest function.
201 * @param ctx the evp message digest context
203 * @return size output size of the message digest function.
205 * @ingroup hcrypto_evp
209 EVP_MD_CTX_size(EVP_MD_CTX
*ctx
)
211 return EVP_MD_size(ctx
->md
);
215 * Return the blocksize of the message digest function.
217 * @param ctx the evp message digest context
219 * @return size size of the message digest block size
221 * @ingroup hcrypto_evp
225 EVP_MD_CTX_block_size(EVP_MD_CTX
*ctx
)
227 return EVP_MD_block_size(ctx
->md
);
231 * Init a EVP_MD_CTX for use a specific message digest and engine.
233 * @param ctx the message digest context to init.
234 * @param md the message digest to use.
235 * @param engine the engine to use, NULL to use the default engine.
237 * @return 1 on success.
239 * @ingroup hcrypto_evp
243 EVP_DigestInit_ex(EVP_MD_CTX
*ctx
, const EVP_MD
*md
, ENGINE
*engine
)
245 if (ctx
->md
!= md
|| ctx
->engine
!= engine
) {
246 EVP_MD_CTX_cleanup(ctx
);
248 ctx
->engine
= engine
;
250 ctx
->ptr
= calloc(1, md
->ctx_size
);
251 if (ctx
->ptr
== NULL
)
254 (ctx
->md
->init
)(ctx
->ptr
);
259 * Update the digest with some data.
261 * @param ctx the context to update
262 * @param data the data to update the context with
263 * @param size length of data
265 * @return 1 on success.
267 * @ingroup hcrypto_evp
271 EVP_DigestUpdate(EVP_MD_CTX
*ctx
, const void *data
, size_t size
)
273 (ctx
->md
->update
)(ctx
->ptr
, data
, size
);
278 * Complete the message digest.
280 * @param ctx the context to complete.
281 * @param hash the output of the message digest function. At least
283 * @param size the output size of hash.
285 * @return 1 on success.
287 * @ingroup hcrypto_evp
291 EVP_DigestFinal_ex(EVP_MD_CTX
*ctx
, void *hash
, unsigned int *size
)
293 (ctx
->md
->final
)(hash
, ctx
->ptr
);
295 *size
= ctx
->md
->hash_size
;
300 * Do the whole EVP_MD_CTX_create(), EVP_DigestInit_ex(),
301 * EVP_DigestUpdate(), EVP_DigestFinal_ex(), EVP_MD_CTX_destroy()
304 * @param data the data to update the context with
305 * @param dsize length of data
306 * @param hash output data of at least EVP_MD_size() length.
307 * @param hsize output length of hash.
308 * @param md message digest to use
309 * @param engine engine to use, NULL for default engine.
311 * @return 1 on success.
313 * @ingroup hcrypto_evp
317 EVP_Digest(const void *data
, size_t dsize
, void *hash
, unsigned int *hsize
,
318 const EVP_MD
*md
, ENGINE
*engine
)
323 ctx
= EVP_MD_CTX_create();
326 ret
= EVP_DigestInit_ex(ctx
, md
, engine
);
328 EVP_MD_CTX_destroy(ctx
);
331 ret
= EVP_DigestUpdate(ctx
, data
, dsize
);
333 EVP_MD_CTX_destroy(ctx
);
336 ret
= EVP_DigestFinal_ex(ctx
, hash
, hsize
);
337 EVP_MD_CTX_destroy(ctx
);
342 * The message digest SHA256
344 * @return the message digest type.
346 * @ingroup hcrypto_evp
352 static const struct hc_evp_md sha256
= {
356 (evp_md_init
)SHA256_Init
,
357 (evp_md_update
)SHA256_Update
,
358 (evp_md_final
)SHA256_Final
,
364 static const struct hc_evp_md sha1
= {
368 (evp_md_init
)SHA1_Init
,
369 (evp_md_update
)SHA1_Update
,
370 (evp_md_final
)SHA1_Final
,
375 * The message digest SHA1
377 * @return the message digest type.
379 * @ingroup hcrypto_evp
389 * The message digest SHA1
391 * @return the message digest type.
393 * @ingroup hcrypto_evp
403 * The message digest MD5
405 * @return the message digest type.
407 * @ingroup hcrypto_evp
413 static const struct hc_evp_md md5
= {
417 (evp_md_init
)MD5_Init
,
418 (evp_md_update
)MD5_Update
,
419 (evp_md_final
)MD5_Final
,
426 * The message digest MD4
428 * @return the message digest type.
430 * @ingroup hcrypto_evp
436 static const struct hc_evp_md md4
= {
440 (evp_md_init
)MD4_Init
,
441 (evp_md_update
)MD4_Update
,
442 (evp_md_final
)MD4_Final
,
449 * The message digest MD2
451 * @return the message digest type.
453 * @ingroup hcrypto_evp
459 static const struct hc_evp_md md2
= {
463 (evp_md_init
)MD2_Init
,
464 (evp_md_update
)MD2_Update
,
465 (evp_md_final
)MD2_Final
,
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 (evp_md_init
)null_Init
,
504 (evp_md_update
)null_Update
,
505 (evp_md_final
)null_Final
,
512 void EVP_MD_CTX_init(EVP_MD_CTX
*ctx
);
513 int EVP_DigestInit(EVP_MD_CTX
*ctx
, const EVP_MD
*type
);
514 int EVP_DigestFinal(EVP_MD_CTX
*ctx
,unsigned char *md
,unsigned int *s
);
515 int EVP_SignFinal(EVP_MD_CTX
*, void *, size_t *, EVP_PKEY
*);
516 int EVP_VerifyFinal(EVP_MD_CTX
*, const void *, size_t, EVP_PKEY
*);
520 * Return the block size of the cipher.
522 * @param c cipher to get the block size from.
524 * @return the block size of the cipher.
526 * @ingroup hcrypto_evp
530 EVP_CIPHER_block_size(const EVP_CIPHER
*c
)
532 return c
->block_size
;
536 * Return the key size of the cipher.
538 * @param c cipher to get the key size from.
540 * @return the key size of the cipher.
542 * @ingroup hcrypto_evp
546 EVP_CIPHER_key_length(const EVP_CIPHER
*c
)
552 * Return the IV size of the cipher.
554 * @param c cipher to get the IV size from.
556 * @return the IV size of the cipher.
558 * @ingroup hcrypto_evp
562 EVP_CIPHER_iv_length(const EVP_CIPHER
*c
)
568 * Initiate a EVP_CIPHER_CTX context. Clean up with
569 * EVP_CIPHER_CTX_cleanup().
571 * @param c the cipher initiate.
573 * @ingroup hcrypto_evp
577 EVP_CIPHER_CTX_init(EVP_CIPHER_CTX
*c
)
579 memset(c
, 0, sizeof(*c
));
583 * Clean up the EVP_CIPHER_CTX context.
585 * @param c the cipher to clean up.
587 * @return 1 on success.
589 * @ingroup hcrypto_evp
593 EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX
*c
)
595 if (c
->cipher
&& c
->cipher
->cleanup
)
596 c
->cipher
->cleanup(c
);
597 if (c
->cipher_data
) {
598 free(c
->cipher_data
);
599 c
->cipher_data
= NULL
;
606 EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX
*c
, int length
)
612 EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX
*c
, int pad
)
619 * Return the EVP_CIPHER for a EVP_CIPHER_CTX context.
621 * @param ctx the context to get the cipher type from.
623 * @return the EVP_CIPHER pointer.
625 * @ingroup hcrypto_evp
629 EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX
*ctx
)
635 * Return the block size of the cipher context.
637 * @param ctx cipher context to get the block size from.
639 * @return the block size of the cipher context.
641 * @ingroup hcrypto_evp
645 EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX
*ctx
)
647 return EVP_CIPHER_block_size(ctx
->cipher
);
651 * Return the key size of the cipher context.
653 * @param ctx cipher context to get the key size from.
655 * @return the key size of the cipher context.
657 * @ingroup hcrypto_evp
661 EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX
*ctx
)
663 return EVP_CIPHER_key_length(ctx
->cipher
);
667 * Return the IV size of the cipher context.
669 * @param ctx cipher context to get the IV size from.
671 * @return the IV size of the cipher context.
673 * @ingroup hcrypto_evp
677 EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX
*ctx
)
679 return EVP_CIPHER_iv_length(ctx
->cipher
);
683 * Get the flags for an EVP_CIPHER_CTX context.
685 * @param ctx the EVP_CIPHER_CTX to get the flags from
687 * @return the flags for an EVP_CIPHER_CTX.
689 * @ingroup hcrypto_evp
693 EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX
*ctx
)
695 return ctx
->cipher
->flags
;
699 * Get the mode for an EVP_CIPHER_CTX context.
701 * @param ctx the EVP_CIPHER_CTX to get the mode from
703 * @return the mode for an EVP_CIPHER_CTX.
705 * @ingroup hcrypto_evp
709 EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX
*ctx
)
711 return EVP_CIPHER_CTX_flags(ctx
) & EVP_CIPH_MODE
;
715 * Get the app data for an EVP_CIPHER_CTX context.
717 * @param ctx the EVP_CIPHER_CTX to get the app data from
719 * @return the app data for an EVP_CIPHER_CTX.
721 * @ingroup hcrypto_evp
725 EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX
*ctx
)
727 return ctx
->app_data
;
731 * Set the app data for an EVP_CIPHER_CTX context.
733 * @param ctx the EVP_CIPHER_CTX to set the app data for
734 * @param data the app data to set for an EVP_CIPHER_CTX.
736 * @ingroup hcrypto_evp
740 EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX
*ctx
, void *data
)
742 ctx
->app_data
= data
;
746 * Initiate the EVP_CIPHER_CTX context to encrypt or decrypt data.
747 * Clean up with EVP_CIPHER_CTX_cleanup().
749 * @param ctx context to initiate
750 * @param c cipher to use.
751 * @param engine crypto engine to use, NULL to select default.
752 * @param key the crypto key to use, NULL will use the previous value.
753 * @param iv the IV to use, NULL will use the previous value.
754 * @param encp non zero will encrypt, -1 use the previous value.
756 * @return 1 on success.
758 * @ingroup hcrypto_evp
762 EVP_CipherInit_ex(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*c
, ENGINE
*engine
,
763 const void *key
, const void *iv
, int encp
)
768 ctx
->encrypt
= (encp
? 1 : 0);
770 if (c
&& (c
!= ctx
->cipher
)) {
771 EVP_CIPHER_CTX_cleanup(ctx
);
773 ctx
->key_len
= c
->key_len
;
775 ctx
->cipher_data
= malloc(c
->ctx_size
);
776 if (ctx
->cipher_data
== NULL
&& c
->ctx_size
!= 0)
779 } else if (ctx
->cipher
== NULL
) {
780 /* reuse of cipher, but not any cipher ever set! */
784 switch (EVP_CIPHER_CTX_flags(ctx
)) {
785 case EVP_CIPH_CBC_MODE
:
787 assert(EVP_CIPHER_CTX_iv_length(ctx
) <= sizeof(ctx
->iv
));
790 memcpy(ctx
->oiv
, iv
, EVP_CIPHER_CTX_iv_length(ctx
));
791 memcpy(ctx
->iv
, ctx
->oiv
, EVP_CIPHER_CTX_iv_length(ctx
));
797 if (key
|| (ctx
->cipher
->flags
& EVP_CIPH_ALWAYS_CALL_INIT
))
798 ctx
->cipher
->init(ctx
, key
, iv
, encp
);
804 * Encypher/decypher data
806 * @param ctx the cipher context.
807 * @param out out data from the operation.
808 * @param in in data to the operation.
809 * @param size length of data.
811 * @return 1 on success.
815 EVP_Cipher(EVP_CIPHER_CTX
*ctx
, void *out
, const void *in
,size_t size
)
817 return ctx
->cipher
->do_cipher(ctx
, out
, in
, size
);
825 enc_null_init(EVP_CIPHER_CTX
*ctx
,
826 const unsigned char * key
,
827 const unsigned char * iv
,
834 enc_null_do_cipher(EVP_CIPHER_CTX
*ctx
,
836 const unsigned char *in
,
839 memmove(out
, in
, size
);
844 enc_null_cleanup(EVP_CIPHER_CTX
*ctx
)
850 * The NULL cipher type, does no encryption/decryption.
852 * @return the null EVP_CIPHER pointer.
854 * @ingroup hcrypto_evp
860 static const EVP_CIPHER enc_null
= {
883 unsigned int maximum_effective_key
;
888 rc2_init(EVP_CIPHER_CTX
*ctx
,
889 const unsigned char * key
,
890 const unsigned char * iv
,
893 struct rc2_cbc
*k
= ctx
->cipher_data
;
894 k
->maximum_effective_key
= EVP_CIPHER_CTX_key_length(ctx
) * 8;
896 EVP_CIPHER_CTX_key_length(ctx
),
898 k
->maximum_effective_key
);
903 rc2_do_cipher(EVP_CIPHER_CTX
*ctx
,
905 const unsigned char *in
,
908 struct rc2_cbc
*k
= ctx
->cipher_data
;
909 RC2_cbc_encrypt(in
, out
, size
, &k
->key
, ctx
->iv
, ctx
->encrypt
);
914 rc2_cleanup(EVP_CIPHER_CTX
*ctx
)
916 memset(ctx
->cipher_data
, 0, sizeof(struct rc2_cbc
));
921 * The RC2 cipher type
923 * @return the RC2 EVP_CIPHER pointer.
925 * @ingroup hcrypto_evp
931 static const EVP_CIPHER rc2_cbc
= {
940 sizeof(struct rc2_cbc
),
950 * The RC2-40 cipher type
952 * @return the RC2-40 EVP_CIPHER pointer.
954 * @ingroup hcrypto_evp
960 static const EVP_CIPHER rc2_40_cbc
= {
969 sizeof(struct rc2_cbc
),
979 * The RC2-64 cipher type
981 * @return the RC2-64 EVP_CIPHER pointer.
983 * @ingroup hcrypto_evp
989 static const EVP_CIPHER rc2_64_cbc
= {
998 sizeof(struct rc2_cbc
),
1008 * The RC4 cipher type
1010 * @return the RC4 EVP_CIPHER pointer.
1012 * @ingroup hcrypto_evp
1018 printf("evp rc4\n");
1024 * The RC4-40 cipher type
1026 * @return the RC4-40 EVP_CIPHER pointer.
1028 * @ingroup hcrypto_evp
1034 printf("evp rc4_40\n");
1043 struct des_ede3_cbc
{
1044 DES_key_schedule ks
[3];
1048 des_ede3_cbc_init(EVP_CIPHER_CTX
*ctx
,
1049 const unsigned char * key
,
1050 const unsigned char * iv
,
1053 struct des_ede3_cbc
*k
= ctx
->cipher_data
;
1055 DES_key_sched((DES_cblock
*)(key
), &k
->ks
[0]);
1056 DES_key_sched((DES_cblock
*)(key
+ 8), &k
->ks
[1]);
1057 DES_key_sched((DES_cblock
*)(key
+ 16), &k
->ks
[2]);
1063 des_ede3_cbc_do_cipher(EVP_CIPHER_CTX
*ctx
,
1065 const unsigned char *in
,
1068 struct des_ede3_cbc
*k
= ctx
->cipher_data
;
1069 DES_ede3_cbc_encrypt(in
, out
, size
,
1070 &k
->ks
[0], &k
->ks
[1], &k
->ks
[2],
1071 (DES_cblock
*)ctx
->iv
, ctx
->encrypt
);
1076 des_ede3_cbc_cleanup(EVP_CIPHER_CTX
*ctx
)
1078 memset(ctx
->cipher_data
, 0, sizeof(struct des_ede3_cbc
));
1083 * The tripple DES cipher type
1085 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
1087 * @ingroup hcrypto_evp
1091 EVP_des_ede3_cbc(void)
1093 static const EVP_CIPHER des_ede3_cbc
= {
1100 des_ede3_cbc_do_cipher
,
1101 des_ede3_cbc_cleanup
,
1102 sizeof(struct des_ede3_cbc
),
1108 return &des_ede3_cbc
;
1116 aes_init(EVP_CIPHER_CTX
*ctx
,
1117 const unsigned char * key
,
1118 const unsigned char * iv
,
1121 AES_KEY
*k
= ctx
->cipher_data
;
1123 AES_set_encrypt_key(key
, ctx
->cipher
->key_len
* 8, k
);
1125 AES_set_decrypt_key(key
, ctx
->cipher
->key_len
* 8, k
);
1130 aes_do_cipher(EVP_CIPHER_CTX
*ctx
,
1132 const unsigned char *in
,
1135 AES_KEY
*k
= ctx
->cipher_data
;
1136 AES_cbc_encrypt(in
, out
, size
, k
, ctx
->iv
, ctx
->encrypt
);
1141 aes_cleanup(EVP_CIPHER_CTX
*ctx
)
1143 memset(ctx
->cipher_data
, 0, sizeof(AES_KEY
));
1148 * The AES-128 cipher type
1150 * @return the AES-128 EVP_CIPHER pointer.
1152 * @ingroup hcrypto_evp
1156 EVP_aes_128_cbc(void)
1158 static const EVP_CIPHER aes_128_cbc
= {
1173 return &aes_128_cbc
;
1177 * The AES-192 cipher type
1179 * @return the AES-192 EVP_CIPHER pointer.
1181 * @ingroup hcrypto_evp
1185 EVP_aes_192_cbc(void)
1187 static const EVP_CIPHER aes_192_cbc
= {
1202 return &aes_192_cbc
;
1206 * The AES-256 cipher type
1208 * @return the AES-256 EVP_CIPHER pointer.
1210 * @ingroup hcrypto_evp
1214 EVP_aes_256_cbc(void)
1216 static const EVP_CIPHER aes_256_cbc
= {
1231 return &aes_256_cbc
;
1235 camellia_init(EVP_CIPHER_CTX
*ctx
,
1236 const unsigned char * key
,
1237 const unsigned char * iv
,
1240 CAMELLIA_KEY
*k
= ctx
->cipher_data
;
1241 k
->bits
= ctx
->cipher
->key_len
* 8;
1242 CAMELLIA_set_key(key
, ctx
->cipher
->key_len
* 8, k
);
1247 camellia_do_cipher(EVP_CIPHER_CTX
*ctx
,
1249 const unsigned char *in
,
1252 CAMELLIA_KEY
*k
= ctx
->cipher_data
;
1253 CAMELLIA_cbc_encrypt(in
, out
, size
, k
, ctx
->iv
, ctx
->encrypt
);
1258 camellia_cleanup(EVP_CIPHER_CTX
*ctx
)
1260 memset(ctx
->cipher_data
, 0, sizeof(CAMELLIA_KEY
));
1265 * The Camellia-128 cipher type
1267 * @return the Camellia-128 EVP_CIPHER pointer.
1269 * @ingroup hcrypto_evp
1273 EVP_camellia_128_cbc(void)
1275 static const EVP_CIPHER cipher
= {
1284 sizeof(CAMELLIA_KEY
),
1294 * The Camellia-198 cipher type
1296 * @return the Camellia-198 EVP_CIPHER pointer.
1298 * @ingroup hcrypto_evp
1302 EVP_camellia_192_cbc(void)
1304 static const EVP_CIPHER cipher
= {
1313 sizeof(CAMELLIA_KEY
),
1323 * The Camellia-256 cipher type
1325 * @return the Camellia-256 EVP_CIPHER pointer.
1327 * @ingroup hcrypto_evp
1331 EVP_camellia_256_cbc(void)
1333 static const EVP_CIPHER cipher
= {
1342 sizeof(CAMELLIA_KEY
),
1355 static const struct cipher_name
{
1357 const EVP_CIPHER
*(*func
)(void);
1359 { "des-ede3-cbc", EVP_des_ede3_cbc
},
1360 { "aes-128-cbc", EVP_aes_128_cbc
},
1361 { "aes-192-cbc", EVP_aes_192_cbc
},
1362 { "aes-256-cbc", EVP_aes_256_cbc
},
1363 { "camellia-128-cbc", EVP_camellia_128_cbc
},
1364 { "camellia-192-cbc", EVP_camellia_192_cbc
},
1365 { "camellia-256-cbc", EVP_camellia_256_cbc
}
1369 * Get the cipher type using their name.
1371 * @param name the name of the cipher.
1373 * @return the selected EVP_CIPHER pointer or NULL if not found.
1375 * @ingroup hcrypto_evp
1379 EVP_get_cipherbyname(const char *name
)
1382 for (i
= 0; i
< sizeof(cipher_name
)/sizeof(cipher_name
[0]); i
++) {
1383 if (strcasecmp(cipher_name
[i
].name
, name
) == 0)
1384 return (*cipher_name
[i
].func
)();
1395 #define min(a,b) (((a)>(b))?(b):(a))
1399 * Provides a legancy string to key function, used in PEM files.
1401 * New protocols should use new string to key functions like NIST
1402 * SP56-800A or PKCS#5 v2.0 (see PKCS5_PBKDF2_HMAC_SHA1()).
1404 * @param type type of cipher to use
1405 * @param md message digest to use
1406 * @param salt salt salt string, should be an binary 8 byte buffer.
1407 * @param data the password/input key string.
1408 * @param datalen length of data parameter.
1409 * @param count iteration counter.
1410 * @param keydata output keydata, needs to of the size EVP_CIPHER_key_length().
1411 * @param ivdata output ivdata, needs to of the size EVP_CIPHER_block_size().
1413 * @return the size of derived key.
1415 * @ingroup hcrypto_evp
1419 EVP_BytesToKey(const EVP_CIPHER
*type
,
1422 const void *data
, size_t datalen
,
1427 int ivlen
, keylen
, first
= 0;
1428 unsigned int mds
= 0, i
;
1429 unsigned char *key
= keydata
;
1430 unsigned char *iv
= ivdata
;
1434 keylen
= EVP_CIPHER_key_length(type
);
1435 ivlen
= EVP_CIPHER_iv_length(type
);
1440 buf
= malloc(EVP_MD_size(md
));
1444 EVP_MD_CTX_init(&c
);
1448 EVP_DigestInit_ex(&c
, md
, NULL
);
1450 EVP_DigestUpdate(&c
, buf
, mds
);
1452 EVP_DigestUpdate(&c
,data
,datalen
);
1454 #define PKCS5_SALT_LEN 8
1457 EVP_DigestUpdate(&c
, salt
, PKCS5_SALT_LEN
);
1459 EVP_DigestFinal_ex(&c
, buf
, &mds
);
1460 assert(mds
== EVP_MD_size(md
));
1462 for (i
= 1; i
< count
; i
++) {
1463 EVP_DigestInit_ex(&c
, md
, NULL
);
1464 EVP_DigestUpdate(&c
, buf
, mds
);
1465 EVP_DigestFinal_ex(&c
, buf
, &mds
);
1466 assert(mds
== EVP_MD_size(md
));
1471 size_t sz
= min(keylen
, mds
);
1473 memcpy(key
, buf
, sz
);
1479 if (ivlen
&& mds
> i
) {
1480 size_t sz
= min(ivlen
, (mds
- i
));
1482 memcpy(iv
, &buf
[i
], sz
);
1487 if (keylen
== 0 && ivlen
== 0)
1491 EVP_MD_CTX_cleanup(&c
);
1494 return EVP_CIPHER_key_length(type
);
1498 * Add all algorithms to the crypto core.
1500 * @ingroup hcrypto_core
1504 OpenSSL_add_all_algorithms(void)
1510 * Add all algorithms to the crypto core using configuration file.
1512 * @ingroup hcrypto_core
1516 OpenSSL_add_all_algorithms_conf(void)
1522 * Add all algorithms to the crypto core, but don't use the
1523 * configuration file.
1525 * @ingroup hcrypto_core
1529 OpenSSL_add_all_algorithms_noconf(void)