|
1 /* |
|
2 * Copyright (c) 2007-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 * Common implementation of SHA384 and SHA512 |
|
16 * RFC 4634 (US Secure Hash Algorithms (SHA and HMAC-SHA)) |
|
17 * |
|
18 */ |
|
19 |
|
20 |
|
21 /** |
|
22 @file |
|
23 */ |
|
24 |
|
25 #include "sha384and512.h" |
|
26 |
|
27 /** |
|
28 * SHA512 Constants |
|
29 * |
|
30 * SHA-512 uses a sequence of eighty constant 64-bit words. |
|
31 * These words represent the first sixty-four bits of the fractional |
|
32 * parts of the cube roots of the first eighty prime numbers. |
|
33 * |
|
34 * FIPS 180-2 Section 4.2.3 |
|
35 */ |
|
36 const TUint64 K[80] = |
|
37 { |
|
38 UI64LIT(0x428a2f98d728ae22), UI64LIT(0x7137449123ef65cd), UI64LIT(0xb5c0fbcfec4d3b2f), UI64LIT(0xe9b5dba58189dbbc), |
|
39 UI64LIT(0x3956c25bf348b538), UI64LIT(0x59f111f1b605d019), UI64LIT(0x923f82a4af194f9b), UI64LIT(0xab1c5ed5da6d8118), |
|
40 UI64LIT(0xd807aa98a3030242), UI64LIT(0x12835b0145706fbe), UI64LIT(0x243185be4ee4b28c), UI64LIT(0x550c7dc3d5ffb4e2), |
|
41 UI64LIT(0x72be5d74f27b896f), UI64LIT(0x80deb1fe3b1696b1), UI64LIT(0x9bdc06a725c71235), UI64LIT(0xc19bf174cf692694), |
|
42 UI64LIT(0xe49b69c19ef14ad2), UI64LIT(0xefbe4786384f25e3), UI64LIT(0x0fc19dc68b8cd5b5), UI64LIT(0x240ca1cc77ac9c65), |
|
43 UI64LIT(0x2de92c6f592b0275), UI64LIT(0x4a7484aa6ea6e483), UI64LIT(0x5cb0a9dcbd41fbd4), UI64LIT(0x76f988da831153b5), |
|
44 UI64LIT(0x983e5152ee66dfab), UI64LIT(0xa831c66d2db43210), UI64LIT(0xb00327c898fb213f), UI64LIT(0xbf597fc7beef0ee4), |
|
45 UI64LIT(0xc6e00bf33da88fc2), UI64LIT(0xd5a79147930aa725), UI64LIT(0x06ca6351e003826f), UI64LIT(0x142929670a0e6e70), |
|
46 UI64LIT(0x27b70a8546d22ffc), UI64LIT(0x2e1b21385c26c926), UI64LIT(0x4d2c6dfc5ac42aed), UI64LIT(0x53380d139d95b3df), |
|
47 UI64LIT(0x650a73548baf63de), UI64LIT(0x766a0abb3c77b2a8), UI64LIT(0x81c2c92e47edaee6), UI64LIT(0x92722c851482353b), |
|
48 UI64LIT(0xa2bfe8a14cf10364), UI64LIT(0xa81a664bbc423001), UI64LIT(0xc24b8b70d0f89791), UI64LIT(0xc76c51a30654be30), |
|
49 UI64LIT(0xd192e819d6ef5218), UI64LIT(0xd69906245565a910), UI64LIT(0xf40e35855771202a), UI64LIT(0x106aa07032bbd1b8), |
|
50 UI64LIT(0x19a4c116b8d2d0c8), UI64LIT(0x1e376c085141ab53), UI64LIT(0x2748774cdf8eeb99), UI64LIT(0x34b0bcb5e19b48a8), |
|
51 UI64LIT(0x391c0cb3c5c95a63), UI64LIT(0x4ed8aa4ae3418acb), UI64LIT(0x5b9cca4f7763e373), UI64LIT(0x682e6ff3d6b2b8a3), |
|
52 UI64LIT(0x748f82ee5defb2fc), UI64LIT(0x78a5636f43172f60), UI64LIT(0x84c87814a1f0ab72), UI64LIT(0x8cc702081a6439ec), |
|
53 UI64LIT(0x90befffa23631e28), UI64LIT(0xa4506cebde82bde9), UI64LIT(0xbef9a3f7b2c67915), UI64LIT(0xc67178f2e372532b), |
|
54 UI64LIT(0xca273eceea26619c), UI64LIT(0xd186b8c721c0c207), UI64LIT(0xeada7dd6cde0eb1e), UI64LIT(0xf57d4f7fee6ed178), |
|
55 UI64LIT(0x06f067aa72176fba), UI64LIT(0x0a637dc5a2c898a6), UI64LIT(0x113f9804bef90dae), UI64LIT(0x1b710b35131c471b), |
|
56 UI64LIT(0x28db77f523047d84), UI64LIT(0x32caab7b40c72493), UI64LIT(0x3c9ebe0a15c9bebc), UI64LIT(0x431d67c49c100d4c), |
|
57 UI64LIT(0x4cc5d4becb3e42b6), UI64LIT(0x597f299cfc657e2a), UI64LIT(0x5fcb6fab3ad6faec), UI64LIT(0x6c44198c4a475817) |
|
58 }; |
|
59 |
|
60 /** |
|
61 * Define the SHA SIGMA and sigma macros |
|
62 * |
|
63 * FIPS 180-2 section 4.1.3 |
|
64 */ |
|
65 // Equation 4.10 |
|
66 inline TUint64 SHA512_SIGMA0(TUint64 aWord) |
|
67 { |
|
68 return (SHA_ROTR<TUint64>(28,aWord) ^ SHA_ROTR<TUint64>(34,aWord) ^ SHA_ROTR<TUint64>(39,aWord)); |
|
69 } |
|
70 // Equation 4.11 |
|
71 inline TUint64 SHA512_SIGMA1(TUint64 aWord) |
|
72 { |
|
73 return (SHA_ROTR<TUint64>(14,aWord) ^ SHA_ROTR<TUint64>(18,aWord) ^ SHA_ROTR<TUint64>(41,aWord)); |
|
74 } |
|
75 // Equation 4.12 |
|
76 inline TUint64 SHA512_sigma0(TUint64 aWord) |
|
77 { |
|
78 return (SHA_ROTR<TUint64>(1, aWord) ^ SHA_ROTR<TUint64>(8, aWord) ^ SHA_SHR<TUint64>(7, aWord)); |
|
79 } |
|
80 // Equation 4.13 |
|
81 inline TUint64 SHA512_sigma1(TUint64 aWord) |
|
82 { |
|
83 return (SHA_ROTR<TUint64>(19,aWord) ^ SHA_ROTR<TUint64>(61,aWord) ^ SHA_SHR<TUint64>(6,aWord)); |
|
84 } |
|
85 |
|
86 // Macros |
|
87 inline TUint64 Make64BitWord(const TUint8* aData) |
|
88 { |
|
89 TUint64 result = (TUint64)aData[0] << 56 | (TUint64)aData[1] << 48 | (TUint64)aData[2] << 40 | |
|
90 (TUint64)aData[3] << 32 | (TUint64)aData[4] << 24 | (TUint64)aData[5] << 16 | |
|
91 (TUint64)aData[6] << 8 | (TUint64)aData[7]; |
|
92 return result; |
|
93 } |
|
94 |
|
95 |
|
96 CSHA384And512* CSHA384And512::NewL() |
|
97 { |
|
98 CSHA384And512* self=new (ELeave) CSHA384And512(); |
|
99 return self; |
|
100 } |
|
101 |
|
102 CSHA384And512::CSHA384And512() : iHash(KSHA512HashSize) |
|
103 { |
|
104 } |
|
105 |
|
106 CSHA384And512::CSHA384And512(const CSHA384And512& aSHAImpl) |
|
107 : iHash(aSHAImpl.iHash), |
|
108 iA(aSHAImpl.iA), |
|
109 iB(aSHAImpl.iB), |
|
110 iC(aSHAImpl.iC), |
|
111 iD(aSHAImpl.iD), |
|
112 iE(aSHAImpl.iE), |
|
113 iF(aSHAImpl.iF), |
|
114 iG(aSHAImpl.iG), |
|
115 iH(aSHAImpl.iH), |
|
116 iNl(aSHAImpl.iNl), |
|
117 iNh(aSHAImpl.iNh), |
|
118 iBlockIndex(aSHAImpl.iBlockIndex) |
|
119 { |
|
120 Mem::Copy(iData, aSHAImpl.iData, KSHA512BlockSize*sizeof(TUint64)); |
|
121 } |
|
122 |
|
123 void CSHA384And512::Reset(const TAny* aValueArr) |
|
124 { |
|
125 const TUint64* values = static_cast<const TUint64*>(aValueArr); |
|
126 |
|
127 iA = values[0]; |
|
128 iB = values[1]; |
|
129 iC = values[2]; |
|
130 iD = values[3]; |
|
131 iE = values[4]; |
|
132 iF = values[5]; |
|
133 iG = values[6]; |
|
134 iH = values[7]; |
|
135 iNh = 0; |
|
136 iNl = 0; |
|
137 iBlockIndex = 0; |
|
138 } |
|
139 |
|
140 //This function will panic if the total input length is longer than 2^128 in bits |
|
141 _LIT(KPanicString, "Message length exceeds supported length"); |
|
142 inline void CSHA384And512::AddLength(const TUint64 aLength) |
|
143 { |
|
144 TUint64 temp = iNl; |
|
145 iNl += aLength << 3; |
|
146 __ASSERT_ALWAYS(((iNh != KMaxTUint64) || (temp <= iNl)), User::Panic(KPanicString, KErrOverflow)); |
|
147 iNh += (temp > iNl); |
|
148 } |
|
149 |
|
150 // This assumes a big-endian architecture |
|
151 void CSHA384And512::Update(const TUint8* aData,TUint aLength) |
|
152 { |
|
153 while((aLength / 8) > 0 && (iBlockIndex % 8 == 0)) |
|
154 { |
|
155 iData[iBlockIndex>>3] = Make64BitWord(aData); |
|
156 iBlockIndex+=8; |
|
157 aData+=8; |
|
158 aLength-=8; |
|
159 if(iBlockIndex==KSHA512BlockSize) |
|
160 { |
|
161 Block(); |
|
162 AddLength(KSHA512BlockSize); |
|
163 } |
|
164 } |
|
165 |
|
166 while(aLength--) |
|
167 { |
|
168 if(!(iBlockIndex&0x07)) |
|
169 { |
|
170 iData[iBlockIndex >> 3] = 0; |
|
171 } |
|
172 iData[iBlockIndex >> 3] |= static_cast<TUint64>(*aData) << ((7 - iBlockIndex&0x07) << 3) ; |
|
173 ++aData; |
|
174 ++iBlockIndex; |
|
175 if(iBlockIndex==KSHA512BlockSize) |
|
176 { |
|
177 Block(); |
|
178 AddLength(KSHA512BlockSize); |
|
179 } |
|
180 } |
|
181 } |
|
182 |
|
183 static inline void CSHA512_16( const TUint64 aA, |
|
184 const TUint64 aB, |
|
185 const TUint64 aC, |
|
186 TUint64& aD, |
|
187 const TUint64 aE, |
|
188 const TUint64 aF, |
|
189 const TUint64 aG, |
|
190 TUint64& aH, |
|
191 TUint64 aTemp1, |
|
192 TUint64 aTemp2, |
|
193 const TUint64 aK, |
|
194 const TUint64 aWord) |
|
195 { |
|
196 aTemp1 = aH + SHA512_SIGMA1(aE) + SHA_Ch(aE,aF,aG) + aK + aWord; |
|
197 aTemp2 = SHA512_SIGMA0(aA) + SHA_Maj(aA,aB,aC); |
|
198 aD = aD + aTemp1; |
|
199 aH = aTemp1 + aTemp2; |
|
200 } |
|
201 |
|
202 static inline void CSHA512_64( const TUint64 aA, |
|
203 const TUint64 aB, |
|
204 const TUint64 aC, |
|
205 TUint64& aD, |
|
206 const TUint64 aE, |
|
207 const TUint64 aF, |
|
208 const TUint64 aG, |
|
209 TUint64& aH, |
|
210 TUint64 aTemp1, |
|
211 TUint64 aTemp2, |
|
212 const TUint64 aK, |
|
213 TUint64& aWord0, |
|
214 const TUint64 aWord2, |
|
215 const TUint64 aWord7, |
|
216 const TUint64 aWord15, |
|
217 const TUint64 aWord16) |
|
218 { |
|
219 aWord0 = SHA512_sigma1(aWord2) + aWord7 + SHA512_sigma0(aWord15) + aWord16; |
|
220 CSHA512_16(aA, aB, aC, aD, aE, aF, aG, aH, aTemp1, aTemp2, aK, aWord0); |
|
221 } |
|
222 |
|
223 /** |
|
224 * This function actually calculates the hash. |
|
225 * Function is defined in FIPS 180-2 section 6.3.2 |
|
226 * |
|
227 * This function is the expanded version of the following loop. |
|
228 * for(TUint i = 0; i < 80; ++i) |
|
229 * { |
|
230 * if(i >= 16) |
|
231 * { |
|
232 * iData[i] = SHA512_sigma1(iData[i-2]) + iData[i-7] + SHA512_sigma0(iData[i-15]) + iData[i-16]; |
|
233 * } |
|
234 * |
|
235 * temp1 = tempH + SHA512_SIGMA1(tempE) + SHA_Ch(tempE,tempF,tempG) + K[i] + iData[i]; |
|
236 * temp2 = SHA512_SIGMA0(tempA) + SHA_Maj(tempA,tempB,tempC); |
|
237 * tempH = tempG; |
|
238 * tempG = tempF; |
|
239 * tempF = tempE; |
|
240 * tempE = tempD + temp1; |
|
241 * tempD = tempC; |
|
242 * tempC = tempB; |
|
243 * tempB = tempA; |
|
244 * tempA = temp1 + temp2; |
|
245 * } |
|
246 */ |
|
247 void CSHA384And512::Block() |
|
248 { |
|
249 TUint64 tempA=iA; |
|
250 TUint64 tempB=iB; |
|
251 TUint64 tempC=iC; |
|
252 TUint64 tempD=iD; |
|
253 TUint64 tempE=iE; |
|
254 TUint64 tempF=iF; |
|
255 TUint64 tempG=iG; |
|
256 TUint64 tempH=iH; |
|
257 TUint64 temp1=0; |
|
258 TUint64 temp2=0; |
|
259 |
|
260 CSHA512_16(tempA,tempB,tempC,tempD,tempE,tempF,tempG,tempH,temp1,temp2,K[0],iData[0]); |
|
261 CSHA512_16(tempH,tempA,tempB,tempC,tempD,tempE,tempF,tempG,temp1,temp2,K[1],iData[1]); |
|
262 CSHA512_16(tempG,tempH,tempA,tempB,tempC,tempD,tempE,tempF,temp1,temp2,K[2],iData[2]); |
|
263 CSHA512_16(tempF,tempG,tempH,tempA,tempB,tempC,tempD,tempE,temp1,temp2,K[3],iData[3]); |
|
264 CSHA512_16(tempE,tempF,tempG,tempH,tempA,tempB,tempC,tempD,temp1,temp2,K[4],iData[4]); |
|
265 CSHA512_16(tempD,tempE,tempF,tempG,tempH,tempA,tempB,tempC,temp1,temp2,K[5],iData[5]); |
|
266 CSHA512_16(tempC,tempD,tempE,tempF,tempG,tempH,tempA,tempB,temp1,temp2,K[6],iData[6]); |
|
267 CSHA512_16(tempB,tempC,tempD,tempE,tempF,tempG,tempH,tempA,temp1,temp2,K[7],iData[7]); |
|
268 |
|
269 CSHA512_16(tempA,tempB,tempC,tempD,tempE,tempF,tempG,tempH,temp1,temp2,K[8],iData[8]); |
|
270 CSHA512_16(tempH,tempA,tempB,tempC,tempD,tempE,tempF,tempG,temp1,temp2,K[9],iData[9]); |
|
271 CSHA512_16(tempG,tempH,tempA,tempB,tempC,tempD,tempE,tempF,temp1,temp2,K[10],iData[10]); |
|
272 CSHA512_16(tempF,tempG,tempH,tempA,tempB,tempC,tempD,tempE,temp1,temp2,K[11],iData[11]); |
|
273 CSHA512_16(tempE,tempF,tempG,tempH,tempA,tempB,tempC,tempD,temp1,temp2,K[12],iData[12]); |
|
274 CSHA512_16(tempD,tempE,tempF,tempG,tempH,tempA,tempB,tempC,temp1,temp2,K[13],iData[13]); |
|
275 CSHA512_16(tempC,tempD,tempE,tempF,tempG,tempH,tempA,tempB,temp1,temp2,K[14],iData[14]); |
|
276 CSHA512_16(tempB,tempC,tempD,tempE,tempF,tempG,tempH,tempA,temp1,temp2,K[15],iData[15]); |
|
277 |
|
278 CSHA512_64( tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2, |
|
279 K[16], iData[16], iData[14], iData[9], iData[1], iData[0]); |
|
280 CSHA512_64( tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2, |
|
281 K[17], iData[17], iData[15], iData[10], iData[2], iData[1]); |
|
282 CSHA512_64( tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2, |
|
283 K[18], iData[18], iData[16], iData[11], iData[3], iData[2]); |
|
284 CSHA512_64( tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2, |
|
285 K[19], iData[19], iData[17], iData[12], iData[4], iData[3]); |
|
286 CSHA512_64( tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2, |
|
287 K[20], iData[20], iData[18], iData[13], iData[5], iData[4]); |
|
288 CSHA512_64( tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2, |
|
289 K[21], iData[21], iData[19], iData[14], iData[6], iData[5]); |
|
290 CSHA512_64( tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2, |
|
291 K[22], iData[22], iData[20], iData[15], iData[7], iData[6]); |
|
292 CSHA512_64( tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2, |
|
293 K[23], iData[23], iData[21], iData[16], iData[8], iData[7]); |
|
294 |
|
295 CSHA512_64( tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2, |
|
296 K[24], iData[24], iData[22], iData[17], iData[9], iData[8]); |
|
297 CSHA512_64( tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2, |
|
298 K[25], iData[25], iData[23], iData[18], iData[10], iData[9]); |
|
299 CSHA512_64( tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2, |
|
300 K[26], iData[26], iData[24], iData[19], iData[11], iData[10]); |
|
301 CSHA512_64( tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2, |
|
302 K[27], iData[27], iData[25], iData[20], iData[12], iData[11]); |
|
303 CSHA512_64( tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2, |
|
304 K[28], iData[28], iData[26], iData[21], iData[13], iData[12]); |
|
305 CSHA512_64( tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2, |
|
306 K[29], iData[29], iData[27], iData[22], iData[14], iData[13]); |
|
307 CSHA512_64( tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2, |
|
308 K[30], iData[30], iData[28], iData[23], iData[15], iData[14]); |
|
309 CSHA512_64( tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2, |
|
310 K[31], iData[31], iData[29], iData[24], iData[16], iData[15]); |
|
311 |
|
312 CSHA512_64( tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2, |
|
313 K[32], iData[32], iData[30], iData[25], iData[17], iData[16]); |
|
314 CSHA512_64( tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2, |
|
315 K[33], iData[33], iData[31], iData[26], iData[18], iData[17]); |
|
316 CSHA512_64( tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2, |
|
317 K[34], iData[34], iData[32], iData[27], iData[19], iData[18]); |
|
318 CSHA512_64( tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2, |
|
319 K[35], iData[35], iData[33], iData[28], iData[20], iData[19]); |
|
320 CSHA512_64( tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2, |
|
321 K[36], iData[36], iData[34], iData[29], iData[21], iData[20]); |
|
322 CSHA512_64( tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2, |
|
323 K[37], iData[37], iData[35], iData[30], iData[22], iData[21]); |
|
324 CSHA512_64( tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2, |
|
325 K[38], iData[38], iData[36], iData[31], iData[23], iData[22]); |
|
326 CSHA512_64( tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2, |
|
327 K[39], iData[39], iData[37], iData[32], iData[24], iData[23]); |
|
328 |
|
329 CSHA512_64( tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2, |
|
330 K[40], iData[40], iData[38], iData[33], iData[25], iData[24]); |
|
331 CSHA512_64( tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2, |
|
332 K[41], iData[41], iData[39], iData[34], iData[26], iData[25]); |
|
333 CSHA512_64( tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2, |
|
334 K[42], iData[42], iData[40], iData[35], iData[27], iData[26]); |
|
335 CSHA512_64( tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2, |
|
336 K[43], iData[43], iData[41], iData[36], iData[28], iData[27]); |
|
337 CSHA512_64( tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2, |
|
338 K[44], iData[44], iData[42], iData[37], iData[29], iData[28]); |
|
339 CSHA512_64( tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2, |
|
340 K[45], iData[45], iData[43], iData[38], iData[30], iData[29]); |
|
341 CSHA512_64( tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2, |
|
342 K[46], iData[46], iData[44], iData[39], iData[31], iData[30]); |
|
343 CSHA512_64( tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2, |
|
344 K[47], iData[47], iData[45], iData[40], iData[32], iData[31]); |
|
345 |
|
346 CSHA512_64( tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2, |
|
347 K[48], iData[48], iData[46], iData[41], iData[33], iData[32]); |
|
348 CSHA512_64( tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2, |
|
349 K[49], iData[49], iData[47], iData[42], iData[34], iData[33]); |
|
350 CSHA512_64( tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2, |
|
351 K[50], iData[50], iData[48], iData[43], iData[35], iData[34]); |
|
352 CSHA512_64( tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2, |
|
353 K[51], iData[51], iData[49], iData[44], iData[36], iData[35]); |
|
354 CSHA512_64( tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2, |
|
355 K[52], iData[52], iData[50], iData[45], iData[37], iData[36]); |
|
356 CSHA512_64( tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2, |
|
357 K[53], iData[53], iData[51], iData[46], iData[38], iData[37]); |
|
358 CSHA512_64( tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2, |
|
359 K[54], iData[54], iData[52], iData[47], iData[39], iData[38]); |
|
360 CSHA512_64( tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2, |
|
361 K[55], iData[55], iData[53], iData[48], iData[40], iData[39]); |
|
362 |
|
363 CSHA512_64( tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2, |
|
364 K[56], iData[56], iData[54], iData[49], iData[41], iData[40]); |
|
365 CSHA512_64( tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2, |
|
366 K[57], iData[57], iData[55], iData[50], iData[42], iData[41]); |
|
367 CSHA512_64( tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2, |
|
368 K[58], iData[58], iData[56], iData[51], iData[43], iData[42]); |
|
369 CSHA512_64( tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2, |
|
370 K[59], iData[59], iData[57], iData[52], iData[44], iData[43]); |
|
371 CSHA512_64( tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2, |
|
372 K[60], iData[60], iData[58], iData[53], iData[45], iData[44]); |
|
373 CSHA512_64( tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2, |
|
374 K[61], iData[61], iData[59], iData[54], iData[46], iData[45]); |
|
375 CSHA512_64( tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2, |
|
376 K[62], iData[62], iData[60], iData[55], iData[47], iData[46]); |
|
377 CSHA512_64( tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2, |
|
378 K[63], iData[63], iData[61], iData[56], iData[48], iData[47]); |
|
379 |
|
380 CSHA512_64( tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2, |
|
381 K[64], iData[64], iData[62], iData[57], iData[49], iData[48]); |
|
382 CSHA512_64( tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2, |
|
383 K[65], iData[65], iData[63], iData[58], iData[50], iData[49]); |
|
384 CSHA512_64( tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2, |
|
385 K[66], iData[66], iData[64], iData[59], iData[51], iData[50]); |
|
386 CSHA512_64( tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2, |
|
387 K[67], iData[67], iData[65], iData[60], iData[52], iData[51]); |
|
388 CSHA512_64( tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2, |
|
389 K[68], iData[68], iData[66], iData[61], iData[53], iData[52]); |
|
390 CSHA512_64( tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2, |
|
391 K[69], iData[69], iData[67], iData[62], iData[54], iData[53]); |
|
392 CSHA512_64( tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2, |
|
393 K[70], iData[70], iData[68], iData[63], iData[55], iData[54]); |
|
394 CSHA512_64( tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2, |
|
395 K[71], iData[71], iData[69], iData[64], iData[56], iData[55]); |
|
396 |
|
397 CSHA512_64( tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2, |
|
398 K[72], iData[72], iData[70], iData[65], iData[57], iData[56]); |
|
399 CSHA512_64( tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2, |
|
400 K[73], iData[73], iData[71], iData[66], iData[58], iData[57]); |
|
401 CSHA512_64( tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2, |
|
402 K[74], iData[74], iData[72], iData[67], iData[59], iData[58]); |
|
403 CSHA512_64( tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2, |
|
404 K[75], iData[75], iData[73], iData[68], iData[60], iData[59]); |
|
405 CSHA512_64( tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2, |
|
406 K[76], iData[76], iData[74], iData[69], iData[61], iData[60]); |
|
407 CSHA512_64( tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2, |
|
408 K[77], iData[77], iData[75], iData[70], iData[62], iData[61]); |
|
409 CSHA512_64( tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2, |
|
410 K[78], iData[78], iData[76], iData[71], iData[63], iData[62]); |
|
411 CSHA512_64( tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2, |
|
412 K[79], iData[79], iData[77], iData[72], iData[64], iData[63]); |
|
413 |
|
414 iA+=tempA; |
|
415 iB+=tempB; |
|
416 iC+=tempC; |
|
417 iD+=tempD; |
|
418 iE+=tempE; |
|
419 iF+=tempF; |
|
420 iG+=tempG; |
|
421 iH+=tempH; |
|
422 |
|
423 iBlockIndex=0; |
|
424 } |
|
425 |
|
426 /** |
|
427 * According to the standard, the message must be padded to an |
|
428 * even 512 bits. The first padding bit must be a '1'. The last |
|
429 * 64 bits represent the length of the original message. All bits |
|
430 * in between should be 0. This helper function will pad the |
|
431 * message according to those rules by filling the iData array |
|
432 * accordingly. |
|
433 */ |
|
434 void CSHA384And512::PadMessage() |
|
435 { |
|
436 const TUint64 padByte = 0x80; |
|
437 |
|
438 if(!(iBlockIndex&0x07)) |
|
439 { |
|
440 iData[iBlockIndex >> 3] = 0; |
|
441 } |
|
442 iData[iBlockIndex >> 3] |= padByte << ((7 - iBlockIndex&0x07) << 3) ; |
|
443 |
|
444 if (iBlockIndex >= (KSHA512BlockSize - 2*sizeof(TUint64))) |
|
445 { |
|
446 if (iBlockIndex < (KSHA512BlockSize - sizeof(TUint64))) |
|
447 iData[(KSHA512BlockSize>>3)-1]=0; |
|
448 Block(); |
|
449 Mem::FillZ(iData,KSHA512BlockSize); |
|
450 } |
|
451 else |
|
452 { |
|
453 const TUint offset=(iBlockIndex+8)>>3; //+8 to account for the word added in the |
|
454 //switch statement above |
|
455 Mem::FillZ(iData+offset, (KSHA512BlockSize - offset*sizeof(TUint64))); |
|
456 } |
|
457 |
|
458 iData[(KSHA512BlockSize >> 3) - 2] = iNh; |
|
459 iData[(KSHA512BlockSize >> 3) - 1] = iNl; |
|
460 } |
|
461 |
|
462 inline void CSHA384And512::CopyWordToHash(TUint64 aVal, TUint aIndex) |
|
463 { |
|
464 TUint64 value = Make64BitWord(reinterpret_cast<TUint8*>(&aVal)); |
|
465 Mem::Copy(const_cast<TUint8*>(iHash.Ptr())+ (8*aIndex), &value, sizeof(aVal)); |
|
466 } |
|
467 |
|
468 const TDesC8& CSHA384And512::Final() |
|
469 { |
|
470 AddLength(iBlockIndex); |
|
471 PadMessage(); |
|
472 Block(); |
|
473 // |
|
474 // Generate hash value into iHash |
|
475 // |
|
476 CopyWordToHash(iA, 0); |
|
477 CopyWordToHash(iB, 1); |
|
478 CopyWordToHash(iC, 2); |
|
479 CopyWordToHash(iD, 3); |
|
480 CopyWordToHash(iE, 4); |
|
481 CopyWordToHash(iF, 5); |
|
482 CopyWordToHash(iG, 6); |
|
483 CopyWordToHash(iH, 7); |
|
484 |
|
485 return iHash; |
|
486 } |
|
487 |
|
488 void CSHA384And512::RestoreState() |
|
489 { |
|
490 iA = iACopy; |
|
491 iB = iBCopy; |
|
492 iC = iCCopy; |
|
493 iD = iDCopy; |
|
494 iE = iECopy; |
|
495 iF = iFCopy; |
|
496 iG = iGCopy; |
|
497 iH = iHCopy; |
|
498 iNl = iNlCopy; |
|
499 iNh = iNhCopy; |
|
500 iBlockIndex = iBlockIndexCopy; |
|
501 Mem::Copy((TAny*)iData, (TAny*)iDataCopy, KSHA512BlockSize*sizeof(TUint64)); |
|
502 } |
|
503 |
|
504 void CSHA384And512::StoreState() |
|
505 { |
|
506 iACopy = iA; |
|
507 iBCopy = iB; |
|
508 iCCopy = iC; |
|
509 iDCopy = iD; |
|
510 iECopy = iE; |
|
511 iFCopy = iF; |
|
512 iGCopy = iG; |
|
513 iHCopy = iH; |
|
514 iNlCopy = iNl; |
|
515 iNhCopy = iNh; |
|
516 iBlockIndexCopy = iBlockIndex; |
|
517 Mem::Copy((TAny*)iDataCopy, (TAny*)iData, KSHA512BlockSize*sizeof(TUint64)); |
|
518 } |
|
519 |
|
520 // Implemented in hmacimpl.cpp or softwarehashbase.cpp |
|
521 // but required as derived from MHash. No coverage here. |
|
522 #ifdef _BullseyeCoverage |
|
523 #pragma suppress_warnings on |
|
524 #pragma BullseyeCoverage off |
|
525 #pragma suppress_warnings off |
|
526 #endif |
|
527 |