os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/rc2impl.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.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include "rc2impl.h"
sl@0
    20
#include "keys.h"
sl@0
    21
sl@0
    22
#include "rc2table.h"
sl@0
    23
#include "common/inlines.h"
sl@0
    24
#include "pluginconfig.h"
sl@0
    25
#include "symmetriccipherimpl.h"
sl@0
    26
#include <cryptostrength.h>
sl@0
    27
sl@0
    28
using namespace SoftwareCrypto;
sl@0
    29
sl@0
    30
CRc2Impl::CRc2Impl(
sl@0
    31
	TUid aCryptoMode,
sl@0
    32
	TUid aOperationMode,
sl@0
    33
	TUid aPadding) :
sl@0
    34
	CSymmetricBlockCipherImpl(KRc2BlockBytes, aCryptoMode, aOperationMode, aPadding)
sl@0
    35
	{
sl@0
    36
	}
sl@0
    37
sl@0
    38
CRc2Impl* CRc2Impl::NewL(
sl@0
    39
	const CKey& aKey,
sl@0
    40
	TUid aCryptoMode,
sl@0
    41
	TUid aOperationMode,
sl@0
    42
	TUid aPadding,
sl@0
    43
	TInt aEffectiveKeyLenBits)	
sl@0
    44
	{
sl@0
    45
	CRc2Impl* self = CRc2Impl::NewLC(aKey, aCryptoMode, aOperationMode, aPadding, aEffectiveKeyLenBits);
sl@0
    46
	CleanupStack::Pop(self);
sl@0
    47
	return self;
sl@0
    48
	}
sl@0
    49
	
sl@0
    50
CRc2Impl* CRc2Impl::NewLC(
sl@0
    51
	const CKey& aKey,
sl@0
    52
	TUid aCryptoMode,
sl@0
    53
	TUid aOperationMode,
sl@0
    54
	TUid aPadding,
sl@0
    55
	TInt aEffectiveKeyLenBits)
sl@0
    56
	{
sl@0
    57
	CRc2Impl* self = new(ELeave) CRc2Impl(aCryptoMode, aOperationMode, aPadding);
sl@0
    58
	CleanupStack::PushL(self);
sl@0
    59
	self->ConstructL(aKey, aEffectiveKeyLenBits);
sl@0
    60
	
sl@0
    61
	const TDesC8& keyContent = aKey.GetTDesC8L(KSymmetricKeyParameterUid);
sl@0
    62
	TCrypto::IsSymmetricWeakEnoughL(BytesToBits(keyContent.Size()) - keyContent.Size());
sl@0
    63
	return self;
sl@0
    64
	}
sl@0
    65
		
sl@0
    66
CRc2Impl::~CRc2Impl()
sl@0
    67
	{
sl@0
    68
	Mem::FillZ(&iK, sizeof(iK));
sl@0
    69
	}
sl@0
    70
	
sl@0
    71
void CRc2Impl::ConstructL(const CKey& aKey, TInt aEffectiveKeyLenBits)	
sl@0
    72
	{
sl@0
    73
	iEffectiveKeyLenBits = aEffectiveKeyLenBits;	
sl@0
    74
	CSymmetricBlockCipherImpl::ConstructL(aKey);
sl@0
    75
	SetKeySchedule();
sl@0
    76
	}
sl@0
    77
	
sl@0
    78
CExtendedCharacteristics* CRc2Impl::CreateExtendedCharacteristicsL()
sl@0
    79
	{
sl@0
    80
	// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
sl@0
    81
	// for exclusive use and are not CERTIFIED to be standards compliant.
sl@0
    82
	return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
sl@0
    83
	}
sl@0
    84
sl@0
    85
const CExtendedCharacteristics* CRc2Impl::GetExtendedCharacteristicsL()
sl@0
    86
	{
sl@0
    87
	return CRc2Impl::CreateExtendedCharacteristicsL();
sl@0
    88
	}
sl@0
    89
sl@0
    90
TUid CRc2Impl::ImplementationUid() const
sl@0
    91
	{
sl@0
    92
	return KCryptoPluginRc2Uid;
sl@0
    93
	}
sl@0
    94
sl@0
    95
TBool CRc2Impl::IsValidKeyLength(TInt aKeyBytes) const
sl@0
    96
	{
sl@0
    97
	return ((aKeyBytes > 0 && aKeyBytes <= KRc2MaxKeySizeBytes) ? ETrue : EFalse);
sl@0
    98
	}	
sl@0
    99
	
sl@0
   100
TInt CRc2Impl::GetKeyStrength() const
sl@0
   101
	{
sl@0
   102
	return Min(iEffectiveKeyLenBits, BytesToBits(iKeyBytes));
sl@0
   103
	}
sl@0
   104
sl@0
   105
void CRc2Impl::SetKeySchedule()
sl@0
   106
	{				
sl@0
   107
	TUint keyLen = iKey->Length();
sl@0
   108
	
sl@0
   109
	TUint8 L[KRc2MaxKeySizeBytes];	
sl@0
   110
	Mem::Copy((TUint8*)&L[0], (TUint8*)&(*iKey)[0], keyLen);
sl@0
   111
sl@0
   112
	TInt i = keyLen;
sl@0
   113
	for (; i < KRc2MaxKeySizeBytes; i++)
sl@0
   114
		{
sl@0
   115
		L[i] = RC2_TABLE::PITABLE[(L[i-1] + L[i-keyLen]) & 255];
sl@0
   116
		}
sl@0
   117
sl@0
   118
	TUint T8 = (iEffectiveKeyLenBits+7) / 8;
sl@0
   119
	TUint8 TM = (TUint8)(255 >> ((8-(iEffectiveKeyLenBits%8))%8));
sl@0
   120
	L[128-T8] = RC2_TABLE::PITABLE[L[128-T8] & TM];
sl@0
   121
sl@0
   122
	for (i=127-T8; i>=0; i--)
sl@0
   123
		L[i] = RC2_TABLE::PITABLE[L[i+1] ^ L[i+T8]];
sl@0
   124
sl@0
   125
	for (i=0; i < KRc2ExpandedKeyLen; i++)
sl@0
   126
		iK[i] = (TUint16)(L[2*i] + (L[2*i+1] << 8));
sl@0
   127
	}
sl@0
   128
sl@0
   129
#pragma warning (disable : 4244)	//	conversion from 'int' to 'unsigned short', possible loss of data
sl@0
   130
void CRc2Impl::TransformEncrypt(
sl@0
   131
	TUint8* aBuffer,
sl@0
   132
	TUint aNumBlocks)
sl@0
   133
	{
sl@0
   134
	for (TInt blockNum = 0; blockNum < aNumBlocks; ++blockNum)
sl@0
   135
		{		
sl@0
   136
		ModeEncryptStart(aBuffer);
sl@0
   137
		
sl@0
   138
		TUint16 R0, R1, R2, R3;
sl@0
   139
		GetBlockLittleEndian(aBuffer, R0, R1, R2, R3);
sl@0
   140
		
sl@0
   141
		TInt i = 0;
sl@0
   142
		for (; i < 16; i++)
sl@0
   143
			{
sl@0
   144
			R0 += (R1 & ~R3) + (R2 & R3) + iK[4*i+0];
sl@0
   145
			R0 = rotlFixed(R0, 1);
sl@0
   146
sl@0
   147
			R1 += (R2 & ~R0) + (R3 & R0) + iK[4*i+1];
sl@0
   148
			R1 = rotlFixed(R1, 2);
sl@0
   149
sl@0
   150
			R2 += (R3 & ~R1) + (R0 & R1) + iK[4*i+2];
sl@0
   151
			R2 = rotlFixed(R2, 3);
sl@0
   152
sl@0
   153
			R3 += (R0 & ~R2) + (R1 & R2) + iK[4*i+3];
sl@0
   154
			R3 = rotlFixed(R3, 5);
sl@0
   155
sl@0
   156
			if (i == 4 || i == 10)
sl@0
   157
				{
sl@0
   158
				R0 += iK[R3 & 63];
sl@0
   159
				R1 += iK[R0 & 63];
sl@0
   160
				R2 += iK[R1 & 63];
sl@0
   161
				R3 += iK[R2 & 63];
sl@0
   162
				}
sl@0
   163
			}
sl@0
   164
		PutBlockLittleEndian(aBuffer, R0, R1, R2, R3);	
sl@0
   165
		ModeEncryptEnd(aBuffer);
sl@0
   166
		aBuffer += KRc2BlockBytes;
sl@0
   167
		}
sl@0
   168
	}
sl@0
   169
#pragma warning (default : 4244)	//	conversion from 'int' to 'unsigned short', possible loss of data
sl@0
   170
sl@0
   171
sl@0
   172
#pragma warning (disable : 4244)	//	conversion from 'int' to 'unsigned short', possible loss of data
sl@0
   173
void CRc2Impl::TransformDecrypt(
sl@0
   174
	TUint8* aBuffer,
sl@0
   175
	TUint aNumBlocks)
sl@0
   176
	{
sl@0
   177
	for (TInt blockNum = 0; blockNum < aNumBlocks; ++blockNum)
sl@0
   178
		{		
sl@0
   179
		ModeDecryptStart(aBuffer);
sl@0
   180
sl@0
   181
		TUint16 R0, R1, R2, R3;
sl@0
   182
		GetBlockLittleEndian(aBuffer, R0, R1, R2, R3);
sl@0
   183
sl@0
   184
		TInt i = 15;
sl@0
   185
		for (; i >= 0; i--)
sl@0
   186
			{
sl@0
   187
			if (i == 4 || i == 10)
sl@0
   188
				{
sl@0
   189
				R3 -= iK[R2 & 63];
sl@0
   190
				R2 -= iK[R1 & 63];
sl@0
   191
				R1 -= iK[R0 & 63];
sl@0
   192
				R0 -= iK[R3 & 63];
sl@0
   193
				}
sl@0
   194
sl@0
   195
			R3 = rotrFixed(R3, 5);
sl@0
   196
			R3 -= (R0 & ~R2) + (R1 & R2) + iK[4*i+3];
sl@0
   197
sl@0
   198
			R2 = rotrFixed(R2, 3);
sl@0
   199
			R2 -= (R3 & ~R1) + (R0 & R1) + iK[4*i+2];
sl@0
   200
sl@0
   201
			R1 = rotrFixed(R1, 2);
sl@0
   202
			R1 -= (R2 & ~R0) + (R3 & R0) + iK[4*i+1];
sl@0
   203
sl@0
   204
			R0 = rotrFixed(R0, 1);
sl@0
   205
			R0 -= (R1 & ~R3) + (R2 & R3) + iK[4*i+0];
sl@0
   206
			}
sl@0
   207
sl@0
   208
		PutBlockLittleEndian(aBuffer, R0, R1, R2, R3);
sl@0
   209
		ModeDecryptEnd(aBuffer);
sl@0
   210
		aBuffer += KRc2BlockBytes;
sl@0
   211
		}
sl@0
   212
	}
sl@0
   213
#pragma warning (default : 4244)	//	conversion from 'int' to 'unsigned short', possible loss of data