os/security/crypto/weakcryptospi/test/tcryptospi/src/symmetriccipherobjectreusestep.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 "symmetriccipherobjectreusestep.h"
26 #include "filewriter.h"
27 #include "filecompare.h"
29 using namespace CryptoSpi;
32 CSymmetricCipherObjectReuseStep::CSymmetricCipherObjectReuseStep(TInt aOffset) : iOffset(aOffset)
37 CSymmetricCipherObjectReuseStep::~CSymmetricCipherObjectReuseStep()
42 TVerdict CSymmetricCipherObjectReuseStep::doTestStepPreambleL()
44 SetTestStepResult(EPass);
45 return TestStepResult();
49 TVerdict CSymmetricCipherObjectReuseStep::doTestStepL()
51 INFO_PRINTF1(_L("*** Symmetric Cipher - Object Reuse ***"));
52 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
53 if (TestStepResult()==EPass)
56 //Assume failure, unless all is successful
57 SetTestStepResult(EFail);
61 TVariantPtrC algorithm;
62 TVariantPtrC operationMode;
63 TVariantPtrC paddingMode;
65 if( !GetStringFromConfig(ConfigSection(),KConfigEncryptKey, keyPath) ||
66 !GetStringFromConfig(ConfigSection(),KConfigSourcePath, srcPath) ||
67 !GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid, algorithm) ||
68 !GetStringFromConfig(ConfigSection(),KConfigOperationMode, operationMode) ||
69 !GetStringFromConfig(ConfigSection(),KConfigPaddingMode, paddingMode ))
71 User::Leave(KErrNotFound);
76 //Create an instance of TKeyProperty
77 TKeyProperty keyProperty;
79 //Load the key data using the
80 CFileReader* keyData = CFileReader::NewLC(keyPath);
82 CCryptoParams* params = CCryptoParams::NewLC();
83 params->AddL( *keyData, KSymmetricKeyParameterUid);
85 CKey* key=CKey::NewL(keyProperty, *params);
86 CleanupStack::PushL(key);
88 CCryptoParams* xparams = NULL;
90 if (TUid(algorithm) == KArc4Uid)
92 //Set the RC4 DiscardBytes to 0
93 xparams = CCryptoParams::NewL();
94 xparams->AddL(NULL, KARC4DiscardBytes);
95 CleanupStack::PushL(xparams);
98 if (TUid(algorithm) == KRc2Uid)
100 TInt keylen = TPtrC8(*keyData).Length() * 8;
101 xparams = CCryptoParams::NewLC();
103 //Set the RC2 EffectiveKeyLen according to the input key size
104 xparams->AddL( keylen, KRC2EffectiveKeyLenBits);
107 INFO_PRINTF1(_L("Creating Symmetric Cipher Object..."));
109 // Create a Symmetric Cipher with the values from the ini config file
110 CryptoSpi::CSymmetricCipher * impl = NULL;
111 TRAPD(err,CSymmetricCipherFactory::CreateSymmetricCipherL
116 KCryptoModeEncryptUid,
121 if(impl && (err == KErrNone))
123 CleanupStack::PushL(impl);
125 const TInt KObjectReuseItterations = 5; // 5 iterations should be enough to check the object reuse feature
126 // the no of iteration is reduced, to reduce the time taken for execution
128 //Boolean to denote the state
129 TBool testPass = ETrue;
131 /*************** Encrypt/Decrypt Reuse Loop ****************/
132 for(TInt index = 0; index < KObjectReuseItterations; index++)
134 INFO_PRINTF3(_L("i=%d : START HEAP CELLS: %d"),index, User::CountAllocCells());
136 //-----RESET IMPLEMENTATION OBJECT (NORMAL LOGGING)----------
140 TRAP(err,impl->SetKeyL(*key));
144 ERR_PRINTF3(_L("*** ERROR:%d - SetKeyL() i=%d ***"),err,index);
148 TRAP(err,impl->SetCryptoModeL(KCryptoModeEncryptUid));
152 ERR_PRINTF3(_L("*** ERROR:%d - SetCryptoModeL() i=%d ***"),err,index);
156 if(TUid(algorithm) != KArc4Uid)
159 impl->SetOperationModeL(operationMode);
163 ERR_PRINTF3(_L("*** ERROR:%d - SetOperationModeL() i=%d ***"),err,index);
167 TRAP(err,impl->SetPaddingModeL(paddingMode));
171 ERR_PRINTF3(_L("*** ERROR:%d - SetPaddingModeL() i=%d ***"),err,index);
177 //------------------------------------------------------
179 //find out the block size for this algorithm
182 if (TUid(operationMode) == KOperationModeCTRUid)
184 blockSize = CtrModeCalcBlockSizeL(*impl);
188 blockSize = impl->BlockSize();
193 if ((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
195 // block size is in bits so to allocate the correct number of bytes divide by 8
196 // 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.
197 // If this conditional block changes, take care to update the condition for deleting this allocated IV, near the end of this function.
198 iv = HBufC8::NewLC(blockSize/8);
200 // blocksize is in bits so to allocate the correct number of 8 byte chunks divide by 64
201 for(TInt i = 0 ; i <blockSize/64 ; i++)
203 iv->Des().Append(_L8("12345678"));
206 TRAP_LOG(err,impl->SetIvL(iv->Des()));
209 // convert to bytesize
211 blockSize += iOffset;
214 CFileReader* srcData = CFileReader::NewLC(srcPath,blockSize);
216 // first step is to read from the src file one block
217 // at a time, encrypt that block and then write
218 // the encrypted block out to a temporary file.
219 CFileWriter* encryptedDataWriter = CFileWriter::NewLC(TPtrC(KTempEncryptedFilePath));
221 TInt numBlocks = srcData->NumBlocks();
223 INFO_PRINTF1(_L("Starting Incremental Encryption..."));
225 for(TInt i = 1 ; i <= numBlocks ; i++)
227 TRAP_LOG(err,srcData->ReadBlockL());
229 //Create buffer for encrypted data
230 TInt maxOutputLength = impl->MaxFinalOutputLength(TPtrC8(*srcData).Length());
231 HBufC8* encrypted = HBufC8::NewLC(maxOutputLength);
232 TPtr8 encryptedPtr = encrypted->Des();
236 TRAP(err,impl->ProcessFinalL(*srcData, encryptedPtr));
240 ERR_PRINTF3(_L("*** ERROR:%d - ProcessFinalL() Block=%d ***"),err,i);
246 TRAP(err,impl->ProcessL(*srcData, encryptedPtr));
250 ERR_PRINTF3(_L("*** ERROR:%d - ProcessL() Block=%d ***"),err,i);
255 TRAP_LOG(err,encryptedDataWriter->WriteBlockL(encryptedPtr));
257 CleanupStack::PopAndDestroy(encrypted);
260 CleanupStack::PopAndDestroy(encryptedDataWriter);
262 //Switch to Decrypt Crypto Mode
263 TRAP(err,impl->SetCryptoModeL(KCryptoModeDecryptUid));
267 ERR_PRINTF3(_L("*** ERROR:%d - SetCryptoModeL() i=%d ***"),err,index);
271 //If in CTR mode need to reset the keystream to the start of the sequence used for encryption.
272 if(TUid(operationMode) == KOperationModeCTRUid)
274 impl->SetIvL(iv->Des());
278 // the next step is to read the previously encrypted data
279 // from the temporary file decrypting this one block
280 // at a time and outputing this to a temporary file.
281 CFileReader* encryptedDataReader = CFileReader::NewLC(TPtrC(KTempEncryptedFilePath),blockSize);
282 CFileWriter* decryptedDataWriter = CFileWriter::NewLC(TPtrC(KTempDecryptedFilePath));
284 numBlocks = encryptedDataReader->NumBlocks();
286 INFO_PRINTF1(_L("Starting Incremental Decryption..."));
288 for(TInt i = 1 ; i <= numBlocks ; i++)
290 encryptedDataReader->ReadBlockL();
291 //Create buffer for encrypted data
292 TInt maxOutputLength = impl->MaxFinalOutputLength(TPtrC8(*encryptedDataReader).Length());
293 HBufC8* decrypted = HBufC8::NewLC(maxOutputLength);
294 TPtr8 decryptedPtr = decrypted->Des();
296 //Perform the decryption operation
299 TRAP(err,impl->ProcessFinalL(*encryptedDataReader, decryptedPtr));
303 ERR_PRINTF3(_L("*** ERROR:%d - ProcessFinalL() Block=%d ***"),err,i);
309 TRAP(err,impl->ProcessL(*encryptedDataReader, decryptedPtr));
313 ERR_PRINTF3(_L("*** ERROR:%d - ProcessL() Block=%d ***"),err,i);
318 TRAP_LOG(err,decryptedDataWriter->WriteBlockL(decryptedPtr));
320 CleanupStack::PopAndDestroy(decrypted);
323 CleanupStack::PopAndDestroy(decryptedDataWriter);
324 CleanupStack::PopAndDestroy(encryptedDataReader);
325 CleanupStack::PopAndDestroy(srcData);
327 if((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
329 // Iv is left on the cleanupstack at creation.
330 // If it becomes possible for operationMode to be modified during
331 // the test this needs to be re-engineered.
332 CleanupStack::PopAndDestroy(iv);
336 // compare the src with the file thats been
337 // encrypted then decrypted
338 // Note: Returning 0 means that the files match
339 if(!TFileCompare::CompareL(srcPath,TPtrC(KTempDecryptedFilePath)))
341 INFO_PRINTF2(_L("*** PASS = Source File and Decrypted Data Match - i=%d ***"),index);
346 ERR_PRINTF2(_L("*** ERROR: Source File and Decrypted Data Mismatch - i=%d ***"),index);
351 rFs.Delete( KTempDecryptedFilePath );
352 rFs.Delete( KTempEncryptedFilePath );
355 INFO_PRINTF3(_L("*** i=%d : END HEAP CELLS: %d ***"),index, User::CountAllocCells());
358 /*************** END OF LOOP ****************/
360 CleanupStack::PopAndDestroy(impl);
362 if(testPass == EFalse)
364 ERR_PRINTF1(_L("*** TEST FAIL : Symmetric Cipher - Object Reuse ***"));
368 INFO_PRINTF1(_L("*** TEST PASS : Symmetric Cipher - Object Reuse ***"));
369 SetTestStepResult(EPass);
375 ERR_PRINTF2(_L("*** FAIL: Failed to Create Symmetric Object - %d ***"), err);
379 if (TUid(algorithm) == KArc4Uid || TUid(algorithm) == KRc2Uid)
381 CleanupStack::PopAndDestroy(xparams);
384 CleanupStack::PopAndDestroy(key);
385 CleanupStack::PopAndDestroy(params);
386 CleanupStack::PopAndDestroy(keyData);
389 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
391 return TestStepResult();
396 TVerdict CSymmetricCipherObjectReuseStep::doTestStepPostambleL()
398 return TestStepResult();