sl@0: /* sl@0: * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "tactionincremental.h" sl@0: #include "symmetric.h" sl@0: #include "des.h" sl@0: sl@0: CTestAction* CActionIncremental::NewL(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CTestAction* self = CActionIncremental::NewLC(aFs, aConsole, sl@0: aOut, aTestActionSpec); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: CTestAction* CActionIncremental::NewLC(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CActionIncremental* self = new(ELeave) CActionIncremental(aFs, aConsole, aOut); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aTestActionSpec); sl@0: return self; sl@0: } sl@0: sl@0: CActionIncremental::~CActionIncremental() sl@0: { sl@0: } sl@0: sl@0: CActionIncremental::CActionIncremental(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut) sl@0: : CCryptoTestAction(aFs, aConsole, aOut) sl@0: {} sl@0: sl@0: void CActionIncremental::DoPerformPostrequisiteL() sl@0: { sl@0: delete iEncryptor; sl@0: delete iDecryptor; sl@0: } sl@0: sl@0: void CActionIncremental::DoPerformPrerequisiteL() sl@0: { sl@0: TInt err = KErrNone; sl@0: TInt pos = 0; sl@0: TPtrC8 incremental = Input::ParseElement(*iBody, KIncrementalStart, KIncrementalEnd, pos, err); sl@0: sl@0: DoInputParseL(incremental); sl@0: sl@0: CBlockTransformation* encryptor = NULL; sl@0: CBlockTransformation* decryptor = NULL; sl@0: sl@0: iEncryptor =0; sl@0: iDecryptor =0; sl@0: sl@0: switch (iCipherType) sl@0: { sl@0: case (EDESECB): sl@0: { sl@0: //If the test is weak key test sl@0: if(iExpectedWeakResult == KErrWeakKey) sl@0: { sl@0: //we expect to leave with KErrWeakKey reason sl@0: if(CDES::IsWeakKey(iKey->Des())) sl@0: { sl@0: User::Leave(KErrWeakKey); sl@0: } sl@0: else //test is unsuccessful, leave with a different reason sl@0: { sl@0: User::Leave(KErrGeneral); sl@0: } sl@0: } sl@0: encryptor = CDESEncryptor::NewLC(iKey->Des()); sl@0: decryptor = CDESDecryptor::NewL(iKey->Des()); sl@0: CleanupStack::Pop(encryptor); sl@0: } sl@0: break; sl@0: case (EDESCBC): sl@0: { sl@0: CBlockTransformation* desEncryptor = NULL; sl@0: CBlockTransformation* desDecryptor = NULL; sl@0: sl@0: if(iExpectedWeakResult == KErrWeakKey) sl@0: { sl@0: if(CDES::IsWeakKey(iKey->Des())) sl@0: { sl@0: User::Leave(KErrWeakKey); sl@0: } sl@0: else sl@0: { sl@0: User::Leave(KErrGeneral); sl@0: } sl@0: } sl@0: desEncryptor = CDESEncryptor::NewLC(iKey->Des()); sl@0: desDecryptor = CDESDecryptor::NewLC(iKey->Des()); sl@0: sl@0: encryptor = CModeCBCEncryptor::NewL(desEncryptor, iIV->Des()); sl@0: CleanupStack::PushL(encryptor); sl@0: decryptor = CModeCBCDecryptor::NewL(desDecryptor, iIV->Des()); sl@0: CleanupStack::Pop(3); sl@0: } sl@0: break; sl@0: case (E3DESECB): sl@0: { sl@0: encryptor = C3DESEncryptor::NewLC(iKey->Des()); sl@0: decryptor = C3DESDecryptor::NewL(iKey->Des()); sl@0: CleanupStack::Pop(encryptor); sl@0: } sl@0: break; sl@0: case (E3DESCBC): sl@0: { sl@0: CBlockTransformation* the3DESencryptor = NULL; sl@0: CBlockTransformation* the3DESdecryptor = NULL; sl@0: sl@0: the3DESencryptor = C3DESEncryptor::NewLC(iKey->Des()); sl@0: the3DESdecryptor = C3DESDecryptor::NewLC(iKey->Des()); sl@0: sl@0: encryptor = CModeCBCEncryptor::NewL(the3DESencryptor, iIV->Des()); sl@0: CleanupStack::PushL(encryptor); sl@0: decryptor = CModeCBCDecryptor::NewL(the3DESdecryptor, iIV->Des()); sl@0: CleanupStack::Pop(3); sl@0: } sl@0: break; sl@0: case (EAESECB): sl@0: { sl@0: encryptor = CAESEncryptor::NewLC(iKey->Des()); sl@0: decryptor = CAESDecryptor::NewL(iKey->Des()); sl@0: CleanupStack::Pop(encryptor); sl@0: } sl@0: break; sl@0: case (ERC2ECB): sl@0: { sl@0: encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen); sl@0: decryptor = CRC2Decryptor::NewL(iKey->Des(), iEffectiveKeyLen); sl@0: CleanupStack::Pop(encryptor); sl@0: } sl@0: break; sl@0: case (ERC2CBC): sl@0: { sl@0: encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen); sl@0: decryptor = CRC2Decryptor::NewL(iKey->Des(), iEffectiveKeyLen); sl@0: CleanupStack::Pop(encryptor); sl@0: } sl@0: break; sl@0: case (ERC4): sl@0: { sl@0: iEncryptor = CARC4::NewL(*iKey, 0); sl@0: iDecryptor = CARC4::NewL(*iKey, 0); sl@0: } sl@0: break; sl@0: case (ECipherNull): sl@0: { sl@0: iEncryptor = CNullCipher::NewL(); sl@0: iDecryptor = CNullCipher::NewL(); sl@0: } sl@0: break; sl@0: default: sl@0: ASSERT(0); sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: sl@0: CleanupStack::PushL(encryptor); sl@0: CleanupStack::PushL(decryptor); sl@0: if(!iEncryptor && !iDecryptor) sl@0: { sl@0: CPaddingSSLv3* dPadding = CPaddingSSLv3::NewLC(decryptor->BlockSize()); sl@0: CPaddingSSLv3* ePadding = CPaddingSSLv3::NewLC(encryptor->BlockSize()); sl@0: iEncryptor = CBufferedEncryptor::NewL(encryptor, ePadding); sl@0: CleanupStack::Pop(ePadding); sl@0: iDecryptor = CBufferedDecryptor::NewL(decryptor, dPadding); sl@0: CleanupStack::Pop(dPadding); sl@0: } sl@0: sl@0: CleanupStack::Pop(2, encryptor); sl@0: sl@0: iEResult = HBufC8::NewMaxL(iEncryptor->MaxFinalOutputLength(iInput->Length())); sl@0: iDResult = HBufC8::NewMaxL(iDecryptor->MaxFinalOutputLength(iEResult->Size())); sl@0: } sl@0: sl@0: void CActionIncremental::DoPerformActionL() sl@0: { sl@0: TRAPD(res, DoDoPerformActionL()) sl@0: if(res == KErrNoMemory) sl@0: { sl@0: iEncryptor->Reset(); sl@0: iDecryptor->Reset(); sl@0: } sl@0: } sl@0: sl@0: void CActionIncremental::DoDoPerformActionL() sl@0: { sl@0: __UHEAP_MARK; sl@0: iResult = ETrue; sl@0: TPtr8 eResultActual = iEResult->Des(); sl@0: eResultActual.FillZ(eResultActual.MaxLength()); sl@0: eResultActual.SetLength(0); sl@0: sl@0: TPtr8 dResultActual = iDResult->Des(); sl@0: dResultActual.FillZ(dResultActual.MaxLength()); sl@0: dResultActual.SetLength(0); sl@0: sl@0: TInt len = iInput->Length(); sl@0: for(TInt i=1; iProcess(iInput->Mid(j,i), eResultActual); sl@0: } sl@0: sl@0: iEncryptor->ProcessFinalL(iInput->Mid(j), eResultActual); sl@0: sl@0: j=0; sl@0: for(; (j+(len-i))Process(eResultActual.Mid(j, len-i), dResultActual); sl@0: } sl@0: sl@0: iDecryptor->ProcessFinalL(eResultActual.Mid(j), dResultActual); sl@0: sl@0: if(dResultActual != *iInput) sl@0: { sl@0: iResult = EFalse; sl@0: } sl@0: sl@0: eResultActual.FillZ(eResultActual.MaxLength()); sl@0: dResultActual.FillZ(dResultActual.MaxLength()); sl@0: eResultActual.SetLength(0); sl@0: dResultActual.SetLength(0); sl@0: } sl@0: __UHEAP_MARKEND; sl@0: sl@0: } sl@0: