crypto/weakcrypto/test/tsymmetric/tactionmontecarlo.cpp
changeset 0 2c201484c85f
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     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 "tactionmontecarlo.h"
       
    20 #include "bufferedtransformation.h"
       
    21 #include "rijndael.h"
       
    22 #include "cbcmode.h"
       
    23 #include "padding.h"
       
    24 
       
    25 const TInt KAESBlockSizeBytes = 16;	//	128 bits
       
    26 
       
    27 CTestAction* CActionMonteCarlo::NewL(RFs& aFs,
       
    28 									   CConsoleBase& aConsole,
       
    29 									   Output& aOut, 
       
    30 									   const TTestActionSpec& aTestActionSpec)
       
    31 	{
       
    32 	CTestAction* self = CActionMonteCarlo::NewLC(aFs, aConsole,
       
    33 		aOut, aTestActionSpec);
       
    34 	CleanupStack::Pop();
       
    35 	return self;
       
    36 	}
       
    37 
       
    38 CTestAction* CActionMonteCarlo::NewLC(RFs& aFs,
       
    39 										CConsoleBase& aConsole,
       
    40 										Output& aOut, 
       
    41 										const TTestActionSpec& aTestActionSpec)
       
    42 	{
       
    43 	CActionMonteCarlo* self = new(ELeave) CActionMonteCarlo(aFs, aConsole, aOut);
       
    44 	CleanupStack::PushL(self);
       
    45 	self->ConstructL(aTestActionSpec);
       
    46 	return self;
       
    47 	}
       
    48 
       
    49 CActionMonteCarlo::~CActionMonteCarlo()
       
    50 {
       
    51 	delete iEncrypt;
       
    52 	delete iDecrypt;
       
    53 }
       
    54 
       
    55 CActionMonteCarlo::CActionMonteCarlo(RFs& aFs, 
       
    56 								 CConsoleBase& aConsole,
       
    57 								 Output& aOut)
       
    58 								 
       
    59 : CCryptoTestAction(aFs, aConsole, aOut)
       
    60 {}
       
    61 
       
    62 
       
    63 void CActionMonteCarlo::DoPerformPrerequisiteL()
       
    64 {
       
    65 	TInt err = KErrNone;
       
    66 	TInt pos = 0;
       
    67 	TPtrC8 monteCarlo = Input::ParseElement(*iBody, KMonteCarloStart, KMonteCarloEnd, pos, err);
       
    68 
       
    69 	DoInputParseL(monteCarlo);
       
    70 
       
    71 	CBlockTransformation* encryptor = NULL;
       
    72 	CBlockTransformation* decryptor = NULL;
       
    73 
       
    74 	switch (iCipherType)
       
    75 	{
       
    76 		case (EAESMonteCarloEncryptECB):
       
    77 		{
       
    78 			encryptor = CAESEncryptor::NewLC(iKey->Des());
       
    79 		}
       
    80 		break;
       
    81 		case (EAESMonteCarloDecryptECB):
       
    82 		{
       
    83 			decryptor = CAESDecryptor::NewLC(iKey->Des());
       
    84 		}
       
    85 		break;
       
    86 		case (EAESMonteCarloEncryptCBC):
       
    87 		{
       
    88 			CBlockTransformation* aesEncryptor = NULL;		
       
    89 			aesEncryptor = CAESEncryptor::NewLC(iKey->Des());
       
    90 			
       
    91 			encryptor = CModeCBCEncryptor::NewL(aesEncryptor, iIV->Des());
       
    92 			CleanupStack::Pop(aesEncryptor);
       
    93 			CleanupStack::PushL(encryptor);		
       
    94 		}
       
    95 		break;
       
    96 		case (EAESMonteCarloDecryptCBC):
       
    97 		{
       
    98 			CBlockTransformation* aesDecryptor = NULL;		
       
    99 			aesDecryptor = CAESDecryptor::NewLC(iKey->Des());
       
   100 			
       
   101 			decryptor = CModeCBCDecryptor::NewL(aesDecryptor, iIV->Des());
       
   102 			CleanupStack::Pop(aesDecryptor);
       
   103 			CleanupStack::PushL(decryptor);		
       
   104 		}
       
   105 		break;
       
   106 		default:
       
   107 		{
       
   108 			ASSERT(0);
       
   109 			User::Leave(KErrNotSupported);
       
   110 		}
       
   111 	}
       
   112 	
       
   113 
       
   114 	CPaddingSSLv3* padding = 0;
       
   115 	if (encryptor)
       
   116 		{
       
   117 		padding = CPaddingSSLv3::NewLC(encryptor->BlockSize());
       
   118 		iEncrypt = CBufferedEncryptor::NewL(encryptor, padding);	
       
   119 		iEResult = HBufC8::NewMaxL(iEncrypt->MaxOutputLength(iInput->Length()));
       
   120 		}
       
   121 	else if (decryptor)
       
   122 		{
       
   123 		padding = CPaddingSSLv3::NewLC(decryptor->BlockSize());
       
   124 		iDecrypt = CBufferedDecryptor::NewL(decryptor, padding);
       
   125 		iDResult = HBufC8::NewMaxL(iDecrypt->MaxOutputLength(iInput->Size()));
       
   126 		}
       
   127 
       
   128 	CleanupStack::Pop(2);	//	padding, encryptor/decryptor
       
   129 
       
   130 }
       
   131 
       
   132 
       
   133 void CActionMonteCarlo::DoPerformActionL()
       
   134 {
       
   135 	iResult = EFalse;
       
   136 
       
   137 	__ASSERT_DEBUG(iInput->Size()==KAESBlockSizeBytes, User::Panic(_L("tsymmetric"), KErrNotSupported));
       
   138 	
       
   139 	if (iCipherType==EAESMonteCarloEncryptECB)
       
   140 		DoAESEncryptECB();
       
   141 	else if (iCipherType==EAESMonteCarloDecryptECB)
       
   142 		DoAESDecryptECB();	
       
   143 	else if (iCipherType==EAESMonteCarloEncryptCBC)
       
   144 		DoAESEncryptCBC();
       
   145 	else if (iCipherType==EAESMonteCarloDecryptCBC)
       
   146 		DoAESDecryptCBC();
       
   147 	else
       
   148 		User::Leave(KErrNotSupported);
       
   149 }
       
   150 
       
   151 void CActionMonteCarlo::DoAESEncryptECB()
       
   152 {
       
   153 	TPtr8 theEncryptResult(iEResult->Des());
       
   154 	theEncryptResult.FillZ(theEncryptResult.MaxLength());
       
   155 	theEncryptResult.SetLength(0);
       
   156 
       
   157 	TInt index = 0;
       
   158 	TPtr8 theInput(iInput->Des());
       
   159 	for (; index < KMonteCarloIterations; index++)
       
   160 	{
       
   161 		iEncrypt->Process(theInput, theEncryptResult);	
       
   162 		theInput.Copy(theEncryptResult);
       
   163 		theEncryptResult.FillZ(theEncryptResult.MaxLength());
       
   164 		theEncryptResult.SetLength(0);
       
   165 	}
       
   166 	
       
   167 	if (*iOutput==*iEResult)
       
   168 	{	
       
   169 		iResult = ETrue;
       
   170 	}
       
   171 }
       
   172 
       
   173 void CActionMonteCarlo::DoAESDecryptECB()
       
   174 {
       
   175 	TPtr8 theDecryptResult(iDResult->Des());
       
   176 	theDecryptResult.FillZ(theDecryptResult.MaxLength());
       
   177 	theDecryptResult.SetLength(0);
       
   178 
       
   179 	TInt index = 0;
       
   180 	TPtr8 theInput(iInput->Des());
       
   181 	for (; index < KMonteCarloIterations; index++)
       
   182 	{
       
   183 		iDecrypt->Process(theInput, theDecryptResult);	
       
   184 		theInput.Copy(theDecryptResult);
       
   185 		theDecryptResult.FillZ(theDecryptResult.MaxLength());
       
   186 		theDecryptResult.SetLength(0);
       
   187 	}
       
   188 	
       
   189 	if (*iOutput==*iInput)
       
   190 	{	
       
   191 		iResult = ETrue;
       
   192 	}
       
   193 }
       
   194 
       
   195 void CActionMonteCarlo::DoAESEncryptCBC()
       
   196     {	
       
   197 	TPtr8 theEncryptResult(iEResult->Des());
       
   198 	theEncryptResult.FillZ(theEncryptResult.MaxLength());
       
   199 	theEncryptResult.SetLength(0);
       
   200 
       
   201 	TInt index = 0;
       
   202 	TPtr8 theInput(iInput->Des());
       
   203 
       
   204 	TBuf8<KAESBlockSizeBytes> nextBuf;
       
   205 	nextBuf.FillZ(KAESBlockSizeBytes);
       
   206 
       
   207     for (; index < KMonteCarloIterations-1; index++)
       
   208 	    {
       
   209 	    iEncrypt->Process(theInput, theEncryptResult);	
       
   210 
       
   211 	    if (index==0)
       
   212 		theInput.Copy(*iIV);	//	First loop, use the original IV as next PT block
       
   213 		else	
       
   214 		theInput.Copy(nextBuf);	//	Use previous CT block as next PT block
       
   215 		
       
   216 	    //	 Save CT block for next loop when it'll become the PT block	
       
   217 		nextBuf.Copy(theEncryptResult);
       
   218 	    //	 Reset for next encryption	
       
   219 		theEncryptResult.FillZ(theEncryptResult.MaxLength());
       
   220 		theEncryptResult.SetLength(0);
       
   221 	    }
       
   222 	
       
   223     iEncrypt->Process(theInput, theEncryptResult);	
       
   224 	
       
   225 	if  (theEncryptResult.Compare(*iOutput)==KErrNone)
       
   226 	    {	
       
   227 	    iResult = ETrue;
       
   228 	    }
       
   229 
       
   230     }
       
   231 
       
   232 void CActionMonteCarlo::DoAESDecryptCBC()
       
   233     {
       
   234 	TPtr8 theDecryptResult(iDResult->Des());
       
   235 	theDecryptResult.FillZ(theDecryptResult.MaxLength());
       
   236 	theDecryptResult.SetLength(0);
       
   237 
       
   238 	TInt index = 0;
       
   239     TPtr8 theInput(iInput->Des());
       
   240 
       
   241     for (; index < KMonteCarloIterations-1; index++)
       
   242 	    {
       
   243 	    iDecrypt->Process(theInput, theDecryptResult);	
       
   244 		
       
   245 		//	Use previous PT block as next CT block
       
   246 	 	theInput.Copy(theDecryptResult);
       
   247 
       
   248 	    //  Reset for next decryption	
       
   249 		theDecryptResult.FillZ(theDecryptResult.MaxLength());
       
   250 		theDecryptResult.SetLength(0);
       
   251 	    }
       
   252 	
       
   253     // Last loop	
       
   254     iDecrypt->Process(theInput, theDecryptResult);	
       
   255 
       
   256 	if (theDecryptResult.Compare(*iOutput)==KErrNone)
       
   257  	   {	
       
   258 	   iResult = ETrue;
       
   259 	   }
       
   260 
       
   261     }