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 "rc2impl.h" sl@0: #include "keys.h" sl@0: sl@0: #include "rc2table.h" sl@0: #include "common/inlines.h" sl@0: #include "pluginconfig.h" sl@0: #include "symmetriccipherimpl.h" sl@0: #include sl@0: sl@0: using namespace SoftwareCrypto; sl@0: sl@0: CRc2Impl::CRc2Impl( sl@0: TUid aCryptoMode, sl@0: TUid aOperationMode, sl@0: TUid aPadding) : sl@0: CSymmetricBlockCipherImpl(KRc2BlockBytes, aCryptoMode, aOperationMode, aPadding) sl@0: { sl@0: } sl@0: sl@0: CRc2Impl* CRc2Impl::NewL( sl@0: const CKey& aKey, sl@0: TUid aCryptoMode, sl@0: TUid aOperationMode, sl@0: TUid aPadding, sl@0: TInt aEffectiveKeyLenBits) sl@0: { sl@0: CRc2Impl* self = CRc2Impl::NewLC(aKey, aCryptoMode, aOperationMode, aPadding, aEffectiveKeyLenBits); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: CRc2Impl* CRc2Impl::NewLC( sl@0: const CKey& aKey, sl@0: TUid aCryptoMode, sl@0: TUid aOperationMode, sl@0: TUid aPadding, sl@0: TInt aEffectiveKeyLenBits) sl@0: { sl@0: CRc2Impl* self = new(ELeave) CRc2Impl(aCryptoMode, aOperationMode, aPadding); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aKey, aEffectiveKeyLenBits); sl@0: sl@0: const TDesC8& keyContent = aKey.GetTDesC8L(KSymmetricKeyParameterUid); sl@0: TCrypto::IsSymmetricWeakEnoughL(BytesToBits(keyContent.Size()) - keyContent.Size()); sl@0: return self; sl@0: } sl@0: sl@0: CRc2Impl::~CRc2Impl() sl@0: { sl@0: Mem::FillZ(&iK, sizeof(iK)); sl@0: } sl@0: sl@0: void CRc2Impl::ConstructL(const CKey& aKey, TInt aEffectiveKeyLenBits) sl@0: { sl@0: iEffectiveKeyLenBits = aEffectiveKeyLenBits; sl@0: CSymmetricBlockCipherImpl::ConstructL(aKey); sl@0: SetKeySchedule(); sl@0: } sl@0: sl@0: CExtendedCharacteristics* CRc2Impl::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* CRc2Impl::GetExtendedCharacteristicsL() sl@0: { sl@0: return CRc2Impl::CreateExtendedCharacteristicsL(); sl@0: } sl@0: sl@0: TUid CRc2Impl::ImplementationUid() const sl@0: { sl@0: return KCryptoPluginRc2Uid; sl@0: } sl@0: sl@0: TBool CRc2Impl::IsValidKeyLength(TInt aKeyBytes) const sl@0: { sl@0: return ((aKeyBytes > 0 && aKeyBytes <= KRc2MaxKeySizeBytes) ? ETrue : EFalse); sl@0: } sl@0: sl@0: TInt CRc2Impl::GetKeyStrength() const sl@0: { sl@0: return Min(iEffectiveKeyLenBits, BytesToBits(iKeyBytes)); sl@0: } sl@0: sl@0: void CRc2Impl::SetKeySchedule() sl@0: { sl@0: TUint keyLen = iKey->Length(); sl@0: sl@0: TUint8 L[KRc2MaxKeySizeBytes]; sl@0: Mem::Copy((TUint8*)&L[0], (TUint8*)&(*iKey)[0], keyLen); sl@0: sl@0: TInt i = keyLen; sl@0: for (; i < KRc2MaxKeySizeBytes; i++) sl@0: { sl@0: L[i] = RC2_TABLE::PITABLE[(L[i-1] + L[i-keyLen]) & 255]; sl@0: } sl@0: sl@0: TUint T8 = (iEffectiveKeyLenBits+7) / 8; sl@0: TUint8 TM = (TUint8)(255 >> ((8-(iEffectiveKeyLenBits%8))%8)); sl@0: L[128-T8] = RC2_TABLE::PITABLE[L[128-T8] & TM]; sl@0: sl@0: for (i=127-T8; i>=0; i--) sl@0: L[i] = RC2_TABLE::PITABLE[L[i+1] ^ L[i+T8]]; sl@0: sl@0: for (i=0; i < KRc2ExpandedKeyLen; i++) sl@0: iK[i] = (TUint16)(L[2*i] + (L[2*i+1] << 8)); sl@0: } sl@0: sl@0: #pragma warning (disable : 4244) // conversion from 'int' to 'unsigned short', possible loss of data sl@0: void CRc2Impl::TransformEncrypt( sl@0: TUint8* aBuffer, sl@0: TUint aNumBlocks) sl@0: { sl@0: for (TInt blockNum = 0; blockNum < aNumBlocks; ++blockNum) sl@0: { sl@0: ModeEncryptStart(aBuffer); sl@0: sl@0: TUint16 R0, R1, R2, R3; sl@0: GetBlockLittleEndian(aBuffer, R0, R1, R2, R3); sl@0: sl@0: TInt i = 0; sl@0: for (; i < 16; i++) sl@0: { sl@0: R0 += (R1 & ~R3) + (R2 & R3) + iK[4*i+0]; sl@0: R0 = rotlFixed(R0, 1); sl@0: sl@0: R1 += (R2 & ~R0) + (R3 & R0) + iK[4*i+1]; sl@0: R1 = rotlFixed(R1, 2); sl@0: sl@0: R2 += (R3 & ~R1) + (R0 & R1) + iK[4*i+2]; sl@0: R2 = rotlFixed(R2, 3); sl@0: sl@0: R3 += (R0 & ~R2) + (R1 & R2) + iK[4*i+3]; sl@0: R3 = rotlFixed(R3, 5); sl@0: sl@0: if (i == 4 || i == 10) sl@0: { sl@0: R0 += iK[R3 & 63]; sl@0: R1 += iK[R0 & 63]; sl@0: R2 += iK[R1 & 63]; sl@0: R3 += iK[R2 & 63]; sl@0: } sl@0: } sl@0: PutBlockLittleEndian(aBuffer, R0, R1, R2, R3); sl@0: ModeEncryptEnd(aBuffer); sl@0: aBuffer += KRc2BlockBytes; sl@0: } sl@0: } sl@0: #pragma warning (default : 4244) // conversion from 'int' to 'unsigned short', possible loss of data sl@0: sl@0: sl@0: #pragma warning (disable : 4244) // conversion from 'int' to 'unsigned short', possible loss of data sl@0: void CRc2Impl::TransformDecrypt( sl@0: TUint8* aBuffer, sl@0: TUint aNumBlocks) sl@0: { sl@0: for (TInt blockNum = 0; blockNum < aNumBlocks; ++blockNum) sl@0: { sl@0: ModeDecryptStart(aBuffer); sl@0: sl@0: TUint16 R0, R1, R2, R3; sl@0: GetBlockLittleEndian(aBuffer, R0, R1, R2, R3); sl@0: sl@0: TInt i = 15; sl@0: for (; i >= 0; i--) sl@0: { sl@0: if (i == 4 || i == 10) sl@0: { sl@0: R3 -= iK[R2 & 63]; sl@0: R2 -= iK[R1 & 63]; sl@0: R1 -= iK[R0 & 63]; sl@0: R0 -= iK[R3 & 63]; sl@0: } sl@0: sl@0: R3 = rotrFixed(R3, 5); sl@0: R3 -= (R0 & ~R2) + (R1 & R2) + iK[4*i+3]; sl@0: sl@0: R2 = rotrFixed(R2, 3); sl@0: R2 -= (R3 & ~R1) + (R0 & R1) + iK[4*i+2]; sl@0: sl@0: R1 = rotrFixed(R1, 2); sl@0: R1 -= (R2 & ~R0) + (R3 & R0) + iK[4*i+1]; sl@0: sl@0: R0 = rotrFixed(R0, 1); sl@0: R0 -= (R1 & ~R3) + (R2 & R3) + iK[4*i+0]; sl@0: } sl@0: sl@0: PutBlockLittleEndian(aBuffer, R0, R1, R2, R3); sl@0: ModeDecryptEnd(aBuffer); sl@0: aBuffer += KRc2BlockBytes; sl@0: } sl@0: } sl@0: #pragma warning (default : 4244) // conversion from 'int' to 'unsigned short', possible loss of data