s4:heimdal: import lorikeet-heimdal-201009250123 (commit 42cabfb5b683dbcb97d583c397b8...
[mat/samba.git] / source4 / heimdal / lib / 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 SHA1 - hcrypto
294  *
295  * @return the message digest type.
296  *
297  * @ingroup hcrypto_evp
298  */
299
300 const EVP_MD *
301 EVP_hcrypto_sha1(void)
302 {
303     static const struct hc_evp_md sha1 = {
304         20,
305         64,
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
311     };
312     return &sha1;
313 }
314
315 /**
316  * The message digest MD5 - hcrypto
317  *
318  * @return the message digest type.
319  *
320  * @ingroup hcrypto_evp
321  */
322
323 const EVP_MD *
324 EVP_hcrypto_md5(void)
325 {
326     static const struct hc_evp_md md5 = {
327         16,
328         64,
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
334     };
335     return &md5;
336 }
337
338 /**
339  * The message digest MD4 - hcrypto
340  *
341  * @return the message digest type.
342  *
343  * @ingroup hcrypto_evp
344  */
345
346 const EVP_MD *
347 EVP_hcrypto_md4(void)
348 {
349     static const struct hc_evp_md md4 = {
350         16,
351         64,
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
357     };
358     return &md4;
359 }
360
361 /**
362  * The message digest MD2 - hcrypto
363  *
364  * @return the message digest type.
365  *
366  * @ingroup hcrypto_evp
367  */
368
369 const EVP_MD *
370 EVP_hcrypto_md2(void)
371 {
372     static const struct hc_evp_md md2 = {
373         16,
374         16,
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
380     };
381     return &md2;
382 }
383
384 /*
385  *
386  */
387
388 static int
389 des_cbc_init(EVP_CIPHER_CTX *ctx,
390              const unsigned char * key,
391              const unsigned char * iv,
392              int encp)
393 {
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;
399 }
400
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)
406 {
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;
411 }
412
413 /**
414  * The DES cipher type
415  *
416  * @return the DES-CBC EVP_CIPHER pointer.
417  *
418  * @ingroup hcrypto_evp
419  */
420
421 const EVP_CIPHER *
422 EVP_hcrypto_des_cbc(void)
423 {
424     static const EVP_CIPHER des_cbc = {
425         0,
426         8,
427         8,
428         8,
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
438     };
439     return &des_cbc;
440 }
441
442 /*
443  *
444  */
445
446 struct des_ede3_cbc {
447     DES_key_schedule ks[3];
448 };
449
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)
455 {
456     struct des_ede3_cbc *k = ctx->cipher_data;
457     DES_cblock deskey;
458
459     memcpy(&deskey, key, sizeof(deskey));
460     DES_set_odd_parity(&deskey);
461     DES_set_key_unchecked(&deskey, &k->ks[0]);
462
463     memcpy(&deskey, key + 8, sizeof(deskey));
464     DES_set_odd_parity(&deskey);
465     DES_set_key_unchecked(&deskey, &k->ks[1]);
466
467     memcpy(&deskey, key + 16, sizeof(deskey));
468     DES_set_odd_parity(&deskey);
469     DES_set_key_unchecked(&deskey, &k->ks[2]);
470
471     return 1;
472 }
473
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)
479 {
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;
485 }
486
487 /**
488  * The tripple DES cipher type - hcrypto
489  *
490  * @return the DES-EDE3-CBC EVP_CIPHER pointer.
491  *
492  * @ingroup hcrypto_evp
493  */
494
495 const EVP_CIPHER *
496 EVP_hcrypto_des_ede3_cbc(void)
497 {
498     static const EVP_CIPHER des_ede3_cbc = {
499         0,
500         8,
501         24,
502         8,
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
512     };
513     return &des_ede3_cbc;
514 }
515
516 /*
517  *
518  */
519
520 struct rc2_cbc {
521     unsigned int maximum_effective_key;
522     RC2_KEY key;
523 };
524
525 static int
526 rc2_init(EVP_CIPHER_CTX *ctx,
527          const unsigned char * key,
528          const unsigned char * iv,
529          int encp)
530 {
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;
538 }
539
540 static int
541 rc2_do_cipher(EVP_CIPHER_CTX *ctx,
542               unsigned char *out,
543               const unsigned char *in,
544               unsigned int size)
545 {
546     struct rc2_cbc *k = ctx->cipher_data;
547     RC2_cbc_encrypt(in, out, size, &k->key, ctx->iv, ctx->encrypt);
548     return 1;
549 }
550
551 /**
552  * The RC2 cipher type - hcrypto
553  *
554  * @return the RC2 EVP_CIPHER pointer.
555  *
556  * @ingroup hcrypto_evp
557  */
558
559 const EVP_CIPHER *
560 EVP_hcrypto_rc2_cbc(void)
561 {
562     static const EVP_CIPHER rc2_cbc = {
563         0,
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
576     };
577     return &rc2_cbc;
578 }
579
580 /**
581  * The RC2-40 cipher type
582  *
583  * @return the RC2-40 EVP_CIPHER pointer.
584  *
585  * @ingroup hcrypto_evp
586  */
587
588 const EVP_CIPHER *
589 EVP_hcrypto_rc2_40_cbc(void)
590 {
591     static const EVP_CIPHER rc2_40_cbc = {
592         0,
593         RC2_BLOCK_SIZE,
594         5,
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
605     };
606     return &rc2_40_cbc;
607 }
608
609 /**
610  * The RC2-64 cipher type
611  *
612  * @return the RC2-64 EVP_CIPHER pointer.
613  *
614  * @ingroup hcrypto_evp
615  */
616
617 const EVP_CIPHER *
618 EVP_hcrypto_rc2_64_cbc(void)
619 {
620     static const EVP_CIPHER rc2_64_cbc = {
621         0,
622         RC2_BLOCK_SIZE,
623         8,
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
634     };
635     return &rc2_64_cbc;
636 }
637
638 static int
639 camellia_init(EVP_CIPHER_CTX *ctx,
640          const unsigned char * key,
641          const unsigned char * iv,
642          int encp)
643 {
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;
648 }
649
650 static int
651 camellia_do_cipher(EVP_CIPHER_CTX *ctx,
652               unsigned char *out,
653               const unsigned char *in,
654               unsigned int size)
655 {
656     CAMELLIA_KEY *k = ctx->cipher_data;
657     CAMELLIA_cbc_encrypt(in, out, size, k, ctx->iv, ctx->encrypt);
658     return 1;
659 }
660
661 /**
662  * The Camellia-128 cipher type - hcrypto
663  *
664  * @return the Camellia-128 EVP_CIPHER pointer.
665  *
666  * @ingroup hcrypto_evp
667  */
668
669 const EVP_CIPHER *
670 EVP_hcrypto_camellia_128_cbc(void)
671 {
672     static const EVP_CIPHER cipher = {
673         0,
674         16,
675         16,
676         16,
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
686     };
687     return &cipher;
688 }
689
690 /**
691  * The Camellia-198 cipher type - hcrypto
692  *
693  * @return the Camellia-198 EVP_CIPHER pointer.
694  *
695  * @ingroup hcrypto_evp
696  */
697
698 const EVP_CIPHER *
699 EVP_hcrypto_camellia_192_cbc(void)
700 {
701     static const EVP_CIPHER cipher = {
702         0,
703         16,
704         24,
705         16,
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
715     };
716     return &cipher;
717 }
718
719 /**
720  * The Camellia-256 cipher type - hcrypto
721  *
722  * @return the Camellia-256 EVP_CIPHER pointer.
723  *
724  * @ingroup hcrypto_evp
725  */
726
727 const EVP_CIPHER *
728 EVP_hcrypto_camellia_256_cbc(void)
729 {
730     static const EVP_CIPHER cipher = {
731         0,
732         16,
733         32,
734         16,
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
744     };
745     return &cipher;
746 }
747
748 static int
749 rc4_init(EVP_CIPHER_CTX *ctx,
750          const unsigned char *key,
751          const unsigned char *iv,
752          int enc)
753 {
754     RC4_KEY *k = ctx->cipher_data;
755     RC4_set_key(k, ctx->key_len, key);
756     return 1;
757 }
758
759 static int
760 rc4_do_cipher(EVP_CIPHER_CTX *ctx,
761               unsigned char *out,
762               const unsigned char *in,
763               unsigned int size)
764 {
765     RC4_KEY *k = ctx->cipher_data;
766     RC4(k, size, in, out);
767     return 1;
768 }
769
770 const EVP_CIPHER *
771 EVP_hcrypto_rc4(void)
772 {
773     static const EVP_CIPHER rc4 = {
774         0,
775         1,
776         16,
777         0,
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
787     };
788     return &rc4;
789 }
790
791
792 const EVP_CIPHER *
793 EVP_hcrypto_rc4_40(void)
794 {
795     static const EVP_CIPHER rc4_40 = {
796         0,
797         1,
798         5,
799         0,
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
809     };
810     return &rc4_40;
811 }