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