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