os/security/crypto/weakcryptospi/test/tcryptospi/src/hmacincrementalhmacwithresetstep.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tcryptospi/src/hmacincrementalhmacwithresetstep.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,277 @@
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 "hmacincrementalhmacwithresetstep.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 +CHmacIncrementalHmacWithResetStep::~CHmacIncrementalHmacWithResetStep()
1.36 + {
1.37 + }
1.38 +
1.39 +
1.40 +CHmacIncrementalHmacWithResetStep::CHmacIncrementalHmacWithResetStep()
1.41 + {
1.42 + SetTestStepName(KHmacIncrementalHmacWithResetStep);
1.43 + }
1.44 +
1.45 +
1.46 +TVerdict CHmacIncrementalHmacWithResetStep::doTestStepPreambleL()
1.47 + {
1.48 + SetTestStepResult(EPass);
1.49 + return TestStepResult();
1.50 + }
1.51 +
1.52 +
1.53 +TVerdict CHmacIncrementalHmacWithResetStep::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 - Incremental Hash with Reset ***"));
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 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 +
1.113 + //Push the Hmac Implementation Object onto the Cleanup Stack
1.114 + CleanupStack::PushL(hmacImpl);
1.115 +
1.116 + RFs fsSession;
1.117 +
1.118 + //Create a connection to the file server
1.119 + err = fsSession.Connect();
1.120 +
1.121 + if(err != KErrNone)
1.122 + {
1.123 + ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err);
1.124 + SetTestStepResult(EFail);
1.125 + }
1.126 + else
1.127 + {
1.128 + RFile sourceFile;
1.129 + CleanupClosePushL(sourceFile);
1.130 +
1.131 + //Open the specified source file
1.132 + err = sourceFile.Open(fsSession,sourcePath, EFileRead);
1.133 +
1.134 + if(err != KErrNone)
1.135 + {
1.136 + ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err);
1.137 + SetTestStepResult(EFail);
1.138 + }
1.139 + else
1.140 + {
1.141 +
1.142 + TInt sourceLength = 0;
1.143 + TInt readPosition = 0;
1.144 + TInt readIncrement = 0;
1.145 + TBool hashComplete = EFalse;
1.146 + TBool hashReset = EFalse;
1.147 + TPtrC8 hashStr;
1.148 +
1.149 + User::LeaveIfError(sourceFile.Size(sourceLength));
1.150 +
1.151 + //Divide the file size into seperate incremental blocks to read
1.152 + readIncrement = sourceLength/KDataReadBlocks;
1.153 +
1.154 + do
1.155 + {
1.156 + //Create a heap based descriptor to store the data
1.157 + HBufC8* sourceData = HBufC8::NewL(readIncrement);
1.158 + CleanupStack::PushL(sourceData);
1.159 + TPtr8 sourcePtr = sourceData->Des();
1.160 +
1.161 + //Read in a block of data from the source file from the current position
1.162 + err = sourceFile.Read(readPosition,sourcePtr,readIncrement);
1.163 +
1.164 + //Update the read position by adding the number of bytes read
1.165 + readPosition += readIncrement;
1.166 +
1.167 + if(readPosition == readIncrement)
1.168 + {
1.169 + //Read in the first block from the data file into the Hmac implementation object
1.170 + hmacImpl->Hash(*sourceData);
1.171 + INFO_PRINTF2(_L("Intial Hmac - Bytes Read: %d"), readPosition);
1.172 + }
1.173 + else if(readPosition >= sourceLength)
1.174 + {
1.175 + //Reading in the final block, constructs the complete hash value and returns it within a TPtrC8
1.176 + hashStr.Set(hmacImpl->Final(*sourceData));
1.177 +
1.178 + //Sets the Complete Flag to ETrue in order to drop out of the loop
1.179 + hashComplete = ETrue;
1.180 +
1.181 + TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
1.182 + INFO_PRINTF2(_L("Final Hmac - Bytes Read: %d"),totalRead);
1.183 + }
1.184 + //If the read position is half the source length and the implementation
1.185 + //object hasn't already been reset
1.186 + else if((readPosition >= sourceLength/2) && (hashReset == EFalse))
1.187 + {
1.188 + INFO_PRINTF1(_L("Resetting Hmac Object..."));
1.189 +
1.190 + hmacImpl->Reset();
1.191 +
1.192 + //Sets the read position back to 0 inorder to restart the file read from the beginning
1.193 + readPosition = 0;
1.194 +
1.195 + hashReset = ETrue;
1.196 +
1.197 + INFO_PRINTF2(_L("*** HMAC RESET - Bytes Read: %d ***"), readPosition);
1.198 + }
1.199 + else
1.200 + {
1.201 + //Update the message data within the Hmac object with the new block
1.202 + hmacImpl->Update(*sourceData);
1.203 + INFO_PRINTF2(_L("Hmac Update - Bytes Read: %d"), readPosition);
1.204 + }
1.205 +
1.206 + CleanupStack::PopAndDestroy(sourceData);
1.207 +
1.208 + }while(hashComplete == EFalse);
1.209 +
1.210 + //Create a NULL TCharacteristics pointer
1.211 + const TCharacteristics* charsPtr(NULL);
1.212 +
1.213 + //Retrieve the characteristics for the hash implementation object
1.214 + TRAP_LOG(err, hmacImpl->GetCharacteristicsL(charsPtr));
1.215 +
1.216 + //Static cast the characteristics to type THashCharacteristics
1.217 + const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
1.218 +
1.219 + //The hash output size is returned in Bits, divide by 8 to get the Byte size
1.220 + TInt hashSize = hashCharsPtr->iOutputSize/8;
1.221 +
1.222 + //Retrieve the final 8bit hash value and convert to 16bit
1.223 + HBufC* hashData = HBufC::NewLC(hashSize);
1.224 + TPtr hashPtr = hashData->Des();
1.225 +
1.226 + hashPtr.Copy(hashStr);
1.227 +
1.228 + //Take the 16bit descriptor and convert the string to hexadecimal
1.229 + TVariantPtrC convertHash;
1.230 + convertHash.Set(hashPtr);
1.231 + HBufC* hmacResult = convertHash.HexStringLC();
1.232 +
1.233 + INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hmacResult);
1.234 + INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
1.235 +
1.236 + //If the returned hash value matches the expected hash, Pass the test
1.237 + if(*hmacResult == expectedHash)
1.238 + {
1.239 + INFO_PRINTF1(_L("*** Hmac - Incremental Hash with Reset : PASS ***"));
1.240 + SetTestStepResult(EPass);
1.241 + }
1.242 + else
1.243 + {
1.244 + ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err);
1.245 + SetTestStepResult(EFail);
1.246 + }
1.247 +
1.248 + CleanupStack::PopAndDestroy(hmacResult);
1.249 + CleanupStack::PopAndDestroy(hashData);
1.250 + }
1.251 +
1.252 + //Cleanup the Source RFile
1.253 + CleanupStack::PopAndDestroy();
1.254 + }
1.255 +
1.256 + fsSession.Close();
1.257 +
1.258 + CleanupStack::PopAndDestroy(hmacImpl);
1.259 + }
1.260 + else
1.261 + {
1.262 + ERR_PRINTF2(_L("*** FAIL: Failed to Create Hmac Object - %d ***"), err);
1.263 + SetTestStepResult(EFail);
1.264 + }
1.265 +
1.266 + CleanupStack::PopAndDestroy(key);
1.267 + CleanupStack::PopAndDestroy(keyParams);
1.268 + CleanupStack::PopAndDestroy(keyStr);
1.269 + }
1.270 + }
1.271 +
1.272 + INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
1.273 + return TestStepResult();
1.274 + }
1.275 +
1.276 +
1.277 +TVerdict CHmacIncrementalHmacWithResetStep::doTestStepPostambleL()
1.278 + {
1.279 + return TestStepResult();
1.280 + }