sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
* Example CTestStep derived implementation
|
sl@0
|
16 |
*
|
sl@0
|
17 |
*/
|
sl@0
|
18 |
|
sl@0
|
19 |
|
sl@0
|
20 |
/**
|
sl@0
|
21 |
@file
|
sl@0
|
22 |
@internalTechnology
|
sl@0
|
23 |
*/
|
sl@0
|
24 |
#include "hmacsetkeycheckingstep.h"
|
sl@0
|
25 |
|
sl@0
|
26 |
#include <cryptospi/cryptohashapi.h>
|
sl@0
|
27 |
#include <cryptospi/keys.h>
|
sl@0
|
28 |
#include <cryptospi/plugincharacteristics.h>
|
sl@0
|
29 |
|
sl@0
|
30 |
using namespace CryptoSpi;
|
sl@0
|
31 |
|
sl@0
|
32 |
CHmacSetKeyCheckingStep::~CHmacSetKeyCheckingStep()
|
sl@0
|
33 |
{
|
sl@0
|
34 |
}
|
sl@0
|
35 |
|
sl@0
|
36 |
|
sl@0
|
37 |
CHmacSetKeyCheckingStep::CHmacSetKeyCheckingStep()
|
sl@0
|
38 |
{
|
sl@0
|
39 |
SetTestStepName(KHmacSetKeyCheckingStep);
|
sl@0
|
40 |
}
|
sl@0
|
41 |
|
sl@0
|
42 |
|
sl@0
|
43 |
TVerdict CHmacSetKeyCheckingStep::doTestStepPreambleL()
|
sl@0
|
44 |
{
|
sl@0
|
45 |
SetTestStepResult(EPass);
|
sl@0
|
46 |
return TestStepResult();
|
sl@0
|
47 |
}
|
sl@0
|
48 |
|
sl@0
|
49 |
|
sl@0
|
50 |
TVerdict CHmacSetKeyCheckingStep::doTestStepL()
|
sl@0
|
51 |
{
|
sl@0
|
52 |
if (TestStepResult()==EPass)
|
sl@0
|
53 |
{
|
sl@0
|
54 |
|
sl@0
|
55 |
//Assume faliure, unless all is successful
|
sl@0
|
56 |
SetTestStepResult(EFail);
|
sl@0
|
57 |
|
sl@0
|
58 |
INFO_PRINTF1(_L("*** Hmac - Set Key Checking ***"));
|
sl@0
|
59 |
INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
|
sl@0
|
60 |
|
sl@0
|
61 |
TVariantPtrC algorithmUid;
|
sl@0
|
62 |
TVariantPtrC operationModeUid;
|
sl@0
|
63 |
TPtrC sourcePath;
|
sl@0
|
64 |
TPtrC expectedHash;
|
sl@0
|
65 |
TPtrC invalidEncryptKey;
|
sl@0
|
66 |
TPtrC encryptKey;
|
sl@0
|
67 |
TVariantPtrC keyType;
|
sl@0
|
68 |
|
sl@0
|
69 |
//Extract the Test Case ID parameter from the specified INI file
|
sl@0
|
70 |
if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
|
sl@0
|
71 |
!GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
|
sl@0
|
72 |
!GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
|
sl@0
|
73 |
!GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash) ||
|
sl@0
|
74 |
!GetStringFromConfig(ConfigSection(),KConfigInvalidKey,invalidEncryptKey) ||
|
sl@0
|
75 |
!GetStringFromConfig(ConfigSection(),KConfigEncryptKey,encryptKey) ||
|
sl@0
|
76 |
!GetStringFromConfig(ConfigSection(),KConfigEncryptKeyType,keyType))
|
sl@0
|
77 |
{
|
sl@0
|
78 |
ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
|
sl@0
|
79 |
SetTestStepResult(EFail);
|
sl@0
|
80 |
}
|
sl@0
|
81 |
else
|
sl@0
|
82 |
{
|
sl@0
|
83 |
RFs fsSession;
|
sl@0
|
84 |
|
sl@0
|
85 |
//Create a connection to the file server
|
sl@0
|
86 |
User::LeaveIfError(fsSession.Connect());
|
sl@0
|
87 |
|
sl@0
|
88 |
RFile sourceFile;
|
sl@0
|
89 |
CleanupClosePushL(sourceFile);
|
sl@0
|
90 |
|
sl@0
|
91 |
//Open the specified source file
|
sl@0
|
92 |
User::LeaveIfError(sourceFile.Open(fsSession,sourcePath, EFileRead));
|
sl@0
|
93 |
|
sl@0
|
94 |
TInt sourceLength = 0;
|
sl@0
|
95 |
User::LeaveIfError(sourceFile.Size(sourceLength));
|
sl@0
|
96 |
|
sl@0
|
97 |
//Create a heap based descriptor to store the data
|
sl@0
|
98 |
HBufC8* sourceData = HBufC8::NewL(sourceLength);
|
sl@0
|
99 |
CleanupStack::PushL(sourceData);
|
sl@0
|
100 |
TPtr8 sourcePtr = sourceData->Des();
|
sl@0
|
101 |
|
sl@0
|
102 |
sourceFile.Read(sourcePtr);
|
sl@0
|
103 |
|
sl@0
|
104 |
if(sourcePtr.Length() != sourceLength)
|
sl@0
|
105 |
{
|
sl@0
|
106 |
ERR_PRINTF1(_L("*** Error: Reading Source File ***"));
|
sl@0
|
107 |
SetTestStepResult(EFail);
|
sl@0
|
108 |
}
|
sl@0
|
109 |
else
|
sl@0
|
110 |
{
|
sl@0
|
111 |
//Create a pointer for the Hash + Key (Hmac) Implementation Object
|
sl@0
|
112 |
CHash* hmacImpl = NULL;
|
sl@0
|
113 |
|
sl@0
|
114 |
//Convert encryption key to an 8 Bit Descriptor
|
sl@0
|
115 |
HBufC8* invalidKeyStr = HBufC8::NewLC(invalidEncryptKey.Length());
|
sl@0
|
116 |
TPtr8 invalidKeyStrPtr = invalidKeyStr->Des();
|
sl@0
|
117 |
|
sl@0
|
118 |
invalidKeyStrPtr.Copy(invalidEncryptKey);
|
sl@0
|
119 |
|
sl@0
|
120 |
//Create an new CryptoParams object to encapsulate the invalid key type and key string
|
sl@0
|
121 |
CCryptoParams* invalidKeyParams = CCryptoParams::NewL();
|
sl@0
|
122 |
CleanupStack::PushL(invalidKeyParams);
|
sl@0
|
123 |
invalidKeyParams->AddL(*invalidKeyStr,keyType);
|
sl@0
|
124 |
|
sl@0
|
125 |
//Create Invalid Key Object
|
sl@0
|
126 |
TKeyProperty invalidKeyProperty;
|
sl@0
|
127 |
CKey* invalidKey = CKey::NewL(invalidKeyProperty,*invalidKeyParams);
|
sl@0
|
128 |
CleanupStack::PushL(invalidKey);
|
sl@0
|
129 |
|
sl@0
|
130 |
//Retrieve a Hmac Factory Object with an Invalid Key
|
sl@0
|
131 |
TRAPD(err,CHashFactory::CreateHashL(hmacImpl,
|
sl@0
|
132 |
algorithmUid,
|
sl@0
|
133 |
operationModeUid,
|
sl@0
|
134 |
invalidKey,
|
sl@0
|
135 |
NULL));
|
sl@0
|
136 |
|
sl@0
|
137 |
if(hmacImpl && (err == KErrNone))
|
sl@0
|
138 |
{
|
sl@0
|
139 |
|
sl@0
|
140 |
//Push the Hmac Implementation Object onto the Cleanup Stack
|
sl@0
|
141 |
CleanupStack::PushL(hmacImpl);
|
sl@0
|
142 |
|
sl@0
|
143 |
//Create a NULL TCharacteristics pointer
|
sl@0
|
144 |
const TCharacteristics* invalidCharsPtr(NULL);
|
sl@0
|
145 |
|
sl@0
|
146 |
//Retrieve the characteristics for the hash implementation object
|
sl@0
|
147 |
TRAP_LOG(err, hmacImpl->GetCharacteristicsL(invalidCharsPtr));
|
sl@0
|
148 |
|
sl@0
|
149 |
//Static cast the characteristics to type THashCharacteristics
|
sl@0
|
150 |
const THashCharacteristics* hashInvalidCharsPtr = static_cast<const THashCharacteristics*>(invalidCharsPtr);
|
sl@0
|
151 |
|
sl@0
|
152 |
//The hash output size is returned in Bits, divide by 8 to get the Byte size
|
sl@0
|
153 |
TInt hashSize = hashInvalidCharsPtr->iOutputSize/8;
|
sl@0
|
154 |
|
sl@0
|
155 |
//Retrieve the final 8bit hash value and convert to 16bit
|
sl@0
|
156 |
HBufC* invalidHashData = HBufC::NewLC(hashSize);
|
sl@0
|
157 |
TPtr invalidHashPtr = invalidHashData->Des();
|
sl@0
|
158 |
|
sl@0
|
159 |
invalidHashPtr.Copy(hmacImpl->Hash(*sourceData));
|
sl@0
|
160 |
|
sl@0
|
161 |
//Take the 16bit descriptor and convert the string to hexadecimal
|
sl@0
|
162 |
TVariantPtrC convertHash;
|
sl@0
|
163 |
convertHash.Set(invalidHashPtr);
|
sl@0
|
164 |
HBufC* invalidHmacResult = convertHash.HexStringLC();
|
sl@0
|
165 |
|
sl@0
|
166 |
INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*invalidHmacResult);
|
sl@0
|
167 |
INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
|
sl@0
|
168 |
|
sl@0
|
169 |
if(*invalidHmacResult != expectedHash)
|
sl@0
|
170 |
{
|
sl@0
|
171 |
INFO_PRINTF1(_L("*** INVALID KEY - STAGE 1 PASS ***"));
|
sl@0
|
172 |
|
sl@0
|
173 |
//Convert encryption key to an 8 Bit Descriptor
|
sl@0
|
174 |
HBufC8* validKeyStr = HBufC8::NewLC(encryptKey.Length());
|
sl@0
|
175 |
TPtr8 validKeyStrPtr = validKeyStr->Des();
|
sl@0
|
176 |
|
sl@0
|
177 |
validKeyStrPtr.Copy(encryptKey);
|
sl@0
|
178 |
|
sl@0
|
179 |
//Create an new CryptoParams object to encapsulate the valid key type and secret key string
|
sl@0
|
180 |
CCryptoParams* validKeyParams = CCryptoParams::NewL();
|
sl@0
|
181 |
CleanupStack::PushL(validKeyParams);
|
sl@0
|
182 |
validKeyParams->AddL(*validKeyStr,keyType);
|
sl@0
|
183 |
|
sl@0
|
184 |
//Create Valid Key Object
|
sl@0
|
185 |
TKeyProperty validKeyProperty;
|
sl@0
|
186 |
CKey* validKey = CKey::NewL(validKeyProperty,*validKeyParams);
|
sl@0
|
187 |
CleanupStack::PushL(validKey);
|
sl@0
|
188 |
|
sl@0
|
189 |
//Set the valid key within the Hmac Implementation Object
|
sl@0
|
190 |
TRAP(err,hmacImpl->SetKeyL(*validKey));
|
sl@0
|
191 |
|
sl@0
|
192 |
if(err!=KErrNone)
|
sl@0
|
193 |
{
|
sl@0
|
194 |
ERR_PRINTF2(_L("*** ERROR %d: Setting Valid Key ***"),err);
|
sl@0
|
195 |
User::Leave(err);
|
sl@0
|
196 |
}
|
sl@0
|
197 |
else
|
sl@0
|
198 |
{
|
sl@0
|
199 |
INFO_PRINTF1(_L("*** HMAC VALID KEY SET ***"));
|
sl@0
|
200 |
}
|
sl@0
|
201 |
|
sl@0
|
202 |
//Create a NULL TCharacteristics pointer
|
sl@0
|
203 |
const TCharacteristics* validCharsPtr(NULL);
|
sl@0
|
204 |
|
sl@0
|
205 |
//Retrieve the characteristics for the hash implementation object
|
sl@0
|
206 |
TRAP_LOG(err, hmacImpl->GetCharacteristicsL(validCharsPtr));
|
sl@0
|
207 |
|
sl@0
|
208 |
//Static cast the characteristics to type THashCharacteristics
|
sl@0
|
209 |
const THashCharacteristics* hashValidCharsPtr = static_cast<const THashCharacteristics*>(validCharsPtr);
|
sl@0
|
210 |
|
sl@0
|
211 |
//The hash output size is returned in Bits, divide by 8 to get the Byte size
|
sl@0
|
212 |
hashSize = hashValidCharsPtr->iOutputSize/8;
|
sl@0
|
213 |
|
sl@0
|
214 |
//Retrieve the final 8bit hash value and convert to 16bit
|
sl@0
|
215 |
HBufC* validHashData = HBufC::NewLC(hashSize);
|
sl@0
|
216 |
TPtr validHashPtr = validHashData->Des();
|
sl@0
|
217 |
|
sl@0
|
218 |
validHashPtr.Copy(hmacImpl->Hash(*sourceData));
|
sl@0
|
219 |
|
sl@0
|
220 |
//Take the 16bit descriptor and convert the string to hexadecimal
|
sl@0
|
221 |
convertHash.Set(validHashPtr);
|
sl@0
|
222 |
HBufC* validHmacResult = convertHash.HexStringLC();
|
sl@0
|
223 |
|
sl@0
|
224 |
INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*validHmacResult);
|
sl@0
|
225 |
INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
|
sl@0
|
226 |
|
sl@0
|
227 |
if(*validHmacResult == expectedHash)
|
sl@0
|
228 |
{
|
sl@0
|
229 |
INFO_PRINTF1(_L("*** VALID KEY - STAGE 2 PASS ***"));
|
sl@0
|
230 |
INFO_PRINTF1(_L("*** Hmac - Set Key Checking : PASS ***"));
|
sl@0
|
231 |
SetTestStepResult(EPass);
|
sl@0
|
232 |
}
|
sl@0
|
233 |
else
|
sl@0
|
234 |
{
|
sl@0
|
235 |
ERR_PRINTF1(_L("*** STAGE 2 FAIL: Valid Hash and Expected Value Mismatch ***"));
|
sl@0
|
236 |
SetTestStepResult(EFail);
|
sl@0
|
237 |
}
|
sl@0
|
238 |
CleanupStack::PopAndDestroy(validHmacResult);
|
sl@0
|
239 |
CleanupStack::PopAndDestroy(validHashData);
|
sl@0
|
240 |
|
sl@0
|
241 |
CleanupStack::PopAndDestroy(validKey);
|
sl@0
|
242 |
CleanupStack::PopAndDestroy(validKeyParams);
|
sl@0
|
243 |
CleanupStack::PopAndDestroy(validKeyStr);
|
sl@0
|
244 |
}
|
sl@0
|
245 |
else
|
sl@0
|
246 |
{
|
sl@0
|
247 |
ERR_PRINTF1(_L("*** STAGE 1 FAIL: Invalid Hash and Expected Value Match ***"));
|
sl@0
|
248 |
SetTestStepResult(EFail);
|
sl@0
|
249 |
}
|
sl@0
|
250 |
|
sl@0
|
251 |
CleanupStack::PopAndDestroy(invalidHmacResult);
|
sl@0
|
252 |
CleanupStack::PopAndDestroy(invalidHashData);
|
sl@0
|
253 |
CleanupStack::PopAndDestroy(hmacImpl);
|
sl@0
|
254 |
}
|
sl@0
|
255 |
else
|
sl@0
|
256 |
{
|
sl@0
|
257 |
ERR_PRINTF2(_L("*** FAIL: Failed to Create Hmac Object - %d ***"), err);
|
sl@0
|
258 |
SetTestStepResult(EFail);
|
sl@0
|
259 |
}
|
sl@0
|
260 |
|
sl@0
|
261 |
CleanupStack::PopAndDestroy(invalidKey);
|
sl@0
|
262 |
CleanupStack::PopAndDestroy(invalidKeyParams);
|
sl@0
|
263 |
CleanupStack::PopAndDestroy(invalidKeyStr);
|
sl@0
|
264 |
}
|
sl@0
|
265 |
|
sl@0
|
266 |
CleanupStack::PopAndDestroy(sourceData);
|
sl@0
|
267 |
|
sl@0
|
268 |
//Cleanup the Source RFile
|
sl@0
|
269 |
CleanupStack::PopAndDestroy();
|
sl@0
|
270 |
|
sl@0
|
271 |
fsSession.Close();
|
sl@0
|
272 |
}
|
sl@0
|
273 |
|
sl@0
|
274 |
|
sl@0
|
275 |
INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
|
sl@0
|
276 |
}
|
sl@0
|
277 |
|
sl@0
|
278 |
return TestStepResult();
|
sl@0
|
279 |
}
|
sl@0
|
280 |
|
sl@0
|
281 |
|
sl@0
|
282 |
TVerdict CHmacSetKeyCheckingStep::doTestStepPostambleL()
|
sl@0
|
283 |
{
|
sl@0
|
284 |
return TestStepResult();
|
sl@0
|
285 |
}
|