1 /* $NetBSD: evp-cc.c,v 1.1.1.1 2011/04/13 18:14:49 elric Exp $ */
4 * Copyright (c) 2008 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the Institute nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 /* CommonCrypto provider */
44 #include <sys/types.h>
50 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
51 #include <CommonCrypto/CommonDigest.h>
53 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
54 #include <CommonCrypto/CommonCryptor.h>
64 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
71 cc_do_cipher(EVP_CIPHER_CTX
*ctx
,
73 const unsigned char *in
,
76 struct cc_key
*cc
= ctx
->cipher_data
;
80 memcpy(out
, in
, size
);
82 ret
= CCCryptorUpdate(cc
->href
, in
, size
, out
, size
, &moved
);
93 cc_do_cfb8_cipher(EVP_CIPHER_CTX
*ctx
,
95 const unsigned char *in
,
98 struct cc_key
*cc
= ctx
->cipher_data
;
103 for (i
= 0; i
< size
; i
++) {
104 unsigned char oiv
[EVP_MAX_IV_LENGTH
+ 1];
106 assert(ctx
->cipher
->iv_len
+ 1 <= sizeof(oiv
));
107 memcpy(oiv
, ctx
->iv
, ctx
->cipher
->iv_len
);
109 ret
= CCCryptorUpdate(cc
->href
, ctx
->iv
, ctx
->cipher
->iv_len
,
110 ctx
->iv
, ctx
->cipher
->iv_len
, &moved
);
114 if (moved
!= ctx
->cipher
->iv_len
)
118 oiv
[ctx
->cipher
->iv_len
] = in
[i
];
119 out
[i
] = in
[i
] ^ ctx
->iv
[0];
121 oiv
[ctx
->cipher
->iv_len
] = out
[i
];
123 memcpy(ctx
->iv
, &oiv
[1], ctx
->cipher
->iv_len
);
130 cc_cleanup(EVP_CIPHER_CTX
*ctx
)
132 struct cc_key
*cc
= ctx
->cipher_data
;
134 CCCryptorRelease(cc
->href
);
139 init_cc_key(int encp
, CCAlgorithm alg
, CCOptions opts
, const void *key
,
140 size_t keylen
, const void *iv
, CCCryptorRef
*ref
)
142 CCOperation op
= encp
? kCCEncrypt
: kCCDecrypt
;
146 if (key
== NULL
&& iv
) {
147 CCCryptorReset(*ref
, iv
);
150 CCCryptorRelease(*ref
);
153 ret
= CCCryptorCreate(op
, alg
, opts
, key
, keylen
, iv
, ref
);
160 cc_des_ede3_cbc_init(EVP_CIPHER_CTX
*ctx
,
161 const unsigned char * key
,
162 const unsigned char * iv
,
165 struct cc_key
*cc
= ctx
->cipher_data
;
166 return init_cc_key(encp
, kCCAlgorithm3DES
, 0, key
, kCCKeySize3DES
, iv
, &cc
->href
);
169 #endif /* HAVE_COMMONCRYPTO_COMMONCRYPTOR_H */
172 * The tripple DES cipher type (Apple CommonCrypto provider)
174 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
176 * @ingroup hcrypto_evp
180 EVP_cc_des_ede3_cbc(void)
182 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
183 static const EVP_CIPHER des_ede3_cbc
= {
188 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
189 cc_des_ede3_cbc_init
,
192 sizeof(struct cc_key
),
198 return &des_ede3_cbc
;
204 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
210 cc_des_cbc_init(EVP_CIPHER_CTX
*ctx
,
211 const unsigned char * key
,
212 const unsigned char * iv
,
215 struct cc_key
*cc
= ctx
->cipher_data
;
216 return init_cc_key(encp
, kCCAlgorithmDES
, 0, key
, kCCBlockSizeDES
, iv
, &cc
->href
);
221 * The DES cipher type (Apple CommonCrypto provider)
223 * @return the DES-CBC EVP_CIPHER pointer.
225 * @ingroup hcrypto_evp
231 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
232 static const EVP_CIPHER des_ede3_cbc
= {
237 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
241 sizeof(struct cc_key
),
247 return &des_ede3_cbc
;
253 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
259 cc_aes_cbc_init(EVP_CIPHER_CTX
*ctx
,
260 const unsigned char * key
,
261 const unsigned char * iv
,
264 struct cc_key
*cc
= ctx
->cipher_data
;
265 return init_cc_key(encp
, kCCAlgorithmAES128
, 0, key
, ctx
->cipher
->key_len
, iv
, &cc
->href
);
270 * The AES-128 cipher type (Apple CommonCrypto provider)
272 * @return the AES-128-CBC EVP_CIPHER pointer.
274 * @ingroup hcrypto_evp
278 EVP_cc_aes_128_cbc(void)
280 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
281 static const EVP_CIPHER c
= {
286 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
290 sizeof(struct cc_key
),
303 * The AES-192 cipher type (Apple CommonCrypto provider)
305 * @return the AES-192-CBC EVP_CIPHER pointer.
307 * @ingroup hcrypto_evp
311 EVP_cc_aes_192_cbc(void)
313 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
314 static const EVP_CIPHER c
= {
319 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
323 sizeof(struct cc_key
),
336 * The AES-256 cipher type (Apple CommonCrypto provider)
338 * @return the AES-256-CBC EVP_CIPHER pointer.
340 * @ingroup hcrypto_evp
344 EVP_cc_aes_256_cbc(void)
346 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
347 static const EVP_CIPHER c
= {
352 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
356 sizeof(struct cc_key
),
368 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
374 cc_aes_cfb8_init(EVP_CIPHER_CTX
*ctx
,
375 const unsigned char * key
,
376 const unsigned char * iv
,
379 struct cc_key
*cc
= ctx
->cipher_data
;
380 memcpy(ctx
->iv
, iv
, ctx
->cipher
->iv_len
);
381 return init_cc_key(1, kCCAlgorithmAES128
, kCCOptionECBMode
,
382 key
, ctx
->cipher
->key_len
, NULL
, &cc
->href
);
387 * The AES-128 CFB8 cipher type (Apple CommonCrypto provider)
389 * @return the AES-128-CFB8 EVP_CIPHER pointer.
391 * @ingroup hcrypto_evp
395 EVP_cc_aes_128_cfb8(void)
397 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
398 static const EVP_CIPHER c
= {
403 EVP_CIPH_CFB8_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
407 sizeof(struct cc_key
),
420 * The AES-192 CFB8 cipher type (Apple CommonCrypto provider)
422 * @return the AES-192-CFB8 EVP_CIPHER pointer.
424 * @ingroup hcrypto_evp
428 EVP_cc_aes_192_cfb8(void)
430 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
431 static const EVP_CIPHER c
= {
436 EVP_CIPH_CFB8_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
440 sizeof(struct cc_key
),
453 * The AES-256 CFB8 cipher type (Apple CommonCrypto provider)
455 * @return the AES-256-CFB8 EVP_CIPHER pointer.
457 * @ingroup hcrypto_evp
461 EVP_cc_aes_256_cfb8(void)
463 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
464 static const EVP_CIPHER c
= {
469 EVP_CIPH_CFB8_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
473 sizeof(struct cc_key
),
489 #ifdef COMMONCRYPTO_SUPPORTS_RC2
491 cc_rc2_cbc_init(EVP_CIPHER_CTX
*ctx
,
492 const unsigned char * key
,
493 const unsigned char * iv
,
496 struct cc_key
*cc
= ctx
->cipher_data
;
497 return init_cc_key(encp
, kCCAlgorithmRC2
, 0, key
, ctx
->cipher
->key_len
, iv
, &cc
->href
);
502 * The RC2 cipher type - common crypto
504 * @return the RC2 EVP_CIPHER pointer.
506 * @ingroup hcrypto_evp
513 #ifdef COMMONCRYPTO_SUPPORTS_RC2
514 static const EVP_CIPHER rc2_cbc
= {
519 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
523 sizeof(struct cc_key
),
536 * The RC2-40 cipher type - common crypto
538 * @return the RC2-40 EVP_CIPHER pointer.
540 * @ingroup hcrypto_evp
545 EVP_cc_rc2_40_cbc(void)
547 #ifdef COMMONCRYPTO_SUPPORTS_RC2
548 static const EVP_CIPHER rc2_40_cbc
= {
553 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
557 sizeof(struct cc_key
),
571 * The RC2-64 cipher type - common crypto
573 * @return the RC2-64 EVP_CIPHER pointer.
575 * @ingroup hcrypto_evp
580 EVP_cc_rc2_64_cbc(void)
582 #ifdef COMMONCRYPTO_SUPPORTS_RC2
583 static const EVP_CIPHER rc2_64_cbc
= {
588 EVP_CIPH_CBC_MODE
|EVP_CIPH_ALWAYS_CALL_INIT
,
592 sizeof(struct cc_key
),
605 * The CommonCrypto md2 provider
607 * @ingroup hcrypto_evp
613 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
614 static const struct hc_evp_md md2
= {
615 CC_MD2_DIGEST_LENGTH
,
618 (hc_evp_md_init
)CC_MD2_Init
,
619 (hc_evp_md_update
)CC_MD2_Update
,
620 (hc_evp_md_final
)CC_MD2_Final
,
621 (hc_evp_md_cleanup
)NULL
630 * The CommonCrypto md4 provider
632 * @ingroup hcrypto_evp
638 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
639 static const struct hc_evp_md md4
= {
640 CC_MD4_DIGEST_LENGTH
,
643 (hc_evp_md_init
)CC_MD4_Init
,
644 (hc_evp_md_update
)CC_MD4_Update
,
645 (hc_evp_md_final
)CC_MD4_Final
,
646 (hc_evp_md_cleanup
)NULL
655 * The CommonCrypto md5 provider
657 * @ingroup hcrypto_evp
663 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
664 static const struct hc_evp_md md5
= {
665 CC_MD5_DIGEST_LENGTH
,
668 (hc_evp_md_init
)CC_MD5_Init
,
669 (hc_evp_md_update
)CC_MD5_Update
,
670 (hc_evp_md_final
)CC_MD5_Final
,
671 (hc_evp_md_cleanup
)NULL
680 * The CommonCrypto sha1 provider
682 * @ingroup hcrypto_evp
688 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
689 static const struct hc_evp_md sha1
= {
690 CC_SHA1_DIGEST_LENGTH
,
693 (hc_evp_md_init
)CC_SHA1_Init
,
694 (hc_evp_md_update
)CC_SHA1_Update
,
695 (hc_evp_md_final
)CC_SHA1_Final
,
696 (hc_evp_md_cleanup
)NULL
705 * The CommonCrypto sha256 provider
707 * @ingroup hcrypto_evp
713 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
714 static const struct hc_evp_md sha256
= {
715 CC_SHA256_DIGEST_LENGTH
,
716 CC_SHA256_BLOCK_BYTES
,
717 sizeof(CC_SHA256_CTX
),
718 (hc_evp_md_init
)CC_SHA256_Init
,
719 (hc_evp_md_update
)CC_SHA256_Update
,
720 (hc_evp_md_final
)CC_SHA256_Final
,
721 (hc_evp_md_cleanup
)NULL
730 * The Camellia-128 cipher type - CommonCrypto
732 * @return the Camellia-128 EVP_CIPHER pointer.
734 * @ingroup hcrypto_evp
738 EVP_cc_camellia_128_cbc(void)
744 * The Camellia-198 cipher type - CommonCrypto
746 * @return the Camellia-198 EVP_CIPHER pointer.
748 * @ingroup hcrypto_evp
752 EVP_cc_camellia_192_cbc(void)
758 * The Camellia-256 cipher type - CommonCrypto
760 * @return the Camellia-256 EVP_CIPHER pointer.
762 * @ingroup hcrypto_evp
766 EVP_cc_camellia_256_cbc(void)
771 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
778 cc_rc4_init(EVP_CIPHER_CTX
*ctx
,
779 const unsigned char * key
,
780 const unsigned char * iv
,
783 struct cc_key
*cc
= ctx
->cipher_data
;
784 return init_cc_key(encp
, kCCAlgorithmRC4
, 0, key
, ctx
->key_len
, iv
, &cc
->href
);
791 * The RC4 cipher type (Apple CommonCrypto provider)
793 * @return the RC4 EVP_CIPHER pointer.
795 * @ingroup hcrypto_evp
801 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
802 static const EVP_CIPHER rc4
= {
807 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,
811 sizeof(struct cc_key
),
825 * The RC4-40 cipher type (Apple CommonCrypto provider)
827 * @return the RC4 EVP_CIPHER pointer.
829 * @ingroup hcrypto_evp
835 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
836 static const EVP_CIPHER rc4_40
= {
841 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,
845 sizeof(struct cc_key
),
857 #endif /* __APPLE__ */