]> git.saurik.com Git - apple/security.git/blob - AppleCSP/MiscCSPAlgs/castContext.cpp
Security-163.tar.gz
[apple/security.git] / AppleCSP / MiscCSPAlgs / castContext.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 /*
20 * castContext.cpp - glue between BlockCrytpor and ssleay CAST128 (CAST5)
21 * implementation
22 *
23 * Written by Doug Mitchell 4/24/2003
24 */
25
26 #include "castContext.h"
27
28 CastContext::~CastContext()
29 {
30 deleteKey();
31 }
32
33 void CastContext::deleteKey()
34 {
35 memset(&mCastKey, 0, sizeof(mCastKey));
36 mInitFlag = false;
37 }
38
39 /*
40 * Standard CSPContext init, called from CSPFullPluginSession::init().
41 * Reusable, e.g., query followed by en/decrypt.
42 */
43 void CastContext::init(
44 const Context &context,
45 bool encrypting)
46 {
47 if(mInitFlag && !opStarted()) {
48 return;
49 }
50
51 UInt32 keyLen;
52 UInt8 *keyData = NULL;
53 bool sameKeySize = false;
54
55 /* obtain key from context */
56 symmetricKeyBits(context, CSSM_ALGID_CAST,
57 encrypting ? CSSM_KEYUSE_ENCRYPT : CSSM_KEYUSE_DECRYPT,
58 keyData, keyLen);
59 if((keyLen < CAST_MIN_KEY_LENGTH) || (keyLen > CAST_KEY_LENGTH)) {
60 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY);
61 }
62
63 /*
64 * Delete existing key if key size changed
65 */
66 if(mRawKeySize == keyLen) {
67 sameKeySize = true;
68 }
69 else {
70 deleteKey();
71 }
72
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);
76
77 /* save this raw key data */
78 memmove(mRawKey, keyData, keyLen);
79 mRawKeySize = keyLen;
80 }
81
82 /* Finally, have BlockCryptor do its setup */
83 setup(CAST_BLOCK, context);
84 mInitFlag = true;
85 }
86
87 /*
88 * Functions called by BlockCryptor
89 */
90 void CastContext::encryptBlock(
91 const void *plainText, // length implied (one block)
92 size_t plainTextLen,
93 void *cipherText,
94 size_t &cipherTextLen, // in/out, throws on overflow
95 bool final) // ignored
96 {
97 if(plainTextLen != CAST_BLOCK) {
98 CssmError::throwMe(CSSMERR_CSP_INPUT_LENGTH_ERROR);
99 }
100 if(cipherTextLen < CAST_BLOCK) {
101 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR);
102 }
103 CAST_ecb_encrypt((const unsigned char *)plainText, (unsigned char *)cipherText,
104 &mCastKey, CAST_ENCRYPT);
105 cipherTextLen = CAST_BLOCK;
106 }
107
108 void CastContext::decryptBlock(
109 const void *cipherText, // length implied (one block)
110 void *plainText,
111 size_t &plainTextLen, // in/out, throws on overflow
112 bool final) // ignored
113 {
114 if(plainTextLen < CAST_BLOCK) {
115 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR);
116 }
117 CAST_ecb_encrypt((const unsigned char *)cipherText, (unsigned char *)plainText,
118 &mCastKey, CAST_DECRYPT);
119 plainTextLen = CAST_BLOCK;
120 }