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 "hmacsetoperationmodecheckingstep.h" sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: using namespace CryptoSpi; sl@0: sl@0: CHmacSetOperationModeCheckingStep::~CHmacSetOperationModeCheckingStep() sl@0: { sl@0: } sl@0: sl@0: sl@0: CHmacSetOperationModeCheckingStep::CHmacSetOperationModeCheckingStep() sl@0: { sl@0: SetTestStepName(KHmacSetOperationModeCheckingStep); sl@0: } sl@0: sl@0: sl@0: TVerdict CHmacSetOperationModeCheckingStep::doTestStepPreambleL() sl@0: { sl@0: SetTestStepResult(EPass); sl@0: return TestStepResult(); sl@0: } sl@0: sl@0: sl@0: TVerdict CHmacSetOperationModeCheckingStep::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 Operation Mode Checking ***")); sl@0: INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells()); sl@0: sl@0: TVariantPtrC algorithmUid; sl@0: TVariantPtrC operationModeUid; sl@0: TVariantPtrC secondOperationModeUid; sl@0: TPtrC sourcePath; sl@0: TPtrC expectedHash; sl@0: TPtrC expectedHmac; 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(),KConfigSecondOperationMode,secondOperationModeUid) || sl@0: !GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) || sl@0: !GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash) || sl@0: !GetStringFromConfig(ConfigSection(),KConfigExSecondHashHmacValue,expectedHmac) || 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* hashHmacImpl = 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 invalid key type and key string sl@0: CCryptoParams* keyParams = CCryptoParams::NewL(); sl@0: CleanupStack::PushL(keyParams); sl@0: keyParams->AddL(*keyStr,keyType); sl@0: sl@0: //Create a valid CKey Object sl@0: TKeyProperty keyProperty; sl@0: CKey* key = CKey::NewL(keyProperty,*keyParams); sl@0: CleanupStack::PushL(key); sl@0: sl@0: //Construct an initial hash object with NO key, Catching any possible Leaves sl@0: TRAPD(err,CHashFactory::CreateHashL( sl@0: hashHmacImpl, sl@0: algorithmUid, sl@0: operationModeUid, sl@0: NULL, sl@0: NULL)); sl@0: sl@0: sl@0: if(hashHmacImpl && (err == KErrNone)) sl@0: { sl@0: sl@0: //Push the Implementation Object onto the Cleanup Stack sl@0: CleanupStack::PushL(hashHmacImpl); sl@0: sl@0: //Create a NULL TCharacteristics pointer sl@0: const TCharacteristics* charsPtrA(NULL); sl@0: sl@0: //Retrieve the characteristics for the hash implementation object sl@0: TRAP_LOG(err, hashHmacImpl->GetCharacteristicsL(charsPtrA)); sl@0: sl@0: //Static cast the characteristics to type THashCharacteristics sl@0: const THashCharacteristics* hashCharsPtrA = static_cast(charsPtrA); sl@0: sl@0: //The hash output size is returned in Bits, divide by 8 to get the Byte size sl@0: TInt hashSize = hashCharsPtrA->iOutputSize/8; sl@0: sl@0: //Retrieve the final 8bit hash value and convert to 16bit sl@0: HBufC* hashDataA = HBufC::NewLC(hashSize); sl@0: TPtr hashPtrA = hashDataA->Des(); sl@0: sl@0: hashPtrA.Copy(hashHmacImpl->Hash(*sourceData)); sl@0: sl@0: //Take the 16bit descriptor and convert the string to hexadecimal sl@0: TVariantPtrC convertHash; sl@0: convertHash.Set(hashPtrA); sl@0: sl@0: HBufC* resultA = convertHash.HexStringLC(); sl@0: sl@0: INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*resultA); sl@0: INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash); sl@0: sl@0: if(*resultA == expectedHash) sl@0: { sl@0: INFO_PRINTF1(_L("*** PRIMARY HASH VALID - STAGE 1 PASS ***")); sl@0: sl@0: //Set the valid key within the Hmac Implementation Object sl@0: TRAP(err,hashHmacImpl->SetKeyL(*key)); sl@0: sl@0: if(err!=KErrNone) sl@0: { sl@0: ERR_PRINTF2(_L("*** ERROR %d: Setting Key ***"),err); sl@0: User::Leave(err); sl@0: } sl@0: else sl@0: { sl@0: INFO_PRINTF1(_L("*** HMAC KEY SET ***")); sl@0: } sl@0: sl@0: //Set the Operation Mode of the Hmac Implementation Object sl@0: hashHmacImpl->SetOperationModeL(secondOperationModeUid); sl@0: sl@0: if(err!=KErrNone) sl@0: { sl@0: ERR_PRINTF3(_L("*** ERROR %d: Setting Operation Mode %S ***"),err,&secondOperationModeUid); sl@0: User::Leave(err); sl@0: } sl@0: else sl@0: { sl@0: INFO_PRINTF2(_L("*** OPERATION MODE SET : %S ***"),&secondOperationModeUid); sl@0: } sl@0: sl@0: //Create a NULL TCharacteristics pointer sl@0: const TCharacteristics* charsPtrB(NULL); sl@0: sl@0: //Retrieve the characteristics for the hash implementation object sl@0: TRAP_LOG(err, hashHmacImpl->GetCharacteristicsL(charsPtrB)); sl@0: sl@0: //Static cast the characteristics to type THashCharacteristics sl@0: const THashCharacteristics* hashCharsPtrB = static_cast(charsPtrB); sl@0: sl@0: //The hash output size is returned in Bits, divide by 8 to get the Byte size sl@0: hashSize = hashCharsPtrB->iOutputSize/8; sl@0: sl@0: //Retrieve the final 8bit hash value and convert to 16bit sl@0: HBufC* hashDataB = HBufC::NewLC(hashSize); sl@0: TPtr hashPtrB = hashDataB->Des(); sl@0: sl@0: hashPtrB.Copy(hashHmacImpl->Hash(*sourceData)); sl@0: sl@0: //Take the 16bit descriptor and convert the string to hexadecimal sl@0: convertHash.Set(hashPtrB); sl@0: HBufC* resultB = convertHash.HexStringLC(); sl@0: sl@0: INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*resultB); sl@0: INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHmac); sl@0: sl@0: if(*resultB == expectedHmac) sl@0: { sl@0: INFO_PRINTF1(_L("*** SECONDARY HASH VALID - STAGE 2 PASS ***")); sl@0: sl@0: //Set the Operation Mode of the Hmac Implementation Object sl@0: TRAP(err,hashHmacImpl->SetOperationModeL(operationModeUid)); sl@0: sl@0: if(err!=KErrNone) sl@0: { sl@0: ERR_PRINTF3(_L("*** ERROR %d: Setting Operation Mode %S ***"),err,&operationModeUid); sl@0: User::Leave(err); sl@0: } sl@0: else sl@0: { sl@0: INFO_PRINTF2(_L("*** OPERATION MODE SET : %S ***"),&operationModeUid); sl@0: } sl@0: sl@0: //Create a NULL TCharacteristics pointer sl@0: const TCharacteristics* charsPtrC(NULL); sl@0: sl@0: //Retrieve the characteristics for the hash implementation object sl@0: TRAP_LOG(err, hashHmacImpl->GetCharacteristicsL(charsPtrC)); sl@0: sl@0: //Static cast the characteristics to type THashCharacteristics sl@0: const THashCharacteristics* hashCharsPtrC = static_cast(charsPtrC); sl@0: sl@0: //The hash output size is returned in Bits, divide by 8 to get the Byte size sl@0: hashSize = hashCharsPtrC->iOutputSize/8; sl@0: sl@0: //Retrieve the final 8bit hash value and convert to 16bit sl@0: HBufC* hashDataC = HBufC::NewLC(hashSize); sl@0: TPtr hashPtrC = hashDataC->Des(); sl@0: sl@0: hashPtrC.Copy(hashHmacImpl->Hash(*sourceData)); sl@0: sl@0: //Take the 16bit descriptor and convert the string to hexadecimal sl@0: convertHash.Set(hashPtrC); sl@0: HBufC* resultC = convertHash.HexStringLC(); sl@0: sl@0: INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*resultC); sl@0: INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash); sl@0: sl@0: if(*resultC == expectedHash) sl@0: { sl@0: INFO_PRINTF1(_L("*** FINAL HASH VALID - STAGE 3 PASS ***")); sl@0: INFO_PRINTF1(_L("*** Hmac - Set Operation Mode Checking : PASS ***")); sl@0: SetTestStepResult(EPass); sl@0: } sl@0: else sl@0: { sl@0: ERR_PRINTF1(_L("*** STAGE 3 FAIL: Hash and Expected Value Mismatch ***")); sl@0: SetTestStepResult(EFail); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(resultC); sl@0: CleanupStack::PopAndDestroy(hashDataC); sl@0: } sl@0: else sl@0: { sl@0: ERR_PRINTF1(_L("*** STAGE 2 FAIL: Hash and Expected Value Mismatch ***")); sl@0: SetTestStepResult(EFail); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(resultB); sl@0: CleanupStack::PopAndDestroy(hashDataB); sl@0: } sl@0: else sl@0: { sl@0: ERR_PRINTF1(_L("*** STAGE 1 FAIL: Hash and Expected Value Match ***")); sl@0: SetTestStepResult(EFail); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(resultA); sl@0: CleanupStack::PopAndDestroy(hashDataA); sl@0: CleanupStack::PopAndDestroy(hashHmacImpl); sl@0: } sl@0: else if(err==KErrNotSupported) sl@0: { sl@0: if((((TUid)operationModeUid != KHashModeUid) && ((TUid)operationModeUid != KHmacModeUid)) || sl@0: ((TUid)algorithmUid != KMd2Uid) && (TUid)algorithmUid != KMd5Uid && (TUid)algorithmUid != KSha1Uid && (TUid)algorithmUid != KMd4Uid && (TUid)algorithmUid != KSha224Uid && (TUid)algorithmUid != KSha256Uid && (TUid)algorithmUid != KSha384Uid && (TUid)algorithmUid != KSha512Uid) sl@0: { sl@0: ERR_PRINTF2(_L("*** Object Load Failure - Invalid Operation Mode : %d ***"), err); sl@0: User::Leave(err); sl@0: } sl@0: else sl@0: { sl@0: SetTestStepResult(EFail); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: ERR_PRINTF2(_L("*** Hash/Hmac Facotry Object Load Failure : %d ***"), err); sl@0: User::Leave(err); 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: 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: else sl@0: { sl@0: SetTestStepResult(EFail); sl@0: } sl@0: sl@0: INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells()); sl@0: sl@0: return TestStepResult(); sl@0: } sl@0: sl@0: sl@0: TVerdict CHmacSetOperationModeCheckingStep::doTestStepPostambleL() sl@0: { sl@0: return TestStepResult(); sl@0: }