os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/arc4impl.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include "arc4impl.h"
sl@0
    20
#include "keys.h"
sl@0
    21
#include "pluginconfig.h"
sl@0
    22
#include "symmetriccipherimpl.h"
sl@0
    23
#include <cryptostrength.h>
sl@0
    24
#include "common/inlines.h"
sl@0
    25
sl@0
    26
sl@0
    27
using namespace SoftwareCrypto;
sl@0
    28
sl@0
    29
CArc4Impl* CArc4Impl::NewL(const CKey& aKey, TInt aDiscardBytes)
sl@0
    30
	{
sl@0
    31
	CArc4Impl* self = CArc4Impl::NewLC(aKey, aDiscardBytes);
sl@0
    32
	CleanupStack::Pop(self);
sl@0
    33
	return self;		
sl@0
    34
	}
sl@0
    35
sl@0
    36
CArc4Impl* CArc4Impl::NewLC(const CKey& aKey, TInt aDiscardBytes)
sl@0
    37
	{
sl@0
    38
	CArc4Impl* self = new(ELeave) CArc4Impl(aDiscardBytes);
sl@0
    39
	CleanupStack::PushL(self);
sl@0
    40
	self->ConstructL(aKey);
sl@0
    41
	
sl@0
    42
	const TDesC8& keyContent = aKey.GetTDesC8L(KSymmetricKeyParameterUid);
sl@0
    43
	TCrypto::IsSymmetricWeakEnoughL(BytesToBits(keyContent.Size()) - keyContent.Size());
sl@0
    44
sl@0
    45
	return self;		
sl@0
    46
	}
sl@0
    47
sl@0
    48
CArc4Impl::CArc4Impl(TInt aDiscardBytes)
sl@0
    49
:ix(1), iy(0), iDiscardBytes(aDiscardBytes)
sl@0
    50
	{
sl@0
    51
	}
sl@0
    52
sl@0
    53
CArc4Impl::~CArc4Impl()
sl@0
    54
	{
sl@0
    55
	}
sl@0
    56
sl@0
    57
void CArc4Impl::ConstructL(const CKey& aKey)
sl@0
    58
	{
sl@0
    59
	CSymmetricStreamCipherImpl::ConstructL(aKey);
sl@0
    60
	GenerateSBox();
sl@0
    61
	}
sl@0
    62
sl@0
    63
CExtendedCharacteristics* CArc4Impl::CreateExtendedCharacteristicsL()
sl@0
    64
	{
sl@0
    65
	// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
sl@0
    66
	// for exclusive use and are not CERTIFIED to be standards compliant.
sl@0
    67
	return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
sl@0
    68
	}
sl@0
    69
	
sl@0
    70
const CExtendedCharacteristics* CArc4Impl::GetExtendedCharacteristicsL()
sl@0
    71
	{
sl@0
    72
	return CArc4Impl::CreateExtendedCharacteristicsL();
sl@0
    73
	}
sl@0
    74
sl@0
    75
void CArc4Impl::DoProcess(TDes8& aData)
sl@0
    76
	{
sl@0
    77
	TInt blockLen = aData.Size();
sl@0
    78
sl@0
    79
	if (blockLen > 0)
sl@0
    80
		{
sl@0
    81
		TUint8* blockPtr = (TUint8*)&aData[0];	
sl@0
    82
		do
sl@0
    83
			{
sl@0
    84
			*blockPtr++ ^= GenerateByte();
sl@0
    85
			} 
sl@0
    86
		while (--blockLen);
sl@0
    87
		}
sl@0
    88
	}
sl@0
    89
	
sl@0
    90
TBool CArc4Impl::IsValidKeyLength(TInt aKeyBytes) const
sl@0
    91
	{
sl@0
    92
	return ((aKeyBytes > 0 && aKeyBytes <= KMaxARC4KeyBytes) ? ETrue : EFalse);
sl@0
    93
	}
sl@0
    94
sl@0
    95
void CArc4Impl::Reset()
sl@0
    96
	{
sl@0
    97
	ix = 1;
sl@0
    98
	iy = 0;
sl@0
    99
	GenerateSBox();
sl@0
   100
	}
sl@0
   101
sl@0
   102
TUid CArc4Impl::ImplementationUid() const
sl@0
   103
	{
sl@0
   104
	return KCryptoPluginArc4Uid;
sl@0
   105
	}
sl@0
   106
	
sl@0
   107
TUint8 CArc4Impl::GenerateByte()
sl@0
   108
	{
sl@0
   109
	TUint8 a = iState[ix];
sl@0
   110
	iy = (TUint8)((iy + a) & 0xff);
sl@0
   111
	TUint8 b = iState[iy];
sl@0
   112
sl@0
   113
	iState[ix] = b;
sl@0
   114
	iState[iy] = a;
sl@0
   115
	ix = (TUint8)((ix + 1) & 0xff);
sl@0
   116
	return (iState[(a + b) & 0xff]);
sl@0
   117
	}
sl@0
   118
sl@0
   119
void CArc4Impl::DiscardBytes(TInt aDiscardBytes)
sl@0
   120
	{	
sl@0
   121
	if (aDiscardBytes > 0)
sl@0
   122
		{
sl@0
   123
		do
sl@0
   124
			{
sl@0
   125
			GenerateByte();
sl@0
   126
			}
sl@0
   127
		while(--aDiscardBytes);
sl@0
   128
		}
sl@0
   129
	}
sl@0
   130
sl@0
   131
void CArc4Impl::GenerateSBox(void)
sl@0
   132
	{
sl@0
   133
	TUint keyBytes = iKey->Size();
sl@0
   134
		
sl@0
   135
	TInt i = 0;
sl@0
   136
	for (; i < KSBoxSize; i++)
sl@0
   137
		iState[i] = (TUint8)i;
sl@0
   138
	
sl@0
   139
	TUint keyIndex = 0, stateIndex = 0;
sl@0
   140
	i = 0;
sl@0
   141
	for (; i < KSBoxSize; i++)
sl@0
   142
		{
sl@0
   143
		TUint a = iState[i];
sl@0
   144
		stateIndex += (*iKey)[keyIndex] + a;
sl@0
   145
		stateIndex &= 0xff;
sl@0
   146
		iState[i] = iState[stateIndex];
sl@0
   147
		iState[stateIndex] = (TUint8)a;
sl@0
   148
		if (++keyIndex >= (TUint)keyBytes)
sl@0
   149
			keyIndex = 0;
sl@0
   150
		}
sl@0
   151
sl@0
   152
	DiscardBytes(iDiscardBytes);
sl@0
   153
	}