clean better
[heimdal.git] / lib / hcrypto / evp-hcrypto.c
blob9e063545e168342462156568b95de987be53e3ba
1 /*
2 * Copyright (c) 2006 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include <config.h>
36 #define HC_DEPRECATED
38 #include <sys/types.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <assert.h>
44 #include <evp.h>
45 #include <evp-hcrypto.h>
47 #include <krb5-types.h>
49 #include <des.h>
50 #include "camellia.h"
51 #include <aes.h>
53 #include <rc2.h>
54 #include <rc4.h>
56 #include <sha.h>
57 #include <md2.h>
58 #include <md4.h>
59 #include <md5.h>
65 static int
66 aes_init(EVP_CIPHER_CTX *ctx,
67 const unsigned char * key,
68 const unsigned char * iv,
69 int encp)
71 AES_KEY *k = ctx->cipher_data;
72 if (ctx->encrypt)
73 AES_set_encrypt_key(key, ctx->cipher->key_len * 8, k);
74 else
75 AES_set_decrypt_key(key, ctx->cipher->key_len * 8, k);
76 return 1;
79 static int
80 aes_do_cipher(EVP_CIPHER_CTX *ctx,
81 unsigned char *out,
82 const unsigned char *in,
83 unsigned int size)
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);
88 else
89 AES_cbc_encrypt(in, out, size, k, ctx->iv, ctx->encrypt);
90 return 1;
93 /**
94 * The AES-128 cipher type (hcrypto)
96 * @return the AES-128 EVP_CIPHER pointer.
98 * @ingroup hcrypto_evp
101 const EVP_CIPHER *
102 EVP_hcrypto_aes_128_cbc(void)
104 static const EVP_CIPHER aes_128_cbc = {
109 EVP_CIPH_CBC_MODE,
110 aes_init,
111 aes_do_cipher,
112 NULL,
113 sizeof(AES_KEY),
114 NULL,
115 NULL,
116 NULL,
117 NULL
120 return &aes_128_cbc;
124 * The AES-192 cipher type (hcrypto)
126 * @return the AES-192 EVP_CIPHER pointer.
128 * @ingroup hcrypto_evp
131 const EVP_CIPHER *
132 EVP_hcrypto_aes_192_cbc(void)
134 static const EVP_CIPHER aes_192_cbc = {
139 EVP_CIPH_CBC_MODE,
140 aes_init,
141 aes_do_cipher,
142 NULL,
143 sizeof(AES_KEY),
144 NULL,
145 NULL,
146 NULL,
147 NULL
149 return &aes_192_cbc;
153 * The AES-256 cipher type (hcrypto)
155 * @return the AES-256 EVP_CIPHER pointer.
157 * @ingroup hcrypto_evp
160 const EVP_CIPHER *
161 EVP_hcrypto_aes_256_cbc(void)
163 static const EVP_CIPHER aes_256_cbc = {
168 EVP_CIPH_CBC_MODE,
169 aes_init,
170 aes_do_cipher,
171 NULL,
172 sizeof(AES_KEY),
173 NULL,
174 NULL,
175 NULL,
176 NULL
178 return &aes_256_cbc;
182 * The AES-128 CFB8 cipher type (hcrypto)
184 * @return the AES-128 EVP_CIPHER pointer.
186 * @ingroup hcrypto_evp
189 const EVP_CIPHER *
190 EVP_hcrypto_aes_128_cfb8(void)
192 static const EVP_CIPHER aes_128_cfb8 = {
197 EVP_CIPH_CFB8_MODE,
198 aes_init,
199 aes_do_cipher,
200 NULL,
201 sizeof(AES_KEY),
202 NULL,
203 NULL,
204 NULL,
205 NULL
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
219 const EVP_CIPHER *
220 EVP_hcrypto_aes_192_cfb8(void)
222 static const EVP_CIPHER aes_192_cfb8 = {
227 EVP_CIPH_CFB8_MODE,
228 aes_init,
229 aes_do_cipher,
230 NULL,
231 sizeof(AES_KEY),
232 NULL,
233 NULL,
234 NULL,
235 NULL
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
248 const EVP_CIPHER *
249 EVP_hcrypto_aes_256_cfb8(void)
251 static const EVP_CIPHER aes_256_cfb8 = {
256 EVP_CIPH_CFB8_MODE,
257 aes_init,
258 aes_do_cipher,
259 NULL,
260 sizeof(AES_KEY),
261 NULL,
262 NULL,
263 NULL,
264 NULL
266 return &aes_256_cfb8;
270 * The message digest SHA256 - hcrypto
272 * @return the message digest type.
274 * @ingroup hcrypto_evp
277 const EVP_MD *
278 EVP_hcrypto_sha256(void)
280 static const struct hc_evp_md sha256 = {
283 sizeof(SHA256_CTX),
284 (hc_evp_md_init)SHA256_Init,
285 (hc_evp_md_update)SHA256_Update,
286 (hc_evp_md_final)SHA256_Final,
287 NULL
289 return &sha256;
293 * The message digest SHA1 - hcrypto
295 * @return the message digest type.
297 * @ingroup hcrypto_evp
300 const EVP_MD *
301 EVP_hcrypto_sha1(void)
303 static const struct hc_evp_md sha1 = {
306 sizeof(SHA_CTX),
307 (hc_evp_md_init)SHA1_Init,
308 (hc_evp_md_update)SHA1_Update,
309 (hc_evp_md_final)SHA1_Final,
310 NULL
312 return &sha1;
316 * The message digest MD5 - hcrypto
318 * @return the message digest type.
320 * @ingroup hcrypto_evp
323 const EVP_MD *
324 EVP_hcrypto_md5(void)
326 static const struct hc_evp_md md5 = {
329 sizeof(MD5_CTX),
330 (hc_evp_md_init)MD5_Init,
331 (hc_evp_md_update)MD5_Update,
332 (hc_evp_md_final)MD5_Final,
333 NULL
335 return &md5;
339 * The message digest MD4 - hcrypto
341 * @return the message digest type.
343 * @ingroup hcrypto_evp
346 const EVP_MD *
347 EVP_hcrypto_md4(void)
349 static const struct hc_evp_md md4 = {
352 sizeof(MD4_CTX),
353 (hc_evp_md_init)MD4_Init,
354 (hc_evp_md_update)MD4_Update,
355 (hc_evp_md_final)MD4_Final,
356 NULL
358 return &md4;
362 * The message digest MD2 - hcrypto
364 * @return the message digest type.
366 * @ingroup hcrypto_evp
369 const EVP_MD *
370 EVP_hcrypto_md2(void)
372 static const struct hc_evp_md md2 = {
375 sizeof(MD2_CTX),
376 (hc_evp_md_init)MD2_Init,
377 (hc_evp_md_update)MD2_Update,
378 (hc_evp_md_final)MD2_Final,
379 NULL
381 return &md2;
388 static int
389 des_cbc_init(EVP_CIPHER_CTX *ctx,
390 const unsigned char * key,
391 const unsigned char * iv,
392 int encp)
394 DES_key_schedule *k = ctx->cipher_data;
395 DES_cblock deskey;
396 memcpy(&deskey, key, sizeof(deskey));
397 DES_set_key_unchecked(&deskey, k);
398 return 1;
401 static int
402 des_cbc_do_cipher(EVP_CIPHER_CTX *ctx,
403 unsigned char *out,
404 const unsigned char *in,
405 unsigned int size)
407 DES_key_schedule *k = ctx->cipher_data;
408 DES_cbc_encrypt(in, out, size,
409 k, (DES_cblock *)ctx->iv, ctx->encrypt);
410 return 1;
414 * The DES cipher type
416 * @return the DES-CBC EVP_CIPHER pointer.
418 * @ingroup hcrypto_evp
421 const EVP_CIPHER *
422 EVP_hcrypto_des_cbc(void)
424 static const EVP_CIPHER des_cbc = {
429 EVP_CIPH_CBC_MODE,
430 des_cbc_init,
431 des_cbc_do_cipher,
432 NULL,
433 sizeof(DES_key_schedule),
434 NULL,
435 NULL,
436 NULL,
437 NULL
439 return &des_cbc;
446 struct des_ede3_cbc {
447 DES_key_schedule ks[3];
450 static int
451 des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
452 const unsigned char * key,
453 const unsigned char * iv,
454 int encp)
456 struct des_ede3_cbc *k = ctx->cipher_data;
457 DES_cblock deskey;
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]);
471 return 1;
474 static int
475 des_ede3_cbc_do_cipher(EVP_CIPHER_CTX *ctx,
476 unsigned char *out,
477 const unsigned char *in,
478 unsigned int size)
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);
484 return 1;
488 * The tripple DES cipher type - hcrypto
490 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
492 * @ingroup hcrypto_evp
495 const EVP_CIPHER *
496 EVP_hcrypto_des_ede3_cbc(void)
498 static const EVP_CIPHER des_ede3_cbc = {
503 EVP_CIPH_CBC_MODE,
504 des_ede3_cbc_init,
505 des_ede3_cbc_do_cipher,
506 NULL,
507 sizeof(struct des_ede3_cbc),
508 NULL,
509 NULL,
510 NULL,
511 NULL
513 return &des_ede3_cbc;
520 struct rc2_cbc {
521 unsigned int maximum_effective_key;
522 RC2_KEY key;
525 static int
526 rc2_init(EVP_CIPHER_CTX *ctx,
527 const unsigned char * key,
528 const unsigned char * iv,
529 int encp)
531 struct rc2_cbc *k = ctx->cipher_data;
532 k->maximum_effective_key = EVP_CIPHER_CTX_key_length(ctx) * 8;
533 RC2_set_key(&k->key,
534 EVP_CIPHER_CTX_key_length(ctx),
535 key,
536 k->maximum_effective_key);
537 return 1;
540 static int
541 rc2_do_cipher(EVP_CIPHER_CTX *ctx,
542 unsigned char *out,
543 const unsigned char *in,
544 unsigned int size)
546 struct rc2_cbc *k = ctx->cipher_data;
547 RC2_cbc_encrypt(in, out, size, &k->key, ctx->iv, ctx->encrypt);
548 return 1;
552 * The RC2 cipher type - hcrypto
554 * @return the RC2 EVP_CIPHER pointer.
556 * @ingroup hcrypto_evp
559 const EVP_CIPHER *
560 EVP_hcrypto_rc2_cbc(void)
562 static const EVP_CIPHER rc2_cbc = {
564 RC2_BLOCK_SIZE,
565 RC2_KEY_LENGTH,
566 RC2_BLOCK_SIZE,
567 EVP_CIPH_CBC_MODE|EVP_CIPH_VARIABLE_LENGTH,
568 rc2_init,
569 rc2_do_cipher,
570 NULL,
571 sizeof(struct rc2_cbc),
572 NULL,
573 NULL,
574 NULL,
575 NULL
577 return &rc2_cbc;
581 * The RC2-40 cipher type
583 * @return the RC2-40 EVP_CIPHER pointer.
585 * @ingroup hcrypto_evp
588 const EVP_CIPHER *
589 EVP_hcrypto_rc2_40_cbc(void)
591 static const EVP_CIPHER rc2_40_cbc = {
593 RC2_BLOCK_SIZE,
595 RC2_BLOCK_SIZE,
596 EVP_CIPH_CBC_MODE,
597 rc2_init,
598 rc2_do_cipher,
599 NULL,
600 sizeof(struct rc2_cbc),
601 NULL,
602 NULL,
603 NULL,
604 NULL
606 return &rc2_40_cbc;
610 * The RC2-64 cipher type
612 * @return the RC2-64 EVP_CIPHER pointer.
614 * @ingroup hcrypto_evp
617 const EVP_CIPHER *
618 EVP_hcrypto_rc2_64_cbc(void)
620 static const EVP_CIPHER rc2_64_cbc = {
622 RC2_BLOCK_SIZE,
624 RC2_BLOCK_SIZE,
625 EVP_CIPH_CBC_MODE,
626 rc2_init,
627 rc2_do_cipher,
628 NULL,
629 sizeof(struct rc2_cbc),
630 NULL,
631 NULL,
632 NULL,
633 NULL
635 return &rc2_64_cbc;
638 static int
639 camellia_init(EVP_CIPHER_CTX *ctx,
640 const unsigned char * key,
641 const unsigned char * iv,
642 int encp)
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);
647 return 1;
650 static int
651 camellia_do_cipher(EVP_CIPHER_CTX *ctx,
652 unsigned char *out,
653 const unsigned char *in,
654 unsigned int size)
656 CAMELLIA_KEY *k = ctx->cipher_data;
657 CAMELLIA_cbc_encrypt(in, out, size, k, ctx->iv, ctx->encrypt);
658 return 1;
662 * The Camellia-128 cipher type - hcrypto
664 * @return the Camellia-128 EVP_CIPHER pointer.
666 * @ingroup hcrypto_evp
669 const EVP_CIPHER *
670 EVP_hcrypto_camellia_128_cbc(void)
672 static const EVP_CIPHER cipher = {
677 EVP_CIPH_CBC_MODE,
678 camellia_init,
679 camellia_do_cipher,
680 NULL,
681 sizeof(CAMELLIA_KEY),
682 NULL,
683 NULL,
684 NULL,
685 NULL
687 return &cipher;
691 * The Camellia-198 cipher type - hcrypto
693 * @return the Camellia-198 EVP_CIPHER pointer.
695 * @ingroup hcrypto_evp
698 const EVP_CIPHER *
699 EVP_hcrypto_camellia_192_cbc(void)
701 static const EVP_CIPHER cipher = {
706 EVP_CIPH_CBC_MODE,
707 camellia_init,
708 camellia_do_cipher,
709 NULL,
710 sizeof(CAMELLIA_KEY),
711 NULL,
712 NULL,
713 NULL,
714 NULL
716 return &cipher;
720 * The Camellia-256 cipher type - hcrypto
722 * @return the Camellia-256 EVP_CIPHER pointer.
724 * @ingroup hcrypto_evp
727 const EVP_CIPHER *
728 EVP_hcrypto_camellia_256_cbc(void)
730 static const EVP_CIPHER cipher = {
735 EVP_CIPH_CBC_MODE,
736 camellia_init,
737 camellia_do_cipher,
738 NULL,
739 sizeof(CAMELLIA_KEY),
740 NULL,
741 NULL,
742 NULL,
743 NULL
745 return &cipher;
748 static int
749 rc4_init(EVP_CIPHER_CTX *ctx,
750 const unsigned char *key,
751 const unsigned char *iv,
752 int enc)
754 RC4_KEY *k = ctx->cipher_data;
755 RC4_set_key(k, ctx->key_len, key);
756 return 1;
759 static int
760 rc4_do_cipher(EVP_CIPHER_CTX *ctx,
761 unsigned char *out,
762 const unsigned char *in,
763 unsigned int size)
765 RC4_KEY *k = ctx->cipher_data;
766 RC4(k, size, in, out);
767 return 1;
770 const EVP_CIPHER *
771 EVP_hcrypto_rc4(void)
773 static const EVP_CIPHER rc4 = {
778 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
779 rc4_init,
780 rc4_do_cipher,
781 NULL,
782 sizeof(RC4_KEY),
783 NULL,
784 NULL,
785 NULL,
786 NULL
788 return &rc4;
792 const EVP_CIPHER *
793 EVP_hcrypto_rc4_40(void)
795 static const EVP_CIPHER rc4_40 = {
800 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
801 rc4_init,
802 rc4_do_cipher,
803 NULL,
804 sizeof(RC4_KEY),
805 NULL,
806 NULL,
807 NULL,
808 NULL
810 return &rc4_40;