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 SHA1 - hcrypto
295 * @return the message digest type.
297 * @ingroup hcrypto_evp
301 EVP_hcrypto_sha1(void)
303 static const struct hc_evp_md sha1
= {
307 (hc_evp_md_init
)SHA1_Init
,
308 (hc_evp_md_update
)SHA1_Update
,
309 (hc_evp_md_final
)SHA1_Final
,
316 * The message digest MD5 - hcrypto
318 * @return the message digest type.
320 * @ingroup hcrypto_evp
324 EVP_hcrypto_md5(void)
326 static const struct hc_evp_md md5
= {
330 (hc_evp_md_init
)MD5_Init
,
331 (hc_evp_md_update
)MD5_Update
,
332 (hc_evp_md_final
)MD5_Final
,
339 * The message digest MD4 - hcrypto
341 * @return the message digest type.
343 * @ingroup hcrypto_evp
347 EVP_hcrypto_md4(void)
349 static const struct hc_evp_md md4
= {
353 (hc_evp_md_init
)MD4_Init
,
354 (hc_evp_md_update
)MD4_Update
,
355 (hc_evp_md_final
)MD4_Final
,
362 * The message digest MD2 - hcrypto
364 * @return the message digest type.
366 * @ingroup hcrypto_evp
370 EVP_hcrypto_md2(void)
372 static const struct hc_evp_md md2
= {
376 (hc_evp_md_init
)MD2_Init
,
377 (hc_evp_md_update
)MD2_Update
,
378 (hc_evp_md_final
)MD2_Final
,
389 des_cbc_init(EVP_CIPHER_CTX
*ctx
,
390 const unsigned char * key
,
391 const unsigned char * iv
,
394 DES_key_schedule
*k
= ctx
->cipher_data
;
396 memcpy(&deskey
, key
, sizeof(deskey
));
397 DES_set_key_unchecked(&deskey
, k
);
402 des_cbc_do_cipher(EVP_CIPHER_CTX
*ctx
,
404 const unsigned char *in
,
407 DES_key_schedule
*k
= ctx
->cipher_data
;
408 DES_cbc_encrypt(in
, out
, size
,
409 k
, (DES_cblock
*)ctx
->iv
, ctx
->encrypt
);
414 * The DES cipher type
416 * @return the DES-CBC EVP_CIPHER pointer.
418 * @ingroup hcrypto_evp
422 EVP_hcrypto_des_cbc(void)
424 static const EVP_CIPHER des_cbc
= {
433 sizeof(DES_key_schedule
),
446 struct des_ede3_cbc
{
447 DES_key_schedule ks
[3];
451 des_ede3_cbc_init(EVP_CIPHER_CTX
*ctx
,
452 const unsigned char * key
,
453 const unsigned char * iv
,
456 struct des_ede3_cbc
*k
= ctx
->cipher_data
;
459 memcpy(&deskey
, key
, sizeof(deskey
));
460 DES_set_odd_parity(&deskey
);
461 DES_set_key_unchecked(&deskey
, &k
->ks
[0]);
463 memcpy(&deskey
, key
+ 8, sizeof(deskey
));
464 DES_set_odd_parity(&deskey
);
465 DES_set_key_unchecked(&deskey
, &k
->ks
[1]);
467 memcpy(&deskey
, key
+ 16, sizeof(deskey
));
468 DES_set_odd_parity(&deskey
);
469 DES_set_key_unchecked(&deskey
, &k
->ks
[2]);
475 des_ede3_cbc_do_cipher(EVP_CIPHER_CTX
*ctx
,
477 const unsigned char *in
,
480 struct des_ede3_cbc
*k
= ctx
->cipher_data
;
481 DES_ede3_cbc_encrypt(in
, out
, size
,
482 &k
->ks
[0], &k
->ks
[1], &k
->ks
[2],
483 (DES_cblock
*)ctx
->iv
, ctx
->encrypt
);
488 * The tripple DES cipher type - hcrypto
490 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
492 * @ingroup hcrypto_evp
496 EVP_hcrypto_des_ede3_cbc(void)
498 static const EVP_CIPHER des_ede3_cbc
= {
505 des_ede3_cbc_do_cipher
,
507 sizeof(struct des_ede3_cbc
),
513 return &des_ede3_cbc
;
521 unsigned int maximum_effective_key
;
526 rc2_init(EVP_CIPHER_CTX
*ctx
,
527 const unsigned char * key
,
528 const unsigned char * iv
,
531 struct rc2_cbc
*k
= ctx
->cipher_data
;
532 k
->maximum_effective_key
= EVP_CIPHER_CTX_key_length(ctx
) * 8;
534 EVP_CIPHER_CTX_key_length(ctx
),
536 k
->maximum_effective_key
);
541 rc2_do_cipher(EVP_CIPHER_CTX
*ctx
,
543 const unsigned char *in
,
546 struct rc2_cbc
*k
= ctx
->cipher_data
;
547 RC2_cbc_encrypt(in
, out
, size
, &k
->key
, ctx
->iv
, ctx
->encrypt
);
552 * The RC2 cipher type - hcrypto
554 * @return the RC2 EVP_CIPHER pointer.
556 * @ingroup hcrypto_evp
560 EVP_hcrypto_rc2_cbc(void)
562 static const EVP_CIPHER rc2_cbc
= {
567 EVP_CIPH_CBC_MODE
|EVP_CIPH_VARIABLE_LENGTH
,
571 sizeof(struct rc2_cbc
),
581 * The RC2-40 cipher type
583 * @return the RC2-40 EVP_CIPHER pointer.
585 * @ingroup hcrypto_evp
589 EVP_hcrypto_rc2_40_cbc(void)
591 static const EVP_CIPHER rc2_40_cbc
= {
600 sizeof(struct rc2_cbc
),
610 * The RC2-64 cipher type
612 * @return the RC2-64 EVP_CIPHER pointer.
614 * @ingroup hcrypto_evp
618 EVP_hcrypto_rc2_64_cbc(void)
620 static const EVP_CIPHER rc2_64_cbc
= {
629 sizeof(struct rc2_cbc
),
639 camellia_init(EVP_CIPHER_CTX
*ctx
,
640 const unsigned char * key
,
641 const unsigned char * iv
,
644 CAMELLIA_KEY
*k
= ctx
->cipher_data
;
645 k
->bits
= ctx
->cipher
->key_len
* 8;
646 CAMELLIA_set_key(key
, ctx
->cipher
->key_len
* 8, k
);
651 camellia_do_cipher(EVP_CIPHER_CTX
*ctx
,
653 const unsigned char *in
,
656 CAMELLIA_KEY
*k
= ctx
->cipher_data
;
657 CAMELLIA_cbc_encrypt(in
, out
, size
, k
, ctx
->iv
, ctx
->encrypt
);
662 * The Camellia-128 cipher type - hcrypto
664 * @return the Camellia-128 EVP_CIPHER pointer.
666 * @ingroup hcrypto_evp
670 EVP_hcrypto_camellia_128_cbc(void)
672 static const EVP_CIPHER cipher
= {
681 sizeof(CAMELLIA_KEY
),
691 * The Camellia-198 cipher type - hcrypto
693 * @return the Camellia-198 EVP_CIPHER pointer.
695 * @ingroup hcrypto_evp
699 EVP_hcrypto_camellia_192_cbc(void)
701 static const EVP_CIPHER cipher
= {
710 sizeof(CAMELLIA_KEY
),
720 * The Camellia-256 cipher type - hcrypto
722 * @return the Camellia-256 EVP_CIPHER pointer.
724 * @ingroup hcrypto_evp
728 EVP_hcrypto_camellia_256_cbc(void)
730 static const EVP_CIPHER cipher
= {
739 sizeof(CAMELLIA_KEY
),
749 rc4_init(EVP_CIPHER_CTX
*ctx
,
750 const unsigned char *key
,
751 const unsigned char *iv
,
754 RC4_KEY
*k
= ctx
->cipher_data
;
755 RC4_set_key(k
, ctx
->key_len
, key
);
760 rc4_do_cipher(EVP_CIPHER_CTX
*ctx
,
762 const unsigned char *in
,
765 RC4_KEY
*k
= ctx
->cipher_data
;
766 RC4(k
, size
, in
, out
);
771 EVP_hcrypto_rc4(void)
773 static const EVP_CIPHER rc4
= {
778 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,
793 EVP_hcrypto_rc4_40(void)
795 static const EVP_CIPHER rc4_40
= {
800 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,