First public contribution.
2 * Copyright (c) 2007-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 SHA2 implementation
16 * RFC 4634 (US Secure Hash Algorithms (SHA and HMAC-SHA))
17 * FIPS 180-2 (With change notice)
28 #include <cryptospi/hashplugin.h>
29 #include "pluginconfig.h"
30 #include "sha224and256impl.h"
31 #include "sha384and512impl.h"
33 using namespace SoftwareCrypto;
35 // Initial Hash Values of SHA2 algorithms
37 * Initial Hash Value for SHA-224
39 * These words were obtained by taking the first thirty-two bits
40 * of the fractional parts of the square roots of the first eight
44 * FIPS 180-3 Section 5.3.2
46 const TUint SHA224InitVals[] =
59 * Initial Hash Value for SHA-256
61 * These words were obtained by taking the first thirty-two bits
62 * of the fractional parts of the square roots of the first eight
65 * FIPS 180-2 Section 5.3.2
67 const TUint SHA256InitVals[] =
80 * Initial Hash Value for SHA-384
82 * These words were obtained by taking the first sixty-four bits
83 * of the fractional parts of the square roots of the first eight
86 * FIPS 180-2 Section 5.3.3
88 const TUint64 SHA384InitVals[] =
90 UI64LIT(0xcbbb9d5dc1059ed8), // A
91 UI64LIT(0x629a292a367cd507), // B
92 UI64LIT(0x9159015a3070dd17), // C
93 UI64LIT(0x152fecd8f70e5939), // D
94 UI64LIT(0x67332667ffc00b31), // E
95 UI64LIT(0x8eb44a8768581511), // F
96 UI64LIT(0xdb0c2e0d64f98fa7), // G
97 UI64LIT(0x47b5481dbefa4fa4) // H
101 * Initial Hash Value for SHA-512
103 * These words were obtained by taking the first sixty-four bits
104 * of the fractional parts of the square roots of the first eight
107 * FIPS 180-2 Section 5.3.4
109 const TUint64 SHA512InitVals[] =
111 UI64LIT(0x6a09e667f3bcc908), // A
112 UI64LIT(0xbb67ae8584caa73b), // B
113 UI64LIT(0x3c6ef372fe94f82b), // C
114 UI64LIT(0xa54ff53a5f1d36f1), // D
115 UI64LIT(0x510e527fade682d1), // E
116 UI64LIT(0x9b05688c2b3e6c1f), // F
117 UI64LIT(0x1f83d9abfb41bd6b), // G
118 UI64LIT(0x5be0cd19137e2179) // H
122 CSHA2Impl* CSHA2Impl::NewL(TInt32 aAlgorithmId)
124 CSHA2Impl* self = CSHA2Impl::NewLC(aAlgorithmId);
125 CleanupStack::Pop(self);
129 CSHA2Impl* CSHA2Impl::NewLC(TInt32 aAlgorithmId)
131 CSHA2Impl* self = new (ELeave) CSHA2Impl();
132 CleanupStack::PushL(self);
133 self->ConstructL(aAlgorithmId);
137 void CSHA2Impl::ConstructL(const CSHA2Impl& aSHA2Impl)
139 iImplementationUid = aSHA2Impl.iImplementationUid;
140 iInitValues = aSHA2Impl.iInitValues;
141 iHashSize = aSHA2Impl.iHashSize;
142 switch(iImplementationUid.iUid)
144 case KCryptoPluginSha224:
145 case KCryptoPluginSha256:
147 const CSHA224And256Impl* const impl = static_cast<CSHA224And256Impl*>(aSHA2Impl.iImplementation);
148 iImplementation = new (ELeave) CSHA224And256Impl(*impl);
151 case KCryptoPluginSha384:
152 case KCryptoPluginSha512:
154 const CSHA384And512Impl* const impl = static_cast<CSHA384And512Impl*>(aSHA2Impl.iImplementation);
155 iImplementation = new (ELeave) CSHA384And512Impl(*impl);
160 User::Leave(KErrNotSupported);
165 void CSHA2Impl::ConstructL(TInt32 aAlgorithmId)
169 case KCryptoPluginSha224:
171 iImplementation = CSHA224And256Impl::NewL();
172 iInitValues = SHA224InitVals;
173 iImplementationUid = KCryptoPluginSha224Uid;
174 iHashSize = KSHA224HashSize;
177 case KCryptoPluginSha256:
179 iImplementation = CSHA224And256Impl::NewL();
180 iInitValues = SHA256InitVals;
181 iImplementationUid = KCryptoPluginSha256Uid;
182 iHashSize = KSHA256HashSize;
185 case KCryptoPluginSha384:
187 iImplementation = CSHA384And512Impl::NewL();
188 iInitValues = SHA384InitVals;
189 iImplementationUid = KCryptoPluginSha384Uid;
190 iHashSize = KSHA384HashSize;
193 case KCryptoPluginSha512:
195 iImplementation = CSHA384And512Impl::NewL();
196 iInitValues = SHA512InitVals;
197 iImplementationUid = KCryptoPluginSha512Uid;
198 iHashSize = KSHA512HashSize;
203 User::Leave(KErrNotSupported);
210 CSHA2Impl::~CSHA2Impl()
212 delete iImplementation;
215 void CSHA2Impl::Reset()
217 iImplementation->Reset(iInitValues);
220 void CSHA2Impl::Close()
225 MHash* CSHA2Impl::ReplicateL()
227 return CSHA2Impl::NewL(iImplementationUid.iUid);
230 MHash* CSHA2Impl::CopyL()
232 CSHA2Impl* hash = new(ELeave) CSHA2Impl();
233 CleanupStack::PushL(hash);
234 hash->ConstructL(*this);
235 CleanupStack::Pop(hash);
239 TUid CSHA2Impl::ImplementationUid()
241 return iImplementationUid;
244 void CSHA2Impl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
246 aPluginCharacteristics=NULL;
247 TInt hashNum=sizeof(KHashCharacteristics)/sizeof(THashCharacteristics*);
248 for (TInt i=0;i<hashNum;i++)
250 if (KHashCharacteristics[i]->cmn.iImplementationUID == ImplementationUid().iUid)
252 aPluginCharacteristics = KHashCharacteristics[i];
258 CExtendedCharacteristics* CSHA2Impl::CreateExtendedCharacteristicsL()
260 // All Symbian software plug-ins have unlimited concurrency, cannot be reserved
261 // for exclusive use and are not CERTIFIED to be standards compliant.
262 return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
265 const CExtendedCharacteristics* CSHA2Impl::GetExtendedCharacteristicsL()
267 return CSHA2Impl::CreateExtendedCharacteristicsL();
270 TPtrC8 CSHA2Impl::Hash(const TDesC8& aMessage)
272 TPtrC8 ptr(KNullDesC8());
273 iImplementation->Update(aMessage.Ptr(),aMessage.Size());
274 iImplementation->StoreState();
275 ptr.Set(iImplementation->Final().Ptr(), iHashSize);
276 iImplementation->RestoreState();
280 void CSHA2Impl::Update(const TDesC8& aMessage)
282 iImplementation->Update(aMessage.Ptr(),aMessage.Size());
285 TPtrC8 CSHA2Impl::Final(const TDesC8& aMessage)
287 TPtrC8 ptr(KNullDesC8());
288 if (aMessage!=KNullDesC8())
290 iImplementation->Update(aMessage.Ptr(),aMessage.Size());
292 ptr.Set(iImplementation->Final().Ptr(), iHashSize);
297 void CSHA2Impl::RestoreState()
299 iImplementation->RestoreState();
302 void CSHA2Impl::StoreState()
304 iImplementation->StoreState();
307 // Implemented in hmacimpl.cpp or softwarehashbase.cpp
308 // but required as derived from MHash. No coverage here.
309 #ifdef _BullseyeCoverage
310 #pragma suppress_warnings on
311 #pragma BullseyeCoverage off
312 #pragma suppress_warnings off
315 TAny* CSHA2Impl::GetExtension(TUid /*aExtensionId*/)
320 void CSHA2Impl::SetOperationModeL(TUid /*aOperationMode*/)
322 User::Leave(KErrNotSupported);
325 void CSHA2Impl::SetKeyL(const CKey& /*aKey*/)
327 User::Leave(KErrNotSupported);