sl@0: /* sl@0: * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * software SHA2 implementation sl@0: * RFC 4634 (US Secure Hash Algorithms (SHA and HMAC-SHA)) sl@0: * FIPS 180-2 (With change notice) sl@0: * sl@0: */ sl@0: sl@0: sl@0: /** sl@0: @file sl@0: */ sl@0: sl@0: #include "sha2impl.h" sl@0: sl@0: #include sl@0: #include "pluginconfig.h" sl@0: #include "sha224and256impl.h" sl@0: #include "sha384and512impl.h" sl@0: sl@0: using namespace SoftwareCrypto; sl@0: sl@0: // Initial Hash Values of SHA2 algorithms sl@0: /** sl@0: * Initial Hash Value for SHA-224 sl@0: * sl@0: * These words were obtained by taking the first thirty-two bits sl@0: * of the fractional parts of the square roots of the first eight sl@0: * prime numbers. sl@0: * sl@0: * FIPS 180-2 Appendix sl@0: * FIPS 180-3 Section 5.3.2 sl@0: */ sl@0: const TUint SHA224InitVals[] = sl@0: { sl@0: 0xc1059ed8, // A sl@0: 0x367cd507, // B sl@0: 0x3070dd17, // C sl@0: 0xf70e5939, // D sl@0: 0xffc00b31, // E sl@0: 0x68581511, // F sl@0: 0x64f98fa7, // G sl@0: 0xbefa4fa4 // H sl@0: }; sl@0: sl@0: /** sl@0: * Initial Hash Value for SHA-256 sl@0: * sl@0: * These words were obtained by taking the first thirty-two bits sl@0: * of the fractional parts of the square roots of the first eight sl@0: * prime numbers. sl@0: * sl@0: * FIPS 180-2 Section 5.3.2 sl@0: */ sl@0: const TUint SHA256InitVals[] = sl@0: { sl@0: 0x6a09e667, // A sl@0: 0xbb67ae85, // B sl@0: 0x3c6ef372, // C sl@0: 0xa54ff53a, // D sl@0: 0x510e527f, // E sl@0: 0x9b05688c, // F sl@0: 0x1f83d9ab, // G sl@0: 0x5be0cd19 // H sl@0: }; sl@0: sl@0: /** sl@0: * Initial Hash Value for SHA-384 sl@0: * sl@0: * These words were obtained by taking the first sixty-four bits sl@0: * of the fractional parts of the square roots of the first eight sl@0: * prime numbers. sl@0: * sl@0: * FIPS 180-2 Section 5.3.3 sl@0: */ sl@0: const TUint64 SHA384InitVals[] = sl@0: { sl@0: UI64LIT(0xcbbb9d5dc1059ed8), // A sl@0: UI64LIT(0x629a292a367cd507), // B sl@0: UI64LIT(0x9159015a3070dd17), // C sl@0: UI64LIT(0x152fecd8f70e5939), // D sl@0: UI64LIT(0x67332667ffc00b31), // E sl@0: UI64LIT(0x8eb44a8768581511), // F sl@0: UI64LIT(0xdb0c2e0d64f98fa7), // G sl@0: UI64LIT(0x47b5481dbefa4fa4) // H sl@0: }; sl@0: sl@0: /** sl@0: * Initial Hash Value for SHA-512 sl@0: * sl@0: * These words were obtained by taking the first sixty-four bits sl@0: * of the fractional parts of the square roots of the first eight sl@0: * prime numbers. sl@0: * sl@0: * FIPS 180-2 Section 5.3.4 sl@0: */ sl@0: const TUint64 SHA512InitVals[] = sl@0: { sl@0: UI64LIT(0x6a09e667f3bcc908), // A sl@0: UI64LIT(0xbb67ae8584caa73b), // B sl@0: UI64LIT(0x3c6ef372fe94f82b), // C sl@0: UI64LIT(0xa54ff53a5f1d36f1), // D sl@0: UI64LIT(0x510e527fade682d1), // E sl@0: UI64LIT(0x9b05688c2b3e6c1f), // F sl@0: UI64LIT(0x1f83d9abfb41bd6b), // G sl@0: UI64LIT(0x5be0cd19137e2179) // H sl@0: }; sl@0: sl@0: sl@0: CSHA2Impl* CSHA2Impl::NewL(TInt32 aAlgorithmId) sl@0: { sl@0: CSHA2Impl* self = CSHA2Impl::NewLC(aAlgorithmId); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: CSHA2Impl* CSHA2Impl::NewLC(TInt32 aAlgorithmId) sl@0: { sl@0: CSHA2Impl* self = new (ELeave) CSHA2Impl(); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aAlgorithmId); sl@0: return self; sl@0: } sl@0: sl@0: void CSHA2Impl::ConstructL(const CSHA2Impl& aSHA2Impl) sl@0: { sl@0: iImplementationUid = aSHA2Impl.iImplementationUid; sl@0: iInitValues = aSHA2Impl.iInitValues; sl@0: iHashSize = aSHA2Impl.iHashSize; sl@0: switch(iImplementationUid.iUid) sl@0: { sl@0: case KCryptoPluginSha224: sl@0: case KCryptoPluginSha256: sl@0: { sl@0: const CSHA224And256Impl* const impl = static_cast(aSHA2Impl.iImplementation); sl@0: iImplementation = new (ELeave) CSHA224And256Impl(*impl); sl@0: break; sl@0: } sl@0: case KCryptoPluginSha384: sl@0: case KCryptoPluginSha512: sl@0: { sl@0: const CSHA384And512Impl* const impl = static_cast(aSHA2Impl.iImplementation); sl@0: iImplementation = new (ELeave) CSHA384And512Impl(*impl); sl@0: break; sl@0: } sl@0: default: sl@0: { sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: } sl@0: } sl@0: sl@0: void CSHA2Impl::ConstructL(TInt32 aAlgorithmId) sl@0: { sl@0: switch(aAlgorithmId) sl@0: { sl@0: case KCryptoPluginSha224: sl@0: { sl@0: iImplementation = CSHA224And256Impl::NewL(); sl@0: iInitValues = SHA224InitVals; sl@0: iImplementationUid = KCryptoPluginSha224Uid; sl@0: iHashSize = KSHA224HashSize; sl@0: break; sl@0: } sl@0: case KCryptoPluginSha256: sl@0: { sl@0: iImplementation = CSHA224And256Impl::NewL(); sl@0: iInitValues = SHA256InitVals; sl@0: iImplementationUid = KCryptoPluginSha256Uid; sl@0: iHashSize = KSHA256HashSize; sl@0: break; sl@0: } sl@0: case KCryptoPluginSha384: sl@0: { sl@0: iImplementation = CSHA384And512Impl::NewL(); sl@0: iInitValues = SHA384InitVals; sl@0: iImplementationUid = KCryptoPluginSha384Uid; sl@0: iHashSize = KSHA384HashSize; sl@0: break; sl@0: } sl@0: case KCryptoPluginSha512: sl@0: { sl@0: iImplementation = CSHA384And512Impl::NewL(); sl@0: iInitValues = SHA512InitVals; sl@0: iImplementationUid = KCryptoPluginSha512Uid; sl@0: iHashSize = KSHA512HashSize; sl@0: break; sl@0: } sl@0: default: sl@0: { sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: } sl@0: sl@0: Reset(); sl@0: } sl@0: sl@0: CSHA2Impl::~CSHA2Impl() sl@0: { sl@0: delete iImplementation; sl@0: } sl@0: sl@0: void CSHA2Impl::Reset() sl@0: { sl@0: iImplementation->Reset(iInitValues); sl@0: } sl@0: sl@0: void CSHA2Impl::Close() sl@0: { sl@0: delete this; sl@0: } sl@0: sl@0: MHash* CSHA2Impl::ReplicateL() sl@0: { sl@0: return CSHA2Impl::NewL(iImplementationUid.iUid); sl@0: } sl@0: sl@0: MHash* CSHA2Impl::CopyL() sl@0: { sl@0: CSHA2Impl* hash = new(ELeave) CSHA2Impl(); sl@0: CleanupStack::PushL(hash); sl@0: hash->ConstructL(*this); sl@0: CleanupStack::Pop(hash); sl@0: return hash; sl@0: } sl@0: sl@0: TUid CSHA2Impl::ImplementationUid() sl@0: { sl@0: return iImplementationUid; sl@0: } sl@0: sl@0: void CSHA2Impl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics) sl@0: { sl@0: aPluginCharacteristics=NULL; sl@0: TInt hashNum=sizeof(KHashCharacteristics)/sizeof(THashCharacteristics*); sl@0: for (TInt i=0;icmn.iImplementationUID == ImplementationUid().iUid) sl@0: { sl@0: aPluginCharacteristics = KHashCharacteristics[i]; sl@0: break; sl@0: } sl@0: } sl@0: } sl@0: sl@0: CExtendedCharacteristics* CSHA2Impl::CreateExtendedCharacteristicsL() sl@0: { sl@0: // All Symbian software plug-ins have unlimited concurrency, cannot be reserved sl@0: // for exclusive use and are not CERTIFIED to be standards compliant. sl@0: return CExtendedCharacteristics::NewL(KMaxTInt, EFalse); sl@0: } sl@0: sl@0: const CExtendedCharacteristics* CSHA2Impl::GetExtendedCharacteristicsL() sl@0: { sl@0: return CSHA2Impl::CreateExtendedCharacteristicsL(); sl@0: } sl@0: sl@0: TPtrC8 CSHA2Impl::Hash(const TDesC8& aMessage) sl@0: { sl@0: TPtrC8 ptr(KNullDesC8()); sl@0: iImplementation->Update(aMessage.Ptr(),aMessage.Size()); sl@0: iImplementation->StoreState(); sl@0: ptr.Set(iImplementation->Final().Ptr(), iHashSize); sl@0: iImplementation->RestoreState(); sl@0: return ptr; sl@0: } sl@0: sl@0: void CSHA2Impl::Update(const TDesC8& aMessage) sl@0: { sl@0: iImplementation->Update(aMessage.Ptr(),aMessage.Size()); sl@0: } sl@0: sl@0: TPtrC8 CSHA2Impl::Final(const TDesC8& aMessage) sl@0: { sl@0: TPtrC8 ptr(KNullDesC8()); sl@0: if (aMessage!=KNullDesC8()) sl@0: { sl@0: iImplementation->Update(aMessage.Ptr(),aMessage.Size()); sl@0: } sl@0: ptr.Set(iImplementation->Final().Ptr(), iHashSize); sl@0: Reset(); sl@0: return ptr; sl@0: } sl@0: sl@0: void CSHA2Impl::RestoreState() sl@0: { sl@0: iImplementation->RestoreState(); sl@0: } sl@0: sl@0: void CSHA2Impl::StoreState() sl@0: { sl@0: iImplementation->StoreState(); sl@0: } sl@0: sl@0: // Implemented in hmacimpl.cpp or softwarehashbase.cpp sl@0: // but required as derived from MHash. No coverage here. sl@0: #ifdef _BullseyeCoverage sl@0: #pragma suppress_warnings on sl@0: #pragma BullseyeCoverage off sl@0: #pragma suppress_warnings off sl@0: #endif sl@0: sl@0: TAny* CSHA2Impl::GetExtension(TUid /*aExtensionId*/) sl@0: { sl@0: return NULL; sl@0: } sl@0: sl@0: void CSHA2Impl::SetOperationModeL(TUid /*aOperationMode*/) sl@0: { sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: sl@0: void CSHA2Impl::SetKeyL(const CKey& /*aKey*/) sl@0: { sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: