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);
124 cc_cleanup(EVP_CIPHER_CTX *ctx)
126 struct cc_key *cc = ctx->cipher_data;
128 CCCryptorRelease(cc->href);
133 init_cc_key(int encp, CCAlgorithm alg, CCOptions opts, const void *key,
134 size_t keylen, const void *iv, CCCryptorRef *ref)
136 CCOperation op = encp ? kCCEncrypt : kCCDecrypt;
140 if (key == NULL && iv) {
141 CCCryptorReset(*ref, iv);
144 CCCryptorRelease(*ref);
147 ret = CCCryptorCreate(op, alg, opts, key, keylen, iv, ref);
154 cc_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
155 const unsigned char * key,
156 const unsigned char * iv,
159 struct cc_key *cc = ctx->cipher_data;
160 return init_cc_key(encp, kCCAlgorithm3DES, 0, key, kCCKeySize3DES, iv, &cc->href);
164 * The tripple DES cipher type (Apple CommonCrypto provider)
166 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
168 * @ingroup hcrypto_evp
172 EVP_cc_des_ede3_cbc(void)
174 static const EVP_CIPHER des_ede3_cbc = {
179 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
180 cc_des_ede3_cbc_init,
183 sizeof(struct cc_key),
189 return &des_ede3_cbc;
197 cc_des_cbc_init(EVP_CIPHER_CTX *ctx,
198 const unsigned char * key,
199 const unsigned char * iv,
202 struct cc_key *cc = ctx->cipher_data;
203 return init_cc_key(encp, kCCAlgorithmDES, 0, key, kCCBlockSizeDES, iv, &cc->href);
207 * The DES cipher type (Apple CommonCrypto provider)
209 * @return the DES-CBC EVP_CIPHER pointer.
211 * @ingroup hcrypto_evp
217 static const EVP_CIPHER des_ede3_cbc = {
222 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
226 sizeof(struct cc_key),
232 return &des_ede3_cbc;
240 cc_aes_cbc_init(EVP_CIPHER_CTX *ctx,
241 const unsigned char * key,
242 const unsigned char * iv,
245 struct cc_key *cc = ctx->cipher_data;
246 return init_cc_key(encp, kCCAlgorithmAES128, 0, key, ctx->cipher->key_len, iv, &cc->href);
250 * The AES-128 cipher type (Apple CommonCrypto provider)
252 * @return the AES-128-CBC EVP_CIPHER pointer.
254 * @ingroup hcrypto_evp
258 EVP_cc_aes_128_cbc(void)
260 static const EVP_CIPHER c = {
265 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
269 sizeof(struct cc_key),
279 * The AES-192 cipher type (Apple CommonCrypto provider)
281 * @return the AES-192-CBC EVP_CIPHER pointer.
283 * @ingroup hcrypto_evp
287 EVP_cc_aes_192_cbc(void)
289 static const EVP_CIPHER c = {
294 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
298 sizeof(struct cc_key),
308 * The AES-256 cipher type (Apple CommonCrypto provider)
310 * @return the AES-256-CBC EVP_CIPHER pointer.
312 * @ingroup hcrypto_evp
316 EVP_cc_aes_256_cbc(void)
318 static const EVP_CIPHER c = {
323 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
327 sizeof(struct cc_key),
341 cc_aes_cfb8_init(EVP_CIPHER_CTX *ctx,
342 const unsigned char * key,
343 const unsigned char * iv,
346 struct cc_key *cc = ctx->cipher_data;
347 memcpy(ctx->iv, iv, ctx->cipher->iv_len);
348 return init_cc_key(1, kCCAlgorithmAES128, kCCOptionECBMode,
349 key, ctx->cipher->key_len, NULL, &cc->href);
353 * The AES-128 CFB8 cipher type (Apple CommonCrypto provider)
355 * @return the AES-128-CFB8 EVP_CIPHER pointer.
357 * @ingroup hcrypto_evp
361 EVP_cc_aes_128_cfb8(void)
363 static const EVP_CIPHER c = {
368 EVP_CIPH_CFB8_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
372 sizeof(struct cc_key),
382 * The AES-192 CFB8 cipher type (Apple CommonCrypto provider)
384 * @return the AES-192-CFB8 EVP_CIPHER pointer.
386 * @ingroup hcrypto_evp
390 EVP_cc_aes_192_cfb8(void)
392 static const EVP_CIPHER c = {
397 EVP_CIPH_CFB8_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
401 sizeof(struct cc_key),
411 * The AES-256 CFB8 cipher type (Apple CommonCrypto provider)
413 * @return the AES-256-CFB8 EVP_CIPHER pointer.
415 * @ingroup hcrypto_evp
419 EVP_cc_aes_256_cfb8(void)
421 static const EVP_CIPHER c = {
426 EVP_CIPH_CFB8_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
430 sizeof(struct cc_key),
443 #ifdef COMMONCRYPTO_SUPPORTS_RC2
445 cc_rc2_cbc_init(EVP_CIPHER_CTX *ctx,
446 const unsigned char * key,
447 const unsigned char * iv,
450 struct cc_key *cc = ctx->cipher_data;
451 return init_cc_key(encp, kCCAlgorithmRC2, 0, key, ctx->cipher->key_len, iv, &cc->href);
456 * The RC2 cipher type - common crypto
458 * @return the RC2 EVP_CIPHER pointer.
460 * @ingroup hcrypto_evp
467 #ifdef COMMONCRYPTO_SUPPORTS_RC2
468 static const EVP_CIPHER rc2_cbc = {
473 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
477 sizeof(struct cc_key),
490 * The RC2-40 cipher type - common crypto
492 * @return the RC2-40 EVP_CIPHER pointer.
494 * @ingroup hcrypto_evp
499 EVP_cc_rc2_40_cbc(void)
501 #ifdef COMMONCRYPTO_SUPPORTS_RC2
502 static const EVP_CIPHER rc2_40_cbc = {
507 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
511 sizeof(struct cc_key),
525 * The RC2-64 cipher type - common crypto
527 * @return the RC2-64 EVP_CIPHER pointer.
529 * @ingroup hcrypto_evp
534 EVP_cc_rc2_64_cbc(void)
536 #ifdef COMMONCRYPTO_SUPPORTS_RC2
537 static const EVP_CIPHER rc2_64_cbc = {
542 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
546 sizeof(struct cc_key),
559 * The CommonCrypto md2 provider
561 * @ingroup hcrypto_evp
567 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
568 static const struct hc_evp_md md2 = {
569 CC_MD2_DIGEST_LENGTH,
572 (hc_evp_md_init)CC_MD2_Init,
573 (hc_evp_md_update)CC_MD2_Update,
574 (hc_evp_md_final)CC_MD2_Final,
575 (hc_evp_md_cleanup)NULL
584 * The CommonCrypto md4 provider
586 * @ingroup hcrypto_evp
592 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
593 static const struct hc_evp_md md4 = {
594 CC_MD4_DIGEST_LENGTH,
597 (hc_evp_md_init)CC_MD4_Init,
598 (hc_evp_md_update)CC_MD4_Update,
599 (hc_evp_md_final)CC_MD4_Final,
600 (hc_evp_md_cleanup)NULL
609 * The CommonCrypto md5 provider
611 * @ingroup hcrypto_evp
617 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
618 static const struct hc_evp_md md5 = {
619 CC_MD5_DIGEST_LENGTH,
622 (hc_evp_md_init)CC_MD5_Init,
623 (hc_evp_md_update)CC_MD5_Update,
624 (hc_evp_md_final)CC_MD5_Final,
625 (hc_evp_md_cleanup)NULL
634 * The CommonCrypto sha1 provider
636 * @ingroup hcrypto_evp
642 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
643 static const struct hc_evp_md sha1 = {
644 CC_SHA1_DIGEST_LENGTH,
647 (hc_evp_md_init)CC_SHA1_Init,
648 (hc_evp_md_update)CC_SHA1_Update,
649 (hc_evp_md_final)CC_SHA1_Final,
650 (hc_evp_md_cleanup)NULL
659 * The CommonCrypto sha256 provider
661 * @ingroup hcrypto_evp
667 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
668 static const struct hc_evp_md sha256 = {
669 CC_SHA256_DIGEST_LENGTH,
670 CC_SHA256_BLOCK_BYTES,
671 sizeof(CC_SHA256_CTX),
672 (hc_evp_md_init)CC_SHA256_Init,
673 (hc_evp_md_update)CC_SHA256_Update,
674 (hc_evp_md_final)CC_SHA256_Final,
675 (hc_evp_md_cleanup)NULL
684 * The Camellia-128 cipher type - CommonCrypto
686 * @return the Camellia-128 EVP_CIPHER pointer.
688 * @ingroup hcrypto_evp
692 EVP_cc_camellia_128_cbc(void)
698 * The Camellia-198 cipher type - CommonCrypto
700 * @return the Camellia-198 EVP_CIPHER pointer.
702 * @ingroup hcrypto_evp
706 EVP_cc_camellia_192_cbc(void)
712 * The Camellia-256 cipher type - CommonCrypto
714 * @return the Camellia-256 EVP_CIPHER pointer.
716 * @ingroup hcrypto_evp
720 EVP_cc_camellia_256_cbc(void)
730 cc_rc4_init(EVP_CIPHER_CTX *ctx,
731 const unsigned char * key,
732 const unsigned char * iv,
735 struct cc_key *cc = ctx->cipher_data;
736 return init_cc_key(encp, kCCAlgorithmRC4, 0, key, ctx->key_len, iv, &cc->href);
740 * The RC4 cipher type (Apple CommonCrypto provider)
742 * @return the RC4 EVP_CIPHER pointer.
744 * @ingroup hcrypto_evp
750 static const EVP_CIPHER rc4 = {
755 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
759 sizeof(struct cc_key),
770 * The RC4-40 cipher type (Apple CommonCrypto provider)
772 * @return the RC4 EVP_CIPHER pointer.
774 * @ingroup hcrypto_evp
780 static const EVP_CIPHER rc4_40 = {
785 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
789 sizeof(struct cc_key),
798 #endif /* __APPLE__ */