crypto/weakcrypto/source/symmetric/arc4.cpp
author hgs
Thu, 12 Aug 2010 21:07:10 +0530
changeset 90 8c545fea2798
parent 72 de46a57f75fb
permissions -rw-r--r--
201031_01
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     1
/*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     2
* Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     3
* All rights reserved.
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     8
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     9
* Initial Contributors:
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    11
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    12
* Contributors:
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    13
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    14
* Description: 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    15
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    16
*/
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    17
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    18
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    19
#include "arc4.h"
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    20
#include "../common/inlines.h"
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    21
#include <e32base.h>
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    22
#include <cryptostrength.h>
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    23
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    24
#ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    25
/** The size of the substitution box (i.e. lookup table) in bytes. */
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    26
const TInt KSBoxSize = 256;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    27
#endif
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    28
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    29
inline TUint8 CARC4::GenerateByte()
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    30
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    31
	TUint8 a = iState[ix];
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    32
	iy = (TUint8)((iy + a) & 0xff);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    33
	TUint8 b = iState[iy];
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    34
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    35
	iState[ix] = b;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    36
	iState[iy] = a;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    37
	ix = (TUint8)((ix + 1) & 0xff);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    38
	return (iState[(a + b) & 0xff]);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    39
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    40
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    41
CARC4::CARC4(const TDesC8& aKey, TUint aDiscardBytes)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    42
	: ix(1), iy(0), iDiscardBytes(aDiscardBytes)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    43
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    44
	iKey.Copy(aKey);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    45
	GenerateSBox();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    46
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    47
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    48
EXPORT_C CARC4* CARC4::NewL(const TDesC8& aKey, TUint aDiscardBytes)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    49
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    50
	CARC4* self = NewLC(aKey, aDiscardBytes);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    51
	CleanupStack::Pop(self);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    52
	return self;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    53
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    54
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    55
EXPORT_C CARC4* CARC4::NewLC(const TDesC8& aKey, TUint aDiscardBytes)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    56
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    57
	CARC4* self = new(ELeave)CARC4(aKey, aDiscardBytes);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    58
	CleanupStack::PushL(self);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    59
	TCrypto::IsSymmetricWeakEnoughL(BytesToBits(aKey.Size()));
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    60
	return self;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    61
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    62
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    63
void CARC4::DoProcess(TDes8& aData)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    64
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    65
	TInt blockLen = aData.Size();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    66
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    67
	if (blockLen > 0)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    68
		{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    69
		TUint8* blockPtr = (TUint8*)&aData[0];	
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    70
		do
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    71
			{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    72
			*blockPtr++ ^= GenerateByte();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    73
			} 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    74
		while (--blockLen);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    75
		}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    76
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    77
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    78
void CARC4::Reset()
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    79
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    80
	ix = 1;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    81
	iy = 0;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    82
	GenerateSBox();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    83
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    84
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    85
TInt CARC4::KeySize() const
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    86
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    87
	return (iKey.Size());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    88
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    89
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    90
void CARC4::DiscardBytes(TInt aDiscardBytes)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    91
	{	
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    92
	if (aDiscardBytes > 0)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    93
		{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    94
		do
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    95
			{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    96
			GenerateByte();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    97
			}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    98
		while(--aDiscardBytes);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    99
		}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   100
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   101
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   102
void CARC4::GenerateSBox(void)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   103
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   104
	TUint keyBytes = iKey.Size();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   105
		
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   106
	TInt i = 0;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   107
	for (; i < KSBoxSize; i++)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   108
		iState[i] = (TUint8)i;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   109
	
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   110
	TUint keyIndex = 0, stateIndex = 0;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   111
	i = 0;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   112
	for (; i < KSBoxSize; i++)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   113
		{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   114
		TUint a = iState[i];
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   115
		stateIndex += iKey[keyIndex] + a;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   116
		stateIndex &= 0xff;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   117
		iState[i] = iState[stateIndex];
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   118
		iState[stateIndex] = (TUint8)a;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   119
		if (++keyIndex >= (TUint)keyBytes)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   120
			keyIndex = 0;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   121
		}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   122
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   123
	DiscardBytes(iDiscardBytes);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   124
	}