os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/cmacimpl.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
* plugin-dll headers
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
#include "cmacimpl.h"
sl@0
    25
#include "pluginconfig.h"
sl@0
    26
#include <cryptospi/cryptomacapi.h>
sl@0
    27
sl@0
    28
sl@0
    29
using namespace SoftwareCrypto;
sl@0
    30
using namespace CryptoSpi;
sl@0
    31
sl@0
    32
/**
sl@0
    33
 * Constants used to generate Key1, Key2 and Key3
sl@0
    34
 */
sl@0
    35
const TUint8 K1Constant[] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
sl@0
    36
const TUint8 K2Constant[] = {0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02};
sl@0
    37
const TUint8 K3Constant[] = {0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03};
sl@0
    38
sl@0
    39
const TInt KAesXcbcMac96Size = 12;
sl@0
    40
sl@0
    41
sl@0
    42
CCMacImpl* CCMacImpl::NewL(const CKey& aKey, CSymmetricCipher* aSymmetricCipher, TInt32 aAlgorithmUid)
sl@0
    43
	{
sl@0
    44
	CCMacImpl* self = CCMacImpl::NewLC(aKey, aSymmetricCipher, aAlgorithmUid);
sl@0
    45
	CleanupStack::Pop(self);
sl@0
    46
	return self;
sl@0
    47
	}
sl@0
    48
														
sl@0
    49
CCMacImpl* CCMacImpl::NewLC(const CKey& aKey, CSymmetricCipher* aSymmetricCipher, TInt32 aAlgorithmUid)
sl@0
    50
	{
sl@0
    51
	CCMacImpl* self = NULL;
sl@0
    52
 	TRAPD(err, self = new (ELeave) CCMacImpl(aSymmetricCipher));
sl@0
    53
  	if(err!=KErrNone)
sl@0
    54
  		{
sl@0
    55
  		delete aSymmetricCipher;
sl@0
    56
  		User::Leave(err);
sl@0
    57
  		}
sl@0
    58
	CleanupStack::PushL(self);
sl@0
    59
	self->ConstructL(aKey, aAlgorithmUid);
sl@0
    60
	return self;
sl@0
    61
	}
sl@0
    62
sl@0
    63
CKey* CCMacImpl::Create128bitKeyL(const CKey& aKey)
sl@0
    64
	{
sl@0
    65
	TBuf8<KMacBlockSize> keybuffer;
sl@0
    66
	CryptoSpi::CKey* key = NULL;
sl@0
    67
	
sl@0
    68
	const TDesC8& keyContent=aKey.GetTDesC8L(CryptoSpi::KSymmetricKeyParameterUid);
sl@0
    69
sl@0
    70
	if( (TUint32)keyContent.Size() > KMacBlockSize)
sl@0
    71
		{
sl@0
    72
		// Create key
sl@0
    73
		CryptoSpi::CCryptoParams* keyParams = CryptoSpi::CCryptoParams::NewLC();
sl@0
    74
		keybuffer.SetLength(KMacBlockSize);
sl@0
    75
		keybuffer.FillZ();
sl@0
    76
		// 'keybuffer' is the key with 128 zero bits.
sl@0
    77
		keyParams->AddL(keybuffer, CryptoSpi::KSymmetricKeyParameterUid);
sl@0
    78
		key=CryptoSpi::CKey::NewLC(aKey.KeyProperty(),*keyParams);
sl@0
    79
		// evaluate final key data.
sl@0
    80
		SetKeyL(*key);
sl@0
    81
		CleanupStack::PopAndDestroy(2, keyParams);
sl@0
    82
		keybuffer.Copy(FinalL(keyContent));
sl@0
    83
		// 'keybuffer' contains the final key data.
sl@0
    84
		}
sl@0
    85
	else 
sl@0
    86
		{
sl@0
    87
		keybuffer.Copy(keyContent);
sl@0
    88
		TUint i;
sl@0
    89
		for (i=keybuffer.Size();i<KMacBlockSize;++i)
sl@0
    90
			{
sl@0
    91
			keybuffer.Append(0);
sl@0
    92
			}
sl@0
    93
		// 'keybuffer' contains the final key data.
sl@0
    94
		}
sl@0
    95
	
sl@0
    96
	// create a new CKey instance and assign it to iKey using 'keybuffer'.
sl@0
    97
	CryptoSpi::CCryptoParams* keyParams = CryptoSpi::CCryptoParams::NewLC();
sl@0
    98
	keyParams->AddL(keybuffer, CryptoSpi::KSymmetricKeyParameterUid);
sl@0
    99
	key=CryptoSpi::CKey::NewL(aKey.KeyProperty(),*keyParams);
sl@0
   100
	CleanupStack::PopAndDestroy(keyParams);	
sl@0
   101
sl@0
   102
	// 'key' will contain the final CKey instance.
sl@0
   103
	return key;
sl@0
   104
	}
sl@0
   105
sl@0
   106
void CCMacImpl::SetKeyL(const CKey& aKey)
sl@0
   107
	{
sl@0
   108
	const TPtrC8 KeyConstant1(K1Constant, KMacBlockSize);
sl@0
   109
	const TPtrC8 KeyConstant2(K2Constant, KMacBlockSize);
sl@0
   110
	const TPtrC8 KeyConstant3(K3Constant, KMacBlockSize);
sl@0
   111
sl@0
   112
	// Initialize the cipher class to encrypt Keyconstants to generate additional keys.
sl@0
   113
	if (iImplementationUid == CryptoSpi::KAlgorithmCipherAesXcbcPrf128)
sl@0
   114
		{
sl@0
   115
		// RFC 4434: keys that were not equal in length to 128 bits will no longer be
sl@0
   116
		// rejected but instead will be made 128 bits for AES-XCBC-PRF-128 Algorithm only.
sl@0
   117
		CryptoSpi::CKey* key = Create128bitKeyL(aKey);
sl@0
   118
		CleanupStack::PushL(key);
sl@0
   119
		iCipherImpl->SetKeyL(*key);
sl@0
   120
		CleanupStack::PopAndDestroy(key);	
sl@0
   121
		}
sl@0
   122
	else
sl@0
   123
		{
sl@0
   124
		iCipherImpl->SetKeyL(aKey);
sl@0
   125
		}
sl@0
   126
	iCipherImpl->SetCryptoModeL(CryptoSpi::KCryptoModeEncryptUid);
sl@0
   127
	iCipherImpl->SetOperationModeL(CryptoSpi::KOperationModeNoneUid);
sl@0
   128
sl@0
   129
	// cipher class expects the output buffer to be empty.
sl@0
   130
	iKey1.Zero();
sl@0
   131
	iKey2.Zero();
sl@0
   132
	iKey3.Zero();
sl@0
   133
sl@0
   134
	// aKey is used to generate Key1, Key2 and Key3.
sl@0
   135
	// Where Key1 = encrypt KeyConstant1 with aKey
sl@0
   136
	// Where Key2 = encrypt KeyConstant2 with aKey
sl@0
   137
	// Where Key3 = encrypt KeyConstant3 with aKey
sl@0
   138
	
sl@0
   139
	// Key1 is used to encrypt the data whereas
sl@0
   140
	// Key2 and Key3 is used to XOR with the last 
sl@0
   141
	// block.
sl@0
   142
    iCipherImpl->ProcessFinalL(KeyConstant1, iKey1);
sl@0
   143
	iCipherImpl->ProcessFinalL(KeyConstant2, iKey2);
sl@0
   144
	iCipherImpl->ProcessFinalL(KeyConstant3, iKey3);
sl@0
   145
	
sl@0
   146
	// Create CKey instance with key1
sl@0
   147
	CCryptoParams* keyParam =CCryptoParams::NewLC();
sl@0
   148
 	keyParam->AddL(iKey1, CryptoSpi::KSymmetricKeyParameterUid);
sl@0
   149
sl@0
   150
 	delete iKey;
sl@0
   151
 	iKey = NULL;
sl@0
   152
 	iKey=CKey::NewL(aKey.KeyProperty(), *keyParam);
sl@0
   153
 	// Initialize the cipher class for MAC calculation.
sl@0
   154
	iCipherImpl->SetKeyL(*iKey);
sl@0
   155
 	iCipherImpl->SetOperationModeL(CryptoSpi::KOperationModeCBCUid);
sl@0
   156
 	Mem::FillZ(iE, sizeof(iE));
sl@0
   157
 	iCipherImpl->SetIvL(TPtrC8(iE, KMacBlockSize));
sl@0
   158
sl@0
   159
 	CleanupStack::PopAndDestroy(keyParam);
sl@0
   160
	}
sl@0
   161
sl@0
   162
CCMacImpl::~CCMacImpl()
sl@0
   163
	{
sl@0
   164
	delete iKey;
sl@0
   165
	delete iCipherImpl;
sl@0
   166
	}
sl@0
   167
sl@0
   168
CCMacImpl::CCMacImpl(const CCMacImpl& aCCMacImpl)
sl@0
   169
	{
sl@0
   170
	iImplementationUid = aCCMacImpl.iImplementationUid;
sl@0
   171
	iKey1.Copy(aCCMacImpl.iKey1);
sl@0
   172
	iKey2.Copy(aCCMacImpl.iKey2);
sl@0
   173
	iKey3.Copy(aCCMacImpl.iKey3);
sl@0
   174
	
sl@0
   175
	(void)Mem::Copy(iE, aCCMacImpl.iE, sizeof(iE));
sl@0
   176
	(void)Mem::Copy(iData, aCCMacImpl.iData, sizeof(iData));
sl@0
   177
	
sl@0
   178
	iCurrentTotalLength = aCCMacImpl.iCurrentTotalLength;
sl@0
   179
	}
sl@0
   180
sl@0
   181
const CExtendedCharacteristics* CCMacImpl::GetExtendedCharacteristicsL()
sl@0
   182
	{
sl@0
   183
	return iCipherImpl->GetExtendedCharacteristicsL();
sl@0
   184
	}
sl@0
   185
sl@0
   186
CCMacImpl::CCMacImpl(CryptoSpi::CSymmetricCipher* aSymmetricCipher)
sl@0
   187
	{
sl@0
   188
	iCipherImpl = aSymmetricCipher;
sl@0
   189
	aSymmetricCipher = NULL;
sl@0
   190
	iMacValue.SetLength(KMacBlockSize);
sl@0
   191
	}
sl@0
   192
sl@0
   193
void CCMacImpl::ConstructL(const CKey& aKey, TInt32 aAlgorithmUid) 
sl@0
   194
	{
sl@0
   195
	iImplementationUid = aAlgorithmUid;
sl@0
   196
	
sl@0
   197
    switch(aAlgorithmUid)
sl@0
   198
    	{
sl@0
   199
    	case CryptoSpi::KAlgorithmCipherAesXcbcMac96:
sl@0
   200
    	case CryptoSpi::KAlgorithmCipherAesXcbcPrf128:
sl@0
   201
    		{
sl@0
   202
    		SetKeyL(aKey);
sl@0
   203
     		break;
sl@0
   204
    		}
sl@0
   205
    	default:
sl@0
   206
    		{
sl@0
   207
    		User::Leave(KErrNotSupported);
sl@0
   208
    		}
sl@0
   209
    	}
sl@0
   210
	}
sl@0
   211
sl@0
   212
/**
sl@0
   213
 * Takes the message and XOR it with iData.
sl@0
   214
 * 
sl@0
   215
 * @param aKey 128bit key. This key will be XORed with iData.
sl@0
   216
 * @param aOutput  The result of the XOR operation will be copied to this.
sl@0
   217
 * 				   Its length should be 128bit (16bytes).
sl@0
   218
 */
sl@0
   219
sl@0
   220
void CCMacImpl::XORKeyWithData(const TDesC8& aKey, TDes8& aOutput)
sl@0
   221
	{
sl@0
   222
	for (TInt i = 0; i < KMacBlockSize; ++i)
sl@0
   223
		{
sl@0
   224
		aOutput[i] = iData[i] ^ aKey[i];
sl@0
   225
		}
sl@0
   226
	}
sl@0
   227
sl@0
   228
/**
sl@0
   229
 * This function is used to pad message M to make the total message
sl@0
   230
 * length multiple of block size (128bit). The last block M[n] will be 
sl@0
   231
 * padded with a single "1" bit followed by the number of "0" bits required
sl@0
   232
 * to increase M[n]'s size to 128 bits (Block Size).
sl@0
   233
 * 
sl@0
   234
 * Used in AES-XCBC-MAC-96 and AES-XCBC-PRF-128 Mac algorithms.
sl@0
   235
 */
sl@0
   236
void CCMacImpl::PadMessage()
sl@0
   237
	{
sl@0
   238
	if(iCurrentTotalLength < KMacBlockSize)
sl@0
   239
		{
sl@0
   240
		iData[iCurrentTotalLength] = 0x80;
sl@0
   241
		Mem::FillZ(iData + iCurrentTotalLength+1, KMacBlockSize - iCurrentTotalLength - 1);
sl@0
   242
		}
sl@0
   243
	}
sl@0
   244
sl@0
   245
void CCMacImpl::Reset()
sl@0
   246
	{
sl@0
   247
	Mem::FillZ(iE,sizeof(iE));
sl@0
   248
	iCurrentTotalLength =0;
sl@0
   249
	// record for Reset, for the next time MacL, UpdateL or FinalL is called as we
sl@0
   250
	// cannot leave in Reset.
sl@0
   251
	TRAP(iDelayedReset, iCipherImpl->SetIvL(TPtrC8(iE, KMacBlockSize)));
sl@0
   252
	}
sl@0
   253
sl@0
   254
TPtrC8 CCMacImpl::MacL(const TDesC8& aMessage)
sl@0
   255
	{
sl@0
   256
	// Reset the cipher with iE as 128 zero bits as it leaved in previous call to Reset. 
sl@0
   257
	if (iDelayedReset != KErrNone)
sl@0
   258
		{
sl@0
   259
		// iE was reset to 128 zero bits in previous call to Reset which leaved.
sl@0
   260
		iCipherImpl->SetIvL(TPtrC8(iE, KMacBlockSize));
sl@0
   261
		iDelayedReset = KErrNone; 
sl@0
   262
		}
sl@0
   263
	
sl@0
   264
	if (aMessage!=KNullDesC8())
sl@0
   265
		{
sl@0
   266
		DoUpdateL(aMessage);			
sl@0
   267
		}
sl@0
   268
	
sl@0
   269
	// Calculate MAC
sl@0
   270
	TPtrC8 macPtr(KNullDesC8());
sl@0
   271
	macPtr.Set(DoFinalL());
sl@0
   272
sl@0
   273
	// Restore the internal state.
sl@0
   274
	// We don't want to save any state change happened in 
sl@0
   275
	// DoFinalL.
sl@0
   276
	// iE is not updated in DoFinalL function and hence
sl@0
   277
	// can be used to reset iCipherImpl to previous state.
sl@0
   278
	iCipherImpl->SetIvL(TPtrC8(iE, KMacBlockSize));
sl@0
   279
	
sl@0
   280
	return macPtr;		
sl@0
   281
	}
sl@0
   282
sl@0
   283
TPtrC8 CCMacImpl::FinalL(const TDesC8& aMessage)
sl@0
   284
	{
sl@0
   285
	// Reset the cipher with iE as 128 zero bits as it leaved in previous call to Reset. 
sl@0
   286
	if (iDelayedReset == KErrNone)
sl@0
   287
		{
sl@0
   288
		// iE was reset to 128 zero bits in previous call to Reset which leaved.
sl@0
   289
		iCipherImpl->SetIvL(TPtrC8(iE, KMacBlockSize));
sl@0
   290
		iDelayedReset = KErrNone;
sl@0
   291
		}
sl@0
   292
sl@0
   293
	if (aMessage!=KNullDesC8())
sl@0
   294
		{
sl@0
   295
		DoUpdateL(aMessage);			
sl@0
   296
		}
sl@0
   297
	TPtrC8 macPtr(KNullDesC8());
sl@0
   298
	macPtr.Set(DoFinalL());
sl@0
   299
	Reset();
sl@0
   300
	return macPtr;
sl@0
   301
	}
sl@0
   302
sl@0
   303
void CCMacImpl::UpdateL(const TDesC8& aMessage)
sl@0
   304
	{
sl@0
   305
	// Reset the cipher with iE as 128 zero bits as it leaved in previous call to Reset. 
sl@0
   306
	if (iDelayedReset == KErrNone)
sl@0
   307
		{
sl@0
   308
		// iE was reset to 128 zero bits in previous call to Reset which leaved.
sl@0
   309
		iCipherImpl->SetIvL(TPtrC8(iE, KMacBlockSize));
sl@0
   310
		iDelayedReset = KErrNone;
sl@0
   311
		}
sl@0
   312
sl@0
   313
	if (aMessage!=KNullDesC8())
sl@0
   314
		{
sl@0
   315
		DoUpdateL(aMessage);			
sl@0
   316
		}
sl@0
   317
	}
sl@0
   318
sl@0
   319
void CCMacImpl::ProcessBlockL()
sl@0
   320
	{
sl@0
   321
	TPtrC8 dataPtr(iData, KMacBlockSize);
sl@0
   322
	TPtr8 intermediateCipherPtr(iE,0,KMacBlockSize);
sl@0
   323
	// iData (Block) should be XORed with iE calculated
sl@0
   324
	// from previoue processing. If it's the first processing
sl@0
   325
	// then iE will be zero.
sl@0
   326
	// Here we are not doing explicit XORing because iCpherImpl 
sl@0
   327
	// is set in CBC mode. Therefore this operation will be
sl@0
   328
	// done by iCipherImpl
sl@0
   329
	iCipherImpl->ProcessL(dataPtr, intermediateCipherPtr);
sl@0
   330
	// After processing discard the block.
sl@0
   331
	iCurrentTotalLength = 0;
sl@0
   332
	}
sl@0
   333
sl@0
   334
void CCMacImpl::DoUpdateL(const TDesC8& aMessage)
sl@0
   335
	{
sl@0
   336
	TInt curLength = aMessage.Length();
sl@0
   337
	const TUint8* msgPtr = aMessage.Ptr();
sl@0
   338
	
sl@0
   339
	while(curLength > 0)
sl@0
   340
		{
sl@0
   341
		// If block is formed then process it.
sl@0
   342
		if(iCurrentTotalLength == KMacBlockSize)
sl@0
   343
			ProcessBlockL();
sl@0
   344
		
sl@0
   345
		// Check the space left in the block.
sl@0
   346
		TUint remainingLength = KMacBlockSize - iCurrentTotalLength;
sl@0
   347
		// If unprocesed message length is less then remainingLength
sl@0
   348
		// then copy the entire data to iData else copy till iData
sl@0
   349
		// if full.
sl@0
   350
		TUint length = Min(curLength, remainingLength);
sl@0
   351
sl@0
   352
		
sl@0
   353
		// Discard the return value obtained from Mem::Copy( ) function.		
sl@0
   354
		(void)Mem::Copy(iData+iCurrentTotalLength, msgPtr, length);
sl@0
   355
		// Update data offset
sl@0
   356
		iCurrentTotalLength += length;
sl@0
   357
		curLength -= length;
sl@0
   358
		msgPtr += length;
sl@0
   359
		}
sl@0
   360
 	}
sl@0
   361
sl@0
   362
TPtrC8 CCMacImpl::DoFinalL()
sl@0
   363
	{
sl@0
   364
	TBuf8<KMacBlockSize> finalBlock;
sl@0
   365
	finalBlock.SetLength(KMacBlockSize);
sl@0
   366
	
sl@0
   367
	// If padding is required then use Key3
sl@0
   368
	// else use Key2.
sl@0
   369
	if(iCurrentTotalLength < KMacBlockSize)
sl@0
   370
		{
sl@0
   371
		PadMessage();
sl@0
   372
		XORKeyWithData(iKey3, finalBlock);
sl@0
   373
		}
sl@0
   374
	else
sl@0
   375
		{
sl@0
   376
		XORKeyWithData(iKey2, finalBlock);
sl@0
   377
		}
sl@0
   378
sl@0
   379
	// cipher class expects the output buffer to be empty.
sl@0
   380
	iMacValue.Zero();
sl@0
   381
sl@0
   382
	iCipherImpl->ProcessFinalL(finalBlock, iMacValue);
sl@0
   383
	
sl@0
   384
    return (iImplementationUid == CryptoSpi::KAlgorithmCipherAesXcbcMac96)? iMacValue.Left(KAesXcbcMac96Size): TPtrC8(iMacValue);
sl@0
   385
	}
sl@0
   386
sl@0
   387
void CCMacImpl::ReInitialiseAndSetKeyL(const CKey& aKey)
sl@0
   388
	{
sl@0
   389
	Reset();
sl@0
   390
	SetKeyL(aKey);
sl@0
   391
	}
sl@0
   392
sl@0
   393
sl@0
   394
CCMacImpl* CCMacImpl::CopyL()
sl@0
   395
	{
sl@0
   396
	CCMacImpl* clone = new(ELeave) CCMacImpl(*this);
sl@0
   397
	CleanupStack::PushL(clone);
sl@0
   398
	clone->iKey = CKey::NewL(*iKey);
sl@0
   399
	CryptoSpi::CSymmetricCipherFactory::CreateSymmetricCipherL(clone->iCipherImpl,
sl@0
   400
												CryptoSpi::KAesUid,
sl@0
   401
												*iKey,
sl@0
   402
												CryptoSpi::KCryptoModeEncryptUid,
sl@0
   403
												CryptoSpi::KOperationModeCBCUid,
sl@0
   404
												CryptoSpi::KPaddingModeNoneUid,
sl@0
   405
												NULL);
sl@0
   406
	clone->iCipherImpl->SetIvL(TPtrC8(clone->iE, KMacBlockSize));
sl@0
   407
	CleanupStack::Pop();
sl@0
   408
	return clone;	
sl@0
   409
	}
sl@0
   410
	
sl@0
   411
CCMacImpl* CCMacImpl::ReplicateL()
sl@0
   412
	{
sl@0
   413
	CCMacImpl* replica = CopyL();
sl@0
   414
	replica->Reset();
sl@0
   415
	return replica;
sl@0
   416
	}