2 * Copyright (c) 2000-2001,2011-2012,2014 Apple 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 * castContext.cpp - glue between BlockCrytpor and CommonCrypto CAST128 (CAST5)
25 #include "castContext.h"
27 CastContext::CastContext(AppleCSPSession
&session
) :
28 BlockCryptor(session
),
37 CastContext::~CastContext()
42 void CastContext::deleteKey()
44 if (mCastKey
!= NULL
) {
45 CCCryptorRelease(mCastKey
);
52 * Standard CSPContext init, called from CSPFullPluginSession::init().
53 * Reusable, e.g., query followed by en/decrypt.
55 void CastContext::init(
56 const Context
&context
,
59 if(mInitFlag
&& !opStarted()) {
64 uint8
*keyData
= NULL
;
65 bool sameKeySize
= false;
67 /* obtain key from context */
68 symmetricKeyBits(context
, session(), CSSM_ALGID_CAST
,
69 encrypting
? CSSM_KEYUSE_ENCRYPT
: CSSM_KEYUSE_DECRYPT
,
71 if((keyLen
< kCCKeySizeMinCAST
) || (keyLen
> kCCKeySizeMaxCAST
)) {
72 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY
);
76 * Delete existing key if key size changed
78 if(mRawKeySize
== keyLen
) {
85 /* init key only if key size or key bits have changed */
86 if(!sameKeySize
|| memcmp(mRawKey
, keyData
, mRawKeySize
)) {
87 (void) CCCryptorCreateWithMode(0, kCCModeECB
, kCCAlgorithmCAST
, ccDefaultPadding
, NULL
, keyData
, keyLen
, NULL
, 0, 0, 0, &mCastKey
);
89 /* save this raw key data */
90 memmove(mRawKey
, keyData
, keyLen
);
91 mRawKeySize
= (uint32
)keyLen
;
94 /* Finally, have BlockCryptor do its setup */
95 setup(kCCBlockSizeCAST
, context
);
100 * Functions called by BlockCryptor
102 void CastContext::encryptBlock(
103 const void *plainText
, // length implied (one block)
106 size_t &cipherTextLen
, // in/out, throws on overflow
107 bool final
) // ignored
109 if(plainTextLen
!= kCCBlockSizeCAST
) {
110 CssmError::throwMe(CSSMERR_CSP_INPUT_LENGTH_ERROR
);
112 if(cipherTextLen
< kCCBlockSizeCAST
) {
113 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
115 (void) CCCryptorEncryptDataBlock(mCastKey
, NULL
, plainText
, kCCBlockSizeCAST
, cipherText
);
117 cipherTextLen
= kCCBlockSizeCAST
;
120 void CastContext::decryptBlock(
121 const void *cipherText
, // length implied (one block)
122 size_t cipherTextLen
,
124 size_t &plainTextLen
, // in/out, throws on overflow
125 bool final
) // ignored
127 if(plainTextLen
< kCCBlockSizeCAST
) {
128 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
130 (void) CCCryptorDecryptDataBlock(mCastKey
, NULL
, cipherText
, kCCBlockSizeCAST
, plainText
);
132 plainTextLen
= kCCBlockSizeCAST
;