1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/source/symmetric/cbcmodeshim.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,214 @@
1.4 +/*
1.5 +* Copyright (c) 2006-2010 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 "cbcmodeshim.h"
1.23 +
1.24 +#include <cryptopanic.h>
1.25 +#include <cryptospi/cryptospidef.h>
1.26 +#include <padding.h>
1.27 +#include <cryptospi/cryptosymmetriccipherapi.h>
1.28 +#include <cryptospi/plugincharacteristics.h>
1.29 +#include "../common/inlines.h"
1.30 +
1.31 +// CModeCBCEncryptorShim
1.32 +CModeCBCEncryptorShim::CModeCBCEncryptorShim(CryptoSpi::CSymmetricCipher* aSymmetricCipherImpl) :
1.33 + iSymmetricCipherImpl(aSymmetricCipherImpl)
1.34 + {
1.35 + }
1.36 +
1.37 +CModeCBCEncryptorShim* CModeCBCEncryptorShim::NewL(CBlockTransformation* aBT, const TDesC8& aIv)
1.38 + {
1.39 + CModeCBCEncryptorShim* self(0);
1.40 +
1.41 + // Check whether the block transform contains an SPI plug-in
1.42 + TAny* implPtr(0);
1.43 + TInt err = aBT->GetExtension(CryptoSpi::KSymmetricCipherInterface, implPtr, NULL);
1.44 + if (err == KErrNone && implPtr)
1.45 + {
1.46 + CryptoSpi::CSymmetricCipher* impl(static_cast<CryptoSpi::CSymmetricCipher*>(implPtr));
1.47 +
1.48 + const CryptoSpi::TCharacteristics* c(0);
1.49 + impl->GetCharacteristicsL(c);
1.50 +
1.51 + const CryptoSpi::TSymmetricCipherCharacteristics* cipherCharacteristics(
1.52 + static_cast<const CryptoSpi::TSymmetricCipherCharacteristics*>(c));
1.53 +
1.54 + // Verify that the plug-in supports CBC mode
1.55 + if (err == KErrNone &&
1.56 + cipherCharacteristics->IsOperationModeSupported(CryptoSpi::KOperationModeCBCUid))
1.57 + {
1.58 + // Set block transform to encrypt-cbc
1.59 + impl->SetCryptoModeL(CryptoSpi::KCryptoModeEncryptUid);
1.60 + impl->SetOperationModeL(CryptoSpi::KOperationModeCBCUid);
1.61 + impl->SetIvL(aIv);
1.62 + self = new(ELeave) CModeCBCEncryptorShim(impl);
1.63 + CleanupStack::PushL(self);
1.64 + self->ConstructL(aBT, aIv);
1.65 + CleanupStack::Pop(self);
1.66 + }
1.67 + }
1.68 + return self;
1.69 + }
1.70 +
1.71 +void CModeCBCEncryptorShim::ConstructL(CBlockTransformation* aBT, const TDesC8& aIv)
1.72 + {
1.73 + CModeCBCEncryptor::ConstructL(aBT, aIv);
1.74 + }
1.75 +
1.76 +void CModeCBCEncryptorShim::Reset()
1.77 + {
1.78 + iSymmetricCipherImpl->Reset();
1.79 + }
1.80 +
1.81 +TInt CModeCBCEncryptorShim::BlockSize() const
1.82 + {
1.83 + return BitsToBytes(iSymmetricCipherImpl->BlockSize());
1.84 + }
1.85 +
1.86 +TInt CModeCBCEncryptorShim::KeySize() const
1.87 + {
1.88 + return iSymmetricCipherImpl->KeySize();
1.89 + }
1.90 +
1.91 +void CModeCBCEncryptorShim::Transform(TDes8& aBlock)
1.92 + {
1.93 + // This function will never get called if a buffered
1.94 + // encryptor is used because Process and ProcessFinalL call
1.95 + // iSymmetricCipherImpl directly
1.96 + iBT->Transform(aBlock);
1.97 + }
1.98 +
1.99 +void CModeCBCEncryptorShim::SetIV(const TDesC8& aIv)
1.100 + {
1.101 + TRAPD(err, iSymmetricCipherImpl->SetIvL(aIv));
1.102 + if (err == KErrOverflow)
1.103 + {
1.104 + User::Panic(KCryptoPanic, ECryptoPanicInputTooLarge);
1.105 + }
1.106 + else if (err != KErrNone)
1.107 + {
1.108 + // SetIvL should only leave if the aIv is incorrect
1.109 + User::Panic(KCryptoPanic, KErrArgument);
1.110 + }
1.111 + }
1.112 +
1.113 +TInt CModeCBCEncryptorShim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/)
1.114 + {
1.115 + TInt ret(KErrExtensionNotSupported);
1.116 +
1.117 + if (CryptoSpi::KSymmetricCipherInterface == aExtensionId)
1.118 + {
1.119 + a0=iSymmetricCipherImpl;
1.120 + ret=KErrNone;
1.121 + }
1.122 + return ret;
1.123 + }
1.124 +
1.125 +// CModeCBCDecryptorShim
1.126 +CModeCBCDecryptorShim::CModeCBCDecryptorShim(CryptoSpi::CSymmetricCipher* aSymmetricCipherImpl) :
1.127 + iSymmetricCipherImpl(aSymmetricCipherImpl)
1.128 + {
1.129 + }
1.130 +
1.131 +CModeCBCDecryptorShim* CModeCBCDecryptorShim::NewL(CBlockTransformation* aBT, const TDesC8& aIv)
1.132 + {
1.133 + CModeCBCDecryptorShim* self(0);
1.134 +
1.135 + // Check whether the block transform contains an SPI plug-in
1.136 + TAny* implPtr(0);
1.137 + TInt err = aBT->GetExtension(CryptoSpi::KSymmetricCipherInterface, implPtr, NULL);
1.138 + if (err == KErrNone && implPtr)
1.139 + {
1.140 + CryptoSpi::CSymmetricCipher* impl(static_cast<CryptoSpi::CSymmetricCipher*>(implPtr));
1.141 +
1.142 + const CryptoSpi::TCharacteristics* c(0);
1.143 + impl->GetCharacteristicsL(c);
1.144 +
1.145 + const CryptoSpi::TSymmetricCipherCharacteristics* cipherCharacteristics(
1.146 + static_cast<const CryptoSpi::TSymmetricCipherCharacteristics*>(c));
1.147 +
1.148 + // Verify that the plug-in supports CBC mode
1.149 + if (err == KErrNone &&
1.150 + cipherCharacteristics->IsOperationModeSupported(CryptoSpi::KOperationModeCBCUid))
1.151 + {
1.152 + // Set block transform to encrypt-cbc
1.153 + impl->SetCryptoModeL(CryptoSpi::KCryptoModeDecryptUid);
1.154 + impl->SetOperationModeL(CryptoSpi::KOperationModeCBCUid);
1.155 + impl->SetIvL(aIv);
1.156 + self = new(ELeave) CModeCBCDecryptorShim(impl);
1.157 + CleanupStack::PushL(self);
1.158 + self->ConstructL(aBT, aIv);
1.159 + CleanupStack::Pop(self);
1.160 + }
1.161 + }
1.162 + return self;
1.163 + }
1.164 +
1.165 +void CModeCBCDecryptorShim::ConstructL(CBlockTransformation* aBT, const TDesC8& aIv)
1.166 + {
1.167 + CModeCBCDecryptor::ConstructL(aBT, aIv);
1.168 + }
1.169 +
1.170 +void CModeCBCDecryptorShim::Reset()
1.171 + {
1.172 + iSymmetricCipherImpl->Reset();
1.173 + }
1.174 +
1.175 +TInt CModeCBCDecryptorShim::BlockSize() const
1.176 + {
1.177 + return BitsToBytes(iSymmetricCipherImpl->BlockSize());
1.178 + }
1.179 +
1.180 +TInt CModeCBCDecryptorShim::KeySize() const
1.181 + {
1.182 + return iSymmetricCipherImpl->KeySize();
1.183 + }
1.184 +
1.185 +void CModeCBCDecryptorShim::Transform(TDes8& aBlock)
1.186 + {
1.187 + // This function will never get called if a buffered
1.188 + // encryptor is used because Process and ProcessFinalL call
1.189 + // iSymmetricCipherImpl directly
1.190 + iBT->Transform(aBlock);
1.191 + }
1.192 +
1.193 +void CModeCBCDecryptorShim::SetIV(const TDesC8& aIv)
1.194 + {
1.195 + TRAPD(err, iSymmetricCipherImpl->SetIvL(aIv));
1.196 + if (err == KErrOverflow)
1.197 + {
1.198 + User::Panic(KCryptoPanic, ECryptoPanicInputTooLarge);
1.199 + }
1.200 + else if (err != KErrNone)
1.201 + {
1.202 + // SetIvL should only leave if the aIv is incorrect
1.203 + User::Panic(KCryptoPanic, KErrArgument);
1.204 + }
1.205 + }
1.206 +
1.207 +TInt CModeCBCDecryptorShim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/)
1.208 + {
1.209 + TInt ret(KErrExtensionNotSupported);
1.210 +
1.211 + if (CryptoSpi::KSymmetricCipherInterface == aExtensionId)
1.212 + {
1.213 + a0=iSymmetricCipherImpl;
1.214 + ret=KErrNone;
1.215 + }
1.216 + return ret;
1.217 + }