First public contribution.
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.
20 #include "../common/inlines.h"
22 #include <cryptostrength.h>
24 const TInt K3DESBlockBytes = 8;
25 const TInt K3DESKeyBytes = 24;
26 const TInt KDESKeyBytes = 8;
28 void C3DES::Transform(TDes8& aBlock)
30 assert(aBlock.Size() == K3DESBlockBytes);
33 // Split the block into 2 word-sized big endian portions
34 GetBlockBigEndian((TUint8*)&aBlock[0], l, r);
38 DoTransform(l, r, iK1);
39 DoTransform(r, l, iK2);
40 DoTransform(l, r, iK3);
44 // Put the portions back into the block as little endian
45 PutBlockBigEndian((TUint8*)&aBlock[0], r, l);
48 TInt C3DES::BlockSize() const
50 return K3DESBlockBytes;
53 TInt C3DES::KeySize() const
62 void C3DES::ConstructL(const TDesC8& aKey)
64 assert(aKey.Size() == K3DESKeyBytes);
77 EXPORT_C C3DESEncryptor* C3DESEncryptor::NewL(const TDesC8& aKey)
79 C3DESEncryptor* me = C3DESEncryptor::NewLC(aKey);
80 CleanupStack::Pop(me);
84 EXPORT_C C3DESEncryptor* C3DESEncryptor::NewLC(const TDesC8& aKey)
86 C3DESEncryptor* me = new (ELeave) C3DESEncryptor();
87 CleanupStack::PushL(me);
89 // DES only used 7 bits out of every key byte
90 TCrypto::IsSymmetricWeakEnoughL(BytesToBits(aKey.Size()) - aKey.Size());
94 void C3DESEncryptor::DoSetKey(const TDesC8& aKey)
97 SetKey(aKey.Mid(0, KDESKeyBytes), iK1);
99 SetKey(aKey.Mid(KDESKeyBytes, 2*KDESKeyBytes), iK2);
100 ReverseKeySchedule(iK2); // Reverse key schedule order
102 SetKey(aKey.Mid(2*KDESKeyBytes), iK3);
107 EXPORT_C C3DESDecryptor* C3DESDecryptor::NewL(const TDesC8& aKey)
109 C3DESDecryptor* me = C3DESDecryptor::NewLC(aKey);
110 CleanupStack::Pop(me);
114 EXPORT_C C3DESDecryptor* C3DESDecryptor::NewLC(const TDesC8& aKey)
116 C3DESDecryptor* me = new (ELeave) C3DESDecryptor();
117 CleanupStack::PushL(me);
118 me->ConstructL(aKey);
119 // DES only used 7 bits out of every key byte
120 TCrypto::IsSymmetricWeakEnoughL(BytesToBits(aKey.Size()) - aKey.Size());
124 void C3DESDecryptor::DoSetKey(const TDesC8& aKey)
126 // 3DES decryption, reverse through key
128 CDES::SetKey(aKey.Mid(2*KDESKeyBytes), iK1);
129 ReverseKeySchedule(iK1); // Reverse key schedule order
132 CDES::SetKey(aKey.Mid(KDESKeyBytes, 2*KDESKeyBytes), iK2);
135 CDES::SetKey(aKey.Mid(0, KDESKeyBytes), iK3);
136 ReverseKeySchedule(iK3); // Reverse key schedule order