clarify documentation
[heimdal.git] / lib / hcrypto / evp-cc.c
blobbd084a25e20d4cdef1c9817e16a6c20bf11e3123
1 /*
2 * Copyright (c) 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
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
10 * are met:
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
33 * SUCH DAMAGE.
36 /* CommonCrypto provider */
38 #ifdef __APPLE__
40 #include "config.h"
42 #include <sys/types.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <assert.h>
48 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
49 #include <CommonCrypto/CommonDigest.h>
50 #endif
51 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
52 #include <CommonCrypto/CommonCryptor.h>
53 #endif
55 #include <evp.h>
56 #include <evp-cc.h>
62 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
64 struct cc_key {
65 CCCryptorRef href;
68 static int
69 cc_do_cipher(EVP_CIPHER_CTX *ctx,
70 unsigned char *out,
71 const unsigned char *in,
72 unsigned int size)
74 struct cc_key *cc = ctx->cipher_data;
75 CCCryptorStatus ret;
76 size_t moved;
78 memcpy(out, in, size);
80 ret = CCCryptorUpdate(cc->href, in, size, out, size, &moved);
81 if (ret)
82 return 0;
84 if (moved != size)
85 return 0;
87 return 1;
90 static int
91 cc_do_cfb8_cipher(EVP_CIPHER_CTX *ctx,
92 unsigned char *out,
93 const unsigned char *in,
94 unsigned int size)
96 struct cc_key *cc = ctx->cipher_data;
97 CCCryptorStatus ret;
98 size_t moved;
99 unsigned int i;
101 for (i = 0; i < size; i++) {
102 unsigned char oiv[EVP_MAX_IV_LENGTH + 1];
104 assert(ctx->cipher->iv_len + 1 <= sizeof(oiv));
105 memcpy(oiv, ctx->iv, ctx->cipher->iv_len);
107 ret = CCCryptorUpdate(cc->href, ctx->iv, ctx->cipher->iv_len,
108 ctx->iv, ctx->cipher->iv_len, &moved);
109 if (ret)
110 return 0;
112 if (moved != ctx->cipher->iv_len)
113 return 0;
115 if (!ctx->encrypt)
116 oiv[ctx->cipher->iv_len] = in[i];
117 out[i] = in[i] ^ ctx->iv[0];
118 if (ctx->encrypt)
119 oiv[ctx->cipher->iv_len] = out[i];
121 memcpy(ctx->iv, &oiv[1], ctx->cipher->iv_len);
124 return 1;
127 static int
128 cc_cleanup(EVP_CIPHER_CTX *ctx)
130 struct cc_key *cc = ctx->cipher_data;
131 if (cc->href)
132 CCCryptorRelease(cc->href);
133 return 1;
136 static int
137 init_cc_key(int encp, CCAlgorithm alg, CCOptions opts, const void *key,
138 size_t keylen, const void *iv, CCCryptorRef *ref)
140 CCOperation op = encp ? kCCEncrypt : kCCDecrypt;
141 CCCryptorStatus ret;
143 if (*ref) {
144 if (key == NULL && iv) {
145 CCCryptorReset(*ref, iv);
146 return 1;
148 CCCryptorRelease(*ref);
151 ret = CCCryptorCreate(op, alg, opts, key, keylen, iv, ref);
152 if (ret)
153 return 0;
154 return 1;
157 static int
158 cc_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
159 const unsigned char * key,
160 const unsigned char * iv,
161 int encp)
163 struct cc_key *cc = ctx->cipher_data;
164 return init_cc_key(encp, kCCAlgorithm3DES, 0, key, kCCKeySize3DES, iv, &cc->href);
167 #endif /* HAVE_COMMONCRYPTO_COMMONCRYPTOR_H */
170 * The tripple DES cipher type (Apple CommonCrypto provider)
172 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
174 * @ingroup hcrypto_evp
177 const EVP_CIPHER *
178 EVP_cc_des_ede3_cbc(void)
180 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
181 static const EVP_CIPHER des_ede3_cbc = {
186 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
187 cc_des_ede3_cbc_init,
188 cc_do_cipher,
189 cc_cleanup,
190 sizeof(struct cc_key),
191 NULL,
192 NULL,
193 NULL,
194 NULL
196 return &des_ede3_cbc;
197 #else
198 return NULL;
199 #endif
202 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
207 static int
208 cc_des_cbc_init(EVP_CIPHER_CTX *ctx,
209 const unsigned char * key,
210 const unsigned char * iv,
211 int encp)
213 struct cc_key *cc = ctx->cipher_data;
214 return init_cc_key(encp, kCCAlgorithmDES, 0, key, kCCBlockSizeDES, iv, &cc->href);
216 #endif
219 * The DES cipher type (Apple CommonCrypto provider)
221 * @return the DES-CBC EVP_CIPHER pointer.
223 * @ingroup hcrypto_evp
226 const EVP_CIPHER *
227 EVP_cc_des_cbc(void)
229 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
230 static const EVP_CIPHER des_ede3_cbc = {
232 kCCBlockSizeDES,
233 kCCBlockSizeDES,
234 kCCBlockSizeDES,
235 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
236 cc_des_cbc_init,
237 cc_do_cipher,
238 cc_cleanup,
239 sizeof(struct cc_key),
240 NULL,
241 NULL,
242 NULL,
243 NULL
245 return &des_ede3_cbc;
246 #else
247 return NULL;
248 #endif
251 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
256 static int
257 cc_aes_cbc_init(EVP_CIPHER_CTX *ctx,
258 const unsigned char * key,
259 const unsigned char * iv,
260 int encp)
262 struct cc_key *cc = ctx->cipher_data;
263 return init_cc_key(encp, kCCAlgorithmAES128, 0, key, ctx->cipher->key_len, iv, &cc->href);
265 #endif
268 * The AES-128 cipher type (Apple CommonCrypto provider)
270 * @return the AES-128-CBC EVP_CIPHER pointer.
272 * @ingroup hcrypto_evp
275 const EVP_CIPHER *
276 EVP_cc_aes_128_cbc(void)
278 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
279 static const EVP_CIPHER c = {
281 kCCBlockSizeAES128,
282 kCCKeySizeAES128,
283 kCCBlockSizeAES128,
284 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
285 cc_aes_cbc_init,
286 cc_do_cipher,
287 cc_cleanup,
288 sizeof(struct cc_key),
289 NULL,
290 NULL,
291 NULL,
292 NULL
294 return &c;
295 #else
296 return NULL;
297 #endif
301 * The AES-192 cipher type (Apple CommonCrypto provider)
303 * @return the AES-192-CBC EVP_CIPHER pointer.
305 * @ingroup hcrypto_evp
308 const EVP_CIPHER *
309 EVP_cc_aes_192_cbc(void)
311 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
312 static const EVP_CIPHER c = {
314 kCCBlockSizeAES128,
315 kCCKeySizeAES192,
316 kCCBlockSizeAES128,
317 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
318 cc_aes_cbc_init,
319 cc_do_cipher,
320 cc_cleanup,
321 sizeof(struct cc_key),
322 NULL,
323 NULL,
324 NULL,
325 NULL
327 return &c;
328 #else
329 return NULL;
330 #endif
334 * The AES-256 cipher type (Apple CommonCrypto provider)
336 * @return the AES-256-CBC EVP_CIPHER pointer.
338 * @ingroup hcrypto_evp
341 const EVP_CIPHER *
342 EVP_cc_aes_256_cbc(void)
344 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
345 static const EVP_CIPHER c = {
347 kCCBlockSizeAES128,
348 kCCKeySizeAES256,
349 kCCBlockSizeAES128,
350 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
351 cc_aes_cbc_init,
352 cc_do_cipher,
353 cc_cleanup,
354 sizeof(struct cc_key),
355 NULL,
356 NULL,
357 NULL,
358 NULL
360 return &c;
361 #else
362 return NULL;
363 #endif
366 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
371 static int
372 cc_aes_cfb8_init(EVP_CIPHER_CTX *ctx,
373 const unsigned char * key,
374 const unsigned char * iv,
375 int encp)
377 struct cc_key *cc = ctx->cipher_data;
378 memcpy(ctx->iv, iv, ctx->cipher->iv_len);
379 return init_cc_key(1, kCCAlgorithmAES128, kCCOptionECBMode,
380 key, ctx->cipher->key_len, NULL, &cc->href);
382 #endif
385 * The AES-128 CFB8 cipher type (Apple CommonCrypto provider)
387 * @return the AES-128-CFB8 EVP_CIPHER pointer.
389 * @ingroup hcrypto_evp
392 const EVP_CIPHER *
393 EVP_cc_aes_128_cfb8(void)
395 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
396 static const EVP_CIPHER c = {
399 kCCKeySizeAES128,
400 kCCBlockSizeAES128,
401 EVP_CIPH_CFB8_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
402 cc_aes_cfb8_init,
403 cc_do_cfb8_cipher,
404 cc_cleanup,
405 sizeof(struct cc_key),
406 NULL,
407 NULL,
408 NULL,
409 NULL
411 return &c;
412 #else
413 return NULL;
414 #endif
418 * The AES-192 CFB8 cipher type (Apple CommonCrypto provider)
420 * @return the AES-192-CFB8 EVP_CIPHER pointer.
422 * @ingroup hcrypto_evp
425 const EVP_CIPHER *
426 EVP_cc_aes_192_cfb8(void)
428 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
429 static const EVP_CIPHER c = {
432 kCCKeySizeAES192,
433 kCCBlockSizeAES128,
434 EVP_CIPH_CFB8_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
435 cc_aes_cfb8_init,
436 cc_do_cfb8_cipher,
437 cc_cleanup,
438 sizeof(struct cc_key),
439 NULL,
440 NULL,
441 NULL,
442 NULL
444 return &c;
445 #else
446 return NULL;
447 #endif
451 * The AES-256 CFB8 cipher type (Apple CommonCrypto provider)
453 * @return the AES-256-CFB8 EVP_CIPHER pointer.
455 * @ingroup hcrypto_evp
458 const EVP_CIPHER *
459 EVP_cc_aes_256_cfb8(void)
461 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
462 static const EVP_CIPHER c = {
464 kCCBlockSizeAES128,
465 kCCKeySizeAES256,
466 kCCBlockSizeAES128,
467 EVP_CIPH_CFB8_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
468 cc_aes_cfb8_init,
469 cc_do_cfb8_cipher,
470 cc_cleanup,
471 sizeof(struct cc_key),
472 NULL,
473 NULL,
474 NULL,
475 NULL
477 return &c;
478 #else
479 return NULL;
480 #endif
487 #ifdef COMMONCRYPTO_SUPPORTS_RC2
488 static int
489 cc_rc2_cbc_init(EVP_CIPHER_CTX *ctx,
490 const unsigned char * key,
491 const unsigned char * iv,
492 int encp)
494 struct cc_key *cc = ctx->cipher_data;
495 return init_cc_key(encp, kCCAlgorithmRC2, 0, key, ctx->cipher->key_len, iv, &cc->href);
497 #endif
500 * The RC2 cipher type - common crypto
502 * @return the RC2 EVP_CIPHER pointer.
504 * @ingroup hcrypto_evp
508 const EVP_CIPHER *
509 EVP_cc_rc2_cbc(void)
511 #ifdef COMMONCRYPTO_SUPPORTS_RC2
512 static const EVP_CIPHER rc2_cbc = {
514 kCCBlockSizeRC2,
516 kCCBlockSizeRC2,
517 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
518 cc_rc2_cbc_init,
519 cc_do_cipher,
520 cc_cleanup,
521 sizeof(struct cc_key),
522 NULL,
523 NULL,
524 NULL,
525 NULL
527 return &rc2_cbc;
528 #else
529 return NULL;
530 #endif
534 * The RC2-40 cipher type - common crypto
536 * @return the RC2-40 EVP_CIPHER pointer.
538 * @ingroup hcrypto_evp
542 const EVP_CIPHER *
543 EVP_cc_rc2_40_cbc(void)
545 #ifdef COMMONCRYPTO_SUPPORTS_RC2
546 static const EVP_CIPHER rc2_40_cbc = {
548 kCCBlockSizeRC2,
550 kCCBlockSizeRC2,
551 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
552 cc_rc2_cbc_init,
553 cc_do_cipher,
554 cc_cleanup,
555 sizeof(struct cc_key),
556 NULL,
557 NULL,
558 NULL,
559 NULL
561 return &rc2_40_cbc;
562 #else
563 return NULL;
564 #endif
569 * The RC2-64 cipher type - common crypto
571 * @return the RC2-64 EVP_CIPHER pointer.
573 * @ingroup hcrypto_evp
577 const EVP_CIPHER *
578 EVP_cc_rc2_64_cbc(void)
580 #ifdef COMMONCRYPTO_SUPPORTS_RC2
581 static const EVP_CIPHER rc2_64_cbc = {
583 kCCBlockSizeRC2,
585 kCCBlockSizeRC2,
586 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
587 cc_rc2_cbc_init,
588 cc_do_cipher,
589 cc_cleanup,
590 sizeof(struct cc_key),
591 NULL,
592 NULL,
593 NULL,
594 NULL
596 return &rc2_64_cbc;
597 #else
598 return NULL;
599 #endif
603 * The CommonCrypto md2 provider
605 * @ingroup hcrypto_evp
608 const EVP_MD *
609 EVP_cc_md2(void)
611 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
612 static const struct hc_evp_md md2 = {
613 CC_MD2_DIGEST_LENGTH,
614 CC_MD2_BLOCK_BYTES,
615 sizeof(CC_MD2_CTX),
616 (hc_evp_md_init)CC_MD2_Init,
617 (hc_evp_md_update)CC_MD2_Update,
618 (hc_evp_md_final)CC_MD2_Final,
619 (hc_evp_md_cleanup)NULL
621 return &md2;
622 #else
623 return NULL;
624 #endif
628 * The CommonCrypto md4 provider
630 * @ingroup hcrypto_evp
633 const EVP_MD *
634 EVP_cc_md4(void)
636 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
637 static const struct hc_evp_md md4 = {
638 CC_MD4_DIGEST_LENGTH,
639 CC_MD4_BLOCK_BYTES,
640 sizeof(CC_MD4_CTX),
641 (hc_evp_md_init)CC_MD4_Init,
642 (hc_evp_md_update)CC_MD4_Update,
643 (hc_evp_md_final)CC_MD4_Final,
644 (hc_evp_md_cleanup)NULL
646 return &md4;
647 #else
648 return NULL;
649 #endif
653 * The CommonCrypto md5 provider
655 * @ingroup hcrypto_evp
658 const EVP_MD *
659 EVP_cc_md5(void)
661 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
662 static const struct hc_evp_md md5 = {
663 CC_MD5_DIGEST_LENGTH,
664 CC_MD5_BLOCK_BYTES,
665 sizeof(CC_MD5_CTX),
666 (hc_evp_md_init)CC_MD5_Init,
667 (hc_evp_md_update)CC_MD5_Update,
668 (hc_evp_md_final)CC_MD5_Final,
669 (hc_evp_md_cleanup)NULL
671 return &md5;
672 #else
673 return NULL;
674 #endif
678 * The CommonCrypto sha1 provider
680 * @ingroup hcrypto_evp
683 const EVP_MD *
684 EVP_cc_sha1(void)
686 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
687 static const struct hc_evp_md sha1 = {
688 CC_SHA1_DIGEST_LENGTH,
689 CC_SHA1_BLOCK_BYTES,
690 sizeof(CC_SHA1_CTX),
691 (hc_evp_md_init)CC_SHA1_Init,
692 (hc_evp_md_update)CC_SHA1_Update,
693 (hc_evp_md_final)CC_SHA1_Final,
694 (hc_evp_md_cleanup)NULL
696 return &sha1;
697 #else
698 return NULL;
699 #endif
703 * The CommonCrypto sha256 provider
705 * @ingroup hcrypto_evp
708 const EVP_MD *
709 EVP_cc_sha256(void)
711 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
712 static const struct hc_evp_md sha256 = {
713 CC_SHA256_DIGEST_LENGTH,
714 CC_SHA256_BLOCK_BYTES,
715 sizeof(CC_SHA256_CTX),
716 (hc_evp_md_init)CC_SHA256_Init,
717 (hc_evp_md_update)CC_SHA256_Update,
718 (hc_evp_md_final)CC_SHA256_Final,
719 (hc_evp_md_cleanup)NULL
721 return &sha256;
722 #else
723 return NULL;
724 #endif
728 * The Camellia-128 cipher type - CommonCrypto
730 * @return the Camellia-128 EVP_CIPHER pointer.
732 * @ingroup hcrypto_evp
735 const EVP_CIPHER *
736 EVP_cc_camellia_128_cbc(void)
738 return NULL;
742 * The Camellia-198 cipher type - CommonCrypto
744 * @return the Camellia-198 EVP_CIPHER pointer.
746 * @ingroup hcrypto_evp
749 const EVP_CIPHER *
750 EVP_cc_camellia_192_cbc(void)
752 return NULL;
756 * The Camellia-256 cipher type - CommonCrypto
758 * @return the Camellia-256 EVP_CIPHER pointer.
760 * @ingroup hcrypto_evp
763 const EVP_CIPHER *
764 EVP_cc_camellia_256_cbc(void)
766 return NULL;
769 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
775 static int
776 cc_rc4_init(EVP_CIPHER_CTX *ctx,
777 const unsigned char * key,
778 const unsigned char * iv,
779 int encp)
781 struct cc_key *cc = ctx->cipher_data;
782 return init_cc_key(encp, kCCAlgorithmRC4, 0, key, ctx->key_len, iv, &cc->href);
785 #endif
789 * The RC4 cipher type (Apple CommonCrypto provider)
791 * @return the RC4 EVP_CIPHER pointer.
793 * @ingroup hcrypto_evp
796 const EVP_CIPHER *
797 EVP_cc_rc4(void)
799 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
800 static const EVP_CIPHER rc4 = {
805 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
806 cc_rc4_init,
807 cc_do_cipher,
808 cc_cleanup,
809 sizeof(struct cc_key),
810 NULL,
811 NULL,
812 NULL,
813 NULL
815 return &rc4;
816 #else
817 return NULL;
818 #endif
823 * The RC4-40 cipher type (Apple CommonCrypto provider)
825 * @return the RC4 EVP_CIPHER pointer.
827 * @ingroup hcrypto_evp
830 const EVP_CIPHER *
831 EVP_cc_rc4_40(void)
833 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
834 static const EVP_CIPHER rc4_40 = {
839 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
840 cc_rc4_init,
841 cc_do_cipher,
842 cc_cleanup,
843 sizeof(struct cc_key),
844 NULL,
845 NULL,
846 NULL,
847 NULL
849 return &rc4_40;
850 #else
851 return NULL;
852 #endif
855 #endif /* __APPLE__ */