Update contrib.
1 // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // f32test\loader\security\t_sfhash.cpp
22 static inline TUint CMD_R(TUint a,TUint s)
24 return (a<<s) | (a>>(32-s));
32 CSHA1* CSHA1::NewL(void)
34 CSHA1* self=new(ELeave) CSHA1;
39 void CSHA1::Update(const TDesC8& aMessage)
41 DoUpdate(aMessage.Ptr(),aMessage.Size());
46 TPtrC8 ptr(KNullDesC8());
57 void CSHA1::Reset(void)
68 // This assumes a big-endian architecture
69 void CSHA1::DoUpdate(const TUint8* aData,TUint aLength)
71 while((aLength / 4) > 0 && (iNl % 4 == 0))
73 iData[iNl>>2] = aData[0] << 24 | aData[1] << 16 | aData[2] << 8 | aData[3];
90 iData[iNl>>2]=((TUint)(*aData))<<24;
93 iData[iNl>>2]|=((TUint)(*aData))<<16;
96 iData[iNl>>2]|=((TUint)(*aData))<<8;
99 iData[iNl>>2]|=((TUint)(*aData));
115 static inline TUint CSHA1_F(const TUint x,const TUint y,const TUint z)
117 return (x&y) | (~x&z);
120 static inline TUint CSHA1_G(const TUint x,const TUint y,const TUint z)
125 static inline TUint CSHA1_H(const TUint x,const TUint y,const TUint z)
127 return (x&y) | (x&z) | (y&z);
130 /*static inline TUint CSHA1_I(const TUint x,const TUint y,const TUint z)
139 #define CSHA1_16(x,y,z,u,t,v,w) v=CMD_R(x,5)+CSHA1_F(y,z,u)+t+w+0x5a827999;\
141 #define CSHA1_20(x,y,z,u,t,v,w0,w3,w8,w14,w16) v=w3^w8^w14^w16;w0=CMD_R(v,1);\
142 CSHA1_16(x,y,z,u,t,v,w0);
143 #define CSHA1_40(x,y,z,u,t,v,w0,w3,w8,w14,w16) v=w3^w8^w14^w16;w0=CMD_R(v,1);\
144 v=CMD_R(x,5)+CSHA1_G(y,z,u)+t+w0+0x6ed9eba1;\
146 #define CSHA1_60(x,y,z,u,t,v,w0,w3,w8,w14,w16) v=w3^w8^w14^w16;w0=CMD_R(v,1);\
147 v=CMD_R(x,5)+CSHA1_H(y,z,u)+t+w0+0x8f1bbcdc;\
149 #define CSHA1_80(x,y,z,u,t,v,w0,w3,w8,w14,w16) v=w3^w8^w14^w16;w0=CMD_R(v,1);\
150 v=CMD_R(x,5)+CSHA1_G(y,z,u)+t+w0+0xca62c1d6;\
154 static inline void CSHA1_16(const TUint x, TUint& y, const TUint z,
155 const TUint u, TUint& t, TUint& v, const TUint w)
157 v = CMD_R(x,5) + CSHA1_F(y,z,u) + t + w + 0x5a827999;
162 static inline void CSHA1_20(const TUint x,TUint& y,const TUint z,
163 const TUint u,TUint& t,TUint& v,
164 TUint& w0,const TUint w3,const TUint w8,
165 const TUint w14,const TUint w16)
167 v = w3 ^ w8 ^ w14 ^ w16;
169 CSHA1_16(x,y,z,u,t,v,w0);
172 static inline void CSHA1_40(const TUint x,TUint& y,const TUint z,
173 const TUint u,TUint& t,TUint& v,
174 TUint& w0,const TUint w3,const TUint w8,
175 const TUint w14,const TUint w16)
177 v = w3 ^ w8 ^ w14 ^ w16;
179 v = CMD_R(x,5) + CSHA1_G(y,z,u) + t + w0 + 0x6ed9eba1;
184 static inline void CSHA1_60(const TUint x,TUint& y,const TUint z,
185 const TUint u,TUint& t,TUint& v,
186 TUint& w0,const TUint w3,const TUint w8,
187 const TUint w14,const TUint w16)
189 v = w3 ^ w8 ^ w14 ^ w16;
191 v = CMD_R(x,5) + CSHA1_H(y,z,u) + t + w0 + 0x8f1bbcdc;
196 static inline void CSHA1_80(const TUint x,TUint& y,const TUint z,
197 const TUint u,TUint& t,TUint& v,
198 TUint& w0,const TUint w3,const TUint w8,
199 const TUint w14,const TUint w16)
201 v = w3 ^ w8 ^ w14 ^ w16;
203 v = CMD_R(x,5) + CSHA1_G(y,z,u) + t + w0 + 0xca62c1d6;
213 template <class T> inline T rotlFixed(T x, unsigned int y)
215 ASSERT(y < sizeof(T)*8);
216 return (x<<y) | (x>>(sizeof(T)*8-y));
219 template<> inline TUint32 rotlFixed<TUint32>(TUint32 x, unsigned int y)
222 return y ? CMD_R(x, y) : x;
225 #define blk0(i) (W[i] = iData[i])
226 #define blk1(i) (W[i&15] = rotlFixed(W[(i+13)&15]^W[(i+8)&15]^W[(i+2)&15]^W[i&15],1))
228 #define f1(x,y,z) (z^(x&(y^z)))
229 #define f2(x,y,z) (x^y^z)
230 #define f3(x,y,z) ((x&y)|(z&(x|y)))
231 #define f4(x,y,z) (x^y^z)
233 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
234 #define R0(v,w,x,y,z,i) z+=f1(w,x,y)+blk0(i)+0x5A827999+rotlFixed(v,5);w=rotlFixed(w,30);
235 #define R1(v,w,x,y,z,i) z+=f1(w,x,y)+blk1(i)+0x5A827999+rotlFixed(v,5);w=rotlFixed(w,30);
236 #define R2(v,w,x,y,z,i) z+=f2(w,x,y)+blk1(i)+0x6ED9EBA1+rotlFixed(v,5);w=rotlFixed(w,30);
237 #define R3(v,w,x,y,z,i) z+=f3(w,x,y)+blk1(i)+0x8F1BBCDC+rotlFixed(v,5);w=rotlFixed(w,30);
238 #define R4(v,w,x,y,z,i) z+=f4(w,x,y)+blk1(i)+0xCA62C1D6+rotlFixed(v,5);w=rotlFixed(w,30);
246 /* Copy context->state[] to working vars */
253 /* 4 rounds of 20 operations each. Loop unrolled. */
340 /* Add the working vars back into context.state[] */
347 a = b = c = d = e = 0;
348 Mem::FillZ(W, sizeof(W));
358 CSHA1_16(tempA,tempB,tempC,tempD,tempE,temp,iData[0]);
359 CSHA1_16(temp,tempA,tempB,tempC,tempD,tempE,iData[1]);
360 CSHA1_16(tempE,temp,tempA,tempB,tempC,tempD,iData[2]);
361 CSHA1_16(tempD,tempE,temp,tempA,tempB,tempC,iData[3]);
362 CSHA1_16(tempC,tempD,tempE,temp,tempA,tempB,iData[4]);
363 CSHA1_16(tempB,tempC,tempD,tempE,temp,tempA,iData[5]);
364 CSHA1_16(tempA,tempB,tempC,tempD,tempE,temp,iData[6]);
365 CSHA1_16(temp,tempA,tempB,tempC,tempD,tempE,iData[7]);
366 CSHA1_16(tempE,temp,tempA,tempB,tempC,tempD,iData[8]);
367 CSHA1_16(tempD,tempE,temp,tempA,tempB,tempC,iData[9]);
368 CSHA1_16(tempC,tempD,tempE,temp,tempA,tempB,iData[10]);
369 CSHA1_16(tempB,tempC,tempD,tempE,temp,tempA,iData[11]);
370 CSHA1_16(tempA,tempB,tempC,tempD,tempE,temp,iData[12]);
371 CSHA1_16(temp,tempA,tempB,tempC,tempD,tempE,iData[13]);
372 CSHA1_16(tempE,temp,tempA,tempB,tempC,tempD,iData[14]);
373 CSHA1_16(tempD,tempE,temp,tempA,tempB,tempC,iData[15]);
383 temp = CMD_R(tempA,5) + CSHA1_F(tempB,tempC,tempD) + tempE + iData[i++] + 0x5a827999;
386 tempC = CMD_R(tempB,30);
393 CSHA1_20(tempC,tempD,tempE,temp,tempA,tempB,iData[16],iData[13],iData[8],iData[2],iData[0]);
394 CSHA1_20(tempB,tempC,tempD,tempE,temp,tempA,iData[17],iData[14],iData[9],iData[3],iData[1]);
395 CSHA1_20(tempA,tempB,tempC,tempD,tempE,temp,iData[18],iData[15],iData[10],iData[4],iData[2]);
396 CSHA1_20(temp,tempA,tempB,tempC,tempD,tempE,iData[19],iData[16],iData[11],iData[5],iData[3]);
401 temp=iData[i-3] ^ iData[i-8] ^ iData[i-14] ^ iData[i-16];
402 iData[i]=CMD_R(temp,1);
403 temp = CMD_R(tempA,5) + CSHA1_F(tempB,tempC,tempD) + tempE + iData[i++] + 0x5a827999;
406 tempC = CMD_R(tempB,30);
413 CSHA1_40(tempE,temp,tempA,tempB,tempC,tempD,iData[20],iData[17],iData[12],iData[6],iData[4]);
414 CSHA1_40(tempD,tempE,temp,tempA,tempB,tempC,iData[21],iData[18],iData[13],iData[7],iData[5]);
415 CSHA1_40(tempC,tempD,tempE,temp,tempA,tempB,iData[22],iData[19],iData[14],iData[8],iData[6]);
416 CSHA1_40(tempB,tempC,tempD,tempE,temp,tempA,iData[23],iData[20],iData[15],iData[9],iData[7]);
417 CSHA1_40(tempA,tempB,tempC,tempD,tempE,temp,iData[24],iData[21],iData[16],iData[10],iData[8]);
418 CSHA1_40(temp,tempA,tempB,tempC,tempD,tempE,iData[25],iData[22],iData[17],iData[11],iData[9]);
419 CSHA1_40(tempE,temp,tempA,tempB,tempC,tempD,iData[26],iData[23],iData[18],iData[12],iData[10]);
420 CSHA1_40(tempD,tempE,temp,tempA,tempB,tempC,iData[27],iData[24],iData[19],iData[13],iData[11]);
421 CSHA1_40(tempC,tempD,tempE,temp,tempA,tempB,iData[28],iData[25],iData[20],iData[14],iData[12]);
422 CSHA1_40(tempB,tempC,tempD,tempE,temp,tempA,iData[29],iData[26],iData[21],iData[15],iData[13]);
423 CSHA1_40(tempA,tempB,tempC,tempD,tempE,temp,iData[30],iData[27],iData[22],iData[16],iData[14]);
424 CSHA1_40(temp,tempA,tempB,tempC,tempD,tempE,iData[31],iData[28],iData[23],iData[17],iData[15]);
425 CSHA1_40(tempE,temp,tempA,tempB,tempC,tempD,iData[32],iData[29],iData[24],iData[18],iData[16]);
426 CSHA1_40(tempD,tempE,temp,tempA,tempB,tempC,iData[33],iData[30],iData[25],iData[19],iData[17]);
427 CSHA1_40(tempC,tempD,tempE,temp,tempA,tempB,iData[34],iData[31],iData[26],iData[20],iData[18]);
428 CSHA1_40(tempB,tempC,tempD,tempE,temp,tempA,iData[35],iData[32],iData[27],iData[21],iData[19]);
429 CSHA1_40(tempA,tempB,tempC,tempD,tempE,temp,iData[36],iData[33],iData[28],iData[22],iData[20]);
430 CSHA1_40(temp,tempA,tempB,tempC,tempD,tempE,iData[37],iData[34],iData[29],iData[23],iData[21]);
431 CSHA1_40(tempE,temp,tempA,tempB,tempC,tempD,iData[38],iData[35],iData[30],iData[24],iData[22]);
432 CSHA1_40(tempD,tempE,temp,tempA,tempB,tempC,iData[39],iData[36],iData[31],iData[25],iData[23]);
437 temp = iData[i-3] ^ iData[i-8] ^ iData[i-14] ^ iData[i-16];
438 iData[i] = CMD_R(temp,1);
440 temp = CMD_R(tempA,5) + CSHA1_G(tempB,tempC,tempD) + tempE + iData[i++] + 0x6ed9eba1;
443 tempC = CMD_R(tempB,30);
450 CSHA1_60(tempC,tempD,tempE,temp,tempA,tempB,iData[40],iData[37],iData[32],iData[26],iData[24]);
451 CSHA1_60(tempB,tempC,tempD,tempE,temp,tempA,iData[41],iData[38],iData[33],iData[27],iData[25]);
452 CSHA1_60(tempA,tempB,tempC,tempD,tempE,temp,iData[42],iData[39],iData[34],iData[28],iData[26]);
453 CSHA1_60(temp,tempA,tempB,tempC,tempD,tempE,iData[43],iData[40],iData[35],iData[29],iData[27]);
454 CSHA1_60(tempE,temp,tempA,tempB,tempC,tempD,iData[44],iData[41],iData[36],iData[30],iData[28]);
455 CSHA1_60(tempD,tempE,temp,tempA,tempB,tempC,iData[45],iData[42],iData[37],iData[31],iData[29]);
456 CSHA1_60(tempC,tempD,tempE,temp,tempA,tempB,iData[46],iData[43],iData[38],iData[32],iData[30]);
457 CSHA1_60(tempB,tempC,tempD,tempE,temp,tempA,iData[47],iData[44],iData[39],iData[33],iData[31]);
458 CSHA1_60(tempA,tempB,tempC,tempD,tempE,temp,iData[48],iData[45],iData[40],iData[34],iData[32]);
459 CSHA1_60(temp,tempA,tempB,tempC,tempD,tempE,iData[49],iData[46],iData[41],iData[35],iData[33]);
460 CSHA1_60(tempE,temp,tempA,tempB,tempC,tempD,iData[50],iData[47],iData[42],iData[36],iData[34]);
461 CSHA1_60(tempD,tempE,temp,tempA,tempB,tempC,iData[51],iData[48],iData[43],iData[37],iData[35]);
462 CSHA1_60(tempC,tempD,tempE,temp,tempA,tempB,iData[52],iData[49],iData[44],iData[38],iData[36]);
463 CSHA1_60(tempB,tempC,tempD,tempE,temp,tempA,iData[53],iData[50],iData[45],iData[39],iData[37]);
464 CSHA1_60(tempA,tempB,tempC,tempD,tempE,temp,iData[54],iData[51],iData[46],iData[40],iData[38]);
465 CSHA1_60(temp,tempA,tempB,tempC,tempD,tempE,iData[55],iData[52],iData[47],iData[41],iData[39]);
466 CSHA1_60(tempE,temp,tempA,tempB,tempC,tempD,iData[56],iData[53],iData[48],iData[42],iData[40]);
467 CSHA1_60(tempD,tempE,temp,tempA,tempB,tempC,iData[57],iData[54],iData[49],iData[43],iData[41]);
468 CSHA1_60(tempC,tempD,tempE,temp,tempA,tempB,iData[58],iData[55],iData[50],iData[44],iData[42]);
469 CSHA1_60(tempB,tempC,tempD,tempE,temp,tempA,iData[59],iData[56],iData[51],iData[45],iData[43]);
474 temp = iData[i-3] ^ iData[i-8] ^ iData[i-14] ^ iData[i-16];
475 iData[i] = CMD_R(temp,1);
477 temp = CMD_R(tempA,5) + CSHA1_H(tempB,tempC,tempD) + tempE + iData[i++] + 0x8f1bbcdc;
480 tempC = CMD_R(tempB,30);
487 CSHA1_80(tempA,tempB,tempC,tempD,tempE,temp,iData[60],iData[57],iData[52],iData[46],iData[44]);
488 CSHA1_80(temp,tempA,tempB,tempC,tempD,tempE,iData[61],iData[58],iData[53],iData[47],iData[45]);
489 CSHA1_80(tempE,temp,tempA,tempB,tempC,tempD,iData[62],iData[59],iData[54],iData[48],iData[46]);
490 CSHA1_80(tempD,tempE,temp,tempA,tempB,tempC,iData[63],iData[60],iData[55],iData[49],iData[47]);
491 CSHA1_80(tempC,tempD,tempE,temp,tempA,tempB,iData[64],iData[61],iData[56],iData[50],iData[48]);
492 CSHA1_80(tempB,tempC,tempD,tempE,temp,tempA,iData[65],iData[62],iData[57],iData[51],iData[49]);
493 CSHA1_80(tempA,tempB,tempC,tempD,tempE,temp,iData[66],iData[63],iData[58],iData[52],iData[50]);
494 CSHA1_80(temp,tempA,tempB,tempC,tempD,tempE,iData[67],iData[64],iData[59],iData[53],iData[51]);
495 CSHA1_80(tempE,temp,tempA,tempB,tempC,tempD,iData[68],iData[65],iData[60],iData[54],iData[52]);
496 CSHA1_80(tempD,tempE,temp,tempA,tempB,tempC,iData[69],iData[66],iData[61],iData[55],iData[53]);
497 CSHA1_80(tempC,tempD,tempE,temp,tempA,tempB,iData[70],iData[67],iData[62],iData[56],iData[54]);
498 CSHA1_80(tempB,tempC,tempD,tempE,temp,tempA,iData[71],iData[68],iData[63],iData[57],iData[55]);
499 CSHA1_80(tempA,tempB,tempC,tempD,tempE,temp,iData[72],iData[69],iData[64],iData[58],iData[56]);
500 CSHA1_80(temp,tempA,tempB,tempC,tempD,tempE,iData[73],iData[70],iData[65],iData[59],iData[57]);
501 CSHA1_80(tempE,temp,tempA,tempB,tempC,tempD,iData[74],iData[71],iData[66],iData[60],iData[58]);
502 CSHA1_80(tempD,tempE,temp,tempA,tempB,tempC,iData[75],iData[72],iData[67],iData[61],iData[59]);
503 CSHA1_80(tempC,tempD,tempE,temp,tempA,tempB,iData[76],iData[73],iData[68],iData[62],iData[60]);
504 CSHA1_80(tempB,tempC,tempD,tempE,temp,tempA,iData[77],iData[74],iData[69],iData[63],iData[61]);
505 CSHA1_80(tempA,tempB,tempC,tempD,tempE,temp,iData[78],iData[75],iData[70],iData[64],iData[62]);
506 CSHA1_80(temp,tempA,tempB,tempC,tempD,tempE,iData[79],iData[76],iData[71],iData[65],iData[63]);
508 const TUint total=SHA1_LBLOCK*5; // 16 * 5 = 80
511 temp = iData[i-3] ^ iData[i-8] ^ iData[i-14] ^ iData[i-16];
512 iData[i] = CMD_R(temp,1);
514 temp = CMD_R(tempA,5) + CSHA1_I(tempB,tempC,tempD) + tempE + iData[i++] + 0xca62c1d6;
517 tempC = CMD_R(tempB,30);
539 void CSHA1::DoFinal()
542 const TUint ul128=128;
546 iData[iNl>>2] = ul128<<24;
549 iData[iNl>>2] += ul128<<16;
552 iData[iNl>>2] += ul128<<8;
555 iData[iNl>>2] += ul128;
565 Mem::FillZ(iData,14*sizeof(TUint));
569 const TUint offset=(iNl+4)>>2; //+4 to account for the word added in the
570 //switch statement above
571 Mem::FillZ(iData+offset,(14-offset)*sizeof(TUint));
574 //TODO: this will fail if the total input length is longer than 2^32 in bits
575 //(2^31 in bytes) which is roughly half a gig.
577 iData[15]=iNh<<3;//number in bits
580 // Generate hash value into iHash
583 iHash[3]=(TUint8)(tmp & 255);
584 iHash[2]=(TUint8)((tmp >>= 8) & 255);
585 iHash[1]=(TUint8)((tmp >>= 8) & 255);
586 iHash[0]=(TUint8)((tmp >>= 8) & 255);
589 iHash[7]=(TUint8)(tmp & 255);
590 iHash[6]=(TUint8)((tmp >>= 8) & 255);
591 iHash[5]=(TUint8)((tmp >>= 8) & 255);
592 iHash[4]=(TUint8)((tmp >>= 8) & 255);
595 iHash[11]=(TUint8)(tmp & 255);
596 iHash[10]=(TUint8)((tmp >>= 8) & 255);
597 iHash[9]=(TUint8)((tmp >>= 8) & 255);
598 iHash[8]=(TUint8)((tmp >>= 8) & 255);
601 iHash[15]=(TUint8)(tmp & 255);
602 iHash[14]=(TUint8)((tmp >>= 8) & 255);
603 iHash[13]=(TUint8)((tmp >>= 8) & 255);
604 iHash[12]=(TUint8)((tmp >>= 8) & 255);
607 iHash[19]=(TUint8)(tmp & 255);
608 iHash[18]=(TUint8)((tmp >>= 8) & 255);
609 iHash[17]=(TUint8)((tmp >>= 8) & 255);
610 iHash[16]=(TUint8)((tmp >>= 8) & 255);