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