os/security/crypto/weakcrypto/test/tsymmetric/tactionvector.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 "tactionvector.h"
    20 #include "symmetric.h"
    21 
    22 CTestAction* CActionVector::NewL(RFs& aFs,
    23 									   CConsoleBase& aConsole,
    24 									   Output& aOut, 
    25 									   const TTestActionSpec& aTestActionSpec)
    26 {
    27 	CTestAction* self = CActionVector::NewLC(aFs, aConsole,
    28 		aOut, aTestActionSpec);
    29 	CleanupStack::Pop();
    30 	return self;
    31 }
    32 
    33 CTestAction* CActionVector::NewLC(RFs& aFs,
    34 										CConsoleBase& aConsole,
    35 										Output& aOut, 
    36 										const TTestActionSpec& aTestActionSpec)
    37 {
    38 	CActionVector* self = new(ELeave) CActionVector(aFs, aConsole, aOut);
    39 	CleanupStack::PushL(self);
    40 	self->ConstructL(aTestActionSpec);
    41 	return self;
    42 	}
    43 
    44 CActionVector::~CActionVector()
    45 {
    46 	delete iEncryptor;
    47 	delete iDecryptor;
    48 }
    49 
    50 CActionVector::CActionVector(RFs& aFs, 
    51 								 CConsoleBase& aConsole,
    52 								 Output& aOut)
    53 								 
    54 :	CCryptoTestAction(aFs, aConsole, aOut)
    55 {}
    56 
    57 
    58 void CActionVector::DoPerformPrerequisiteL()
    59 {
    60 	TInt err = KErrNone;
    61 	TInt pos = 0;
    62 	TPtrC8 vector = Input::ParseElement(*iBody, KVectorStart, KVectorEnd, pos, err);
    63 
    64 	DoInputParseL(vector);
    65 
    66 	CBlockTransformation* encryptor = NULL;
    67 	CBlockTransformation* decryptor = NULL;
    68 	iEncryptor = 0;
    69 	iDecryptor = 0;
    70 
    71 	switch (iCipherType)
    72 	{
    73 		case (EDESECB):
    74 		{
    75 			encryptor = CDESEncryptor::NewLC(iKey->Des());
    76 			decryptor = CDESDecryptor::NewL(iKey->Des());
    77 			CleanupStack::Pop(encryptor);
    78 		}
    79 		break;
    80 		case(EDESCBC):
    81 		{
    82 			CBlockTransformation* desEncryptor = NULL;
    83 			CBlockTransformation* desDecryptor = NULL;
    84 
    85 			desEncryptor = CDESEncryptor::NewLC(iKey->Des());
    86 			desDecryptor = CDESDecryptor::NewLC(iKey->Des());
    87 			
    88 			encryptor = CModeCBCEncryptor::NewL(desEncryptor, iIV->Des());
    89 			CleanupStack::PushL(encryptor);
    90 			decryptor = CModeCBCDecryptor::NewL(desDecryptor, iIV->Des());		
    91 
    92 			CleanupStack::Pop(3, desEncryptor);
    93 		}
    94 		break;
    95 		case (E3DESECB):
    96 		{
    97 			encryptor = C3DESEncryptor::NewLC(iKey->Des());
    98 			decryptor = C3DESDecryptor::NewL(iKey->Des());
    99 			CleanupStack::Pop(encryptor);
   100 		}
   101 		break;
   102 		case (E3DESCBC):
   103 		{
   104 			CBlockTransformation* the3DESencryptor = NULL;
   105 			CBlockTransformation* the3DESdecryptor = NULL;
   106 
   107 			the3DESencryptor = C3DESEncryptor::NewLC(iKey->Des());
   108 			the3DESdecryptor = C3DESDecryptor::NewLC(iKey->Des());
   109 			
   110 			encryptor = CModeCBCEncryptor::NewL(the3DESencryptor, iIV->Des());
   111 			CleanupStack::PushL(encryptor);
   112 			decryptor = CModeCBCDecryptor::NewL(the3DESdecryptor, iIV->Des());		
   113 
   114 			CleanupStack::Pop(3, the3DESencryptor);
   115 		}
   116 		break;
   117 		case (EAESECB):
   118 		{
   119 			encryptor = CAESEncryptor::NewLC(iKey->Des());
   120 			decryptor = CAESDecryptor::NewL(iKey->Des());
   121 			
   122 			CleanupStack::Pop(encryptor);
   123 		}
   124 		break;
   125 		case (ERC2ECB):
   126 		{
   127 			encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
   128 			decryptor = CRC2Decryptor::NewL(iKey->Des(), iEffectiveKeyLen);
   129 			CleanupStack::Pop(encryptor);
   130 		}
   131 		break;
   132 		case (ERC2CBC):
   133 		{
   134 			CBlockTransformation* theRC2encryptor = NULL;
   135 			CBlockTransformation* theRC2decryptor = NULL;
   136 
   137 			theRC2encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
   138 			theRC2decryptor = CRC2Decryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
   139 			
   140 			encryptor = CModeCBCEncryptor::NewL(theRC2encryptor, iIV->Des());
   141 			CleanupStack::PushL(encryptor);
   142 			decryptor = CModeCBCDecryptor::NewL(theRC2decryptor, iIV->Des());		
   143 
   144 			CleanupStack::Pop(3, theRC2encryptor);
   145 		}
   146 		break;
   147 		case (ERC4):
   148 		{
   149 			iEncryptor = CARC4::NewL(*iKey,0);
   150 			iDecryptor = CARC4::NewL(*iKey,0);
   151 		}
   152 		break;
   153 		case (ECipherNull):
   154 		{
   155 			iEncryptor = CNullCipher::NewL();
   156 			iDecryptor = CNullCipher::NewL();
   157 		}
   158 		break;
   159 			
   160 		default:
   161 		{
   162 			ASSERT(0);
   163 			User::Leave(KErrNotSupported);
   164 		}
   165 	}
   166 	
   167 	CleanupStack::PushL(encryptor);
   168 	CleanupStack::PushL(decryptor);
   169 
   170 	if(!iEncryptor && !iDecryptor)
   171 		{
   172 		CPaddingSSLv3* dPadding = CPaddingSSLv3::NewLC(decryptor->BlockSize());
   173 		CPaddingSSLv3* ePadding = CPaddingSSLv3::NewLC(encryptor->BlockSize());
   174 		iEncryptor = CBufferedEncryptor::NewL(encryptor, ePadding);	
   175 		CleanupStack::Pop(ePadding);
   176 		iDecryptor = CBufferedDecryptor::NewL(decryptor, dPadding);
   177 		CleanupStack::Pop(dPadding);
   178 		}
   179 
   180 	iEResult = HBufC8::NewMaxL(iEncryptor->MaxOutputLength(iInput->Length()));
   181 	iDResult = HBufC8::NewMaxL(iDecryptor->MaxOutputLength(iEResult->Size()));
   182 
   183 	CleanupStack::Pop(2, encryptor);
   184 }
   185 
   186 void CActionVector::DoPerformActionL()
   187 {
   188 //	First perform the test blockwise (ie passing in one block at a time)	
   189 	TUint blockSize = iEncryptor->BlockSize();
   190 	TUint index = 0;
   191 	while (index <= (iInput->Size() - blockSize)) 
   192 	{
   193 		TPtr8 theEncryptResult((TUint8*)&(iEResult->operator[](index)), blockSize, blockSize);
   194 		theEncryptResult.FillZ(theEncryptResult.MaxLength());
   195 		theEncryptResult.SetLength(0);
   196 
   197 		TPtrC8 theEncryptInput(iInput->Mid(index, blockSize));	
   198 		iEncryptor->Process(theEncryptInput, theEncryptResult);
   199 
   200 		if (iOutput->Mid(index, blockSize) == theEncryptResult)
   201 			iResult = ETrue;
   202 		else
   203 			break;	//	No point doing any more
   204 	
   205 		index+=blockSize;
   206 	}
   207 	
   208 	if (*iOutput==*iEResult)
   209 	{
   210 		iResult = ETrue;
   211 
   212 		index = 0;
   213 		while (index <= (iEResult->Size()- blockSize))
   214 		{
   215 			TPtr8 theDecryptResult((TUint8*)&(iDResult->operator[](index)), blockSize, blockSize);
   216 			theDecryptResult.FillZ(theDecryptResult.MaxLength());
   217 			theDecryptResult.SetLength(0);
   218 
   219 			TPtrC8 theDecryptInput(iEResult->Mid(index, blockSize));
   220 			iDecryptor->Process(theDecryptInput, theDecryptResult);
   221 			
   222 			if(iInput->Mid(index, blockSize) != theDecryptResult)
   223 			{
   224 				iResult = EFalse;
   225 				break;	//	No point doing any more
   226 			}
   227 		
   228 			index+=blockSize;
   229 		}
   230 	
   231 		if (*iInput!=*iDResult)
   232 		iResult = EFalse;	
   233 	}
   234 	
   235 	
   236 	iEncryptor->Reset();
   237 	iDecryptor->Reset();
   238 //	Now, if input is longer than a single block, repeat passing all the data
   239 	if ((TUint)iInput->Size() > blockSize)
   240 	{
   241 		TPtr8 theEncryptResult(iEResult->Des());
   242 		theEncryptResult.FillZ(theEncryptResult.MaxLength());
   243 		theEncryptResult.SetLength(0);
   244 
   245 		TPtrC8 theInput(*iInput);
   246 		iEncryptor->Process(theInput, theEncryptResult);
   247 
   248 		if (*iOutput!=*iEResult)
   249 		{
   250 			iResult = EFalse;
   251 		}
   252 		else
   253 		{
   254 			TPtr8 theDecryptResult(iDResult->Des());
   255 			theDecryptResult.FillZ(theDecryptResult.MaxLength());
   256 			theDecryptResult.SetLength(0);
   257 
   258 			iDecryptor->Process(*iEResult, theDecryptResult);
   259 			if (*iInput!=*iDResult)
   260 				iResult = EFalse;
   261 		}
   262 	}
   263 }
   264