Update contrib.
2 * Copyright (c) 2006-2010 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 * hash shim implementation
16 * hash shim implementation
26 #include <cryptospi/cryptospidef.h>
27 #include <cryptospi/plugincharacteristics.h>
28 #include <cryptospi/keys.h>
31 using namespace CryptoSpi;
34 // MD2 shim implementation
36 CMD2Shim* CMD2Shim::NewL()
38 CMD2Shim* self=CMD2Shim::NewLC();
43 CMD2Shim* CMD2Shim::NewLC()
45 CMD2Shim* self=new(ELeave) CMD2Shim();
46 CleanupStack::PushL(self);
51 TInt CMD2Shim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/)
53 TInt ret(KErrNotSupported);
55 if (KHashInterfaceUid.iUid==aExtensionId && iHashImpl)
74 void CMD2Shim::ConstructL()
76 CHashFactory::CreateHashL(iHashImpl,
82 CMessageDigest* CMD2Shim::CopyL()
84 CMD2Shim* copy=new(ELeave) CMD2Shim();
85 CleanupStack::PushL(copy);
86 copy->iHashImpl=iHashImpl->CopyL();
91 CMessageDigest* CMD2Shim::ReplicateL()
93 CMD2Shim* copy=new(ELeave) CMD2Shim();
94 CleanupStack::PushL(copy);
95 copy->iHashImpl=iHashImpl->ReplicateL();
101 TInt CMD2Shim::BlockSize()
103 const TCharacteristics* ptr(NULL);
104 TRAPD(err, iHashImpl->GetCharacteristicsL(ptr);)
108 return ((err>0)? KErrGeneral : err);
110 const THashCharacteristics* hashPtr=static_cast<const THashCharacteristics*>(ptr);
111 return hashPtr->iBlockSize/8;
114 TInt CMD2Shim::HashSize()
116 const TCharacteristics* ptr(NULL);
117 TRAPD(err, iHashImpl->GetCharacteristicsL(ptr);)
120 return ((err>0)? KErrGeneral : err);
122 const THashCharacteristics* hashPtr=static_cast<const THashCharacteristics*>(ptr);
123 return hashPtr->iOutputSize/8;
126 void CMD2Shim::Update(const TDesC8& aMessage)
128 iHashImpl->Update(aMessage);
132 TPtrC8 CMD2Shim::Final(const TDesC8& aMessage)
134 return iHashImpl->Final(aMessage);
137 TPtrC8 CMD2Shim::Final()
139 TPtrC8 ptr(KNullDesC8());
140 return iHashImpl->Final(ptr);
143 void CMD2Shim::Reset()
149 TPtrC8 CMD2Shim::Hash(const TDesC8& aMessage)
151 return iHashImpl->Hash(aMessage);
155 // Implementation of MD5 shim
157 CMD5Shim* CMD5Shim::NewL()
159 CMD5Shim* self=CMD5Shim::NewLC();
164 CMD5Shim* CMD5Shim::NewLC()
167 CMD5Shim* self=new(ELeave) CMD5Shim();
168 CleanupStack::PushL(self);
178 TInt CMD5Shim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/)
180 TInt ret(KErrNotSupported);
182 if (KHashInterfaceUid.iUid==aExtensionId && iHashImpl)
191 CMD5Shim::~CMD5Shim()
197 void CMD5Shim::ConstructL()
199 CHashFactory::CreateHashL(iHashImpl,
206 CMessageDigest* CMD5Shim::CopyL()
208 CMD5Shim* copy=new(ELeave) CMD5Shim();
209 CleanupStack::PushL(copy);
210 copy->iHashImpl=iHashImpl->CopyL();
215 CMessageDigest* CMD5Shim::ReplicateL()
217 CMD5Shim* copy=new(ELeave) CMD5Shim();
218 CleanupStack::PushL(copy);
219 copy->iHashImpl=iHashImpl->ReplicateL();
224 TInt CMD5Shim::BlockSize()
226 const TCharacteristics* ptr(NULL);
227 TRAPD(err, iHashImpl->GetCharacteristicsL(ptr);)
230 return ((err>0)? KErrGeneral : err);
232 const THashCharacteristics* hashPtr=static_cast<const THashCharacteristics*>(ptr);
233 return hashPtr->iBlockSize/8;
236 TInt CMD5Shim::HashSize()
238 const TCharacteristics* ptr(NULL);
239 TRAPD(err, iHashImpl->GetCharacteristicsL(ptr);)
242 return ((err>0)? KErrGeneral : err);
244 const THashCharacteristics* hashPtr=static_cast<const THashCharacteristics*>(ptr);
245 return hashPtr->iOutputSize/8;
248 void CMD5Shim::Update(const TDesC8& aMessage)
250 iHashImpl->Update(aMessage);
253 TPtrC8 CMD5Shim::Final(const TDesC8& aMessage)
255 return iHashImpl->Final(aMessage);
258 TPtrC8 CMD5Shim::Final()
260 TPtrC8 ptr(KNullDesC8());
261 return iHashImpl->Final(ptr);
264 void CMD5Shim::Reset()
270 TPtrC8 CMD5Shim::Hash(const TDesC8& aMessage)
272 return iHashImpl->Hash(aMessage);
276 // Implementation of SHA1 shim
279 CSHA1Shim* CSHA1Shim::NewL()
281 CSHA1Shim* self=CSHA1Shim::NewLC();
286 CSHA1Shim* CSHA1Shim::NewLC()
289 CSHA1Shim* self=new(ELeave) CSHA1Shim();
290 CleanupStack::PushL(self);
296 CSHA1Shim::CSHA1Shim()
301 TInt CSHA1Shim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/)
303 TInt ret(KErrNotSupported);
305 if (KHashInterfaceUid.iUid==aExtensionId && iHashImpl)
314 CSHA1Shim::~CSHA1Shim()
320 void CSHA1Shim::ConstructL()
322 CHashFactory::CreateHashL(iHashImpl,
329 CMessageDigest* CSHA1Shim::CopyL()
331 CSHA1Shim* copy=new(ELeave) CSHA1Shim();
332 CleanupStack::PushL(copy);
333 copy->iHashImpl=iHashImpl->CopyL();
338 CMessageDigest* CSHA1Shim::ReplicateL()
340 CSHA1Shim* copy=new(ELeave) CSHA1Shim();
341 CleanupStack::PushL(copy);
342 copy->iHashImpl=iHashImpl->ReplicateL();
347 TInt CSHA1Shim::BlockSize()
349 const TCharacteristics* ptr(NULL);
350 TRAPD(err, iHashImpl->GetCharacteristicsL(ptr);)
353 return ((err>0)? KErrGeneral : err);
355 const THashCharacteristics* hashPtr=static_cast<const THashCharacteristics*>(ptr);
356 return hashPtr->iBlockSize/8;
359 TInt CSHA1Shim::HashSize()
361 const TCharacteristics* ptr(NULL);
362 TRAPD(err, iHashImpl->GetCharacteristicsL(ptr);)
365 return ((err>0)? KErrGeneral : err);
367 const THashCharacteristics* hashPtr=static_cast<const THashCharacteristics*>(ptr);
368 return hashPtr->iOutputSize/8;
371 void CSHA1Shim::Update(const TDesC8& aMessage)
373 iHashImpl->Update(aMessage);
376 TPtrC8 CSHA1Shim::Final(const TDesC8& aMessage)
378 return iHashImpl->Final(aMessage);
381 TPtrC8 CSHA1Shim::Final()
383 TPtrC8 ptr(KNullDesC8());
384 return iHashImpl->Final(ptr);
387 void CSHA1Shim::Reset()
392 TPtrC8 CSHA1Shim::Hash(const TDesC8& aMessage)
394 return iHashImpl->Hash(aMessage);
399 // Implementation of SHA2 shim
402 CSHA2Shim* CSHA2Shim::NewL(TSH2Algo aAlgorithmId)
404 CSHA2Shim* self=CSHA2Shim::NewLC(aAlgorithmId);
409 CSHA2Shim* CSHA2Shim::NewLC(TSH2Algo aAlgorithmId)
412 CSHA2Shim* self=new(ELeave) CSHA2Shim();
413 CleanupStack::PushL(self);
414 self->ConstructL(aAlgorithmId);
418 CSHA2Shim::CSHA2Shim()
422 TInt CSHA2Shim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/)
424 TInt ret(KErrNotSupported);
426 if (KHashInterfaceUid.iUid==aExtensionId && iHashImpl)
434 CSHA2Shim::~CSHA2Shim()
439 void CSHA2Shim::ConstructL(TSH2Algo aAlgorithmId)
465 User::Leave(KErrNotSupported);
468 CHashFactory::CreateHashL(iHashImpl,
475 CMessageDigest* CSHA2Shim::CopyL()
477 CSHA2Shim* copy=new(ELeave) CSHA2Shim();
478 CleanupStack::PushL(copy);
479 copy->iHashImpl=iHashImpl->CopyL();
484 CMessageDigest* CSHA2Shim::ReplicateL()
486 CSHA2Shim* copy=new(ELeave) CSHA2Shim();
487 CleanupStack::PushL(copy);
488 copy->iHashImpl=iHashImpl->ReplicateL();
493 TInt CSHA2Shim::BlockSize()
495 const TCharacteristics* ptr(NULL);
496 TRAPD(err, iHashImpl->GetCharacteristicsL(ptr);)
499 return ((err>0)? KErrGeneral : err);
501 const THashCharacteristics* hashPtr=static_cast<const THashCharacteristics*>(ptr);
502 return hashPtr->iBlockSize/8;
505 TInt CSHA2Shim::HashSize()
507 const TCharacteristics* ptr(NULL);
508 TRAPD(err, iHashImpl->GetCharacteristicsL(ptr);)
511 return ((err>0)? KErrGeneral : err);
513 const THashCharacteristics* hashPtr=static_cast<const THashCharacteristics*>(ptr);
514 return hashPtr->iOutputSize/8;
517 void CSHA2Shim::Update(const TDesC8& aMessage)
519 iHashImpl->Update(aMessage);
522 TPtrC8 CSHA2Shim::Final(const TDesC8& aMessage)
524 return iHashImpl->Final(aMessage);
527 TPtrC8 CSHA2Shim::Final()
529 TPtrC8 ptr(KNullDesC8());
530 return iHashImpl->Final(ptr);
533 void CSHA2Shim::Reset()
538 TPtrC8 CSHA2Shim::Hash(const TDesC8& aMessage)
540 return iHashImpl->Hash(aMessage);
544 // Implementation of HMAC shim
547 CHMACShim* CHMACShim::NewL(const TDesC8& aKey, CMessageDigest* aDigest)
551 // Check whether the hash contains an SPI plug-in
553 TInt err = aDigest->GetExtension(CryptoSpi::KHashInterface, implPtr, NULL);
554 if (err == KErrNone && implPtr)
556 CryptoSpi::CHash* impl(static_cast<CryptoSpi::CHash*>(implPtr));
557 const CryptoSpi::TCharacteristics* c(0);
558 impl->GetCharacteristicsL(c);
559 const CryptoSpi::THashCharacteristics* hashCharacteristics(static_cast<const CryptoSpi::THashCharacteristics*>(c));
562 // Verify that the plug-in supports Hmac mode
563 if (hashCharacteristics->IsOperationModeSupported(CryptoSpi::KHmacModeUid))
566 #ifndef SYMBIAN_SDP_IPSEC_VOIP_SUPPORT
568 CHash* myHash=impl->ReplicateL();
569 CleanupStack::PushL(myHash);
572 TKeyProperty keyProperty = {KHmacModeUid, KNullUid, KSymmetricKey, KNonEmbeddedKeyUid};
573 CCryptoParams* keyParam =CCryptoParams::NewLC();
574 keyParam->AddL(aKey, KHmacKeyParameterUid);
575 CKey* uniKey= CKey::NewLC(keyProperty, *keyParam);
576 #ifndef SYMBIAN_SDP_IPSEC_VOIP_SUPPORT
577 myHash->SetKeyL(*uniKey);
578 CleanupStack::PopAndDestroy(2, keyParam);
579 // Set hash to Hmac mode
580 myHash->SetOperationModeL(CryptoSpi::KHmacModeUid);
581 self = new(ELeave) CHMACShim(myHash, aDigest);
582 CleanupStack::Pop(myHash);
584 TUid algorithmUID = algorithmUID.Uid(hashCharacteristics->cmn.iAlgorithmUID);
585 //Create a pointer for the Mac Implementation Object
586 CMac* macImpl = NULL;
587 CMacFactory::CreateMacL(macImpl,algorithmUID,*uniKey, NULL);
588 CleanupStack::PushL(macImpl);
589 self = new(ELeave) CHMACShim(macImpl, aDigest,uniKey, algorithmUID);
590 CleanupStack::Pop(macImpl);
591 CleanupStack::Pop(uniKey);
592 CleanupStack::PopAndDestroy(keyParam);
599 #ifdef SYMBIAN_SDP_IPSEC_VOIP_SUPPORT
601 CHMACShim::CHMACShim(CryptoSpi::CMac* aImpl, CMessageDigest* aDigest, CryptoSpi::CKey* aKey, TUid aAlgorithmUid)
602 : CHMAC(aDigest), iMacImpl(aImpl), iKey(aKey), iAlgorithmUid(aAlgorithmUid)
608 CHMACShim::CHMACShim(CryptoSpi::CHash* aImpl, CMessageDigest* aDigest)
609 : CHMAC(aDigest), iMacImpl(aImpl)
615 CHMACShim::CHMACShim()
619 CHMACShim::~CHMACShim()
622 #ifdef SYMBIAN_SDP_IPSEC_VOIP_SUPPORT
627 TInt CHMACShim::BlockSize()
629 const TCharacteristics* ptr(NULL);
630 TRAPD(err, iMacImpl->GetCharacteristicsL(ptr));
634 return ((err>0)? KErrGeneral : err);
637 #ifndef SYMBIAN_SDP_IPSEC_VOIP_SUPPORT
638 const THashCharacteristics* hashPtr=static_cast<const THashCharacteristics*>(ptr);
639 return hashPtr->iBlockSize/8;
641 const TMacCharacteristics* macPtr=static_cast<const TMacCharacteristics*>(ptr);
642 return macPtr->iHashAlgorithmChar->iBlockSize/8;
646 TInt CHMACShim::HashSize()
648 const TCharacteristics* ptr(NULL);
649 TRAPD(err, iMacImpl->GetCharacteristicsL(ptr));
652 return ((err>0)? KErrGeneral : err);
655 #ifndef SYMBIAN_SDP_IPSEC_VOIP_SUPPORT
656 const THashCharacteristics* hashPtr=static_cast<const THashCharacteristics*>(ptr);
657 return hashPtr->iOutputSize/8;
659 const TMacCharacteristics* macPtr=static_cast<const TMacCharacteristics*>(ptr);
660 return macPtr->iHashAlgorithmChar->iOutputSize/8;
664 void CHMACShim::Update(const TDesC8& aMessage)
666 #ifdef SYMBIAN_SDP_IPSEC_VOIP_SUPPORT
667 // The error is ignored as the legacy code methods are non-leaving and they call
668 // the new MAC interfaces which uses leaving methods for processing MAC value.
669 // This call always call the non-leaving legacy method.
670 TRAP_IGNORE(iMacImpl->UpdateL(aMessage));
672 iMacImpl->Update(aMessage);
676 TPtrC8 CHMACShim::Final(const TDesC8& aMessage)
678 #ifdef SYMBIAN_SDP_IPSEC_VOIP_SUPPORT
679 TPtrC8 macPtr(KNullDesC8());
680 // The error is ignored as the legacy code methods are non-leaving and they call
681 // the new MAC interfaces which uses leaving methods for processing MAC value.
682 // This call always call the non-leaving legacy method.
683 TRAP_IGNORE(macPtr.Set(iMacImpl->FinalL(aMessage)));
686 return iMacImpl->Final(aMessage);
690 TPtrC8 CHMACShim::Final()
692 TPtrC8 ptr(KNullDesC8());
693 #ifdef SYMBIAN_SDP_IPSEC_VOIP_SUPPORT
694 TPtrC8 macPtr(KNullDesC8());
695 // The error is ignored as the legacy code methods are non-leaving and they call
696 // the new MAC interfaces which uses leaving methods for processing MAC value.
697 // This call always call the non-leaving legacy method.
698 TRAP_IGNORE(macPtr.Set(iMacImpl->FinalL(ptr)));
701 return iMacImpl->Final(ptr);
705 void CHMACShim::Reset()
710 TPtrC8 CHMACShim::Hash(const TDesC8& aMessage)
712 #ifdef SYMBIAN_SDP_IPSEC_VOIP_SUPPORT
713 TPtrC8 macPtr(KNullDesC8());
714 // The error is ignored as the legacy code methods are non-leaving and they call
715 // the new MAC interfaces which uses leaving methods for processing MAC value.
716 // This call always call the non-leaving legacy method.
717 TRAP_IGNORE(macPtr.Set(iMacImpl->MacL(aMessage)));
720 return iMacImpl->Hash(aMessage);
724 CMessageDigest* CHMACShim::ReplicateL()
726 CHMACShim* copy=new(ELeave) CHMACShim();
727 CleanupStack::PushL(copy);
728 copy->iMacImpl=iMacImpl->ReplicateL();
729 CleanupStack::Pop(copy);
733 CMessageDigest* CHMACShim::CopyL()
735 CHMACShim* copy=new(ELeave) CHMACShim();
736 CleanupStack::PushL(copy);
737 copy->iMacImpl=iMacImpl->CopyL();
738 CleanupStack::Pop(copy);
743 // Implemetation of MD4 shim
745 CMD4Shim* CMD4Shim::NewL()
747 CMD4Shim* self=CMD4Shim::NewLC();
752 CMD4Shim* CMD4Shim::NewLC()
754 CMD4Shim* self=new(ELeave) CMD4Shim();
755 CleanupStack::PushL(self);
764 TInt CMD4Shim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/)
766 TInt ret(KErrNotSupported);
768 if (KHashInterfaceUid.iUid==aExtensionId && iHashImpl)
777 CMD4Shim::~CMD4Shim()
783 void CMD4Shim::ConstructL()
785 CHashFactory::CreateHashL(iHashImpl,KMd4Uid,KHashModeUid,NULL,NULL);
788 CMessageDigest* CMD4Shim::CopyL()
790 CMD4Shim* copy=new(ELeave) CMD4Shim();
791 CleanupStack::PushL(copy);
792 copy->iHashImpl=iHashImpl->CopyL();
797 CMessageDigest* CMD4Shim::ReplicateL()
799 CMD4Shim* copy=new(ELeave) CMD4Shim();
800 CleanupStack::PushL(copy);
801 copy->iHashImpl=iHashImpl->ReplicateL();
806 TInt CMD4Shim::BlockSize()
808 const TCharacteristics* ptr(NULL);
809 TRAPD(err, iHashImpl->GetCharacteristicsL(ptr);)
812 return ((err>0)? KErrGeneral : err);
814 const THashCharacteristics* hashPtr=static_cast<const THashCharacteristics*>(ptr);
815 return hashPtr->iBlockSize/8;
818 TInt CMD4Shim::HashSize()
820 const TCharacteristics* ptr(NULL);
821 TRAPD(err, iHashImpl->GetCharacteristicsL(ptr);)
824 return ((err>0)? KErrGeneral : err);
826 const THashCharacteristics* hashPtr=static_cast<const THashCharacteristics*>(ptr);
827 return hashPtr->iOutputSize/8;
830 void CMD4Shim::Update(const TDesC8& aMessage)
832 iHashImpl->Update(aMessage);
835 TPtrC8 CMD4Shim::Final(const TDesC8& aMessage)
837 return iHashImpl->Final(aMessage);
840 TPtrC8 CMD4Shim::Final()
842 TPtrC8 ptr(KNullDesC8());
843 return iHashImpl->Final(ptr);
846 void CMD4Shim::Reset()
852 TPtrC8 CMD4Shim::Hash(const TDesC8& aMessage)
854 return iHashImpl->Hash(aMessage);