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