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 "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;
40 CSymmetricBlockCipherImpl(aBlockBytes, aCryptoMode, aOperationMode, aPadding)
44 CDesImpl* CDesImpl::NewL(const CKey& aKey, TUid aCryptoMode, TUid aOperationMode, TUid aPadding)
46 CDesImpl* self = CDesImpl::NewLC(aKey, aCryptoMode, aOperationMode, aPadding);
47 CleanupStack::Pop(self);
51 CDesImpl* CDesImpl::NewLC(const CKey& aKey, TUid aCryptoMode, TUid aOperationMode, TUid aPadding)
53 CDesImpl* self = new(ELeave) CDesImpl(KDesBlockBytes, aCryptoMode, aOperationMode, aPadding);
54 CleanupStack::PushL(self);
55 self->ConstructL(aKey);
57 const TDesC8& keyContent = aKey.GetTDesC8L(KSymmetricKeyParameterUid);
58 TCrypto::IsSymmetricWeakEnoughL(BytesToBits(keyContent.Size()) - keyContent.Size());
64 // make sure key information isn't visible to other processes if the
66 Mem::FillZ(&iK, sizeof(iK));
69 void CDesImpl::ConstructL(const CKey& aKey)
71 CSymmetricBlockCipherImpl::ConstructL(aKey);
75 CExtendedCharacteristics* CDesImpl::CreateExtendedCharacteristicsL()
77 // All Symbian software plug-ins have unlimited concurrency, cannot be reserved
78 // for exclusive use and are not CERTIFIED to be standards compliant.
79 return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
82 const CExtendedCharacteristics* CDesImpl::GetExtendedCharacteristicsL()
84 return CDesImpl::CreateExtendedCharacteristicsL();
87 TUid CDesImpl::ImplementationUid() const
89 return KCryptoPluginDesUid;
92 TBool CDesImpl::IsValidKeyLength(TInt aKeyBytes) const
94 return (aKeyBytes == KDesKeyBytes);
97 TInt CDesImpl::GetKeyStrength() const
99 // parity bits are excluded
100 return BytesToBits(KDesKeyBytes - 8);
103 void CDesImpl::TransformEncrypt(
107 for (TInt i = 0; i < aNumBlocks; ++i)
109 ModeEncryptStart(aBuffer);
111 // Split the block into 2 word-sized big endian portions
112 GetBlockBigEndian(aBuffer, l, r);
114 DoTransform(l, r, iK);
117 // Put the portions back into the block as little endian
118 PutBlockBigEndian(aBuffer, r, l);
120 ModeEncryptEnd(aBuffer);
121 aBuffer += KDesBlockBytes;
125 void CDesImpl::TransformDecrypt(
129 for (TInt i = 0; i < aNumBlocks; ++i)
131 ModeDecryptStart(aBuffer);
134 // Split the block into 2 word-sized big endian portions
135 GetBlockBigEndian(aBuffer, l, r);
138 DoTransform(l, r, iK);
141 // Put the portions back into the block as little endian
142 PutBlockBigEndian(aBuffer, r, l);
144 ModeDecryptEnd(aBuffer);
145 aBuffer += KDesBlockBytes;
149 void CDesImpl::SetKeySchedule()
151 if (iCryptoMode.iUid == KCryptoModeEncrypt)
153 SetEncryptKeySchedule(*iKey, iK);
157 ASSERT(iCryptoMode.iUid == KCryptoModeDecrypt);
158 SetDecryptKeySchedule(*iKey, iK);
162 void CDesImpl::DoTransform(TUint32& l, TUint32& r, const TUint32* aKeySchedule)
167 TUint32 work = rotrFixed(r, 4U) ^ aKeySchedule[4*i+0];
168 l ^= DES_TABLE::sbox[6][(work) & 0x3f]
169 ^ DES_TABLE::sbox[4][(work >> 8) & 0x3f]
170 ^ DES_TABLE::sbox[2][(work >> 16) & 0x3f]
171 ^ DES_TABLE::sbox[0][(work >> 24) & 0x3f];
172 work = r ^ aKeySchedule[4*i+1];
173 l ^= DES_TABLE::sbox[7][(work) & 0x3f]
174 ^ DES_TABLE::sbox[5][(work >> 8) & 0x3f]
175 ^ DES_TABLE::sbox[3][(work >> 16) & 0x3f]
176 ^ DES_TABLE::sbox[1][(work >> 24) & 0x3f];
178 work = rotrFixed(l, 4U) ^ aKeySchedule[4*i+2];
179 r ^= DES_TABLE::sbox[6][(work) & 0x3f]
180 ^ DES_TABLE::sbox[4][(work >> 8) & 0x3f]
181 ^ DES_TABLE::sbox[2][(work >> 16) & 0x3f]
182 ^ DES_TABLE::sbox[0][(work >> 24) & 0x3f];
183 work = l ^ aKeySchedule[4*i+3];
184 r ^= DES_TABLE::sbox[7][(work) & 0x3f]
185 ^ DES_TABLE::sbox[5][(work >> 8) & 0x3f]
186 ^ DES_TABLE::sbox[3][(work >> 16) & 0x3f]
187 ^ DES_TABLE::sbox[1][(work >> 24) & 0x3f];
191 void CDesImpl::SetEncryptKeySchedule(const TDesC8& aKey, TUint32* aKeySchedule)
193 TInt i=0, j=0, l=0, m=0;
195 // Form a byte array from aKey, taking endianess into account (little->big)
196 TUint8 key[8]; // For big endian byte array
197 Mem::Copy(&key, &aKey[0], 8);
199 TUint8 buffer[56+56+8];
200 TUint8* const pc1m = &buffer[0]; /* place to modify pc1 into */
201 TUint8* const pcr = pc1m + 56; /* place to rotate pc1 into */
202 TUint8* const ks = pcr + 56;
205 {/* convert pc1 to bits of key */
206 l = DES_TABLE::pc1[j]-1; /* integer bit location */
207 m = l & 07; /* find bit */
208 pc1m[j]=(key[l>>3] & /* find which key byte l is in */
209 bytebit[m]) /* and which bit of that byte */
210 ? (TUint8)1 : (TUint8)0; /* and store 1-bit result */
214 {/* key chunk for each iteration */
215 Mem::FillZ(ks,8); /* Clear key schedule */
217 /* rotate pc1 the right amount */
218 pcr[j] = pc1m[(l=j+DES_TABLE::totrot[i])<(j<28? 28 : 56) ? l: l-28];
220 /* rotate left and right halves independently */
223 {/* select bits individually */
224 /* check bit that goes to ks[j] */
225 if (pcr[DES_TABLE::pc2[j]-1])
226 {/* mask it in if it's there */
228 ks[j/6] |= bytebit[l] >> 2;
232 /* Now convert to odd/even interleaved form for use in F */
233 (*(aKeySchedule+(2*i))) = ((TUint32)ks[0] << 24)
234 | ((TUint32)ks[2] << 16)
235 | ((TUint32)ks[4] << 8)
238 (*(aKeySchedule+(2*i+1))) = ((TUint32)ks[1] << 24)
239 | ((TUint32)ks[3] << 16)
240 | ((TUint32)ks[5] << 8)
245 void CDesImpl::SetDecryptKeySchedule(const TDesC8& aKey, TUint32* aKeySchedule)
247 SetEncryptKeySchedule(aKey, aKeySchedule);
248 ReverseKeySchedule(aKeySchedule);