1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcrypto/source/symmetric/3des.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,138 @@
1.4 +/*
1.5 +* Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include "3des.h"
1.23 +#include "../common/inlines.h"
1.24 +#include "des.inl"
1.25 +#include <cryptostrength.h>
1.26 +
1.27 +const TInt K3DESBlockBytes = 8;
1.28 +const TInt K3DESKeyBytes = 24;
1.29 +const TInt KDESKeyBytes = 8;
1.30 +
1.31 +void C3DES::Transform(TDes8& aBlock)
1.32 + {
1.33 + assert(aBlock.Size() == K3DESBlockBytes);
1.34 +
1.35 + TUint32 l, r;
1.36 +// Split the block into 2 word-sized big endian portions
1.37 + GetBlockBigEndian((TUint8*)&aBlock[0], l, r);
1.38 +
1.39 + IPerm(l,r);
1.40 +
1.41 + DoTransform(l, r, iK1);
1.42 + DoTransform(r, l, iK2);
1.43 + DoTransform(l, r, iK3);
1.44 +
1.45 + FPerm(l,r);
1.46 +
1.47 +// Put the portions back into the block as little endian
1.48 + PutBlockBigEndian((TUint8*)&aBlock[0], r, l);
1.49 + }
1.50 +
1.51 +TInt C3DES::BlockSize() const
1.52 + {
1.53 + return K3DESBlockBytes;
1.54 + }
1.55 +
1.56 +TInt C3DES::KeySize() const
1.57 + {
1.58 + return K3DESKeyBytes;
1.59 + }
1.60 +
1.61 +C3DES::C3DES()
1.62 + {
1.63 + }
1.64 +
1.65 +void C3DES::ConstructL(const TDesC8& aKey)
1.66 + {
1.67 + assert(aKey.Size() == K3DESKeyBytes);
1.68 +
1.69 + iKey = aKey.AllocL();
1.70 + DoSetKey(*iKey);
1.71 + }
1.72 +
1.73 +void C3DES::Reset()
1.74 + {
1.75 + DoSetKey(*iKey);
1.76 + }
1.77 +
1.78 +/* C3DESEncryptor */
1.79 +
1.80 +EXPORT_C C3DESEncryptor* C3DESEncryptor::NewL(const TDesC8& aKey)
1.81 + {
1.82 + C3DESEncryptor* me = C3DESEncryptor::NewLC(aKey);
1.83 + CleanupStack::Pop(me);
1.84 + return (me);
1.85 + }
1.86 +
1.87 +EXPORT_C C3DESEncryptor* C3DESEncryptor::NewLC(const TDesC8& aKey)
1.88 + {
1.89 + C3DESEncryptor* me = new (ELeave) C3DESEncryptor();
1.90 + CleanupStack::PushL(me);
1.91 + me->ConstructL(aKey);
1.92 + // DES only used 7 bits out of every key byte
1.93 + TCrypto::IsSymmetricWeakEnoughL(BytesToBits(aKey.Size()) - aKey.Size());
1.94 + return (me);
1.95 + }
1.96 +
1.97 +void C3DESEncryptor::DoSetKey(const TDesC8& aKey)
1.98 + {
1.99 + // Encryptor key
1.100 + SetKey(aKey.Mid(0, KDESKeyBytes), iK1);
1.101 + // Decryptor key
1.102 + SetKey(aKey.Mid(KDESKeyBytes, 2*KDESKeyBytes), iK2);
1.103 + ReverseKeySchedule(iK2); // Reverse key schedule order
1.104 + // Encryptor key
1.105 + SetKey(aKey.Mid(2*KDESKeyBytes), iK3);
1.106 + }
1.107 +
1.108 +/* C3DESDecryptor */
1.109 +
1.110 +EXPORT_C C3DESDecryptor* C3DESDecryptor::NewL(const TDesC8& aKey)
1.111 + {
1.112 + C3DESDecryptor* me = C3DESDecryptor::NewLC(aKey);
1.113 + CleanupStack::Pop(me);
1.114 + return (me);
1.115 + }
1.116 +
1.117 +EXPORT_C C3DESDecryptor* C3DESDecryptor::NewLC(const TDesC8& aKey)
1.118 + {
1.119 + C3DESDecryptor* me = new (ELeave) C3DESDecryptor();
1.120 + CleanupStack::PushL(me);
1.121 + me->ConstructL(aKey);
1.122 + // DES only used 7 bits out of every key byte
1.123 + TCrypto::IsSymmetricWeakEnoughL(BytesToBits(aKey.Size()) - aKey.Size());
1.124 + return (me);
1.125 + }
1.126 +
1.127 +void C3DESDecryptor::DoSetKey(const TDesC8& aKey)
1.128 + {
1.129 + // 3DES decryption, reverse through key
1.130 + // Decryptor key
1.131 + CDES::SetKey(aKey.Mid(2*KDESKeyBytes), iK1);
1.132 + ReverseKeySchedule(iK1); // Reverse key schedule order
1.133 +
1.134 + // Encryptor key
1.135 + CDES::SetKey(aKey.Mid(KDESKeyBytes, 2*KDESKeyBytes), iK2);
1.136 +
1.137 + // Decryptor key
1.138 + CDES::SetKey(aKey.Mid(0, KDESKeyBytes), iK3);
1.139 + ReverseKeySchedule(iK3); // Reverse key schedule order
1.140 + }
1.141 +