crypto/weakcryptospi/test/tcryptospi/src/symmetriccipherincrementalencryptdecryptstep.cpp
changeset 8 35751d3474b7
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     1 /*
       
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * Example CTestStep derived implementation
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21  @file
       
    22  @internalTechnology
       
    23 */
       
    24 #include "symmetriccipherincrementalencryptdecryptstep.h"
       
    25 
       
    26 #include "filewriter.h"
       
    27 #include "filecompare.h"
       
    28 
       
    29 using namespace CryptoSpi;
       
    30 
       
    31 CSymmetricCipherIncrementalEncryptDecryptStep::~CSymmetricCipherIncrementalEncryptDecryptStep()
       
    32 	{
       
    33 	}
       
    34 
       
    35 
       
    36 CSymmetricCipherIncrementalEncryptDecryptStep::CSymmetricCipherIncrementalEncryptDecryptStep(TInt aOffset) : iOffset(aOffset)
       
    37 	{
       
    38 	SetTestStepName(KSymmetricCipherIncrementalEncryptDecryptStep);
       
    39 	}
       
    40 
       
    41 
       
    42 TVerdict CSymmetricCipherIncrementalEncryptDecryptStep::doTestStepPreambleL()
       
    43 	{
       
    44 	SetTestStepResult(EPass);
       
    45 	return TestStepResult();
       
    46 	}
       
    47 
       
    48 
       
    49 TVerdict CSymmetricCipherIncrementalEncryptDecryptStep::doTestStepL()
       
    50 	{
       
    51 	INFO_PRINTF1(_L("*** Symmetric Cipher - Incremental Encrypt/Decrypt ***"));
       
    52 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
       
    53 	
       
    54   	if (TestStepResult()==EPass)
       
    55 		{
       
    56 		
       
    57 		//Assume faliure, unless all is successful
       
    58 		SetTestStepResult(EFail);
       
    59 		
       
    60 		TPtrC srcPath;
       
    61 		if (!GetStringFromConfig(ConfigSection(),KConfigSourcePath, srcPath))
       
    62 			{
       
    63 			User::Leave(KErrNotFound);
       
    64 			}
       
    65 		
       
    66 		TVariantPtrC operationMode;
       
    67 
       
    68 		// Create a Symmetric Cipher with the values from the ini file	
       
    69 		CryptoSpi::CSymmetricCipher * impl = NULL;	
       
    70 			
       
    71 		CKey* key = NULL;
       
    72 		SetupCipherL(ETrue, EFalse, operationMode, impl, key);
       
    73 
       
    74 		INFO_PRINTF1(_L("Plugin loaded."));
       
    75 	
       
    76 		CleanupStack::PushL(key);
       
    77 		CleanupStack::PushL(impl);
       
    78 		
       
    79 		HBufC8* iv = NULL;
       
    80 		TInt err(0);
       
    81 		
       
    82 		TInt blockSize(0);
       
    83 		if (TUid(operationMode) == KOperationModeCTRUid)
       
    84 			{
       
    85 			blockSize = CtrModeCalcBlockSizeL(*impl);
       
    86 			}
       
    87 		else
       
    88 			{
       
    89 			blockSize = impl->BlockSize();
       
    90 			}
       
    91 
       
    92 		if((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
       
    93 			{
       
    94 			// blocksize is in bits so to allocate the correct number of bytes divide by 8
       
    95 			// 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.
       
    96 			// If this conditional block changes, take care to update the condition for deleting this allocated IV, near the end of this function.
       
    97 			iv = HBufC8::NewLC(blockSize/8);	
       
    98 				
       
    99 			// blocksize is in bits so to allocate the correct number of 8 byte chunks divide by 64
       
   100 			for(TInt i = 0 ; i <blockSize/64 ; i++)
       
   101 				{
       
   102 				iv->Des().Append(_L8("12345678"));
       
   103 				}
       
   104 					
       
   105 			TRAP_LOG(err,impl->SetIvL(iv->Des()));
       
   106 			}	
       
   107 
       
   108 		// convert to bytesize
       
   109 		blockSize/=8;
       
   110 		blockSize += iOffset;
       
   111 		
       
   112 		//read from src file
       
   113 		CFileReader* srcData = CFileReader::NewLC(srcPath,blockSize);
       
   114 
       
   115 
       
   116 		// first step is to read from the src file one block
       
   117 		// at a time, encrypt that block and then write
       
   118 		// the encrypted block out to a temporary file.
       
   119 		CFileWriter* encryptedDataWriter = CFileWriter::NewLC(TPtrC(KEncryptedFilePath));
       
   120 			
       
   121 		TInt numBlocks = srcData->NumBlocks();
       
   122 				
       
   123 		INFO_PRINTF1(_L("Encrypting Source Data..."));
       
   124 	
       
   125 		for(TInt i = 1 ; i <= numBlocks ; i++)
       
   126 			{
       
   127 			TRAP_LOG(err,srcData->ReadBlockL());
       
   128 			
       
   129 			//Create buffer for encrypted data
       
   130 			TInt maxOutputLength = impl->MaxFinalOutputLength(TPtrC8(*srcData).Length());
       
   131 			HBufC8* encrypted =	HBufC8::NewLC(maxOutputLength);
       
   132 			TPtr8 encryptedPtr = encrypted->Des();
       
   133 
       
   134 			if(i == numBlocks)
       
   135 				{
       
   136 				TRAP_LOG(err,impl->ProcessFinalL(*srcData, encryptedPtr));
       
   137 				}
       
   138 			else
       
   139 				{
       
   140 				TRAP_LOG(err,impl->ProcessL(*srcData, encryptedPtr));
       
   141 				}
       
   142 					
       
   143 			TRAP_LOG(err,encryptedDataWriter->WriteBlockL(encryptedPtr));
       
   144 				
       
   145 			CleanupStack::PopAndDestroy(encrypted); 
       
   146 			}
       
   147 		CleanupStack::PopAndDestroy(encryptedDataWriter); 
       
   148 				
       
   149 		if(err == KErrNone)
       
   150 			{
       
   151 			//Switch to decrypt
       
   152 			TRAP_LOG(err,impl->SetCryptoModeL(KCryptoModeDecryptUid));
       
   153 					
       
   154 			//If in CTR mode need to reset the keystream to the start of the sequence used for encryption.
       
   155 			if(TUid(operationMode) == KOperationModeCTRUid)
       
   156 				{
       
   157 				impl->SetIvL(iv->Des());
       
   158 				}
       
   159 				
       
   160 			// the next step is to read the previously encrypted data
       
   161 			// from the temporary file decrypting this one block
       
   162 			// at a time and outputing this to a temporary file.
       
   163 			CFileReader* encryptedDataReader = CFileReader::NewLC(TPtrC(KEncryptedFilePath),blockSize);
       
   164 			CFileWriter* decryptedDataWriter = CFileWriter::NewLC(TPtrC(KDecryptedFilePath));
       
   165 				
       
   166 			numBlocks = encryptedDataReader->NumBlocks();
       
   167 					
       
   168 			INFO_PRINTF1(_L("Decrypting Data..."));
       
   169 					
       
   170 			for(TInt i = 1 ; i <= numBlocks ; i++)
       
   171 				{
       
   172 				TRAP_LOG(err,encryptedDataReader->ReadBlockL());
       
   173 						
       
   174 				//Create buffer for encrypted data
       
   175 				TInt maxOutputLength = impl->MaxFinalOutputLength(TPtrC8(*encryptedDataReader).Length());
       
   176 				HBufC8* decrypted =	HBufC8::NewLC(maxOutputLength);
       
   177 				TPtr8 decryptedPtr = decrypted->Des();
       
   178 
       
   179 				//Perform the decryption operation
       
   180 				if(i == numBlocks)
       
   181 					{
       
   182 					TRAP_LOG(err,impl->ProcessFinalL(*encryptedDataReader, decryptedPtr));
       
   183 					}
       
   184 				else
       
   185 					{
       
   186 					TRAP_LOG(err,impl->ProcessL(*encryptedDataReader, decryptedPtr));
       
   187 					}
       
   188 							
       
   189 				TRAP_LOG(err,decryptedDataWriter->WriteBlockL(decryptedPtr));
       
   190 					
       
   191 				CleanupStack::PopAndDestroy(decrypted); 
       
   192 				}
       
   193 						
       
   194 			CleanupStack::PopAndDestroy(decryptedDataWriter); 
       
   195 			CleanupStack::PopAndDestroy(encryptedDataReader); 	
       
   196 			}
       
   197 				
       
   198 		CleanupStack::PopAndDestroy(srcData);
       
   199 		if((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
       
   200 			{
       
   201 			// Iv is left on the cleanupstack at creation.  
       
   202 			// If it becomes possible for operationMode to be modified during
       
   203 			// the test this needs to be re-engineered.
       
   204 			CleanupStack::PopAndDestroy(iv);
       
   205 			}
       
   206 		CleanupStack::PopAndDestroy(impl);							
       
   207 		CleanupStack::PopAndDestroy(key);
       
   208 	
       
   209 		// compare the src with the file thats been
       
   210 		// encrypted then decrypted
       
   211 		if(!TFileCompare::CompareL(srcPath,TPtrC(KDecryptedFilePath)))
       
   212 			{
       
   213 			INFO_PRINTF1(_L("PASS : Source File and Decrypted Data Match"));
       
   214 			SetTestStepResult(EPass);
       
   215 			}
       
   216 		else
       
   217 			{
       
   218 			INFO_PRINTF1(_L("FAIL : Source File and Decrypted Data Mismatch"));	
       
   219 			SetTestStepResult(EFail);
       
   220 			}
       
   221 				
       
   222 		RFs rFs;
       
   223 		rFs.Connect();
       
   224 		rFs.Delete(	KDecryptedFilePath );
       
   225 		rFs.Delete(	KEncryptedFilePath );
       
   226 		rFs.Close();
       
   227 		}
       
   228 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
       
   229 
       
   230 	return TestStepResult();
       
   231 	}
       
   232 
       
   233 
       
   234 TVerdict CSymmetricCipherIncrementalEncryptDecryptStep::doTestStepPostambleL()
       
   235 	{
       
   236 	return TestStepResult();
       
   237 	}
       
   238