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 "hmacbasichashofdatastep.h"
26 #include <cryptospi/cryptohashapi.h>
27 #include <cryptospi/keys.h>
28 #include <cryptospi/plugincharacteristics.h>
30 using namespace CryptoSpi;
32 CHmacBasicHashOfDataStep::~CHmacBasicHashOfDataStep()
37 CHmacBasicHashOfDataStep::CHmacBasicHashOfDataStep()
39 SetTestStepName(KHmacBasicHashOfDataStep);
43 TVerdict CHmacBasicHashOfDataStep::doTestStepPreambleL()
45 SetTestStepResult(EPass);
46 return TestStepResult();
50 TVerdict CHmacBasicHashOfDataStep::doTestStepL()
52 if (TestStepResult()==EPass)
55 //Assume faliure, unless all is successful
56 SetTestStepResult(EFail);
58 INFO_PRINTF1(_L("*** Hmac - Basic Hash of Data ***"));
59 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
61 TVariantPtrC algorithmUid;
62 TVariantPtrC operationModeUid;
68 //Extract the Test Case ID parameter from the specified INI file
69 if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
70 !GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
71 !GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
72 !GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash)||
73 !GetStringFromConfig(ConfigSection(),KConfigEncryptKey,encryptKey) ||
74 !GetStringFromConfig(ConfigSection(),KConfigEncryptKeyType,keyType))
76 ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
77 SetTestStepResult(EFail);
81 //Create a pointer for the Hash + Key (Hmac) Implementation Object
82 CHash* hmacImpl = NULL;
84 //Convert encryption key to an 8 Bit Descriptor
85 HBufC8* keyStr = HBufC8::NewLC(encryptKey.Length());
86 TPtr8 keyStrPtr = keyStr->Des();
88 keyStrPtr.Copy(encryptKey);
90 //Create an new CryptoParams object to encapsulate the key type and secret key string
91 CCryptoParams* keyParams = CCryptoParams::NewL();
92 CleanupStack::PushL(keyParams);
93 keyParams->AddL(*keyStr,keyType);
96 TKeyProperty keyProperty;
97 CKey* key=CKey::NewL(keyProperty,*keyParams);
98 CleanupStack::PushL(key);
100 //Retrieve a Hmac Factory Object
101 TRAPD(err,CHashFactory::CreateHashL(hmacImpl,
107 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);
137 TInt sourceLength = 0;
138 User::LeaveIfError(sourceFile.Size(sourceLength));
140 //Create a heap based descriptor to store the data
141 HBufC8* sourceData = HBufC8::NewL(sourceLength);
142 CleanupStack::PushL(sourceData);
143 TPtr8 sourcePtr = sourceData->Des();
145 sourceFile.Read(sourcePtr);
147 if(sourcePtr.Length() != sourceLength)
149 ERR_PRINTF1(_L("*** Error: Reading Source File ***"));
150 SetTestStepResult(EFail);
154 //Create a NULL TCharacteristics pointer
155 const TCharacteristics* charsPtr(NULL);
157 //Retrieve the characteristics for the hash implementation object
158 TRAP_LOG(err, hmacImpl->GetCharacteristicsL(charsPtr));
160 //Static cast the characteristics to type THashCharacteristics
161 const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
163 //The hash output size is returned in Bits, divide by 8 to get the Byte size
164 TInt hashSize = hashCharsPtr->iOutputSize/8;
166 //Retrieve the final 8bit hash value and convert to 16bit
167 HBufC* hashData = HBufC::NewLC(hashSize);
168 TPtr hashPtr = hashData->Des();
170 hashPtr.Copy(hmacImpl->Hash(*sourceData));
172 //Take the 16bit descriptor and convert the string to hexadecimal
173 TVariantPtrC convertHash;
174 convertHash.Set(hashPtr);
175 HBufC* hmacResult = convertHash.HexStringLC();
177 INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hmacResult);
178 INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
180 //If the returned hash value matches the expected hash, Pass the test
181 if(*hmacResult == expectedHash)
183 INFO_PRINTF1(_L("*** Hmac - Basic Hash of Data : PASS ***"));
184 SetTestStepResult(EPass);
188 ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err);
189 SetTestStepResult(EFail);
192 CleanupStack::PopAndDestroy(hmacResult);
193 CleanupStack::PopAndDestroy(hashData);
197 CleanupStack::PopAndDestroy(sourceData);
200 //Cleanup the Source RFile
201 CleanupStack::PopAndDestroy();
206 CleanupStack::PopAndDestroy(hmacImpl);
210 ERR_PRINTF2(_L("*** FAIL: Failed to Create Hmac Object - %d ***"), err);
211 SetTestStepResult(EFail);
214 CleanupStack::PopAndDestroy(key);
215 CleanupStack::PopAndDestroy(keyParams);
216 CleanupStack::PopAndDestroy(keyStr);
220 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
222 return TestStepResult();
226 TVerdict CHmacBasicHashOfDataStep::doTestStepPostambleL()
229 return TestStepResult();