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 #include <CommonCrypto/CommonCryptor.h>
65 cc_do_cipher(EVP_CIPHER_CTX *ctx,
67 const unsigned char *in,
70 struct cc_key *cc = ctx->cipher_data;
74 memcpy(out, in, size);
76 ret = CCCryptorUpdate(cc->href, in, size, out, size, &moved);
87 cc_do_cfb8_cipher(EVP_CIPHER_CTX *ctx,
89 const unsigned char *in,
92 struct cc_key *cc = ctx->cipher_data;
97 for (i = 0; i < size; i++) {
98 unsigned char oiv[EVP_MAX_IV_LENGTH + 1];
100 assert(ctx->cipher->iv_len + 1 <= sizeof(oiv));
101 memcpy(oiv, ctx->iv, ctx->cipher->iv_len);
103 ret = CCCryptorUpdate(cc->href, ctx->iv, ctx->cipher->iv_len,
104 ctx->iv, ctx->cipher->iv_len, &moved);
108 if (moved != ctx->cipher->iv_len)
112 oiv[ctx->cipher->iv_len] = in[i];
113 out[i] = in[i] ^ ctx->iv[0];
115 oiv[ctx->cipher->iv_len] = out[i];
117 memcpy(ctx->iv, &oiv[1], ctx->cipher->iv_len);
125 cc_cleanup(EVP_CIPHER_CTX *ctx)
127 struct cc_key *cc = ctx->cipher_data;
129 CCCryptorRelease(cc->href);
134 init_cc_key(int encp, CCAlgorithm alg, CCOptions opts, const void *key,
135 size_t keylen, const void *iv, CCCryptorRef *ref)
137 CCOperation op = encp ? kCCEncrypt : kCCDecrypt;
141 if (key == NULL && iv) {
142 CCCryptorReset(*ref, iv);
145 CCCryptorRelease(*ref);
148 ret = CCCryptorCreate(op, alg, opts, key, keylen, iv, ref);
155 cc_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
156 const unsigned char * key,
157 const unsigned char * iv,
160 struct cc_key *cc = ctx->cipher_data;
161 return init_cc_key(encp, kCCAlgorithm3DES, 0, key, kCCKeySize3DES, iv, &cc->href);
165 * The tripple DES cipher type (Apple CommonCrypto provider)
167 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
169 * @ingroup hcrypto_evp
173 EVP_cc_des_ede3_cbc(void)
175 static const EVP_CIPHER des_ede3_cbc = {
180 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
181 cc_des_ede3_cbc_init,
184 sizeof(struct cc_key),
190 return &des_ede3_cbc;
198 cc_des_cbc_init(EVP_CIPHER_CTX *ctx,
199 const unsigned char * key,
200 const unsigned char * iv,
203 struct cc_key *cc = ctx->cipher_data;
204 return init_cc_key(encp, kCCAlgorithmDES, 0, key, kCCBlockSizeDES, iv, &cc->href);
208 * The DES cipher type (Apple CommonCrypto provider)
210 * @return the DES-CBC EVP_CIPHER pointer.
212 * @ingroup hcrypto_evp
218 static const EVP_CIPHER des_ede3_cbc = {
223 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
227 sizeof(struct cc_key),
233 return &des_ede3_cbc;
241 cc_aes_cbc_init(EVP_CIPHER_CTX *ctx,
242 const unsigned char * key,
243 const unsigned char * iv,
246 struct cc_key *cc = ctx->cipher_data;
247 return init_cc_key(encp, kCCAlgorithmAES128, 0, key, ctx->cipher->key_len, iv, &cc->href);
251 * The AES-128 cipher type (Apple CommonCrypto provider)
253 * @return the AES-128-CBC EVP_CIPHER pointer.
255 * @ingroup hcrypto_evp
259 EVP_cc_aes_128_cbc(void)
261 static const EVP_CIPHER c = {
266 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
270 sizeof(struct cc_key),
280 * The AES-192 cipher type (Apple CommonCrypto provider)
282 * @return the AES-192-CBC EVP_CIPHER pointer.
284 * @ingroup hcrypto_evp
288 EVP_cc_aes_192_cbc(void)
290 static const EVP_CIPHER c = {
295 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
299 sizeof(struct cc_key),
309 * The AES-256 cipher type (Apple CommonCrypto provider)
311 * @return the AES-256-CBC EVP_CIPHER pointer.
313 * @ingroup hcrypto_evp
317 EVP_cc_aes_256_cbc(void)
319 static const EVP_CIPHER c = {
324 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
328 sizeof(struct cc_key),
338 cc_aes_cfb8_init(EVP_CIPHER_CTX *ctx,
339 const unsigned char * key,
340 const unsigned char * iv,
343 struct cc_key *cc = ctx->cipher_data;
344 return init_cc_key(1, kCCAlgorithmAES128, kCCOptionECBMode,
345 key, ctx->cipher->key_len, NULL, &cc->href);
349 * The AES-128 CFB8 cipher type (Apple CommonCrypto provider)
351 * @return the AES-128-CFB8 EVP_CIPHER pointer.
353 * @ingroup hcrypto_evp
357 EVP_cc_aes_128_cfb8(void)
359 static const EVP_CIPHER c = {
368 sizeof(struct cc_key),
378 * The AES-192 CFB8 cipher type (Apple CommonCrypto provider)
380 * @return the AES-192-CFB8 EVP_CIPHER pointer.
382 * @ingroup hcrypto_evp
386 EVP_cc_aes_192_cfb8(void)
388 static const EVP_CIPHER c = {
397 sizeof(struct cc_key),
407 * The AES-256 CFB8 cipher type (Apple CommonCrypto provider)
409 * @return the AES-256-CFB8 EVP_CIPHER pointer.
411 * @ingroup hcrypto_evp
415 EVP_cc_aes_256_cfb8(void)
417 static const EVP_CIPHER c = {
426 sizeof(struct cc_key),
439 #ifdef COMMONCRYPTO_SUPPORTS_RC2
441 cc_rc2_cbc_init(EVP_CIPHER_CTX *ctx,
442 const unsigned char * key,
443 const unsigned char * iv,
446 struct cc_key *cc = ctx->cipher_data;
447 return init_cc_key(encp, kCCAlgorithmRC2, 0, key, ctx->cipher->key_len, iv, &cc->href);
452 * The RC2 cipher type - common crypto
454 * @return the RC2 EVP_CIPHER pointer.
456 * @ingroup hcrypto_evp
463 #ifdef COMMONCRYPTO_SUPPORTS_RC2
464 static const EVP_CIPHER rc2_cbc = {
469 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
473 sizeof(struct cc_key),
486 * The RC2-40 cipher type - common crypto
488 * @return the RC2-40 EVP_CIPHER pointer.
490 * @ingroup hcrypto_evp
495 EVP_cc_rc2_40_cbc(void)
497 #ifdef COMMONCRYPTO_SUPPORTS_RC2
498 static const EVP_CIPHER rc2_40_cbc = {
503 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
507 sizeof(struct cc_key),
521 * The RC2-64 cipher type - common crypto
523 * @return the RC2-64 EVP_CIPHER pointer.
525 * @ingroup hcrypto_evp
530 EVP_cc_rc2_64_cbc(void)
532 #ifdef COMMONCRYPTO_SUPPORTS_RC2
533 static const EVP_CIPHER rc2_64_cbc = {
538 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
542 sizeof(struct cc_key),
555 * The CommonCrypto md2 provider
557 * @ingroup hcrypto_evp
563 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
564 static const struct hc_evp_md md2 = {
565 CC_MD2_DIGEST_LENGTH,
568 (hc_evp_md_init)CC_MD2_Init,
569 (hc_evp_md_update)CC_MD2_Update,
570 (hc_evp_md_final)CC_MD2_Final,
571 (hc_evp_md_cleanup)NULL
580 * The CommonCrypto md4 provider
582 * @ingroup hcrypto_evp
588 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
589 static const struct hc_evp_md md4 = {
590 CC_MD4_DIGEST_LENGTH,
593 (hc_evp_md_init)CC_MD4_Init,
594 (hc_evp_md_update)CC_MD4_Update,
595 (hc_evp_md_final)CC_MD4_Final,
596 (hc_evp_md_cleanup)NULL
605 * The CommonCrypto md5 provider
607 * @ingroup hcrypto_evp
613 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
614 static const struct hc_evp_md md5 = {
615 CC_MD5_DIGEST_LENGTH,
618 (hc_evp_md_init)CC_MD5_Init,
619 (hc_evp_md_update)CC_MD5_Update,
620 (hc_evp_md_final)CC_MD5_Final,
621 (hc_evp_md_cleanup)NULL
630 * The CommonCrypto sha1 provider
632 * @ingroup hcrypto_evp
638 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
639 static const struct hc_evp_md sha1 = {
640 CC_SHA1_DIGEST_LENGTH,
643 (hc_evp_md_init)CC_SHA1_Init,
644 (hc_evp_md_update)CC_SHA1_Update,
645 (hc_evp_md_final)CC_SHA1_Final,
646 (hc_evp_md_cleanup)NULL
655 * The CommonCrypto sha256 provider
657 * @ingroup hcrypto_evp
663 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
664 static const struct hc_evp_md sha256 = {
665 CC_SHA256_DIGEST_LENGTH,
666 CC_SHA256_BLOCK_BYTES,
667 sizeof(CC_SHA256_CTX),
668 (hc_evp_md_init)CC_SHA256_Init,
669 (hc_evp_md_update)CC_SHA256_Update,
670 (hc_evp_md_final)CC_SHA256_Final,
671 (hc_evp_md_cleanup)NULL
680 * The Camellia-128 cipher type - CommonCrypto
682 * @return the Camellia-128 EVP_CIPHER pointer.
684 * @ingroup hcrypto_evp
688 EVP_cc_camellia_128_cbc(void)
694 * The Camellia-198 cipher type - CommonCrypto
696 * @return the Camellia-198 EVP_CIPHER pointer.
698 * @ingroup hcrypto_evp
702 EVP_cc_camellia_192_cbc(void)
708 * The Camellia-256 cipher type - CommonCrypto
710 * @return the Camellia-256 EVP_CIPHER pointer.
712 * @ingroup hcrypto_evp
716 EVP_cc_camellia_256_cbc(void)
726 cc_rc4_init(EVP_CIPHER_CTX *ctx,
727 const unsigned char * key,
728 const unsigned char * iv,
731 struct cc_key *cc = ctx->cipher_data;
732 return init_cc_key(encp, kCCAlgorithmRC4, 0, key, ctx->key_len, iv, &cc->href);
736 * The RC4 cipher type (Apple CommonCrypto provider)
738 * @return the RC4 EVP_CIPHER pointer.
740 * @ingroup hcrypto_evp
746 static const EVP_CIPHER rc4 = {
751 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
755 sizeof(struct cc_key),
766 * The RC4-40 cipher type (Apple CommonCrypto provider)
768 * @return the RC4 EVP_CIPHER pointer.
770 * @ingroup hcrypto_evp
776 static const EVP_CIPHER rc4_40 = {
781 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
785 sizeof(struct cc_key),
794 #endif /* __APPLE__ */