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 "hmacsetkeycheckingstep.h"
26 #include <cryptospi/cryptohashapi.h>
27 #include <cryptospi/keys.h>
28 #include <cryptospi/plugincharacteristics.h>
30 using namespace CryptoSpi;
32 CHmacSetKeyCheckingStep::~CHmacSetKeyCheckingStep()
37 CHmacSetKeyCheckingStep::CHmacSetKeyCheckingStep()
39 SetTestStepName(KHmacSetKeyCheckingStep);
43 TVerdict CHmacSetKeyCheckingStep::doTestStepPreambleL()
45 SetTestStepResult(EPass);
46 return TestStepResult();
50 TVerdict CHmacSetKeyCheckingStep::doTestStepL()
52 if (TestStepResult()==EPass)
55 //Assume faliure, unless all is successful
56 SetTestStepResult(EFail);
58 INFO_PRINTF1(_L("*** Hmac - Set Key Checking ***"));
59 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
61 TVariantPtrC algorithmUid;
62 TVariantPtrC operationModeUid;
65 TPtrC invalidEncryptKey;
69 //Extract the Test Case ID parameter from the specified INI file
70 if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
71 !GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
72 !GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
73 !GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash) ||
74 !GetStringFromConfig(ConfigSection(),KConfigInvalidKey,invalidEncryptKey) ||
75 !GetStringFromConfig(ConfigSection(),KConfigEncryptKey,encryptKey) ||
76 !GetStringFromConfig(ConfigSection(),KConfigEncryptKeyType,keyType))
78 ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
79 SetTestStepResult(EFail);
85 //Create a connection to the file server
86 User::LeaveIfError(fsSession.Connect());
89 CleanupClosePushL(sourceFile);
91 //Open the specified source file
92 User::LeaveIfError(sourceFile.Open(fsSession,sourcePath, EFileRead));
94 TInt sourceLength = 0;
95 User::LeaveIfError(sourceFile.Size(sourceLength));
97 //Create a heap based descriptor to store the data
98 HBufC8* sourceData = HBufC8::NewL(sourceLength);
99 CleanupStack::PushL(sourceData);
100 TPtr8 sourcePtr = sourceData->Des();
102 sourceFile.Read(sourcePtr);
104 if(sourcePtr.Length() != sourceLength)
106 ERR_PRINTF1(_L("*** Error: Reading Source File ***"));
107 SetTestStepResult(EFail);
111 //Create a pointer for the Hash + Key (Hmac) Implementation Object
112 CHash* hmacImpl = NULL;
114 //Convert encryption key to an 8 Bit Descriptor
115 HBufC8* invalidKeyStr = HBufC8::NewLC(invalidEncryptKey.Length());
116 TPtr8 invalidKeyStrPtr = invalidKeyStr->Des();
118 invalidKeyStrPtr.Copy(invalidEncryptKey);
120 //Create an new CryptoParams object to encapsulate the invalid key type and key string
121 CCryptoParams* invalidKeyParams = CCryptoParams::NewL();
122 CleanupStack::PushL(invalidKeyParams);
123 invalidKeyParams->AddL(*invalidKeyStr,keyType);
125 //Create Invalid Key Object
126 TKeyProperty invalidKeyProperty;
127 CKey* invalidKey = CKey::NewL(invalidKeyProperty,*invalidKeyParams);
128 CleanupStack::PushL(invalidKey);
130 //Retrieve a Hmac Factory Object with an Invalid Key
131 TRAPD(err,CHashFactory::CreateHashL(hmacImpl,
137 if(hmacImpl && (err == KErrNone))
140 //Push the Hmac Implementation Object onto the Cleanup Stack
141 CleanupStack::PushL(hmacImpl);
143 //Create a NULL TCharacteristics pointer
144 const TCharacteristics* invalidCharsPtr(NULL);
146 //Retrieve the characteristics for the hash implementation object
147 TRAP_LOG(err, hmacImpl->GetCharacteristicsL(invalidCharsPtr));
149 //Static cast the characteristics to type THashCharacteristics
150 const THashCharacteristics* hashInvalidCharsPtr = static_cast<const THashCharacteristics*>(invalidCharsPtr);
152 //The hash output size is returned in Bits, divide by 8 to get the Byte size
153 TInt hashSize = hashInvalidCharsPtr->iOutputSize/8;
155 //Retrieve the final 8bit hash value and convert to 16bit
156 HBufC* invalidHashData = HBufC::NewLC(hashSize);
157 TPtr invalidHashPtr = invalidHashData->Des();
159 invalidHashPtr.Copy(hmacImpl->Hash(*sourceData));
161 //Take the 16bit descriptor and convert the string to hexadecimal
162 TVariantPtrC convertHash;
163 convertHash.Set(invalidHashPtr);
164 HBufC* invalidHmacResult = convertHash.HexStringLC();
166 INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*invalidHmacResult);
167 INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
169 if(*invalidHmacResult != expectedHash)
171 INFO_PRINTF1(_L("*** INVALID KEY - STAGE 1 PASS ***"));
173 //Convert encryption key to an 8 Bit Descriptor
174 HBufC8* validKeyStr = HBufC8::NewLC(encryptKey.Length());
175 TPtr8 validKeyStrPtr = validKeyStr->Des();
177 validKeyStrPtr.Copy(encryptKey);
179 //Create an new CryptoParams object to encapsulate the valid key type and secret key string
180 CCryptoParams* validKeyParams = CCryptoParams::NewL();
181 CleanupStack::PushL(validKeyParams);
182 validKeyParams->AddL(*validKeyStr,keyType);
184 //Create Valid Key Object
185 TKeyProperty validKeyProperty;
186 CKey* validKey = CKey::NewL(validKeyProperty,*validKeyParams);
187 CleanupStack::PushL(validKey);
189 //Set the valid key within the Hmac Implementation Object
190 TRAP(err,hmacImpl->SetKeyL(*validKey));
194 ERR_PRINTF2(_L("*** ERROR %d: Setting Valid Key ***"),err);
199 INFO_PRINTF1(_L("*** HMAC VALID KEY SET ***"));
202 //Create a NULL TCharacteristics pointer
203 const TCharacteristics* validCharsPtr(NULL);
205 //Retrieve the characteristics for the hash implementation object
206 TRAP_LOG(err, hmacImpl->GetCharacteristicsL(validCharsPtr));
208 //Static cast the characteristics to type THashCharacteristics
209 const THashCharacteristics* hashValidCharsPtr = static_cast<const THashCharacteristics*>(validCharsPtr);
211 //The hash output size is returned in Bits, divide by 8 to get the Byte size
212 hashSize = hashValidCharsPtr->iOutputSize/8;
214 //Retrieve the final 8bit hash value and convert to 16bit
215 HBufC* validHashData = HBufC::NewLC(hashSize);
216 TPtr validHashPtr = validHashData->Des();
218 validHashPtr.Copy(hmacImpl->Hash(*sourceData));
220 //Take the 16bit descriptor and convert the string to hexadecimal
221 convertHash.Set(validHashPtr);
222 HBufC* validHmacResult = convertHash.HexStringLC();
224 INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*validHmacResult);
225 INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
227 if(*validHmacResult == expectedHash)
229 INFO_PRINTF1(_L("*** VALID KEY - STAGE 2 PASS ***"));
230 INFO_PRINTF1(_L("*** Hmac - Set Key Checking : PASS ***"));
231 SetTestStepResult(EPass);
235 ERR_PRINTF1(_L("*** STAGE 2 FAIL: Valid Hash and Expected Value Mismatch ***"));
236 SetTestStepResult(EFail);
238 CleanupStack::PopAndDestroy(validHmacResult);
239 CleanupStack::PopAndDestroy(validHashData);
241 CleanupStack::PopAndDestroy(validKey);
242 CleanupStack::PopAndDestroy(validKeyParams);
243 CleanupStack::PopAndDestroy(validKeyStr);
247 ERR_PRINTF1(_L("*** STAGE 1 FAIL: Invalid Hash and Expected Value Match ***"));
248 SetTestStepResult(EFail);
251 CleanupStack::PopAndDestroy(invalidHmacResult);
252 CleanupStack::PopAndDestroy(invalidHashData);
253 CleanupStack::PopAndDestroy(hmacImpl);
257 ERR_PRINTF2(_L("*** FAIL: Failed to Create Hmac Object - %d ***"), err);
258 SetTestStepResult(EFail);
261 CleanupStack::PopAndDestroy(invalidKey);
262 CleanupStack::PopAndDestroy(invalidKeyParams);
263 CleanupStack::PopAndDestroy(invalidKeyStr);
266 CleanupStack::PopAndDestroy(sourceData);
268 //Cleanup the Source RFile
269 CleanupStack::PopAndDestroy();
275 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
278 return TestStepResult();
282 TVerdict CHmacSetKeyCheckingStep::doTestStepPostambleL()
284 return TestStepResult();