crypto/weakcryptospi/source/random/sha1impl.cpp
changeset 19 cd501b96611d
equal deleted inserted replaced
15:da2ae96f639b 19:cd501b96611d
       
     1 /*
       
     2 * Copyright (c) 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 * software sha1 implementation
       
    16 * software sha1 implementation
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 /**
       
    22  @file
       
    23 */
       
    24 
       
    25 #include "sha1impl.h"
       
    26 
       
    27 #include <cryptospi/hashplugin.h>
       
    28 #include "pluginconfig.h"
       
    29 #define EXPANDLOOP
       
    30 
       
    31 
       
    32 using namespace SoftwareCrypto;
       
    33 	
       
    34 CSHA1Impl* CSHA1Impl::NewL()
       
    35 	{
       
    36 	CSHA1Impl* self=new (ELeave) CSHA1Impl();
       
    37 	self->Reset();
       
    38 	return self;						
       
    39 	}
       
    40 												
       
    41 CSHA1Impl::CSHA1Impl() : iHash(KSHA1HashSize)
       
    42 	{		
       
    43 	}
       
    44 
       
    45 void CSHA1Impl::Reset()
       
    46 	{
       
    47 	iA=0x67452301;
       
    48 	iB=0xefcdab89;
       
    49 	iC=0x98badcfe;
       
    50 	iD=0x10325476;
       
    51 	iE=0xc3d2e1f0;
       
    52 	iNh=0;
       
    53 	iNl=0;
       
    54 	}
       
    55 	
       
    56 TUid CSHA1Impl::ImplementationUid()
       
    57 	{
       
    58 	return KCryptoPluginSha1Uid;
       
    59 	}
       
    60 
       
    61 void CSHA1Impl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
       
    62 	{
       
    63 	aPluginCharacteristics=NULL;
       
    64 	TInt hashNum=sizeof(KHashCharacteristics)/sizeof(THashCharacteristics*);
       
    65 	for (TInt i=0;i<hashNum;i++)
       
    66 		{
       
    67 		if (KHashCharacteristics[i]->cmn.iImplementationUID == ImplementationUid().iUid)
       
    68 			{
       
    69 			aPluginCharacteristics = KHashCharacteristics[i];
       
    70 			break;
       
    71 			}
       
    72 		}	
       
    73 	}
       
    74 
       
    75 TPtrC8 CSHA1Impl::Hash(const TDesC8& aMessage)
       
    76 	{
       
    77 	TPtrC8 ptr(KNullDesC8());
       
    78 	DoUpdate(aMessage.Ptr(),aMessage.Size());
       
    79 	StoreState();
       
    80 	DoFinal();
       
    81 	ptr.Set(iHash);
       
    82 	RestoreState();
       
    83 	return ptr;
       
    84 	}
       
    85 
       
    86 // This assumes a big-endian architecture
       
    87 void CSHA1Impl::DoUpdate(const TUint8* aData,TUint aLength)
       
    88 	{
       
    89 	while((aLength / 4) > 0 && (iNl % 4 == 0))
       
    90 		{
       
    91 		iData[iNl>>2] = aData[0] << 24 | aData[1] << 16 | aData[2] << 8 | aData[3];
       
    92 		iNl+=4;
       
    93 		aData+=4;
       
    94 		aLength-=4;
       
    95 		if(iNl==64) 
       
    96 			{
       
    97 			Block();
       
    98 			iNh+=64;
       
    99 			iNl=0;
       
   100 			}
       
   101 		}
       
   102 
       
   103 	while(aLength--)
       
   104 		{
       
   105 		switch (iNl&3) 
       
   106 			{
       
   107 			case 0:
       
   108 				iData[iNl>>2]=((TUint)(*aData))<<24;
       
   109 				break;
       
   110 			case 1:
       
   111 				iData[iNl>>2]|=((TUint)(*aData))<<16;
       
   112 				break;
       
   113 			case 2:
       
   114 				iData[iNl>>2]|=((TUint)(*aData))<<8;
       
   115 				break;
       
   116 			case 3:
       
   117 				iData[iNl>>2]|=((TUint)(*aData));
       
   118 				break;
       
   119 			default:
       
   120 				break;
       
   121 			};
       
   122 			aData++;
       
   123 			iNl++;
       
   124 			if(iNl==64) 
       
   125 				{
       
   126 				Block();
       
   127 				iNh+=64;
       
   128 				iNl=0;
       
   129 				}
       
   130 		}
       
   131 	}
       
   132 
       
   133 static inline TUint CSHA1_F(const TUint x,const TUint y,const TUint z)
       
   134 	{
       
   135 	return (x&y) | (~x&z);
       
   136 	}
       
   137 
       
   138 static inline TUint CSHA1_G(const TUint x,const TUint y,const TUint z)
       
   139 	{
       
   140 	return x^y^z;
       
   141 	}
       
   142 
       
   143 static inline TUint CSHA1_H(const TUint x,const TUint y,const TUint z)
       
   144 	{
       
   145 	return (x&y) | (x&z) | (y&z);
       
   146 	}
       
   147 
       
   148 /*static inline TUint CSHA1_I(const TUint x,const TUint y,const TUint z)
       
   149 	{
       
   150 	return x^y^z;
       
   151 	}*/
       
   152 
       
   153 #ifdef EXPANDLOOP
       
   154 
       
   155 #ifdef MACRO
       
   156 
       
   157 #define CSHA1_16(x,y,z,u,t,v,w)					v=CMD_R(x,5)+CSHA1_F(y,z,u)+t+w+0x5a827999;\
       
   158 												y=CMD_R(y,30);t=v;
       
   159 #define CSHA1_20(x,y,z,u,t,v,w0,w3,w8,w14,w16)  v=w3^w8^w14^w16;w0=CMD_R(v,1);\
       
   160 												CSHA1_16(x,y,z,u,t,v,w0);
       
   161 #define CSHA1_40(x,y,z,u,t,v,w0,w3,w8,w14,w16)	v=w3^w8^w14^w16;w0=CMD_R(v,1);\
       
   162 												v=CMD_R(x,5)+CSHA1_G(y,z,u)+t+w0+0x6ed9eba1;\
       
   163 												y=CMD_R(y,30);t=v;
       
   164 #define CSHA1_60(x,y,z,u,t,v,w0,w3,w8,w14,w16)	v=w3^w8^w14^w16;w0=CMD_R(v,1);\
       
   165 												v=CMD_R(x,5)+CSHA1_H(y,z,u)+t+w0+0x8f1bbcdc;\
       
   166 												y=CMD_R(y,30);t=v;
       
   167 #define CSHA1_80(x,y,z,u,t,v,w0,w3,w8,w14,w16)	v=w3^w8^w14^w16;w0=CMD_R(v,1);\
       
   168 												v=CMD_R(x,5)+CSHA1_G(y,z,u)+t+w0+0xca62c1d6;\
       
   169 												y=CMD_R(y,30);t=v;
       
   170 #else
       
   171 
       
   172 static inline void CSHA1_16(const TUint x, TUint& y, const TUint z,
       
   173 							const TUint u, TUint& t, TUint& v, const TUint w)
       
   174 	{
       
   175 	v = CMD_R(x,5) + CSHA1_F(y,z,u) + t + w + 0x5a827999;
       
   176 	y = CMD_R(y,30);
       
   177 	t = v;
       
   178 	}
       
   179 
       
   180 static inline void CSHA1_20(const TUint x,TUint& y,const TUint z,
       
   181 							const TUint u,TUint& t,TUint& v,
       
   182 							TUint& w0,const TUint w3,const TUint w8,
       
   183 							const TUint w14,const TUint w16)
       
   184 	{
       
   185 	v = w3 ^ w8 ^ w14 ^ w16;
       
   186 	w0 = CMD_R(v,1);
       
   187 	CSHA1_16(x,y,z,u,t,v,w0);
       
   188 	}
       
   189 
       
   190 static inline void CSHA1_40(const TUint x,TUint& y,const TUint z,
       
   191 							const TUint u,TUint& t,TUint& v,
       
   192 							TUint& w0,const TUint w3,const TUint w8,
       
   193 							const TUint w14,const TUint w16)
       
   194 	{
       
   195 	v = w3 ^ w8 ^ w14 ^ w16;
       
   196 	w0 = CMD_R(v,1);
       
   197 	v = CMD_R(x,5) + CSHA1_G(y,z,u) + t + w0 + 0x6ed9eba1;
       
   198 	y = CMD_R(y,30);
       
   199 	t = v;
       
   200 	}
       
   201 
       
   202 static inline void CSHA1_60(const TUint x,TUint& y,const TUint z,
       
   203 							const TUint u,TUint& t,TUint& v,
       
   204 							TUint& w0,const TUint w3,const TUint w8,
       
   205 							const TUint w14,const TUint w16)
       
   206 	{
       
   207 	v = w3 ^ w8 ^ w14 ^ w16;
       
   208 	w0 = CMD_R(v,1);
       
   209 	v = CMD_R(x,5) + CSHA1_H(y,z,u) + t + w0 + 0x8f1bbcdc;
       
   210 	y = CMD_R(y,30);
       
   211 	t = v;
       
   212 	}
       
   213 
       
   214 static inline void CSHA1_80(const TUint x,TUint& y,const TUint z,
       
   215 							const TUint u,TUint& t,TUint& v,
       
   216 							TUint& w0,const TUint w3,const TUint w8,
       
   217 							const TUint w14,const TUint w16)
       
   218 	{
       
   219 	v = w3 ^ w8 ^ w14 ^ w16;
       
   220 	w0 = CMD_R(v,1);
       
   221 	v = CMD_R(x,5) + CSHA1_G(y,z,u) + t + w0 + 0xca62c1d6;
       
   222 	y = CMD_R(y,30);
       
   223 	t = v;
       
   224 	}
       
   225 
       
   226 #endif // MACRO
       
   227 #endif // EXPANDLOOP
       
   228 
       
   229 #ifdef WEIDAI
       
   230 
       
   231 template <class T> inline T rotlFixed(T x, unsigned int y)
       
   232 {
       
   233 	ASSERT(y < sizeof(T)*8);
       
   234 	return (x<<y) | (x>>(sizeof(T)*8-y));
       
   235 }
       
   236 
       
   237 template<> inline TUint32 rotlFixed<TUint32>(TUint32 x, unsigned int y)
       
   238 {
       
   239 	ASSERT(y < 32);
       
   240 	return y ? CMD_R(x, y) : x;
       
   241 }
       
   242 
       
   243 #define blk0(i) (W[i] = iData[i])
       
   244 #define blk1(i) (W[i&15] = rotlFixed(W[(i+13)&15]^W[(i+8)&15]^W[(i+2)&15]^W[i&15],1))
       
   245 
       
   246 #define f1(x,y,z) (z^(x&(y^z)))
       
   247 #define f2(x,y,z) (x^y^z)
       
   248 #define f3(x,y,z) ((x&y)|(z&(x|y)))
       
   249 #define f4(x,y,z) (x^y^z)
       
   250 
       
   251 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
       
   252 #define R0(v,w,x,y,z,i) z+=f1(w,x,y)+blk0(i)+0x5A827999+rotlFixed(v,5);w=rotlFixed(w,30);
       
   253 #define R1(v,w,x,y,z,i) z+=f1(w,x,y)+blk1(i)+0x5A827999+rotlFixed(v,5);w=rotlFixed(w,30);
       
   254 #define R2(v,w,x,y,z,i) z+=f2(w,x,y)+blk1(i)+0x6ED9EBA1+rotlFixed(v,5);w=rotlFixed(w,30);
       
   255 #define R3(v,w,x,y,z,i) z+=f3(w,x,y)+blk1(i)+0x8F1BBCDC+rotlFixed(v,5);w=rotlFixed(w,30);
       
   256 #define R4(v,w,x,y,z,i) z+=f4(w,x,y)+blk1(i)+0xCA62C1D6+rotlFixed(v,5);w=rotlFixed(w,30);
       
   257 
       
   258 #endif // WEIDAI
       
   259 
       
   260 void CSHA1Impl::Block()
       
   261 	{
       
   262 #ifdef WEIDAI
       
   263 	TUint32 W[16];
       
   264     /* Copy context->state[] to working vars */
       
   265     TUint32 a = iA;
       
   266     TUint32 b = iB;
       
   267     TUint32 c = iC;
       
   268     TUint32 d = iD;
       
   269     TUint32 e = iE;
       
   270     
       
   271 	/* 4 rounds of 20 operations each. Loop unrolled. */
       
   272     
       
   273 	R0(a,b,c,d,e, 0); 
       
   274 	R0(e,a,b,c,d, 1); 
       
   275 	R0(d,e,a,b,c, 2); 
       
   276 	R0(c,d,e,a,b, 3);
       
   277     R0(b,c,d,e,a, 4); 
       
   278 	R0(a,b,c,d,e, 5); 
       
   279 	R0(e,a,b,c,d, 6); 
       
   280 	R0(d,e,a,b,c, 7);
       
   281     R0(c,d,e,a,b, 8); 
       
   282 	R0(b,c,d,e,a, 9); 
       
   283 	R0(a,b,c,d,e,10); 
       
   284 	R0(e,a,b,c,d,11);
       
   285     R0(d,e,a,b,c,12); 
       
   286 	R0(c,d,e,a,b,13); 
       
   287 	R0(b,c,d,e,a,14); 
       
   288 	R0(a,b,c,d,e,15);
       
   289 
       
   290     R1(e,a,b,c,d,16); 
       
   291 	R1(d,e,a,b,c,17); 
       
   292 	R1(c,d,e,a,b,18); 
       
   293 	R1(b,c,d,e,a,19);
       
   294 
       
   295     R2(a,b,c,d,e,20); 
       
   296 	R2(e,a,b,c,d,21); 
       
   297 	R2(d,e,a,b,c,22); 
       
   298 	R2(c,d,e,a,b,23);
       
   299     R2(b,c,d,e,a,24); 
       
   300 	R2(a,b,c,d,e,25); 
       
   301 	R2(e,a,b,c,d,26); 
       
   302 	R2(d,e,a,b,c,27);
       
   303     R2(c,d,e,a,b,28); 
       
   304 	R2(b,c,d,e,a,29); 
       
   305 	R2(a,b,c,d,e,30); 
       
   306 	R2(e,a,b,c,d,31);
       
   307     R2(d,e,a,b,c,32); 
       
   308 	R2(c,d,e,a,b,33); 
       
   309 	R2(b,c,d,e,a,34); 
       
   310 	R2(a,b,c,d,e,35);
       
   311     R2(e,a,b,c,d,36); 
       
   312 	R2(d,e,a,b,c,37); 
       
   313 	R2(c,d,e,a,b,38); 
       
   314 	R2(b,c,d,e,a,39);
       
   315 
       
   316     R3(a,b,c,d,e,40); 
       
   317 	R3(e,a,b,c,d,41); 
       
   318 	R3(d,e,a,b,c,42); 
       
   319 	R3(c,d,e,a,b,43);
       
   320     R3(b,c,d,e,a,44); 
       
   321 	R3(a,b,c,d,e,45); 
       
   322 	R3(e,a,b,c,d,46); 
       
   323 	R3(d,e,a,b,c,47);
       
   324     R3(c,d,e,a,b,48); 
       
   325 	R3(b,c,d,e,a,49); 
       
   326 	R3(a,b,c,d,e,50); 
       
   327 	R3(e,a,b,c,d,51);
       
   328     R3(d,e,a,b,c,52); 
       
   329 	R3(c,d,e,a,b,53); 
       
   330 	R3(b,c,d,e,a,54); 
       
   331 	R3(a,b,c,d,e,55);
       
   332     R3(e,a,b,c,d,56); 
       
   333 	R3(d,e,a,b,c,57); 
       
   334 	R3(c,d,e,a,b,58); 
       
   335 	R3(b,c,d,e,a,59);
       
   336 
       
   337     R4(a,b,c,d,e,60); 
       
   338 	R4(e,a,b,c,d,61); 
       
   339 	R4(d,e,a,b,c,62); 
       
   340 	R4(c,d,e,a,b,63);
       
   341     R4(b,c,d,e,a,64); 
       
   342 	R4(a,b,c,d,e,65); 
       
   343 	R4(e,a,b,c,d,66); 
       
   344 	R4(d,e,a,b,c,67);
       
   345     R4(c,d,e,a,b,68); 
       
   346 	R4(b,c,d,e,a,69); 
       
   347 	R4(a,b,c,d,e,70); 
       
   348 	R4(e,a,b,c,d,71);
       
   349     R4(d,e,a,b,c,72); 
       
   350 	R4(c,d,e,a,b,73); 
       
   351 	R4(b,c,d,e,a,74); 
       
   352 	R4(a,b,c,d,e,75);
       
   353     R4(e,a,b,c,d,76); 
       
   354 	R4(d,e,a,b,c,77); 
       
   355 	R4(c,d,e,a,b,78); 
       
   356 	R4(b,c,d,e,a,79);
       
   357     
       
   358 	/* Add the working vars back into context.state[] */
       
   359     iA += a;
       
   360     iB += b;
       
   361     iC += c;
       
   362     iD += d;
       
   363     iE += e;
       
   364     /* Wipe variables */
       
   365     a = b = c = d = e = 0;
       
   366 	Mem::FillZ(W, sizeof(W));
       
   367 #else
       
   368 	TUint tempA=iA;
       
   369 	TUint tempB=iB;
       
   370 	TUint tempC=iC;
       
   371 	TUint tempD=iD;
       
   372 	TUint tempE=iE;
       
   373 	TUint temp=0;
       
   374 
       
   375 #ifdef EXPANDLOOP
       
   376 	CSHA1_16(tempA,tempB,tempC,tempD,tempE,temp,iData[0]);
       
   377 	CSHA1_16(temp,tempA,tempB,tempC,tempD,tempE,iData[1]);
       
   378 	CSHA1_16(tempE,temp,tempA,tempB,tempC,tempD,iData[2]);
       
   379 	CSHA1_16(tempD,tempE,temp,tempA,tempB,tempC,iData[3]);
       
   380 	CSHA1_16(tempC,tempD,tempE,temp,tempA,tempB,iData[4]);
       
   381 	CSHA1_16(tempB,tempC,tempD,tempE,temp,tempA,iData[5]);
       
   382 	CSHA1_16(tempA,tempB,tempC,tempD,tempE,temp,iData[6]);
       
   383 	CSHA1_16(temp,tempA,tempB,tempC,tempD,tempE,iData[7]);
       
   384 	CSHA1_16(tempE,temp,tempA,tempB,tempC,tempD,iData[8]);
       
   385 	CSHA1_16(tempD,tempE,temp,tempA,tempB,tempC,iData[9]);
       
   386 	CSHA1_16(tempC,tempD,tempE,temp,tempA,tempB,iData[10]);
       
   387 	CSHA1_16(tempB,tempC,tempD,tempE,temp,tempA,iData[11]);
       
   388 	CSHA1_16(tempA,tempB,tempC,tempD,tempE,temp,iData[12]);
       
   389 	CSHA1_16(temp,tempA,tempB,tempC,tempD,tempE,iData[13]);
       
   390 	CSHA1_16(tempE,temp,tempA,tempB,tempC,tempD,iData[14]);
       
   391 	CSHA1_16(tempD,tempE,temp,tempA,tempB,tempC,iData[15]);
       
   392 	/*
       
   393 	i = 16;
       
   394 	TUint temp1 = tempA;
       
   395 	tempA = 
       
   396 	*/
       
   397 #else
       
   398     TUint i=0;
       
   399 	while (i<16) 
       
   400 		{
       
   401 		temp = CMD_R(tempA,5) + CSHA1_F(tempB,tempC,tempD) + tempE + iData[i++] + 0x5a827999;
       
   402 		tempE = tempD;
       
   403 		tempD = tempC;
       
   404 		tempC = CMD_R(tempB,30);
       
   405 		tempB = tempA;
       
   406 		tempA = temp;
       
   407 		}
       
   408 #endif
       
   409 
       
   410 #ifdef EXPANDLOOP
       
   411 	CSHA1_20(tempC,tempD,tempE,temp,tempA,tempB,iData[16],iData[13],iData[8],iData[2],iData[0]);
       
   412 	CSHA1_20(tempB,tempC,tempD,tempE,temp,tempA,iData[17],iData[14],iData[9],iData[3],iData[1]);
       
   413 	CSHA1_20(tempA,tempB,tempC,tempD,tempE,temp,iData[18],iData[15],iData[10],iData[4],iData[2]);
       
   414 	CSHA1_20(temp,tempA,tempB,tempC,tempD,tempE,iData[19],iData[16],iData[11],iData[5],iData[3]);
       
   415 	//i = 20;
       
   416 #else
       
   417 	while (i<20) 
       
   418 		{
       
   419 		temp=iData[i-3] ^ iData[i-8] ^ iData[i-14] ^ iData[i-16];
       
   420 		iData[i]=CMD_R(temp,1);
       
   421 		temp = CMD_R(tempA,5) + CSHA1_F(tempB,tempC,tempD) + tempE + iData[i++] + 0x5a827999; 
       
   422 		tempE = tempD;
       
   423 		tempD = tempC; 
       
   424 		tempC = CMD_R(tempB,30); 
       
   425 		tempB = tempA; 
       
   426 		tempA = temp;
       
   427 		}
       
   428 #endif
       
   429 
       
   430 #ifdef EXPANDLOOP
       
   431 	CSHA1_40(tempE,temp,tempA,tempB,tempC,tempD,iData[20],iData[17],iData[12],iData[6],iData[4]);
       
   432 	CSHA1_40(tempD,tempE,temp,tempA,tempB,tempC,iData[21],iData[18],iData[13],iData[7],iData[5]);
       
   433 	CSHA1_40(tempC,tempD,tempE,temp,tempA,tempB,iData[22],iData[19],iData[14],iData[8],iData[6]);
       
   434 	CSHA1_40(tempB,tempC,tempD,tempE,temp,tempA,iData[23],iData[20],iData[15],iData[9],iData[7]);
       
   435 	CSHA1_40(tempA,tempB,tempC,tempD,tempE,temp,iData[24],iData[21],iData[16],iData[10],iData[8]);
       
   436 	CSHA1_40(temp,tempA,tempB,tempC,tempD,tempE,iData[25],iData[22],iData[17],iData[11],iData[9]);
       
   437 	CSHA1_40(tempE,temp,tempA,tempB,tempC,tempD,iData[26],iData[23],iData[18],iData[12],iData[10]);
       
   438 	CSHA1_40(tempD,tempE,temp,tempA,tempB,tempC,iData[27],iData[24],iData[19],iData[13],iData[11]);
       
   439 	CSHA1_40(tempC,tempD,tempE,temp,tempA,tempB,iData[28],iData[25],iData[20],iData[14],iData[12]);
       
   440 	CSHA1_40(tempB,tempC,tempD,tempE,temp,tempA,iData[29],iData[26],iData[21],iData[15],iData[13]);
       
   441 	CSHA1_40(tempA,tempB,tempC,tempD,tempE,temp,iData[30],iData[27],iData[22],iData[16],iData[14]);
       
   442 	CSHA1_40(temp,tempA,tempB,tempC,tempD,tempE,iData[31],iData[28],iData[23],iData[17],iData[15]);
       
   443 	CSHA1_40(tempE,temp,tempA,tempB,tempC,tempD,iData[32],iData[29],iData[24],iData[18],iData[16]);
       
   444 	CSHA1_40(tempD,tempE,temp,tempA,tempB,tempC,iData[33],iData[30],iData[25],iData[19],iData[17]);
       
   445 	CSHA1_40(tempC,tempD,tempE,temp,tempA,tempB,iData[34],iData[31],iData[26],iData[20],iData[18]);
       
   446 	CSHA1_40(tempB,tempC,tempD,tempE,temp,tempA,iData[35],iData[32],iData[27],iData[21],iData[19]);
       
   447 	CSHA1_40(tempA,tempB,tempC,tempD,tempE,temp,iData[36],iData[33],iData[28],iData[22],iData[20]);
       
   448 	CSHA1_40(temp,tempA,tempB,tempC,tempD,tempE,iData[37],iData[34],iData[29],iData[23],iData[21]);
       
   449 	CSHA1_40(tempE,temp,tempA,tempB,tempC,tempD,iData[38],iData[35],iData[30],iData[24],iData[22]);
       
   450 	CSHA1_40(tempD,tempE,temp,tempA,tempB,tempC,iData[39],iData[36],iData[31],iData[25],iData[23]);
       
   451 	//i = 40;
       
   452 #else
       
   453 	while (i<40) 
       
   454 		{
       
   455 		temp = iData[i-3] ^ iData[i-8] ^ iData[i-14] ^ iData[i-16];
       
   456 		iData[i] = CMD_R(temp,1);
       
   457 
       
   458 		temp = CMD_R(tempA,5) + CSHA1_G(tempB,tempC,tempD) + tempE + iData[i++] + 0x6ed9eba1; 
       
   459 		tempE = tempD; 
       
   460 		tempD = tempC; 
       
   461 		tempC = CMD_R(tempB,30); 
       
   462 		tempB = tempA; 
       
   463 		tempA = temp;
       
   464 		}
       
   465 #endif
       
   466 
       
   467 #ifdef EXPANDLOOP
       
   468 	CSHA1_60(tempC,tempD,tempE,temp,tempA,tempB,iData[40],iData[37],iData[32],iData[26],iData[24]);
       
   469 	CSHA1_60(tempB,tempC,tempD,tempE,temp,tempA,iData[41],iData[38],iData[33],iData[27],iData[25]);
       
   470 	CSHA1_60(tempA,tempB,tempC,tempD,tempE,temp,iData[42],iData[39],iData[34],iData[28],iData[26]);
       
   471 	CSHA1_60(temp,tempA,tempB,tempC,tempD,tempE,iData[43],iData[40],iData[35],iData[29],iData[27]);
       
   472 	CSHA1_60(tempE,temp,tempA,tempB,tempC,tempD,iData[44],iData[41],iData[36],iData[30],iData[28]);
       
   473 	CSHA1_60(tempD,tempE,temp,tempA,tempB,tempC,iData[45],iData[42],iData[37],iData[31],iData[29]);
       
   474 	CSHA1_60(tempC,tempD,tempE,temp,tempA,tempB,iData[46],iData[43],iData[38],iData[32],iData[30]);
       
   475 	CSHA1_60(tempB,tempC,tempD,tempE,temp,tempA,iData[47],iData[44],iData[39],iData[33],iData[31]);
       
   476 	CSHA1_60(tempA,tempB,tempC,tempD,tempE,temp,iData[48],iData[45],iData[40],iData[34],iData[32]);
       
   477 	CSHA1_60(temp,tempA,tempB,tempC,tempD,tempE,iData[49],iData[46],iData[41],iData[35],iData[33]);
       
   478 	CSHA1_60(tempE,temp,tempA,tempB,tempC,tempD,iData[50],iData[47],iData[42],iData[36],iData[34]);
       
   479 	CSHA1_60(tempD,tempE,temp,tempA,tempB,tempC,iData[51],iData[48],iData[43],iData[37],iData[35]);
       
   480 	CSHA1_60(tempC,tempD,tempE,temp,tempA,tempB,iData[52],iData[49],iData[44],iData[38],iData[36]);
       
   481 	CSHA1_60(tempB,tempC,tempD,tempE,temp,tempA,iData[53],iData[50],iData[45],iData[39],iData[37]);
       
   482 	CSHA1_60(tempA,tempB,tempC,tempD,tempE,temp,iData[54],iData[51],iData[46],iData[40],iData[38]);
       
   483 	CSHA1_60(temp,tempA,tempB,tempC,tempD,tempE,iData[55],iData[52],iData[47],iData[41],iData[39]);
       
   484 	CSHA1_60(tempE,temp,tempA,tempB,tempC,tempD,iData[56],iData[53],iData[48],iData[42],iData[40]);
       
   485 	CSHA1_60(tempD,tempE,temp,tempA,tempB,tempC,iData[57],iData[54],iData[49],iData[43],iData[41]);
       
   486 	CSHA1_60(tempC,tempD,tempE,temp,tempA,tempB,iData[58],iData[55],iData[50],iData[44],iData[42]);
       
   487 	CSHA1_60(tempB,tempC,tempD,tempE,temp,tempA,iData[59],iData[56],iData[51],iData[45],iData[43]);
       
   488 	//i = 60;
       
   489 #else
       
   490 	while (i<60) 
       
   491 		{
       
   492 		temp = iData[i-3] ^ iData[i-8] ^ iData[i-14] ^ iData[i-16];
       
   493 		iData[i] = CMD_R(temp,1);
       
   494 
       
   495 		temp = CMD_R(tempA,5) + CSHA1_H(tempB,tempC,tempD) + tempE + iData[i++] + 0x8f1bbcdc; 
       
   496 		tempE = tempD; 
       
   497 		tempD = tempC; 
       
   498 		tempC = CMD_R(tempB,30); 
       
   499 		tempB = tempA; 
       
   500 		tempA = temp;
       
   501 		}
       
   502 #endif
       
   503 
       
   504 #ifdef EXPANDLOOP
       
   505 	CSHA1_80(tempA,tempB,tempC,tempD,tempE,temp,iData[60],iData[57],iData[52],iData[46],iData[44]);
       
   506 	CSHA1_80(temp,tempA,tempB,tempC,tempD,tempE,iData[61],iData[58],iData[53],iData[47],iData[45]);
       
   507 	CSHA1_80(tempE,temp,tempA,tempB,tempC,tempD,iData[62],iData[59],iData[54],iData[48],iData[46]);
       
   508 	CSHA1_80(tempD,tempE,temp,tempA,tempB,tempC,iData[63],iData[60],iData[55],iData[49],iData[47]);
       
   509 	CSHA1_80(tempC,tempD,tempE,temp,tempA,tempB,iData[64],iData[61],iData[56],iData[50],iData[48]);
       
   510 	CSHA1_80(tempB,tempC,tempD,tempE,temp,tempA,iData[65],iData[62],iData[57],iData[51],iData[49]);
       
   511 	CSHA1_80(tempA,tempB,tempC,tempD,tempE,temp,iData[66],iData[63],iData[58],iData[52],iData[50]);
       
   512 	CSHA1_80(temp,tempA,tempB,tempC,tempD,tempE,iData[67],iData[64],iData[59],iData[53],iData[51]);
       
   513 	CSHA1_80(tempE,temp,tempA,tempB,tempC,tempD,iData[68],iData[65],iData[60],iData[54],iData[52]);
       
   514 	CSHA1_80(tempD,tempE,temp,tempA,tempB,tempC,iData[69],iData[66],iData[61],iData[55],iData[53]);
       
   515 	CSHA1_80(tempC,tempD,tempE,temp,tempA,tempB,iData[70],iData[67],iData[62],iData[56],iData[54]);
       
   516 	CSHA1_80(tempB,tempC,tempD,tempE,temp,tempA,iData[71],iData[68],iData[63],iData[57],iData[55]);
       
   517 	CSHA1_80(tempA,tempB,tempC,tempD,tempE,temp,iData[72],iData[69],iData[64],iData[58],iData[56]);
       
   518 	CSHA1_80(temp,tempA,tempB,tempC,tempD,tempE,iData[73],iData[70],iData[65],iData[59],iData[57]);
       
   519 	CSHA1_80(tempE,temp,tempA,tempB,tempC,tempD,iData[74],iData[71],iData[66],iData[60],iData[58]);
       
   520 	CSHA1_80(tempD,tempE,temp,tempA,tempB,tempC,iData[75],iData[72],iData[67],iData[61],iData[59]);
       
   521 	CSHA1_80(tempC,tempD,tempE,temp,tempA,tempB,iData[76],iData[73],iData[68],iData[62],iData[60]);
       
   522 	CSHA1_80(tempB,tempC,tempD,tempE,temp,tempA,iData[77],iData[74],iData[69],iData[63],iData[61]);
       
   523 	CSHA1_80(tempA,tempB,tempC,tempD,tempE,temp,iData[78],iData[75],iData[70],iData[64],iData[62]);
       
   524 	CSHA1_80(temp,tempA,tempB,tempC,tempD,tempE,iData[79],iData[76],iData[71],iData[65],iData[63]);
       
   525 #else
       
   526 	const TUint total=KSHA1BlockSize*5; // 16 * 5 = 80
       
   527 	while (i<total) 
       
   528 		{
       
   529 		temp = iData[i-3] ^ iData[i-8] ^ iData[i-14] ^ iData[i-16];
       
   530 		iData[i] = CMD_R(temp,1);
       
   531 
       
   532 		temp = CMD_R(tempA,5) + CSHA1_I(tempB,tempC,tempD) + tempE + iData[i++] + 0xca62c1d6; 
       
   533 		tempE = tempD; 
       
   534 		tempD = tempC; 
       
   535 		tempC = CMD_R(tempB,30); 
       
   536 		tempB = tempA; 
       
   537 		tempA = temp;
       
   538 		}
       
   539 #endif
       
   540 
       
   541 #ifdef EXPANDLOOP
       
   542 	iA+=tempE;
       
   543 	iB+=temp;
       
   544 	iC+=tempA;
       
   545 	iD+=tempB;
       
   546 	iE+=tempC;
       
   547 #else
       
   548 	iA+=tempA;
       
   549 	iB+=tempB;
       
   550 	iC+=tempC;
       
   551 	iD+=tempD;
       
   552 	iE+=tempE;
       
   553 #endif // EXPANDLOOP
       
   554 #endif // WEIDAI
       
   555 	}
       
   556 
       
   557 void CSHA1Impl::DoFinal()
       
   558 	{
       
   559 	iNh += iNl;
       
   560 	const TUint ul128=128;
       
   561 	switch (iNl&3) 
       
   562 		{
       
   563 		case 0:
       
   564 			iData[iNl>>2] = ul128<<24;
       
   565 			break;
       
   566 		case 1:
       
   567 			iData[iNl>>2] += ul128<<16;
       
   568 			break;
       
   569 		case 2:
       
   570 			iData[iNl>>2] += ul128<<8;
       
   571 			break;
       
   572 		case 3:
       
   573 			iData[iNl>>2] += ul128;
       
   574 			break;
       
   575 		default:
       
   576 			break;
       
   577 		};
       
   578 	if (iNl>=56) 
       
   579 		{
       
   580 		if (iNl<60)
       
   581 			iData[15]=0;		
       
   582 		Block();
       
   583 		Mem::FillZ(iData,14*sizeof(TUint));
       
   584 		} 
       
   585 	else
       
   586 		{
       
   587 		const TUint offset=(iNl+4)>>2; //+4 to account for the word added in the
       
   588 		//switch statement above
       
   589 		Mem::FillZ(iData+offset,(14-offset)*sizeof(TUint));
       
   590 		}
       
   591 
       
   592 	// this will fail if the total input length is longer than 2^32 in bits
       
   593 	//(2^31 in bytes) which is roughly half a gig.
       
   594 	iData[14]=0;
       
   595 	iData[15]=iNh<<3;//number in bits
       
   596 	Block();
       
   597 	//
       
   598 	// Generate hash value into iHash
       
   599 	//
       
   600 	TUint tmp=iA;
       
   601 	iHash[3]=(TUint8)(tmp & 255);
       
   602 	iHash[2]=(TUint8)((tmp >>= 8) & 255);
       
   603 	iHash[1]=(TUint8)((tmp >>= 8) & 255);
       
   604 	iHash[0]=(TUint8)((tmp >>= 8) & 255);
       
   605 
       
   606 	tmp=iB;
       
   607 	iHash[7]=(TUint8)(tmp & 255);
       
   608 	iHash[6]=(TUint8)((tmp >>= 8) & 255);
       
   609 	iHash[5]=(TUint8)((tmp >>= 8) & 255);
       
   610 	iHash[4]=(TUint8)((tmp >>= 8) & 255);
       
   611 
       
   612 	tmp=iC;
       
   613 	iHash[11]=(TUint8)(tmp & 255);
       
   614 	iHash[10]=(TUint8)((tmp >>= 8) & 255);
       
   615 	iHash[9]=(TUint8)((tmp >>= 8) & 255);
       
   616 	iHash[8]=(TUint8)((tmp >>= 8) & 255);
       
   617 
       
   618 	tmp=iD;
       
   619 	iHash[15]=(TUint8)(tmp & 255);
       
   620 	iHash[14]=(TUint8)((tmp >>= 8) & 255);
       
   621 	iHash[13]=(TUint8)((tmp >>= 8) & 255);
       
   622 	iHash[12]=(TUint8)((tmp >>= 8) & 255);
       
   623 	
       
   624 	tmp=iE;
       
   625 	iHash[19]=(TUint8)(tmp & 255);
       
   626 	iHash[18]=(TUint8)((tmp >>= 8) & 255);
       
   627 	iHash[17]=(TUint8)((tmp >>= 8) & 255);
       
   628 	iHash[16]=(TUint8)((tmp >>= 8) & 255);
       
   629 	}
       
   630 
       
   631 void CSHA1Impl::RestoreState()
       
   632 	{
       
   633 	iA = iACopy;
       
   634 	iB = iBCopy;
       
   635 	iC = iCCopy;
       
   636 	iD = iDCopy;
       
   637 	iE = iECopy;
       
   638 	iNl = iNlCopy;
       
   639 	iNh = iNhCopy;	
       
   640 	Mem::Copy(&iData[0], &iDataCopy[0], KSHA1BlockSize*5*sizeof(TUint)); 
       
   641 	}
       
   642 
       
   643 void CSHA1Impl::StoreState()
       
   644 	{
       
   645 	iACopy = iA;
       
   646 	iBCopy = iB;
       
   647 	iCCopy = iC;
       
   648 	iDCopy = iD;
       
   649 	iECopy = iE;
       
   650 	iNlCopy = iNl;
       
   651 	iNhCopy = iNh;	
       
   652 	Mem::Copy(&iDataCopy[0], &iData[0], KSHA1BlockSize*5*sizeof(TUint));
       
   653 	}
       
   654 
       
   655 // Implemented in hmacimpl.cpp or softwarehashbase.cpp
       
   656 // but required as derived from MHash. No coverage here.
       
   657 #ifdef _BullseyeCoverage
       
   658 #pragma suppress_warnings on
       
   659 #pragma BullseyeCoverage off
       
   660 #pragma suppress_warnings off
       
   661 #endif
       
   662 
       
   663 void CSHA1Impl::SetOperationModeL(TUid /*aOperationMode*/)
       
   664 	{
       
   665 	User::Leave(KErrNotSupported);
       
   666 	}
       
   667 
       
   668 void CSHA1Impl::SetKeyL(const CKey& /*aKey*/)
       
   669 	{
       
   670 	User::Leave(KErrNotSupported);
       
   671 	}
       
   672 
       
   673 TAny* CSHA1Impl::GetExtension(TUid /*aExtensionId*/)
       
   674 	{
       
   675 	return NULL;	
       
   676 	}
       
   677 
       
   678 CExtendedCharacteristics* CSHA1Impl::CreateExtendedCharacteristicsL()
       
   679 	{
       
   680 	// Not supported
       
   681 	return NULL;
       
   682 	}
       
   683 
       
   684 // The following methods are kept for compatibility but are not used by 
       
   685 // randsvr.exe (via SHA1Shim). So, Turn off coverage for these.
       
   686 MHash* CSHA1Impl::ReplicateL()
       
   687 	{	 
       
   688 	return CSHA1Impl::NewL();
       
   689 	}
       
   690 	
       
   691 MHash* CSHA1Impl::CopyL()
       
   692 	{
       
   693 	return new(ELeave) CSHA1Impl(*this);	
       
   694 	}
       
   695 
       
   696 void CSHA1Impl::Update(const TDesC8& aMessage)
       
   697 	{
       
   698 	DoUpdate(aMessage.Ptr(),aMessage.Size());	
       
   699 	}
       
   700 	
       
   701 TPtrC8 CSHA1Impl::Final(const TDesC8& aMessage)
       
   702 	{
       
   703 	TPtrC8 ptr(KNullDesC8());
       
   704 	if (aMessage!=KNullDesC8())
       
   705 		{
       
   706 		DoUpdate(aMessage.Ptr(),aMessage.Size());			
       
   707 		}
       
   708 	DoFinal();
       
   709 	ptr.Set(iHash);
       
   710 	Reset();
       
   711 	return ptr;
       
   712 	}
       
   713 
       
   714 // Since CreateExtendedCharacteristicsL is not supported, the method which using it also can not be supported.
       
   715 const CExtendedCharacteristics* CSHA1Impl::GetExtendedCharacteristicsL()
       
   716 	{
       
   717 	return CSHA1Impl::CreateExtendedCharacteristicsL();
       
   718 	}
       
   719 
       
   720 // These methods can only be covered from SHA1Shim, but not get covered because the 
       
   721 // SHA1Shim not get destroyed anywhere(may be a long runing service). So, these are excluded.
       
   722 CSHA1Impl::CSHA1Impl(const CSHA1Impl& aSHA1Impl)
       
   723 	: iHash(aSHA1Impl.iHash),iA(aSHA1Impl.iA),iB(aSHA1Impl.iB),iC(aSHA1Impl.iC),iD(aSHA1Impl.iD),iE(aSHA1Impl.iE),
       
   724 	iNl(aSHA1Impl.iNl),iNh(aSHA1Impl.iNh)
       
   725 	{
       
   726 	(void)Mem::Copy(iData, aSHA1Impl.iData, KSHA1BlockSize*5);
       
   727 	}
       
   728 
       
   729 CSHA1Impl* CSHA1Impl::NewLC()
       
   730 	{
       
   731 	CSHA1Impl* self=NewL();
       
   732 	CleanupStack::PushL(self);
       
   733 	return self;						
       
   734 	}
       
   735 
       
   736 void CSHA1Impl::Close()
       
   737 	{
       
   738 	delete this;	
       
   739 	}
       
   740 
       
   741 CSHA1Impl::~CSHA1Impl()
       
   742 	{	
       
   743 	}