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 "tactionvector.h" sl@0: #include "symmetric.h" sl@0: sl@0: CTestAction* CActionVector::NewL(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CTestAction* self = CActionVector::NewLC(aFs, aConsole, sl@0: aOut, aTestActionSpec); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: CTestAction* CActionVector::NewLC(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CActionVector* self = new(ELeave) CActionVector(aFs, aConsole, aOut); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aTestActionSpec); sl@0: return self; sl@0: } sl@0: sl@0: CActionVector::~CActionVector() sl@0: { sl@0: delete iEncryptor; sl@0: delete iDecryptor; sl@0: } sl@0: sl@0: CActionVector::CActionVector(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut) sl@0: sl@0: : CCryptoTestAction(aFs, aConsole, aOut) sl@0: {} sl@0: sl@0: sl@0: void CActionVector::DoPerformPrerequisiteL() sl@0: { sl@0: TInt err = KErrNone; sl@0: TInt pos = 0; sl@0: TPtrC8 vector = Input::ParseElement(*iBody, KVectorStart, KVectorEnd, pos, err); sl@0: sl@0: DoInputParseL(vector); sl@0: sl@0: CBlockTransformation* encryptor = NULL; sl@0: CBlockTransformation* decryptor = NULL; sl@0: iEncryptor = 0; sl@0: iDecryptor = 0; sl@0: sl@0: switch (iCipherType) sl@0: { sl@0: case (EDESECB): 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: 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: sl@0: CleanupStack::Pop(3, desEncryptor); 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: sl@0: CleanupStack::Pop(3, the3DESencryptor); 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: 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: CBlockTransformation* theRC2encryptor = NULL; sl@0: CBlockTransformation* theRC2decryptor = NULL; sl@0: sl@0: theRC2encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen); sl@0: theRC2decryptor = CRC2Decryptor::NewLC(iKey->Des(), iEffectiveKeyLen); sl@0: sl@0: encryptor = CModeCBCEncryptor::NewL(theRC2encryptor, iIV->Des()); sl@0: CleanupStack::PushL(encryptor); sl@0: decryptor = CModeCBCDecryptor::NewL(theRC2decryptor, iIV->Des()); sl@0: sl@0: CleanupStack::Pop(3, theRC2encryptor); 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: sl@0: default: sl@0: { sl@0: ASSERT(0); sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: } sl@0: sl@0: CleanupStack::PushL(encryptor); sl@0: CleanupStack::PushL(decryptor); sl@0: 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: iEResult = HBufC8::NewMaxL(iEncryptor->MaxOutputLength(iInput->Length())); sl@0: iDResult = HBufC8::NewMaxL(iDecryptor->MaxOutputLength(iEResult->Size())); sl@0: sl@0: CleanupStack::Pop(2, encryptor); sl@0: } sl@0: sl@0: void CActionVector::DoPerformActionL() sl@0: { sl@0: // First perform the test blockwise (ie passing in one block at a time) sl@0: TUint blockSize = iEncryptor->BlockSize(); sl@0: TUint index = 0; sl@0: while (index <= (iInput->Size() - blockSize)) sl@0: { sl@0: TPtr8 theEncryptResult((TUint8*)&(iEResult->operator[](index)), blockSize, blockSize); sl@0: theEncryptResult.FillZ(theEncryptResult.MaxLength()); sl@0: theEncryptResult.SetLength(0); sl@0: sl@0: TPtrC8 theEncryptInput(iInput->Mid(index, blockSize)); sl@0: iEncryptor->Process(theEncryptInput, theEncryptResult); sl@0: sl@0: if (iOutput->Mid(index, blockSize) == theEncryptResult) sl@0: iResult = ETrue; sl@0: else sl@0: break; // No point doing any more sl@0: sl@0: index+=blockSize; sl@0: } sl@0: sl@0: if (*iOutput==*iEResult) sl@0: { sl@0: iResult = ETrue; sl@0: sl@0: index = 0; sl@0: while (index <= (iEResult->Size()- blockSize)) sl@0: { sl@0: TPtr8 theDecryptResult((TUint8*)&(iDResult->operator[](index)), blockSize, blockSize); sl@0: theDecryptResult.FillZ(theDecryptResult.MaxLength()); sl@0: theDecryptResult.SetLength(0); sl@0: sl@0: TPtrC8 theDecryptInput(iEResult->Mid(index, blockSize)); sl@0: iDecryptor->Process(theDecryptInput, theDecryptResult); sl@0: sl@0: if(iInput->Mid(index, blockSize) != theDecryptResult) sl@0: { sl@0: iResult = EFalse; sl@0: break; // No point doing any more sl@0: } sl@0: sl@0: index+=blockSize; sl@0: } sl@0: sl@0: if (*iInput!=*iDResult) sl@0: iResult = EFalse; sl@0: } sl@0: sl@0: sl@0: iEncryptor->Reset(); sl@0: iDecryptor->Reset(); sl@0: // Now, if input is longer than a single block, repeat passing all the data sl@0: if ((TUint)iInput->Size() > blockSize) sl@0: { sl@0: TPtr8 theEncryptResult(iEResult->Des()); sl@0: theEncryptResult.FillZ(theEncryptResult.MaxLength()); sl@0: theEncryptResult.SetLength(0); sl@0: sl@0: TPtrC8 theInput(*iInput); sl@0: iEncryptor->Process(theInput, theEncryptResult); sl@0: sl@0: if (*iOutput!=*iEResult) sl@0: { sl@0: iResult = EFalse; sl@0: } sl@0: else sl@0: { sl@0: TPtr8 theDecryptResult(iDResult->Des()); sl@0: theDecryptResult.FillZ(theDecryptResult.MaxLength()); sl@0: theDecryptResult.SetLength(0); sl@0: sl@0: iDecryptor->Process(*iEResult, theDecryptResult); sl@0: if (*iInput!=*iDResult) sl@0: iResult = EFalse; sl@0: } sl@0: } sl@0: } sl@0: