s4:torture: Adapt KDC canon test to Heimdal upstream changes
[Samba.git] / source4 / heimdal / lib / hcrypto / evp-hcrypto.c
blob321efb08c52ad2ec580bb544e0ba2b536fce7efa
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>
35 #include <roken.h>
37 #define HC_DEPRECATED
39 #include <assert.h>
41 #include <evp.h>
42 #include <evp-hcrypto.h>
44 #include <krb5-types.h>
46 #include <des.h>
47 #include "camellia.h"
48 #include <aes.h>
50 #include <rc2.h>
51 #include <rc4.h>
53 #include <sha.h>
54 #include <md2.h>
55 #include <md4.h>
56 #include <md5.h>
62 static int
63 aes_init(EVP_CIPHER_CTX *ctx,
64 const unsigned char * key,
65 const unsigned char * iv,
66 int encp)
68 AES_KEY *k = ctx->cipher_data;
69 if (ctx->encrypt || EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CFB8_MODE)
70 AES_set_encrypt_key(key, ctx->cipher->key_len * 8, k);
71 else
72 AES_set_decrypt_key(key, ctx->cipher->key_len * 8, k);
73 return 1;
76 static int
77 aes_do_cipher(EVP_CIPHER_CTX *ctx,
78 unsigned char *out,
79 const unsigned char *in,
80 unsigned int size)
82 AES_KEY *k = ctx->cipher_data;
83 if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CFB8_MODE)
84 AES_cfb8_encrypt(in, out, size, k, ctx->iv, ctx->encrypt);
85 else
86 AES_cbc_encrypt(in, out, size, k, ctx->iv, ctx->encrypt);
87 return 1;
90 /**
91 * The AES-128 cipher type (hcrypto)
93 * @return the AES-128 EVP_CIPHER pointer.
95 * @ingroup hcrypto_evp
98 const EVP_CIPHER *
99 EVP_hcrypto_aes_128_cbc(void)
101 static const EVP_CIPHER aes_128_cbc = {
106 EVP_CIPH_CBC_MODE,
107 aes_init,
108 aes_do_cipher,
109 NULL,
110 sizeof(AES_KEY),
111 NULL,
112 NULL,
113 NULL,
114 NULL
117 return &aes_128_cbc;
121 * The AES-192 cipher type (hcrypto)
123 * @return the AES-192 EVP_CIPHER pointer.
125 * @ingroup hcrypto_evp
128 const EVP_CIPHER *
129 EVP_hcrypto_aes_192_cbc(void)
131 static const EVP_CIPHER aes_192_cbc = {
136 EVP_CIPH_CBC_MODE,
137 aes_init,
138 aes_do_cipher,
139 NULL,
140 sizeof(AES_KEY),
141 NULL,
142 NULL,
143 NULL,
144 NULL
146 return &aes_192_cbc;
150 * The AES-256 cipher type (hcrypto)
152 * @return the AES-256 EVP_CIPHER pointer.
154 * @ingroup hcrypto_evp
157 const EVP_CIPHER *
158 EVP_hcrypto_aes_256_cbc(void)
160 static const EVP_CIPHER aes_256_cbc = {
165 EVP_CIPH_CBC_MODE,
166 aes_init,
167 aes_do_cipher,
168 NULL,
169 sizeof(AES_KEY),
170 NULL,
171 NULL,
172 NULL,
173 NULL
175 return &aes_256_cbc;
179 * The AES-128 CFB8 cipher type (hcrypto)
181 * @return the AES-128 EVP_CIPHER pointer.
183 * @ingroup hcrypto_evp
186 const EVP_CIPHER *
187 EVP_hcrypto_aes_128_cfb8(void)
189 static const EVP_CIPHER aes_128_cfb8 = {
194 EVP_CIPH_CFB8_MODE,
195 aes_init,
196 aes_do_cipher,
197 NULL,
198 sizeof(AES_KEY),
199 NULL,
200 NULL,
201 NULL,
202 NULL
205 return &aes_128_cfb8;
209 * The AES-192 CFB8 cipher type (hcrypto)
211 * @return the AES-192 EVP_CIPHER pointer.
213 * @ingroup hcrypto_evp
216 const EVP_CIPHER *
217 EVP_hcrypto_aes_192_cfb8(void)
219 static const EVP_CIPHER aes_192_cfb8 = {
224 EVP_CIPH_CFB8_MODE,
225 aes_init,
226 aes_do_cipher,
227 NULL,
228 sizeof(AES_KEY),
229 NULL,
230 NULL,
231 NULL,
232 NULL
234 return &aes_192_cfb8;
238 * The AES-256 CFB8 cipher type (hcrypto)
240 * @return the AES-256 EVP_CIPHER pointer.
242 * @ingroup hcrypto_evp
245 const EVP_CIPHER *
246 EVP_hcrypto_aes_256_cfb8(void)
248 static const EVP_CIPHER aes_256_cfb8 = {
253 EVP_CIPH_CFB8_MODE,
254 aes_init,
255 aes_do_cipher,
256 NULL,
257 sizeof(AES_KEY),
258 NULL,
259 NULL,
260 NULL,
261 NULL
263 return &aes_256_cfb8;
267 * The message digest SHA256 - hcrypto
269 * @return the message digest type.
271 * @ingroup hcrypto_evp
274 const EVP_MD *
275 EVP_hcrypto_sha256(void)
277 static const struct hc_evp_md sha256 = {
280 sizeof(SHA256_CTX),
281 (hc_evp_md_init)SHA256_Init,
282 (hc_evp_md_update)SHA256_Update,
283 (hc_evp_md_final)SHA256_Final,
284 NULL
286 return &sha256;
290 * The message digest SHA384 - hcrypto
292 * @return the message digest type.
294 * @ingroup hcrypto_evp
297 const EVP_MD *
298 EVP_hcrypto_sha384(void)
300 static const struct hc_evp_md sha384 = {
302 128,
303 sizeof(SHA384_CTX),
304 (hc_evp_md_init)SHA384_Init,
305 (hc_evp_md_update)SHA384_Update,
306 (hc_evp_md_final)SHA384_Final,
307 NULL
309 return &sha384;
313 * The message digest SHA512 - hcrypto
315 * @return the message digest type.
317 * @ingroup hcrypto_evp
320 const EVP_MD *
321 EVP_hcrypto_sha512(void)
323 static const struct hc_evp_md sha512 = {
325 128,
326 sizeof(SHA512_CTX),
327 (hc_evp_md_init)SHA512_Init,
328 (hc_evp_md_update)SHA512_Update,
329 (hc_evp_md_final)SHA512_Final,
330 NULL
332 return &sha512;
336 * The message digest SHA1 - hcrypto
338 * @return the message digest type.
340 * @ingroup hcrypto_evp
343 const EVP_MD *
344 EVP_hcrypto_sha1(void)
346 static const struct hc_evp_md sha1 = {
349 sizeof(SHA_CTX),
350 (hc_evp_md_init)SHA1_Init,
351 (hc_evp_md_update)SHA1_Update,
352 (hc_evp_md_final)SHA1_Final,
353 NULL
355 return &sha1;
359 * The message digest MD5 - hcrypto
361 * @return the message digest type.
363 * @ingroup hcrypto_evp
366 const EVP_MD *
367 EVP_hcrypto_md5(void)
369 static const struct hc_evp_md md5 = {
372 sizeof(MD5_CTX),
373 (hc_evp_md_init)MD5_Init,
374 (hc_evp_md_update)MD5_Update,
375 (hc_evp_md_final)MD5_Final,
376 NULL
378 return &md5;
382 * The message digest MD4 - hcrypto
384 * @return the message digest type.
386 * @ingroup hcrypto_evp
389 const EVP_MD *
390 EVP_hcrypto_md4(void)
392 static const struct hc_evp_md md4 = {
395 sizeof(MD4_CTX),
396 (hc_evp_md_init)MD4_Init,
397 (hc_evp_md_update)MD4_Update,
398 (hc_evp_md_final)MD4_Final,
399 NULL
401 return &md4;
405 * The message digest MD2 - hcrypto
407 * @return the message digest type.
409 * @ingroup hcrypto_evp
412 const EVP_MD *
413 EVP_hcrypto_md2(void)
415 static const struct hc_evp_md md2 = {
418 sizeof(MD2_CTX),
419 (hc_evp_md_init)MD2_Init,
420 (hc_evp_md_update)MD2_Update,
421 (hc_evp_md_final)MD2_Final,
422 NULL
424 return &md2;
431 static int
432 des_cbc_init(EVP_CIPHER_CTX *ctx,
433 const unsigned char * key,
434 const unsigned char * iv,
435 int encp)
437 DES_key_schedule *k = ctx->cipher_data;
438 DES_cblock deskey;
439 memcpy(&deskey, key, sizeof(deskey));
440 DES_set_key_unchecked(&deskey, k);
441 return 1;
444 static int
445 des_cbc_do_cipher(EVP_CIPHER_CTX *ctx,
446 unsigned char *out,
447 const unsigned char *in,
448 unsigned int size)
450 DES_key_schedule *k = ctx->cipher_data;
451 DES_cbc_encrypt(in, out, size,
452 k, (DES_cblock *)ctx->iv, ctx->encrypt);
453 return 1;
457 * The DES cipher type
459 * @return the DES-CBC EVP_CIPHER pointer.
461 * @ingroup hcrypto_evp
464 const EVP_CIPHER *
465 EVP_hcrypto_des_cbc(void)
467 static const EVP_CIPHER des_cbc = {
472 EVP_CIPH_CBC_MODE,
473 des_cbc_init,
474 des_cbc_do_cipher,
475 NULL,
476 sizeof(DES_key_schedule),
477 NULL,
478 NULL,
479 NULL,
480 NULL
482 return &des_cbc;
489 struct des_ede3_cbc {
490 DES_key_schedule ks[3];
493 static int
494 des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
495 const unsigned char * key,
496 const unsigned char * iv,
497 int encp)
499 struct des_ede3_cbc *k = ctx->cipher_data;
500 DES_cblock deskey;
502 memcpy(&deskey, key, sizeof(deskey));
503 DES_set_odd_parity(&deskey);
504 DES_set_key_unchecked(&deskey, &k->ks[0]);
506 memcpy(&deskey, key + 8, sizeof(deskey));
507 DES_set_odd_parity(&deskey);
508 DES_set_key_unchecked(&deskey, &k->ks[1]);
510 memcpy(&deskey, key + 16, sizeof(deskey));
511 DES_set_odd_parity(&deskey);
512 DES_set_key_unchecked(&deskey, &k->ks[2]);
514 return 1;
517 static int
518 des_ede3_cbc_do_cipher(EVP_CIPHER_CTX *ctx,
519 unsigned char *out,
520 const unsigned char *in,
521 unsigned int size)
523 struct des_ede3_cbc *k = ctx->cipher_data;
524 DES_ede3_cbc_encrypt(in, out, size,
525 &k->ks[0], &k->ks[1], &k->ks[2],
526 (DES_cblock *)ctx->iv, ctx->encrypt);
527 return 1;
531 * The triple DES cipher type - hcrypto
533 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
535 * @ingroup hcrypto_evp
538 const EVP_CIPHER *
539 EVP_hcrypto_des_ede3_cbc(void)
541 static const EVP_CIPHER des_ede3_cbc = {
546 EVP_CIPH_CBC_MODE,
547 des_ede3_cbc_init,
548 des_ede3_cbc_do_cipher,
549 NULL,
550 sizeof(struct des_ede3_cbc),
551 NULL,
552 NULL,
553 NULL,
554 NULL
556 return &des_ede3_cbc;
563 struct rc2_cbc {
564 unsigned int maximum_effective_key;
565 RC2_KEY key;
568 static int
569 rc2_init(EVP_CIPHER_CTX *ctx,
570 const unsigned char * key,
571 const unsigned char * iv,
572 int encp)
574 struct rc2_cbc *k = ctx->cipher_data;
575 k->maximum_effective_key = EVP_CIPHER_CTX_key_length(ctx) * 8;
576 RC2_set_key(&k->key,
577 EVP_CIPHER_CTX_key_length(ctx),
578 key,
579 k->maximum_effective_key);
580 return 1;
583 static int
584 rc2_do_cipher(EVP_CIPHER_CTX *ctx,
585 unsigned char *out,
586 const unsigned char *in,
587 unsigned int size)
589 struct rc2_cbc *k = ctx->cipher_data;
590 RC2_cbc_encrypt(in, out, size, &k->key, ctx->iv, ctx->encrypt);
591 return 1;
595 * The RC2 cipher type - hcrypto
597 * @return the RC2 EVP_CIPHER pointer.
599 * @ingroup hcrypto_evp
602 const EVP_CIPHER *
603 EVP_hcrypto_rc2_cbc(void)
605 static const EVP_CIPHER rc2_cbc = {
607 RC2_BLOCK_SIZE,
608 RC2_KEY_LENGTH,
609 RC2_BLOCK_SIZE,
610 EVP_CIPH_CBC_MODE|EVP_CIPH_VARIABLE_LENGTH,
611 rc2_init,
612 rc2_do_cipher,
613 NULL,
614 sizeof(struct rc2_cbc),
615 NULL,
616 NULL,
617 NULL,
618 NULL
620 return &rc2_cbc;
624 * The RC2-40 cipher type
626 * @return the RC2-40 EVP_CIPHER pointer.
628 * @ingroup hcrypto_evp
631 const EVP_CIPHER *
632 EVP_hcrypto_rc2_40_cbc(void)
634 static const EVP_CIPHER rc2_40_cbc = {
636 RC2_BLOCK_SIZE,
638 RC2_BLOCK_SIZE,
639 EVP_CIPH_CBC_MODE,
640 rc2_init,
641 rc2_do_cipher,
642 NULL,
643 sizeof(struct rc2_cbc),
644 NULL,
645 NULL,
646 NULL,
647 NULL
649 return &rc2_40_cbc;
653 * The RC2-64 cipher type
655 * @return the RC2-64 EVP_CIPHER pointer.
657 * @ingroup hcrypto_evp
660 const EVP_CIPHER *
661 EVP_hcrypto_rc2_64_cbc(void)
663 static const EVP_CIPHER rc2_64_cbc = {
665 RC2_BLOCK_SIZE,
667 RC2_BLOCK_SIZE,
668 EVP_CIPH_CBC_MODE,
669 rc2_init,
670 rc2_do_cipher,
671 NULL,
672 sizeof(struct rc2_cbc),
673 NULL,
674 NULL,
675 NULL,
676 NULL
678 return &rc2_64_cbc;
681 static int
682 camellia_init(EVP_CIPHER_CTX *ctx,
683 const unsigned char * key,
684 const unsigned char * iv,
685 int encp)
687 CAMELLIA_KEY *k = ctx->cipher_data;
688 k->bits = ctx->cipher->key_len * 8;
689 CAMELLIA_set_key(key, ctx->cipher->key_len * 8, k);
690 return 1;
693 static int
694 camellia_do_cipher(EVP_CIPHER_CTX *ctx,
695 unsigned char *out,
696 const unsigned char *in,
697 unsigned int size)
699 CAMELLIA_KEY *k = ctx->cipher_data;
700 CAMELLIA_cbc_encrypt(in, out, size, k, ctx->iv, ctx->encrypt);
701 return 1;
705 * The Camellia-128 cipher type - hcrypto
707 * @return the Camellia-128 EVP_CIPHER pointer.
709 * @ingroup hcrypto_evp
712 const EVP_CIPHER *
713 EVP_hcrypto_camellia_128_cbc(void)
715 static const EVP_CIPHER cipher = {
720 EVP_CIPH_CBC_MODE,
721 camellia_init,
722 camellia_do_cipher,
723 NULL,
724 sizeof(CAMELLIA_KEY),
725 NULL,
726 NULL,
727 NULL,
728 NULL
730 return &cipher;
734 * The Camellia-198 cipher type - hcrypto
736 * @return the Camellia-198 EVP_CIPHER pointer.
738 * @ingroup hcrypto_evp
741 const EVP_CIPHER *
742 EVP_hcrypto_camellia_192_cbc(void)
744 static const EVP_CIPHER cipher = {
749 EVP_CIPH_CBC_MODE,
750 camellia_init,
751 camellia_do_cipher,
752 NULL,
753 sizeof(CAMELLIA_KEY),
754 NULL,
755 NULL,
756 NULL,
757 NULL
759 return &cipher;
763 * The Camellia-256 cipher type - hcrypto
765 * @return the Camellia-256 EVP_CIPHER pointer.
767 * @ingroup hcrypto_evp
770 const EVP_CIPHER *
771 EVP_hcrypto_camellia_256_cbc(void)
773 static const EVP_CIPHER cipher = {
778 EVP_CIPH_CBC_MODE,
779 camellia_init,
780 camellia_do_cipher,
781 NULL,
782 sizeof(CAMELLIA_KEY),
783 NULL,
784 NULL,
785 NULL,
786 NULL
788 return &cipher;
791 static int
792 rc4_init(EVP_CIPHER_CTX *ctx,
793 const unsigned char *key,
794 const unsigned char *iv,
795 int enc)
797 RC4_KEY *k = ctx->cipher_data;
798 RC4_set_key(k, ctx->key_len, key);
799 return 1;
802 static int
803 rc4_do_cipher(EVP_CIPHER_CTX *ctx,
804 unsigned char *out,
805 const unsigned char *in,
806 unsigned int size)
808 RC4_KEY *k = ctx->cipher_data;
809 RC4(k, size, in, out);
810 return 1;
813 const EVP_CIPHER *
814 EVP_hcrypto_rc4(void)
816 static const EVP_CIPHER rc4 = {
821 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
822 rc4_init,
823 rc4_do_cipher,
824 NULL,
825 sizeof(RC4_KEY),
826 NULL,
827 NULL,
828 NULL,
829 NULL
831 return &rc4;
835 const EVP_CIPHER *
836 EVP_hcrypto_rc4_40(void)
838 static const EVP_CIPHER rc4_40 = {
843 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
844 rc4_init,
845 rc4_do_cipher,
846 NULL,
847 sizeof(RC4_KEY),
848 NULL,
849 NULL,
850 NULL,
851 NULL
853 return &rc4_40;