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 */
42 #include <sys/types.h>
48 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
49 #include <CommonCrypto/CommonDigest.h>
51 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
52 #include <CommonCrypto/CommonCryptor.h>
62 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
69 cc_do_cipher(EVP_CIPHER_CTX
*ctx
,
71 const unsigned char *in
,
74 struct cc_key
*cc
= ctx
->cipher_data
;
78 memcpy(out
, in
, size
);
80 ret
= CCCryptorUpdate(cc
->href
, in
, size
, out
, size
, &moved
);
91 cc_do_cfb8_cipher(EVP_CIPHER_CTX
*ctx
,
93 const unsigned char *in
,
96 struct cc_key
*cc
= ctx
->cipher_data
;
101 for (i
= 0; i
< size
; i
++) {
102 unsigned char oiv
[EVP_MAX_IV_LENGTH
+ 1];
104 assert(ctx
->cipher
->iv_len
+ 1 <= sizeof(oiv
));
105 memcpy(oiv
, ctx
->iv
, ctx
->cipher
->iv_len
);
107 ret
= CCCryptorUpdate(cc
->href
, ctx
->iv
, ctx
->cipher
->iv_len
,
108 ctx
->iv
, ctx
->cipher
->iv_len
, &moved
);
112 if (moved
!= ctx
->cipher
->iv_len
)
116 oiv
[ctx
->cipher
->iv_len
] = in
[i
];
117 out
[i
] = in
[i
] ^ ctx
->iv
[0];
119 oiv
[ctx
->cipher
->iv_len
] = out
[i
];
121 memcpy(ctx
->iv
, &oiv
[1], ctx
->cipher
->iv_len
);
128 cc_cleanup(EVP_CIPHER_CTX
*ctx
)
130 struct cc_key
*cc
= ctx
->cipher_data
;
132 CCCryptorRelease(cc
->href
);
137 init_cc_key(int encp
, CCAlgorithm alg
, CCOptions opts
, const void *key
,
138 size_t keylen
, const void *iv
, CCCryptorRef
*ref
)
140 CCOperation op
= encp
? kCCEncrypt
: kCCDecrypt
;
144 if (key
== NULL
&& iv
) {
145 CCCryptorReset(*ref
, iv
);
148 CCCryptorRelease(*ref
);
151 ret
= CCCryptorCreate(op
, alg
, opts
, key
, keylen
, iv
, ref
);
158 cc_des_ede3_cbc_init(EVP_CIPHER_CTX
*ctx
,
159 const unsigned char * key
,
160 const unsigned char * iv
,
163 struct cc_key
*cc
= ctx
->cipher_data
;
164 return init_cc_key(encp
, kCCAlgorithm3DES
, 0, key
, kCCKeySize3DES
, iv
, &cc
->href
);
167 #endif /* HAVE_COMMONCRYPTO_COMMONCRYPTOR_H */
170 * The tripple DES cipher type (Apple CommonCrypto provider)
172 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
174 * @ingroup hcrypto_evp
178 EVP_cc_des_ede3_cbc(void)
180 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
181 static const EVP_CIPHER des_ede3_cbc
= {
186 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
187 cc_des_ede3_cbc_init
,
190 sizeof(struct cc_key
),
196 return &des_ede3_cbc
;
202 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
208 cc_des_cbc_init(EVP_CIPHER_CTX
*ctx
,
209 const unsigned char * key
,
210 const unsigned char * iv
,
213 struct cc_key
*cc
= ctx
->cipher_data
;
214 return init_cc_key(encp
, kCCAlgorithmDES
, 0, key
, kCCBlockSizeDES
, iv
, &cc
->href
);
219 * The DES cipher type (Apple CommonCrypto provider)
221 * @return the DES-CBC EVP_CIPHER pointer.
223 * @ingroup hcrypto_evp
229 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
230 static const EVP_CIPHER des_ede3_cbc
= {
235 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
239 sizeof(struct cc_key
),
245 return &des_ede3_cbc
;
251 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
257 cc_aes_cbc_init(EVP_CIPHER_CTX
*ctx
,
258 const unsigned char * key
,
259 const unsigned char * iv
,
262 struct cc_key
*cc
= ctx
->cipher_data
;
263 return init_cc_key(encp
, kCCAlgorithmAES128
, 0, key
, ctx
->cipher
->key_len
, iv
, &cc
->href
);
268 * The AES-128 cipher type (Apple CommonCrypto provider)
270 * @return the AES-128-CBC EVP_CIPHER pointer.
272 * @ingroup hcrypto_evp
276 EVP_cc_aes_128_cbc(void)
278 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
279 static const EVP_CIPHER c
= {
284 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
288 sizeof(struct cc_key
),
301 * The AES-192 cipher type (Apple CommonCrypto provider)
303 * @return the AES-192-CBC EVP_CIPHER pointer.
305 * @ingroup hcrypto_evp
309 EVP_cc_aes_192_cbc(void)
311 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
312 static const EVP_CIPHER c
= {
317 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
321 sizeof(struct cc_key
),
334 * The AES-256 cipher type (Apple CommonCrypto provider)
336 * @return the AES-256-CBC EVP_CIPHER pointer.
338 * @ingroup hcrypto_evp
342 EVP_cc_aes_256_cbc(void)
344 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
345 static const EVP_CIPHER c
= {
350 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
354 sizeof(struct cc_key
),
366 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
372 cc_aes_cfb8_init(EVP_CIPHER_CTX
*ctx
,
373 const unsigned char * key
,
374 const unsigned char * iv
,
377 struct cc_key
*cc
= ctx
->cipher_data
;
378 memcpy(ctx
->iv
, iv
, ctx
->cipher
->iv_len
);
379 return init_cc_key(1, kCCAlgorithmAES128
, kCCOptionECBMode
,
380 key
, ctx
->cipher
->key_len
, NULL
, &cc
->href
);
385 * The AES-128 CFB8 cipher type (Apple CommonCrypto provider)
387 * @return the AES-128-CFB8 EVP_CIPHER pointer.
389 * @ingroup hcrypto_evp
393 EVP_cc_aes_128_cfb8(void)
395 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
396 static const EVP_CIPHER c
= {
401 EVP_CIPH_CFB8_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
405 sizeof(struct cc_key
),
418 * The AES-192 CFB8 cipher type (Apple CommonCrypto provider)
420 * @return the AES-192-CFB8 EVP_CIPHER pointer.
422 * @ingroup hcrypto_evp
426 EVP_cc_aes_192_cfb8(void)
428 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
429 static const EVP_CIPHER c
= {
434 EVP_CIPH_CFB8_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
438 sizeof(struct cc_key
),
451 * The AES-256 CFB8 cipher type (Apple CommonCrypto provider)
453 * @return the AES-256-CFB8 EVP_CIPHER pointer.
455 * @ingroup hcrypto_evp
459 EVP_cc_aes_256_cfb8(void)
461 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
462 static const EVP_CIPHER c
= {
467 EVP_CIPH_CFB8_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
471 sizeof(struct cc_key
),
487 #ifdef COMMONCRYPTO_SUPPORTS_RC2
489 cc_rc2_cbc_init(EVP_CIPHER_CTX
*ctx
,
490 const unsigned char * key
,
491 const unsigned char * iv
,
494 struct cc_key
*cc
= ctx
->cipher_data
;
495 return init_cc_key(encp
, kCCAlgorithmRC2
, 0, 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
),
534 * The RC2-40 cipher type - common crypto
536 * @return the RC2-40 EVP_CIPHER pointer.
538 * @ingroup hcrypto_evp
543 EVP_cc_rc2_40_cbc(void)
545 #ifdef COMMONCRYPTO_SUPPORTS_RC2
546 static const EVP_CIPHER rc2_40_cbc
= {
551 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
555 sizeof(struct cc_key
),
569 * The RC2-64 cipher type - common crypto
571 * @return the RC2-64 EVP_CIPHER pointer.
573 * @ingroup hcrypto_evp
578 EVP_cc_rc2_64_cbc(void)
580 #ifdef COMMONCRYPTO_SUPPORTS_RC2
581 static const EVP_CIPHER rc2_64_cbc
= {
586 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
590 sizeof(struct cc_key
),
603 * The CommonCrypto md2 provider
605 * @ingroup hcrypto_evp
611 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
612 static const struct hc_evp_md md2
= {
613 CC_MD2_DIGEST_LENGTH
,
616 (hc_evp_md_init
)CC_MD2_Init
,
617 (hc_evp_md_update
)CC_MD2_Update
,
618 (hc_evp_md_final
)CC_MD2_Final
,
619 (hc_evp_md_cleanup
)NULL
628 * The CommonCrypto md4 provider
630 * @ingroup hcrypto_evp
636 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
637 static const struct hc_evp_md md4
= {
638 CC_MD4_DIGEST_LENGTH
,
641 (hc_evp_md_init
)CC_MD4_Init
,
642 (hc_evp_md_update
)CC_MD4_Update
,
643 (hc_evp_md_final
)CC_MD4_Final
,
644 (hc_evp_md_cleanup
)NULL
653 * The CommonCrypto md5 provider
655 * @ingroup hcrypto_evp
661 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
662 static const struct hc_evp_md md5
= {
663 CC_MD5_DIGEST_LENGTH
,
666 (hc_evp_md_init
)CC_MD5_Init
,
667 (hc_evp_md_update
)CC_MD5_Update
,
668 (hc_evp_md_final
)CC_MD5_Final
,
669 (hc_evp_md_cleanup
)NULL
678 * The CommonCrypto sha1 provider
680 * @ingroup hcrypto_evp
686 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
687 static const struct hc_evp_md sha1
= {
688 CC_SHA1_DIGEST_LENGTH
,
691 (hc_evp_md_init
)CC_SHA1_Init
,
692 (hc_evp_md_update
)CC_SHA1_Update
,
693 (hc_evp_md_final
)CC_SHA1_Final
,
694 (hc_evp_md_cleanup
)NULL
703 * The CommonCrypto sha256 provider
705 * @ingroup hcrypto_evp
711 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
712 static const struct hc_evp_md sha256
= {
713 CC_SHA256_DIGEST_LENGTH
,
714 CC_SHA256_BLOCK_BYTES
,
715 sizeof(CC_SHA256_CTX
),
716 (hc_evp_md_init
)CC_SHA256_Init
,
717 (hc_evp_md_update
)CC_SHA256_Update
,
718 (hc_evp_md_final
)CC_SHA256_Final
,
719 (hc_evp_md_cleanup
)NULL
728 * The Camellia-128 cipher type - CommonCrypto
730 * @return the Camellia-128 EVP_CIPHER pointer.
732 * @ingroup hcrypto_evp
736 EVP_cc_camellia_128_cbc(void)
742 * The Camellia-198 cipher type - CommonCrypto
744 * @return the Camellia-198 EVP_CIPHER pointer.
746 * @ingroup hcrypto_evp
750 EVP_cc_camellia_192_cbc(void)
756 * The Camellia-256 cipher type - CommonCrypto
758 * @return the Camellia-256 EVP_CIPHER pointer.
760 * @ingroup hcrypto_evp
764 EVP_cc_camellia_256_cbc(void)
769 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
776 cc_rc4_init(EVP_CIPHER_CTX
*ctx
,
777 const unsigned char * key
,
778 const unsigned char * iv
,
781 struct cc_key
*cc
= ctx
->cipher_data
;
782 return init_cc_key(encp
, kCCAlgorithmRC4
, 0, key
, ctx
->key_len
, iv
, &cc
->href
);
789 * The RC4 cipher type (Apple CommonCrypto provider)
791 * @return the RC4 EVP_CIPHER pointer.
793 * @ingroup hcrypto_evp
799 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
800 static const EVP_CIPHER rc4
= {
805 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,
809 sizeof(struct cc_key
),
823 * The RC4-40 cipher type (Apple CommonCrypto provider)
825 * @return the RC4 EVP_CIPHER pointer.
827 * @ingroup hcrypto_evp
833 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
834 static const EVP_CIPHER rc4_40
= {
839 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,
843 sizeof(struct cc_key
),
855 #endif /* __APPLE__ */