os/security/crypto/weakcryptospi/test/tcryptospi/src/hmacincrementalhmacwithcopystep.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 "hmacincrementalhmacwithcopystep.h"
26 #include <cryptospi/cryptohashapi.h>
27 #include <cryptospi/keys.h>
28 #include <cryptospi/plugincharacteristics.h>
30 using namespace CryptoSpi;
32 CHmacIncrementalHmacWithCopyStep::~CHmacIncrementalHmacWithCopyStep()
37 CHmacIncrementalHmacWithCopyStep::CHmacIncrementalHmacWithCopyStep()
39 SetTestStepName(KHmacIncrementalHmacWithCopyStep);
43 TVerdict CHmacIncrementalHmacWithCopyStep::doTestStepPreambleL()
45 SetTestStepResult(EPass);
46 return TestStepResult();
50 TVerdict CHmacIncrementalHmacWithCopyStep::doTestStepL()
52 if (TestStepResult()==EPass)
55 //Assume faliure, unless all is successful
56 SetTestStepResult(EFail);
58 INFO_PRINTF1(_L("*** Hmac - Incremental Hash with Copy ***"));
59 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
61 TVariantPtrC algorithmUid;
62 TVariantPtrC operationModeUid;
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))
76 ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
77 SetTestStepResult(EFail);
81 //Create a pointer for the Hash + Key (Hmac) Implementation Object
82 CHash* hmacImpl = NULL;
84 //Convert encryption key to an 8 Bit Descriptor
85 HBufC8* keyStr = HBufC8::NewLC(encryptKey.Length());
86 TPtr8 keyStrPtr = keyStr->Des();
88 keyStrPtr.Copy(encryptKey);
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);
96 TKeyProperty keyProperty;
97 CKey* key=CKey::NewL(keyProperty,*keyParams);
98 CleanupStack::PushL(key);
100 //Retrieve a Hmac Factory Object
101 TRAPD(err,CHashFactory::CreateHashL(hmacImpl,
107 if(hmacImpl && (err == KErrNone))
110 //Push the Hmac Implementation Object onto the Cleanup Stack
111 CleanupStack::PushL(hmacImpl);
115 //Create a connection to the file server
116 err = fsSession.Connect();
120 ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err);
121 SetTestStepResult(EFail);
126 CleanupClosePushL(sourceFile);
128 //Open the specified source file
129 err = sourceFile.Open(fsSession,sourcePath, EFileRead);
133 ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err);
134 SetTestStepResult(EFail);
139 TInt sourceLength = 0;
140 TInt readPosition = 0;
141 TInt readIncrement = 0;
142 TBool hashComplete = EFalse;
143 TBool hashCopied = EFalse;
146 CHash* hmacCopyImpl = NULL;
148 User::LeaveIfError(sourceFile.Size(sourceLength));
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;
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();
161 //Read in a block of data from the source file from the current position
162 err = sourceFile.Read(readPosition,sourcePtr,readIncrement);
164 //Update the read position by adding the number of bytes read
165 readPosition += readIncrement;
167 if(readPosition == readIncrement)
169 //Read in the first block from the data file into the Hmac implementation object
170 hmacImpl->Hash(*sourceData);
171 INFO_PRINTF2(_L("Intial Hmac - Bytes Read: %d"), readPosition);
173 else if(readPosition >= sourceLength)
175 //Reading in the final block, constructs the complete hash value and returns it within a TPtrC8
176 hashStr.Set(hmacCopyImpl->Final(*sourceData));
178 //Sets the Complete Flag to ETrue in order to drop out of the loop
179 hashComplete = ETrue;
181 TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
182 INFO_PRINTF2(_L("Final Hmac - Bytes Read: %d"),totalRead);
184 //If the read position is half the source length and the implementation
185 //object hasn't already been copied
186 else if((readPosition >= sourceLength/2) && (hashCopied == EFalse))
188 //Update the hash message before copying
189 hmacImpl->Update(*sourceData);
191 INFO_PRINTF1(_L("Copying Hmac Object..."));
193 //Create a Copy of the existing Hmac Object and all internal state of the message digest
194 hmacCopyImpl = hmacImpl->CopyL();
198 INFO_PRINTF2(_L("*** HMAC COPY - Bytes Read: %d ***"), readPosition);
202 //Update the message data within the Hmac object with the new block
203 if(hashCopied == EFalse)
205 hmacImpl->Update(*sourceData);
206 INFO_PRINTF2(_L("Hmac Update - Bytes Read: %d"), readPosition);
210 hmacCopyImpl->Update(*sourceData);
211 INFO_PRINTF2(_L("Hmac Update (Copy) - Bytes Read: %d"), readPosition);
215 CleanupStack::PopAndDestroy(sourceData);
218 }while(hashComplete == EFalse);
220 //Create a NULL TCharacteristics pointer
221 const TCharacteristics* charsPtr(NULL);
223 //Retrieve the characteristics for the hash implementation object
224 TRAP_LOG(err, hmacImpl->GetCharacteristicsL(charsPtr));
226 //Static cast the characteristics to type THashCharacteristics
227 const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
229 //The hash output size is returned in Bits, divide by 8 to get the Byte size
230 TInt hashSize = hashCharsPtr->iOutputSize/8;
232 //Retrieve the final 8bit hash value and convert to 16bit
233 HBufC* hashData = HBufC::NewLC(hashSize);
234 TPtr hashPtr = hashData->Des();
236 hashPtr.Copy(hashStr);
238 //Take the 16bit descriptor and convert the string to hexadecimal
239 TVariantPtrC convertHash;
240 convertHash.Set(hashPtr);
241 HBufC* hmacResult = convertHash.HexStringLC();
243 INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hmacResult);
244 INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
246 //If the returned hash value matches the expected hash, Pass the test
247 if(*hmacResult == expectedHash)
249 INFO_PRINTF1(_L("*** Hmac - Incremental Hash with Copy : PASS ***"));
250 SetTestStepResult(EPass);
254 ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err);
255 SetTestStepResult(EFail);
258 CleanupStack::PopAndDestroy(hmacResult);
259 CleanupStack::PopAndDestroy(hashData);
264 //Cleanup the Source RFile
265 CleanupStack::PopAndDestroy();
270 CleanupStack::PopAndDestroy(hmacImpl);
274 ERR_PRINTF2(_L("*** FAIL: Failed to Create Hmac Object - %d ***"), err);
275 SetTestStepResult(EFail);
278 CleanupStack::PopAndDestroy(key);
279 CleanupStack::PopAndDestroy(keyParams);
280 CleanupStack::PopAndDestroy(keyStr);
284 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
285 return TestStepResult();
289 TVerdict CHmacIncrementalHmacWithCopyStep::doTestStepPostambleL()
291 return TestStepResult();