os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/3desimpl.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/3desimpl.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,190 @@
     1.4 +/*
     1.5 +* Copyright (c) 2006-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 "3desimpl.h"
    1.23 +
    1.24 +#include "destables.h"
    1.25 +#include "common/inlines.h"
    1.26 +#include "des.inl"
    1.27 +#include "pluginconfig.h"
    1.28 +#include "symmetriccipherimpl.h"
    1.29 +#include <cryptostrength.h>
    1.30 +
    1.31 +using namespace SoftwareCrypto;
    1.32 +
    1.33 +/* C3DesImpl */
    1.34 +C3DesImpl::C3DesImpl(
    1.35 +	TUid aCryptoMode,
    1.36 +	TUid aOperationMode,
    1.37 +	TUid aPadding) :
    1.38 +	CDesImpl(KDesBlockBytes, aCryptoMode, aOperationMode, aPadding)
    1.39 +	{
    1.40 +	}
    1.41 +
    1.42 +C3DesImpl* C3DesImpl::NewL(const CKey& aKey, TUid aCryptoMode, TUid aOperationMode, TUid aPadding)
    1.43 +	{
    1.44 +	C3DesImpl* self = C3DesImpl::NewLC(aKey, aCryptoMode, aOperationMode, aPadding);
    1.45 +	CleanupStack::Pop(self);
    1.46 +	return self;
    1.47 +	}
    1.48 +	
    1.49 +C3DesImpl* C3DesImpl::NewLC(const CKey& aKey, TUid aCryptoMode, TUid aOperationMode, TUid aPadding)
    1.50 +	{
    1.51 +	C3DesImpl* self = new(ELeave) C3DesImpl(aCryptoMode, aOperationMode, aPadding);
    1.52 +	CleanupStack::PushL(self);
    1.53 +	self->ConstructL(aKey);
    1.54 +
    1.55 +	const TDesC8& keyContent = aKey.GetTDesC8L(KSymmetricKeyParameterUid);
    1.56 +	TCrypto::IsSymmetricWeakEnoughL(BytesToBits(keyContent.Size()) - keyContent.Size());
    1.57 +	return self;
    1.58 +	}
    1.59 +		
    1.60 +C3DesImpl::~C3DesImpl()
    1.61 +	{
    1.62 +	// make sure key information isn't visible to other processes if the
    1.63 +	// page is reused.
    1.64 +	Mem::FillZ(&iK1, sizeof(iK1));
    1.65 +	Mem::FillZ(&iK2, sizeof(iK2));
    1.66 +	Mem::FillZ(&iK3, sizeof(iK3));
    1.67 +	}
    1.68 +	
    1.69 +void C3DesImpl::ConstructL(const CKey& aKey)
    1.70 +	{
    1.71 +	CDesImpl::ConstructL(aKey);
    1.72 +	SetKeySchedule();
    1.73 +	}	
    1.74 +	
    1.75 +CExtendedCharacteristics* C3DesImpl::CreateExtendedCharacteristicsL()
    1.76 +	{
    1.77 +	// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
    1.78 +	// for exclusive use and are not CERTIFIED to be standards compliant.
    1.79 +	return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
    1.80 +	}
    1.81 +	
    1.82 +const CExtendedCharacteristics* C3DesImpl::GetExtendedCharacteristicsL()
    1.83 +	{
    1.84 +	return C3DesImpl::CreateExtendedCharacteristicsL();
    1.85 +	}
    1.86 +
    1.87 +TUid C3DesImpl::ImplementationUid() const
    1.88 +	{
    1.89 +	return KCryptoPlugin3DesUid;
    1.90 +	}
    1.91 +	
    1.92 +TBool C3DesImpl::IsValidKeyLength(TInt aKeyBytes) const
    1.93 +	{
    1.94 +	return (aKeyBytes == K3DesKeyBytes);
    1.95 +	}
    1.96 +	
    1.97 +TInt C3DesImpl::GetKeyStrength() const
    1.98 +	{
    1.99 +	// Exclude parity bits from each subkey
   1.100 +	return BytesToBits(K3DesKeyBytes - (3 * 8));
   1.101 +	}	
   1.102 +	
   1.103 +void C3DesImpl::TransformEncrypt(
   1.104 +	TUint8* aBuffer,
   1.105 +	TUint aNumBlocks)
   1.106 +	{			
   1.107 +	for (TInt i = 0; i < aNumBlocks; ++i)
   1.108 +		{		
   1.109 +		ModeEncryptStart(aBuffer);
   1.110 +
   1.111 +		TUint32 l, r;
   1.112 +		// Split the block into 2 word-sized big endian portions
   1.113 +		GetBlockBigEndian(aBuffer, l, r);
   1.114 +
   1.115 +		IPerm(l,r);
   1.116 +		// The mode is applied to the entire operation and NOT 
   1.117 +		// for each DES transform
   1.118 +		TUid opMode = iOperationMode;
   1.119 +		iOperationMode = KOperationModeECBUid;
   1.120 +		DoTransform(l, r, iK1);
   1.121 +		DoTransform(r, l, iK2);
   1.122 +		DoTransform(l, r, iK3);
   1.123 +		iOperationMode = opMode;
   1.124 +		FPerm(l,r);
   1.125 +
   1.126 +		// Put the portions back into the block as little endian
   1.127 +		PutBlockBigEndian(aBuffer, r, l);
   1.128 +		ModeEncryptEnd(aBuffer);
   1.129 +		aBuffer += KDesBlockBytes;
   1.130 +		}
   1.131 +	}
   1.132 +	
   1.133 +void C3DesImpl::TransformDecrypt(
   1.134 +	TUint8* aBuffer,
   1.135 +	const TUint aNumBlocks)
   1.136 +	{	
   1.137 +	for (TInt i = 0; i < aNumBlocks; ++i)
   1.138 +		{		
   1.139 +		ModeDecryptStart(aBuffer);
   1.140 +			
   1.141 +		TUint32 l, r;
   1.142 +		// Split the block into 2 word-sized big endian portions
   1.143 +		GetBlockBigEndian(aBuffer, l, r);
   1.144 +
   1.145 +		IPerm(l,r);
   1.146 +		
   1.147 +		// The mode is applied to the entire operation and NOT 
   1.148 +		// for each DES transform
   1.149 +		TUid opMode = iOperationMode;
   1.150 +		iOperationMode = KOperationModeECBUid;
   1.151 +		DoTransform(l, r, iK1);
   1.152 +		DoTransform(r, l, iK2);
   1.153 +		DoTransform(l, r, iK3);
   1.154 +		iOperationMode = opMode;
   1.155 +		FPerm(l,r);
   1.156 +
   1.157 +		// Put the portions back into the block as little endian
   1.158 +		PutBlockBigEndian(aBuffer, r, l);	
   1.159 +		ModeDecryptEnd(aBuffer);
   1.160 +		aBuffer += K3DesBlockBytes;
   1.161 +		}
   1.162 +	}	
   1.163 +
   1.164 +void C3DesImpl::SetKeySchedule()
   1.165 +	{
   1.166 +	if (iCryptoMode.iUid == KCryptoModeEncrypt)
   1.167 +		{
   1.168 +		// Encrypt -> Decrypt -> Encrypt
   1.169 +		// Encryptor key	
   1.170 +		SetEncryptKeySchedule(iKey->Mid(0, KDesKeyBytes), iK1);
   1.171 +		
   1.172 +		// Decryptor key
   1.173 +		SetDecryptKeySchedule(iKey->Mid(KDesKeyBytes, 2 * KDesKeyBytes), iK2);
   1.174 +		
   1.175 +		// Encryptor key
   1.176 +		SetEncryptKeySchedule(iKey->Mid(2 * KDesKeyBytes), iK3);
   1.177 +		}
   1.178 +	else 
   1.179 +		{
   1.180 +		// Decrypt -> Encrypt -> Decrypt
   1.181 +		// Key order is reversed !
   1.182 +		
   1.183 +		ASSERT(iCryptoMode.iUid == KCryptoModeDecrypt);
   1.184 +		// Decryptor key	
   1.185 +		SetDecryptKeySchedule(iKey->Mid(0, KDesKeyBytes), iK3);
   1.186 +		
   1.187 +		// Encryptor key
   1.188 +		SetEncryptKeySchedule(iKey->Mid(KDesKeyBytes, 2 * KDesKeyBytes), iK2);
   1.189 +		
   1.190 +		// Decryptor key
   1.191 +		SetDecryptKeySchedule(iKey->Mid(2 * KDesKeyBytes), iK1);
   1.192 +		}	
   1.193 +	}