os/security/crypto/weakcryptospi/test/tcryptospi/src/symmetriccipherencrypteddatacheckstep.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tcryptospi/src/symmetriccipherencrypteddatacheckstep.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,150 @@
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 "symmetriccipherencrypteddatacheckstep.h"
1.28 +
1.29 +using namespace CryptoSpi;
1.30 +
1.31 +
1.32 +CSymmetricCipherEncryptedDataCheckStep::CSymmetricCipherEncryptedDataCheckStep()
1.33 + {
1.34 + }
1.35 +
1.36 +
1.37 +CSymmetricCipherEncryptedDataCheckStep::~CSymmetricCipherEncryptedDataCheckStep()
1.38 +
1.39 + {
1.40 + }
1.41 +
1.42 +
1.43 +TVerdict CSymmetricCipherEncryptedDataCheckStep::doTestStepPreambleL()
1.44 + {
1.45 + SetTestStepResult(EPass);
1.46 + return TestStepResult();
1.47 + }
1.48 +
1.49 +
1.50 +TVerdict CSymmetricCipherEncryptedDataCheckStep::doTestStepL()
1.51 + {
1.52 + INFO_PRINTF1(_L("*** Symmetric Cipher - Encrypted Data Check ***"));
1.53 + INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
1.54 +
1.55 + if (TestStepResult()==EPass)
1.56 + {
1.57 + //Assume faliure, unless all is successful
1.58 + SetTestStepResult(EFail);
1.59 +
1.60 + TVariantPtrC operationMode;
1.61 +
1.62 + // Create a Symmetric Cipher with the values from the ini file
1.63 + CryptoSpi::CSymmetricCipher * impl = NULL;
1.64 +
1.65 + CKey* key = NULL;
1.66 + SetupCipherL(ETrue, ETrue, operationMode, impl, key);
1.67 +
1.68 + INFO_PRINTF1(_L("Plugin loaded."));
1.69 +
1.70 + CleanupStack::PushL(key);
1.71 + CleanupStack::PushL(impl);
1.72 +
1.73 + //read from src file
1.74 + HBufC8* srcData = ReadInPlaintextL();
1.75 + CleanupStack::PushL(srcData);
1.76 +
1.77 + //Create buffer for encrypted data
1.78 + TInt maxOutputLength = impl->MaxFinalOutputLength(srcData->Length());
1.79 + HBufC8* encrypted = HBufC8::NewLC(maxOutputLength);
1.80 + TPtr8 encryptedPtr = encrypted->Des();
1.81 +
1.82 + TInt err;
1.83 +
1.84 + if (((TUid)operationMode == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
1.85 + {
1.86 + TInt blockSize(0);
1.87 +
1.88 + if (TUid(operationMode) == KOperationModeCTRUid)
1.89 + {
1.90 + blockSize = CtrModeCalcBlockSizeL(*impl);
1.91 + }
1.92 + else
1.93 + {
1.94 + blockSize = impl->BlockSize();
1.95 + }
1.96 +
1.97 + // blocksize is in bits so to allocate the correct number of bytes devide by 8
1.98 + HBufC8* iv = HBufC8::NewLC(blockSize/8);
1.99 +
1.100 + // blocksize is in bits so to allocate the correct number of 8 byte chunks divide by 64
1.101 + for(TInt i = 0 ; i < blockSize/64 ; i++)
1.102 + {
1.103 + iv->Des().Append(_L8("12345678"));
1.104 + }
1.105 +
1.106 + TRAP_LOG(err,impl->SetIvL(iv->Des()));
1.107 +
1.108 + CleanupStack::PopAndDestroy(iv);
1.109 + }
1.110 +
1.111 + INFO_PRINTF1(_L("Encrypting Source Data..."));
1.112 +
1.113 + //Perform the encryption operation
1.114 + TRAP_LOG(err, impl->ProcessFinalL((*srcData), encryptedPtr));
1.115 +
1.116 + if(err == KErrNone)
1.117 + {
1.118 + //Check that expected data equals the encrypted data
1.119 + HBufC8* encryptedFileData = ReadInHexCiphertextL();
1.120 + CleanupStack::PushL(encryptedFileData);
1.121 +
1.122 +
1.123 + if( !encryptedPtr.Compare(TPtrC8(*encryptedFileData)))
1.124 + {
1.125 + INFO_PRINTF1(_L("PASS : Encrypted Cipher Text Matches Expected Data"));
1.126 + SetTestStepResult(EPass);
1.127 + }
1.128 + else
1.129 + {
1.130 + INFO_PRINTF1(_L("FAIL : Encrypted Cipher Text and Expected Data Mismatch"));
1.131 + SetTestStepResult(EFail);
1.132 + }
1.133 +
1.134 + CleanupStack::PopAndDestroy(encryptedFileData);
1.135 + }
1.136 +
1.137 + CleanupStack::PopAndDestroy(encrypted);
1.138 + CleanupStack::PopAndDestroy(srcData);
1.139 + CleanupStack::PopAndDestroy(impl);
1.140 + CleanupStack::PopAndDestroy(key);
1.141 + }
1.142 +
1.143 + INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
1.144 +
1.145 + return TestStepResult();
1.146 +
1.147 + }
1.148 +
1.149 +
1.150 +TVerdict CSymmetricCipherEncryptedDataCheckStep::doTestStepPostambleL()
1.151 + {
1.152 + return TestStepResult();
1.153 + }