|
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". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // f32test\loader\security\t_sfhash.cpp |
|
15 // |
|
16 // |
|
17 |
|
18 #include "t_hash.h" |
|
19 #define EXPANDLOOP |
|
20 |
|
21 |
|
22 static inline TUint CMD_R(TUint a,TUint s) |
|
23 { |
|
24 return (a<<s) | (a>>(32-s)); |
|
25 } |
|
26 |
|
27 CSHA1::CSHA1(void) |
|
28 : iHash(SHA1_HASH) |
|
29 { |
|
30 } |
|
31 |
|
32 CSHA1* CSHA1::NewL(void) |
|
33 { |
|
34 CSHA1* self=new(ELeave) CSHA1; |
|
35 self->Reset(); |
|
36 return self; |
|
37 } |
|
38 |
|
39 void CSHA1::Update(const TDesC8& aMessage) |
|
40 { |
|
41 DoUpdate(aMessage.Ptr(),aMessage.Size()); |
|
42 } |
|
43 |
|
44 TPtrC8 CSHA1::Final() |
|
45 { |
|
46 TPtrC8 ptr(KNullDesC8()); |
|
47 DoFinal(); |
|
48 ptr.Set(iHash); |
|
49 Reset(); |
|
50 return ptr; |
|
51 } |
|
52 |
|
53 CSHA1::~CSHA1(void) |
|
54 { |
|
55 } |
|
56 |
|
57 void CSHA1::Reset(void) |
|
58 { |
|
59 iA=0x67452301; |
|
60 iB=0xefcdab89; |
|
61 iC=0x98badcfe; |
|
62 iD=0x10325476; |
|
63 iE=0xc3d2e1f0; |
|
64 iNh=0; |
|
65 iNl=0; |
|
66 } |
|
67 |
|
68 // This assumes a big-endian architecture |
|
69 void CSHA1::DoUpdate(const TUint8* aData,TUint aLength) |
|
70 { |
|
71 while((aLength / 4) > 0 && (iNl % 4 == 0)) |
|
72 { |
|
73 iData[iNl>>2] = aData[0] << 24 | aData[1] << 16 | aData[2] << 8 | aData[3]; |
|
74 iNl+=4; |
|
75 aData+=4; |
|
76 aLength-=4; |
|
77 if(iNl==64) |
|
78 { |
|
79 Block(); |
|
80 iNh+=64; |
|
81 iNl=0; |
|
82 } |
|
83 } |
|
84 |
|
85 while(aLength--) |
|
86 { |
|
87 switch (iNl&3) |
|
88 { |
|
89 case 0: |
|
90 iData[iNl>>2]=((TUint)(*aData))<<24; |
|
91 break; |
|
92 case 1: |
|
93 iData[iNl>>2]|=((TUint)(*aData))<<16; |
|
94 break; |
|
95 case 2: |
|
96 iData[iNl>>2]|=((TUint)(*aData))<<8; |
|
97 break; |
|
98 case 3: |
|
99 iData[iNl>>2]|=((TUint)(*aData)); |
|
100 break; |
|
101 default: |
|
102 break; |
|
103 }; |
|
104 aData++; |
|
105 iNl++; |
|
106 if(iNl==64) |
|
107 { |
|
108 Block(); |
|
109 iNh+=64; |
|
110 iNl=0; |
|
111 } |
|
112 } |
|
113 } |
|
114 |
|
115 static inline TUint CSHA1_F(const TUint x,const TUint y,const TUint z) |
|
116 { |
|
117 return (x&y) | (~x&z); |
|
118 } |
|
119 |
|
120 static inline TUint CSHA1_G(const TUint x,const TUint y,const TUint z) |
|
121 { |
|
122 return x^y^z; |
|
123 } |
|
124 |
|
125 static inline TUint CSHA1_H(const TUint x,const TUint y,const TUint z) |
|
126 { |
|
127 return (x&y) | (x&z) | (y&z); |
|
128 } |
|
129 |
|
130 /*static inline TUint CSHA1_I(const TUint x,const TUint y,const TUint z) |
|
131 { |
|
132 return x^y^z; |
|
133 }*/ |
|
134 |
|
135 #ifdef EXPANDLOOP |
|
136 |
|
137 #ifdef MACRO |
|
138 |
|
139 #define CSHA1_16(x,y,z,u,t,v,w) v=CMD_R(x,5)+CSHA1_F(y,z,u)+t+w+0x5a827999;\ |
|
140 y=CMD_R(y,30);t=v; |
|
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;\ |
|
145 y=CMD_R(y,30);t=v; |
|
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;\ |
|
148 y=CMD_R(y,30);t=v; |
|
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;\ |
|
151 y=CMD_R(y,30);t=v; |
|
152 #else |
|
153 |
|
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) |
|
156 { |
|
157 v = CMD_R(x,5) + CSHA1_F(y,z,u) + t + w + 0x5a827999; |
|
158 y = CMD_R(y,30); |
|
159 t = v; |
|
160 } |
|
161 |
|
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) |
|
166 { |
|
167 v = w3 ^ w8 ^ w14 ^ w16; |
|
168 w0 = CMD_R(v,1); |
|
169 CSHA1_16(x,y,z,u,t,v,w0); |
|
170 } |
|
171 |
|
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) |
|
176 { |
|
177 v = w3 ^ w8 ^ w14 ^ w16; |
|
178 w0 = CMD_R(v,1); |
|
179 v = CMD_R(x,5) + CSHA1_G(y,z,u) + t + w0 + 0x6ed9eba1; |
|
180 y = CMD_R(y,30); |
|
181 t = v; |
|
182 } |
|
183 |
|
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) |
|
188 { |
|
189 v = w3 ^ w8 ^ w14 ^ w16; |
|
190 w0 = CMD_R(v,1); |
|
191 v = CMD_R(x,5) + CSHA1_H(y,z,u) + t + w0 + 0x8f1bbcdc; |
|
192 y = CMD_R(y,30); |
|
193 t = v; |
|
194 } |
|
195 |
|
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) |
|
200 { |
|
201 v = w3 ^ w8 ^ w14 ^ w16; |
|
202 w0 = CMD_R(v,1); |
|
203 v = CMD_R(x,5) + CSHA1_G(y,z,u) + t + w0 + 0xca62c1d6; |
|
204 y = CMD_R(y,30); |
|
205 t = v; |
|
206 } |
|
207 |
|
208 #endif // MACRO |
|
209 #endif // EXPANDLOOP |
|
210 |
|
211 #ifdef WEIDAI |
|
212 |
|
213 template <class T> inline T rotlFixed(T x, unsigned int y) |
|
214 { |
|
215 ASSERT(y < sizeof(T)*8); |
|
216 return (x<<y) | (x>>(sizeof(T)*8-y)); |
|
217 } |
|
218 |
|
219 template<> inline TUint32 rotlFixed<TUint32>(TUint32 x, unsigned int y) |
|
220 { |
|
221 ASSERT(y < 32); |
|
222 return y ? CMD_R(x, y) : x; |
|
223 } |
|
224 |
|
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)) |
|
227 |
|
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) |
|
232 |
|
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); |
|
239 |
|
240 #endif // WEIDAI |
|
241 |
|
242 void CSHA1::Block() |
|
243 { |
|
244 #ifdef WEIDAI |
|
245 TUint32 W[16]; |
|
246 /* Copy context->state[] to working vars */ |
|
247 TUint32 a = iA; |
|
248 TUint32 b = iB; |
|
249 TUint32 c = iC; |
|
250 TUint32 d = iD; |
|
251 TUint32 e = iE; |
|
252 |
|
253 /* 4 rounds of 20 operations each. Loop unrolled. */ |
|
254 |
|
255 R0(a,b,c,d,e, 0); |
|
256 R0(e,a,b,c,d, 1); |
|
257 R0(d,e,a,b,c, 2); |
|
258 R0(c,d,e,a,b, 3); |
|
259 R0(b,c,d,e,a, 4); |
|
260 R0(a,b,c,d,e, 5); |
|
261 R0(e,a,b,c,d, 6); |
|
262 R0(d,e,a,b,c, 7); |
|
263 R0(c,d,e,a,b, 8); |
|
264 R0(b,c,d,e,a, 9); |
|
265 R0(a,b,c,d,e,10); |
|
266 R0(e,a,b,c,d,11); |
|
267 R0(d,e,a,b,c,12); |
|
268 R0(c,d,e,a,b,13); |
|
269 R0(b,c,d,e,a,14); |
|
270 R0(a,b,c,d,e,15); |
|
271 |
|
272 R1(e,a,b,c,d,16); |
|
273 R1(d,e,a,b,c,17); |
|
274 R1(c,d,e,a,b,18); |
|
275 R1(b,c,d,e,a,19); |
|
276 |
|
277 R2(a,b,c,d,e,20); |
|
278 R2(e,a,b,c,d,21); |
|
279 R2(d,e,a,b,c,22); |
|
280 R2(c,d,e,a,b,23); |
|
281 R2(b,c,d,e,a,24); |
|
282 R2(a,b,c,d,e,25); |
|
283 R2(e,a,b,c,d,26); |
|
284 R2(d,e,a,b,c,27); |
|
285 R2(c,d,e,a,b,28); |
|
286 R2(b,c,d,e,a,29); |
|
287 R2(a,b,c,d,e,30); |
|
288 R2(e,a,b,c,d,31); |
|
289 R2(d,e,a,b,c,32); |
|
290 R2(c,d,e,a,b,33); |
|
291 R2(b,c,d,e,a,34); |
|
292 R2(a,b,c,d,e,35); |
|
293 R2(e,a,b,c,d,36); |
|
294 R2(d,e,a,b,c,37); |
|
295 R2(c,d,e,a,b,38); |
|
296 R2(b,c,d,e,a,39); |
|
297 |
|
298 R3(a,b,c,d,e,40); |
|
299 R3(e,a,b,c,d,41); |
|
300 R3(d,e,a,b,c,42); |
|
301 R3(c,d,e,a,b,43); |
|
302 R3(b,c,d,e,a,44); |
|
303 R3(a,b,c,d,e,45); |
|
304 R3(e,a,b,c,d,46); |
|
305 R3(d,e,a,b,c,47); |
|
306 R3(c,d,e,a,b,48); |
|
307 R3(b,c,d,e,a,49); |
|
308 R3(a,b,c,d,e,50); |
|
309 R3(e,a,b,c,d,51); |
|
310 R3(d,e,a,b,c,52); |
|
311 R3(c,d,e,a,b,53); |
|
312 R3(b,c,d,e,a,54); |
|
313 R3(a,b,c,d,e,55); |
|
314 R3(e,a,b,c,d,56); |
|
315 R3(d,e,a,b,c,57); |
|
316 R3(c,d,e,a,b,58); |
|
317 R3(b,c,d,e,a,59); |
|
318 |
|
319 R4(a,b,c,d,e,60); |
|
320 R4(e,a,b,c,d,61); |
|
321 R4(d,e,a,b,c,62); |
|
322 R4(c,d,e,a,b,63); |
|
323 R4(b,c,d,e,a,64); |
|
324 R4(a,b,c,d,e,65); |
|
325 R4(e,a,b,c,d,66); |
|
326 R4(d,e,a,b,c,67); |
|
327 R4(c,d,e,a,b,68); |
|
328 R4(b,c,d,e,a,69); |
|
329 R4(a,b,c,d,e,70); |
|
330 R4(e,a,b,c,d,71); |
|
331 R4(d,e,a,b,c,72); |
|
332 R4(c,d,e,a,b,73); |
|
333 R4(b,c,d,e,a,74); |
|
334 R4(a,b,c,d,e,75); |
|
335 R4(e,a,b,c,d,76); |
|
336 R4(d,e,a,b,c,77); |
|
337 R4(c,d,e,a,b,78); |
|
338 R4(b,c,d,e,a,79); |
|
339 |
|
340 /* Add the working vars back into context.state[] */ |
|
341 iA += a; |
|
342 iB += b; |
|
343 iC += c; |
|
344 iD += d; |
|
345 iE += e; |
|
346 /* Wipe variables */ |
|
347 a = b = c = d = e = 0; |
|
348 Mem::FillZ(W, sizeof(W)); |
|
349 #else |
|
350 TUint tempA=iA; |
|
351 TUint tempB=iB; |
|
352 TUint tempC=iC; |
|
353 TUint tempD=iD; |
|
354 TUint tempE=iE; |
|
355 TUint temp=0; |
|
356 |
|
357 #ifdef EXPANDLOOP |
|
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]); |
|
374 /* |
|
375 i = 16; |
|
376 TUint temp1 = tempA; |
|
377 tempA = |
|
378 */ |
|
379 #else |
|
380 TUint i=0; |
|
381 while (i<16) |
|
382 { |
|
383 temp = CMD_R(tempA,5) + CSHA1_F(tempB,tempC,tempD) + tempE + iData[i++] + 0x5a827999; |
|
384 tempE = tempD; |
|
385 tempD = tempC; |
|
386 tempC = CMD_R(tempB,30); |
|
387 tempB = tempA; |
|
388 tempA = temp; |
|
389 } |
|
390 #endif |
|
391 |
|
392 #ifdef EXPANDLOOP |
|
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]); |
|
397 //i = 20; |
|
398 #else |
|
399 while (i<20) |
|
400 { |
|
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; |
|
404 tempE = tempD; |
|
405 tempD = tempC; |
|
406 tempC = CMD_R(tempB,30); |
|
407 tempB = tempA; |
|
408 tempA = temp; |
|
409 } |
|
410 #endif |
|
411 |
|
412 #ifdef EXPANDLOOP |
|
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]); |
|
433 //i = 40; |
|
434 #else |
|
435 while (i<40) |
|
436 { |
|
437 temp = iData[i-3] ^ iData[i-8] ^ iData[i-14] ^ iData[i-16]; |
|
438 iData[i] = CMD_R(temp,1); |
|
439 |
|
440 temp = CMD_R(tempA,5) + CSHA1_G(tempB,tempC,tempD) + tempE + iData[i++] + 0x6ed9eba1; |
|
441 tempE = tempD; |
|
442 tempD = tempC; |
|
443 tempC = CMD_R(tempB,30); |
|
444 tempB = tempA; |
|
445 tempA = temp; |
|
446 } |
|
447 #endif |
|
448 |
|
449 #ifdef EXPANDLOOP |
|
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]); |
|
470 //i = 60; |
|
471 #else |
|
472 while (i<60) |
|
473 { |
|
474 temp = iData[i-3] ^ iData[i-8] ^ iData[i-14] ^ iData[i-16]; |
|
475 iData[i] = CMD_R(temp,1); |
|
476 |
|
477 temp = CMD_R(tempA,5) + CSHA1_H(tempB,tempC,tempD) + tempE + iData[i++] + 0x8f1bbcdc; |
|
478 tempE = tempD; |
|
479 tempD = tempC; |
|
480 tempC = CMD_R(tempB,30); |
|
481 tempB = tempA; |
|
482 tempA = temp; |
|
483 } |
|
484 #endif |
|
485 |
|
486 #ifdef EXPANDLOOP |
|
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]); |
|
507 #else |
|
508 const TUint total=SHA1_LBLOCK*5; // 16 * 5 = 80 |
|
509 while (i<total) |
|
510 { |
|
511 temp = iData[i-3] ^ iData[i-8] ^ iData[i-14] ^ iData[i-16]; |
|
512 iData[i] = CMD_R(temp,1); |
|
513 |
|
514 temp = CMD_R(tempA,5) + CSHA1_I(tempB,tempC,tempD) + tempE + iData[i++] + 0xca62c1d6; |
|
515 tempE = tempD; |
|
516 tempD = tempC; |
|
517 tempC = CMD_R(tempB,30); |
|
518 tempB = tempA; |
|
519 tempA = temp; |
|
520 } |
|
521 #endif |
|
522 |
|
523 #ifdef EXPANDLOOP |
|
524 iA+=tempE; |
|
525 iB+=temp; |
|
526 iC+=tempA; |
|
527 iD+=tempB; |
|
528 iE+=tempC; |
|
529 #else |
|
530 iA+=tempA; |
|
531 iB+=tempB; |
|
532 iC+=tempC; |
|
533 iD+=tempD; |
|
534 iE+=tempE; |
|
535 #endif // EXPANDLOOP |
|
536 #endif // WEIDAI |
|
537 } |
|
538 |
|
539 void CSHA1::DoFinal() |
|
540 { |
|
541 iNh += iNl; |
|
542 const TUint ul128=128; |
|
543 switch (iNl&3) |
|
544 { |
|
545 case 0: |
|
546 iData[iNl>>2] = ul128<<24; |
|
547 break; |
|
548 case 1: |
|
549 iData[iNl>>2] += ul128<<16; |
|
550 break; |
|
551 case 2: |
|
552 iData[iNl>>2] += ul128<<8; |
|
553 break; |
|
554 case 3: |
|
555 iData[iNl>>2] += ul128; |
|
556 break; |
|
557 default: |
|
558 break; |
|
559 }; |
|
560 if (iNl>=56) |
|
561 { |
|
562 if (iNl<60) |
|
563 iData[15]=0; |
|
564 Block(); |
|
565 Mem::FillZ(iData,14*sizeof(TUint)); |
|
566 } |
|
567 else |
|
568 { |
|
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)); |
|
572 } |
|
573 |
|
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. |
|
576 iData[14]=0; |
|
577 iData[15]=iNh<<3;//number in bits |
|
578 Block(); |
|
579 // |
|
580 // Generate hash value into iHash |
|
581 // |
|
582 TUint tmp=iA; |
|
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); |
|
587 |
|
588 tmp=iB; |
|
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); |
|
593 |
|
594 tmp=iC; |
|
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); |
|
599 |
|
600 tmp=iD; |
|
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); |
|
605 |
|
606 tmp=iE; |
|
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); |
|
611 } |