1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/sha1impl.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,741 @@
1.4 +/*
1.5 +* Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +* software sha1 implementation
1.19 +* software sha1 implementation
1.20 +*
1.21 +*/
1.22 +
1.23 +
1.24 +/**
1.25 + @file
1.26 +*/
1.27 +
1.28 +#include "sha1impl.h"
1.29 +
1.30 +#include <cryptospi/hashplugin.h>
1.31 +#include "pluginconfig.h"
1.32 +#define EXPANDLOOP
1.33 +
1.34 +
1.35 +using namespace SoftwareCrypto;
1.36 +
1.37 +CSHA1Impl* CSHA1Impl::NewL()
1.38 + {
1.39 + CSHA1Impl* self=new (ELeave) CSHA1Impl();
1.40 + self->Reset();
1.41 + return self;
1.42 + }
1.43 +
1.44 +CSHA1Impl* CSHA1Impl::NewLC()
1.45 + {
1.46 + CSHA1Impl* self=NewL();
1.47 + CleanupStack::PushL(self);
1.48 + return self;
1.49 + }
1.50 +
1.51 +CSHA1Impl::CSHA1Impl() : iHash(KSHA1HashSize)
1.52 + {
1.53 + }
1.54 +
1.55 +CSHA1Impl::CSHA1Impl(const CSHA1Impl& aSHA1Impl)
1.56 +: iHash(aSHA1Impl.iHash),iA(aSHA1Impl.iA),iB(aSHA1Impl.iB),iC(aSHA1Impl.iC),iD(aSHA1Impl.iD),iE(aSHA1Impl.iE),
1.57 + iNl(aSHA1Impl.iNl),iNh(aSHA1Impl.iNh)
1.58 + {
1.59 + (void)Mem::Copy(iData, aSHA1Impl.iData, KSHA1BlockSize*5);
1.60 + }
1.61 +
1.62 +CSHA1Impl::~CSHA1Impl()
1.63 + {
1.64 + }
1.65 +
1.66 +void CSHA1Impl::Reset()
1.67 + {
1.68 + iA=0x67452301;
1.69 + iB=0xefcdab89;
1.70 + iC=0x98badcfe;
1.71 + iD=0x10325476;
1.72 + iE=0xc3d2e1f0;
1.73 + iNh=0;
1.74 + iNl=0;
1.75 + }
1.76 +
1.77 +void CSHA1Impl::Close()
1.78 + {
1.79 + delete this;
1.80 + }
1.81 +
1.82 +MHash* CSHA1Impl::ReplicateL()
1.83 + {
1.84 + return CSHA1Impl::NewL();
1.85 + }
1.86 +
1.87 +MHash* CSHA1Impl::CopyL()
1.88 + {
1.89 + return new(ELeave) CSHA1Impl(*this);
1.90 + }
1.91 +
1.92 +TUid CSHA1Impl::ImplementationUid()
1.93 + {
1.94 + return KCryptoPluginSha1Uid;
1.95 + }
1.96 +
1.97 +void CSHA1Impl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
1.98 + {
1.99 + aPluginCharacteristics=NULL;
1.100 + TInt hashNum=sizeof(KHashCharacteristics)/sizeof(THashCharacteristics*);
1.101 + for (TInt i=0;i<hashNum;i++)
1.102 + {
1.103 + if (KHashCharacteristics[i]->cmn.iImplementationUID == ImplementationUid().iUid)
1.104 + {
1.105 + aPluginCharacteristics = KHashCharacteristics[i];
1.106 + break;
1.107 + }
1.108 + }
1.109 + }
1.110 +
1.111 +CExtendedCharacteristics* CSHA1Impl::CreateExtendedCharacteristicsL()
1.112 + {
1.113 + // All Symbian software plug-ins have unlimited concurrency, cannot be reserved
1.114 + // for exclusive use and are not CERTIFIED to be standards compliant.
1.115 + return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
1.116 + }
1.117 +
1.118 +const CExtendedCharacteristics* CSHA1Impl::GetExtendedCharacteristicsL()
1.119 + {
1.120 + return CSHA1Impl::CreateExtendedCharacteristicsL();
1.121 + }
1.122 +
1.123 +TPtrC8 CSHA1Impl::Hash(const TDesC8& aMessage)
1.124 + {
1.125 + TPtrC8 ptr(KNullDesC8());
1.126 + DoUpdate(aMessage.Ptr(),aMessage.Size());
1.127 + StoreState();
1.128 + DoFinal();
1.129 + ptr.Set(iHash);
1.130 + RestoreState();
1.131 + return ptr;
1.132 + }
1.133 +
1.134 +void CSHA1Impl::Update(const TDesC8& aMessage)
1.135 + {
1.136 + DoUpdate(aMessage.Ptr(),aMessage.Size());
1.137 + }
1.138 +
1.139 +TPtrC8 CSHA1Impl::Final(const TDesC8& aMessage)
1.140 + {
1.141 + TPtrC8 ptr(KNullDesC8());
1.142 + if (aMessage!=KNullDesC8())
1.143 + {
1.144 + DoUpdate(aMessage.Ptr(),aMessage.Size());
1.145 + }
1.146 + DoFinal();
1.147 + ptr.Set(iHash);
1.148 + Reset();
1.149 + return ptr;
1.150 + }
1.151 +
1.152 +
1.153 +// This assumes a big-endian architecture
1.154 +void CSHA1Impl::DoUpdate(const TUint8* aData,TUint aLength)
1.155 + {
1.156 + while((aLength / 4) > 0 && (iNl % 4 == 0))
1.157 + {
1.158 + iData[iNl>>2] = aData[0] << 24 | aData[1] << 16 | aData[2] << 8 | aData[3];
1.159 + iNl+=4;
1.160 + aData+=4;
1.161 + aLength-=4;
1.162 + if(iNl==64)
1.163 + {
1.164 + Block();
1.165 + iNh+=64;
1.166 + iNl=0;
1.167 + }
1.168 + }
1.169 +
1.170 + while(aLength--)
1.171 + {
1.172 + switch (iNl&3)
1.173 + {
1.174 + case 0:
1.175 + iData[iNl>>2]=((TUint)(*aData))<<24;
1.176 + break;
1.177 + case 1:
1.178 + iData[iNl>>2]|=((TUint)(*aData))<<16;
1.179 + break;
1.180 + case 2:
1.181 + iData[iNl>>2]|=((TUint)(*aData))<<8;
1.182 + break;
1.183 + case 3:
1.184 + iData[iNl>>2]|=((TUint)(*aData));
1.185 + break;
1.186 + default:
1.187 + break;
1.188 + };
1.189 + aData++;
1.190 + iNl++;
1.191 + if(iNl==64)
1.192 + {
1.193 + Block();
1.194 + iNh+=64;
1.195 + iNl=0;
1.196 + }
1.197 + }
1.198 + }
1.199 +
1.200 +static inline TUint CSHA1_F(const TUint x,const TUint y,const TUint z)
1.201 + {
1.202 + return (x&y) | (~x&z);
1.203 + }
1.204 +
1.205 +static inline TUint CSHA1_G(const TUint x,const TUint y,const TUint z)
1.206 + {
1.207 + return x^y^z;
1.208 + }
1.209 +
1.210 +static inline TUint CSHA1_H(const TUint x,const TUint y,const TUint z)
1.211 + {
1.212 + return (x&y) | (x&z) | (y&z);
1.213 + }
1.214 +
1.215 +/*static inline TUint CSHA1_I(const TUint x,const TUint y,const TUint z)
1.216 + {
1.217 + return x^y^z;
1.218 + }*/
1.219 +
1.220 +#ifdef EXPANDLOOP
1.221 +
1.222 +#ifdef MACRO
1.223 +
1.224 +#define CSHA1_16(x,y,z,u,t,v,w) v=CMD_R(x,5)+CSHA1_F(y,z,u)+t+w+0x5a827999;\
1.225 + y=CMD_R(y,30);t=v;
1.226 +#define CSHA1_20(x,y,z,u,t,v,w0,w3,w8,w14,w16) v=w3^w8^w14^w16;w0=CMD_R(v,1);\
1.227 + CSHA1_16(x,y,z,u,t,v,w0);
1.228 +#define CSHA1_40(x,y,z,u,t,v,w0,w3,w8,w14,w16) v=w3^w8^w14^w16;w0=CMD_R(v,1);\
1.229 + v=CMD_R(x,5)+CSHA1_G(y,z,u)+t+w0+0x6ed9eba1;\
1.230 + y=CMD_R(y,30);t=v;
1.231 +#define CSHA1_60(x,y,z,u,t,v,w0,w3,w8,w14,w16) v=w3^w8^w14^w16;w0=CMD_R(v,1);\
1.232 + v=CMD_R(x,5)+CSHA1_H(y,z,u)+t+w0+0x8f1bbcdc;\
1.233 + y=CMD_R(y,30);t=v;
1.234 +#define CSHA1_80(x,y,z,u,t,v,w0,w3,w8,w14,w16) v=w3^w8^w14^w16;w0=CMD_R(v,1);\
1.235 + v=CMD_R(x,5)+CSHA1_G(y,z,u)+t+w0+0xca62c1d6;\
1.236 + y=CMD_R(y,30);t=v;
1.237 +#else
1.238 +
1.239 +static inline void CSHA1_16(const TUint x, TUint& y, const TUint z,
1.240 + const TUint u, TUint& t, TUint& v, const TUint w)
1.241 + {
1.242 + v = CMD_R(x,5) + CSHA1_F(y,z,u) + t + w + 0x5a827999;
1.243 + y = CMD_R(y,30);
1.244 + t = v;
1.245 + }
1.246 +
1.247 +static inline void CSHA1_20(const TUint x,TUint& y,const TUint z,
1.248 + const TUint u,TUint& t,TUint& v,
1.249 + TUint& w0,const TUint w3,const TUint w8,
1.250 + const TUint w14,const TUint w16)
1.251 + {
1.252 + v = w3 ^ w8 ^ w14 ^ w16;
1.253 + w0 = CMD_R(v,1);
1.254 + CSHA1_16(x,y,z,u,t,v,w0);
1.255 + }
1.256 +
1.257 +static inline void CSHA1_40(const TUint x,TUint& y,const TUint z,
1.258 + const TUint u,TUint& t,TUint& v,
1.259 + TUint& w0,const TUint w3,const TUint w8,
1.260 + const TUint w14,const TUint w16)
1.261 + {
1.262 + v = w3 ^ w8 ^ w14 ^ w16;
1.263 + w0 = CMD_R(v,1);
1.264 + v = CMD_R(x,5) + CSHA1_G(y,z,u) + t + w0 + 0x6ed9eba1;
1.265 + y = CMD_R(y,30);
1.266 + t = v;
1.267 + }
1.268 +
1.269 +static inline void CSHA1_60(const TUint x,TUint& y,const TUint z,
1.270 + const TUint u,TUint& t,TUint& v,
1.271 + TUint& w0,const TUint w3,const TUint w8,
1.272 + const TUint w14,const TUint w16)
1.273 + {
1.274 + v = w3 ^ w8 ^ w14 ^ w16;
1.275 + w0 = CMD_R(v,1);
1.276 + v = CMD_R(x,5) + CSHA1_H(y,z,u) + t + w0 + 0x8f1bbcdc;
1.277 + y = CMD_R(y,30);
1.278 + t = v;
1.279 + }
1.280 +
1.281 +static inline void CSHA1_80(const TUint x,TUint& y,const TUint z,
1.282 + const TUint u,TUint& t,TUint& v,
1.283 + TUint& w0,const TUint w3,const TUint w8,
1.284 + const TUint w14,const TUint w16)
1.285 + {
1.286 + v = w3 ^ w8 ^ w14 ^ w16;
1.287 + w0 = CMD_R(v,1);
1.288 + v = CMD_R(x,5) + CSHA1_G(y,z,u) + t + w0 + 0xca62c1d6;
1.289 + y = CMD_R(y,30);
1.290 + t = v;
1.291 + }
1.292 +
1.293 +#endif // MACRO
1.294 +#endif // EXPANDLOOP
1.295 +
1.296 +#ifdef WEIDAI
1.297 +
1.298 +template <class T> inline T rotlFixed(T x, unsigned int y)
1.299 +{
1.300 + ASSERT(y < sizeof(T)*8);
1.301 + return (x<<y) | (x>>(sizeof(T)*8-y));
1.302 +}
1.303 +
1.304 +template<> inline TUint32 rotlFixed<TUint32>(TUint32 x, unsigned int y)
1.305 +{
1.306 + ASSERT(y < 32);
1.307 + return y ? CMD_R(x, y) : x;
1.308 +}
1.309 +
1.310 +#define blk0(i) (W[i] = iData[i])
1.311 +#define blk1(i) (W[i&15] = rotlFixed(W[(i+13)&15]^W[(i+8)&15]^W[(i+2)&15]^W[i&15],1))
1.312 +
1.313 +#define f1(x,y,z) (z^(x&(y^z)))
1.314 +#define f2(x,y,z) (x^y^z)
1.315 +#define f3(x,y,z) ((x&y)|(z&(x|y)))
1.316 +#define f4(x,y,z) (x^y^z)
1.317 +
1.318 +/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
1.319 +#define R0(v,w,x,y,z,i) z+=f1(w,x,y)+blk0(i)+0x5A827999+rotlFixed(v,5);w=rotlFixed(w,30);
1.320 +#define R1(v,w,x,y,z,i) z+=f1(w,x,y)+blk1(i)+0x5A827999+rotlFixed(v,5);w=rotlFixed(w,30);
1.321 +#define R2(v,w,x,y,z,i) z+=f2(w,x,y)+blk1(i)+0x6ED9EBA1+rotlFixed(v,5);w=rotlFixed(w,30);
1.322 +#define R3(v,w,x,y,z,i) z+=f3(w,x,y)+blk1(i)+0x8F1BBCDC+rotlFixed(v,5);w=rotlFixed(w,30);
1.323 +#define R4(v,w,x,y,z,i) z+=f4(w,x,y)+blk1(i)+0xCA62C1D6+rotlFixed(v,5);w=rotlFixed(w,30);
1.324 +
1.325 +#endif // WEIDAI
1.326 +
1.327 +void CSHA1Impl::Block()
1.328 + {
1.329 +#ifdef WEIDAI
1.330 + TUint32 W[16];
1.331 + /* Copy context->state[] to working vars */
1.332 + TUint32 a = iA;
1.333 + TUint32 b = iB;
1.334 + TUint32 c = iC;
1.335 + TUint32 d = iD;
1.336 + TUint32 e = iE;
1.337 +
1.338 + /* 4 rounds of 20 operations each. Loop unrolled. */
1.339 +
1.340 + R0(a,b,c,d,e, 0);
1.341 + R0(e,a,b,c,d, 1);
1.342 + R0(d,e,a,b,c, 2);
1.343 + R0(c,d,e,a,b, 3);
1.344 + R0(b,c,d,e,a, 4);
1.345 + R0(a,b,c,d,e, 5);
1.346 + R0(e,a,b,c,d, 6);
1.347 + R0(d,e,a,b,c, 7);
1.348 + R0(c,d,e,a,b, 8);
1.349 + R0(b,c,d,e,a, 9);
1.350 + R0(a,b,c,d,e,10);
1.351 + R0(e,a,b,c,d,11);
1.352 + R0(d,e,a,b,c,12);
1.353 + R0(c,d,e,a,b,13);
1.354 + R0(b,c,d,e,a,14);
1.355 + R0(a,b,c,d,e,15);
1.356 +
1.357 + R1(e,a,b,c,d,16);
1.358 + R1(d,e,a,b,c,17);
1.359 + R1(c,d,e,a,b,18);
1.360 + R1(b,c,d,e,a,19);
1.361 +
1.362 + R2(a,b,c,d,e,20);
1.363 + R2(e,a,b,c,d,21);
1.364 + R2(d,e,a,b,c,22);
1.365 + R2(c,d,e,a,b,23);
1.366 + R2(b,c,d,e,a,24);
1.367 + R2(a,b,c,d,e,25);
1.368 + R2(e,a,b,c,d,26);
1.369 + R2(d,e,a,b,c,27);
1.370 + R2(c,d,e,a,b,28);
1.371 + R2(b,c,d,e,a,29);
1.372 + R2(a,b,c,d,e,30);
1.373 + R2(e,a,b,c,d,31);
1.374 + R2(d,e,a,b,c,32);
1.375 + R2(c,d,e,a,b,33);
1.376 + R2(b,c,d,e,a,34);
1.377 + R2(a,b,c,d,e,35);
1.378 + R2(e,a,b,c,d,36);
1.379 + R2(d,e,a,b,c,37);
1.380 + R2(c,d,e,a,b,38);
1.381 + R2(b,c,d,e,a,39);
1.382 +
1.383 + R3(a,b,c,d,e,40);
1.384 + R3(e,a,b,c,d,41);
1.385 + R3(d,e,a,b,c,42);
1.386 + R3(c,d,e,a,b,43);
1.387 + R3(b,c,d,e,a,44);
1.388 + R3(a,b,c,d,e,45);
1.389 + R3(e,a,b,c,d,46);
1.390 + R3(d,e,a,b,c,47);
1.391 + R3(c,d,e,a,b,48);
1.392 + R3(b,c,d,e,a,49);
1.393 + R3(a,b,c,d,e,50);
1.394 + R3(e,a,b,c,d,51);
1.395 + R3(d,e,a,b,c,52);
1.396 + R3(c,d,e,a,b,53);
1.397 + R3(b,c,d,e,a,54);
1.398 + R3(a,b,c,d,e,55);
1.399 + R3(e,a,b,c,d,56);
1.400 + R3(d,e,a,b,c,57);
1.401 + R3(c,d,e,a,b,58);
1.402 + R3(b,c,d,e,a,59);
1.403 +
1.404 + R4(a,b,c,d,e,60);
1.405 + R4(e,a,b,c,d,61);
1.406 + R4(d,e,a,b,c,62);
1.407 + R4(c,d,e,a,b,63);
1.408 + R4(b,c,d,e,a,64);
1.409 + R4(a,b,c,d,e,65);
1.410 + R4(e,a,b,c,d,66);
1.411 + R4(d,e,a,b,c,67);
1.412 + R4(c,d,e,a,b,68);
1.413 + R4(b,c,d,e,a,69);
1.414 + R4(a,b,c,d,e,70);
1.415 + R4(e,a,b,c,d,71);
1.416 + R4(d,e,a,b,c,72);
1.417 + R4(c,d,e,a,b,73);
1.418 + R4(b,c,d,e,a,74);
1.419 + R4(a,b,c,d,e,75);
1.420 + R4(e,a,b,c,d,76);
1.421 + R4(d,e,a,b,c,77);
1.422 + R4(c,d,e,a,b,78);
1.423 + R4(b,c,d,e,a,79);
1.424 +
1.425 + /* Add the working vars back into context.state[] */
1.426 + iA += a;
1.427 + iB += b;
1.428 + iC += c;
1.429 + iD += d;
1.430 + iE += e;
1.431 + /* Wipe variables */
1.432 + a = b = c = d = e = 0;
1.433 + Mem::FillZ(W, sizeof(W));
1.434 +#else
1.435 + TUint tempA=iA;
1.436 + TUint tempB=iB;
1.437 + TUint tempC=iC;
1.438 + TUint tempD=iD;
1.439 + TUint tempE=iE;
1.440 + TUint temp=0;
1.441 +
1.442 +#ifdef EXPANDLOOP
1.443 + CSHA1_16(tempA,tempB,tempC,tempD,tempE,temp,iData[0]);
1.444 + CSHA1_16(temp,tempA,tempB,tempC,tempD,tempE,iData[1]);
1.445 + CSHA1_16(tempE,temp,tempA,tempB,tempC,tempD,iData[2]);
1.446 + CSHA1_16(tempD,tempE,temp,tempA,tempB,tempC,iData[3]);
1.447 + CSHA1_16(tempC,tempD,tempE,temp,tempA,tempB,iData[4]);
1.448 + CSHA1_16(tempB,tempC,tempD,tempE,temp,tempA,iData[5]);
1.449 + CSHA1_16(tempA,tempB,tempC,tempD,tempE,temp,iData[6]);
1.450 + CSHA1_16(temp,tempA,tempB,tempC,tempD,tempE,iData[7]);
1.451 + CSHA1_16(tempE,temp,tempA,tempB,tempC,tempD,iData[8]);
1.452 + CSHA1_16(tempD,tempE,temp,tempA,tempB,tempC,iData[9]);
1.453 + CSHA1_16(tempC,tempD,tempE,temp,tempA,tempB,iData[10]);
1.454 + CSHA1_16(tempB,tempC,tempD,tempE,temp,tempA,iData[11]);
1.455 + CSHA1_16(tempA,tempB,tempC,tempD,tempE,temp,iData[12]);
1.456 + CSHA1_16(temp,tempA,tempB,tempC,tempD,tempE,iData[13]);
1.457 + CSHA1_16(tempE,temp,tempA,tempB,tempC,tempD,iData[14]);
1.458 + CSHA1_16(tempD,tempE,temp,tempA,tempB,tempC,iData[15]);
1.459 + /*
1.460 + i = 16;
1.461 + TUint temp1 = tempA;
1.462 + tempA =
1.463 + */
1.464 +#else
1.465 + TUint i=0;
1.466 + while (i<16)
1.467 + {
1.468 + temp = CMD_R(tempA,5) + CSHA1_F(tempB,tempC,tempD) + tempE + iData[i++] + 0x5a827999;
1.469 + tempE = tempD;
1.470 + tempD = tempC;
1.471 + tempC = CMD_R(tempB,30);
1.472 + tempB = tempA;
1.473 + tempA = temp;
1.474 + }
1.475 +#endif
1.476 +
1.477 +#ifdef EXPANDLOOP
1.478 + CSHA1_20(tempC,tempD,tempE,temp,tempA,tempB,iData[16],iData[13],iData[8],iData[2],iData[0]);
1.479 + CSHA1_20(tempB,tempC,tempD,tempE,temp,tempA,iData[17],iData[14],iData[9],iData[3],iData[1]);
1.480 + CSHA1_20(tempA,tempB,tempC,tempD,tempE,temp,iData[18],iData[15],iData[10],iData[4],iData[2]);
1.481 + CSHA1_20(temp,tempA,tempB,tempC,tempD,tempE,iData[19],iData[16],iData[11],iData[5],iData[3]);
1.482 + //i = 20;
1.483 +#else
1.484 + while (i<20)
1.485 + {
1.486 + temp=iData[i-3] ^ iData[i-8] ^ iData[i-14] ^ iData[i-16];
1.487 + iData[i]=CMD_R(temp,1);
1.488 + temp = CMD_R(tempA,5) + CSHA1_F(tempB,tempC,tempD) + tempE + iData[i++] + 0x5a827999;
1.489 + tempE = tempD;
1.490 + tempD = tempC;
1.491 + tempC = CMD_R(tempB,30);
1.492 + tempB = tempA;
1.493 + tempA = temp;
1.494 + }
1.495 +#endif
1.496 +
1.497 +#ifdef EXPANDLOOP
1.498 + CSHA1_40(tempE,temp,tempA,tempB,tempC,tempD,iData[20],iData[17],iData[12],iData[6],iData[4]);
1.499 + CSHA1_40(tempD,tempE,temp,tempA,tempB,tempC,iData[21],iData[18],iData[13],iData[7],iData[5]);
1.500 + CSHA1_40(tempC,tempD,tempE,temp,tempA,tempB,iData[22],iData[19],iData[14],iData[8],iData[6]);
1.501 + CSHA1_40(tempB,tempC,tempD,tempE,temp,tempA,iData[23],iData[20],iData[15],iData[9],iData[7]);
1.502 + CSHA1_40(tempA,tempB,tempC,tempD,tempE,temp,iData[24],iData[21],iData[16],iData[10],iData[8]);
1.503 + CSHA1_40(temp,tempA,tempB,tempC,tempD,tempE,iData[25],iData[22],iData[17],iData[11],iData[9]);
1.504 + CSHA1_40(tempE,temp,tempA,tempB,tempC,tempD,iData[26],iData[23],iData[18],iData[12],iData[10]);
1.505 + CSHA1_40(tempD,tempE,temp,tempA,tempB,tempC,iData[27],iData[24],iData[19],iData[13],iData[11]);
1.506 + CSHA1_40(tempC,tempD,tempE,temp,tempA,tempB,iData[28],iData[25],iData[20],iData[14],iData[12]);
1.507 + CSHA1_40(tempB,tempC,tempD,tempE,temp,tempA,iData[29],iData[26],iData[21],iData[15],iData[13]);
1.508 + CSHA1_40(tempA,tempB,tempC,tempD,tempE,temp,iData[30],iData[27],iData[22],iData[16],iData[14]);
1.509 + CSHA1_40(temp,tempA,tempB,tempC,tempD,tempE,iData[31],iData[28],iData[23],iData[17],iData[15]);
1.510 + CSHA1_40(tempE,temp,tempA,tempB,tempC,tempD,iData[32],iData[29],iData[24],iData[18],iData[16]);
1.511 + CSHA1_40(tempD,tempE,temp,tempA,tempB,tempC,iData[33],iData[30],iData[25],iData[19],iData[17]);
1.512 + CSHA1_40(tempC,tempD,tempE,temp,tempA,tempB,iData[34],iData[31],iData[26],iData[20],iData[18]);
1.513 + CSHA1_40(tempB,tempC,tempD,tempE,temp,tempA,iData[35],iData[32],iData[27],iData[21],iData[19]);
1.514 + CSHA1_40(tempA,tempB,tempC,tempD,tempE,temp,iData[36],iData[33],iData[28],iData[22],iData[20]);
1.515 + CSHA1_40(temp,tempA,tempB,tempC,tempD,tempE,iData[37],iData[34],iData[29],iData[23],iData[21]);
1.516 + CSHA1_40(tempE,temp,tempA,tempB,tempC,tempD,iData[38],iData[35],iData[30],iData[24],iData[22]);
1.517 + CSHA1_40(tempD,tempE,temp,tempA,tempB,tempC,iData[39],iData[36],iData[31],iData[25],iData[23]);
1.518 + //i = 40;
1.519 +#else
1.520 + while (i<40)
1.521 + {
1.522 + temp = iData[i-3] ^ iData[i-8] ^ iData[i-14] ^ iData[i-16];
1.523 + iData[i] = CMD_R(temp,1);
1.524 +
1.525 + temp = CMD_R(tempA,5) + CSHA1_G(tempB,tempC,tempD) + tempE + iData[i++] + 0x6ed9eba1;
1.526 + tempE = tempD;
1.527 + tempD = tempC;
1.528 + tempC = CMD_R(tempB,30);
1.529 + tempB = tempA;
1.530 + tempA = temp;
1.531 + }
1.532 +#endif
1.533 +
1.534 +#ifdef EXPANDLOOP
1.535 + CSHA1_60(tempC,tempD,tempE,temp,tempA,tempB,iData[40],iData[37],iData[32],iData[26],iData[24]);
1.536 + CSHA1_60(tempB,tempC,tempD,tempE,temp,tempA,iData[41],iData[38],iData[33],iData[27],iData[25]);
1.537 + CSHA1_60(tempA,tempB,tempC,tempD,tempE,temp,iData[42],iData[39],iData[34],iData[28],iData[26]);
1.538 + CSHA1_60(temp,tempA,tempB,tempC,tempD,tempE,iData[43],iData[40],iData[35],iData[29],iData[27]);
1.539 + CSHA1_60(tempE,temp,tempA,tempB,tempC,tempD,iData[44],iData[41],iData[36],iData[30],iData[28]);
1.540 + CSHA1_60(tempD,tempE,temp,tempA,tempB,tempC,iData[45],iData[42],iData[37],iData[31],iData[29]);
1.541 + CSHA1_60(tempC,tempD,tempE,temp,tempA,tempB,iData[46],iData[43],iData[38],iData[32],iData[30]);
1.542 + CSHA1_60(tempB,tempC,tempD,tempE,temp,tempA,iData[47],iData[44],iData[39],iData[33],iData[31]);
1.543 + CSHA1_60(tempA,tempB,tempC,tempD,tempE,temp,iData[48],iData[45],iData[40],iData[34],iData[32]);
1.544 + CSHA1_60(temp,tempA,tempB,tempC,tempD,tempE,iData[49],iData[46],iData[41],iData[35],iData[33]);
1.545 + CSHA1_60(tempE,temp,tempA,tempB,tempC,tempD,iData[50],iData[47],iData[42],iData[36],iData[34]);
1.546 + CSHA1_60(tempD,tempE,temp,tempA,tempB,tempC,iData[51],iData[48],iData[43],iData[37],iData[35]);
1.547 + CSHA1_60(tempC,tempD,tempE,temp,tempA,tempB,iData[52],iData[49],iData[44],iData[38],iData[36]);
1.548 + CSHA1_60(tempB,tempC,tempD,tempE,temp,tempA,iData[53],iData[50],iData[45],iData[39],iData[37]);
1.549 + CSHA1_60(tempA,tempB,tempC,tempD,tempE,temp,iData[54],iData[51],iData[46],iData[40],iData[38]);
1.550 + CSHA1_60(temp,tempA,tempB,tempC,tempD,tempE,iData[55],iData[52],iData[47],iData[41],iData[39]);
1.551 + CSHA1_60(tempE,temp,tempA,tempB,tempC,tempD,iData[56],iData[53],iData[48],iData[42],iData[40]);
1.552 + CSHA1_60(tempD,tempE,temp,tempA,tempB,tempC,iData[57],iData[54],iData[49],iData[43],iData[41]);
1.553 + CSHA1_60(tempC,tempD,tempE,temp,tempA,tempB,iData[58],iData[55],iData[50],iData[44],iData[42]);
1.554 + CSHA1_60(tempB,tempC,tempD,tempE,temp,tempA,iData[59],iData[56],iData[51],iData[45],iData[43]);
1.555 + //i = 60;
1.556 +#else
1.557 + while (i<60)
1.558 + {
1.559 + temp = iData[i-3] ^ iData[i-8] ^ iData[i-14] ^ iData[i-16];
1.560 + iData[i] = CMD_R(temp,1);
1.561 +
1.562 + temp = CMD_R(tempA,5) + CSHA1_H(tempB,tempC,tempD) + tempE + iData[i++] + 0x8f1bbcdc;
1.563 + tempE = tempD;
1.564 + tempD = tempC;
1.565 + tempC = CMD_R(tempB,30);
1.566 + tempB = tempA;
1.567 + tempA = temp;
1.568 + }
1.569 +#endif
1.570 +
1.571 +#ifdef EXPANDLOOP
1.572 + CSHA1_80(tempA,tempB,tempC,tempD,tempE,temp,iData[60],iData[57],iData[52],iData[46],iData[44]);
1.573 + CSHA1_80(temp,tempA,tempB,tempC,tempD,tempE,iData[61],iData[58],iData[53],iData[47],iData[45]);
1.574 + CSHA1_80(tempE,temp,tempA,tempB,tempC,tempD,iData[62],iData[59],iData[54],iData[48],iData[46]);
1.575 + CSHA1_80(tempD,tempE,temp,tempA,tempB,tempC,iData[63],iData[60],iData[55],iData[49],iData[47]);
1.576 + CSHA1_80(tempC,tempD,tempE,temp,tempA,tempB,iData[64],iData[61],iData[56],iData[50],iData[48]);
1.577 + CSHA1_80(tempB,tempC,tempD,tempE,temp,tempA,iData[65],iData[62],iData[57],iData[51],iData[49]);
1.578 + CSHA1_80(tempA,tempB,tempC,tempD,tempE,temp,iData[66],iData[63],iData[58],iData[52],iData[50]);
1.579 + CSHA1_80(temp,tempA,tempB,tempC,tempD,tempE,iData[67],iData[64],iData[59],iData[53],iData[51]);
1.580 + CSHA1_80(tempE,temp,tempA,tempB,tempC,tempD,iData[68],iData[65],iData[60],iData[54],iData[52]);
1.581 + CSHA1_80(tempD,tempE,temp,tempA,tempB,tempC,iData[69],iData[66],iData[61],iData[55],iData[53]);
1.582 + CSHA1_80(tempC,tempD,tempE,temp,tempA,tempB,iData[70],iData[67],iData[62],iData[56],iData[54]);
1.583 + CSHA1_80(tempB,tempC,tempD,tempE,temp,tempA,iData[71],iData[68],iData[63],iData[57],iData[55]);
1.584 + CSHA1_80(tempA,tempB,tempC,tempD,tempE,temp,iData[72],iData[69],iData[64],iData[58],iData[56]);
1.585 + CSHA1_80(temp,tempA,tempB,tempC,tempD,tempE,iData[73],iData[70],iData[65],iData[59],iData[57]);
1.586 + CSHA1_80(tempE,temp,tempA,tempB,tempC,tempD,iData[74],iData[71],iData[66],iData[60],iData[58]);
1.587 + CSHA1_80(tempD,tempE,temp,tempA,tempB,tempC,iData[75],iData[72],iData[67],iData[61],iData[59]);
1.588 + CSHA1_80(tempC,tempD,tempE,temp,tempA,tempB,iData[76],iData[73],iData[68],iData[62],iData[60]);
1.589 + CSHA1_80(tempB,tempC,tempD,tempE,temp,tempA,iData[77],iData[74],iData[69],iData[63],iData[61]);
1.590 + CSHA1_80(tempA,tempB,tempC,tempD,tempE,temp,iData[78],iData[75],iData[70],iData[64],iData[62]);
1.591 + CSHA1_80(temp,tempA,tempB,tempC,tempD,tempE,iData[79],iData[76],iData[71],iData[65],iData[63]);
1.592 +#else
1.593 + const TUint total=KSHA1BlockSize*5; // 16 * 5 = 80
1.594 + while (i<total)
1.595 + {
1.596 + temp = iData[i-3] ^ iData[i-8] ^ iData[i-14] ^ iData[i-16];
1.597 + iData[i] = CMD_R(temp,1);
1.598 +
1.599 + temp = CMD_R(tempA,5) + CSHA1_I(tempB,tempC,tempD) + tempE + iData[i++] + 0xca62c1d6;
1.600 + tempE = tempD;
1.601 + tempD = tempC;
1.602 + tempC = CMD_R(tempB,30);
1.603 + tempB = tempA;
1.604 + tempA = temp;
1.605 + }
1.606 +#endif
1.607 +
1.608 +#ifdef EXPANDLOOP
1.609 + iA+=tempE;
1.610 + iB+=temp;
1.611 + iC+=tempA;
1.612 + iD+=tempB;
1.613 + iE+=tempC;
1.614 +#else
1.615 + iA+=tempA;
1.616 + iB+=tempB;
1.617 + iC+=tempC;
1.618 + iD+=tempD;
1.619 + iE+=tempE;
1.620 +#endif // EXPANDLOOP
1.621 +#endif // WEIDAI
1.622 + }
1.623 +
1.624 +void CSHA1Impl::DoFinal()
1.625 + {
1.626 + iNh += iNl;
1.627 + const TUint ul128=128;
1.628 + switch (iNl&3)
1.629 + {
1.630 + case 0:
1.631 + iData[iNl>>2] = ul128<<24;
1.632 + break;
1.633 + case 1:
1.634 + iData[iNl>>2] += ul128<<16;
1.635 + break;
1.636 + case 2:
1.637 + iData[iNl>>2] += ul128<<8;
1.638 + break;
1.639 + case 3:
1.640 + iData[iNl>>2] += ul128;
1.641 + break;
1.642 + default:
1.643 + break;
1.644 + };
1.645 + if (iNl>=56)
1.646 + {
1.647 + if (iNl<60)
1.648 + iData[15]=0;
1.649 + Block();
1.650 + Mem::FillZ(iData,14*sizeof(TUint));
1.651 + }
1.652 + else
1.653 + {
1.654 + const TUint offset=(iNl+4)>>2; //+4 to account for the word added in the
1.655 + //switch statement above
1.656 + Mem::FillZ(iData+offset,(14-offset)*sizeof(TUint));
1.657 + }
1.658 +
1.659 + // this will fail if the total input length is longer than 2^32 in bits
1.660 + //(2^31 in bytes) which is roughly half a gig.
1.661 + iData[14]=0;
1.662 + iData[15]=iNh<<3;//number in bits
1.663 + Block();
1.664 + //
1.665 + // Generate hash value into iHash
1.666 + //
1.667 + TUint tmp=iA;
1.668 + iHash[3]=(TUint8)(tmp & 255);
1.669 + iHash[2]=(TUint8)((tmp >>= 8) & 255);
1.670 + iHash[1]=(TUint8)((tmp >>= 8) & 255);
1.671 + iHash[0]=(TUint8)((tmp >>= 8) & 255);
1.672 +
1.673 + tmp=iB;
1.674 + iHash[7]=(TUint8)(tmp & 255);
1.675 + iHash[6]=(TUint8)((tmp >>= 8) & 255);
1.676 + iHash[5]=(TUint8)((tmp >>= 8) & 255);
1.677 + iHash[4]=(TUint8)((tmp >>= 8) & 255);
1.678 +
1.679 + tmp=iC;
1.680 + iHash[11]=(TUint8)(tmp & 255);
1.681 + iHash[10]=(TUint8)((tmp >>= 8) & 255);
1.682 + iHash[9]=(TUint8)((tmp >>= 8) & 255);
1.683 + iHash[8]=(TUint8)((tmp >>= 8) & 255);
1.684 +
1.685 + tmp=iD;
1.686 + iHash[15]=(TUint8)(tmp & 255);
1.687 + iHash[14]=(TUint8)((tmp >>= 8) & 255);
1.688 + iHash[13]=(TUint8)((tmp >>= 8) & 255);
1.689 + iHash[12]=(TUint8)((tmp >>= 8) & 255);
1.690 +
1.691 + tmp=iE;
1.692 + iHash[19]=(TUint8)(tmp & 255);
1.693 + iHash[18]=(TUint8)((tmp >>= 8) & 255);
1.694 + iHash[17]=(TUint8)((tmp >>= 8) & 255);
1.695 + iHash[16]=(TUint8)((tmp >>= 8) & 255);
1.696 + }
1.697 +
1.698 +void CSHA1Impl::RestoreState()
1.699 + {
1.700 + iA = iACopy;
1.701 + iB = iBCopy;
1.702 + iC = iCCopy;
1.703 + iD = iDCopy;
1.704 + iE = iECopy;
1.705 + iNl = iNlCopy;
1.706 + iNh = iNhCopy;
1.707 + Mem::Copy(&iData[0], &iDataCopy[0], KSHA1BlockSize*5*sizeof(TUint));
1.708 + }
1.709 +
1.710 +void CSHA1Impl::StoreState()
1.711 + {
1.712 + iACopy = iA;
1.713 + iBCopy = iB;
1.714 + iCCopy = iC;
1.715 + iDCopy = iD;
1.716 + iECopy = iE;
1.717 + iNlCopy = iNl;
1.718 + iNhCopy = iNh;
1.719 + Mem::Copy(&iDataCopy[0], &iData[0], KSHA1BlockSize*5*sizeof(TUint));
1.720 + }
1.721 +
1.722 +// Implemented in hmacimpl.cpp or softwarehashbase.cpp
1.723 +// but required as derived from MHash. No coverage here.
1.724 +#ifdef _BullseyeCoverage
1.725 +#pragma suppress_warnings on
1.726 +#pragma BullseyeCoverage off
1.727 +#pragma suppress_warnings off
1.728 +#endif
1.729 +
1.730 +TAny* CSHA1Impl::GetExtension(TUid /*aExtensionId*/)
1.731 + {
1.732 + return NULL;
1.733 + }
1.734 +
1.735 +void CSHA1Impl::SetOperationModeL(TUid /*aOperationMode*/)
1.736 + {
1.737 + User::Leave(KErrNotSupported);
1.738 + }
1.739 +
1.740 +void CSHA1Impl::SetKeyL(const CKey& /*aKey*/)
1.741 + {
1.742 + User::Leave(KErrNotSupported);
1.743 + }
1.744 +