os/security/crypto/weakcryptospi/test/tcryptospi/src/hashincrementalhashwithresetstep.cpp
Update contrib.
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Example CTestStep derived implementation
24 #include "hashincrementalhashwithresetstep.h"
26 #include <cryptospi/cryptohashapi.h>
27 #include <cryptospi/plugincharacteristics.h>
29 using namespace CryptoSpi;
31 CHashIncrementalHashWithResetStep::~CHashIncrementalHashWithResetStep()
36 CHashIncrementalHashWithResetStep::CHashIncrementalHashWithResetStep()
38 SetTestStepName(KHashIncrementalHashWithResetStep);
42 TVerdict CHashIncrementalHashWithResetStep::doTestStepPreambleL()
44 SetTestStepResult(EPass);
45 return TestStepResult();
49 TVerdict CHashIncrementalHashWithResetStep::doTestStepL()
51 if (TestStepResult()==EPass)
54 //Assume faliure, unless all is successful
55 SetTestStepResult(EFail);
57 INFO_PRINTF1(_L("*** Hash - Incremental Hash with Reset ***"));
58 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
60 TVariantPtrC algorithmUid;
61 TVariantPtrC operationModeUid;
65 //Extract the Test Case ID parameter from the specified INI file
66 if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
67 !GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
68 !GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
69 !GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash))
71 ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
72 SetTestStepResult(EFail);
76 //Create a pointer for the Hash Implementation Object
77 CHash* hashImpl = NULL;
79 //Retrieve a Hash Factory Object
80 TRAPD(err,CHashFactory::CreateHashL(hashImpl,
86 if(hashImpl && (err == KErrNone))
89 //Push the Hash Implementation Object onto the Cleanup Stack
90 CleanupStack::PushL(hashImpl);
94 //Create a connection to the file server
95 err = fsSession.Connect();
99 ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err);
100 SetTestStepResult(EFail);
105 CleanupClosePushL(sourceFile);
107 //Open the specified source file
108 err = sourceFile.Open(fsSession,sourcePath, EFileRead);
112 ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err);
113 SetTestStepResult(EFail);
118 TInt sourceLength = 0;
119 TInt readPosition = 0;
120 TInt readIncrement = 0;
121 TBool hashComplete = EFalse;
122 TBool hashReset = EFalse;
125 User::LeaveIfError(sourceFile.Size(sourceLength));
127 //Divide the file size into seperate incremental blocks to read
128 readIncrement = sourceLength/KDataReadBlocks;
132 //Create a heap based descriptor to store the data
133 HBufC8* sourceData = HBufC8::NewL(readIncrement);
134 CleanupStack::PushL(sourceData);
135 TPtr8 sourcePtr = sourceData->Des();
137 //Read in a block of data from the source file from the current position
138 err = sourceFile.Read(readPosition,sourcePtr,readIncrement);
140 //Update the read position by adding the number of bytes read
141 readPosition += readIncrement;
143 if(readPosition == readIncrement)
145 //Read in the first block from the data file into the Hash implementation object
146 hashImpl->Hash(*sourceData);
147 INFO_PRINTF2(_L("Intial Hash - Bytes Read: %d"), readPosition);
149 else if(readPosition >= sourceLength)
151 //Reading in the final block, constructs the complete hash value and returns it within a TPtrC8
152 hashStr.Set(hashImpl->Final(*sourceData));
154 //Sets the Complete Flag to ETrue in order to drop out of the loop
155 hashComplete = ETrue;
157 TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
158 INFO_PRINTF2(_L("Final Hash - Bytes Read: %d"),totalRead);
160 //If the read position is half the source length and the implementation
161 //object hasn't already been reset
162 else if((readPosition >= sourceLength/2) && (hashReset == EFalse))
164 INFO_PRINTF1(_L("Resetting Hash Object..."));
168 //Sets the read position back to 0 inorder to restart the file read from the beginning
173 INFO_PRINTF2(_L("*** HASH RESET - Bytes Read: %d ***"), readPosition);
177 //Update the message data within the Hash object with the new block
178 hashImpl->Update(*sourceData);
179 INFO_PRINTF2(_L("Hash Update - Bytes Read: %d"), readPosition);
182 CleanupStack::PopAndDestroy(sourceData);
184 }while(hashComplete == EFalse);
186 //Create a NULL TCharacteristics pointer
187 const TCharacteristics* charsPtr(NULL);
189 //Retrieve the characteristics for the hash implementation object
190 TRAP_LOG(err, hashImpl->GetCharacteristicsL(charsPtr));
192 //Static cast the characteristics to type THashCharacteristics
193 const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
195 //The hash output size is returned in Bits, divide by 8 to get the Byte size
196 TInt hashSize = hashCharsPtr->iOutputSize/8;
198 //Retrieve the final 8bit hash value and convert to 16bit
199 HBufC* hashData = HBufC::NewLC(hashSize);
200 TPtr hashPtr = hashData->Des();
202 //Copy the hashed content into the heap based descriptor
203 hashPtr.Copy(hashStr);
205 //Take the 16bit descriptor and convert the string to hexadecimal
206 TVariantPtrC convertHash;
207 convertHash.Set(hashPtr);
208 HBufC* hashResult = convertHash.HexStringLC();
210 INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hashResult);
211 INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
213 //If the returned hash value matches the expected hash, Pass the test
214 if(*hashResult == expectedHash)
216 INFO_PRINTF1(_L("*** Hash - Incremental Hash with Reset : PASS ***"));
217 SetTestStepResult(EPass);
221 ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err);
222 SetTestStepResult(EFail);
225 CleanupStack::PopAndDestroy(hashResult);
226 CleanupStack::PopAndDestroy(hashData);
229 //Cleanup the Source RFile
230 CleanupStack::PopAndDestroy();
235 CleanupStack::PopAndDestroy(hashImpl);
239 ERR_PRINTF2(_L("*** FAIL: Failed to Create Hash Object - %d ***"), err);
240 SetTestStepResult(EFail);
245 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
246 return TestStepResult();
250 TVerdict CHashIncrementalHashWithResetStep::doTestStepPostambleL()
253 return TestStepResult();