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