s3:include: change smb_request->vuid to uint64_t
[Samba/gebeck_regimport.git] / source4 / heimdal / lib / hcrypto / evp-hcrypto.c
blobbf37b42edcaca83b52d4a64859e71c690053a30e
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 SHA384 - hcrypto
295 * @return the message digest type.
297 * @ingroup hcrypto_evp
300 const EVP_MD *
301 EVP_hcrypto_sha384(void)
303 static const struct hc_evp_md sha384 = {
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
312 return &sha384;
316 * The message digest SHA512 - hcrypto
318 * @return the message digest type.
320 * @ingroup hcrypto_evp
323 const EVP_MD *
324 EVP_hcrypto_sha512(void)
326 static const struct hc_evp_md sha512 = {
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
335 return &sha512;
339 * The message digest SHA1 - hcrypto
341 * @return the message digest type.
343 * @ingroup hcrypto_evp
346 const EVP_MD *
347 EVP_hcrypto_sha1(void)
349 static const struct hc_evp_md sha1 = {
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
358 return &sha1;
362 * The message digest MD5 - hcrypto
364 * @return the message digest type.
366 * @ingroup hcrypto_evp
369 const EVP_MD *
370 EVP_hcrypto_md5(void)
372 static const struct hc_evp_md md5 = {
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
381 return &md5;
385 * The message digest MD4 - hcrypto
387 * @return the message digest type.
389 * @ingroup hcrypto_evp
392 const EVP_MD *
393 EVP_hcrypto_md4(void)
395 static const struct hc_evp_md md4 = {
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
404 return &md4;
408 * The message digest MD2 - hcrypto
410 * @return the message digest type.
412 * @ingroup hcrypto_evp
415 const EVP_MD *
416 EVP_hcrypto_md2(void)
418 static const struct hc_evp_md md2 = {
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
427 return &md2;
434 static int
435 des_cbc_init(EVP_CIPHER_CTX *ctx,
436 const unsigned char * key,
437 const unsigned char * iv,
438 int encp)
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;
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)
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;
460 * The DES cipher type
462 * @return the DES-CBC EVP_CIPHER pointer.
464 * @ingroup hcrypto_evp
467 const EVP_CIPHER *
468 EVP_hcrypto_des_cbc(void)
470 static const EVP_CIPHER des_cbc = {
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
485 return &des_cbc;
492 struct des_ede3_cbc {
493 DES_key_schedule ks[3];
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)
502 struct des_ede3_cbc *k = ctx->cipher_data;
503 DES_cblock deskey;
505 memcpy(&deskey, key, sizeof(deskey));
506 DES_set_odd_parity(&deskey);
507 DES_set_key_unchecked(&deskey, &k->ks[0]);
509 memcpy(&deskey, key + 8, sizeof(deskey));
510 DES_set_odd_parity(&deskey);
511 DES_set_key_unchecked(&deskey, &k->ks[1]);
513 memcpy(&deskey, key + 16, sizeof(deskey));
514 DES_set_odd_parity(&deskey);
515 DES_set_key_unchecked(&deskey, &k->ks[2]);
517 return 1;
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)
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;
534 * The tripple DES cipher type - hcrypto
536 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
538 * @ingroup hcrypto_evp
541 const EVP_CIPHER *
542 EVP_hcrypto_des_ede3_cbc(void)
544 static const EVP_CIPHER des_ede3_cbc = {
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
559 return &des_ede3_cbc;
566 struct rc2_cbc {
567 unsigned int maximum_effective_key;
568 RC2_KEY key;
571 static int
572 rc2_init(EVP_CIPHER_CTX *ctx,
573 const unsigned char * key,
574 const unsigned char * iv,
575 int encp)
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;
586 static int
587 rc2_do_cipher(EVP_CIPHER_CTX *ctx,
588 unsigned char *out,
589 const unsigned char *in,
590 unsigned int size)
592 struct rc2_cbc *k = ctx->cipher_data;
593 RC2_cbc_encrypt(in, out, size, &k->key, ctx->iv, ctx->encrypt);
594 return 1;
598 * The RC2 cipher type - hcrypto
600 * @return the RC2 EVP_CIPHER pointer.
602 * @ingroup hcrypto_evp
605 const EVP_CIPHER *
606 EVP_hcrypto_rc2_cbc(void)
608 static const EVP_CIPHER rc2_cbc = {
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
623 return &rc2_cbc;
627 * The RC2-40 cipher type
629 * @return the RC2-40 EVP_CIPHER pointer.
631 * @ingroup hcrypto_evp
634 const EVP_CIPHER *
635 EVP_hcrypto_rc2_40_cbc(void)
637 static const EVP_CIPHER rc2_40_cbc = {
639 RC2_BLOCK_SIZE,
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
652 return &rc2_40_cbc;
656 * The RC2-64 cipher type
658 * @return the RC2-64 EVP_CIPHER pointer.
660 * @ingroup hcrypto_evp
663 const EVP_CIPHER *
664 EVP_hcrypto_rc2_64_cbc(void)
666 static const EVP_CIPHER rc2_64_cbc = {
668 RC2_BLOCK_SIZE,
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
681 return &rc2_64_cbc;
684 static int
685 camellia_init(EVP_CIPHER_CTX *ctx,
686 const unsigned char * key,
687 const unsigned char * iv,
688 int encp)
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;
696 static int
697 camellia_do_cipher(EVP_CIPHER_CTX *ctx,
698 unsigned char *out,
699 const unsigned char *in,
700 unsigned int size)
702 CAMELLIA_KEY *k = ctx->cipher_data;
703 CAMELLIA_cbc_encrypt(in, out, size, k, ctx->iv, ctx->encrypt);
704 return 1;
708 * The Camellia-128 cipher type - hcrypto
710 * @return the Camellia-128 EVP_CIPHER pointer.
712 * @ingroup hcrypto_evp
715 const EVP_CIPHER *
716 EVP_hcrypto_camellia_128_cbc(void)
718 static const EVP_CIPHER cipher = {
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
733 return &cipher;
737 * The Camellia-198 cipher type - hcrypto
739 * @return the Camellia-198 EVP_CIPHER pointer.
741 * @ingroup hcrypto_evp
744 const EVP_CIPHER *
745 EVP_hcrypto_camellia_192_cbc(void)
747 static const EVP_CIPHER cipher = {
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
762 return &cipher;
766 * The Camellia-256 cipher type - hcrypto
768 * @return the Camellia-256 EVP_CIPHER pointer.
770 * @ingroup hcrypto_evp
773 const EVP_CIPHER *
774 EVP_hcrypto_camellia_256_cbc(void)
776 static const EVP_CIPHER cipher = {
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
791 return &cipher;
794 static int
795 rc4_init(EVP_CIPHER_CTX *ctx,
796 const unsigned char *key,
797 const unsigned char *iv,
798 int enc)
800 RC4_KEY *k = ctx->cipher_data;
801 RC4_set_key(k, ctx->key_len, key);
802 return 1;
805 static int
806 rc4_do_cipher(EVP_CIPHER_CTX *ctx,
807 unsigned char *out,
808 const unsigned char *in,
809 unsigned int size)
811 RC4_KEY *k = ctx->cipher_data;
812 RC4(k, size, in, out);
813 return 1;
816 const EVP_CIPHER *
817 EVP_hcrypto_rc4(void)
819 static const EVP_CIPHER rc4 = {
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
834 return &rc4;
838 const EVP_CIPHER *
839 EVP_hcrypto_rc4_40(void)
841 static const EVP_CIPHER rc4_40 = {
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
856 return &rc4_40;