os/security/crypto/weakcryptospi/test/tcryptospi/src/hmacsetoperationmodecheckingstep.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcryptospi/test/tcryptospi/src/hmacsetoperationmodecheckingstep.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,352 @@
     1.4 +/*
     1.5 +* Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +* Example CTestStep derived implementation
    1.19 +*
    1.20 +*/
    1.21 +
    1.22 +
    1.23 +/**
    1.24 + @file
    1.25 + @internalTechnology
    1.26 +*/
    1.27 +#include "hmacsetoperationmodecheckingstep.h"
    1.28 +
    1.29 +#include <cryptospi/cryptohashapi.h>
    1.30 +#include <cryptospi/keys.h>
    1.31 +#include <cryptospi/plugincharacteristics.h>
    1.32 +
    1.33 +using namespace CryptoSpi;
    1.34 +
    1.35 +CHmacSetOperationModeCheckingStep::~CHmacSetOperationModeCheckingStep()
    1.36 +	{
    1.37 +	}
    1.38 +
    1.39 +
    1.40 +CHmacSetOperationModeCheckingStep::CHmacSetOperationModeCheckingStep()
    1.41 +	{
    1.42 +	SetTestStepName(KHmacSetOperationModeCheckingStep);
    1.43 +	}
    1.44 +
    1.45 +
    1.46 +TVerdict CHmacSetOperationModeCheckingStep::doTestStepPreambleL()
    1.47 +	{
    1.48 +	SetTestStepResult(EPass);
    1.49 +	return TestStepResult();
    1.50 +	}
    1.51 +
    1.52 +
    1.53 +TVerdict CHmacSetOperationModeCheckingStep::doTestStepL()
    1.54 +	{
    1.55 +	if (TestStepResult()==EPass)
    1.56 +		{
    1.57 +		
    1.58 +		//Assume faliure, unless all is successful
    1.59 +		SetTestStepResult(EFail);
    1.60 +		
    1.61 +		INFO_PRINTF1(_L("*** Hmac - Set Operation Mode Checking ***"));
    1.62 +		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
    1.63 +		
    1.64 +		TVariantPtrC algorithmUid;
    1.65 +		TVariantPtrC operationModeUid;
    1.66 +		TVariantPtrC secondOperationModeUid;
    1.67 +		TPtrC sourcePath;
    1.68 +		TPtrC expectedHash;
    1.69 +		TPtrC expectedHmac;
    1.70 +		TPtrC encryptKey;
    1.71 +		TVariantPtrC keyType;
    1.72 +		
    1.73 +		//Extract the Test Case ID parameter from the specified INI file
    1.74 +		if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
    1.75 +			!GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
    1.76 +			!GetStringFromConfig(ConfigSection(),KConfigSecondOperationMode,secondOperationModeUid) ||
    1.77 +			!GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
    1.78 +			!GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash) ||
    1.79 +			!GetStringFromConfig(ConfigSection(),KConfigExSecondHashHmacValue,expectedHmac) ||
    1.80 +			!GetStringFromConfig(ConfigSection(),KConfigEncryptKey,encryptKey) ||
    1.81 +			!GetStringFromConfig(ConfigSection(),KConfigEncryptKeyType,keyType))
    1.82 +			{
    1.83 +			ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
    1.84 +			SetTestStepResult(EFail);
    1.85 +			}
    1.86 +		else
    1.87 +			{	
    1.88 +			RFs fsSession;
    1.89 +				
    1.90 +			//Create a connection to the file server	
    1.91 +			User::LeaveIfError(fsSession.Connect());
    1.92 +						
    1.93 +			RFile sourceFile;
    1.94 +	    	CleanupClosePushL(sourceFile);
    1.95 +	    			
    1.96 +	    	//Open the specified source file		
    1.97 +	    	User::LeaveIfError(sourceFile.Open(fsSession,sourcePath, EFileRead));
    1.98 +	    					
    1.99 +			TInt sourceLength = 0;
   1.100 +			User::LeaveIfError(sourceFile.Size(sourceLength));
   1.101 +						
   1.102 +			//Create a heap based descriptor to store the data
   1.103 +			HBufC8* sourceData = HBufC8::NewL(sourceLength);						
   1.104 +			CleanupStack::PushL(sourceData);
   1.105 +			TPtr8 sourcePtr = sourceData->Des();
   1.106 +						
   1.107 +			sourceFile.Read(sourcePtr);
   1.108 +							
   1.109 +			if(sourcePtr.Length() != sourceLength)
   1.110 +				{
   1.111 +				ERR_PRINTF1(_L("*** Error: Reading Source File ***"));
   1.112 +				SetTestStepResult(EFail);	
   1.113 +				}
   1.114 +			else
   1.115 +				{
   1.116 +				//Create a pointer for the Hash + Key (Hmac) Implementation Object
   1.117 +				CHash* hashHmacImpl = NULL;
   1.118 +				
   1.119 +				//Convert encryption key to an 8 Bit Descriptor
   1.120 +				HBufC8* keyStr = HBufC8::NewLC(encryptKey.Length());
   1.121 +				TPtr8 keyStrPtr = keyStr->Des();
   1.122 +				
   1.123 +				keyStrPtr.Copy(encryptKey);
   1.124 +				
   1.125 +				//Create an new CryptoParams object to encapsulate the invalid key type and key string
   1.126 +				CCryptoParams* keyParams = CCryptoParams::NewL();
   1.127 +				CleanupStack::PushL(keyParams);
   1.128 +				keyParams->AddL(*keyStr,keyType);
   1.129 +				
   1.130 +				//Create a valid CKey Object
   1.131 +				TKeyProperty keyProperty;
   1.132 +				CKey* key = CKey::NewL(keyProperty,*keyParams);
   1.133 +				CleanupStack::PushL(key);
   1.134 +				
   1.135 +				//Construct an initial hash object with NO key, Catching any possible Leaves			
   1.136 +				TRAPD(err,CHashFactory::CreateHashL(
   1.137 +									hashHmacImpl,
   1.138 +									algorithmUid,
   1.139 +									operationModeUid,
   1.140 +									NULL,
   1.141 +									NULL));
   1.142 +									
   1.143 +										
   1.144 +				if(hashHmacImpl && (err == KErrNone))
   1.145 +					{
   1.146 +					
   1.147 +					//Push the Implementation Object onto the Cleanup Stack
   1.148 +					CleanupStack::PushL(hashHmacImpl);
   1.149 +					
   1.150 +					//Create a NULL TCharacteristics pointer
   1.151 +					const TCharacteristics* charsPtrA(NULL);
   1.152 +					
   1.153 +					//Retrieve the characteristics for the hash implementation object
   1.154 +					TRAP_LOG(err, hashHmacImpl->GetCharacteristicsL(charsPtrA));
   1.155 +					
   1.156 +					//Static cast the characteristics to type THashCharacteristics
   1.157 +					const THashCharacteristics* hashCharsPtrA = static_cast<const THashCharacteristics*>(charsPtrA);
   1.158 +					
   1.159 +					//The hash output size is returned in Bits, divide by 8 to get the Byte size
   1.160 +					TInt hashSize = hashCharsPtrA->iOutputSize/8;
   1.161 +					
   1.162 +					//Retrieve the final 8bit hash value and convert to 16bit												
   1.163 +					HBufC* hashDataA = HBufC::NewLC(hashSize);
   1.164 +					TPtr hashPtrA = hashDataA->Des();
   1.165 +					
   1.166 +					hashPtrA.Copy(hashHmacImpl->Hash(*sourceData));
   1.167 +					
   1.168 +					//Take the 16bit descriptor and convert the string to hexadecimal
   1.169 +					TVariantPtrC convertHash;
   1.170 +					convertHash.Set(hashPtrA);
   1.171 +					
   1.172 +					HBufC* resultA = convertHash.HexStringLC();							
   1.173 +						
   1.174 +					INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*resultA);
   1.175 +					INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
   1.176 +					
   1.177 +					if(*resultA == expectedHash)
   1.178 +						{
   1.179 +						INFO_PRINTF1(_L("*** PRIMARY HASH VALID - STAGE 1 PASS ***"));
   1.180 +						
   1.181 +						//Set the valid key within the Hmac Implementation Object
   1.182 +						TRAP(err,hashHmacImpl->SetKeyL(*key));
   1.183 +						
   1.184 +						if(err!=KErrNone)
   1.185 +							{
   1.186 +							ERR_PRINTF2(_L("*** ERROR %d: Setting Key ***"),err);
   1.187 +							User::Leave(err);	
   1.188 +							}
   1.189 +						else
   1.190 +							{
   1.191 +							INFO_PRINTF1(_L("*** HMAC KEY SET ***"));	
   1.192 +							}
   1.193 +										
   1.194 +						//Set the Operation Mode of the Hmac Implementation Object
   1.195 +						hashHmacImpl->SetOperationModeL(secondOperationModeUid);
   1.196 +						
   1.197 +						if(err!=KErrNone)
   1.198 +							{
   1.199 +							ERR_PRINTF3(_L("*** ERROR %d: Setting Operation Mode %S ***"),err,&secondOperationModeUid);	
   1.200 +							User::Leave(err);
   1.201 +							}
   1.202 +						else
   1.203 +							{
   1.204 +							INFO_PRINTF2(_L("*** OPERATION MODE SET : %S ***"),&secondOperationModeUid);	
   1.205 +							}
   1.206 +						
   1.207 +						//Create a NULL TCharacteristics pointer
   1.208 +						const TCharacteristics* charsPtrB(NULL);
   1.209 +					
   1.210 +						//Retrieve the characteristics for the hash implementation object
   1.211 +						TRAP_LOG(err, hashHmacImpl->GetCharacteristicsL(charsPtrB));
   1.212 +					
   1.213 +						//Static cast the characteristics to type THashCharacteristics
   1.214 +						const THashCharacteristics* hashCharsPtrB = static_cast<const THashCharacteristics*>(charsPtrB);
   1.215 +						
   1.216 +						//The hash output size is returned in Bits, divide by 8 to get the Byte size
   1.217 +						hashSize = hashCharsPtrB->iOutputSize/8;
   1.218 +						
   1.219 +						//Retrieve the final 8bit hash value and convert to 16bit												
   1.220 +						HBufC* hashDataB = HBufC::NewLC(hashSize);
   1.221 +						TPtr hashPtrB = hashDataB->Des();
   1.222 +						
   1.223 +						hashPtrB.Copy(hashHmacImpl->Hash(*sourceData));
   1.224 +						
   1.225 +						//Take the 16bit descriptor and convert the string to hexadecimal
   1.226 +						convertHash.Set(hashPtrB);
   1.227 +						HBufC* resultB = convertHash.HexStringLC();						
   1.228 +						
   1.229 +						INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*resultB);
   1.230 +						INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHmac);
   1.231 +						
   1.232 +						if(*resultB == expectedHmac)
   1.233 +							{
   1.234 +							INFO_PRINTF1(_L("*** SECONDARY HASH VALID - STAGE 2 PASS ***"));
   1.235 +							
   1.236 +							//Set the Operation Mode of the Hmac Implementation Object
   1.237 +							TRAP(err,hashHmacImpl->SetOperationModeL(operationModeUid));
   1.238 +							
   1.239 +							if(err!=KErrNone)
   1.240 +								{
   1.241 +								ERR_PRINTF3(_L("*** ERROR %d: Setting Operation Mode %S ***"),err,&operationModeUid);	
   1.242 +								User::Leave(err);
   1.243 +								}
   1.244 +							else
   1.245 +								{
   1.246 +								INFO_PRINTF2(_L("*** OPERATION MODE SET : %S ***"),&operationModeUid);	
   1.247 +								}
   1.248 +								
   1.249 +							//Create a NULL TCharacteristics pointer
   1.250 +							const TCharacteristics* charsPtrC(NULL);
   1.251 +						
   1.252 +							//Retrieve the characteristics for the hash implementation object
   1.253 +							TRAP_LOG(err, hashHmacImpl->GetCharacteristicsL(charsPtrC));
   1.254 +						
   1.255 +							//Static cast the characteristics to type THashCharacteristics
   1.256 +							const THashCharacteristics* hashCharsPtrC = static_cast<const THashCharacteristics*>(charsPtrC);
   1.257 +							
   1.258 +							//The hash output size is returned in Bits, divide by 8 to get the Byte size
   1.259 +							hashSize = hashCharsPtrC->iOutputSize/8;
   1.260 +							
   1.261 +							//Retrieve the final 8bit hash value and convert to 16bit												
   1.262 +							HBufC* hashDataC = HBufC::NewLC(hashSize);
   1.263 +							TPtr hashPtrC = hashDataC->Des();
   1.264 +							
   1.265 +							hashPtrC.Copy(hashHmacImpl->Hash(*sourceData));
   1.266 +							
   1.267 +							//Take the 16bit descriptor and convert the string to hexadecimal
   1.268 +							convertHash.Set(hashPtrC);
   1.269 +							HBufC* resultC = convertHash.HexStringLC();							
   1.270 +							
   1.271 +							INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*resultC);
   1.272 +							INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
   1.273 +									
   1.274 +							if(*resultC == expectedHash)
   1.275 +								{
   1.276 +								INFO_PRINTF1(_L("*** FINAL HASH VALID - STAGE 3 PASS ***"));
   1.277 +								INFO_PRINTF1(_L("*** Hmac - Set Operation Mode Checking : PASS ***"));
   1.278 +								SetTestStepResult(EPass);
   1.279 +								}
   1.280 +							else
   1.281 +								{
   1.282 +								ERR_PRINTF1(_L("*** STAGE 3 FAIL: Hash and Expected Value Mismatch ***"));
   1.283 +								SetTestStepResult(EFail);	
   1.284 +								}
   1.285 +								
   1.286 +							CleanupStack::PopAndDestroy(resultC);
   1.287 +							CleanupStack::PopAndDestroy(hashDataC);
   1.288 +							}
   1.289 +						else
   1.290 +							{
   1.291 +							ERR_PRINTF1(_L("*** STAGE 2 FAIL: Hash and Expected Value Mismatch ***"));
   1.292 +							SetTestStepResult(EFail);	
   1.293 +							}
   1.294 +							
   1.295 +						CleanupStack::PopAndDestroy(resultB);
   1.296 +						CleanupStack::PopAndDestroy(hashDataB);	
   1.297 +						}
   1.298 +					else
   1.299 +						{
   1.300 +						ERR_PRINTF1(_L("*** STAGE 1 FAIL: Hash and Expected Value Match ***"));
   1.301 +						SetTestStepResult(EFail);	
   1.302 +						}
   1.303 +						
   1.304 +					CleanupStack::PopAndDestroy(resultA);
   1.305 +					CleanupStack::PopAndDestroy(hashDataA);
   1.306 +					CleanupStack::PopAndDestroy(hashHmacImpl);										
   1.307 +					}
   1.308 +				else if(err==KErrNotSupported)
   1.309 +					{
   1.310 +					if((((TUid)operationModeUid != KHashModeUid) && ((TUid)operationModeUid != KHmacModeUid)) ||
   1.311 +						((TUid)algorithmUid != KMd2Uid) && (TUid)algorithmUid != KMd5Uid && (TUid)algorithmUid != KSha1Uid && (TUid)algorithmUid != KMd4Uid && (TUid)algorithmUid != KSha224Uid && (TUid)algorithmUid != KSha256Uid && (TUid)algorithmUid != KSha384Uid && (TUid)algorithmUid != KSha512Uid)
   1.312 +						{
   1.313 +						ERR_PRINTF2(_L("*** Object Load Failure - Invalid Operation Mode : %d ***"), err);
   1.314 +						User::Leave(err);	
   1.315 +						}
   1.316 +					else
   1.317 +						{
   1.318 +						SetTestStepResult(EFail);	
   1.319 +						}
   1.320 +					}
   1.321 +				else
   1.322 +					{
   1.323 +					ERR_PRINTF2(_L("*** Hash/Hmac Facotry Object Load Failure : %d ***"), err);
   1.324 +					User::Leave(err);	
   1.325 +					}
   1.326 +					
   1.327 +				CleanupStack::PopAndDestroy(key);
   1.328 +				CleanupStack::PopAndDestroy(keyParams);
   1.329 +				CleanupStack::PopAndDestroy(keyStr);
   1.330 +				}
   1.331 +							
   1.332 +			CleanupStack::PopAndDestroy(sourceData);
   1.333 +								
   1.334 +			//Cleanup the Source RFile	
   1.335 +			CleanupStack::PopAndDestroy();	
   1.336 +						
   1.337 +			fsSession.Close();	
   1.338 +			}
   1.339 +			
   1.340 +		}
   1.341 +		else
   1.342 +		{
   1.343 +			SetTestStepResult(EFail);	
   1.344 +		}		
   1.345 +		
   1.346 +	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());	
   1.347 +	
   1.348 +	return TestStepResult();
   1.349 +	}
   1.350 +
   1.351 +
   1.352 +TVerdict CHmacSetOperationModeCheckingStep::doTestStepPostambleL()
   1.353 +	{
   1.354 +	return TestStepResult();
   1.355 +	}