os/security/crypto/weakcryptospi/test/tcryptospi/src/symmetriccipherencryptdecryptstep.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 "symmetriccipherencryptdecryptstep.h"
26 using namespace CryptoSpi;
28 CSymmetricCipherEncryptDecryptStep::~CSymmetricCipherEncryptDecryptStep()
33 CSymmetricCipherEncryptDecryptStep::CSymmetricCipherEncryptDecryptStep()
35 SetTestStepName(KSymmetricCipherEncryptDecryptStep);
39 TVerdict CSymmetricCipherEncryptDecryptStep::doTestStepPreambleL()
41 SetTestStepResult(EPass);
42 return TestStepResult();
46 TVerdict CSymmetricCipherEncryptDecryptStep::doTestStepL()
48 INFO_PRINTF1(_L("*** Symmetric Cipher - Encrypt/Decrypt ***"));
49 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
51 if (TestStepResult()==EPass)
54 //Assume faliure, unless all is successful
55 SetTestStepResult(EFail);
57 TVariantPtrC operationMode;
59 // Create a Symmetric Cipher with the values from the ini file
60 CryptoSpi::CSymmetricCipher * impl = NULL;
63 SetupCipherL(ETrue, EFalse, operationMode, impl, key);
65 INFO_PRINTF1(_L("Plugin loaded."));
67 CleanupStack::PushL(key);
68 CleanupStack::PushL(impl);
74 if((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
78 if (TUid(operationMode) == KOperationModeCTRUid)
80 blockSize = CtrModeCalcBlockSizeL(*impl);
84 blockSize = impl->BlockSize();
87 // (blocksize is in bits so to allocate the correct number of bytes divide by 8)
88 // 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.
89 // If this conditional block changes, take care to update the condition for deleting this allocated IV, near the end of this function.
90 iv = HBufC8::NewLC(blockSize/8);
92 // blocksize is in bits so to allocate the correct number of 8 byte chunks divide by 64
93 for(TInt i = 0 ; i <blockSize/64 ; i++)
95 iv->Des().Append(_L8("12345678"));
98 TRAP_LOG(err,impl->SetIvL(iv->Des()));
102 HBufC8* srcData = ReadInPlaintextL();
103 CleanupStack::PushL(srcData);
105 //Create buffer for encrypted data
106 TInt maxOutputLength = impl->MaxFinalOutputLength(srcData->Length());
107 HBufC8* encrypted = HBufC8::NewLC(maxOutputLength);
108 TPtr8 encryptedPtr = encrypted->Des();
110 INFO_PRINTF1(_L("Encrypting Source Data..."));
112 //Perform the encryption operation
113 TRAP_LOG(err, impl->ProcessFinalL((*srcData), encryptedPtr));
115 if(encryptedPtr.Compare((*srcData)) != 0)
118 TRAP_LOG(err,impl->SetCryptoModeL(KCryptoModeDecryptUid));
120 //If in CTR mode need to reset the keystream to the start of the sequence used for encryption
121 if(TUid(operationMode) == KOperationModeCTRUid)
123 impl->SetIvL(iv->Des());
126 //Create a buffer for the decrypted data
127 maxOutputLength = encryptedPtr.Length();
129 TInt bufSize = impl->MaxFinalOutputLength(maxOutputLength);
131 HBufC8* output = HBufC8::NewLC(bufSize);
132 TPtr8 outputPtr = output->Des();
134 INFO_PRINTF1(_L("Decrypting Data..."));
136 //Perform the decryption operation
137 TRAP_LOG(err, impl->ProcessFinalL(encryptedPtr, outputPtr));
141 //Check that the source data matches the data thats
142 //been encrypted then decrypted
143 if( !outputPtr.Compare(*srcData) )
145 INFO_PRINTF1(_L("PASS : Decrypted Data and Source Match"));
146 SetTestStepResult(EPass);
150 INFO_PRINTF1(_L("FAIL : Decrypted Data and Source Mismatch"));
151 SetTestStepResult(EFail);
155 CleanupStack::PopAndDestroy(output);
159 INFO_PRINTF1(_L("FAIL : Encrpyted Data and Source Data length and content is the same"));
160 SetTestStepResult(EFail);
163 CleanupStack::PopAndDestroy(encrypted);
164 CleanupStack::PopAndDestroy(srcData);
165 if((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
167 // Iv is left on the cleanupstack at creation.
168 // If it becomes possible for operationMode to be modified during
169 // the test this needs to be re-engineered.
170 CleanupStack::PopAndDestroy(iv);
172 CleanupStack::PopAndDestroy(2, key);
174 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
176 return TestStepResult();
181 TVerdict CSymmetricCipherEncryptDecryptStep::doTestStepPostambleL()
183 return TestStepResult();