os/security/crypto/weakcryptospi/test/tcryptospi/src/hmacsetkeycheckingstep.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/hmacsetkeycheckingstep.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,285 @@
     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 "hmacsetkeycheckingstep.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 +CHmacSetKeyCheckingStep::~CHmacSetKeyCheckingStep()
    1.36 +	{
    1.37 +	}
    1.38 +
    1.39 +
    1.40 +CHmacSetKeyCheckingStep::CHmacSetKeyCheckingStep()
    1.41 +	{
    1.42 +	SetTestStepName(KHmacSetKeyCheckingStep);
    1.43 +	}
    1.44 +
    1.45 +
    1.46 +TVerdict CHmacSetKeyCheckingStep::doTestStepPreambleL()
    1.47 +	{
    1.48 +	SetTestStepResult(EPass);
    1.49 +	return TestStepResult();
    1.50 +	}
    1.51 +
    1.52 +
    1.53 +TVerdict CHmacSetKeyCheckingStep::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 Key Checking ***"));
    1.62 +		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
    1.63 +		
    1.64 +		TVariantPtrC algorithmUid;
    1.65 +		TVariantPtrC operationModeUid;
    1.66 +		TPtrC sourcePath;
    1.67 +		TPtrC expectedHash;
    1.68 +		TPtrC invalidEncryptKey;
    1.69 +		TPtrC encryptKey;
    1.70 +		TVariantPtrC keyType;
    1.71 +		
    1.72 +		//Extract the Test Case ID parameter from the specified INI file
    1.73 +		if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
    1.74 +			!GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
    1.75 +			!GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
    1.76 +			!GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash) ||
    1.77 +			!GetStringFromConfig(ConfigSection(),KConfigInvalidKey,invalidEncryptKey) ||
    1.78 +			!GetStringFromConfig(ConfigSection(),KConfigEncryptKey,encryptKey) ||
    1.79 +			!GetStringFromConfig(ConfigSection(),KConfigEncryptKeyType,keyType))
    1.80 +			{
    1.81 +			ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
    1.82 +			SetTestStepResult(EFail);
    1.83 +			}
    1.84 +		else
    1.85 +			{	
    1.86 +			RFs fsSession;
    1.87 +				
    1.88 +			//Create a connection to the file server	
    1.89 +			User::LeaveIfError(fsSession.Connect());
    1.90 +						
    1.91 +			RFile sourceFile;
    1.92 +	    	CleanupClosePushL(sourceFile);
    1.93 +	    			
    1.94 +	    	//Open the specified source file		
    1.95 +	    	User::LeaveIfError(sourceFile.Open(fsSession,sourcePath, EFileRead));
    1.96 +	    					
    1.97 +			TInt sourceLength = 0;
    1.98 +			User::LeaveIfError(sourceFile.Size(sourceLength));
    1.99 +						
   1.100 +			//Create a heap based descriptor to store the data
   1.101 +			HBufC8* sourceData = HBufC8::NewL(sourceLength);						
   1.102 +			CleanupStack::PushL(sourceData);
   1.103 +			TPtr8 sourcePtr = sourceData->Des();
   1.104 +						
   1.105 +			sourceFile.Read(sourcePtr);
   1.106 +							
   1.107 +			if(sourcePtr.Length() != sourceLength)
   1.108 +				{
   1.109 +				ERR_PRINTF1(_L("*** Error: Reading Source File ***"));
   1.110 +				SetTestStepResult(EFail);	
   1.111 +				}
   1.112 +			else
   1.113 +				{
   1.114 +				//Create a pointer for the Hash + Key (Hmac) Implementation Object
   1.115 +				CHash* hmacImpl = NULL;
   1.116 +
   1.117 +				//Convert encryption key to an 8 Bit Descriptor
   1.118 +				HBufC8* invalidKeyStr = HBufC8::NewLC(invalidEncryptKey.Length());
   1.119 +				TPtr8 invalidKeyStrPtr = invalidKeyStr->Des();
   1.120 +			
   1.121 +				invalidKeyStrPtr.Copy(invalidEncryptKey);
   1.122 +				
   1.123 +				//Create an new CryptoParams object to encapsulate the invalid key type and key string
   1.124 +				CCryptoParams* invalidKeyParams = CCryptoParams::NewL();
   1.125 +				CleanupStack::PushL(invalidKeyParams);
   1.126 +				invalidKeyParams->AddL(*invalidKeyStr,keyType);
   1.127 +				
   1.128 +				//Create Invalid Key Object
   1.129 +				TKeyProperty invalidKeyProperty;
   1.130 +				CKey* invalidKey = CKey::NewL(invalidKeyProperty,*invalidKeyParams);
   1.131 +				CleanupStack::PushL(invalidKey);
   1.132 +				
   1.133 +				//Retrieve a Hmac Factory Object with an Invalid Key			
   1.134 +				TRAPD(err,CHashFactory::CreateHashL(hmacImpl,
   1.135 +													algorithmUid,
   1.136 +													operationModeUid,
   1.137 +													invalidKey,
   1.138 +													NULL));						
   1.139 +				
   1.140 +				if(hmacImpl && (err == KErrNone))
   1.141 +					{
   1.142 +					
   1.143 +					//Push the Hmac Implementation Object onto the Cleanup Stack
   1.144 +					CleanupStack::PushL(hmacImpl);
   1.145 +										
   1.146 +					//Create a NULL TCharacteristics pointer
   1.147 +					const TCharacteristics* invalidCharsPtr(NULL);
   1.148 +					
   1.149 +					//Retrieve the characteristics for the hash implementation object
   1.150 +					TRAP_LOG(err, hmacImpl->GetCharacteristicsL(invalidCharsPtr));
   1.151 +					
   1.152 +					//Static cast the characteristics to type THashCharacteristics
   1.153 +					const THashCharacteristics* hashInvalidCharsPtr = static_cast<const THashCharacteristics*>(invalidCharsPtr);
   1.154 +					
   1.155 +					//The hash output size is returned in Bits, divide by 8 to get the Byte size
   1.156 +					TInt hashSize = hashInvalidCharsPtr->iOutputSize/8;
   1.157 +					
   1.158 +					//Retrieve the final 8bit hash value and convert to 16bit												
   1.159 +					HBufC* invalidHashData = HBufC::NewLC(hashSize);
   1.160 +					TPtr invalidHashPtr = invalidHashData->Des();
   1.161 +					
   1.162 +					invalidHashPtr.Copy(hmacImpl->Hash(*sourceData));
   1.163 +					
   1.164 +					//Take the 16bit descriptor and convert the string to hexadecimal
   1.165 +					TVariantPtrC convertHash;
   1.166 +					convertHash.Set(invalidHashPtr);
   1.167 +					HBufC* invalidHmacResult = convertHash.HexStringLC();							
   1.168 +						
   1.169 +					INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*invalidHmacResult);
   1.170 +					INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
   1.171 +					
   1.172 +					if(*invalidHmacResult != expectedHash)
   1.173 +						{
   1.174 +						INFO_PRINTF1(_L("*** INVALID KEY - STAGE 1 PASS ***"));
   1.175 +						
   1.176 +						//Convert encryption key to an 8 Bit Descriptor
   1.177 +						HBufC8* validKeyStr = HBufC8::NewLC(encryptKey.Length());
   1.178 +						TPtr8 validKeyStrPtr = validKeyStr->Des();
   1.179 +			
   1.180 +						validKeyStrPtr.Copy(encryptKey);
   1.181 +						
   1.182 +						//Create an new CryptoParams object to encapsulate the valid key type and secret key string
   1.183 +						CCryptoParams* validKeyParams = CCryptoParams::NewL();
   1.184 +						CleanupStack::PushL(validKeyParams);
   1.185 +						validKeyParams->AddL(*validKeyStr,keyType);
   1.186 +						
   1.187 +						//Create Valid Key Object
   1.188 +						TKeyProperty validKeyProperty;
   1.189 +						CKey* validKey = CKey::NewL(validKeyProperty,*validKeyParams);
   1.190 +						CleanupStack::PushL(validKey);
   1.191 +												
   1.192 +						//Set the valid key within the Hmac Implementation Object
   1.193 +						TRAP(err,hmacImpl->SetKeyL(*validKey));
   1.194 +						
   1.195 +						if(err!=KErrNone)
   1.196 +							{
   1.197 +							ERR_PRINTF2(_L("*** ERROR %d: Setting Valid Key ***"),err);
   1.198 +							User::Leave(err);	
   1.199 +							}
   1.200 +						else
   1.201 +							{
   1.202 +							INFO_PRINTF1(_L("*** HMAC VALID KEY SET ***"));	
   1.203 +							}
   1.204 +							
   1.205 +						//Create a NULL TCharacteristics pointer
   1.206 +						const TCharacteristics* validCharsPtr(NULL);
   1.207 +					
   1.208 +						//Retrieve the characteristics for the hash implementation object
   1.209 +						TRAP_LOG(err, hmacImpl->GetCharacteristicsL(validCharsPtr));
   1.210 +					
   1.211 +						//Static cast the characteristics to type THashCharacteristics
   1.212 +						const THashCharacteristics* hashValidCharsPtr = static_cast<const THashCharacteristics*>(validCharsPtr);
   1.213 +						
   1.214 +						//The hash output size is returned in Bits, divide by 8 to get the Byte size
   1.215 +						hashSize = hashValidCharsPtr->iOutputSize/8;
   1.216 +						
   1.217 +						//Retrieve the final 8bit hash value and convert to 16bit												
   1.218 +						HBufC* validHashData = HBufC::NewLC(hashSize);
   1.219 +						TPtr validHashPtr = validHashData->Des();
   1.220 +						
   1.221 +						validHashPtr.Copy(hmacImpl->Hash(*sourceData));
   1.222 +						
   1.223 +						//Take the 16bit descriptor and convert the string to hexadecimal
   1.224 +						convertHash.Set(validHashPtr);
   1.225 +						HBufC* validHmacResult = convertHash.HexStringLC();							
   1.226 +						
   1.227 +						INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*validHmacResult);
   1.228 +						INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
   1.229 +						
   1.230 +						if(*validHmacResult == expectedHash)
   1.231 +							{
   1.232 +							INFO_PRINTF1(_L("*** VALID KEY - STAGE 2 PASS ***"));
   1.233 +							INFO_PRINTF1(_L("*** Hmac - Set Key Checking : PASS ***"));
   1.234 +							SetTestStepResult(EPass);
   1.235 +							}
   1.236 +						else
   1.237 +							{
   1.238 +							ERR_PRINTF1(_L("*** STAGE 2 FAIL: Valid Hash and Expected Value Mismatch ***"));
   1.239 +							SetTestStepResult(EFail);	
   1.240 +							}
   1.241 +						CleanupStack::PopAndDestroy(validHmacResult);
   1.242 +						CleanupStack::PopAndDestroy(validHashData);
   1.243 +						
   1.244 +						CleanupStack::PopAndDestroy(validKey);
   1.245 +						CleanupStack::PopAndDestroy(validKeyParams);
   1.246 +						CleanupStack::PopAndDestroy(validKeyStr);	
   1.247 +						}
   1.248 +					else
   1.249 +						{
   1.250 +						ERR_PRINTF1(_L("*** STAGE 1 FAIL: Invalid Hash and Expected Value Match ***"));
   1.251 +						SetTestStepResult(EFail);	
   1.252 +						}
   1.253 +						
   1.254 +					CleanupStack::PopAndDestroy(invalidHmacResult);	
   1.255 +					CleanupStack::PopAndDestroy(invalidHashData);
   1.256 +					CleanupStack::PopAndDestroy(hmacImpl);									
   1.257 +					}
   1.258 +				else
   1.259 +					{
   1.260 +					ERR_PRINTF2(_L("*** FAIL: Failed to Create Hmac Object - %d ***"), err);
   1.261 +					SetTestStepResult(EFail);	
   1.262 +					}
   1.263 +					
   1.264 +				CleanupStack::PopAndDestroy(invalidKey);
   1.265 +				CleanupStack::PopAndDestroy(invalidKeyParams);
   1.266 +				CleanupStack::PopAndDestroy(invalidKeyStr);
   1.267 +				}
   1.268 +						
   1.269 +			CleanupStack::PopAndDestroy(sourceData);
   1.270 +								
   1.271 +			//Cleanup the Source RFile	
   1.272 +			CleanupStack::PopAndDestroy();	
   1.273 +						
   1.274 +			fsSession.Close();	
   1.275 +			}
   1.276 +			
   1.277 +			
   1.278 +		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());	
   1.279 +		}
   1.280 +		
   1.281 +	return TestStepResult();
   1.282 +	}
   1.283 +
   1.284 +
   1.285 +TVerdict CHmacSetKeyCheckingStep::doTestStepPostambleL()
   1.286 +	{
   1.287 +	return TestStepResult();
   1.288 +	}