First public contribution.
2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * software hmac implementation
16 * software hmac implementation
26 #include "pluginconfig.h"
34 using namespace SoftwareCrypto;
36 CHMacImpl* CHMacImpl::NewL(const CKey& aKey, MSoftwareHash* aHash)
38 CHMacImpl* self=NewLC(aKey, aHash);
43 CHMacImpl* CHMacImpl::NewLC(const CKey& aKey, MSoftwareHash* aHash)
45 CHMacImpl* self=new(ELeave) CHMacImpl();
46 CleanupStack::PushL(self);
47 self->ConstructL(aKey, aHash);
51 CHMacImpl* CHMacImpl::NewL(MSoftwareHash* aHash)
53 CHMacImpl* self=NewLC(aHash);
58 CHMacImpl* CHMacImpl::NewLC(MSoftwareHash* aHash)
60 CHMacImpl* self=new(ELeave) CHMacImpl();
61 CleanupStack::PushL(self);
62 self->ConstructL(aHash);
67 CHMacImpl::CHMacImpl()
71 CHMacImpl::CHMacImpl(const CHMacImpl& aMD)
72 : iDigest(NULL), iInnerPad(aMD.iInnerPad),
73 iOuterPad(aMD.iOuterPad), iBlockSize(aMD.iBlockSize)
77 CHMacImpl::~CHMacImpl()
85 void CHMacImpl::ConstructL(const CKey& aKey, MSoftwareHash* aHash)
87 //Clone the hash implementation
88 iDigest=static_cast<MSoftwareHash*>(aHash->ReplicateL());
90 const TDesC8& keyContent=aKey.GetTDesC8L(KHmacKeyParameterUid);
91 Initialise(keyContent);
94 void CHMacImpl::ConstructL(MSoftwareHash* aHash)
96 //Clone the hash implementation
97 iDigest=static_cast<MSoftwareHash*>(aHash->ReplicateL());
101 void CHMacImpl::InitBlockSizeL()
103 const TCharacteristics* ptr(NULL);
104 iDigest->GetCharacteristicsL(ptr);
105 const THashCharacteristics* hashPtr=static_cast<const THashCharacteristics*>(ptr);
106 iBlockSize = hashPtr->iBlockSize/8;
108 iInnerPad.SetLength(iBlockSize);
109 iOuterPad.SetLength(iBlockSize);
110 iInnerPadCopy.SetLength(iBlockSize);
111 iOuterPadCopy.SetLength(iBlockSize);
114 void CHMacImpl::Initialise(const TDesC8& aKey)
120 if( (TUint32)aKey.Size() > iBlockSize)
122 iInnerPad = iDigest->Final(aKey);
130 for (i=iInnerPad.Size();i<iBlockSize;i++)
135 const TUint8 Magic1=0x36, Magic2=0x5c;
136 for (i=0;i<iBlockSize;i++)
138 iInnerPad[i]^=Magic1;
139 iOuterPad[i]^=Magic2;
142 iDigest->Hash(iInnerPad);
146 MHash* CHMacImpl::CopyL()
148 CHMacImpl* that=new(ELeave) CHMacImpl(*this);
149 CleanupStack::PushL(that);
150 that->iDigest=iDigest ? static_cast<MSoftwareHash*>(iDigest->CopyL()) : NULL;
155 MHash* CHMacImpl::ReplicateL()
157 CHMacImpl* that=new(ELeave) CHMacImpl(*this);
158 CleanupStack::PushL(that);
159 that->iDigest=iDigest ? static_cast<MSoftwareHash*>(iDigest->ReplicateL()) : NULL;
165 void CHMacImpl::Reset()
170 iDigest->Update(iInnerPad);
174 TPtrC8 CHMacImpl::Hash(const TDesC8& aMessage)
176 TPtrC8 ptr(KNullDesC8());
177 TPtrC8 finalPtr(KNullDesC8());
181 ptr.Set(iDigest->Final(aMessage));
182 iDigest->Update(iOuterPad);
183 finalPtr.Set(iDigest->Final(ptr));
190 iDigest->Update(aMessage);
198 void CHMacImpl::Update(const TDesC8& aMessage)
202 iDigest->Update(aMessage);
206 TPtrC8 CHMacImpl::Final(const TDesC8& aMessage)
208 TPtrC8 ptr(KNullDesC8());
211 ptr.Set(iDigest->Final(aMessage));
212 iDigest->Update(iOuterPad);
219 void CHMacImpl::RestoreState()
221 iOuterPad.Copy(iOuterPadCopy);
222 iInnerPad.Copy(iInnerPadCopy);
225 iDigest->RestoreState();
229 void CHMacImpl::StoreState()
231 iOuterPadCopy.Copy(iOuterPad);
232 iInnerPadCopy.Copy(iInnerPad);
235 iDigest->StoreState();
239 void CHMacImpl::SetKeyL(const CKey& aKey)
241 const TDesC8& keyContent=aKey.GetTDesC8L(KHmacKeyParameterUid);
242 Initialise(keyContent);
245 void CHMacImpl::Close()
250 void CHMacImpl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
252 iDigest->GetCharacteristicsL(aPluginCharacteristics);
255 const CExtendedCharacteristics* CHMacImpl::GetExtendedCharacteristicsL()
257 return iDigest->GetExtendedCharacteristicsL();
260 // Methods which are not supported can be excluded from the coverage.
261 #ifdef _BullseyeCoverage
262 #pragma suppress_warnings on
263 #pragma BullseyeCoverage off
264 #pragma suppress_warnings off
267 TAny* CHMacImpl::GetExtension(TUid /*aExtensionId*/)
272 void CHMacImpl::SetOperationModeL(TUid /*aOperationMode*/)
274 User::Leave(KErrNotSupported);