sl@0: /* sl@0: * Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * Example CTestStep derived implementation sl@0: * sl@0: */ sl@0: sl@0: sl@0: /** sl@0: @file sl@0: @internalTechnology sl@0: */ sl@0: #include "hmacincrementalhmacstep.h" sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: using namespace CryptoSpi; sl@0: sl@0: CHmacIncrementalHmacStep::~CHmacIncrementalHmacStep() sl@0: { sl@0: } sl@0: sl@0: sl@0: CHmacIncrementalHmacStep::CHmacIncrementalHmacStep() sl@0: { sl@0: SetTestStepName(KHmacIncrementalHmacStep); sl@0: } sl@0: sl@0: sl@0: TVerdict CHmacIncrementalHmacStep::doTestStepPreambleL() sl@0: { sl@0: SetTestStepResult(EPass); sl@0: return TestStepResult(); sl@0: } sl@0: sl@0: sl@0: TVerdict CHmacIncrementalHmacStep::doTestStepL() sl@0: { sl@0: if (TestStepResult()==EPass) sl@0: { sl@0: //Assume faliure, unless all is successful sl@0: SetTestStepResult(EFail); sl@0: sl@0: INFO_PRINTF1(_L("*** Hmac - Incremental Hash ***")); sl@0: INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells()); sl@0: sl@0: TVariantPtrC algorithmUid; sl@0: TVariantPtrC operationModeUid; sl@0: TPtrC sourcePath; sl@0: TPtrC expectedHash; sl@0: TPtrC encryptKey; sl@0: TVariantPtrC keyType; sl@0: sl@0: //Extract the Test Case ID parameter from the specified INI file sl@0: if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) || sl@0: !GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) || sl@0: !GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) || sl@0: !GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash) || sl@0: !GetStringFromConfig(ConfigSection(),KConfigEncryptKey,encryptKey) || sl@0: !GetStringFromConfig(ConfigSection(),KConfigEncryptKeyType,keyType)) sl@0: { sl@0: ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **")); sl@0: SetTestStepResult(EFail); sl@0: } sl@0: else sl@0: { sl@0: //Create a pointer for the Hash + Key (Hamc) Implementation Object sl@0: CHash* hmacImpl = NULL; sl@0: sl@0: //Convert encryption key to an 8 Bit Descriptor sl@0: HBufC8* keyStr = HBufC8::NewLC(encryptKey.Length()); sl@0: TPtr8 keyStrPtr = keyStr->Des(); sl@0: sl@0: keyStrPtr.Copy(encryptKey); sl@0: sl@0: //Create an new CryptoParams object to encapsulate the key type and secret key string sl@0: CCryptoParams* keyParams = CCryptoParams::NewL(); sl@0: CleanupStack::PushL(keyParams); sl@0: keyParams->AddL(*keyStr,keyType); sl@0: sl@0: //Create Key Object sl@0: TKeyProperty keyProperty; sl@0: CKey* key=CKey::NewL(keyProperty,*keyParams); sl@0: CleanupStack::PushL(key); sl@0: sl@0: //Retrieve a Hmac Factory Object sl@0: TRAPD(err,CHashFactory::CreateHashL(hmacImpl, sl@0: algorithmUid, sl@0: operationModeUid, sl@0: key, sl@0: NULL)); sl@0: sl@0: if(hmacImpl && (err == KErrNone)) sl@0: { sl@0: sl@0: //Push the Hmac Implementation Object onto the Cleanup Stack sl@0: CleanupStack::PushL(hmacImpl); sl@0: sl@0: RFs fsSession; sl@0: sl@0: //Create a connection to the file server sl@0: err = fsSession.Connect(); sl@0: sl@0: if(err != KErrNone) sl@0: { sl@0: ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err); sl@0: SetTestStepResult(EFail); sl@0: } sl@0: else sl@0: { sl@0: RFile sourceFile; sl@0: CleanupClosePushL(sourceFile); sl@0: sl@0: //Open the specified source file sl@0: err = sourceFile.Open(fsSession,sourcePath, EFileRead); sl@0: sl@0: if(err != KErrNone) sl@0: { sl@0: ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err); sl@0: SetTestStepResult(EFail); sl@0: } sl@0: else sl@0: { sl@0: sl@0: TInt sourceLength = 0; sl@0: TInt readPosition = 0; sl@0: TInt readIncrement = 0; sl@0: TBool hashComplete = EFalse; sl@0: TPtrC8 hashStr; sl@0: sl@0: User::LeaveIfError(sourceFile.Size(sourceLength)); sl@0: sl@0: //Divide the total size of the source file up into individual equal sized blocks to read sl@0: //over several increments sl@0: readIncrement = sourceLength/KDataReadBlocks; sl@0: sl@0: do sl@0: { sl@0: //Create a heap based descriptor to store the data sl@0: HBufC8* sourceData = HBufC8::NewL(readIncrement); sl@0: CleanupStack::PushL(sourceData); sl@0: TPtr8 sourcePtr = sourceData->Des(); sl@0: sl@0: //Read in a block of data from the source file from the current position sl@0: err = sourceFile.Read(readPosition,sourcePtr,readIncrement); sl@0: sl@0: //Update the read position by adding the number of bytes read sl@0: readPosition += readIncrement; sl@0: sl@0: if(readPosition == readIncrement) sl@0: { sl@0: //Read in the first block from the data file into the Hmac implementation object sl@0: hmacImpl->Hash(*sourceData); sl@0: INFO_PRINTF2(_L("Intial Hmac - Bytes Read: %d"), readPosition); sl@0: } sl@0: else if(readPosition >= sourceLength) sl@0: { sl@0: //Reading in the final block, constructs the complete hash value and returns it within a TPtrC8 sl@0: hashStr.Set(hmacImpl->Final(*sourceData)); sl@0: sl@0: //Sets the Complete Flag to ETrue in order to drop out of the loop sl@0: hashComplete = ETrue; sl@0: sl@0: TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length(); sl@0: INFO_PRINTF2(_L("Final Hmac - Bytes Read: %d"),totalRead); sl@0: } sl@0: else sl@0: { sl@0: //Update the message data within the Hmac object with the new block sl@0: hmacImpl->Update(*sourceData); sl@0: INFO_PRINTF2(_L("Hmac Update - Bytes Read: %d"), readPosition); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(sourceData); sl@0: sl@0: }while(hashComplete == EFalse); sl@0: sl@0: //Create a NULL TCharacteristics pointer sl@0: const TCharacteristics* charsPtr(NULL); sl@0: sl@0: //Retrieve the characteristics for the hash implementation object sl@0: TRAP_LOG(err, hmacImpl->GetCharacteristicsL(charsPtr)); sl@0: sl@0: //Static cast the characteristics to type THashCharacteristics sl@0: const THashCharacteristics* hashCharsPtr = static_cast(charsPtr); sl@0: sl@0: //The hash output size is returned in Bits, divide by 8 to get the Byte size sl@0: TInt hashSize = hashCharsPtr->iOutputSize/8; sl@0: sl@0: //Retrieve the final 8bit hash value and convert to 16bit sl@0: HBufC* hashData = HBufC::NewLC(hashSize); sl@0: TPtr hashPtr = hashData->Des(); sl@0: sl@0: hashPtr.Copy(hashStr); sl@0: sl@0: //Take the 16bit descriptor and convert the string to hexadecimal sl@0: TVariantPtrC convertHash; sl@0: convertHash.Set(hashPtr); sl@0: HBufC* hmacResult = convertHash.HexStringLC(); sl@0: sl@0: INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hmacResult); sl@0: INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash); sl@0: sl@0: //If the returned hash value matches the expected hash, Pass the test sl@0: if(*hmacResult == expectedHash) sl@0: { sl@0: INFO_PRINTF1(_L("*** Hmac - Incremental Hash : PASS ***")); sl@0: SetTestStepResult(EPass); sl@0: } sl@0: else sl@0: { sl@0: ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err); sl@0: SetTestStepResult(EFail); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(hmacResult); sl@0: CleanupStack::PopAndDestroy(hashData); sl@0: } sl@0: sl@0: //Cleanup the Source RFile sl@0: CleanupStack::PopAndDestroy(); sl@0: } sl@0: sl@0: fsSession.Close(); sl@0: sl@0: CleanupStack::PopAndDestroy(hmacImpl); sl@0: } sl@0: else sl@0: { sl@0: ERR_PRINTF2(_L("*** FAIL: Failed to Create Hmac Object - %d ***"), err); sl@0: SetTestStepResult(EFail); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(key); sl@0: CleanupStack::PopAndDestroy(keyParams); sl@0: CleanupStack::PopAndDestroy(keyStr); sl@0: } sl@0: } sl@0: sl@0: INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells()); sl@0: return TestStepResult(); sl@0: } sl@0: sl@0: sl@0: TVerdict CHmacIncrementalHmacStep::doTestStepPostambleL() sl@0: { sl@0: return TestStepResult(); sl@0: }