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.
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 using namespace SoftwareCrypto;
36 TUid aImplementationUid,
41 CSymmetricBlockCipherImpl(aBlockBytes, aCryptoMode, aOperationMode, aPadding),
42 iImplementationUid(aImplementationUid)
46 CDesImpl* CDesImpl::NewL(TUid aImplementationUid, const CKey& aKey, TUid aCryptoMode, TUid aOperationMode, TUid aPadding)
48 CDesImpl* self = CDesImpl::NewLC(aImplementationUid, aKey, aCryptoMode, aOperationMode, aPadding);
49 CleanupStack::Pop(self);
53 CDesImpl* CDesImpl::NewLC(TUid aImplementationUid, const CKey& aKey, TUid aCryptoMode, TUid aOperationMode, TUid aPadding)
55 CDesImpl* self = new(ELeave) CDesImpl(aImplementationUid, KDesBlockBytes, aCryptoMode, aOperationMode, aPadding);
56 CleanupStack::PushL(self);
57 self->ConstructL(aKey);
59 const TDesC8& keyContent = aKey.GetTDesC8L(KSymmetricKeyParameterUid);
60 TCrypto::IsSymmetricWeakEnoughL(BytesToBits(keyContent.Size()) - keyContent.Size());
66 // make sure key information isn't visible to other processes if the
68 Mem::FillZ(&iK, sizeof(iK));
71 void CDesImpl::ConstructL(const CKey& aKey)
73 CSymmetricBlockCipherImpl::ConstructL(aKey);
77 CExtendedCharacteristics* CDesImpl::CreateExtendedCharacteristicsL()
79 // All Symbian software plug-ins have unlimited concurrency, cannot be reserved
80 // for exclusive use and are not CERTIFIED to be standards compliant.
82 return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
85 const CExtendedCharacteristics* CDesImpl::GetExtendedCharacteristicsL()
87 return CDesImpl::CreateExtendedCharacteristicsL();
90 TUid CDesImpl::ImplementationUid() const
92 return iImplementationUid;
95 TBool CDesImpl::IsValidKeyLength(TInt aKeyBytes) const
97 return (aKeyBytes == KDesKeyBytes);
100 TInt CDesImpl::GetKeyStrength() const
102 // parity bits are excluded
103 return BytesToBits(KDesKeyBytes - 8);
106 void CDesImpl::TransformEncrypt(
110 for (TInt i = 0; i < aNumBlocks; ++i)
112 ModeEncryptStart(aBuffer);
114 // Split the block into 2 word-sized big endian portions
115 GetBlockBigEndian(aBuffer, l, r);
117 DoTransform(l, r, iK);
120 // Put the portions back into the block as little endian
121 PutBlockBigEndian(aBuffer, r, l);
123 ModeEncryptEnd(aBuffer);
124 aBuffer += KDesBlockBytes;
128 void CDesImpl::TransformDecrypt(
132 for (TInt i = 0; i < aNumBlocks; ++i)
134 ModeDecryptStart(aBuffer);
137 // Split the block into 2 word-sized big endian portions
138 GetBlockBigEndian(aBuffer, l, r);
141 DoTransform(l, r, iK);
144 // Put the portions back into the block as little endian
145 PutBlockBigEndian(aBuffer, r, l);
147 ModeDecryptEnd(aBuffer);
148 aBuffer += KDesBlockBytes;
152 void CDesImpl::SetKeySchedule()
154 if (iCryptoMode.iUid == KCryptoModeEncrypt)
156 SetEncryptKeySchedule(*iKey, iK);
160 ASSERT(iCryptoMode.iUid == KCryptoModeDecrypt);
161 SetDecryptKeySchedule(*iKey, iK);
165 void CDesImpl::DoTransform(TUint32& l, TUint32& r, const TUint32* aKeySchedule)
170 TUint32 work = rotrFixed(r, 4U) ^ aKeySchedule[4*i+0];
171 l ^= DES_TABLE::sbox[6][(work) & 0x3f]
172 ^ DES_TABLE::sbox[4][(work >> 8) & 0x3f]
173 ^ DES_TABLE::sbox[2][(work >> 16) & 0x3f]
174 ^ DES_TABLE::sbox[0][(work >> 24) & 0x3f];
175 work = r ^ aKeySchedule[4*i+1];
176 l ^= DES_TABLE::sbox[7][(work) & 0x3f]
177 ^ DES_TABLE::sbox[5][(work >> 8) & 0x3f]
178 ^ DES_TABLE::sbox[3][(work >> 16) & 0x3f]
179 ^ DES_TABLE::sbox[1][(work >> 24) & 0x3f];
181 work = rotrFixed(l, 4U) ^ aKeySchedule[4*i+2];
182 r ^= DES_TABLE::sbox[6][(work) & 0x3f]
183 ^ DES_TABLE::sbox[4][(work >> 8) & 0x3f]
184 ^ DES_TABLE::sbox[2][(work >> 16) & 0x3f]
185 ^ DES_TABLE::sbox[0][(work >> 24) & 0x3f];
186 work = l ^ aKeySchedule[4*i+3];
187 r ^= DES_TABLE::sbox[7][(work) & 0x3f]
188 ^ DES_TABLE::sbox[5][(work >> 8) & 0x3f]
189 ^ DES_TABLE::sbox[3][(work >> 16) & 0x3f]
190 ^ DES_TABLE::sbox[1][(work >> 24) & 0x3f];
194 void CDesImpl::SetEncryptKeySchedule(const TDesC8& aKey, TUint32* aKeySchedule)
196 TInt i=0, j=0, l=0, m=0;
198 // Form a byte array from aKey, taking endianess into account (little->big)
199 TUint8 key[8]; // For big endian byte array
200 Mem::Copy(&key, &aKey[0], 8);
202 TUint8 buffer[56+56+8];
203 TUint8* const pc1m = &buffer[0]; /* place to modify pc1 into */
204 TUint8* const pcr = pc1m + 56; /* place to rotate pc1 into */
205 TUint8* const ks = pcr + 56;
208 {/* convert pc1 to bits of key */
209 l = DES_TABLE::pc1[j]-1; /* integer bit location */
210 m = l & 07; /* find bit */
211 pc1m[j]=(key[l>>3] & /* find which key byte l is in */
212 bytebit[m]) /* and which bit of that byte */
213 ? (TUint8)1 : (TUint8)0; /* and store 1-bit result */
217 {/* key chunk for each iteration */
218 Mem::FillZ(ks,8); /* Clear key schedule */
220 /* rotate pc1 the right amount */
221 pcr[j] = pc1m[(l=j+DES_TABLE::totrot[i])<(j<28? 28 : 56) ? l: l-28];
223 /* rotate left and right halves independently */
226 {/* select bits individually */
227 /* check bit that goes to ks[j] */
228 if (pcr[DES_TABLE::pc2[j]-1])
229 {/* mask it in if it's there */
231 ks[j/6] |= bytebit[l] >> 2;
235 /* Now convert to odd/even interleaved form for use in F */
236 (*(aKeySchedule+(2*i))) = ((TUint32)ks[0] << 24)
237 | ((TUint32)ks[2] << 16)
238 | ((TUint32)ks[4] << 8)
241 (*(aKeySchedule+(2*i+1))) = ((TUint32)ks[1] << 24)
242 | ((TUint32)ks[3] << 16)
243 | ((TUint32)ks[5] << 8)
248 void CDesImpl::SetDecryptKeySchedule(const TDesC8& aKey, TUint32* aKeySchedule)
250 SetEncryptKeySchedule(aKey, aKeySchedule);
251 ReverseKeySchedule(aKeySchedule);