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 "hmacincrementalhmacwithresetstep.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 |
CHmacIncrementalHmacWithResetStep::~CHmacIncrementalHmacWithResetStep()
|
sl@0
|
33 |
{
|
sl@0
|
34 |
}
|
sl@0
|
35 |
|
sl@0
|
36 |
|
sl@0
|
37 |
CHmacIncrementalHmacWithResetStep::CHmacIncrementalHmacWithResetStep()
|
sl@0
|
38 |
{
|
sl@0
|
39 |
SetTestStepName(KHmacIncrementalHmacWithResetStep);
|
sl@0
|
40 |
}
|
sl@0
|
41 |
|
sl@0
|
42 |
|
sl@0
|
43 |
TVerdict CHmacIncrementalHmacWithResetStep::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 CHmacIncrementalHmacWithResetStep::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 - Incremental Hash with Reset ***"));
|
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 encryptKey;
|
sl@0
|
66 |
TVariantPtrC keyType;
|
sl@0
|
67 |
|
sl@0
|
68 |
//Extract the Test Case ID parameter from the specified INI file
|
sl@0
|
69 |
if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
|
sl@0
|
70 |
!GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
|
sl@0
|
71 |
!GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
|
sl@0
|
72 |
!GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash) ||
|
sl@0
|
73 |
!GetStringFromConfig(ConfigSection(),KConfigEncryptKey,encryptKey) ||
|
sl@0
|
74 |
!GetStringFromConfig(ConfigSection(),KConfigEncryptKeyType,keyType))
|
sl@0
|
75 |
{
|
sl@0
|
76 |
ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
|
sl@0
|
77 |
SetTestStepResult(EFail);
|
sl@0
|
78 |
}
|
sl@0
|
79 |
else
|
sl@0
|
80 |
{
|
sl@0
|
81 |
//Create a pointer for the Hmac Implementation Object
|
sl@0
|
82 |
CHash* hmacImpl = NULL;
|
sl@0
|
83 |
|
sl@0
|
84 |
//Convert encryption key to an 8 Bit Descriptor
|
sl@0
|
85 |
HBufC8* keyStr = HBufC8::NewLC(encryptKey.Length());
|
sl@0
|
86 |
TPtr8 keyStrPtr = keyStr->Des();
|
sl@0
|
87 |
|
sl@0
|
88 |
keyStrPtr.Copy(encryptKey);
|
sl@0
|
89 |
|
sl@0
|
90 |
//Create an new CryptoParams object to encapsulate the key type and secret key string
|
sl@0
|
91 |
CCryptoParams* keyParams = CCryptoParams::NewL();
|
sl@0
|
92 |
CleanupStack::PushL(keyParams);
|
sl@0
|
93 |
keyParams->AddL(*keyStr,keyType);
|
sl@0
|
94 |
|
sl@0
|
95 |
//Create Key Object
|
sl@0
|
96 |
TKeyProperty keyProperty;
|
sl@0
|
97 |
CKey* key=CKey::NewL(keyProperty,*keyParams);
|
sl@0
|
98 |
CleanupStack::PushL(key);
|
sl@0
|
99 |
|
sl@0
|
100 |
//Retrieve a Hmac Factory Object
|
sl@0
|
101 |
TRAPD(err,CHashFactory::CreateHashL(hmacImpl,
|
sl@0
|
102 |
algorithmUid,
|
sl@0
|
103 |
operationModeUid,
|
sl@0
|
104 |
key,
|
sl@0
|
105 |
NULL));
|
sl@0
|
106 |
|
sl@0
|
107 |
if(hmacImpl && (err==KErrNone))
|
sl@0
|
108 |
{
|
sl@0
|
109 |
|
sl@0
|
110 |
//Push the Hmac Implementation Object onto the Cleanup Stack
|
sl@0
|
111 |
CleanupStack::PushL(hmacImpl);
|
sl@0
|
112 |
|
sl@0
|
113 |
RFs fsSession;
|
sl@0
|
114 |
|
sl@0
|
115 |
//Create a connection to the file server
|
sl@0
|
116 |
err = fsSession.Connect();
|
sl@0
|
117 |
|
sl@0
|
118 |
if(err != KErrNone)
|
sl@0
|
119 |
{
|
sl@0
|
120 |
ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err);
|
sl@0
|
121 |
SetTestStepResult(EFail);
|
sl@0
|
122 |
}
|
sl@0
|
123 |
else
|
sl@0
|
124 |
{
|
sl@0
|
125 |
RFile sourceFile;
|
sl@0
|
126 |
CleanupClosePushL(sourceFile);
|
sl@0
|
127 |
|
sl@0
|
128 |
//Open the specified source file
|
sl@0
|
129 |
err = sourceFile.Open(fsSession,sourcePath, EFileRead);
|
sl@0
|
130 |
|
sl@0
|
131 |
if(err != KErrNone)
|
sl@0
|
132 |
{
|
sl@0
|
133 |
ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err);
|
sl@0
|
134 |
SetTestStepResult(EFail);
|
sl@0
|
135 |
}
|
sl@0
|
136 |
else
|
sl@0
|
137 |
{
|
sl@0
|
138 |
|
sl@0
|
139 |
TInt sourceLength = 0;
|
sl@0
|
140 |
TInt readPosition = 0;
|
sl@0
|
141 |
TInt readIncrement = 0;
|
sl@0
|
142 |
TBool hashComplete = EFalse;
|
sl@0
|
143 |
TBool hashReset = EFalse;
|
sl@0
|
144 |
TPtrC8 hashStr;
|
sl@0
|
145 |
|
sl@0
|
146 |
User::LeaveIfError(sourceFile.Size(sourceLength));
|
sl@0
|
147 |
|
sl@0
|
148 |
//Divide the file size into seperate incremental blocks to read
|
sl@0
|
149 |
readIncrement = sourceLength/KDataReadBlocks;
|
sl@0
|
150 |
|
sl@0
|
151 |
do
|
sl@0
|
152 |
{
|
sl@0
|
153 |
//Create a heap based descriptor to store the data
|
sl@0
|
154 |
HBufC8* sourceData = HBufC8::NewL(readIncrement);
|
sl@0
|
155 |
CleanupStack::PushL(sourceData);
|
sl@0
|
156 |
TPtr8 sourcePtr = sourceData->Des();
|
sl@0
|
157 |
|
sl@0
|
158 |
//Read in a block of data from the source file from the current position
|
sl@0
|
159 |
err = sourceFile.Read(readPosition,sourcePtr,readIncrement);
|
sl@0
|
160 |
|
sl@0
|
161 |
//Update the read position by adding the number of bytes read
|
sl@0
|
162 |
readPosition += readIncrement;
|
sl@0
|
163 |
|
sl@0
|
164 |
if(readPosition == readIncrement)
|
sl@0
|
165 |
{
|
sl@0
|
166 |
//Read in the first block from the data file into the Hmac implementation object
|
sl@0
|
167 |
hmacImpl->Hash(*sourceData);
|
sl@0
|
168 |
INFO_PRINTF2(_L("Intial Hmac - Bytes Read: %d"), readPosition);
|
sl@0
|
169 |
}
|
sl@0
|
170 |
else if(readPosition >= sourceLength)
|
sl@0
|
171 |
{
|
sl@0
|
172 |
//Reading in the final block, constructs the complete hash value and returns it within a TPtrC8
|
sl@0
|
173 |
hashStr.Set(hmacImpl->Final(*sourceData));
|
sl@0
|
174 |
|
sl@0
|
175 |
//Sets the Complete Flag to ETrue in order to drop out of the loop
|
sl@0
|
176 |
hashComplete = ETrue;
|
sl@0
|
177 |
|
sl@0
|
178 |
TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
|
sl@0
|
179 |
INFO_PRINTF2(_L("Final Hmac - Bytes Read: %d"),totalRead);
|
sl@0
|
180 |
}
|
sl@0
|
181 |
//If the read position is half the source length and the implementation
|
sl@0
|
182 |
//object hasn't already been reset
|
sl@0
|
183 |
else if((readPosition >= sourceLength/2) && (hashReset == EFalse))
|
sl@0
|
184 |
{
|
sl@0
|
185 |
INFO_PRINTF1(_L("Resetting Hmac Object..."));
|
sl@0
|
186 |
|
sl@0
|
187 |
hmacImpl->Reset();
|
sl@0
|
188 |
|
sl@0
|
189 |
//Sets the read position back to 0 inorder to restart the file read from the beginning
|
sl@0
|
190 |
readPosition = 0;
|
sl@0
|
191 |
|
sl@0
|
192 |
hashReset = ETrue;
|
sl@0
|
193 |
|
sl@0
|
194 |
INFO_PRINTF2(_L("*** HMAC RESET - Bytes Read: %d ***"), readPosition);
|
sl@0
|
195 |
}
|
sl@0
|
196 |
else
|
sl@0
|
197 |
{
|
sl@0
|
198 |
//Update the message data within the Hmac object with the new block
|
sl@0
|
199 |
hmacImpl->Update(*sourceData);
|
sl@0
|
200 |
INFO_PRINTF2(_L("Hmac Update - Bytes Read: %d"), readPosition);
|
sl@0
|
201 |
}
|
sl@0
|
202 |
|
sl@0
|
203 |
CleanupStack::PopAndDestroy(sourceData);
|
sl@0
|
204 |
|
sl@0
|
205 |
}while(hashComplete == EFalse);
|
sl@0
|
206 |
|
sl@0
|
207 |
//Create a NULL TCharacteristics pointer
|
sl@0
|
208 |
const TCharacteristics* charsPtr(NULL);
|
sl@0
|
209 |
|
sl@0
|
210 |
//Retrieve the characteristics for the hash implementation object
|
sl@0
|
211 |
TRAP_LOG(err, hmacImpl->GetCharacteristicsL(charsPtr));
|
sl@0
|
212 |
|
sl@0
|
213 |
//Static cast the characteristics to type THashCharacteristics
|
sl@0
|
214 |
const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
|
sl@0
|
215 |
|
sl@0
|
216 |
//The hash output size is returned in Bits, divide by 8 to get the Byte size
|
sl@0
|
217 |
TInt hashSize = hashCharsPtr->iOutputSize/8;
|
sl@0
|
218 |
|
sl@0
|
219 |
//Retrieve the final 8bit hash value and convert to 16bit
|
sl@0
|
220 |
HBufC* hashData = HBufC::NewLC(hashSize);
|
sl@0
|
221 |
TPtr hashPtr = hashData->Des();
|
sl@0
|
222 |
|
sl@0
|
223 |
hashPtr.Copy(hashStr);
|
sl@0
|
224 |
|
sl@0
|
225 |
//Take the 16bit descriptor and convert the string to hexadecimal
|
sl@0
|
226 |
TVariantPtrC convertHash;
|
sl@0
|
227 |
convertHash.Set(hashPtr);
|
sl@0
|
228 |
HBufC* hmacResult = convertHash.HexStringLC();
|
sl@0
|
229 |
|
sl@0
|
230 |
INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hmacResult);
|
sl@0
|
231 |
INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
|
sl@0
|
232 |
|
sl@0
|
233 |
//If the returned hash value matches the expected hash, Pass the test
|
sl@0
|
234 |
if(*hmacResult == expectedHash)
|
sl@0
|
235 |
{
|
sl@0
|
236 |
INFO_PRINTF1(_L("*** Hmac - Incremental Hash with Reset : PASS ***"));
|
sl@0
|
237 |
SetTestStepResult(EPass);
|
sl@0
|
238 |
}
|
sl@0
|
239 |
else
|
sl@0
|
240 |
{
|
sl@0
|
241 |
ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err);
|
sl@0
|
242 |
SetTestStepResult(EFail);
|
sl@0
|
243 |
}
|
sl@0
|
244 |
|
sl@0
|
245 |
CleanupStack::PopAndDestroy(hmacResult);
|
sl@0
|
246 |
CleanupStack::PopAndDestroy(hashData);
|
sl@0
|
247 |
}
|
sl@0
|
248 |
|
sl@0
|
249 |
//Cleanup the Source RFile
|
sl@0
|
250 |
CleanupStack::PopAndDestroy();
|
sl@0
|
251 |
}
|
sl@0
|
252 |
|
sl@0
|
253 |
fsSession.Close();
|
sl@0
|
254 |
|
sl@0
|
255 |
CleanupStack::PopAndDestroy(hmacImpl);
|
sl@0
|
256 |
}
|
sl@0
|
257 |
else
|
sl@0
|
258 |
{
|
sl@0
|
259 |
ERR_PRINTF2(_L("*** FAIL: Failed to Create Hmac Object - %d ***"), err);
|
sl@0
|
260 |
SetTestStepResult(EFail);
|
sl@0
|
261 |
}
|
sl@0
|
262 |
|
sl@0
|
263 |
CleanupStack::PopAndDestroy(key);
|
sl@0
|
264 |
CleanupStack::PopAndDestroy(keyParams);
|
sl@0
|
265 |
CleanupStack::PopAndDestroy(keyStr);
|
sl@0
|
266 |
}
|
sl@0
|
267 |
}
|
sl@0
|
268 |
|
sl@0
|
269 |
INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
|
sl@0
|
270 |
return TestStepResult();
|
sl@0
|
271 |
}
|
sl@0
|
272 |
|
sl@0
|
273 |
|
sl@0
|
274 |
TVerdict CHmacIncrementalHmacWithResetStep::doTestStepPostambleL()
|
sl@0
|
275 |
{
|
sl@0
|
276 |
return TestStepResult();
|
sl@0
|
277 |
}
|