os/security/crypto/weakcryptospi/test/tcryptospi/src/symmetriccipherincrementalencryptdecryptstep.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tcryptospi/src/symmetriccipherincrementalencryptdecryptstep.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,238 @@
1.4 +/*
1.5 +* Copyright (c) 2007-2009 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 "symmetriccipherincrementalencryptdecryptstep.h"
1.28 +
1.29 +#include "filewriter.h"
1.30 +#include "filecompare.h"
1.31 +
1.32 +using namespace CryptoSpi;
1.33 +
1.34 +CSymmetricCipherIncrementalEncryptDecryptStep::~CSymmetricCipherIncrementalEncryptDecryptStep()
1.35 + {
1.36 + }
1.37 +
1.38 +
1.39 +CSymmetricCipherIncrementalEncryptDecryptStep::CSymmetricCipherIncrementalEncryptDecryptStep(TInt aOffset) : iOffset(aOffset)
1.40 + {
1.41 + SetTestStepName(KSymmetricCipherIncrementalEncryptDecryptStep);
1.42 + }
1.43 +
1.44 +
1.45 +TVerdict CSymmetricCipherIncrementalEncryptDecryptStep::doTestStepPreambleL()
1.46 + {
1.47 + SetTestStepResult(EPass);
1.48 + return TestStepResult();
1.49 + }
1.50 +
1.51 +
1.52 +TVerdict CSymmetricCipherIncrementalEncryptDecryptStep::doTestStepL()
1.53 + {
1.54 + INFO_PRINTF1(_L("*** Symmetric Cipher - Incremental Encrypt/Decrypt ***"));
1.55 + INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
1.56 +
1.57 + if (TestStepResult()==EPass)
1.58 + {
1.59 +
1.60 + //Assume faliure, unless all is successful
1.61 + SetTestStepResult(EFail);
1.62 +
1.63 + TPtrC srcPath;
1.64 + if (!GetStringFromConfig(ConfigSection(),KConfigSourcePath, srcPath))
1.65 + {
1.66 + User::Leave(KErrNotFound);
1.67 + }
1.68 +
1.69 + TVariantPtrC operationMode;
1.70 +
1.71 + // Create a Symmetric Cipher with the values from the ini file
1.72 + CryptoSpi::CSymmetricCipher * impl = NULL;
1.73 +
1.74 + CKey* key = NULL;
1.75 + SetupCipherL(ETrue, EFalse, operationMode, impl, key);
1.76 +
1.77 + INFO_PRINTF1(_L("Plugin loaded."));
1.78 +
1.79 + CleanupStack::PushL(key);
1.80 + CleanupStack::PushL(impl);
1.81 +
1.82 + HBufC8* iv = NULL;
1.83 + TInt err(0);
1.84 +
1.85 + TInt blockSize(0);
1.86 + if (TUid(operationMode) == KOperationModeCTRUid)
1.87 + {
1.88 + blockSize = CtrModeCalcBlockSizeL(*impl);
1.89 + }
1.90 + else
1.91 + {
1.92 + blockSize = impl->BlockSize();
1.93 + }
1.94 +
1.95 + if((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
1.96 + {
1.97 + // blocksize is in bits so to allocate the correct number of bytes divide by 8
1.98 + // iv is left on the cleanup stack for the duration of the test and deleted in a conditional at the end of the outer block.
1.99 + // If this conditional block changes, take care to update the condition for deleting this allocated IV, near the end of this function.
1.100 + iv = HBufC8::NewLC(blockSize/8);
1.101 +
1.102 + // blocksize is in bits so to allocate the correct number of 8 byte chunks divide by 64
1.103 + for(TInt i = 0 ; i <blockSize/64 ; i++)
1.104 + {
1.105 + iv->Des().Append(_L8("12345678"));
1.106 + }
1.107 +
1.108 + TRAP_LOG(err,impl->SetIvL(iv->Des()));
1.109 + }
1.110 +
1.111 + // convert to bytesize
1.112 + blockSize/=8;
1.113 + blockSize += iOffset;
1.114 +
1.115 + //read from src file
1.116 + CFileReader* srcData = CFileReader::NewLC(srcPath,blockSize);
1.117 +
1.118 +
1.119 + // first step is to read from the src file one block
1.120 + // at a time, encrypt that block and then write
1.121 + // the encrypted block out to a temporary file.
1.122 + CFileWriter* encryptedDataWriter = CFileWriter::NewLC(TPtrC(KEncryptedFilePath));
1.123 +
1.124 + TInt numBlocks = srcData->NumBlocks();
1.125 +
1.126 + INFO_PRINTF1(_L("Encrypting Source Data..."));
1.127 +
1.128 + for(TInt i = 1 ; i <= numBlocks ; i++)
1.129 + {
1.130 + TRAP_LOG(err,srcData->ReadBlockL());
1.131 +
1.132 + //Create buffer for encrypted data
1.133 + TInt maxOutputLength = impl->MaxFinalOutputLength(TPtrC8(*srcData).Length());
1.134 + HBufC8* encrypted = HBufC8::NewLC(maxOutputLength);
1.135 + TPtr8 encryptedPtr = encrypted->Des();
1.136 +
1.137 + if(i == numBlocks)
1.138 + {
1.139 + TRAP_LOG(err,impl->ProcessFinalL(*srcData, encryptedPtr));
1.140 + }
1.141 + else
1.142 + {
1.143 + TRAP_LOG(err,impl->ProcessL(*srcData, encryptedPtr));
1.144 + }
1.145 +
1.146 + TRAP_LOG(err,encryptedDataWriter->WriteBlockL(encryptedPtr));
1.147 +
1.148 + CleanupStack::PopAndDestroy(encrypted);
1.149 + }
1.150 + CleanupStack::PopAndDestroy(encryptedDataWriter);
1.151 +
1.152 + if(err == KErrNone)
1.153 + {
1.154 + //Switch to decrypt
1.155 + TRAP_LOG(err,impl->SetCryptoModeL(KCryptoModeDecryptUid));
1.156 +
1.157 + //If in CTR mode need to reset the keystream to the start of the sequence used for encryption.
1.158 + if(TUid(operationMode) == KOperationModeCTRUid)
1.159 + {
1.160 + impl->SetIvL(iv->Des());
1.161 + }
1.162 +
1.163 + // the next step is to read the previously encrypted data
1.164 + // from the temporary file decrypting this one block
1.165 + // at a time and outputing this to a temporary file.
1.166 + CFileReader* encryptedDataReader = CFileReader::NewLC(TPtrC(KEncryptedFilePath),blockSize);
1.167 + CFileWriter* decryptedDataWriter = CFileWriter::NewLC(TPtrC(KDecryptedFilePath));
1.168 +
1.169 + numBlocks = encryptedDataReader->NumBlocks();
1.170 +
1.171 + INFO_PRINTF1(_L("Decrypting Data..."));
1.172 +
1.173 + for(TInt i = 1 ; i <= numBlocks ; i++)
1.174 + {
1.175 + TRAP_LOG(err,encryptedDataReader->ReadBlockL());
1.176 +
1.177 + //Create buffer for encrypted data
1.178 + TInt maxOutputLength = impl->MaxFinalOutputLength(TPtrC8(*encryptedDataReader).Length());
1.179 + HBufC8* decrypted = HBufC8::NewLC(maxOutputLength);
1.180 + TPtr8 decryptedPtr = decrypted->Des();
1.181 +
1.182 + //Perform the decryption operation
1.183 + if(i == numBlocks)
1.184 + {
1.185 + TRAP_LOG(err,impl->ProcessFinalL(*encryptedDataReader, decryptedPtr));
1.186 + }
1.187 + else
1.188 + {
1.189 + TRAP_LOG(err,impl->ProcessL(*encryptedDataReader, decryptedPtr));
1.190 + }
1.191 +
1.192 + TRAP_LOG(err,decryptedDataWriter->WriteBlockL(decryptedPtr));
1.193 +
1.194 + CleanupStack::PopAndDestroy(decrypted);
1.195 + }
1.196 +
1.197 + CleanupStack::PopAndDestroy(decryptedDataWriter);
1.198 + CleanupStack::PopAndDestroy(encryptedDataReader);
1.199 + }
1.200 +
1.201 + CleanupStack::PopAndDestroy(srcData);
1.202 + if((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
1.203 + {
1.204 + // Iv is left on the cleanupstack at creation.
1.205 + // If it becomes possible for operationMode to be modified during
1.206 + // the test this needs to be re-engineered.
1.207 + CleanupStack::PopAndDestroy(iv);
1.208 + }
1.209 + CleanupStack::PopAndDestroy(impl);
1.210 + CleanupStack::PopAndDestroy(key);
1.211 +
1.212 + // compare the src with the file thats been
1.213 + // encrypted then decrypted
1.214 + if(!TFileCompare::CompareL(srcPath,TPtrC(KDecryptedFilePath)))
1.215 + {
1.216 + INFO_PRINTF1(_L("PASS : Source File and Decrypted Data Match"));
1.217 + SetTestStepResult(EPass);
1.218 + }
1.219 + else
1.220 + {
1.221 + INFO_PRINTF1(_L("FAIL : Source File and Decrypted Data Mismatch"));
1.222 + SetTestStepResult(EFail);
1.223 + }
1.224 +
1.225 + RFs rFs;
1.226 + rFs.Connect();
1.227 + rFs.Delete( KDecryptedFilePath );
1.228 + rFs.Delete( KEncryptedFilePath );
1.229 + rFs.Close();
1.230 + }
1.231 + INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
1.232 +
1.233 + return TestStepResult();
1.234 + }
1.235 +
1.236 +
1.237 +TVerdict CSymmetricCipherIncrementalEncryptDecryptStep::doTestStepPostambleL()
1.238 + {
1.239 + return TestStepResult();
1.240 + }
1.241 +