os/security/crypto/weakcrypto/source/symmetric/cbcmode.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcrypto/source/symmetric/cbcmode.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,156 @@
     1.4 +/*
     1.5 +* Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include "cbcmode.h"
    1.23 +#include "../common/inlines.h"
    1.24 +
    1.25 +void CBlockChainingMode::Reset()
    1.26 +	{
    1.27 +	iRegister.Copy(iIV);
    1.28 +	iBT->Reset();
    1.29 +	}
    1.30 +
    1.31 +TInt CBlockChainingMode::BlockSize() const
    1.32 +	{
    1.33 +	return (iBT->BlockSize());
    1.34 +	}
    1.35 +
    1.36 +TInt CBlockChainingMode::KeySize() const
    1.37 +	{
    1.38 +	return (iBT->KeySize());
    1.39 +	}
    1.40 +
    1.41 +void CBlockChainingMode::SetIV(const TDesC8& aIV)
    1.42 +	{
    1.43 +	//We are making the stipulation that anybody calling SetIV is not setting it
    1.44 +	//to a longer IV than they originally did.  Otherwise SetIV needs to leave.
    1.45 +	assert(aIV.Size() <= iIV.Size());
    1.46 +	iIV.Copy(aIV);
    1.47 +	Reset();
    1.48 +	}
    1.49 +
    1.50 +EXPORT_C CBlockChainingMode::CBlockChainingMode() 
    1.51 +	: iBT(NULL), iRegister(0,0,0), iIV(0,0,0)
    1.52 +	{
    1.53 +	}
    1.54 +
    1.55 +EXPORT_C CBlockChainingMode::~CBlockChainingMode()
    1.56 +	{
    1.57 +	delete iBT;
    1.58 +	delete iRegisterBuf;
    1.59 +	delete iIVBuf;
    1.60 +	}
    1.61 +
    1.62 +EXPORT_C void CBlockChainingMode::ConstructL(CBlockTransformation* aBT, const TDesC8& aIV)
    1.63 +	{
    1.64 +	iRegisterBuf = aIV.AllocL();
    1.65 +	iRegister.Set(iRegisterBuf->Des());
    1.66 +	iIVBuf = aIV.AllocL();
    1.67 +	iIV.Set(iIVBuf->Des());
    1.68 +
    1.69 +	// Take ownership last - doesn't take ownership if we leave
    1.70 +	iBT = aBT;
    1.71 +	}
    1.72 +
    1.73 +/* CModeCBCEncryptor */
    1.74 +EXPORT_C CModeCBCEncryptor* CModeCBCEncryptor::NewL(CBlockTransformation* aBT, 
    1.75 +	const TDesC8& aIV)
    1.76 +	{
    1.77 +	CModeCBCEncryptor* self = NewLC(aBT, aIV);	
    1.78 +	CleanupStack::Pop(self);
    1.79 +	return self;
    1.80 +	}
    1.81 +
    1.82 +EXPORT_C CModeCBCEncryptor* CModeCBCEncryptor::NewLC(CBlockTransformation* aBT,
    1.83 +	const TDesC8& aIV)
    1.84 +	{
    1.85 +	CModeCBCEncryptor* self = new (ELeave)CModeCBCEncryptor();	
    1.86 +	CleanupStack::PushL(self);
    1.87 +	self->ConstructL(aBT, aIV);
    1.88 +	return self;
    1.89 +	}
    1.90 +
    1.91 +CModeCBCEncryptor::CModeCBCEncryptor()
    1.92 +	{
    1.93 +	}
    1.94 +
    1.95 +void CModeCBCEncryptor::Transform(TDes8& aBlock)
    1.96 +	{
    1.97 +	assert(aBlock.Size() == iBT->BlockSize());
    1.98 +	assert(iRegister.Size() == aBlock.Size());
    1.99 +
   1.100 +	XorBuf(const_cast<TUint8*>(iRegister.Ptr()), aBlock.Ptr(), aBlock.Size());
   1.101 +	iBT->Transform(iRegister);
   1.102 +	aBlock.Copy(iRegister);
   1.103 +	}
   1.104 +
   1.105 +/* CModeCBCDecryptor */
   1.106 +EXPORT_C CModeCBCDecryptor* CModeCBCDecryptor::NewL(CBlockTransformation* aBT, 
   1.107 +	const TDesC8& aIV)
   1.108 +	{
   1.109 +	CModeCBCDecryptor* self = NewLC(aBT, aIV);
   1.110 +	CleanupStack::Pop(self);
   1.111 +	return self;
   1.112 +	}
   1.113 +
   1.114 +EXPORT_C CModeCBCDecryptor* CModeCBCDecryptor::NewLC(CBlockTransformation* aBT, 
   1.115 +	const TDesC8& aIV)
   1.116 +	{
   1.117 +	CModeCBCDecryptor* self = new (ELeave)CModeCBCDecryptor();	
   1.118 +	CleanupStack::PushL(self);
   1.119 +	self->ConstructL(aBT, aIV);
   1.120 +	return self;	
   1.121 +	}
   1.122 +
   1.123 +void CModeCBCDecryptor::ConstructL(CBlockTransformation* aBT, const TDesC8& aIV)
   1.124 +	{
   1.125 +	iIVBakBuf = aIV.AllocL();
   1.126 +	iIVBak.Set(iIVBakBuf->Des());
   1.127 +	CBlockChainingMode::ConstructL(aBT, aIV);
   1.128 +	}
   1.129 +
   1.130 +CModeCBCDecryptor::~CModeCBCDecryptor(void)
   1.131 +	{
   1.132 +	delete iIVBakBuf;
   1.133 +	}
   1.134 +
   1.135 +CModeCBCDecryptor::CModeCBCDecryptor()
   1.136 +	: iIVBak(0,0,0)
   1.137 +	{
   1.138 +	}
   1.139 +
   1.140 +void CModeCBCDecryptor::Transform(TDes8& aBlock)
   1.141 +	{
   1.142 +	assert(aBlock.Size() == iBT->BlockSize());
   1.143 +	assert(iRegister.Size() == aBlock.Size());
   1.144 +	assert(iIVBak.Size() == aBlock.Size());
   1.145 +	
   1.146 +	// Take a copy of incoming block
   1.147 +	iIVBak.Copy(aBlock);
   1.148 +
   1.149 +	// transform the block
   1.150 +	iBT->Transform(aBlock);
   1.151 +
   1.152 +	// xor the output with the register
   1.153 +	XorBuf(const_cast<TUint8*>(aBlock.Ptr()), iRegister.Ptr(), 
   1.154 +		aBlock.Size());
   1.155 +
   1.156 +	// Update the register to be the original block
   1.157 +	iRegister.Copy(iIVBak);
   1.158 +}
   1.159 +