os/security/crypto/weakcryptospi/test/tplugins/src/tplugin01/macimpl.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 // HMAC plugin headers
    34 #include "md2impl.h"
    35 #include "hmacimpl.h"
    36 
    37 
    38 using namespace SoftwareCrypto;
    39 
    40 
    41 CMacImpl* CMacImpl::NewL(const CKey& aKey, const TUid aImplementationId, const CCryptoParams* aAlgorithmParams)
    42 	{
    43 	CMacImpl* self = new (ELeave) CMacImpl();
    44 	CleanupStack::PushL(self);
    45 	self->ConstructL(aKey, aImplementationId, aAlgorithmParams);
    46 	CleanupStack::Pop();
    47 	return self;
    48 	}
    49 
    50 CMacImpl::CMacImpl() 
    51 	{
    52 	}
    53 
    54 void CMacImpl::ConstructL(const CKey& aKey, const TUid aImplementationId, const CCryptoParams* /*aAlgorithmParams*/) 
    55 	{
    56 	iImplementationUid = aImplementationId; 
    57 	iKey = CryptoSpi::CKey::NewL(aKey);
    58 		
    59 	MSoftwareHash* hashImpl = NULL;
    60 	
    61 	switch (aImplementationId.iUid)
    62 		{
    63 		case KTestPlugin01MacMd2_1:
    64 		case KTestPlugin01MacMd2_2:
    65 			{
    66 			hashImpl = CMD2Impl::NewL(KTestPlugin01Md2_1Uid);
    67 			CleanupClosePushL(*hashImpl);
    68 			iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
    69 			iBase = EHashBased;
    70 			}
    71 			break;
    72 		case KTestPlugin01XcbcMac96:
    73 			{
    74 			CSymmetricCipher* symmetricCipher = NULL;
    75 			CryptoSpi::CSymmetricCipherFactory::CreateSymmetricCipherL(symmetricCipher,
    76 														CryptoSpi::KAesUid, *iKey,
    77 														CryptoSpi::KCryptoModeEncryptUid,
    78 														CryptoSpi::KOperationModeCBCUid,
    79 														CryptoSpi::KPaddingModeNoneUid,
    80 														NULL);
    81 			
    82 			iCmacImpl= CCMacImpl::NewL(*iKey, symmetricCipher, CryptoSpi::KAlgorithmCipherAesXcbcMac96);
    83 			iBase = ECipherBased;
    84 			}
    85 			break;
    86 		default:
    87 			{
    88 			User::Leave(KErrNotSupported);
    89 			}
    90 		}
    91 
    92 	if(iBase == EHashBased)
    93 		{
    94 		CleanupStack::PopAndDestroy(hashImpl);
    95 		}
    96 	}
    97 
    98 CMacImpl::~CMacImpl()
    99 	{
   100 	delete iKey;
   101 	if(iHmacImpl)
   102 		{
   103 		iHmacImpl->Close();
   104 		}
   105 	delete iCmacImpl;
   106 	}
   107 	
   108 void CMacImpl::Reset()
   109 	{
   110 	if (iBase == EHashBased)
   111 		{
   112 		iHmacImpl->Reset();
   113 		}
   114 	else if (iBase == ECipherBased)
   115 		{
   116 		iCmacImpl->Reset();
   117 		}
   118 	}
   119 	
   120 void CMacImpl::Close()
   121 	{
   122 	delete this;	
   123 	}
   124 	
   125 void CMacImpl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
   126 	{
   127 	aPluginCharacteristics=NULL;
   128 	TInt macNum=sizeof(KMacCharacteristics)/sizeof(TMacCharacteristics*);
   129 	for (TInt i=0;i<macNum;++i)
   130 		{
   131 		if (KMacCharacteristics[i]->iMacChar.iImplementationUID==ImplementationUid().iUid)
   132 			{
   133 			aPluginCharacteristics = KMacCharacteristics[i];
   134 			break;
   135 			}
   136 		}	
   137 	}
   138 
   139 const CExtendedCharacteristics* CMacImpl::GetExtendedCharacteristicsL()
   140 	{
   141 	return (iBase == EHashBased) ? iHmacImpl->GetExtendedCharacteristicsL(): iCmacImpl->GetExtendedCharacteristicsL();
   142 	}
   143 
   144 TAny* CMacImpl::GetExtension(TUid aExtensionId)
   145 	{
   146 	return (iBase == EHashBased) ? iHmacImpl->GetExtension(aExtensionId): NULL;
   147 	}
   148 
   149 TUid CMacImpl::ImplementationUid() const
   150 	{
   151 	return iImplementationUid;
   152 	}
   153 
   154 TPtrC8 CMacImpl::MacL(const TDesC8& aMessage)
   155 	{
   156 	return (iBase == EHashBased) ? iHmacImpl->Hash(aMessage):iCmacImpl->MacL(aMessage);
   157 	}		
   158 	
   159 void CMacImpl::UpdateL(const TDesC8& aMessage)
   160 	{
   161 	(iBase == EHashBased) ? iHmacImpl->Update(aMessage):iCmacImpl->UpdateL(aMessage);
   162 	}
   163 
   164 TPtrC8 CMacImpl::FinalL(const TDesC8& aMessage)
   165 	{
   166 	return (iBase == EHashBased) ? iHmacImpl->Final(aMessage):iCmacImpl->FinalL(aMessage);
   167 	}
   168 
   169 void CMacImpl::ReInitialiseAndSetKeyL(const CKey& aKey)
   170 	{
   171 	delete iKey;
   172 	iKey = NULL;
   173 	iKey = CryptoSpi::CKey::NewL(aKey);
   174 	
   175 	if (iBase == EHashBased)
   176 		{
   177 		iHmacImpl->SetKeyL(aKey);
   178 		iHmacImpl->Reset();
   179 		}
   180 	else if (iBase == ECipherBased)
   181 		{
   182 		iCmacImpl->ReInitialiseAndSetKeyL(aKey);
   183 		}
   184 	}
   185 
   186 MMac* CMacImpl::ReplicateL()
   187 	{
   188 	CMacImpl* that= new(ELeave) CMacImpl();
   189 	CleanupStack::PushL(that);
   190 	that->iImplementationUid = iImplementationUid;
   191 	that->iBase = iBase;
   192 	that->iKey=CKey::NewL(*iKey);
   193 	
   194 	if(iBase == EHashBased)
   195 		{
   196 		that->iHmacImpl=static_cast<CHMacImpl*>(iHmacImpl->ReplicateL());
   197 		}
   198 	else if (iBase == ECipherBased)
   199 		{
   200 		that->iCmacImpl= iCmacImpl->ReplicateL();
   201 		}
   202 	CleanupStack::Pop(that);
   203 	return that;
   204 	}
   205 	
   206 MMac* CMacImpl::CopyL()
   207 	{
   208 	CMacImpl* that= new(ELeave) CMacImpl();
   209 	CleanupStack::PushL(that);
   210 	that->iImplementationUid = iImplementationUid;
   211 	that->iBase = iBase;
   212 	that->iKey=CKey::NewL(*iKey);
   213 
   214 	if(iBase == EHashBased)
   215 		{
   216 		that->iHmacImpl=static_cast<CHMacImpl*>(iHmacImpl->CopyL());
   217 		}
   218 	else if (iBase == ECipherBased)
   219 		{
   220 		that->iCmacImpl= iCmacImpl->CopyL();
   221 		}
   222 	CleanupStack::Pop(that);
   223 	return that;
   224 	}