1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tcryptospi/src/mac_incremental_step.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,224 @@
1.4 +/*
1.5 +* Copyright (c) 2008-2010 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 "mac_incremental_step.h"
1.28 +#include <cryptospi/cryptomacapi.h>
1.29 +#include <cryptospi/keys.h>
1.30 +#include <cryptospi/plugincharacteristics.h>
1.31 +
1.32 +using namespace CryptoSpi;
1.33 +
1.34 +CMacIncrementalStep::~CMacIncrementalStep()
1.35 + {
1.36 + }
1.37 +
1.38 +
1.39 +CMacIncrementalStep::CMacIncrementalStep()
1.40 + {
1.41 + SetTestStepName(KMacIncrementalStep);
1.42 + }
1.43 +
1.44 +
1.45 +TVerdict CMacIncrementalStep::doTestStepPreambleL()
1.46 + {
1.47 + return EPass;
1.48 + }
1.49 +
1.50 +
1.51 +TVerdict CMacIncrementalStep::doTestStepL()
1.52 + {
1.53 + //Assume faliure, unless all is successful
1.54 + SetTestStepResult(EFail);
1.55 +
1.56 + INFO_PRINTF1(_L("*** Mac - Incremental ***"));
1.57 + INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
1.58 +
1.59 + TVariantPtrC algorithmUid;
1.60 + TPtrC sourcePath;
1.61 + TPtrC expectedMac;
1.62 + TPtrC encryptKey;
1.63 + TVariantPtrC keyType;
1.64 +
1.65 + //Extract the Test Case ID parameter from the specified INI file
1.66 + if (!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
1.67 + !GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
1.68 + !GetStringFromConfig(ConfigSection(),KConfigExMacValue,expectedMac) ||
1.69 + !GetStringFromConfig(ConfigSection(),KConfigEncryptKey,encryptKey) ||
1.70 + !GetStringFromConfig(ConfigSection(),KConfigEncryptKeyType,keyType))
1.71 + {
1.72 + ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
1.73 + return EFail;
1.74 + }
1.75 +
1.76 + //Create a pointer for the Mac + Key Implementation Object
1.77 + CMac* macImpl = NULL;
1.78 +
1.79 + //Convert encryption key to an 8 Bit Descriptor
1.80 + HBufC8* keyStr = HBufC8::NewLC(encryptKey.Length());
1.81 + TPtr8 keyStrPtr = keyStr->Des();
1.82 +
1.83 + keyStrPtr.Copy(encryptKey);
1.84 +
1.85 + //Create an new CryptoParams object to encapsulate the key type and secret key string
1.86 + CCryptoParams* keyParams = CCryptoParams::NewL();
1.87 + CleanupStack::PushL(keyParams);
1.88 + keyParams->AddL(*keyStr,keyType);
1.89 +
1.90 + //Create Key Object
1.91 + TKeyProperty keyProperty;
1.92 + CKey* key=CKey::NewL(keyProperty,*keyParams);
1.93 + CleanupStack::PushL(key);
1.94 +
1.95 + //Retrieve a Mac Factory Object
1.96 + TRAPD(err,CMacFactory::CreateMacL(macImpl,
1.97 + algorithmUid,
1.98 + *key,
1.99 + NULL));
1.100 +
1.101 + if (err != KErrNone)
1.102 + {
1.103 + CleanupStack::PopAndDestroy(3, keyStr); // keyStr, keyParams, key
1.104 + delete macImpl;
1.105 + ERR_PRINTF2(_L("*** FAIL: Failed to Create Mac Object - %d ***"), err);
1.106 + return EFail;
1.107 + }
1.108 +
1.109 + //Push the Mac Implementation Object onto the Cleanup Stack
1.110 + CleanupStack::PushL(macImpl);
1.111 +
1.112 + RFs fsSession;
1.113 + User::LeaveIfError(fsSession.Connect());
1.114 + CleanupClosePushL(fsSession);
1.115 +
1.116 + RFile sourceFile;
1.117 + CleanupClosePushL(sourceFile);
1.118 +
1.119 + //Open the specified source file
1.120 + User::LeaveIfError(sourceFile.Open(fsSession,sourcePath, EFileRead));
1.121 +
1.122 + TInt sourceLength = 0;
1.123 + TInt readPosition = 0;
1.124 + TInt readIncrement = 0;
1.125 + TBool macComplete = EFalse;
1.126 + TPtrC8 macStr;
1.127 +
1.128 + User::LeaveIfError(sourceFile.Size(sourceLength));
1.129 +
1.130 + //Divide the total size of the source file up into individual equal sized blocks to read
1.131 + //over several increments
1.132 + readIncrement = sourceLength/KDataReadBlocks;
1.133 +
1.134 + if (readIncrement == 0)
1.135 + {
1.136 + ERR_PRINTF2(_L("*** Error: Source File must be larger than %d bytes ***"), KDataReadBlocks);
1.137 + User::LeaveIfError(KErrNotSupported);
1.138 + }
1.139 +
1.140 + do
1.141 + {
1.142 + //Create a heap based descriptor to store the data
1.143 + HBufC8* sourceData = HBufC8::NewL(readIncrement);
1.144 + CleanupStack::PushL(sourceData);
1.145 + TPtr8 sourcePtr = sourceData->Des();
1.146 +
1.147 + //Read in a block of data from the source file from the current position
1.148 + err = sourceFile.Read(readPosition,sourcePtr,readIncrement);
1.149 +
1.150 + //Update the read position by adding the number of bytes read
1.151 + readPosition += readIncrement;
1.152 +
1.153 + if (readPosition == readIncrement)
1.154 + {
1.155 + //Read in the first block from the data file into the Mac implementation object
1.156 + macImpl->MacL(*sourceData);
1.157 + INFO_PRINTF2(_L("Intial Mac - Bytes Read: %d"), readPosition);
1.158 + }
1.159 + else if (readPosition >= sourceLength)
1.160 + {
1.161 + //Reading in the final block, constructs the complete mac value and returns it within a TPtrC8
1.162 + macStr.Set(macImpl->FinalL(*sourceData));
1.163 +
1.164 + //Sets the Complete Flag to ETrue in order to drop out of the loop
1.165 + macComplete = ETrue;
1.166 +
1.167 + TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
1.168 + INFO_PRINTF2(_L("Final Mac - Bytes Read: %d"),totalRead);
1.169 + }
1.170 + else
1.171 + {
1.172 + //Update the message data within the Mac object with the new block
1.173 + macImpl->UpdateL(*sourceData);
1.174 + INFO_PRINTF2(_L("Mac Update - Bytes Read: %d"), readPosition);
1.175 + }
1.176 +
1.177 + CleanupStack::PopAndDestroy(sourceData);
1.178 +
1.179 + }while(macComplete == EFalse);
1.180 +
1.181 + //Create a NULL TCharacteristics pointer
1.182 + const TCharacteristics* charsPtr(NULL);
1.183 +
1.184 + //Retrieve the characteristics for the mac implementation object
1.185 + TRAP_LOG(err, macImpl->GetCharacteristicsL(charsPtr));
1.186 +
1.187 + //Static cast the characteristics to type TMacCharacteristics
1.188 + const TMacCharacteristics* macCharsPtr = static_cast<const TMacCharacteristics*>(charsPtr);
1.189 +
1.190 + //The mac output size is returned in Bits, divide by 8 to get the Byte size
1.191 + TInt macSize = macCharsPtr->iHashAlgorithmChar->iOutputSize/8;
1.192 +
1.193 + //Retrieve the final 8 bit mac value and convert to 16bit
1.194 + HBufC* macData = HBufC::NewLC(macSize);
1.195 + TPtr macPtr = macData->Des();
1.196 +
1.197 + macPtr.Copy(macStr);
1.198 +
1.199 + //Take the 16 bit descriptor and convert the string to hexadecimal
1.200 + TVariantPtrC convertMac;
1.201 + convertMac.Set(macPtr);
1.202 + HBufC* macResult = convertMac.HexStringLC();
1.203 +
1.204 + INFO_PRINTF2(_L("*** Mac Data: %S ***"),&*macResult);
1.205 + INFO_PRINTF2(_L("*** Expected Mac: %S ***"),&expectedMac);
1.206 +
1.207 + //If the returned mac value matches the expected mac, Pass the test
1.208 + if (*macResult == expectedMac)
1.209 + {
1.210 + INFO_PRINTF1(_L("*** Mac - Incremental Hash : PASS ***"));
1.211 + SetTestStepResult(EPass);
1.212 + }
1.213 + else
1.214 + {
1.215 + ERR_PRINTF2(_L("*** FAIL: Generated Mac and Expected Mac Mismatch ***"), err);
1.216 + }
1.217 +
1.218 + CleanupStack::PopAndDestroy(8, keyStr); // keyStr, keyParams, key, macImpl, &fsSession, &sourceFile, macData, macResult
1.219 + INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
1.220 + return TestStepResult();
1.221 + }
1.222 +
1.223 +
1.224 +TVerdict CMacIncrementalStep::doTestStepPostambleL()
1.225 + {
1.226 + return TestStepResult();
1.227 + }