Remove building with NOCRYPTO option
[minix.git] / crypto / external / bsd / heimdal / dist / lib / hcrypto / evp-cc.c
blobb1a4dd11a2d891dc6f189e41d1354dbff35e2e86
1 /* $NetBSD: evp-cc.c,v 1.1.1.1 2011/04/13 18:14:49 elric Exp $ */
3 /*
4 * Copyright (c) 2008 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
8 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the Institute nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
38 /* CommonCrypto provider */
40 #ifdef __APPLE__
42 #include "config.h"
44 #include <sys/types.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <assert.h>
50 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
51 #include <CommonCrypto/CommonDigest.h>
52 #endif
53 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
54 #include <CommonCrypto/CommonCryptor.h>
55 #endif
57 #include <evp.h>
58 #include <evp-cc.h>
64 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
66 struct cc_key {
67 CCCryptorRef href;
70 static int
71 cc_do_cipher(EVP_CIPHER_CTX *ctx,
72 unsigned char *out,
73 const unsigned char *in,
74 unsigned int size)
76 struct cc_key *cc = ctx->cipher_data;
77 CCCryptorStatus ret;
78 size_t moved;
80 memcpy(out, in, size);
82 ret = CCCryptorUpdate(cc->href, in, size, out, size, &moved);
83 if (ret)
84 return 0;
86 if (moved != size)
87 return 0;
89 return 1;
92 static int
93 cc_do_cfb8_cipher(EVP_CIPHER_CTX *ctx,
94 unsigned char *out,
95 const unsigned char *in,
96 unsigned int size)
98 struct cc_key *cc = ctx->cipher_data;
99 CCCryptorStatus ret;
100 size_t moved;
101 unsigned int i;
103 for (i = 0; i < size; i++) {
104 unsigned char oiv[EVP_MAX_IV_LENGTH + 1];
106 assert(ctx->cipher->iv_len + 1 <= sizeof(oiv));
107 memcpy(oiv, ctx->iv, ctx->cipher->iv_len);
109 ret = CCCryptorUpdate(cc->href, ctx->iv, ctx->cipher->iv_len,
110 ctx->iv, ctx->cipher->iv_len, &moved);
111 if (ret)
112 return 0;
114 if (moved != ctx->cipher->iv_len)
115 return 0;
117 if (!ctx->encrypt)
118 oiv[ctx->cipher->iv_len] = in[i];
119 out[i] = in[i] ^ ctx->iv[0];
120 if (ctx->encrypt)
121 oiv[ctx->cipher->iv_len] = out[i];
123 memcpy(ctx->iv, &oiv[1], ctx->cipher->iv_len);
126 return 1;
129 static int
130 cc_cleanup(EVP_CIPHER_CTX *ctx)
132 struct cc_key *cc = ctx->cipher_data;
133 if (cc->href)
134 CCCryptorRelease(cc->href);
135 return 1;
138 static int
139 init_cc_key(int encp, CCAlgorithm alg, CCOptions opts, const void *key,
140 size_t keylen, const void *iv, CCCryptorRef *ref)
142 CCOperation op = encp ? kCCEncrypt : kCCDecrypt;
143 CCCryptorStatus ret;
145 if (*ref) {
146 if (key == NULL && iv) {
147 CCCryptorReset(*ref, iv);
148 return 1;
150 CCCryptorRelease(*ref);
153 ret = CCCryptorCreate(op, alg, opts, key, keylen, iv, ref);
154 if (ret)
155 return 0;
156 return 1;
159 static int
160 cc_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
161 const unsigned char * key,
162 const unsigned char * iv,
163 int encp)
165 struct cc_key *cc = ctx->cipher_data;
166 return init_cc_key(encp, kCCAlgorithm3DES, 0, key, kCCKeySize3DES, iv, &cc->href);
169 #endif /* HAVE_COMMONCRYPTO_COMMONCRYPTOR_H */
172 * The tripple DES cipher type (Apple CommonCrypto provider)
174 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
176 * @ingroup hcrypto_evp
179 const EVP_CIPHER *
180 EVP_cc_des_ede3_cbc(void)
182 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
183 static const EVP_CIPHER des_ede3_cbc = {
188 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
189 cc_des_ede3_cbc_init,
190 cc_do_cipher,
191 cc_cleanup,
192 sizeof(struct cc_key),
193 NULL,
194 NULL,
195 NULL,
196 NULL
198 return &des_ede3_cbc;
199 #else
200 return NULL;
201 #endif
204 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
209 static int
210 cc_des_cbc_init(EVP_CIPHER_CTX *ctx,
211 const unsigned char * key,
212 const unsigned char * iv,
213 int encp)
215 struct cc_key *cc = ctx->cipher_data;
216 return init_cc_key(encp, kCCAlgorithmDES, 0, key, kCCBlockSizeDES, iv, &cc->href);
218 #endif
221 * The DES cipher type (Apple CommonCrypto provider)
223 * @return the DES-CBC EVP_CIPHER pointer.
225 * @ingroup hcrypto_evp
228 const EVP_CIPHER *
229 EVP_cc_des_cbc(void)
231 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
232 static const EVP_CIPHER des_ede3_cbc = {
234 kCCBlockSizeDES,
235 kCCBlockSizeDES,
236 kCCBlockSizeDES,
237 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
238 cc_des_cbc_init,
239 cc_do_cipher,
240 cc_cleanup,
241 sizeof(struct cc_key),
242 NULL,
243 NULL,
244 NULL,
245 NULL
247 return &des_ede3_cbc;
248 #else
249 return NULL;
250 #endif
253 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
258 static int
259 cc_aes_cbc_init(EVP_CIPHER_CTX *ctx,
260 const unsigned char * key,
261 const unsigned char * iv,
262 int encp)
264 struct cc_key *cc = ctx->cipher_data;
265 return init_cc_key(encp, kCCAlgorithmAES128, 0, key, ctx->cipher->key_len, iv, &cc->href);
267 #endif
270 * The AES-128 cipher type (Apple CommonCrypto provider)
272 * @return the AES-128-CBC EVP_CIPHER pointer.
274 * @ingroup hcrypto_evp
277 const EVP_CIPHER *
278 EVP_cc_aes_128_cbc(void)
280 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
281 static const EVP_CIPHER c = {
283 kCCBlockSizeAES128,
284 kCCKeySizeAES128,
285 kCCBlockSizeAES128,
286 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
287 cc_aes_cbc_init,
288 cc_do_cipher,
289 cc_cleanup,
290 sizeof(struct cc_key),
291 NULL,
292 NULL,
293 NULL,
294 NULL
296 return &c;
297 #else
298 return NULL;
299 #endif
303 * The AES-192 cipher type (Apple CommonCrypto provider)
305 * @return the AES-192-CBC EVP_CIPHER pointer.
307 * @ingroup hcrypto_evp
310 const EVP_CIPHER *
311 EVP_cc_aes_192_cbc(void)
313 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
314 static const EVP_CIPHER c = {
316 kCCBlockSizeAES128,
317 kCCKeySizeAES192,
318 kCCBlockSizeAES128,
319 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
320 cc_aes_cbc_init,
321 cc_do_cipher,
322 cc_cleanup,
323 sizeof(struct cc_key),
324 NULL,
325 NULL,
326 NULL,
327 NULL
329 return &c;
330 #else
331 return NULL;
332 #endif
336 * The AES-256 cipher type (Apple CommonCrypto provider)
338 * @return the AES-256-CBC EVP_CIPHER pointer.
340 * @ingroup hcrypto_evp
343 const EVP_CIPHER *
344 EVP_cc_aes_256_cbc(void)
346 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
347 static const EVP_CIPHER c = {
349 kCCBlockSizeAES128,
350 kCCKeySizeAES256,
351 kCCBlockSizeAES128,
352 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
353 cc_aes_cbc_init,
354 cc_do_cipher,
355 cc_cleanup,
356 sizeof(struct cc_key),
357 NULL,
358 NULL,
359 NULL,
360 NULL
362 return &c;
363 #else
364 return NULL;
365 #endif
368 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
373 static int
374 cc_aes_cfb8_init(EVP_CIPHER_CTX *ctx,
375 const unsigned char * key,
376 const unsigned char * iv,
377 int encp)
379 struct cc_key *cc = ctx->cipher_data;
380 memcpy(ctx->iv, iv, ctx->cipher->iv_len);
381 return init_cc_key(1, kCCAlgorithmAES128, kCCOptionECBMode,
382 key, ctx->cipher->key_len, NULL, &cc->href);
384 #endif
387 * The AES-128 CFB8 cipher type (Apple CommonCrypto provider)
389 * @return the AES-128-CFB8 EVP_CIPHER pointer.
391 * @ingroup hcrypto_evp
394 const EVP_CIPHER *
395 EVP_cc_aes_128_cfb8(void)
397 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
398 static const EVP_CIPHER c = {
401 kCCKeySizeAES128,
402 kCCBlockSizeAES128,
403 EVP_CIPH_CFB8_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
404 cc_aes_cfb8_init,
405 cc_do_cfb8_cipher,
406 cc_cleanup,
407 sizeof(struct cc_key),
408 NULL,
409 NULL,
410 NULL,
411 NULL
413 return &c;
414 #else
415 return NULL;
416 #endif
420 * The AES-192 CFB8 cipher type (Apple CommonCrypto provider)
422 * @return the AES-192-CFB8 EVP_CIPHER pointer.
424 * @ingroup hcrypto_evp
427 const EVP_CIPHER *
428 EVP_cc_aes_192_cfb8(void)
430 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
431 static const EVP_CIPHER c = {
434 kCCKeySizeAES192,
435 kCCBlockSizeAES128,
436 EVP_CIPH_CFB8_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
437 cc_aes_cfb8_init,
438 cc_do_cfb8_cipher,
439 cc_cleanup,
440 sizeof(struct cc_key),
441 NULL,
442 NULL,
443 NULL,
444 NULL
446 return &c;
447 #else
448 return NULL;
449 #endif
453 * The AES-256 CFB8 cipher type (Apple CommonCrypto provider)
455 * @return the AES-256-CFB8 EVP_CIPHER pointer.
457 * @ingroup hcrypto_evp
460 const EVP_CIPHER *
461 EVP_cc_aes_256_cfb8(void)
463 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
464 static const EVP_CIPHER c = {
466 kCCBlockSizeAES128,
467 kCCKeySizeAES256,
468 kCCBlockSizeAES128,
469 EVP_CIPH_CFB8_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
470 cc_aes_cfb8_init,
471 cc_do_cfb8_cipher,
472 cc_cleanup,
473 sizeof(struct cc_key),
474 NULL,
475 NULL,
476 NULL,
477 NULL
479 return &c;
480 #else
481 return NULL;
482 #endif
489 #ifdef COMMONCRYPTO_SUPPORTS_RC2
490 static int
491 cc_rc2_cbc_init(EVP_CIPHER_CTX *ctx,
492 const unsigned char * key,
493 const unsigned char * iv,
494 int encp)
496 struct cc_key *cc = ctx->cipher_data;
497 return init_cc_key(encp, kCCAlgorithmRC2, 0, key, ctx->cipher->key_len, iv, &cc->href);
499 #endif
502 * The RC2 cipher type - common crypto
504 * @return the RC2 EVP_CIPHER pointer.
506 * @ingroup hcrypto_evp
510 const EVP_CIPHER *
511 EVP_cc_rc2_cbc(void)
513 #ifdef COMMONCRYPTO_SUPPORTS_RC2
514 static const EVP_CIPHER rc2_cbc = {
516 kCCBlockSizeRC2,
518 kCCBlockSizeRC2,
519 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
520 cc_rc2_cbc_init,
521 cc_do_cipher,
522 cc_cleanup,
523 sizeof(struct cc_key),
524 NULL,
525 NULL,
526 NULL,
527 NULL
529 return &rc2_cbc;
530 #else
531 return NULL;
532 #endif
536 * The RC2-40 cipher type - common crypto
538 * @return the RC2-40 EVP_CIPHER pointer.
540 * @ingroup hcrypto_evp
544 const EVP_CIPHER *
545 EVP_cc_rc2_40_cbc(void)
547 #ifdef COMMONCRYPTO_SUPPORTS_RC2
548 static const EVP_CIPHER rc2_40_cbc = {
550 kCCBlockSizeRC2,
552 kCCBlockSizeRC2,
553 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
554 cc_rc2_cbc_init,
555 cc_do_cipher,
556 cc_cleanup,
557 sizeof(struct cc_key),
558 NULL,
559 NULL,
560 NULL,
561 NULL
563 return &rc2_40_cbc;
564 #else
565 return NULL;
566 #endif
571 * The RC2-64 cipher type - common crypto
573 * @return the RC2-64 EVP_CIPHER pointer.
575 * @ingroup hcrypto_evp
579 const EVP_CIPHER *
580 EVP_cc_rc2_64_cbc(void)
582 #ifdef COMMONCRYPTO_SUPPORTS_RC2
583 static const EVP_CIPHER rc2_64_cbc = {
585 kCCBlockSizeRC2,
587 kCCBlockSizeRC2,
588 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
589 cc_rc2_cbc_init,
590 cc_do_cipher,
591 cc_cleanup,
592 sizeof(struct cc_key),
593 NULL,
594 NULL,
595 NULL,
596 NULL
598 return &rc2_64_cbc;
599 #else
600 return NULL;
601 #endif
605 * The CommonCrypto md2 provider
607 * @ingroup hcrypto_evp
610 const EVP_MD *
611 EVP_cc_md2(void)
613 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
614 static const struct hc_evp_md md2 = {
615 CC_MD2_DIGEST_LENGTH,
616 CC_MD2_BLOCK_BYTES,
617 sizeof(CC_MD2_CTX),
618 (hc_evp_md_init)CC_MD2_Init,
619 (hc_evp_md_update)CC_MD2_Update,
620 (hc_evp_md_final)CC_MD2_Final,
621 (hc_evp_md_cleanup)NULL
623 return &md2;
624 #else
625 return NULL;
626 #endif
630 * The CommonCrypto md4 provider
632 * @ingroup hcrypto_evp
635 const EVP_MD *
636 EVP_cc_md4(void)
638 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
639 static const struct hc_evp_md md4 = {
640 CC_MD4_DIGEST_LENGTH,
641 CC_MD4_BLOCK_BYTES,
642 sizeof(CC_MD4_CTX),
643 (hc_evp_md_init)CC_MD4_Init,
644 (hc_evp_md_update)CC_MD4_Update,
645 (hc_evp_md_final)CC_MD4_Final,
646 (hc_evp_md_cleanup)NULL
648 return &md4;
649 #else
650 return NULL;
651 #endif
655 * The CommonCrypto md5 provider
657 * @ingroup hcrypto_evp
660 const EVP_MD *
661 EVP_cc_md5(void)
663 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
664 static const struct hc_evp_md md5 = {
665 CC_MD5_DIGEST_LENGTH,
666 CC_MD5_BLOCK_BYTES,
667 sizeof(CC_MD5_CTX),
668 (hc_evp_md_init)CC_MD5_Init,
669 (hc_evp_md_update)CC_MD5_Update,
670 (hc_evp_md_final)CC_MD5_Final,
671 (hc_evp_md_cleanup)NULL
673 return &md5;
674 #else
675 return NULL;
676 #endif
680 * The CommonCrypto sha1 provider
682 * @ingroup hcrypto_evp
685 const EVP_MD *
686 EVP_cc_sha1(void)
688 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
689 static const struct hc_evp_md sha1 = {
690 CC_SHA1_DIGEST_LENGTH,
691 CC_SHA1_BLOCK_BYTES,
692 sizeof(CC_SHA1_CTX),
693 (hc_evp_md_init)CC_SHA1_Init,
694 (hc_evp_md_update)CC_SHA1_Update,
695 (hc_evp_md_final)CC_SHA1_Final,
696 (hc_evp_md_cleanup)NULL
698 return &sha1;
699 #else
700 return NULL;
701 #endif
705 * The CommonCrypto sha256 provider
707 * @ingroup hcrypto_evp
710 const EVP_MD *
711 EVP_cc_sha256(void)
713 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
714 static const struct hc_evp_md sha256 = {
715 CC_SHA256_DIGEST_LENGTH,
716 CC_SHA256_BLOCK_BYTES,
717 sizeof(CC_SHA256_CTX),
718 (hc_evp_md_init)CC_SHA256_Init,
719 (hc_evp_md_update)CC_SHA256_Update,
720 (hc_evp_md_final)CC_SHA256_Final,
721 (hc_evp_md_cleanup)NULL
723 return &sha256;
724 #else
725 return NULL;
726 #endif
730 * The Camellia-128 cipher type - CommonCrypto
732 * @return the Camellia-128 EVP_CIPHER pointer.
734 * @ingroup hcrypto_evp
737 const EVP_CIPHER *
738 EVP_cc_camellia_128_cbc(void)
740 return NULL;
744 * The Camellia-198 cipher type - CommonCrypto
746 * @return the Camellia-198 EVP_CIPHER pointer.
748 * @ingroup hcrypto_evp
751 const EVP_CIPHER *
752 EVP_cc_camellia_192_cbc(void)
754 return NULL;
758 * The Camellia-256 cipher type - CommonCrypto
760 * @return the Camellia-256 EVP_CIPHER pointer.
762 * @ingroup hcrypto_evp
765 const EVP_CIPHER *
766 EVP_cc_camellia_256_cbc(void)
768 return NULL;
771 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
777 static int
778 cc_rc4_init(EVP_CIPHER_CTX *ctx,
779 const unsigned char * key,
780 const unsigned char * iv,
781 int encp)
783 struct cc_key *cc = ctx->cipher_data;
784 return init_cc_key(encp, kCCAlgorithmRC4, 0, key, ctx->key_len, iv, &cc->href);
787 #endif
791 * The RC4 cipher type (Apple CommonCrypto provider)
793 * @return the RC4 EVP_CIPHER pointer.
795 * @ingroup hcrypto_evp
798 const EVP_CIPHER *
799 EVP_cc_rc4(void)
801 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
802 static const EVP_CIPHER rc4 = {
807 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
808 cc_rc4_init,
809 cc_do_cipher,
810 cc_cleanup,
811 sizeof(struct cc_key),
812 NULL,
813 NULL,
814 NULL,
815 NULL
817 return &rc4;
818 #else
819 return NULL;
820 #endif
825 * The RC4-40 cipher type (Apple CommonCrypto provider)
827 * @return the RC4 EVP_CIPHER pointer.
829 * @ingroup hcrypto_evp
832 const EVP_CIPHER *
833 EVP_cc_rc4_40(void)
835 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
836 static const EVP_CIPHER rc4_40 = {
841 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
842 cc_rc4_init,
843 cc_do_cipher,
844 cc_cleanup,
845 sizeof(struct cc_key),
846 NULL,
847 NULL,
848 NULL,
849 NULL
851 return &rc4_40;
852 #else
853 return NULL;
854 #endif
857 #endif /* __APPLE__ */