|
50 | 50 |
|
51 | 51 | /* This implementation has built-in support for multiple AES APIs. Set any
|
52 | 52 | / one of the following to non-zero to specify which to use. */
|
| 53 | +#if 0 |
| 54 | +#define USE_APPLE_COMMON_CRYPTO_AES 0 |
| 55 | +#define USE_NETTLE_AES 0 |
53 | 56 | #define USE_OPENSSL_AES 1 /* http://openssl.org */
|
54 | 57 | #define USE_REFERENCE_AES 0 /* Internet search: rijndael-alg-fst.c */
|
55 | 58 | #define USE_AES_NI 0 /* Uses compiler's intrinsics */
|
| 59 | +#endif |
56 | 60 |
|
57 | 61 | /* During encryption and decryption, various "L values" are required.
|
58 | 62 | / The L values can be precomputed during initialization (requiring extra
|
|
72 | 76 | /* Includes and compiler specific definitions */
|
73 | 77 | /* ----------------------------------------------------------------------- */
|
74 | 78 |
|
| 79 | +#include "config.h" |
75 | 80 | #include "ae.h"
|
76 | 81 | #include <stdlib.h>
|
77 | 82 | #include <string.h>
|
|
95 | 100 | #include <intrin.h>
|
96 | 101 | #pragma intrinsic(_byteswap_uint64, _BitScanForward, memcpy)
|
97 | 102 | #elif __GNUC__
|
| 103 | + #ifndef inline |
98 | 104 | #define inline __inline__ /* No "inline" in GCC ansi C mode */
|
| 105 | + #endif |
| 106 | + #ifndef restrict |
99 | 107 | #define restrict __restrict__ /* No "restrict" in GCC ansi C mode */
|
| 108 | + #endif |
100 | 109 | #endif
|
101 | 110 |
|
102 | 111 | #if _MSC_VER
|
@@ -347,6 +356,131 @@ static inline void AES_ecb_decrypt_blks(block *blks, unsigned nblks, AES_KEY *ke
|
347 | 356 |
|
348 | 357 | #define BPI 4 /* Number of blocks in buffer per ECB call */
|
349 | 358 |
|
| 359 | +/*-------------------*/ |
| 360 | +#elif USE_APPLE_COMMON_CRYPTO_AES |
| 361 | +/*-------------------*/ |
| 362 | + |
| 363 | +#include <fatal_assert.h> |
| 364 | +#include <CommonCrypto/CommonCryptor.h> |
| 365 | + |
| 366 | +typedef struct { |
| 367 | + CCCryptorRef ref; |
| 368 | + uint8_t b[4096]; |
| 369 | +} AES_KEY; |
| 370 | +#if (OCB_KEY_LEN == 0) |
| 371 | +#define ROUNDS(ctx) ((ctx)->rounds) |
| 372 | +#else |
| 373 | +#define ROUNDS(ctx) (6+OCB_KEY_LEN/4) |
| 374 | +#endif |
| 375 | + |
| 376 | +static inline void AES_set_encrypt_key(unsigned char *handle, const int bits, AES_KEY *key) |
| 377 | +{ |
| 378 | + CCCryptorStatus rv = CCCryptorCreateFromData( |
| 379 | + kCCEncrypt, |
| 380 | + kCCAlgorithmAES, |
| 381 | + kCCOptionECBMode, |
| 382 | + handle, |
| 383 | + bits / 8, |
| 384 | + NULL, |
| 385 | + &(key->b), |
| 386 | + sizeof (key->b), |
| 387 | + &(key->ref), |
| 388 | + NULL); |
| 389 | + |
| 390 | + fatal_assert(rv == kCCSuccess); |
| 391 | +} |
| 392 | +static inline void AES_set_decrypt_key(unsigned char *handle, const int bits, AES_KEY *key) |
| 393 | +{ |
| 394 | + CCCryptorStatus rv = CCCryptorCreateFromData( |
| 395 | + kCCDecrypt, |
| 396 | + kCCAlgorithmAES, |
| 397 | + kCCOptionECBMode, |
| 398 | + handle, |
| 399 | + bits / 8, |
| 400 | + NULL, |
| 401 | + &(key->b), |
| 402 | + sizeof (key->b), |
| 403 | + &(key->ref), |
| 404 | + NULL); |
| 405 | + |
| 406 | + fatal_assert(rv == kCCSuccess); |
| 407 | +} |
| 408 | +static inline void AES_encrypt(unsigned char *src, unsigned char *dst, AES_KEY *key) { |
| 409 | + size_t dataOutMoved; |
| 410 | + CCCryptorStatus rv = CCCryptorUpdate( |
| 411 | + key->ref, |
| 412 | + (const void *)src, |
| 413 | + kCCBlockSizeAES128, |
| 414 | + (void *)dst, |
| 415 | + kCCBlockSizeAES128, |
| 416 | + &dataOutMoved); |
| 417 | + fatal_assert(rv == kCCSuccess); |
| 418 | + fatal_assert(dataOutMoved == kCCBlockSizeAES128); |
| 419 | +} |
| 420 | +#if 0 |
| 421 | +/* unused */ |
| 422 | +static inline void AES_decrypt(unsigned char *src, unsigned char *dst, AES_KEY *key) { |
| 423 | + AES_encrypt(src, dst, key); |
| 424 | +} |
| 425 | +#endif |
| 426 | +static inline void AES_ecb_encrypt_blks(block *blks, unsigned nblks, AES_KEY *key) { |
| 427 | + const size_t dataSize = kCCBlockSizeAES128 * nblks; |
| 428 | + size_t dataOutMoved; |
| 429 | + CCCryptorStatus rv = CCCryptorUpdate( |
| 430 | + key->ref, |
| 431 | + (const void *)blks, |
| 432 | + dataSize, |
| 433 | + (void *)blks, |
| 434 | + dataSize, |
| 435 | + &dataOutMoved); |
| 436 | + fatal_assert(rv == kCCSuccess); |
| 437 | + fatal_assert(dataOutMoved == dataSize); |
| 438 | +} |
| 439 | +static inline void AES_ecb_decrypt_blks(block *blks, unsigned nblks, AES_KEY *key) { |
| 440 | + AES_ecb_encrypt_blks(blks, nblks, key); |
| 441 | +} |
| 442 | + |
| 443 | +#define BPI 4 /* Number of blocks in buffer per ECB call */ |
| 444 | + |
| 445 | +/*-------------------*/ |
| 446 | +#elif USE_NETTLE_AES |
| 447 | +/*-------------------*/ |
| 448 | + |
| 449 | +#include <nettle/aes.h> |
| 450 | + |
| 451 | +typedef struct aes_ctx AES_KEY; |
| 452 | +#if (OCB_KEY_LEN == 0) |
| 453 | +#define ROUNDS(ctx) ((ctx)->rounds) |
| 454 | +#else |
| 455 | +#define ROUNDS(ctx) (6+OCB_KEY_LEN/4) |
| 456 | +#endif |
| 457 | + |
| 458 | +static inline void AES_set_encrypt_key(unsigned char *handle, const int bits, AES_KEY *key) |
| 459 | +{ |
| 460 | + nettle_aes_set_encrypt_key(key, bits/8, (const uint8_t *)handle); |
| 461 | +} |
| 462 | +static inline void AES_set_decrypt_key(unsigned char *handle, const int bits, AES_KEY *key) |
| 463 | +{ |
| 464 | + nettle_aes_set_decrypt_key(key, bits/8, (const uint8_t *)handle); |
| 465 | +} |
| 466 | +static inline void AES_encrypt(unsigned char *src, unsigned char *dst, AES_KEY *key) { |
| 467 | + nettle_aes_encrypt(key, AES_BLOCK_SIZE, dst, src); |
| 468 | +} |
| 469 | +#if 0 |
| 470 | +/* unused */ |
| 471 | +static inline void AES_decrypt(unsigned char *src, unsigned char *dst, AES_KEY *key) { |
| 472 | + nettle_aes_decrypt(key, AES_BLOCK_SIZE, dst, src); |
| 473 | +} |
| 474 | +#endif |
| 475 | +static inline void AES_ecb_encrypt_blks(block *blks, unsigned nblks, AES_KEY *key) { |
| 476 | + nettle_aes_encrypt(key, nblks * AES_BLOCK_SIZE, (unsigned char*)blks, (unsigned char*)blks); |
| 477 | +} |
| 478 | +static inline void AES_ecb_decrypt_blks(block *blks, unsigned nblks, AES_KEY *key) { |
| 479 | + nettle_aes_decrypt(key, nblks * AES_BLOCK_SIZE, (unsigned char*)blks, (unsigned char*)blks); |
| 480 | +} |
| 481 | + |
| 482 | +#define BPI 4 /* Number of blocks in buffer per ECB call */ |
| 483 | + |
350 | 484 | /*-------------------*/
|
351 | 485 | #elif USE_REFERENCE_AES
|
352 | 486 | /*-------------------*/
|
@@ -560,6 +694,8 @@ static inline void AES_ecb_decrypt_blks(block *blks, unsigned nblks, AES_KEY *ke
|
560 | 694 | #define BPI 8 /* Number of blocks in buffer per ECB call */
|
561 | 695 | /* Set to 4 for Westmere, 8 for Sandy Bridge */
|
562 | 696 |
|
| 697 | +#else |
| 698 | +#error "No AES implementation selected." |
563 | 699 | #endif
|
564 | 700 |
|
565 | 701 | /* ----------------------------------------------------------------------- */
|
|
0 commit comments