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
42 #include <evp-hcrypto.h>
44 #include <krb5-types.h>
63 aes_init(EVP_CIPHER_CTX
*ctx
,
64 const unsigned char * key
,
65 const unsigned char * iv
,
68 AES_KEY
*k
= ctx
->cipher_data
;
69 if (ctx
->encrypt
|| EVP_CIPHER_CTX_mode(ctx
) == EVP_CIPH_CFB8_MODE
)
70 AES_set_encrypt_key(key
, ctx
->cipher
->key_len
* 8, k
);
72 AES_set_decrypt_key(key
, ctx
->cipher
->key_len
* 8, k
);
77 aes_do_cipher(EVP_CIPHER_CTX
*ctx
,
79 const unsigned char *in
,
82 AES_KEY
*k
= ctx
->cipher_data
;
83 if (EVP_CIPHER_CTX_mode(ctx
) == EVP_CIPH_CFB8_MODE
)
84 AES_cfb8_encrypt(in
, out
, size
, k
, ctx
->iv
, ctx
->encrypt
);
86 AES_cbc_encrypt(in
, out
, size
, k
, ctx
->iv
, ctx
->encrypt
);
91 * The AES-128 cipher type (hcrypto)
93 * @return the AES-128 EVP_CIPHER pointer.
95 * @ingroup hcrypto_evp
99 EVP_hcrypto_aes_128_cbc(void)
101 static const EVP_CIPHER aes_128_cbc
= {
121 * The AES-192 cipher type (hcrypto)
123 * @return the AES-192 EVP_CIPHER pointer.
125 * @ingroup hcrypto_evp
129 EVP_hcrypto_aes_192_cbc(void)
131 static const EVP_CIPHER aes_192_cbc
= {
150 * The AES-256 cipher type (hcrypto)
152 * @return the AES-256 EVP_CIPHER pointer.
154 * @ingroup hcrypto_evp
158 EVP_hcrypto_aes_256_cbc(void)
160 static const EVP_CIPHER aes_256_cbc
= {
179 * The AES-128 CFB8 cipher type (hcrypto)
181 * @return the AES-128 EVP_CIPHER pointer.
183 * @ingroup hcrypto_evp
187 EVP_hcrypto_aes_128_cfb8(void)
189 static const EVP_CIPHER aes_128_cfb8
= {
205 return &aes_128_cfb8
;
209 * The AES-192 CFB8 cipher type (hcrypto)
211 * @return the AES-192 EVP_CIPHER pointer.
213 * @ingroup hcrypto_evp
217 EVP_hcrypto_aes_192_cfb8(void)
219 static const EVP_CIPHER aes_192_cfb8
= {
234 return &aes_192_cfb8
;
238 * The AES-256 CFB8 cipher type (hcrypto)
240 * @return the AES-256 EVP_CIPHER pointer.
242 * @ingroup hcrypto_evp
246 EVP_hcrypto_aes_256_cfb8(void)
248 static const EVP_CIPHER aes_256_cfb8
= {
263 return &aes_256_cfb8
;
267 * The message digest SHA256 - hcrypto
269 * @return the message digest type.
271 * @ingroup hcrypto_evp
275 EVP_hcrypto_sha256(void)
277 static const struct hc_evp_md sha256
= {
281 (hc_evp_md_init
)SHA256_Init
,
282 (hc_evp_md_update
)SHA256_Update
,
283 (hc_evp_md_final
)SHA256_Final
,
290 * The message digest SHA384 - hcrypto
292 * @return the message digest type.
294 * @ingroup hcrypto_evp
298 EVP_hcrypto_sha384(void)
300 static const struct hc_evp_md sha384
= {
304 (hc_evp_md_init
)SHA384_Init
,
305 (hc_evp_md_update
)SHA384_Update
,
306 (hc_evp_md_final
)SHA384_Final
,
313 * The message digest SHA512 - hcrypto
315 * @return the message digest type.
317 * @ingroup hcrypto_evp
321 EVP_hcrypto_sha512(void)
323 static const struct hc_evp_md sha512
= {
327 (hc_evp_md_init
)SHA512_Init
,
328 (hc_evp_md_update
)SHA512_Update
,
329 (hc_evp_md_final
)SHA512_Final
,
336 * The message digest SHA1 - hcrypto
338 * @return the message digest type.
340 * @ingroup hcrypto_evp
344 EVP_hcrypto_sha1(void)
346 static const struct hc_evp_md sha1
= {
350 (hc_evp_md_init
)SHA1_Init
,
351 (hc_evp_md_update
)SHA1_Update
,
352 (hc_evp_md_final
)SHA1_Final
,
359 * The message digest MD5 - hcrypto
361 * @return the message digest type.
363 * @ingroup hcrypto_evp
367 EVP_hcrypto_md5(void)
369 static const struct hc_evp_md md5
= {
373 (hc_evp_md_init
)MD5_Init
,
374 (hc_evp_md_update
)MD5_Update
,
375 (hc_evp_md_final
)MD5_Final
,
382 * The message digest MD4 - hcrypto
384 * @return the message digest type.
386 * @ingroup hcrypto_evp
390 EVP_hcrypto_md4(void)
392 static const struct hc_evp_md md4
= {
396 (hc_evp_md_init
)MD4_Init
,
397 (hc_evp_md_update
)MD4_Update
,
398 (hc_evp_md_final
)MD4_Final
,
405 * The message digest MD2 - hcrypto
407 * @return the message digest type.
409 * @ingroup hcrypto_evp
413 EVP_hcrypto_md2(void)
415 static const struct hc_evp_md md2
= {
419 (hc_evp_md_init
)MD2_Init
,
420 (hc_evp_md_update
)MD2_Update
,
421 (hc_evp_md_final
)MD2_Final
,
432 des_cbc_init(EVP_CIPHER_CTX
*ctx
,
433 const unsigned char * key
,
434 const unsigned char * iv
,
437 DES_key_schedule
*k
= ctx
->cipher_data
;
439 memcpy(&deskey
, key
, sizeof(deskey
));
440 DES_set_key_unchecked(&deskey
, k
);
445 des_cbc_do_cipher(EVP_CIPHER_CTX
*ctx
,
447 const unsigned char *in
,
450 DES_key_schedule
*k
= ctx
->cipher_data
;
451 DES_cbc_encrypt(in
, out
, size
,
452 k
, (DES_cblock
*)ctx
->iv
, ctx
->encrypt
);
457 * The DES cipher type
459 * @return the DES-CBC EVP_CIPHER pointer.
461 * @ingroup hcrypto_evp
465 EVP_hcrypto_des_cbc(void)
467 static const EVP_CIPHER des_cbc
= {
476 sizeof(DES_key_schedule
),
489 struct des_ede3_cbc
{
490 DES_key_schedule ks
[3];
494 des_ede3_cbc_init(EVP_CIPHER_CTX
*ctx
,
495 const unsigned char * key
,
496 const unsigned char * iv
,
499 struct des_ede3_cbc
*k
= ctx
->cipher_data
;
502 memcpy(&deskey
, key
, sizeof(deskey
));
503 DES_set_odd_parity(&deskey
);
504 DES_set_key_unchecked(&deskey
, &k
->ks
[0]);
506 memcpy(&deskey
, key
+ 8, sizeof(deskey
));
507 DES_set_odd_parity(&deskey
);
508 DES_set_key_unchecked(&deskey
, &k
->ks
[1]);
510 memcpy(&deskey
, key
+ 16, sizeof(deskey
));
511 DES_set_odd_parity(&deskey
);
512 DES_set_key_unchecked(&deskey
, &k
->ks
[2]);
518 des_ede3_cbc_do_cipher(EVP_CIPHER_CTX
*ctx
,
520 const unsigned char *in
,
523 struct des_ede3_cbc
*k
= ctx
->cipher_data
;
524 DES_ede3_cbc_encrypt(in
, out
, size
,
525 &k
->ks
[0], &k
->ks
[1], &k
->ks
[2],
526 (DES_cblock
*)ctx
->iv
, ctx
->encrypt
);
531 * The triple DES cipher type - hcrypto
533 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
535 * @ingroup hcrypto_evp
539 EVP_hcrypto_des_ede3_cbc(void)
541 static const EVP_CIPHER des_ede3_cbc
= {
548 des_ede3_cbc_do_cipher
,
550 sizeof(struct des_ede3_cbc
),
556 return &des_ede3_cbc
;
564 unsigned int maximum_effective_key
;
569 rc2_init(EVP_CIPHER_CTX
*ctx
,
570 const unsigned char * key
,
571 const unsigned char * iv
,
574 struct rc2_cbc
*k
= ctx
->cipher_data
;
575 k
->maximum_effective_key
= EVP_CIPHER_CTX_key_length(ctx
) * 8;
577 EVP_CIPHER_CTX_key_length(ctx
),
579 k
->maximum_effective_key
);
584 rc2_do_cipher(EVP_CIPHER_CTX
*ctx
,
586 const unsigned char *in
,
589 struct rc2_cbc
*k
= ctx
->cipher_data
;
590 RC2_cbc_encrypt(in
, out
, size
, &k
->key
, ctx
->iv
, ctx
->encrypt
);
595 * The RC2 cipher type - hcrypto
597 * @return the RC2 EVP_CIPHER pointer.
599 * @ingroup hcrypto_evp
603 EVP_hcrypto_rc2_cbc(void)
605 static const EVP_CIPHER rc2_cbc
= {
610 EVP_CIPH_CBC_MODE
|EVP_CIPH_VARIABLE_LENGTH
,
614 sizeof(struct rc2_cbc
),
624 * The RC2-40 cipher type
626 * @return the RC2-40 EVP_CIPHER pointer.
628 * @ingroup hcrypto_evp
632 EVP_hcrypto_rc2_40_cbc(void)
634 static const EVP_CIPHER rc2_40_cbc
= {
643 sizeof(struct rc2_cbc
),
653 * The RC2-64 cipher type
655 * @return the RC2-64 EVP_CIPHER pointer.
657 * @ingroup hcrypto_evp
661 EVP_hcrypto_rc2_64_cbc(void)
663 static const EVP_CIPHER rc2_64_cbc
= {
672 sizeof(struct rc2_cbc
),
682 camellia_init(EVP_CIPHER_CTX
*ctx
,
683 const unsigned char * key
,
684 const unsigned char * iv
,
687 CAMELLIA_KEY
*k
= ctx
->cipher_data
;
688 k
->bits
= ctx
->cipher
->key_len
* 8;
689 CAMELLIA_set_key(key
, ctx
->cipher
->key_len
* 8, k
);
694 camellia_do_cipher(EVP_CIPHER_CTX
*ctx
,
696 const unsigned char *in
,
699 CAMELLIA_KEY
*k
= ctx
->cipher_data
;
700 CAMELLIA_cbc_encrypt(in
, out
, size
, k
, ctx
->iv
, ctx
->encrypt
);
705 * The Camellia-128 cipher type - hcrypto
707 * @return the Camellia-128 EVP_CIPHER pointer.
709 * @ingroup hcrypto_evp
713 EVP_hcrypto_camellia_128_cbc(void)
715 static const EVP_CIPHER cipher
= {
724 sizeof(CAMELLIA_KEY
),
734 * The Camellia-198 cipher type - hcrypto
736 * @return the Camellia-198 EVP_CIPHER pointer.
738 * @ingroup hcrypto_evp
742 EVP_hcrypto_camellia_192_cbc(void)
744 static const EVP_CIPHER cipher
= {
753 sizeof(CAMELLIA_KEY
),
763 * The Camellia-256 cipher type - hcrypto
765 * @return the Camellia-256 EVP_CIPHER pointer.
767 * @ingroup hcrypto_evp
771 EVP_hcrypto_camellia_256_cbc(void)
773 static const EVP_CIPHER cipher
= {
782 sizeof(CAMELLIA_KEY
),
792 rc4_init(EVP_CIPHER_CTX
*ctx
,
793 const unsigned char *key
,
794 const unsigned char *iv
,
797 RC4_KEY
*k
= ctx
->cipher_data
;
798 RC4_set_key(k
, ctx
->key_len
, key
);
803 rc4_do_cipher(EVP_CIPHER_CTX
*ctx
,
805 const unsigned char *in
,
808 RC4_KEY
*k
= ctx
->cipher_data
;
809 RC4(k
, size
, in
, out
);
814 EVP_hcrypto_rc4(void)
816 static const EVP_CIPHER rc4
= {
821 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,
836 EVP_hcrypto_rc4_40(void)
838 static const EVP_CIPHER rc4_40
= {
843 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,