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 * bfContext.cpp - glue between BlockCrytpor and ssleay Blowfish
22 * Written by Doug Mitchell 4/23/2003
25 #include "bfContext.h"
27 BlowfishContext::~BlowfishContext()
32 void BlowfishContext::deleteKey()
34 memset(&mBfKey
, 0, sizeof(mBfKey
));
39 * Standard CSPContext init, called from CSPFullPluginSession::init().
40 * Reusable, e.g., query followed by en/decrypt.
42 void BlowfishContext::init(
43 const Context
&context
,
46 if(mInitFlag
&& !opStarted()) {
51 UInt8
*keyData
= NULL
;
52 bool sameKeySize
= false;
54 /* obtain key from context */
55 symmetricKeyBits(context
, CSSM_ALGID_BLOWFISH
,
56 encrypting
? CSSM_KEYUSE_ENCRYPT
: CSSM_KEYUSE_DECRYPT
,
58 if((keyLen
< BF_MIN_KEY_SIZE_BYTES
) || (keyLen
> BF_MAX_KEY_SIZE_BYTES
)) {
59 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY
);
63 * Delete existing key if key size changed
65 if(mRawKeySize
== keyLen
) {
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
);
76 /* save this raw key data */
77 memmove(mRawKey
, keyData
, keyLen
);
81 /* Finally, have BlockCryptor do its setup */
82 setup(BF_BLOCK
, context
);
87 * Functions called by BlockCryptor
89 void BlowfishContext::encryptBlock(
90 const void *plainText
, // length implied (one block)
93 size_t &cipherTextLen
, // in/out, throws on overflow
94 bool final
) // ignored
96 if(plainTextLen
!= BF_BLOCK
) {
97 CssmError::throwMe(CSSMERR_CSP_INPUT_LENGTH_ERROR
);
99 if(cipherTextLen
< BF_BLOCK
) {
100 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
102 BF_ecb_encrypt((const unsigned char *)plainText
, (unsigned char *)cipherText
,
103 &mBfKey
, BF_ENCRYPT
);
104 cipherTextLen
= BF_BLOCK
;
107 void BlowfishContext::decryptBlock(
108 const void *cipherText
, // length implied (one block)
110 size_t &plainTextLen
, // in/out, throws on overflow
111 bool final
) // ignored
113 if(plainTextLen
< BF_BLOCK
) {
114 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
116 BF_ecb_encrypt((const unsigned char *)cipherText
, (unsigned char *)plainText
,
117 &mBfKey
, BF_DECRYPT
);
118 plainTextLen
= BF_BLOCK
;