backport to buster
[hcoop/debian/openafs.git] / src / external / heimdal / hcrypto / evp-hcrypto.c
1 /*
2 * Copyright (c) 2006 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
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.
16 *
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.
20 *
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.
32 */
33
34 #include <config.h>
35
36 #define HC_DEPRECATED
37
38 #include <sys/types.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <assert.h>
43
44 #include <evp.h>
45 #include <evp-hcrypto.h>
46
47 #include <krb5-types.h>
48
49 #include <des.h>
50 #include "camellia.h"
51 #include <aes.h>
52
53 #include <rc2.h>
54 #include <rc4.h>
55
56 #include <sha.h>
57 #include <md2.h>
58 #include <md4.h>
59 #include <md5.h>
60
61 /*
62 *
63 */
64
65 static int
66 aes_init(EVP_CIPHER_CTX *ctx,
67 const unsigned char * key,
68 const unsigned char * iv,
69 int encp)
70 {
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;
77 }
78
79 static int
80 aes_do_cipher(EVP_CIPHER_CTX *ctx,
81 unsigned char *out,
82 const unsigned char *in,
83 unsigned int size)
84 {
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;
91 }
92
93 /**
94 * The AES-128 cipher type (hcrypto)
95 *
96 * @return the AES-128 EVP_CIPHER pointer.
97 *
98 * @ingroup hcrypto_evp
99 */
100
101 const EVP_CIPHER *
102 EVP_hcrypto_aes_128_cbc(void)
103 {
104 static const EVP_CIPHER aes_128_cbc = {
105 0,
106 16,
107 16,
108 16,
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
118 };
119
120 return &aes_128_cbc;
121 }
122
123 /**
124 * The AES-192 cipher type (hcrypto)
125 *
126 * @return the AES-192 EVP_CIPHER pointer.
127 *
128 * @ingroup hcrypto_evp
129 */
130
131 const EVP_CIPHER *
132 EVP_hcrypto_aes_192_cbc(void)
133 {
134 static const EVP_CIPHER aes_192_cbc = {
135 0,
136 16,
137 24,
138 16,
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
148 };
149 return &aes_192_cbc;
150 }
151
152 /**
153 * The AES-256 cipher type (hcrypto)
154 *
155 * @return the AES-256 EVP_CIPHER pointer.
156 *
157 * @ingroup hcrypto_evp
158 */
159
160 const EVP_CIPHER *
161 EVP_hcrypto_aes_256_cbc(void)
162 {
163 static const EVP_CIPHER aes_256_cbc = {
164 0,
165 16,
166 32,
167 16,
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
177 };
178 return &aes_256_cbc;
179 }
180
181 /**
182 * The AES-128 CFB8 cipher type (hcrypto)
183 *
184 * @return the AES-128 EVP_CIPHER pointer.
185 *
186 * @ingroup hcrypto_evp
187 */
188
189 const EVP_CIPHER *
190 EVP_hcrypto_aes_128_cfb8(void)
191 {
192 static const EVP_CIPHER aes_128_cfb8 = {
193 0,
194 1,
195 16,
196 16,
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
206 };
207
208 return &aes_128_cfb8;
209 }
210
211 /**
212 * The AES-192 CFB8 cipher type (hcrypto)
213 *
214 * @return the AES-192 EVP_CIPHER pointer.
215 *
216 * @ingroup hcrypto_evp
217 */
218
219 const EVP_CIPHER *
220 EVP_hcrypto_aes_192_cfb8(void)
221 {
222 static const EVP_CIPHER aes_192_cfb8 = {
223 0,
224 1,
225 24,
226 16,
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
236 };
237 return &aes_192_cfb8;
238 }
239
240 /**
241 * The AES-256 CFB8 cipher type (hcrypto)
242 *
243 * @return the AES-256 EVP_CIPHER pointer.
244 *
245 * @ingroup hcrypto_evp
246 */
247
248 const EVP_CIPHER *
249 EVP_hcrypto_aes_256_cfb8(void)
250 {
251 static const EVP_CIPHER aes_256_cfb8 = {
252 0,
253 1,
254 32,
255 16,
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
265 };
266 return &aes_256_cfb8;
267 }
268
269 /**
270 * The message digest SHA256 - hcrypto
271 *
272 * @return the message digest type.
273 *
274 * @ingroup hcrypto_evp
275 */
276
277 const EVP_MD *
278 EVP_hcrypto_sha256(void)
279 {
280 static const struct hc_evp_md sha256 = {
281 32,
282 64,
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
288 };
289 return &sha256;
290 }
291
292 /**
293 * The message digest SHA384 - hcrypto
294 *
295 * @return the message digest type.
296 *
297 * @ingroup hcrypto_evp
298 */
299
300 const EVP_MD *
301 EVP_hcrypto_sha384(void)
302 {
303 static const struct hc_evp_md sha384 = {
304 48,
305 128,
306 sizeof(SHA384_CTX),
307 (hc_evp_md_init)SHA384_Init,
308 (hc_evp_md_update)SHA384_Update,
309 (hc_evp_md_final)SHA384_Final,
310 NULL
311 };
312 return &sha384;
313 }
314
315 /**
316 * The message digest SHA512 - hcrypto
317 *
318 * @return the message digest type.
319 *
320 * @ingroup hcrypto_evp
321 */
322
323 const EVP_MD *
324 EVP_hcrypto_sha512(void)
325 {
326 static const struct hc_evp_md sha512 = {
327 64,
328 128,
329 sizeof(SHA512_CTX),
330 (hc_evp_md_init)SHA512_Init,
331 (hc_evp_md_update)SHA512_Update,
332 (hc_evp_md_final)SHA512_Final,
333 NULL
334 };
335 return &sha512;
336 }
337
338 /**
339 * The message digest SHA1 - hcrypto
340 *
341 * @return the message digest type.
342 *
343 * @ingroup hcrypto_evp
344 */
345
346 const EVP_MD *
347 EVP_hcrypto_sha1(void)
348 {
349 static const struct hc_evp_md sha1 = {
350 20,
351 64,
352 sizeof(SHA_CTX),
353 (hc_evp_md_init)SHA1_Init,
354 (hc_evp_md_update)SHA1_Update,
355 (hc_evp_md_final)SHA1_Final,
356 NULL
357 };
358 return &sha1;
359 }
360
361 /**
362 * The message digest MD5 - hcrypto
363 *
364 * @return the message digest type.
365 *
366 * @ingroup hcrypto_evp
367 */
368
369 const EVP_MD *
370 EVP_hcrypto_md5(void)
371 {
372 static const struct hc_evp_md md5 = {
373 16,
374 64,
375 sizeof(MD5_CTX),
376 (hc_evp_md_init)MD5_Init,
377 (hc_evp_md_update)MD5_Update,
378 (hc_evp_md_final)MD5_Final,
379 NULL
380 };
381 return &md5;
382 }
383
384 /**
385 * The message digest MD4 - hcrypto
386 *
387 * @return the message digest type.
388 *
389 * @ingroup hcrypto_evp
390 */
391
392 const EVP_MD *
393 EVP_hcrypto_md4(void)
394 {
395 static const struct hc_evp_md md4 = {
396 16,
397 64,
398 sizeof(MD4_CTX),
399 (hc_evp_md_init)MD4_Init,
400 (hc_evp_md_update)MD4_Update,
401 (hc_evp_md_final)MD4_Final,
402 NULL
403 };
404 return &md4;
405 }
406
407 /**
408 * The message digest MD2 - hcrypto
409 *
410 * @return the message digest type.
411 *
412 * @ingroup hcrypto_evp
413 */
414
415 const EVP_MD *
416 EVP_hcrypto_md2(void)
417 {
418 static const struct hc_evp_md md2 = {
419 16,
420 16,
421 sizeof(MD2_CTX),
422 (hc_evp_md_init)MD2_Init,
423 (hc_evp_md_update)MD2_Update,
424 (hc_evp_md_final)MD2_Final,
425 NULL
426 };
427 return &md2;
428 }
429
430 /*
431 *
432 */
433
434 static int
435 des_cbc_init(EVP_CIPHER_CTX *ctx,
436 const unsigned char * key,
437 const unsigned char * iv,
438 int encp)
439 {
440 DES_key_schedule *k = ctx->cipher_data;
441 DES_cblock deskey;
442 memcpy(&deskey, key, sizeof(deskey));
443 DES_set_key_unchecked(&deskey, k);
444 return 1;
445 }
446
447 static int
448 des_cbc_do_cipher(EVP_CIPHER_CTX *ctx,
449 unsigned char *out,
450 const unsigned char *in,
451 unsigned int size)
452 {
453 DES_key_schedule *k = ctx->cipher_data;
454 DES_cbc_encrypt(in, out, size,
455 k, (DES_cblock *)ctx->iv, ctx->encrypt);
456 return 1;
457 }
458
459 /**
460 * The DES cipher type
461 *
462 * @return the DES-CBC EVP_CIPHER pointer.
463 *
464 * @ingroup hcrypto_evp
465 */
466
467 const EVP_CIPHER *
468 EVP_hcrypto_des_cbc(void)
469 {
470 static const EVP_CIPHER des_cbc = {
471 0,
472 8,
473 8,
474 8,
475 EVP_CIPH_CBC_MODE,
476 des_cbc_init,
477 des_cbc_do_cipher,
478 NULL,
479 sizeof(DES_key_schedule),
480 NULL,
481 NULL,
482 NULL,
483 NULL
484 };
485 return &des_cbc;
486 }
487
488 /*
489 *
490 */
491
492 struct des_ede3_cbc {
493 DES_key_schedule ks[3];
494 };
495
496 static int
497 des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
498 const unsigned char * key,
499 const unsigned char * iv,
500 int encp)
501 {
502 struct des_ede3_cbc *k = ctx->cipher_data;
503 DES_cblock deskey;
504
505 memcpy(&deskey, key, sizeof(deskey));
506 DES_set_odd_parity(&deskey);
507 DES_set_key_unchecked(&deskey, &k->ks[0]);
508
509 memcpy(&deskey, key + 8, sizeof(deskey));
510 DES_set_odd_parity(&deskey);
511 DES_set_key_unchecked(&deskey, &k->ks[1]);
512
513 memcpy(&deskey, key + 16, sizeof(deskey));
514 DES_set_odd_parity(&deskey);
515 DES_set_key_unchecked(&deskey, &k->ks[2]);
516
517 return 1;
518 }
519
520 static int
521 des_ede3_cbc_do_cipher(EVP_CIPHER_CTX *ctx,
522 unsigned char *out,
523 const unsigned char *in,
524 unsigned int size)
525 {
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);
530 return 1;
531 }
532
533 /**
534 * The tripple DES cipher type - hcrypto
535 *
536 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
537 *
538 * @ingroup hcrypto_evp
539 */
540
541 const EVP_CIPHER *
542 EVP_hcrypto_des_ede3_cbc(void)
543 {
544 static const EVP_CIPHER des_ede3_cbc = {
545 0,
546 8,
547 24,
548 8,
549 EVP_CIPH_CBC_MODE,
550 des_ede3_cbc_init,
551 des_ede3_cbc_do_cipher,
552 NULL,
553 sizeof(struct des_ede3_cbc),
554 NULL,
555 NULL,
556 NULL,
557 NULL
558 };
559 return &des_ede3_cbc;
560 }
561
562 /*
563 *
564 */
565
566 struct rc2_cbc {
567 unsigned int maximum_effective_key;
568 RC2_KEY key;
569 };
570
571 static int
572 rc2_init(EVP_CIPHER_CTX *ctx,
573 const unsigned char * key,
574 const unsigned char * iv,
575 int encp)
576 {
577 struct rc2_cbc *k = ctx->cipher_data;
578 k->maximum_effective_key = EVP_CIPHER_CTX_key_length(ctx) * 8;
579 RC2_set_key(&k->key,
580 EVP_CIPHER_CTX_key_length(ctx),
581 key,
582 k->maximum_effective_key);
583 return 1;
584 }
585
586 static int
587 rc2_do_cipher(EVP_CIPHER_CTX *ctx,
588 unsigned char *out,
589 const unsigned char *in,
590 unsigned int size)
591 {
592 struct rc2_cbc *k = ctx->cipher_data;
593 RC2_cbc_encrypt(in, out, size, &k->key, ctx->iv, ctx->encrypt);
594 return 1;
595 }
596
597 /**
598 * The RC2 cipher type - hcrypto
599 *
600 * @return the RC2 EVP_CIPHER pointer.
601 *
602 * @ingroup hcrypto_evp
603 */
604
605 const EVP_CIPHER *
606 EVP_hcrypto_rc2_cbc(void)
607 {
608 static const EVP_CIPHER rc2_cbc = {
609 0,
610 RC2_BLOCK_SIZE,
611 RC2_KEY_LENGTH,
612 RC2_BLOCK_SIZE,
613 EVP_CIPH_CBC_MODE|EVP_CIPH_VARIABLE_LENGTH,
614 rc2_init,
615 rc2_do_cipher,
616 NULL,
617 sizeof(struct rc2_cbc),
618 NULL,
619 NULL,
620 NULL,
621 NULL
622 };
623 return &rc2_cbc;
624 }
625
626 /**
627 * The RC2-40 cipher type
628 *
629 * @return the RC2-40 EVP_CIPHER pointer.
630 *
631 * @ingroup hcrypto_evp
632 */
633
634 const EVP_CIPHER *
635 EVP_hcrypto_rc2_40_cbc(void)
636 {
637 static const EVP_CIPHER rc2_40_cbc = {
638 0,
639 RC2_BLOCK_SIZE,
640 5,
641 RC2_BLOCK_SIZE,
642 EVP_CIPH_CBC_MODE,
643 rc2_init,
644 rc2_do_cipher,
645 NULL,
646 sizeof(struct rc2_cbc),
647 NULL,
648 NULL,
649 NULL,
650 NULL
651 };
652 return &rc2_40_cbc;
653 }
654
655 /**
656 * The RC2-64 cipher type
657 *
658 * @return the RC2-64 EVP_CIPHER pointer.
659 *
660 * @ingroup hcrypto_evp
661 */
662
663 const EVP_CIPHER *
664 EVP_hcrypto_rc2_64_cbc(void)
665 {
666 static const EVP_CIPHER rc2_64_cbc = {
667 0,
668 RC2_BLOCK_SIZE,
669 8,
670 RC2_BLOCK_SIZE,
671 EVP_CIPH_CBC_MODE,
672 rc2_init,
673 rc2_do_cipher,
674 NULL,
675 sizeof(struct rc2_cbc),
676 NULL,
677 NULL,
678 NULL,
679 NULL
680 };
681 return &rc2_64_cbc;
682 }
683
684 static int
685 camellia_init(EVP_CIPHER_CTX *ctx,
686 const unsigned char * key,
687 const unsigned char * iv,
688 int encp)
689 {
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);
693 return 1;
694 }
695
696 static int
697 camellia_do_cipher(EVP_CIPHER_CTX *ctx,
698 unsigned char *out,
699 const unsigned char *in,
700 unsigned int size)
701 {
702 CAMELLIA_KEY *k = ctx->cipher_data;
703 CAMELLIA_cbc_encrypt(in, out, size, k, ctx->iv, ctx->encrypt);
704 return 1;
705 }
706
707 /**
708 * The Camellia-128 cipher type - hcrypto
709 *
710 * @return the Camellia-128 EVP_CIPHER pointer.
711 *
712 * @ingroup hcrypto_evp
713 */
714
715 const EVP_CIPHER *
716 EVP_hcrypto_camellia_128_cbc(void)
717 {
718 static const EVP_CIPHER cipher = {
719 0,
720 16,
721 16,
722 16,
723 EVP_CIPH_CBC_MODE,
724 camellia_init,
725 camellia_do_cipher,
726 NULL,
727 sizeof(CAMELLIA_KEY),
728 NULL,
729 NULL,
730 NULL,
731 NULL
732 };
733 return &cipher;
734 }
735
736 /**
737 * The Camellia-198 cipher type - hcrypto
738 *
739 * @return the Camellia-198 EVP_CIPHER pointer.
740 *
741 * @ingroup hcrypto_evp
742 */
743
744 const EVP_CIPHER *
745 EVP_hcrypto_camellia_192_cbc(void)
746 {
747 static const EVP_CIPHER cipher = {
748 0,
749 16,
750 24,
751 16,
752 EVP_CIPH_CBC_MODE,
753 camellia_init,
754 camellia_do_cipher,
755 NULL,
756 sizeof(CAMELLIA_KEY),
757 NULL,
758 NULL,
759 NULL,
760 NULL
761 };
762 return &cipher;
763 }
764
765 /**
766 * The Camellia-256 cipher type - hcrypto
767 *
768 * @return the Camellia-256 EVP_CIPHER pointer.
769 *
770 * @ingroup hcrypto_evp
771 */
772
773 const EVP_CIPHER *
774 EVP_hcrypto_camellia_256_cbc(void)
775 {
776 static const EVP_CIPHER cipher = {
777 0,
778 16,
779 32,
780 16,
781 EVP_CIPH_CBC_MODE,
782 camellia_init,
783 camellia_do_cipher,
784 NULL,
785 sizeof(CAMELLIA_KEY),
786 NULL,
787 NULL,
788 NULL,
789 NULL
790 };
791 return &cipher;
792 }
793
794 static int
795 rc4_init(EVP_CIPHER_CTX *ctx,
796 const unsigned char *key,
797 const unsigned char *iv,
798 int enc)
799 {
800 RC4_KEY *k = ctx->cipher_data;
801 RC4_set_key(k, ctx->key_len, key);
802 return 1;
803 }
804
805 static int
806 rc4_do_cipher(EVP_CIPHER_CTX *ctx,
807 unsigned char *out,
808 const unsigned char *in,
809 unsigned int size)
810 {
811 RC4_KEY *k = ctx->cipher_data;
812 RC4(k, size, in, out);
813 return 1;
814 }
815
816 const EVP_CIPHER *
817 EVP_hcrypto_rc4(void)
818 {
819 static const EVP_CIPHER rc4 = {
820 0,
821 1,
822 16,
823 0,
824 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
825 rc4_init,
826 rc4_do_cipher,
827 NULL,
828 sizeof(RC4_KEY),
829 NULL,
830 NULL,
831 NULL,
832 NULL
833 };
834 return &rc4;
835 }
836
837
838 const EVP_CIPHER *
839 EVP_hcrypto_rc4_40(void)
840 {
841 static const EVP_CIPHER rc4_40 = {
842 0,
843 1,
844 5,
845 0,
846 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
847 rc4_init,
848 rc4_do_cipher,
849 NULL,
850 sizeof(RC4_KEY),
851 NULL,
852 NULL,
853 NULL,
854 NULL
855 };
856 return &rc4_40;
857 }