|
1 /* |
|
2 * Copyright (c) 2008-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 Mac Implementation |
|
16 * Software Mac Implementation |
|
17 * MAC plugin header |
|
18 * |
|
19 */ |
|
20 |
|
21 |
|
22 /** |
|
23 @file |
|
24 */ |
|
25 #include "macimpl.h" |
|
26 #include "pluginconfig.h" |
|
27 |
|
28 /** |
|
29 * HMAC plugin headers |
|
30 */ |
|
31 #include "md2impl.h" |
|
32 #include "md4impl.h" |
|
33 #include "md5impl.h" |
|
34 #include "sha1impl.h" |
|
35 #include "sha2impl.h" |
|
36 #include "hmacimpl.h" |
|
37 |
|
38 |
|
39 using namespace SoftwareCrypto; |
|
40 using namespace CryptoSpi; |
|
41 |
|
42 |
|
43 CMacImpl* CMacImpl::NewL(const CKey& aKey, const TUid aImplementationId, const CCryptoParams* aAlgorithmParams) |
|
44 { |
|
45 CMacImpl* self = new (ELeave) CMacImpl(); |
|
46 CleanupStack::PushL(self); |
|
47 self->ConstructL(aKey, aImplementationId, aAlgorithmParams); |
|
48 CleanupStack::Pop(); |
|
49 return self; |
|
50 } |
|
51 |
|
52 CMacImpl::CMacImpl() |
|
53 { |
|
54 } |
|
55 |
|
56 void CMacImpl::ConstructL(const CKey& aKey, const TUid aImplementationId, const CCryptoParams* /*aAlgorithmParams*/) |
|
57 { |
|
58 iImplementationUid = aImplementationId; |
|
59 iKey = CryptoSpi::CKey::NewL(aKey); |
|
60 |
|
61 MSoftwareHash* hashImpl = NULL; |
|
62 |
|
63 switch (aImplementationId.iUid) |
|
64 { |
|
65 case KCryptoPluginMacHashMd2: |
|
66 { |
|
67 hashImpl = CMD2Impl::NewL(); |
|
68 CleanupClosePushL(*hashImpl); |
|
69 iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl); |
|
70 iBase = EHashBased; |
|
71 } |
|
72 break; |
|
73 case KCryptoPluginMacHashMd5: |
|
74 { |
|
75 hashImpl = CMD5Impl::NewL(); |
|
76 CleanupClosePushL(*hashImpl); |
|
77 iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl); |
|
78 iBase = EHashBased; |
|
79 } |
|
80 break; |
|
81 case KCryptoPluginMacHashMd4: |
|
82 { |
|
83 hashImpl = CMD4Impl::NewL(); |
|
84 CleanupClosePushL(*hashImpl); |
|
85 iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl); |
|
86 iBase = EHashBased; |
|
87 } |
|
88 break; |
|
89 case KCryptoPluginMacHashSha1: |
|
90 { |
|
91 hashImpl = CSHA1Impl::NewL(); |
|
92 CleanupClosePushL(*hashImpl); |
|
93 iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl); |
|
94 iBase = EHashBased; |
|
95 } |
|
96 break; |
|
97 case KCryptoPluginMacHashSha224: |
|
98 { |
|
99 hashImpl = CSHA2Impl::NewL(KCryptoPluginSha224); |
|
100 CleanupClosePushL(*hashImpl); |
|
101 iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl); |
|
102 iBase = EHashBased; |
|
103 } |
|
104 break; |
|
105 case KCryptoPluginMacHashSha256: |
|
106 { |
|
107 hashImpl = CSHA2Impl::NewL(KCryptoPluginSha256); |
|
108 CleanupClosePushL(*hashImpl); |
|
109 iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl); |
|
110 iBase = EHashBased; |
|
111 } |
|
112 break; |
|
113 case KCryptoPluginMacHashSha384: |
|
114 { |
|
115 hashImpl = CSHA2Impl::NewL(KCryptoPluginSha384); |
|
116 CleanupClosePushL(*hashImpl); |
|
117 iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl); |
|
118 iBase = EHashBased; |
|
119 } |
|
120 break; |
|
121 case KCryptoPluginMacHashSha512: |
|
122 { |
|
123 hashImpl = CSHA2Impl::NewL(KCryptoPluginSha512); |
|
124 CleanupClosePushL(*hashImpl); |
|
125 iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl); |
|
126 iBase = EHashBased; |
|
127 } |
|
128 break; |
|
129 case KCryptoPluginMacAesXcbcMac96: |
|
130 { |
|
131 CSymmetricCipher* symmetricCipher = NULL; |
|
132 CryptoSpi::CSymmetricCipherFactory::CreateSymmetricCipherL(symmetricCipher, |
|
133 CryptoSpi::KAesUid, *iKey, |
|
134 CryptoSpi::KCryptoModeEncryptUid, |
|
135 CryptoSpi::KOperationModeCBCUid, |
|
136 CryptoSpi::KPaddingModeNoneUid, |
|
137 NULL); |
|
138 |
|
139 iCmacImpl= CCMacImpl::NewL(*iKey, symmetricCipher, CryptoSpi::KAlgorithmCipherAesXcbcMac96); |
|
140 iBase = ECipherBased; |
|
141 } |
|
142 break; |
|
143 case KCryptoPluginMacAesXcbcPrf128: |
|
144 { |
|
145 TBuf8<16> tempKey; |
|
146 tempKey.SetLength(16); |
|
147 |
|
148 CryptoSpi::CCryptoParams* keyParams = CryptoSpi::CCryptoParams::NewLC(); |
|
149 keyParams->AddL(tempKey, CryptoSpi::KSymmetricKeyParameterUid); |
|
150 CryptoSpi::CKey* key = CryptoSpi::CKey::NewLC(aKey.KeyProperty(),*keyParams); |
|
151 |
|
152 CSymmetricCipher* symmetricCipher = NULL; |
|
153 CryptoSpi::CSymmetricCipherFactory::CreateSymmetricCipherL(symmetricCipher, |
|
154 CryptoSpi::KAesUid, *key, |
|
155 CryptoSpi::KCryptoModeEncryptUid, |
|
156 CryptoSpi::KOperationModeCBCUid, |
|
157 CryptoSpi::KPaddingModeNoneUid, |
|
158 NULL); |
|
159 CleanupStack::PopAndDestroy(2, keyParams); //key and keyParams |
|
160 |
|
161 iCmacImpl= CCMacImpl::NewL(*iKey, symmetricCipher, CryptoSpi::KAlgorithmCipherAesXcbcPrf128); |
|
162 iBase = ECipherBased; |
|
163 } |
|
164 break; |
|
165 |
|
166 default: |
|
167 User::Leave(KErrNotSupported); |
|
168 } |
|
169 |
|
170 if(iBase == EHashBased) |
|
171 { |
|
172 CleanupStack::PopAndDestroy(hashImpl); |
|
173 } |
|
174 } |
|
175 |
|
176 CMacImpl::~CMacImpl() |
|
177 { |
|
178 delete iKey; |
|
179 if(iHmacImpl) |
|
180 { |
|
181 iHmacImpl->Close(); |
|
182 } |
|
183 delete iCmacImpl; |
|
184 } |
|
185 |
|
186 void CMacImpl::Reset() |
|
187 { |
|
188 if (iBase == EHashBased) |
|
189 { |
|
190 iHmacImpl->Reset(); |
|
191 } |
|
192 else if (iBase == ECipherBased) |
|
193 { |
|
194 iCmacImpl->Reset(); |
|
195 } |
|
196 } |
|
197 |
|
198 void CMacImpl::Close() |
|
199 { |
|
200 delete this; |
|
201 } |
|
202 |
|
203 void CMacImpl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics) |
|
204 { |
|
205 aPluginCharacteristics=NULL; |
|
206 TInt macNum=sizeof(KMacCharacteristics)/sizeof(TMacCharacteristics*); |
|
207 for (TInt i=0;i<macNum;++i) |
|
208 { |
|
209 if (KMacCharacteristics[i]->iMacChar.iImplementationUID==ImplementationUid().iUid) |
|
210 { |
|
211 aPluginCharacteristics = KMacCharacteristics[i]; |
|
212 break; |
|
213 } |
|
214 } |
|
215 } |
|
216 |
|
217 const CExtendedCharacteristics* CMacImpl::GetExtendedCharacteristicsL() |
|
218 { |
|
219 return (iBase == EHashBased) ? iHmacImpl->GetExtendedCharacteristicsL(): iCmacImpl->GetExtendedCharacteristicsL(); |
|
220 } |
|
221 |
|
222 TAny* CMacImpl::GetExtension(TUid aExtensionId) |
|
223 { |
|
224 return (iBase == EHashBased) ? iHmacImpl->GetExtension(aExtensionId): NULL; |
|
225 } |
|
226 |
|
227 TUid CMacImpl::ImplementationUid() const |
|
228 { |
|
229 return iImplementationUid; |
|
230 } |
|
231 |
|
232 TPtrC8 CMacImpl::MacL(const TDesC8& aMessage) |
|
233 { |
|
234 return (iBase == EHashBased) ? iHmacImpl->Hash(aMessage):iCmacImpl->MacL(aMessage); |
|
235 } |
|
236 |
|
237 void CMacImpl::UpdateL(const TDesC8& aMessage) |
|
238 { |
|
239 (iBase == EHashBased) ? iHmacImpl->Update(aMessage):iCmacImpl->UpdateL(aMessage); |
|
240 } |
|
241 |
|
242 TPtrC8 CMacImpl::FinalL(const TDesC8& aMessage) |
|
243 { |
|
244 return (iBase == EHashBased) ? iHmacImpl->Final(aMessage):iCmacImpl->FinalL(aMessage); |
|
245 } |
|
246 |
|
247 void CMacImpl::ReInitialiseAndSetKeyL(const CKey& aKey) |
|
248 { |
|
249 delete iKey; |
|
250 iKey = NULL; |
|
251 iKey = CryptoSpi::CKey::NewL(aKey); |
|
252 |
|
253 if (iBase == EHashBased) |
|
254 { |
|
255 iHmacImpl->SetKeyL(aKey); |
|
256 iHmacImpl->Reset(); |
|
257 } |
|
258 else if (iBase == ECipherBased) |
|
259 { |
|
260 iCmacImpl->ReInitialiseAndSetKeyL(aKey); |
|
261 } |
|
262 } |
|
263 |
|
264 MMac* CMacImpl::ReplicateL() |
|
265 { |
|
266 CMacImpl* that= new(ELeave) CMacImpl(); |
|
267 CleanupStack::PushL(that); |
|
268 that->iImplementationUid = iImplementationUid; |
|
269 that->iBase = iBase; |
|
270 that->iKey=CKey::NewL(*iKey); |
|
271 |
|
272 if(iBase == EHashBased) |
|
273 { |
|
274 that->iHmacImpl=static_cast<CHMacImpl*>(iHmacImpl->ReplicateL()); |
|
275 } |
|
276 else if (iBase == ECipherBased) |
|
277 { |
|
278 that->iCmacImpl= iCmacImpl->ReplicateL(); |
|
279 } |
|
280 CleanupStack::Pop(that); |
|
281 return that; |
|
282 } |
|
283 |
|
284 MMac* CMacImpl::CopyL() |
|
285 { |
|
286 CMacImpl* that= new(ELeave) CMacImpl(); |
|
287 CleanupStack::PushL(that); |
|
288 that->iImplementationUid = iImplementationUid; |
|
289 that->iBase = iBase; |
|
290 that->iKey=CKey::NewL(*iKey); |
|
291 |
|
292 if(iBase == EHashBased) |
|
293 { |
|
294 that->iHmacImpl=static_cast<CHMacImpl*>(iHmacImpl->CopyL()); |
|
295 } |
|
296 else if (iBase == ECipherBased) |
|
297 { |
|
298 that->iCmacImpl= iCmacImpl->CopyL(); |
|
299 } |
|
300 CleanupStack::Pop(that); |
|
301 return that; |
|
302 } |