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