os/security/crypto/weakcryptospi/test/tcryptospi/src/symmetriccipherencrypteddatacheckstep.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 "symmetriccipherencrypteddatacheckstep.h"
    25 
    26 using namespace CryptoSpi;
    27 
    28 
    29 CSymmetricCipherEncryptedDataCheckStep::CSymmetricCipherEncryptedDataCheckStep()
    30 	{
    31 	}
    32 
    33 
    34 CSymmetricCipherEncryptedDataCheckStep::~CSymmetricCipherEncryptedDataCheckStep()
    35 
    36 	{
    37 	}
    38 
    39 
    40 TVerdict CSymmetricCipherEncryptedDataCheckStep::doTestStepPreambleL()
    41 	{
    42 	SetTestStepResult(EPass);
    43 	return TestStepResult();
    44 	}
    45 
    46 
    47 TVerdict CSymmetricCipherEncryptedDataCheckStep::doTestStepL()
    48 	{
    49 	INFO_PRINTF1(_L("*** Symmetric Cipher - Encrypted Data Check ***"));
    50 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
    51   	
    52   	if (TestStepResult()==EPass)
    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, ETrue, operationMode, impl, key);
    64 		
    65 		INFO_PRINTF1(_L("Plugin loaded."));
    66 	
    67 		CleanupStack::PushL(key);
    68 		CleanupStack::PushL(impl);
    69 			
    70 		//read from src file
    71 		HBufC8* srcData = ReadInPlaintextL();
    72 		CleanupStack::PushL(srcData);
    73 			
    74 		//Create buffer for encrypted data
    75 		TInt maxOutputLength = impl->MaxFinalOutputLength(srcData->Length());
    76 		HBufC8* encrypted =	HBufC8::NewLC(maxOutputLength);
    77 		TPtr8 encryptedPtr = encrypted->Des();
    78 		
    79 		TInt err;
    80 
    81 		if (((TUid)operationMode == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
    82 			{
    83 			TInt blockSize(0);
    84 					
    85 			if (TUid(operationMode) == KOperationModeCTRUid)
    86 				{
    87 				blockSize = CtrModeCalcBlockSizeL(*impl);
    88 				}
    89 			else
    90 				{
    91 				blockSize = impl->BlockSize();
    92 				}
    93 			
    94 			// blocksize is in bits so to allocate the correct number of bytes devide by 8
    95 			HBufC8* iv = HBufC8::NewLC(blockSize/8);
    96 				
    97 			// blocksize is in bits so to allocate the correct number of 8 byte chunks divide by 64
    98 			for(TInt i = 0 ; i < blockSize/64 ; i++)
    99 				{
   100 				iv->Des().Append(_L8("12345678"));
   101 				}
   102 						
   103 			TRAP_LOG(err,impl->SetIvL(iv->Des()));
   104 					
   105 			CleanupStack::PopAndDestroy(iv);
   106 			}
   107 					
   108 		INFO_PRINTF1(_L("Encrypting Source Data..."));
   109 
   110 		//Perform the encryption operation
   111 		TRAP_LOG(err, impl->ProcessFinalL((*srcData), encryptedPtr));
   112 	
   113 		if(err == KErrNone)
   114 			{
   115 			//Check that expected data equals the encrypted data
   116 			HBufC8* encryptedFileData = ReadInHexCiphertextL();
   117 			CleanupStack::PushL(encryptedFileData);
   118 
   119 					
   120 			if(	!encryptedPtr.Compare(TPtrC8(*encryptedFileData)))
   121 				{
   122 				INFO_PRINTF1(_L("PASS : Encrypted Cipher Text Matches Expected Data"));
   123 				SetTestStepResult(EPass);
   124 				}
   125 			else
   126 				{
   127 				INFO_PRINTF1(_L("FAIL : Encrypted Cipher Text and Expected Data Mismatch"));	
   128 				SetTestStepResult(EFail);
   129 				}
   130 
   131 			CleanupStack::PopAndDestroy(encryptedFileData);
   132 			}
   133 	
   134 		CleanupStack::PopAndDestroy(encrypted); 
   135 		CleanupStack::PopAndDestroy(srcData);
   136 		CleanupStack::PopAndDestroy(impl);
   137 		CleanupStack::PopAndDestroy(key);
   138 		}
   139 
   140 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
   141 
   142 	return TestStepResult();
   143 
   144 	}
   145 
   146 
   147 TVerdict CSymmetricCipherEncryptedDataCheckStep::doTestStepPostambleL()
   148 	{
   149 	return TestStepResult();
   150 	}