First public contribution.
2 * Copyright (c) 2006-2010 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"
30 #include <cryptospi/keys.h>
32 using namespace SoftwareCrypto;
34 CHMacImpl* CHMacImpl::NewL(const CKey& aKey, MSoftwareHash* aHash)
36 CHMacImpl* self=NewLC(aKey, aHash);
41 CHMacImpl* CHMacImpl::NewLC(const CKey& aKey, MSoftwareHash* aHash)
43 CHMacImpl* self=new(ELeave) CHMacImpl();
44 CleanupStack::PushL(self);
45 self->ConstructL(aKey, aHash);
49 CHMacImpl* CHMacImpl::NewL(MSoftwareHash* aHash)
51 CHMacImpl* self=NewLC(aHash);
56 CHMacImpl* CHMacImpl::NewLC(MSoftwareHash* aHash)
58 CHMacImpl* self=new(ELeave) CHMacImpl();
59 CleanupStack::PushL(self);
60 self->ConstructL(aHash);
65 CHMacImpl::CHMacImpl()
66 : iInnerPad(KHMacPad), iOuterPad(KHMacPad)
70 CHMacImpl::CHMacImpl(const CHMacImpl& aMD)
71 : iDigest(NULL), iInnerPad(aMD.iInnerPad), iOuterPad(aMD.iOuterPad)
75 CHMacImpl::~CHMacImpl()
83 void CHMacImpl::ConstructL(const CKey& aKey, MSoftwareHash* aHash)
85 //Clone the hash implementation
86 iDigest=static_cast<MSoftwareHash*>(aHash->ReplicateL());
87 const TDesC8& keyContent=aKey.GetTDesC8L(KHmacKeyParameterUid);
88 Initialise(keyContent);
91 void CHMacImpl::ConstructL(MSoftwareHash* aHash)
93 //Clone the hash implementation
94 iDigest=static_cast<MSoftwareHash*>(aHash->ReplicateL());
98 void CHMacImpl::Initialise(const TDesC8& aKey)
104 if( (TUint32)aKey.Size() > KHMacPad)
106 iInnerPad = iDigest->Final(aKey);
114 for (i=iInnerPad.Size();i<KHMacPad;i++)
119 const TUint8 Magic1=0x36, Magic2=0x5c;
120 for (i=0;i<KHMacPad;i++)
122 iInnerPad[i]^=Magic1;
123 iOuterPad[i]^=Magic2;
126 iDigest->Hash(iInnerPad);
130 MHash* CHMacImpl::CopyL()
132 CHMacImpl* that=new(ELeave) CHMacImpl(*this);
133 CleanupStack::PushL(that);
134 that->iDigest=iDigest ? static_cast<MSoftwareHash*>(iDigest->CopyL()) : NULL;
139 MHash* CHMacImpl::ReplicateL()
141 CHMacImpl* that=new(ELeave) CHMacImpl(*this);
142 CleanupStack::PushL(that);
143 that->iDigest=iDigest ? static_cast<MSoftwareHash*>(iDigest->ReplicateL()) : NULL;
149 void CHMacImpl::Reset()
154 iDigest->Update(iInnerPad);
158 TPtrC8 CHMacImpl::Hash(const TDesC8& aMessage)
160 TPtrC8 ptr(KNullDesC8());
161 TPtrC8 finalPtr(KNullDesC8());
165 ptr.Set(iDigest->Final(aMessage));
166 iDigest->Update(iOuterPad);
167 finalPtr.Set(iDigest->Final(ptr));
171 iDigest->Update(aMessage);
176 void CHMacImpl::Update(const TDesC8& aMessage)
180 iDigest->Update(aMessage);
184 TPtrC8 CHMacImpl::Final(const TDesC8& aMessage)
186 TPtrC8 ptr(KNullDesC8());
189 ptr.Set(iDigest->Final(aMessage));
190 iDigest->Update(iOuterPad);
197 void CHMacImpl::RestoreState()
199 iOuterPad.Copy(iOuterPadCopy);
200 iInnerPad.Copy(iInnerPadCopy);
203 iDigest->RestoreState();
207 void CHMacImpl::StoreState()
209 iOuterPadCopy.Copy(iOuterPad);
210 iInnerPadCopy.Copy(iInnerPad);
213 iDigest->StoreState();
217 void CHMacImpl::SetKeyL(const CKey& aKey)
219 const TDesC8& keyContent=aKey.GetTDesC8L(KHmacKeyParameterUid);
220 Initialise(keyContent);
223 void CHMacImpl::Close()
228 void CHMacImpl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
230 iDigest->GetCharacteristicsL(aPluginCharacteristics);
233 const CExtendedCharacteristics* CHMacImpl::GetExtendedCharacteristicsL()
235 return iDigest->GetExtendedCharacteristicsL();
238 TAny* CHMacImpl::GetExtension(TUid /*aExtensionId*/)
243 void CHMacImpl::SetOperationModeL(TUid /*aOperationMode*/)
245 User::Leave(KErrNotSupported);