Debian Codegraph @

lib/hcrypto/evp-hcrypto.c

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