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