|
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 hash base class implementation |
|
16 * software hash base class implementation |
|
17 * |
|
18 */ |
|
19 |
|
20 |
|
21 /** |
|
22 @file |
|
23 */ |
|
24 |
|
25 #include "softwarehashbase.h" |
|
26 |
|
27 #include <cryptospi/hashplugin.h> |
|
28 #include "pluginconfig.h" |
|
29 #include "keys.h" |
|
30 #include "md2impl.h" |
|
31 #include "md5impl.h" |
|
32 #include "md4impl.h" |
|
33 #include "sha1impl.h" |
|
34 #include "sha2impl.h" |
|
35 #include "hmacimpl.h" |
|
36 |
|
37 using namespace SoftwareCrypto; |
|
38 |
|
39 CSoftwareHash* CSoftwareHash::NewL(TUid aAlgorithm, TUid aOperationMode, const CKey* aKey) |
|
40 { |
|
41 CSoftwareHash* self=NewLC(aAlgorithm, aOperationMode, aKey); |
|
42 CleanupStack::Pop(); |
|
43 return self; |
|
44 } |
|
45 |
|
46 CSoftwareHash* CSoftwareHash::NewL(TUid aAlgorithm) |
|
47 { |
|
48 CSoftwareHash* self=NewLC(aAlgorithm, CryptoSpi::KHashModeUid, NULL); |
|
49 CleanupStack::Pop(); |
|
50 return self; |
|
51 } |
|
52 |
|
53 CSoftwareHash* CSoftwareHash::NewLC(TUid aAlgorithm, TUid aOperationMode, const CKey* aKey) |
|
54 { |
|
55 CSoftwareHash* self=new (ELeave) CSoftwareHash(); |
|
56 CleanupStack::PushL(self); |
|
57 self->ConstructL(aAlgorithm, aOperationMode, aKey); |
|
58 return self; |
|
59 } |
|
60 |
|
61 CSoftwareHash::CSoftwareHash() |
|
62 { |
|
63 } |
|
64 |
|
65 CSoftwareHash::~CSoftwareHash() |
|
66 { |
|
67 if (iHashImpl) |
|
68 { |
|
69 iHashImpl->Close(); |
|
70 } |
|
71 |
|
72 if (iHmacImpl) |
|
73 { |
|
74 iHmacImpl->Close(); |
|
75 } |
|
76 delete iKey; |
|
77 } |
|
78 |
|
79 void CSoftwareHash::ConstructL(TUid aAlgorithm, TUid aOperationMode, const CKey* aKey) |
|
80 { |
|
81 // |
|
82 // Only Hash and Hmac mode are supported. |
|
83 // |
|
84 if (aOperationMode!=KHmacModeUid && aOperationMode!=KHashModeUid) |
|
85 { |
|
86 User::Leave(KErrNotSupported); |
|
87 } |
|
88 |
|
89 //Set the key if there is one |
|
90 if (aKey) |
|
91 { |
|
92 SetKeyL(*aKey); |
|
93 } |
|
94 |
|
95 switch (aAlgorithm.iUid) |
|
96 { |
|
97 case KCryptoPluginMd2: |
|
98 { |
|
99 iHashImpl=CMD2Impl::NewL(); |
|
100 } |
|
101 break; |
|
102 |
|
103 case KCryptoPluginMd5: |
|
104 { |
|
105 iHashImpl=CMD5Impl::NewL(); |
|
106 } |
|
107 break; |
|
108 |
|
109 case KCryptoPluginMd4: |
|
110 { |
|
111 iHashImpl=CMD4Impl::NewL(); |
|
112 } |
|
113 break; |
|
114 |
|
115 case KCryptoPluginSha1: |
|
116 { |
|
117 iHashImpl=CSHA1Impl::NewL(); |
|
118 } |
|
119 break; |
|
120 |
|
121 case KCryptoPluginSha224: |
|
122 case KCryptoPluginSha256: |
|
123 case KCryptoPluginSha384: |
|
124 case KCryptoPluginSha512: |
|
125 { |
|
126 iHashImpl=CSHA2Impl::NewL(aAlgorithm.iUid); |
|
127 } |
|
128 break; |
|
129 |
|
130 default: |
|
131 User::Leave(KErrNotSupported); |
|
132 } |
|
133 |
|
134 SetOperationModeL(aOperationMode); |
|
135 } |
|
136 |
|
137 void CSoftwareHash::SetOperationModeL(TUid aOperationMode) |
|
138 { |
|
139 switch (aOperationMode.iUid) |
|
140 { |
|
141 case KHmacMode: |
|
142 { |
|
143 // |
|
144 //Only create hmac implementation if there isn't one |
|
145 // |
|
146 if (!iHmacImpl) |
|
147 { |
|
148 if (iKey) |
|
149 { |
|
150 iHmacImpl=CHMacImpl::NewL(*iKey, iHashImpl); |
|
151 } |
|
152 else |
|
153 { |
|
154 iHmacImpl=CHMacImpl::NewL(iHashImpl); |
|
155 } |
|
156 } |
|
157 } |
|
158 break; |
|
159 |
|
160 case KHashMode: |
|
161 { |
|
162 Reset(); |
|
163 } |
|
164 break; |
|
165 |
|
166 default: |
|
167 User::Leave(KErrNotSupported); |
|
168 } |
|
169 |
|
170 // |
|
171 // Set the operation mode. |
|
172 // |
|
173 iOperationMode=aOperationMode; |
|
174 } |
|
175 |
|
176 MSoftwareHash* CSoftwareHash::Impl() |
|
177 { |
|
178 MSoftwareHash* impl=NULL; |
|
179 if (iOperationMode==KHashModeUid) |
|
180 { |
|
181 impl=iHashImpl; |
|
182 } |
|
183 else if (iOperationMode==KHmacModeUid && iKey) |
|
184 { |
|
185 impl=iHmacImpl; |
|
186 } |
|
187 return impl; |
|
188 } |
|
189 |
|
190 void CSoftwareHash::SetKeyL(const CKey& aKey) |
|
191 { |
|
192 Reset(); |
|
193 delete iKey; |
|
194 iKey=CKey::NewL(aKey); |
|
195 if (iHmacImpl) |
|
196 { |
|
197 iHmacImpl->SetKeyL(aKey); |
|
198 } |
|
199 } |
|
200 |
|
201 void CSoftwareHash::Reset() |
|
202 { |
|
203 if (iHashImpl) |
|
204 { |
|
205 iHashImpl->Reset(); |
|
206 } |
|
207 |
|
208 if (iHmacImpl) |
|
209 { |
|
210 iHmacImpl->Reset(); |
|
211 } |
|
212 } |
|
213 |
|
214 void CSoftwareHash::Close() |
|
215 { |
|
216 delete this; |
|
217 } |
|
218 |
|
219 void CSoftwareHash::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics) |
|
220 { |
|
221 MSoftwareHash* impl=Impl(); |
|
222 if (impl) |
|
223 { |
|
224 impl->GetCharacteristicsL(aPluginCharacteristics); |
|
225 } |
|
226 else |
|
227 { |
|
228 User::Leave(KErrNotReady); |
|
229 } |
|
230 } |
|
231 |
|
232 const CExtendedCharacteristics* CSoftwareHash::GetExtendedCharacteristicsL() |
|
233 { |
|
234 MSoftwareHash* impl=Impl(); |
|
235 if (!impl) |
|
236 { |
|
237 User::Leave(KErrNotReady); |
|
238 } |
|
239 return impl->GetExtendedCharacteristicsL(); |
|
240 } |
|
241 |
|
242 TAny* CSoftwareHash::GetExtension(TUid aExtensionId) |
|
243 { |
|
244 MSoftwareHash* impl=Impl(); |
|
245 if (impl) |
|
246 { |
|
247 return impl->GetExtension(aExtensionId); |
|
248 } |
|
249 else |
|
250 { |
|
251 return NULL; |
|
252 } |
|
253 } |
|
254 |
|
255 TPtrC8 CSoftwareHash::Hash(const TDesC8& aMessage) |
|
256 { |
|
257 MSoftwareHash* impl=Impl(); |
|
258 if (impl) |
|
259 { |
|
260 return impl->Hash(aMessage); |
|
261 } |
|
262 else |
|
263 { |
|
264 return KNullDesC8(); |
|
265 } |
|
266 } |
|
267 |
|
268 void CSoftwareHash::Update(const TDesC8& aMessage) |
|
269 { |
|
270 MSoftwareHash* impl=Impl(); |
|
271 if (impl) |
|
272 { |
|
273 return impl->Update(aMessage); |
|
274 } |
|
275 } |
|
276 |
|
277 TPtrC8 CSoftwareHash::Final(const TDesC8& aMessage) |
|
278 { |
|
279 MSoftwareHash* impl=Impl(); |
|
280 if (impl) |
|
281 { |
|
282 return impl->Final(aMessage); |
|
283 } |
|
284 else |
|
285 { |
|
286 return KNullDesC8(); |
|
287 } |
|
288 } |
|
289 |
|
290 MHash* CSoftwareHash::ReplicateL() |
|
291 { |
|
292 CSoftwareHash* that=new(ELeave)CSoftwareHash(); |
|
293 CleanupStack::PushL(that); |
|
294 if (this->iKey) |
|
295 { |
|
296 that->iKey=CKey::NewL(*this->iKey); |
|
297 } |
|
298 that->iOperationMode=this->iOperationMode; |
|
299 that->iHashImpl=static_cast<MSoftwareHash*>(this->iHashImpl->ReplicateL()); |
|
300 if (this->iHmacImpl) |
|
301 { |
|
302 that->iHmacImpl=static_cast<MSoftwareHash*>(this->iHmacImpl->ReplicateL()); |
|
303 } |
|
304 CleanupStack::Pop(); |
|
305 return that; |
|
306 } |
|
307 |
|
308 MHash* CSoftwareHash::CopyL() |
|
309 { |
|
310 CSoftwareHash* that=new(ELeave)CSoftwareHash(); |
|
311 CleanupStack::PushL(that); |
|
312 if (this->iKey) |
|
313 { |
|
314 that->iKey=CKey::NewL(*this->iKey); |
|
315 } |
|
316 that->iOperationMode=this->iOperationMode; |
|
317 that->iHashImpl=static_cast<MSoftwareHash*>(this->iHashImpl->CopyL()); |
|
318 if (this->iHmacImpl) |
|
319 { |
|
320 that->iHmacImpl=static_cast<MSoftwareHash*>(this->iHmacImpl->CopyL()); |
|
321 } |
|
322 CleanupStack::Pop(); |
|
323 return that; |
|
324 } |
|
325 |
|
326 |