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