[HEIMDAL-646] malloc(0) checks for AIX
[heimdal.git] / lib / hcrypto / evp-hcrypto.c
blob699fc667b09a0c9fa98eb01030d84fdd3cf97e47
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 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 message digest SHA256 - hcrypto
181 * @return the message digest type.
183 * @ingroup hcrypto_evp
186 const EVP_MD *
187 EVP_hcrypto_sha256(void)
189 static const struct hc_evp_md sha256 = {
192 sizeof(SHA256_CTX),
193 (hc_evp_md_init)SHA256_Init,
194 (hc_evp_md_update)SHA256_Update,
195 (hc_evp_md_final)SHA256_Final,
196 NULL
198 return &sha256;
202 * The message digest SHA1 - hcrypto
204 * @return the message digest type.
206 * @ingroup hcrypto_evp
209 const EVP_MD *
210 EVP_hcrypto_sha1(void)
212 static const struct hc_evp_md sha1 = {
215 sizeof(SHA_CTX),
216 (hc_evp_md_init)SHA1_Init,
217 (hc_evp_md_update)SHA1_Update,
218 (hc_evp_md_final)SHA1_Final,
219 NULL
221 return &sha1;
225 * The message digest MD5 - hcrypto
227 * @return the message digest type.
229 * @ingroup hcrypto_evp
232 const EVP_MD *
233 EVP_hcrypto_md5(void)
235 static const struct hc_evp_md md5 = {
238 sizeof(MD5_CTX),
239 (hc_evp_md_init)MD5_Init,
240 (hc_evp_md_update)MD5_Update,
241 (hc_evp_md_final)MD5_Final,
242 NULL
244 return &md5;
248 * The message digest MD4 - hcrypto
250 * @return the message digest type.
252 * @ingroup hcrypto_evp
255 const EVP_MD *
256 EVP_hcrypto_md4(void)
258 static const struct hc_evp_md md4 = {
261 sizeof(MD4_CTX),
262 (hc_evp_md_init)MD4_Init,
263 (hc_evp_md_update)MD4_Update,
264 (hc_evp_md_final)MD4_Final,
265 NULL
267 return &md4;
271 * The message digest MD2 - hcrypto
273 * @return the message digest type.
275 * @ingroup hcrypto_evp
278 const EVP_MD *
279 EVP_hcrypto_md2(void)
281 static const struct hc_evp_md md2 = {
284 sizeof(MD2_CTX),
285 (hc_evp_md_init)MD2_Init,
286 (hc_evp_md_update)MD2_Update,
287 (hc_evp_md_final)MD2_Final,
288 NULL
290 return &md2;
297 static int
298 des_cbc_init(EVP_CIPHER_CTX *ctx,
299 const unsigned char * key,
300 const unsigned char * iv,
301 int encp)
303 DES_key_schedule *k = ctx->cipher_data;
304 DES_cblock deskey;
305 memcpy(&deskey, key, sizeof(deskey));
306 DES_set_key_unchecked(&deskey, k);
307 return 1;
310 static int
311 des_cbc_do_cipher(EVP_CIPHER_CTX *ctx,
312 unsigned char *out,
313 const unsigned char *in,
314 unsigned int size)
316 DES_key_schedule *k = ctx->cipher_data;
317 DES_cbc_encrypt(in, out, size,
318 k, (DES_cblock *)ctx->iv, ctx->encrypt);
319 return 1;
323 * The DES cipher type
325 * @return the DES-CBC EVP_CIPHER pointer.
327 * @ingroup hcrypto_evp
330 const EVP_CIPHER *
331 EVP_hcrypto_des_cbc(void)
333 static const EVP_CIPHER des_cbc = {
338 EVP_CIPH_CBC_MODE,
339 des_cbc_init,
340 des_cbc_do_cipher,
341 NULL,
342 sizeof(DES_key_schedule),
343 NULL,
344 NULL,
345 NULL,
346 NULL
348 return &des_cbc;
355 struct des_ede3_cbc {
356 DES_key_schedule ks[3];
359 static int
360 des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
361 const unsigned char * key,
362 const unsigned char * iv,
363 int encp)
365 struct des_ede3_cbc *k = ctx->cipher_data;
366 DES_cblock deskey;
368 memcpy(&deskey, key, sizeof(deskey));
369 DES_set_odd_parity(&deskey);
370 DES_set_key_unchecked(&deskey, &k->ks[0]);
372 memcpy(&deskey, key + 8, sizeof(deskey));
373 DES_set_odd_parity(&deskey);
374 DES_set_key_unchecked(&deskey, &k->ks[1]);
376 memcpy(&deskey, key + 16, sizeof(deskey));
377 DES_set_odd_parity(&deskey);
378 DES_set_key_unchecked(&deskey, &k->ks[2]);
380 return 1;
383 static int
384 des_ede3_cbc_do_cipher(EVP_CIPHER_CTX *ctx,
385 unsigned char *out,
386 const unsigned char *in,
387 unsigned int size)
389 struct des_ede3_cbc *k = ctx->cipher_data;
390 DES_ede3_cbc_encrypt(in, out, size,
391 &k->ks[0], &k->ks[1], &k->ks[2],
392 (DES_cblock *)ctx->iv, ctx->encrypt);
393 return 1;
397 * The tripple DES cipher type - hcrypto
399 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
401 * @ingroup hcrypto_evp
404 const EVP_CIPHER *
405 EVP_hcrypto_des_ede3_cbc(void)
407 static const EVP_CIPHER des_ede3_cbc = {
412 EVP_CIPH_CBC_MODE,
413 des_ede3_cbc_init,
414 des_ede3_cbc_do_cipher,
415 NULL,
416 sizeof(struct des_ede3_cbc),
417 NULL,
418 NULL,
419 NULL,
420 NULL
422 return &des_ede3_cbc;
429 struct rc2_cbc {
430 unsigned int maximum_effective_key;
431 RC2_KEY key;
434 static int
435 rc2_init(EVP_CIPHER_CTX *ctx,
436 const unsigned char * key,
437 const unsigned char * iv,
438 int encp)
440 struct rc2_cbc *k = ctx->cipher_data;
441 k->maximum_effective_key = EVP_CIPHER_CTX_key_length(ctx) * 8;
442 RC2_set_key(&k->key,
443 EVP_CIPHER_CTX_key_length(ctx),
444 key,
445 k->maximum_effective_key);
446 return 1;
449 static int
450 rc2_do_cipher(EVP_CIPHER_CTX *ctx,
451 unsigned char *out,
452 const unsigned char *in,
453 unsigned int size)
455 struct rc2_cbc *k = ctx->cipher_data;
456 RC2_cbc_encrypt(in, out, size, &k->key, ctx->iv, ctx->encrypt);
457 return 1;
461 * The RC2 cipher type - hcrypto
463 * @return the RC2 EVP_CIPHER pointer.
465 * @ingroup hcrypto_evp
468 const EVP_CIPHER *
469 EVP_hcrypto_rc2_cbc(void)
471 static const EVP_CIPHER rc2_cbc = {
473 RC2_BLOCK_SIZE,
474 RC2_KEY_LENGTH,
475 RC2_BLOCK_SIZE,
476 EVP_CIPH_CBC_MODE|EVP_CIPH_VARIABLE_LENGTH,
477 rc2_init,
478 rc2_do_cipher,
479 NULL,
480 sizeof(struct rc2_cbc),
481 NULL,
482 NULL,
483 NULL,
484 NULL
486 return &rc2_cbc;
490 * The RC2-40 cipher type
492 * @return the RC2-40 EVP_CIPHER pointer.
494 * @ingroup hcrypto_evp
497 const EVP_CIPHER *
498 EVP_hcrypto_rc2_40_cbc(void)
500 static const EVP_CIPHER rc2_40_cbc = {
502 RC2_BLOCK_SIZE,
504 RC2_BLOCK_SIZE,
505 EVP_CIPH_CBC_MODE,
506 rc2_init,
507 rc2_do_cipher,
508 NULL,
509 sizeof(struct rc2_cbc),
510 NULL,
511 NULL,
512 NULL,
513 NULL
515 return &rc2_40_cbc;
519 * The RC2-64 cipher type
521 * @return the RC2-64 EVP_CIPHER pointer.
523 * @ingroup hcrypto_evp
526 const EVP_CIPHER *
527 EVP_hcrypto_rc2_64_cbc(void)
529 static const EVP_CIPHER rc2_64_cbc = {
531 RC2_BLOCK_SIZE,
533 RC2_BLOCK_SIZE,
534 EVP_CIPH_CBC_MODE,
535 rc2_init,
536 rc2_do_cipher,
537 NULL,
538 sizeof(struct rc2_cbc),
539 NULL,
540 NULL,
541 NULL,
542 NULL
544 return &rc2_64_cbc;
547 static int
548 camellia_init(EVP_CIPHER_CTX *ctx,
549 const unsigned char * key,
550 const unsigned char * iv,
551 int encp)
553 CAMELLIA_KEY *k = ctx->cipher_data;
554 k->bits = ctx->cipher->key_len * 8;
555 CAMELLIA_set_key(key, ctx->cipher->key_len * 8, k);
556 return 1;
559 static int
560 camellia_do_cipher(EVP_CIPHER_CTX *ctx,
561 unsigned char *out,
562 const unsigned char *in,
563 unsigned int size)
565 CAMELLIA_KEY *k = ctx->cipher_data;
566 CAMELLIA_cbc_encrypt(in, out, size, k, ctx->iv, ctx->encrypt);
567 return 1;
571 * The Camellia-128 cipher type - hcrypto
573 * @return the Camellia-128 EVP_CIPHER pointer.
575 * @ingroup hcrypto_evp
578 const EVP_CIPHER *
579 EVP_hcrypto_camellia_128_cbc(void)
581 static const EVP_CIPHER cipher = {
586 EVP_CIPH_CBC_MODE,
587 camellia_init,
588 camellia_do_cipher,
589 NULL,
590 sizeof(CAMELLIA_KEY),
591 NULL,
592 NULL,
593 NULL,
594 NULL
596 return &cipher;
600 * The Camellia-198 cipher type - hcrypto
602 * @return the Camellia-198 EVP_CIPHER pointer.
604 * @ingroup hcrypto_evp
607 const EVP_CIPHER *
608 EVP_hcrypto_camellia_192_cbc(void)
610 static const EVP_CIPHER cipher = {
615 EVP_CIPH_CBC_MODE,
616 camellia_init,
617 camellia_do_cipher,
618 NULL,
619 sizeof(CAMELLIA_KEY),
620 NULL,
621 NULL,
622 NULL,
623 NULL
625 return &cipher;
629 * The Camellia-256 cipher type - hcrypto
631 * @return the Camellia-256 EVP_CIPHER pointer.
633 * @ingroup hcrypto_evp
636 const EVP_CIPHER *
637 EVP_hcrypto_camellia_256_cbc(void)
639 static const EVP_CIPHER cipher = {
644 EVP_CIPH_CBC_MODE,
645 camellia_init,
646 camellia_do_cipher,
647 NULL,
648 sizeof(CAMELLIA_KEY),
649 NULL,
650 NULL,
651 NULL,
652 NULL
654 return &cipher;
657 static int
658 rc4_init(EVP_CIPHER_CTX *ctx,
659 const unsigned char *key,
660 const unsigned char *iv,
661 int enc)
663 RC4_KEY *k = ctx->cipher_data;
664 RC4_set_key(k, ctx->key_len, key);
665 return 1;
668 static int
669 rc4_do_cipher(EVP_CIPHER_CTX *ctx,
670 unsigned char *out,
671 const unsigned char *in,
672 unsigned int size)
674 RC4_KEY *k = ctx->cipher_data;
675 RC4(k, size, in, out);
676 return 1;
679 const EVP_CIPHER *
680 EVP_hcrypto_rc4(void)
682 static const EVP_CIPHER rc4 = {
687 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
688 rc4_init,
689 rc4_do_cipher,
690 NULL,
691 sizeof(RC4_KEY),
692 NULL,
693 NULL,
694 NULL,
695 NULL
697 return &rc4;
701 const EVP_CIPHER *
702 EVP_hcrypto_rc4_40(void)
704 static const EVP_CIPHER rc4_40 = {
709 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
710 rc4_init,
711 rc4_do_cipher,
712 NULL,
713 sizeof(RC4_KEY),
714 NULL,
715 NULL,
716 NULL,
717 NULL
719 return &rc4_40;