sl@0: /* sl@0: * Copyright (c) 2002-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 sl@0: sl@0: #include "cbcmodeshim.h" sl@0: #include "../common/inlines.h" sl@0: sl@0: void CBlockChainingMode::Reset() sl@0: { sl@0: iRegister.Copy(iIV); sl@0: iBT->Reset(); sl@0: } sl@0: sl@0: TInt CBlockChainingMode::BlockSize() const sl@0: { sl@0: return (iBT->BlockSize()); sl@0: } sl@0: sl@0: TInt CBlockChainingMode::KeySize() const sl@0: { sl@0: return (iBT->KeySize()); sl@0: } sl@0: sl@0: void CBlockChainingMode::SetIV(const TDesC8& aIV) sl@0: { sl@0: //We are making the stipulation that anybody calling SetIV is not setting it sl@0: //to a longer IV than they originally did. Otherwise SetIV needs to leave. sl@0: assert(aIV.Size() <= iIV.Size()); sl@0: iIV.Copy(aIV); sl@0: Reset(); sl@0: } sl@0: sl@0: EXPORT_C CBlockChainingMode::CBlockChainingMode() sl@0: : iBT(NULL), iRegister(0,0,0), iIV(0,0,0) sl@0: { sl@0: } sl@0: sl@0: EXPORT_C CBlockChainingMode::~CBlockChainingMode() sl@0: { sl@0: delete iBT; sl@0: delete iRegisterBuf; sl@0: delete iIVBuf; sl@0: } sl@0: sl@0: EXPORT_C void CBlockChainingMode::ConstructL(CBlockTransformation* aBT, const TDesC8& aIV) sl@0: { sl@0: iRegisterBuf = aIV.AllocL(); sl@0: iRegister.Set(iRegisterBuf->Des()); sl@0: iIVBuf = aIV.AllocL(); sl@0: iIV.Set(iIVBuf->Des()); sl@0: sl@0: // Take ownership last - doesn't take ownership if we leave sl@0: iBT = aBT; sl@0: } sl@0: sl@0: /* CModeCBCEncryptor */ sl@0: EXPORT_C CModeCBCEncryptor* CModeCBCEncryptor::NewL(CBlockTransformation* aBT, sl@0: const TDesC8& aIV) sl@0: { sl@0: CModeCBCEncryptor* self = CModeCBCEncryptorShim::NewL(aBT, aIV); sl@0: if (! self) sl@0: { sl@0: // not able to use CryptoSpi, possibly due to an exterally sl@0: // derived legacy class so fallback to old implementation. sl@0: self = NewLC(aBT,aIV); sl@0: CleanupStack::Pop(self); sl@0: } sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CModeCBCEncryptor* CModeCBCEncryptor::NewLC(CBlockTransformation* aBT, sl@0: const TDesC8& aIV) sl@0: { sl@0: CModeCBCEncryptor* self = new (ELeave)CModeCBCEncryptor(); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aBT, aIV); sl@0: return self; sl@0: } sl@0: sl@0: CModeCBCEncryptor::CModeCBCEncryptor() sl@0: { sl@0: } sl@0: sl@0: void CModeCBCEncryptor::Transform(TDes8& aBlock) sl@0: { sl@0: assert(aBlock.Size() == iBT->BlockSize()); sl@0: assert(iRegister.Size() == aBlock.Size()); sl@0: sl@0: XorBuf(const_cast(iRegister.Ptr()), aBlock.Ptr(), aBlock.Size()); sl@0: iBT->Transform(iRegister); sl@0: aBlock.Copy(iRegister); sl@0: } sl@0: sl@0: /* CModeCBCDecryptor */ sl@0: EXPORT_C CModeCBCDecryptor* CModeCBCDecryptor::NewL(CBlockTransformation* aBT, sl@0: const TDesC8& aIV) sl@0: { sl@0: CModeCBCDecryptor* self = CModeCBCDecryptorShim::NewL(aBT, aIV); sl@0: if (! self) sl@0: { sl@0: // not able to use CryptoSpi, possibly due to an exterally sl@0: // derived legacy class so fallback to old implementation. sl@0: self = NewLC(aBT,aIV); sl@0: CleanupStack::Pop(self); sl@0: } sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CModeCBCDecryptor* CModeCBCDecryptor::NewLC(CBlockTransformation* aBT, sl@0: const TDesC8& aIV) sl@0: { sl@0: CModeCBCDecryptor* self = new (ELeave)CModeCBCDecryptor(); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aBT, aIV); sl@0: return self; sl@0: } sl@0: sl@0: void CModeCBCDecryptor::ConstructL(CBlockTransformation* aBT, const TDesC8& aIV) sl@0: { sl@0: iIVBakBuf = aIV.AllocL(); sl@0: iIVBak.Set(iIVBakBuf->Des()); sl@0: CBlockChainingMode::ConstructL(aBT, aIV); sl@0: } sl@0: sl@0: CModeCBCDecryptor::~CModeCBCDecryptor(void) sl@0: { sl@0: delete iIVBakBuf; sl@0: } sl@0: sl@0: CModeCBCDecryptor::CModeCBCDecryptor() sl@0: : iIVBak(0,0,0) sl@0: { sl@0: } sl@0: sl@0: void CModeCBCDecryptor::Transform(TDes8& aBlock) sl@0: { sl@0: assert(aBlock.Size() == iBT->BlockSize()); sl@0: assert(iRegister.Size() == aBlock.Size()); sl@0: assert(iIVBak.Size() == aBlock.Size()); sl@0: sl@0: // Take a copy of incoming block sl@0: iIVBak.Copy(aBlock); sl@0: sl@0: // transform the block sl@0: iBT->Transform(aBlock); sl@0: sl@0: // xor the output with the register sl@0: XorBuf(const_cast(aBlock.Ptr()), iRegister.Ptr(), sl@0: aBlock.Size()); sl@0: sl@0: // Update the register to be the original block sl@0: iRegister.Copy(iIVBak); sl@0: } sl@0: