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
),
300 cc_rc2_cbc_init(EVP_CIPHER_CTX
*ctx
,
301 const unsigned char * key
,
302 const unsigned char * iv
,
305 struct cc_key
*cc
= ctx
->cipher_data
;
306 return init_cc_key(encp
, kCCAlgorithmRC2
, key
, ctx
->cipher
->key_len
, iv
, &cc
->href
);
310 * The RC2 cipher type - common crypto
312 * @return the RC2 EVP_CIPHER pointer.
314 * @ingroup hcrypto_evp
321 static const EVP_CIPHER rc2_cbc
= {
326 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
330 sizeof(struct cc_key
),
340 * The RC2-40 cipher type - common crypto
342 * @return the RC2-40 EVP_CIPHER pointer.
344 * @ingroup hcrypto_evp
349 EVP_cc_rc2_40_cbc(void)
351 static const EVP_CIPHER rc2_40_cbc
= {
356 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
360 sizeof(struct cc_key
),
371 * The RC2-64 cipher type - common crypto
373 * @return the RC2-64 EVP_CIPHER pointer.
375 * @ingroup hcrypto_evp
380 EVP_cc_rc2_64_cbc(void)
382 static const EVP_CIPHER rc2_64_cbc
= {
387 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
391 sizeof(struct cc_key
),
401 * The CommonCrypto md2 provider
403 * @ingroup hcrypto_evp
409 static const struct hc_evp_md md2
= {
410 CC_MD2_DIGEST_LENGTH
,
413 (hc_evp_md_init
)CC_MD2_Init
,
414 (hc_evp_md_update
)CC_MD2_Update
,
415 (hc_evp_md_final
)CC_MD2_Final
,
416 (hc_evp_md_cleanup
)NULL
422 * The CommonCrypto md4 provider
424 * @ingroup hcrypto_evp
430 static const struct hc_evp_md md4
= {
431 CC_MD4_DIGEST_LENGTH
,
434 (hc_evp_md_init
)CC_MD4_Init
,
435 (hc_evp_md_update
)CC_MD4_Update
,
436 (hc_evp_md_final
)CC_MD4_Final
,
437 (hc_evp_md_cleanup
)NULL
443 * The CommonCrypto md5 provider
445 * @ingroup hcrypto_evp
451 static const struct hc_evp_md md5
= {
452 CC_MD5_DIGEST_LENGTH
,
455 (hc_evp_md_init
)CC_MD5_Init
,
456 (hc_evp_md_update
)CC_MD5_Update
,
457 (hc_evp_md_final
)CC_MD5_Final
,
458 (hc_evp_md_cleanup
)NULL
464 * The CommonCrypto sha1 provider
466 * @ingroup hcrypto_evp
472 static const struct hc_evp_md sha1
= {
473 CC_SHA1_DIGEST_LENGTH
,
476 (hc_evp_md_init
)CC_SHA1_Init
,
477 (hc_evp_md_update
)CC_SHA1_Update
,
478 (hc_evp_md_final
)CC_SHA1_Final
,
479 (hc_evp_md_cleanup
)NULL
485 * The CommonCrypto sha256 provider
487 * @ingroup hcrypto_evp
493 static const struct hc_evp_md sha256
= {
494 CC_SHA256_DIGEST_LENGTH
,
495 CC_SHA256_BLOCK_BYTES
,
496 sizeof(CC_SHA256_CTX
),
497 (hc_evp_md_init
)CC_SHA256_Init
,
498 (hc_evp_md_update
)CC_SHA256_Update
,
499 (hc_evp_md_final
)CC_SHA256_Final
,
500 (hc_evp_md_cleanup
)NULL
506 * The Camellia-128 cipher type - CommonCrypto
508 * @return the Camellia-128 EVP_CIPHER pointer.
510 * @ingroup hcrypto_evp
514 EVP_cc_camellia_128_cbc(void)
520 * The Camellia-198 cipher type - CommonCrypto
522 * @return the Camellia-198 EVP_CIPHER pointer.
524 * @ingroup hcrypto_evp
528 EVP_cc_camellia_192_cbc(void)
534 * The Camellia-256 cipher type - CommonCrypto
536 * @return the Camellia-256 EVP_CIPHER pointer.
538 * @ingroup hcrypto_evp
542 EVP_cc_camellia_256_cbc(void)
552 cc_rc4_init(EVP_CIPHER_CTX
*ctx
,
553 const unsigned char * key
,
554 const unsigned char * iv
,
557 struct cc_key
*cc
= ctx
->cipher_data
;
558 return init_cc_key(encp
, kCCAlgorithmRC4
, key
, ctx
->key_len
, iv
, &cc
->href
);
562 * The RC4 cipher type (Apple CommonCrypto provider)
564 * @return the RC4 EVP_CIPHER pointer.
566 * @ingroup hcrypto_evp
572 static const EVP_CIPHER rc4
= {
577 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,
581 sizeof(struct cc_key
),
592 * The RC4-40 cipher type (Apple CommonCrypto provider)
594 * @return the RC4 EVP_CIPHER pointer.
596 * @ingroup hcrypto_evp
602 static const EVP_CIPHER rc4_40
= {
607 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,
611 sizeof(struct cc_key
),
620 #endif /* __APPLE__ */