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