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