|
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 * software SHA2 implementation |
|
16 * RFC 4634 (US Secure Hash Algorithms (SHA and HMAC-SHA)) |
|
17 * FIPS 180-2 (With change notice) |
|
18 * |
|
19 */ |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 /** |
|
25 @file |
|
26 */ |
|
27 |
|
28 #include <hash.h> |
|
29 #include "sha224and256.h" |
|
30 #include "sha384and512.h" |
|
31 |
|
32 // Initial Hash Values of SHA2 algorithms |
|
33 /** |
|
34 * Initial Hash Value for SHA-224 |
|
35 * |
|
36 * These words were obtained by taking the first thirty-two bits |
|
37 * of the fractional parts of the square roots of the first eight |
|
38 * prime numbers. |
|
39 * |
|
40 * FIPS 180-2 Appendix |
|
41 * FIPS 180-3 Section 5.3.2 |
|
42 */ |
|
43 const TUint SHA224InitVals[] = |
|
44 { |
|
45 0xc1059ed8, // A |
|
46 0x367cd507, // B |
|
47 0x3070dd17, // C |
|
48 0xf70e5939, // D |
|
49 0xffc00b31, // E |
|
50 0x68581511, // F |
|
51 0x64f98fa7, // G |
|
52 0xbefa4fa4 // H |
|
53 }; |
|
54 |
|
55 /** |
|
56 * Initial Hash Value for SHA-256 |
|
57 * |
|
58 * These words were obtained by taking the first thirty-two bits |
|
59 * of the fractional parts of the square roots of the first eight |
|
60 * prime numbers. |
|
61 * |
|
62 * FIPS 180-2 Section 5.3.2 |
|
63 */ |
|
64 const TUint SHA256InitVals[] = |
|
65 { |
|
66 0x6a09e667, // A |
|
67 0xbb67ae85, // B |
|
68 0x3c6ef372, // C |
|
69 0xa54ff53a, // D |
|
70 0x510e527f, // E |
|
71 0x9b05688c, // F |
|
72 0x1f83d9ab, // G |
|
73 0x5be0cd19 // H |
|
74 }; |
|
75 |
|
76 /** |
|
77 * Initial Hash Value for SHA-384 |
|
78 * |
|
79 * These words were obtained by taking the first sixty-four bits |
|
80 * of the fractional parts of the square roots of the first eight |
|
81 * prime numbers. |
|
82 * |
|
83 * FIPS 180-2 Section 5.3.3 |
|
84 */ |
|
85 const TUint64 SHA384InitVals[] = |
|
86 { |
|
87 UI64LIT(0xcbbb9d5dc1059ed8), // A |
|
88 UI64LIT(0x629a292a367cd507), // B |
|
89 UI64LIT(0x9159015a3070dd17), // C |
|
90 UI64LIT(0x152fecd8f70e5939), // D |
|
91 UI64LIT(0x67332667ffc00b31), // E |
|
92 UI64LIT(0x8eb44a8768581511), // F |
|
93 UI64LIT(0xdb0c2e0d64f98fa7), // G |
|
94 UI64LIT(0x47b5481dbefa4fa4) // H |
|
95 }; |
|
96 |
|
97 /** |
|
98 * Initial Hash Value for SHA-512 |
|
99 * |
|
100 * These words were obtained by taking the first sixty-four bits |
|
101 * of the fractional parts of the square roots of the first eight |
|
102 * prime numbers. |
|
103 * |
|
104 * FIPS 180-2 Section 5.3.4 |
|
105 */ |
|
106 const TUint64 SHA512InitVals[] = |
|
107 { |
|
108 UI64LIT(0x6a09e667f3bcc908), // A |
|
109 UI64LIT(0xbb67ae8584caa73b), // B |
|
110 UI64LIT(0x3c6ef372fe94f82b), // C |
|
111 UI64LIT(0xa54ff53a5f1d36f1), // D |
|
112 UI64LIT(0x510e527fade682d1), // E |
|
113 UI64LIT(0x9b05688c2b3e6c1f), // F |
|
114 UI64LIT(0x1f83d9abfb41bd6b), // G |
|
115 UI64LIT(0x5be0cd19137e2179) // H |
|
116 }; |
|
117 |
|
118 |
|
119 EXPORT_C CSHA2* CSHA2::NewL(TSH2Algo aAlgorithmId) |
|
120 { |
|
121 CSHA2* self = CSHA2::NewLC(aAlgorithmId); |
|
122 CleanupStack::Pop(self); |
|
123 return self; |
|
124 } |
|
125 |
|
126 EXPORT_C CSHA2* CSHA2::NewLC(TSH2Algo aAlgorithmId) |
|
127 { |
|
128 CSHA2* self = new (ELeave) CSHA2(); |
|
129 CleanupStack::PushL(self); |
|
130 self->ConstructL(aAlgorithmId); |
|
131 return self; |
|
132 } |
|
133 |
|
134 void CSHA2::ConstructL(const CSHA2& aSHA2) |
|
135 { |
|
136 iAlgorithmType = aSHA2.iAlgorithmType; |
|
137 iInitValues = aSHA2.iInitValues; |
|
138 iHashSize = aSHA2.iHashSize; |
|
139 switch(iAlgorithmType) |
|
140 { |
|
141 case E224Bit: |
|
142 case E256Bit: |
|
143 { |
|
144 const CSHA224And256* const impl = static_cast<CSHA224And256*>(aSHA2.iImplementation); |
|
145 iImplementation = new (ELeave) CSHA224And256(*impl); |
|
146 break; |
|
147 } |
|
148 case E384Bit: |
|
149 case E512Bit: |
|
150 { |
|
151 const CSHA384And512* const impl = static_cast<CSHA384And512*>(aSHA2.iImplementation); |
|
152 iImplementation = new (ELeave) CSHA384And512(*impl); |
|
153 break; |
|
154 } |
|
155 default: |
|
156 { |
|
157 User::Leave(KErrNotSupported); |
|
158 } |
|
159 } |
|
160 } |
|
161 |
|
162 void CSHA2::ConstructL(TSH2Algo aAlgorithmId) |
|
163 { |
|
164 switch(aAlgorithmId) |
|
165 { |
|
166 case E224Bit: |
|
167 { |
|
168 iImplementation = CSHA224And256::NewL(); |
|
169 iInitValues = SHA224InitVals; |
|
170 iAlgorithmType = E224Bit; |
|
171 iHashSize = KSHA224HashSize; |
|
172 break; |
|
173 } |
|
174 case E256Bit: |
|
175 { |
|
176 iImplementation = CSHA224And256::NewL(); |
|
177 iInitValues = SHA256InitVals; |
|
178 iAlgorithmType = E256Bit; |
|
179 iHashSize = KSHA256HashSize; |
|
180 break; |
|
181 } |
|
182 case E384Bit: |
|
183 { |
|
184 iImplementation = CSHA384And512::NewL(); |
|
185 iInitValues = SHA384InitVals; |
|
186 iAlgorithmType = E384Bit; |
|
187 iHashSize = KSHA384HashSize; |
|
188 break; |
|
189 } |
|
190 case E512Bit: |
|
191 { |
|
192 iImplementation = CSHA384And512::NewL(); |
|
193 iInitValues = SHA512InitVals; |
|
194 iAlgorithmType = E512Bit; |
|
195 iHashSize = KSHA512HashSize; |
|
196 break; |
|
197 } |
|
198 default: |
|
199 { |
|
200 User::Leave(KErrNotSupported); |
|
201 } |
|
202 } |
|
203 |
|
204 Reset(); |
|
205 } |
|
206 |
|
207 EXPORT_C CSHA2::~CSHA2() |
|
208 { |
|
209 delete iImplementation; |
|
210 } |
|
211 |
|
212 EXPORT_C CMessageDigest* CSHA2::ReplicateL() |
|
213 { |
|
214 return CSHA2::NewL(iAlgorithmType); |
|
215 } |
|
216 |
|
217 EXPORT_C TPtrC8 CSHA2::Hash(const TDesC8& aMessage) |
|
218 { |
|
219 TPtrC8 ptr(KNullDesC8()); |
|
220 iImplementation->Update(aMessage.Ptr(),aMessage.Size()); |
|
221 iImplementation->StoreState(); |
|
222 ptr.Set(iImplementation->Final().Ptr(), iHashSize); |
|
223 iImplementation->RestoreState(); |
|
224 return ptr; |
|
225 } |
|
226 |
|
227 EXPORT_C CMessageDigest* CSHA2::CopyL() |
|
228 { |
|
229 CSHA2* hash = new(ELeave) CSHA2(); |
|
230 CleanupStack::PushL(hash); |
|
231 hash->ConstructL(*this); |
|
232 CleanupStack::Pop(hash); |
|
233 return hash; |
|
234 } |
|
235 |
|
236 EXPORT_C TInt CSHA2::BlockSize(void) |
|
237 { |
|
238 TInt blockSize = KSHA256BlockSize; |
|
239 if(E384Bit == iAlgorithmType || E512Bit == iAlgorithmType) |
|
240 { |
|
241 blockSize = KSHA512BlockSize; |
|
242 } |
|
243 return blockSize; |
|
244 } |
|
245 |
|
246 EXPORT_C TInt CSHA2::HashSize(void) |
|
247 { |
|
248 return iHashSize; |
|
249 } |
|
250 |
|
251 EXPORT_C void CSHA2::Reset() |
|
252 { |
|
253 iImplementation->Reset(iInitValues); |
|
254 } |
|
255 |
|
256 EXPORT_C void CSHA2::Update(const TDesC8& aMessage) |
|
257 { |
|
258 iImplementation->Update(aMessage.Ptr(),aMessage.Size()); |
|
259 } |
|
260 |
|
261 EXPORT_C TPtrC8 CSHA2::Final(void) |
|
262 { |
|
263 TPtrC8 ptr(KNullDesC8()); |
|
264 ptr.Set(iImplementation->Final().Ptr(), iHashSize); |
|
265 Reset(); |
|
266 return ptr; |
|
267 } |
|
268 |
|
269 EXPORT_C TPtrC8 CSHA2::Final(const TDesC8& aMessage) |
|
270 { |
|
271 iImplementation->Update(aMessage.Ptr(),aMessage.Size()); |
|
272 TPtrC8 ptr(KNullDesC8()); |
|
273 ptr.Set(iImplementation->Final().Ptr(), iHashSize); |
|
274 Reset(); |
|
275 return ptr; |
|
276 } |
|
277 |
|
278 void CSHA2::RestoreState() |
|
279 { |
|
280 iImplementation->RestoreState(); |
|
281 } |
|
282 |
|
283 void CSHA2::StoreState() |
|
284 { |
|
285 iImplementation->StoreState(); |
|
286 } |
|
287 |
|
288 // Implemented in hmacimpl.cpp or softwarehashbase.cpp |
|
289 // but required as derived from MHash. No coverage here. |
|
290 #ifdef _BullseyeCoverage |
|
291 #pragma suppress_warnings on |
|
292 #pragma BullseyeCoverage off |
|
293 #pragma suppress_warnings off |
|
294 #endif |