xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/hcrypto/evp-cc.c (revision afab4e300d3a9fb07dd8c80daf53d0feb3345706)
1 /*	$NetBSD: evp-cc.c,v 1.3 2023/06/19 21:41:43 christos 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 #include <krb5/roken.h>
44 
45 #include <assert.h>
46 
47 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
48 #include <CommonCrypto/CommonDigest.h>
49 #endif
50 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
51 #include <CommonCrypto/CommonCryptor.h>
52 #endif
53 
54 #include <evp.h>
55 #include <evp-hcrypto.h>
56 #include <evp-cc.h>
57 
58 /*
59  *
60  */
61 
62 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
63 
64 struct cc_key {
65     CCCryptorRef href;
66 };
67 
68 static int
cc_do_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,unsigned int size)69 cc_do_cipher(EVP_CIPHER_CTX *ctx,
70 	     unsigned char *out,
71 	     const unsigned char *in,
72 	     unsigned int size)
73 {
74     struct cc_key *cc = ctx->cipher_data;
75     CCCryptorStatus ret;
76     size_t moved;
77 
78     memcpy(out, in, size);
79 
80     ret = CCCryptorUpdate(cc->href, in, size, out, size, &moved);
81     if (ret)
82 	return 0;
83 
84     if (moved != size)
85 	return 0;
86 
87     return 1;
88 }
89 
90 static int
cc_cleanup(EVP_CIPHER_CTX * ctx)91 cc_cleanup(EVP_CIPHER_CTX *ctx)
92 {
93     struct cc_key *cc = ctx->cipher_data;
94     if (cc->href)
95 	CCCryptorRelease(cc->href);
96     return 1;
97 }
98 
99 static int
init_cc_key(int encp,unsigned long flags,CCAlgorithm alg,const void * key,size_t keylen,const void * iv,CCCryptorRef * ref)100 init_cc_key(int encp, unsigned long flags,
101 	    CCAlgorithm alg, const void *key, size_t keylen,
102 	    const void *iv, CCCryptorRef *ref)
103 {
104     CCOperation op = encp ? kCCEncrypt : kCCDecrypt;
105     CCMode mode;
106     CCModeOptions options = 0;
107     CCCryptorStatus ret;
108 
109     if (*ref) {
110 	if (key == NULL && iv) {
111 	    CCCryptorReset(*ref, iv);
112 	    return 1;
113 	}
114 	CCCryptorRelease(*ref);
115     }
116 
117     if (key) {
118 	switch (flags & EVP_CIPH_MODE) {
119 	case EVP_CIPH_STREAM_CIPHER:
120 	    mode = kCCModeRC4;
121 	    break;
122 	case EVP_CIPH_CFB8_MODE:
123 	    mode = kCCModeCFB8;
124 	    break;
125 	default:
126 	    mode = kCCModeCBC;
127 	    break;
128 	}
129 
130 	ret = CCCryptorCreateWithMode(op, mode, alg, ccNoPadding,
131 				      iv, key, keylen, NULL, 0, 0,
132 				      options, ref);
133 	if (ret)
134 	    return 0;
135     }
136 
137     return 1;
138 }
139 
140 static int
cc_des_ede3_cbc_init(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int encp)141 cc_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
142 		     const unsigned char * key,
143 		     const unsigned char * iv,
144 		     int encp)
145 {
146     struct cc_key *cc = ctx->cipher_data;
147     return init_cc_key(encp, ctx->cipher->flags, kCCAlgorithm3DES,
148 		       key, kCCKeySize3DES, iv, &cc->href);
149 }
150 
151 #endif /* HAVE_COMMONCRYPTO_COMMONCRYPTOR_H */
152 
153 /**
154  * The triple DES cipher type (Apple CommonCrypto provider)
155  *
156  * @return the DES-EDE3-CBC EVP_CIPHER pointer.
157  *
158  * @ingroup hcrypto_evp
159  */
160 
161 const EVP_CIPHER *
EVP_cc_des_ede3_cbc(void)162 EVP_cc_des_ede3_cbc(void)
163 {
164 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
165     static const EVP_CIPHER des_ede3_cbc = {
166 	0,
167 	8,
168 	24,
169 	8,
170 	EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
171 	cc_des_ede3_cbc_init,
172 	cc_do_cipher,
173 	cc_cleanup,
174 	sizeof(struct cc_key),
175 	NULL,
176 	NULL,
177 	NULL,
178 	NULL
179     };
180     return &des_ede3_cbc;
181 #elif HCRYPTO_FALLBACK
182     return EVP_hcrypto_des_ede3_cbc();
183 #else
184     return NULL;
185 #endif
186 }
187 
188 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
189 /*
190  *
191  */
192 
193 static int
cc_des_cbc_init(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int encp)194 cc_des_cbc_init(EVP_CIPHER_CTX *ctx,
195 		const unsigned char * key,
196 		const unsigned char * iv,
197 		int encp)
198 {
199     struct cc_key *cc = ctx->cipher_data;
200     return init_cc_key(encp, ctx->cipher->flags, kCCAlgorithmDES,
201 		       key, kCCBlockSizeDES, iv, &cc->href);
202 }
203 #endif
204 
205 /**
206  * The DES cipher type (Apple CommonCrypto provider)
207  *
208  * @return the DES-CBC EVP_CIPHER pointer.
209  *
210  * @ingroup hcrypto_evp
211  */
212 
213 const EVP_CIPHER *
EVP_cc_des_cbc(void)214 EVP_cc_des_cbc(void)
215 {
216 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
217     static const EVP_CIPHER des_ede3_cbc = {
218 	0,
219 	kCCBlockSizeDES,
220 	kCCBlockSizeDES,
221 	kCCBlockSizeDES,
222 	EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
223 	cc_des_cbc_init,
224 	cc_do_cipher,
225 	cc_cleanup,
226 	sizeof(struct cc_key),
227 	NULL,
228 	NULL,
229 	NULL,
230 	NULL
231     };
232     return &des_ede3_cbc;
233 #elif HCRYPTO_FALLBACK
234     return EVP_hcrypto_des_cbc();
235 #else
236     return NULL;
237 #endif
238 }
239 
240 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
241 /*
242  *
243  */
244 
245 static int
cc_aes_cbc_init(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int encp)246 cc_aes_cbc_init(EVP_CIPHER_CTX *ctx,
247 		const unsigned char * key,
248 		const unsigned char * iv,
249 		int encp)
250 {
251     struct cc_key *cc = ctx->cipher_data;
252     return init_cc_key(encp, ctx->cipher->flags, kCCAlgorithmAES128,
253 		       key, ctx->cipher->key_len, iv, &cc->href);
254 }
255 #endif
256 
257 /**
258  * The AES-128 cipher type (Apple CommonCrypto provider)
259  *
260  * @return the AES-128-CBC EVP_CIPHER pointer.
261  *
262  * @ingroup hcrypto_evp
263  */
264 
265 const EVP_CIPHER *
EVP_cc_aes_128_cbc(void)266 EVP_cc_aes_128_cbc(void)
267 {
268 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
269     static const EVP_CIPHER c = {
270 	0,
271 	kCCBlockSizeAES128,
272 	kCCKeySizeAES128,
273 	kCCBlockSizeAES128,
274 	EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
275 	cc_aes_cbc_init,
276 	cc_do_cipher,
277 	cc_cleanup,
278 	sizeof(struct cc_key),
279 	NULL,
280 	NULL,
281 	NULL,
282 	NULL
283     };
284     return &c;
285 #elif HCRYPTO_FALLBACK
286     return EVP_hcrypto_aes_128_cbc();
287 #else
288     return NULL;
289 #endif
290 }
291 
292 /**
293  * The AES-192 cipher type (Apple CommonCrypto provider)
294  *
295  * @return the AES-192-CBC EVP_CIPHER pointer.
296  *
297  * @ingroup hcrypto_evp
298  */
299 
300 const EVP_CIPHER *
EVP_cc_aes_192_cbc(void)301 EVP_cc_aes_192_cbc(void)
302 {
303 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
304     static const EVP_CIPHER c = {
305 	0,
306 	kCCBlockSizeAES128,
307 	kCCKeySizeAES192,
308 	kCCBlockSizeAES128,
309 	EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
310 	cc_aes_cbc_init,
311 	cc_do_cipher,
312 	cc_cleanup,
313 	sizeof(struct cc_key),
314 	NULL,
315 	NULL,
316 	NULL,
317 	NULL
318     };
319     return &c;
320 #elif HCRYPTO_FALLBACK
321     return EVP_hcrypto_aes_192_cbc();
322 #else
323     return NULL;
324 #endif
325 }
326 
327 /**
328  * The AES-256 cipher type (Apple CommonCrypto provider)
329  *
330  * @return the AES-256-CBC EVP_CIPHER pointer.
331  *
332  * @ingroup hcrypto_evp
333  */
334 
335 const EVP_CIPHER *
EVP_cc_aes_256_cbc(void)336 EVP_cc_aes_256_cbc(void)
337 {
338 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
339     static const EVP_CIPHER c = {
340 	0,
341 	kCCBlockSizeAES128,
342 	kCCKeySizeAES256,
343 	kCCBlockSizeAES128,
344 	EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
345 	cc_aes_cbc_init,
346 	cc_do_cipher,
347 	cc_cleanup,
348 	sizeof(struct cc_key),
349 	NULL,
350 	NULL,
351 	NULL,
352 	NULL
353     };
354     return &c;
355 #elif HCRYPTO_FALLBACK
356     return EVP_hcrypto_aes_256_cbc();
357 #else
358     return NULL;
359 #endif
360 }
361 
362 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
363 /*
364  *
365  */
366 
367 static int
cc_aes_cfb8_init(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int encp)368 cc_aes_cfb8_init(EVP_CIPHER_CTX *ctx,
369 		const unsigned char * key,
370 		const unsigned char * iv,
371 		int encp)
372 {
373     struct cc_key *cc = ctx->cipher_data;
374     return init_cc_key(encp, ctx->cipher->flags, kCCAlgorithmAES128,
375 		       key, ctx->cipher->key_len, NULL, &cc->href);
376 }
377 #endif
378 
379 /**
380  * The AES-128 CFB8 cipher type (Apple CommonCrypto provider)
381  *
382  * @return the AES-128-CFB8 EVP_CIPHER pointer.
383  *
384  * @ingroup hcrypto_evp
385  */
386 
387 const EVP_CIPHER *
EVP_cc_aes_128_cfb8(void)388 EVP_cc_aes_128_cfb8(void)
389 {
390 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
391     static const EVP_CIPHER c = {
392 	0,
393 	1,
394 	kCCKeySizeAES128,
395 	kCCBlockSizeAES128,
396 	EVP_CIPH_CFB8_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
397 	cc_aes_cfb8_init,
398 	cc_do_cipher,
399 	cc_cleanup,
400 	sizeof(struct cc_key),
401 	NULL,
402 	NULL,
403 	NULL,
404 	NULL
405     };
406     return &c;
407 #elif HCRYPTO_FALLBACK
408     return EVP_hcrypto_aes_128_cfb8();
409 #else
410     return NULL;
411 #endif
412 }
413 
414 /**
415  * The AES-192 CFB8 cipher type (Apple CommonCrypto provider)
416  *
417  * @return the AES-192-CFB8 EVP_CIPHER pointer.
418  *
419  * @ingroup hcrypto_evp
420  */
421 
422 const EVP_CIPHER *
EVP_cc_aes_192_cfb8(void)423 EVP_cc_aes_192_cfb8(void)
424 {
425 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
426     static const EVP_CIPHER c = {
427 	0,
428 	1,
429 	kCCKeySizeAES192,
430 	kCCBlockSizeAES128,
431 	EVP_CIPH_CFB8_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
432 	cc_aes_cfb8_init,
433 	cc_do_cipher,
434 	cc_cleanup,
435 	sizeof(struct cc_key),
436 	NULL,
437 	NULL,
438 	NULL,
439 	NULL
440     };
441     return &c;
442 #elif HCRYPTO_FALLBACK
443     return EVP_hcrypto_aes_192_cfb8();
444 #else
445     return NULL;
446 #endif
447 }
448 
449 /**
450  * The AES-256 CFB8 cipher type (Apple CommonCrypto provider)
451  *
452  * @return the AES-256-CFB8 EVP_CIPHER pointer.
453  *
454  * @ingroup hcrypto_evp
455  */
456 
457 const EVP_CIPHER *
EVP_cc_aes_256_cfb8(void)458 EVP_cc_aes_256_cfb8(void)
459 {
460 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
461     static const EVP_CIPHER c = {
462 	0,
463 	kCCBlockSizeAES128,
464 	kCCKeySizeAES256,
465 	kCCBlockSizeAES128,
466 	EVP_CIPH_CFB8_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
467 	cc_aes_cfb8_init,
468 	cc_do_cipher,
469 	cc_cleanup,
470 	sizeof(struct cc_key),
471 	NULL,
472 	NULL,
473 	NULL,
474 	NULL
475     };
476     return &c;
477 #elif HCRYPTO_FALLBACK
478     return EVP_hcrypto_aes_256_cfb8();
479 #else
480     return NULL;
481 #endif
482 }
483 
484 /*
485  *
486  */
487 
488 #ifdef COMMONCRYPTO_SUPPORTS_RC2
489 static int
cc_rc2_cbc_init(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int encp)490 cc_rc2_cbc_init(EVP_CIPHER_CTX *ctx,
491 		const unsigned char * key,
492 		const unsigned char * iv,
493 		int encp)
494 {
495     struct cc_key *cc = ctx->cipher_data;
496     return init_cc_key(encp, ctx->cipher->flags, kCCAlgorithmRC2,
497 		       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 #elif HCRYPTO_FALLBACK
531     return EVP_hcrypto_rc2_cbc();
532 #else
533     return NULL;
534 #endif
535 }
536 
537 /**
538  * The RC2-40 cipher type - common crypto
539  *
540  * @return the RC2-40 EVP_CIPHER pointer.
541  *
542  * @ingroup hcrypto_evp
543  */
544 
545 
546 const EVP_CIPHER *
EVP_cc_rc2_40_cbc(void)547 EVP_cc_rc2_40_cbc(void)
548 {
549 #ifdef COMMONCRYPTO_SUPPORTS_RC2
550     static const EVP_CIPHER rc2_40_cbc = {
551 	0,
552 	kCCBlockSizeRC2,
553 	5,
554 	kCCBlockSizeRC2,
555 	EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
556 	cc_rc2_cbc_init,
557 	cc_do_cipher,
558 	cc_cleanup,
559 	sizeof(struct cc_key),
560 	NULL,
561 	NULL,
562 	NULL,
563 	NULL
564     };
565     return &rc2_40_cbc;
566 #elif HCRYPTO_FALLBACK
567     return EVP_hcrypto_rc2_40_cbc();
568 #else
569     return NULL;
570 #endif
571 }
572 
573 
574 /**
575  * The RC2-64 cipher type - common crypto
576  *
577  * @return the RC2-64 EVP_CIPHER pointer.
578  *
579  * @ingroup hcrypto_evp
580  */
581 
582 
583 const EVP_CIPHER *
EVP_cc_rc2_64_cbc(void)584 EVP_cc_rc2_64_cbc(void)
585 {
586 #ifdef COMMONCRYPTO_SUPPORTS_RC2
587     static const EVP_CIPHER rc2_64_cbc = {
588 	0,
589 	kCCBlockSizeRC2,
590 	8,
591 	kCCBlockSizeRC2,
592 	EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
593 	cc_rc2_cbc_init,
594 	cc_do_cipher,
595 	cc_cleanup,
596 	sizeof(struct cc_key),
597 	NULL,
598 	NULL,
599 	NULL,
600 	NULL
601     };
602     return &rc2_64_cbc;
603 #elif HCRYPTO_FALLBACK
604     return EVP_hcrypto_rc2_64_cbc();
605 #else
606     return NULL;
607 #endif
608 }
609 
610 
611 /**
612  * The CommonCrypto md4 provider
613  *
614  * @ingroup hcrypto_evp
615  */
616 
617 const EVP_MD *
EVP_cc_md4(void)618 EVP_cc_md4(void)
619 {
620 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
621     static const struct hc_evp_md md4 = {
622 	CC_MD4_DIGEST_LENGTH,
623 	CC_MD4_BLOCK_BYTES,
624 	sizeof(CC_MD4_CTX),
625 	(hc_evp_md_init)CC_MD4_Init,
626 	(hc_evp_md_update)CC_MD4_Update,
627 	(hc_evp_md_final)CC_MD4_Final,
628 	(hc_evp_md_cleanup)NULL
629     };
630     return &md4;
631 #elif HCRYPTO_FALLBACK
632     return EVP_hcrypto_md4();
633 #else
634     return NULL;
635 #endif
636 }
637 
638 /**
639  * The CommonCrypto md5 provider
640  *
641  * @ingroup hcrypto_evp
642  */
643 
644 const EVP_MD *
EVP_cc_md5(void)645 EVP_cc_md5(void)
646 {
647 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
648     static const struct hc_evp_md md5 = {
649 	CC_MD5_DIGEST_LENGTH,
650 	CC_MD5_BLOCK_BYTES,
651 	sizeof(CC_MD5_CTX),
652 	(hc_evp_md_init)CC_MD5_Init,
653 	(hc_evp_md_update)CC_MD5_Update,
654 	(hc_evp_md_final)CC_MD5_Final,
655 	(hc_evp_md_cleanup)NULL
656     };
657     return &md5;
658 #elif HCRYPTO_FALLBACK
659     return EVP_hcrypto_md5();
660 #else
661     return NULL;
662 #endif
663 }
664 
665 /**
666  * The CommonCrypto sha1 provider
667  *
668  * @ingroup hcrypto_evp
669  */
670 
671 const EVP_MD *
EVP_cc_sha1(void)672 EVP_cc_sha1(void)
673 {
674 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
675     static const struct hc_evp_md sha1 = {
676 	CC_SHA1_DIGEST_LENGTH,
677 	CC_SHA1_BLOCK_BYTES,
678 	sizeof(CC_SHA1_CTX),
679 	(hc_evp_md_init)CC_SHA1_Init,
680 	(hc_evp_md_update)CC_SHA1_Update,
681 	(hc_evp_md_final)CC_SHA1_Final,
682 	(hc_evp_md_cleanup)NULL
683     };
684     return &sha1;
685 #elif HCRYPTO_FALLBACK
686     return EVP_hcrypto_sha1();
687 #else
688     return NULL;
689 #endif
690 }
691 
692 /**
693  * The CommonCrypto sha256 provider
694  *
695  * @ingroup hcrypto_evp
696  */
697 
698 const EVP_MD *
EVP_cc_sha256(void)699 EVP_cc_sha256(void)
700 {
701 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
702     static const struct hc_evp_md sha256 = {
703 	CC_SHA256_DIGEST_LENGTH,
704 	CC_SHA256_BLOCK_BYTES,
705 	sizeof(CC_SHA256_CTX),
706 	(hc_evp_md_init)CC_SHA256_Init,
707 	(hc_evp_md_update)CC_SHA256_Update,
708 	(hc_evp_md_final)CC_SHA256_Final,
709 	(hc_evp_md_cleanup)NULL
710     };
711     return &sha256;
712 #elif HCRYPTO_FALLBACK
713     return EVP_hcrypto_sha256();
714 #else
715     return NULL;
716 #endif
717 }
718 
719 /**
720  * The CommonCrypto sha384 provider
721  *
722  * @ingroup hcrypto_evp
723  */
724 
725 const EVP_MD *
EVP_cc_sha384(void)726 EVP_cc_sha384(void)
727 {
728 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
729     static const struct hc_evp_md sha384 = {
730 	CC_SHA384_DIGEST_LENGTH,
731 	CC_SHA384_BLOCK_BYTES,
732 	sizeof(CC_SHA512_CTX),
733 	(hc_evp_md_init)CC_SHA384_Init,
734 	(hc_evp_md_update)CC_SHA384_Update,
735 	(hc_evp_md_final)CC_SHA384_Final,
736 	(hc_evp_md_cleanup)NULL
737     };
738     return &sha384;
739 #elif HCRYPTO_FALLBACK
740     return EVP_hcrypto_sha384();
741 #else
742     return NULL;
743 #endif
744 }
745 
746 /**
747  * The CommonCrypto sha512 provider
748  *
749  * @ingroup hcrypto_evp
750  */
751 
752 const EVP_MD *
EVP_cc_sha512(void)753 EVP_cc_sha512(void)
754 {
755 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
756     static const struct hc_evp_md sha512 = {
757 	CC_SHA512_DIGEST_LENGTH,
758 	CC_SHA512_BLOCK_BYTES,
759 	sizeof(CC_SHA512_CTX),
760 	(hc_evp_md_init)CC_SHA512_Init,
761 	(hc_evp_md_update)CC_SHA512_Update,
762 	(hc_evp_md_final)CC_SHA512_Final,
763 	(hc_evp_md_cleanup)NULL
764     };
765     return &sha512;
766 #elif HCRYPTO_FALLBACK
767     return EVP_hcrypto_sha512();
768 #else
769     return NULL;
770 #endif
771 }
772 
773 /**
774  * The Camellia-128 cipher type - CommonCrypto
775  *
776  * @return the Camellia-128 EVP_CIPHER pointer.
777  *
778  * @ingroup hcrypto_evp
779  */
780 
781 const EVP_CIPHER *
EVP_cc_camellia_128_cbc(void)782 EVP_cc_camellia_128_cbc(void)
783 {
784 #if HCRYPTO_FALLBACK
785     return EVP_hcrypto_camellia_128_cbc();
786 #else
787     return NULL;
788 #endif
789 }
790 
791 /**
792  * The Camellia-198 cipher type - CommonCrypto
793  *
794  * @return the Camellia-198 EVP_CIPHER pointer.
795  *
796  * @ingroup hcrypto_evp
797  */
798 
799 const EVP_CIPHER *
EVP_cc_camellia_192_cbc(void)800 EVP_cc_camellia_192_cbc(void)
801 {
802 #if HCRYPTO_FALLBACK
803     return EVP_hcrypto_camellia_192_cbc();
804 #else
805     return NULL;
806 #endif
807 }
808 
809 /**
810  * The Camellia-256 cipher type - CommonCrypto
811  *
812  * @return the Camellia-256 EVP_CIPHER pointer.
813  *
814  * @ingroup hcrypto_evp
815  */
816 
817 const EVP_CIPHER *
EVP_cc_camellia_256_cbc(void)818 EVP_cc_camellia_256_cbc(void)
819 {
820 #if HCRYPTO_FALLBACK
821     return EVP_hcrypto_camellia_256_cbc();
822 #else
823     return NULL;
824 #endif
825 }
826 
827 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
828 
829 /*
830  *
831  */
832 
833 static int
cc_rc4_init(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int encp)834 cc_rc4_init(EVP_CIPHER_CTX *ctx,
835 	    const unsigned char * key,
836 	    const unsigned char * iv,
837 	    int encp)
838 {
839     struct cc_key *cc = ctx->cipher_data;
840     return init_cc_key(encp, ctx->cipher->flags, kCCAlgorithmRC4,
841 		       key, ctx->key_len, iv, &cc->href);
842 }
843 
844 #endif
845 
846 /**
847 
848  * The RC4 cipher type (Apple CommonCrypto provider)
849  *
850  * @return the RC4 EVP_CIPHER pointer.
851  *
852  * @ingroup hcrypto_evp
853  */
854 
855 const EVP_CIPHER *
EVP_cc_rc4(void)856 EVP_cc_rc4(void)
857 {
858 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
859     static const EVP_CIPHER rc4 = {
860 	0,
861 	1,
862 	16,
863 	0,
864 	EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
865 	cc_rc4_init,
866 	cc_do_cipher,
867 	cc_cleanup,
868 	sizeof(struct cc_key),
869 	NULL,
870 	NULL,
871 	NULL,
872 	NULL
873     };
874     return &rc4;
875 #elif HCRYPTO_FALLBACK
876     return EVP_hcrypto_rc4();
877 #else
878     return NULL;
879 #endif
880 }
881 
882 
883 /**
884  * The RC4-40 cipher type (Apple CommonCrypto provider)
885  *
886  * @return the RC4 EVP_CIPHER pointer.
887  *
888  * @ingroup hcrypto_evp
889  */
890 
891 const EVP_CIPHER *
EVP_cc_rc4_40(void)892 EVP_cc_rc4_40(void)
893 {
894 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
895     static const EVP_CIPHER rc4_40 = {
896 	0,
897 	1,
898 	5,
899 	0,
900 	EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
901 	cc_rc4_init,
902 	cc_do_cipher,
903 	cc_cleanup,
904 	sizeof(struct cc_key),
905 	NULL,
906 	NULL,
907 	NULL,
908 	NULL
909     };
910     return &rc4_40;
911 #elif HCRYPTO_FALLBACK
912     return EVP_hcrypto_rc4_40();
913 #else
914     return NULL;
915 #endif
916 }
917 
918 #endif /* __APPLE__ */
919 
920