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