os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/softwarehashbase.cpp
First public contribution.
2 * Copyright (c) 2007-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 hash base class implementation
16 * software hash base class implementation
25 #include "softwarehashbase.h"
27 #include <cryptospi/hashplugin.h>
28 #include "pluginconfig.h"
37 using namespace SoftwareCrypto;
39 CSoftwareHash* CSoftwareHash::NewL(TUid aAlgorithm, TUid aOperationMode, const CKey* aKey)
41 CSoftwareHash* self=NewLC(aAlgorithm, aOperationMode, aKey);
46 CSoftwareHash* CSoftwareHash::NewL(TUid aAlgorithm)
48 CSoftwareHash* self=NewLC(aAlgorithm, CryptoSpi::KHashModeUid, NULL);
53 CSoftwareHash* CSoftwareHash::NewLC(TUid aAlgorithm, TUid aOperationMode, const CKey* aKey)
55 CSoftwareHash* self=new (ELeave) CSoftwareHash();
56 CleanupStack::PushL(self);
57 self->ConstructL(aAlgorithm, aOperationMode, aKey);
61 CSoftwareHash::CSoftwareHash()
65 CSoftwareHash::~CSoftwareHash()
79 void CSoftwareHash::ConstructL(TUid aAlgorithm, TUid aOperationMode, const CKey* aKey)
82 // Only Hash and Hmac mode are supported.
84 if (aOperationMode!=KHmacModeUid && aOperationMode!=KHashModeUid)
86 User::Leave(KErrNotSupported);
89 //Set the key if there is one
95 switch (aAlgorithm.iUid)
97 case KCryptoPluginMd2:
99 iHashImpl=CMD2Impl::NewL();
103 case KCryptoPluginMd5:
105 iHashImpl=CMD5Impl::NewL();
109 case KCryptoPluginMd4:
111 iHashImpl=CMD4Impl::NewL();
115 case KCryptoPluginSha1:
117 iHashImpl=CSHA1Impl::NewL();
121 case KCryptoPluginSha224:
122 case KCryptoPluginSha256:
123 case KCryptoPluginSha384:
124 case KCryptoPluginSha512:
126 iHashImpl=CSHA2Impl::NewL(aAlgorithm.iUid);
131 User::Leave(KErrNotSupported);
134 SetOperationModeL(aOperationMode);
137 void CSoftwareHash::SetOperationModeL(TUid aOperationMode)
139 switch (aOperationMode.iUid)
144 //Only create hmac implementation if there isn't one
150 iHmacImpl=CHMacImpl::NewL(*iKey, iHashImpl);
154 iHmacImpl=CHMacImpl::NewL(iHashImpl);
167 User::Leave(KErrNotSupported);
171 // Set the operation mode.
173 iOperationMode=aOperationMode;
176 MSoftwareHash* CSoftwareHash::Impl()
178 MSoftwareHash* impl=NULL;
179 if (iOperationMode==KHashModeUid)
183 else if (iOperationMode==KHmacModeUid && iKey)
190 void CSoftwareHash::SetKeyL(const CKey& aKey)
194 iKey=CKey::NewL(aKey);
197 iHmacImpl->SetKeyL(aKey);
201 void CSoftwareHash::Reset()
214 void CSoftwareHash::Close()
219 void CSoftwareHash::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
221 MSoftwareHash* impl=Impl();
224 impl->GetCharacteristicsL(aPluginCharacteristics);
228 User::Leave(KErrNotReady);
232 const CExtendedCharacteristics* CSoftwareHash::GetExtendedCharacteristicsL()
234 MSoftwareHash* impl=Impl();
237 User::Leave(KErrNotReady);
239 return impl->GetExtendedCharacteristicsL();
242 TAny* CSoftwareHash::GetExtension(TUid aExtensionId)
244 MSoftwareHash* impl=Impl();
247 return impl->GetExtension(aExtensionId);
255 TPtrC8 CSoftwareHash::Hash(const TDesC8& aMessage)
257 MSoftwareHash* impl=Impl();
260 return impl->Hash(aMessage);
268 void CSoftwareHash::Update(const TDesC8& aMessage)
270 MSoftwareHash* impl=Impl();
273 return impl->Update(aMessage);
277 TPtrC8 CSoftwareHash::Final(const TDesC8& aMessage)
279 MSoftwareHash* impl=Impl();
282 return impl->Final(aMessage);
290 MHash* CSoftwareHash::ReplicateL()
292 CSoftwareHash* that=new(ELeave)CSoftwareHash();
293 CleanupStack::PushL(that);
296 that->iKey=CKey::NewL(*this->iKey);
298 that->iOperationMode=this->iOperationMode;
299 that->iHashImpl=static_cast<MSoftwareHash*>(this->iHashImpl->ReplicateL());
302 that->iHmacImpl=static_cast<MSoftwareHash*>(this->iHmacImpl->ReplicateL());
308 MHash* CSoftwareHash::CopyL()
310 CSoftwareHash* that=new(ELeave)CSoftwareHash();
311 CleanupStack::PushL(that);
314 that->iKey=CKey::NewL(*this->iKey);
316 that->iOperationMode=this->iOperationMode;
317 that->iHashImpl=static_cast<MSoftwareHash*>(this->iHashImpl->CopyL());
320 that->iHmacImpl=static_cast<MSoftwareHash*>(this->iHmacImpl->CopyL());