Update contrib.
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.
19 #include "desextendimpl.h"
21 #include "destables.h"
22 #include "../../../source/common/inlines.h"
24 #include "pluginconfig.h"
25 #include "symmetriccipherimpl.h"
26 #include <cryptostrength.h>
29 // bit 0 is left-most in byte
30 static const TInt bytebit[] = {0200,0100,040,020,010,04,02,01};
32 //Extended Charcteristics
34 static const TInt32 KExtendCharAttribute1 = 0x102ABCD1;
35 static const TUid KExtendCharAttribute1Uid ={KExtendCharAttribute1};
37 static const TInt32 KExtendCharAttribute2 = 0x102ABCD2;
38 static const TUid KExtendCharAttribute2Uid ={KExtendCharAttribute2};
40 static const TInt32 KExtendCharAttribute3 = 0x102ABCD3;
41 static const TUid KExtendCharAttribute3Uid ={KExtendCharAttribute3};
43 using namespace SoftwareCrypto;
46 CDesExtendImpl::CDesExtendImpl(
47 TUid aImplementationUid,
52 CSymmetricBlockCipherImpl(aBlockBytes, aCryptoMode, aOperationMode, aPadding),
53 iImplementationUid(aImplementationUid)
57 CDesExtendImpl* CDesExtendImpl::NewL(TUid aImplementationUid, const CKey& aKey, TUid aCryptoMode, TUid aOperationMode, TUid aPadding)
59 CDesExtendImpl* self = CDesExtendImpl::NewLC(aImplementationUid, aKey, aCryptoMode, aOperationMode, aPadding);
60 CleanupStack::Pop(self);
64 CDesExtendImpl* CDesExtendImpl::NewLC(TUid aImplementationUid, const CKey& aKey, TUid aCryptoMode, TUid aOperationMode, TUid aPadding)
66 CDesExtendImpl* self = new(ELeave) CDesExtendImpl(aImplementationUid, KDesBlockBytes, aCryptoMode, aOperationMode, aPadding);
67 CleanupStack::PushL(self);
68 self->ConstructL(aKey);
70 const TDesC8& keyContent = aKey.GetTDesC8L(KSymmetricKeyParameterUid);
71 TCrypto::IsSymmetricWeakEnoughL(BytesToBits(keyContent.Size()) - keyContent.Size());
75 CDesExtendImpl::~CDesExtendImpl()
77 // make sure key information isn't visible to other processes if the
80 Mem::FillZ(&iK, sizeof(iK));
83 void CDesExtendImpl::ConstructL(const CKey& aKey)
85 CSymmetricBlockCipherImpl::ConstructL(aKey);
86 iExtendChars = CreateExtendedCharacteristicsL();
90 CExtendedCharacteristics* CDesExtendImpl::CreateExtendedCharacteristicsL()
92 // All Symbian software plug-ins have unlimited concurrency, cannot be reserved
93 // for exclusive use and are not CERTIFIED to be standards compliant.
95 //***************************************************************
96 CExtendedCharacteristics* exChars = CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
97 CleanupStack::PushL(exChars);
99 exChars->AddCharacteristicL(1234,KExtendCharAttribute1Uid);
100 exChars->AddCharacteristicL(5678,KExtendCharAttribute2Uid);
101 exChars->AddCharacteristicL(_L8("HAPPYDAYS"),KExtendCharAttribute3Uid);
102 //**************************************************************
103 CleanupStack::Pop(exChars);
108 const CExtendedCharacteristics* CDesExtendImpl::GetExtendedCharacteristicsL()
113 TUid CDesExtendImpl::ImplementationUid() const
115 return iImplementationUid;
118 TBool CDesExtendImpl::IsValidKeyLength(TInt aKeyBytes) const
120 return (aKeyBytes == KDesKeyBytes);
123 TInt CDesExtendImpl::GetKeyStrength() const
125 // parity bits are excluded
126 return BytesToBits(KDesKeyBytes - 8);
129 void CDesExtendImpl::TransformEncrypt(
133 for (TInt i = 0; i < aNumBlocks; ++i)
135 ModeEncryptStart(aBuffer);
137 // Split the block into 2 word-sized big endian portions
138 GetBlockBigEndian(aBuffer, l, r);
140 DoTransform(l, r, iK);
143 // Put the portions back into the block as little endian
144 PutBlockBigEndian(aBuffer, r, l);
146 ModeEncryptEnd(aBuffer);
147 aBuffer += KDesBlockBytes;
151 void CDesExtendImpl::TransformDecrypt(
155 for (TInt i = 0; i < aNumBlocks; ++i)
157 ModeDecryptStart(aBuffer);
160 // Split the block into 2 word-sized big endian portions
161 GetBlockBigEndian(aBuffer, l, r);
164 DoTransform(l, r, iK);
167 // Put the portions back into the block as little endian
168 PutBlockBigEndian(aBuffer, r, l);
170 ModeDecryptEnd(aBuffer);
171 aBuffer += KDesBlockBytes;
175 void CDesExtendImpl::SetKeySchedule()
177 if (iCryptoMode.iUid == KCryptoModeEncrypt)
179 SetEncryptKeySchedule(*iKey, iK);
183 ASSERT(iCryptoMode.iUid == KCryptoModeDecrypt);
184 SetDecryptKeySchedule(*iKey, iK);
188 void CDesExtendImpl::DoTransform(TUint32& l, TUint32& r, const TUint32* aKeySchedule)
193 TUint32 work = rotrFixed(r, 4U) ^ aKeySchedule[4*i+0];
194 l ^= DES_TABLE::sbox[6][(work) & 0x3f]
195 ^ DES_TABLE::sbox[4][(work >> 8) & 0x3f]
196 ^ DES_TABLE::sbox[2][(work >> 16) & 0x3f]
197 ^ DES_TABLE::sbox[0][(work >> 24) & 0x3f];
198 work = r ^ aKeySchedule[4*i+1];
199 l ^= DES_TABLE::sbox[7][(work) & 0x3f]
200 ^ DES_TABLE::sbox[5][(work >> 8) & 0x3f]
201 ^ DES_TABLE::sbox[3][(work >> 16) & 0x3f]
202 ^ DES_TABLE::sbox[1][(work >> 24) & 0x3f];
204 work = rotrFixed(l, 4U) ^ aKeySchedule[4*i+2];
205 r ^= DES_TABLE::sbox[6][(work) & 0x3f]
206 ^ DES_TABLE::sbox[4][(work >> 8) & 0x3f]
207 ^ DES_TABLE::sbox[2][(work >> 16) & 0x3f]
208 ^ DES_TABLE::sbox[0][(work >> 24) & 0x3f];
209 work = l ^ aKeySchedule[4*i+3];
210 r ^= DES_TABLE::sbox[7][(work) & 0x3f]
211 ^ DES_TABLE::sbox[5][(work >> 8) & 0x3f]
212 ^ DES_TABLE::sbox[3][(work >> 16) & 0x3f]
213 ^ DES_TABLE::sbox[1][(work >> 24) & 0x3f];
217 void CDesExtendImpl::SetEncryptKeySchedule(const TDesC8& aKey, TUint32* aKeySchedule)
219 TInt i=0, j=0, l=0, m=0;
221 // Form a byte array from aKey, taking endianess into account (little->big)
222 TUint8 key[8]; // For big endian byte array
223 Mem::Copy(&key, &aKey[0], 8);
225 TUint8 buffer[56+56+8];
226 TUint8* const pc1m = &buffer[0]; /* place to modify pc1 into */
227 TUint8* const pcr = pc1m + 56; /* place to rotate pc1 into */
228 TUint8* const ks = pcr + 56;
231 {/* convert pc1 to bits of key */
232 l = DES_TABLE::pc1[j]-1; /* integer bit location */
233 m = l & 07; /* find bit */
234 pc1m[j]=(key[l>>3] & /* find which key byte l is in */
235 bytebit[m]) /* and which bit of that byte */
236 ? (TUint8)1 : (TUint8)0; /* and store 1-bit result */
240 {/* key chunk for each iteration */
241 Mem::FillZ(ks,8); /* Clear key schedule */
243 /* rotate pc1 the right amount */
244 pcr[j] = pc1m[(l=j+DES_TABLE::totrot[i])<(j<28? 28 : 56) ? l: l-28];
246 /* rotate left and right halves independently */
249 {/* select bits individually */
250 /* check bit that goes to ks[j] */
251 if (pcr[DES_TABLE::pc2[j]-1])
252 {/* mask it in if it's there */
254 ks[j/6] |= bytebit[l] >> 2;
258 /* Now convert to odd/even interleaved form for use in F */
259 (*(aKeySchedule+(2*i))) = ((TUint32)ks[0] << 24)
260 | ((TUint32)ks[2] << 16)
261 | ((TUint32)ks[4] << 8)
264 (*(aKeySchedule+(2*i+1))) = ((TUint32)ks[1] << 24)
265 | ((TUint32)ks[3] << 16)
266 | ((TUint32)ks[5] << 8)
271 void CDesExtendImpl::SetDecryptKeySchedule(const TDesC8& aKey, TUint32* aKeySchedule)
273 SetEncryptKeySchedule(aKey, aKeySchedule);
274 ReverseKeySchedule(aKeySchedule);