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