os/security/crypto/weakcryptospi/test/tcryptospi/src/mac_incremental_step.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-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
* Example CTestStep derived implementation
sl@0
    16
*
sl@0
    17
*/
sl@0
    18
sl@0
    19
sl@0
    20
/**
sl@0
    21
 @file
sl@0
    22
 @internalTechnology
sl@0
    23
*/
sl@0
    24
#include "mac_incremental_step.h"
sl@0
    25
#include <cryptospi/cryptomacapi.h>
sl@0
    26
#include <cryptospi/keys.h>
sl@0
    27
#include <cryptospi/plugincharacteristics.h>
sl@0
    28
sl@0
    29
using namespace CryptoSpi;
sl@0
    30
sl@0
    31
CMacIncrementalStep::~CMacIncrementalStep()
sl@0
    32
	{
sl@0
    33
	}
sl@0
    34
sl@0
    35
sl@0
    36
CMacIncrementalStep::CMacIncrementalStep()
sl@0
    37
	{
sl@0
    38
	SetTestStepName(KMacIncrementalStep);
sl@0
    39
	}
sl@0
    40
sl@0
    41
sl@0
    42
TVerdict CMacIncrementalStep::doTestStepPreambleL()
sl@0
    43
	{
sl@0
    44
	return EPass;
sl@0
    45
	}
sl@0
    46
sl@0
    47
sl@0
    48
TVerdict CMacIncrementalStep::doTestStepL()
sl@0
    49
	{
sl@0
    50
	//Assume faliure, unless all is successful
sl@0
    51
	SetTestStepResult(EFail);
sl@0
    52
		
sl@0
    53
	INFO_PRINTF1(_L("*** Mac - Incremental ***"));
sl@0
    54
	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
sl@0
    55
	
sl@0
    56
	TVariantPtrC algorithmUid;
sl@0
    57
	TPtrC sourcePath;
sl@0
    58
	TPtrC expectedMac;
sl@0
    59
	TPtrC encryptKey;
sl@0
    60
	TVariantPtrC keyType;
sl@0
    61
	
sl@0
    62
	//Extract the Test Case ID parameter from the specified INI file
sl@0
    63
	if (!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
sl@0
    64
		!GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
sl@0
    65
		!GetStringFromConfig(ConfigSection(),KConfigExMacValue,expectedMac) ||
sl@0
    66
		!GetStringFromConfig(ConfigSection(),KConfigEncryptKey,encryptKey) ||
sl@0
    67
		!GetStringFromConfig(ConfigSection(),KConfigEncryptKeyType,keyType))
sl@0
    68
		{
sl@0
    69
		ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
sl@0
    70
		return EFail;
sl@0
    71
		}
sl@0
    72
sl@0
    73
	//Create a pointer for the Mac + Key Implementation Object
sl@0
    74
	CMac* macImpl = NULL;
sl@0
    75
	
sl@0
    76
	//Convert encryption key to an 8 Bit Descriptor
sl@0
    77
	HBufC8* keyStr = HBufC8::NewLC(encryptKey.Length());
sl@0
    78
	TPtr8 keyStrPtr = keyStr->Des();
sl@0
    79
	
sl@0
    80
	keyStrPtr.Copy(encryptKey);
sl@0
    81
	
sl@0
    82
	//Create an new CryptoParams object to encapsulate the key type and secret key string
sl@0
    83
	CCryptoParams* keyParams = CCryptoParams::NewL();
sl@0
    84
	CleanupStack::PushL(keyParams);
sl@0
    85
	keyParams->AddL(*keyStr,keyType);
sl@0
    86
	
sl@0
    87
	//Create Key Object
sl@0
    88
	TKeyProperty keyProperty;
sl@0
    89
	CKey* key=CKey::NewL(keyProperty,*keyParams);
sl@0
    90
	CleanupStack::PushL(key);
sl@0
    91
	
sl@0
    92
	//Retrieve a Mac Factory Object		
sl@0
    93
	TRAPD(err,CMacFactory::CreateMacL(macImpl,
sl@0
    94
										algorithmUid,
sl@0
    95
										*key,
sl@0
    96
										NULL));										
sl@0
    97
sl@0
    98
	if (err != KErrNone)
sl@0
    99
		{
sl@0
   100
		CleanupStack::PopAndDestroy(3, keyStr);	// keyStr, keyParams, key
sl@0
   101
		delete macImpl;
sl@0
   102
		ERR_PRINTF2(_L("*** FAIL: Failed to Create Mac Object - %d ***"), err);
sl@0
   103
		return EFail;
sl@0
   104
		}
sl@0
   105
	
sl@0
   106
	//Push the Mac Implementation Object onto the Cleanup Stack
sl@0
   107
	CleanupStack::PushL(macImpl);
sl@0
   108
	
sl@0
   109
	RFs fsSession;
sl@0
   110
	User::LeaveIfError(fsSession.Connect());
sl@0
   111
	CleanupClosePushL(fsSession);
sl@0
   112
		
sl@0
   113
	RFile sourceFile;
sl@0
   114
	CleanupClosePushL(sourceFile);
sl@0
   115
	
sl@0
   116
	//Open the specified source file
sl@0
   117
	User::LeaveIfError(sourceFile.Open(fsSession,sourcePath, EFileRead));
sl@0
   118
sl@0
   119
	TInt sourceLength = 0;
sl@0
   120
	TInt readPosition = 0;
sl@0
   121
	TInt readIncrement = 0;
sl@0
   122
	TBool macComplete = EFalse;
sl@0
   123
	TPtrC8 macStr;
sl@0
   124
	
sl@0
   125
	User::LeaveIfError(sourceFile.Size(sourceLength));
sl@0
   126
	
sl@0
   127
	//Divide the total size of the source file up into individual equal sized blocks to read
sl@0
   128
	//over several increments
sl@0
   129
	readIncrement = sourceLength/KDataReadBlocks;
sl@0
   130
	
sl@0
   131
	if (readIncrement == 0)
sl@0
   132
		{
sl@0
   133
		ERR_PRINTF2(_L("*** Error: Source File must be larger than %d bytes ***"), KDataReadBlocks);
sl@0
   134
		User::LeaveIfError(KErrNotSupported);
sl@0
   135
		}
sl@0
   136
						
sl@0
   137
	do 
sl@0
   138
		{
sl@0
   139
		//Create a heap based descriptor to store the data
sl@0
   140
		HBufC8* sourceData = HBufC8::NewL(readIncrement);
sl@0
   141
		CleanupStack::PushL(sourceData);
sl@0
   142
		TPtr8 sourcePtr = sourceData->Des();
sl@0
   143
		
sl@0
   144
		//Read in a block of data from the source file from the current position
sl@0
   145
		err = sourceFile.Read(readPosition,sourcePtr,readIncrement);
sl@0
   146
		
sl@0
   147
		//Update the read position by adding the number of bytes read
sl@0
   148
		readPosition += readIncrement;
sl@0
   149
		
sl@0
   150
		if (readPosition == readIncrement)
sl@0
   151
			{
sl@0
   152
			//Read in the first block from the data file into the Mac implementation object
sl@0
   153
			macImpl->MacL(*sourceData);
sl@0
   154
			INFO_PRINTF2(_L("Intial Mac - Bytes Read: %d"), readPosition);
sl@0
   155
			}
sl@0
   156
		else if (readPosition >= sourceLength)
sl@0
   157
			{
sl@0
   158
			//Reading in the final block, constructs the complete mac value and returns it within a TPtrC8
sl@0
   159
			macStr.Set(macImpl->FinalL(*sourceData));
sl@0
   160
			
sl@0
   161
			//Sets the Complete Flag to ETrue in order to drop out of the loop
sl@0
   162
			macComplete = ETrue;
sl@0
   163
			
sl@0
   164
			TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
sl@0
   165
			INFO_PRINTF2(_L("Final Mac - Bytes Read: %d"),totalRead);
sl@0
   166
			}
sl@0
   167
		else
sl@0
   168
			{
sl@0
   169
			//Update the message data within the Mac object with the new block
sl@0
   170
			macImpl->UpdateL(*sourceData);
sl@0
   171
			INFO_PRINTF2(_L("Mac Update - Bytes Read: %d"), readPosition);
sl@0
   172
			}
sl@0
   173
		
sl@0
   174
		CleanupStack::PopAndDestroy(sourceData);
sl@0
   175
			
sl@0
   176
		}while(macComplete == EFalse);
sl@0
   177
	
sl@0
   178
	//Create a NULL TCharacteristics pointer
sl@0
   179
	const TCharacteristics* charsPtr(NULL);
sl@0
   180
	
sl@0
   181
	//Retrieve the characteristics for the mac implementation object
sl@0
   182
	TRAP_LOG(err, macImpl->GetCharacteristicsL(charsPtr));
sl@0
   183
	
sl@0
   184
	//Static cast the characteristics to type TMacCharacteristics
sl@0
   185
	const TMacCharacteristics* macCharsPtr = static_cast<const TMacCharacteristics*>(charsPtr);
sl@0
   186
	
sl@0
   187
	//The mac output size is returned in Bits, divide by 8 to get the Byte size
sl@0
   188
	 TInt macSize = macCharsPtr->iHashAlgorithmChar->iOutputSize/8;
sl@0
   189
		
sl@0
   190
	//Retrieve the final 8 bit mac value and convert to 16bit												
sl@0
   191
	HBufC* macData = HBufC::NewLC(macSize);
sl@0
   192
	TPtr macPtr = macData->Des();
sl@0
   193
	
sl@0
   194
	macPtr.Copy(macStr);
sl@0
   195
	
sl@0
   196
	//Take the 16 bit descriptor and convert the string to hexadecimal
sl@0
   197
	TVariantPtrC convertMac;
sl@0
   198
	convertMac.Set(macPtr);
sl@0
   199
	HBufC* macResult = convertMac.HexStringLC();								
sl@0
   200
	
sl@0
   201
	INFO_PRINTF2(_L("*** Mac Data: %S ***"),&*macResult);
sl@0
   202
	INFO_PRINTF2(_L("*** Expected Mac: %S ***"),&expectedMac);
sl@0
   203
		
sl@0
   204
	//If the returned mac value matches the expected mac, Pass the test	
sl@0
   205
	if (*macResult == expectedMac)
sl@0
   206
		{
sl@0
   207
		INFO_PRINTF1(_L("*** Mac - Incremental Hash : PASS ***"));
sl@0
   208
		SetTestStepResult(EPass);	
sl@0
   209
		}
sl@0
   210
	else
sl@0
   211
		{
sl@0
   212
		ERR_PRINTF2(_L("*** FAIL: Generated Mac and Expected Mac Mismatch  ***"), err);
sl@0
   213
		}				
sl@0
   214
sl@0
   215
	CleanupStack::PopAndDestroy(8, keyStr);	// keyStr, keyParams, key, macImpl, &fsSession, &sourceFile, macData, macResult
sl@0
   216
	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
sl@0
   217
	return TestStepResult();
sl@0
   218
	}
sl@0
   219
sl@0
   220
sl@0
   221
TVerdict CMacIncrementalStep::doTestStepPostambleL()
sl@0
   222
	{
sl@0
   223
	return TestStepResult();
sl@0
   224
	}