os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/softwarehashbase.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/softwarehashbase.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,326 @@
     1.4 +/*
     1.5 +* Copyright (c) 2007-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 hash base class implementation
    1.19 +* software hash base class implementation
    1.20 +*
    1.21 +*/
    1.22 +
    1.23 +
    1.24 +/**
    1.25 + @file
    1.26 +*/
    1.27 +
    1.28 +#include "softwarehashbase.h"
    1.29 +
    1.30 +#include <cryptospi/hashplugin.h>
    1.31 +#include "pluginconfig.h"
    1.32 +#include "keys.h"
    1.33 +#include "md2impl.h"
    1.34 +#include "md5impl.h"
    1.35 +#include "md4impl.h"
    1.36 +#include "sha1impl.h"
    1.37 +#include "sha2impl.h"
    1.38 +#include "hmacimpl.h"
    1.39 +
    1.40 +using namespace SoftwareCrypto;
    1.41 +
    1.42 +CSoftwareHash* CSoftwareHash::NewL(TUid aAlgorithm, TUid aOperationMode, const CKey* aKey)
    1.43 +	{
    1.44 +	CSoftwareHash* self=NewLC(aAlgorithm, aOperationMode, aKey);
    1.45 +	CleanupStack::Pop();
    1.46 +	return self;
    1.47 +	}
    1.48 +
    1.49 +CSoftwareHash* CSoftwareHash::NewL(TUid aAlgorithm)
    1.50 +	{
    1.51 +	CSoftwareHash* self=NewLC(aAlgorithm, CryptoSpi::KHashModeUid, NULL);
    1.52 +	CleanupStack::Pop();
    1.53 +	return self;
    1.54 +	}
    1.55 +
    1.56 +CSoftwareHash* CSoftwareHash::NewLC(TUid aAlgorithm, TUid aOperationMode, const CKey* aKey)
    1.57 +	{
    1.58 +	CSoftwareHash* self=new (ELeave) CSoftwareHash();
    1.59 +	CleanupStack::PushL(self);
    1.60 +	self->ConstructL(aAlgorithm, aOperationMode, aKey);
    1.61 +	return self;
    1.62 +	}
    1.63 +
    1.64 +CSoftwareHash::CSoftwareHash()
    1.65 +	{		
    1.66 +	}
    1.67 +
    1.68 +CSoftwareHash::~CSoftwareHash()
    1.69 +	{
    1.70 +	if (iHashImpl)
    1.71 +		{
    1.72 +		iHashImpl->Close();			
    1.73 +		}
    1.74 +
    1.75 +	if (iHmacImpl)
    1.76 +		{
    1.77 +		iHmacImpl->Close();
    1.78 +		}
    1.79 +	delete iKey;
    1.80 +	}
    1.81 +
    1.82 +void CSoftwareHash::ConstructL(TUid aAlgorithm, TUid aOperationMode, const CKey* aKey)
    1.83 +	{
    1.84 +	//
    1.85 +	// Only Hash and Hmac mode are supported.
    1.86 +	//
    1.87 +	if (aOperationMode!=KHmacModeUid && aOperationMode!=KHashModeUid)
    1.88 +		{
    1.89 +		User::Leave(KErrNotSupported);			
    1.90 +		}
    1.91 +		
    1.92 +	//Set the key if there is one
    1.93 +	if (aKey)
    1.94 +		{
    1.95 +		SetKeyL(*aKey);
    1.96 +		}
    1.97 +	
    1.98 +	switch (aAlgorithm.iUid)
    1.99 +		{
   1.100 +	case KCryptoPluginMd2:
   1.101 +		{
   1.102 +		iHashImpl=CMD2Impl::NewL();
   1.103 +		}
   1.104 +		break;
   1.105 +		
   1.106 +	case KCryptoPluginMd5:
   1.107 +		{
   1.108 +		iHashImpl=CMD5Impl::NewL();
   1.109 +		}
   1.110 +		break;
   1.111 +		
   1.112 +	case KCryptoPluginMd4:
   1.113 +		{
   1.114 +		iHashImpl=CMD4Impl::NewL();
   1.115 +		}
   1.116 +		break;
   1.117 +
   1.118 +	case KCryptoPluginSha1:
   1.119 +		{
   1.120 +		iHashImpl=CSHA1Impl::NewL();
   1.121 +		}
   1.122 +		break;
   1.123 +		
   1.124 +	case KCryptoPluginSha224:
   1.125 +	case KCryptoPluginSha256:
   1.126 +	case KCryptoPluginSha384:
   1.127 +	case KCryptoPluginSha512:
   1.128 +		{
   1.129 +		iHashImpl=CSHA2Impl::NewL(aAlgorithm.iUid);
   1.130 +		}
   1.131 +		break;
   1.132 +		
   1.133 +	default:
   1.134 +		User::Leave(KErrNotSupported);
   1.135 +		}
   1.136 +		
   1.137 +	SetOperationModeL(aOperationMode);
   1.138 +	}
   1.139 +
   1.140 +void CSoftwareHash::SetOperationModeL(TUid aOperationMode)
   1.141 +	{
   1.142 +	switch (aOperationMode.iUid)
   1.143 +		{
   1.144 +	case KHmacMode:
   1.145 +		{
   1.146 +		//
   1.147 +		//Only create hmac implementation if there isn't one
   1.148 +		//
   1.149 +		if (!iHmacImpl)
   1.150 +			{
   1.151 +			if (iKey)
   1.152 +				{
   1.153 +				iHmacImpl=CHMacImpl::NewL(*iKey, iHashImpl);	
   1.154 +				}
   1.155 +			else
   1.156 +				{
   1.157 +				iHmacImpl=CHMacImpl::NewL(iHashImpl);
   1.158 +				}							
   1.159 +			}
   1.160 +		}
   1.161 +		break;
   1.162 +		
   1.163 +	case KHashMode:
   1.164 +		{
   1.165 +		Reset();	
   1.166 +		}
   1.167 +		break;
   1.168 +		
   1.169 +	default:
   1.170 +		User::Leave(KErrNotSupported);
   1.171 +		}
   1.172 +		
   1.173 +	//
   1.174 +	// Set the operation mode.
   1.175 +	//
   1.176 +	iOperationMode=aOperationMode;
   1.177 +	}
   1.178 +
   1.179 +MSoftwareHash* CSoftwareHash::Impl()
   1.180 +	{
   1.181 +	MSoftwareHash* impl=NULL;
   1.182 +	if (iOperationMode==KHashModeUid)
   1.183 +		{
   1.184 +		impl=iHashImpl;
   1.185 +		}
   1.186 +	else if (iOperationMode==KHmacModeUid && iKey)
   1.187 +			{
   1.188 +			impl=iHmacImpl;
   1.189 +			}
   1.190 +	return impl;
   1.191 +	}
   1.192 +	
   1.193 +void CSoftwareHash::SetKeyL(const CKey& aKey)
   1.194 +	{
   1.195 +	Reset();
   1.196 +	delete iKey;
   1.197 +	iKey=CKey::NewL(aKey);
   1.198 +	if (iHmacImpl)
   1.199 +		{
   1.200 +		iHmacImpl->SetKeyL(aKey);
   1.201 +		}
   1.202 +	}
   1.203 +	
   1.204 +void CSoftwareHash::Reset()
   1.205 +	{
   1.206 +	if (iHashImpl)
   1.207 +		{
   1.208 +		iHashImpl->Reset();			
   1.209 +		}
   1.210 +		
   1.211 +	if (iHmacImpl)
   1.212 +		{
   1.213 +		iHmacImpl->Reset();			
   1.214 +		}
   1.215 +	}
   1.216 +	
   1.217 +void CSoftwareHash::Close()
   1.218 +	{
   1.219 +	delete this;
   1.220 +	}
   1.221 +	
   1.222 +void CSoftwareHash::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
   1.223 +	{
   1.224 +	MSoftwareHash* impl=Impl();
   1.225 +	if (impl)
   1.226 +		{
   1.227 +		impl->GetCharacteristicsL(aPluginCharacteristics);			
   1.228 +		}
   1.229 +	else
   1.230 +		{
   1.231 +		User::Leave(KErrNotReady);
   1.232 +		}		
   1.233 +	}
   1.234 +	
   1.235 +const CExtendedCharacteristics* CSoftwareHash::GetExtendedCharacteristicsL()
   1.236 +	{
   1.237 +	MSoftwareHash* impl=Impl();
   1.238 +	if (!impl)
   1.239 +		{
   1.240 +		User::Leave(KErrNotReady);	
   1.241 +		}
   1.242 +	return impl->GetExtendedCharacteristicsL();
   1.243 +	}
   1.244 +	
   1.245 +TAny* CSoftwareHash::GetExtension(TUid aExtensionId)
   1.246 +	{
   1.247 +	MSoftwareHash* impl=Impl();
   1.248 +	if (impl)
   1.249 +		{
   1.250 +		return impl->GetExtension(aExtensionId);			
   1.251 +		}
   1.252 +	else
   1.253 +		{
   1.254 +		return NULL;	
   1.255 +		}
   1.256 +	}
   1.257 +
   1.258 +TPtrC8 CSoftwareHash::Hash(const TDesC8& aMessage)
   1.259 +	{
   1.260 +	MSoftwareHash* impl=Impl();
   1.261 +	if (impl)
   1.262 +		{
   1.263 +		return impl->Hash(aMessage);			
   1.264 +		}
   1.265 +	else
   1.266 +		{
   1.267 +		return KNullDesC8();
   1.268 +		}
   1.269 +	}
   1.270 +	
   1.271 +void CSoftwareHash::Update(const TDesC8& aMessage)
   1.272 +	{
   1.273 +	MSoftwareHash* impl=Impl();
   1.274 +	if (impl)
   1.275 +		{
   1.276 +		return impl->Update(aMessage);
   1.277 +		}
   1.278 +	}
   1.279 +	
   1.280 +TPtrC8 CSoftwareHash::Final(const TDesC8& aMessage)
   1.281 +	{
   1.282 +	MSoftwareHash* impl=Impl();
   1.283 +	if (impl)
   1.284 +		{
   1.285 +		return impl->Final(aMessage);
   1.286 +		}
   1.287 +	else
   1.288 +		{
   1.289 +		return KNullDesC8();
   1.290 +		}
   1.291 +	}
   1.292 +	
   1.293 +MHash* CSoftwareHash::ReplicateL()
   1.294 +	{
   1.295 +	CSoftwareHash* that=new(ELeave)CSoftwareHash();
   1.296 +	CleanupStack::PushL(that);
   1.297 +	if (this->iKey)
   1.298 +		{
   1.299 +		that->iKey=CKey::NewL(*this->iKey);			
   1.300 +		}
   1.301 +	that->iOperationMode=this->iOperationMode;
   1.302 +	that->iHashImpl=static_cast<MSoftwareHash*>(this->iHashImpl->ReplicateL());
   1.303 +	if (this->iHmacImpl)
   1.304 +		{
   1.305 +		that->iHmacImpl=static_cast<MSoftwareHash*>(this->iHmacImpl->ReplicateL());			
   1.306 +		}
   1.307 +	CleanupStack::Pop();
   1.308 +	return that;
   1.309 +	}
   1.310 +	
   1.311 +MHash* CSoftwareHash::CopyL()
   1.312 +	{
   1.313 +	CSoftwareHash* that=new(ELeave)CSoftwareHash();
   1.314 +	CleanupStack::PushL(that);
   1.315 +	if (this->iKey)
   1.316 +		{
   1.317 +		that->iKey=CKey::NewL(*this->iKey);			
   1.318 +		}
   1.319 +	that->iOperationMode=this->iOperationMode;
   1.320 +	that->iHashImpl=static_cast<MSoftwareHash*>(this->iHashImpl->CopyL());
   1.321 +	if (this->iHmacImpl)
   1.322 +		{
   1.323 +		that->iHmacImpl=static_cast<MSoftwareHash*>(this->iHmacImpl->CopyL());
   1.324 +		}
   1.325 +	CleanupStack::Pop();
   1.326 +	return that;
   1.327 +	}
   1.328 +		
   1.329 +