os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/macimpl.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2008-2009 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 Mac Implementation
sl@0
    16
* Software Mac Implementation
sl@0
    17
* MAC plugin header
sl@0
    18
*
sl@0
    19
*/
sl@0
    20
sl@0
    21
sl@0
    22
/**
sl@0
    23
 @file
sl@0
    24
*/
sl@0
    25
#include "macimpl.h"
sl@0
    26
#include "pluginconfig.h"
sl@0
    27
sl@0
    28
/**
sl@0
    29
 * HMAC plugin headers
sl@0
    30
 */ 
sl@0
    31
#include "md2impl.h"
sl@0
    32
#include "md4impl.h"
sl@0
    33
#include "md5impl.h"
sl@0
    34
#include "sha1impl.h"
sl@0
    35
#include "sha2impl.h"
sl@0
    36
#include "hmacimpl.h"
sl@0
    37
sl@0
    38
sl@0
    39
using namespace SoftwareCrypto;
sl@0
    40
using namespace CryptoSpi;
sl@0
    41
sl@0
    42
sl@0
    43
CMacImpl* CMacImpl::NewL(const CKey& aKey, const TUid aImplementationId, const CCryptoParams* aAlgorithmParams)
sl@0
    44
	{
sl@0
    45
	CMacImpl* self = new (ELeave) CMacImpl();
sl@0
    46
	CleanupStack::PushL(self);
sl@0
    47
	self->ConstructL(aKey, aImplementationId, aAlgorithmParams);
sl@0
    48
	CleanupStack::Pop();
sl@0
    49
	return self;
sl@0
    50
	}
sl@0
    51
sl@0
    52
CMacImpl::CMacImpl() 
sl@0
    53
	{
sl@0
    54
	}
sl@0
    55
sl@0
    56
void CMacImpl::ConstructL(const CKey& aKey, const TUid aImplementationId, const CCryptoParams* /*aAlgorithmParams*/) 
sl@0
    57
	{
sl@0
    58
	iImplementationUid = aImplementationId; 
sl@0
    59
	iKey = CryptoSpi::CKey::NewL(aKey);
sl@0
    60
		
sl@0
    61
	MSoftwareHash* hashImpl = NULL;
sl@0
    62
	
sl@0
    63
	switch (aImplementationId.iUid)
sl@0
    64
		{
sl@0
    65
		case KCryptoPluginMacHashMd2:
sl@0
    66
			{
sl@0
    67
			hashImpl = CMD2Impl::NewL();
sl@0
    68
			CleanupClosePushL(*hashImpl);
sl@0
    69
			iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
sl@0
    70
			iBase = EHashBased;
sl@0
    71
			}
sl@0
    72
			break;
sl@0
    73
		case KCryptoPluginMacHashMd5:
sl@0
    74
			{
sl@0
    75
			hashImpl = CMD5Impl::NewL();
sl@0
    76
			CleanupClosePushL(*hashImpl);
sl@0
    77
			iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
sl@0
    78
			iBase = EHashBased;
sl@0
    79
			}
sl@0
    80
			break;
sl@0
    81
		case KCryptoPluginMacHashMd4:
sl@0
    82
			{
sl@0
    83
			hashImpl = CMD4Impl::NewL();
sl@0
    84
			CleanupClosePushL(*hashImpl);
sl@0
    85
			iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
sl@0
    86
			iBase = EHashBased;
sl@0
    87
			}
sl@0
    88
			break;
sl@0
    89
		case KCryptoPluginMacHashSha1:
sl@0
    90
			{
sl@0
    91
			hashImpl = CSHA1Impl::NewL();
sl@0
    92
			CleanupClosePushL(*hashImpl);
sl@0
    93
			iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
sl@0
    94
			iBase = EHashBased;
sl@0
    95
			}
sl@0
    96
			break;
sl@0
    97
		case KCryptoPluginMacHashSha224:
sl@0
    98
			{
sl@0
    99
			hashImpl = CSHA2Impl::NewL(KCryptoPluginSha224);
sl@0
   100
			CleanupClosePushL(*hashImpl);
sl@0
   101
			iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
sl@0
   102
			iBase = EHashBased;
sl@0
   103
			}
sl@0
   104
			break;
sl@0
   105
		case KCryptoPluginMacHashSha256:
sl@0
   106
			{
sl@0
   107
			hashImpl = CSHA2Impl::NewL(KCryptoPluginSha256);
sl@0
   108
			CleanupClosePushL(*hashImpl);
sl@0
   109
			iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
sl@0
   110
			iBase = EHashBased;
sl@0
   111
			}
sl@0
   112
			break;
sl@0
   113
		case KCryptoPluginMacHashSha384:
sl@0
   114
			{
sl@0
   115
			hashImpl = CSHA2Impl::NewL(KCryptoPluginSha384);
sl@0
   116
			CleanupClosePushL(*hashImpl);
sl@0
   117
			iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
sl@0
   118
			iBase = EHashBased;
sl@0
   119
			}
sl@0
   120
			break;
sl@0
   121
		case KCryptoPluginMacHashSha512:
sl@0
   122
			{
sl@0
   123
			hashImpl = CSHA2Impl::NewL(KCryptoPluginSha512);
sl@0
   124
			CleanupClosePushL(*hashImpl);
sl@0
   125
			iHmacImpl=CHMacImpl::NewL(*iKey, hashImpl);
sl@0
   126
			iBase = EHashBased;
sl@0
   127
			}
sl@0
   128
			break;
sl@0
   129
		case KCryptoPluginMacAesXcbcMac96:
sl@0
   130
			{
sl@0
   131
			CSymmetricCipher* symmetricCipher = NULL;
sl@0
   132
			CryptoSpi::CSymmetricCipherFactory::CreateSymmetricCipherL(symmetricCipher,
sl@0
   133
														CryptoSpi::KAesUid, *iKey,
sl@0
   134
														CryptoSpi::KCryptoModeEncryptUid,
sl@0
   135
														CryptoSpi::KOperationModeCBCUid,
sl@0
   136
														CryptoSpi::KPaddingModeNoneUid,
sl@0
   137
														NULL);
sl@0
   138
			
sl@0
   139
			iCmacImpl= CCMacImpl::NewL(*iKey, symmetricCipher, CryptoSpi::KAlgorithmCipherAesXcbcMac96);
sl@0
   140
			iBase = ECipherBased;
sl@0
   141
			}
sl@0
   142
			break;
sl@0
   143
		case KCryptoPluginMacAesXcbcPrf128:
sl@0
   144
			{
sl@0
   145
			TBuf8<16> tempKey;
sl@0
   146
			tempKey.SetLength(16);
sl@0
   147
			
sl@0
   148
			CryptoSpi::CCryptoParams* keyParams = CryptoSpi::CCryptoParams::NewLC();
sl@0
   149
			keyParams->AddL(tempKey, CryptoSpi::KSymmetricKeyParameterUid);
sl@0
   150
			CryptoSpi::CKey* key = CryptoSpi::CKey::NewLC(aKey.KeyProperty(),*keyParams);
sl@0
   151
				
sl@0
   152
			CSymmetricCipher* symmetricCipher = NULL;
sl@0
   153
			CryptoSpi::CSymmetricCipherFactory::CreateSymmetricCipherL(symmetricCipher,
sl@0
   154
														CryptoSpi::KAesUid, *key,
sl@0
   155
														CryptoSpi::KCryptoModeEncryptUid,
sl@0
   156
														CryptoSpi::KOperationModeCBCUid,
sl@0
   157
														CryptoSpi::KPaddingModeNoneUid,
sl@0
   158
														NULL);
sl@0
   159
			CleanupStack::PopAndDestroy(2, keyParams); //key and keyParams
sl@0
   160
			
sl@0
   161
			iCmacImpl= CCMacImpl::NewL(*iKey, symmetricCipher, CryptoSpi::KAlgorithmCipherAesXcbcPrf128);
sl@0
   162
			iBase = ECipherBased;
sl@0
   163
			}
sl@0
   164
			break;
sl@0
   165
sl@0
   166
		default:
sl@0
   167
			User::Leave(KErrNotSupported);
sl@0
   168
		}
sl@0
   169
sl@0
   170
	if(iBase == EHashBased)
sl@0
   171
		{
sl@0
   172
		CleanupStack::PopAndDestroy(hashImpl);
sl@0
   173
		}
sl@0
   174
	}
sl@0
   175
sl@0
   176
CMacImpl::~CMacImpl()
sl@0
   177
	{
sl@0
   178
	delete iKey;
sl@0
   179
	if(iHmacImpl)
sl@0
   180
		{
sl@0
   181
		iHmacImpl->Close();
sl@0
   182
		}
sl@0
   183
	delete iCmacImpl;
sl@0
   184
	}
sl@0
   185
	
sl@0
   186
void CMacImpl::Reset()
sl@0
   187
	{
sl@0
   188
	if (iBase == EHashBased)
sl@0
   189
		{
sl@0
   190
		iHmacImpl->Reset();
sl@0
   191
		}
sl@0
   192
	else if (iBase == ECipherBased)
sl@0
   193
		{
sl@0
   194
		iCmacImpl->Reset();
sl@0
   195
		}
sl@0
   196
	}
sl@0
   197
	
sl@0
   198
void CMacImpl::Close()
sl@0
   199
	{
sl@0
   200
	delete this;	
sl@0
   201
	}
sl@0
   202
	
sl@0
   203
void CMacImpl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
sl@0
   204
	{
sl@0
   205
	aPluginCharacteristics=NULL;
sl@0
   206
	TInt macNum=sizeof(KMacCharacteristics)/sizeof(TMacCharacteristics*);
sl@0
   207
	for (TInt i=0;i<macNum;++i)
sl@0
   208
		{
sl@0
   209
		if (KMacCharacteristics[i]->iMacChar.iImplementationUID==ImplementationUid().iUid)
sl@0
   210
			{
sl@0
   211
			aPluginCharacteristics = KMacCharacteristics[i];
sl@0
   212
			break;
sl@0
   213
			}
sl@0
   214
		}	
sl@0
   215
	}
sl@0
   216
sl@0
   217
const CExtendedCharacteristics* CMacImpl::GetExtendedCharacteristicsL()
sl@0
   218
	{
sl@0
   219
	return (iBase == EHashBased) ? iHmacImpl->GetExtendedCharacteristicsL(): iCmacImpl->GetExtendedCharacteristicsL();
sl@0
   220
	}
sl@0
   221
sl@0
   222
TAny* CMacImpl::GetExtension(TUid aExtensionId)
sl@0
   223
	{
sl@0
   224
	return (iBase == EHashBased) ? iHmacImpl->GetExtension(aExtensionId): NULL;
sl@0
   225
	}
sl@0
   226
sl@0
   227
TUid CMacImpl::ImplementationUid() const
sl@0
   228
	{
sl@0
   229
	return iImplementationUid;
sl@0
   230
	}
sl@0
   231
sl@0
   232
TPtrC8 CMacImpl::MacL(const TDesC8& aMessage)
sl@0
   233
	{
sl@0
   234
	return (iBase == EHashBased) ? iHmacImpl->Hash(aMessage):iCmacImpl->MacL(aMessage);
sl@0
   235
	}		
sl@0
   236
	
sl@0
   237
void CMacImpl::UpdateL(const TDesC8& aMessage)
sl@0
   238
	{
sl@0
   239
	(iBase == EHashBased) ? iHmacImpl->Update(aMessage):iCmacImpl->UpdateL(aMessage);
sl@0
   240
	}
sl@0
   241
sl@0
   242
TPtrC8 CMacImpl::FinalL(const TDesC8& aMessage)
sl@0
   243
	{
sl@0
   244
	return (iBase == EHashBased) ? iHmacImpl->Final(aMessage):iCmacImpl->FinalL(aMessage);
sl@0
   245
	}
sl@0
   246
sl@0
   247
void CMacImpl::ReInitialiseAndSetKeyL(const CKey& aKey)
sl@0
   248
	{
sl@0
   249
	delete iKey;
sl@0
   250
	iKey = NULL;
sl@0
   251
	iKey = CryptoSpi::CKey::NewL(aKey);
sl@0
   252
	
sl@0
   253
	if (iBase == EHashBased)
sl@0
   254
		{
sl@0
   255
		iHmacImpl->SetKeyL(aKey);
sl@0
   256
		iHmacImpl->Reset();
sl@0
   257
		}
sl@0
   258
	else if (iBase == ECipherBased)
sl@0
   259
		{
sl@0
   260
		iCmacImpl->ReInitialiseAndSetKeyL(aKey);
sl@0
   261
		}
sl@0
   262
	}
sl@0
   263
sl@0
   264
MMac* CMacImpl::ReplicateL()
sl@0
   265
	{
sl@0
   266
	CMacImpl* that= new(ELeave) CMacImpl();
sl@0
   267
	CleanupStack::PushL(that);
sl@0
   268
	that->iImplementationUid = iImplementationUid;
sl@0
   269
	that->iBase = iBase;
sl@0
   270
	that->iKey=CKey::NewL(*iKey);
sl@0
   271
	
sl@0
   272
	if(iBase == EHashBased)
sl@0
   273
		{
sl@0
   274
		that->iHmacImpl=static_cast<CHMacImpl*>(iHmacImpl->ReplicateL());
sl@0
   275
		}
sl@0
   276
	else if (iBase == ECipherBased)
sl@0
   277
		{
sl@0
   278
		that->iCmacImpl= iCmacImpl->ReplicateL();
sl@0
   279
		}
sl@0
   280
	CleanupStack::Pop(that);
sl@0
   281
	return that;
sl@0
   282
	}
sl@0
   283
	
sl@0
   284
MMac* CMacImpl::CopyL()
sl@0
   285
	{
sl@0
   286
	CMacImpl* that= new(ELeave) CMacImpl();
sl@0
   287
	CleanupStack::PushL(that);
sl@0
   288
	that->iImplementationUid = iImplementationUid;
sl@0
   289
	that->iBase = iBase;
sl@0
   290
	that->iKey=CKey::NewL(*iKey);
sl@0
   291
sl@0
   292
	if(iBase == EHashBased)
sl@0
   293
		{
sl@0
   294
		that->iHmacImpl=static_cast<CHMacImpl*>(iHmacImpl->CopyL());
sl@0
   295
		}
sl@0
   296
	else if (iBase == ECipherBased)
sl@0
   297
		{
sl@0
   298
		that->iCmacImpl= iCmacImpl->CopyL();
sl@0
   299
		}
sl@0
   300
	CleanupStack::Pop(that);
sl@0
   301
	return that;
sl@0
   302
	}