crypto/weakcrypto/source/symmetric/rc2.cpp
changeset 72 de46a57f75fb
equal deleted inserted replaced
65:970c0057d9bc 72:de46a57f75fb
       
     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 "rc2.h"
       
    20 #include "rc2table.h"
       
    21 #include "../common/inlines.h"
       
    22 #include <cryptostrength.h>
       
    23 
       
    24 const TInt KRC2BlockBytes = 8;
       
    25 
       
    26 /* CRC2Encryptor */
       
    27 
       
    28 void CRC2::SetKey(const TDesC8& aKey, TInt aEffectiveKeyLenBits)
       
    29 	{
       
    30 	TUint keyLen = (TUint)aKey.Size();
       
    31 
       
    32 	iKey.Copy(aKey);
       
    33 	iEffectiveKeyLenBits = aEffectiveKeyLenBits;
       
    34 
       
    35 	TUint8 L[KRC2MaxKeySizeBytes];	
       
    36 	Mem::Copy((TUint8*)&L[0], (TUint8*)&aKey[0], keyLen);
       
    37 
       
    38 	TInt maxKeySizeBytes = (TInt)KRC2MaxKeySizeBytes;
       
    39 	TInt expandedKeyLen = (TInt)KRC2ExpandedKeyLen;
       
    40 	TInt i = keyLen;
       
    41 	for (; i < maxKeySizeBytes; i++)
       
    42 		{
       
    43 		L[i] = RC2_TABLE::PITABLE[(L[i-1] + L[i-keyLen]) & 255];
       
    44 		}
       
    45 
       
    46 	TUint T8 = (aEffectiveKeyLenBits+7) / 8;
       
    47 	TUint8 TM = (TUint8)(255 >> ((8-(iEffectiveKeyLenBits%8))%8));
       
    48 	L[128-T8] = RC2_TABLE::PITABLE[L[128-T8] & TM];
       
    49 
       
    50 	for (i=127-T8; i>=0; i--)
       
    51 		L[i] = RC2_TABLE::PITABLE[L[i+1] ^ L[i+T8]];
       
    52 
       
    53 	for (i=0; i < expandedKeyLen; i++)
       
    54 		iK[i] = (TUint16)(L[2*i] + (L[2*i+1] << 8));
       
    55 	}
       
    56 
       
    57 void CRC2::Reset()
       
    58 	{
       
    59 	SetKey(iKey, iEffectiveKeyLenBits);
       
    60 	}
       
    61 
       
    62 TInt CRC2::BlockSize() const
       
    63 	{
       
    64 	return KRC2BlockBytes;
       
    65 	}
       
    66 
       
    67 TInt CRC2::KeySize() const
       
    68 	{
       
    69 	return iKey.Size();
       
    70 	}
       
    71 
       
    72 CRC2::CRC2(void)
       
    73 	{
       
    74 	}
       
    75 
       
    76 /* CRC2Encryptor */
       
    77 
       
    78 EXPORT_C CRC2Encryptor* CRC2Encryptor::NewL(const TDesC8& aKey, 
       
    79 	TInt aEffectiveKeyLenBits)
       
    80 	{
       
    81 	CRC2Encryptor* me = CRC2Encryptor::NewLC(aKey, aEffectiveKeyLenBits);
       
    82 	CleanupStack::Pop(me);
       
    83 	return (me);
       
    84 	}
       
    85 
       
    86 EXPORT_C CRC2Encryptor* CRC2Encryptor::NewLC(const TDesC8& aKey, 
       
    87 	TInt aEffectiveKeyLenBits)
       
    88 	{
       
    89 	CRC2Encryptor* me = new (ELeave) CRC2Encryptor;
       
    90 	CleanupStack::PushL(me);	//	Does not leave but function requires it be Push-ed
       
    91 	me->SetKey(aKey, aEffectiveKeyLenBits);	
       
    92 	// weak enough if either aKey or aEffectiveKeyLenBits is weak
       
    93 	TInt minKeySize = Min(aEffectiveKeyLenBits, BytesToBits(aKey.Size()));
       
    94 	TCrypto::IsSymmetricWeakEnoughL(minKeySize);
       
    95 	return (me);
       
    96 	}
       
    97 
       
    98 #pragma warning (disable : 4244)	//	conversion from 'int' to 'unsigned short', possible loss of data
       
    99 void CRC2Encryptor::Transform(TDes8& aBlock)
       
   100 	{
       
   101 	assert(aBlock.Size() == KRC2BlockBytes);
       
   102 	
       
   103 	TUint16 R0, R1, R2, R3;
       
   104 	GetBlockLittleEndian((TUint8*)&aBlock[0], R0, R1, R2, R3);
       
   105 	
       
   106 	TInt i = 0;
       
   107 	for (; i < 16; i++)
       
   108 		{
       
   109 		R0 += (R1 & ~R3) + (R2 & R3) + iK[4*i+0];
       
   110 		R0 = rotlFixed(R0, 1);
       
   111 
       
   112 		R1 += (R2 & ~R0) + (R3 & R0) + iK[4*i+1];
       
   113 		R1 = rotlFixed(R1, 2);
       
   114 
       
   115 		R2 += (R3 & ~R1) + (R0 & R1) + iK[4*i+2];
       
   116 		R2 = rotlFixed(R2, 3);
       
   117 
       
   118 		R3 += (R0 & ~R2) + (R1 & R2) + iK[4*i+3];
       
   119 		R3 = rotlFixed(R3, 5);
       
   120 
       
   121 		if (i == 4 || i == 10)
       
   122 			{
       
   123 			R0 += iK[R3 & 63];
       
   124 			R1 += iK[R0 & 63];
       
   125 			R2 += iK[R1 & 63];
       
   126 			R3 += iK[R2 & 63];
       
   127 			}
       
   128 		}
       
   129 
       
   130 	PutBlockLittleEndian((TUint8*)&aBlock[0], R0, R1, R2, R3);	
       
   131 	}
       
   132 #pragma warning (default : 4244)	//	conversion from 'int' to 'unsigned short', possible loss of data
       
   133 
       
   134 CRC2Encryptor::CRC2Encryptor(void)
       
   135 	{
       
   136 	}
       
   137 
       
   138 /* CRC2Decryptor */
       
   139 
       
   140 EXPORT_C CRC2Decryptor* CRC2Decryptor::NewL(const TDesC8& aKey, 
       
   141 	TInt aEffectiveKeyLenBits)
       
   142 	{
       
   143 	CRC2Decryptor* me = CRC2Decryptor::NewLC(aKey, aEffectiveKeyLenBits);
       
   144 	CleanupStack::Pop(me);
       
   145 	return (me);
       
   146 	}
       
   147 
       
   148 EXPORT_C CRC2Decryptor* CRC2Decryptor::NewLC(const TDesC8& aKey, 
       
   149 	TInt aEffectiveKeyLenBits)
       
   150 	{
       
   151 	CRC2Decryptor* me = new (ELeave) CRC2Decryptor;
       
   152 	CleanupStack::PushL(me);	//	Does not leave but function requires it be Push-ed
       
   153 	me->SetKey(aKey, aEffectiveKeyLenBits);
       
   154 	// weak enough if either aKey or aEffectiveKeyLenBits is weak
       
   155 	TInt minKeySize = Min(aEffectiveKeyLenBits, BytesToBits(aKey.Size()));
       
   156 	TCrypto::IsSymmetricWeakEnoughL(minKeySize);
       
   157 	return (me);
       
   158 	}
       
   159 
       
   160 #pragma warning (disable : 4244)	//	conversion from 'int' to 'unsigned short', possible loss of data
       
   161 void CRC2Decryptor::Transform(TDes8& aBlock)
       
   162 	{
       
   163 	assert(aBlock.Size() == KRC2BlockBytes);
       
   164 
       
   165 	TUint16 R0, R1, R2, R3;
       
   166 	GetBlockLittleEndian((TUint8*)&aBlock[0], R0, R1, R2, R3);
       
   167 
       
   168 	TInt i = 15;
       
   169 	for (; i >= 0; i--)
       
   170 		{
       
   171 		if (i == 4 || i == 10)
       
   172 			{
       
   173 			R3 -= iK[R2 & 63];
       
   174 			R2 -= iK[R1 & 63];
       
   175 			R1 -= iK[R0 & 63];
       
   176 			R0 -= iK[R3 & 63];
       
   177 			}
       
   178 
       
   179 		R3 = rotrFixed(R3, 5);
       
   180 		R3 -= (R0 & ~R2) + (R1 & R2) + iK[4*i+3];
       
   181 
       
   182 		R2 = rotrFixed(R2, 3);
       
   183 		R2 -= (R3 & ~R1) + (R0 & R1) + iK[4*i+2];
       
   184 
       
   185 		R1 = rotrFixed(R1, 2);
       
   186 		R1 -= (R2 & ~R0) + (R3 & R0) + iK[4*i+1];
       
   187 
       
   188 		R0 = rotrFixed(R0, 1);
       
   189 		R0 -= (R1 & ~R3) + (R2 & R3) + iK[4*i+0];
       
   190 		}
       
   191 
       
   192 	PutBlockLittleEndian((TUint8*)&aBlock[0], R0, R1, R2, R3);
       
   193 	}
       
   194 
       
   195 #pragma warning (default : 4244)	//	conversion from 'int' to 'unsigned short', possible loss of data
       
   196 
       
   197 CRC2Decryptor::CRC2Decryptor(void)
       
   198 	{
       
   199 	}