Update contrib.
2 * Copyright (c) 2007-2010 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 "hmacincrementalhmacstep.h"
26 #include <cryptospi/cryptohashapi.h>
27 #include <cryptospi/keys.h>
28 #include <cryptospi/plugincharacteristics.h>
30 using namespace CryptoSpi;
32 CHmacIncrementalHmacStep::~CHmacIncrementalHmacStep()
37 CHmacIncrementalHmacStep::CHmacIncrementalHmacStep()
39 SetTestStepName(KHmacIncrementalHmacStep);
43 TVerdict CHmacIncrementalHmacStep::doTestStepPreambleL()
45 SetTestStepResult(EPass);
46 return TestStepResult();
50 TVerdict CHmacIncrementalHmacStep::doTestStepL()
52 if (TestStepResult()==EPass)
54 //Assume faliure, unless all is successful
55 SetTestStepResult(EFail);
57 INFO_PRINTF1(_L("*** Hmac - Incremental Hash ***"));
58 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
60 TVariantPtrC algorithmUid;
61 TVariantPtrC operationModeUid;
67 //Extract the Test Case ID parameter from the specified INI file
68 if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
69 !GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
70 !GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
71 !GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash) ||
72 !GetStringFromConfig(ConfigSection(),KConfigEncryptKey,encryptKey) ||
73 !GetStringFromConfig(ConfigSection(),KConfigEncryptKeyType,keyType))
75 ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
76 SetTestStepResult(EFail);
80 //Create a pointer for the Hash + Key (Hamc) Implementation Object
81 CHash* hmacImpl = NULL;
83 //Convert encryption key to an 8 Bit Descriptor
84 HBufC8* keyStr = HBufC8::NewLC(encryptKey.Length());
85 TPtr8 keyStrPtr = keyStr->Des();
87 keyStrPtr.Copy(encryptKey);
89 //Create an new CryptoParams object to encapsulate the key type and secret key string
90 CCryptoParams* keyParams = CCryptoParams::NewL();
91 CleanupStack::PushL(keyParams);
92 keyParams->AddL(*keyStr,keyType);
95 TKeyProperty keyProperty;
96 CKey* key=CKey::NewL(keyProperty,*keyParams);
97 CleanupStack::PushL(key);
99 //Retrieve a Hmac Factory Object
100 TRAPD(err,CHashFactory::CreateHashL(hmacImpl,
106 if(hmacImpl && (err == KErrNone))
109 //Push the Hmac Implementation Object onto the Cleanup Stack
110 CleanupStack::PushL(hmacImpl);
114 //Create a connection to the file server
115 err = fsSession.Connect();
119 ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err);
120 SetTestStepResult(EFail);
125 CleanupClosePushL(sourceFile);
127 //Open the specified source file
128 err = sourceFile.Open(fsSession,sourcePath, EFileRead);
132 ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err);
133 SetTestStepResult(EFail);
138 TInt sourceLength = 0;
139 TInt readPosition = 0;
140 TInt readIncrement = 0;
141 TBool hashComplete = EFalse;
144 User::LeaveIfError(sourceFile.Size(sourceLength));
146 //Divide the total size of the source file up into individual equal sized blocks to read
147 //over several increments
148 readIncrement = sourceLength/KDataReadBlocks;
152 //Create a heap based descriptor to store the data
153 HBufC8* sourceData = HBufC8::NewL(readIncrement);
154 CleanupStack::PushL(sourceData);
155 TPtr8 sourcePtr = sourceData->Des();
157 //Read in a block of data from the source file from the current position
158 err = sourceFile.Read(readPosition,sourcePtr,readIncrement);
160 //Update the read position by adding the number of bytes read
161 readPosition += readIncrement;
163 if(readPosition == readIncrement)
165 //Read in the first block from the data file into the Hmac implementation object
166 hmacImpl->Hash(*sourceData);
167 INFO_PRINTF2(_L("Intial Hmac - Bytes Read: %d"), readPosition);
169 else if(readPosition >= sourceLength)
171 //Reading in the final block, constructs the complete hash value and returns it within a TPtrC8
172 hashStr.Set(hmacImpl->Final(*sourceData));
174 //Sets the Complete Flag to ETrue in order to drop out of the loop
175 hashComplete = ETrue;
177 TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
178 INFO_PRINTF2(_L("Final Hmac - Bytes Read: %d"),totalRead);
182 //Update the message data within the Hmac object with the new block
183 hmacImpl->Update(*sourceData);
184 INFO_PRINTF2(_L("Hmac Update - Bytes Read: %d"), readPosition);
187 CleanupStack::PopAndDestroy(sourceData);
189 }while(hashComplete == EFalse);
191 //Create a NULL TCharacteristics pointer
192 const TCharacteristics* charsPtr(NULL);
194 //Retrieve the characteristics for the hash implementation object
195 TRAP_LOG(err, hmacImpl->GetCharacteristicsL(charsPtr));
197 //Static cast the characteristics to type THashCharacteristics
198 const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
200 //The hash output size is returned in Bits, divide by 8 to get the Byte size
201 TInt hashSize = hashCharsPtr->iOutputSize/8;
203 //Retrieve the final 8bit hash value and convert to 16bit
204 HBufC* hashData = HBufC::NewLC(hashSize);
205 TPtr hashPtr = hashData->Des();
207 hashPtr.Copy(hashStr);
209 //Take the 16bit descriptor and convert the string to hexadecimal
210 TVariantPtrC convertHash;
211 convertHash.Set(hashPtr);
212 HBufC* hmacResult = convertHash.HexStringLC();
214 INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hmacResult);
215 INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
217 //If the returned hash value matches the expected hash, Pass the test
218 if(*hmacResult == expectedHash)
220 INFO_PRINTF1(_L("*** Hmac - Incremental Hash : PASS ***"));
221 SetTestStepResult(EPass);
225 ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err);
226 SetTestStepResult(EFail);
229 CleanupStack::PopAndDestroy(hmacResult);
230 CleanupStack::PopAndDestroy(hashData);
233 //Cleanup the Source RFile
234 CleanupStack::PopAndDestroy();
239 CleanupStack::PopAndDestroy(hmacImpl);
243 ERR_PRINTF2(_L("*** FAIL: Failed to Create Hmac Object - %d ***"), err);
244 SetTestStepResult(EFail);
247 CleanupStack::PopAndDestroy(key);
248 CleanupStack::PopAndDestroy(keyParams);
249 CleanupStack::PopAndDestroy(keyStr);
253 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
254 return TestStepResult();
258 TVerdict CHmacIncrementalHmacStep::doTestStepPostambleL()
260 return TestStepResult();