1/*
2 * Copyright (c) 2010 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 *  CommonCryptoAESShoefly.c
25 *  CommonCrypto
26 *
27 *  Shim for Diskimages to bridge to a non-hw based aes-cbc
28 *
29 */
30
31// #define COMMON_AESSHOEFLY_FUNCTIONS
32#define CC_Building
33#include "aes.h"
34
35#include "ccdebug.h"
36
37
38void aes_encrypt_key128(const unsigned char *in_key, aes_encrypt_ctx cx[1])
39{
40    aes_encrypt_ctx *ctx = cx;
41    size_t dataUsed;
42
43    CC_DEBUG_LOG(ASL_LEVEL_ERR, "Entering\n");
44
45    (void) CCCryptorCreateFromDataWithMode(kCCEncrypt, kCCModeCBC, kCCAlgorithmAES128NoHardware, ccNoPadding, NULL, in_key, 16, NULL, 0, 0, 0,
46    	&ctx->ctx, kCCContextSizeGENERIC, &ctx->cref, &dataUsed);
47}
48
49void aes_encrypt_key256(const unsigned char *in_key, aes_encrypt_ctx cx[1])
50{
51    aes_encrypt_ctx *ctx = cx;
52    size_t dataUsed;
53
54    CC_DEBUG_LOG(ASL_LEVEL_ERR, "Entering\n");
55    (void) CCCryptorCreateFromDataWithMode(kCCEncrypt, kCCModeCBC, kCCAlgorithmAES128NoHardware, ccNoPadding, NULL, in_key, 32, NULL, 0, 0, 0,
56                                           &ctx->ctx, kCCContextSizeGENERIC, &ctx->cref, &dataUsed);
57}
58
59void aes_decrypt_key128(const unsigned char *in_key, aes_decrypt_ctx cx[1])
60{
61    aes_decrypt_ctx *ctx = cx;
62    size_t dataUsed;
63
64    CC_DEBUG_LOG(ASL_LEVEL_ERR, "Entering\n");
65    (void) CCCryptorCreateFromDataWithMode(kCCDecrypt, kCCModeCBC, kCCAlgorithmAES128NoHardware, ccNoPadding, NULL, in_key, 16, NULL, 0, 0, 0,
66                                           &ctx->ctx, kCCContextSizeGENERIC, &ctx->cref, &dataUsed);
67}
68
69void aes_decrypt_key256(const unsigned char *in_key, aes_decrypt_ctx cx[1])
70{
71    aes_decrypt_ctx *ctx = cx;
72    size_t dataUsed;
73
74    CC_DEBUG_LOG(ASL_LEVEL_ERR, "Entering\n");
75    (void) CCCryptorCreateFromDataWithMode(kCCDecrypt, kCCModeCBC, kCCAlgorithmAES128NoHardware, ccNoPadding, NULL, in_key, 32, NULL, 0, 0, 0,
76                                           &ctx->ctx, kCCContextSizeGENERIC, &ctx->cref, &dataUsed);
77}
78
79
80void aes_encrypt_cbc(const unsigned char *in_blk, const unsigned char *in_iv, unsigned int num_blk,
81                     unsigned char *out_blk, aes_encrypt_ctx cx[1])
82{
83    aes_encrypt_ctx *ctx = cx;
84    CC_DEBUG_LOG(ASL_LEVEL_ERR, "Entering\n");
85	(void) CCCryptorEncryptDataBlock(ctx->cref, in_iv, in_blk, num_blk * AES_BLOCK_SIZE, out_blk);
86}
87
88
89void aes_decrypt_cbc(const unsigned char *in_blk, const unsigned char *in_iv, unsigned int num_blk,
90                     unsigned char *out_blk, aes_decrypt_ctx cx[1])
91{
92    aes_decrypt_ctx *ctx = cx;
93    CC_DEBUG_LOG(ASL_LEVEL_ERR, "Entering\n");
94	(void) CCCryptorDecryptDataBlock(ctx->cref, in_iv, in_blk, num_blk * AES_BLOCK_SIZE, out_blk);
95}
96
97