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
38 #include <sys/types.h>
45 #include <evp-hcrypto.h>
47 #include <krb5-types.h>
66 aes_init(EVP_CIPHER_CTX
*ctx
,
67 const unsigned char * key
,
68 const unsigned char * iv
,
71 AES_KEY
*k
= ctx
->cipher_data
;
73 AES_set_encrypt_key(key
, ctx
->cipher
->key_len
* 8, k
);
75 AES_set_decrypt_key(key
, ctx
->cipher
->key_len
* 8, k
);
80 aes_do_cipher(EVP_CIPHER_CTX
*ctx
,
82 const unsigned char *in
,
85 AES_KEY
*k
= ctx
->cipher_data
;
86 if (ctx
->flags
& EVP_CIPH_CFB8_MODE
)
87 AES_cfb8_encrypt(in
, out
, size
, k
, ctx
->iv
, ctx
->encrypt
);
89 AES_cbc_encrypt(in
, out
, size
, k
, ctx
->iv
, ctx
->encrypt
);
94 * The AES-128 cipher type (hcrypto)
96 * @return the AES-128 EVP_CIPHER pointer.
98 * @ingroup hcrypto_evp
102 EVP_hcrypto_aes_128_cbc(void)
104 static const EVP_CIPHER aes_128_cbc
= {
124 * The AES-192 cipher type (hcrypto)
126 * @return the AES-192 EVP_CIPHER pointer.
128 * @ingroup hcrypto_evp
132 EVP_hcrypto_aes_192_cbc(void)
134 static const EVP_CIPHER aes_192_cbc
= {
153 * The AES-256 cipher type (hcrypto)
155 * @return the AES-256 EVP_CIPHER pointer.
157 * @ingroup hcrypto_evp
161 EVP_hcrypto_aes_256_cbc(void)
163 static const EVP_CIPHER aes_256_cbc
= {
182 * The AES-128 CFB8 cipher type (hcrypto)
184 * @return the AES-128 EVP_CIPHER pointer.
186 * @ingroup hcrypto_evp
190 EVP_hcrypto_aes_128_cfb8(void)
192 static const EVP_CIPHER aes_128_cfb8
= {
208 return &aes_128_cfb8
;
212 * The AES-192 CFB8 cipher type (hcrypto)
214 * @return the AES-192 EVP_CIPHER pointer.
216 * @ingroup hcrypto_evp
220 EVP_hcrypto_aes_192_cfb8(void)
222 static const EVP_CIPHER aes_192_cfb8
= {
237 return &aes_192_cfb8
;
241 * The AES-256 CFB8 cipher type (hcrypto)
243 * @return the AES-256 EVP_CIPHER pointer.
245 * @ingroup hcrypto_evp
249 EVP_hcrypto_aes_256_cfb8(void)
251 static const EVP_CIPHER aes_256_cfb8
= {
266 return &aes_256_cfb8
;
270 * The message digest SHA256 - hcrypto
272 * @return the message digest type.
274 * @ingroup hcrypto_evp
278 EVP_hcrypto_sha256(void)
280 static const struct hc_evp_md sha256
= {
284 (hc_evp_md_init
)SHA256_Init
,
285 (hc_evp_md_update
)SHA256_Update
,
286 (hc_evp_md_final
)SHA256_Final
,
293 * The message digest SHA384 - hcrypto
295 * @return the message digest type.
297 * @ingroup hcrypto_evp
301 EVP_hcrypto_sha384(void)
303 static const struct hc_evp_md sha384
= {
307 (hc_evp_md_init
)SHA384_Init
,
308 (hc_evp_md_update
)SHA384_Update
,
309 (hc_evp_md_final
)SHA384_Final
,
316 * The message digest SHA512 - hcrypto
318 * @return the message digest type.
320 * @ingroup hcrypto_evp
324 EVP_hcrypto_sha512(void)
326 static const struct hc_evp_md sha512
= {
330 (hc_evp_md_init
)SHA512_Init
,
331 (hc_evp_md_update
)SHA512_Update
,
332 (hc_evp_md_final
)SHA512_Final
,
339 * The message digest SHA1 - hcrypto
341 * @return the message digest type.
343 * @ingroup hcrypto_evp
347 EVP_hcrypto_sha1(void)
349 static const struct hc_evp_md sha1
= {
353 (hc_evp_md_init
)SHA1_Init
,
354 (hc_evp_md_update
)SHA1_Update
,
355 (hc_evp_md_final
)SHA1_Final
,
362 * The message digest MD5 - hcrypto
364 * @return the message digest type.
366 * @ingroup hcrypto_evp
370 EVP_hcrypto_md5(void)
372 static const struct hc_evp_md md5
= {
376 (hc_evp_md_init
)MD5_Init
,
377 (hc_evp_md_update
)MD5_Update
,
378 (hc_evp_md_final
)MD5_Final
,
385 * The message digest MD4 - hcrypto
387 * @return the message digest type.
389 * @ingroup hcrypto_evp
393 EVP_hcrypto_md4(void)
395 static const struct hc_evp_md md4
= {
399 (hc_evp_md_init
)MD4_Init
,
400 (hc_evp_md_update
)MD4_Update
,
401 (hc_evp_md_final
)MD4_Final
,
408 * The message digest MD2 - hcrypto
410 * @return the message digest type.
412 * @ingroup hcrypto_evp
416 EVP_hcrypto_md2(void)
418 static const struct hc_evp_md md2
= {
422 (hc_evp_md_init
)MD2_Init
,
423 (hc_evp_md_update
)MD2_Update
,
424 (hc_evp_md_final
)MD2_Final
,
435 des_cbc_init(EVP_CIPHER_CTX
*ctx
,
436 const unsigned char * key
,
437 const unsigned char * iv
,
440 DES_key_schedule
*k
= ctx
->cipher_data
;
442 memcpy(&deskey
, key
, sizeof(deskey
));
443 DES_set_key_unchecked(&deskey
, k
);
448 des_cbc_do_cipher(EVP_CIPHER_CTX
*ctx
,
450 const unsigned char *in
,
453 DES_key_schedule
*k
= ctx
->cipher_data
;
454 DES_cbc_encrypt(in
, out
, size
,
455 k
, (DES_cblock
*)ctx
->iv
, ctx
->encrypt
);
460 * The DES cipher type
462 * @return the DES-CBC EVP_CIPHER pointer.
464 * @ingroup hcrypto_evp
468 EVP_hcrypto_des_cbc(void)
470 static const EVP_CIPHER des_cbc
= {
479 sizeof(DES_key_schedule
),
492 struct des_ede3_cbc
{
493 DES_key_schedule ks
[3];
497 des_ede3_cbc_init(EVP_CIPHER_CTX
*ctx
,
498 const unsigned char * key
,
499 const unsigned char * iv
,
502 struct des_ede3_cbc
*k
= ctx
->cipher_data
;
505 memcpy(&deskey
, key
, sizeof(deskey
));
506 DES_set_odd_parity(&deskey
);
507 DES_set_key_unchecked(&deskey
, &k
->ks
[0]);
509 memcpy(&deskey
, key
+ 8, sizeof(deskey
));
510 DES_set_odd_parity(&deskey
);
511 DES_set_key_unchecked(&deskey
, &k
->ks
[1]);
513 memcpy(&deskey
, key
+ 16, sizeof(deskey
));
514 DES_set_odd_parity(&deskey
);
515 DES_set_key_unchecked(&deskey
, &k
->ks
[2]);
521 des_ede3_cbc_do_cipher(EVP_CIPHER_CTX
*ctx
,
523 const unsigned char *in
,
526 struct des_ede3_cbc
*k
= ctx
->cipher_data
;
527 DES_ede3_cbc_encrypt(in
, out
, size
,
528 &k
->ks
[0], &k
->ks
[1], &k
->ks
[2],
529 (DES_cblock
*)ctx
->iv
, ctx
->encrypt
);
534 * The tripple DES cipher type - hcrypto
536 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
538 * @ingroup hcrypto_evp
542 EVP_hcrypto_des_ede3_cbc(void)
544 static const EVP_CIPHER des_ede3_cbc
= {
551 des_ede3_cbc_do_cipher
,
553 sizeof(struct des_ede3_cbc
),
559 return &des_ede3_cbc
;
567 unsigned int maximum_effective_key
;
572 rc2_init(EVP_CIPHER_CTX
*ctx
,
573 const unsigned char * key
,
574 const unsigned char * iv
,
577 struct rc2_cbc
*k
= ctx
->cipher_data
;
578 k
->maximum_effective_key
= EVP_CIPHER_CTX_key_length(ctx
) * 8;
580 EVP_CIPHER_CTX_key_length(ctx
),
582 k
->maximum_effective_key
);
587 rc2_do_cipher(EVP_CIPHER_CTX
*ctx
,
589 const unsigned char *in
,
592 struct rc2_cbc
*k
= ctx
->cipher_data
;
593 RC2_cbc_encrypt(in
, out
, size
, &k
->key
, ctx
->iv
, ctx
->encrypt
);
598 * The RC2 cipher type - hcrypto
600 * @return the RC2 EVP_CIPHER pointer.
602 * @ingroup hcrypto_evp
606 EVP_hcrypto_rc2_cbc(void)
608 static const EVP_CIPHER rc2_cbc
= {
613 EVP_CIPH_CBC_MODE
|EVP_CIPH_VARIABLE_LENGTH
,
617 sizeof(struct rc2_cbc
),
627 * The RC2-40 cipher type
629 * @return the RC2-40 EVP_CIPHER pointer.
631 * @ingroup hcrypto_evp
635 EVP_hcrypto_rc2_40_cbc(void)
637 static const EVP_CIPHER rc2_40_cbc
= {
646 sizeof(struct rc2_cbc
),
656 * The RC2-64 cipher type
658 * @return the RC2-64 EVP_CIPHER pointer.
660 * @ingroup hcrypto_evp
664 EVP_hcrypto_rc2_64_cbc(void)
666 static const EVP_CIPHER rc2_64_cbc
= {
675 sizeof(struct rc2_cbc
),
685 camellia_init(EVP_CIPHER_CTX
*ctx
,
686 const unsigned char * key
,
687 const unsigned char * iv
,
690 CAMELLIA_KEY
*k
= ctx
->cipher_data
;
691 k
->bits
= ctx
->cipher
->key_len
* 8;
692 CAMELLIA_set_key(key
, ctx
->cipher
->key_len
* 8, k
);
697 camellia_do_cipher(EVP_CIPHER_CTX
*ctx
,
699 const unsigned char *in
,
702 CAMELLIA_KEY
*k
= ctx
->cipher_data
;
703 CAMELLIA_cbc_encrypt(in
, out
, size
, k
, ctx
->iv
, ctx
->encrypt
);
708 * The Camellia-128 cipher type - hcrypto
710 * @return the Camellia-128 EVP_CIPHER pointer.
712 * @ingroup hcrypto_evp
716 EVP_hcrypto_camellia_128_cbc(void)
718 static const EVP_CIPHER cipher
= {
727 sizeof(CAMELLIA_KEY
),
737 * The Camellia-198 cipher type - hcrypto
739 * @return the Camellia-198 EVP_CIPHER pointer.
741 * @ingroup hcrypto_evp
745 EVP_hcrypto_camellia_192_cbc(void)
747 static const EVP_CIPHER cipher
= {
756 sizeof(CAMELLIA_KEY
),
766 * The Camellia-256 cipher type - hcrypto
768 * @return the Camellia-256 EVP_CIPHER pointer.
770 * @ingroup hcrypto_evp
774 EVP_hcrypto_camellia_256_cbc(void)
776 static const EVP_CIPHER cipher
= {
785 sizeof(CAMELLIA_KEY
),
795 rc4_init(EVP_CIPHER_CTX
*ctx
,
796 const unsigned char *key
,
797 const unsigned char *iv
,
800 RC4_KEY
*k
= ctx
->cipher_data
;
801 RC4_set_key(k
, ctx
->key_len
, key
);
806 rc4_do_cipher(EVP_CIPHER_CTX
*ctx
,
808 const unsigned char *in
,
811 RC4_KEY
*k
= ctx
->cipher_data
;
812 RC4(k
, size
, in
, out
);
817 EVP_hcrypto_rc4(void)
819 static const EVP_CIPHER rc4
= {
824 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,
839 EVP_hcrypto_rc4_40(void)
841 static const EVP_CIPHER rc4_40
= {
846 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,