2 * Copyright (c) 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 /* CommonCrypto provider */
45 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
46 #include <CommonCrypto/CommonDigest.h>
48 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
49 #include <CommonCrypto/CommonCryptor.h>
53 #include <evp-hcrypto.h>
60 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
67 cc_do_cipher(EVP_CIPHER_CTX
*ctx
,
69 const unsigned char *in
,
72 struct cc_key
*cc
= ctx
->cipher_data
;
76 memcpy(out
, in
, size
);
78 ret
= CCCryptorUpdate(cc
->href
, in
, size
, out
, size
, &moved
);
89 cc_cleanup(EVP_CIPHER_CTX
*ctx
)
91 struct cc_key
*cc
= ctx
->cipher_data
;
93 CCCryptorRelease(cc
->href
);
98 init_cc_key(int encp
, unsigned long flags
,
99 CCAlgorithm alg
, const void *key
, size_t keylen
,
100 const void *iv
, CCCryptorRef
*ref
)
102 CCOperation op
= encp
? kCCEncrypt
: kCCDecrypt
;
104 CCModeOptions options
= 0;
108 if (key
== NULL
&& iv
) {
109 CCCryptorReset(*ref
, iv
);
112 CCCryptorRelease(*ref
);
116 switch (flags
& EVP_CIPH_MODE
) {
117 case EVP_CIPH_STREAM_CIPHER
:
120 case EVP_CIPH_CFB8_MODE
:
128 ret
= CCCryptorCreateWithMode(op
, mode
, alg
, ccNoPadding
,
129 iv
, key
, keylen
, NULL
, 0, 0,
139 cc_des_ede3_cbc_init(EVP_CIPHER_CTX
*ctx
,
140 const unsigned char * key
,
141 const unsigned char * iv
,
144 struct cc_key
*cc
= ctx
->cipher_data
;
145 return init_cc_key(encp
, ctx
->cipher
->flags
, kCCAlgorithm3DES
,
146 key
, kCCKeySize3DES
, iv
, &cc
->href
);
149 #endif /* HAVE_COMMONCRYPTO_COMMONCRYPTOR_H */
152 * The triple DES cipher type (Apple CommonCrypto provider)
154 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
156 * @ingroup hcrypto_evp
160 EVP_cc_des_ede3_cbc(void)
162 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
163 static const EVP_CIPHER des_ede3_cbc
= {
168 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
169 cc_des_ede3_cbc_init
,
172 sizeof(struct cc_key
),
178 return &des_ede3_cbc
;
179 #elif HCRYPTO_FALLBACK
180 return EVP_hcrypto_des_ede3_cbc();
186 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
192 cc_des_cbc_init(EVP_CIPHER_CTX
*ctx
,
193 const unsigned char * key
,
194 const unsigned char * iv
,
197 struct cc_key
*cc
= ctx
->cipher_data
;
198 return init_cc_key(encp
, ctx
->cipher
->flags
, kCCAlgorithmDES
,
199 key
, kCCBlockSizeDES
, iv
, &cc
->href
);
204 * The DES cipher type (Apple CommonCrypto provider)
206 * @return the DES-CBC EVP_CIPHER pointer.
208 * @ingroup hcrypto_evp
214 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
215 static const EVP_CIPHER des_ede3_cbc
= {
220 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
224 sizeof(struct cc_key
),
230 return &des_ede3_cbc
;
231 #elif HCRYPTO_FALLBACK
232 return EVP_hcrypto_des_cbc();
238 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
244 cc_aes_cbc_init(EVP_CIPHER_CTX
*ctx
,
245 const unsigned char * key
,
246 const unsigned char * iv
,
249 struct cc_key
*cc
= ctx
->cipher_data
;
250 return init_cc_key(encp
, ctx
->cipher
->flags
, kCCAlgorithmAES128
,
251 key
, ctx
->cipher
->key_len
, iv
, &cc
->href
);
256 * The AES-128 cipher type (Apple CommonCrypto provider)
258 * @return the AES-128-CBC EVP_CIPHER pointer.
260 * @ingroup hcrypto_evp
264 EVP_cc_aes_128_cbc(void)
266 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
267 static const EVP_CIPHER c
= {
272 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
276 sizeof(struct cc_key
),
283 #elif HCRYPTO_FALLBACK
284 return EVP_hcrypto_aes_128_cbc();
291 * The AES-192 cipher type (Apple CommonCrypto provider)
293 * @return the AES-192-CBC EVP_CIPHER pointer.
295 * @ingroup hcrypto_evp
299 EVP_cc_aes_192_cbc(void)
301 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
302 static const EVP_CIPHER c
= {
307 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
311 sizeof(struct cc_key
),
318 #elif HCRYPTO_FALLBACK
319 return EVP_hcrypto_aes_192_cbc();
326 * The AES-256 cipher type (Apple CommonCrypto provider)
328 * @return the AES-256-CBC EVP_CIPHER pointer.
330 * @ingroup hcrypto_evp
334 EVP_cc_aes_256_cbc(void)
336 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
337 static const EVP_CIPHER c
= {
342 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
346 sizeof(struct cc_key
),
353 #elif HCRYPTO_FALLBACK
354 return EVP_hcrypto_aes_256_cbc();
360 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
366 cc_aes_cfb8_init(EVP_CIPHER_CTX
*ctx
,
367 const unsigned char * key
,
368 const unsigned char * iv
,
371 struct cc_key
*cc
= ctx
->cipher_data
;
372 return init_cc_key(encp
, ctx
->cipher
->flags
, kCCAlgorithmAES128
,
373 key
, ctx
->cipher
->key_len
, NULL
, &cc
->href
);
378 * The AES-128 CFB8 cipher type (Apple CommonCrypto provider)
380 * @return the AES-128-CFB8 EVP_CIPHER pointer.
382 * @ingroup hcrypto_evp
386 EVP_cc_aes_128_cfb8(void)
388 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
389 static const EVP_CIPHER c
= {
394 EVP_CIPH_CFB8_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
398 sizeof(struct cc_key
),
405 #elif HCRYPTO_FALLBACK
406 return EVP_hcrypto_aes_128_cfb8();
413 * The AES-192 CFB8 cipher type (Apple CommonCrypto provider)
415 * @return the AES-192-CFB8 EVP_CIPHER pointer.
417 * @ingroup hcrypto_evp
421 EVP_cc_aes_192_cfb8(void)
423 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
424 static const EVP_CIPHER c
= {
429 EVP_CIPH_CFB8_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
433 sizeof(struct cc_key
),
440 #elif HCRYPTO_FALLBACK
441 return EVP_hcrypto_aes_192_cfb8();
448 * The AES-256 CFB8 cipher type (Apple CommonCrypto provider)
450 * @return the AES-256-CFB8 EVP_CIPHER pointer.
452 * @ingroup hcrypto_evp
456 EVP_cc_aes_256_cfb8(void)
458 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
459 static const EVP_CIPHER c
= {
464 EVP_CIPH_CFB8_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
468 sizeof(struct cc_key
),
475 #elif HCRYPTO_FALLBACK
476 return EVP_hcrypto_aes_256_cfb8();
486 #ifdef COMMONCRYPTO_SUPPORTS_RC2
488 cc_rc2_cbc_init(EVP_CIPHER_CTX
*ctx
,
489 const unsigned char * key
,
490 const unsigned char * iv
,
493 struct cc_key
*cc
= ctx
->cipher_data
;
494 return init_cc_key(encp
, ctx
->cipher
->flags
, kCCAlgorithmRC2
,
495 key
, ctx
->cipher
->key_len
, iv
, &cc
->href
);
500 * The RC2 cipher type - common crypto
502 * @return the RC2 EVP_CIPHER pointer.
504 * @ingroup hcrypto_evp
511 #ifdef COMMONCRYPTO_SUPPORTS_RC2
512 static const EVP_CIPHER rc2_cbc
= {
517 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
521 sizeof(struct cc_key
),
528 #elif HCRYPTO_FALLBACK
529 return EVP_hcrypto_rc2_cbc();
536 * The RC2-40 cipher type - common crypto
538 * @return the RC2-40 EVP_CIPHER pointer.
540 * @ingroup hcrypto_evp
545 EVP_cc_rc2_40_cbc(void)
547 #ifdef COMMONCRYPTO_SUPPORTS_RC2
548 static const EVP_CIPHER rc2_40_cbc
= {
553 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
557 sizeof(struct cc_key
),
564 #elif HCRYPTO_FALLBACK
565 return EVP_hcrypto_rc2_40_cbc();
573 * The RC2-64 cipher type - common crypto
575 * @return the RC2-64 EVP_CIPHER pointer.
577 * @ingroup hcrypto_evp
582 EVP_cc_rc2_64_cbc(void)
584 #ifdef COMMONCRYPTO_SUPPORTS_RC2
585 static const EVP_CIPHER rc2_64_cbc
= {
590 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
594 sizeof(struct cc_key
),
601 #elif HCRYPTO_FALLBACK
602 return EVP_hcrypto_rc2_64_cbc();
609 * The CommonCrypto md2 provider
611 * @ingroup hcrypto_evp
617 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
618 static const struct hc_evp_md md2
= {
619 CC_MD2_DIGEST_LENGTH
,
622 (hc_evp_md_init
)CC_MD2_Init
,
623 (hc_evp_md_update
)CC_MD2_Update
,
624 (hc_evp_md_final
)CC_MD2_Final
,
625 (hc_evp_md_cleanup
)NULL
628 #elif HCRYPTO_FALLBACK
629 return EVP_hcrypto_md2();
636 * The CommonCrypto md4 provider
638 * @ingroup hcrypto_evp
644 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
645 static const struct hc_evp_md md4
= {
646 CC_MD4_DIGEST_LENGTH
,
649 (hc_evp_md_init
)CC_MD4_Init
,
650 (hc_evp_md_update
)CC_MD4_Update
,
651 (hc_evp_md_final
)CC_MD4_Final
,
652 (hc_evp_md_cleanup
)NULL
655 #elif HCRYPTO_FALLBACK
656 return EVP_hcrypto_md4();
663 * The CommonCrypto md5 provider
665 * @ingroup hcrypto_evp
671 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
672 static const struct hc_evp_md md5
= {
673 CC_MD5_DIGEST_LENGTH
,
676 (hc_evp_md_init
)CC_MD5_Init
,
677 (hc_evp_md_update
)CC_MD5_Update
,
678 (hc_evp_md_final
)CC_MD5_Final
,
679 (hc_evp_md_cleanup
)NULL
682 #elif HCRYPTO_FALLBACK
683 return EVP_hcrypto_md5();
690 * The CommonCrypto sha1 provider
692 * @ingroup hcrypto_evp
698 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
699 static const struct hc_evp_md sha1
= {
700 CC_SHA1_DIGEST_LENGTH
,
703 (hc_evp_md_init
)CC_SHA1_Init
,
704 (hc_evp_md_update
)CC_SHA1_Update
,
705 (hc_evp_md_final
)CC_SHA1_Final
,
706 (hc_evp_md_cleanup
)NULL
709 #elif HCRYPTO_FALLBACK
710 return EVP_hcrypto_sha1();
717 * The CommonCrypto sha256 provider
719 * @ingroup hcrypto_evp
725 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
726 static const struct hc_evp_md sha256
= {
727 CC_SHA256_DIGEST_LENGTH
,
728 CC_SHA256_BLOCK_BYTES
,
729 sizeof(CC_SHA256_CTX
),
730 (hc_evp_md_init
)CC_SHA256_Init
,
731 (hc_evp_md_update
)CC_SHA256_Update
,
732 (hc_evp_md_final
)CC_SHA256_Final
,
733 (hc_evp_md_cleanup
)NULL
736 #elif HCRYPTO_FALLBACK
737 return EVP_hcrypto_sha256();
744 * The CommonCrypto sha384 provider
746 * @ingroup hcrypto_evp
752 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
753 static const struct hc_evp_md sha384
= {
754 CC_SHA384_DIGEST_LENGTH
,
755 CC_SHA384_BLOCK_BYTES
,
756 sizeof(CC_SHA512_CTX
),
757 (hc_evp_md_init
)CC_SHA384_Init
,
758 (hc_evp_md_update
)CC_SHA384_Update
,
759 (hc_evp_md_final
)CC_SHA384_Final
,
760 (hc_evp_md_cleanup
)NULL
763 #elif HCRYPTO_FALLBACK
764 return EVP_hcrypto_sha384();
771 * The CommonCrypto sha512 provider
773 * @ingroup hcrypto_evp
779 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
780 static const struct hc_evp_md sha512
= {
781 CC_SHA512_DIGEST_LENGTH
,
782 CC_SHA512_BLOCK_BYTES
,
783 sizeof(CC_SHA512_CTX
),
784 (hc_evp_md_init
)CC_SHA512_Init
,
785 (hc_evp_md_update
)CC_SHA512_Update
,
786 (hc_evp_md_final
)CC_SHA512_Final
,
787 (hc_evp_md_cleanup
)NULL
790 #elif HCRYPTO_FALLBACK
791 return EVP_hcrypto_sha512();
798 * The Camellia-128 cipher type - CommonCrypto
800 * @return the Camellia-128 EVP_CIPHER pointer.
802 * @ingroup hcrypto_evp
806 EVP_cc_camellia_128_cbc(void)
809 return EVP_hcrypto_camellia_128_cbc();
816 * The Camellia-198 cipher type - CommonCrypto
818 * @return the Camellia-198 EVP_CIPHER pointer.
820 * @ingroup hcrypto_evp
824 EVP_cc_camellia_192_cbc(void)
827 return EVP_hcrypto_camellia_192_cbc();
834 * The Camellia-256 cipher type - CommonCrypto
836 * @return the Camellia-256 EVP_CIPHER pointer.
838 * @ingroup hcrypto_evp
842 EVP_cc_camellia_256_cbc(void)
845 return EVP_hcrypto_camellia_256_cbc();
851 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
858 cc_rc4_init(EVP_CIPHER_CTX
*ctx
,
859 const unsigned char * key
,
860 const unsigned char * iv
,
863 struct cc_key
*cc
= ctx
->cipher_data
;
864 return init_cc_key(encp
, ctx
->cipher
->flags
, kCCAlgorithmRC4
,
865 key
, ctx
->key_len
, iv
, &cc
->href
);
872 * The RC4 cipher type (Apple CommonCrypto provider)
874 * @return the RC4 EVP_CIPHER pointer.
876 * @ingroup hcrypto_evp
882 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
883 static const EVP_CIPHER rc4
= {
888 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,
892 sizeof(struct cc_key
),
899 #elif HCRYPTO_FALLBACK
900 return EVP_hcrypto_rc4();
908 * The RC4-40 cipher type (Apple CommonCrypto provider)
910 * @return the RC4 EVP_CIPHER pointer.
912 * @ingroup hcrypto_evp
918 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
919 static const EVP_CIPHER rc4_40
= {
924 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,
928 sizeof(struct cc_key
),
935 #elif HCRYPTO_FALLBACK
936 return EVP_hcrypto_rc4_40();
942 #endif /* __APPLE__ */