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