xref: /minix3/crypto/external/bsd/heimdal/dist/lib/hcrypto/evp-cc.c (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1 /*	$NetBSD: evp-cc.c,v 1.1.1.1 2011/04/13 18:14:49 elric Exp $	*/
2 
3 /*
4  * Copyright (c) 2008 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  *
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.
20  *
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.
24  *
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.
36  */
37 
38 /* CommonCrypto provider */
39 
40 #ifdef __APPLE__
41 
42 #include "config.h"
43 
44 #include <sys/types.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <assert.h>
49 
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
56 
57 #include <evp.h>
58 #include <evp-cc.h>
59 
60 /*
61  *
62  */
63 
64 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
65 
66 struct cc_key {
67     CCCryptorRef href;
68 };
69 
70 static int
cc_do_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,unsigned int size)71 cc_do_cipher(EVP_CIPHER_CTX *ctx,
72 	     unsigned char *out,
73 	     const unsigned char *in,
74 	     unsigned int size)
75 {
76     struct cc_key *cc = ctx->cipher_data;
77     CCCryptorStatus ret;
78     size_t moved;
79 
80     memcpy(out, in, size);
81 
82     ret = CCCryptorUpdate(cc->href, in, size, out, size, &moved);
83     if (ret)
84 	return 0;
85 
86     if (moved != size)
87 	return 0;
88 
89     return 1;
90 }
91 
92 static int
cc_do_cfb8_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,unsigned int size)93 cc_do_cfb8_cipher(EVP_CIPHER_CTX *ctx,
94                   unsigned char *out,
95                   const unsigned char *in,
96                   unsigned int size)
97 {
98     struct cc_key *cc = ctx->cipher_data;
99     CCCryptorStatus ret;
100     size_t moved;
101     unsigned int i;
102 
103     for (i = 0; i < size; i++) {
104         unsigned char oiv[EVP_MAX_IV_LENGTH + 1];
105 
106         assert(ctx->cipher->iv_len + 1 <= sizeof(oiv));
107         memcpy(oiv, ctx->iv, ctx->cipher->iv_len);
108 
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;
113 
114         if (moved != ctx->cipher->iv_len)
115             return 0;
116 
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];
122 
123         memcpy(ctx->iv, &oiv[1], ctx->cipher->iv_len);
124     }
125 
126     return 1;
127 }
128 
129 static int
cc_cleanup(EVP_CIPHER_CTX * ctx)130 cc_cleanup(EVP_CIPHER_CTX *ctx)
131 {
132     struct cc_key *cc = ctx->cipher_data;
133     if (cc->href)
134 	CCCryptorRelease(cc->href);
135     return 1;
136 }
137 
138 static int
init_cc_key(int encp,CCAlgorithm alg,CCOptions opts,const void * key,size_t keylen,const void * iv,CCCryptorRef * ref)139 init_cc_key(int encp, CCAlgorithm alg, CCOptions opts, const void *key,
140 	    size_t keylen, const void *iv, CCCryptorRef *ref)
141 {
142     CCOperation op = encp ? kCCEncrypt : kCCDecrypt;
143     CCCryptorStatus ret;
144 
145     if (*ref) {
146 	if (key == NULL && iv) {
147 	    CCCryptorReset(*ref, iv);
148 	    return 1;
149 	}
150 	CCCryptorRelease(*ref);
151     }
152 
153     ret = CCCryptorCreate(op, alg, opts, key, keylen, iv, ref);
154     if (ret)
155 	return 0;
156     return 1;
157 }
158 
159 static int
cc_des_ede3_cbc_init(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int encp)160 cc_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
161 		     const unsigned char * key,
162 		     const unsigned char * iv,
163 		     int encp)
164 {
165     struct cc_key *cc = ctx->cipher_data;
166     return init_cc_key(encp, kCCAlgorithm3DES, 0, key, kCCKeySize3DES, iv, &cc->href);
167 }
168 
169 #endif /* HAVE_COMMONCRYPTO_COMMONCRYPTOR_H */
170 
171 /**
172  * The tripple DES cipher type (Apple CommonCrypto provider)
173  *
174  * @return the DES-EDE3-CBC EVP_CIPHER pointer.
175  *
176  * @ingroup hcrypto_evp
177  */
178 
179 const EVP_CIPHER *
EVP_cc_des_ede3_cbc(void)180 EVP_cc_des_ede3_cbc(void)
181 {
182 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
183     static const EVP_CIPHER des_ede3_cbc = {
184 	0,
185 	8,
186 	24,
187 	8,
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
197     };
198     return &des_ede3_cbc;
199 #else
200     return NULL;
201 #endif
202 }
203 
204 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
205 /*
206  *
207  */
208 
209 static int
cc_des_cbc_init(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int encp)210 cc_des_cbc_init(EVP_CIPHER_CTX *ctx,
211 		const unsigned char * key,
212 		const unsigned char * iv,
213 		int encp)
214 {
215     struct cc_key *cc = ctx->cipher_data;
216     return init_cc_key(encp, kCCAlgorithmDES, 0, key, kCCBlockSizeDES, iv, &cc->href);
217 }
218 #endif
219 
220 /**
221  * The DES cipher type (Apple CommonCrypto provider)
222  *
223  * @return the DES-CBC EVP_CIPHER pointer.
224  *
225  * @ingroup hcrypto_evp
226  */
227 
228 const EVP_CIPHER *
EVP_cc_des_cbc(void)229 EVP_cc_des_cbc(void)
230 {
231 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
232     static const EVP_CIPHER des_ede3_cbc = {
233 	0,
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
246     };
247     return &des_ede3_cbc;
248 #else
249     return NULL;
250 #endif
251 }
252 
253 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
254 /*
255  *
256  */
257 
258 static int
cc_aes_cbc_init(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int encp)259 cc_aes_cbc_init(EVP_CIPHER_CTX *ctx,
260 		const unsigned char * key,
261 		const unsigned char * iv,
262 		int encp)
263 {
264     struct cc_key *cc = ctx->cipher_data;
265     return init_cc_key(encp, kCCAlgorithmAES128, 0, key, ctx->cipher->key_len, iv, &cc->href);
266 }
267 #endif
268 
269 /**
270  * The AES-128 cipher type (Apple CommonCrypto provider)
271  *
272  * @return the AES-128-CBC EVP_CIPHER pointer.
273  *
274  * @ingroup hcrypto_evp
275  */
276 
277 const EVP_CIPHER *
EVP_cc_aes_128_cbc(void)278 EVP_cc_aes_128_cbc(void)
279 {
280 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
281     static const EVP_CIPHER c = {
282 	0,
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
295     };
296     return &c;
297 #else
298     return NULL;
299 #endif
300 }
301 
302 /**
303  * The AES-192 cipher type (Apple CommonCrypto provider)
304  *
305  * @return the AES-192-CBC EVP_CIPHER pointer.
306  *
307  * @ingroup hcrypto_evp
308  */
309 
310 const EVP_CIPHER *
EVP_cc_aes_192_cbc(void)311 EVP_cc_aes_192_cbc(void)
312 {
313 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
314     static const EVP_CIPHER c = {
315 	0,
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
328     };
329     return &c;
330 #else
331     return NULL;
332 #endif
333 }
334 
335 /**
336  * The AES-256 cipher type (Apple CommonCrypto provider)
337  *
338  * @return the AES-256-CBC EVP_CIPHER pointer.
339  *
340  * @ingroup hcrypto_evp
341  */
342 
343 const EVP_CIPHER *
EVP_cc_aes_256_cbc(void)344 EVP_cc_aes_256_cbc(void)
345 {
346 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
347     static const EVP_CIPHER c = {
348 	0,
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
361     };
362     return &c;
363 #else
364     return NULL;
365 #endif
366 }
367 
368 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
369 /*
370  *
371  */
372 
373 static int
cc_aes_cfb8_init(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int encp)374 cc_aes_cfb8_init(EVP_CIPHER_CTX *ctx,
375 		const unsigned char * key,
376 		const unsigned char * iv,
377 		int encp)
378 {
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);
383 }
384 #endif
385 
386 /**
387  * The AES-128 CFB8 cipher type (Apple CommonCrypto provider)
388  *
389  * @return the AES-128-CFB8 EVP_CIPHER pointer.
390  *
391  * @ingroup hcrypto_evp
392  */
393 
394 const EVP_CIPHER *
EVP_cc_aes_128_cfb8(void)395 EVP_cc_aes_128_cfb8(void)
396 {
397 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
398     static const EVP_CIPHER c = {
399 	0,
400 	1,
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
412     };
413     return &c;
414 #else
415     return NULL;
416 #endif
417 }
418 
419 /**
420  * The AES-192 CFB8 cipher type (Apple CommonCrypto provider)
421  *
422  * @return the AES-192-CFB8 EVP_CIPHER pointer.
423  *
424  * @ingroup hcrypto_evp
425  */
426 
427 const EVP_CIPHER *
EVP_cc_aes_192_cfb8(void)428 EVP_cc_aes_192_cfb8(void)
429 {
430 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
431     static const EVP_CIPHER c = {
432 	0,
433 	1,
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
445     };
446     return &c;
447 #else
448     return NULL;
449 #endif
450 }
451 
452 /**
453  * The AES-256 CFB8 cipher type (Apple CommonCrypto provider)
454  *
455  * @return the AES-256-CFB8 EVP_CIPHER pointer.
456  *
457  * @ingroup hcrypto_evp
458  */
459 
460 const EVP_CIPHER *
EVP_cc_aes_256_cfb8(void)461 EVP_cc_aes_256_cfb8(void)
462 {
463 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
464     static const EVP_CIPHER c = {
465 	0,
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
478     };
479     return &c;
480 #else
481     return NULL;
482 #endif
483 }
484 
485 /*
486  *
487  */
488 
489 #ifdef COMMONCRYPTO_SUPPORTS_RC2
490 static int
cc_rc2_cbc_init(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int encp)491 cc_rc2_cbc_init(EVP_CIPHER_CTX *ctx,
492 		const unsigned char * key,
493 		const unsigned char * iv,
494 		int encp)
495 {
496     struct cc_key *cc = ctx->cipher_data;
497     return init_cc_key(encp, kCCAlgorithmRC2, 0, key, ctx->cipher->key_len, iv, &cc->href);
498 }
499 #endif
500 
501 /**
502  * The RC2 cipher type - common crypto
503  *
504  * @return the RC2 EVP_CIPHER pointer.
505  *
506  * @ingroup hcrypto_evp
507  */
508 
509 
510 const EVP_CIPHER *
EVP_cc_rc2_cbc(void)511 EVP_cc_rc2_cbc(void)
512 {
513 #ifdef COMMONCRYPTO_SUPPORTS_RC2
514     static const EVP_CIPHER rc2_cbc = {
515 	0,
516 	kCCBlockSizeRC2,
517 	16,
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
528     };
529     return &rc2_cbc;
530 #else
531     return NULL;
532 #endif
533 }
534 
535 /**
536  * The RC2-40 cipher type - common crypto
537  *
538  * @return the RC2-40 EVP_CIPHER pointer.
539  *
540  * @ingroup hcrypto_evp
541  */
542 
543 
544 const EVP_CIPHER *
EVP_cc_rc2_40_cbc(void)545 EVP_cc_rc2_40_cbc(void)
546 {
547 #ifdef COMMONCRYPTO_SUPPORTS_RC2
548     static const EVP_CIPHER rc2_40_cbc = {
549 	0,
550 	kCCBlockSizeRC2,
551 	5,
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
562     };
563     return &rc2_40_cbc;
564 #else
565     return NULL;
566 #endif
567 }
568 
569 
570 /**
571  * The RC2-64 cipher type - common crypto
572  *
573  * @return the RC2-64 EVP_CIPHER pointer.
574  *
575  * @ingroup hcrypto_evp
576  */
577 
578 
579 const EVP_CIPHER *
EVP_cc_rc2_64_cbc(void)580 EVP_cc_rc2_64_cbc(void)
581 {
582 #ifdef COMMONCRYPTO_SUPPORTS_RC2
583     static const EVP_CIPHER rc2_64_cbc = {
584 	0,
585 	kCCBlockSizeRC2,
586 	8,
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
597     };
598     return &rc2_64_cbc;
599 #else
600     return NULL;
601 #endif
602 }
603 
604 /**
605  * The CommonCrypto md2 provider
606  *
607  * @ingroup hcrypto_evp
608  */
609 
610 const EVP_MD *
EVP_cc_md2(void)611 EVP_cc_md2(void)
612 {
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
622     };
623     return &md2;
624 #else
625     return NULL;
626 #endif
627 }
628 
629 /**
630  * The CommonCrypto md4 provider
631  *
632  * @ingroup hcrypto_evp
633  */
634 
635 const EVP_MD *
EVP_cc_md4(void)636 EVP_cc_md4(void)
637 {
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
647     };
648     return &md4;
649 #else
650     return NULL;
651 #endif
652 }
653 
654 /**
655  * The CommonCrypto md5 provider
656  *
657  * @ingroup hcrypto_evp
658  */
659 
660 const EVP_MD *
EVP_cc_md5(void)661 EVP_cc_md5(void)
662 {
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
672     };
673     return &md5;
674 #else
675     return NULL;
676 #endif
677 }
678 
679 /**
680  * The CommonCrypto sha1 provider
681  *
682  * @ingroup hcrypto_evp
683  */
684 
685 const EVP_MD *
EVP_cc_sha1(void)686 EVP_cc_sha1(void)
687 {
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
697     };
698     return &sha1;
699 #else
700     return NULL;
701 #endif
702 }
703 
704 /**
705  * The CommonCrypto sha256 provider
706  *
707  * @ingroup hcrypto_evp
708  */
709 
710 const EVP_MD *
EVP_cc_sha256(void)711 EVP_cc_sha256(void)
712 {
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
722     };
723     return &sha256;
724 #else
725     return NULL;
726 #endif
727 }
728 
729 /**
730  * The Camellia-128 cipher type - CommonCrypto
731  *
732  * @return the Camellia-128 EVP_CIPHER pointer.
733  *
734  * @ingroup hcrypto_evp
735  */
736 
737 const EVP_CIPHER *
EVP_cc_camellia_128_cbc(void)738 EVP_cc_camellia_128_cbc(void)
739 {
740     return NULL;
741 }
742 
743 /**
744  * The Camellia-198 cipher type - CommonCrypto
745  *
746  * @return the Camellia-198 EVP_CIPHER pointer.
747  *
748  * @ingroup hcrypto_evp
749  */
750 
751 const EVP_CIPHER *
EVP_cc_camellia_192_cbc(void)752 EVP_cc_camellia_192_cbc(void)
753 {
754     return NULL;
755 }
756 
757 /**
758  * The Camellia-256 cipher type - CommonCrypto
759  *
760  * @return the Camellia-256 EVP_CIPHER pointer.
761  *
762  * @ingroup hcrypto_evp
763  */
764 
765 const EVP_CIPHER *
EVP_cc_camellia_256_cbc(void)766 EVP_cc_camellia_256_cbc(void)
767 {
768     return NULL;
769 }
770 
771 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
772 
773 /*
774  *
775  */
776 
777 static int
cc_rc4_init(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int encp)778 cc_rc4_init(EVP_CIPHER_CTX *ctx,
779 	    const unsigned char * key,
780 	    const unsigned char * iv,
781 	    int encp)
782 {
783     struct cc_key *cc = ctx->cipher_data;
784     return init_cc_key(encp, kCCAlgorithmRC4, 0, key, ctx->key_len, iv, &cc->href);
785 }
786 
787 #endif
788 
789 /**
790 
791  * The RC4 cipher type (Apple CommonCrypto provider)
792  *
793  * @return the RC4 EVP_CIPHER pointer.
794  *
795  * @ingroup hcrypto_evp
796  */
797 
798 const EVP_CIPHER *
EVP_cc_rc4(void)799 EVP_cc_rc4(void)
800 {
801 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
802     static const EVP_CIPHER rc4 = {
803 	0,
804 	1,
805 	16,
806 	0,
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
816     };
817     return &rc4;
818 #else
819     return NULL;
820 #endif
821 }
822 
823 
824 /**
825  * The RC4-40 cipher type (Apple CommonCrypto provider)
826  *
827  * @return the RC4 EVP_CIPHER pointer.
828  *
829  * @ingroup hcrypto_evp
830  */
831 
832 const EVP_CIPHER *
EVP_cc_rc4_40(void)833 EVP_cc_rc4_40(void)
834 {
835 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
836     static const EVP_CIPHER rc4_40 = {
837 	0,
838 	1,
839 	5,
840 	0,
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
850     };
851     return &rc4_40;
852 #else
853     return NULL;
854 #endif
855 }
856 
857 #endif /* __APPLE__ */
858 
859