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.
23 #include "common/inlines.h"
24 #include "pluginconfig.h"
25 #include "symmetriccipherimpl.h"
26 #include <cryptostrength.h>
28 using namespace SoftwareCrypto;
34 CSymmetricBlockCipherImpl(KRc2BlockBytes, aCryptoMode, aOperationMode, aPadding)
38 CRc2Impl* CRc2Impl::NewL(
43 TInt aEffectiveKeyLenBits)
45 CRc2Impl* self = CRc2Impl::NewLC(aKey, aCryptoMode, aOperationMode, aPadding, aEffectiveKeyLenBits);
46 CleanupStack::Pop(self);
50 CRc2Impl* CRc2Impl::NewLC(
55 TInt aEffectiveKeyLenBits)
57 CRc2Impl* self = new(ELeave) CRc2Impl(aCryptoMode, aOperationMode, aPadding);
58 CleanupStack::PushL(self);
59 self->ConstructL(aKey, aEffectiveKeyLenBits);
61 const TDesC8& keyContent = aKey.GetTDesC8L(KSymmetricKeyParameterUid);
62 TCrypto::IsSymmetricWeakEnoughL(BytesToBits(keyContent.Size()) - keyContent.Size());
68 Mem::FillZ(&iK, sizeof(iK));
71 void CRc2Impl::ConstructL(const CKey& aKey, TInt aEffectiveKeyLenBits)
73 iEffectiveKeyLenBits = aEffectiveKeyLenBits;
74 CSymmetricBlockCipherImpl::ConstructL(aKey);
78 CExtendedCharacteristics* CRc2Impl::CreateExtendedCharacteristicsL()
80 // All Symbian software plug-ins have unlimited concurrency, cannot be reserved
81 // for exclusive use and are not CERTIFIED to be standards compliant.
82 return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
85 const CExtendedCharacteristics* CRc2Impl::GetExtendedCharacteristicsL()
87 return CRc2Impl::CreateExtendedCharacteristicsL();
90 TUid CRc2Impl::ImplementationUid() const
92 return KCryptoPluginRc2Uid;
95 TBool CRc2Impl::IsValidKeyLength(TInt aKeyBytes) const
97 return ((aKeyBytes > 0 && aKeyBytes <= KRc2MaxKeySizeBytes) ? ETrue : EFalse);
100 TInt CRc2Impl::GetKeyStrength() const
102 return Min(iEffectiveKeyLenBits, BytesToBits(iKeyBytes));
105 void CRc2Impl::SetKeySchedule()
107 TUint keyLen = iKey->Length();
109 TUint8 L[KRc2MaxKeySizeBytes];
110 Mem::Copy((TUint8*)&L[0], (TUint8*)&(*iKey)[0], keyLen);
113 for (; i < KRc2MaxKeySizeBytes; i++)
115 L[i] = RC2_TABLE::PITABLE[(L[i-1] + L[i-keyLen]) & 255];
118 TUint T8 = (iEffectiveKeyLenBits+7) / 8;
119 TUint8 TM = (TUint8)(255 >> ((8-(iEffectiveKeyLenBits%8))%8));
120 L[128-T8] = RC2_TABLE::PITABLE[L[128-T8] & TM];
122 for (i=127-T8; i>=0; i--)
123 L[i] = RC2_TABLE::PITABLE[L[i+1] ^ L[i+T8]];
125 for (i=0; i < KRc2ExpandedKeyLen; i++)
126 iK[i] = (TUint16)(L[2*i] + (L[2*i+1] << 8));
129 #pragma warning (disable : 4244) // conversion from 'int' to 'unsigned short', possible loss of data
130 void CRc2Impl::TransformEncrypt(
134 for (TInt blockNum = 0; blockNum < aNumBlocks; ++blockNum)
136 ModeEncryptStart(aBuffer);
138 TUint16 R0, R1, R2, R3;
139 GetBlockLittleEndian(aBuffer, R0, R1, R2, R3);
144 R0 += (R1 & ~R3) + (R2 & R3) + iK[4*i+0];
145 R0 = rotlFixed(R0, 1);
147 R1 += (R2 & ~R0) + (R3 & R0) + iK[4*i+1];
148 R1 = rotlFixed(R1, 2);
150 R2 += (R3 & ~R1) + (R0 & R1) + iK[4*i+2];
151 R2 = rotlFixed(R2, 3);
153 R3 += (R0 & ~R2) + (R1 & R2) + iK[4*i+3];
154 R3 = rotlFixed(R3, 5);
156 if (i == 4 || i == 10)
164 PutBlockLittleEndian(aBuffer, R0, R1, R2, R3);
165 ModeEncryptEnd(aBuffer);
166 aBuffer += KRc2BlockBytes;
169 #pragma warning (default : 4244) // conversion from 'int' to 'unsigned short', possible loss of data
172 #pragma warning (disable : 4244) // conversion from 'int' to 'unsigned short', possible loss of data
173 void CRc2Impl::TransformDecrypt(
177 for (TInt blockNum = 0; blockNum < aNumBlocks; ++blockNum)
179 ModeDecryptStart(aBuffer);
181 TUint16 R0, R1, R2, R3;
182 GetBlockLittleEndian(aBuffer, R0, R1, R2, R3);
187 if (i == 4 || i == 10)
195 R3 = rotrFixed(R3, 5);
196 R3 -= (R0 & ~R2) + (R1 & R2) + iK[4*i+3];
198 R2 = rotrFixed(R2, 3);
199 R2 -= (R3 & ~R1) + (R0 & R1) + iK[4*i+2];
201 R1 = rotrFixed(R1, 2);
202 R1 -= (R2 & ~R0) + (R3 & R0) + iK[4*i+1];
204 R0 = rotrFixed(R0, 1);
205 R0 -= (R1 & ~R3) + (R2 & R3) + iK[4*i+0];
208 PutBlockLittleEndian(aBuffer, R0, R1, R2, R3);
209 ModeDecryptEnd(aBuffer);
210 aBuffer += KRc2BlockBytes;
213 #pragma warning (default : 4244) // conversion from 'int' to 'unsigned short', possible loss of data