crypto/weakcryptospi/test/tsymmetric/tactionincremental.cpp
changeset 8 35751d3474b7
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     1 /*
       
     2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "tactionincremental.h"
       
    20 #include "symmetric.h"
       
    21 #include "des.h"
       
    22 #include "securityerr.h"
       
    23 
       
    24 CTestAction* CActionIncremental::NewL(RFs& aFs,
       
    25 									   CConsoleBase& aConsole,
       
    26 									   Output& aOut, 
       
    27 									   const TTestActionSpec& aTestActionSpec)
       
    28 	{
       
    29 	CTestAction* self = CActionIncremental::NewLC(aFs, aConsole,
       
    30 		aOut, aTestActionSpec);
       
    31 	CleanupStack::Pop();
       
    32 	return self;
       
    33 	}
       
    34 
       
    35 CTestAction* CActionIncremental::NewLC(RFs& aFs,
       
    36 										CConsoleBase& aConsole,
       
    37 										Output& aOut, 
       
    38 										const TTestActionSpec& aTestActionSpec)
       
    39 	{
       
    40 	CActionIncremental* self = new(ELeave) CActionIncremental(aFs, aConsole, aOut);
       
    41 	CleanupStack::PushL(self);
       
    42 	self->ConstructL(aTestActionSpec);
       
    43 	return self;
       
    44 	}
       
    45 
       
    46 CActionIncremental::~CActionIncremental()
       
    47 {
       
    48 }
       
    49 
       
    50 CActionIncremental::CActionIncremental(RFs& aFs, 
       
    51 								 CConsoleBase& aConsole,
       
    52 								 Output& aOut)						 
       
    53 : CCryptoTestAction(aFs, aConsole, aOut)
       
    54 {}
       
    55 
       
    56 void CActionIncremental::DoPerformPostrequisiteL()
       
    57 {
       
    58 	if (iEncryptor)
       
    59 		{
       
    60 		iEncryptor->Reset();
       
    61 		delete iEncryptor;
       
    62 		}
       
    63 	
       
    64 	if (iDecryptor)
       
    65 		{
       
    66 		iDecryptor->Reset();
       
    67 		delete iDecryptor;
       
    68 		}
       
    69 }
       
    70 
       
    71 void CActionIncremental::DoPerformPrerequisiteL()
       
    72 {
       
    73 	iResult = ETrue;
       
    74 	TInt err = KErrNone;
       
    75 	TInt pos = 0;
       
    76 	TPtrC8 incremental = Input::ParseElement(*iBody, KIncrementalStart, KIncrementalEnd, pos, err);
       
    77 
       
    78 	DoInputParseL(incremental);
       
    79 
       
    80 	CBlockTransformation* encryptor = NULL;
       
    81 	CBlockTransformation* decryptor = NULL;
       
    82 
       
    83 	iEncryptor =0;
       
    84 	iDecryptor =0;
       
    85 
       
    86 	switch (iCipherType)
       
    87 	{
       
    88 		case (EDESECB):
       
    89 		{
       
    90 		//If the test is weak key test
       
    91 		if(iExpectedWeakResult == KErrWeakKey)
       
    92 			{
       
    93 			//we expect to leave with KErrWeakKey reason
       
    94 			if(CDES::IsWeakKey(iKey->Des()))
       
    95 				{
       
    96 				User::Leave(KErrWeakKey);
       
    97 				}
       
    98 			else //test is unsuccessful, leave with a different reason
       
    99 				{
       
   100 				User::Leave(KErrGeneral);
       
   101 				}	
       
   102 			}		
       
   103 			encryptor = CDESEncryptor::NewLC(iKey->Des());
       
   104 			decryptor = CDESDecryptor::NewL(iKey->Des());
       
   105 			CleanupStack::Pop(encryptor);
       
   106 		}
       
   107 		break;
       
   108 		case (EDESCBC):
       
   109 		{
       
   110 			CBlockTransformation* desEncryptor = NULL;
       
   111 			CBlockTransformation* desDecryptor = NULL;
       
   112  			
       
   113  			if(iExpectedWeakResult == KErrWeakKey)
       
   114  				{
       
   115  				if(CDES::IsWeakKey(iKey->Des()))
       
   116  					{
       
   117  					User::Leave(KErrWeakKey);
       
   118  					}
       
   119  				else
       
   120  					{
       
   121  					User::Leave(KErrGeneral);
       
   122  					}	
       
   123  				}
       
   124 
       
   125 			desEncryptor = CDESEncryptor::NewLC(iKey->Des());
       
   126 			desDecryptor = CDESDecryptor::NewLC(iKey->Des());
       
   127 			
       
   128 			encryptor = CModeCBCEncryptor::NewL(desEncryptor, iIV->Des());
       
   129 			CleanupStack::PushL(encryptor);
       
   130 			decryptor = CModeCBCDecryptor::NewL(desDecryptor, iIV->Des());		
       
   131 			CleanupStack::Pop(3);
       
   132 		}
       
   133 		break;
       
   134 		case (E3DESECB):
       
   135 		{
       
   136 			encryptor = C3DESEncryptor::NewLC(iKey->Des());
       
   137 			decryptor = C3DESDecryptor::NewL(iKey->Des());
       
   138 			CleanupStack::Pop(encryptor);
       
   139 		}
       
   140 		break;
       
   141 		case (E3DESCBC):
       
   142 		{
       
   143 			CBlockTransformation* the3DESencryptor = NULL;
       
   144 			CBlockTransformation* the3DESdecryptor = NULL;
       
   145 
       
   146 			the3DESencryptor = C3DESEncryptor::NewLC(iKey->Des());
       
   147 			the3DESdecryptor = C3DESDecryptor::NewLC(iKey->Des());
       
   148 			
       
   149 			encryptor = CModeCBCEncryptor::NewL(the3DESencryptor, iIV->Des());
       
   150 			CleanupStack::PushL(encryptor);
       
   151 			decryptor = CModeCBCDecryptor::NewL(the3DESdecryptor, iIV->Des());		
       
   152 			CleanupStack::Pop(3);
       
   153 		}
       
   154 		break;
       
   155 		case (EAESECB):
       
   156 		{
       
   157 			encryptor = CAESEncryptor::NewLC(iKey->Des());
       
   158 			decryptor = CAESDecryptor::NewL(iKey->Des());
       
   159 			CleanupStack::Pop(encryptor);
       
   160 		}
       
   161 		break;
       
   162 		case (ERC2ECB):
       
   163 		{
       
   164 			encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
       
   165 			decryptor = CRC2Decryptor::NewL(iKey->Des(), iEffectiveKeyLen);
       
   166 			CleanupStack::Pop(encryptor);
       
   167 		}
       
   168 		break;
       
   169 		case (ERC2CBC):
       
   170 		{
       
   171 			encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
       
   172 			decryptor = CRC2Decryptor::NewL(iKey->Des(), iEffectiveKeyLen);
       
   173 			CleanupStack::Pop(encryptor);
       
   174 		}
       
   175 		break;
       
   176 		case (ERC4):
       
   177 		{
       
   178 			iEncryptor = CARC4::NewL(*iKey, 0); 
       
   179 			iDecryptor = CARC4::NewL(*iKey, 0); 
       
   180 		}
       
   181 		break;
       
   182 		case (ECipherNull):
       
   183 		{
       
   184 			iEncryptor = CNullCipher::NewL(); 
       
   185 			iDecryptor = CNullCipher::NewL(); 
       
   186 		}
       
   187 		break;	
       
   188 		default:
       
   189 			ASSERT(0);
       
   190 			User::Leave(KErrNotSupported);
       
   191 	}
       
   192 
       
   193 	CleanupStack::PushL(encryptor);
       
   194 	CleanupStack::PushL(decryptor);
       
   195 	if(!iEncryptor && !iDecryptor)
       
   196 		{
       
   197 		CPaddingSSLv3* dPadding = CPaddingSSLv3::NewLC(decryptor->BlockSize());
       
   198 		CPaddingSSLv3* ePadding = CPaddingSSLv3::NewLC(encryptor->BlockSize());
       
   199 		iEncryptor = CBufferedEncryptor::NewL(encryptor, ePadding);
       
   200 		CleanupStack::Pop(ePadding);
       
   201 		iDecryptor = CBufferedDecryptor::NewL(decryptor, dPadding);
       
   202 		CleanupStack::Pop(dPadding);
       
   203 		
       
   204 		TInt desEBlockSize = encryptor->BlockSize();
       
   205 		TInt desDBlockSize = decryptor->BlockSize();
       
   206 		TInt bufEBlockSize = iEncryptor->BlockSize();
       
   207 		TInt bufDBlockSize = iDecryptor->BlockSize();
       
   208 		if ((desEBlockSize/desDBlockSize) != (bufEBlockSize/bufDBlockSize))
       
   209 			{
       
   210 			iResult = EFalse;
       
   211 			}
       
   212 
       
   213 		TInt desEKeySize = encryptor->KeySize();
       
   214 		TInt desDKeySize = decryptor->KeySize();
       
   215 		if (desEKeySize != desDKeySize)
       
   216 			{
       
   217 			iResult = EFalse;
       
   218 			}
       
   219 		
       
   220 		encryptor->Reset();
       
   221 		decryptor->Reset();		
       
   222 		}
       
   223 	
       
   224 	TInt encryptorKeySize = iEncryptor->KeySize();
       
   225 	TInt decryptorKeySize = iDecryptor->KeySize();
       
   226 	if (encryptorKeySize != decryptorKeySize)
       
   227 		{
       
   228 		iResult = EFalse;
       
   229 		}
       
   230 
       
   231 	CleanupStack::Pop(2, encryptor);
       
   232 
       
   233 	iEResult = HBufC8::NewMaxL(iEncryptor->MaxFinalOutputLength(iInput->Length()));
       
   234 	iDResult = HBufC8::NewMaxL(iDecryptor->MaxFinalOutputLength(iEResult->Size()));
       
   235 }
       
   236 
       
   237 void CActionIncremental::DoPerformActionL()
       
   238 	{
       
   239 	TRAPD(res, DoDoPerformActionL())
       
   240 	if(res == KErrNoMemory)
       
   241 		{
       
   242 		iEncryptor->Reset();
       
   243 		iDecryptor->Reset();
       
   244 		}
       
   245 	}
       
   246 
       
   247 void CActionIncremental::DoDoPerformActionL()
       
   248 {
       
   249 	__UHEAP_MARK;	
       
   250 	TPtr8 eResultActual = iEResult->Des();
       
   251 	eResultActual.FillZ(eResultActual.MaxLength());
       
   252 	eResultActual.SetLength(0);
       
   253 
       
   254 	TPtr8 dResultActual = iDResult->Des();
       
   255 	dResultActual.FillZ(dResultActual.MaxLength());
       
   256 	dResultActual.SetLength(0);
       
   257 
       
   258 	TInt len = iInput->Length();
       
   259 	for(TInt i=1; i<len; i++)
       
   260 	{
       
   261 		TInt j = 0;
       
   262 		for(; j+i<len; j+=i)
       
   263 		{//	encryption in blocks of size i
       
   264 			iEncryptor->Process(iInput->Mid(j,i), eResultActual);
       
   265 		}
       
   266 	
       
   267 		iEncryptor->ProcessFinalL(iInput->Mid(j), eResultActual);
       
   268 		
       
   269 		j=0;
       
   270 		for(; (j+(len-i))<len; j+=(len-i))
       
   271 		{//	Decryption in blocks of size (len - i)
       
   272 			iDecryptor->Process(eResultActual.Mid(j, len-i), dResultActual);
       
   273 		}
       
   274 		
       
   275 		iDecryptor->ProcessFinalL(eResultActual.Mid(j), dResultActual);
       
   276 		
       
   277 		if(dResultActual != *iInput)
       
   278 		{
       
   279 			iResult = EFalse;
       
   280 		}
       
   281 		
       
   282 		eResultActual.FillZ(eResultActual.MaxLength());
       
   283 		dResultActual.FillZ(dResultActual.MaxLength());
       
   284 		eResultActual.SetLength(0);
       
   285 		dResultActual.SetLength(0);
       
   286 	}
       
   287 	__UHEAP_MARKEND;
       
   288 
       
   289 }
       
   290