os/security/crypto/weakcryptospi/test/tcryptospi/src/hashincrementalhashwithcopystep.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 "hashincrementalhashwithcopystep.h"
    25 
    26 #include <cryptospi/cryptohashapi.h>
    27 #include <cryptospi/plugincharacteristics.h>
    28 
    29 using namespace CryptoSpi;
    30 
    31 CHashIncrementalHashWithCopyStep::~CHashIncrementalHashWithCopyStep()
    32 	{
    33 	}
    34 
    35 
    36 CHashIncrementalHashWithCopyStep::CHashIncrementalHashWithCopyStep()
    37 	{
    38 	SetTestStepName(KHashIncrementalHashWithCopyStep);
    39 	}
    40 
    41 
    42 TVerdict CHashIncrementalHashWithCopyStep::doTestStepPreambleL()
    43 	{
    44 	SetTestStepResult(EPass);
    45 	return TestStepResult();
    46 	}
    47 
    48 
    49 TVerdict CHashIncrementalHashWithCopyStep::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 Copy ***"));
    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 hashCopied = EFalse;
   123 						TPtrC8 hashStr;
   124 						
   125 						CHash* hashCopyImpl = NULL;
   126 						
   127 						User::LeaveIfError(sourceFile.Size(sourceLength));
   128 						
   129 						//Divide the total size of the source file up into individual equal sized blocks to read
   130 						//over several increments
   131 						readIncrement = sourceLength/KDataReadBlocks;
   132 						
   133 						do 
   134 							{							
   135 							//Create a heap based descriptor to store the data
   136 							HBufC8* sourceData = HBufC8::NewL(readIncrement);
   137 							CleanupStack::PushL(sourceData);
   138 							TPtr8 sourcePtr = sourceData->Des();
   139 							
   140 							//Read in a block of data from the source file from the current position
   141 							err = sourceFile.Read(readPosition,sourcePtr,readIncrement);
   142 							
   143 							//Update the read position by adding the number of bytes read
   144 							readPosition += readIncrement;
   145 							
   146 							if(readPosition == readIncrement)
   147 								{
   148 								//Read in the first block from the data file into the Hash implementation object
   149 								hashImpl->Hash(*sourceData);
   150 								INFO_PRINTF2(_L("Intial Hash - Bytes Read: %d"), readPosition);
   151 								}
   152 							else if(readPosition >= sourceLength)
   153 								{
   154 								//Reading in the final block, constructs the complete hash value and returns it within a TPtrC8
   155 								hashStr.Set(hashCopyImpl->Final(*sourceData));
   156 								
   157 								//Sets the Complete Flag to ETrue in order to drop out of the loop
   158 								hashComplete = ETrue;
   159 								
   160 								TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
   161 								INFO_PRINTF2(_L("Final Hash - Bytes Read: %d"),totalRead);
   162 								}
   163 							//If the read position is half the source length and the implementation
   164 							//object hasn't already been copied
   165 							else if((readPosition >= sourceLength/2) && (hashCopied == EFalse))
   166 								{
   167 								//Update the hash message before copying
   168 								hashImpl->Update(*sourceData);
   169 								
   170 								INFO_PRINTF1(_L("Copying Hash Object..."));
   171 								
   172 								//Create a Copy of the existing Hash Object and all internal state of the message digest
   173 								hashCopyImpl = hashImpl->CopyL();
   174 								
   175 								hashCopied = ETrue;
   176 								
   177 								INFO_PRINTF2(_L("*** HASH COPY - Bytes Read: %d ***"), readPosition);
   178 								}
   179 							else
   180 								{
   181 								//Update the message data within the Hash object with the new block
   182 								if(hashCopied == EFalse)
   183 									{
   184 									hashImpl->Update(*sourceData);
   185 									INFO_PRINTF2(_L("Hash Update - Bytes Read: %d"), readPosition);		
   186 									}
   187 								else
   188 									{
   189 									hashCopyImpl->Update(*sourceData);
   190 									INFO_PRINTF2(_L("Hash Update (Copy) - Bytes Read: %d"), readPosition);		
   191 									}
   192 								}
   193 							
   194 							CleanupStack::PopAndDestroy(sourceData);
   195 							
   196 								
   197 							}while(hashComplete == EFalse);
   198 						
   199 						//Create a NULL TCharacteristics pointer
   200 						const TCharacteristics* charsPtr(NULL);
   201 						
   202 						//Retrieve the characteristics for the hash implementation object
   203 						TRAP_LOG(err, hashImpl->GetCharacteristicsL(charsPtr));
   204 						
   205 						//Static cast the characteristics to type THashCharacteristics
   206 						const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
   207 						
   208 						//The hash output size is returned in Bits, divide by 8 to get the Byte size
   209 						TInt hashSize = hashCharsPtr->iOutputSize/8;
   210 						
   211 						//Retrieve the final 8bit hash value and convert to 16bit												
   212 						HBufC* hashData = HBufC::NewLC(hashSize);
   213 						TPtr hashPtr = hashData->Des();
   214 						
   215 						//Copy the hashed content into the heap based descriptor
   216 						hashPtr.Copy(hashStr);
   217 						
   218 					 	//Take the 16bit descriptor and convert the string to hexadecimal
   219 						TVariantPtrC convertHash;
   220 						convertHash.Set(hashPtr);
   221 						HBufC* hashResult = convertHash.HexStringLC();
   222 						
   223 						INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hashResult);
   224 						INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
   225 						
   226 						//If the returned hash value matches the expected hash, Pass the test		
   227 						if(*hashResult == expectedHash)
   228 							{
   229 							INFO_PRINTF1(_L("*** Hash - Incremental Hash with Copy : PASS ***"));
   230 							SetTestStepResult(EPass);	
   231 							}
   232 						else
   233 							{
   234 							ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch  ***"), err);
   235 							SetTestStepResult(EFail);	
   236 							}
   237 								
   238 						CleanupStack::PopAndDestroy(hashResult);
   239 						CleanupStack::PopAndDestroy(hashData);						
   240 						
   241 						delete hashCopyImpl;
   242 						}
   243 					
   244 					//Cleanup the Source RFile	
   245 					CleanupStack::PopAndDestroy();	
   246 					}
   247 
   248 				fsSession.Close();	
   249 				
   250 				CleanupStack::PopAndDestroy(hashImpl);
   251 				}
   252 			else
   253 				{
   254 				ERR_PRINTF2(_L("*** FAIL: Failed to Create Hash Object - %d ***"), err);
   255 				SetTestStepResult(EFail);	
   256 				}
   257 			}
   258 			
   259 		}
   260 		
   261 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
   262 	return TestStepResult();
   263 	}
   264 
   265 
   266 TVerdict CHashIncrementalHashWithCopyStep::doTestStepPostambleL()
   267 	{
   268 	
   269 	return TestStepResult();
   270 	}