sl@0: /* sl@0: * Copyright (c) 2007-2009 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 "symmetriccipherincrementalencryptdecryptstep.h" sl@0: sl@0: #include "filewriter.h" sl@0: #include "filecompare.h" sl@0: sl@0: using namespace CryptoSpi; sl@0: sl@0: CSymmetricCipherIncrementalEncryptDecryptStep::~CSymmetricCipherIncrementalEncryptDecryptStep() sl@0: { sl@0: } sl@0: sl@0: sl@0: CSymmetricCipherIncrementalEncryptDecryptStep::CSymmetricCipherIncrementalEncryptDecryptStep(TInt aOffset) : iOffset(aOffset) sl@0: { sl@0: SetTestStepName(KSymmetricCipherIncrementalEncryptDecryptStep); sl@0: } sl@0: sl@0: sl@0: TVerdict CSymmetricCipherIncrementalEncryptDecryptStep::doTestStepPreambleL() sl@0: { sl@0: SetTestStepResult(EPass); sl@0: return TestStepResult(); sl@0: } sl@0: sl@0: sl@0: TVerdict CSymmetricCipherIncrementalEncryptDecryptStep::doTestStepL() sl@0: { sl@0: INFO_PRINTF1(_L("*** Symmetric Cipher - Incremental Encrypt/Decrypt ***")); sl@0: INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells()); 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: TPtrC srcPath; sl@0: if (!GetStringFromConfig(ConfigSection(),KConfigSourcePath, srcPath)) sl@0: { sl@0: User::Leave(KErrNotFound); sl@0: } sl@0: sl@0: TVariantPtrC operationMode; sl@0: sl@0: // Create a Symmetric Cipher with the values from the ini file sl@0: CryptoSpi::CSymmetricCipher * impl = NULL; sl@0: sl@0: CKey* key = NULL; sl@0: SetupCipherL(ETrue, EFalse, operationMode, impl, key); sl@0: sl@0: INFO_PRINTF1(_L("Plugin loaded.")); sl@0: sl@0: CleanupStack::PushL(key); sl@0: CleanupStack::PushL(impl); sl@0: sl@0: HBufC8* iv = NULL; sl@0: TInt err(0); sl@0: sl@0: TInt blockSize(0); sl@0: if (TUid(operationMode) == KOperationModeCTRUid) sl@0: { sl@0: blockSize = CtrModeCalcBlockSizeL(*impl); sl@0: } sl@0: else sl@0: { sl@0: blockSize = impl->BlockSize(); sl@0: } sl@0: sl@0: if((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid)) sl@0: { sl@0: // blocksize is in bits so to allocate the correct number of bytes divide by 8 sl@0: // iv is left on the cleanup stack for the duration of the test and deleted in a conditional at the end of the outer block. sl@0: // If this conditional block changes, take care to update the condition for deleting this allocated IV, near the end of this function. sl@0: iv = HBufC8::NewLC(blockSize/8); sl@0: sl@0: // blocksize is in bits so to allocate the correct number of 8 byte chunks divide by 64 sl@0: for(TInt i = 0 ; i Des().Append(_L8("12345678")); sl@0: } sl@0: sl@0: TRAP_LOG(err,impl->SetIvL(iv->Des())); sl@0: } sl@0: sl@0: // convert to bytesize sl@0: blockSize/=8; sl@0: blockSize += iOffset; sl@0: sl@0: //read from src file sl@0: CFileReader* srcData = CFileReader::NewLC(srcPath,blockSize); sl@0: sl@0: sl@0: // first step is to read from the src file one block sl@0: // at a time, encrypt that block and then write sl@0: // the encrypted block out to a temporary file. sl@0: CFileWriter* encryptedDataWriter = CFileWriter::NewLC(TPtrC(KEncryptedFilePath)); sl@0: sl@0: TInt numBlocks = srcData->NumBlocks(); sl@0: sl@0: INFO_PRINTF1(_L("Encrypting Source Data...")); sl@0: sl@0: for(TInt i = 1 ; i <= numBlocks ; i++) sl@0: { sl@0: TRAP_LOG(err,srcData->ReadBlockL()); sl@0: sl@0: //Create buffer for encrypted data sl@0: TInt maxOutputLength = impl->MaxFinalOutputLength(TPtrC8(*srcData).Length()); sl@0: HBufC8* encrypted = HBufC8::NewLC(maxOutputLength); sl@0: TPtr8 encryptedPtr = encrypted->Des(); sl@0: sl@0: if(i == numBlocks) sl@0: { sl@0: TRAP_LOG(err,impl->ProcessFinalL(*srcData, encryptedPtr)); sl@0: } sl@0: else sl@0: { sl@0: TRAP_LOG(err,impl->ProcessL(*srcData, encryptedPtr)); sl@0: } sl@0: sl@0: TRAP_LOG(err,encryptedDataWriter->WriteBlockL(encryptedPtr)); sl@0: sl@0: CleanupStack::PopAndDestroy(encrypted); sl@0: } sl@0: CleanupStack::PopAndDestroy(encryptedDataWriter); sl@0: sl@0: if(err == KErrNone) sl@0: { sl@0: //Switch to decrypt sl@0: TRAP_LOG(err,impl->SetCryptoModeL(KCryptoModeDecryptUid)); sl@0: sl@0: //If in CTR mode need to reset the keystream to the start of the sequence used for encryption. sl@0: if(TUid(operationMode) == KOperationModeCTRUid) sl@0: { sl@0: impl->SetIvL(iv->Des()); sl@0: } sl@0: sl@0: // the next step is to read the previously encrypted data sl@0: // from the temporary file decrypting this one block sl@0: // at a time and outputing this to a temporary file. sl@0: CFileReader* encryptedDataReader = CFileReader::NewLC(TPtrC(KEncryptedFilePath),blockSize); sl@0: CFileWriter* decryptedDataWriter = CFileWriter::NewLC(TPtrC(KDecryptedFilePath)); sl@0: sl@0: numBlocks = encryptedDataReader->NumBlocks(); sl@0: sl@0: INFO_PRINTF1(_L("Decrypting Data...")); sl@0: sl@0: for(TInt i = 1 ; i <= numBlocks ; i++) sl@0: { sl@0: TRAP_LOG(err,encryptedDataReader->ReadBlockL()); sl@0: sl@0: //Create buffer for encrypted data sl@0: TInt maxOutputLength = impl->MaxFinalOutputLength(TPtrC8(*encryptedDataReader).Length()); sl@0: HBufC8* decrypted = HBufC8::NewLC(maxOutputLength); sl@0: TPtr8 decryptedPtr = decrypted->Des(); sl@0: sl@0: //Perform the decryption operation sl@0: if(i == numBlocks) sl@0: { sl@0: TRAP_LOG(err,impl->ProcessFinalL(*encryptedDataReader, decryptedPtr)); sl@0: } sl@0: else sl@0: { sl@0: TRAP_LOG(err,impl->ProcessL(*encryptedDataReader, decryptedPtr)); sl@0: } sl@0: sl@0: TRAP_LOG(err,decryptedDataWriter->WriteBlockL(decryptedPtr)); sl@0: sl@0: CleanupStack::PopAndDestroy(decrypted); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(decryptedDataWriter); sl@0: CleanupStack::PopAndDestroy(encryptedDataReader); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(srcData); sl@0: if((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid)) sl@0: { sl@0: // Iv is left on the cleanupstack at creation. sl@0: // If it becomes possible for operationMode to be modified during sl@0: // the test this needs to be re-engineered. sl@0: CleanupStack::PopAndDestroy(iv); sl@0: } sl@0: CleanupStack::PopAndDestroy(impl); sl@0: CleanupStack::PopAndDestroy(key); sl@0: sl@0: // compare the src with the file thats been sl@0: // encrypted then decrypted sl@0: if(!TFileCompare::CompareL(srcPath,TPtrC(KDecryptedFilePath))) sl@0: { sl@0: INFO_PRINTF1(_L("PASS : Source File and Decrypted Data Match")); sl@0: SetTestStepResult(EPass); sl@0: } sl@0: else sl@0: { sl@0: INFO_PRINTF1(_L("FAIL : Source File and Decrypted Data Mismatch")); sl@0: SetTestStepResult(EFail); sl@0: } sl@0: sl@0: RFs rFs; sl@0: rFs.Connect(); sl@0: rFs.Delete( KDecryptedFilePath ); sl@0: rFs.Delete( KEncryptedFilePath ); sl@0: rFs.Close(); 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 CSymmetricCipherIncrementalEncryptDecryptStep::doTestStepPostambleL() sl@0: { sl@0: return TestStepResult(); sl@0: } sl@0: