1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/source/symmetric/cbcmode.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,170 @@
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 +
1.24 +#include "cbcmodeshim.h"
1.25 +#include "../common/inlines.h"
1.26 +
1.27 +void CBlockChainingMode::Reset()
1.28 + {
1.29 + iRegister.Copy(iIV);
1.30 + iBT->Reset();
1.31 + }
1.32 +
1.33 +TInt CBlockChainingMode::BlockSize() const
1.34 + {
1.35 + return (iBT->BlockSize());
1.36 + }
1.37 +
1.38 +TInt CBlockChainingMode::KeySize() const
1.39 + {
1.40 + return (iBT->KeySize());
1.41 + }
1.42 +
1.43 +void CBlockChainingMode::SetIV(const TDesC8& aIV)
1.44 + {
1.45 + //We are making the stipulation that anybody calling SetIV is not setting it
1.46 + //to a longer IV than they originally did. Otherwise SetIV needs to leave.
1.47 + assert(aIV.Size() <= iIV.Size());
1.48 + iIV.Copy(aIV);
1.49 + Reset();
1.50 + }
1.51 +
1.52 +EXPORT_C CBlockChainingMode::CBlockChainingMode()
1.53 + : iBT(NULL), iRegister(0,0,0), iIV(0,0,0)
1.54 + {
1.55 + }
1.56 +
1.57 +EXPORT_C CBlockChainingMode::~CBlockChainingMode()
1.58 + {
1.59 + delete iBT;
1.60 + delete iRegisterBuf;
1.61 + delete iIVBuf;
1.62 + }
1.63 +
1.64 +EXPORT_C void CBlockChainingMode::ConstructL(CBlockTransformation* aBT, const TDesC8& aIV)
1.65 + {
1.66 + iRegisterBuf = aIV.AllocL();
1.67 + iRegister.Set(iRegisterBuf->Des());
1.68 + iIVBuf = aIV.AllocL();
1.69 + iIV.Set(iIVBuf->Des());
1.70 +
1.71 + // Take ownership last - doesn't take ownership if we leave
1.72 + iBT = aBT;
1.73 + }
1.74 +
1.75 +/* CModeCBCEncryptor */
1.76 +EXPORT_C CModeCBCEncryptor* CModeCBCEncryptor::NewL(CBlockTransformation* aBT,
1.77 + const TDesC8& aIV)
1.78 + {
1.79 + CModeCBCEncryptor* self = CModeCBCEncryptorShim::NewL(aBT, aIV);
1.80 + if (! self)
1.81 + {
1.82 + // not able to use CryptoSpi, possibly due to an exterally
1.83 + // derived legacy class so fallback to old implementation.
1.84 + self = NewLC(aBT,aIV);
1.85 + CleanupStack::Pop(self);
1.86 + }
1.87 + return self;
1.88 + }
1.89 +
1.90 +EXPORT_C CModeCBCEncryptor* CModeCBCEncryptor::NewLC(CBlockTransformation* aBT,
1.91 + const TDesC8& aIV)
1.92 + {
1.93 + CModeCBCEncryptor* self = new (ELeave)CModeCBCEncryptor();
1.94 + CleanupStack::PushL(self);
1.95 + self->ConstructL(aBT, aIV);
1.96 + return self;
1.97 + }
1.98 +
1.99 +CModeCBCEncryptor::CModeCBCEncryptor()
1.100 + {
1.101 + }
1.102 +
1.103 +void CModeCBCEncryptor::Transform(TDes8& aBlock)
1.104 + {
1.105 + assert(aBlock.Size() == iBT->BlockSize());
1.106 + assert(iRegister.Size() == aBlock.Size());
1.107 +
1.108 + XorBuf(const_cast<TUint8*>(iRegister.Ptr()), aBlock.Ptr(), aBlock.Size());
1.109 + iBT->Transform(iRegister);
1.110 + aBlock.Copy(iRegister);
1.111 + }
1.112 +
1.113 +/* CModeCBCDecryptor */
1.114 +EXPORT_C CModeCBCDecryptor* CModeCBCDecryptor::NewL(CBlockTransformation* aBT,
1.115 + const TDesC8& aIV)
1.116 + {
1.117 + CModeCBCDecryptor* self = CModeCBCDecryptorShim::NewL(aBT, aIV);
1.118 + if (! self)
1.119 + {
1.120 + // not able to use CryptoSpi, possibly due to an exterally
1.121 + // derived legacy class so fallback to old implementation.
1.122 + self = NewLC(aBT,aIV);
1.123 + CleanupStack::Pop(self);
1.124 + }
1.125 + return self;
1.126 + }
1.127 +
1.128 +EXPORT_C CModeCBCDecryptor* CModeCBCDecryptor::NewLC(CBlockTransformation* aBT,
1.129 + const TDesC8& aIV)
1.130 + {
1.131 + CModeCBCDecryptor* self = new (ELeave)CModeCBCDecryptor();
1.132 + CleanupStack::PushL(self);
1.133 + self->ConstructL(aBT, aIV);
1.134 + return self;
1.135 + }
1.136 +
1.137 +void CModeCBCDecryptor::ConstructL(CBlockTransformation* aBT, const TDesC8& aIV)
1.138 + {
1.139 + iIVBakBuf = aIV.AllocL();
1.140 + iIVBak.Set(iIVBakBuf->Des());
1.141 + CBlockChainingMode::ConstructL(aBT, aIV);
1.142 + }
1.143 +
1.144 +CModeCBCDecryptor::~CModeCBCDecryptor(void)
1.145 + {
1.146 + delete iIVBakBuf;
1.147 + }
1.148 +
1.149 +CModeCBCDecryptor::CModeCBCDecryptor()
1.150 + : iIVBak(0,0,0)
1.151 + {
1.152 + }
1.153 +
1.154 +void CModeCBCDecryptor::Transform(TDes8& aBlock)
1.155 + {
1.156 + assert(aBlock.Size() == iBT->BlockSize());
1.157 + assert(iRegister.Size() == aBlock.Size());
1.158 + assert(iIVBak.Size() == aBlock.Size());
1.159 +
1.160 + // Take a copy of incoming block
1.161 + iIVBak.Copy(aBlock);
1.162 +
1.163 + // transform the block
1.164 + iBT->Transform(aBlock);
1.165 +
1.166 + // xor the output with the register
1.167 + XorBuf(const_cast<TUint8*>(aBlock.Ptr()), iRegister.Ptr(),
1.168 + aBlock.Size());
1.169 +
1.170 + // Update the register to be the original block
1.171 + iRegister.Copy(iIVBak);
1.172 +}
1.173 +