os/security/crypto/weakcryptospi/test/tcryptospi/src/symmetriccipherencrypteddatacheckstep.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 "symmetriccipherencrypteddatacheckstep.h"
26 using namespace CryptoSpi;
29 CSymmetricCipherEncryptedDataCheckStep::CSymmetricCipherEncryptedDataCheckStep()
34 CSymmetricCipherEncryptedDataCheckStep::~CSymmetricCipherEncryptedDataCheckStep()
40 TVerdict CSymmetricCipherEncryptedDataCheckStep::doTestStepPreambleL()
42 SetTestStepResult(EPass);
43 return TestStepResult();
47 TVerdict CSymmetricCipherEncryptedDataCheckStep::doTestStepL()
49 INFO_PRINTF1(_L("*** Symmetric Cipher - Encrypted Data Check ***"));
50 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
52 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, ETrue, operationMode, impl, key);
65 INFO_PRINTF1(_L("Plugin loaded."));
67 CleanupStack::PushL(key);
68 CleanupStack::PushL(impl);
71 HBufC8* srcData = ReadInPlaintextL();
72 CleanupStack::PushL(srcData);
74 //Create buffer for encrypted data
75 TInt maxOutputLength = impl->MaxFinalOutputLength(srcData->Length());
76 HBufC8* encrypted = HBufC8::NewLC(maxOutputLength);
77 TPtr8 encryptedPtr = encrypted->Des();
81 if (((TUid)operationMode == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
85 if (TUid(operationMode) == KOperationModeCTRUid)
87 blockSize = CtrModeCalcBlockSizeL(*impl);
91 blockSize = impl->BlockSize();
94 // blocksize is in bits so to allocate the correct number of bytes devide by 8
95 HBufC8* iv = HBufC8::NewLC(blockSize/8);
97 // blocksize is in bits so to allocate the correct number of 8 byte chunks divide by 64
98 for(TInt i = 0 ; i < blockSize/64 ; i++)
100 iv->Des().Append(_L8("12345678"));
103 TRAP_LOG(err,impl->SetIvL(iv->Des()));
105 CleanupStack::PopAndDestroy(iv);
108 INFO_PRINTF1(_L("Encrypting Source Data..."));
110 //Perform the encryption operation
111 TRAP_LOG(err, impl->ProcessFinalL((*srcData), encryptedPtr));
115 //Check that expected data equals the encrypted data
116 HBufC8* encryptedFileData = ReadInHexCiphertextL();
117 CleanupStack::PushL(encryptedFileData);
120 if( !encryptedPtr.Compare(TPtrC8(*encryptedFileData)))
122 INFO_PRINTF1(_L("PASS : Encrypted Cipher Text Matches Expected Data"));
123 SetTestStepResult(EPass);
127 INFO_PRINTF1(_L("FAIL : Encrypted Cipher Text and Expected Data Mismatch"));
128 SetTestStepResult(EFail);
131 CleanupStack::PopAndDestroy(encryptedFileData);
134 CleanupStack::PopAndDestroy(encrypted);
135 CleanupStack::PopAndDestroy(srcData);
136 CleanupStack::PopAndDestroy(impl);
137 CleanupStack::PopAndDestroy(key);
140 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
142 return TestStepResult();
147 TVerdict CSymmetricCipherEncryptedDataCheckStep::doTestStepPostambleL()
149 return TestStepResult();