os/security/crypto/weakcryptospi/test/tcryptospi/src/symmetriccipherencryptdecryptstep.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 "symmetriccipherencryptdecryptstep.h"
    25 
    26 using namespace CryptoSpi;
    27 
    28 CSymmetricCipherEncryptDecryptStep::~CSymmetricCipherEncryptDecryptStep()
    29 	{
    30 	}
    31 
    32 
    33 CSymmetricCipherEncryptDecryptStep::CSymmetricCipherEncryptDecryptStep()
    34 	{
    35 	SetTestStepName(KSymmetricCipherEncryptDecryptStep);
    36 	}
    37 
    38 
    39 TVerdict CSymmetricCipherEncryptDecryptStep::doTestStepPreambleL()
    40 	{
    41 	SetTestStepResult(EPass);
    42 	return TestStepResult();
    43 	}
    44 
    45 
    46 TVerdict CSymmetricCipherEncryptDecryptStep::doTestStepL()
    47 	{
    48 	INFO_PRINTF1(_L("*** Symmetric Cipher - Encrypt/Decrypt ***"));
    49 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
    50 	
    51   	if (TestStepResult()==EPass)
    52 		{
    53 		
    54 		//Assume faliure, unless all is successful
    55 		SetTestStepResult(EFail);
    56 		
    57 		TVariantPtrC operationMode;
    58 
    59 		// Create a Symmetric Cipher with the values from the ini file	
    60 		CryptoSpi::CSymmetricCipher * impl = NULL;	
    61 			
    62 		CKey* key = NULL;
    63 		SetupCipherL(ETrue, EFalse, operationMode, impl, key);
    64 
    65 		INFO_PRINTF1(_L("Plugin loaded."));
    66 	
    67 		CleanupStack::PushL(key);
    68 		CleanupStack::PushL(impl);
    69 
    70 				
    71 		HBufC8* iv = NULL;
    72 		TInt err(0);
    73 				
    74 		if((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
    75 			{
    76 			TInt blockSize(0);
    77 					
    78 			if (TUid(operationMode) == KOperationModeCTRUid)
    79 				{
    80 				blockSize = CtrModeCalcBlockSizeL(*impl);
    81 				}
    82 			else
    83 				{
    84 				blockSize = impl->BlockSize();
    85 				}
    86 			
    87 			// (blocksize is in bits so to allocate the correct number of bytes divide by 8)
    88 			// 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.
    89 			// If this conditional block changes, take care to update the condition for deleting this allocated IV, near the end of this function.
    90 			iv = HBufC8::NewLC(blockSize/8);	
    91 			
    92 			// blocksize is in bits so to allocate the correct number of 8 byte chunks divide by 64
    93 			for(TInt i = 0 ; i <blockSize/64 ; i++)
    94 				{
    95 				iv->Des().Append(_L8("12345678"));
    96 				}
    97 					
    98 			TRAP_LOG(err,impl->SetIvL(iv->Des()));
    99 			}
   100 			
   101 		//read from src file
   102 		HBufC8* srcData = ReadInPlaintextL();
   103 		CleanupStack::PushL(srcData);
   104 					
   105 		//Create buffer for encrypted data
   106 		TInt maxOutputLength = impl->MaxFinalOutputLength(srcData->Length());
   107 		HBufC8* encrypted =	HBufC8::NewLC(maxOutputLength);
   108 		TPtr8 encryptedPtr = encrypted->Des();
   109 
   110 		INFO_PRINTF1(_L("Encrypting Source Data..."));
   111 
   112 		//Perform the encryption operation
   113 		TRAP_LOG(err, impl->ProcessFinalL((*srcData), encryptedPtr));
   114 					
   115 		if(encryptedPtr.Compare((*srcData)) != 0)
   116 			{
   117 			//Switch to decrypt
   118 			TRAP_LOG(err,impl->SetCryptoModeL(KCryptoModeDecryptUid));
   119 
   120 			//If in CTR mode need to reset the keystream to the start of the sequence used for encryption
   121 			if(TUid(operationMode) == KOperationModeCTRUid)
   122 				{
   123 				impl->SetIvL(iv->Des());
   124 				}
   125 					
   126 			//Create a buffer for the decrypted data
   127 			maxOutputLength = encryptedPtr.Length();
   128 					
   129 			TInt bufSize = impl->MaxFinalOutputLength(maxOutputLength);
   130 					
   131 			HBufC8* output = HBufC8::NewLC(bufSize);
   132 			TPtr8 outputPtr = output->Des();
   133 					
   134 			INFO_PRINTF1(_L("Decrypting Data..."));
   135 					
   136 			//Perform the decryption operation
   137 			TRAP_LOG(err, impl->ProcessFinalL(encryptedPtr, outputPtr));
   138 					
   139 			if(err == KErrNone)
   140 				{
   141 				//Check that the source data matches the data thats 
   142 				//been encrypted then decrypted
   143 				if(	!outputPtr.Compare(*srcData) )
   144 					{
   145 					INFO_PRINTF1(_L("PASS : Decrypted Data and Source Match"));
   146 					SetTestStepResult(EPass);
   147 					}
   148 				else
   149 					{
   150 					INFO_PRINTF1(_L("FAIL : Decrypted Data and Source Mismatch"));	
   151 					SetTestStepResult(EFail);
   152 					}
   153 				}
   154 						
   155 			CleanupStack::PopAndDestroy(output); 
   156 			}
   157 		else
   158 			{
   159 			INFO_PRINTF1(_L("FAIL : Encrpyted Data and Source Data length and content is the same"));	
   160 			SetTestStepResult(EFail);	
   161 			}
   162 					
   163 		CleanupStack::PopAndDestroy(encrypted); 
   164 		CleanupStack::PopAndDestroy(srcData);
   165 		if((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
   166 			{
   167 			// Iv is left on the cleanupstack at creation.  
   168 			// If it becomes possible for operationMode to be modified during
   169 			// the test this needs to be re-engineered.
   170 			CleanupStack::PopAndDestroy(iv);	
   171 			}
   172 		CleanupStack::PopAndDestroy(2, key);		
   173 		}
   174 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
   175 
   176 	return TestStepResult();
   177 
   178 	}
   179 
   180 
   181 TVerdict CSymmetricCipherEncryptDecryptStep::doTestStepPostambleL()
   182 	{
   183 	return TestStepResult();
   184 	}