crypto/weakcrypto/test/tsymmetric/tactionincremental.cpp
changeset 71 dd83586b62d6
equal deleted inserted replaced
66:8873e6835f7b 71:dd83586b62d6
       
     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 
       
    23 CTestAction* CActionIncremental::NewL(RFs& aFs,
       
    24 									   CConsoleBase& aConsole,
       
    25 									   Output& aOut, 
       
    26 									   const TTestActionSpec& aTestActionSpec)
       
    27 	{
       
    28 	CTestAction* self = CActionIncremental::NewLC(aFs, aConsole,
       
    29 		aOut, aTestActionSpec);
       
    30 	CleanupStack::Pop();
       
    31 	return self;
       
    32 	}
       
    33 
       
    34 CTestAction* CActionIncremental::NewLC(RFs& aFs,
       
    35 										CConsoleBase& aConsole,
       
    36 										Output& aOut, 
       
    37 										const TTestActionSpec& aTestActionSpec)
       
    38 	{
       
    39 	CActionIncremental* self = new(ELeave) CActionIncremental(aFs, aConsole, aOut);
       
    40 	CleanupStack::PushL(self);
       
    41 	self->ConstructL(aTestActionSpec);
       
    42 	return self;
       
    43 	}
       
    44 
       
    45 CActionIncremental::~CActionIncremental()
       
    46 {
       
    47 }
       
    48 
       
    49 CActionIncremental::CActionIncremental(RFs& aFs, 
       
    50 								 CConsoleBase& aConsole,
       
    51 								 Output& aOut)						 
       
    52 : CCryptoTestAction(aFs, aConsole, aOut)
       
    53 {}
       
    54 
       
    55 void CActionIncremental::DoPerformPostrequisiteL()
       
    56 {
       
    57 	delete iEncryptor;
       
    58 	delete iDecryptor;
       
    59 }
       
    60 
       
    61 void CActionIncremental::DoPerformPrerequisiteL()
       
    62 {
       
    63 	TInt err = KErrNone;
       
    64 	TInt pos = 0;
       
    65 	TPtrC8 incremental = Input::ParseElement(*iBody, KIncrementalStart, KIncrementalEnd, pos, err);
       
    66 
       
    67 	DoInputParseL(incremental);
       
    68 
       
    69 	CBlockTransformation* encryptor = NULL;
       
    70 	CBlockTransformation* decryptor = NULL;
       
    71 
       
    72 	iEncryptor =0;
       
    73 	iDecryptor =0;
       
    74 
       
    75 	switch (iCipherType)
       
    76 	{
       
    77 		case (EDESECB):
       
    78 		{
       
    79 			//If the test is weak key test
       
    80 			if(iExpectedWeakResult == KErrWeakKey)
       
    81 				{
       
    82 				//we expect to leave with KErrWeakKey reason
       
    83 				if(CDES::IsWeakKey(iKey->Des()))
       
    84 					{
       
    85 					User::Leave(KErrWeakKey);
       
    86 					}
       
    87 				else //test is unsuccessful, leave with a different reason
       
    88 					{
       
    89 					User::Leave(KErrGeneral);
       
    90 					}	
       
    91 				}
       
    92 			encryptor = CDESEncryptor::NewLC(iKey->Des());
       
    93 			decryptor = CDESDecryptor::NewL(iKey->Des());
       
    94 			CleanupStack::Pop(encryptor);
       
    95 		}
       
    96 		break;
       
    97 		case (EDESCBC):
       
    98 		{
       
    99 			CBlockTransformation* desEncryptor = NULL;
       
   100 			CBlockTransformation* desDecryptor = NULL;
       
   101 			
       
   102 			if(iExpectedWeakResult == KErrWeakKey)
       
   103 				{
       
   104 				if(CDES::IsWeakKey(iKey->Des()))
       
   105 					{
       
   106 					User::Leave(KErrWeakKey);
       
   107 					}
       
   108 				else
       
   109 					{
       
   110 					User::Leave(KErrGeneral);
       
   111 					}	
       
   112 				}
       
   113 			desEncryptor = CDESEncryptor::NewLC(iKey->Des());
       
   114 			desDecryptor = CDESDecryptor::NewLC(iKey->Des());
       
   115 			
       
   116 			encryptor = CModeCBCEncryptor::NewL(desEncryptor, iIV->Des());
       
   117 			CleanupStack::PushL(encryptor);
       
   118 			decryptor = CModeCBCDecryptor::NewL(desDecryptor, iIV->Des());		
       
   119 			CleanupStack::Pop(3);
       
   120 		}
       
   121 		break;
       
   122 		case (E3DESECB):
       
   123 		{
       
   124 			encryptor = C3DESEncryptor::NewLC(iKey->Des());
       
   125 			decryptor = C3DESDecryptor::NewL(iKey->Des());
       
   126 			CleanupStack::Pop(encryptor);
       
   127 		}
       
   128 		break;
       
   129 		case (E3DESCBC):
       
   130 		{
       
   131 			CBlockTransformation* the3DESencryptor = NULL;
       
   132 			CBlockTransformation* the3DESdecryptor = NULL;
       
   133 
       
   134 			the3DESencryptor = C3DESEncryptor::NewLC(iKey->Des());
       
   135 			the3DESdecryptor = C3DESDecryptor::NewLC(iKey->Des());
       
   136 			
       
   137 			encryptor = CModeCBCEncryptor::NewL(the3DESencryptor, iIV->Des());
       
   138 			CleanupStack::PushL(encryptor);
       
   139 			decryptor = CModeCBCDecryptor::NewL(the3DESdecryptor, iIV->Des());		
       
   140 			CleanupStack::Pop(3);
       
   141 		}
       
   142 		break;
       
   143 		case (EAESECB):
       
   144 		{
       
   145 			encryptor = CAESEncryptor::NewLC(iKey->Des());
       
   146 			decryptor = CAESDecryptor::NewL(iKey->Des());
       
   147 			CleanupStack::Pop(encryptor);
       
   148 		}
       
   149 		break;
       
   150 		case (ERC2ECB):
       
   151 		{
       
   152 			encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
       
   153 			decryptor = CRC2Decryptor::NewL(iKey->Des(), iEffectiveKeyLen);
       
   154 			CleanupStack::Pop(encryptor);
       
   155 		}
       
   156 		break;
       
   157 		case (ERC2CBC):
       
   158 		{
       
   159 			encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
       
   160 			decryptor = CRC2Decryptor::NewL(iKey->Des(), iEffectiveKeyLen);
       
   161 			CleanupStack::Pop(encryptor);
       
   162 		}
       
   163 		break;
       
   164 		case (ERC4):
       
   165 		{
       
   166 			iEncryptor = CARC4::NewL(*iKey, 0); 
       
   167 			iDecryptor = CARC4::NewL(*iKey, 0); 
       
   168 		}
       
   169 		break;
       
   170 		case (ECipherNull):
       
   171 		{
       
   172 			iEncryptor = CNullCipher::NewL(); 
       
   173 			iDecryptor = CNullCipher::NewL(); 
       
   174 		}
       
   175 		break;	
       
   176 		default:
       
   177 			ASSERT(0);
       
   178 			User::Leave(KErrNotSupported);
       
   179 	}
       
   180 
       
   181 	CleanupStack::PushL(encryptor);
       
   182 	CleanupStack::PushL(decryptor);
       
   183 	if(!iEncryptor && !iDecryptor)
       
   184 		{
       
   185 		CPaddingSSLv3* dPadding = CPaddingSSLv3::NewLC(decryptor->BlockSize());
       
   186 		CPaddingSSLv3* ePadding = CPaddingSSLv3::NewLC(encryptor->BlockSize());
       
   187 		iEncryptor = CBufferedEncryptor::NewL(encryptor, ePadding);
       
   188 		CleanupStack::Pop(ePadding);
       
   189 		iDecryptor = CBufferedDecryptor::NewL(decryptor, dPadding);
       
   190 		CleanupStack::Pop(dPadding);
       
   191 		}
       
   192 
       
   193 	CleanupStack::Pop(2, encryptor);
       
   194 
       
   195 	iEResult = HBufC8::NewMaxL(iEncryptor->MaxFinalOutputLength(iInput->Length()));
       
   196 	iDResult = HBufC8::NewMaxL(iDecryptor->MaxFinalOutputLength(iEResult->Size()));
       
   197 }
       
   198 
       
   199 void CActionIncremental::DoPerformActionL()
       
   200 	{
       
   201 	TRAPD(res, DoDoPerformActionL())
       
   202 	if(res == KErrNoMemory)
       
   203 		{
       
   204 		iEncryptor->Reset();
       
   205 		iDecryptor->Reset();
       
   206 		}
       
   207 	}
       
   208 
       
   209 void CActionIncremental::DoDoPerformActionL()
       
   210 {
       
   211 	__UHEAP_MARK;
       
   212 	iResult = ETrue;
       
   213 	TPtr8 eResultActual = iEResult->Des();
       
   214 	eResultActual.FillZ(eResultActual.MaxLength());
       
   215 	eResultActual.SetLength(0);
       
   216 
       
   217 	TPtr8 dResultActual = iDResult->Des();
       
   218 	dResultActual.FillZ(dResultActual.MaxLength());
       
   219 	dResultActual.SetLength(0);
       
   220 
       
   221 	TInt len = iInput->Length();
       
   222 	for(TInt i=1; i<len; i++)
       
   223 	{
       
   224 		TInt j = 0;
       
   225 		for(; j+i<len; j+=i)
       
   226 		{//	encryption in blocks of size i
       
   227 			iEncryptor->Process(iInput->Mid(j,i), eResultActual);
       
   228 		}
       
   229 	
       
   230 		iEncryptor->ProcessFinalL(iInput->Mid(j), eResultActual);
       
   231 		
       
   232 		j=0;
       
   233 		for(; (j+(len-i))<len; j+=(len-i))
       
   234 		{//	Decryption in blocks of size (len - i)
       
   235 			iDecryptor->Process(eResultActual.Mid(j, len-i), dResultActual);
       
   236 		}
       
   237 		
       
   238 		iDecryptor->ProcessFinalL(eResultActual.Mid(j), dResultActual);
       
   239 		
       
   240 		if(dResultActual != *iInput)
       
   241 		{
       
   242 			iResult = EFalse;
       
   243 		}
       
   244 		
       
   245 		eResultActual.FillZ(eResultActual.MaxLength());
       
   246 		dResultActual.FillZ(dResultActual.MaxLength());
       
   247 		eResultActual.SetLength(0);
       
   248 		dResultActual.SetLength(0);
       
   249 	}
       
   250 	__UHEAP_MARKEND;
       
   251 
       
   252 }
       
   253