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