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 * castContext.cpp - glue between BlockCrytpor and ssleay CAST128 (CAST5)
23 * Written by Doug Mitchell 4/24/2003
26 #include "castContext.h"
28 CastContext::~CastContext()
33 void CastContext::deleteKey()
35 memset(&mCastKey
, 0, sizeof(mCastKey
));
40 * Standard CSPContext init, called from CSPFullPluginSession::init().
41 * Reusable, e.g., query followed by en/decrypt.
43 void CastContext::init(
44 const Context
&context
,
47 if(mInitFlag
&& !opStarted()) {
52 UInt8
*keyData
= NULL
;
53 bool sameKeySize
= false;
55 /* obtain key from context */
56 symmetricKeyBits(context
, CSSM_ALGID_CAST
,
57 encrypting
? CSSM_KEYUSE_ENCRYPT
: CSSM_KEYUSE_DECRYPT
,
59 if((keyLen
< CAST_MIN_KEY_LENGTH
) || (keyLen
> CAST_KEY_LENGTH
)) {
60 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY
);
64 * Delete existing key if key size changed
66 if(mRawKeySize
== keyLen
) {
73 /* init key only if key size or key bits have changed */
74 if(!sameKeySize
|| memcmp(mRawKey
, keyData
, mRawKeySize
)) {
75 CAST_set_key(&mCastKey
, keyLen
, keyData
);
77 /* save this raw key data */
78 memmove(mRawKey
, keyData
, keyLen
);
82 /* Finally, have BlockCryptor do its setup */
83 setup(CAST_BLOCK
, context
);
88 * Functions called by BlockCryptor
90 void CastContext::encryptBlock(
91 const void *plainText
, // length implied (one block)
94 size_t &cipherTextLen
, // in/out, throws on overflow
95 bool final
) // ignored
97 if(plainTextLen
!= CAST_BLOCK
) {
98 CssmError::throwMe(CSSMERR_CSP_INPUT_LENGTH_ERROR
);
100 if(cipherTextLen
< CAST_BLOCK
) {
101 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
103 CAST_ecb_encrypt((const unsigned char *)plainText
, (unsigned char *)cipherText
,
104 &mCastKey
, CAST_ENCRYPT
);
105 cipherTextLen
= CAST_BLOCK
;
108 void CastContext::decryptBlock(
109 const void *cipherText
, // length implied (one block)
111 size_t &plainTextLen
, // in/out, throws on overflow
112 bool final
) // ignored
114 if(plainTextLen
< CAST_BLOCK
) {
115 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
117 CAST_ecb_encrypt((const unsigned char *)cipherText
, (unsigned char *)plainText
,
118 &mCastKey
, CAST_DECRYPT
);
119 plainTextLen
= CAST_BLOCK
;