os/security/crypto/weakcryptospi/test/tcryptospi/src/symmetriccipherincrementalencryptdecryptstep.cpp
Update contrib.
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Example CTestStep derived implementation
24 #include "symmetriccipherincrementalencryptdecryptstep.h"
26 #include "filewriter.h"
27 #include "filecompare.h"
29 using namespace CryptoSpi;
31 CSymmetricCipherIncrementalEncryptDecryptStep::~CSymmetricCipherIncrementalEncryptDecryptStep()
36 CSymmetricCipherIncrementalEncryptDecryptStep::CSymmetricCipherIncrementalEncryptDecryptStep(TInt aOffset) : iOffset(aOffset)
38 SetTestStepName(KSymmetricCipherIncrementalEncryptDecryptStep);
42 TVerdict CSymmetricCipherIncrementalEncryptDecryptStep::doTestStepPreambleL()
44 SetTestStepResult(EPass);
45 return TestStepResult();
49 TVerdict CSymmetricCipherIncrementalEncryptDecryptStep::doTestStepL()
51 INFO_PRINTF1(_L("*** Symmetric Cipher - Incremental Encrypt/Decrypt ***"));
52 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
54 if (TestStepResult()==EPass)
57 //Assume faliure, unless all is successful
58 SetTestStepResult(EFail);
61 if (!GetStringFromConfig(ConfigSection(),KConfigSourcePath, srcPath))
63 User::Leave(KErrNotFound);
66 TVariantPtrC operationMode;
68 // Create a Symmetric Cipher with the values from the ini file
69 CryptoSpi::CSymmetricCipher * impl = NULL;
72 SetupCipherL(ETrue, EFalse, operationMode, impl, key);
74 INFO_PRINTF1(_L("Plugin loaded."));
76 CleanupStack::PushL(key);
77 CleanupStack::PushL(impl);
83 if (TUid(operationMode) == KOperationModeCTRUid)
85 blockSize = CtrModeCalcBlockSizeL(*impl);
89 blockSize = impl->BlockSize();
92 if((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
94 // blocksize is in bits so to allocate the correct number of bytes divide by 8
95 // 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.
96 // If this conditional block changes, take care to update the condition for deleting this allocated IV, near the end of this function.
97 iv = HBufC8::NewLC(blockSize/8);
99 // blocksize is in bits so to allocate the correct number of 8 byte chunks divide by 64
100 for(TInt i = 0 ; i <blockSize/64 ; i++)
102 iv->Des().Append(_L8("12345678"));
105 TRAP_LOG(err,impl->SetIvL(iv->Des()));
108 // convert to bytesize
110 blockSize += iOffset;
113 CFileReader* srcData = CFileReader::NewLC(srcPath,blockSize);
116 // first step is to read from the src file one block
117 // at a time, encrypt that block and then write
118 // the encrypted block out to a temporary file.
119 CFileWriter* encryptedDataWriter = CFileWriter::NewLC(TPtrC(KEncryptedFilePath));
121 TInt numBlocks = srcData->NumBlocks();
123 INFO_PRINTF1(_L("Encrypting Source Data..."));
125 for(TInt i = 1 ; i <= numBlocks ; i++)
127 TRAP_LOG(err,srcData->ReadBlockL());
129 //Create buffer for encrypted data
130 TInt maxOutputLength = impl->MaxFinalOutputLength(TPtrC8(*srcData).Length());
131 HBufC8* encrypted = HBufC8::NewLC(maxOutputLength);
132 TPtr8 encryptedPtr = encrypted->Des();
136 TRAP_LOG(err,impl->ProcessFinalL(*srcData, encryptedPtr));
140 TRAP_LOG(err,impl->ProcessL(*srcData, encryptedPtr));
143 TRAP_LOG(err,encryptedDataWriter->WriteBlockL(encryptedPtr));
145 CleanupStack::PopAndDestroy(encrypted);
147 CleanupStack::PopAndDestroy(encryptedDataWriter);
152 TRAP_LOG(err,impl->SetCryptoModeL(KCryptoModeDecryptUid));
154 //If in CTR mode need to reset the keystream to the start of the sequence used for encryption.
155 if(TUid(operationMode) == KOperationModeCTRUid)
157 impl->SetIvL(iv->Des());
160 // the next step is to read the previously encrypted data
161 // from the temporary file decrypting this one block
162 // at a time and outputing this to a temporary file.
163 CFileReader* encryptedDataReader = CFileReader::NewLC(TPtrC(KEncryptedFilePath),blockSize);
164 CFileWriter* decryptedDataWriter = CFileWriter::NewLC(TPtrC(KDecryptedFilePath));
166 numBlocks = encryptedDataReader->NumBlocks();
168 INFO_PRINTF1(_L("Decrypting Data..."));
170 for(TInt i = 1 ; i <= numBlocks ; i++)
172 TRAP_LOG(err,encryptedDataReader->ReadBlockL());
174 //Create buffer for encrypted data
175 TInt maxOutputLength = impl->MaxFinalOutputLength(TPtrC8(*encryptedDataReader).Length());
176 HBufC8* decrypted = HBufC8::NewLC(maxOutputLength);
177 TPtr8 decryptedPtr = decrypted->Des();
179 //Perform the decryption operation
182 TRAP_LOG(err,impl->ProcessFinalL(*encryptedDataReader, decryptedPtr));
186 TRAP_LOG(err,impl->ProcessL(*encryptedDataReader, decryptedPtr));
189 TRAP_LOG(err,decryptedDataWriter->WriteBlockL(decryptedPtr));
191 CleanupStack::PopAndDestroy(decrypted);
194 CleanupStack::PopAndDestroy(decryptedDataWriter);
195 CleanupStack::PopAndDestroy(encryptedDataReader);
198 CleanupStack::PopAndDestroy(srcData);
199 if((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
201 // Iv is left on the cleanupstack at creation.
202 // If it becomes possible for operationMode to be modified during
203 // the test this needs to be re-engineered.
204 CleanupStack::PopAndDestroy(iv);
206 CleanupStack::PopAndDestroy(impl);
207 CleanupStack::PopAndDestroy(key);
209 // compare the src with the file thats been
210 // encrypted then decrypted
211 if(!TFileCompare::CompareL(srcPath,TPtrC(KDecryptedFilePath)))
213 INFO_PRINTF1(_L("PASS : Source File and Decrypted Data Match"));
214 SetTestStepResult(EPass);
218 INFO_PRINTF1(_L("FAIL : Source File and Decrypted Data Mismatch"));
219 SetTestStepResult(EFail);
224 rFs.Delete( KDecryptedFilePath );
225 rFs.Delete( KEncryptedFilePath );
228 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
230 return TestStepResult();
234 TVerdict CSymmetricCipherIncrementalEncryptDecryptStep::doTestStepPostambleL()
236 return TestStepResult();