Update contrib.
2 * Copyright (c) 2005-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.
30 // HMAC implementation
36 CHMAC::CHMAC(CMessageDigest* aDigest)
39 iInnerPad(KMaxBlockSize),
40 iOuterPad(KMaxBlockSize)
43 CHMAC::CHMAC(const CHMAC& aMD)
46 iInnerPad(aMD.iInnerPad),
47 iOuterPad(aMD.iOuterPad),
48 iBlockSize(aMD.iBlockSize)
51 EXPORT_C CHMAC::~CHMAC(void)
56 EXPORT_C CHMAC* CHMAC::NewL(const TDesC8& aKey,CMessageDigest* aDigest)
58 CHMAC* self=new(ELeave) CHMAC(aDigest);
59 CleanupStack::PushL(self);
60 self->InitialiseL(aKey);
61 CleanupStack::Pop(self);
64 void CHMAC::InitialiseL(const TDesC8& aKey)
71 if( (TUint32)aKey.Size() > iBlockSize)
73 iInnerPad = iDigest->Final(aKey);
81 for (i=iInnerPad.Size();i<iBlockSize;i++)
86 const TUint8 Magic1=0x36, Magic2=0x5c;
87 for (i=0;i<iBlockSize;i++)
93 iDigest->Hash(iInnerPad);
97 void CHMAC::InitBlockSizeL()
100 iBlockSize = iDigest->BlockSize();
101 if(iBlockSize > KMaxBlockSize)
103 User::Leave(KErrNotSupported);
106 iInnerPad.SetLength(iBlockSize);
107 iOuterPad.SetLength(iBlockSize);
108 iInnerPadCopy.SetLength(iBlockSize);
109 iOuterPadCopy.SetLength(iBlockSize);
112 EXPORT_C CMessageDigest* CHMAC::CopyL(void)
114 CHMAC* that=new(ELeave) CHMAC(*this);
115 CleanupStack::PushL(that);
116 that->iDigest=iDigest ? iDigest->CopyL() : NULL;
120 EXPORT_C CMessageDigest* CHMAC::ReplicateL(void)
122 CHMAC* that=new(ELeave) CHMAC(*this);
123 CleanupStack::PushL(that);
124 that->iDigest=iDigest ? iDigest->ReplicateL() : NULL;
129 EXPORT_C TInt CHMAC::BlockSize(void)
134 EXPORT_C TInt CHMAC::HashSize(void)
136 return iDigest ? iDigest->HashSize() : 0;
138 EXPORT_C void CHMAC::Reset(void)
143 iDigest->Update(iInnerPad);
147 // JCS, There may be a more efficient method but I can't find it
148 // because using the DoFinal/DoUpdate functions directly calls
149 // Store/Restore at inappropriate times and scribbles over stored
151 // This is the only way I've found to both generate a hash value
152 // and get this in the correctly updated state
153 EXPORT_C TPtrC8 CHMAC::Hash(const TDesC8& aMessage)
155 TPtrC8 ptr(KNullDesC8());
156 TPtrC8 finalPtr(KNullDesC8());
160 ptr.Set(iDigest->Final(aMessage));
161 iDigest->Update(iOuterPad);
162 finalPtr.Set(iDigest->Final(ptr));
166 iDigest->Update(aMessage);
171 EXPORT_C void CHMAC::Update(const TDesC8& aMessage)
175 iDigest->Update(aMessage);
179 EXPORT_C TPtrC8 CHMAC::Final(const TDesC8& aMessage)
181 TPtrC8 ptr(KNullDesC8());
184 ptr.Set(iDigest->Final(aMessage));
185 iDigest->Update(iOuterPad);
192 EXPORT_C TPtrC8 CHMAC::Final()
194 TPtrC8 ptr(KNullDesC8());
197 ptr.Set(iDigest->Final());
198 iDigest->Update(iOuterPad);
205 void CHMAC::RestoreState()
207 iOuterPad.Copy(iOuterPadCopy);
208 iInnerPad.Copy(iInnerPadCopy);
210 iDigest->RestoreState();
213 void CHMAC::StoreState()
215 iOuterPadCopy.Copy(iOuterPad);
216 iInnerPadCopy.Copy(iInnerPad);
218 iDigest->StoreState();