os/security/crypto/weakcryptospi/test/tplugins/src/tplugin02/softwarehashbase.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2007-2010 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 hash base class implementation
    16 * software hash base class implementation
    17 *
    18 */
    19 
    20 
    21 /**
    22  @file
    23 */
    24 
    25 #include "softwarehashbase.h"
    26 
    27 #include <cryptospi/hashplugin.h>
    28 #include "pluginconfig.h"
    29 #include <cryptospi/keys.h>
    30 #include "md5impl.h"
    31 #include "hmacimpl.h"
    32 
    33 using namespace SoftwareCrypto;
    34 
    35 CSoftwareHash* CSoftwareHash::NewL(TUid aAlgorithm, TUid aOperationMode, const CKey* aKey)
    36 	{
    37 	CSoftwareHash* self=NewLC(aAlgorithm, aOperationMode, aKey);
    38 	CleanupStack::Pop();
    39 	return self;
    40 	}
    41 	
    42 #ifdef SYMBIAN_SDP_IPSEC_VOIP_SUPPORT
    43 CSoftwareHash* CSoftwareHash::NewL(TUid aAlgorithm)
    44 	{
    45 	CSoftwareHash* self=NewLC(aAlgorithm, KHashModeUid, NULL);
    46 	CleanupStack::Pop();
    47 	return self;
    48 	}
    49 #endif
    50 
    51 CSoftwareHash* CSoftwareHash::NewLC(TUid aAlgorithm, TUid aOperationMode, const CKey* aKey)
    52 	{
    53 	CSoftwareHash* self=new (ELeave) CSoftwareHash();
    54 	CleanupStack::PushL(self);
    55 	self->ConstructL(aAlgorithm, aOperationMode, aKey);
    56 	return self;
    57 	}
    58 
    59 CSoftwareHash::CSoftwareHash()
    60 	{		
    61 	}
    62 
    63 CSoftwareHash::~CSoftwareHash()
    64 	{
    65 	if (iHashImpl)
    66 		{
    67 		iHashImpl->Close();			
    68 		}
    69 
    70 	if (iHmacImpl)
    71 		{
    72 		iHmacImpl->Close();
    73 		}
    74 	delete iKey;
    75 	}
    76 
    77 void CSoftwareHash::ConstructL(TUid aAlgorithm, TUid aOperationMode, const CKey* aKey)
    78 	{
    79 	//
    80 	// Only Hash and Hmac mode are supported.
    81 	//
    82 	if (aOperationMode!=KHmacModeUid && aOperationMode!=KHashModeUid)
    83 		{
    84 		User::Leave(KErrNotSupported);			
    85 		}
    86 		
    87 	//Set the key if there is one
    88 	if (aKey)
    89 		{
    90 		SetKeyL(*aKey);
    91 		}
    92 	
    93 	switch (aAlgorithm.iUid)
    94 		{
    95 		/*case KCryptoPluginMd2:
    96 			{
    97 			iHashImpl=CMD2Impl::NewL();
    98 			}
    99 			break;*/
   100 			
   101 		case KTestPlugin02Md5_1:
   102 			{
   103 			iHashImpl=CMD5Impl::NewL(aAlgorithm);
   104 			}
   105 			break;
   106 			
   107 		/*case KTestPlugin02Sha1_1:
   108 			{
   109 			iHashImpl=CSHA1Impl::NewL(aAlgorithm);
   110 			}
   111 			break;*/
   112 		
   113 	default:
   114 		User::Leave(KErrNotSupported);
   115 		}
   116 		
   117 	SetOperationModeL(aOperationMode);
   118 	}
   119 
   120 void CSoftwareHash::SetOperationModeL(TUid aOperationMode)
   121 	{
   122 	switch (aOperationMode.iUid)
   123 		{
   124 	case KHmacMode:
   125 		{
   126 		//
   127 		//Only create hmac implementation if there isn't one
   128 		//
   129 		if (!iHmacImpl)
   130 			{
   131 			if (iKey)
   132 				{
   133 				iHmacImpl=CHMacImpl::NewL(*iKey, iHashImpl);	
   134 				}
   135 			else
   136 				{
   137 				iHmacImpl=CHMacImpl::NewL(iHashImpl);
   138 				}							
   139 			}
   140 		}
   141 		break;
   142 		
   143 	case KHashMode:
   144 		{
   145 		Reset();	
   146 		}
   147 		break;
   148 		
   149 	default:
   150 		User::Leave(KErrNotSupported);
   151 		}
   152 		
   153 	//
   154 	// Set the operation mode.
   155 	//
   156 	iOperationMode=aOperationMode;
   157 	}
   158 
   159 MSoftwareHash* CSoftwareHash::Impl()
   160 	{
   161 	MSoftwareHash* impl=NULL;
   162 	if (iOperationMode==KHashModeUid)
   163 		{
   164 		impl=iHashImpl;
   165 		}
   166 	else if (iOperationMode==KHmacModeUid && iKey)
   167 			{
   168 			impl=iHmacImpl;
   169 			}
   170 	return impl;
   171 	}
   172 	
   173 void CSoftwareHash::SetKeyL(const CKey& aKey)
   174 	{
   175 	Reset();
   176 	delete iKey;
   177 	iKey=CKey::NewL(aKey);
   178 	if (iHmacImpl)
   179 		{
   180 		iHmacImpl->SetKeyL(aKey);
   181 		}
   182 	}
   183 	
   184 void CSoftwareHash::Reset()
   185 	{
   186 	if (iHashImpl)
   187 		{
   188 		iHashImpl->Reset();			
   189 		}
   190 		
   191 	if (iHmacImpl)
   192 		{
   193 		iHmacImpl->Reset();			
   194 		}
   195 	}
   196 	
   197 void CSoftwareHash::Close()
   198 	{
   199 	delete this;
   200 	}
   201 	
   202 void CSoftwareHash::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
   203 	{
   204 	MSoftwareHash* impl=Impl();
   205 	if (impl)
   206 		{
   207 		impl->GetCharacteristicsL(aPluginCharacteristics);			
   208 		}
   209 	else
   210 		{
   211 		User::Leave(KErrNotReady);
   212 		}		
   213 	}
   214 	
   215 const CExtendedCharacteristics* CSoftwareHash::GetExtendedCharacteristicsL()
   216 	{
   217 	MSoftwareHash* impl=Impl();
   218 	if (!impl)
   219 		{
   220 		User::Leave(KErrNotReady);	
   221 		}
   222 	return impl->GetExtendedCharacteristicsL();
   223 	}
   224 	
   225 TAny* CSoftwareHash::GetExtension(TUid aExtensionId)
   226 	{
   227 	MSoftwareHash* impl=Impl();
   228 	if (impl)
   229 		{
   230 		return impl->GetExtension(aExtensionId);			
   231 		}
   232 	else
   233 		{
   234 		return NULL;	
   235 		}
   236 	}
   237 
   238 TPtrC8 CSoftwareHash::Hash(const TDesC8& aMessage)
   239 	{
   240 	MSoftwareHash* impl=Impl();
   241 	if (impl)
   242 		{
   243 		return impl->Hash(aMessage);			
   244 		}
   245 	else
   246 		{
   247 		return KNullDesC8();
   248 		}
   249 	}
   250 	
   251 void CSoftwareHash::Update(const TDesC8& aMessage)
   252 	{
   253 	MSoftwareHash* impl=Impl();
   254 	if (impl)
   255 		{
   256 		return impl->Update(aMessage);
   257 		}
   258 	}
   259 	
   260 TPtrC8 CSoftwareHash::Final(const TDesC8& aMessage)
   261 	{
   262 	MSoftwareHash* impl=Impl();
   263 	if (impl)
   264 		{
   265 		return impl->Final(aMessage);
   266 		}
   267 	else
   268 		{
   269 		return KNullDesC8();
   270 		}
   271 	}
   272 	
   273 MHash* CSoftwareHash::ReplicateL()
   274 	{
   275 	CSoftwareHash* that=new(ELeave)CSoftwareHash();
   276 	CleanupStack::PushL(that);
   277 	if (this->iKey)
   278 		{
   279 		that->iKey=CKey::NewL(*this->iKey);			
   280 		}
   281 	that->iOperationMode=this->iOperationMode;
   282 	that->iHashImpl=static_cast<MSoftwareHash*>(this->iHashImpl->ReplicateL());
   283 	if (this->iHmacImpl)
   284 		{
   285 		that->iHmacImpl=static_cast<MSoftwareHash*>(this->iHmacImpl->ReplicateL());			
   286 		}
   287 	CleanupStack::Pop();
   288 	return that;
   289 	}
   290 	
   291 MHash* CSoftwareHash::CopyL()
   292 	{
   293 	CSoftwareHash* that=new(ELeave)CSoftwareHash();
   294 	CleanupStack::PushL(that);
   295 	if (this->iKey)
   296 		{
   297 		that->iKey=CKey::NewL(*this->iKey);			
   298 		}
   299 	that->iOperationMode=this->iOperationMode;
   300 	that->iHashImpl=static_cast<MSoftwareHash*>(this->iHashImpl->CopyL());
   301 	if (this->iHmacImpl)
   302 		{
   303 		that->iHmacImpl=static_cast<MSoftwareHash*>(this->iHmacImpl->CopyL());
   304 		}
   305 	CleanupStack::Pop();
   306 	return that;
   307 	}
   308 		
   309