Update contrib.
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)
27 #include "sha224and256.h"
28 #include "sha384and512.h"
30 // Initial Hash Values of SHA2 algorithms
32 * Initial Hash Value for SHA-224
34 * These words were obtained by taking the first thirty-two bits
35 * of the fractional parts of the square roots of the first eight
39 * FIPS 180-3 Section 5.3.2
41 const TUint SHA224InitVals[] =
54 * Initial Hash Value for SHA-256
56 * These words were obtained by taking the first thirty-two bits
57 * of the fractional parts of the square roots of the first eight
60 * FIPS 180-2 Section 5.3.2
62 const TUint SHA256InitVals[] =
75 * Initial Hash Value for SHA-384
77 * These words were obtained by taking the first sixty-four bits
78 * of the fractional parts of the square roots of the first eight
81 * FIPS 180-2 Section 5.3.3
83 const TUint64 SHA384InitVals[] =
85 UI64LIT(0xcbbb9d5dc1059ed8), // A
86 UI64LIT(0x629a292a367cd507), // B
87 UI64LIT(0x9159015a3070dd17), // C
88 UI64LIT(0x152fecd8f70e5939), // D
89 UI64LIT(0x67332667ffc00b31), // E
90 UI64LIT(0x8eb44a8768581511), // F
91 UI64LIT(0xdb0c2e0d64f98fa7), // G
92 UI64LIT(0x47b5481dbefa4fa4) // H
96 * Initial Hash Value for SHA-512
98 * These words were obtained by taking the first sixty-four bits
99 * of the fractional parts of the square roots of the first eight
102 * FIPS 180-2 Section 5.3.4
104 const TUint64 SHA512InitVals[] =
106 UI64LIT(0x6a09e667f3bcc908), // A
107 UI64LIT(0xbb67ae8584caa73b), // B
108 UI64LIT(0x3c6ef372fe94f82b), // C
109 UI64LIT(0xa54ff53a5f1d36f1), // D
110 UI64LIT(0x510e527fade682d1), // E
111 UI64LIT(0x9b05688c2b3e6c1f), // F
112 UI64LIT(0x1f83d9abfb41bd6b), // G
113 UI64LIT(0x5be0cd19137e2179) // H
117 EXPORT_C CSHA2* CSHA2::NewL(TSH2Algo aAlgorithmId)
119 CSHA2* self = CSHA2::NewLC(aAlgorithmId);
120 CleanupStack::Pop(self);
124 EXPORT_C CSHA2* CSHA2::NewLC(TSH2Algo aAlgorithmId)
126 CSHA2* self = new (ELeave) CSHA2();
127 CleanupStack::PushL(self);
128 self->ConstructL(aAlgorithmId);
132 void CSHA2::ConstructL(const CSHA2& aSHA2)
134 iAlgorithmType = aSHA2.iAlgorithmType;
135 iInitValues = aSHA2.iInitValues;
136 iHashSize = aSHA2.iHashSize;
137 switch(iAlgorithmType)
142 const CSHA224And256* const impl = static_cast<CSHA224And256*>(aSHA2.iImplementation);
143 iImplementation = new (ELeave) CSHA224And256(*impl);
149 const CSHA384And512* const impl = static_cast<CSHA384And512*>(aSHA2.iImplementation);
150 iImplementation = new (ELeave) CSHA384And512(*impl);
155 User::Leave(KErrNotSupported);
160 void CSHA2::ConstructL(TSH2Algo aAlgorithmId)
166 iImplementation = CSHA224And256::NewL();
167 iInitValues = SHA224InitVals;
168 iAlgorithmType = E224Bit;
169 iHashSize = KSHA224HashSize;
174 iImplementation = CSHA224And256::NewL();
175 iInitValues = SHA256InitVals;
176 iAlgorithmType = E256Bit;
177 iHashSize = KSHA256HashSize;
182 iImplementation = CSHA384And512::NewL();
183 iInitValues = SHA384InitVals;
184 iAlgorithmType = E384Bit;
185 iHashSize = KSHA384HashSize;
190 iImplementation = CSHA384And512::NewL();
191 iInitValues = SHA512InitVals;
192 iAlgorithmType = E512Bit;
193 iHashSize = KSHA512HashSize;
198 User::Leave(KErrNotSupported);
205 EXPORT_C CSHA2::~CSHA2()
207 delete iImplementation;
210 EXPORT_C CMessageDigest* CSHA2::ReplicateL()
212 return CSHA2::NewL(iAlgorithmType);
215 EXPORT_C TPtrC8 CSHA2::Hash(const TDesC8& aMessage)
217 TPtrC8 ptr(KNullDesC8());
218 iImplementation->Update(aMessage.Ptr(),aMessage.Size());
219 iImplementation->StoreState();
220 ptr.Set(iImplementation->Final().Ptr(), iHashSize);
221 iImplementation->RestoreState();
225 EXPORT_C CMessageDigest* CSHA2::CopyL()
227 CSHA2* hash = new(ELeave) CSHA2();
228 CleanupStack::PushL(hash);
229 hash->ConstructL(*this);
230 CleanupStack::Pop(hash);
234 EXPORT_C TInt CSHA2::BlockSize(void)
236 TInt blockSize = KSHA256BlockSize;
237 if(E384Bit == iAlgorithmType || E512Bit == iAlgorithmType)
239 blockSize = KSHA512BlockSize;
244 EXPORT_C TInt CSHA2::HashSize(void)
249 EXPORT_C void CSHA2::Reset()
251 iImplementation->Reset(iInitValues);
254 EXPORT_C void CSHA2::Update(const TDesC8& aMessage)
256 iImplementation->Update(aMessage.Ptr(),aMessage.Size());
259 EXPORT_C TPtrC8 CSHA2::Final(void)
261 TPtrC8 ptr(KNullDesC8());
262 ptr.Set(iImplementation->Final().Ptr(), iHashSize);
267 EXPORT_C TPtrC8 CSHA2::Final(const TDesC8& aMessage)
269 iImplementation->Update(aMessage.Ptr(),aMessage.Size());
270 TPtrC8 ptr(KNullDesC8());
271 ptr.Set(iImplementation->Final().Ptr(), iHashSize);
276 void CSHA2::RestoreState()
278 iImplementation->RestoreState();
281 void CSHA2::StoreState()
283 iImplementation->StoreState();
286 // Implemented in hmacimpl.cpp or softwarehashbase.cpp
287 // but required as derived from MHash. No coverage here.
288 #ifdef _BullseyeCoverage
289 #pragma suppress_warnings on
290 #pragma BullseyeCoverage off
291 #pragma suppress_warnings off