os/security/crypto/weakcryptospi/test/tsymmetric/tactionvector.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcryptospi/test/tsymmetric/tactionvector.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,273 @@
     1.4 +/*
     1.5 +* Copyright (c) 1998-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 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include "tactionvector.h"
    1.23 +#include "symmetric.h"
    1.24 +
    1.25 +CTestAction* CActionVector::NewL(RFs& aFs,
    1.26 +									   CConsoleBase& aConsole,
    1.27 +									   Output& aOut, 
    1.28 +									   const TTestActionSpec& aTestActionSpec)
    1.29 +{
    1.30 +	CTestAction* self = CActionVector::NewLC(aFs, aConsole,
    1.31 +		aOut, aTestActionSpec);
    1.32 +	CleanupStack::Pop();
    1.33 +	return self;
    1.34 +}
    1.35 +
    1.36 +CTestAction* CActionVector::NewLC(RFs& aFs,
    1.37 +										CConsoleBase& aConsole,
    1.38 +										Output& aOut, 
    1.39 +										const TTestActionSpec& aTestActionSpec)
    1.40 +{
    1.41 +	CActionVector* self = new(ELeave) CActionVector(aFs, aConsole, aOut);
    1.42 +	CleanupStack::PushL(self);
    1.43 +	self->ConstructL(aTestActionSpec);
    1.44 +	return self;
    1.45 +	}
    1.46 +
    1.47 +CActionVector::~CActionVector()
    1.48 +{
    1.49 +	delete iEncryptor;
    1.50 +	delete iDecryptor;
    1.51 +}
    1.52 +
    1.53 +CActionVector::CActionVector(RFs& aFs, 
    1.54 +								 CConsoleBase& aConsole,
    1.55 +								 Output& aOut)
    1.56 +								 
    1.57 +:	CCryptoTestAction(aFs, aConsole, aOut)
    1.58 +{}
    1.59 +
    1.60 +
    1.61 +void CActionVector::DoPerformPrerequisiteL()
    1.62 +{
    1.63 +	TInt err = KErrNone;
    1.64 +	TInt pos = 0;
    1.65 +	TPtrC8 vector = Input::ParseElement(*iBody, KVectorStart, KVectorEnd, pos, err);
    1.66 +
    1.67 +	DoInputParseL(vector);
    1.68 +
    1.69 +	CBlockTransformation* encryptor = NULL;
    1.70 +	CBlockTransformation* decryptor = NULL;
    1.71 +	iEncryptor = 0;
    1.72 +	iDecryptor = 0;
    1.73 +
    1.74 +	switch (iCipherType)
    1.75 +	{
    1.76 +		case (EDESECB):
    1.77 +		{
    1.78 +			encryptor = CDESEncryptor::NewLC(iKey->Des());
    1.79 +			decryptor = CDESDecryptor::NewL(iKey->Des());
    1.80 +			CleanupStack::Pop(encryptor);
    1.81 +		}
    1.82 +		break;
    1.83 +		case(EDESCBC):
    1.84 +		{
    1.85 +			CBlockTransformation* desEncryptor = NULL;
    1.86 +			CBlockTransformation* desDecryptor = NULL;
    1.87 +
    1.88 +			desEncryptor = CDESEncryptor::NewLC(iKey->Des());
    1.89 +			desDecryptor = CDESDecryptor::NewLC(iKey->Des());
    1.90 +			
    1.91 +			encryptor = CModeCBCEncryptor::NewL(desEncryptor, iIV->Des());
    1.92 +			CleanupStack::PushL(encryptor);
    1.93 +			decryptor = CModeCBCDecryptor::NewL(desDecryptor, iIV->Des());		
    1.94 +
    1.95 +			CleanupStack::Pop(3, desEncryptor);
    1.96 +		}
    1.97 +		break;
    1.98 +		case (E3DESECB):
    1.99 +		{
   1.100 +			encryptor = C3DESEncryptor::NewLC(iKey->Des());
   1.101 +			decryptor = C3DESDecryptor::NewL(iKey->Des());
   1.102 +			CleanupStack::Pop(encryptor);
   1.103 +		}
   1.104 +		break;
   1.105 +		case (E3DESCBC):
   1.106 +		{
   1.107 +			CBlockTransformation* the3DESencryptor = NULL;
   1.108 +			CBlockTransformation* the3DESdecryptor = NULL;
   1.109 +
   1.110 +			the3DESencryptor = C3DESEncryptor::NewLC(iKey->Des());
   1.111 +			the3DESdecryptor = C3DESDecryptor::NewLC(iKey->Des());
   1.112 +			
   1.113 +			encryptor = CModeCBCEncryptor::NewL(the3DESencryptor, iIV->Des());
   1.114 +			CleanupStack::PushL(encryptor);
   1.115 +			decryptor = CModeCBCDecryptor::NewL(the3DESdecryptor, iIV->Des());		
   1.116 +
   1.117 +			CleanupStack::Pop(3, the3DESencryptor);
   1.118 +		}
   1.119 +		break;
   1.120 +		case (EAESECB):
   1.121 +		{
   1.122 +			encryptor = CAESEncryptor::NewLC(iKey->Des());
   1.123 +			decryptor = CAESDecryptor::NewL(iKey->Des());
   1.124 +			
   1.125 +			CleanupStack::Pop(encryptor);
   1.126 +		}
   1.127 +		break;
   1.128 +		case (ERC2ECB):
   1.129 +		{
   1.130 +			encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
   1.131 +			decryptor = CRC2Decryptor::NewL(iKey->Des(), iEffectiveKeyLen);
   1.132 +			CleanupStack::Pop(encryptor);
   1.133 +		}
   1.134 +		break;
   1.135 +		case (ERC2CBC):
   1.136 +		{
   1.137 +			CBlockTransformation* theRC2encryptor = NULL;
   1.138 +			CBlockTransformation* theRC2decryptor = NULL;
   1.139 +
   1.140 +			theRC2encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
   1.141 +			theRC2decryptor = CRC2Decryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
   1.142 +			
   1.143 +			encryptor = CModeCBCEncryptor::NewL(theRC2encryptor, iIV->Des());
   1.144 +			CleanupStack::PushL(encryptor);
   1.145 +			decryptor = CModeCBCDecryptor::NewL(theRC2decryptor, iIV->Des());		
   1.146 +
   1.147 +			CleanupStack::Pop(3, theRC2encryptor);
   1.148 +		}
   1.149 +		break;
   1.150 +		case (ERC4):
   1.151 +		{
   1.152 +			iEncryptor = CARC4::NewL(*iKey,0);
   1.153 +			iDecryptor = CARC4::NewL(*iKey,0);
   1.154 +		}
   1.155 +		break;
   1.156 +		case (ECipherNull):
   1.157 +		{
   1.158 +			iEncryptor = CNullCipher::NewL();
   1.159 +			iDecryptor = CNullCipher::NewL();
   1.160 +		}
   1.161 +		break;
   1.162 +			
   1.163 +		default:
   1.164 +		{
   1.165 +			ASSERT(0);
   1.166 +			User::Leave(KErrNotSupported);
   1.167 +		}
   1.168 +	}
   1.169 +	
   1.170 +	CleanupStack::PushL(encryptor);
   1.171 +	CleanupStack::PushL(decryptor);
   1.172 +
   1.173 +	if(!iEncryptor && !iDecryptor)
   1.174 +		{
   1.175 +		CPaddingSSLv3* dPadding = CPaddingSSLv3::NewLC(decryptor->BlockSize());
   1.176 +		CPaddingSSLv3* ePadding = CPaddingSSLv3::NewLC(encryptor->BlockSize());
   1.177 +		iEncryptor = CBufferedEncryptor::NewL(encryptor, ePadding);	
   1.178 +		CleanupStack::Pop(ePadding);
   1.179 +		iDecryptor = CBufferedDecryptor::NewL(decryptor, dPadding);
   1.180 +		CleanupStack::Pop(dPadding);
   1.181 +		}
   1.182 +
   1.183 +	iEResult = HBufC8::NewMaxL(iEncryptor->MaxOutputLength(iInput->Length()));
   1.184 +	iDResult = HBufC8::NewMaxL(iDecryptor->MaxOutputLength(iEResult->Size()));
   1.185 +
   1.186 +	CleanupStack::Pop(2, encryptor);
   1.187 +}
   1.188 +
   1.189 +void CActionVector::DoPerformActionL()
   1.190 +{
   1.191 +//	First perform the test blockwise (ie passing in one block at a time)	
   1.192 +	TUint blockSize = iEncryptor->BlockSize();
   1.193 +	
   1.194 +	if((iInput->Size() % blockSize) != 0)
   1.195 +		{
   1.196 +		// input was not a multiple of the blocksize
   1.197 +		iResult = EFalse;
   1.198 +		return;
   1.199 +		}
   1.200 +
   1.201 +	TUint index = 0;
   1.202 +	while (index <= (iInput->Size() - blockSize)) 
   1.203 +	{
   1.204 +		TPtr8 theEncryptResult((TUint8*)&(iEResult->operator[](index)), blockSize, blockSize);
   1.205 +		theEncryptResult.FillZ(theEncryptResult.MaxLength());
   1.206 +		theEncryptResult.SetLength(0);
   1.207 +
   1.208 +		TPtrC8 theEncryptInput(iInput->Mid(index, blockSize));	
   1.209 +		iEncryptor->Process(theEncryptInput, theEncryptResult);
   1.210 +
   1.211 +		if (iOutput->Mid(index, blockSize) == theEncryptResult)
   1.212 +			iResult = ETrue;
   1.213 +		else
   1.214 +			break;	//	No point doing any more
   1.215 +	
   1.216 +		index+=blockSize;
   1.217 +	}
   1.218 +	
   1.219 +	
   1.220 +	if (*iOutput==*iEResult)
   1.221 +	{
   1.222 +		iResult = ETrue;
   1.223 +
   1.224 +		index = 0;
   1.225 +		while (index <= (iEResult->Size()- blockSize))
   1.226 +		{
   1.227 +			TPtr8 theDecryptResult((TUint8*)&(iDResult->operator[](index)), blockSize, blockSize);
   1.228 +			theDecryptResult.FillZ(theDecryptResult.MaxLength());
   1.229 +			theDecryptResult.SetLength(0);
   1.230 +
   1.231 +			TPtrC8 theDecryptInput(iEResult->Mid(index, blockSize));
   1.232 +			iDecryptor->Process(theDecryptInput, theDecryptResult);
   1.233 +			
   1.234 +			if(iInput->Mid(index, blockSize) != theDecryptResult)
   1.235 +			{
   1.236 +				iResult = EFalse;
   1.237 +				break;	//	No point doing any more
   1.238 +			}
   1.239 +		
   1.240 +			index+=blockSize;
   1.241 +		}
   1.242 +	
   1.243 +		if (*iInput!=*iDResult)
   1.244 +		iResult = EFalse;	
   1.245 +	}
   1.246 +	
   1.247 +	
   1.248 +	iEncryptor->Reset();
   1.249 +	iDecryptor->Reset();
   1.250 +//	Now, if input is longer than a single block, repeat passing all the data
   1.251 +	if ((TUint)iInput->Size() > blockSize)
   1.252 +	{
   1.253 +		TPtr8 theEncryptResult(iEResult->Des());
   1.254 +		theEncryptResult.FillZ(theEncryptResult.MaxLength());
   1.255 +		theEncryptResult.SetLength(0);
   1.256 +
   1.257 +		TPtrC8 theInput(*iInput);
   1.258 +		iEncryptor->Process(theInput, theEncryptResult);
   1.259 +
   1.260 +		if (*iOutput!=*iEResult)
   1.261 +		{
   1.262 +			iResult = EFalse;
   1.263 +		}
   1.264 +		else
   1.265 +		{
   1.266 +			TPtr8 theDecryptResult(iDResult->Des());
   1.267 +			theDecryptResult.FillZ(theDecryptResult.MaxLength());
   1.268 +			theDecryptResult.SetLength(0);
   1.269 +
   1.270 +			iDecryptor->Process(*iEResult, theDecryptResult);
   1.271 +			if (*iInput!=*iDResult)
   1.272 +				iResult = EFalse;
   1.273 +		}
   1.274 +	}
   1.275 +}
   1.276 +