os/security/crypto/weakcryptospi/test/tcryptospi/src/hmacsetoperationmodecheckingstep.cpp
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 "hmacsetoperationmodecheckingstep.h"
26 #include <cryptospi/cryptohashapi.h>
27 #include <cryptospi/keys.h>
28 #include <cryptospi/plugincharacteristics.h>
30 using namespace CryptoSpi;
32 CHmacSetOperationModeCheckingStep::~CHmacSetOperationModeCheckingStep()
37 CHmacSetOperationModeCheckingStep::CHmacSetOperationModeCheckingStep()
39 SetTestStepName(KHmacSetOperationModeCheckingStep);
43 TVerdict CHmacSetOperationModeCheckingStep::doTestStepPreambleL()
45 SetTestStepResult(EPass);
46 return TestStepResult();
50 TVerdict CHmacSetOperationModeCheckingStep::doTestStepL()
52 if (TestStepResult()==EPass)
55 //Assume faliure, unless all is successful
56 SetTestStepResult(EFail);
58 INFO_PRINTF1(_L("*** Hmac - Set Operation Mode Checking ***"));
59 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
61 TVariantPtrC algorithmUid;
62 TVariantPtrC operationModeUid;
63 TVariantPtrC secondOperationModeUid;
70 //Extract the Test Case ID parameter from the specified INI file
71 if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
72 !GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
73 !GetStringFromConfig(ConfigSection(),KConfigSecondOperationMode,secondOperationModeUid) ||
74 !GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
75 !GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash) ||
76 !GetStringFromConfig(ConfigSection(),KConfigExSecondHashHmacValue,expectedHmac) ||
77 !GetStringFromConfig(ConfigSection(),KConfigEncryptKey,encryptKey) ||
78 !GetStringFromConfig(ConfigSection(),KConfigEncryptKeyType,keyType))
80 ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
81 SetTestStepResult(EFail);
87 //Create a connection to the file server
88 User::LeaveIfError(fsSession.Connect());
91 CleanupClosePushL(sourceFile);
93 //Open the specified source file
94 User::LeaveIfError(sourceFile.Open(fsSession,sourcePath, EFileRead));
96 TInt sourceLength = 0;
97 User::LeaveIfError(sourceFile.Size(sourceLength));
99 //Create a heap based descriptor to store the data
100 HBufC8* sourceData = HBufC8::NewL(sourceLength);
101 CleanupStack::PushL(sourceData);
102 TPtr8 sourcePtr = sourceData->Des();
104 sourceFile.Read(sourcePtr);
106 if(sourcePtr.Length() != sourceLength)
108 ERR_PRINTF1(_L("*** Error: Reading Source File ***"));
109 SetTestStepResult(EFail);
113 //Create a pointer for the Hash + Key (Hmac) Implementation Object
114 CHash* hashHmacImpl = NULL;
116 //Convert encryption key to an 8 Bit Descriptor
117 HBufC8* keyStr = HBufC8::NewLC(encryptKey.Length());
118 TPtr8 keyStrPtr = keyStr->Des();
120 keyStrPtr.Copy(encryptKey);
122 //Create an new CryptoParams object to encapsulate the invalid key type and key string
123 CCryptoParams* keyParams = CCryptoParams::NewL();
124 CleanupStack::PushL(keyParams);
125 keyParams->AddL(*keyStr,keyType);
127 //Create a valid CKey Object
128 TKeyProperty keyProperty;
129 CKey* key = CKey::NewL(keyProperty,*keyParams);
130 CleanupStack::PushL(key);
132 //Construct an initial hash object with NO key, Catching any possible Leaves
133 TRAPD(err,CHashFactory::CreateHashL(
141 if(hashHmacImpl && (err == KErrNone))
144 //Push the Implementation Object onto the Cleanup Stack
145 CleanupStack::PushL(hashHmacImpl);
147 //Create a NULL TCharacteristics pointer
148 const TCharacteristics* charsPtrA(NULL);
150 //Retrieve the characteristics for the hash implementation object
151 TRAP_LOG(err, hashHmacImpl->GetCharacteristicsL(charsPtrA));
153 //Static cast the characteristics to type THashCharacteristics
154 const THashCharacteristics* hashCharsPtrA = static_cast<const THashCharacteristics*>(charsPtrA);
156 //The hash output size is returned in Bits, divide by 8 to get the Byte size
157 TInt hashSize = hashCharsPtrA->iOutputSize/8;
159 //Retrieve the final 8bit hash value and convert to 16bit
160 HBufC* hashDataA = HBufC::NewLC(hashSize);
161 TPtr hashPtrA = hashDataA->Des();
163 hashPtrA.Copy(hashHmacImpl->Hash(*sourceData));
165 //Take the 16bit descriptor and convert the string to hexadecimal
166 TVariantPtrC convertHash;
167 convertHash.Set(hashPtrA);
169 HBufC* resultA = convertHash.HexStringLC();
171 INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*resultA);
172 INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
174 if(*resultA == expectedHash)
176 INFO_PRINTF1(_L("*** PRIMARY HASH VALID - STAGE 1 PASS ***"));
178 //Set the valid key within the Hmac Implementation Object
179 TRAP(err,hashHmacImpl->SetKeyL(*key));
183 ERR_PRINTF2(_L("*** ERROR %d: Setting Key ***"),err);
188 INFO_PRINTF1(_L("*** HMAC KEY SET ***"));
191 //Set the Operation Mode of the Hmac Implementation Object
192 hashHmacImpl->SetOperationModeL(secondOperationModeUid);
196 ERR_PRINTF3(_L("*** ERROR %d: Setting Operation Mode %S ***"),err,&secondOperationModeUid);
201 INFO_PRINTF2(_L("*** OPERATION MODE SET : %S ***"),&secondOperationModeUid);
204 //Create a NULL TCharacteristics pointer
205 const TCharacteristics* charsPtrB(NULL);
207 //Retrieve the characteristics for the hash implementation object
208 TRAP_LOG(err, hashHmacImpl->GetCharacteristicsL(charsPtrB));
210 //Static cast the characteristics to type THashCharacteristics
211 const THashCharacteristics* hashCharsPtrB = static_cast<const THashCharacteristics*>(charsPtrB);
213 //The hash output size is returned in Bits, divide by 8 to get the Byte size
214 hashSize = hashCharsPtrB->iOutputSize/8;
216 //Retrieve the final 8bit hash value and convert to 16bit
217 HBufC* hashDataB = HBufC::NewLC(hashSize);
218 TPtr hashPtrB = hashDataB->Des();
220 hashPtrB.Copy(hashHmacImpl->Hash(*sourceData));
222 //Take the 16bit descriptor and convert the string to hexadecimal
223 convertHash.Set(hashPtrB);
224 HBufC* resultB = convertHash.HexStringLC();
226 INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*resultB);
227 INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHmac);
229 if(*resultB == expectedHmac)
231 INFO_PRINTF1(_L("*** SECONDARY HASH VALID - STAGE 2 PASS ***"));
233 //Set the Operation Mode of the Hmac Implementation Object
234 TRAP(err,hashHmacImpl->SetOperationModeL(operationModeUid));
238 ERR_PRINTF3(_L("*** ERROR %d: Setting Operation Mode %S ***"),err,&operationModeUid);
243 INFO_PRINTF2(_L("*** OPERATION MODE SET : %S ***"),&operationModeUid);
246 //Create a NULL TCharacteristics pointer
247 const TCharacteristics* charsPtrC(NULL);
249 //Retrieve the characteristics for the hash implementation object
250 TRAP_LOG(err, hashHmacImpl->GetCharacteristicsL(charsPtrC));
252 //Static cast the characteristics to type THashCharacteristics
253 const THashCharacteristics* hashCharsPtrC = static_cast<const THashCharacteristics*>(charsPtrC);
255 //The hash output size is returned in Bits, divide by 8 to get the Byte size
256 hashSize = hashCharsPtrC->iOutputSize/8;
258 //Retrieve the final 8bit hash value and convert to 16bit
259 HBufC* hashDataC = HBufC::NewLC(hashSize);
260 TPtr hashPtrC = hashDataC->Des();
262 hashPtrC.Copy(hashHmacImpl->Hash(*sourceData));
264 //Take the 16bit descriptor and convert the string to hexadecimal
265 convertHash.Set(hashPtrC);
266 HBufC* resultC = convertHash.HexStringLC();
268 INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*resultC);
269 INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
271 if(*resultC == expectedHash)
273 INFO_PRINTF1(_L("*** FINAL HASH VALID - STAGE 3 PASS ***"));
274 INFO_PRINTF1(_L("*** Hmac - Set Operation Mode Checking : PASS ***"));
275 SetTestStepResult(EPass);
279 ERR_PRINTF1(_L("*** STAGE 3 FAIL: Hash and Expected Value Mismatch ***"));
280 SetTestStepResult(EFail);
283 CleanupStack::PopAndDestroy(resultC);
284 CleanupStack::PopAndDestroy(hashDataC);
288 ERR_PRINTF1(_L("*** STAGE 2 FAIL: Hash and Expected Value Mismatch ***"));
289 SetTestStepResult(EFail);
292 CleanupStack::PopAndDestroy(resultB);
293 CleanupStack::PopAndDestroy(hashDataB);
297 ERR_PRINTF1(_L("*** STAGE 1 FAIL: Hash and Expected Value Match ***"));
298 SetTestStepResult(EFail);
301 CleanupStack::PopAndDestroy(resultA);
302 CleanupStack::PopAndDestroy(hashDataA);
303 CleanupStack::PopAndDestroy(hashHmacImpl);
305 else if(err==KErrNotSupported)
307 if((((TUid)operationModeUid != KHashModeUid) && ((TUid)operationModeUid != KHmacModeUid)) ||
308 ((TUid)algorithmUid != KMd2Uid) && (TUid)algorithmUid != KMd5Uid && (TUid)algorithmUid != KSha1Uid && (TUid)algorithmUid != KMd4Uid && (TUid)algorithmUid != KSha224Uid && (TUid)algorithmUid != KSha256Uid && (TUid)algorithmUid != KSha384Uid && (TUid)algorithmUid != KSha512Uid)
310 ERR_PRINTF2(_L("*** Object Load Failure - Invalid Operation Mode : %d ***"), err);
315 SetTestStepResult(EFail);
320 ERR_PRINTF2(_L("*** Hash/Hmac Facotry Object Load Failure : %d ***"), err);
324 CleanupStack::PopAndDestroy(key);
325 CleanupStack::PopAndDestroy(keyParams);
326 CleanupStack::PopAndDestroy(keyStr);
329 CleanupStack::PopAndDestroy(sourceData);
331 //Cleanup the Source RFile
332 CleanupStack::PopAndDestroy();
340 SetTestStepResult(EFail);
343 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
345 return TestStepResult();
349 TVerdict CHmacSetOperationModeCheckingStep::doTestStepPostambleL()
351 return TestStepResult();