First public contribution.
2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Software Mac Implementation
16 * Software Mac Implementation
26 #include "pluginconfig.h"
39 using namespace SoftwareCrypto;
40 using namespace CryptoSpi;
43 CMacImpl* CMacImpl::NewL(const CKey& aKey, const TUid aImplementationId, const CCryptoParams* aAlgorithmParams)
45 CMacImpl* self = new (ELeave) CMacImpl();
46 CleanupStack::PushL(self);
47 self->ConstructL(aKey, aImplementationId, aAlgorithmParams);
56 void CMacImpl::ConstructL(const CKey& aKey, const TUid aImplementationId, const CCryptoParams* /*aAlgorithmParams*/)
58 iImplementationUid = aImplementationId;
59 iKey = CryptoSpi::CKey::NewL(aKey);
61 MSoftwareHash* hashImpl = NULL;
63 switch (aImplementationId.iUid)
65 case KCryptoPluginMacHashMd2:
67 hashImpl = CMD2Impl::NewL();
68 CleanupClosePushL(*hashImpl);
69 iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
73 case KCryptoPluginMacHashMd5:
75 hashImpl = CMD5Impl::NewL();
76 CleanupClosePushL(*hashImpl);
77 iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
81 case KCryptoPluginMacHashMd4:
83 hashImpl = CMD4Impl::NewL();
84 CleanupClosePushL(*hashImpl);
85 iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
89 case KCryptoPluginMacHashSha1:
91 hashImpl = CSHA1Impl::NewL();
92 CleanupClosePushL(*hashImpl);
93 iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
97 case KCryptoPluginMacHashSha224:
99 hashImpl = CSHA2Impl::NewL(KCryptoPluginSha224);
100 CleanupClosePushL(*hashImpl);
101 iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
105 case KCryptoPluginMacHashSha256:
107 hashImpl = CSHA2Impl::NewL(KCryptoPluginSha256);
108 CleanupClosePushL(*hashImpl);
109 iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
113 case KCryptoPluginMacHashSha384:
115 hashImpl = CSHA2Impl::NewL(KCryptoPluginSha384);
116 CleanupClosePushL(*hashImpl);
117 iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
121 case KCryptoPluginMacHashSha512:
123 hashImpl = CSHA2Impl::NewL(KCryptoPluginSha512);
124 CleanupClosePushL(*hashImpl);
125 iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
129 case KCryptoPluginMacAesXcbcMac96:
131 CSymmetricCipher* symmetricCipher = NULL;
132 CryptoSpi::CSymmetricCipherFactory::CreateSymmetricCipherL(symmetricCipher,
133 CryptoSpi::KAesUid, *iKey,
134 CryptoSpi::KCryptoModeEncryptUid,
135 CryptoSpi::KOperationModeCBCUid,
136 CryptoSpi::KPaddingModeNoneUid,
139 iCmacImpl= CCMacImpl::NewL(*iKey, symmetricCipher, CryptoSpi::KAlgorithmCipherAesXcbcMac96);
140 iBase = ECipherBased;
143 case KCryptoPluginMacAesXcbcPrf128:
146 tempKey.SetLength(16);
148 CryptoSpi::CCryptoParams* keyParams = CryptoSpi::CCryptoParams::NewLC();
149 keyParams->AddL(tempKey, CryptoSpi::KSymmetricKeyParameterUid);
150 CryptoSpi::CKey* key = CryptoSpi::CKey::NewLC(aKey.KeyProperty(),*keyParams);
152 CSymmetricCipher* symmetricCipher = NULL;
153 CryptoSpi::CSymmetricCipherFactory::CreateSymmetricCipherL(symmetricCipher,
154 CryptoSpi::KAesUid, *key,
155 CryptoSpi::KCryptoModeEncryptUid,
156 CryptoSpi::KOperationModeCBCUid,
157 CryptoSpi::KPaddingModeNoneUid,
159 CleanupStack::PopAndDestroy(2, keyParams); //key and keyParams
161 iCmacImpl= CCMacImpl::NewL(*iKey, symmetricCipher, CryptoSpi::KAlgorithmCipherAesXcbcPrf128);
162 iBase = ECipherBased;
167 User::Leave(KErrNotSupported);
170 if(iBase == EHashBased)
172 CleanupStack::PopAndDestroy(hashImpl);
176 CMacImpl::~CMacImpl()
186 void CMacImpl::Reset()
188 if (iBase == EHashBased)
192 else if (iBase == ECipherBased)
198 void CMacImpl::Close()
203 void CMacImpl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
205 aPluginCharacteristics=NULL;
206 TInt macNum=sizeof(KMacCharacteristics)/sizeof(TMacCharacteristics*);
207 for (TInt i=0;i<macNum;++i)
209 if (KMacCharacteristics[i]->iMacChar.iImplementationUID==ImplementationUid().iUid)
211 aPluginCharacteristics = KMacCharacteristics[i];
217 const CExtendedCharacteristics* CMacImpl::GetExtendedCharacteristicsL()
219 return (iBase == EHashBased) ? iHmacImpl->GetExtendedCharacteristicsL(): iCmacImpl->GetExtendedCharacteristicsL();
222 TAny* CMacImpl::GetExtension(TUid aExtensionId)
224 return (iBase == EHashBased) ? iHmacImpl->GetExtension(aExtensionId): NULL;
227 TUid CMacImpl::ImplementationUid() const
229 return iImplementationUid;
232 TPtrC8 CMacImpl::MacL(const TDesC8& aMessage)
234 return (iBase == EHashBased) ? iHmacImpl->Hash(aMessage):iCmacImpl->MacL(aMessage);
237 void CMacImpl::UpdateL(const TDesC8& aMessage)
239 (iBase == EHashBased) ? iHmacImpl->Update(aMessage):iCmacImpl->UpdateL(aMessage);
242 TPtrC8 CMacImpl::FinalL(const TDesC8& aMessage)
244 return (iBase == EHashBased) ? iHmacImpl->Final(aMessage):iCmacImpl->FinalL(aMessage);
247 void CMacImpl::ReInitialiseAndSetKeyL(const CKey& aKey)
251 iKey = CryptoSpi::CKey::NewL(aKey);
253 if (iBase == EHashBased)
255 iHmacImpl->SetKeyL(aKey);
258 else if (iBase == ECipherBased)
260 iCmacImpl->ReInitialiseAndSetKeyL(aKey);
264 MMac* CMacImpl::ReplicateL()
266 CMacImpl* that= new(ELeave) CMacImpl();
267 CleanupStack::PushL(that);
268 that->iImplementationUid = iImplementationUid;
270 that->iKey=CKey::NewL(*iKey);
272 if(iBase == EHashBased)
274 that->iHmacImpl=static_cast<CHMacImpl*>(iHmacImpl->ReplicateL());
276 else if (iBase == ECipherBased)
278 that->iCmacImpl= iCmacImpl->ReplicateL();
280 CleanupStack::Pop(that);
284 MMac* CMacImpl::CopyL()
286 CMacImpl* that= new(ELeave) CMacImpl();
287 CleanupStack::PushL(that);
288 that->iImplementationUid = iImplementationUid;
290 that->iKey=CKey::NewL(*iKey);
292 if(iBase == EHashBased)
294 that->iHmacImpl=static_cast<CHMacImpl*>(iHmacImpl->CopyL());
296 else if (iBase == ECipherBased)
298 that->iCmacImpl= iCmacImpl->CopyL();
300 CleanupStack::Pop(that);