Update contrib.
2 * Copyright (c) 2008-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 "mac_incremental_step.h"
25 #include <cryptospi/cryptomacapi.h>
26 #include <cryptospi/keys.h>
27 #include <cryptospi/plugincharacteristics.h>
29 using namespace CryptoSpi;
31 CMacIncrementalStep::~CMacIncrementalStep()
36 CMacIncrementalStep::CMacIncrementalStep()
38 SetTestStepName(KMacIncrementalStep);
42 TVerdict CMacIncrementalStep::doTestStepPreambleL()
48 TVerdict CMacIncrementalStep::doTestStepL()
50 //Assume faliure, unless all is successful
51 SetTestStepResult(EFail);
53 INFO_PRINTF1(_L("*** Mac - Incremental ***"));
54 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
56 TVariantPtrC algorithmUid;
62 //Extract the Test Case ID parameter from the specified INI file
63 if (!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
64 !GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
65 !GetStringFromConfig(ConfigSection(),KConfigExMacValue,expectedMac) ||
66 !GetStringFromConfig(ConfigSection(),KConfigEncryptKey,encryptKey) ||
67 !GetStringFromConfig(ConfigSection(),KConfigEncryptKeyType,keyType))
69 ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
73 //Create a pointer for the Mac + Key Implementation Object
76 //Convert encryption key to an 8 Bit Descriptor
77 HBufC8* keyStr = HBufC8::NewLC(encryptKey.Length());
78 TPtr8 keyStrPtr = keyStr->Des();
80 keyStrPtr.Copy(encryptKey);
82 //Create an new CryptoParams object to encapsulate the key type and secret key string
83 CCryptoParams* keyParams = CCryptoParams::NewL();
84 CleanupStack::PushL(keyParams);
85 keyParams->AddL(*keyStr,keyType);
88 TKeyProperty keyProperty;
89 CKey* key=CKey::NewL(keyProperty,*keyParams);
90 CleanupStack::PushL(key);
92 //Retrieve a Mac Factory Object
93 TRAPD(err,CMacFactory::CreateMacL(macImpl,
100 CleanupStack::PopAndDestroy(3, keyStr); // keyStr, keyParams, key
102 ERR_PRINTF2(_L("*** FAIL: Failed to Create Mac Object - %d ***"), err);
106 //Push the Mac Implementation Object onto the Cleanup Stack
107 CleanupStack::PushL(macImpl);
110 User::LeaveIfError(fsSession.Connect());
111 CleanupClosePushL(fsSession);
114 CleanupClosePushL(sourceFile);
116 //Open the specified source file
117 User::LeaveIfError(sourceFile.Open(fsSession,sourcePath, EFileRead));
119 TInt sourceLength = 0;
120 TInt readPosition = 0;
121 TInt readIncrement = 0;
122 TBool macComplete = EFalse;
125 User::LeaveIfError(sourceFile.Size(sourceLength));
127 //Divide the total size of the source file up into individual equal sized blocks to read
128 //over several increments
129 readIncrement = sourceLength/KDataReadBlocks;
131 if (readIncrement == 0)
133 ERR_PRINTF2(_L("*** Error: Source File must be larger than %d bytes ***"), KDataReadBlocks);
134 User::LeaveIfError(KErrNotSupported);
139 //Create a heap based descriptor to store the data
140 HBufC8* sourceData = HBufC8::NewL(readIncrement);
141 CleanupStack::PushL(sourceData);
142 TPtr8 sourcePtr = sourceData->Des();
144 //Read in a block of data from the source file from the current position
145 err = sourceFile.Read(readPosition,sourcePtr,readIncrement);
147 //Update the read position by adding the number of bytes read
148 readPosition += readIncrement;
150 if (readPosition == readIncrement)
152 //Read in the first block from the data file into the Mac implementation object
153 macImpl->MacL(*sourceData);
154 INFO_PRINTF2(_L("Intial Mac - Bytes Read: %d"), readPosition);
156 else if (readPosition >= sourceLength)
158 //Reading in the final block, constructs the complete mac value and returns it within a TPtrC8
159 macStr.Set(macImpl->FinalL(*sourceData));
161 //Sets the Complete Flag to ETrue in order to drop out of the loop
164 TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
165 INFO_PRINTF2(_L("Final Mac - Bytes Read: %d"),totalRead);
169 //Update the message data within the Mac object with the new block
170 macImpl->UpdateL(*sourceData);
171 INFO_PRINTF2(_L("Mac Update - Bytes Read: %d"), readPosition);
174 CleanupStack::PopAndDestroy(sourceData);
176 }while(macComplete == EFalse);
178 //Create a NULL TCharacteristics pointer
179 const TCharacteristics* charsPtr(NULL);
181 //Retrieve the characteristics for the mac implementation object
182 TRAP_LOG(err, macImpl->GetCharacteristicsL(charsPtr));
184 //Static cast the characteristics to type TMacCharacteristics
185 const TMacCharacteristics* macCharsPtr = static_cast<const TMacCharacteristics*>(charsPtr);
187 //The mac output size is returned in Bits, divide by 8 to get the Byte size
188 TInt macSize = macCharsPtr->iHashAlgorithmChar->iOutputSize/8;
190 //Retrieve the final 8 bit mac value and convert to 16bit
191 HBufC* macData = HBufC::NewLC(macSize);
192 TPtr macPtr = macData->Des();
196 //Take the 16 bit descriptor and convert the string to hexadecimal
197 TVariantPtrC convertMac;
198 convertMac.Set(macPtr);
199 HBufC* macResult = convertMac.HexStringLC();
201 INFO_PRINTF2(_L("*** Mac Data: %S ***"),&*macResult);
202 INFO_PRINTF2(_L("*** Expected Mac: %S ***"),&expectedMac);
204 //If the returned mac value matches the expected mac, Pass the test
205 if (*macResult == expectedMac)
207 INFO_PRINTF1(_L("*** Mac - Incremental Hash : PASS ***"));
208 SetTestStepResult(EPass);
212 ERR_PRINTF2(_L("*** FAIL: Generated Mac and Expected Mac Mismatch ***"), err);
215 CleanupStack::PopAndDestroy(8, keyStr); // keyStr, keyParams, key, macImpl, &fsSession, &sourceFile, macData, macResult
216 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
217 return TestStepResult();
221 TVerdict CMacIncrementalStep::doTestStepPostambleL()
223 return TestStepResult();