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