1 /* $NetBSD: evp-hcrypto.c,v 1.1.1.1 2011/04/13 18:14:49 elric 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
40 #include <sys/types.h>
47 #include <evp-hcrypto.h>
49 #include <krb5/krb5-types.h>
68 aes_init(EVP_CIPHER_CTX
*ctx
,
69 const unsigned char * key
,
70 const unsigned char * iv
,
73 AES_KEY
*k
= ctx
->cipher_data
;
75 AES_set_encrypt_key(key
, ctx
->cipher
->key_len
* 8, k
);
77 AES_set_decrypt_key(key
, ctx
->cipher
->key_len
* 8, k
);
82 aes_do_cipher(EVP_CIPHER_CTX
*ctx
,
84 const unsigned char *in
,
87 AES_KEY
*k
= ctx
->cipher_data
;
88 if (ctx
->flags
& EVP_CIPH_CFB8_MODE
)
89 AES_cfb8_encrypt(in
, out
, size
, k
, ctx
->iv
, ctx
->encrypt
);
91 AES_cbc_encrypt(in
, out
, size
, k
, ctx
->iv
, ctx
->encrypt
);
96 * The AES-128 cipher type (hcrypto)
98 * @return the AES-128 EVP_CIPHER pointer.
100 * @ingroup hcrypto_evp
104 EVP_hcrypto_aes_128_cbc(void)
106 static const EVP_CIPHER aes_128_cbc
= {
126 * The AES-192 cipher type (hcrypto)
128 * @return the AES-192 EVP_CIPHER pointer.
130 * @ingroup hcrypto_evp
134 EVP_hcrypto_aes_192_cbc(void)
136 static const EVP_CIPHER aes_192_cbc
= {
155 * The AES-256 cipher type (hcrypto)
157 * @return the AES-256 EVP_CIPHER pointer.
159 * @ingroup hcrypto_evp
163 EVP_hcrypto_aes_256_cbc(void)
165 static const EVP_CIPHER aes_256_cbc
= {
184 * The AES-128 CFB8 cipher type (hcrypto)
186 * @return the AES-128 EVP_CIPHER pointer.
188 * @ingroup hcrypto_evp
192 EVP_hcrypto_aes_128_cfb8(void)
194 static const EVP_CIPHER aes_128_cfb8
= {
210 return &aes_128_cfb8
;
214 * The AES-192 CFB8 cipher type (hcrypto)
216 * @return the AES-192 EVP_CIPHER pointer.
218 * @ingroup hcrypto_evp
222 EVP_hcrypto_aes_192_cfb8(void)
224 static const EVP_CIPHER aes_192_cfb8
= {
239 return &aes_192_cfb8
;
243 * The AES-256 CFB8 cipher type (hcrypto)
245 * @return the AES-256 EVP_CIPHER pointer.
247 * @ingroup hcrypto_evp
251 EVP_hcrypto_aes_256_cfb8(void)
253 static const EVP_CIPHER aes_256_cfb8
= {
268 return &aes_256_cfb8
;
272 * The message digest SHA256 - hcrypto
274 * @return the message digest type.
276 * @ingroup hcrypto_evp
280 EVP_hcrypto_sha256(void)
282 static const struct hc_evp_md sha256
= {
286 (hc_evp_md_init
)SHA256_Init
,
287 (hc_evp_md_update
)SHA256_Update
,
288 (hc_evp_md_final
)SHA256_Final
,
295 * The message digest SHA384 - hcrypto
297 * @return the message digest type.
299 * @ingroup hcrypto_evp
303 EVP_hcrypto_sha384(void)
305 static const struct hc_evp_md sha384
= {
309 (hc_evp_md_init
)SHA384_Init
,
310 (hc_evp_md_update
)SHA384_Update
,
311 (hc_evp_md_final
)SHA384_Final
,
318 * The message digest SHA512 - hcrypto
320 * @return the message digest type.
322 * @ingroup hcrypto_evp
326 EVP_hcrypto_sha512(void)
328 static const struct hc_evp_md sha512
= {
332 (hc_evp_md_init
)SHA512_Init
,
333 (hc_evp_md_update
)SHA512_Update
,
334 (hc_evp_md_final
)SHA512_Final
,
341 * The message digest SHA1 - hcrypto
343 * @return the message digest type.
345 * @ingroup hcrypto_evp
349 EVP_hcrypto_sha1(void)
351 static const struct hc_evp_md sha1
= {
355 (hc_evp_md_init
)SHA1_Init
,
356 (hc_evp_md_update
)SHA1_Update
,
357 (hc_evp_md_final
)SHA1_Final
,
364 * The message digest MD5 - hcrypto
366 * @return the message digest type.
368 * @ingroup hcrypto_evp
372 EVP_hcrypto_md5(void)
374 static const struct hc_evp_md md5
= {
378 (hc_evp_md_init
)MD5_Init
,
379 (hc_evp_md_update
)MD5_Update
,
380 (hc_evp_md_final
)MD5_Final
,
387 * The message digest MD4 - hcrypto
389 * @return the message digest type.
391 * @ingroup hcrypto_evp
395 EVP_hcrypto_md4(void)
397 static const struct hc_evp_md md4
= {
401 (hc_evp_md_init
)MD4_Init
,
402 (hc_evp_md_update
)MD4_Update
,
403 (hc_evp_md_final
)MD4_Final
,
410 * The message digest MD2 - hcrypto
412 * @return the message digest type.
414 * @ingroup hcrypto_evp
418 EVP_hcrypto_md2(void)
420 static const struct hc_evp_md md2
= {
424 (hc_evp_md_init
)MD2_Init
,
425 (hc_evp_md_update
)MD2_Update
,
426 (hc_evp_md_final
)MD2_Final
,
437 des_cbc_init(EVP_CIPHER_CTX
*ctx
,
438 const unsigned char * key
,
439 const unsigned char * iv
,
442 DES_key_schedule
*k
= ctx
->cipher_data
;
444 memcpy(&deskey
, key
, sizeof(deskey
));
445 DES_set_key_unchecked(&deskey
, k
);
450 des_cbc_do_cipher(EVP_CIPHER_CTX
*ctx
,
452 const unsigned char *in
,
455 DES_key_schedule
*k
= ctx
->cipher_data
;
456 DES_cbc_encrypt(in
, out
, size
,
457 k
, (DES_cblock
*)ctx
->iv
, ctx
->encrypt
);
462 * The DES cipher type
464 * @return the DES-CBC EVP_CIPHER pointer.
466 * @ingroup hcrypto_evp
470 EVP_hcrypto_des_cbc(void)
472 static const EVP_CIPHER des_cbc
= {
481 sizeof(DES_key_schedule
),
494 struct des_ede3_cbc
{
495 DES_key_schedule ks
[3];
499 des_ede3_cbc_init(EVP_CIPHER_CTX
*ctx
,
500 const unsigned char * key
,
501 const unsigned char * iv
,
504 struct des_ede3_cbc
*k
= ctx
->cipher_data
;
507 memcpy(&deskey
, key
, sizeof(deskey
));
508 DES_set_odd_parity(&deskey
);
509 DES_set_key_unchecked(&deskey
, &k
->ks
[0]);
511 memcpy(&deskey
, key
+ 8, sizeof(deskey
));
512 DES_set_odd_parity(&deskey
);
513 DES_set_key_unchecked(&deskey
, &k
->ks
[1]);
515 memcpy(&deskey
, key
+ 16, sizeof(deskey
));
516 DES_set_odd_parity(&deskey
);
517 DES_set_key_unchecked(&deskey
, &k
->ks
[2]);
523 des_ede3_cbc_do_cipher(EVP_CIPHER_CTX
*ctx
,
525 const unsigned char *in
,
528 struct des_ede3_cbc
*k
= ctx
->cipher_data
;
529 DES_ede3_cbc_encrypt(in
, out
, size
,
530 &k
->ks
[0], &k
->ks
[1], &k
->ks
[2],
531 (DES_cblock
*)ctx
->iv
, ctx
->encrypt
);
536 * The tripple DES cipher type - hcrypto
538 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
540 * @ingroup hcrypto_evp
544 EVP_hcrypto_des_ede3_cbc(void)
546 static const EVP_CIPHER des_ede3_cbc
= {
553 des_ede3_cbc_do_cipher
,
555 sizeof(struct des_ede3_cbc
),
561 return &des_ede3_cbc
;
569 unsigned int maximum_effective_key
;
574 rc2_init(EVP_CIPHER_CTX
*ctx
,
575 const unsigned char * key
,
576 const unsigned char * iv
,
579 struct rc2_cbc
*k
= ctx
->cipher_data
;
580 k
->maximum_effective_key
= EVP_CIPHER_CTX_key_length(ctx
) * 8;
582 EVP_CIPHER_CTX_key_length(ctx
),
584 k
->maximum_effective_key
);
589 rc2_do_cipher(EVP_CIPHER_CTX
*ctx
,
591 const unsigned char *in
,
594 struct rc2_cbc
*k
= ctx
->cipher_data
;
595 RC2_cbc_encrypt(in
, out
, size
, &k
->key
, ctx
->iv
, ctx
->encrypt
);
600 * The RC2 cipher type - hcrypto
602 * @return the RC2 EVP_CIPHER pointer.
604 * @ingroup hcrypto_evp
608 EVP_hcrypto_rc2_cbc(void)
610 static const EVP_CIPHER rc2_cbc
= {
615 EVP_CIPH_CBC_MODE
|EVP_CIPH_VARIABLE_LENGTH
,
619 sizeof(struct rc2_cbc
),
629 * The RC2-40 cipher type
631 * @return the RC2-40 EVP_CIPHER pointer.
633 * @ingroup hcrypto_evp
637 EVP_hcrypto_rc2_40_cbc(void)
639 static const EVP_CIPHER rc2_40_cbc
= {
648 sizeof(struct rc2_cbc
),
658 * The RC2-64 cipher type
660 * @return the RC2-64 EVP_CIPHER pointer.
662 * @ingroup hcrypto_evp
666 EVP_hcrypto_rc2_64_cbc(void)
668 static const EVP_CIPHER rc2_64_cbc
= {
677 sizeof(struct rc2_cbc
),
687 camellia_init(EVP_CIPHER_CTX
*ctx
,
688 const unsigned char * key
,
689 const unsigned char * iv
,
692 CAMELLIA_KEY
*k
= ctx
->cipher_data
;
693 k
->bits
= ctx
->cipher
->key_len
* 8;
694 CAMELLIA_set_key(key
, ctx
->cipher
->key_len
* 8, k
);
699 camellia_do_cipher(EVP_CIPHER_CTX
*ctx
,
701 const unsigned char *in
,
704 CAMELLIA_KEY
*k
= ctx
->cipher_data
;
705 CAMELLIA_cbc_encrypt(in
, out
, size
, k
, ctx
->iv
, ctx
->encrypt
);
710 * The Camellia-128 cipher type - hcrypto
712 * @return the Camellia-128 EVP_CIPHER pointer.
714 * @ingroup hcrypto_evp
718 EVP_hcrypto_camellia_128_cbc(void)
720 static const EVP_CIPHER cipher
= {
729 sizeof(CAMELLIA_KEY
),
739 * The Camellia-198 cipher type - hcrypto
741 * @return the Camellia-198 EVP_CIPHER pointer.
743 * @ingroup hcrypto_evp
747 EVP_hcrypto_camellia_192_cbc(void)
749 static const EVP_CIPHER cipher
= {
758 sizeof(CAMELLIA_KEY
),
768 * The Camellia-256 cipher type - hcrypto
770 * @return the Camellia-256 EVP_CIPHER pointer.
772 * @ingroup hcrypto_evp
776 EVP_hcrypto_camellia_256_cbc(void)
778 static const EVP_CIPHER cipher
= {
787 sizeof(CAMELLIA_KEY
),
797 rc4_init(EVP_CIPHER_CTX
*ctx
,
798 const unsigned char *key
,
799 const unsigned char *iv
,
802 RC4_KEY
*k
= ctx
->cipher_data
;
803 RC4_set_key(k
, ctx
->key_len
, key
);
808 rc4_do_cipher(EVP_CIPHER_CTX
*ctx
,
810 const unsigned char *in
,
813 RC4_KEY
*k
= ctx
->cipher_data
;
814 RC4(k
, size
, in
, out
);
819 EVP_hcrypto_rc4(void)
821 static const EVP_CIPHER rc4
= {
826 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,
841 EVP_hcrypto_rc4_40(void)
843 static const EVP_CIPHER rc4_40
= {
848 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,