os/security/crypto/weakcryptospi/test/tcryptospi/src/hmacincrementalhmacwithreplicatestep.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tcryptospi/src/hmacincrementalhmacwithreplicatestep.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,300 @@
1.4 +/*
1.5 +* Copyright (c) 2007-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 "hmacincrementalhmacwithreplicatestep.h"
1.28 +
1.29 +#include <cryptospi/cryptohashapi.h>
1.30 +#include <cryptospi/keys.h>
1.31 +#include <cryptospi/plugincharacteristics.h>
1.32 +
1.33 +using namespace CryptoSpi;
1.34 +
1.35 +CHmacIncrementalHmacWithReplicateStep::~CHmacIncrementalHmacWithReplicateStep()
1.36 + {
1.37 + }
1.38 +
1.39 +
1.40 +CHmacIncrementalHmacWithReplicateStep::CHmacIncrementalHmacWithReplicateStep()
1.41 + {
1.42 + SetTestStepName(KHmacIncrementalHmacWithReplicateStep);
1.43 + }
1.44 +
1.45 +
1.46 +TVerdict CHmacIncrementalHmacWithReplicateStep::doTestStepPreambleL()
1.47 + {
1.48 + SetTestStepResult(EPass);
1.49 + return TestStepResult();
1.50 + }
1.51 +
1.52 +
1.53 +TVerdict CHmacIncrementalHmacWithReplicateStep::doTestStepL()
1.54 + {
1.55 + if (TestStepResult()==EPass)
1.56 + {
1.57 +
1.58 + //Assume faliure, unless all is successful
1.59 + SetTestStepResult(EFail);
1.60 +
1.61 + INFO_PRINTF1(_L("*** Hmac - Incremental Hash with Replicate ***"));
1.62 + INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
1.63 +
1.64 + TVariantPtrC algorithmUid;
1.65 + TVariantPtrC operationModeUid;
1.66 + TPtrC sourcePath;
1.67 + TPtrC expectedHash;
1.68 + TPtrC encryptKey;
1.69 + TVariantPtrC keyType;
1.70 +
1.71 + //Extract the Test Case ID parameter from the specified INI file
1.72 + if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
1.73 + !GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
1.74 + !GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
1.75 + !GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash) ||
1.76 + !GetStringFromConfig(ConfigSection(),KConfigEncryptKey,encryptKey) ||
1.77 + !GetStringFromConfig(ConfigSection(),KConfigEncryptKeyType,keyType))
1.78 + {
1.79 + ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
1.80 + SetTestStepResult(EFail);
1.81 + }
1.82 + else
1.83 + {
1.84 + //Create a pointer for the Hash + Key (Hmac) Implementation Object
1.85 + CHash* hmacImpl = NULL;
1.86 +
1.87 + //Convert encryption key to an 8 Bit Descriptor
1.88 + HBufC8* keyStr = HBufC8::NewLC(encryptKey.Length());
1.89 + TPtr8 keyStrPtr = keyStr->Des();
1.90 +
1.91 + keyStrPtr.Copy(encryptKey);
1.92 +
1.93 + //Create an new CryptoParams object to encapsulate the key type and secret key string
1.94 + CCryptoParams* keyParams = CCryptoParams::NewL();
1.95 + CleanupStack::PushL(keyParams);
1.96 + keyParams->AddL(*keyStr,keyType);
1.97 +
1.98 + //Create Key Object
1.99 + TKeyProperty keyProperty;
1.100 + CKey* key=CKey::NewL(keyProperty,*keyParams);
1.101 + CleanupStack::PushL(key);
1.102 +
1.103 + //Retrieve a Hmac Factory Object
1.104 + TRAPD(err,CHashFactory::CreateHashL(hmacImpl,
1.105 + algorithmUid,
1.106 + operationModeUid,
1.107 + key,
1.108 + NULL));
1.109 +
1.110 + if(hmacImpl && (err == KErrNone))
1.111 + {
1.112 +
1.113 + //Push the Hmac Implementation Object onto the Cleanup Stack
1.114 + CleanupStack::PushL(hmacImpl);
1.115 +
1.116 + RFs fsSession;
1.117 +
1.118 + //Create a connection to the file server
1.119 + err = fsSession.Connect();
1.120 +
1.121 + if(err != KErrNone)
1.122 + {
1.123 + ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err);
1.124 + SetTestStepResult(EFail);
1.125 + }
1.126 + else
1.127 + {
1.128 + RFile sourceFile;
1.129 + CleanupClosePushL(sourceFile);
1.130 +
1.131 + //Open the specified source file
1.132 + err = sourceFile.Open(fsSession,sourcePath, EFileRead);
1.133 +
1.134 + if(err != KErrNone)
1.135 + {
1.136 + ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err);
1.137 + SetTestStepResult(EFail);
1.138 + }
1.139 + else
1.140 + {
1.141 +
1.142 + TInt sourceLength = 0;
1.143 + TInt readPosition = 0;
1.144 + TInt readIncrement = 0;
1.145 + TBool hashComplete = EFalse;
1.146 + TBool hashReplicated = EFalse;
1.147 + TPtrC8 hashStr;
1.148 +
1.149 + CHash* hmacReplicateImpl = NULL;
1.150 +
1.151 + User::LeaveIfError(sourceFile.Size(sourceLength));
1.152 +
1.153 + //Divide the total size of the source file up into individual equal sized blocks to read
1.154 + //over several increments
1.155 + readIncrement = sourceLength/KDataReadBlocks;
1.156 +
1.157 + do
1.158 + {
1.159 + //Create a heap based descriptor to store the data
1.160 + HBufC8* sourceData = HBufC8::NewL(readIncrement);
1.161 + CleanupStack::PushL(sourceData);
1.162 + TPtr8 sourcePtr = sourceData->Des();
1.163 +
1.164 + //Read in a block of data from the source file from the current position
1.165 + err = sourceFile.Read(readPosition,sourcePtr,readIncrement);
1.166 +
1.167 + //Update the read position by adding the number of bytes read
1.168 + readPosition += readIncrement;
1.169 +
1.170 + if(readPosition == readIncrement)
1.171 + {
1.172 + //Read in the first block from the data file into the Hmac implementation object
1.173 + if(hashReplicated == EFalse)
1.174 + {
1.175 + hmacImpl->Hash(*sourceData);
1.176 + INFO_PRINTF2(_L("Intial Hmac - Bytes Read: %d"), readPosition);
1.177 + }
1.178 + else
1.179 + {
1.180 + hmacReplicateImpl->Hash(*sourceData);
1.181 + INFO_PRINTF2(_L("Intial Hmac (Replicate) - Bytes Read: %d"), readPosition);
1.182 + }
1.183 + }
1.184 + else if(readPosition >= sourceLength)
1.185 + {
1.186 + //Reading in the final block, constructs the complete hash value and returns it within a TPtrC8
1.187 + hashStr.Set(hmacReplicateImpl->Final(*sourceData));
1.188 +
1.189 + //Sets the Complete Flag to ETrue in order to drop out of the loop
1.190 + hashComplete = ETrue;
1.191 +
1.192 + TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
1.193 + INFO_PRINTF2(_L("Final Hmac - Bytes Read: %d"),totalRead);
1.194 + }
1.195 + //If the read position is half the source length and the implementation
1.196 + //object hasn't already been replicated
1.197 + else if((readPosition >= sourceLength/2) && (hashReplicated == EFalse))
1.198 + {
1.199 + INFO_PRINTF1(_L("Replicating Hmac Object..."));
1.200 +
1.201 + //Create a Copy of the existing Hmac Object with NO internal message state
1.202 + hmacReplicateImpl = hmacImpl->ReplicateL();
1.203 +
1.204 + hashReplicated = ETrue;
1.205 +
1.206 + //Sets the read position back to 0 inorder to restart the file read from the beginning
1.207 + readPosition =0;
1.208 +
1.209 + INFO_PRINTF2(_L("*** HMAC REPLICATE - Bytes Read: %d ***"), readPosition);
1.210 + }
1.211 + else
1.212 + {
1.213 + //Update the message data within the Hmac object with the new block
1.214 + if(hashReplicated == EFalse)
1.215 + {
1.216 + hmacImpl->Update(*sourceData);
1.217 + INFO_PRINTF2(_L("Hmac Update - Bytes Read: %d"), readPosition);
1.218 + }
1.219 + else
1.220 + {
1.221 + hmacReplicateImpl->Update(*sourceData);
1.222 + INFO_PRINTF2(_L("Hmac Update (Replicate) - Bytes Read: %d"), readPosition);
1.223 + }
1.224 + }
1.225 +
1.226 + CleanupStack::PopAndDestroy(sourceData);
1.227 +
1.228 +
1.229 + }while(hashComplete == EFalse);
1.230 +
1.231 + //Create a NULL TCharacteristics pointer
1.232 + const TCharacteristics* charsPtr(NULL);
1.233 +
1.234 + //Retrieve the characteristics for the hash implementation object
1.235 + TRAP_LOG(err, hmacImpl->GetCharacteristicsL(charsPtr));
1.236 +
1.237 + //Static cast the characteristics to type THashCharacteristics
1.238 + const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
1.239 +
1.240 + //The hash output size is returned in Bits, divide by 8 to get the Byte size
1.241 + TInt hashSize = hashCharsPtr->iOutputSize/8;
1.242 +
1.243 + //Retrieve the final 8bit hash value and convert to 16bit
1.244 + HBufC* hashData = HBufC::NewLC(hashSize);
1.245 + TPtr hashPtr = hashData->Des();
1.246 +
1.247 + hashPtr.Copy(hashStr);
1.248 +
1.249 + //Take the 16bit descriptor and convert the string to hexadecimal
1.250 + TVariantPtrC convertHash;
1.251 + convertHash.Set(hashPtr);
1.252 + HBufC* hmacResult = convertHash.HexStringLC();
1.253 +
1.254 + INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hmacResult);
1.255 + INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
1.256 +
1.257 + //If the returned hash value matches the expected hash, Pass the test
1.258 + if(*hmacResult == expectedHash)
1.259 + {
1.260 + INFO_PRINTF1(_L("*** Hmac - Incremental Hash with Replicate : PASS ***"));
1.261 + SetTestStepResult(EPass);
1.262 + }
1.263 + else
1.264 + {
1.265 + ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err);
1.266 + SetTestStepResult(EFail);
1.267 + }
1.268 +
1.269 + CleanupStack::PopAndDestroy(hmacResult);
1.270 + CleanupStack::PopAndDestroy(hashData);
1.271 +
1.272 + delete hmacReplicateImpl;
1.273 + }
1.274 +
1.275 + //Cleanup the Source RFile
1.276 + CleanupStack::PopAndDestroy();
1.277 + }
1.278 +
1.279 + fsSession.Close();
1.280 +
1.281 + CleanupStack::PopAndDestroy(hmacImpl);
1.282 + }
1.283 + else
1.284 + {
1.285 + ERR_PRINTF2(_L("*** FAIL: Failed to Create Hmac Object - %d ***"), err);
1.286 + SetTestStepResult(EFail);
1.287 + }
1.288 +
1.289 + CleanupStack::PopAndDestroy(key);
1.290 + CleanupStack::PopAndDestroy(keyParams);
1.291 + CleanupStack::PopAndDestroy(keyStr);
1.292 + }
1.293 + }
1.294 +
1.295 + INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
1.296 + return TestStepResult();
1.297 + }
1.298 +
1.299 +
1.300 +TVerdict CHmacIncrementalHmacWithReplicateStep::doTestStepPostambleL()
1.301 + {
1.302 + return TestStepResult();
1.303 + }