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: * Common implementation of SHA224 and SHA256 sl@0: * RFC 4634 (US Secure Hash Algorithms (SHA and HMAC-SHA)) sl@0: * sl@0: */ sl@0: sl@0: sl@0: /** sl@0: @file sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include "pluginconfig.h" sl@0: #include "sha224and256impl.h" sl@0: sl@0: using namespace SoftwareCrypto; sl@0: sl@0: /** sl@0: * SHA256 Constants sl@0: * sl@0: * SHA-256 uses a sequence of sixty-four constant 32-bit words. sl@0: * These words represent the first thirty-two bits of the fractional sl@0: * parts of the cube roots of the first sixtyfour prime numbers. sl@0: * sl@0: * FIPS 180-2 Section 4.2.2 sl@0: */ sl@0: const TUint K[64] = sl@0: { sl@0: 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, sl@0: 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, sl@0: 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, sl@0: 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, sl@0: 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, sl@0: 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, sl@0: 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, sl@0: 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, sl@0: 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, sl@0: 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, sl@0: 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, sl@0: 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, sl@0: 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, sl@0: 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, sl@0: 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, sl@0: 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 sl@0: }; sl@0: sl@0: /** sl@0: * Define the SHA SIGMA and sigma macros sl@0: * sl@0: * FIPS 180-2 section 4.1.2 sl@0: */ sl@0: // Equation 4.4 sl@0: inline TUint SHA256_SIGMA0(TUint aWord) sl@0: { sl@0: return (SHA_ROTR( 2,aWord) ^ SHA_ROTR(13,aWord) ^ SHA_ROTR(22,aWord)); sl@0: } sl@0: // Equation 4.5 sl@0: inline TUint SHA256_SIGMA1(TUint aWord) sl@0: { sl@0: return (SHA_ROTR( 6,aWord) ^ SHA_ROTR(11,aWord) ^ SHA_ROTR(25,aWord)); sl@0: } sl@0: // Equation 4.6 sl@0: inline TUint SHA256_sigma0(TUint aWord) sl@0: { sl@0: return (SHA_ROTR( 7,aWord) ^ SHA_ROTR(18,aWord) ^ SHA_SHR( 3,aWord)); sl@0: } sl@0: // Equation 4.7 sl@0: inline TUint SHA256_sigma1(TUint aWord) sl@0: { sl@0: return (SHA_ROTR(17,aWord) ^ SHA_ROTR(19,aWord) ^ SHA_SHR(10,aWord)); sl@0: } sl@0: sl@0: sl@0: // Macros sl@0: inline TUint MakeWord(const TUint8* aData) sl@0: { sl@0: return (aData[0] << 24 | aData[1] << 16 | aData[2] << 8 | aData[3]); sl@0: } sl@0: sl@0: sl@0: CSHA224And256Impl* CSHA224And256Impl::NewL() sl@0: { sl@0: CSHA224And256Impl* self=new (ELeave) CSHA224And256Impl(); sl@0: return self; sl@0: } sl@0: sl@0: CSHA224And256Impl::CSHA224And256Impl() : iHash(KSHA256HashSize) sl@0: { sl@0: } sl@0: sl@0: CSHA224And256Impl::CSHA224And256Impl(const CSHA224And256Impl& aSHA256Impl) sl@0: : iHash(aSHA256Impl.iHash), sl@0: iA(aSHA256Impl.iA), sl@0: iB(aSHA256Impl.iB), sl@0: iC(aSHA256Impl.iC), sl@0: iD(aSHA256Impl.iD), sl@0: iE(aSHA256Impl.iE), sl@0: iF(aSHA256Impl.iF), sl@0: iG(aSHA256Impl.iG), sl@0: iH(aSHA256Impl.iH), sl@0: iNl(aSHA256Impl.iNl), sl@0: iNh(aSHA256Impl.iNh) sl@0: { sl@0: Mem::Copy(iData, aSHA256Impl.iData, KSHA256BlockSize*sizeof(TUint)); sl@0: } sl@0: sl@0: void CSHA224And256Impl::Reset(const TAny* aValArray) sl@0: { sl@0: const TUint* values = static_cast(aValArray); sl@0: /** sl@0: * Initial Hash Value 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: iA=values[0]; sl@0: iB=values[1]; sl@0: iC=values[2]; sl@0: iD=values[3]; sl@0: iE=values[4]; sl@0: iF=values[5]; sl@0: iG=values[6]; sl@0: iH=values[7]; sl@0: iNh=0; sl@0: iNl=0; sl@0: } sl@0: sl@0: // This assumes a big-endian architecture sl@0: void CSHA224And256Impl::Update(const TUint8* aData,TUint aLength) sl@0: { sl@0: while((aLength / 4) > 0 && (iNl % 4 == 0)) sl@0: { sl@0: iData[iNl>>2] = MakeWord(aData); sl@0: iNl+=4; sl@0: aData+=4; sl@0: aLength-=4; sl@0: if(iNl==KSHA256BlockSize) sl@0: { sl@0: Block(); sl@0: AddLength(KSHA256BlockSize); sl@0: } sl@0: } sl@0: sl@0: while(aLength--) sl@0: { sl@0: if(!(iNl&0x03)) sl@0: { sl@0: iData[iNl >> 2] = 0; sl@0: } sl@0: iData[iNl >> 2] |= *aData << ((3 - iNl&0x03) << 3) ; sl@0: ++aData; sl@0: ++iNl; sl@0: if(iNl==KSHA256BlockSize) sl@0: { sl@0: Block(); sl@0: AddLength(KSHA256BlockSize); sl@0: } sl@0: } sl@0: } sl@0: sl@0: //This function will panic if the total input length is longer than 2^64 in bits sl@0: _LIT(KPanicString, "Message length exceeds supported length"); sl@0: inline void CSHA224And256Impl::AddLength(const TUint aLength) sl@0: { sl@0: TUint64 temp = iNh; sl@0: iNh += aLength << 3; sl@0: __ASSERT_ALWAYS((temp <= iNh), User::Panic(KPanicString, KErrOverflow)); sl@0: } sl@0: sl@0: sl@0: static inline void CSHA256_16( const TUint aA, sl@0: const TUint aB, sl@0: const TUint aC, sl@0: TUint& aD, sl@0: const TUint aE, sl@0: const TUint aF, sl@0: const TUint aG, sl@0: TUint& aH, sl@0: TUint aTemp1, sl@0: TUint aTemp2, sl@0: const TUint aK, sl@0: const TUint aWord) sl@0: { sl@0: aTemp1 = aH + SHA256_SIGMA1(aE) + SHA_Ch(aE,aF,aG) + aK + aWord; sl@0: aTemp2 = SHA256_SIGMA0(aA) + SHA_Maj(aA,aB,aC); sl@0: aD = aD + aTemp1; sl@0: aH = aTemp1 + aTemp2; sl@0: } sl@0: sl@0: static inline void CSHA256_48( const TUint aA, sl@0: const TUint aB, sl@0: const TUint aC, sl@0: TUint& aD, sl@0: const TUint aE, sl@0: const TUint aF, sl@0: const TUint aG, sl@0: TUint& aH, sl@0: TUint aTemp1, sl@0: TUint aTemp2, sl@0: const TUint aK, sl@0: TUint& aWord0, sl@0: const TUint aWord2, sl@0: const TUint aWord7, sl@0: const TUint aWord15, sl@0: const TUint aWord16) sl@0: { sl@0: aWord0 = SHA256_sigma1(aWord2) + aWord7 + SHA256_sigma0(aWord15) + aWord16; sl@0: CSHA256_16(aA, aB, aC, aD, aE, aF, aG, aH, aTemp1, aTemp2, aK, aWord0); sl@0: } sl@0: sl@0: /** sl@0: * This function actually calculates the hash. sl@0: * Function is defined in FIPS 180-2 section 6.2.2 sl@0: * sl@0: * This function is the expanded version of the following loop. sl@0: * for(TUint i = 0; i < 64; ++i) sl@0: * { sl@0: * if(i >= 16) sl@0: * { sl@0: * iData[i] = SHA256_sigma1(iData[i-2]) + iData[i-7] + SHA256_sigma0(iData[i-15]) + iData[i-16]; sl@0: * } sl@0: * sl@0: * temp1 = tempH + SHA256_SIGMA1(tempE) + SHA_Ch(tempE,tempF,tempG) + K[i] + iData[i]; sl@0: * temp2 = SHA256_SIGMA0(tempA) + SHA_Maj(tempA,tempB,tempC); sl@0: * tempH = tempG; sl@0: * tempG = tempF; sl@0: * tempF = tempE; sl@0: * tempE = tempD + temp1; sl@0: * tempD = tempC; sl@0: * tempC = tempB; sl@0: * tempB = tempA; sl@0: * tempA = temp1 + temp2; sl@0: * } sl@0: */ sl@0: void CSHA224And256Impl::Block() sl@0: { sl@0: TUint tempA=iA; sl@0: TUint tempB=iB; sl@0: TUint tempC=iC; sl@0: TUint tempD=iD; sl@0: TUint tempE=iE; sl@0: TUint tempF=iF; sl@0: TUint tempG=iG; sl@0: TUint tempH=iH; sl@0: TUint temp1=0; sl@0: TUint temp2=0; sl@0: sl@0: CSHA256_16(tempA,tempB,tempC,tempD,tempE,tempF,tempG,tempH,temp1,temp2,K[0],iData[0]); sl@0: CSHA256_16(tempH,tempA,tempB,tempC,tempD,tempE,tempF,tempG,temp1,temp2,K[1],iData[1]); sl@0: CSHA256_16(tempG,tempH,tempA,tempB,tempC,tempD,tempE,tempF,temp1,temp2,K[2],iData[2]); sl@0: CSHA256_16(tempF,tempG,tempH,tempA,tempB,tempC,tempD,tempE,temp1,temp2,K[3],iData[3]); sl@0: CSHA256_16(tempE,tempF,tempG,tempH,tempA,tempB,tempC,tempD,temp1,temp2,K[4],iData[4]); sl@0: CSHA256_16(tempD,tempE,tempF,tempG,tempH,tempA,tempB,tempC,temp1,temp2,K[5],iData[5]); sl@0: CSHA256_16(tempC,tempD,tempE,tempF,tempG,tempH,tempA,tempB,temp1,temp2,K[6],iData[6]); sl@0: CSHA256_16(tempB,tempC,tempD,tempE,tempF,tempG,tempH,tempA,temp1,temp2,K[7],iData[7]); sl@0: sl@0: CSHA256_16(tempA,tempB,tempC,tempD,tempE,tempF,tempG,tempH,temp1,temp2,K[8],iData[8]); sl@0: CSHA256_16(tempH,tempA,tempB,tempC,tempD,tempE,tempF,tempG,temp1,temp2,K[9],iData[9]); sl@0: CSHA256_16(tempG,tempH,tempA,tempB,tempC,tempD,tempE,tempF,temp1,temp2,K[10],iData[10]); sl@0: CSHA256_16(tempF,tempG,tempH,tempA,tempB,tempC,tempD,tempE,temp1,temp2,K[11],iData[11]); sl@0: CSHA256_16(tempE,tempF,tempG,tempH,tempA,tempB,tempC,tempD,temp1,temp2,K[12],iData[12]); sl@0: CSHA256_16(tempD,tempE,tempF,tempG,tempH,tempA,tempB,tempC,temp1,temp2,K[13],iData[13]); sl@0: CSHA256_16(tempC,tempD,tempE,tempF,tempG,tempH,tempA,tempB,temp1,temp2,K[14],iData[14]); sl@0: CSHA256_16(tempB,tempC,tempD,tempE,tempF,tempG,tempH,tempA,temp1,temp2,K[15],iData[15]); sl@0: sl@0: CSHA256_48( tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2, sl@0: K[16], iData[16], iData[14], iData[9], iData[1], iData[0]); sl@0: CSHA256_48( tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2, sl@0: K[17], iData[17], iData[15], iData[10], iData[2], iData[1]); sl@0: CSHA256_48( tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2, sl@0: K[18], iData[18], iData[16], iData[11], iData[3], iData[2]); sl@0: CSHA256_48( tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2, sl@0: K[19], iData[19], iData[17], iData[12], iData[4], iData[3]); sl@0: CSHA256_48( tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2, sl@0: K[20], iData[20], iData[18], iData[13], iData[5], iData[4]); sl@0: CSHA256_48( tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2, sl@0: K[21], iData[21], iData[19], iData[14], iData[6], iData[5]); sl@0: CSHA256_48( tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2, sl@0: K[22], iData[22], iData[20], iData[15], iData[7], iData[6]); sl@0: CSHA256_48( tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2, sl@0: K[23], iData[23], iData[21], iData[16], iData[8], iData[7]); sl@0: sl@0: CSHA256_48( tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2, sl@0: K[24], iData[24], iData[22], iData[17], iData[9], iData[8]); sl@0: CSHA256_48( tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2, sl@0: K[25], iData[25], iData[23], iData[18], iData[10], iData[9]); sl@0: CSHA256_48( tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2, sl@0: K[26], iData[26], iData[24], iData[19], iData[11], iData[10]); sl@0: CSHA256_48( tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2, sl@0: K[27], iData[27], iData[25], iData[20], iData[12], iData[11]); sl@0: CSHA256_48( tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2, sl@0: K[28], iData[28], iData[26], iData[21], iData[13], iData[12]); sl@0: CSHA256_48( tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2, sl@0: K[29], iData[29], iData[27], iData[22], iData[14], iData[13]); sl@0: CSHA256_48( tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2, sl@0: K[30], iData[30], iData[28], iData[23], iData[15], iData[14]); sl@0: CSHA256_48( tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2, sl@0: K[31], iData[31], iData[29], iData[24], iData[16], iData[15]); sl@0: sl@0: CSHA256_48( tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2, sl@0: K[32], iData[32], iData[30], iData[25], iData[17], iData[16]); sl@0: CSHA256_48( tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2, sl@0: K[33], iData[33], iData[31], iData[26], iData[18], iData[17]); sl@0: CSHA256_48( tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2, sl@0: K[34], iData[34], iData[32], iData[27], iData[19], iData[18]); sl@0: CSHA256_48( tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2, sl@0: K[35], iData[35], iData[33], iData[28], iData[20], iData[19]); sl@0: CSHA256_48( tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2, sl@0: K[36], iData[36], iData[34], iData[29], iData[21], iData[20]); sl@0: CSHA256_48( tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2, sl@0: K[37], iData[37], iData[35], iData[30], iData[22], iData[21]); sl@0: CSHA256_48( tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2, sl@0: K[38], iData[38], iData[36], iData[31], iData[23], iData[22]); sl@0: CSHA256_48( tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2, sl@0: K[39], iData[39], iData[37], iData[32], iData[24], iData[23]); sl@0: sl@0: CSHA256_48( tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2, sl@0: K[40], iData[40], iData[38], iData[33], iData[25], iData[24]); sl@0: CSHA256_48( tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2, sl@0: K[41], iData[41], iData[39], iData[34], iData[26], iData[25]); sl@0: CSHA256_48( tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2, sl@0: K[42], iData[42], iData[40], iData[35], iData[27], iData[26]); sl@0: CSHA256_48( tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2, sl@0: K[43], iData[43], iData[41], iData[36], iData[28], iData[27]); sl@0: CSHA256_48( tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2, sl@0: K[44], iData[44], iData[42], iData[37], iData[29], iData[28]); sl@0: CSHA256_48( tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2, sl@0: K[45], iData[45], iData[43], iData[38], iData[30], iData[29]); sl@0: CSHA256_48( tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2, sl@0: K[46], iData[46], iData[44], iData[39], iData[31], iData[30]); sl@0: CSHA256_48( tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2, sl@0: K[47], iData[47], iData[45], iData[40], iData[32], iData[31]); sl@0: sl@0: CSHA256_48( tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2, sl@0: K[48], iData[48], iData[46], iData[41], iData[33], iData[32]); sl@0: CSHA256_48( tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2, sl@0: K[49], iData[49], iData[47], iData[42], iData[34], iData[33]); sl@0: CSHA256_48( tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2, sl@0: K[50], iData[50], iData[48], iData[43], iData[35], iData[34]); sl@0: CSHA256_48( tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2, sl@0: K[51], iData[51], iData[49], iData[44], iData[36], iData[35]); sl@0: CSHA256_48( tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2, sl@0: K[52], iData[52], iData[50], iData[45], iData[37], iData[36]); sl@0: CSHA256_48( tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2, sl@0: K[53], iData[53], iData[51], iData[46], iData[38], iData[37]); sl@0: CSHA256_48( tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2, sl@0: K[54], iData[54], iData[52], iData[47], iData[39], iData[38]); sl@0: CSHA256_48( tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2, sl@0: K[55], iData[55], iData[53], iData[48], iData[40], iData[39]); sl@0: sl@0: CSHA256_48( tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2, sl@0: K[56], iData[56], iData[54], iData[49], iData[41], iData[40]); sl@0: CSHA256_48( tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2, sl@0: K[57], iData[57], iData[55], iData[50], iData[42], iData[41]); sl@0: CSHA256_48( tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2, sl@0: K[58], iData[58], iData[56], iData[51], iData[43], iData[42]); sl@0: CSHA256_48( tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2, sl@0: K[59], iData[59], iData[57], iData[52], iData[44], iData[43]); sl@0: CSHA256_48( tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2, sl@0: K[60], iData[60], iData[58], iData[53], iData[45], iData[44]); sl@0: CSHA256_48( tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2, sl@0: K[61], iData[61], iData[59], iData[54], iData[46], iData[45]); sl@0: CSHA256_48( tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2, sl@0: K[62], iData[62], iData[60], iData[55], iData[47], iData[46]); sl@0: CSHA256_48( tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2, sl@0: K[63], iData[63], iData[61], iData[56], iData[48], iData[47]); sl@0: sl@0: iA+=tempA; sl@0: iB+=tempB; sl@0: iC+=tempC; sl@0: iD+=tempD; sl@0: iE+=tempE; sl@0: iF+=tempF; sl@0: iG+=tempG; sl@0: iH+=tempH; sl@0: sl@0: iNl=0; sl@0: } sl@0: sl@0: /** sl@0: * According to the standard, the message must be padded to an sl@0: * even 512 bits. The first padding bit must be a '1'. The last sl@0: * 64 bits represent the length of the original message. All bits sl@0: * in between should be 0. This helper function will pad the sl@0: * message according to those rules by filling the iData array sl@0: * accordingly. sl@0: */ sl@0: void CSHA224And256Impl::PadMessage() sl@0: { sl@0: const TUint padByte = 0x80; sl@0: sl@0: if(!(iNl&0x03)) sl@0: { sl@0: iData[iNl >> 2] = 0; sl@0: } sl@0: iData[iNl >> 2] |= padByte << ((3 - iNl&0x03) << 3) ; sl@0: sl@0: if (iNl >= (KSHA256BlockSize - 2*sizeof(TUint))) sl@0: { sl@0: if (iNl < (KSHA256BlockSize - sizeof(TUint))) sl@0: iData[(KSHA256BlockSize >> 2) - 1]=0; sl@0: Block(); sl@0: Mem::FillZ(iData, KSHA256BlockSize); sl@0: } sl@0: else sl@0: { sl@0: const TUint offset=(iNl+4)>>2; //+4 to account for the word added in the sl@0: //switch statement above sl@0: Mem::FillZ(iData+offset,(KSHA256BlockSize - offset*sizeof(TUint))); sl@0: } sl@0: sl@0: //Length in bits sl@0: TUint64 msgLength = iNh; sl@0: sl@0: iData[(KSHA256BlockSize >> 2) - 2] = (msgLength) >> 32; sl@0: iData[(KSHA256BlockSize >> 2) - 1] = (msgLength & 0xFFFFFFFF); sl@0: } sl@0: sl@0: inline void CSHA224And256Impl::CopyWordToHash(TUint aVal, TUint aIndex) sl@0: { sl@0: TUint value = MakeWord(reinterpret_cast(&aVal)); sl@0: Mem::Copy(const_cast(iHash.Ptr())+ (4*aIndex), &value, sizeof(aVal)); sl@0: } sl@0: sl@0: const TDes8& CSHA224And256Impl::Final() sl@0: { sl@0: AddLength(iNl); sl@0: PadMessage(); sl@0: Block(); sl@0: // sl@0: // Generate hash value into iHash sl@0: // sl@0: CopyWordToHash(iA, 0); sl@0: CopyWordToHash(iB, 1); sl@0: CopyWordToHash(iC, 2); sl@0: CopyWordToHash(iD, 3); sl@0: CopyWordToHash(iE, 4); sl@0: CopyWordToHash(iF, 5); sl@0: CopyWordToHash(iG, 6); sl@0: CopyWordToHash(iH, 7); sl@0: sl@0: return iHash; sl@0: } sl@0: sl@0: void CSHA224And256Impl::RestoreState() sl@0: { sl@0: iA = iACopy; sl@0: iB = iBCopy; sl@0: iC = iCCopy; sl@0: iD = iDCopy; sl@0: iE = iECopy; sl@0: iF = iFCopy; sl@0: iG = iGCopy; sl@0: iH = iHCopy; sl@0: iNl = iNlCopy; sl@0: iNh = iNhCopy; sl@0: Mem::Copy(iData, iDataCopy, KSHA256BlockSize*sizeof(TUint)); sl@0: } sl@0: sl@0: void CSHA224And256Impl::StoreState() sl@0: { sl@0: iACopy = iA; sl@0: iBCopy = iB; sl@0: iCCopy = iC; sl@0: iDCopy = iD; sl@0: iECopy = iE; sl@0: iFCopy = iF; sl@0: iGCopy = iG; sl@0: iHCopy = iH; sl@0: iNlCopy = iNl; sl@0: iNhCopy = iNh; sl@0: Mem::Copy(iDataCopy, iData, KSHA256BlockSize*sizeof(TUint)); 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: