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)
32 * AES symmetric key generation.
33 * This algorithm has key size restrictions which don't fit with the
34 * standard AppleSymmKeyGenContext model so we have to do some addditional
37 void AESKeyGenContext::generate(
38 const Context
&context
,
42 uint32 reqKeySize
= context
.getInt(
43 CSSM_ATTRIBUTE_KEY_LENGTH
,
44 CSSMERR_CSP_MISSING_ATTR_KEY_LENGTH
);
46 case MIN_AES_KEY_BITS
:
47 case MID_AES_KEY_BITS
:
48 case MAX_AES_KEY_BITS
:
51 CssmError::throwMe(CSSMERR_CSP_UNSUPPORTED_KEY_SIZE
);
53 AppleSymmKeyGenContext::generateSymKey(
60 * AES encrypt/decrypt.
62 AESContext::~AESContext()
65 memset(mRawKey
, 0, MAX_AES_KEY_BITS
/ 8);
69 void AESContext::aesError(
74 errorLog2("AESContext: %s : %d\n", errStr
, artn
);
76 case BAD_KEY_INSTANCE
:
78 crtn
= CSSMERR_CSP_INTERNAL_ERROR
;
81 crtn
= CSSMERR_CSP_INVALID_KEY
;
84 CssmError::throwMe(crtn
);
87 void AESContext::deleteKey()
90 memset(mAesKey
, 0, sizeof(keyInstance
));
91 session().free(mAesKey
);
97 * Standard CSPContext init, called from CSPFullPluginSession::init().
98 * Reusable, e.g., query followed by en/decrypt. Even reusable after context
99 * changed (i.e., new IV in Encrypted File System).
101 void AESContext::init(
102 const Context
&context
,
105 if(mInitFlag
&& !opStarted()) {
110 uint8
*keyData
= NULL
;
111 unsigned lastBlockSize
= mBlockSize
; // may be 0 (first time thru)
112 bool sameKeyAndBlockSizes
= false;
114 /* obtain key from context */
115 symmetricKeyBits(context
, session(), CSSM_ALGID_AES
,
116 encrypting
? CSSM_KEYUSE_ENCRYPT
: CSSM_KEYUSE_DECRYPT
,
120 case MIN_AES_KEY_BITS
/ 8:
121 case MID_AES_KEY_BITS
/ 8:
122 case MAX_AES_KEY_BITS
/ 8:
125 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY
);
130 * block size is optional
132 mBlockSize
= context
.getInt(CSSM_ATTRIBUTE_BLOCK_SIZE
);
133 if(mBlockSize
== 0) {
134 mBlockSize
= DEFAULT_BLOCK_SIZE
;
139 * Delete existing key if key size or block size changed
141 if((lastBlockSize
== mBlockSize
) && (mRawKeySize
== keyLen
)) {
142 sameKeyAndBlockSizes
= true;
144 if((mAesKey
!= NULL
) && !sameKeyAndBlockSizes
) {
149 #if !GLADMAN_AES_128_ENABLE
150 if((mBlockSize
== (MIN_AES_BLOCK_BITS
/8)) &&
151 (keyLen
== (MIN_AES_KEY_BITS
/8)) &&
155 #endif /* !GLADMAN_AES_128_ENABLE */
157 /* create new key if needed */
158 if(mAesKey
== NULL
) {
159 mAesKey
= (keyInstance
*)session().malloc(sizeof(keyInstance
));
162 /* init key only if key size, block size, or key bits have changed */
163 if(!sameKeyAndBlockSizes
|| memcmp(mRawKey
, keyData
, mRawKeySize
)) {
164 int artn
= makeKey((keyInstance
*)mAesKey
,
170 aesError(artn
, "makeKey");
173 /* save this raw key data */
174 memmove(mRawKey
, keyData
, mRawKeySize
);
175 mRawKeySize
= (uint32
)keyLen
;
178 #if !GLADMAN_AES_128_ENABLE
181 mEncryptFcn
= rijndaelBlockEncrypt128
;
182 mDecryptFcn
= rijndaelBlockDecrypt128
;
185 /* common standard path */
186 mEncryptFcn
= rijndaelBlockEncrypt
;
187 mDecryptFcn
= rijndaelBlockDecrypt
;
190 /* common standard path */
191 mEncryptFcn
= rijndaelBlockEncrypt
;
192 mDecryptFcn
= rijndaelBlockDecrypt
;
193 #endif /* !GLADMAN_AES_128_ENABLE */
195 /* Finally, have BlockCryptor do its setup */
196 setup(mBlockSize
, context
);
201 * Functions called by BlockCryptor
203 void AESContext::encryptBlock(
204 const void *plainText
, // length implied (one block)
207 size_t &cipherTextLen
, // in/out, throws on overflow
208 bool final
) // ignored
210 if(plainTextLen
!= mBlockSize
) {
211 CssmError::throwMe(CSSMERR_CSP_INPUT_LENGTH_ERROR
);
213 if(cipherTextLen
< mBlockSize
) {
214 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
216 int artn
= mEncryptFcn(mAesKey
,
218 (word8
*)cipherText
);
220 aesError(artn
, "rijndaelBlockEncrypt");
222 cipherTextLen
= mBlockSize
;
225 void AESContext::decryptBlock(
226 const void *cipherText
, // length implied (one cipher block)
227 size_t cipherTextLen
,
229 size_t &plainTextLen
, // in/out, throws on overflow
230 bool final
) // ignored
232 if(plainTextLen
< mBlockSize
) {
233 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
235 int artn
= mDecryptFcn(mAesKey
,
239 aesError(artn
, "rijndaelBlockDecrypt");
241 plainTextLen
= mBlockSize
;