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