2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 * aescsp.cpp - glue between BlockCryptor and AES implementation
21 * Written by Doug Mitchell 10/3/2000
25 #include "rijndaelApi.h"
26 #include "rijndael-alg-ref.h"
27 #include "cspdebugging.h"
29 #define DEFAULT_BLOCK_SIZE (MIN_AES_BLOCK_BITS / 8)
31 #define DEBUG_ED 0 /* general encrypt/decrypt debug */
33 #define dprint(s) printf s
40 #define logSize(s, final, encr, ibs, in, out) \
41 printf("%s final %d encr %d inbufsz %d inSize %d outSize %d\n", \
42 s, final, encr, ibs, in, out)
44 #define logSize(s, final, encr, ibs, in, out)
48 * AES symmetric key generation.
49 * This algorithm has key size restrictions which don't fit with the
50 * standard AppleSymmKeyGenContext model so we have to do some addditional
53 void AESKeyGenContext::generate(
54 const Context
&context
,
58 uint32 reqKeySize
= context
.getInt(
59 CSSM_ATTRIBUTE_KEY_LENGTH
,
60 CSSMERR_CSP_MISSING_ATTR_KEY_LENGTH
);
62 case MIN_AES_KEY_BITS
:
63 case MID_AES_KEY_BITS
:
64 case MAX_AES_KEY_BITS
:
67 CssmError::throwMe(CSSMERR_CSP_UNSUPPORTED_KEY_SIZE
);
69 AppleSymmKeyGenContext::generateSymKey(
76 * AES encrypt/decrypt.
78 AESContext::~AESContext()
81 memset(mRawKey
, 0, MAX_AES_KEY_BITS
/ 8);
85 void AESContext::aesError(
90 errorLog2("AESContext: %s : %d\n", errStr
, artn
);
92 case BAD_KEY_INSTANCE
:
94 crtn
= CSSMERR_CSP_INTERNAL_ERROR
;
97 crtn
= CSSMERR_CSP_INVALID_KEY
;
100 CssmError::throwMe(crtn
);
103 void AESContext::deleteKey()
106 memset(mAesKey
, 0, sizeof(keyInstance
));
107 session().free(mAesKey
);
113 * Standard CSPContext init, called from CSPFullPluginSession::init().
114 * Reusable, e.g., query followed by en/decrypt. Even reusable after context
115 * changed (i.e., new IV in Encrypted File System).
117 void AESContext::init(
118 const Context
&context
,
121 if(mInitFlag
&& !opStarted()) {
126 UInt8
*keyData
= NULL
;
127 unsigned lastBlockSize
= mBlockSize
; // may be 0 (first time thru)
128 bool sameKeyAndBlockSizes
= false;
130 /* obtain key from context */
131 symmetricKeyBits(context
, CSSM_ALGID_AES
,
132 encrypting
? CSSM_KEYUSE_ENCRYPT
: CSSM_KEYUSE_DECRYPT
,
137 * block size is optional
139 mBlockSize
= context
.getInt(CSSM_ATTRIBUTE_BLOCK_SIZE
);
140 if(mBlockSize
== 0) {
141 mBlockSize
= DEFAULT_BLOCK_SIZE
;
146 * Delete existing key if key size or block size changed
148 if((lastBlockSize
== mBlockSize
) && (mRawKeySize
== keyLen
)) {
149 sameKeyAndBlockSizes
= true;
151 if((mAesKey
!= NULL
) && !sameKeyAndBlockSizes
) {
156 if((mBlockSize
== (MIN_AES_BLOCK_BITS
/8)) &&
157 (keyLen
== (MIN_AES_KEY_BITS
/8)) &&
162 /* create new key if needed */
163 if(mAesKey
== NULL
) {
164 mAesKey
= (keyInstance
*)session().malloc(sizeof(keyInstance
));
167 /* init key only if key size, block size, or key bits have changed */
168 if(!sameKeyAndBlockSizes
|| memcmp(mRawKey
, keyData
, mRawKeySize
)) {
169 int artn
= makeKey((keyInstance
*)mAesKey
,
175 aesError(artn
, "makeKey");
178 /* save this raw key data */
179 memmove(mRawKey
, keyData
, mRawKeySize
);
180 mRawKeySize
= keyLen
;
185 mEncryptFcn
= rijndaelBlockEncrypt128
;
186 mDecryptFcn
= rijndaelBlockDecrypt128
;
189 /* common standard path */
190 mEncryptFcn
= rijndaelBlockEncrypt
;
191 mDecryptFcn
= rijndaelBlockDecrypt
;
194 /* Finally, have BlockCryptor do its setup */
195 setup(mBlockSize
, context
);
200 * Functions called by BlockCryptor
202 void AESContext::encryptBlock(
203 const void *plainText
, // length implied (one block)
206 size_t &cipherTextLen
, // in/out, throws on overflow
207 bool final
) // ignored
209 if(plainTextLen
!= mBlockSize
) {
210 CssmError::throwMe(CSSMERR_CSP_INPUT_LENGTH_ERROR
);
212 if(cipherTextLen
< mBlockSize
) {
213 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
215 int artn
= mEncryptFcn(mAesKey
,
217 (word8
*)cipherText
);
219 aesError(artn
, "rijndaelBlockEncrypt");
221 cipherTextLen
= mBlockSize
;
224 void AESContext::decryptBlock(
225 const void *cipherText
, // length implied (one cipher block)
227 size_t &plainTextLen
, // in/out, throws on overflow
228 bool final
) // ignored
230 if(plainTextLen
< mBlockSize
) {
231 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
233 int artn
= mDecryptFcn(mAesKey
,
237 aesError(artn
, "rijndaelBlockDecrypt");
239 plainTextLen
= mBlockSize
;