1/*
2 * Copyright (c) 2011 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24/*
25 * Copyright (c) 2008 Kungliga Tekniska Högskolan
26 * (Royal Institute of Technology, Stockholm, Sweden).
27 * All rights reserved.
28 *
29 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
34 *
35 * 1. Redistributions of source code must retain the above copyright
36 *    notice, this list of conditions and the following disclaimer.
37 *
38 * 2. Redistributions in binary form must reproduce the above copyright
39 *    notice, this list of conditions and the following disclaimer in the
40 *    documentation and/or other materials provided with the distribution.
41 *
42 * 3. Neither the name of the Institute nor the names of its contributors
43 *    may be used to endorse or promote products derived from this software
44 *    without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 */
58
59/* CommonCrypto shim provider */
60
61#ifdef __APPLE__
62
63#include "ossl-config.h"
64
65#include <sys/types.h>
66#include <stdio.h>
67#include <stdlib.h>
68#include <string.h>
69#include <assert.h>
70
71#ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
72#include <CommonCrypto/CommonDigest.h>
73#endif
74#ifdef HAVE_COMMONCRYPTO_COMMONDIGESTSPI_H
75#include <CommonCrypto/CommonDigestSPI.h>
76#endif
77#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
78#include <CommonCrypto/CommonCryptor.h>
79#endif
80
81#include "ossl-objects.h"
82#include "ossl-evp.h"
83#include "ossl-evp-cc.h"
84#include "ossl-engine.h"
85
86/*
87 *
88 */
89
90#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
91
92struct cc_key {
93	CCCryptorRef href;
94};
95
96static int
97cc_do_cipher(EVP_CIPHER_CTX *ctx,
98    unsigned char *out,
99    const unsigned char *in,
100    unsigned int size)
101{
102	struct cc_key *cc = ctx->cipher_data;
103	CCCryptorStatus ret;
104	size_t moved;
105
106	memcpy(out, in, size);
107
108	ret = CCCryptorUpdate(cc->href, in, size, out, size, &moved);
109	if (ret) {
110		return (0);
111	}
112
113	if (moved != size) {
114		return (0);
115	}
116
117	return (1);
118}
119
120
121static int
122init_cc_key(int encp, CCAlgorithm alg, CCOptions opts, const void *key,
123    size_t keylen, const void *iv, CCCryptorRef *ref)
124{
125	CCOperation op = encp ? kCCEncrypt : kCCDecrypt;
126	CCCryptorStatus ret;
127
128	if (*ref) {
129		if ((key == NULL) && iv) {
130			CCCryptorReset(*ref, iv);
131			return (1);
132		}
133		CCCryptorRelease(*ref);
134	}
135
136	ret = CCCryptorCreate(op, alg, opts, key, keylen, iv, ref);
137	if (ret) {
138		return (0);
139	}
140	return (1);
141}
142
143
144#ifdef USE_COMMONCRYPTO_CBC_MODE
145static int
146cc_do_cbc_cipher(EVP_CIPHER_CTX *ctx,
147    unsigned char *out,
148    const unsigned char *in,
149    unsigned int size)
150{
151	return (cc_do_cipher(ctx, out, in, size));
152}
153
154
155#else
156
157/*
158 * We implement our own CBC mode so OpenSSH can get at the IV state.
159 */
160static int
161cc_do_cbc_cipher(EVP_CIPHER_CTX *ctx,
162    unsigned char *out,
163    const unsigned char *in,
164    unsigned int size)
165{
166	unsigned int n;
167	unsigned int len = size;
168	unsigned char *iv = ctx->iv;
169	unsigned int block_size = (unsigned int)ctx->cipher->block_size;
170	size_t iv_len = (size_t)ctx->cipher->iv_len;
171	unsigned char tmp[EVP_MAX_BLOCK_LENGTH];
172
173	if (ctx->encrypt) {
174		/* Encrypt case */
175		while (len >= block_size) {
176			for (n = 0; n < block_size; ++n) {
177				out[n] = in[n] ^ iv[n];
178			}
179			if (cc_do_cipher(ctx, out, out, block_size) == 0) {
180				return (0);
181			}
182			iv = out;
183			len -= block_size;
184			in += block_size;
185			out += block_size;
186		}
187		if (len) {
188			for (n = 0; n < len; ++n) {
189				out[n] = in[n] ^ iv[n];
190			}
191			for (n = len; n < block_size; ++n) {
192				out[n] = iv[n];
193			}
194			if (cc_do_cipher(ctx, out, out, block_size) == 0) {
195				return (0);
196			}
197			iv = out;
198		}
199		memcpy(ctx->iv, iv, iv_len);
200	} else if (in != out) {
201		/* Decrypt case, in/out buffers are different */
202		while (len >= block_size) {
203			if (cc_do_cipher(ctx, out, in, block_size) == 0) {
204				return (0);
205			}
206			for (n = 0; n < block_size; ++n) {
207				out[n] ^= iv[n];
208			}
209			iv = (unsigned char *)in;
210			len -= block_size;
211			in += block_size;
212			out += block_size;
213		}
214		if (len) {
215			if (cc_do_cipher(ctx, tmp, in, block_size) == 0) {
216				return (0);
217			}
218			for (n = 0; n < len; ++n) {
219				out[n] = tmp[n] ^ iv[n];
220			}
221			iv = (unsigned char *)in;
222		}
223		memcpy(ctx->iv, iv, iv_len);
224	} else {
225		/* Decrypt case, in/out buffers are same */
226		while (len >= block_size) {
227			memcpy(tmp, in, block_size);
228			if (cc_do_cipher(ctx, out, tmp, block_size) == 0) {
229				return (0);
230			}
231			for (n = 0; n < block_size; ++n) {
232				out[n] ^= iv[n];
233			}
234			memcpy(iv, tmp, iv_len);
235			len -= block_size;
236			in += block_size;
237			out += block_size;
238		}
239		if (len) {
240			memcpy(tmp, in, block_size);
241			if (cc_do_cipher(ctx, out, tmp, block_size) == 0) {
242				return (0);
243			}
244			for (n = 0; n < len; ++n) {
245				out[n] ^= iv[n];
246			}
247			for (n = len; n < block_size; ++n) {
248				out[n] = tmp[n];
249			}
250			memcpy(iv, tmp, iv_len);
251		}
252		memcpy(ctx->iv, iv, iv_len);
253	}
254
255	return (1);
256}
257
258
259#endif /* ! USE_COMMONCRYPTO_CBC_MODE */
260
261static int
262cc_do_cfb8_cipher(EVP_CIPHER_CTX *ctx,
263    unsigned char *out,
264    const unsigned char *in,
265    unsigned int size)
266{
267	struct cc_key *cc = ctx->cipher_data;
268	CCCryptorStatus ret;
269	size_t moved;
270	unsigned int i;
271
272	for (i = 0; i < size; i++) {
273		unsigned char oiv[EVP_MAX_IV_LENGTH + 1];
274
275		assert(ctx->cipher->iv_len + 1 <= sizeof(oiv));
276		memcpy(oiv, ctx->iv, ctx->cipher->iv_len);
277
278		ret = CCCryptorUpdate(cc->href, ctx->iv, ctx->cipher->iv_len,
279			ctx->iv, ctx->cipher->iv_len, &moved);
280		if (ret) {
281			return (0);
282		}
283
284		if (moved != ctx->cipher->iv_len) {
285			return (0);
286		}
287
288		if (!ctx->encrypt) {
289			oiv[ctx->cipher->iv_len] = in[i];
290		}
291		out[i] = in[i] ^ ctx->iv[0];
292		if (ctx->encrypt) {
293			oiv[ctx->cipher->iv_len] = out[i];
294		}
295
296		memcpy(ctx->iv, &oiv[1], ctx->cipher->iv_len);
297	}
298
299	return (1);
300}
301
302
303static int
304cc_cleanup(EVP_CIPHER_CTX *ctx)
305{
306	struct cc_key *cc = ctx->cipher_data;
307
308	if (cc->href) {
309		CCCryptorRelease(cc->href);
310	}
311	return (1);
312}
313
314
315static int
316cc_des_ede3_ecb_init(EVP_CIPHER_CTX *ctx,
317    const unsigned char *key,
318    const unsigned char *iv,
319    int encp)
320{
321	struct cc_key *cc = ctx->cipher_data;
322
323	return (init_cc_key(encp, kCCAlgorithm3DES, kCCOptionECBMode, key, kCCKeySize3DES, iv, &cc->href));
324}
325
326
327#ifdef USE_COMMONCRYPTO_CBC_MODE
328static int
329cc_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
330    const unsigned char *key,
331    const unsigned char *iv,
332    int encp)
333{
334	struct cc_key *cc = ctx->cipher_data;
335
336	return (init_cc_key(encp, kCCAlgorithm3DES, 0, key, kCCKeySize3DES, iv, &cc->href));
337}
338
339
340#else
341
342static int
343cc_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
344    const unsigned char *key,
345    const unsigned char *iv,
346    int encp)
347{
348	return (cc_des_ede3_ecb_init(ctx, key, NULL, encp));
349}
350
351
352#endif  /* ! USE_COMMONCRYPTO_CBC_MODE */
353
354#endif  /* HAVE_COMMONCRYPTO_COMMONCRYPTOR_H */
355
356/**
357 * The tripple DES cipher type (Apple CommonCrypto provider)
358 *
359 * @return the DES-EDE3-ECB EVP_CIPHER pointer.
360 *
361 * @ingroup hcrypto_evp
362 */
363const EVP_CIPHER *
364EVP_cc_des_ede3_ecb(void)
365{
366#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
367	static const EVP_CIPHER c =
368	{
369		.nid			= NID_des_ede3_ecb,
370		.block_size		= kCCBlockSize3DES,
371		.key_len		= kCCKeySize3DES,
372		.iv_len			= kCCBlockSize3DES,
373		.flags			= EVP_CIPH_ECB_MODE | EVP_CIPH_ALWAYS_CALL_INIT,
374		.init			= cc_des_ede3_ecb_init,
375		.do_cipher		= cc_do_cipher,
376		.cleanup		= cc_cleanup,
377		.ctx_size		= sizeof(struct cc_key),
378		.set_asn1_parameters	= NULL,
379		.get_asn1_parameters	= NULL,
380		.ctrl			= NULL,
381		.app_data		= NULL
382	};
383	return (&c);
384
385#else
386	return (NULL);
387#endif
388}
389
390
391/**
392 * The tripple DES cipher type (Apple CommonCrypto provider)
393 *
394 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
395 *
396 * @ingroup hcrypto_evp
397 */
398const EVP_CIPHER *
399EVP_cc_des_ede3_cbc(void)
400{
401#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
402	static const EVP_CIPHER c =
403	{
404		.nid			= NID_des_ede3_cbc,
405		.block_size		= kCCBlockSize3DES,
406		.key_len		= kCCKeySize3DES,
407		.iv_len			= kCCBlockSize3DES,
408		.flags			= EVP_CIPH_CBC_MODE | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV,
409		.init			= cc_des_ede3_cbc_init,
410		.do_cipher		= cc_do_cbc_cipher,
411		.cleanup		= cc_cleanup,
412		.ctx_size		= sizeof(struct cc_key),
413		.set_asn1_parameters	= NULL,
414		.get_asn1_parameters	= NULL,
415		.ctrl			= NULL,
416		.app_data		= NULL
417	};
418	return (&c);
419
420#else
421	return (NULL);
422#endif
423}
424
425
426#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
427
428/*
429 *
430 */
431static int
432cc_des_ecb_init(EVP_CIPHER_CTX *ctx,
433    const unsigned char *key,
434    const unsigned char *iv,
435    int encp)
436{
437	struct cc_key *cc = ctx->cipher_data;
438
439	return (init_cc_key(encp, kCCAlgorithmDES, kCCOptionECBMode, key, kCCBlockSizeDES, iv, &cc->href));
440}
441
442
443#ifdef USE_COMMONCRYPTO_CBC_MODE
444static int
445cc_des_cbc_init(EVP_CIPHER_CTX *ctx,
446    const unsigned char *key,
447    const unsigned char *iv,
448    int encp)
449{
450	struct cc_key *cc = ctx->cipher_data;
451
452	return (init_cc_key(encp, kCCAlgorithmDES, 0, key, kCCBlockSizeDES, iv, &cc->href));
453}
454
455
456#else
457
458static int
459cc_des_cbc_init(EVP_CIPHER_CTX *ctx,
460    const unsigned char *key,
461    const unsigned char *iv,
462    int encp)
463{
464	return (cc_des_ecb_init(ctx, key, NULL, encp));
465}
466
467
468#endif  /* USE_COMMONCRYPTO_CBC_MODE */
469#endif  /* HAVE_COMMONCRYPTO_COMMONCRYPTOR_H */
470
471/**
472 * The DES cipher type (Apple CommonCrypto provider)
473 *
474 * @return the DES-ECB EVP_CIPHER pointer.
475 *
476 * @ingroup hcrypto_evp
477 */
478const EVP_CIPHER *
479EVP_cc_des_ecb(void)
480{
481#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
482	static const EVP_CIPHER c =
483	{
484		.nid			= NID_des_ecb,
485		.block_size		= kCCBlockSizeDES,
486		.key_len		= kCCKeySizeDES,
487		.iv_len			= kCCBlockSizeDES,
488		.flags			= EVP_CIPH_ECB_MODE | EVP_CIPH_ALWAYS_CALL_INIT,
489		.init			= cc_des_ecb_init,
490		.do_cipher		= cc_do_cipher,
491		.cleanup		= cc_cleanup,
492		.ctx_size		= sizeof(struct cc_key),
493		.set_asn1_parameters	= NULL,
494		.get_asn1_parameters	= NULL,
495		.ctrl			= NULL,
496		.app_data		= NULL
497	};
498	return (&c);
499
500#else
501	return (NULL);
502#endif
503}
504
505
506/**
507 * The DES cipher type (Apple CommonCrypto provider)
508 *
509 * @return the DES-CBC EVP_CIPHER pointer.
510 *
511 * @ingroup hcrypto_evp
512 */
513const EVP_CIPHER *
514EVP_cc_des_cbc(void)
515{
516#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
517	static const EVP_CIPHER c =
518	{
519		.nid			= NID_des_cbc,
520		.block_size		= kCCBlockSizeDES,
521		.key_len		= kCCKeySizeDES,
522		.iv_len			= kCCBlockSizeDES,
523		.flags			= EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT|EVP_CIPH_CUSTOM_IV,
524		.init			= cc_des_cbc_init,
525		.do_cipher		= cc_do_cbc_cipher,
526		.cleanup		= cc_cleanup,
527		.ctx_size		= sizeof(struct cc_key),
528		.set_asn1_parameters	= NULL,
529		.get_asn1_parameters	= NULL,
530		.ctrl			= NULL,
531		.app_data		= NULL
532	};
533	return (&c);
534
535#else
536	return (NULL);
537#endif
538}
539
540
541#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
542static int
543cc_bf_ecb_init(EVP_CIPHER_CTX *ctx,
544    const unsigned char *key,
545    const unsigned char *iv,
546    int encp)
547{
548	struct cc_key *cc = ctx->cipher_data;
549
550	return (init_cc_key(encp, kCCAlgorithmBlowfish, kCCOptionECBMode, key, ctx->cipher->key_len, iv, &cc->href));
551}
552
553
554#ifdef USE_COMMONCRYPTO_CBC_MODE
555static int
556cc_bf_cbc_init(EVP_CIPHER_CTX *ctx,
557    const unsigned char *key,
558    const unsigned char *iv,
559    int encp)
560{
561	struct cc_key *cc = ctx->cipher_data;
562
563	return (init_cc_key(encp, kCCAlgorithmBlowfish, 0, key, ctx->cipher->key_len, iv, &cc->href));
564}
565
566
567#else
568
569static int
570cc_bf_cbc_init(EVP_CIPHER_CTX *ctx,
571    const unsigned char *key,
572    const unsigned char *iv,
573    int encp)
574{
575	return (cc_bf_ecb_init(ctx, key, NULL, encp));
576}
577
578
579#endif  /* ! USE_COMMONCRYPTO_CBC_MODE */
580
581#endif  /* HAVE_COMMONCRYPTO_COMMONCRYPTOR_H */
582
583const EVP_CIPHER *
584EVP_cc_bf_cbc(void)
585{
586#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
587	static const EVP_CIPHER c =
588	{
589		.nid			= NID_bf_cbc,
590		.block_size		= 8 /* kCCBlockSizeBlowfish */,
591		.key_len		= 16,
592		.iv_len			= 8 /* kCCBlockSizeBlowfish */,
593		.flags			= EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_ALWAYS_CALL_INIT |
594					    EVP_CIPH_CUSTOM_IV | EVP_CIPH_CBC_MODE,
595		.init			= cc_bf_cbc_init,
596		.do_cipher		= cc_do_cbc_cipher,
597		.cleanup		= cc_cleanup,
598		.ctx_size		= sizeof(struct cc_key),
599		.set_asn1_parameters	= NULL,
600		.get_asn1_parameters	= NULL,
601		.ctrl			= NULL,
602		.app_data		= NULL
603	};
604	return (&c);
605
606#else
607	return (NULL);
608#endif
609}
610
611
612const EVP_CIPHER *
613EVP_cc_bf_ecb(void)
614{
615#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
616	static const EVP_CIPHER c =
617	{
618		.nid			= NID_bf_ecb,
619		.block_size		= kCCBlockSizeBlowfish,
620		.key_len		= 8,
621		.iv_len			= kCCBlockSizeBlowfish,
622		.flags			= EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_ECB_MODE,
623		.init			= cc_bf_ecb_init,
624		.do_cipher		= cc_do_cipher,
625		.cleanup		= cc_cleanup,
626		.ctx_size		= sizeof(struct cc_key),
627		.set_asn1_parameters	= NULL,
628		.get_asn1_parameters	= NULL,
629		.ctrl			= NULL,
630		.app_data		= NULL
631	};
632	return (&c);
633
634#else
635	return (NULL);
636#endif
637}
638
639
640#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
641static int
642cc_cast5_ecb_init(EVP_CIPHER_CTX *ctx,
643    const unsigned char *key,
644    const unsigned char *iv,
645    int encp)
646{
647	struct cc_key *cc = ctx->cipher_data;
648
649	return (init_cc_key(encp, kCCAlgorithmCAST, kCCOptionECBMode, key, ctx->key_len, iv, &cc->href));
650}
651
652
653#ifdef USE_COMMONCRYPTO_CBC_MODE
654static int
655cc_cast5_cbc_init(EVP_CIPHER_CTX *ctx,
656    const unsigned char *key,
657    const unsigned char *iv,
658    int encp)
659{
660	struct cc_key *cc = ctx->cipher_data;
661
662	return (init_cc_key(encp, kCCAlgorithmCAST, 0, key, ctx->key_len, iv, &cc->href));
663}
664
665
666#else
667
668static int
669cc_cast5_cbc_init(EVP_CIPHER_CTX *ctx,
670    const unsigned char *key,
671    const unsigned char *iv,
672    int encp)
673{
674	return (cc_cast5_ecb_init(ctx, key, NULL, encp));
675}
676
677
678#endif  /* ! USE_COMMONCRYPTO_CBC_MODE */
679
680#endif  /* HAVE_COMMONCRYPTO_COMMONCRYPTOR_H */
681
682const EVP_CIPHER *
683EVP_cc_cast5_ecb(void)
684{
685#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
686	static const EVP_CIPHER c =
687	{
688		.nid			= NID_cast5_ecb,
689		.block_size		= kCCBlockSizeCAST,
690		.key_len		= 16,
691		.iv_len			= kCCBlockSizeCAST,
692		.flags			= EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_ECB_MODE,
693		.init			= cc_cast5_ecb_init,
694		.do_cipher		= cc_do_cipher,
695		.cleanup		= cc_cleanup,
696		.ctx_size		= sizeof(struct cc_key),
697		.set_asn1_parameters	= NULL,
698		.get_asn1_parameters	= NULL,
699		.ctrl			= NULL,
700		.app_data		= NULL
701	};
702	return (&c);
703
704#else
705	return (NULL);
706#endif
707}
708
709
710const EVP_CIPHER *
711EVP_cc_cast5_cbc(void)
712{
713#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
714	static const EVP_CIPHER c =
715	{
716		.nid			= NID_cast5_cbc,
717		.block_size		= kCCBlockSizeCAST,
718		.key_len		= 16,
719		.iv_len			= kCCBlockSizeCAST,
720		.flags			= EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_ALWAYS_CALL_INIT |
721					    EVP_CIPH_CUSTOM_IV | EVP_CIPH_CBC_MODE,
722		.init			= cc_cast5_cbc_init,
723		.do_cipher		= cc_do_cbc_cipher,
724		.cleanup		= cc_cleanup,
725		.ctx_size		= sizeof(struct cc_key),
726		.set_asn1_parameters	= NULL,
727		.get_asn1_parameters	= NULL,
728		.ctrl			= NULL,
729		.app_data		= NULL
730	};
731	return (&c);
732
733#else
734	return (NULL);
735#endif
736}
737
738
739#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
740
741/*
742 *
743 */
744static int
745cc_aes_ecb_init(EVP_CIPHER_CTX *ctx,
746    const unsigned char *key,
747    const unsigned char *iv,
748    int encp)
749{
750	struct cc_key *cc = ctx->cipher_data;
751
752	return (init_cc_key(encp, kCCAlgorithmAES128, kCCOptionECBMode, key, ctx->cipher->key_len, iv, &cc->href));
753}
754
755
756#ifdef USE_COMMONCRYPTO_CBC_MODE
757static int
758cc_aes_cbc_init(EVP_CIPHER_CTX *ctx,
759    const unsigned char *key,
760    const unsigned char *iv,
761    int encp)
762{
763	struct cc_key *cc = ctx->cipher_data;
764
765	return (init_cc_key(encp, kCCAlgorithmAES128, 0, key, ctx->cipher->key_len, iv, &cc->href));
766}
767
768
769#else
770
771static int
772cc_aes_cbc_init(EVP_CIPHER_CTX *ctx,
773    const unsigned char *key,
774    const unsigned char *iv,
775    int encp)
776{
777	return (cc_aes_ecb_init(ctx, key, NULL, encp));
778}
779
780
781#endif  /* ! USE_COMMONCRYPTO_CBC_MODE */
782
783#endif  /* HAVE_COMMONCRYPTO_COMMONCRYPTOR_H */
784
785/**
786 * The AES-128 cipher type (Apple CommonCrypto provider)
787 *
788 * @return the AES-128-ECB EVP_CIPHER pointer.
789 */
790const EVP_CIPHER *
791EVP_cc_aes_128_ecb(void)
792{
793#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
794	static const EVP_CIPHER c =
795	{
796		.nid			= NID_aes_128_ecb,
797		.block_size		= kCCBlockSizeAES128,
798		.key_len		= kCCKeySizeAES128,
799		.iv_len			= kCCBlockSizeAES128,
800		.flags			= EVP_CIPH_ECB_MODE | EVP_CIPH_ALWAYS_CALL_INIT,
801		.init			= cc_aes_ecb_init,
802		.do_cipher		= cc_do_cipher,
803		.cleanup		= cc_cleanup,
804		.ctx_size		= sizeof(struct cc_key),
805		.set_asn1_parameters	= NULL,
806		.get_asn1_parameters	= NULL,
807		.ctrl			= NULL,
808		.app_data		= NULL
809	};
810	return (&c);
811
812#else
813	return (NULL);
814#endif
815}
816
817
818/**
819 * The AES-128 cipher type (Apple CommonCrypto provider)
820 *
821 * @return the AES-128-CBC EVP_CIPHER pointer.
822 */
823const EVP_CIPHER *
824EVP_cc_aes_128_cbc(void)
825{
826#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
827	static const EVP_CIPHER c =
828	{
829		.nid			= NID_aes_128_cbc,
830		.block_size		= kCCBlockSizeAES128,
831		.key_len		= kCCKeySizeAES128,
832		.iv_len			= kCCBlockSizeAES128,
833		.flags			= EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT|EVP_CIPH_CUSTOM_IV,
834		.init			= cc_aes_cbc_init,
835		.do_cipher		= cc_do_cbc_cipher,
836		.cleanup		= cc_cleanup,
837		.ctx_size		= sizeof(struct cc_key),
838		.set_asn1_parameters	= NULL,
839		.get_asn1_parameters	= NULL,
840		.ctrl			= NULL,
841		.app_data		= NULL
842	};
843	return (&c);
844
845#else
846	return (NULL);
847#endif
848}
849
850
851/**
852 * The AES-192 cipher type (Apple CommonCrypto provider)
853 *
854 * @return the AES-192-ECB EVP_CIPHER pointer.
855 */
856const EVP_CIPHER *
857EVP_cc_aes_192_ecb(void)
858{
859#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
860	static const EVP_CIPHER c =
861	{
862		.nid			= NID_aes_192_ecb,
863		.block_size		= kCCBlockSizeAES128,
864		.key_len		= kCCKeySizeAES192,
865		.iv_len			= kCCBlockSizeAES128,
866		.flags			= EVP_CIPH_ECB_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
867		.init			= cc_aes_ecb_init,
868		.do_cipher		= cc_do_cipher,
869		.cleanup		= cc_cleanup,
870		.ctx_size		= sizeof(struct cc_key),
871		.set_asn1_parameters	= NULL,
872		.get_asn1_parameters	= NULL,
873		.ctrl			= NULL,
874		.app_data		= NULL
875	};
876	return (&c);
877
878#else
879	return (NULL);
880#endif
881}
882
883
884/**
885 * The AES-192 cipher type (Apple CommonCrypto provider)
886 *
887 * @return the AES-192-CBC EVP_CIPHER pointer.
888 */
889const EVP_CIPHER *
890EVP_cc_aes_192_cbc(void)
891{
892#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
893	static const EVP_CIPHER c =
894	{
895		.nid			= NID_aes_192_cbc,
896		.block_size		= kCCBlockSizeAES128,
897		.key_len		= kCCKeySizeAES192,
898		.iv_len			= kCCBlockSizeAES128,
899		.flags			= EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT|EVP_CIPH_CUSTOM_IV,
900		.init			= cc_aes_cbc_init,
901		.do_cipher		= cc_do_cbc_cipher,
902		.cleanup		= cc_cleanup,
903		.ctx_size		= sizeof(struct cc_key),
904		.set_asn1_parameters	= NULL,
905		.get_asn1_parameters	= NULL,
906		.ctrl			= NULL,
907		.app_data		= NULL
908	};
909	return (&c);
910
911#else
912	return (NULL);
913#endif
914}
915
916
917/**
918 * The AES-256 cipher type (Apple CommonCrypto provider)
919 *
920 * @return the AES-256-ECB EVP_CIPHER pointer.
921 */
922const EVP_CIPHER *
923EVP_cc_aes_256_ecb(void)
924{
925#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
926	static const EVP_CIPHER c =
927	{
928		.nid			= NID_aes_256_ecb,
929		.block_size		= kCCBlockSizeAES128,
930		.key_len		= kCCKeySizeAES256,
931		.iv_len			= kCCBlockSizeAES128,
932		.flags			= EVP_CIPH_ECB_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
933		.init			= cc_aes_ecb_init,
934		.do_cipher		= cc_do_cipher,
935		.cleanup		= cc_cleanup,
936		.ctx_size		= sizeof(struct cc_key),
937		.set_asn1_parameters	= NULL,
938		.get_asn1_parameters	= NULL,
939		.ctrl			= NULL,
940		.app_data		= NULL
941	};
942	return (&c);
943
944#else
945	return (NULL);
946#endif
947}
948
949
950/**
951 * The AES-256 cipher type (Apple CommonCrypto provider)
952 *
953 * @return the AES-256-CBC EVP_CIPHER pointer.
954 */
955const EVP_CIPHER *
956EVP_cc_aes_256_cbc(void)
957{
958#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
959	static const EVP_CIPHER c =
960	{
961		.nid			= NID_aes_256_cbc,
962		.block_size		= kCCBlockSizeAES128,
963		.key_len		= kCCKeySizeAES256,
964		.iv_len			= kCCBlockSizeAES128,
965		.flags			= EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT|EVP_CIPH_CUSTOM_IV,
966		.init			= cc_aes_cbc_init,
967		.do_cipher		= cc_do_cbc_cipher,
968		.cleanup		= cc_cleanup,
969		.ctx_size		= sizeof(struct cc_key),
970		.set_asn1_parameters	= NULL,
971		.get_asn1_parameters	= NULL,
972		.ctrl			= NULL,
973		.app_data		= NULL
974	};
975	return (&c);
976
977#else
978	return (NULL);
979#endif
980}
981
982
983#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
984static int
985cc_aes_cfb8_init(EVP_CIPHER_CTX *ctx,
986    const unsigned char *key,
987    const unsigned char *iv,
988    int encp)
989{
990	struct cc_key *cc = ctx->cipher_data;
991
992	if (iv) {
993		memcpy(ctx->iv, iv, ctx->cipher->iv_len);
994	} else{
995		memset(ctx->iv, 0, ctx->cipher->iv_len);
996	}
997	return (init_cc_key(1, kCCAlgorithmAES128, kCCOptionECBMode,
998	       key, ctx->cipher->key_len, NULL, &cc->href));
999}
1000
1001
1002#endif
1003
1004/**
1005 * The AES-128 CFB8 cipher type (Apple CommonCrypto provider)
1006 *
1007 * @return the AES-128-CFB8 EVP_CIPHER pointer.
1008 */
1009const EVP_CIPHER *
1010EVP_cc_aes_128_cfb8(void)
1011{
1012#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
1013	static const EVP_CIPHER c =
1014	{
1015		.nid			= NID_aes_128_cfb1,
1016		.block_size		= 1,
1017		.key_len		= kCCKeySizeAES128,
1018		.iv_len			= kCCBlockSizeAES128,
1019		.flags			= EVP_CIPH_CFB8_MODE | EVP_CIPH_ALWAYS_CALL_INIT,
1020		.init			= cc_aes_cfb8_init,
1021		.do_cipher		= cc_do_cfb8_cipher,
1022		.cleanup		= cc_cleanup,
1023		.ctx_size		= sizeof(struct cc_key),
1024		.set_asn1_parameters	= NULL,
1025		.get_asn1_parameters	= NULL,
1026		.ctrl			= NULL,
1027		.app_data		= NULL
1028	};
1029	return (&c);
1030
1031#else
1032	return (NULL);
1033#endif
1034}
1035
1036
1037/**
1038 * The AES-192 CFB8 cipher type (Apple CommonCrypto provider)
1039 *
1040 * @return the AES-192-CFB8 EVP_CIPHER pointer.
1041 */
1042const EVP_CIPHER *
1043EVP_cc_aes_192_cfb8(void)
1044{
1045#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
1046	static const EVP_CIPHER c =
1047	{
1048		.nid			= NID_aes_192_cfb1,
1049		.block_size		= 1,
1050		.key_len		= kCCKeySizeAES192,
1051		.iv_len			= kCCBlockSizeAES128,
1052		.flags			= EVP_CIPH_CFB8_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
1053		.init			= cc_aes_cfb8_init,
1054		.do_cipher		= cc_do_cfb8_cipher,
1055		.cleanup		= cc_cleanup,
1056		.ctx_size		= sizeof(struct cc_key),
1057		.set_asn1_parameters	= NULL,
1058		.get_asn1_parameters	= NULL,
1059		.ctrl			= NULL,
1060		.app_data		= NULL
1061	};
1062	return (&c);
1063
1064#else
1065	return (NULL);
1066#endif
1067}
1068
1069
1070/**
1071 * The AES-256 CFB8 cipher type (Apple CommonCrypto provider)
1072 *
1073 * @return the AES-256-CFB8 EVP_CIPHER pointer.
1074 */
1075const EVP_CIPHER *
1076EVP_cc_aes_256_cfb8(void)
1077{
1078#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
1079	static const EVP_CIPHER c =
1080	{
1081		.nid			= NID_aes_256_cfb1,
1082		.block_size		= 1,
1083		.key_len		= kCCKeySizeAES256,
1084		.iv_len			= kCCBlockSizeAES128,
1085		.flags			= EVP_CIPH_CFB8_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
1086		.init			= cc_aes_cfb8_init,
1087		.do_cipher		= cc_do_cfb8_cipher,
1088		.cleanup		= cc_cleanup,
1089		.ctx_size		= sizeof(struct cc_key),
1090		.set_asn1_parameters	= NULL,
1091		.get_asn1_parameters	= NULL,
1092		.ctrl			= NULL,
1093		.app_data		= NULL
1094	};
1095	return (&c);
1096
1097#else
1098	return (NULL);
1099#endif
1100}
1101
1102
1103/*
1104 *
1105 */
1106
1107#ifdef COMMONCRYPTO_SUPPORTS_RC2
1108static int
1109cc_rc2_cbc_init(EVP_CIPHER_CTX *ctx,
1110    const unsigned char *key,
1111    const unsigned char *iv,
1112    int encp)
1113{
1114	struct cc_key *cc = ctx->cipher_data;
1115
1116	return (init_cc_key(encp, kCCAlgorithmRC2, 0, key, ctx->cipher->key_len, iv, &cc->href));
1117}
1118
1119
1120#endif
1121
1122/**
1123 * The RC2 cipher type - common crypto
1124 *
1125 * @return the RC2 EVP_CIPHER pointer.
1126 */
1127const EVP_CIPHER *
1128EVP_cc_rc2_cbc(void)
1129{
1130#ifdef COMMONCRYPTO_SUPPORTS_RC2
1131	static const EVP_CIPHER rc2_cbc =
1132	{
1133		.nid			= NID_rc2_cbc,
1134		.block_size		= kCCBlockSizeRC2,
1135		.key_len		= 16,
1136		.iv_len			= kCCBlockSizeRC2,
1137		.flags			= EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
1138		.init			= cc_rc2_cbc_init,
1139		.do_cipher		= cc_do_cipher,
1140		.cleanup		= cc_cleanup,
1141		.ctx_size		= sizeof(struct cc_key),
1142		.set_asn1_parameters	= NULL,
1143		.get_asn1_parameters	= NULL,
1144		.ctrl			= NULL,
1145		.app_data		= NULL
1146	};
1147	return (&rc2_cbc);
1148
1149#else
1150	return (NULL);
1151#endif
1152}
1153
1154
1155/**
1156 * The RC2-40 cipher type - common crypto
1157 *
1158 * @return the RC2-40 EVP_CIPHER pointer.
1159 *
1160 */
1161const EVP_CIPHER *
1162EVP_cc_rc2_40_cbc(void)
1163{
1164#ifdef COMMONCRYPTO_SUPPORTS_RC2
1165	static const EVP_CIPHER rc2_40_cbc =
1166	{
1167		.nid			= NID_rc2_40_cbc,
1168		.block_size		= kCCBlockSizeRC2,
1169		.key_len		= 5,
1170		.iv_len			= kCCBlockSizeRC2,
1171		.flags			= EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
1172		.init			= cc_rc2_cbc_init,
1173		.do_cipher		= cc_do_cipher,
1174		.cleanup		= cc_cleanup,
1175		.ctx_size		= sizeof(struct cc_key),
1176		.set_asn1_parameters	= NULL,
1177		.get_asn1_parameters	= NULL,
1178		.ctrl			= NULL,
1179		.app_data		= NULL
1180	};
1181	return (&rc2_40_cbc);
1182
1183#else
1184	return (NULL);
1185#endif
1186}
1187
1188
1189/**
1190 * The RC2-64 cipher type - common crypto
1191 *
1192 * @return the RC2-64 EVP_CIPHER pointer.
1193 *
1194 */
1195const EVP_CIPHER *
1196EVP_cc_rc2_64_cbc(void)
1197{
1198#ifdef COMMONCRYPTO_SUPPORTS_RC2
1199	static const EVP_CIPHER rc2_64_cbc =
1200	{
1201		.nid			= NID_rc2_64_cbc,
1202		.block_size		= kCCBlockSizeRC2,
1203		.key_len		= 8,
1204		.iv_len			= kCCBlockSizeRC2,
1205		.flags			= EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
1206		.init			= cc_rc2_cbc_init,
1207		.do_cipher		= cc_do_cipher,
1208		.cleanup		= cc_cleanup,
1209		.ctx_size		= sizeof(struct cc_key),
1210		.set_asn1_parameters	= NULL,
1211		.get_asn1_parameters	= NULL,
1212		.ctrl			= NULL,
1213		.app_data		= NULL
1214	};
1215	return (&rc2_64_cbc);
1216
1217#else
1218	return (NULL);
1219#endif
1220}
1221
1222
1223/**
1224 * The CommonCrypto md2 provider
1225 *
1226 * @ingroup hcrypto_evp
1227 */
1228const EVP_MD *
1229EVP_cc_md2(void)
1230{
1231#ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
1232	static const struct ossl_evp_md md2 =
1233	{
1234		.hash_size	= CC_MD2_DIGEST_LENGTH,
1235		.block_size	= CC_MD2_BLOCK_BYTES,
1236		.ctx_size	= sizeof(CC_MD2_CTX),
1237		.init		= (ossl_evp_md_init)CC_MD2_Init,
1238		.update		= (ossl_evp_md_update)CC_MD2_Update,
1239		.final		= (ossl_evp_md_final)CC_MD2_Final,
1240		.cleanup	= (ossl_evp_md_cleanup)NULL
1241	};
1242	return (&md2);
1243
1244#else
1245	return (NULL);
1246#endif
1247}
1248
1249
1250/**
1251 * The CommonCrypto md4 provider
1252 *
1253 * @ingroup hcrypto_evp
1254 */
1255const EVP_MD *
1256EVP_cc_md4(void)
1257{
1258#ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
1259	static const struct ossl_evp_md md4 =
1260	{
1261		.hash_size	= CC_MD4_DIGEST_LENGTH,
1262		.block_size	= CC_MD4_BLOCK_BYTES,
1263		.ctx_size	= sizeof(CC_MD4_CTX),
1264		.init		= (ossl_evp_md_init)CC_MD4_Init,
1265		.update		= (ossl_evp_md_update)CC_MD4_Update,
1266		.final		= (ossl_evp_md_final)CC_MD4_Final,
1267		.cleanup	= (ossl_evp_md_cleanup)NULL
1268	};
1269	return (&md4);
1270
1271#else
1272	return (NULL);
1273#endif
1274}
1275
1276
1277/**
1278 * The CommonCrypto md5 provider
1279 *
1280 * @ingroup hcrypto_evp
1281 */
1282const EVP_MD *
1283EVP_cc_md5(void)
1284{
1285#ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
1286	static const struct ossl_evp_md md5 =
1287	{
1288		.hash_size	= CC_MD5_DIGEST_LENGTH,
1289		.block_size	= CC_MD5_BLOCK_BYTES,
1290		.ctx_size	= sizeof(CC_MD5_CTX),
1291		.init		= (ossl_evp_md_init)CC_MD5_Init,
1292		.update		= (ossl_evp_md_update)CC_MD5_Update,
1293		.final		= (ossl_evp_md_final)CC_MD5_Final,
1294		.cleanup	= (ossl_evp_md_cleanup)NULL
1295	};
1296	return (&md5);
1297
1298#else
1299	return (NULL);
1300#endif
1301}
1302
1303
1304/**
1305 * The CommonCrypto sha1 provider
1306 *
1307 * @ingroup hcrypto_evp
1308 */
1309const EVP_MD *
1310EVP_cc_sha1(void)
1311{
1312#ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
1313	static const struct ossl_evp_md sha1 =
1314	{
1315		.hash_size	= CC_SHA1_DIGEST_LENGTH,
1316		.block_size	= CC_SHA1_BLOCK_BYTES,
1317		.ctx_size	= sizeof(CC_SHA1_CTX),
1318		.init		= (ossl_evp_md_init)CC_SHA1_Init,
1319		.update		= (ossl_evp_md_update)CC_SHA1_Update,
1320		.final		= (ossl_evp_md_final)CC_SHA1_Final,
1321		.cleanup	= (ossl_evp_md_cleanup)NULL
1322	};
1323	return (&sha1);
1324
1325#else
1326	return (NULL);
1327#endif
1328}
1329
1330
1331/**
1332 * The CommonCrypto sha224 provider
1333 *
1334 * @ingroup hcrypto_evp
1335 */
1336const EVP_MD *
1337EVP_cc_sha224(void)
1338{
1339#ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
1340	static const struct ossl_evp_md sha224 =
1341	{
1342		.hash_size	= CC_SHA224_DIGEST_LENGTH,
1343		.block_size	= CC_SHA224_BLOCK_BYTES,
1344		.ctx_size	= sizeof(CC_SHA256_CTX),
1345		.init		= (ossl_evp_md_init)CC_SHA224_Init,
1346		.update		= (ossl_evp_md_update)CC_SHA224_Update,
1347		.final		= (ossl_evp_md_final)CC_SHA224_Final,
1348		.cleanup	= (ossl_evp_md_cleanup)NULL
1349	};
1350	return (&sha224);
1351
1352#else
1353	return (NULL);
1354#endif
1355}
1356
1357
1358/**
1359 * The CommonCrypto sha256 provider
1360 *
1361 * @ingroup hcrypto_evp
1362 */
1363const EVP_MD *
1364EVP_cc_sha256(void)
1365{
1366#ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
1367	static const struct ossl_evp_md sha256 =
1368	{
1369		.hash_size	= CC_SHA256_DIGEST_LENGTH,
1370		.block_size	= CC_SHA256_BLOCK_BYTES,
1371		.ctx_size	= sizeof(CC_SHA256_CTX),
1372		.init		= (ossl_evp_md_init)CC_SHA256_Init,
1373		.update		= (ossl_evp_md_update)CC_SHA256_Update,
1374		.final		= (ossl_evp_md_final)CC_SHA256_Final,
1375		.cleanup	= (ossl_evp_md_cleanup)NULL
1376	};
1377	return (&sha256);
1378
1379#else
1380	return (NULL);
1381#endif
1382}
1383
1384
1385/**
1386 * The CommonCrypto sha384 provider
1387 *
1388 * @ingroup hcrypto_evp
1389 */
1390
1391/**
1392 * The CommonCrypto sha384 provider
1393 *
1394 */
1395const EVP_MD *
1396EVP_cc_sha384(void)
1397{
1398#ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
1399	static const struct ossl_evp_md sha384 =
1400	{
1401		.hash_size	= CC_SHA384_DIGEST_LENGTH,
1402		.block_size	= CC_SHA384_BLOCK_BYTES,
1403		.ctx_size	= sizeof(CC_SHA512_CTX),
1404		.init		= (ossl_evp_md_init)CC_SHA384_Init,
1405		.update		= (ossl_evp_md_update)CC_SHA384_Update,
1406		.final		= (ossl_evp_md_final)CC_SHA384_Final,
1407		.cleanup	= (ossl_evp_md_cleanup)NULL
1408	};
1409	return (&sha384);
1410
1411#else
1412	return (NULL);
1413#endif
1414}
1415
1416/**
1417 * The CommonCrypto sha512 provider
1418 *
1419 */
1420const EVP_MD *
1421EVP_cc_sha512(void)
1422{
1423#ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
1424	static const struct ossl_evp_md sha512 =
1425	{
1426		.hash_size	= CC_SHA512_DIGEST_LENGTH,
1427		.block_size	= CC_SHA512_BLOCK_BYTES,
1428		.ctx_size	= sizeof(CC_SHA512_CTX),
1429		.init		= (ossl_evp_md_init)CC_SHA512_Init,
1430		.update		= (ossl_evp_md_update)CC_SHA512_Update,
1431		.final		= (ossl_evp_md_final)CC_SHA512_Final,
1432		.cleanup	= (ossl_evp_md_cleanup)NULL
1433	};
1434	return (&sha512);
1435
1436#else
1437	return (NULL);
1438#endif
1439}
1440
1441/**
1442 * The CommonCrypto rmd128 provider
1443 *
1444 */
1445#ifdef HAVE_COMMONCRYPTO_COMMONDIGESTSPI_H
1446static int
1447CC_RMD128_Init(EVP_MD_CTX *c)
1448{
1449	return (CCDigestInit(kCCDigestRMD128, (CCDigestRef)c));
1450}
1451
1452
1453static int
1454CC_RMD_Update(EVP_MD_CTX *c, const void *data, size_t len)
1455{
1456	return (CCDigestUpdate((CCDigestRef)c, data, len));
1457}
1458
1459
1460static int
1461CC_RMD_Final(void *out, EVP_MD_CTX *c)
1462{
1463	return (CCDigestFinal((CCDigestRef)c, (uint8_t *)out));
1464}
1465
1466
1467#endif /* HAVE_COMMONCRYPTO_COMMONDIGESTSPI_H */
1468
1469/**
1470 * The CommonCrypto rmd128 provider
1471 *
1472 * @ingroup hcrypto_evp
1473 */
1474const EVP_MD *
1475EVP_cc_rmd128(void)
1476{
1477#ifdef HAVE_COMMONCRYPTO_COMMONDIGESTSPI_H
1478	static const struct ossl_evp_md rmd128 =
1479	{
1480		.hash_size	= CC_RMD128_DIGEST_LENGTH,
1481		.block_size	= CC_RMD128_BLOCK_BYTES,
1482		.ctx_size	= sizeof(CCDigestCtx),
1483		.init		= (ossl_evp_md_init)CC_RMD128_Init,
1484		.update		= (ossl_evp_md_update)CC_RMD_Update,
1485		.final		= (ossl_evp_md_final)CC_RMD_Final,
1486		.cleanup	= (ossl_evp_md_cleanup)NULL
1487	};
1488	return (&rmd128);
1489
1490#else
1491	return (NULL);
1492#endif
1493}
1494
1495
1496/**
1497 * The CommonCrypto rmd160 provider
1498 *
1499 * @ingroup hcrypto_evp
1500 */
1501#ifdef HAVE_COMMONCRYPTO_COMMONDIGESTSPI_H
1502static int
1503CC_RMD160_Init(EVP_MD_CTX *c)
1504{
1505	return (CCDigestInit(kCCDigestRMD160, (CCDigestRef)c));
1506}
1507
1508#endif /* HAVE_COMMONCRYPTO_COMMONDIGESTSPI_H */
1509
1510const EVP_MD *
1511EVP_cc_rmd160(void)
1512{
1513#ifdef HAVE_COMMONCRYPTO_COMMONDIGESTSPI_H
1514	static const struct ossl_evp_md rmd160 =
1515	{
1516		.hash_size	= CC_RMD160_DIGEST_LENGTH,
1517		.block_size	= CC_RMD160_BLOCK_BYTES,
1518		.ctx_size	= sizeof(CCDigestCtx),
1519		.init		= (ossl_evp_md_init)CC_RMD160_Init,
1520		.update		= (ossl_evp_md_update)CC_RMD_Update,
1521		.final		= (ossl_evp_md_final)CC_RMD_Final,
1522		.cleanup	= (ossl_evp_md_cleanup)NULL
1523	};
1524	return (&rmd160);
1525
1526#else
1527	return (NULL);
1528#endif
1529}
1530
1531
1532/**
1533 * The CommonCrypto rmd256 provider
1534 */
1535#ifdef HAVE_COMMONCRYPTO_COMMONDIGESTSPI_H
1536static int
1537CC_RMD256_Init(EVP_MD_CTX *c)
1538{
1539	return (CCDigestInit(kCCDigestRMD256, (CCDigestRef)c));
1540}
1541
1542
1543#endif /* HAVE_COMMONCRYPTO_COMMONDIGESTSPI_H */
1544
1545const EVP_MD *
1546EVP_cc_rmd256(void)
1547{
1548#ifdef HAVE_COMMONCRYPTO_COMMONDIGESTSPI_H
1549	static const struct ossl_evp_md rmd256 =
1550	{
1551		.hash_size	= CC_RMD256_DIGEST_LENGTH,
1552		.block_size	= CC_RMD256_BLOCK_BYTES,
1553		.ctx_size	= sizeof(CCDigestCtx),
1554		.init		= (ossl_evp_md_init)CC_RMD256_Init,
1555		.update		= (ossl_evp_md_update)CC_RMD_Update,
1556		.final		= (ossl_evp_md_final)CC_RMD_Final,
1557		.cleanup	= (ossl_evp_md_cleanup)NULL
1558	};
1559	return (&rmd256);
1560
1561#else
1562	return (NULL);
1563#endif
1564}
1565
1566
1567/**
1568 * The CommonCrypto rmd256 provider
1569 */
1570#ifdef HAVE_COMMONCRYPTO_COMMONDIGESTSPI_H
1571static int
1572CC_RMD320_Init(EVP_MD_CTX *c)
1573{
1574	return (CCDigestInit(kCCDigestRMD320, (CCDigestRef)c));
1575}
1576
1577
1578#endif /* HAVE_COMMONCRYPTO_COMMONDIGESTSPI_H */
1579
1580const EVP_MD *
1581EVP_cc_rmd320(void)
1582{
1583#ifdef HAVE_COMMONCRYPTO_COMMONDIGESTSPI_H
1584	static const struct ossl_evp_md rmd320 =
1585	{
1586		.hash_size	= CC_RMD320_DIGEST_LENGTH,
1587		.block_size	= CC_RMD320_BLOCK_BYTES,
1588		.ctx_size	= sizeof(CCDigestCtx),
1589		.init		= (ossl_evp_md_init)CC_RMD320_Init,
1590		.update		= (ossl_evp_md_update)CC_RMD_Update,
1591		.final		= (ossl_evp_md_final)CC_RMD_Final,
1592		.cleanup	= (ossl_evp_md_cleanup)NULL
1593	};
1594	return (&rmd320);
1595
1596#else
1597	return (NULL);
1598#endif
1599}
1600
1601
1602/**
1603 * The Camellia-128 cipher type - CommonCrypto
1604 *
1605 * @return the Camellia-128 EVP_CIPHER pointer.
1606 *
1607 * @ingroup hcrypto_evp
1608 */
1609const EVP_CIPHER *
1610EVP_cc_camellia_128_cbc(void)
1611{
1612	return (NULL);
1613}
1614
1615
1616/**
1617 * The Camellia-198 cipher type - CommonCrypto
1618 *
1619 * @return the Camellia-198 EVP_CIPHER pointer.
1620 *
1621 * @ingroup hcrypto_evp
1622 */
1623const EVP_CIPHER *
1624EVP_cc_camellia_192_cbc(void)
1625{
1626	return (NULL);
1627}
1628
1629
1630/**
1631 * The Camellia-256 cipher type - CommonCrypto
1632 *
1633 * @return the Camellia-256 EVP_CIPHER pointer.
1634 *
1635 * @ingroup hcrypto_evp
1636 */
1637const EVP_CIPHER *
1638EVP_cc_camellia_256_cbc(void)
1639{
1640	return (NULL);
1641}
1642
1643
1644#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
1645
1646/*
1647 *
1648 */
1649
1650void CC_RC4_set_key(void *ks, int len, const unsigned char *data);
1651void CC_RC4(void *ks, unsigned long len, const unsigned char *indata,
1652    unsigned char *outdata);
1653
1654#define data(ctx)    ((ctx)->cipher_data)
1655
1656static int
1657cc_rc4_init(EVP_CIPHER_CTX *ctx,
1658    const unsigned char *key,
1659    const unsigned char *iv __unused,
1660    int encp __unused)
1661{
1662	void *ks = ctx->cipher_data;
1663
1664	CC_RC4_set_key(ks, (int)EVP_CIPHER_CTX_key_length(ctx), key);
1665	return (1);
1666}
1667
1668
1669static int
1670cc_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1671    const unsigned char *in, unsigned int inl)
1672{
1673	void *ks = ctx->cipher_data;
1674
1675	CC_RC4(ks, (unsigned long)inl, in, out);
1676	return (1);
1677}
1678
1679
1680#endif
1681
1682/**
1683 *
1684 * The RC4 cipher type (Apple CommonCrypto provider)
1685 *
1686 * @return the RC4 EVP_CIPHER pointer.
1687 *
1688 * @ingroup hcrypto_evp
1689 */
1690const EVP_CIPHER *
1691EVP_cc_rc4(void)
1692{
1693#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
1694	static const EVP_CIPHER c =
1695	{
1696		.nid			= NID_rc4,
1697		.block_size		= 1,
1698		.key_len		= 16,
1699		.iv_len			= 0,
1700		.flags			= EVP_CIPH_STREAM_CIPHER | EVP_CIPH_VARIABLE_LENGTH,
1701		.init			= cc_rc4_init,
1702		.do_cipher		= cc_rc4_cipher,
1703		.cleanup		= NULL,
1704		.ctx_size		= kCCContextSizeRC4,
1705		.set_asn1_parameters	= NULL,
1706		.get_asn1_parameters	= NULL,
1707		.ctrl			= NULL,
1708		.app_data		= NULL
1709	};
1710	return (&c);
1711
1712#else
1713	return (NULL);
1714#endif
1715}
1716
1717
1718/**
1719 * The RC4-40 cipher type (Apple CommonCrypto provider)
1720 *
1721 * @return the RC4 EVP_CIPHER pointer.
1722 *
1723 * @ingroup hcrypto_evp
1724 */
1725const EVP_CIPHER *
1726EVP_cc_rc4_40(void)
1727{
1728#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
1729	static const EVP_CIPHER c =
1730	{
1731		.nid			= NID_rc4_40,
1732		.block_size		= 1,
1733		.key_len		= 5 /* 40 bit */,
1734		.iv_len			= 0,
1735		.flags			= EVP_CIPH_STREAM_CIPHER | EVP_CIPH_VARIABLE_LENGTH,
1736		.init			= cc_rc4_init,
1737		.do_cipher		= cc_rc4_cipher,
1738		.cleanup		= NULL,
1739		.ctx_size		= kCCContextSizeRC4,
1740		.set_asn1_parameters	= NULL,
1741		.get_asn1_parameters	= NULL,
1742		.ctrl			= NULL,
1743		.app_data		= NULL
1744	};
1745	return (&c);
1746
1747#else
1748	return (NULL);
1749#endif
1750}
1751
1752
1753#endif /* __APPLE__ */
1754