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>
28 using namespace SoftwareCrypto;
35 CDesImpl(KDesBlockBytes, aCryptoMode, aOperationMode, aPadding)
39 C3DesImpl* C3DesImpl::NewL(const CKey& aKey, TUid aCryptoMode, TUid aOperationMode, TUid aPadding)
41 C3DesImpl* self = C3DesImpl::NewLC(aKey, aCryptoMode, aOperationMode, aPadding);
42 CleanupStack::Pop(self);
46 C3DesImpl* C3DesImpl::NewLC(const CKey& aKey, TUid aCryptoMode, TUid aOperationMode, TUid aPadding)
48 C3DesImpl* self = new(ELeave) C3DesImpl(aCryptoMode, aOperationMode, aPadding);
49 CleanupStack::PushL(self);
50 self->ConstructL(aKey);
52 const TDesC8& keyContent = aKey.GetTDesC8L(KSymmetricKeyParameterUid);
53 TCrypto::IsSymmetricWeakEnoughL(BytesToBits(keyContent.Size()) - keyContent.Size());
57 C3DesImpl::~C3DesImpl()
59 // make sure key information isn't visible to other processes if the
61 Mem::FillZ(&iK1, sizeof(iK1));
62 Mem::FillZ(&iK2, sizeof(iK2));
63 Mem::FillZ(&iK3, sizeof(iK3));
66 void C3DesImpl::ConstructL(const CKey& aKey)
68 CDesImpl::ConstructL(aKey);
72 CExtendedCharacteristics* C3DesImpl::CreateExtendedCharacteristicsL()
74 // All Symbian software plug-ins have unlimited concurrency, cannot be reserved
75 // for exclusive use and are not CERTIFIED to be standards compliant.
76 return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
79 const CExtendedCharacteristics* C3DesImpl::GetExtendedCharacteristicsL()
81 return C3DesImpl::CreateExtendedCharacteristicsL();
84 TUid C3DesImpl::ImplementationUid() const
86 return KCryptoPlugin3DesUid;
89 TBool C3DesImpl::IsValidKeyLength(TInt aKeyBytes) const
91 return (aKeyBytes == K3DesKeyBytes);
94 TInt C3DesImpl::GetKeyStrength() const
96 // Exclude parity bits from each subkey
97 return BytesToBits(K3DesKeyBytes - (3 * 8));
100 void C3DesImpl::TransformEncrypt(
104 for (TInt i = 0; i < aNumBlocks; ++i)
106 ModeEncryptStart(aBuffer);
109 // Split the block into 2 word-sized big endian portions
110 GetBlockBigEndian(aBuffer, l, r);
113 // The mode is applied to the entire operation and NOT
114 // for each DES transform
115 TUid opMode = iOperationMode;
116 iOperationMode = KOperationModeECBUid;
117 DoTransform(l, r, iK1);
118 DoTransform(r, l, iK2);
119 DoTransform(l, r, iK3);
120 iOperationMode = opMode;
123 // Put the portions back into the block as little endian
124 PutBlockBigEndian(aBuffer, r, l);
125 ModeEncryptEnd(aBuffer);
126 aBuffer += KDesBlockBytes;
130 void C3DesImpl::TransformDecrypt(
132 const TUint aNumBlocks)
134 for (TInt i = 0; i < aNumBlocks; ++i)
136 ModeDecryptStart(aBuffer);
139 // Split the block into 2 word-sized big endian portions
140 GetBlockBigEndian(aBuffer, l, r);
144 // The mode is applied to the entire operation and NOT
145 // for each DES transform
146 TUid opMode = iOperationMode;
147 iOperationMode = KOperationModeECBUid;
148 DoTransform(l, r, iK1);
149 DoTransform(r, l, iK2);
150 DoTransform(l, r, iK3);
151 iOperationMode = opMode;
154 // Put the portions back into the block as little endian
155 PutBlockBigEndian(aBuffer, r, l);
156 ModeDecryptEnd(aBuffer);
157 aBuffer += K3DesBlockBytes;
161 void C3DesImpl::SetKeySchedule()
163 if (iCryptoMode.iUid == KCryptoModeEncrypt)
165 // Encrypt -> Decrypt -> Encrypt
167 SetEncryptKeySchedule(iKey->Mid(0, KDesKeyBytes), iK1);
170 SetDecryptKeySchedule(iKey->Mid(KDesKeyBytes, 2 * KDesKeyBytes), iK2);
173 SetEncryptKeySchedule(iKey->Mid(2 * KDesKeyBytes), iK3);
177 // Decrypt -> Encrypt -> Decrypt
178 // Key order is reversed !
180 ASSERT(iCryptoMode.iUid == KCryptoModeDecrypt);
182 SetDecryptKeySchedule(iKey->Mid(0, KDesKeyBytes), iK3);
185 SetEncryptKeySchedule(iKey->Mid(KDesKeyBytes, 2 * KDesKeyBytes), iK2);
188 SetDecryptKeySchedule(iKey->Mid(2 * KDesKeyBytes), iK1);