os/security/crypto/weakcryptospi/test/tcryptospi/src/symmetriccipherobjectreusestep.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcryptospi/test/tcryptospi/src/symmetriccipherobjectreusestep.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,399 @@
     1.4 +/*
     1.5 +* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +* Example CTestStep derived implementation
    1.19 +*
    1.20 +*/
    1.21 +
    1.22 +
    1.23 +/**
    1.24 + @file
    1.25 + @internalTechnology
    1.26 +*/
    1.27 +#include "symmetriccipherobjectreusestep.h"
    1.28 +
    1.29 +#include "filewriter.h"
    1.30 +#include "filecompare.h"
    1.31 +
    1.32 +using namespace CryptoSpi;
    1.33 +
    1.34 +
    1.35 +CSymmetricCipherObjectReuseStep::CSymmetricCipherObjectReuseStep(TInt aOffset) : iOffset(aOffset)
    1.36 +	{
    1.37 +	}
    1.38 +
    1.39 +
    1.40 +CSymmetricCipherObjectReuseStep::~CSymmetricCipherObjectReuseStep()
    1.41 +	{
    1.42 +	}
    1.43 +
    1.44 +
    1.45 +TVerdict CSymmetricCipherObjectReuseStep::doTestStepPreambleL()
    1.46 +	{
    1.47 +	SetTestStepResult(EPass);
    1.48 +	return TestStepResult();
    1.49 +	}
    1.50 +
    1.51 +
    1.52 +TVerdict CSymmetricCipherObjectReuseStep::doTestStepL()
    1.53 +	{
    1.54 +	INFO_PRINTF1(_L("*** Symmetric Cipher - Object Reuse ***"));
    1.55 +	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
    1.56 +  	if (TestStepResult()==EPass)
    1.57 +		{
    1.58 +		
    1.59 +		//Assume failure, unless all is successful
    1.60 +		SetTestStepResult(EFail);
    1.61 +		
    1.62 +		TPtrC keyPath;
    1.63 +		TPtrC srcPath;
    1.64 +		TVariantPtrC algorithm;
    1.65 +		TVariantPtrC operationMode;
    1.66 +		TVariantPtrC paddingMode;
    1.67 +		
    1.68 +		if(	!GetStringFromConfig(ConfigSection(),KConfigEncryptKey, keyPath) ||
    1.69 +			!GetStringFromConfig(ConfigSection(),KConfigSourcePath, srcPath) || 
    1.70 +			!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid, algorithm) || 
    1.71 +			!GetStringFromConfig(ConfigSection(),KConfigOperationMode, operationMode) ||
    1.72 +			!GetStringFromConfig(ConfigSection(),KConfigPaddingMode, paddingMode ))
    1.73 +			{
    1.74 +			User::Leave(KErrNotFound);
    1.75 +			}
    1.76 +		else
    1.77 +			{
    1.78 +			
    1.79 +			//Create an instance of TKeyProperty
    1.80 +			TKeyProperty keyProperty;
    1.81 +			
    1.82 +			//Load the key data using the
    1.83 +			CFileReader* keyData = CFileReader::NewLC(keyPath);
    1.84 +			
    1.85 +			CCryptoParams* params = CCryptoParams::NewLC(); 
    1.86 +			params->AddL( *keyData, KSymmetricKeyParameterUid); 
    1.87 +			
    1.88 +			CKey* key=CKey::NewL(keyProperty, *params);
    1.89 +			CleanupStack::PushL(key);
    1.90 +			
    1.91 +			CCryptoParams* xparams = NULL;
    1.92 +			
    1.93 +			if (TUid(algorithm) == KArc4Uid)
    1.94 +				{
    1.95 +				//Set the RC4 DiscardBytes to 0
    1.96 +				xparams = CCryptoParams::NewL();
    1.97 +				xparams->AddL(NULL, KARC4DiscardBytes);
    1.98 +				CleanupStack::PushL(xparams);
    1.99 +				}
   1.100 +
   1.101 +			if (TUid(algorithm) == KRc2Uid)
   1.102 +				{
   1.103 +				TInt keylen = TPtrC8(*keyData).Length() * 8;
   1.104 +				xparams = CCryptoParams::NewLC();
   1.105 +				
   1.106 +				//Set the RC2 EffectiveKeyLen according to the input key size
   1.107 +				xparams->AddL( keylen, KRC2EffectiveKeyLenBits);
   1.108 +				}
   1.109 +			
   1.110 +			INFO_PRINTF1(_L("Creating Symmetric Cipher Object..."));
   1.111 +
   1.112 +			// Create a Symmetric Cipher with the values from the ini config file	
   1.113 +			CryptoSpi::CSymmetricCipher * impl = NULL;	
   1.114 +			TRAPD(err,CSymmetricCipherFactory::CreateSymmetricCipherL
   1.115 +										(
   1.116 +										impl,
   1.117 +										algorithm,
   1.118 +										*key,
   1.119 +										KCryptoModeEncryptUid,
   1.120 +										operationMode,
   1.121 +										paddingMode,
   1.122 +										xparams));
   1.123 +	
   1.124 +			if(impl && (err == KErrNone))
   1.125 +				{
   1.126 +				CleanupStack::PushL(impl);
   1.127 +				
   1.128 +				const TInt KObjectReuseItterations = 5; // 5 iterations should be enough to check the object reuse feature
   1.129 +														// the no of iteration is reduced, to reduce the time taken for execution
   1.130 +				
   1.131 +				//Boolean to denote the state				
   1.132 +				TBool testPass = ETrue;
   1.133 +				
   1.134 +				/*************** Encrypt/Decrypt Reuse Loop ****************/
   1.135 +				for(TInt index = 0; index < KObjectReuseItterations; index++)
   1.136 +					{
   1.137 +					INFO_PRINTF3(_L("i=%d : START HEAP CELLS: %d"),index, User::CountAllocCells());
   1.138 +					
   1.139 +					//-----RESET IMPLEMENTATION OBJECT (NORMAL LOGGING)----------
   1.140 +					
   1.141 +					impl->Reset();
   1.142 +					
   1.143 +					TRAP(err,impl->SetKeyL(*key));
   1.144 +					
   1.145 +					if(err != KErrNone)
   1.146 +						{
   1.147 +						ERR_PRINTF3(_L("*** ERROR:%d - SetKeyL() i=%d ***"),err,index);
   1.148 +						User::Leave(err);	
   1.149 +						}
   1.150 +						
   1.151 +					TRAP(err,impl->SetCryptoModeL(KCryptoModeEncryptUid));
   1.152 +					
   1.153 +					if(err != KErrNone)
   1.154 +						{
   1.155 +						ERR_PRINTF3(_L("*** ERROR:%d - SetCryptoModeL() i=%d ***"),err,index);
   1.156 +						User::Leave(err);	
   1.157 +						}
   1.158 +						
   1.159 +					if(TUid(algorithm) != KArc4Uid)
   1.160 +						{
   1.161 +						
   1.162 +						impl->SetOperationModeL(operationMode);
   1.163 +					
   1.164 +						if(err != KErrNone)
   1.165 +							{
   1.166 +							ERR_PRINTF3(_L("*** ERROR:%d - SetOperationModeL() i=%d ***"),err,index);
   1.167 +							User::Leave(err);	
   1.168 +							}
   1.169 +							
   1.170 +						TRAP(err,impl->SetPaddingModeL(paddingMode));
   1.171 +						
   1.172 +						if(err != KErrNone)
   1.173 +							{
   1.174 +							ERR_PRINTF3(_L("*** ERROR:%d - SetPaddingModeL() i=%d ***"),err,index);
   1.175 +							User::Leave(err);	
   1.176 +							}
   1.177 +								
   1.178 +						}
   1.179 +
   1.180 +					//------------------------------------------------------
   1.181 +					
   1.182 +					//find out the block size for this algorithm
   1.183 +					TInt blockSize(0);
   1.184 +					
   1.185 +					if (TUid(operationMode) == KOperationModeCTRUid)
   1.186 +						{
   1.187 +						blockSize = CtrModeCalcBlockSizeL(*impl);
   1.188 +						}
   1.189 +					else
   1.190 +						{
   1.191 +						blockSize = impl->BlockSize();	
   1.192 +						}
   1.193 +					
   1.194 +					HBufC8* iv = NULL;
   1.195 +					
   1.196 +					if ((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
   1.197 +						{
   1.198 +						// block size is in bits so to allocate the correct number of bytes divide by 8
   1.199 +						// 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.
   1.200 +						// If this conditional block changes, take care to update the condition for deleting this allocated IV, near the end of this function.
   1.201 +						iv = HBufC8::NewLC(blockSize/8);	
   1.202 +						
   1.203 +						// blocksize is in bits so to allocate the correct number of 8 byte chunks divide by 64
   1.204 +						for(TInt i = 0 ; i <blockSize/64 ; i++)
   1.205 +						{
   1.206 +							iv->Des().Append(_L8("12345678"));
   1.207 +						}
   1.208 +		
   1.209 +						TRAP_LOG(err,impl->SetIvL(iv->Des())); 
   1.210 +						}
   1.211 +					
   1.212 +					// convert to bytesize
   1.213 +					blockSize/=8;
   1.214 +					blockSize += iOffset;
   1.215 +					
   1.216 +					//read from src file
   1.217 +					CFileReader* srcData = CFileReader::NewLC(srcPath,blockSize);
   1.218 +					
   1.219 +					// first step is to read from the src file one block
   1.220 +					// at a time, encrypt that block and then write
   1.221 +					// the encrypted block out to a temporary file.
   1.222 +					CFileWriter* encryptedDataWriter = CFileWriter::NewLC(TPtrC(KTempEncryptedFilePath));
   1.223 +					
   1.224 +					TInt numBlocks = srcData->NumBlocks();
   1.225 +					
   1.226 +					INFO_PRINTF1(_L("Starting Incremental Encryption..."));
   1.227 +					
   1.228 +					for(TInt i = 1 ; i <= numBlocks ; i++)
   1.229 +						{
   1.230 +						TRAP_LOG(err,srcData->ReadBlockL());
   1.231 +					
   1.232 +						//Create buffer for encrypted data
   1.233 +						TInt maxOutputLength = impl->MaxFinalOutputLength(TPtrC8(*srcData).Length());
   1.234 +						HBufC8* encrypted =	HBufC8::NewLC(maxOutputLength);
   1.235 +						TPtr8 encryptedPtr = encrypted->Des();
   1.236 +					
   1.237 +						if(i == numBlocks)
   1.238 +							{
   1.239 +							TRAP(err,impl->ProcessFinalL(*srcData, encryptedPtr));
   1.240 +							
   1.241 +							if(err != KErrNone)
   1.242 +								{
   1.243 +								ERR_PRINTF3(_L("*** ERROR:%d - ProcessFinalL() Block=%d ***"),err,i);
   1.244 +								User::Leave(err);	
   1.245 +								}
   1.246 +							}
   1.247 +						else
   1.248 +							{
   1.249 +							TRAP(err,impl->ProcessL(*srcData, encryptedPtr));
   1.250 +							
   1.251 +							if(err != KErrNone)
   1.252 +								{
   1.253 +								ERR_PRINTF3(_L("*** ERROR:%d - ProcessL() Block=%d ***"),err,i);
   1.254 +								User::Leave(err);	
   1.255 +								}
   1.256 +							}	
   1.257 +						
   1.258 +						TRAP_LOG(err,encryptedDataWriter->WriteBlockL(encryptedPtr));
   1.259 +					
   1.260 +						CleanupStack::PopAndDestroy(encrypted); 
   1.261 +						}
   1.262 +						
   1.263 +					CleanupStack::PopAndDestroy(encryptedDataWriter); 
   1.264 +						
   1.265 +					//Switch to Decrypt Crypto Mode
   1.266 +					TRAP(err,impl->SetCryptoModeL(KCryptoModeDecryptUid));
   1.267 +					
   1.268 +					if(err != KErrNone)
   1.269 +						{
   1.270 +						ERR_PRINTF3(_L("*** ERROR:%d - SetCryptoModeL() i=%d ***"),err,index);
   1.271 +						User::Leave(err);	
   1.272 +						}
   1.273 +
   1.274 +					//If in CTR mode need to reset the keystream to the start of the sequence used for encryption.
   1.275 +					if(TUid(operationMode) == KOperationModeCTRUid)
   1.276 +						{
   1.277 +						impl->SetIvL(iv->Des());
   1.278 +						}
   1.279 +
   1.280 +					
   1.281 +					// the next step is to read the previously encrypted data
   1.282 +					// from the temporary file decrypting this one block
   1.283 +					// at a time and outputing this to a temporary file.
   1.284 +					CFileReader* encryptedDataReader = CFileReader::NewLC(TPtrC(KTempEncryptedFilePath),blockSize);
   1.285 +					CFileWriter* decryptedDataWriter = CFileWriter::NewLC(TPtrC(KTempDecryptedFilePath));
   1.286 +					
   1.287 +					numBlocks = encryptedDataReader->NumBlocks();
   1.288 +					
   1.289 +					INFO_PRINTF1(_L("Starting Incremental Decryption..."));
   1.290 +					
   1.291 +					for(TInt i = 1 ; i <= numBlocks ; i++)
   1.292 +						{
   1.293 +						encryptedDataReader->ReadBlockL();
   1.294 +						//Create buffer for encrypted data
   1.295 +						TInt maxOutputLength = impl->MaxFinalOutputLength(TPtrC8(*encryptedDataReader).Length());
   1.296 +						HBufC8* decrypted =	HBufC8::NewLC(maxOutputLength);
   1.297 +						TPtr8 decryptedPtr = decrypted->Des();
   1.298 +
   1.299 +						//Perform the decryption operation
   1.300 +						if(i == numBlocks)
   1.301 +							{
   1.302 +							TRAP(err,impl->ProcessFinalL(*encryptedDataReader, decryptedPtr));
   1.303 +							
   1.304 +							if(err != KErrNone)
   1.305 +								{
   1.306 +								ERR_PRINTF3(_L("*** ERROR:%d - ProcessFinalL() Block=%d ***"),err,i);
   1.307 +								User::Leave(err);	
   1.308 +								}
   1.309 +							}
   1.310 +						else
   1.311 +							{
   1.312 +							TRAP(err,impl->ProcessL(*encryptedDataReader, decryptedPtr));
   1.313 +							
   1.314 +							if(err != KErrNone)
   1.315 +								{
   1.316 +								ERR_PRINTF3(_L("*** ERROR:%d - ProcessL() Block=%d ***"),err,i);
   1.317 +								User::Leave(err);	
   1.318 +								}
   1.319 +							}
   1.320 +							
   1.321 +						TRAP_LOG(err,decryptedDataWriter->WriteBlockL(decryptedPtr));
   1.322 +					
   1.323 +						CleanupStack::PopAndDestroy(decrypted); 
   1.324 +						}
   1.325 +						
   1.326 +					CleanupStack::PopAndDestroy(decryptedDataWriter); 
   1.327 +					CleanupStack::PopAndDestroy(encryptedDataReader); 
   1.328 +					CleanupStack::PopAndDestroy(srcData);
   1.329 +					
   1.330 +					if((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
   1.331 +						{
   1.332 +						// Iv is left on the cleanupstack at creation.  
   1.333 +						// If it becomes possible for operationMode to be modified during
   1.334 +						// the test this needs to be re-engineered.
   1.335 +						CleanupStack::PopAndDestroy(iv);
   1.336 +						}
   1.337 +					
   1.338 +					
   1.339 +					// compare the src with the file thats been
   1.340 +					// encrypted then decrypted
   1.341 +					// Note: Returning 0 means that the files match
   1.342 +					if(!TFileCompare::CompareL(srcPath,TPtrC(KTempDecryptedFilePath)))
   1.343 +						{
   1.344 +						INFO_PRINTF2(_L("*** PASS = Source File and Decrypted Data Match - i=%d ***"),index);
   1.345 +						}
   1.346 +					else
   1.347 +						{
   1.348 +						testPass = EFalse;
   1.349 +						ERR_PRINTF2(_L("*** ERROR: Source File and Decrypted Data Mismatch - i=%d ***"),index);
   1.350 +						}
   1.351 +						
   1.352 +					RFs rFs;
   1.353 +					rFs.Connect();
   1.354 +					rFs.Delete(	KTempDecryptedFilePath );
   1.355 +					rFs.Delete(	KTempEncryptedFilePath );
   1.356 +					rFs.Close();
   1.357 +										
   1.358 +					INFO_PRINTF3(_L("*** i=%d : END HEAP CELLS: %d ***"),index, User::CountAllocCells());
   1.359 +					}
   1.360 +					
   1.361 +				/*************** END OF LOOP ****************/
   1.362 +					
   1.363 +				CleanupStack::PopAndDestroy(impl);
   1.364 +				
   1.365 +				if(testPass == EFalse)
   1.366 +					{
   1.367 +					ERR_PRINTF1(_L("*** TEST FAIL : Symmetric Cipher - Object Reuse ***"));
   1.368 +					}
   1.369 +				else
   1.370 +					{
   1.371 +					INFO_PRINTF1(_L("*** TEST PASS : Symmetric Cipher - Object Reuse ***"));
   1.372 +					SetTestStepResult(EPass);	
   1.373 +					}
   1.374 +				
   1.375 +				}
   1.376 +			else
   1.377 +				{
   1.378 +				ERR_PRINTF2(_L("*** FAIL: Failed to Create Symmetric Object - %d ***"), err);
   1.379 +				User::Leave(err);	
   1.380 +				}
   1.381 +								
   1.382 +			if (TUid(algorithm) == KArc4Uid || TUid(algorithm) == KRc2Uid)
   1.383 +				{
   1.384 +				CleanupStack::PopAndDestroy(xparams);				
   1.385 +				}
   1.386 +				
   1.387 +			CleanupStack::PopAndDestroy(key);
   1.388 +			CleanupStack::PopAndDestroy(params);
   1.389 +			CleanupStack::PopAndDestroy(keyData);
   1.390 +			}
   1.391 +		}
   1.392 +	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
   1.393 +
   1.394 +	return TestStepResult();
   1.395 +
   1.396 +	}
   1.397 +
   1.398 +
   1.399 +TVerdict CSymmetricCipherObjectReuseStep::doTestStepPostambleL()
   1.400 +	{
   1.401 +	return TestStepResult();
   1.402 +	}