1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/source/symmetric/rc2shim.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,204 @@
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 "rc2shim.h"
1.23 +
1.24 +#include <cryptospi/cryptoparams.h>
1.25 +#include <cryptospi/cryptosymmetriccipherapi.h>
1.26 +#include <cryptospi/cryptospidef.h>
1.27 +#include <cryptospi/plugincharacteristics.h>
1.28 +#include <cryptospi/keys.h>
1.29 +#include <cryptostrength.h>
1.30 +
1.31 +#include "../common/inlines.h"
1.32 +
1.33 +using namespace CryptoSpi;
1.34 +
1.35 +// CRC2EncryptorShim ////////////////////////////////////////////////////////
1.36 +CRC2EncryptorShim* CRC2EncryptorShim::NewL(const TDesC8& aKey, TInt aEffectiveKeyLenBits)
1.37 + {
1.38 + CRC2EncryptorShim* self = CRC2EncryptorShim::NewLC(aKey, aEffectiveKeyLenBits);
1.39 + CleanupStack::Pop(self);
1.40 + return self;
1.41 + }
1.42 +
1.43 +CRC2EncryptorShim* CRC2EncryptorShim::NewLC(const TDesC8& aKey, TInt aEffectiveKeyLenBits)
1.44 + {
1.45 + CRC2EncryptorShim* self = new (ELeave) CRC2EncryptorShim();
1.46 + CleanupStack::PushL(self);
1.47 + self->ConstructL(aKey, aEffectiveKeyLenBits);
1.48 + // weak enough if either aKey or aEffectiveKeyLenBits is weak
1.49 + TInt minKeySize = Min(aEffectiveKeyLenBits, BytesToBits(aKey.Size()));
1.50 + TCrypto::IsSymmetricWeakEnoughL(minKeySize);
1.51 + return self;
1.52 + }
1.53 +
1.54 +CRC2EncryptorShim::CRC2EncryptorShim()
1.55 + {
1.56 + }
1.57 +
1.58 +CRC2EncryptorShim::~CRC2EncryptorShim()
1.59 + {
1.60 + delete iSymmetricCipherImpl;
1.61 + delete iKey;
1.62 + delete iAlgorithmParams;
1.63 + }
1.64 +
1.65 +void CRC2EncryptorShim::ConstructL(const TDesC8& aKey, TInt aEffectiveKeyLenBits)
1.66 + {
1.67 + TKeyProperty keyProperty = {KRc2Uid, KNullUid, KSymmetricKeyUid, KNonEmbeddedKeyUid};
1.68 + CCryptoParams* keyParam =CCryptoParams::NewLC();
1.69 + keyParam->AddL(aKey, KSymmetricKeyParameterUid);
1.70 + iKey=CKey::NewL(keyProperty, *keyParam);
1.71 + CleanupStack::PopAndDestroy(keyParam);
1.72 +
1.73 + iAlgorithmParams = CCryptoParams::NewL();
1.74 + iAlgorithmParams->AddL(aEffectiveKeyLenBits, KRC2EffectiveKeyLenBits);
1.75 +
1.76 + CSymmetricCipherFactory::CreateSymmetricCipherL(
1.77 + iSymmetricCipherImpl,
1.78 + KRc2Uid,
1.79 + *iKey,
1.80 + KCryptoModeEncryptUid,
1.81 + KOperationModeECBUid,
1.82 + KPaddingModeNoneUid,
1.83 + iAlgorithmParams);
1.84 + }
1.85 +
1.86 +TInt CRC2EncryptorShim::BlockSize() const
1.87 + {
1.88 + // SPI returns block size in BITS
1.89 + return BitsToBytes(iSymmetricCipherImpl->BlockSize());
1.90 + }
1.91 +
1.92 +TInt CRC2EncryptorShim::KeySize() const
1.93 + {
1.94 + return iSymmetricCipherImpl->KeySize();
1.95 + }
1.96 +
1.97 +void CRC2EncryptorShim::Transform(TDes8& aBlock)
1.98 + {
1.99 + iOutputBlock.Zero();
1.100 + TRAP_IGNORE(iSymmetricCipherImpl->ProcessL(aBlock, iOutputBlock);)
1.101 + aBlock = iOutputBlock;
1.102 + }
1.103 +
1.104 +void CRC2EncryptorShim::Reset()
1.105 + {
1.106 + iSymmetricCipherImpl->Reset();
1.107 + }
1.108 +
1.109 +TInt CRC2EncryptorShim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/)
1.110 + {
1.111 + TInt ret(KErrExtensionNotSupported);
1.112 +
1.113 + if (KSymmetricCipherInterface == aExtensionId && iSymmetricCipherImpl)
1.114 + {
1.115 + a0=iSymmetricCipherImpl;
1.116 + ret=KErrNone;
1.117 + }
1.118 + return ret;
1.119 + }
1.120 +
1.121 +// CRC2DecryptorShim ////////////////////////////////////////////////////////
1.122 +CRC2DecryptorShim* CRC2DecryptorShim::NewL(const TDesC8& aKey, TInt aEffectiveKeyLenBits)
1.123 + {
1.124 + CRC2DecryptorShim* self = CRC2DecryptorShim::NewLC(aKey, aEffectiveKeyLenBits);
1.125 + CleanupStack::Pop(self);
1.126 + return self;
1.127 + }
1.128 +
1.129 +
1.130 +CRC2DecryptorShim* CRC2DecryptorShim::NewLC(const TDesC8& aKey, TInt aEffectiveKeyLenBits)
1.131 + {
1.132 + CRC2DecryptorShim* self = new (ELeave) CRC2DecryptorShim();
1.133 + CleanupStack::PushL(self);
1.134 + self->ConstructL(aKey, aEffectiveKeyLenBits);
1.135 + // weak enough if either aKey or aEffectiveKeyLenBits is weak
1.136 + TInt minKeySize = Min(aEffectiveKeyLenBits, BytesToBits(aKey.Size()));
1.137 + TCrypto::IsSymmetricWeakEnoughL(minKeySize);
1.138 + return self;
1.139 + }
1.140 +
1.141 +CRC2DecryptorShim::CRC2DecryptorShim()
1.142 + {
1.143 + }
1.144 +
1.145 +CRC2DecryptorShim::~CRC2DecryptorShim()
1.146 + {
1.147 + delete iSymmetricCipherImpl;
1.148 + delete iKey;
1.149 + delete iAlgorithmParams;
1.150 + }
1.151 +
1.152 +
1.153 +void CRC2DecryptorShim::ConstructL(const TDesC8& aKey, TInt aEffectiveKeyLenBits)
1.154 + {
1.155 + TKeyProperty keyProperty = {KRc2Uid, KNullUid, KSymmetricKeyUid, KNonEmbeddedKeyUid};
1.156 + CCryptoParams* keyParam =CCryptoParams::NewLC();
1.157 + keyParam->AddL(aKey, KSymmetricKeyParameterUid);
1.158 + iKey=CKey::NewL(keyProperty, *keyParam);
1.159 + CleanupStack::PopAndDestroy(keyParam);
1.160 +
1.161 + iAlgorithmParams = CCryptoParams::NewL();
1.162 + iAlgorithmParams->AddL(aEffectiveKeyLenBits, KRC2EffectiveKeyLenBits);
1.163 +
1.164 + CSymmetricCipherFactory::CreateSymmetricCipherL(
1.165 + iSymmetricCipherImpl,
1.166 + KRc2Uid,
1.167 + *iKey,
1.168 + KCryptoModeDecryptUid,
1.169 + KOperationModeECBUid,
1.170 + KPaddingModeNoneUid,
1.171 + iAlgorithmParams);
1.172 + }
1.173 +
1.174 +TInt CRC2DecryptorShim::BlockSize() const
1.175 + {
1.176 + // SPI returns block size in BITS
1.177 + return BitsToBytes(iSymmetricCipherImpl->BlockSize());
1.178 + }
1.179 +
1.180 +TInt CRC2DecryptorShim::KeySize() const
1.181 + {
1.182 + return iSymmetricCipherImpl->KeySize();
1.183 + }
1.184 +
1.185 +void CRC2DecryptorShim::Transform(TDes8& aBlock)
1.186 + {
1.187 + iOutputBlock.Zero();
1.188 + TRAP_IGNORE(iSymmetricCipherImpl->ProcessL(aBlock, iOutputBlock);)
1.189 + aBlock = iOutputBlock;
1.190 + }
1.191 +
1.192 +void CRC2DecryptorShim::Reset()
1.193 + {
1.194 + iSymmetricCipherImpl->Reset();
1.195 + }
1.196 +
1.197 +TInt CRC2DecryptorShim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/)
1.198 + {
1.199 + TInt ret(KErrExtensionNotSupported);
1.200 +
1.201 + if (CryptoSpi::KSymmetricCipherInterface == aExtensionId && iSymmetricCipherImpl)
1.202 + {
1.203 + a0=iSymmetricCipherImpl;
1.204 + ret=KErrNone;
1.205 + }
1.206 + return ret;
1.207 + }