sl@0: /* sl@0: * Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "3des.h" sl@0: #include "../common/inlines.h" sl@0: #include "des.inl" sl@0: #include sl@0: sl@0: const TInt K3DESBlockBytes = 8; sl@0: const TInt K3DESKeyBytes = 24; sl@0: const TInt KDESKeyBytes = 8; sl@0: sl@0: void C3DES::Transform(TDes8& aBlock) sl@0: { sl@0: assert(aBlock.Size() == K3DESBlockBytes); sl@0: sl@0: TUint32 l, r; sl@0: // Split the block into 2 word-sized big endian portions sl@0: GetBlockBigEndian((TUint8*)&aBlock[0], l, r); sl@0: sl@0: IPerm(l,r); sl@0: sl@0: DoTransform(l, r, iK1); sl@0: DoTransform(r, l, iK2); sl@0: DoTransform(l, r, iK3); sl@0: sl@0: FPerm(l,r); sl@0: sl@0: // Put the portions back into the block as little endian sl@0: PutBlockBigEndian((TUint8*)&aBlock[0], r, l); sl@0: } sl@0: sl@0: TInt C3DES::BlockSize() const sl@0: { sl@0: return K3DESBlockBytes; sl@0: } sl@0: sl@0: TInt C3DES::KeySize() const sl@0: { sl@0: return K3DESKeyBytes; sl@0: } sl@0: sl@0: C3DES::C3DES() sl@0: { sl@0: } sl@0: sl@0: void C3DES::ConstructL(const TDesC8& aKey) sl@0: { sl@0: assert(aKey.Size() == K3DESKeyBytes); sl@0: sl@0: iKey = aKey.AllocL(); sl@0: DoSetKey(*iKey); sl@0: } sl@0: sl@0: void C3DES::Reset() sl@0: { sl@0: DoSetKey(*iKey); sl@0: } sl@0: sl@0: /* C3DESEncryptor */ sl@0: sl@0: EXPORT_C C3DESEncryptor* C3DESEncryptor::NewL(const TDesC8& aKey) sl@0: { sl@0: C3DESEncryptor* me = C3DESEncryptor::NewLC(aKey); sl@0: CleanupStack::Pop(me); sl@0: return (me); sl@0: } sl@0: sl@0: EXPORT_C C3DESEncryptor* C3DESEncryptor::NewLC(const TDesC8& aKey) sl@0: { sl@0: C3DESEncryptor* me = new (ELeave) C3DESEncryptor(); sl@0: CleanupStack::PushL(me); sl@0: me->ConstructL(aKey); sl@0: // DES only used 7 bits out of every key byte sl@0: TCrypto::IsSymmetricWeakEnoughL(BytesToBits(aKey.Size()) - aKey.Size()); sl@0: return (me); sl@0: } sl@0: sl@0: void C3DESEncryptor::DoSetKey(const TDesC8& aKey) sl@0: { sl@0: // Encryptor key sl@0: SetKey(aKey.Mid(0, KDESKeyBytes), iK1); sl@0: // Decryptor key sl@0: SetKey(aKey.Mid(KDESKeyBytes, 2*KDESKeyBytes), iK2); sl@0: ReverseKeySchedule(iK2); // Reverse key schedule order sl@0: // Encryptor key sl@0: SetKey(aKey.Mid(2*KDESKeyBytes), iK3); sl@0: } sl@0: sl@0: /* C3DESDecryptor */ sl@0: sl@0: EXPORT_C C3DESDecryptor* C3DESDecryptor::NewL(const TDesC8& aKey) sl@0: { sl@0: C3DESDecryptor* me = C3DESDecryptor::NewLC(aKey); sl@0: CleanupStack::Pop(me); sl@0: return (me); sl@0: } sl@0: sl@0: EXPORT_C C3DESDecryptor* C3DESDecryptor::NewLC(const TDesC8& aKey) sl@0: { sl@0: C3DESDecryptor* me = new (ELeave) C3DESDecryptor(); sl@0: CleanupStack::PushL(me); sl@0: me->ConstructL(aKey); sl@0: // DES only used 7 bits out of every key byte sl@0: TCrypto::IsSymmetricWeakEnoughL(BytesToBits(aKey.Size()) - aKey.Size()); sl@0: return (me); sl@0: } sl@0: sl@0: void C3DESDecryptor::DoSetKey(const TDesC8& aKey) sl@0: { sl@0: // 3DES decryption, reverse through key sl@0: // Decryptor key sl@0: CDES::SetKey(aKey.Mid(2*KDESKeyBytes), iK1); sl@0: ReverseKeySchedule(iK1); // Reverse key schedule order sl@0: sl@0: // Encryptor key sl@0: CDES::SetKey(aKey.Mid(KDESKeyBytes, 2*KDESKeyBytes), iK2); sl@0: sl@0: // Decryptor key sl@0: CDES::SetKey(aKey.Mid(0, KDESKeyBytes), iK3); sl@0: ReverseKeySchedule(iK3); // Reverse key schedule order sl@0: } sl@0: