os/security/crypto/weakcryptospi/test/tcryptospi/src/symmetriccipherencryptdecryptstep.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tcryptospi/src/symmetriccipherencryptdecryptstep.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,184 @@
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 "symmetriccipherencryptdecryptstep.h"
1.28 +
1.29 +using namespace CryptoSpi;
1.30 +
1.31 +CSymmetricCipherEncryptDecryptStep::~CSymmetricCipherEncryptDecryptStep()
1.32 + {
1.33 + }
1.34 +
1.35 +
1.36 +CSymmetricCipherEncryptDecryptStep::CSymmetricCipherEncryptDecryptStep()
1.37 + {
1.38 + SetTestStepName(KSymmetricCipherEncryptDecryptStep);
1.39 + }
1.40 +
1.41 +
1.42 +TVerdict CSymmetricCipherEncryptDecryptStep::doTestStepPreambleL()
1.43 + {
1.44 + SetTestStepResult(EPass);
1.45 + return TestStepResult();
1.46 + }
1.47 +
1.48 +
1.49 +TVerdict CSymmetricCipherEncryptDecryptStep::doTestStepL()
1.50 + {
1.51 + INFO_PRINTF1(_L("*** Symmetric Cipher - Encrypt/Decrypt ***"));
1.52 + INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
1.53 +
1.54 + if (TestStepResult()==EPass)
1.55 + {
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, EFalse, 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 +
1.74 + HBufC8* iv = NULL;
1.75 + TInt err(0);
1.76 +
1.77 + if((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
1.78 + {
1.79 + TInt blockSize(0);
1.80 +
1.81 + if (TUid(operationMode) == KOperationModeCTRUid)
1.82 + {
1.83 + blockSize = CtrModeCalcBlockSizeL(*impl);
1.84 + }
1.85 + else
1.86 + {
1.87 + blockSize = impl->BlockSize();
1.88 + }
1.89 +
1.90 + // (blocksize is in bits so to allocate the correct number of bytes divide by 8)
1.91 + // 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.
1.92 + // If this conditional block changes, take care to update the condition for deleting this allocated IV, near the end of this function.
1.93 + iv = HBufC8::NewLC(blockSize/8);
1.94 +
1.95 + // blocksize is in bits so to allocate the correct number of 8 byte chunks divide by 64
1.96 + for(TInt i = 0 ; i <blockSize/64 ; i++)
1.97 + {
1.98 + iv->Des().Append(_L8("12345678"));
1.99 + }
1.100 +
1.101 + TRAP_LOG(err,impl->SetIvL(iv->Des()));
1.102 + }
1.103 +
1.104 + //read from src file
1.105 + HBufC8* srcData = ReadInPlaintextL();
1.106 + CleanupStack::PushL(srcData);
1.107 +
1.108 + //Create buffer for encrypted data
1.109 + TInt maxOutputLength = impl->MaxFinalOutputLength(srcData->Length());
1.110 + HBufC8* encrypted = HBufC8::NewLC(maxOutputLength);
1.111 + TPtr8 encryptedPtr = encrypted->Des();
1.112 +
1.113 + INFO_PRINTF1(_L("Encrypting Source Data..."));
1.114 +
1.115 + //Perform the encryption operation
1.116 + TRAP_LOG(err, impl->ProcessFinalL((*srcData), encryptedPtr));
1.117 +
1.118 + if(encryptedPtr.Compare((*srcData)) != 0)
1.119 + {
1.120 + //Switch to decrypt
1.121 + TRAP_LOG(err,impl->SetCryptoModeL(KCryptoModeDecryptUid));
1.122 +
1.123 + //If in CTR mode need to reset the keystream to the start of the sequence used for encryption
1.124 + if(TUid(operationMode) == KOperationModeCTRUid)
1.125 + {
1.126 + impl->SetIvL(iv->Des());
1.127 + }
1.128 +
1.129 + //Create a buffer for the decrypted data
1.130 + maxOutputLength = encryptedPtr.Length();
1.131 +
1.132 + TInt bufSize = impl->MaxFinalOutputLength(maxOutputLength);
1.133 +
1.134 + HBufC8* output = HBufC8::NewLC(bufSize);
1.135 + TPtr8 outputPtr = output->Des();
1.136 +
1.137 + INFO_PRINTF1(_L("Decrypting Data..."));
1.138 +
1.139 + //Perform the decryption operation
1.140 + TRAP_LOG(err, impl->ProcessFinalL(encryptedPtr, outputPtr));
1.141 +
1.142 + if(err == KErrNone)
1.143 + {
1.144 + //Check that the source data matches the data thats
1.145 + //been encrypted then decrypted
1.146 + if( !outputPtr.Compare(*srcData) )
1.147 + {
1.148 + INFO_PRINTF1(_L("PASS : Decrypted Data and Source Match"));
1.149 + SetTestStepResult(EPass);
1.150 + }
1.151 + else
1.152 + {
1.153 + INFO_PRINTF1(_L("FAIL : Decrypted Data and Source Mismatch"));
1.154 + SetTestStepResult(EFail);
1.155 + }
1.156 + }
1.157 +
1.158 + CleanupStack::PopAndDestroy(output);
1.159 + }
1.160 + else
1.161 + {
1.162 + INFO_PRINTF1(_L("FAIL : Encrpyted Data and Source Data length and content is the same"));
1.163 + SetTestStepResult(EFail);
1.164 + }
1.165 +
1.166 + CleanupStack::PopAndDestroy(encrypted);
1.167 + CleanupStack::PopAndDestroy(srcData);
1.168 + if((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
1.169 + {
1.170 + // Iv is left on the cleanupstack at creation.
1.171 + // If it becomes possible for operationMode to be modified during
1.172 + // the test this needs to be re-engineered.
1.173 + CleanupStack::PopAndDestroy(iv);
1.174 + }
1.175 + CleanupStack::PopAndDestroy(2, key);
1.176 + }
1.177 + INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
1.178 +
1.179 + return TestStepResult();
1.180 +
1.181 + }
1.182 +
1.183 +
1.184 +TVerdict CSymmetricCipherEncryptDecryptStep::doTestStepPostambleL()
1.185 + {
1.186 + return TestStepResult();
1.187 + }