2 * Copyright (c) 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 /* CommonCrypto provider */
40 #include <sys/types.h>
46 #include <CommonCrypto/CommonDigest.h>
47 #include <CommonCrypto/CommonCryptor.h>
61 cc_do_cipher(EVP_CIPHER_CTX
*ctx
,
63 const unsigned char *in
,
66 struct cc_key
*cc
= ctx
->cipher_data
;
70 memcpy(out
, in
, size
);
72 ret
= CCCryptorUpdate(cc
->href
, in
, size
, out
, size
, &moved
);
83 cc_cleanup(EVP_CIPHER_CTX
*ctx
)
85 struct cc_key
*cc
= ctx
->cipher_data
;
87 CCCryptorRelease(cc
->href
);
92 init_cc_key(int encp
, CCAlgorithm alg
, const void *key
,
93 size_t keylen
, const void *iv
, CCCryptorRef
*ref
)
95 CCOperation op
= encp
? kCCEncrypt
: kCCDecrypt
;
99 if (key
== NULL
&& iv
) {
100 CCCryptorReset(*ref
, iv
);
103 CCCryptorRelease(*ref
);
106 ret
= CCCryptorCreate(op
, alg
, 0, key
, keylen
, iv
, ref
);
113 cc_des_ede3_cbc_init(EVP_CIPHER_CTX
*ctx
,
114 const unsigned char * key
,
115 const unsigned char * iv
,
118 struct cc_key
*cc
= ctx
->cipher_data
;
119 return init_cc_key(encp
, kCCAlgorithm3DES
, key
, kCCKeySize3DES
, iv
, &cc
->href
);
123 * The tripple DES cipher type (Apple CommonCrypto provider)
125 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
127 * @ingroup hcrypto_evp
131 EVP_cc_des_ede3_cbc(void)
133 static const EVP_CIPHER des_ede3_cbc
= {
138 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
139 cc_des_ede3_cbc_init
,
142 sizeof(struct cc_key
),
148 return &des_ede3_cbc
;
156 cc_des_cbc_init(EVP_CIPHER_CTX
*ctx
,
157 const unsigned char * key
,
158 const unsigned char * iv
,
161 struct cc_key
*cc
= ctx
->cipher_data
;
162 return init_cc_key(encp
, kCCAlgorithmDES
, key
, kCCBlockSizeDES
, iv
, &cc
->href
);
166 * The DES cipher type (Apple CommonCrypto provider)
168 * @return the DES-CBC EVP_CIPHER pointer.
170 * @ingroup hcrypto_evp
176 static const EVP_CIPHER des_ede3_cbc
= {
181 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
185 sizeof(struct cc_key
),
191 return &des_ede3_cbc
;
199 cc_aes_cbc_init(EVP_CIPHER_CTX
*ctx
,
200 const unsigned char * key
,
201 const unsigned char * iv
,
204 struct cc_key
*cc
= ctx
->cipher_data
;
205 return init_cc_key(encp
, kCCAlgorithmAES128
, key
, ctx
->cipher
->key_len
, iv
, &cc
->href
);
209 * The AES-128 cipher type (Apple CommonCrypto provider)
211 * @return the AES-128-CBC EVP_CIPHER pointer.
213 * @ingroup hcrypto_evp
217 EVP_cc_aes_128_cbc(void)
219 static const EVP_CIPHER c
= {
224 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
228 sizeof(struct cc_key
),
238 * The AES-192 cipher type (Apple CommonCrypto provider)
240 * @return the AES-192-CBC EVP_CIPHER pointer.
242 * @ingroup hcrypto_evp
246 EVP_cc_aes_192_cbc(void)
248 static const EVP_CIPHER c
= {
253 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
257 sizeof(struct cc_key
),
267 * The AES-256 cipher type (Apple CommonCrypto provider)
269 * @return the AES-256-CBC EVP_CIPHER pointer.
271 * @ingroup hcrypto_evp
275 EVP_cc_aes_256_cbc(void)
277 static const EVP_CIPHER c
= {
282 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
286 sizeof(struct cc_key
),
299 #ifdef COMMONCRYPTO_SUPPORTS_RC2
301 cc_rc2_cbc_init(EVP_CIPHER_CTX
*ctx
,
302 const unsigned char * key
,
303 const unsigned char * iv
,
306 struct cc_key
*cc
= ctx
->cipher_data
;
307 return init_cc_key(encp
, kCCAlgorithmRC2
, key
, ctx
->cipher
->key_len
, iv
, &cc
->href
);
312 * The RC2 cipher type - common crypto
314 * @return the RC2 EVP_CIPHER pointer.
316 * @ingroup hcrypto_evp
323 #ifdef COMMONCRYPTO_SUPPORTS_RC2
324 static const EVP_CIPHER rc2_cbc
= {
329 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
333 sizeof(struct cc_key
),
346 * The RC2-40 cipher type - common crypto
348 * @return the RC2-40 EVP_CIPHER pointer.
350 * @ingroup hcrypto_evp
355 EVP_cc_rc2_40_cbc(void)
357 #ifdef COMMONCRYPTO_SUPPORTS_RC2
358 static const EVP_CIPHER rc2_40_cbc
= {
363 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
367 sizeof(struct cc_key
),
381 * The RC2-64 cipher type - common crypto
383 * @return the RC2-64 EVP_CIPHER pointer.
385 * @ingroup hcrypto_evp
390 EVP_cc_rc2_64_cbc(void)
392 #ifdef COMMONCRYPTO_SUPPORTS_RC2
393 static const EVP_CIPHER rc2_64_cbc
= {
398 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
402 sizeof(struct cc_key
),
415 * The CommonCrypto md2 provider
417 * @ingroup hcrypto_evp
423 static const struct hc_evp_md md2
= {
424 CC_MD2_DIGEST_LENGTH
,
427 (hc_evp_md_init
)CC_MD2_Init
,
428 (hc_evp_md_update
)CC_MD2_Update
,
429 (hc_evp_md_final
)CC_MD2_Final
,
430 (hc_evp_md_cleanup
)NULL
436 * The CommonCrypto md4 provider
438 * @ingroup hcrypto_evp
444 static const struct hc_evp_md md4
= {
445 CC_MD4_DIGEST_LENGTH
,
448 (hc_evp_md_init
)CC_MD4_Init
,
449 (hc_evp_md_update
)CC_MD4_Update
,
450 (hc_evp_md_final
)CC_MD4_Final
,
451 (hc_evp_md_cleanup
)NULL
457 * The CommonCrypto md5 provider
459 * @ingroup hcrypto_evp
465 static const struct hc_evp_md md5
= {
466 CC_MD5_DIGEST_LENGTH
,
469 (hc_evp_md_init
)CC_MD5_Init
,
470 (hc_evp_md_update
)CC_MD5_Update
,
471 (hc_evp_md_final
)CC_MD5_Final
,
472 (hc_evp_md_cleanup
)NULL
478 * The CommonCrypto sha1 provider
480 * @ingroup hcrypto_evp
486 static const struct hc_evp_md sha1
= {
487 CC_SHA1_DIGEST_LENGTH
,
490 (hc_evp_md_init
)CC_SHA1_Init
,
491 (hc_evp_md_update
)CC_SHA1_Update
,
492 (hc_evp_md_final
)CC_SHA1_Final
,
493 (hc_evp_md_cleanup
)NULL
499 * The CommonCrypto sha256 provider
501 * @ingroup hcrypto_evp
507 static const struct hc_evp_md sha256
= {
508 CC_SHA256_DIGEST_LENGTH
,
509 CC_SHA256_BLOCK_BYTES
,
510 sizeof(CC_SHA256_CTX
),
511 (hc_evp_md_init
)CC_SHA256_Init
,
512 (hc_evp_md_update
)CC_SHA256_Update
,
513 (hc_evp_md_final
)CC_SHA256_Final
,
514 (hc_evp_md_cleanup
)NULL
520 * The Camellia-128 cipher type - CommonCrypto
522 * @return the Camellia-128 EVP_CIPHER pointer.
524 * @ingroup hcrypto_evp
528 EVP_cc_camellia_128_cbc(void)
534 * The Camellia-198 cipher type - CommonCrypto
536 * @return the Camellia-198 EVP_CIPHER pointer.
538 * @ingroup hcrypto_evp
542 EVP_cc_camellia_192_cbc(void)
548 * The Camellia-256 cipher type - CommonCrypto
550 * @return the Camellia-256 EVP_CIPHER pointer.
552 * @ingroup hcrypto_evp
556 EVP_cc_camellia_256_cbc(void)
566 cc_rc4_init(EVP_CIPHER_CTX
*ctx
,
567 const unsigned char * key
,
568 const unsigned char * iv
,
571 struct cc_key
*cc
= ctx
->cipher_data
;
572 return init_cc_key(encp
, kCCAlgorithmRC4
, key
, ctx
->key_len
, iv
, &cc
->href
);
576 * The RC4 cipher type (Apple CommonCrypto provider)
578 * @return the RC4 EVP_CIPHER pointer.
580 * @ingroup hcrypto_evp
586 static const EVP_CIPHER rc4
= {
591 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,
595 sizeof(struct cc_key
),
606 * The RC4-40 cipher type (Apple CommonCrypto provider)
608 * @return the RC4 EVP_CIPHER pointer.
610 * @ingroup hcrypto_evp
616 static const EVP_CIPHER rc4_40
= {
621 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,
625 sizeof(struct cc_key
),
634 #endif /* __APPLE__ */