sl@0: /* sl@0: * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * software hash base class implementation sl@0: * software hash base class implementation sl@0: * sl@0: */ sl@0: sl@0: sl@0: /** sl@0: @file sl@0: */ sl@0: sl@0: #include "softwarehashbase.h" sl@0: sl@0: #include sl@0: #include "pluginconfig.h" sl@0: #include "keys.h" sl@0: #include "md2impl.h" sl@0: #include "md5impl.h" sl@0: #include "md4impl.h" sl@0: #include "sha1impl.h" sl@0: #include "sha2impl.h" sl@0: #include "hmacimpl.h" sl@0: sl@0: using namespace SoftwareCrypto; sl@0: sl@0: CSoftwareHash* CSoftwareHash::NewL(TUid aAlgorithm, TUid aOperationMode, const CKey* aKey) sl@0: { sl@0: CSoftwareHash* self=NewLC(aAlgorithm, aOperationMode, aKey); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: CSoftwareHash* CSoftwareHash::NewL(TUid aAlgorithm) sl@0: { sl@0: CSoftwareHash* self=NewLC(aAlgorithm, CryptoSpi::KHashModeUid, NULL); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: CSoftwareHash* CSoftwareHash::NewLC(TUid aAlgorithm, TUid aOperationMode, const CKey* aKey) sl@0: { sl@0: CSoftwareHash* self=new (ELeave) CSoftwareHash(); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aAlgorithm, aOperationMode, aKey); sl@0: return self; sl@0: } sl@0: sl@0: CSoftwareHash::CSoftwareHash() sl@0: { sl@0: } sl@0: sl@0: CSoftwareHash::~CSoftwareHash() sl@0: { sl@0: if (iHashImpl) sl@0: { sl@0: iHashImpl->Close(); sl@0: } sl@0: sl@0: if (iHmacImpl) sl@0: { sl@0: iHmacImpl->Close(); sl@0: } sl@0: delete iKey; sl@0: } sl@0: sl@0: void CSoftwareHash::ConstructL(TUid aAlgorithm, TUid aOperationMode, const CKey* aKey) sl@0: { sl@0: // sl@0: // Only Hash and Hmac mode are supported. sl@0: // sl@0: if (aOperationMode!=KHmacModeUid && aOperationMode!=KHashModeUid) sl@0: { sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: sl@0: //Set the key if there is one sl@0: if (aKey) sl@0: { sl@0: SetKeyL(*aKey); sl@0: } sl@0: sl@0: switch (aAlgorithm.iUid) sl@0: { sl@0: case KCryptoPluginMd2: sl@0: { sl@0: iHashImpl=CMD2Impl::NewL(); sl@0: } sl@0: break; sl@0: sl@0: case KCryptoPluginMd5: sl@0: { sl@0: iHashImpl=CMD5Impl::NewL(); sl@0: } sl@0: break; sl@0: sl@0: case KCryptoPluginMd4: sl@0: { sl@0: iHashImpl=CMD4Impl::NewL(); sl@0: } sl@0: break; sl@0: sl@0: case KCryptoPluginSha1: sl@0: { sl@0: iHashImpl=CSHA1Impl::NewL(); sl@0: } sl@0: break; sl@0: sl@0: case KCryptoPluginSha224: sl@0: case KCryptoPluginSha256: sl@0: case KCryptoPluginSha384: sl@0: case KCryptoPluginSha512: sl@0: { sl@0: iHashImpl=CSHA2Impl::NewL(aAlgorithm.iUid); sl@0: } sl@0: break; sl@0: sl@0: default: sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: sl@0: SetOperationModeL(aOperationMode); sl@0: } sl@0: sl@0: void CSoftwareHash::SetOperationModeL(TUid aOperationMode) sl@0: { sl@0: switch (aOperationMode.iUid) sl@0: { sl@0: case KHmacMode: sl@0: { sl@0: // sl@0: //Only create hmac implementation if there isn't one sl@0: // sl@0: if (!iHmacImpl) sl@0: { sl@0: if (iKey) sl@0: { sl@0: iHmacImpl=CHMacImpl::NewL(*iKey, iHashImpl); sl@0: } sl@0: else sl@0: { sl@0: iHmacImpl=CHMacImpl::NewL(iHashImpl); sl@0: } sl@0: } sl@0: } sl@0: break; sl@0: sl@0: case KHashMode: sl@0: { sl@0: Reset(); sl@0: } sl@0: break; sl@0: sl@0: default: sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: sl@0: // sl@0: // Set the operation mode. sl@0: // sl@0: iOperationMode=aOperationMode; sl@0: } sl@0: sl@0: MSoftwareHash* CSoftwareHash::Impl() sl@0: { sl@0: MSoftwareHash* impl=NULL; sl@0: if (iOperationMode==KHashModeUid) sl@0: { sl@0: impl=iHashImpl; sl@0: } sl@0: else if (iOperationMode==KHmacModeUid && iKey) sl@0: { sl@0: impl=iHmacImpl; sl@0: } sl@0: return impl; sl@0: } sl@0: sl@0: void CSoftwareHash::SetKeyL(const CKey& aKey) sl@0: { sl@0: Reset(); sl@0: delete iKey; sl@0: iKey=CKey::NewL(aKey); sl@0: if (iHmacImpl) sl@0: { sl@0: iHmacImpl->SetKeyL(aKey); sl@0: } sl@0: } sl@0: sl@0: void CSoftwareHash::Reset() sl@0: { sl@0: if (iHashImpl) sl@0: { sl@0: iHashImpl->Reset(); sl@0: } sl@0: sl@0: if (iHmacImpl) sl@0: { sl@0: iHmacImpl->Reset(); sl@0: } sl@0: } sl@0: sl@0: void CSoftwareHash::Close() sl@0: { sl@0: delete this; sl@0: } sl@0: sl@0: void CSoftwareHash::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics) sl@0: { sl@0: MSoftwareHash* impl=Impl(); sl@0: if (impl) sl@0: { sl@0: impl->GetCharacteristicsL(aPluginCharacteristics); sl@0: } sl@0: else sl@0: { sl@0: User::Leave(KErrNotReady); sl@0: } sl@0: } sl@0: sl@0: const CExtendedCharacteristics* CSoftwareHash::GetExtendedCharacteristicsL() sl@0: { sl@0: MSoftwareHash* impl=Impl(); sl@0: if (!impl) sl@0: { sl@0: User::Leave(KErrNotReady); sl@0: } sl@0: return impl->GetExtendedCharacteristicsL(); sl@0: } sl@0: sl@0: TAny* CSoftwareHash::GetExtension(TUid aExtensionId) sl@0: { sl@0: MSoftwareHash* impl=Impl(); sl@0: if (impl) sl@0: { sl@0: return impl->GetExtension(aExtensionId); sl@0: } sl@0: else sl@0: { sl@0: return NULL; sl@0: } sl@0: } sl@0: sl@0: TPtrC8 CSoftwareHash::Hash(const TDesC8& aMessage) sl@0: { sl@0: MSoftwareHash* impl=Impl(); sl@0: if (impl) sl@0: { sl@0: return impl->Hash(aMessage); sl@0: } sl@0: else sl@0: { sl@0: return KNullDesC8(); sl@0: } sl@0: } sl@0: sl@0: void CSoftwareHash::Update(const TDesC8& aMessage) sl@0: { sl@0: MSoftwareHash* impl=Impl(); sl@0: if (impl) sl@0: { sl@0: return impl->Update(aMessage); sl@0: } sl@0: } sl@0: sl@0: TPtrC8 CSoftwareHash::Final(const TDesC8& aMessage) sl@0: { sl@0: MSoftwareHash* impl=Impl(); sl@0: if (impl) sl@0: { sl@0: return impl->Final(aMessage); sl@0: } sl@0: else sl@0: { sl@0: return KNullDesC8(); sl@0: } sl@0: } sl@0: sl@0: MHash* CSoftwareHash::ReplicateL() sl@0: { sl@0: CSoftwareHash* that=new(ELeave)CSoftwareHash(); sl@0: CleanupStack::PushL(that); sl@0: if (this->iKey) sl@0: { sl@0: that->iKey=CKey::NewL(*this->iKey); sl@0: } sl@0: that->iOperationMode=this->iOperationMode; sl@0: that->iHashImpl=static_cast(this->iHashImpl->ReplicateL()); sl@0: if (this->iHmacImpl) sl@0: { sl@0: that->iHmacImpl=static_cast(this->iHmacImpl->ReplicateL()); sl@0: } sl@0: CleanupStack::Pop(); sl@0: return that; sl@0: } sl@0: sl@0: MHash* CSoftwareHash::CopyL() sl@0: { sl@0: CSoftwareHash* that=new(ELeave)CSoftwareHash(); sl@0: CleanupStack::PushL(that); sl@0: if (this->iKey) sl@0: { sl@0: that->iKey=CKey::NewL(*this->iKey); sl@0: } sl@0: that->iOperationMode=this->iOperationMode; sl@0: that->iHashImpl=static_cast(this->iHashImpl->CopyL()); sl@0: if (this->iHmacImpl) sl@0: { sl@0: that->iHmacImpl=static_cast(this->iHmacImpl->CopyL()); sl@0: } sl@0: CleanupStack::Pop(); sl@0: return that; sl@0: } sl@0: sl@0: