os/security/crypto/weakcrypto/source/symmetric/arc4.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcrypto/source/symmetric/arc4.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,124 @@
     1.4 +/*
     1.5 +* Copyright (c) 2002-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 "arc4.h"
    1.23 +#include "../common/inlines.h"
    1.24 +#include <e32base.h>
    1.25 +#include <cryptostrength.h>
    1.26 +
    1.27 +#ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
    1.28 +/** The size of the substitution box (i.e. lookup table) in bytes. */
    1.29 +const TInt KSBoxSize = 256;
    1.30 +#endif
    1.31 +
    1.32 +inline TUint8 CARC4::GenerateByte()
    1.33 +	{
    1.34 +	TUint8 a = iState[ix];
    1.35 +	iy = (TUint8)((iy + a) & 0xff);
    1.36 +	TUint8 b = iState[iy];
    1.37 +
    1.38 +	iState[ix] = b;
    1.39 +	iState[iy] = a;
    1.40 +	ix = (TUint8)((ix + 1) & 0xff);
    1.41 +	return (iState[(a + b) & 0xff]);
    1.42 +	}
    1.43 +
    1.44 +CARC4::CARC4(const TDesC8& aKey, TUint aDiscardBytes)
    1.45 +	: ix(1), iy(0), iDiscardBytes(aDiscardBytes)
    1.46 +	{
    1.47 +	iKey.Copy(aKey);
    1.48 +	GenerateSBox();
    1.49 +	}
    1.50 +
    1.51 +EXPORT_C CARC4* CARC4::NewL(const TDesC8& aKey, TUint aDiscardBytes)
    1.52 +	{
    1.53 +	CARC4* self = NewLC(aKey, aDiscardBytes);
    1.54 +	CleanupStack::Pop(self);
    1.55 +	return self;
    1.56 +	}
    1.57 +
    1.58 +EXPORT_C CARC4* CARC4::NewLC(const TDesC8& aKey, TUint aDiscardBytes)
    1.59 +	{
    1.60 +	CARC4* self = new(ELeave)CARC4(aKey, aDiscardBytes);
    1.61 +	CleanupStack::PushL(self);
    1.62 +	TCrypto::IsSymmetricWeakEnoughL(BytesToBits(aKey.Size()));
    1.63 +	return self;
    1.64 +	}
    1.65 +
    1.66 +void CARC4::DoProcess(TDes8& aData)
    1.67 +	{
    1.68 +	TInt blockLen = aData.Size();
    1.69 +
    1.70 +	if (blockLen > 0)
    1.71 +		{
    1.72 +		TUint8* blockPtr = (TUint8*)&aData[0];	
    1.73 +		do
    1.74 +			{
    1.75 +			*blockPtr++ ^= GenerateByte();
    1.76 +			} 
    1.77 +		while (--blockLen);
    1.78 +		}
    1.79 +	}
    1.80 +
    1.81 +void CARC4::Reset()
    1.82 +	{
    1.83 +	ix = 1;
    1.84 +	iy = 0;
    1.85 +	GenerateSBox();
    1.86 +	}
    1.87 +
    1.88 +TInt CARC4::KeySize() const
    1.89 +	{
    1.90 +	return (iKey.Size());
    1.91 +	}
    1.92 +
    1.93 +void CARC4::DiscardBytes(TInt aDiscardBytes)
    1.94 +	{	
    1.95 +	if (aDiscardBytes > 0)
    1.96 +		{
    1.97 +		do
    1.98 +			{
    1.99 +			GenerateByte();
   1.100 +			}
   1.101 +		while(--aDiscardBytes);
   1.102 +		}
   1.103 +	}
   1.104 +
   1.105 +void CARC4::GenerateSBox(void)
   1.106 +	{
   1.107 +	TUint keyBytes = iKey.Size();
   1.108 +		
   1.109 +	TInt i = 0;
   1.110 +	for (; i < KSBoxSize; i++)
   1.111 +		iState[i] = (TUint8)i;
   1.112 +	
   1.113 +	TUint keyIndex = 0, stateIndex = 0;
   1.114 +	i = 0;
   1.115 +	for (; i < KSBoxSize; i++)
   1.116 +		{
   1.117 +		TUint a = iState[i];
   1.118 +		stateIndex += iKey[keyIndex] + a;
   1.119 +		stateIndex &= 0xff;
   1.120 +		iState[i] = iState[stateIndex];
   1.121 +		iState[stateIndex] = (TUint8)a;
   1.122 +		if (++keyIndex >= (TUint)keyBytes)
   1.123 +			keyIndex = 0;
   1.124 +		}
   1.125 +
   1.126 +	DiscardBytes(iDiscardBytes);
   1.127 +	}