os/security/crypto/weakcryptospi/test/tcryptospi/src/hmacbasichashofdatastep.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/hmacbasichashofdatastep.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,230 @@
     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 "hmacbasichashofdatastep.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 +CHmacBasicHashOfDataStep::~CHmacBasicHashOfDataStep()
    1.36 +	{
    1.37 +	}
    1.38 +
    1.39 +
    1.40 +CHmacBasicHashOfDataStep::CHmacBasicHashOfDataStep()
    1.41 +	{
    1.42 +	SetTestStepName(KHmacBasicHashOfDataStep);
    1.43 +	}
    1.44 +
    1.45 +
    1.46 +TVerdict CHmacBasicHashOfDataStep::doTestStepPreambleL()
    1.47 +	{
    1.48 +	SetTestStepResult(EPass);
    1.49 +	return TestStepResult();
    1.50 +	}
    1.51 +
    1.52 +
    1.53 +TVerdict CHmacBasicHashOfDataStep::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 - Basic Hash of Data ***"));
    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 encryptKey;
    1.69 +		TVariantPtrC keyType;
    1.70 +		
    1.71 +		//Extract the Test Case ID parameter from the specified INI file
    1.72 +		if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
    1.73 +			!GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
    1.74 +			!GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
    1.75 +			!GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash)||
    1.76 +			!GetStringFromConfig(ConfigSection(),KConfigEncryptKey,encryptKey) ||
    1.77 +			!GetStringFromConfig(ConfigSection(),KConfigEncryptKeyType,keyType))
    1.78 +			{
    1.79 +			ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
    1.80 +			SetTestStepResult(EFail);
    1.81 +			}
    1.82 +		else
    1.83 +			{
    1.84 +			//Create a pointer for the Hash + Key (Hmac) Implementation Object
    1.85 +			CHash* hmacImpl = NULL;
    1.86 +			
    1.87 +			//Convert encryption key to an 8 Bit Descriptor
    1.88 +			HBufC8* keyStr = HBufC8::NewLC(encryptKey.Length());
    1.89 +			TPtr8 keyStrPtr = keyStr->Des();
    1.90 +			
    1.91 +			keyStrPtr.Copy(encryptKey);
    1.92 +						
    1.93 +			//Create an new CryptoParams object to encapsulate the key type and secret key string
    1.94 +			CCryptoParams* keyParams = CCryptoParams::NewL();
    1.95 +			CleanupStack::PushL(keyParams);
    1.96 +			keyParams->AddL(*keyStr,keyType);
    1.97 +			
    1.98 +			//Create Key Object
    1.99 +			TKeyProperty keyProperty;
   1.100 +			CKey* key=CKey::NewL(keyProperty,*keyParams);
   1.101 +			CleanupStack::PushL(key);
   1.102 +			
   1.103 +			//Retrieve a Hmac Factory Object			
   1.104 +			TRAPD(err,CHashFactory::CreateHashL(hmacImpl,
   1.105 +												algorithmUid,
   1.106 +												operationModeUid,
   1.107 +												key,
   1.108 +												NULL));											
   1.109 +	
   1.110 +			if(hmacImpl && (err == KErrNone))
   1.111 +				{
   1.112 +				//Push the Hmac Implementation Object onto the Cleanup Stack
   1.113 +				CleanupStack::PushL(hmacImpl);
   1.114 +				
   1.115 +				RFs fsSession;
   1.116 +				
   1.117 +				//Create a connection to the file server	
   1.118 +				err = fsSession.Connect();
   1.119 +					
   1.120 +				if(err != KErrNone)
   1.121 +					{
   1.122 +					ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err);
   1.123 +					SetTestStepResult(EFail);
   1.124 +					}	
   1.125 +				else
   1.126 +					{
   1.127 +					RFile sourceFile;
   1.128 +	    			CleanupClosePushL(sourceFile);
   1.129 +	    			
   1.130 +	    			//Open the specified source file		
   1.131 +	    			err = sourceFile.Open(fsSession,sourcePath, EFileRead);
   1.132 +	    					
   1.133 +	    			if(err != KErrNone)
   1.134 +						{
   1.135 +						ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err);
   1.136 +						SetTestStepResult(EFail);
   1.137 +						}
   1.138 +					else
   1.139 +						{
   1.140 +						TInt sourceLength = 0;
   1.141 +						User::LeaveIfError(sourceFile.Size(sourceLength));
   1.142 +						
   1.143 +						//Create a heap based descriptor to store the data
   1.144 +						HBufC8* sourceData = HBufC8::NewL(sourceLength);						
   1.145 +						CleanupStack::PushL(sourceData);
   1.146 +						TPtr8 sourcePtr = sourceData->Des();
   1.147 +						
   1.148 +						sourceFile.Read(sourcePtr);
   1.149 +							
   1.150 +						if(sourcePtr.Length() != sourceLength)
   1.151 +							{
   1.152 +							ERR_PRINTF1(_L("*** Error: Reading Source File ***"));
   1.153 +							SetTestStepResult(EFail);	
   1.154 +							}
   1.155 +						else
   1.156 +							{
   1.157 +							//Create a NULL TCharacteristics pointer
   1.158 +							const TCharacteristics* charsPtr(NULL);
   1.159 +							
   1.160 +							//Retrieve the characteristics for the hash implementation object
   1.161 +							TRAP_LOG(err, hmacImpl->GetCharacteristicsL(charsPtr));
   1.162 +							
   1.163 +							//Static cast the characteristics to type THashCharacteristics
   1.164 +							const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
   1.165 +							
   1.166 +							//The hash output size is returned in Bits, divide by 8 to get the Byte size
   1.167 +							TInt hashSize = hashCharsPtr->iOutputSize/8;
   1.168 +							
   1.169 +							//Retrieve the final 8bit hash value and convert to 16bit												
   1.170 +							HBufC* hashData = HBufC::NewLC(hashSize);
   1.171 +							TPtr hashPtr = hashData->Des();
   1.172 +							
   1.173 +							hashPtr.Copy(hmacImpl->Hash(*sourceData));
   1.174 +							
   1.175 +							//Take the 16bit descriptor and convert the string to hexadecimal
   1.176 +							TVariantPtrC convertHash;
   1.177 +							convertHash.Set(hashPtr);
   1.178 +							HBufC* hmacResult = convertHash.HexStringLC();								
   1.179 +							
   1.180 +							INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hmacResult);
   1.181 +							INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
   1.182 +							
   1.183 +							//If the returned hash value matches the expected hash, Pass the test	
   1.184 +							if(*hmacResult == expectedHash)
   1.185 +								{
   1.186 +								INFO_PRINTF1(_L("*** Hmac - Basic Hash of Data : PASS ***"));
   1.187 +								SetTestStepResult(EPass);	
   1.188 +								}
   1.189 +							else
   1.190 +								{
   1.191 +								ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch  ***"), err);
   1.192 +								SetTestStepResult(EFail);	
   1.193 +								}
   1.194 +								
   1.195 +							CleanupStack::PopAndDestroy(hmacResult);
   1.196 +							CleanupStack::PopAndDestroy(hashData);						
   1.197 +							}
   1.198 +							
   1.199 +
   1.200 +						CleanupStack::PopAndDestroy(sourceData);
   1.201 +						}
   1.202 +						
   1.203 +					//Cleanup the Source RFile	
   1.204 +					CleanupStack::PopAndDestroy();	
   1.205 +					}
   1.206 +					
   1.207 +				fsSession.Close();	
   1.208 +				
   1.209 +				CleanupStack::PopAndDestroy(hmacImpl);
   1.210 +				}
   1.211 +			else
   1.212 +				{
   1.213 +				ERR_PRINTF2(_L("*** FAIL: Failed to Create Hmac Object - %d ***"), err);
   1.214 +				SetTestStepResult(EFail);	
   1.215 +				}
   1.216 +			
   1.217 +			CleanupStack::PopAndDestroy(key);
   1.218 +			CleanupStack::PopAndDestroy(keyParams);
   1.219 +			CleanupStack::PopAndDestroy(keyStr);
   1.220 +			}
   1.221 +		}
   1.222 +		
   1.223 +	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());	
   1.224 +	
   1.225 +	return TestStepResult();
   1.226 +	}
   1.227 +
   1.228 +
   1.229 +TVerdict CHmacBasicHashOfDataStep::doTestStepPostambleL()
   1.230 +	{
   1.231 +	
   1.232 +	return TestStepResult();
   1.233 +	}