crypto/weakcrypto/source/hash/sha224and256.cpp
changeset 0 2c201484c85f
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * Common implementation of SHA224 and SHA256
       
    16 * RFC 4634 (US Secure Hash Algorithms (SHA and HMAC-SHA))
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 /**
       
    24  @file
       
    25 */
       
    26 
       
    27 
       
    28 #include "sha224and256.h"
       
    29 
       
    30 /**
       
    31  * SHA256 Constants
       
    32  * 
       
    33  * SHA-256 uses a sequence of sixty-four constant 32-bit words. 
       
    34  * These words represent the first thirty-two bits of the fractional 
       
    35  * parts of the cube roots of the first sixtyfour prime numbers.
       
    36  * 
       
    37  * FIPS 180-2 Section 4.2.2
       
    38  */
       
    39 const TUint K[64] = 
       
    40 	{
       
    41 	0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,	
       
    42 	0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
       
    43 	0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
       
    44 	0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
       
    45 	0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
       
    46 	0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
       
    47 	0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
       
    48 	0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
       
    49 	0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
       
    50 	0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
       
    51 	0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 
       
    52 	0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
       
    53 	0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 
       
    54 	0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
       
    55 	0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
       
    56 	0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
       
    57 	};
       
    58 
       
    59 /**
       
    60  * Define the SHA SIGMA and sigma macros 
       
    61  * 
       
    62  * FIPS 180-2 section 4.1.2
       
    63  */
       
    64 // Equation 4.4
       
    65 inline TUint SHA256_SIGMA0(TUint aWord)
       
    66 	{
       
    67 	return (SHA_ROTR<TUint>( 2,aWord) ^ SHA_ROTR<TUint>(13,aWord) ^ SHA_ROTR<TUint>(22,aWord));
       
    68 	}
       
    69 // Equation 4.5
       
    70 inline TUint SHA256_SIGMA1(TUint aWord)
       
    71 	{
       
    72 	return (SHA_ROTR<TUint>( 6,aWord) ^ SHA_ROTR<TUint>(11,aWord) ^ SHA_ROTR<TUint>(25,aWord));
       
    73 	}
       
    74 // Equation 4.6
       
    75 inline TUint SHA256_sigma0(TUint aWord)
       
    76 	{
       
    77 	return (SHA_ROTR<TUint>( 7,aWord) ^ SHA_ROTR<TUint>(18,aWord) ^ SHA_SHR<TUint>( 3,aWord));
       
    78 	}
       
    79 // Equation 4.7
       
    80 inline TUint SHA256_sigma1(TUint aWord)
       
    81 	{
       
    82 	return (SHA_ROTR<TUint>(17,aWord) ^ SHA_ROTR<TUint>(19,aWord) ^ SHA_SHR<TUint>(10,aWord));
       
    83 	}
       
    84 
       
    85 
       
    86 // Macros
       
    87 inline TUint MakeWord(const TUint8* aData)
       
    88 	{
       
    89 	return (aData[0] << 24 | aData[1] << 16 | aData[2] << 8 | aData[3]);
       
    90 	}
       
    91 
       
    92 	
       
    93 CSHA224And256* CSHA224And256::NewL()
       
    94 	{
       
    95 	CSHA224And256* self=new (ELeave) CSHA224And256();
       
    96 	return self;						
       
    97 	}
       
    98 														
       
    99 CSHA224And256::CSHA224And256() : iHash(KSHA256HashSize)
       
   100 	{		
       
   101 	}
       
   102 	
       
   103 CSHA224And256::CSHA224And256(const CSHA224And256& aSHAImpl)
       
   104 				: 	iHash(aSHAImpl.iHash),
       
   105 					iA(aSHAImpl.iA),
       
   106 					iB(aSHAImpl.iB),
       
   107 					iC(aSHAImpl.iC),
       
   108 					iD(aSHAImpl.iD),
       
   109 					iE(aSHAImpl.iE),
       
   110 					iF(aSHAImpl.iF),
       
   111 					iG(aSHAImpl.iG),
       
   112 					iH(aSHAImpl.iH),
       
   113 					iNl(aSHAImpl.iNl),
       
   114 					iNh(aSHAImpl.iNh)
       
   115 	{
       
   116 	Mem::Copy(iData, aSHAImpl.iData, KSHA256BlockSize*sizeof(TUint));
       
   117 	}
       
   118 	
       
   119 void CSHA224And256::Reset(const TAny* aValArray)
       
   120 	{
       
   121 	const TUint* values = static_cast<const TUint*>(aValArray);
       
   122 	iA=values[0];
       
   123 	iB=values[1];
       
   124 	iC=values[2];
       
   125 	iD=values[3];
       
   126 	iE=values[4];
       
   127 	iF=values[5];
       
   128 	iG=values[6];
       
   129 	iH=values[7];
       
   130 	iNh=0;
       
   131 	iNl=0;
       
   132 	}
       
   133 
       
   134 // This assumes a big-endian architecture
       
   135 void CSHA224And256::Update(const TUint8* aData,TUint aLength)
       
   136 	{
       
   137 	while((aLength / 4) > 0 && (iNl % 4 == 0))
       
   138 		{
       
   139 		iData[iNl>>2] = MakeWord(aData);
       
   140 		iNl+=4;
       
   141 		aData+=4;
       
   142 		aLength-=4;
       
   143 		if(iNl==KSHA256BlockSize) 
       
   144 			{
       
   145 			Block();
       
   146 			AddLength(KSHA256BlockSize);
       
   147 			}
       
   148 		}
       
   149 
       
   150 	while(aLength--)
       
   151 		{
       
   152 		if(!(iNl&0x03))
       
   153 			{
       
   154 			iData[iNl >> 2] = 0;
       
   155 			}
       
   156 		iData[iNl >> 2] |= *aData << ((3 - iNl&0x03) << 3) ;
       
   157 		++aData;
       
   158 		++iNl;
       
   159 		if(iNl==KSHA256BlockSize) 
       
   160 			{
       
   161 			Block();
       
   162 			AddLength(KSHA256BlockSize);
       
   163 			}
       
   164 		}
       
   165 	}
       
   166 
       
   167 //This function will panic if the total input length is longer than 2^64 in bits
       
   168 _LIT(KPanicString, "Message length exceeds supported length");
       
   169 inline void CSHA224And256::AddLength(const TUint aLength)
       
   170 	{
       
   171 	TUint64 temp = iNh;
       
   172 	iNh += aLength << 3;
       
   173 	__ASSERT_ALWAYS((temp <= iNh), User::Panic(KPanicString, KErrOverflow));
       
   174 	}
       
   175 
       
   176 
       
   177 static inline void CSHA256_16(	const TUint aA, 
       
   178 								const TUint aB, 
       
   179 								const TUint aC,
       
   180 								TUint& aD, 
       
   181 								const TUint aE, 
       
   182 								const TUint aF,
       
   183 								const TUint aG, 
       
   184 								TUint& aH,
       
   185 								TUint aTemp1,
       
   186 								TUint aTemp2,
       
   187 								const TUint aK,
       
   188 								const TUint aWord)
       
   189 	{
       
   190 	aTemp1 = aH + SHA256_SIGMA1(aE) + SHA_Ch(aE,aF,aG) + aK + aWord;
       
   191 	aTemp2 = SHA256_SIGMA0(aA) + SHA_Maj(aA,aB,aC);
       
   192 	aD = aD + aTemp1;
       
   193 	aH = aTemp1 + aTemp2;
       
   194 	}
       
   195 
       
   196 static inline void CSHA256_48(	const TUint aA, 
       
   197 								const TUint aB, 
       
   198 								const TUint aC,
       
   199 								TUint& aD, 
       
   200 								const TUint aE, 
       
   201 								const TUint aF,
       
   202 								const TUint aG, 
       
   203 								TUint& aH,
       
   204 								TUint aTemp1,
       
   205 								TUint aTemp2,
       
   206 								const TUint aK,
       
   207 								TUint& aWord0,
       
   208 								const TUint aWord2,
       
   209 								const TUint aWord7,
       
   210 								const TUint aWord15,
       
   211 								const TUint aWord16)
       
   212 	{
       
   213 	aWord0 = SHA256_sigma1(aWord2) + aWord7 + SHA256_sigma0(aWord15) + aWord16;
       
   214 	CSHA256_16(aA, aB, aC, aD, aE, aF, aG, aH, aTemp1, aTemp2, aK, aWord0);
       
   215 	}
       
   216 
       
   217 /**
       
   218  * This function actually calculates the hash.
       
   219  * Function is defined in FIPS 180-2 section 6.2.2
       
   220  * 
       
   221  * This function is the expanded version of the following loop.
       
   222  *	for(TUint i = 0; i < 64; ++i)
       
   223  *		{
       
   224  *		if(i >= 16)
       
   225  *			{
       
   226  * 			iData[i] = SHA256_sigma1(iData[i-2]) + iData[i-7] + SHA256_sigma0(iData[i-15]) + iData[i-16];
       
   227  *			}
       
   228  *
       
   229  *		temp1 = tempH + SHA256_SIGMA1(tempE) + SHA_Ch(tempE,tempF,tempG) + K[i] + iData[i];
       
   230  *		temp2 = SHA256_SIGMA0(tempA) + SHA_Maj(tempA,tempB,tempC);
       
   231  *	    tempH = tempG;
       
   232  *	    tempG = tempF;
       
   233  *	    tempF = tempE;
       
   234  *	    tempE = tempD + temp1;
       
   235  *	    tempD = tempC;
       
   236  *	    tempC = tempB;
       
   237  *	    tempB = tempA;
       
   238  *	    tempA = temp1 + temp2;		
       
   239  *		}
       
   240  */
       
   241 void CSHA224And256::Block()
       
   242 	{
       
   243 	TUint tempA=iA;
       
   244 	TUint tempB=iB;
       
   245 	TUint tempC=iC;
       
   246 	TUint tempD=iD;
       
   247 	TUint tempE=iE;
       
   248 	TUint tempF=iF;
       
   249 	TUint tempG=iG;
       
   250 	TUint tempH=iH;
       
   251 	TUint temp1=0;
       
   252 	TUint temp2=0;
       
   253 	
       
   254 	CSHA256_16(tempA,tempB,tempC,tempD,tempE,tempF,tempG,tempH,temp1,temp2,K[0],iData[0]);
       
   255 	CSHA256_16(tempH,tempA,tempB,tempC,tempD,tempE,tempF,tempG,temp1,temp2,K[1],iData[1]);
       
   256 	CSHA256_16(tempG,tempH,tempA,tempB,tempC,tempD,tempE,tempF,temp1,temp2,K[2],iData[2]);
       
   257 	CSHA256_16(tempF,tempG,tempH,tempA,tempB,tempC,tempD,tempE,temp1,temp2,K[3],iData[3]);
       
   258 	CSHA256_16(tempE,tempF,tempG,tempH,tempA,tempB,tempC,tempD,temp1,temp2,K[4],iData[4]);
       
   259 	CSHA256_16(tempD,tempE,tempF,tempG,tempH,tempA,tempB,tempC,temp1,temp2,K[5],iData[5]);
       
   260 	CSHA256_16(tempC,tempD,tempE,tempF,tempG,tempH,tempA,tempB,temp1,temp2,K[6],iData[6]);
       
   261 	CSHA256_16(tempB,tempC,tempD,tempE,tempF,tempG,tempH,tempA,temp1,temp2,K[7],iData[7]);
       
   262 
       
   263 	CSHA256_16(tempA,tempB,tempC,tempD,tempE,tempF,tempG,tempH,temp1,temp2,K[8],iData[8]);
       
   264 	CSHA256_16(tempH,tempA,tempB,tempC,tempD,tempE,tempF,tempG,temp1,temp2,K[9],iData[9]);
       
   265 	CSHA256_16(tempG,tempH,tempA,tempB,tempC,tempD,tempE,tempF,temp1,temp2,K[10],iData[10]);
       
   266 	CSHA256_16(tempF,tempG,tempH,tempA,tempB,tempC,tempD,tempE,temp1,temp2,K[11],iData[11]);
       
   267 	CSHA256_16(tempE,tempF,tempG,tempH,tempA,tempB,tempC,tempD,temp1,temp2,K[12],iData[12]);
       
   268 	CSHA256_16(tempD,tempE,tempF,tempG,tempH,tempA,tempB,tempC,temp1,temp2,K[13],iData[13]);
       
   269 	CSHA256_16(tempC,tempD,tempE,tempF,tempG,tempH,tempA,tempB,temp1,temp2,K[14],iData[14]);
       
   270 	CSHA256_16(tempB,tempC,tempD,tempE,tempF,tempG,tempH,tempA,temp1,temp2,K[15],iData[15]);
       
   271 
       
   272 	CSHA256_48(	tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2,
       
   273 				K[16], iData[16], iData[14], iData[9], iData[1], iData[0]);
       
   274 	CSHA256_48(	tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2,
       
   275 				K[17], iData[17], iData[15], iData[10], iData[2], iData[1]);
       
   276 	CSHA256_48(	tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2,
       
   277 				K[18], iData[18], iData[16], iData[11], iData[3], iData[2]);
       
   278 	CSHA256_48(	tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2,
       
   279 				K[19], iData[19], iData[17], iData[12], iData[4], iData[3]);
       
   280 	CSHA256_48(	tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2,
       
   281 				K[20], iData[20], iData[18], iData[13], iData[5], iData[4]);
       
   282 	CSHA256_48(	tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2,
       
   283 				K[21], iData[21], iData[19], iData[14], iData[6], iData[5]);
       
   284 	CSHA256_48(	tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2,
       
   285 				K[22], iData[22], iData[20], iData[15], iData[7], iData[6]);
       
   286 	CSHA256_48(	tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2,
       
   287 				K[23], iData[23], iData[21], iData[16], iData[8], iData[7]);
       
   288 
       
   289 	CSHA256_48(	tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2,
       
   290 				K[24], iData[24], iData[22], iData[17], iData[9], iData[8]);
       
   291 	CSHA256_48(	tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2,
       
   292 				K[25], iData[25], iData[23], iData[18], iData[10], iData[9]);
       
   293 	CSHA256_48(	tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2,
       
   294 				K[26], iData[26], iData[24], iData[19], iData[11], iData[10]);
       
   295 	CSHA256_48(	tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2,
       
   296 				K[27], iData[27], iData[25], iData[20], iData[12], iData[11]);
       
   297 	CSHA256_48(	tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2,
       
   298 				K[28], iData[28], iData[26], iData[21], iData[13], iData[12]);
       
   299 	CSHA256_48(	tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2,
       
   300 				K[29], iData[29], iData[27], iData[22], iData[14], iData[13]);
       
   301 	CSHA256_48(	tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2,
       
   302 				K[30], iData[30], iData[28], iData[23], iData[15], iData[14]);
       
   303 	CSHA256_48(	tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2,
       
   304 				K[31], iData[31], iData[29], iData[24], iData[16], iData[15]);
       
   305 
       
   306 	CSHA256_48(	tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2,
       
   307 				K[32], iData[32], iData[30], iData[25], iData[17], iData[16]);
       
   308 	CSHA256_48(	tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2,
       
   309 				K[33], iData[33], iData[31], iData[26], iData[18], iData[17]);
       
   310 	CSHA256_48(	tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2,
       
   311 				K[34], iData[34], iData[32], iData[27], iData[19], iData[18]);
       
   312 	CSHA256_48(	tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2,
       
   313 				K[35], iData[35], iData[33], iData[28], iData[20], iData[19]);
       
   314 	CSHA256_48(	tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2,
       
   315 				K[36], iData[36], iData[34], iData[29], iData[21], iData[20]);
       
   316 	CSHA256_48(	tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2,
       
   317 				K[37], iData[37], iData[35], iData[30], iData[22], iData[21]);
       
   318 	CSHA256_48(	tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2,
       
   319 				K[38], iData[38], iData[36], iData[31], iData[23], iData[22]);
       
   320 	CSHA256_48(	tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2,
       
   321 				K[39], iData[39], iData[37], iData[32], iData[24], iData[23]);
       
   322 
       
   323 	CSHA256_48(	tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2,
       
   324 				K[40], iData[40], iData[38], iData[33], iData[25], iData[24]);
       
   325 	CSHA256_48(	tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2,
       
   326 				K[41], iData[41], iData[39], iData[34], iData[26], iData[25]);
       
   327 	CSHA256_48(	tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2,
       
   328 				K[42], iData[42], iData[40], iData[35], iData[27], iData[26]);
       
   329 	CSHA256_48(	tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2,
       
   330 				K[43], iData[43], iData[41], iData[36], iData[28], iData[27]);
       
   331 	CSHA256_48(	tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2,
       
   332 				K[44], iData[44], iData[42], iData[37], iData[29], iData[28]);
       
   333 	CSHA256_48(	tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2,
       
   334 				K[45], iData[45], iData[43], iData[38], iData[30], iData[29]);
       
   335 	CSHA256_48(	tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2,
       
   336 				K[46], iData[46], iData[44], iData[39], iData[31], iData[30]);
       
   337 	CSHA256_48(	tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2,
       
   338 				K[47], iData[47], iData[45], iData[40], iData[32], iData[31]);
       
   339 
       
   340 	CSHA256_48(	tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2,
       
   341 				K[48], iData[48], iData[46], iData[41], iData[33], iData[32]);
       
   342 	CSHA256_48(	tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2,
       
   343 				K[49], iData[49], iData[47], iData[42], iData[34], iData[33]);
       
   344 	CSHA256_48(	tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2,
       
   345 				K[50], iData[50], iData[48], iData[43], iData[35], iData[34]);
       
   346 	CSHA256_48(	tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2,
       
   347 				K[51], iData[51], iData[49], iData[44], iData[36], iData[35]);
       
   348 	CSHA256_48(	tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2,
       
   349 				K[52], iData[52], iData[50], iData[45], iData[37], iData[36]);
       
   350 	CSHA256_48(	tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2,
       
   351 				K[53], iData[53], iData[51], iData[46], iData[38], iData[37]);
       
   352 	CSHA256_48(	tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2,
       
   353 				K[54], iData[54], iData[52], iData[47], iData[39], iData[38]);
       
   354 	CSHA256_48(	tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2,
       
   355 				K[55], iData[55], iData[53], iData[48], iData[40], iData[39]);
       
   356 
       
   357 	CSHA256_48(	tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2,
       
   358 				K[56], iData[56], iData[54], iData[49], iData[41], iData[40]);
       
   359 	CSHA256_48(	tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2,
       
   360 				K[57], iData[57], iData[55], iData[50], iData[42], iData[41]);
       
   361 	CSHA256_48(	tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2,
       
   362 				K[58], iData[58], iData[56], iData[51], iData[43], iData[42]);
       
   363 	CSHA256_48(	tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2,
       
   364 				K[59], iData[59], iData[57], iData[52], iData[44], iData[43]);
       
   365 	CSHA256_48(	tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2,
       
   366 				K[60], iData[60], iData[58], iData[53], iData[45], iData[44]);
       
   367 	CSHA256_48(	tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2,
       
   368 				K[61], iData[61], iData[59], iData[54], iData[46], iData[45]);
       
   369 	CSHA256_48(	tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2,
       
   370 				K[62], iData[62], iData[60], iData[55], iData[47], iData[46]);
       
   371 	CSHA256_48(	tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2,
       
   372 				K[63], iData[63], iData[61], iData[56], iData[48], iData[47]);
       
   373 
       
   374 	iA+=tempA;
       
   375 	iB+=tempB;
       
   376 	iC+=tempC;
       
   377 	iD+=tempD;
       
   378 	iE+=tempE;
       
   379 	iF+=tempF;
       
   380 	iG+=tempG;
       
   381 	iH+=tempH;
       
   382 
       
   383 	iNl=0;
       
   384 	}
       
   385 
       
   386 /**
       
   387  * According to the standard, the message must be padded to an
       
   388  * even 512 bits. The first padding bit must be a '1'. The last
       
   389  * 64 bits represent the length of the original message. All bits 
       
   390  * in between should be 0. This helper function will pad the 
       
   391  * message according to those rules by filling the iData array 
       
   392  * accordingly. 
       
   393  */ 
       
   394 void CSHA224And256::PadMessage()
       
   395 	{
       
   396 	const TUint padByte = 0x80;
       
   397 	
       
   398 	if(!(iNl&0x03))
       
   399 		{
       
   400 		iData[iNl >> 2] = 0;
       
   401 		}
       
   402 	iData[iNl >> 2] |= padByte << ((3 - iNl&0x03) << 3) ;
       
   403 
       
   404 	if (iNl >= (KSHA256BlockSize - 2*sizeof(TUint))) 
       
   405 		{
       
   406 		if (iNl < (KSHA256BlockSize - sizeof(TUint)))
       
   407 			iData[(KSHA256BlockSize >> 2) - 1]=0;		
       
   408 		Block();
       
   409 		Mem::FillZ(iData, KSHA256BlockSize);
       
   410 		} 
       
   411 	else
       
   412 		{
       
   413 		const TUint offset=(iNl+4)>>2; //+4 to account for the word added in the
       
   414 		//switch statement above
       
   415 		Mem::FillZ(iData+offset,(KSHA256BlockSize - offset*sizeof(TUint)));
       
   416 		}
       
   417 
       
   418 	//Length in bits
       
   419 	TUint64 msgLength = iNh;
       
   420 
       
   421 	iData[(KSHA256BlockSize >> 2) - 2] = (msgLength) >> 32;
       
   422 	iData[(KSHA256BlockSize >> 2) - 1] = (msgLength & 0xFFFFFFFF);	
       
   423 	}
       
   424 
       
   425 inline void CSHA224And256::CopyWordToHash(TUint aVal, TUint aIndex)
       
   426 	{
       
   427 	TUint value = MakeWord(reinterpret_cast<TUint8*>(&aVal));
       
   428 	Mem::Copy(const_cast<TUint8*>(iHash.Ptr())+ (4*aIndex), &value, sizeof(aVal));
       
   429 	}
       
   430 
       
   431 const TDes8& CSHA224And256::Final()
       
   432 	{
       
   433 	AddLength(iNl);
       
   434 	PadMessage();
       
   435 	Block();
       
   436 	//
       
   437 	// Generate hash value into iHash
       
   438 	//
       
   439 	CopyWordToHash(iA, 0);
       
   440 	CopyWordToHash(iB, 1);
       
   441 	CopyWordToHash(iC, 2);
       
   442 	CopyWordToHash(iD, 3);
       
   443 	CopyWordToHash(iE, 4);
       
   444 	CopyWordToHash(iF, 5);
       
   445 	CopyWordToHash(iG, 6);
       
   446 	CopyWordToHash(iH, 7);
       
   447 	
       
   448 	return iHash;
       
   449 	}
       
   450 
       
   451 void CSHA224And256::RestoreState()
       
   452 	{
       
   453 	iA = iACopy;
       
   454 	iB = iBCopy;
       
   455 	iC = iCCopy;
       
   456 	iD = iDCopy;
       
   457 	iE = iECopy;
       
   458 	iF = iFCopy;
       
   459 	iG = iGCopy;
       
   460 	iH = iHCopy;
       
   461 	iNl = iNlCopy;
       
   462 	iNh = iNhCopy;	
       
   463 	Mem::Copy(iData, iDataCopy, KSHA256BlockSize*sizeof(TUint)); 
       
   464 	}
       
   465 
       
   466 void CSHA224And256::StoreState()
       
   467 	{
       
   468 	iACopy = iA;
       
   469 	iBCopy = iB;
       
   470 	iCCopy = iC;
       
   471 	iDCopy = iD;
       
   472 	iECopy = iE;
       
   473 	iFCopy = iF;
       
   474 	iGCopy = iG;
       
   475 	iHCopy = iH;
       
   476 	iNlCopy = iNl;
       
   477 	iNhCopy = iNh;	
       
   478 	Mem::Copy(iDataCopy, iData, KSHA256BlockSize*sizeof(TUint));
       
   479 	}
       
   480 
       
   481 // Implemented in hmacimpl.cpp or softwarehashbase.cpp
       
   482 // but required as derived from MHash. No coverage here.
       
   483 #ifdef _BullseyeCoverage
       
   484 #pragma suppress_warnings on
       
   485 #pragma BullseyeCoverage off
       
   486 #pragma suppress_warnings off
       
   487 #endif