Update contrib.
2 * Copyright (c) 2002-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 "../common/inlines.h"
22 #include <cryptostrength.h>
24 const TInt KRC2BlockBytes = 8;
28 void CRC2::SetKey(const TDesC8& aKey, TInt aEffectiveKeyLenBits)
30 TUint keyLen = (TUint)aKey.Size();
33 iEffectiveKeyLenBits = aEffectiveKeyLenBits;
35 TUint8 L[KRC2MaxKeySizeBytes];
36 Mem::Copy((TUint8*)&L[0], (TUint8*)&aKey[0], keyLen);
38 TInt maxKeySizeBytes = (TInt)KRC2MaxKeySizeBytes;
39 TInt expandedKeyLen = (TInt)KRC2ExpandedKeyLen;
41 for (; i < maxKeySizeBytes; i++)
43 L[i] = RC2_TABLE::PITABLE[(L[i-1] + L[i-keyLen]) & 255];
46 TUint T8 = (aEffectiveKeyLenBits+7) / 8;
47 TUint8 TM = (TUint8)(255 >> ((8-(iEffectiveKeyLenBits%8))%8));
48 L[128-T8] = RC2_TABLE::PITABLE[L[128-T8] & TM];
50 for (i=127-T8; i>=0; i--)
51 L[i] = RC2_TABLE::PITABLE[L[i+1] ^ L[i+T8]];
53 for (i=0; i < expandedKeyLen; i++)
54 iK[i] = (TUint16)(L[2*i] + (L[2*i+1] << 8));
59 SetKey(iKey, iEffectiveKeyLenBits);
62 TInt CRC2::BlockSize() const
64 return KRC2BlockBytes;
67 TInt CRC2::KeySize() const
78 EXPORT_C CRC2Encryptor* CRC2Encryptor::NewL(const TDesC8& aKey,
79 TInt aEffectiveKeyLenBits)
81 CRC2Encryptor* me = CRC2Encryptor::NewLC(aKey, aEffectiveKeyLenBits);
82 CleanupStack::Pop(me);
86 EXPORT_C CRC2Encryptor* CRC2Encryptor::NewLC(const TDesC8& aKey,
87 TInt aEffectiveKeyLenBits)
89 CRC2Encryptor* me = new (ELeave) CRC2Encryptor;
90 CleanupStack::PushL(me); // Does not leave but function requires it be Push-ed
91 me->SetKey(aKey, aEffectiveKeyLenBits);
92 // weak enough if either aKey or aEffectiveKeyLenBits is weak
93 TInt minKeySize = Min(aEffectiveKeyLenBits, BytesToBits(aKey.Size()));
94 TCrypto::IsSymmetricWeakEnoughL(minKeySize);
98 #pragma warning (disable : 4244) // conversion from 'int' to 'unsigned short', possible loss of data
99 void CRC2Encryptor::Transform(TDes8& aBlock)
101 assert(aBlock.Size() == KRC2BlockBytes);
103 TUint16 R0, R1, R2, R3;
104 GetBlockLittleEndian((TUint8*)&aBlock[0], R0, R1, R2, R3);
109 R0 += (R1 & ~R3) + (R2 & R3) + iK[4*i+0];
110 R0 = rotlFixed(R0, 1);
112 R1 += (R2 & ~R0) + (R3 & R0) + iK[4*i+1];
113 R1 = rotlFixed(R1, 2);
115 R2 += (R3 & ~R1) + (R0 & R1) + iK[4*i+2];
116 R2 = rotlFixed(R2, 3);
118 R3 += (R0 & ~R2) + (R1 & R2) + iK[4*i+3];
119 R3 = rotlFixed(R3, 5);
121 if (i == 4 || i == 10)
130 PutBlockLittleEndian((TUint8*)&aBlock[0], R0, R1, R2, R3);
132 #pragma warning (default : 4244) // conversion from 'int' to 'unsigned short', possible loss of data
134 CRC2Encryptor::CRC2Encryptor(void)
140 EXPORT_C CRC2Decryptor* CRC2Decryptor::NewL(const TDesC8& aKey,
141 TInt aEffectiveKeyLenBits)
143 CRC2Decryptor* me = CRC2Decryptor::NewLC(aKey, aEffectiveKeyLenBits);
144 CleanupStack::Pop(me);
148 EXPORT_C CRC2Decryptor* CRC2Decryptor::NewLC(const TDesC8& aKey,
149 TInt aEffectiveKeyLenBits)
151 CRC2Decryptor* me = new (ELeave) CRC2Decryptor;
152 CleanupStack::PushL(me); // Does not leave but function requires it be Push-ed
153 me->SetKey(aKey, aEffectiveKeyLenBits);
154 // weak enough if either aKey or aEffectiveKeyLenBits is weak
155 TInt minKeySize = Min(aEffectiveKeyLenBits, BytesToBits(aKey.Size()));
156 TCrypto::IsSymmetricWeakEnoughL(minKeySize);
160 #pragma warning (disable : 4244) // conversion from 'int' to 'unsigned short', possible loss of data
161 void CRC2Decryptor::Transform(TDes8& aBlock)
163 assert(aBlock.Size() == KRC2BlockBytes);
165 TUint16 R0, R1, R2, R3;
166 GetBlockLittleEndian((TUint8*)&aBlock[0], R0, R1, R2, R3);
171 if (i == 4 || i == 10)
179 R3 = rotrFixed(R3, 5);
180 R3 -= (R0 & ~R2) + (R1 & R2) + iK[4*i+3];
182 R2 = rotrFixed(R2, 3);
183 R2 -= (R3 & ~R1) + (R0 & R1) + iK[4*i+2];
185 R1 = rotrFixed(R1, 2);
186 R1 -= (R2 & ~R0) + (R3 & R0) + iK[4*i+1];
188 R0 = rotrFixed(R0, 1);
189 R0 -= (R1 & ~R3) + (R2 & R3) + iK[4*i+0];
192 PutBlockLittleEndian((TUint8*)&aBlock[0], R0, R1, R2, R3);
195 #pragma warning (default : 4244) // conversion from 'int' to 'unsigned short', possible loss of data
197 CRC2Decryptor::CRC2Decryptor(void)