|
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 "sha1impl.h" |
|
33 #include "hmacimpl.h" |
|
34 |
|
35 using namespace SoftwareCrypto; |
|
36 |
|
37 CSoftwareHash* CSoftwareHash::NewL(TUid aAlgorithm, TUid aOperationMode, const CKey* aKey) |
|
38 { |
|
39 CSoftwareHash* self=NewLC(aAlgorithm, aOperationMode, aKey); |
|
40 CleanupStack::Pop(); |
|
41 return self; |
|
42 } |
|
43 |
|
44 #ifdef SYMBIAN_SDP_IPSEC_VOIP_SUPPORT |
|
45 CSoftwareHash* CSoftwareHash::NewL(TUid aAlgorithm) |
|
46 { |
|
47 CSoftwareHash* self=NewLC(aAlgorithm, KHashModeUid, NULL); |
|
48 CleanupStack::Pop(); |
|
49 return self; |
|
50 } |
|
51 #endif |
|
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 KTestPlugin01Md2_1: |
|
98 case KTestPlugin01Md2_2: |
|
99 { |
|
100 iHashImpl=CMD2Impl::NewL(aAlgorithm); |
|
101 } |
|
102 break; |
|
103 |
|
104 // case KCryptoPluginMd5: |
|
105 // { |
|
106 // iHashImpl=CMD5Impl::NewL(); |
|
107 // } |
|
108 // break; |
|
109 |
|
110 // case KCryptoPluginSha1: |
|
111 // { |
|
112 // iHashImpl=CSHA1Impl::NewL(); |
|
113 // } |
|
114 // break; |
|
115 |
|
116 default: |
|
117 User::Leave(KErrNotSupported); |
|
118 } |
|
119 |
|
120 SetOperationModeL(aOperationMode); |
|
121 } |
|
122 |
|
123 void CSoftwareHash::SetOperationModeL(TUid aOperationMode) |
|
124 { |
|
125 switch (aOperationMode.iUid) |
|
126 { |
|
127 case KHmacMode: |
|
128 { |
|
129 // |
|
130 //Only create hmac implementation if there isn't one |
|
131 // |
|
132 if (!iHmacImpl) |
|
133 { |
|
134 if (iKey) |
|
135 { |
|
136 iHmacImpl=CHMacImpl::NewL(*iKey, iHashImpl); |
|
137 } |
|
138 else |
|
139 { |
|
140 iHmacImpl=CHMacImpl::NewL(iHashImpl); |
|
141 } |
|
142 } |
|
143 } |
|
144 break; |
|
145 |
|
146 case KHashMode: |
|
147 { |
|
148 Reset(); |
|
149 } |
|
150 break; |
|
151 |
|
152 default: |
|
153 User::Leave(KErrNotSupported); |
|
154 } |
|
155 |
|
156 // |
|
157 // Set the operation mode. |
|
158 // |
|
159 iOperationMode=aOperationMode; |
|
160 } |
|
161 |
|
162 MSoftwareHash* CSoftwareHash::Impl() |
|
163 { |
|
164 MSoftwareHash* impl=NULL; |
|
165 if (iOperationMode==KHashModeUid) |
|
166 { |
|
167 impl=iHashImpl; |
|
168 } |
|
169 else if (iOperationMode==KHmacModeUid && iKey) |
|
170 { |
|
171 impl=iHmacImpl; |
|
172 } |
|
173 return impl; |
|
174 } |
|
175 |
|
176 void CSoftwareHash::SetKeyL(const CKey& aKey) |
|
177 { |
|
178 Reset(); |
|
179 delete iKey; |
|
180 iKey=CKey::NewL(aKey); |
|
181 if (iHmacImpl) |
|
182 { |
|
183 iHmacImpl->SetKeyL(aKey); |
|
184 } |
|
185 } |
|
186 |
|
187 void CSoftwareHash::Reset() |
|
188 { |
|
189 if (iHashImpl) |
|
190 { |
|
191 iHashImpl->Reset(); |
|
192 } |
|
193 |
|
194 if (iHmacImpl) |
|
195 { |
|
196 iHmacImpl->Reset(); |
|
197 } |
|
198 } |
|
199 |
|
200 void CSoftwareHash::Close() |
|
201 { |
|
202 delete this; |
|
203 } |
|
204 |
|
205 void CSoftwareHash::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics) |
|
206 { |
|
207 MSoftwareHash* impl=Impl(); |
|
208 if (impl) |
|
209 { |
|
210 impl->GetCharacteristicsL(aPluginCharacteristics); |
|
211 } |
|
212 else |
|
213 { |
|
214 User::Leave(KErrNotReady); |
|
215 } |
|
216 } |
|
217 |
|
218 const CExtendedCharacteristics* CSoftwareHash::GetExtendedCharacteristicsL() |
|
219 { |
|
220 MSoftwareHash* impl=Impl(); |
|
221 if (!impl) |
|
222 { |
|
223 User::Leave(KErrNotReady); |
|
224 } |
|
225 return impl->GetExtendedCharacteristicsL(); |
|
226 } |
|
227 |
|
228 TAny* CSoftwareHash::GetExtension(TUid aExtensionId) |
|
229 { |
|
230 MSoftwareHash* impl=Impl(); |
|
231 if (impl) |
|
232 { |
|
233 return impl->GetExtension(aExtensionId); |
|
234 } |
|
235 else |
|
236 { |
|
237 return NULL; |
|
238 } |
|
239 } |
|
240 |
|
241 TPtrC8 CSoftwareHash::Hash(const TDesC8& aMessage) |
|
242 { |
|
243 MSoftwareHash* impl=Impl(); |
|
244 if (impl) |
|
245 { |
|
246 return impl->Hash(aMessage); |
|
247 } |
|
248 else |
|
249 { |
|
250 return KNullDesC8(); |
|
251 } |
|
252 } |
|
253 |
|
254 void CSoftwareHash::Update(const TDesC8& aMessage) |
|
255 { |
|
256 MSoftwareHash* impl=Impl(); |
|
257 if (impl) |
|
258 { |
|
259 return impl->Update(aMessage); |
|
260 } |
|
261 } |
|
262 |
|
263 TPtrC8 CSoftwareHash::Final(const TDesC8& aMessage) |
|
264 { |
|
265 MSoftwareHash* impl=Impl(); |
|
266 if (impl) |
|
267 { |
|
268 return impl->Final(aMessage); |
|
269 } |
|
270 else |
|
271 { |
|
272 return KNullDesC8(); |
|
273 } |
|
274 } |
|
275 |
|
276 MHash* CSoftwareHash::ReplicateL() |
|
277 { |
|
278 CSoftwareHash* that=new(ELeave)CSoftwareHash(); |
|
279 CleanupStack::PushL(that); |
|
280 if (this->iKey) |
|
281 { |
|
282 that->iKey=CKey::NewL(*this->iKey); |
|
283 } |
|
284 that->iOperationMode=this->iOperationMode; |
|
285 that->iHashImpl=static_cast<MSoftwareHash*>(this->iHashImpl->ReplicateL()); |
|
286 if (this->iHmacImpl) |
|
287 { |
|
288 that->iHmacImpl=static_cast<MSoftwareHash*>(this->iHmacImpl->ReplicateL()); |
|
289 } |
|
290 CleanupStack::Pop(); |
|
291 return that; |
|
292 } |
|
293 |
|
294 MHash* CSoftwareHash::CopyL() |
|
295 { |
|
296 CSoftwareHash* that=new(ELeave)CSoftwareHash(); |
|
297 CleanupStack::PushL(that); |
|
298 if (this->iKey) |
|
299 { |
|
300 that->iKey=CKey::NewL(*this->iKey); |
|
301 } |
|
302 that->iOperationMode=this->iOperationMode; |
|
303 that->iHashImpl=static_cast<MSoftwareHash*>(this->iHashImpl->CopyL()); |
|
304 if (this->iHmacImpl) |
|
305 { |
|
306 that->iHmacImpl=static_cast<MSoftwareHash*>(this->iHmacImpl->CopyL()); |
|
307 } |
|
308 CleanupStack::Pop(); |
|
309 return that; |
|
310 } |
|
311 |
|
312 |