1 /* $NetBSD: evp-cc.c,v 1.2 2017/01/28 21:31:47 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2008 Kungliga Tekniska Högskolan 5 * (Royal Institute of Technology, Stockholm, Sweden). 6 * All rights reserved. 7 * 8 * Portions Copyright (c) 2009 Apple Inc. All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 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. 20 * 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. 24 * 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 35 * SUCH DAMAGE. 36 */ 37 38 /* CommonCrypto provider */ 39 40 #ifdef __APPLE__ 41 42 #include <config.h> 43 #include <krb5/roken.h> 44 45 #include <assert.h> 46 47 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H 48 #include <CommonCrypto/CommonDigest.h> 49 #endif 50 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H 51 #include <CommonCrypto/CommonCryptor.h> 52 #endif 53 54 #include <evp.h> 55 #include <evp-hcrypto.h> 56 #include <evp-cc.h> 57 58 /* 59 * 60 */ 61 62 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H 63 64 struct cc_key { 65 CCCryptorRef href; 66 }; 67 68 static int 69 cc_do_cipher(EVP_CIPHER_CTX *ctx, 70 unsigned char *out, 71 const unsigned char *in, 72 unsigned int size) 73 { 74 struct cc_key *cc = ctx->cipher_data; 75 CCCryptorStatus ret; 76 size_t moved; 77 78 memcpy(out, in, size); 79 80 ret = CCCryptorUpdate(cc->href, in, size, out, size, &moved); 81 if (ret) 82 return 0; 83 84 if (moved != size) 85 return 0; 86 87 return 1; 88 } 89 90 static int 91 cc_cleanup(EVP_CIPHER_CTX *ctx) 92 { 93 struct cc_key *cc = ctx->cipher_data; 94 if (cc->href) 95 CCCryptorRelease(cc->href); 96 return 1; 97 } 98 99 static int 100 init_cc_key(int encp, unsigned long flags, 101 CCAlgorithm alg, const void *key, size_t keylen, 102 const void *iv, CCCryptorRef *ref) 103 { 104 CCOperation op = encp ? kCCEncrypt : kCCDecrypt; 105 CCMode mode; 106 CCModeOptions options = 0; 107 CCCryptorStatus ret; 108 109 if (*ref) { 110 if (key == NULL && iv) { 111 CCCryptorReset(*ref, iv); 112 return 1; 113 } 114 CCCryptorRelease(*ref); 115 } 116 117 if (key) { 118 switch (flags & EVP_CIPH_MODE) { 119 case EVP_CIPH_STREAM_CIPHER: 120 mode = kCCModeRC4; 121 break; 122 case EVP_CIPH_CFB8_MODE: 123 mode = kCCModeCFB8; 124 break; 125 default: 126 mode = kCCModeCBC; 127 break; 128 } 129 130 ret = CCCryptorCreateWithMode(op, mode, alg, ccNoPadding, 131 iv, key, keylen, NULL, 0, 0, 132 options, ref); 133 if (ret) 134 return 0; 135 } 136 137 return 1; 138 } 139 140 static int 141 cc_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx, 142 const unsigned char * key, 143 const unsigned char * iv, 144 int encp) 145 { 146 struct cc_key *cc = ctx->cipher_data; 147 return init_cc_key(encp, ctx->cipher->flags, kCCAlgorithm3DES, 148 key, kCCKeySize3DES, iv, &cc->href); 149 } 150 151 #endif /* HAVE_COMMONCRYPTO_COMMONCRYPTOR_H */ 152 153 /** 154 * The triple DES cipher type (Apple CommonCrypto provider) 155 * 156 * @return the DES-EDE3-CBC EVP_CIPHER pointer. 157 * 158 * @ingroup hcrypto_evp 159 */ 160 161 const EVP_CIPHER * 162 EVP_cc_des_ede3_cbc(void) 163 { 164 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H 165 static const EVP_CIPHER des_ede3_cbc = { 166 0, 167 8, 168 24, 169 8, 170 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT, 171 cc_des_ede3_cbc_init, 172 cc_do_cipher, 173 cc_cleanup, 174 sizeof(struct cc_key), 175 NULL, 176 NULL, 177 NULL, 178 NULL 179 }; 180 return &des_ede3_cbc; 181 #elif HCRYPTO_FALLBACK 182 return EVP_hcrypto_des_ede3_cbc(); 183 #else 184 return NULL; 185 #endif 186 } 187 188 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H 189 /* 190 * 191 */ 192 193 static int 194 cc_des_cbc_init(EVP_CIPHER_CTX *ctx, 195 const unsigned char * key, 196 const unsigned char * iv, 197 int encp) 198 { 199 struct cc_key *cc = ctx->cipher_data; 200 return init_cc_key(encp, ctx->cipher->flags, kCCAlgorithmDES, 201 key, kCCBlockSizeDES, iv, &cc->href); 202 } 203 #endif 204 205 /** 206 * The DES cipher type (Apple CommonCrypto provider) 207 * 208 * @return the DES-CBC EVP_CIPHER pointer. 209 * 210 * @ingroup hcrypto_evp 211 */ 212 213 const EVP_CIPHER * 214 EVP_cc_des_cbc(void) 215 { 216 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H 217 static const EVP_CIPHER des_ede3_cbc = { 218 0, 219 kCCBlockSizeDES, 220 kCCBlockSizeDES, 221 kCCBlockSizeDES, 222 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT, 223 cc_des_cbc_init, 224 cc_do_cipher, 225 cc_cleanup, 226 sizeof(struct cc_key), 227 NULL, 228 NULL, 229 NULL, 230 NULL 231 }; 232 return &des_ede3_cbc; 233 #elif HCRYPTO_FALLBACK 234 return EVP_hcrypto_des_cbc(); 235 #else 236 return NULL; 237 #endif 238 } 239 240 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H 241 /* 242 * 243 */ 244 245 static int 246 cc_aes_cbc_init(EVP_CIPHER_CTX *ctx, 247 const unsigned char * key, 248 const unsigned char * iv, 249 int encp) 250 { 251 struct cc_key *cc = ctx->cipher_data; 252 return init_cc_key(encp, ctx->cipher->flags, kCCAlgorithmAES128, 253 key, ctx->cipher->key_len, iv, &cc->href); 254 } 255 #endif 256 257 /** 258 * The AES-128 cipher type (Apple CommonCrypto provider) 259 * 260 * @return the AES-128-CBC EVP_CIPHER pointer. 261 * 262 * @ingroup hcrypto_evp 263 */ 264 265 const EVP_CIPHER * 266 EVP_cc_aes_128_cbc(void) 267 { 268 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H 269 static const EVP_CIPHER c = { 270 0, 271 kCCBlockSizeAES128, 272 kCCKeySizeAES128, 273 kCCBlockSizeAES128, 274 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT, 275 cc_aes_cbc_init, 276 cc_do_cipher, 277 cc_cleanup, 278 sizeof(struct cc_key), 279 NULL, 280 NULL, 281 NULL, 282 NULL 283 }; 284 return &c; 285 #elif HCRYPTO_FALLBACK 286 return EVP_hcrypto_aes_128_cbc(); 287 #else 288 return NULL; 289 #endif 290 } 291 292 /** 293 * The AES-192 cipher type (Apple CommonCrypto provider) 294 * 295 * @return the AES-192-CBC EVP_CIPHER pointer. 296 * 297 * @ingroup hcrypto_evp 298 */ 299 300 const EVP_CIPHER * 301 EVP_cc_aes_192_cbc(void) 302 { 303 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H 304 static const EVP_CIPHER c = { 305 0, 306 kCCBlockSizeAES128, 307 kCCKeySizeAES192, 308 kCCBlockSizeAES128, 309 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT, 310 cc_aes_cbc_init, 311 cc_do_cipher, 312 cc_cleanup, 313 sizeof(struct cc_key), 314 NULL, 315 NULL, 316 NULL, 317 NULL 318 }; 319 return &c; 320 #elif HCRYPTO_FALLBACK 321 return EVP_hcrypto_aes_192_cbc(); 322 #else 323 return NULL; 324 #endif 325 } 326 327 /** 328 * The AES-256 cipher type (Apple CommonCrypto provider) 329 * 330 * @return the AES-256-CBC EVP_CIPHER pointer. 331 * 332 * @ingroup hcrypto_evp 333 */ 334 335 const EVP_CIPHER * 336 EVP_cc_aes_256_cbc(void) 337 { 338 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H 339 static const EVP_CIPHER c = { 340 0, 341 kCCBlockSizeAES128, 342 kCCKeySizeAES256, 343 kCCBlockSizeAES128, 344 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT, 345 cc_aes_cbc_init, 346 cc_do_cipher, 347 cc_cleanup, 348 sizeof(struct cc_key), 349 NULL, 350 NULL, 351 NULL, 352 NULL 353 }; 354 return &c; 355 #elif HCRYPTO_FALLBACK 356 return EVP_hcrypto_aes_256_cbc(); 357 #else 358 return NULL; 359 #endif 360 } 361 362 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H 363 /* 364 * 365 */ 366 367 static int 368 cc_aes_cfb8_init(EVP_CIPHER_CTX *ctx, 369 const unsigned char * key, 370 const unsigned char * iv, 371 int encp) 372 { 373 struct cc_key *cc = ctx->cipher_data; 374 return init_cc_key(encp, ctx->cipher->flags, kCCAlgorithmAES128, 375 key, ctx->cipher->key_len, NULL, &cc->href); 376 } 377 #endif 378 379 /** 380 * The AES-128 CFB8 cipher type (Apple CommonCrypto provider) 381 * 382 * @return the AES-128-CFB8 EVP_CIPHER pointer. 383 * 384 * @ingroup hcrypto_evp 385 */ 386 387 const EVP_CIPHER * 388 EVP_cc_aes_128_cfb8(void) 389 { 390 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H 391 static const EVP_CIPHER c = { 392 0, 393 1, 394 kCCKeySizeAES128, 395 kCCBlockSizeAES128, 396 EVP_CIPH_CFB8_MODE|EVP_CIPH_ALWAYS_CALL_INIT, 397 cc_aes_cfb8_init, 398 cc_do_cipher, 399 cc_cleanup, 400 sizeof(struct cc_key), 401 NULL, 402 NULL, 403 NULL, 404 NULL 405 }; 406 return &c; 407 #elif HCRYPTO_FALLBACK 408 return EVP_hcrypto_aes_128_cfb8(); 409 #else 410 return NULL; 411 #endif 412 } 413 414 /** 415 * The AES-192 CFB8 cipher type (Apple CommonCrypto provider) 416 * 417 * @return the AES-192-CFB8 EVP_CIPHER pointer. 418 * 419 * @ingroup hcrypto_evp 420 */ 421 422 const EVP_CIPHER * 423 EVP_cc_aes_192_cfb8(void) 424 { 425 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H 426 static const EVP_CIPHER c = { 427 0, 428 1, 429 kCCKeySizeAES192, 430 kCCBlockSizeAES128, 431 EVP_CIPH_CFB8_MODE|EVP_CIPH_ALWAYS_CALL_INIT, 432 cc_aes_cfb8_init, 433 cc_do_cipher, 434 cc_cleanup, 435 sizeof(struct cc_key), 436 NULL, 437 NULL, 438 NULL, 439 NULL 440 }; 441 return &c; 442 #elif HCRYPTO_FALLBACK 443 return EVP_hcrypto_aes_192_cfb8(); 444 #else 445 return NULL; 446 #endif 447 } 448 449 /** 450 * The AES-256 CFB8 cipher type (Apple CommonCrypto provider) 451 * 452 * @return the AES-256-CFB8 EVP_CIPHER pointer. 453 * 454 * @ingroup hcrypto_evp 455 */ 456 457 const EVP_CIPHER * 458 EVP_cc_aes_256_cfb8(void) 459 { 460 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H 461 static const EVP_CIPHER c = { 462 0, 463 kCCBlockSizeAES128, 464 kCCKeySizeAES256, 465 kCCBlockSizeAES128, 466 EVP_CIPH_CFB8_MODE|EVP_CIPH_ALWAYS_CALL_INIT, 467 cc_aes_cfb8_init, 468 cc_do_cipher, 469 cc_cleanup, 470 sizeof(struct cc_key), 471 NULL, 472 NULL, 473 NULL, 474 NULL 475 }; 476 return &c; 477 #elif HCRYPTO_FALLBACK 478 return EVP_hcrypto_aes_256_cfb8(); 479 #else 480 return NULL; 481 #endif 482 } 483 484 /* 485 * 486 */ 487 488 #ifdef COMMONCRYPTO_SUPPORTS_RC2 489 static int 490 cc_rc2_cbc_init(EVP_CIPHER_CTX *ctx, 491 const unsigned char * key, 492 const unsigned char * iv, 493 int encp) 494 { 495 struct cc_key *cc = ctx->cipher_data; 496 return init_cc_key(encp, ctx->cipher->flags, kCCAlgorithmRC2, 497 key, ctx->cipher->key_len, iv, &cc->href); 498 } 499 #endif 500 501 /** 502 * The RC2 cipher type - common crypto 503 * 504 * @return the RC2 EVP_CIPHER pointer. 505 * 506 * @ingroup hcrypto_evp 507 */ 508 509 510 const EVP_CIPHER * 511 EVP_cc_rc2_cbc(void) 512 { 513 #ifdef COMMONCRYPTO_SUPPORTS_RC2 514 static const EVP_CIPHER rc2_cbc = { 515 0, 516 kCCBlockSizeRC2, 517 16, 518 kCCBlockSizeRC2, 519 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT, 520 cc_rc2_cbc_init, 521 cc_do_cipher, 522 cc_cleanup, 523 sizeof(struct cc_key), 524 NULL, 525 NULL, 526 NULL, 527 NULL 528 }; 529 return &rc2_cbc; 530 #elif HCRYPTO_FALLBACK 531 return EVP_hcrypto_rc2_cbc(); 532 #else 533 return NULL; 534 #endif 535 } 536 537 /** 538 * The RC2-40 cipher type - common crypto 539 * 540 * @return the RC2-40 EVP_CIPHER pointer. 541 * 542 * @ingroup hcrypto_evp 543 */ 544 545 546 const EVP_CIPHER * 547 EVP_cc_rc2_40_cbc(void) 548 { 549 #ifdef COMMONCRYPTO_SUPPORTS_RC2 550 static const EVP_CIPHER rc2_40_cbc = { 551 0, 552 kCCBlockSizeRC2, 553 5, 554 kCCBlockSizeRC2, 555 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT, 556 cc_rc2_cbc_init, 557 cc_do_cipher, 558 cc_cleanup, 559 sizeof(struct cc_key), 560 NULL, 561 NULL, 562 NULL, 563 NULL 564 }; 565 return &rc2_40_cbc; 566 #elif HCRYPTO_FALLBACK 567 return EVP_hcrypto_rc2_40_cbc(); 568 #else 569 return NULL; 570 #endif 571 } 572 573 574 /** 575 * The RC2-64 cipher type - common crypto 576 * 577 * @return the RC2-64 EVP_CIPHER pointer. 578 * 579 * @ingroup hcrypto_evp 580 */ 581 582 583 const EVP_CIPHER * 584 EVP_cc_rc2_64_cbc(void) 585 { 586 #ifdef COMMONCRYPTO_SUPPORTS_RC2 587 static const EVP_CIPHER rc2_64_cbc = { 588 0, 589 kCCBlockSizeRC2, 590 8, 591 kCCBlockSizeRC2, 592 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT, 593 cc_rc2_cbc_init, 594 cc_do_cipher, 595 cc_cleanup, 596 sizeof(struct cc_key), 597 NULL, 598 NULL, 599 NULL, 600 NULL 601 }; 602 return &rc2_64_cbc; 603 #elif HCRYPTO_FALLBACK 604 return EVP_hcrypto_rc2_64_cbc(); 605 #else 606 return NULL; 607 #endif 608 } 609 610 /** 611 * The CommonCrypto md2 provider 612 * 613 * @ingroup hcrypto_evp 614 */ 615 616 const EVP_MD * 617 EVP_cc_md2(void) 618 { 619 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H 620 static const struct hc_evp_md md2 = { 621 CC_MD2_DIGEST_LENGTH, 622 CC_MD2_BLOCK_BYTES, 623 sizeof(CC_MD2_CTX), 624 (hc_evp_md_init)CC_MD2_Init, 625 (hc_evp_md_update)CC_MD2_Update, 626 (hc_evp_md_final)CC_MD2_Final, 627 (hc_evp_md_cleanup)NULL 628 }; 629 return &md2; 630 #elif HCRYPTO_FALLBACK 631 return EVP_hcrypto_md2(); 632 #else 633 return NULL; 634 #endif 635 } 636 637 /** 638 * The CommonCrypto md4 provider 639 * 640 * @ingroup hcrypto_evp 641 */ 642 643 const EVP_MD * 644 EVP_cc_md4(void) 645 { 646 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H 647 static const struct hc_evp_md md4 = { 648 CC_MD4_DIGEST_LENGTH, 649 CC_MD4_BLOCK_BYTES, 650 sizeof(CC_MD4_CTX), 651 (hc_evp_md_init)CC_MD4_Init, 652 (hc_evp_md_update)CC_MD4_Update, 653 (hc_evp_md_final)CC_MD4_Final, 654 (hc_evp_md_cleanup)NULL 655 }; 656 return &md4; 657 #elif HCRYPTO_FALLBACK 658 return EVP_hcrypto_md4(); 659 #else 660 return NULL; 661 #endif 662 } 663 664 /** 665 * The CommonCrypto md5 provider 666 * 667 * @ingroup hcrypto_evp 668 */ 669 670 const EVP_MD * 671 EVP_cc_md5(void) 672 { 673 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H 674 static const struct hc_evp_md md5 = { 675 CC_MD5_DIGEST_LENGTH, 676 CC_MD5_BLOCK_BYTES, 677 sizeof(CC_MD5_CTX), 678 (hc_evp_md_init)CC_MD5_Init, 679 (hc_evp_md_update)CC_MD5_Update, 680 (hc_evp_md_final)CC_MD5_Final, 681 (hc_evp_md_cleanup)NULL 682 }; 683 return &md5; 684 #elif HCRYPTO_FALLBACK 685 return EVP_hcrypto_md5(); 686 #else 687 return NULL; 688 #endif 689 } 690 691 /** 692 * The CommonCrypto sha1 provider 693 * 694 * @ingroup hcrypto_evp 695 */ 696 697 const EVP_MD * 698 EVP_cc_sha1(void) 699 { 700 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H 701 static const struct hc_evp_md sha1 = { 702 CC_SHA1_DIGEST_LENGTH, 703 CC_SHA1_BLOCK_BYTES, 704 sizeof(CC_SHA1_CTX), 705 (hc_evp_md_init)CC_SHA1_Init, 706 (hc_evp_md_update)CC_SHA1_Update, 707 (hc_evp_md_final)CC_SHA1_Final, 708 (hc_evp_md_cleanup)NULL 709 }; 710 return &sha1; 711 #elif HCRYPTO_FALLBACK 712 return EVP_hcrypto_sha1(); 713 #else 714 return NULL; 715 #endif 716 } 717 718 /** 719 * The CommonCrypto sha256 provider 720 * 721 * @ingroup hcrypto_evp 722 */ 723 724 const EVP_MD * 725 EVP_cc_sha256(void) 726 { 727 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H 728 static const struct hc_evp_md sha256 = { 729 CC_SHA256_DIGEST_LENGTH, 730 CC_SHA256_BLOCK_BYTES, 731 sizeof(CC_SHA256_CTX), 732 (hc_evp_md_init)CC_SHA256_Init, 733 (hc_evp_md_update)CC_SHA256_Update, 734 (hc_evp_md_final)CC_SHA256_Final, 735 (hc_evp_md_cleanup)NULL 736 }; 737 return &sha256; 738 #elif HCRYPTO_FALLBACK 739 return EVP_hcrypto_sha256(); 740 #else 741 return NULL; 742 #endif 743 } 744 745 /** 746 * The CommonCrypto sha384 provider 747 * 748 * @ingroup hcrypto_evp 749 */ 750 751 const EVP_MD * 752 EVP_cc_sha384(void) 753 { 754 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H 755 static const struct hc_evp_md sha384 = { 756 CC_SHA384_DIGEST_LENGTH, 757 CC_SHA384_BLOCK_BYTES, 758 sizeof(CC_SHA512_CTX), 759 (hc_evp_md_init)CC_SHA384_Init, 760 (hc_evp_md_update)CC_SHA384_Update, 761 (hc_evp_md_final)CC_SHA384_Final, 762 (hc_evp_md_cleanup)NULL 763 }; 764 return &sha384; 765 #elif HCRYPTO_FALLBACK 766 return EVP_hcrypto_sha384(); 767 #else 768 return NULL; 769 #endif 770 } 771 772 /** 773 * The CommonCrypto sha512 provider 774 * 775 * @ingroup hcrypto_evp 776 */ 777 778 const EVP_MD * 779 EVP_cc_sha512(void) 780 { 781 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H 782 static const struct hc_evp_md sha512 = { 783 CC_SHA512_DIGEST_LENGTH, 784 CC_SHA512_BLOCK_BYTES, 785 sizeof(CC_SHA512_CTX), 786 (hc_evp_md_init)CC_SHA512_Init, 787 (hc_evp_md_update)CC_SHA512_Update, 788 (hc_evp_md_final)CC_SHA512_Final, 789 (hc_evp_md_cleanup)NULL 790 }; 791 return &sha512; 792 #elif HCRYPTO_FALLBACK 793 return EVP_hcrypto_sha512(); 794 #else 795 return NULL; 796 #endif 797 } 798 799 /** 800 * The Camellia-128 cipher type - CommonCrypto 801 * 802 * @return the Camellia-128 EVP_CIPHER pointer. 803 * 804 * @ingroup hcrypto_evp 805 */ 806 807 const EVP_CIPHER * 808 EVP_cc_camellia_128_cbc(void) 809 { 810 #if HCRYPTO_FALLBACK 811 return EVP_hcrypto_camellia_128_cbc(); 812 #else 813 return NULL; 814 #endif 815 } 816 817 /** 818 * The Camellia-198 cipher type - CommonCrypto 819 * 820 * @return the Camellia-198 EVP_CIPHER pointer. 821 * 822 * @ingroup hcrypto_evp 823 */ 824 825 const EVP_CIPHER * 826 EVP_cc_camellia_192_cbc(void) 827 { 828 #if HCRYPTO_FALLBACK 829 return EVP_hcrypto_camellia_192_cbc(); 830 #else 831 return NULL; 832 #endif 833 } 834 835 /** 836 * The Camellia-256 cipher type - CommonCrypto 837 * 838 * @return the Camellia-256 EVP_CIPHER pointer. 839 * 840 * @ingroup hcrypto_evp 841 */ 842 843 const EVP_CIPHER * 844 EVP_cc_camellia_256_cbc(void) 845 { 846 #if HCRYPTO_FALLBACK 847 return EVP_hcrypto_camellia_256_cbc(); 848 #else 849 return NULL; 850 #endif 851 } 852 853 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H 854 855 /* 856 * 857 */ 858 859 static int 860 cc_rc4_init(EVP_CIPHER_CTX *ctx, 861 const unsigned char * key, 862 const unsigned char * iv, 863 int encp) 864 { 865 struct cc_key *cc = ctx->cipher_data; 866 return init_cc_key(encp, ctx->cipher->flags, kCCAlgorithmRC4, 867 key, ctx->key_len, iv, &cc->href); 868 } 869 870 #endif 871 872 /** 873 874 * The RC4 cipher type (Apple CommonCrypto provider) 875 * 876 * @return the RC4 EVP_CIPHER pointer. 877 * 878 * @ingroup hcrypto_evp 879 */ 880 881 const EVP_CIPHER * 882 EVP_cc_rc4(void) 883 { 884 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H 885 static const EVP_CIPHER rc4 = { 886 0, 887 1, 888 16, 889 0, 890 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH, 891 cc_rc4_init, 892 cc_do_cipher, 893 cc_cleanup, 894 sizeof(struct cc_key), 895 NULL, 896 NULL, 897 NULL, 898 NULL 899 }; 900 return &rc4; 901 #elif HCRYPTO_FALLBACK 902 return EVP_hcrypto_rc4(); 903 #else 904 return NULL; 905 #endif 906 } 907 908 909 /** 910 * The RC4-40 cipher type (Apple CommonCrypto provider) 911 * 912 * @return the RC4 EVP_CIPHER pointer. 913 * 914 * @ingroup hcrypto_evp 915 */ 916 917 const EVP_CIPHER * 918 EVP_cc_rc4_40(void) 919 { 920 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H 921 static const EVP_CIPHER rc4_40 = { 922 0, 923 1, 924 5, 925 0, 926 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH, 927 cc_rc4_init, 928 cc_do_cipher, 929 cc_cleanup, 930 sizeof(struct cc_key), 931 NULL, 932 NULL, 933 NULL, 934 NULL 935 }; 936 return &rc4_40; 937 #elif HCRYPTO_FALLBACK 938 return EVP_hcrypto_rc4_40(); 939 #else 940 return NULL; 941 #endif 942 } 943 944 #endif /* __APPLE__ */ 945 946