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