17
|
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 "sha2impl.h"
|
|
27 |
|
|
28 |
#include <cryptospi/hashplugin.h>
|
|
29 |
#include "pluginconfig.h"
|
|
30 |
#include "sha224and256impl.h"
|
|
31 |
#include "sha384and512impl.h"
|
|
32 |
|
|
33 |
using namespace SoftwareCrypto;
|
|
34 |
|
|
35 |
// Initial Hash Values of SHA2 algorithms
|
|
36 |
/**
|
|
37 |
* Initial Hash Value for SHA-224
|
|
38 |
*
|
|
39 |
* These words were obtained by taking the first thirty-two bits
|
|
40 |
* of the fractional parts of the square roots of the first eight
|
|
41 |
* prime numbers.
|
|
42 |
*
|
|
43 |
* FIPS 180-2 Appendix
|
|
44 |
* FIPS 180-3 Section 5.3.2
|
|
45 |
*/
|
|
46 |
const TUint SHA224InitVals[] =
|
|
47 |
{
|
|
48 |
0xc1059ed8, // A
|
|
49 |
0x367cd507, // B
|
|
50 |
0x3070dd17, // C
|
|
51 |
0xf70e5939, // D
|
|
52 |
0xffc00b31, // E
|
|
53 |
0x68581511, // F
|
|
54 |
0x64f98fa7, // G
|
|
55 |
0xbefa4fa4 // H
|
|
56 |
};
|
|
57 |
|
|
58 |
/**
|
|
59 |
* Initial Hash Value for SHA-256
|
|
60 |
*
|
|
61 |
* These words were obtained by taking the first thirty-two bits
|
|
62 |
* of the fractional parts of the square roots of the first eight
|
|
63 |
* prime numbers.
|
|
64 |
*
|
|
65 |
* FIPS 180-2 Section 5.3.2
|
|
66 |
*/
|
|
67 |
const TUint SHA256InitVals[] =
|
|
68 |
{
|
|
69 |
0x6a09e667, // A
|
|
70 |
0xbb67ae85, // B
|
|
71 |
0x3c6ef372, // C
|
|
72 |
0xa54ff53a, // D
|
|
73 |
0x510e527f, // E
|
|
74 |
0x9b05688c, // F
|
|
75 |
0x1f83d9ab, // G
|
|
76 |
0x5be0cd19 // H
|
|
77 |
};
|
|
78 |
|
|
79 |
/**
|
|
80 |
* Initial Hash Value for SHA-384
|
|
81 |
*
|
|
82 |
* These words were obtained by taking the first sixty-four bits
|
|
83 |
* of the fractional parts of the square roots of the first eight
|
|
84 |
* prime numbers.
|
|
85 |
*
|
|
86 |
* FIPS 180-2 Section 5.3.3
|
|
87 |
*/
|
|
88 |
const TUint64 SHA384InitVals[] =
|
|
89 |
{
|
|
90 |
UI64LIT(0xcbbb9d5dc1059ed8), // A
|
|
91 |
UI64LIT(0x629a292a367cd507), // B
|
|
92 |
UI64LIT(0x9159015a3070dd17), // C
|
|
93 |
UI64LIT(0x152fecd8f70e5939), // D
|
|
94 |
UI64LIT(0x67332667ffc00b31), // E
|
|
95 |
UI64LIT(0x8eb44a8768581511), // F
|
|
96 |
UI64LIT(0xdb0c2e0d64f98fa7), // G
|
|
97 |
UI64LIT(0x47b5481dbefa4fa4) // H
|
|
98 |
};
|
|
99 |
|
|
100 |
/**
|
|
101 |
* Initial Hash Value for SHA-512
|
|
102 |
*
|
|
103 |
* These words were obtained by taking the first sixty-four bits
|
|
104 |
* of the fractional parts of the square roots of the first eight
|
|
105 |
* prime numbers.
|
|
106 |
*
|
|
107 |
* FIPS 180-2 Section 5.3.4
|
|
108 |
*/
|
|
109 |
const TUint64 SHA512InitVals[] =
|
|
110 |
{
|
|
111 |
UI64LIT(0x6a09e667f3bcc908), // A
|
|
112 |
UI64LIT(0xbb67ae8584caa73b), // B
|
|
113 |
UI64LIT(0x3c6ef372fe94f82b), // C
|
|
114 |
UI64LIT(0xa54ff53a5f1d36f1), // D
|
|
115 |
UI64LIT(0x510e527fade682d1), // E
|
|
116 |
UI64LIT(0x9b05688c2b3e6c1f), // F
|
|
117 |
UI64LIT(0x1f83d9abfb41bd6b), // G
|
|
118 |
UI64LIT(0x5be0cd19137e2179) // H
|
|
119 |
};
|
|
120 |
|
|
121 |
|
|
122 |
CSHA2Impl* CSHA2Impl::NewL(TInt32 aAlgorithmId)
|
|
123 |
{
|
|
124 |
CSHA2Impl* self = CSHA2Impl::NewLC(aAlgorithmId);
|
|
125 |
CleanupStack::Pop(self);
|
|
126 |
return self;
|
|
127 |
}
|
|
128 |
|
|
129 |
CSHA2Impl* CSHA2Impl::NewLC(TInt32 aAlgorithmId)
|
|
130 |
{
|
|
131 |
CSHA2Impl* self = new (ELeave) CSHA2Impl();
|
|
132 |
CleanupStack::PushL(self);
|
|
133 |
self->ConstructL(aAlgorithmId);
|
|
134 |
return self;
|
|
135 |
}
|
|
136 |
|
|
137 |
void CSHA2Impl::ConstructL(const CSHA2Impl& aSHA2Impl)
|
|
138 |
{
|
|
139 |
iImplementationUid = aSHA2Impl.iImplementationUid;
|
|
140 |
iInitValues = aSHA2Impl.iInitValues;
|
|
141 |
iHashSize = aSHA2Impl.iHashSize;
|
|
142 |
switch(iImplementationUid.iUid)
|
|
143 |
{
|
|
144 |
case KCryptoPluginSha224:
|
|
145 |
case KCryptoPluginSha256:
|
|
146 |
{
|
|
147 |
const CSHA224And256Impl* const impl = static_cast<CSHA224And256Impl*>(aSHA2Impl.iImplementation);
|
|
148 |
iImplementation = new (ELeave) CSHA224And256Impl(*impl);
|
|
149 |
break;
|
|
150 |
}
|
|
151 |
case KCryptoPluginSha384:
|
|
152 |
case KCryptoPluginSha512:
|
|
153 |
{
|
|
154 |
const CSHA384And512Impl* const impl = static_cast<CSHA384And512Impl*>(aSHA2Impl.iImplementation);
|
|
155 |
iImplementation = new (ELeave) CSHA384And512Impl(*impl);
|
|
156 |
break;
|
|
157 |
}
|
|
158 |
default:
|
|
159 |
{
|
|
160 |
User::Leave(KErrNotSupported);
|
|
161 |
}
|
|
162 |
}
|
|
163 |
}
|
|
164 |
|
|
165 |
void CSHA2Impl::ConstructL(TInt32 aAlgorithmId)
|
|
166 |
{
|
|
167 |
switch(aAlgorithmId)
|
|
168 |
{
|
|
169 |
case KCryptoPluginSha224:
|
|
170 |
{
|
|
171 |
iImplementation = CSHA224And256Impl::NewL();
|
|
172 |
iInitValues = SHA224InitVals;
|
|
173 |
iImplementationUid = KCryptoPluginSha224Uid;
|
|
174 |
iHashSize = KSHA224HashSize;
|
|
175 |
break;
|
|
176 |
}
|
|
177 |
case KCryptoPluginSha256:
|
|
178 |
{
|
|
179 |
iImplementation = CSHA224And256Impl::NewL();
|
|
180 |
iInitValues = SHA256InitVals;
|
|
181 |
iImplementationUid = KCryptoPluginSha256Uid;
|
|
182 |
iHashSize = KSHA256HashSize;
|
|
183 |
break;
|
|
184 |
}
|
|
185 |
case KCryptoPluginSha384:
|
|
186 |
{
|
|
187 |
iImplementation = CSHA384And512Impl::NewL();
|
|
188 |
iInitValues = SHA384InitVals;
|
|
189 |
iImplementationUid = KCryptoPluginSha384Uid;
|
|
190 |
iHashSize = KSHA384HashSize;
|
|
191 |
break;
|
|
192 |
}
|
|
193 |
case KCryptoPluginSha512:
|
|
194 |
{
|
|
195 |
iImplementation = CSHA384And512Impl::NewL();
|
|
196 |
iInitValues = SHA512InitVals;
|
|
197 |
iImplementationUid = KCryptoPluginSha512Uid;
|
|
198 |
iHashSize = KSHA512HashSize;
|
|
199 |
break;
|
|
200 |
}
|
|
201 |
default:
|
|
202 |
{
|
|
203 |
User::Leave(KErrNotSupported);
|
|
204 |
}
|
|
205 |
}
|
|
206 |
|
|
207 |
Reset();
|
|
208 |
}
|
|
209 |
|
|
210 |
CSHA2Impl::~CSHA2Impl()
|
|
211 |
{
|
|
212 |
delete iImplementation;
|
|
213 |
}
|
|
214 |
|
|
215 |
void CSHA2Impl::Reset()
|
|
216 |
{
|
|
217 |
iImplementation->Reset(iInitValues);
|
|
218 |
}
|
|
219 |
|
|
220 |
void CSHA2Impl::Close()
|
|
221 |
{
|
|
222 |
delete this;
|
|
223 |
}
|
|
224 |
|
|
225 |
MHash* CSHA2Impl::ReplicateL()
|
|
226 |
{
|
|
227 |
return CSHA2Impl::NewL(iImplementationUid.iUid);
|
|
228 |
}
|
|
229 |
|
|
230 |
MHash* CSHA2Impl::CopyL()
|
|
231 |
{
|
|
232 |
CSHA2Impl* hash = new(ELeave) CSHA2Impl();
|
|
233 |
CleanupStack::PushL(hash);
|
|
234 |
hash->ConstructL(*this);
|
|
235 |
CleanupStack::Pop(hash);
|
|
236 |
return hash;
|
|
237 |
}
|
|
238 |
|
|
239 |
TUid CSHA2Impl::ImplementationUid()
|
|
240 |
{
|
|
241 |
return iImplementationUid;
|
|
242 |
}
|
|
243 |
|
|
244 |
void CSHA2Impl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
|
|
245 |
{
|
|
246 |
aPluginCharacteristics=NULL;
|
|
247 |
TInt hashNum=sizeof(KHashCharacteristics)/sizeof(THashCharacteristics*);
|
|
248 |
for (TInt i=0;i<hashNum;i++)
|
|
249 |
{
|
|
250 |
if (KHashCharacteristics[i]->cmn.iImplementationUID == ImplementationUid().iUid)
|
|
251 |
{
|
|
252 |
aPluginCharacteristics = KHashCharacteristics[i];
|
|
253 |
break;
|
|
254 |
}
|
|
255 |
}
|
|
256 |
}
|
|
257 |
|
|
258 |
CExtendedCharacteristics* CSHA2Impl::CreateExtendedCharacteristicsL()
|
|
259 |
{
|
|
260 |
// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
|
|
261 |
// for exclusive use and are not CERTIFIED to be standards compliant.
|
|
262 |
return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
|
|
263 |
}
|
|
264 |
|
|
265 |
const CExtendedCharacteristics* CSHA2Impl::GetExtendedCharacteristicsL()
|
|
266 |
{
|
|
267 |
return CSHA2Impl::CreateExtendedCharacteristicsL();
|
|
268 |
}
|
|
269 |
|
|
270 |
TPtrC8 CSHA2Impl::Hash(const TDesC8& aMessage)
|
|
271 |
{
|
|
272 |
TPtrC8 ptr(KNullDesC8());
|
|
273 |
iImplementation->Update(aMessage.Ptr(),aMessage.Size());
|
|
274 |
iImplementation->StoreState();
|
|
275 |
ptr.Set(iImplementation->Final().Ptr(), iHashSize);
|
|
276 |
iImplementation->RestoreState();
|
|
277 |
return ptr;
|
|
278 |
}
|
|
279 |
|
|
280 |
void CSHA2Impl::Update(const TDesC8& aMessage)
|
|
281 |
{
|
|
282 |
iImplementation->Update(aMessage.Ptr(),aMessage.Size());
|
|
283 |
}
|
|
284 |
|
|
285 |
TPtrC8 CSHA2Impl::Final(const TDesC8& aMessage)
|
|
286 |
{
|
|
287 |
TPtrC8 ptr(KNullDesC8());
|
|
288 |
if (aMessage!=KNullDesC8())
|
|
289 |
{
|
|
290 |
iImplementation->Update(aMessage.Ptr(),aMessage.Size());
|
|
291 |
}
|
|
292 |
ptr.Set(iImplementation->Final().Ptr(), iHashSize);
|
|
293 |
Reset();
|
|
294 |
return ptr;
|
|
295 |
}
|
|
296 |
|
|
297 |
void CSHA2Impl::RestoreState()
|
|
298 |
{
|
|
299 |
iImplementation->RestoreState();
|
|
300 |
}
|
|
301 |
|
|
302 |
void CSHA2Impl::StoreState()
|
|
303 |
{
|
|
304 |
iImplementation->StoreState();
|
|
305 |
}
|
|
306 |
|
|
307 |
// Implemented in hmacimpl.cpp or softwarehashbase.cpp
|
|
308 |
// but required as derived from MHash. No coverage here.
|
|
309 |
#ifdef _BullseyeCoverage
|
|
310 |
#pragma suppress_warnings on
|
|
311 |
#pragma BullseyeCoverage off
|
|
312 |
#pragma suppress_warnings off
|
|
313 |
#endif
|
|
314 |
|
|
315 |
TAny* CSHA2Impl::GetExtension(TUid /*aExtensionId*/)
|
|
316 |
{
|
|
317 |
return NULL;
|
|
318 |
}
|
|
319 |
|
|
320 |
void CSHA2Impl::SetOperationModeL(TUid /*aOperationMode*/)
|
|
321 |
{
|
|
322 |
User::Leave(KErrNotSupported);
|
|
323 |
}
|
|
324 |
|
|
325 |
void CSHA2Impl::SetKeyL(const CKey& /*aKey*/)
|
|
326 |
{
|
|
327 |
User::Leave(KErrNotSupported);
|
|
328 |
}
|
|
329 |
|