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