sl@0: /* sl@0: * Copyright (c) 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: #include "tactionincrementallegacy.h" sl@0: #include "symmetric.h" sl@0: #include "des.h" sl@0: #include "securityerr.h" sl@0: sl@0: CTestAction* CActionIncrementalLegacy::NewL(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CTestAction* self = CActionIncrementalLegacy::NewLC(aFs, aConsole, sl@0: aOut, aTestActionSpec); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: CTestAction* CActionIncrementalLegacy::NewLC(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CActionIncrementalLegacy* self = new(ELeave) CActionIncrementalLegacy(aFs, aConsole, aOut); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aTestActionSpec); sl@0: return self; sl@0: } sl@0: sl@0: CActionIncrementalLegacy::~CActionIncrementalLegacy() sl@0: { sl@0: } sl@0: sl@0: CActionIncrementalLegacy::CActionIncrementalLegacy(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut) sl@0: : CCryptoTestAction(aFs, aConsole, aOut) sl@0: {} sl@0: sl@0: void CActionIncrementalLegacy::DoPerformPostrequisiteL() sl@0: { sl@0: if (iEncryptor) sl@0: { sl@0: iEncryptor->Reset(); sl@0: delete iEncryptor; sl@0: } sl@0: sl@0: if (iDecryptor) sl@0: { sl@0: iDecryptor->Reset(); sl@0: delete iDecryptor; sl@0: } sl@0: } sl@0: sl@0: void CActionIncrementalLegacy::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::NewL(iKey->Des()); sl@0: CleanupStack::PushL(encryptor); 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: sl@0: desEncryptor = CDESEncryptor::NewL(iKey->Des()); sl@0: CleanupStack::PushL(desEncryptor); sl@0: desDecryptor = CDESDecryptor::NewL(iKey->Des()); sl@0: CleanupStack::PushL(desDecryptor); sl@0: sl@0: encryptor = CModeCBCEncryptor::NewL(desEncryptor, iIV->Des()); sl@0: CleanupStack::PushL(encryptor); sl@0: decryptor = CModeCBCDecryptor::NewLC(desDecryptor, iIV->Des()); sl@0: CleanupStack::Pop(4); sl@0: } sl@0: break; sl@0: case (E3DESECB): sl@0: { sl@0: encryptor = C3DESEncryptor::NewL(iKey->Des()); sl@0: CleanupStack::PushL(encryptor); 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::NewLC(the3DESdecryptor, iIV->Des()); sl@0: CleanupStack::Pop(4); sl@0: } sl@0: break; sl@0: case (EAESECB): sl@0: { sl@0: encryptor = CAESEncryptor::NewL(iKey->Des()); sl@0: CleanupStack::PushL(encryptor); 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::NewL(iKey->Des(), iEffectiveKeyLen); sl@0: CleanupStack::PushL(encryptor); 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::NewLC(*iKey, 0); sl@0: CleanupStack::Pop(iDecryptor); sl@0: } sl@0: break; sl@0: case (ECipherNull): sl@0: { sl@0: iEncryptor = CNullCipher::NewL(); sl@0: iDecryptor = CNullCipher::NewLC(); sl@0: CleanupStack::Pop(iDecryptor); 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: sl@0: iEncryptor = CBufferedEncryptor::NewLC(encryptor, ePadding); sl@0: CleanupStack::Pop(2, ePadding); sl@0: iDecryptor = CBufferedDecryptor::NewLC(decryptor, dPadding); sl@0: CleanupStack::Pop(2, dPadding); sl@0: sl@0: TInt desEBlockSize = encryptor->BlockSize(); sl@0: TInt desDBlockSize = decryptor->BlockSize(); sl@0: TInt bufEBlockSize = iEncryptor->BlockSize(); sl@0: TInt bufDBlockSize = iDecryptor->BlockSize(); sl@0: ASSERT((desEBlockSize/desDBlockSize) == (bufEBlockSize/bufDBlockSize)); sl@0: sl@0: TInt desEKeySize = encryptor->KeySize(); sl@0: TInt desDKeySize = decryptor->KeySize(); sl@0: ASSERT(desEKeySize == desDKeySize); sl@0: sl@0: encryptor->Reset(); sl@0: decryptor->Reset(); sl@0: } sl@0: sl@0: TInt encryptorKeySize = iEncryptor->KeySize(); sl@0: TInt decryptorKeySize = iDecryptor->KeySize(); sl@0: ASSERT(encryptorKeySize == decryptorKeySize); 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 CActionIncrementalLegacy::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 CActionIncrementalLegacy::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: