|
1 /* |
|
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
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". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * Example CTestStep derived implementation |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 /** |
|
21 @file |
|
22 @internalTechnology |
|
23 */ |
|
24 #include "hmacincrementalhmacwithreplicatestep.h" |
|
25 |
|
26 #include <cryptospi/cryptohashapi.h> |
|
27 #include "keys.h" |
|
28 #include <cryptospi/plugincharacteristics.h> |
|
29 |
|
30 using namespace CryptoSpi; |
|
31 |
|
32 CHmacIncrementalHmacWithReplicateStep::~CHmacIncrementalHmacWithReplicateStep() |
|
33 { |
|
34 } |
|
35 |
|
36 |
|
37 CHmacIncrementalHmacWithReplicateStep::CHmacIncrementalHmacWithReplicateStep() |
|
38 { |
|
39 SetTestStepName(KHmacIncrementalHmacWithReplicateStep); |
|
40 } |
|
41 |
|
42 |
|
43 TVerdict CHmacIncrementalHmacWithReplicateStep::doTestStepPreambleL() |
|
44 { |
|
45 SetTestStepResult(EPass); |
|
46 return TestStepResult(); |
|
47 } |
|
48 |
|
49 |
|
50 TVerdict CHmacIncrementalHmacWithReplicateStep::doTestStepL() |
|
51 { |
|
52 if (TestStepResult()==EPass) |
|
53 { |
|
54 |
|
55 //Assume faliure, unless all is successful |
|
56 SetTestStepResult(EFail); |
|
57 |
|
58 INFO_PRINTF1(_L("*** Hmac - Incremental Hash with Replicate ***")); |
|
59 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells()); |
|
60 |
|
61 TVariantPtrC algorithmUid; |
|
62 TVariantPtrC operationModeUid; |
|
63 TPtrC sourcePath; |
|
64 TPtrC expectedHash; |
|
65 TPtrC encryptKey; |
|
66 TVariantPtrC keyType; |
|
67 |
|
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)) |
|
75 { |
|
76 ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **")); |
|
77 SetTestStepResult(EFail); |
|
78 } |
|
79 else |
|
80 { |
|
81 //Create a pointer for the Hash + Key (Hmac) Implementation Object |
|
82 CHash* hmacImpl = NULL; |
|
83 |
|
84 //Convert encryption key to an 8 Bit Descriptor |
|
85 HBufC8* keyStr = HBufC8::NewLC(encryptKey.Length()); |
|
86 TPtr8 keyStrPtr = keyStr->Des(); |
|
87 |
|
88 keyStrPtr.Copy(encryptKey); |
|
89 |
|
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); |
|
94 |
|
95 //Create Key Object |
|
96 TKeyProperty keyProperty; |
|
97 CKey* key=CKey::NewL(keyProperty,*keyParams); |
|
98 CleanupStack::PushL(key); |
|
99 |
|
100 //Retrieve a Hmac Factory Object |
|
101 TRAPD(err,CHashFactory::CreateHashL(hmacImpl, |
|
102 algorithmUid, |
|
103 operationModeUid, |
|
104 key, |
|
105 NULL)); |
|
106 |
|
107 if(hmacImpl && (err == KErrNone)) |
|
108 { |
|
109 |
|
110 //Push the Hmac Implementation Object onto the Cleanup Stack |
|
111 CleanupStack::PushL(hmacImpl); |
|
112 |
|
113 RFs fsSession; |
|
114 |
|
115 //Create a connection to the file server |
|
116 err = fsSession.Connect(); |
|
117 |
|
118 if(err != KErrNone) |
|
119 { |
|
120 ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err); |
|
121 SetTestStepResult(EFail); |
|
122 } |
|
123 else |
|
124 { |
|
125 RFile sourceFile; |
|
126 CleanupClosePushL(sourceFile); |
|
127 |
|
128 //Open the specified source file |
|
129 err = sourceFile.Open(fsSession,sourcePath, EFileRead); |
|
130 |
|
131 if(err != KErrNone) |
|
132 { |
|
133 ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err); |
|
134 SetTestStepResult(EFail); |
|
135 } |
|
136 else |
|
137 { |
|
138 |
|
139 TInt sourceLength = 0; |
|
140 TInt readPosition = 0; |
|
141 TInt readIncrement = 0; |
|
142 TBool hashComplete = EFalse; |
|
143 TBool hashReplicated = EFalse; |
|
144 TPtrC8 hashStr; |
|
145 |
|
146 CHash* hmacReplicateImpl = NULL; |
|
147 |
|
148 User::LeaveIfError(sourceFile.Size(sourceLength)); |
|
149 |
|
150 //Divide the total size of the source file up into individual equal sized blocks to read |
|
151 //over several increments |
|
152 readIncrement = sourceLength/KDataReadBlocks; |
|
153 |
|
154 do |
|
155 { |
|
156 //Create a heap based descriptor to store the data |
|
157 HBufC8* sourceData = HBufC8::NewL(readIncrement); |
|
158 CleanupStack::PushL(sourceData); |
|
159 TPtr8 sourcePtr = sourceData->Des(); |
|
160 |
|
161 //Read in a block of data from the source file from the current position |
|
162 err = sourceFile.Read(readPosition,sourcePtr,readIncrement); |
|
163 |
|
164 //Update the read position by adding the number of bytes read |
|
165 readPosition += readIncrement; |
|
166 |
|
167 if(readPosition == readIncrement) |
|
168 { |
|
169 //Read in the first block from the data file into the Hmac implementation object |
|
170 if(hashReplicated == EFalse) |
|
171 { |
|
172 hmacImpl->Hash(*sourceData); |
|
173 INFO_PRINTF2(_L("Intial Hmac - Bytes Read: %d"), readPosition); |
|
174 } |
|
175 else |
|
176 { |
|
177 hmacReplicateImpl->Hash(*sourceData); |
|
178 INFO_PRINTF2(_L("Intial Hmac (Replicate) - Bytes Read: %d"), readPosition); |
|
179 } |
|
180 } |
|
181 else if(readPosition >= sourceLength) |
|
182 { |
|
183 //Reading in the final block, constructs the complete hash value and returns it within a TPtrC8 |
|
184 hashStr.Set(hmacReplicateImpl->Final(*sourceData)); |
|
185 |
|
186 //Sets the Complete Flag to ETrue in order to drop out of the loop |
|
187 hashComplete = ETrue; |
|
188 |
|
189 TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length(); |
|
190 INFO_PRINTF2(_L("Final Hmac - Bytes Read: %d"),totalRead); |
|
191 } |
|
192 //If the read position is half the source length and the implementation |
|
193 //object hasn't already been replicated |
|
194 else if((readPosition >= sourceLength/2) && (hashReplicated == EFalse)) |
|
195 { |
|
196 INFO_PRINTF1(_L("Replicating Hmac Object...")); |
|
197 |
|
198 //Create a Copy of the existing Hmac Object with NO internal message state |
|
199 hmacReplicateImpl = hmacImpl->ReplicateL(); |
|
200 |
|
201 hashReplicated = ETrue; |
|
202 |
|
203 //Sets the read position back to 0 inorder to restart the file read from the beginning |
|
204 readPosition =0; |
|
205 |
|
206 INFO_PRINTF2(_L("*** HMAC REPLICATE - Bytes Read: %d ***"), readPosition); |
|
207 } |
|
208 else |
|
209 { |
|
210 //Update the message data within the Hmac object with the new block |
|
211 if(hashReplicated == EFalse) |
|
212 { |
|
213 hmacImpl->Update(*sourceData); |
|
214 INFO_PRINTF2(_L("Hmac Update - Bytes Read: %d"), readPosition); |
|
215 } |
|
216 else |
|
217 { |
|
218 hmacReplicateImpl->Update(*sourceData); |
|
219 INFO_PRINTF2(_L("Hmac Update (Replicate) - Bytes Read: %d"), readPosition); |
|
220 } |
|
221 } |
|
222 |
|
223 CleanupStack::PopAndDestroy(sourceData); |
|
224 |
|
225 |
|
226 }while(hashComplete == EFalse); |
|
227 |
|
228 //Create a NULL TCharacteristics pointer |
|
229 const TCharacteristics* charsPtr(NULL); |
|
230 |
|
231 //Retrieve the characteristics for the hash implementation object |
|
232 TRAP_LOG(err, hmacImpl->GetCharacteristicsL(charsPtr)); |
|
233 |
|
234 //Static cast the characteristics to type THashCharacteristics |
|
235 const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr); |
|
236 |
|
237 //The hash output size is returned in Bits, divide by 8 to get the Byte size |
|
238 TInt hashSize = hashCharsPtr->iOutputSize/8; |
|
239 |
|
240 //Retrieve the final 8bit hash value and convert to 16bit |
|
241 HBufC* hashData = HBufC::NewLC(hashSize); |
|
242 TPtr hashPtr = hashData->Des(); |
|
243 |
|
244 hashPtr.Copy(hashStr); |
|
245 |
|
246 //Take the 16bit descriptor and convert the string to hexadecimal |
|
247 TVariantPtrC convertHash; |
|
248 convertHash.Set(hashPtr); |
|
249 HBufC* hmacResult = convertHash.HexStringLC(); |
|
250 |
|
251 INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hmacResult); |
|
252 INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash); |
|
253 |
|
254 //If the returned hash value matches the expected hash, Pass the test |
|
255 if(*hmacResult == expectedHash) |
|
256 { |
|
257 INFO_PRINTF1(_L("*** Hmac - Incremental Hash with Replicate : PASS ***")); |
|
258 SetTestStepResult(EPass); |
|
259 } |
|
260 else |
|
261 { |
|
262 ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err); |
|
263 SetTestStepResult(EFail); |
|
264 } |
|
265 |
|
266 CleanupStack::PopAndDestroy(hmacResult); |
|
267 CleanupStack::PopAndDestroy(hashData); |
|
268 |
|
269 delete hmacReplicateImpl; |
|
270 } |
|
271 |
|
272 //Cleanup the Source RFile |
|
273 CleanupStack::PopAndDestroy(); |
|
274 } |
|
275 |
|
276 fsSession.Close(); |
|
277 |
|
278 CleanupStack::PopAndDestroy(hmacImpl); |
|
279 } |
|
280 else |
|
281 { |
|
282 ERR_PRINTF2(_L("*** FAIL: Failed to Create Hmac Object - %d ***"), err); |
|
283 SetTestStepResult(EFail); |
|
284 } |
|
285 |
|
286 CleanupStack::PopAndDestroy(key); |
|
287 CleanupStack::PopAndDestroy(keyParams); |
|
288 CleanupStack::PopAndDestroy(keyStr); |
|
289 } |
|
290 } |
|
291 |
|
292 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells()); |
|
293 return TestStepResult(); |
|
294 } |
|
295 |
|
296 |
|
297 TVerdict CHmacIncrementalHmacWithReplicateStep::doTestStepPostambleL() |
|
298 { |
|
299 return TestStepResult(); |
|
300 } |