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 "hmacsetkeycheckingstep.h" sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: using namespace CryptoSpi; sl@0: sl@0: CHmacSetKeyCheckingStep::~CHmacSetKeyCheckingStep() sl@0: { sl@0: } sl@0: sl@0: sl@0: CHmacSetKeyCheckingStep::CHmacSetKeyCheckingStep() sl@0: { sl@0: SetTestStepName(KHmacSetKeyCheckingStep); sl@0: } sl@0: sl@0: sl@0: TVerdict CHmacSetKeyCheckingStep::doTestStepPreambleL() sl@0: { sl@0: SetTestStepResult(EPass); sl@0: return TestStepResult(); sl@0: } sl@0: sl@0: sl@0: TVerdict CHmacSetKeyCheckingStep::doTestStepL() sl@0: { sl@0: if (TestStepResult()==EPass) sl@0: { sl@0: sl@0: //Assume faliure, unless all is successful sl@0: SetTestStepResult(EFail); sl@0: sl@0: INFO_PRINTF1(_L("*** Hmac - Set Key Checking ***")); 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 invalidEncryptKey; 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(),KConfigInvalidKey,invalidEncryptKey) || 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: RFs fsSession; sl@0: sl@0: //Create a connection to the file server sl@0: User::LeaveIfError(fsSession.Connect()); sl@0: sl@0: RFile sourceFile; sl@0: CleanupClosePushL(sourceFile); sl@0: sl@0: //Open the specified source file sl@0: User::LeaveIfError(sourceFile.Open(fsSession,sourcePath, EFileRead)); sl@0: sl@0: TInt sourceLength = 0; sl@0: User::LeaveIfError(sourceFile.Size(sourceLength)); sl@0: sl@0: //Create a heap based descriptor to store the data sl@0: HBufC8* sourceData = HBufC8::NewL(sourceLength); sl@0: CleanupStack::PushL(sourceData); sl@0: TPtr8 sourcePtr = sourceData->Des(); sl@0: sl@0: sourceFile.Read(sourcePtr); sl@0: sl@0: if(sourcePtr.Length() != sourceLength) sl@0: { sl@0: ERR_PRINTF1(_L("*** Error: Reading Source File ***")); sl@0: SetTestStepResult(EFail); sl@0: } sl@0: else sl@0: { sl@0: //Create a pointer for the Hash + Key (Hmac) Implementation Object sl@0: CHash* hmacImpl = NULL; sl@0: sl@0: //Convert encryption key to an 8 Bit Descriptor sl@0: HBufC8* invalidKeyStr = HBufC8::NewLC(invalidEncryptKey.Length()); sl@0: TPtr8 invalidKeyStrPtr = invalidKeyStr->Des(); sl@0: sl@0: invalidKeyStrPtr.Copy(invalidEncryptKey); sl@0: sl@0: //Create an new CryptoParams object to encapsulate the invalid key type and key string sl@0: CCryptoParams* invalidKeyParams = CCryptoParams::NewL(); sl@0: CleanupStack::PushL(invalidKeyParams); sl@0: invalidKeyParams->AddL(*invalidKeyStr,keyType); sl@0: sl@0: //Create Invalid Key Object sl@0: TKeyProperty invalidKeyProperty; sl@0: CKey* invalidKey = CKey::NewL(invalidKeyProperty,*invalidKeyParams); sl@0: CleanupStack::PushL(invalidKey); sl@0: sl@0: //Retrieve a Hmac Factory Object with an Invalid Key sl@0: TRAPD(err,CHashFactory::CreateHashL(hmacImpl, sl@0: algorithmUid, sl@0: operationModeUid, sl@0: invalidKey, 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: //Create a NULL TCharacteristics pointer sl@0: const TCharacteristics* invalidCharsPtr(NULL); sl@0: sl@0: //Retrieve the characteristics for the hash implementation object sl@0: TRAP_LOG(err, hmacImpl->GetCharacteristicsL(invalidCharsPtr)); sl@0: sl@0: //Static cast the characteristics to type THashCharacteristics sl@0: const THashCharacteristics* hashInvalidCharsPtr = static_cast(invalidCharsPtr); sl@0: sl@0: //The hash output size is returned in Bits, divide by 8 to get the Byte size sl@0: TInt hashSize = hashInvalidCharsPtr->iOutputSize/8; sl@0: sl@0: //Retrieve the final 8bit hash value and convert to 16bit sl@0: HBufC* invalidHashData = HBufC::NewLC(hashSize); sl@0: TPtr invalidHashPtr = invalidHashData->Des(); sl@0: sl@0: invalidHashPtr.Copy(hmacImpl->Hash(*sourceData)); sl@0: sl@0: //Take the 16bit descriptor and convert the string to hexadecimal sl@0: TVariantPtrC convertHash; sl@0: convertHash.Set(invalidHashPtr); sl@0: HBufC* invalidHmacResult = convertHash.HexStringLC(); sl@0: sl@0: INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*invalidHmacResult); sl@0: INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash); sl@0: sl@0: if(*invalidHmacResult != expectedHash) sl@0: { sl@0: INFO_PRINTF1(_L("*** INVALID KEY - STAGE 1 PASS ***")); sl@0: sl@0: //Convert encryption key to an 8 Bit Descriptor sl@0: HBufC8* validKeyStr = HBufC8::NewLC(encryptKey.Length()); sl@0: TPtr8 validKeyStrPtr = validKeyStr->Des(); sl@0: sl@0: validKeyStrPtr.Copy(encryptKey); sl@0: sl@0: //Create an new CryptoParams object to encapsulate the valid key type and secret key string sl@0: CCryptoParams* validKeyParams = CCryptoParams::NewL(); sl@0: CleanupStack::PushL(validKeyParams); sl@0: validKeyParams->AddL(*validKeyStr,keyType); sl@0: sl@0: //Create Valid Key Object sl@0: TKeyProperty validKeyProperty; sl@0: CKey* validKey = CKey::NewL(validKeyProperty,*validKeyParams); sl@0: CleanupStack::PushL(validKey); sl@0: sl@0: //Set the valid key within the Hmac Implementation Object sl@0: TRAP(err,hmacImpl->SetKeyL(*validKey)); sl@0: sl@0: if(err!=KErrNone) sl@0: { sl@0: ERR_PRINTF2(_L("*** ERROR %d: Setting Valid Key ***"),err); sl@0: User::Leave(err); sl@0: } sl@0: else sl@0: { sl@0: INFO_PRINTF1(_L("*** HMAC VALID KEY SET ***")); sl@0: } sl@0: sl@0: //Create a NULL TCharacteristics pointer sl@0: const TCharacteristics* validCharsPtr(NULL); sl@0: sl@0: //Retrieve the characteristics for the hash implementation object sl@0: TRAP_LOG(err, hmacImpl->GetCharacteristicsL(validCharsPtr)); sl@0: sl@0: //Static cast the characteristics to type THashCharacteristics sl@0: const THashCharacteristics* hashValidCharsPtr = static_cast(validCharsPtr); sl@0: sl@0: //The hash output size is returned in Bits, divide by 8 to get the Byte size sl@0: hashSize = hashValidCharsPtr->iOutputSize/8; sl@0: sl@0: //Retrieve the final 8bit hash value and convert to 16bit sl@0: HBufC* validHashData = HBufC::NewLC(hashSize); sl@0: TPtr validHashPtr = validHashData->Des(); sl@0: sl@0: validHashPtr.Copy(hmacImpl->Hash(*sourceData)); sl@0: sl@0: //Take the 16bit descriptor and convert the string to hexadecimal sl@0: convertHash.Set(validHashPtr); sl@0: HBufC* validHmacResult = convertHash.HexStringLC(); sl@0: sl@0: INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*validHmacResult); sl@0: INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash); sl@0: sl@0: if(*validHmacResult == expectedHash) sl@0: { sl@0: INFO_PRINTF1(_L("*** VALID KEY - STAGE 2 PASS ***")); sl@0: INFO_PRINTF1(_L("*** Hmac - Set Key Checking : PASS ***")); sl@0: SetTestStepResult(EPass); sl@0: } sl@0: else sl@0: { sl@0: ERR_PRINTF1(_L("*** STAGE 2 FAIL: Valid Hash and Expected Value Mismatch ***")); sl@0: SetTestStepResult(EFail); sl@0: } sl@0: CleanupStack::PopAndDestroy(validHmacResult); sl@0: CleanupStack::PopAndDestroy(validHashData); sl@0: sl@0: CleanupStack::PopAndDestroy(validKey); sl@0: CleanupStack::PopAndDestroy(validKeyParams); sl@0: CleanupStack::PopAndDestroy(validKeyStr); sl@0: } sl@0: else sl@0: { sl@0: ERR_PRINTF1(_L("*** STAGE 1 FAIL: Invalid Hash and Expected Value Match ***")); sl@0: SetTestStepResult(EFail); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(invalidHmacResult); sl@0: CleanupStack::PopAndDestroy(invalidHashData); 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(invalidKey); sl@0: CleanupStack::PopAndDestroy(invalidKeyParams); sl@0: CleanupStack::PopAndDestroy(invalidKeyStr); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(sourceData); sl@0: sl@0: //Cleanup the Source RFile sl@0: CleanupStack::PopAndDestroy(); sl@0: sl@0: fsSession.Close(); sl@0: } sl@0: sl@0: sl@0: INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells()); sl@0: } sl@0: sl@0: return TestStepResult(); sl@0: } sl@0: sl@0: sl@0: TVerdict CHmacSetKeyCheckingStep::doTestStepPostambleL() sl@0: { sl@0: return TestStepResult(); sl@0: }