os/security/crypto/weakcryptospi/test/tplugins/src/hmacimpl.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcryptospi/test/tplugins/src/hmacimpl.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,247 @@
     1.4 +/*
     1.5 +* Copyright (c) 2006-2010 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 hmac implementation
    1.19 +* software hmac implementation
    1.20 +*
    1.21 +*/
    1.22 +
    1.23 +
    1.24 +/**
    1.25 + @file
    1.26 +*/
    1.27 +
    1.28 +#include "hmacimpl.h"
    1.29 +#include "pluginconfig.h"
    1.30 +#include "md2impl.h"
    1.31 +#include "md5impl.h"
    1.32 +#include "sha1impl.h"
    1.33 +#include <cryptospi/keys.h>
    1.34 +
    1.35 +using namespace SoftwareCrypto;
    1.36 +
    1.37 +CHMacImpl* CHMacImpl::NewL(const CKey& aKey, MSoftwareHash* aHash)
    1.38 +	{
    1.39 +	CHMacImpl* self=NewLC(aKey, aHash);
    1.40 +	CleanupStack::Pop();
    1.41 +	return self;
    1.42 +	}
    1.43 +	
    1.44 +CHMacImpl* CHMacImpl::NewLC(const CKey& aKey, MSoftwareHash* aHash)
    1.45 +	{
    1.46 +	CHMacImpl* self=new(ELeave) CHMacImpl();
    1.47 +	CleanupStack::PushL(self);
    1.48 +	self->ConstructL(aKey, aHash);
    1.49 +	return self;
    1.50 +	}
    1.51 +
    1.52 +CHMacImpl* CHMacImpl::NewL(MSoftwareHash* aHash)
    1.53 +	{
    1.54 +	CHMacImpl* self=NewLC(aHash);
    1.55 +	CleanupStack::Pop();
    1.56 +	return self;
    1.57 +	}
    1.58 +	
    1.59 +CHMacImpl* CHMacImpl::NewLC(MSoftwareHash* aHash)
    1.60 +	{
    1.61 +	CHMacImpl* self=new(ELeave) CHMacImpl();
    1.62 +	CleanupStack::PushL(self);
    1.63 +	self->ConstructL(aHash);
    1.64 +	return self;
    1.65 +	}
    1.66 +
    1.67 +
    1.68 +CHMacImpl::CHMacImpl()
    1.69 +	: iInnerPad(KHMacPad), iOuterPad(KHMacPad)
    1.70 +	{
    1.71 +	}
    1.72 +
    1.73 +CHMacImpl::CHMacImpl(const CHMacImpl& aMD)
    1.74 +	: iDigest(NULL), iInnerPad(aMD.iInnerPad), iOuterPad(aMD.iOuterPad)
    1.75 +	{
    1.76 +	}
    1.77 +
    1.78 +CHMacImpl::~CHMacImpl()
    1.79 +	{
    1.80 +	if (iDigest)
    1.81 +		{
    1.82 +		iDigest->Close();	
    1.83 +		}
    1.84 +	}
    1.85 +
    1.86 +void CHMacImpl::ConstructL(const CKey& aKey, MSoftwareHash* aHash)
    1.87 +	{
    1.88 +	//Clone the hash implementation
    1.89 +	iDigest=static_cast<MSoftwareHash*>(aHash->ReplicateL());
    1.90 +	const TDesC8& keyContent=aKey.GetTDesC8L(KHmacKeyParameterUid);
    1.91 +	Initialise(keyContent);	
    1.92 +	}
    1.93 +
    1.94 +void CHMacImpl::ConstructL(MSoftwareHash* aHash)
    1.95 +	{
    1.96 +	//Clone the hash implementation
    1.97 +	iDigest=static_cast<MSoftwareHash*>(aHash->ReplicateL());
    1.98 +	}
    1.99 +
   1.100 +	
   1.101 +void CHMacImpl::Initialise(const TDesC8& aKey)
   1.102 +	{
   1.103 +	// initialisation
   1.104 +	if (iDigest)
   1.105 +		{
   1.106 +		iDigest->Reset();
   1.107 +		if( (TUint32)aKey.Size() > KHMacPad)
   1.108 +			{
   1.109 +			iInnerPad = iDigest->Final(aKey);
   1.110 +			}
   1.111 +		else 
   1.112 +			{
   1.113 +			iInnerPad = aKey;
   1.114 +			}
   1.115 +			
   1.116 +		TUint i;
   1.117 +		for (i=iInnerPad.Size();i<KHMacPad;i++)
   1.118 +			iInnerPad.Append(0);
   1.119 +
   1.120 +		iOuterPad=iInnerPad;
   1.121 +
   1.122 +		const TUint8 Magic1=0x36, Magic2=0x5c;
   1.123 +		for (i=0;i<KHMacPad;i++)
   1.124 +			{
   1.125 +			iInnerPad[i]^=Magic1;
   1.126 +			iOuterPad[i]^=Magic2;
   1.127 +			}
   1.128 +		//start inner hash
   1.129 +		iDigest->Hash(iInnerPad);
   1.130 +		}
   1.131 +	}
   1.132 +	
   1.133 +MHash* CHMacImpl::CopyL()
   1.134 +	{
   1.135 +	CHMacImpl* that=new(ELeave) CHMacImpl(*this);
   1.136 +	CleanupStack::PushL(that);
   1.137 +	that->iDigest=iDigest ? static_cast<MSoftwareHash*>(iDigest->CopyL()) : NULL;
   1.138 +	CleanupStack::Pop();
   1.139 +	return that;
   1.140 +	}
   1.141 +	
   1.142 +MHash* CHMacImpl::ReplicateL()
   1.143 +	{
   1.144 +	CHMacImpl* that=new(ELeave) CHMacImpl(*this);
   1.145 +	CleanupStack::PushL(that);
   1.146 +	that->iDigest=iDigest ? static_cast<MSoftwareHash*>(iDigest->ReplicateL()) : NULL;
   1.147 +	that->Reset();
   1.148 +	CleanupStack::Pop();
   1.149 +	return that;
   1.150 +	}
   1.151 +	
   1.152 +void CHMacImpl::Reset()
   1.153 +	{
   1.154 +	if (iDigest)
   1.155 +		{
   1.156 +		iDigest->Reset();
   1.157 +		iDigest->Update(iInnerPad);
   1.158 +		}
   1.159 +	}
   1.160 +
   1.161 +TPtrC8 CHMacImpl::Hash(const TDesC8& aMessage)
   1.162 +	{
   1.163 +	TPtrC8 ptr(KNullDesC8());
   1.164 +	TPtrC8 finalPtr(KNullDesC8());
   1.165 +	StoreState();
   1.166 +	if (iDigest)
   1.167 +		{
   1.168 +		ptr.Set(iDigest->Final(aMessage));
   1.169 +		iDigest->Update(iOuterPad);
   1.170 +		finalPtr.Set(iDigest->Final(ptr));
   1.171 +		}
   1.172 +
   1.173 +	RestoreState();
   1.174 +	iDigest->Update(aMessage);
   1.175 +
   1.176 +	return (finalPtr);
   1.177 +	}
   1.178 +
   1.179 +void CHMacImpl::Update(const TDesC8& aMessage)
   1.180 +	{
   1.181 +	if(iDigest)
   1.182 +		{
   1.183 +		iDigest->Update(aMessage);
   1.184 +		}
   1.185 +	}
   1.186 +
   1.187 +TPtrC8 CHMacImpl::Final(const TDesC8& aMessage)
   1.188 +	{
   1.189 +	TPtrC8 ptr(KNullDesC8());
   1.190 +	if(iDigest)
   1.191 +		{
   1.192 +		ptr.Set(iDigest->Final(aMessage));
   1.193 +		iDigest->Update(iOuterPad);
   1.194 +		iDigest->Final(ptr);
   1.195 +		Reset();
   1.196 +		}
   1.197 +	return (ptr);
   1.198 +	}
   1.199 +	
   1.200 +void CHMacImpl::RestoreState()
   1.201 +	{
   1.202 +	iOuterPad.Copy(iOuterPadCopy);
   1.203 +	iInnerPad.Copy(iInnerPadCopy);
   1.204 +	if (iDigest)
   1.205 +		{
   1.206 +		iDigest->RestoreState();
   1.207 +		}
   1.208 +	}
   1.209 +
   1.210 +void CHMacImpl::StoreState()
   1.211 +	{
   1.212 +	iOuterPadCopy.Copy(iOuterPad);
   1.213 +	iInnerPadCopy.Copy(iInnerPad);
   1.214 +	if (iDigest)
   1.215 +		{
   1.216 +		iDigest->StoreState();	
   1.217 +		}
   1.218 +	}
   1.219 +
   1.220 +void CHMacImpl::SetKeyL(const CKey& aKey)
   1.221 +	{
   1.222 +	const TDesC8& keyContent=aKey.GetTDesC8L(KHmacKeyParameterUid);
   1.223 +	Initialise(keyContent);	
   1.224 +	}
   1.225 +
   1.226 +void CHMacImpl::Close()
   1.227 +	{
   1.228 +	delete this;	
   1.229 +	}
   1.230 +	
   1.231 +void CHMacImpl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
   1.232 +	{
   1.233 +	iDigest->GetCharacteristicsL(aPluginCharacteristics);	
   1.234 +	}
   1.235 +	
   1.236 +const CExtendedCharacteristics* CHMacImpl::GetExtendedCharacteristicsL()
   1.237 +	{
   1.238 +	return iDigest->GetExtendedCharacteristicsL();
   1.239 +	}
   1.240 +	
   1.241 +TAny* CHMacImpl::GetExtension(TUid /*aExtensionId*/)
   1.242 +	{
   1.243 +	return NULL;	
   1.244 +	}
   1.245 +	
   1.246 +void CHMacImpl::SetOperationModeL(TUid /*aOperationMode*/)
   1.247 +	{
   1.248 +	User::Leave(KErrNotSupported);
   1.249 +	}
   1.250 +