|
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 "hmacbasichashofdatastep.h" |
|
25 |
|
26 #include <cryptospi/cryptohashapi.h> |
|
27 #include "keys.h" |
|
28 #include <cryptospi/plugincharacteristics.h> |
|
29 |
|
30 using namespace CryptoSpi; |
|
31 |
|
32 CHmacBasicHashOfDataStep::~CHmacBasicHashOfDataStep() |
|
33 { |
|
34 } |
|
35 |
|
36 |
|
37 CHmacBasicHashOfDataStep::CHmacBasicHashOfDataStep() |
|
38 { |
|
39 SetTestStepName(KHmacBasicHashOfDataStep); |
|
40 } |
|
41 |
|
42 |
|
43 TVerdict CHmacBasicHashOfDataStep::doTestStepPreambleL() |
|
44 { |
|
45 SetTestStepResult(EPass); |
|
46 return TestStepResult(); |
|
47 } |
|
48 |
|
49 |
|
50 TVerdict CHmacBasicHashOfDataStep::doTestStepL() |
|
51 { |
|
52 if (TestStepResult()==EPass) |
|
53 { |
|
54 |
|
55 //Assume faliure, unless all is successful |
|
56 SetTestStepResult(EFail); |
|
57 |
|
58 INFO_PRINTF1(_L("*** Hmac - Basic Hash of Data ***")); |
|
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 //Push the Hmac Implementation Object onto the Cleanup Stack |
|
110 CleanupStack::PushL(hmacImpl); |
|
111 |
|
112 RFs fsSession; |
|
113 |
|
114 //Create a connection to the file server |
|
115 err = fsSession.Connect(); |
|
116 |
|
117 if(err != KErrNone) |
|
118 { |
|
119 ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err); |
|
120 SetTestStepResult(EFail); |
|
121 } |
|
122 else |
|
123 { |
|
124 RFile sourceFile; |
|
125 CleanupClosePushL(sourceFile); |
|
126 |
|
127 //Open the specified source file |
|
128 err = sourceFile.Open(fsSession,sourcePath, EFileRead); |
|
129 |
|
130 if(err != KErrNone) |
|
131 { |
|
132 ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err); |
|
133 SetTestStepResult(EFail); |
|
134 } |
|
135 else |
|
136 { |
|
137 TInt sourceLength = 0; |
|
138 User::LeaveIfError(sourceFile.Size(sourceLength)); |
|
139 |
|
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(); |
|
144 |
|
145 sourceFile.Read(sourcePtr); |
|
146 |
|
147 if(sourcePtr.Length() != sourceLength) |
|
148 { |
|
149 ERR_PRINTF1(_L("*** Error: Reading Source File ***")); |
|
150 SetTestStepResult(EFail); |
|
151 } |
|
152 else |
|
153 { |
|
154 //Create a NULL TCharacteristics pointer |
|
155 const TCharacteristics* charsPtr(NULL); |
|
156 |
|
157 //Retrieve the characteristics for the hash implementation object |
|
158 TRAP_LOG(err, hmacImpl->GetCharacteristicsL(charsPtr)); |
|
159 |
|
160 //Static cast the characteristics to type THashCharacteristics |
|
161 const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr); |
|
162 |
|
163 //The hash output size is returned in Bits, divide by 8 to get the Byte size |
|
164 TInt hashSize = hashCharsPtr->iOutputSize/8; |
|
165 |
|
166 //Retrieve the final 8bit hash value and convert to 16bit |
|
167 HBufC* hashData = HBufC::NewLC(hashSize); |
|
168 TPtr hashPtr = hashData->Des(); |
|
169 |
|
170 hashPtr.Copy(hmacImpl->Hash(*sourceData)); |
|
171 |
|
172 //Take the 16bit descriptor and convert the string to hexadecimal |
|
173 TVariantPtrC convertHash; |
|
174 convertHash.Set(hashPtr); |
|
175 HBufC* hmacResult = convertHash.HexStringLC(); |
|
176 |
|
177 INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hmacResult); |
|
178 INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash); |
|
179 |
|
180 //If the returned hash value matches the expected hash, Pass the test |
|
181 if(*hmacResult == expectedHash) |
|
182 { |
|
183 INFO_PRINTF1(_L("*** Hmac - Basic Hash of Data : PASS ***")); |
|
184 SetTestStepResult(EPass); |
|
185 } |
|
186 else |
|
187 { |
|
188 ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err); |
|
189 SetTestStepResult(EFail); |
|
190 } |
|
191 |
|
192 CleanupStack::PopAndDestroy(hmacResult); |
|
193 CleanupStack::PopAndDestroy(hashData); |
|
194 } |
|
195 |
|
196 |
|
197 CleanupStack::PopAndDestroy(sourceData); |
|
198 } |
|
199 |
|
200 //Cleanup the Source RFile |
|
201 CleanupStack::PopAndDestroy(); |
|
202 } |
|
203 |
|
204 fsSession.Close(); |
|
205 |
|
206 CleanupStack::PopAndDestroy(hmacImpl); |
|
207 } |
|
208 else |
|
209 { |
|
210 ERR_PRINTF2(_L("*** FAIL: Failed to Create Hmac Object - %d ***"), err); |
|
211 SetTestStepResult(EFail); |
|
212 } |
|
213 |
|
214 CleanupStack::PopAndDestroy(key); |
|
215 CleanupStack::PopAndDestroy(keyParams); |
|
216 CleanupStack::PopAndDestroy(keyStr); |
|
217 } |
|
218 } |
|
219 |
|
220 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells()); |
|
221 |
|
222 return TestStepResult(); |
|
223 } |
|
224 |
|
225 |
|
226 TVerdict CHmacBasicHashOfDataStep::doTestStepPostambleL() |
|
227 { |
|
228 |
|
229 return TestStepResult(); |
|
230 } |