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