sl@0: /* sl@0: * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "arc4impl.h" sl@0: #include "keys.h" sl@0: #include "pluginconfig.h" sl@0: #include "symmetriccipherimpl.h" sl@0: #include sl@0: #include "common/inlines.h" sl@0: sl@0: sl@0: using namespace SoftwareCrypto; sl@0: sl@0: CArc4Impl* CArc4Impl::NewL(const CKey& aKey, TInt aDiscardBytes) sl@0: { sl@0: CArc4Impl* self = CArc4Impl::NewLC(aKey, aDiscardBytes); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: CArc4Impl* CArc4Impl::NewLC(const CKey& aKey, TInt aDiscardBytes) sl@0: { sl@0: CArc4Impl* self = new(ELeave) CArc4Impl(aDiscardBytes); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aKey); sl@0: sl@0: const TDesC8& keyContent = aKey.GetTDesC8L(KSymmetricKeyParameterUid); sl@0: TCrypto::IsSymmetricWeakEnoughL(BytesToBits(keyContent.Size()) - keyContent.Size()); sl@0: sl@0: return self; sl@0: } sl@0: sl@0: CArc4Impl::CArc4Impl(TInt aDiscardBytes) sl@0: :ix(1), iy(0), iDiscardBytes(aDiscardBytes) sl@0: { sl@0: } sl@0: sl@0: CArc4Impl::~CArc4Impl() sl@0: { sl@0: } sl@0: sl@0: void CArc4Impl::ConstructL(const CKey& aKey) sl@0: { sl@0: CSymmetricStreamCipherImpl::ConstructL(aKey); sl@0: GenerateSBox(); sl@0: } sl@0: sl@0: CExtendedCharacteristics* CArc4Impl::CreateExtendedCharacteristicsL() sl@0: { sl@0: // All Symbian software plug-ins have unlimited concurrency, cannot be reserved sl@0: // for exclusive use and are not CERTIFIED to be standards compliant. sl@0: return CExtendedCharacteristics::NewL(KMaxTInt, EFalse); sl@0: } sl@0: sl@0: const CExtendedCharacteristics* CArc4Impl::GetExtendedCharacteristicsL() sl@0: { sl@0: return CArc4Impl::CreateExtendedCharacteristicsL(); sl@0: } sl@0: sl@0: void CArc4Impl::DoProcess(TDes8& aData) sl@0: { sl@0: TInt blockLen = aData.Size(); sl@0: sl@0: if (blockLen > 0) sl@0: { sl@0: TUint8* blockPtr = (TUint8*)&aData[0]; sl@0: do sl@0: { sl@0: *blockPtr++ ^= GenerateByte(); sl@0: } sl@0: while (--blockLen); sl@0: } sl@0: } sl@0: sl@0: TBool CArc4Impl::IsValidKeyLength(TInt aKeyBytes) const sl@0: { sl@0: return ((aKeyBytes > 0 && aKeyBytes <= KMaxARC4KeyBytes) ? ETrue : EFalse); sl@0: } sl@0: sl@0: void CArc4Impl::Reset() sl@0: { sl@0: ix = 1; sl@0: iy = 0; sl@0: GenerateSBox(); sl@0: } sl@0: sl@0: TUid CArc4Impl::ImplementationUid() const sl@0: { sl@0: return KCryptoPluginArc4Uid; sl@0: } sl@0: sl@0: TUint8 CArc4Impl::GenerateByte() sl@0: { sl@0: TUint8 a = iState[ix]; sl@0: iy = (TUint8)((iy + a) & 0xff); sl@0: TUint8 b = iState[iy]; sl@0: sl@0: iState[ix] = b; sl@0: iState[iy] = a; sl@0: ix = (TUint8)((ix + 1) & 0xff); sl@0: return (iState[(a + b) & 0xff]); sl@0: } sl@0: sl@0: void CArc4Impl::DiscardBytes(TInt aDiscardBytes) sl@0: { sl@0: if (aDiscardBytes > 0) sl@0: { sl@0: do sl@0: { sl@0: GenerateByte(); sl@0: } sl@0: while(--aDiscardBytes); sl@0: } sl@0: } sl@0: sl@0: void CArc4Impl::GenerateSBox(void) sl@0: { sl@0: TUint keyBytes = iKey->Size(); sl@0: sl@0: TInt i = 0; sl@0: for (; i < KSBoxSize; i++) sl@0: iState[i] = (TUint8)i; sl@0: sl@0: TUint keyIndex = 0, stateIndex = 0; sl@0: i = 0; sl@0: for (; i < KSBoxSize; i++) sl@0: { sl@0: TUint a = iState[i]; sl@0: stateIndex += (*iKey)[keyIndex] + a; sl@0: stateIndex &= 0xff; sl@0: iState[i] = iState[stateIndex]; sl@0: iState[stateIndex] = (TUint8)a; sl@0: if (++keyIndex >= (TUint)keyBytes) sl@0: keyIndex = 0; sl@0: } sl@0: sl@0: DiscardBytes(iDiscardBytes); sl@0: }