crypto/weakcryptospi/test/tcryptospi/src/hmacsetkeycheckingstep.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 "hmacsetkeycheckingstep.h"
       
    25 
       
    26 #include <cryptospi/cryptohashapi.h>
       
    27 #include "keys.h"
       
    28 #include <cryptospi/plugincharacteristics.h>
       
    29 
       
    30 using namespace CryptoSpi;
       
    31 
       
    32 CHmacSetKeyCheckingStep::~CHmacSetKeyCheckingStep()
       
    33 	{
       
    34 	}
       
    35 
       
    36 
       
    37 CHmacSetKeyCheckingStep::CHmacSetKeyCheckingStep()
       
    38 	{
       
    39 	SetTestStepName(KHmacSetKeyCheckingStep);
       
    40 	}
       
    41 
       
    42 
       
    43 TVerdict CHmacSetKeyCheckingStep::doTestStepPreambleL()
       
    44 	{
       
    45 	SetTestStepResult(EPass);
       
    46 	return TestStepResult();
       
    47 	}
       
    48 
       
    49 
       
    50 TVerdict CHmacSetKeyCheckingStep::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 Key Checking ***"));
       
    59 		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
       
    60 		
       
    61 		TVariantPtrC algorithmUid;
       
    62 		TVariantPtrC operationModeUid;
       
    63 		TPtrC sourcePath;
       
    64 		TPtrC expectedHash;
       
    65 		TPtrC invalidEncryptKey;
       
    66 		TPtrC encryptKey;
       
    67 		TVariantPtrC keyType;
       
    68 		
       
    69 		//Extract the Test Case ID parameter from the specified INI file
       
    70 		if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
       
    71 			!GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
       
    72 			!GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
       
    73 			!GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash) ||
       
    74 			!GetStringFromConfig(ConfigSection(),KConfigInvalidKey,invalidEncryptKey) ||
       
    75 			!GetStringFromConfig(ConfigSection(),KConfigEncryptKey,encryptKey) ||
       
    76 			!GetStringFromConfig(ConfigSection(),KConfigEncryptKeyType,keyType))
       
    77 			{
       
    78 			ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
       
    79 			SetTestStepResult(EFail);
       
    80 			}
       
    81 		else
       
    82 			{	
       
    83 			RFs fsSession;
       
    84 				
       
    85 			//Create a connection to the file server	
       
    86 			User::LeaveIfError(fsSession.Connect());
       
    87 						
       
    88 			RFile sourceFile;
       
    89 	    	CleanupClosePushL(sourceFile);
       
    90 	    			
       
    91 	    	//Open the specified source file		
       
    92 	    	User::LeaveIfError(sourceFile.Open(fsSession,sourcePath, EFileRead));
       
    93 	    					
       
    94 			TInt sourceLength = 0;
       
    95 			User::LeaveIfError(sourceFile.Size(sourceLength));
       
    96 						
       
    97 			//Create a heap based descriptor to store the data
       
    98 			HBufC8* sourceData = HBufC8::NewL(sourceLength);						
       
    99 			CleanupStack::PushL(sourceData);
       
   100 			TPtr8 sourcePtr = sourceData->Des();
       
   101 						
       
   102 			sourceFile.Read(sourcePtr);
       
   103 							
       
   104 			if(sourcePtr.Length() != sourceLength)
       
   105 				{
       
   106 				ERR_PRINTF1(_L("*** Error: Reading Source File ***"));
       
   107 				SetTestStepResult(EFail);	
       
   108 				}
       
   109 			else
       
   110 				{
       
   111 				//Create a pointer for the Hash + Key (Hmac) Implementation Object
       
   112 				CHash* hmacImpl = NULL;
       
   113 
       
   114 				//Convert encryption key to an 8 Bit Descriptor
       
   115 				HBufC8* invalidKeyStr = HBufC8::NewLC(invalidEncryptKey.Length());
       
   116 				TPtr8 invalidKeyStrPtr = invalidKeyStr->Des();
       
   117 			
       
   118 				invalidKeyStrPtr.Copy(invalidEncryptKey);
       
   119 				
       
   120 				//Create an new CryptoParams object to encapsulate the invalid key type and key string
       
   121 				CCryptoParams* invalidKeyParams = CCryptoParams::NewL();
       
   122 				CleanupStack::PushL(invalidKeyParams);
       
   123 				invalidKeyParams->AddL(*invalidKeyStr,keyType);
       
   124 				
       
   125 				//Create Invalid Key Object
       
   126 				TKeyProperty invalidKeyProperty;
       
   127 				CKey* invalidKey = CKey::NewL(invalidKeyProperty,*invalidKeyParams);
       
   128 				CleanupStack::PushL(invalidKey);
       
   129 				
       
   130 				//Retrieve a Hmac Factory Object with an Invalid Key			
       
   131 				TRAPD(err,CHashFactory::CreateHashL(hmacImpl,
       
   132 													algorithmUid,
       
   133 													operationModeUid,
       
   134 													invalidKey,
       
   135 													NULL));						
       
   136 				
       
   137 				if(hmacImpl && (err == KErrNone))
       
   138 					{
       
   139 					
       
   140 					//Push the Hmac Implementation Object onto the Cleanup Stack
       
   141 					CleanupStack::PushL(hmacImpl);
       
   142 										
       
   143 					//Create a NULL TCharacteristics pointer
       
   144 					const TCharacteristics* invalidCharsPtr(NULL);
       
   145 					
       
   146 					//Retrieve the characteristics for the hash implementation object
       
   147 					TRAP_LOG(err, hmacImpl->GetCharacteristicsL(invalidCharsPtr));
       
   148 					
       
   149 					//Static cast the characteristics to type THashCharacteristics
       
   150 					const THashCharacteristics* hashInvalidCharsPtr = static_cast<const THashCharacteristics*>(invalidCharsPtr);
       
   151 					
       
   152 					//The hash output size is returned in Bits, divide by 8 to get the Byte size
       
   153 					TInt hashSize = hashInvalidCharsPtr->iOutputSize/8;
       
   154 					
       
   155 					//Retrieve the final 8bit hash value and convert to 16bit												
       
   156 					HBufC* invalidHashData = HBufC::NewLC(hashSize);
       
   157 					TPtr invalidHashPtr = invalidHashData->Des();
       
   158 					
       
   159 					invalidHashPtr.Copy(hmacImpl->Hash(*sourceData));
       
   160 					
       
   161 					//Take the 16bit descriptor and convert the string to hexadecimal
       
   162 					TVariantPtrC convertHash;
       
   163 					convertHash.Set(invalidHashPtr);
       
   164 					HBufC* invalidHmacResult = convertHash.HexStringLC();							
       
   165 						
       
   166 					INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*invalidHmacResult);
       
   167 					INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
       
   168 					
       
   169 					if(*invalidHmacResult != expectedHash)
       
   170 						{
       
   171 						INFO_PRINTF1(_L("*** INVALID KEY - STAGE 1 PASS ***"));
       
   172 						
       
   173 						//Convert encryption key to an 8 Bit Descriptor
       
   174 						HBufC8* validKeyStr = HBufC8::NewLC(encryptKey.Length());
       
   175 						TPtr8 validKeyStrPtr = validKeyStr->Des();
       
   176 			
       
   177 						validKeyStrPtr.Copy(encryptKey);
       
   178 						
       
   179 						//Create an new CryptoParams object to encapsulate the valid key type and secret key string
       
   180 						CCryptoParams* validKeyParams = CCryptoParams::NewL();
       
   181 						CleanupStack::PushL(validKeyParams);
       
   182 						validKeyParams->AddL(*validKeyStr,keyType);
       
   183 						
       
   184 						//Create Valid Key Object
       
   185 						TKeyProperty validKeyProperty;
       
   186 						CKey* validKey = CKey::NewL(validKeyProperty,*validKeyParams);
       
   187 						CleanupStack::PushL(validKey);
       
   188 												
       
   189 						//Set the valid key within the Hmac Implementation Object
       
   190 						TRAP(err,hmacImpl->SetKeyL(*validKey));
       
   191 						
       
   192 						if(err!=KErrNone)
       
   193 							{
       
   194 							ERR_PRINTF2(_L("*** ERROR %d: Setting Valid Key ***"),err);
       
   195 							User::Leave(err);	
       
   196 							}
       
   197 						else
       
   198 							{
       
   199 							INFO_PRINTF1(_L("*** HMAC VALID KEY SET ***"));	
       
   200 							}
       
   201 							
       
   202 						//Create a NULL TCharacteristics pointer
       
   203 						const TCharacteristics* validCharsPtr(NULL);
       
   204 					
       
   205 						//Retrieve the characteristics for the hash implementation object
       
   206 						TRAP_LOG(err, hmacImpl->GetCharacteristicsL(validCharsPtr));
       
   207 					
       
   208 						//Static cast the characteristics to type THashCharacteristics
       
   209 						const THashCharacteristics* hashValidCharsPtr = static_cast<const THashCharacteristics*>(validCharsPtr);
       
   210 						
       
   211 						//The hash output size is returned in Bits, divide by 8 to get the Byte size
       
   212 						hashSize = hashValidCharsPtr->iOutputSize/8;
       
   213 						
       
   214 						//Retrieve the final 8bit hash value and convert to 16bit												
       
   215 						HBufC* validHashData = HBufC::NewLC(hashSize);
       
   216 						TPtr validHashPtr = validHashData->Des();
       
   217 						
       
   218 						validHashPtr.Copy(hmacImpl->Hash(*sourceData));
       
   219 						
       
   220 						//Take the 16bit descriptor and convert the string to hexadecimal
       
   221 						convertHash.Set(validHashPtr);
       
   222 						HBufC* validHmacResult = convertHash.HexStringLC();							
       
   223 						
       
   224 						INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*validHmacResult);
       
   225 						INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
       
   226 						
       
   227 						if(*validHmacResult == expectedHash)
       
   228 							{
       
   229 							INFO_PRINTF1(_L("*** VALID KEY - STAGE 2 PASS ***"));
       
   230 							INFO_PRINTF1(_L("*** Hmac - Set Key Checking : PASS ***"));
       
   231 							SetTestStepResult(EPass);
       
   232 							}
       
   233 						else
       
   234 							{
       
   235 							ERR_PRINTF1(_L("*** STAGE 2 FAIL: Valid Hash and Expected Value Mismatch ***"));
       
   236 							SetTestStepResult(EFail);	
       
   237 							}
       
   238 						CleanupStack::PopAndDestroy(validHmacResult);
       
   239 						CleanupStack::PopAndDestroy(validHashData);
       
   240 						
       
   241 						CleanupStack::PopAndDestroy(validKey);
       
   242 						CleanupStack::PopAndDestroy(validKeyParams);
       
   243 						CleanupStack::PopAndDestroy(validKeyStr);	
       
   244 						}
       
   245 					else
       
   246 						{
       
   247 						ERR_PRINTF1(_L("*** STAGE 1 FAIL: Invalid Hash and Expected Value Match ***"));
       
   248 						SetTestStepResult(EFail);	
       
   249 						}
       
   250 						
       
   251 					CleanupStack::PopAndDestroy(invalidHmacResult);	
       
   252 					CleanupStack::PopAndDestroy(invalidHashData);
       
   253 					CleanupStack::PopAndDestroy(hmacImpl);									
       
   254 					}
       
   255 				else
       
   256 					{
       
   257 					ERR_PRINTF2(_L("*** FAIL: Failed to Create Hmac Object - %d ***"), err);
       
   258 					SetTestStepResult(EFail);	
       
   259 					}
       
   260 					
       
   261 				CleanupStack::PopAndDestroy(invalidKey);
       
   262 				CleanupStack::PopAndDestroy(invalidKeyParams);
       
   263 				CleanupStack::PopAndDestroy(invalidKeyStr);
       
   264 				}
       
   265 						
       
   266 			CleanupStack::PopAndDestroy(sourceData);
       
   267 								
       
   268 			//Cleanup the Source RFile	
       
   269 			CleanupStack::PopAndDestroy();	
       
   270 						
       
   271 			fsSession.Close();	
       
   272 			}
       
   273 			
       
   274 			
       
   275 		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());	
       
   276 		}
       
   277 		
       
   278 	return TestStepResult();
       
   279 	}
       
   280 
       
   281 
       
   282 TVerdict CHmacSetKeyCheckingStep::doTestStepPostambleL()
       
   283 	{
       
   284 	return TestStepResult();
       
   285 	}