crypto/weakcryptospi/test/tcryptospi/src/symmetric_mac_incremental_step.cpp
changeset 8 35751d3474b7
child 49 2f10d260163b
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     1 /*
       
     2 * Copyright (c) 2008-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 "symmetric_mac_incremental_step.h"
       
    25 #include <cryptospi/cryptomacapi.h>
       
    26 #include "keys.h"
       
    27 #include <cryptospi/plugincharacteristics.h>
       
    28 
       
    29 using namespace CryptoSpi;
       
    30 
       
    31 CSymmetricMacIncrementalStep::~CSymmetricMacIncrementalStep()
       
    32 	{
       
    33 	}
       
    34 
       
    35 
       
    36 CSymmetricMacIncrementalStep::CSymmetricMacIncrementalStep()
       
    37 	{
       
    38 	SetTestStepName(KSymmetricMacIncrementalStep);
       
    39 	}
       
    40 
       
    41 
       
    42 TVerdict CSymmetricMacIncrementalStep::doTestStepPreambleL()
       
    43 	{
       
    44 	return EPass;
       
    45 	}
       
    46 
       
    47 
       
    48 TVerdict CSymmetricMacIncrementalStep::doTestStepL()
       
    49 	{
       
    50 	//Assume faliure, unless all is successful
       
    51 	SetTestStepResult(EFail);
       
    52 		
       
    53 	INFO_PRINTF1(_L("*** Symmetric Mac - Incremental ***"));
       
    54 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
       
    55 	
       
    56 	TPtrC keyPath;
       
    57 	TPtrC sourcePath;
       
    58 	TVariantPtrC algorithm;
       
    59 
       
    60 	if(	!GetStringFromConfig(ConfigSection(),KConfigEncryptKeyPath, keyPath) ||
       
    61 		!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid, algorithm) ||
       
    62 		!GetStringFromConfig(ConfigSection(),KConfigSourcePath, sourcePath))
       
    63 		{
       
    64 		User::Leave(KErrNotFound);
       
    65 		}
       
    66 	
       
    67 	// Create key 
       
    68 	TKeyProperty keyProperty;
       
    69 	CCryptoParams* keyParams = CCryptoParams::NewLC(); 
       
    70 	
       
    71 	HBufC8* convertKey = ReadInHexPlainTextL(keyPath);
       
    72 	CleanupStack::PushL(convertKey);
       
    73 	
       
    74 	keyParams->AddL(*convertKey, KSymmetricKeyParameterUid);
       
    75 	
       
    76 	CKey* key=CKey::NewL(keyProperty, *keyParams);
       
    77 	CleanupStack::PushL(key);
       
    78 	
       
    79 	// Create a Symmetric Mac with the values from the ini file	
       
    80 	CMac* macImpl = NULL;
       
    81 
       
    82 	TRAPD(err,CMacFactory::CreateMacL(macImpl,
       
    83 										algorithm,
       
    84 										*key,
       
    85 										NULL));									
       
    86 
       
    87 	if (err != KErrNone)
       
    88 		{
       
    89 		CleanupStack::PopAndDestroy(3, keyParams); // keyParams, convertKey, key
       
    90 		delete macImpl;
       
    91 		ERR_PRINTF2(_L("*** FAIL: Failed to Create Symmetric Mac Object - %d ***"), err);
       
    92 		return EFail;
       
    93 		}
       
    94 	
       
    95 	//Push the Mac Implementation Object onto the Cleanup Stack
       
    96 	CleanupStack::PushL(macImpl);
       
    97 	
       
    98 	RFs fsSession;
       
    99 	CleanupClosePushL(fsSession);
       
   100 	
       
   101 	//Create a connection to the file server	
       
   102 	err = fsSession.Connect();
       
   103 		
       
   104 	if (err != KErrNone)
       
   105 		{
       
   106 		ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err);
       
   107 		}	
       
   108 	else
       
   109 		{
       
   110 		RFile sourceFile;
       
   111 		CleanupClosePushL(sourceFile);
       
   112 		
       
   113 		//Open the specified source file		
       
   114 		err = sourceFile.Open(fsSession,sourcePath, EFileRead);
       
   115 				
       
   116 		if (err != KErrNone)
       
   117 			{
       
   118 			ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err);
       
   119 			}
       
   120 		else
       
   121 			{
       
   122 			TInt sourceLength = 0;
       
   123 			TInt readPosition = 0;
       
   124 			TInt readIncrement = 0;
       
   125 			TBool macComplete = EFalse;
       
   126 			TPtrC8 macStr;
       
   127 			
       
   128 			User::LeaveIfError(sourceFile.Size(sourceLength));
       
   129 			
       
   130 			//Divide the total size of the source file up into individual equal sized blocks to read
       
   131 			//over several increments
       
   132 			readIncrement = sourceLength/KDataReadBlocks;
       
   133 			
       
   134 			if (readIncrement == 0)
       
   135 				{
       
   136 				ERR_PRINTF2(_L("*** Error: Source File must be larger than %d bytes ***"), KDataReadBlocks);
       
   137 				User::LeaveIfError(KErrNotSupported);
       
   138 				}
       
   139 							
       
   140 			do 
       
   141 				{
       
   142 				//Create a heap based descriptor to store the data
       
   143 				HBufC8* sourceData = HBufC8::NewL(readIncrement);
       
   144 				CleanupStack::PushL(sourceData);
       
   145 				TPtr8 sourcePtr = sourceData->Des();
       
   146 				
       
   147 				//Read in a block of data from the source file from the current position
       
   148 				err = sourceFile.Read(readPosition,sourcePtr,readIncrement);
       
   149 				
       
   150 				HBufC8* convertSrc = ConvertFromHexFormatToRawL(*sourceData);
       
   151 				CleanupStack::PushL(convertSrc);
       
   152 				
       
   153 				//Update the read position by adding the number of bytes read
       
   154 				readPosition += readIncrement;
       
   155 				
       
   156 				if (readPosition == readIncrement)
       
   157 					{
       
   158 					//Read in the first block from the data file into the Mac implementation object
       
   159 					macImpl->MacL(*convertSrc);
       
   160 					INFO_PRINTF2(_L("Intial Mac - Bytes Read: %d"), readPosition);
       
   161 					}
       
   162 				else if (readPosition >= sourceLength)
       
   163 					{
       
   164 					//Reading in the final block, constructs the complete mac value and returns it within a TPtrC8
       
   165 					macStr.Set(macImpl->FinalL(*convertSrc));
       
   166 					
       
   167 					//Sets the Complete Flag to ETrue in order to drop out of the loop
       
   168 					macComplete = ETrue;
       
   169 					
       
   170 					TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
       
   171 					INFO_PRINTF2(_L("Final Mac - Bytes Read: %d"),totalRead);
       
   172 					}
       
   173 				else
       
   174 					{
       
   175 					//Update the message data within the Mac object with the new block
       
   176 					macImpl->UpdateL(*convertSrc);
       
   177 					INFO_PRINTF2(_L("Mac Update - Bytes Read: %d"), readPosition);
       
   178 					}
       
   179 
       
   180 				CleanupStack::PopAndDestroy(2, sourceData);	// sourceData, convertSrc
       
   181 				}while(macComplete == EFalse);
       
   182 			
       
   183 			//Create a NULL TCharacteristics pointer
       
   184 			const TCharacteristics* charsPtr(NULL);
       
   185 
       
   186 			//Retrieve the characteristics for the mac implementation object
       
   187 			TRAP_LOG(err, macImpl->GetCharacteristicsL(charsPtr));
       
   188 
       
   189 			//Static cast the characteristics to type TMacCharacteristics
       
   190 			const TMacCharacteristics* macCharsPtr = static_cast<const TMacCharacteristics*>(charsPtr);
       
   191 
       
   192 			//Create buffer for encrypted data
       
   193 			TInt macSize = macCharsPtr->iCipherAlgorithmChar->iBlockSize/8;
       
   194 			HBufC8* macData = HBufC8::NewLC(macSize);
       
   195 			TPtr8 macPtr = macData->Des();
       
   196 			
       
   197 			macPtr.Copy(macStr);
       
   198 			
       
   199 			//Check that expected data equals the encrypted data
       
   200 			HBufC8* encryptedFileData = ReadInHexCiphertextL();
       
   201 			CleanupStack::PushL(encryptedFileData);
       
   202 			
       
   203 			if(	!macPtr.Compare(TPtrC8(*encryptedFileData)))
       
   204 				{
       
   205 				INFO_PRINTF1(_L("*** Symmetric Mac - Incremental : PASS ***"));
       
   206 				SetTestStepResult(EPass);	
       
   207 				}
       
   208 			else
       
   209 				{
       
   210 				ERR_PRINTF2(_L("*** FAIL: Generated Mac and Expected Mac Mismatch  ***"), err);
       
   211 				}				
       
   212 
       
   213 			CleanupStack::PopAndDestroy(2, macData);	// macData, encryptedFileData
       
   214 			}
       
   215 		
       
   216 		//Cleanup the Source RFile	
       
   217 		CleanupStack::PopAndDestroy();	
       
   218 		}
       
   219 
       
   220 	CleanupStack::PopAndDestroy(5, keyParams);	// keyParams, convertKey, key, macImpl, &fsSessio
       
   221 		
       
   222 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
       
   223 	return TestStepResult();
       
   224 	}
       
   225 
       
   226 
       
   227 TVerdict CSymmetricMacIncrementalStep::doTestStepPostambleL()
       
   228 	{
       
   229 	return TestStepResult();
       
   230 	}