os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/macimpl.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/macimpl.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,302 @@
     1.4 +/*
     1.5 +* Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +* Software Mac Implementation
    1.19 +* Software Mac Implementation
    1.20 +* MAC plugin header
    1.21 +*
    1.22 +*/
    1.23 +
    1.24 +
    1.25 +/**
    1.26 + @file
    1.27 +*/
    1.28 +#include "macimpl.h"
    1.29 +#include "pluginconfig.h"
    1.30 +
    1.31 +/**
    1.32 + * HMAC plugin headers
    1.33 + */ 
    1.34 +#include "md2impl.h"
    1.35 +#include "md4impl.h"
    1.36 +#include "md5impl.h"
    1.37 +#include "sha1impl.h"
    1.38 +#include "sha2impl.h"
    1.39 +#include "hmacimpl.h"
    1.40 +
    1.41 +
    1.42 +using namespace SoftwareCrypto;
    1.43 +using namespace CryptoSpi;
    1.44 +
    1.45 +
    1.46 +CMacImpl* CMacImpl::NewL(const CKey& aKey, const TUid aImplementationId, const CCryptoParams* aAlgorithmParams)
    1.47 +	{
    1.48 +	CMacImpl* self = new (ELeave) CMacImpl();
    1.49 +	CleanupStack::PushL(self);
    1.50 +	self->ConstructL(aKey, aImplementationId, aAlgorithmParams);
    1.51 +	CleanupStack::Pop();
    1.52 +	return self;
    1.53 +	}
    1.54 +
    1.55 +CMacImpl::CMacImpl() 
    1.56 +	{
    1.57 +	}
    1.58 +
    1.59 +void CMacImpl::ConstructL(const CKey& aKey, const TUid aImplementationId, const CCryptoParams* /*aAlgorithmParams*/) 
    1.60 +	{
    1.61 +	iImplementationUid = aImplementationId; 
    1.62 +	iKey = CryptoSpi::CKey::NewL(aKey);
    1.63 +		
    1.64 +	MSoftwareHash* hashImpl = NULL;
    1.65 +	
    1.66 +	switch (aImplementationId.iUid)
    1.67 +		{
    1.68 +		case KCryptoPluginMacHashMd2:
    1.69 +			{
    1.70 +			hashImpl = CMD2Impl::NewL();
    1.71 +			CleanupClosePushL(*hashImpl);
    1.72 +			iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
    1.73 +			iBase = EHashBased;
    1.74 +			}
    1.75 +			break;
    1.76 +		case KCryptoPluginMacHashMd5:
    1.77 +			{
    1.78 +			hashImpl = CMD5Impl::NewL();
    1.79 +			CleanupClosePushL(*hashImpl);
    1.80 +			iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
    1.81 +			iBase = EHashBased;
    1.82 +			}
    1.83 +			break;
    1.84 +		case KCryptoPluginMacHashMd4:
    1.85 +			{
    1.86 +			hashImpl = CMD4Impl::NewL();
    1.87 +			CleanupClosePushL(*hashImpl);
    1.88 +			iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
    1.89 +			iBase = EHashBased;
    1.90 +			}
    1.91 +			break;
    1.92 +		case KCryptoPluginMacHashSha1:
    1.93 +			{
    1.94 +			hashImpl = CSHA1Impl::NewL();
    1.95 +			CleanupClosePushL(*hashImpl);
    1.96 +			iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
    1.97 +			iBase = EHashBased;
    1.98 +			}
    1.99 +			break;
   1.100 +		case KCryptoPluginMacHashSha224:
   1.101 +			{
   1.102 +			hashImpl = CSHA2Impl::NewL(KCryptoPluginSha224);
   1.103 +			CleanupClosePushL(*hashImpl);
   1.104 +			iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
   1.105 +			iBase = EHashBased;
   1.106 +			}
   1.107 +			break;
   1.108 +		case KCryptoPluginMacHashSha256:
   1.109 +			{
   1.110 +			hashImpl = CSHA2Impl::NewL(KCryptoPluginSha256);
   1.111 +			CleanupClosePushL(*hashImpl);
   1.112 +			iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
   1.113 +			iBase = EHashBased;
   1.114 +			}
   1.115 +			break;
   1.116 +		case KCryptoPluginMacHashSha384:
   1.117 +			{
   1.118 +			hashImpl = CSHA2Impl::NewL(KCryptoPluginSha384);
   1.119 +			CleanupClosePushL(*hashImpl);
   1.120 +			iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
   1.121 +			iBase = EHashBased;
   1.122 +			}
   1.123 +			break;
   1.124 +		case KCryptoPluginMacHashSha512:
   1.125 +			{
   1.126 +			hashImpl = CSHA2Impl::NewL(KCryptoPluginSha512);
   1.127 +			CleanupClosePushL(*hashImpl);
   1.128 +			iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
   1.129 +			iBase = EHashBased;
   1.130 +			}
   1.131 +			break;
   1.132 +		case KCryptoPluginMacAesXcbcMac96:
   1.133 +			{
   1.134 +			CSymmetricCipher* symmetricCipher = NULL;
   1.135 +			CryptoSpi::CSymmetricCipherFactory::CreateSymmetricCipherL(symmetricCipher,
   1.136 +														CryptoSpi::KAesUid, *iKey,
   1.137 +														CryptoSpi::KCryptoModeEncryptUid,
   1.138 +														CryptoSpi::KOperationModeCBCUid,
   1.139 +														CryptoSpi::KPaddingModeNoneUid,
   1.140 +														NULL);
   1.141 +			
   1.142 +			iCmacImpl= CCMacImpl::NewL(*iKey, symmetricCipher, CryptoSpi::KAlgorithmCipherAesXcbcMac96);
   1.143 +			iBase = ECipherBased;
   1.144 +			}
   1.145 +			break;
   1.146 +		case KCryptoPluginMacAesXcbcPrf128:
   1.147 +			{
   1.148 +			TBuf8<16> tempKey;
   1.149 +			tempKey.SetLength(16);
   1.150 +			
   1.151 +			CryptoSpi::CCryptoParams* keyParams = CryptoSpi::CCryptoParams::NewLC();
   1.152 +			keyParams->AddL(tempKey, CryptoSpi::KSymmetricKeyParameterUid);
   1.153 +			CryptoSpi::CKey* key = CryptoSpi::CKey::NewLC(aKey.KeyProperty(),*keyParams);
   1.154 +				
   1.155 +			CSymmetricCipher* symmetricCipher = NULL;
   1.156 +			CryptoSpi::CSymmetricCipherFactory::CreateSymmetricCipherL(symmetricCipher,
   1.157 +														CryptoSpi::KAesUid, *key,
   1.158 +														CryptoSpi::KCryptoModeEncryptUid,
   1.159 +														CryptoSpi::KOperationModeCBCUid,
   1.160 +														CryptoSpi::KPaddingModeNoneUid,
   1.161 +														NULL);
   1.162 +			CleanupStack::PopAndDestroy(2, keyParams); //key and keyParams
   1.163 +			
   1.164 +			iCmacImpl= CCMacImpl::NewL(*iKey, symmetricCipher, CryptoSpi::KAlgorithmCipherAesXcbcPrf128);
   1.165 +			iBase = ECipherBased;
   1.166 +			}
   1.167 +			break;
   1.168 +
   1.169 +		default:
   1.170 +			User::Leave(KErrNotSupported);
   1.171 +		}
   1.172 +
   1.173 +	if(iBase == EHashBased)
   1.174 +		{
   1.175 +		CleanupStack::PopAndDestroy(hashImpl);
   1.176 +		}
   1.177 +	}
   1.178 +
   1.179 +CMacImpl::~CMacImpl()
   1.180 +	{
   1.181 +	delete iKey;
   1.182 +	if(iHmacImpl)
   1.183 +		{
   1.184 +		iHmacImpl->Close();
   1.185 +		}
   1.186 +	delete iCmacImpl;
   1.187 +	}
   1.188 +	
   1.189 +void CMacImpl::Reset()
   1.190 +	{
   1.191 +	if (iBase == EHashBased)
   1.192 +		{
   1.193 +		iHmacImpl->Reset();
   1.194 +		}
   1.195 +	else if (iBase == ECipherBased)
   1.196 +		{
   1.197 +		iCmacImpl->Reset();
   1.198 +		}
   1.199 +	}
   1.200 +	
   1.201 +void CMacImpl::Close()
   1.202 +	{
   1.203 +	delete this;	
   1.204 +	}
   1.205 +	
   1.206 +void CMacImpl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
   1.207 +	{
   1.208 +	aPluginCharacteristics=NULL;
   1.209 +	TInt macNum=sizeof(KMacCharacteristics)/sizeof(TMacCharacteristics*);
   1.210 +	for (TInt i=0;i<macNum;++i)
   1.211 +		{
   1.212 +		if (KMacCharacteristics[i]->iMacChar.iImplementationUID==ImplementationUid().iUid)
   1.213 +			{
   1.214 +			aPluginCharacteristics = KMacCharacteristics[i];
   1.215 +			break;
   1.216 +			}
   1.217 +		}	
   1.218 +	}
   1.219 +
   1.220 +const CExtendedCharacteristics* CMacImpl::GetExtendedCharacteristicsL()
   1.221 +	{
   1.222 +	return (iBase == EHashBased) ? iHmacImpl->GetExtendedCharacteristicsL(): iCmacImpl->GetExtendedCharacteristicsL();
   1.223 +	}
   1.224 +
   1.225 +TAny* CMacImpl::GetExtension(TUid aExtensionId)
   1.226 +	{
   1.227 +	return (iBase == EHashBased) ? iHmacImpl->GetExtension(aExtensionId): NULL;
   1.228 +	}
   1.229 +
   1.230 +TUid CMacImpl::ImplementationUid() const
   1.231 +	{
   1.232 +	return iImplementationUid;
   1.233 +	}
   1.234 +
   1.235 +TPtrC8 CMacImpl::MacL(const TDesC8& aMessage)
   1.236 +	{
   1.237 +	return (iBase == EHashBased) ? iHmacImpl->Hash(aMessage):iCmacImpl->MacL(aMessage);
   1.238 +	}		
   1.239 +	
   1.240 +void CMacImpl::UpdateL(const TDesC8& aMessage)
   1.241 +	{
   1.242 +	(iBase == EHashBased) ? iHmacImpl->Update(aMessage):iCmacImpl->UpdateL(aMessage);
   1.243 +	}
   1.244 +
   1.245 +TPtrC8 CMacImpl::FinalL(const TDesC8& aMessage)
   1.246 +	{
   1.247 +	return (iBase == EHashBased) ? iHmacImpl->Final(aMessage):iCmacImpl->FinalL(aMessage);
   1.248 +	}
   1.249 +
   1.250 +void CMacImpl::ReInitialiseAndSetKeyL(const CKey& aKey)
   1.251 +	{
   1.252 +	delete iKey;
   1.253 +	iKey = NULL;
   1.254 +	iKey = CryptoSpi::CKey::NewL(aKey);
   1.255 +	
   1.256 +	if (iBase == EHashBased)
   1.257 +		{
   1.258 +		iHmacImpl->SetKeyL(aKey);
   1.259 +		iHmacImpl->Reset();
   1.260 +		}
   1.261 +	else if (iBase == ECipherBased)
   1.262 +		{
   1.263 +		iCmacImpl->ReInitialiseAndSetKeyL(aKey);
   1.264 +		}
   1.265 +	}
   1.266 +
   1.267 +MMac* CMacImpl::ReplicateL()
   1.268 +	{
   1.269 +	CMacImpl* that= new(ELeave) CMacImpl();
   1.270 +	CleanupStack::PushL(that);
   1.271 +	that->iImplementationUid = iImplementationUid;
   1.272 +	that->iBase = iBase;
   1.273 +	that->iKey=CKey::NewL(*iKey);
   1.274 +	
   1.275 +	if(iBase == EHashBased)
   1.276 +		{
   1.277 +		that->iHmacImpl=static_cast<CHMacImpl*>(iHmacImpl->ReplicateL());
   1.278 +		}
   1.279 +	else if (iBase == ECipherBased)
   1.280 +		{
   1.281 +		that->iCmacImpl= iCmacImpl->ReplicateL();
   1.282 +		}
   1.283 +	CleanupStack::Pop(that);
   1.284 +	return that;
   1.285 +	}
   1.286 +	
   1.287 +MMac* CMacImpl::CopyL()
   1.288 +	{
   1.289 +	CMacImpl* that= new(ELeave) CMacImpl();
   1.290 +	CleanupStack::PushL(that);
   1.291 +	that->iImplementationUid = iImplementationUid;
   1.292 +	that->iBase = iBase;
   1.293 +	that->iKey=CKey::NewL(*iKey);
   1.294 +
   1.295 +	if(iBase == EHashBased)
   1.296 +		{
   1.297 +		that->iHmacImpl=static_cast<CHMacImpl*>(iHmacImpl->CopyL());
   1.298 +		}
   1.299 +	else if (iBase == ECipherBased)
   1.300 +		{
   1.301 +		that->iCmacImpl= iCmacImpl->CopyL();
   1.302 +		}
   1.303 +	CleanupStack::Pop(that);
   1.304 +	return that;
   1.305 +	}