os/security/crypto/weakcrypto/source/symmetric/3des.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     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".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 *
    16 */
    17 
    18 
    19 #include "3des.h"
    20 #include "../common/inlines.h"
    21 #include "des.inl"
    22 #include <cryptostrength.h>
    23 
    24 const TInt K3DESBlockBytes = 8;
    25 const TInt K3DESKeyBytes = 24;
    26 const TInt KDESKeyBytes = 8;
    27 
    28 void C3DES::Transform(TDes8& aBlock)
    29 	{
    30 	assert(aBlock.Size() == K3DESBlockBytes);
    31 		
    32 	TUint32 l, r;
    33 //	Split the block into 2 word-sized big endian portions
    34 	GetBlockBigEndian((TUint8*)&aBlock[0], l, r);
    35 
    36 	IPerm(l,r);
    37 
    38 	DoTransform(l, r, iK1);
    39 	DoTransform(r, l, iK2);
    40 	DoTransform(l, r, iK3);
    41 		
    42 	FPerm(l,r);
    43 
    44 //	Put the portions back into the block as little endian
    45 	PutBlockBigEndian((TUint8*)&aBlock[0], r, l);
    46 	}
    47 
    48 TInt C3DES::BlockSize() const
    49 	{
    50 	return K3DESBlockBytes;
    51 	}
    52 
    53 TInt C3DES::KeySize() const
    54 	{
    55 	return K3DESKeyBytes;
    56 	}
    57 
    58 C3DES::C3DES()
    59 	{
    60 	}
    61 
    62 void C3DES::ConstructL(const TDesC8& aKey)
    63 	{
    64 	assert(aKey.Size() == K3DESKeyBytes);
    65 
    66 	iKey = aKey.AllocL();
    67 	DoSetKey(*iKey);
    68 	}
    69 
    70 void C3DES::Reset()
    71 	{
    72 	DoSetKey(*iKey);
    73 	}
    74 
    75 /* C3DESEncryptor */
    76 
    77 EXPORT_C C3DESEncryptor* C3DESEncryptor::NewL(const TDesC8& aKey)
    78 	{
    79 	C3DESEncryptor* me = C3DESEncryptor::NewLC(aKey);
    80 	CleanupStack::Pop(me);
    81 	return (me);	
    82 	}
    83 
    84 EXPORT_C C3DESEncryptor* C3DESEncryptor::NewLC(const TDesC8& aKey)
    85 	{
    86 	C3DESEncryptor* me = new (ELeave) C3DESEncryptor();
    87 	CleanupStack::PushL(me);
    88 	me->ConstructL(aKey);
    89 	// DES only used 7 bits out of every key byte
    90 	TCrypto::IsSymmetricWeakEnoughL(BytesToBits(aKey.Size()) - aKey.Size());
    91 	return (me);	
    92 	}
    93 
    94 void C3DESEncryptor::DoSetKey(const TDesC8& aKey)
    95 	{
    96 	// Encryptor key	
    97 	SetKey(aKey.Mid(0, KDESKeyBytes), iK1);
    98 	// Decryptor key
    99 	SetKey(aKey.Mid(KDESKeyBytes, 2*KDESKeyBytes), iK2);
   100 	ReverseKeySchedule(iK2); // Reverse key schedule order
   101 	// Encryptor key
   102 	SetKey(aKey.Mid(2*KDESKeyBytes), iK3);
   103 	}
   104  
   105 /* C3DESDecryptor */
   106 
   107 EXPORT_C C3DESDecryptor* C3DESDecryptor::NewL(const TDesC8& aKey)
   108 	{
   109 	C3DESDecryptor* me = C3DESDecryptor::NewLC(aKey);
   110 	CleanupStack::Pop(me);
   111 	return (me);
   112 	}
   113 
   114 EXPORT_C C3DESDecryptor* C3DESDecryptor::NewLC(const TDesC8& aKey)
   115 	{
   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());
   121 	return (me);
   122 	}
   123 
   124 void C3DESDecryptor::DoSetKey(const TDesC8& aKey)
   125 	{
   126 	// 3DES decryption, reverse through key
   127 	// Decryptor key
   128 	CDES::SetKey(aKey.Mid(2*KDESKeyBytes), iK1);
   129 	ReverseKeySchedule(iK1);	//	Reverse key schedule order	
   130 
   131 	// Encryptor key
   132 	CDES::SetKey(aKey.Mid(KDESKeyBytes, 2*KDESKeyBytes), iK2);
   133 	
   134 	// Decryptor key
   135 	CDES::SetKey(aKey.Mid(0, KDESKeyBytes), iK3);
   136 	ReverseKeySchedule(iK3);	//	Reverse key schedule order
   137 	}
   138