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 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
47 #include <CommonCrypto/CommonDigest.h>
49 #include <CommonCrypto/CommonCryptor.h>
63 cc_do_cipher(EVP_CIPHER_CTX
*ctx
,
65 const unsigned char *in
,
68 struct cc_key
*cc
= ctx
->cipher_data
;
72 memcpy(out
, in
, size
);
74 ret
= CCCryptorUpdate(cc
->href
, in
, size
, out
, size
, &moved
);
85 cc_cleanup(EVP_CIPHER_CTX
*ctx
)
87 struct cc_key
*cc
= ctx
->cipher_data
;
89 CCCryptorRelease(cc
->href
);
94 init_cc_key(int encp
, CCAlgorithm alg
, const void *key
,
95 size_t keylen
, const void *iv
, CCCryptorRef
*ref
)
97 CCOperation op
= encp
? kCCEncrypt
: kCCDecrypt
;
101 if (key
== NULL
&& iv
) {
102 CCCryptorReset(*ref
, iv
);
105 CCCryptorRelease(*ref
);
108 ret
= CCCryptorCreate(op
, alg
, 0, key
, keylen
, iv
, ref
);
115 cc_des_ede3_cbc_init(EVP_CIPHER_CTX
*ctx
,
116 const unsigned char * key
,
117 const unsigned char * iv
,
120 struct cc_key
*cc
= ctx
->cipher_data
;
121 return init_cc_key(encp
, kCCAlgorithm3DES
, key
, kCCKeySize3DES
, iv
, &cc
->href
);
125 * The tripple DES cipher type (Apple CommonCrypto provider)
127 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
129 * @ingroup hcrypto_evp
133 EVP_cc_des_ede3_cbc(void)
135 static const EVP_CIPHER des_ede3_cbc
= {
140 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
141 cc_des_ede3_cbc_init
,
144 sizeof(struct cc_key
),
150 return &des_ede3_cbc
;
158 cc_des_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
, kCCAlgorithmDES
, key
, kCCBlockSizeDES
, iv
, &cc
->href
);
168 * The DES cipher type (Apple CommonCrypto provider)
170 * @return the DES-CBC EVP_CIPHER pointer.
172 * @ingroup hcrypto_evp
178 static const EVP_CIPHER des_ede3_cbc
= {
183 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
187 sizeof(struct cc_key
),
193 return &des_ede3_cbc
;
201 cc_aes_cbc_init(EVP_CIPHER_CTX
*ctx
,
202 const unsigned char * key
,
203 const unsigned char * iv
,
206 struct cc_key
*cc
= ctx
->cipher_data
;
207 return init_cc_key(encp
, kCCAlgorithmAES128
, key
, ctx
->cipher
->key_len
, iv
, &cc
->href
);
211 * The AES-128 cipher type (Apple CommonCrypto provider)
213 * @return the AES-128-CBC EVP_CIPHER pointer.
215 * @ingroup hcrypto_evp
219 EVP_cc_aes_128_cbc(void)
221 static const EVP_CIPHER c
= {
226 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
230 sizeof(struct cc_key
),
240 * The AES-192 cipher type (Apple CommonCrypto provider)
242 * @return the AES-192-CBC EVP_CIPHER pointer.
244 * @ingroup hcrypto_evp
248 EVP_cc_aes_192_cbc(void)
250 static const EVP_CIPHER c
= {
255 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
259 sizeof(struct cc_key
),
269 * The AES-256 cipher type (Apple CommonCrypto provider)
271 * @return the AES-256-CBC EVP_CIPHER pointer.
273 * @ingroup hcrypto_evp
277 EVP_cc_aes_256_cbc(void)
279 static const EVP_CIPHER c
= {
284 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
288 sizeof(struct cc_key
),
301 #ifdef COMMONCRYPTO_SUPPORTS_RC2
303 cc_rc2_cbc_init(EVP_CIPHER_CTX
*ctx
,
304 const unsigned char * key
,
305 const unsigned char * iv
,
308 struct cc_key
*cc
= ctx
->cipher_data
;
309 return init_cc_key(encp
, kCCAlgorithmRC2
, key
, ctx
->cipher
->key_len
, iv
, &cc
->href
);
314 * The RC2 cipher type - common crypto
316 * @return the RC2 EVP_CIPHER pointer.
318 * @ingroup hcrypto_evp
325 #ifdef COMMONCRYPTO_SUPPORTS_RC2
326 static const EVP_CIPHER rc2_cbc
= {
331 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
335 sizeof(struct cc_key
),
348 * The RC2-40 cipher type - common crypto
350 * @return the RC2-40 EVP_CIPHER pointer.
352 * @ingroup hcrypto_evp
357 EVP_cc_rc2_40_cbc(void)
359 #ifdef COMMONCRYPTO_SUPPORTS_RC2
360 static const EVP_CIPHER rc2_40_cbc
= {
365 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
369 sizeof(struct cc_key
),
383 * The RC2-64 cipher type - common crypto
385 * @return the RC2-64 EVP_CIPHER pointer.
387 * @ingroup hcrypto_evp
392 EVP_cc_rc2_64_cbc(void)
394 #ifdef COMMONCRYPTO_SUPPORTS_RC2
395 static const EVP_CIPHER rc2_64_cbc
= {
400 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
404 sizeof(struct cc_key
),
417 * The CommonCrypto md2 provider
419 * @ingroup hcrypto_evp
425 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
426 static const struct hc_evp_md md2
= {
427 CC_MD2_DIGEST_LENGTH
,
430 (hc_evp_md_init
)CC_MD2_Init
,
431 (hc_evp_md_update
)CC_MD2_Update
,
432 (hc_evp_md_final
)CC_MD2_Final
,
433 (hc_evp_md_cleanup
)NULL
442 * The CommonCrypto md4 provider
444 * @ingroup hcrypto_evp
450 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
451 static const struct hc_evp_md md4
= {
452 CC_MD4_DIGEST_LENGTH
,
455 (hc_evp_md_init
)CC_MD4_Init
,
456 (hc_evp_md_update
)CC_MD4_Update
,
457 (hc_evp_md_final
)CC_MD4_Final
,
458 (hc_evp_md_cleanup
)NULL
467 * The CommonCrypto md5 provider
469 * @ingroup hcrypto_evp
475 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
476 static const struct hc_evp_md md5
= {
477 CC_MD5_DIGEST_LENGTH
,
480 (hc_evp_md_init
)CC_MD5_Init
,
481 (hc_evp_md_update
)CC_MD5_Update
,
482 (hc_evp_md_final
)CC_MD5_Final
,
483 (hc_evp_md_cleanup
)NULL
492 * The CommonCrypto sha1 provider
494 * @ingroup hcrypto_evp
500 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
501 static const struct hc_evp_md sha1
= {
502 CC_SHA1_DIGEST_LENGTH
,
505 (hc_evp_md_init
)CC_SHA1_Init
,
506 (hc_evp_md_update
)CC_SHA1_Update
,
507 (hc_evp_md_final
)CC_SHA1_Final
,
508 (hc_evp_md_cleanup
)NULL
517 * The CommonCrypto sha256 provider
519 * @ingroup hcrypto_evp
525 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
526 static const struct hc_evp_md sha256
= {
527 CC_SHA256_DIGEST_LENGTH
,
528 CC_SHA256_BLOCK_BYTES
,
529 sizeof(CC_SHA256_CTX
),
530 (hc_evp_md_init
)CC_SHA256_Init
,
531 (hc_evp_md_update
)CC_SHA256_Update
,
532 (hc_evp_md_final
)CC_SHA256_Final
,
533 (hc_evp_md_cleanup
)NULL
542 * The Camellia-128 cipher type - CommonCrypto
544 * @return the Camellia-128 EVP_CIPHER pointer.
546 * @ingroup hcrypto_evp
550 EVP_cc_camellia_128_cbc(void)
556 * The Camellia-198 cipher type - CommonCrypto
558 * @return the Camellia-198 EVP_CIPHER pointer.
560 * @ingroup hcrypto_evp
564 EVP_cc_camellia_192_cbc(void)
570 * The Camellia-256 cipher type - CommonCrypto
572 * @return the Camellia-256 EVP_CIPHER pointer.
574 * @ingroup hcrypto_evp
578 EVP_cc_camellia_256_cbc(void)
588 cc_rc4_init(EVP_CIPHER_CTX
*ctx
,
589 const unsigned char * key
,
590 const unsigned char * iv
,
593 struct cc_key
*cc
= ctx
->cipher_data
;
594 return init_cc_key(encp
, kCCAlgorithmRC4
, key
, ctx
->key_len
, iv
, &cc
->href
);
598 * The RC4 cipher type (Apple CommonCrypto provider)
600 * @return the RC4 EVP_CIPHER pointer.
602 * @ingroup hcrypto_evp
608 static const EVP_CIPHER rc4
= {
613 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,
617 sizeof(struct cc_key
),
628 * The RC4-40 cipher type (Apple CommonCrypto provider)
630 * @return the RC4 EVP_CIPHER pointer.
632 * @ingroup hcrypto_evp
638 static const EVP_CIPHER rc4_40
= {
643 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,
647 sizeof(struct cc_key
),
656 #endif /* __APPLE__ */