sl@0: /* sl@0: * Copyright (c) 2008-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 Mac Implementation sl@0: * Software Mac Implementation sl@0: * MAC plugin header sl@0: * sl@0: */ sl@0: sl@0: sl@0: /** sl@0: @file sl@0: */ sl@0: #include "macimpl.h" sl@0: #include "pluginconfig.h" sl@0: sl@0: /** sl@0: * HMAC plugin headers sl@0: */ sl@0: #include "md2impl.h" sl@0: #include "md4impl.h" sl@0: #include "md5impl.h" sl@0: #include "sha1impl.h" sl@0: #include "sha2impl.h" sl@0: #include "hmacimpl.h" sl@0: sl@0: sl@0: using namespace SoftwareCrypto; sl@0: using namespace CryptoSpi; sl@0: sl@0: sl@0: CMacImpl* CMacImpl::NewL(const CKey& aKey, const TUid aImplementationId, const CCryptoParams* aAlgorithmParams) sl@0: { sl@0: CMacImpl* self = new (ELeave) CMacImpl(); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aKey, aImplementationId, aAlgorithmParams); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: CMacImpl::CMacImpl() sl@0: { sl@0: } sl@0: sl@0: void CMacImpl::ConstructL(const CKey& aKey, const TUid aImplementationId, const CCryptoParams* /*aAlgorithmParams*/) sl@0: { sl@0: iImplementationUid = aImplementationId; sl@0: iKey = CryptoSpi::CKey::NewL(aKey); sl@0: sl@0: MSoftwareHash* hashImpl = NULL; sl@0: sl@0: switch (aImplementationId.iUid) sl@0: { sl@0: case KCryptoPluginMacHashMd2: sl@0: { sl@0: hashImpl = CMD2Impl::NewL(); sl@0: CleanupClosePushL(*hashImpl); sl@0: iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl); sl@0: iBase = EHashBased; sl@0: } sl@0: break; sl@0: case KCryptoPluginMacHashMd5: sl@0: { sl@0: hashImpl = CMD5Impl::NewL(); sl@0: CleanupClosePushL(*hashImpl); sl@0: iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl); sl@0: iBase = EHashBased; sl@0: } sl@0: break; sl@0: case KCryptoPluginMacHashMd4: sl@0: { sl@0: hashImpl = CMD4Impl::NewL(); sl@0: CleanupClosePushL(*hashImpl); sl@0: iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl); sl@0: iBase = EHashBased; sl@0: } sl@0: break; sl@0: case KCryptoPluginMacHashSha1: sl@0: { sl@0: hashImpl = CSHA1Impl::NewL(); sl@0: CleanupClosePushL(*hashImpl); sl@0: iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl); sl@0: iBase = EHashBased; sl@0: } sl@0: break; sl@0: case KCryptoPluginMacHashSha224: sl@0: { sl@0: hashImpl = CSHA2Impl::NewL(KCryptoPluginSha224); sl@0: CleanupClosePushL(*hashImpl); sl@0: iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl); sl@0: iBase = EHashBased; sl@0: } sl@0: break; sl@0: case KCryptoPluginMacHashSha256: sl@0: { sl@0: hashImpl = CSHA2Impl::NewL(KCryptoPluginSha256); sl@0: CleanupClosePushL(*hashImpl); sl@0: iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl); sl@0: iBase = EHashBased; sl@0: } sl@0: break; sl@0: case KCryptoPluginMacHashSha384: sl@0: { sl@0: hashImpl = CSHA2Impl::NewL(KCryptoPluginSha384); sl@0: CleanupClosePushL(*hashImpl); sl@0: iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl); sl@0: iBase = EHashBased; sl@0: } sl@0: break; sl@0: case KCryptoPluginMacHashSha512: sl@0: { sl@0: hashImpl = CSHA2Impl::NewL(KCryptoPluginSha512); sl@0: CleanupClosePushL(*hashImpl); sl@0: iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl); sl@0: iBase = EHashBased; sl@0: } sl@0: break; sl@0: case KCryptoPluginMacAesXcbcMac96: sl@0: { sl@0: CSymmetricCipher* symmetricCipher = NULL; sl@0: CryptoSpi::CSymmetricCipherFactory::CreateSymmetricCipherL(symmetricCipher, sl@0: CryptoSpi::KAesUid, *iKey, sl@0: CryptoSpi::KCryptoModeEncryptUid, sl@0: CryptoSpi::KOperationModeCBCUid, sl@0: CryptoSpi::KPaddingModeNoneUid, sl@0: NULL); sl@0: sl@0: iCmacImpl= CCMacImpl::NewL(*iKey, symmetricCipher, CryptoSpi::KAlgorithmCipherAesXcbcMac96); sl@0: iBase = ECipherBased; sl@0: } sl@0: break; sl@0: case KCryptoPluginMacAesXcbcPrf128: sl@0: { sl@0: TBuf8<16> tempKey; sl@0: tempKey.SetLength(16); sl@0: sl@0: CryptoSpi::CCryptoParams* keyParams = CryptoSpi::CCryptoParams::NewLC(); sl@0: keyParams->AddL(tempKey, CryptoSpi::KSymmetricKeyParameterUid); sl@0: CryptoSpi::CKey* key = CryptoSpi::CKey::NewLC(aKey.KeyProperty(),*keyParams); sl@0: sl@0: CSymmetricCipher* symmetricCipher = NULL; sl@0: CryptoSpi::CSymmetricCipherFactory::CreateSymmetricCipherL(symmetricCipher, sl@0: CryptoSpi::KAesUid, *key, sl@0: CryptoSpi::KCryptoModeEncryptUid, sl@0: CryptoSpi::KOperationModeCBCUid, sl@0: CryptoSpi::KPaddingModeNoneUid, sl@0: NULL); sl@0: CleanupStack::PopAndDestroy(2, keyParams); //key and keyParams sl@0: sl@0: iCmacImpl= CCMacImpl::NewL(*iKey, symmetricCipher, CryptoSpi::KAlgorithmCipherAesXcbcPrf128); sl@0: iBase = ECipherBased; sl@0: } sl@0: break; sl@0: sl@0: default: sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: sl@0: if(iBase == EHashBased) sl@0: { sl@0: CleanupStack::PopAndDestroy(hashImpl); sl@0: } sl@0: } sl@0: sl@0: CMacImpl::~CMacImpl() sl@0: { sl@0: delete iKey; sl@0: if(iHmacImpl) sl@0: { sl@0: iHmacImpl->Close(); sl@0: } sl@0: delete iCmacImpl; sl@0: } sl@0: sl@0: void CMacImpl::Reset() sl@0: { sl@0: if (iBase == EHashBased) sl@0: { sl@0: iHmacImpl->Reset(); sl@0: } sl@0: else if (iBase == ECipherBased) sl@0: { sl@0: iCmacImpl->Reset(); sl@0: } sl@0: } sl@0: sl@0: void CMacImpl::Close() sl@0: { sl@0: delete this; sl@0: } sl@0: sl@0: void CMacImpl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics) sl@0: { sl@0: aPluginCharacteristics=NULL; sl@0: TInt macNum=sizeof(KMacCharacteristics)/sizeof(TMacCharacteristics*); sl@0: for (TInt i=0;iiMacChar.iImplementationUID==ImplementationUid().iUid) sl@0: { sl@0: aPluginCharacteristics = KMacCharacteristics[i]; sl@0: break; sl@0: } sl@0: } sl@0: } sl@0: sl@0: const CExtendedCharacteristics* CMacImpl::GetExtendedCharacteristicsL() sl@0: { sl@0: return (iBase == EHashBased) ? iHmacImpl->GetExtendedCharacteristicsL(): iCmacImpl->GetExtendedCharacteristicsL(); sl@0: } sl@0: sl@0: TAny* CMacImpl::GetExtension(TUid aExtensionId) sl@0: { sl@0: return (iBase == EHashBased) ? iHmacImpl->GetExtension(aExtensionId): NULL; sl@0: } sl@0: sl@0: TUid CMacImpl::ImplementationUid() const sl@0: { sl@0: return iImplementationUid; sl@0: } sl@0: sl@0: TPtrC8 CMacImpl::MacL(const TDesC8& aMessage) sl@0: { sl@0: return (iBase == EHashBased) ? iHmacImpl->Hash(aMessage):iCmacImpl->MacL(aMessage); sl@0: } sl@0: sl@0: void CMacImpl::UpdateL(const TDesC8& aMessage) sl@0: { sl@0: (iBase == EHashBased) ? iHmacImpl->Update(aMessage):iCmacImpl->UpdateL(aMessage); sl@0: } sl@0: sl@0: TPtrC8 CMacImpl::FinalL(const TDesC8& aMessage) sl@0: { sl@0: return (iBase == EHashBased) ? iHmacImpl->Final(aMessage):iCmacImpl->FinalL(aMessage); sl@0: } sl@0: sl@0: void CMacImpl::ReInitialiseAndSetKeyL(const CKey& aKey) sl@0: { sl@0: delete iKey; sl@0: iKey = NULL; sl@0: iKey = CryptoSpi::CKey::NewL(aKey); sl@0: sl@0: if (iBase == EHashBased) sl@0: { sl@0: iHmacImpl->SetKeyL(aKey); sl@0: iHmacImpl->Reset(); sl@0: } sl@0: else if (iBase == ECipherBased) sl@0: { sl@0: iCmacImpl->ReInitialiseAndSetKeyL(aKey); sl@0: } sl@0: } sl@0: sl@0: MMac* CMacImpl::ReplicateL() sl@0: { sl@0: CMacImpl* that= new(ELeave) CMacImpl(); sl@0: CleanupStack::PushL(that); sl@0: that->iImplementationUid = iImplementationUid; sl@0: that->iBase = iBase; sl@0: that->iKey=CKey::NewL(*iKey); sl@0: sl@0: if(iBase == EHashBased) sl@0: { sl@0: that->iHmacImpl=static_cast(iHmacImpl->ReplicateL()); sl@0: } sl@0: else if (iBase == ECipherBased) sl@0: { sl@0: that->iCmacImpl= iCmacImpl->ReplicateL(); sl@0: } sl@0: CleanupStack::Pop(that); sl@0: return that; sl@0: } sl@0: sl@0: MMac* CMacImpl::CopyL() sl@0: { sl@0: CMacImpl* that= new(ELeave) CMacImpl(); sl@0: CleanupStack::PushL(that); sl@0: that->iImplementationUid = iImplementationUid; sl@0: that->iBase = iBase; sl@0: that->iKey=CKey::NewL(*iKey); sl@0: sl@0: if(iBase == EHashBased) sl@0: { sl@0: that->iHmacImpl=static_cast(iHmacImpl->CopyL()); sl@0: } sl@0: else if (iBase == ECipherBased) sl@0: { sl@0: that->iCmacImpl= iCmacImpl->CopyL(); sl@0: } sl@0: CleanupStack::Pop(that); sl@0: return that; sl@0: }