os/security/crypto/weakcryptospi/source/symmetric/desshim.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcryptospi/source/symmetric/desshim.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 "desshim.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 +
    1.30 +#include "../common/inlines.h"
    1.31 +
    1.32 +using namespace CryptoSpi;
    1.33 +
    1.34 +// CDESEncryptorShim ////////////////////////////////////////////////////////
    1.35 +CDESEncryptorShim* CDESEncryptorShim::NewL(const TDesC8& aKey)
    1.36 +	{
    1.37 +	CDESEncryptorShim* self = CDESEncryptorShim::NewLC(aKey);
    1.38 +	CleanupStack::Pop(self);
    1.39 +	return self;
    1.40 +	}
    1.41 +
    1.42 +CDESEncryptorShim* CDESEncryptorShim::NewLC(const TDesC8& aKey)
    1.43 +	{
    1.44 +	CDESEncryptorShim* self = new (ELeave) CDESEncryptorShim();
    1.45 +	CleanupStack::PushL(self);
    1.46 +	self->ConstructL(aKey);
    1.47 +	// DES only used 7 bits out of every key byte
    1.48 +	TCrypto::IsSymmetricWeakEnoughL(BytesToBits(aKey.Size()) - aKey.Size());
    1.49 +	return self;
    1.50 +	}
    1.51 +
    1.52 +CDESEncryptorShim::CDESEncryptorShim()
    1.53 +	{
    1.54 +	}
    1.55 +
    1.56 +CDESEncryptorShim::~CDESEncryptorShim()
    1.57 +	{
    1.58 +	delete iSymmetricCipherImpl;
    1.59 +	delete iKey;			
    1.60 +	}	
    1.61 +
    1.62 +void CDESEncryptorShim::ConstructL(const TDesC8& aKey)
    1.63 +	{
    1.64 +	TKeyProperty keyProperty = {KDesUid, KNullUid, KSymmetricKey, KNonEmbeddedKeyUid};
    1.65 +	CCryptoParams* keyParam =CCryptoParams::NewLC();
    1.66 +	keyParam->AddL(aKey, KSymmetricKeyParameterUid);
    1.67 +	iKey=CKey::NewL(keyProperty, *keyParam);
    1.68 +	CleanupStack::PopAndDestroy(keyParam);
    1.69 +	CSymmetricCipherFactory::CreateSymmetricCipherL(
    1.70 +											iSymmetricCipherImpl,
    1.71 +											KDesUid,
    1.72 +											*iKey,
    1.73 +											KCryptoModeEncryptUid,
    1.74 +											KOperationModeECBUid,
    1.75 +											KPaddingModeNoneUid,
    1.76 +											NULL);
    1.77 +	}		
    1.78 +
    1.79 +TInt CDESEncryptorShim::BlockSize() const
    1.80 +	{
    1.81 +	// SPI returns block size in BITS
    1.82 +	return BitsToBytes(iSymmetricCipherImpl->BlockSize());
    1.83 +	}	
    1.84 +
    1.85 +TInt CDESEncryptorShim::KeySize() const
    1.86 +	{
    1.87 +	return iSymmetricCipherImpl->KeySize();
    1.88 +	}	
    1.89 +
    1.90 +void CDESEncryptorShim::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 CDESEncryptorShim::Reset()
    1.98 +	{
    1.99 +	iSymmetricCipherImpl->Reset();
   1.100 +	}	
   1.101 +
   1.102 +TInt CDESEncryptorShim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/)
   1.103 +	{
   1.104 +	TInt ret(KErrNotSupported);
   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 +// CDESDecryptorShim ////////////////////////////////////////////////////////
   1.115 +CDESDecryptorShim* CDESDecryptorShim::NewL(const TDesC8& aKey)
   1.116 +	{
   1.117 +	CDESDecryptorShim* self = CDESDecryptorShim::NewLC(aKey);
   1.118 +	CleanupStack::Pop(self);
   1.119 +	return self;
   1.120 +	}
   1.121 +
   1.122 +
   1.123 +CDESDecryptorShim* CDESDecryptorShim::NewLC(const TDesC8& aKey)
   1.124 +	{
   1.125 +	CDESDecryptorShim* self = new (ELeave) CDESDecryptorShim();
   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 +CDESDecryptorShim::CDESDecryptorShim()
   1.134 +	{	
   1.135 +	}
   1.136 +
   1.137 +CDESDecryptorShim::~CDESDecryptorShim()
   1.138 +	{
   1.139 +	delete iSymmetricCipherImpl;
   1.140 +	delete iKey;			
   1.141 +	}
   1.142 +
   1.143 +
   1.144 +void CDESDecryptorShim::ConstructL(const TDesC8& aKey)
   1.145 +	{
   1.146 +	TKeyProperty keyProperty = {KDesUid, 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 +											KDesUid,
   1.154 +											*iKey,
   1.155 +											KCryptoModeDecryptUid,
   1.156 +											KOperationModeECBUid,
   1.157 +											KPaddingModeNoneUid,
   1.158 +											NULL);	
   1.159 +	}	
   1.160 +
   1.161 +TInt CDESDecryptorShim::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 CDESDecryptorShim::KeySize() const
   1.168 +	{
   1.169 +	return iSymmetricCipherImpl->KeySize();
   1.170 +	}	
   1.171 +
   1.172 +void CDESDecryptorShim::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 CDESDecryptorShim::Reset()
   1.180 +	{
   1.181 +	iSymmetricCipherImpl->Reset();
   1.182 +	}
   1.183 +
   1.184 +TInt CDESDecryptorShim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/)
   1.185 +	{
   1.186 +	TInt ret(KErrNotSupported);
   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 +	}