os/security/crypto/weakcryptospi/test/tcryptospi/src/hmacsetoperationmodecheckingstep.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-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 "hmacsetoperationmodecheckingstep.h"
    25 
    26 #include <cryptospi/cryptohashapi.h>
    27 #include <cryptospi/keys.h>
    28 #include <cryptospi/plugincharacteristics.h>
    29 
    30 using namespace CryptoSpi;
    31 
    32 CHmacSetOperationModeCheckingStep::~CHmacSetOperationModeCheckingStep()
    33 	{
    34 	}
    35 
    36 
    37 CHmacSetOperationModeCheckingStep::CHmacSetOperationModeCheckingStep()
    38 	{
    39 	SetTestStepName(KHmacSetOperationModeCheckingStep);
    40 	}
    41 
    42 
    43 TVerdict CHmacSetOperationModeCheckingStep::doTestStepPreambleL()
    44 	{
    45 	SetTestStepResult(EPass);
    46 	return TestStepResult();
    47 	}
    48 
    49 
    50 TVerdict CHmacSetOperationModeCheckingStep::doTestStepL()
    51 	{
    52 	if (TestStepResult()==EPass)
    53 		{
    54 		
    55 		//Assume faliure, unless all is successful
    56 		SetTestStepResult(EFail);
    57 		
    58 		INFO_PRINTF1(_L("*** Hmac - Set Operation Mode Checking ***"));
    59 		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
    60 		
    61 		TVariantPtrC algorithmUid;
    62 		TVariantPtrC operationModeUid;
    63 		TVariantPtrC secondOperationModeUid;
    64 		TPtrC sourcePath;
    65 		TPtrC expectedHash;
    66 		TPtrC expectedHmac;
    67 		TPtrC encryptKey;
    68 		TVariantPtrC keyType;
    69 		
    70 		//Extract the Test Case ID parameter from the specified INI file
    71 		if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
    72 			!GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
    73 			!GetStringFromConfig(ConfigSection(),KConfigSecondOperationMode,secondOperationModeUid) ||
    74 			!GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
    75 			!GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash) ||
    76 			!GetStringFromConfig(ConfigSection(),KConfigExSecondHashHmacValue,expectedHmac) ||
    77 			!GetStringFromConfig(ConfigSection(),KConfigEncryptKey,encryptKey) ||
    78 			!GetStringFromConfig(ConfigSection(),KConfigEncryptKeyType,keyType))
    79 			{
    80 			ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
    81 			SetTestStepResult(EFail);
    82 			}
    83 		else
    84 			{	
    85 			RFs fsSession;
    86 				
    87 			//Create a connection to the file server	
    88 			User::LeaveIfError(fsSession.Connect());
    89 						
    90 			RFile sourceFile;
    91 	    	CleanupClosePushL(sourceFile);
    92 	    			
    93 	    	//Open the specified source file		
    94 	    	User::LeaveIfError(sourceFile.Open(fsSession,sourcePath, EFileRead));
    95 	    					
    96 			TInt sourceLength = 0;
    97 			User::LeaveIfError(sourceFile.Size(sourceLength));
    98 						
    99 			//Create a heap based descriptor to store the data
   100 			HBufC8* sourceData = HBufC8::NewL(sourceLength);						
   101 			CleanupStack::PushL(sourceData);
   102 			TPtr8 sourcePtr = sourceData->Des();
   103 						
   104 			sourceFile.Read(sourcePtr);
   105 							
   106 			if(sourcePtr.Length() != sourceLength)
   107 				{
   108 				ERR_PRINTF1(_L("*** Error: Reading Source File ***"));
   109 				SetTestStepResult(EFail);	
   110 				}
   111 			else
   112 				{
   113 				//Create a pointer for the Hash + Key (Hmac) Implementation Object
   114 				CHash* hashHmacImpl = NULL;
   115 				
   116 				//Convert encryption key to an 8 Bit Descriptor
   117 				HBufC8* keyStr = HBufC8::NewLC(encryptKey.Length());
   118 				TPtr8 keyStrPtr = keyStr->Des();
   119 				
   120 				keyStrPtr.Copy(encryptKey);
   121 				
   122 				//Create an new CryptoParams object to encapsulate the invalid key type and key string
   123 				CCryptoParams* keyParams = CCryptoParams::NewL();
   124 				CleanupStack::PushL(keyParams);
   125 				keyParams->AddL(*keyStr,keyType);
   126 				
   127 				//Create a valid CKey Object
   128 				TKeyProperty keyProperty;
   129 				CKey* key = CKey::NewL(keyProperty,*keyParams);
   130 				CleanupStack::PushL(key);
   131 				
   132 				//Construct an initial hash object with NO key, Catching any possible Leaves			
   133 				TRAPD(err,CHashFactory::CreateHashL(
   134 									hashHmacImpl,
   135 									algorithmUid,
   136 									operationModeUid,
   137 									NULL,
   138 									NULL));
   139 									
   140 										
   141 				if(hashHmacImpl && (err == KErrNone))
   142 					{
   143 					
   144 					//Push the Implementation Object onto the Cleanup Stack
   145 					CleanupStack::PushL(hashHmacImpl);
   146 					
   147 					//Create a NULL TCharacteristics pointer
   148 					const TCharacteristics* charsPtrA(NULL);
   149 					
   150 					//Retrieve the characteristics for the hash implementation object
   151 					TRAP_LOG(err, hashHmacImpl->GetCharacteristicsL(charsPtrA));
   152 					
   153 					//Static cast the characteristics to type THashCharacteristics
   154 					const THashCharacteristics* hashCharsPtrA = static_cast<const THashCharacteristics*>(charsPtrA);
   155 					
   156 					//The hash output size is returned in Bits, divide by 8 to get the Byte size
   157 					TInt hashSize = hashCharsPtrA->iOutputSize/8;
   158 					
   159 					//Retrieve the final 8bit hash value and convert to 16bit												
   160 					HBufC* hashDataA = HBufC::NewLC(hashSize);
   161 					TPtr hashPtrA = hashDataA->Des();
   162 					
   163 					hashPtrA.Copy(hashHmacImpl->Hash(*sourceData));
   164 					
   165 					//Take the 16bit descriptor and convert the string to hexadecimal
   166 					TVariantPtrC convertHash;
   167 					convertHash.Set(hashPtrA);
   168 					
   169 					HBufC* resultA = convertHash.HexStringLC();							
   170 						
   171 					INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*resultA);
   172 					INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
   173 					
   174 					if(*resultA == expectedHash)
   175 						{
   176 						INFO_PRINTF1(_L("*** PRIMARY HASH VALID - STAGE 1 PASS ***"));
   177 						
   178 						//Set the valid key within the Hmac Implementation Object
   179 						TRAP(err,hashHmacImpl->SetKeyL(*key));
   180 						
   181 						if(err!=KErrNone)
   182 							{
   183 							ERR_PRINTF2(_L("*** ERROR %d: Setting Key ***"),err);
   184 							User::Leave(err);	
   185 							}
   186 						else
   187 							{
   188 							INFO_PRINTF1(_L("*** HMAC KEY SET ***"));	
   189 							}
   190 										
   191 						//Set the Operation Mode of the Hmac Implementation Object
   192 						hashHmacImpl->SetOperationModeL(secondOperationModeUid);
   193 						
   194 						if(err!=KErrNone)
   195 							{
   196 							ERR_PRINTF3(_L("*** ERROR %d: Setting Operation Mode %S ***"),err,&secondOperationModeUid);	
   197 							User::Leave(err);
   198 							}
   199 						else
   200 							{
   201 							INFO_PRINTF2(_L("*** OPERATION MODE SET : %S ***"),&secondOperationModeUid);	
   202 							}
   203 						
   204 						//Create a NULL TCharacteristics pointer
   205 						const TCharacteristics* charsPtrB(NULL);
   206 					
   207 						//Retrieve the characteristics for the hash implementation object
   208 						TRAP_LOG(err, hashHmacImpl->GetCharacteristicsL(charsPtrB));
   209 					
   210 						//Static cast the characteristics to type THashCharacteristics
   211 						const THashCharacteristics* hashCharsPtrB = static_cast<const THashCharacteristics*>(charsPtrB);
   212 						
   213 						//The hash output size is returned in Bits, divide by 8 to get the Byte size
   214 						hashSize = hashCharsPtrB->iOutputSize/8;
   215 						
   216 						//Retrieve the final 8bit hash value and convert to 16bit												
   217 						HBufC* hashDataB = HBufC::NewLC(hashSize);
   218 						TPtr hashPtrB = hashDataB->Des();
   219 						
   220 						hashPtrB.Copy(hashHmacImpl->Hash(*sourceData));
   221 						
   222 						//Take the 16bit descriptor and convert the string to hexadecimal
   223 						convertHash.Set(hashPtrB);
   224 						HBufC* resultB = convertHash.HexStringLC();						
   225 						
   226 						INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*resultB);
   227 						INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHmac);
   228 						
   229 						if(*resultB == expectedHmac)
   230 							{
   231 							INFO_PRINTF1(_L("*** SECONDARY HASH VALID - STAGE 2 PASS ***"));
   232 							
   233 							//Set the Operation Mode of the Hmac Implementation Object
   234 							TRAP(err,hashHmacImpl->SetOperationModeL(operationModeUid));
   235 							
   236 							if(err!=KErrNone)
   237 								{
   238 								ERR_PRINTF3(_L("*** ERROR %d: Setting Operation Mode %S ***"),err,&operationModeUid);	
   239 								User::Leave(err);
   240 								}
   241 							else
   242 								{
   243 								INFO_PRINTF2(_L("*** OPERATION MODE SET : %S ***"),&operationModeUid);	
   244 								}
   245 								
   246 							//Create a NULL TCharacteristics pointer
   247 							const TCharacteristics* charsPtrC(NULL);
   248 						
   249 							//Retrieve the characteristics for the hash implementation object
   250 							TRAP_LOG(err, hashHmacImpl->GetCharacteristicsL(charsPtrC));
   251 						
   252 							//Static cast the characteristics to type THashCharacteristics
   253 							const THashCharacteristics* hashCharsPtrC = static_cast<const THashCharacteristics*>(charsPtrC);
   254 							
   255 							//The hash output size is returned in Bits, divide by 8 to get the Byte size
   256 							hashSize = hashCharsPtrC->iOutputSize/8;
   257 							
   258 							//Retrieve the final 8bit hash value and convert to 16bit												
   259 							HBufC* hashDataC = HBufC::NewLC(hashSize);
   260 							TPtr hashPtrC = hashDataC->Des();
   261 							
   262 							hashPtrC.Copy(hashHmacImpl->Hash(*sourceData));
   263 							
   264 							//Take the 16bit descriptor and convert the string to hexadecimal
   265 							convertHash.Set(hashPtrC);
   266 							HBufC* resultC = convertHash.HexStringLC();							
   267 							
   268 							INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*resultC);
   269 							INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
   270 									
   271 							if(*resultC == expectedHash)
   272 								{
   273 								INFO_PRINTF1(_L("*** FINAL HASH VALID - STAGE 3 PASS ***"));
   274 								INFO_PRINTF1(_L("*** Hmac - Set Operation Mode Checking : PASS ***"));
   275 								SetTestStepResult(EPass);
   276 								}
   277 							else
   278 								{
   279 								ERR_PRINTF1(_L("*** STAGE 3 FAIL: Hash and Expected Value Mismatch ***"));
   280 								SetTestStepResult(EFail);	
   281 								}
   282 								
   283 							CleanupStack::PopAndDestroy(resultC);
   284 							CleanupStack::PopAndDestroy(hashDataC);
   285 							}
   286 						else
   287 							{
   288 							ERR_PRINTF1(_L("*** STAGE 2 FAIL: Hash and Expected Value Mismatch ***"));
   289 							SetTestStepResult(EFail);	
   290 							}
   291 							
   292 						CleanupStack::PopAndDestroy(resultB);
   293 						CleanupStack::PopAndDestroy(hashDataB);	
   294 						}
   295 					else
   296 						{
   297 						ERR_PRINTF1(_L("*** STAGE 1 FAIL: Hash and Expected Value Match ***"));
   298 						SetTestStepResult(EFail);	
   299 						}
   300 						
   301 					CleanupStack::PopAndDestroy(resultA);
   302 					CleanupStack::PopAndDestroy(hashDataA);
   303 					CleanupStack::PopAndDestroy(hashHmacImpl);										
   304 					}
   305 				else if(err==KErrNotSupported)
   306 					{
   307 					if((((TUid)operationModeUid != KHashModeUid) && ((TUid)operationModeUid != KHmacModeUid)) ||
   308 						((TUid)algorithmUid != KMd2Uid) && (TUid)algorithmUid != KMd5Uid && (TUid)algorithmUid != KSha1Uid && (TUid)algorithmUid != KMd4Uid && (TUid)algorithmUid != KSha224Uid && (TUid)algorithmUid != KSha256Uid && (TUid)algorithmUid != KSha384Uid && (TUid)algorithmUid != KSha512Uid)
   309 						{
   310 						ERR_PRINTF2(_L("*** Object Load Failure - Invalid Operation Mode : %d ***"), err);
   311 						User::Leave(err);	
   312 						}
   313 					else
   314 						{
   315 						SetTestStepResult(EFail);	
   316 						}
   317 					}
   318 				else
   319 					{
   320 					ERR_PRINTF2(_L("*** Hash/Hmac Facotry Object Load Failure : %d ***"), err);
   321 					User::Leave(err);	
   322 					}
   323 					
   324 				CleanupStack::PopAndDestroy(key);
   325 				CleanupStack::PopAndDestroy(keyParams);
   326 				CleanupStack::PopAndDestroy(keyStr);
   327 				}
   328 							
   329 			CleanupStack::PopAndDestroy(sourceData);
   330 								
   331 			//Cleanup the Source RFile	
   332 			CleanupStack::PopAndDestroy();	
   333 						
   334 			fsSession.Close();	
   335 			}
   336 			
   337 		}
   338 		else
   339 		{
   340 			SetTestStepResult(EFail);	
   341 		}		
   342 		
   343 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());	
   344 	
   345 	return TestStepResult();
   346 	}
   347 
   348 
   349 TVerdict CHmacSetOperationModeCheckingStep::doTestStepPostambleL()
   350 	{
   351 	return TestStepResult();
   352 	}