crypto/weakcryptospi/test/tcryptospi/src/symmetric_mac_incremental_with_replicate_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_with_replicate_step.h"
       
    25 #include <cryptospi/cryptomacapi.h>
       
    26 #include "keys.h"
       
    27 #include <cryptospi/plugincharacteristics.h>
       
    28 
       
    29 using namespace CryptoSpi;
       
    30 
       
    31 CSymmetricMacIncrementalWithReplicateStep::~CSymmetricMacIncrementalWithReplicateStep()
       
    32 	{
       
    33 	}
       
    34 
       
    35 
       
    36 CSymmetricMacIncrementalWithReplicateStep::CSymmetricMacIncrementalWithReplicateStep()
       
    37 	{
       
    38 	SetTestStepName(KSymmetricMacIncrementalWithReplicateStep);
       
    39 	}
       
    40 
       
    41 
       
    42 TVerdict CSymmetricMacIncrementalWithReplicateStep::doTestStepPreambleL()
       
    43 	{
       
    44 	return EPass;
       
    45 	}
       
    46 
       
    47 
       
    48 TVerdict CSymmetricMacIncrementalWithReplicateStep::doTestStepL()
       
    49 	{
       
    50 	//Assume faliure, unless all is successful
       
    51 	SetTestStepResult(EFail);
       
    52 		
       
    53 	INFO_PRINTF1(_L("*** Symmetric Mac - Incremental with Replicate ***"));
       
    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 pointer for the Hmac Implementation Object
       
    80 	CMac* macImpl= NULL;
       
    81 	
       
    82 	//Retrieve a Mac Factory Object			
       
    83 	TRAPD(err,CMacFactory::CreateMacL(macImpl,
       
    84 										algorithm,
       
    85 										*key,
       
    86 										NULL));
       
    87 				
       
    88 	if (err != KErrNone)
       
    89 		{
       
    90 		CleanupStack::PopAndDestroy(3, keyParams); // keyParams, convertKey, key
       
    91 		delete macImpl;
       
    92 		ERR_PRINTF2(_L("*** FAIL: Failed to Create Symmetric Mac Object - %d ***"), err);
       
    93 		return EFail;
       
    94 		}
       
    95 	
       
    96 	INFO_PRINTF1(_L("Plugin loaded."));
       
    97 	
       
    98 	//Push the Mac Implementation Object onto the Cleanup Stack
       
    99 	CleanupStack::PushL(macImpl);
       
   100 
       
   101 	RFs fsSession;
       
   102 	
       
   103 	User::LeaveIfError(fsSession.Connect());
       
   104 	CleanupClosePushL(fsSession);
       
   105 
       
   106 	RFile sourceFile;
       
   107 	CleanupClosePushL(sourceFile);
       
   108 	
       
   109 	//Open the specified source file
       
   110 	User::LeaveIfError(sourceFile.Open(fsSession,sourcePath, EFileRead));
       
   111 
       
   112 	TInt sourceLength = 0;
       
   113 	TInt readPosition = 0;
       
   114 	TInt readIncrement = 0;
       
   115 	TBool macComplete = EFalse;
       
   116 	TBool macReplicated = EFalse;
       
   117 	TPtrC8 macStr;
       
   118 	
       
   119 	CMac* macReplicateImpl = NULL;
       
   120 	
       
   121 	User::LeaveIfError(sourceFile.Size(sourceLength));
       
   122 	
       
   123 	//Divide the total size of the source file up into individual equal sized blocks to read
       
   124 	//over several increments
       
   125 	readIncrement = sourceLength/KDataReadBlocks;
       
   126 	
       
   127 	if (readIncrement == 0)
       
   128 		{
       
   129 		ERR_PRINTF2(_L("*** Error: Source File must be larger than %d bytes ***"), KDataReadBlocks);
       
   130 		User::LeaveIfError(KErrNotSupported);
       
   131 		}
       
   132 
       
   133 	do 
       
   134 		{							
       
   135 		//Create a heap based descriptor to store the data
       
   136 		HBufC8* sourceData = HBufC8::NewL(readIncrement);
       
   137 		CleanupStack::PushL(sourceData);
       
   138 		TPtr8 sourcePtr = sourceData->Des();
       
   139 		
       
   140 		//Read in a block of data from the source file from the current position
       
   141 		err = sourceFile.Read(readPosition,sourcePtr,readIncrement);
       
   142 		
       
   143 		HBufC8* convertSrc = ConvertFromHexFormatToRawL(*sourceData);
       
   144 		CleanupStack::PopAndDestroy(sourceData);
       
   145 		CleanupStack::PushL(convertSrc);
       
   146 		
       
   147 		//Update the read position by adding the number of bytes read
       
   148 		readPosition += readIncrement;
       
   149 		
       
   150 		if(readPosition == readIncrement)
       
   151 			{
       
   152 			//Read in the first block from the data file into the Mac implementation object
       
   153 			if(macReplicated == EFalse)
       
   154 				{
       
   155 				macImpl->MacL(*convertSrc);
       
   156 				INFO_PRINTF2(_L("Intial Mac - Bytes Read: %d"), readPosition);
       
   157 				}
       
   158 			else
       
   159 				{
       
   160 				macReplicateImpl->MacL(*convertSrc);
       
   161 				INFO_PRINTF2(_L("Intial Mac (Replicate) - Bytes Read: %d"), readPosition);	
       
   162 				}
       
   163 			CleanupStack::PopAndDestroy(convertSrc);
       
   164 			}
       
   165 		else if(readPosition >= sourceLength)
       
   166 			{
       
   167 			//Reading in the final block, constructs the complete hash value and returns it within a TPtrC8
       
   168 			macStr.Set(macReplicateImpl->FinalL(*convertSrc));
       
   169 			
       
   170 			CleanupStack::PopAndDestroy(convertSrc);
       
   171 			
       
   172 			//Sets the Complete Flag to ETrue in order to drop out of the loop
       
   173 			macComplete = ETrue;
       
   174 			
       
   175 			TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
       
   176 			INFO_PRINTF2(_L("Final Mac - Bytes Read: %d"),totalRead);
       
   177 			}
       
   178 		//If the read position is half the source length and the implementation
       
   179 		//object hasn't already been replicated
       
   180 		else if((readPosition >= sourceLength/2) && (macReplicated == EFalse))
       
   181 			{
       
   182 			INFO_PRINTF1(_L("Replicating Mac Object..."));
       
   183 			macImpl->UpdateL(*convertSrc);
       
   184 			
       
   185 			CleanupStack::PopAndDestroy(convertSrc);
       
   186 			
       
   187 			//Create a Copy of the existing Mac Object with NO internal message state
       
   188 			macReplicateImpl = macImpl->ReplicateL();
       
   189 			
       
   190 			macReplicated = ETrue;
       
   191 			
       
   192 			//Sets the read position back to 0 inorder to restart the file read from the beginning
       
   193 			readPosition =0;
       
   194 			CleanupStack::PushL(macReplicateImpl);
       
   195 			INFO_PRINTF2(_L("*** Mac REPLICATE - Bytes Read: %d ***"), readPosition);
       
   196 			}
       
   197 		else
       
   198 			{
       
   199 			//Update the message data within the Mac object with the new block
       
   200 			if(macReplicated == EFalse)
       
   201 				{
       
   202 				macImpl->UpdateL(*convertSrc);
       
   203 				INFO_PRINTF2(_L("Mac Update - Bytes Read: %d"), readPosition);		
       
   204 				}
       
   205 			else
       
   206 				{
       
   207 				macReplicateImpl->UpdateL(*convertSrc);
       
   208 				INFO_PRINTF2(_L("Mac Update (Replicate) - Bytes Read: %d"), readPosition);		
       
   209 				}
       
   210 			CleanupStack::PopAndDestroy(convertSrc);
       
   211 			}					
       
   212 		}while(macComplete == EFalse);
       
   213 
       
   214 	//Create a NULL TCharacteristics pointer
       
   215 	const TCharacteristics* charsPtr(NULL);
       
   216 	
       
   217 	//Retrieve the characteristics for the mac implementation object
       
   218 	TRAP_LOG(err, macImpl->GetCharacteristicsL(charsPtr));
       
   219 	
       
   220 	//Static cast the characteristics to type TMacCharacteristics
       
   221 	const TMacCharacteristics* macCharsPtr = static_cast<const TMacCharacteristics*>(charsPtr);
       
   222 	
       
   223 	//The mac output size is returned in Bits, divide by 8 to get the Byte size
       
   224 	TInt macSize = macCharsPtr->iCipherAlgorithmChar->iBlockSize/8;
       
   225 	HBufC8* macData = HBufC8::NewLC(macSize);
       
   226 	TPtr8 macPtr = macData->Des();
       
   227 
       
   228 	macPtr.Copy(macStr);
       
   229 
       
   230 	//Check that expected data equals the encrypted data
       
   231 	HBufC8* encryptedFileData = ReadInHexCiphertextL();
       
   232 	CleanupStack::PushL(encryptedFileData);
       
   233 			
       
   234 	if(	!macPtr.Compare(TPtrC8(*encryptedFileData)))
       
   235 		{
       
   236 		INFO_PRINTF1(_L("*** Mac - Incremental with Replicate : PASS ***"));
       
   237 		SetTestStepResult(EPass);	
       
   238 		}
       
   239 	else
       
   240 		{
       
   241 		ERR_PRINTF2(_L("*** FAIL: Mac Mismatch  ***"), err);
       
   242 		}						
       
   243 
       
   244 	CleanupStack::PopAndDestroy(9, keyParams);	// keyParams, convertKey, key, macImpl, &fsSession, &sourceFile, macReplicateImpl, macData, encryptedFileData
       
   245 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
       
   246 	return TestStepResult();  
       
   247 	}
       
   248 
       
   249 
       
   250 TVerdict CSymmetricMacIncrementalWithReplicateStep::doTestStepPostambleL()
       
   251 	{
       
   252 	return TestStepResult();
       
   253 	}