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.
22 #include <cryptospi/cryptospidef.h>
23 #include <cryptospi/cryptohashapi.h>
24 #include <cryptospi/plugincharacteristics.h>
31 // HMAC implementation
39 CHMAC::CHMAC(CMessageDigest* aDigest)
42 iInnerPad(KMaxBlockSize),
43 iOuterPad(KMaxBlockSize)
47 CHMAC::CHMAC(const CHMAC& aMD)
50 iInnerPad(aMD.iInnerPad),
51 iOuterPad(aMD.iOuterPad),
52 iBlockSize(aMD.iBlockSize)
56 EXPORT_C CHMAC::~CHMAC(void)
61 EXPORT_C CHMAC* CHMAC::NewL(const TDesC8& aKey,CMessageDigest* aDigest)
63 CHMAC* self = CHMACShim::NewL(aKey, aDigest);
66 // not able to use CryptoSpi, possibly due to an exterally
67 // derived legacy class so fallback to old implementation.
68 self=new(ELeave) CHMAC(aDigest);
69 CleanupStack::PushL(self);
70 self->InitialiseL(aKey);
71 CleanupStack::Pop(self);
77 // These methods are now deprecated. This is because hashes and HMACs
78 // should be implemented as plugins to the SPI framework. However, for
79 // binary compatibility reasons, these methods must be left here.
80 // There are no tests (hence no coverage) for HMACs outside the plugin
81 // framework, but this original code is left unmodified.
83 #ifdef _BullseyeCoverage
84 #pragma suppress_warnings on
85 #pragma BullseyeCoverage off
86 #pragma suppress_warnings off
90 void CHMAC::InitialiseL(const TDesC8& aKey)
97 if( (TUint32)aKey.Size() > iBlockSize)
99 iInnerPad = iDigest->Final(aKey);
107 for (i=iInnerPad.Size();i<iBlockSize;i++)
112 const TUint8 Magic1=0x36, Magic2=0x5c;
113 for (i=0;i<iBlockSize;i++)
115 iInnerPad[i]^=Magic1;
116 iOuterPad[i]^=Magic2;
119 iDigest->Hash(iInnerPad);
123 void CHMAC::InitBlockSizeL()
127 iBlockSize = iDigest->BlockSize();
129 if(iBlockSize > KMaxBlockSize)
131 User::Leave(KErrNotSupported);
134 iInnerPad.SetLength(iBlockSize);
135 iOuterPad.SetLength(iBlockSize);
136 iInnerPadCopy.SetLength(iBlockSize);
137 iOuterPadCopy.SetLength(iBlockSize);
140 EXPORT_C CMessageDigest* CHMAC::CopyL(void)
142 CHMAC* that=new(ELeave) CHMAC(*this);
143 CleanupStack::PushL(that);
144 that->iDigest=iDigest ? iDigest->CopyL() : NULL;
149 EXPORT_C CMessageDigest* CHMAC::ReplicateL(void)
151 CHMAC* that=new(ELeave) CHMAC(*this);
152 CleanupStack::PushL(that);
153 that->iDigest=iDigest ? iDigest->ReplicateL() : NULL;
159 EXPORT_C TInt CHMAC::BlockSize(void)
164 EXPORT_C TInt CHMAC::HashSize(void)
166 return iDigest ? iDigest->HashSize() : 0;
169 EXPORT_C void CHMAC::Reset(void)
174 iDigest->Update(iInnerPad);
178 // JCS, There may be a more efficient method but I can't find it
179 // because using the DoFinal/DoUpdate functions directly calls
180 // Store/Restore at inappropriate times and scribbles over stored
182 // This is the only way I've found to both generate a hash value
183 // and get this in the correctly updated state
184 EXPORT_C TPtrC8 CHMAC::Hash(const TDesC8& aMessage)
186 TPtrC8 ptr(KNullDesC8());
187 TPtrC8 finalPtr(KNullDesC8());
191 ptr.Set(iDigest->Final(aMessage));
192 iDigest->Update(iOuterPad);
193 finalPtr.Set(iDigest->Final(ptr));
200 iDigest->Update(aMessage);
206 EXPORT_C void CHMAC::Update(const TDesC8& aMessage)
210 iDigest->Update(aMessage);
214 EXPORT_C TPtrC8 CHMAC::Final(const TDesC8& aMessage)
216 TPtrC8 ptr(KNullDesC8());
219 ptr.Set(iDigest->Final(aMessage));
220 iDigest->Update(iOuterPad);
227 EXPORT_C TPtrC8 CHMAC::Final()
229 TPtrC8 ptr(KNullDesC8());
232 ptr.Set(iDigest->Final());
233 iDigest->Update(iOuterPad);
240 void CHMAC::RestoreState()
242 iOuterPad.Copy(iOuterPadCopy);
243 iInnerPad.Copy(iInnerPadCopy);
245 iDigest->RestoreState();
248 void CHMAC::StoreState()
250 iOuterPadCopy.Copy(iOuterPad);
251 iInnerPadCopy.Copy(iInnerPad);
253 iDigest->StoreState();