os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/arc4impl.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/arc4impl.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,153 @@
     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 "arc4impl.h"
    1.23 +#include "keys.h"
    1.24 +#include "pluginconfig.h"
    1.25 +#include "symmetriccipherimpl.h"
    1.26 +#include <cryptostrength.h>
    1.27 +#include "common/inlines.h"
    1.28 +
    1.29 +
    1.30 +using namespace SoftwareCrypto;
    1.31 +
    1.32 +CArc4Impl* CArc4Impl::NewL(const CKey& aKey, TInt aDiscardBytes)
    1.33 +	{
    1.34 +	CArc4Impl* self = CArc4Impl::NewLC(aKey, aDiscardBytes);
    1.35 +	CleanupStack::Pop(self);
    1.36 +	return self;		
    1.37 +	}
    1.38 +
    1.39 +CArc4Impl* CArc4Impl::NewLC(const CKey& aKey, TInt aDiscardBytes)
    1.40 +	{
    1.41 +	CArc4Impl* self = new(ELeave) CArc4Impl(aDiscardBytes);
    1.42 +	CleanupStack::PushL(self);
    1.43 +	self->ConstructL(aKey);
    1.44 +	
    1.45 +	const TDesC8& keyContent = aKey.GetTDesC8L(KSymmetricKeyParameterUid);
    1.46 +	TCrypto::IsSymmetricWeakEnoughL(BytesToBits(keyContent.Size()) - keyContent.Size());
    1.47 +
    1.48 +	return self;		
    1.49 +	}
    1.50 +
    1.51 +CArc4Impl::CArc4Impl(TInt aDiscardBytes)
    1.52 +:ix(1), iy(0), iDiscardBytes(aDiscardBytes)
    1.53 +	{
    1.54 +	}
    1.55 +
    1.56 +CArc4Impl::~CArc4Impl()
    1.57 +	{
    1.58 +	}
    1.59 +
    1.60 +void CArc4Impl::ConstructL(const CKey& aKey)
    1.61 +	{
    1.62 +	CSymmetricStreamCipherImpl::ConstructL(aKey);
    1.63 +	GenerateSBox();
    1.64 +	}
    1.65 +
    1.66 +CExtendedCharacteristics* CArc4Impl::CreateExtendedCharacteristicsL()
    1.67 +	{
    1.68 +	// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
    1.69 +	// for exclusive use and are not CERTIFIED to be standards compliant.
    1.70 +	return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
    1.71 +	}
    1.72 +	
    1.73 +const CExtendedCharacteristics* CArc4Impl::GetExtendedCharacteristicsL()
    1.74 +	{
    1.75 +	return CArc4Impl::CreateExtendedCharacteristicsL();
    1.76 +	}
    1.77 +
    1.78 +void CArc4Impl::DoProcess(TDes8& aData)
    1.79 +	{
    1.80 +	TInt blockLen = aData.Size();
    1.81 +
    1.82 +	if (blockLen > 0)
    1.83 +		{
    1.84 +		TUint8* blockPtr = (TUint8*)&aData[0];	
    1.85 +		do
    1.86 +			{
    1.87 +			*blockPtr++ ^= GenerateByte();
    1.88 +			} 
    1.89 +		while (--blockLen);
    1.90 +		}
    1.91 +	}
    1.92 +	
    1.93 +TBool CArc4Impl::IsValidKeyLength(TInt aKeyBytes) const
    1.94 +	{
    1.95 +	return ((aKeyBytes > 0 && aKeyBytes <= KMaxARC4KeyBytes) ? ETrue : EFalse);
    1.96 +	}
    1.97 +
    1.98 +void CArc4Impl::Reset()
    1.99 +	{
   1.100 +	ix = 1;
   1.101 +	iy = 0;
   1.102 +	GenerateSBox();
   1.103 +	}
   1.104 +
   1.105 +TUid CArc4Impl::ImplementationUid() const
   1.106 +	{
   1.107 +	return KCryptoPluginArc4Uid;
   1.108 +	}
   1.109 +	
   1.110 +TUint8 CArc4Impl::GenerateByte()
   1.111 +	{
   1.112 +	TUint8 a = iState[ix];
   1.113 +	iy = (TUint8)((iy + a) & 0xff);
   1.114 +	TUint8 b = iState[iy];
   1.115 +
   1.116 +	iState[ix] = b;
   1.117 +	iState[iy] = a;
   1.118 +	ix = (TUint8)((ix + 1) & 0xff);
   1.119 +	return (iState[(a + b) & 0xff]);
   1.120 +	}
   1.121 +
   1.122 +void CArc4Impl::DiscardBytes(TInt aDiscardBytes)
   1.123 +	{	
   1.124 +	if (aDiscardBytes > 0)
   1.125 +		{
   1.126 +		do
   1.127 +			{
   1.128 +			GenerateByte();
   1.129 +			}
   1.130 +		while(--aDiscardBytes);
   1.131 +		}
   1.132 +	}
   1.133 +
   1.134 +void CArc4Impl::GenerateSBox(void)
   1.135 +	{
   1.136 +	TUint keyBytes = iKey->Size();
   1.137 +		
   1.138 +	TInt i = 0;
   1.139 +	for (; i < KSBoxSize; i++)
   1.140 +		iState[i] = (TUint8)i;
   1.141 +	
   1.142 +	TUint keyIndex = 0, stateIndex = 0;
   1.143 +	i = 0;
   1.144 +	for (; i < KSBoxSize; i++)
   1.145 +		{
   1.146 +		TUint a = iState[i];
   1.147 +		stateIndex += (*iKey)[keyIndex] + a;
   1.148 +		stateIndex &= 0xff;
   1.149 +		iState[i] = iState[stateIndex];
   1.150 +		iState[stateIndex] = (TUint8)a;
   1.151 +		if (++keyIndex >= (TUint)keyBytes)
   1.152 +			keyIndex = 0;
   1.153 +		}
   1.154 +
   1.155 +	DiscardBytes(iDiscardBytes);
   1.156 +	}