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