First public contribution.
2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
21 #include "pluginconfig.h"
22 #include "symmetriccipherimpl.h"
23 #include <cryptostrength.h>
24 #include "common/inlines.h"
27 using namespace SoftwareCrypto;
29 CArc4Impl* CArc4Impl::NewL(const CKey& aKey, TInt aDiscardBytes)
31 CArc4Impl* self = CArc4Impl::NewLC(aKey, aDiscardBytes);
32 CleanupStack::Pop(self);
36 CArc4Impl* CArc4Impl::NewLC(const CKey& aKey, TInt aDiscardBytes)
38 CArc4Impl* self = new(ELeave) CArc4Impl(aDiscardBytes);
39 CleanupStack::PushL(self);
40 self->ConstructL(aKey);
42 const TDesC8& keyContent = aKey.GetTDesC8L(KSymmetricKeyParameterUid);
43 TCrypto::IsSymmetricWeakEnoughL(BytesToBits(keyContent.Size()) - keyContent.Size());
48 CArc4Impl::CArc4Impl(TInt aDiscardBytes)
49 :ix(1), iy(0), iDiscardBytes(aDiscardBytes)
53 CArc4Impl::~CArc4Impl()
57 void CArc4Impl::ConstructL(const CKey& aKey)
59 CSymmetricStreamCipherImpl::ConstructL(aKey);
63 CExtendedCharacteristics* CArc4Impl::CreateExtendedCharacteristicsL()
65 // All Symbian software plug-ins have unlimited concurrency, cannot be reserved
66 // for exclusive use and are not CERTIFIED to be standards compliant.
67 return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
70 const CExtendedCharacteristics* CArc4Impl::GetExtendedCharacteristicsL()
72 return CArc4Impl::CreateExtendedCharacteristicsL();
75 void CArc4Impl::DoProcess(TDes8& aData)
77 TInt blockLen = aData.Size();
81 TUint8* blockPtr = (TUint8*)&aData[0];
84 *blockPtr++ ^= GenerateByte();
90 TBool CArc4Impl::IsValidKeyLength(TInt aKeyBytes) const
92 return ((aKeyBytes > 0 && aKeyBytes <= KMaxARC4KeyBytes) ? ETrue : EFalse);
95 void CArc4Impl::Reset()
102 TUid CArc4Impl::ImplementationUid() const
104 return KCryptoPluginArc4Uid;
107 TUint8 CArc4Impl::GenerateByte()
109 TUint8 a = iState[ix];
110 iy = (TUint8)((iy + a) & 0xff);
111 TUint8 b = iState[iy];
115 ix = (TUint8)((ix + 1) & 0xff);
116 return (iState[(a + b) & 0xff]);
119 void CArc4Impl::DiscardBytes(TInt aDiscardBytes)
121 if (aDiscardBytes > 0)
127 while(--aDiscardBytes);
131 void CArc4Impl::GenerateSBox(void)
133 TUint keyBytes = iKey->Size();
136 for (; i < KSBoxSize; i++)
137 iState[i] = (TUint8)i;
139 TUint keyIndex = 0, stateIndex = 0;
141 for (; i < KSBoxSize; i++)
144 stateIndex += (*iKey)[keyIndex] + a;
146 iState[i] = iState[stateIndex];
147 iState[stateIndex] = (TUint8)a;
148 if (++keyIndex >= (TUint)keyBytes)
152 DiscardBytes(iDiscardBytes);