os/security/crypto/weakcrypto/test/tsymmetric/tactionincremental.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcrypto/test/tsymmetric/tactionincremental.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,253 @@
     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 "tactionincremental.h"
    1.23 +#include "symmetric.h"
    1.24 +#include "des.h"
    1.25 +
    1.26 +CTestAction* CActionIncremental::NewL(RFs& aFs,
    1.27 +									   CConsoleBase& aConsole,
    1.28 +									   Output& aOut, 
    1.29 +									   const TTestActionSpec& aTestActionSpec)
    1.30 +	{
    1.31 +	CTestAction* self = CActionIncremental::NewLC(aFs, aConsole,
    1.32 +		aOut, aTestActionSpec);
    1.33 +	CleanupStack::Pop();
    1.34 +	return self;
    1.35 +	}
    1.36 +
    1.37 +CTestAction* CActionIncremental::NewLC(RFs& aFs,
    1.38 +										CConsoleBase& aConsole,
    1.39 +										Output& aOut, 
    1.40 +										const TTestActionSpec& aTestActionSpec)
    1.41 +	{
    1.42 +	CActionIncremental* self = new(ELeave) CActionIncremental(aFs, aConsole, aOut);
    1.43 +	CleanupStack::PushL(self);
    1.44 +	self->ConstructL(aTestActionSpec);
    1.45 +	return self;
    1.46 +	}
    1.47 +
    1.48 +CActionIncremental::~CActionIncremental()
    1.49 +{
    1.50 +}
    1.51 +
    1.52 +CActionIncremental::CActionIncremental(RFs& aFs, 
    1.53 +								 CConsoleBase& aConsole,
    1.54 +								 Output& aOut)						 
    1.55 +: CCryptoTestAction(aFs, aConsole, aOut)
    1.56 +{}
    1.57 +
    1.58 +void CActionIncremental::DoPerformPostrequisiteL()
    1.59 +{
    1.60 +	delete iEncryptor;
    1.61 +	delete iDecryptor;
    1.62 +}
    1.63 +
    1.64 +void CActionIncremental::DoPerformPrerequisiteL()
    1.65 +{
    1.66 +	TInt err = KErrNone;
    1.67 +	TInt pos = 0;
    1.68 +	TPtrC8 incremental = Input::ParseElement(*iBody, KIncrementalStart, KIncrementalEnd, pos, err);
    1.69 +
    1.70 +	DoInputParseL(incremental);
    1.71 +
    1.72 +	CBlockTransformation* encryptor = NULL;
    1.73 +	CBlockTransformation* decryptor = NULL;
    1.74 +
    1.75 +	iEncryptor =0;
    1.76 +	iDecryptor =0;
    1.77 +
    1.78 +	switch (iCipherType)
    1.79 +	{
    1.80 +		case (EDESECB):
    1.81 +		{
    1.82 +			//If the test is weak key test
    1.83 +			if(iExpectedWeakResult == KErrWeakKey)
    1.84 +				{
    1.85 +				//we expect to leave with KErrWeakKey reason
    1.86 +				if(CDES::IsWeakKey(iKey->Des()))
    1.87 +					{
    1.88 +					User::Leave(KErrWeakKey);
    1.89 +					}
    1.90 +				else //test is unsuccessful, leave with a different reason
    1.91 +					{
    1.92 +					User::Leave(KErrGeneral);
    1.93 +					}	
    1.94 +				}
    1.95 +			encryptor = CDESEncryptor::NewLC(iKey->Des());
    1.96 +			decryptor = CDESDecryptor::NewL(iKey->Des());
    1.97 +			CleanupStack::Pop(encryptor);
    1.98 +		}
    1.99 +		break;
   1.100 +		case (EDESCBC):
   1.101 +		{
   1.102 +			CBlockTransformation* desEncryptor = NULL;
   1.103 +			CBlockTransformation* desDecryptor = NULL;
   1.104 +			
   1.105 +			if(iExpectedWeakResult == KErrWeakKey)
   1.106 +				{
   1.107 +				if(CDES::IsWeakKey(iKey->Des()))
   1.108 +					{
   1.109 +					User::Leave(KErrWeakKey);
   1.110 +					}
   1.111 +				else
   1.112 +					{
   1.113 +					User::Leave(KErrGeneral);
   1.114 +					}	
   1.115 +				}
   1.116 +			desEncryptor = CDESEncryptor::NewLC(iKey->Des());
   1.117 +			desDecryptor = CDESDecryptor::NewLC(iKey->Des());
   1.118 +			
   1.119 +			encryptor = CModeCBCEncryptor::NewL(desEncryptor, iIV->Des());
   1.120 +			CleanupStack::PushL(encryptor);
   1.121 +			decryptor = CModeCBCDecryptor::NewL(desDecryptor, iIV->Des());		
   1.122 +			CleanupStack::Pop(3);
   1.123 +		}
   1.124 +		break;
   1.125 +		case (E3DESECB):
   1.126 +		{
   1.127 +			encryptor = C3DESEncryptor::NewLC(iKey->Des());
   1.128 +			decryptor = C3DESDecryptor::NewL(iKey->Des());
   1.129 +			CleanupStack::Pop(encryptor);
   1.130 +		}
   1.131 +		break;
   1.132 +		case (E3DESCBC):
   1.133 +		{
   1.134 +			CBlockTransformation* the3DESencryptor = NULL;
   1.135 +			CBlockTransformation* the3DESdecryptor = NULL;
   1.136 +
   1.137 +			the3DESencryptor = C3DESEncryptor::NewLC(iKey->Des());
   1.138 +			the3DESdecryptor = C3DESDecryptor::NewLC(iKey->Des());
   1.139 +			
   1.140 +			encryptor = CModeCBCEncryptor::NewL(the3DESencryptor, iIV->Des());
   1.141 +			CleanupStack::PushL(encryptor);
   1.142 +			decryptor = CModeCBCDecryptor::NewL(the3DESdecryptor, iIV->Des());		
   1.143 +			CleanupStack::Pop(3);
   1.144 +		}
   1.145 +		break;
   1.146 +		case (EAESECB):
   1.147 +		{
   1.148 +			encryptor = CAESEncryptor::NewLC(iKey->Des());
   1.149 +			decryptor = CAESDecryptor::NewL(iKey->Des());
   1.150 +			CleanupStack::Pop(encryptor);
   1.151 +		}
   1.152 +		break;
   1.153 +		case (ERC2ECB):
   1.154 +		{
   1.155 +			encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
   1.156 +			decryptor = CRC2Decryptor::NewL(iKey->Des(), iEffectiveKeyLen);
   1.157 +			CleanupStack::Pop(encryptor);
   1.158 +		}
   1.159 +		break;
   1.160 +		case (ERC2CBC):
   1.161 +		{
   1.162 +			encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
   1.163 +			decryptor = CRC2Decryptor::NewL(iKey->Des(), iEffectiveKeyLen);
   1.164 +			CleanupStack::Pop(encryptor);
   1.165 +		}
   1.166 +		break;
   1.167 +		case (ERC4):
   1.168 +		{
   1.169 +			iEncryptor = CARC4::NewL(*iKey, 0); 
   1.170 +			iDecryptor = CARC4::NewL(*iKey, 0); 
   1.171 +		}
   1.172 +		break;
   1.173 +		case (ECipherNull):
   1.174 +		{
   1.175 +			iEncryptor = CNullCipher::NewL(); 
   1.176 +			iDecryptor = CNullCipher::NewL(); 
   1.177 +		}
   1.178 +		break;	
   1.179 +		default:
   1.180 +			ASSERT(0);
   1.181 +			User::Leave(KErrNotSupported);
   1.182 +	}
   1.183 +
   1.184 +	CleanupStack::PushL(encryptor);
   1.185 +	CleanupStack::PushL(decryptor);
   1.186 +	if(!iEncryptor && !iDecryptor)
   1.187 +		{
   1.188 +		CPaddingSSLv3* dPadding = CPaddingSSLv3::NewLC(decryptor->BlockSize());
   1.189 +		CPaddingSSLv3* ePadding = CPaddingSSLv3::NewLC(encryptor->BlockSize());
   1.190 +		iEncryptor = CBufferedEncryptor::NewL(encryptor, ePadding);
   1.191 +		CleanupStack::Pop(ePadding);
   1.192 +		iDecryptor = CBufferedDecryptor::NewL(decryptor, dPadding);
   1.193 +		CleanupStack::Pop(dPadding);
   1.194 +		}
   1.195 +
   1.196 +	CleanupStack::Pop(2, encryptor);
   1.197 +
   1.198 +	iEResult = HBufC8::NewMaxL(iEncryptor->MaxFinalOutputLength(iInput->Length()));
   1.199 +	iDResult = HBufC8::NewMaxL(iDecryptor->MaxFinalOutputLength(iEResult->Size()));
   1.200 +}
   1.201 +
   1.202 +void CActionIncremental::DoPerformActionL()
   1.203 +	{
   1.204 +	TRAPD(res, DoDoPerformActionL())
   1.205 +	if(res == KErrNoMemory)
   1.206 +		{
   1.207 +		iEncryptor->Reset();
   1.208 +		iDecryptor->Reset();
   1.209 +		}
   1.210 +	}
   1.211 +
   1.212 +void CActionIncremental::DoDoPerformActionL()
   1.213 +{
   1.214 +	__UHEAP_MARK;
   1.215 +	iResult = ETrue;
   1.216 +	TPtr8 eResultActual = iEResult->Des();
   1.217 +	eResultActual.FillZ(eResultActual.MaxLength());
   1.218 +	eResultActual.SetLength(0);
   1.219 +
   1.220 +	TPtr8 dResultActual = iDResult->Des();
   1.221 +	dResultActual.FillZ(dResultActual.MaxLength());
   1.222 +	dResultActual.SetLength(0);
   1.223 +
   1.224 +	TInt len = iInput->Length();
   1.225 +	for(TInt i=1; i<len; i++)
   1.226 +	{
   1.227 +		TInt j = 0;
   1.228 +		for(; j+i<len; j+=i)
   1.229 +		{//	encryption in blocks of size i
   1.230 +			iEncryptor->Process(iInput->Mid(j,i), eResultActual);
   1.231 +		}
   1.232 +	
   1.233 +		iEncryptor->ProcessFinalL(iInput->Mid(j), eResultActual);
   1.234 +		
   1.235 +		j=0;
   1.236 +		for(; (j+(len-i))<len; j+=(len-i))
   1.237 +		{//	Decryption in blocks of size (len - i)
   1.238 +			iDecryptor->Process(eResultActual.Mid(j, len-i), dResultActual);
   1.239 +		}
   1.240 +		
   1.241 +		iDecryptor->ProcessFinalL(eResultActual.Mid(j), dResultActual);
   1.242 +		
   1.243 +		if(dResultActual != *iInput)
   1.244 +		{
   1.245 +			iResult = EFalse;
   1.246 +		}
   1.247 +		
   1.248 +		eResultActual.FillZ(eResultActual.MaxLength());
   1.249 +		dResultActual.FillZ(dResultActual.MaxLength());
   1.250 +		eResultActual.SetLength(0);
   1.251 +		dResultActual.SetLength(0);
   1.252 +	}
   1.253 +	__UHEAP_MARKEND;
   1.254 +
   1.255 +}
   1.256 +