crypto/weakcryptospi/test/tplugins/src/tplugin02/macimpl.cpp
changeset 8 35751d3474b7
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     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 *
       
    18 */
       
    19 
       
    20 
       
    21 /**
       
    22  @file
       
    23 */
       
    24 
       
    25 
       
    26 // MAC plugin header
       
    27 #include "macimpl.h"
       
    28 #include "pluginconfig.h"
       
    29 
       
    30 // headers from cryptospi framework
       
    31 #include <cryptospi/cryptospidef.h>
       
    32 
       
    33 #include "hmacimpl.h"
       
    34 
       
    35 
       
    36 using namespace SoftwareCrypto;
       
    37 
       
    38 
       
    39 CMacImpl* CMacImpl::NewL(const CKey& aKey, const TUid aImplementationId, const CCryptoParams* aAlgorithmParams)
       
    40 	{
       
    41 	CMacImpl* self = new (ELeave) CMacImpl();
       
    42 	CleanupStack::PushL(self);
       
    43 	self->ConstructL(aKey, aImplementationId, aAlgorithmParams);
       
    44 	CleanupStack::Pop();
       
    45 	return self;
       
    46 	}
       
    47 
       
    48 CMacImpl::CMacImpl() 
       
    49 	{
       
    50 	}
       
    51 
       
    52 void CMacImpl::ConstructL(const CKey& aKey, const TUid aImplementationId, const CCryptoParams* /*aAlgorithmParams*/) 
       
    53 	{
       
    54 	iImplementationUid = aImplementationId; 
       
    55 	iKey = CryptoSpi::CKey::NewL(aKey);
       
    56 		
       
    57 	MSoftwareHash* hashImpl = NULL;
       
    58 	
       
    59 	switch (aImplementationId.iUid)
       
    60 		{
       
    61 		case KTestPlugin02XcbcMac96:
       
    62 	    	{
       
    63 			CSymmetricCipher* symmetricCipher = NULL;
       
    64 			CryptoSpi::CSymmetricCipherFactory::CreateSymmetricCipherL(symmetricCipher,
       
    65 														CryptoSpi::KAesUid, *iKey,
       
    66 														CryptoSpi::KCryptoModeEncryptUid,
       
    67 														CryptoSpi::KOperationModeCBCUid,
       
    68 														CryptoSpi::KPaddingModeNoneUid,
       
    69 														NULL);
       
    70 			
       
    71 			iCmacImpl= CCMacImpl::NewL(*iKey, symmetricCipher, CryptoSpi::KAlgorithmCipherAesXcbcMac96);
       
    72 			iBase = ECipherBased;
       
    73 			}
       
    74 			break;
       
    75 			
       
    76 	    case KTestPlugin02XcbcPrf128:
       
    77 	    	{
       
    78 			TBuf8<16> tempKey;
       
    79 			tempKey.SetLength(16);
       
    80 			
       
    81 			CryptoSpi::CCryptoParams* keyParams = CryptoSpi::CCryptoParams::NewLC();
       
    82 			keyParams->AddL(tempKey, CryptoSpi::KSymmetricKeyParameterUid);
       
    83 			CryptoSpi::CKey* key = CryptoSpi::CKey::NewLC(aKey.KeyProperty(),*keyParams);
       
    84 				
       
    85 			CSymmetricCipher* symmetricCipher = NULL;
       
    86 			CryptoSpi::CSymmetricCipherFactory::CreateSymmetricCipherL(symmetricCipher,
       
    87 														CryptoSpi::KAesUid, *key,
       
    88 														CryptoSpi::KCryptoModeEncryptUid,
       
    89 														CryptoSpi::KOperationModeCBCUid,
       
    90 														CryptoSpi::KPaddingModeNoneUid,
       
    91 														NULL);
       
    92 			CleanupStack::PopAndDestroy(2, keyParams); //key and keyParams
       
    93 			
       
    94 			iCmacImpl= CCMacImpl::NewL(*iKey, symmetricCipher, CryptoSpi::KAlgorithmCipherAesXcbcPrf128);
       
    95 			iBase = ECipherBased;
       
    96 			}
       
    97 			break;
       
    98 			
       
    99 		default:
       
   100 			{
       
   101 			User::Leave(KErrNotSupported);
       
   102 			}
       
   103 		}
       
   104 
       
   105 	if(iBase == EHashBased)
       
   106 		{
       
   107 		CleanupStack::PopAndDestroy(hashImpl);
       
   108 		}
       
   109 	}
       
   110 
       
   111 CMacImpl::~CMacImpl()
       
   112 	{
       
   113 	delete iKey;
       
   114 	if(iHmacImpl)
       
   115 		{
       
   116 		iHmacImpl->Close();
       
   117 		}
       
   118 	delete iCmacImpl;
       
   119 	}
       
   120 	
       
   121 void CMacImpl::Reset()
       
   122 	{
       
   123 	if (iBase == EHashBased)
       
   124 		{
       
   125 		iHmacImpl->Reset();
       
   126 		}
       
   127 	else if (iBase == ECipherBased)
       
   128 		{
       
   129 		iCmacImpl->Reset();
       
   130 		}
       
   131 	}
       
   132 	
       
   133 void CMacImpl::Close()
       
   134 	{
       
   135 	delete this;	
       
   136 	}
       
   137 	
       
   138 void CMacImpl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
       
   139 	{
       
   140 	aPluginCharacteristics=NULL;
       
   141 	TInt macNum=sizeof(KMacCharacteristics)/sizeof(TMacCharacteristics*);
       
   142 	for (TInt i=0;i<macNum;++i)
       
   143 		{
       
   144 		if (KMacCharacteristics[i]->iMacChar.iImplementationUID==ImplementationUid().iUid)
       
   145 			{
       
   146 			aPluginCharacteristics = KMacCharacteristics[i];
       
   147 			break;
       
   148 			}
       
   149 		}	
       
   150 	}
       
   151 
       
   152 const CExtendedCharacteristics* CMacImpl::GetExtendedCharacteristicsL()
       
   153 	{
       
   154 	return (iBase == EHashBased) ? iHmacImpl->GetExtendedCharacteristicsL(): iCmacImpl->GetExtendedCharacteristicsL();
       
   155 	}
       
   156 
       
   157 TAny* CMacImpl::GetExtension(TUid aExtensionId)
       
   158 	{
       
   159 	return (iBase == EHashBased) ? iHmacImpl->GetExtension(aExtensionId): NULL;
       
   160 	}
       
   161 
       
   162 TUid CMacImpl::ImplementationUid() const
       
   163 	{
       
   164 	return iImplementationUid;
       
   165 	}
       
   166 
       
   167 TPtrC8 CMacImpl::MacL(const TDesC8& aMessage)
       
   168 	{
       
   169 	return (iBase == EHashBased) ? iHmacImpl->Hash(aMessage):iCmacImpl->MacL(aMessage);
       
   170 	}		
       
   171 	
       
   172 void CMacImpl::UpdateL(const TDesC8& aMessage)
       
   173 	{
       
   174 	(iBase == EHashBased) ? iHmacImpl->Update(aMessage):iCmacImpl->UpdateL(aMessage);
       
   175 	}
       
   176 
       
   177 TPtrC8 CMacImpl::FinalL(const TDesC8& aMessage)
       
   178 	{
       
   179 	return (iBase == EHashBased) ? iHmacImpl->Final(aMessage):iCmacImpl->FinalL(aMessage);
       
   180 	}
       
   181 
       
   182 void CMacImpl::ReInitialiseAndSetKeyL(const CKey& aKey)
       
   183 	{
       
   184 	delete iKey;
       
   185 	iKey = NULL;
       
   186 	iKey = CryptoSpi::CKey::NewL(aKey);
       
   187 	
       
   188 	if (iBase == EHashBased)
       
   189 		{
       
   190 		iHmacImpl->SetKeyL(aKey);
       
   191 		iHmacImpl->Reset();
       
   192 		}
       
   193 	else if (iBase == ECipherBased)
       
   194 		{
       
   195 		iCmacImpl->ReInitialiseAndSetKeyL(aKey);
       
   196 		}
       
   197 	}
       
   198 
       
   199 MMac* CMacImpl::ReplicateL()
       
   200 	{
       
   201 	CMacImpl* that= new(ELeave) CMacImpl();
       
   202 	CleanupStack::PushL(that);
       
   203 	that->iImplementationUid = iImplementationUid;
       
   204 	that->iBase = iBase;
       
   205 	that->iKey=CKey::NewL(*iKey);
       
   206 	
       
   207 	if(iBase == EHashBased)
       
   208 		{
       
   209 		that->iHmacImpl=static_cast<CHMacImpl*>(iHmacImpl->ReplicateL());
       
   210 		}
       
   211 	else if (iBase == ECipherBased)
       
   212 		{
       
   213 		that->iCmacImpl= iCmacImpl->ReplicateL();
       
   214 		}
       
   215 	CleanupStack::Pop(that);
       
   216 	return that;
       
   217 	}
       
   218 	
       
   219 MMac* CMacImpl::CopyL()
       
   220 	{
       
   221 	CMacImpl* that= new(ELeave) CMacImpl();
       
   222 	CleanupStack::PushL(that);
       
   223 	that->iImplementationUid = iImplementationUid;
       
   224 	that->iBase = iBase;
       
   225 	that->iKey=CKey::NewL(*iKey);
       
   226 
       
   227 	if(iBase == EHashBased)
       
   228 		{
       
   229 		that->iHmacImpl=static_cast<CHMacImpl*>(iHmacImpl->CopyL());
       
   230 		}
       
   231 	else if (iBase == ECipherBased)
       
   232 		{
       
   233 		that->iCmacImpl= iCmacImpl->CopyL();
       
   234 		}
       
   235 	CleanupStack::Pop(that);
       
   236 	return that;
       
   237 	}