os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/3desimpl.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 "3desimpl.h"
sl@0
    20
sl@0
    21
#include "destables.h"
sl@0
    22
#include "common/inlines.h"
sl@0
    23
#include "des.inl"
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
/* C3DesImpl */
sl@0
    31
C3DesImpl::C3DesImpl(
sl@0
    32
	TUid aCryptoMode,
sl@0
    33
	TUid aOperationMode,
sl@0
    34
	TUid aPadding) :
sl@0
    35
	CDesImpl(KDesBlockBytes, aCryptoMode, aOperationMode, aPadding)
sl@0
    36
	{
sl@0
    37
	}
sl@0
    38
sl@0
    39
C3DesImpl* C3DesImpl::NewL(const CKey& aKey, TUid aCryptoMode, TUid aOperationMode, TUid aPadding)
sl@0
    40
	{
sl@0
    41
	C3DesImpl* self = C3DesImpl::NewLC(aKey, aCryptoMode, aOperationMode, aPadding);
sl@0
    42
	CleanupStack::Pop(self);
sl@0
    43
	return self;
sl@0
    44
	}
sl@0
    45
	
sl@0
    46
C3DesImpl* C3DesImpl::NewLC(const CKey& aKey, TUid aCryptoMode, TUid aOperationMode, TUid aPadding)
sl@0
    47
	{
sl@0
    48
	C3DesImpl* self = new(ELeave) C3DesImpl(aCryptoMode, aOperationMode, aPadding);
sl@0
    49
	CleanupStack::PushL(self);
sl@0
    50
	self->ConstructL(aKey);
sl@0
    51
sl@0
    52
	const TDesC8& keyContent = aKey.GetTDesC8L(KSymmetricKeyParameterUid);
sl@0
    53
	TCrypto::IsSymmetricWeakEnoughL(BytesToBits(keyContent.Size()) - keyContent.Size());
sl@0
    54
	return self;
sl@0
    55
	}
sl@0
    56
		
sl@0
    57
C3DesImpl::~C3DesImpl()
sl@0
    58
	{
sl@0
    59
	// make sure key information isn't visible to other processes if the
sl@0
    60
	// page is reused.
sl@0
    61
	Mem::FillZ(&iK1, sizeof(iK1));
sl@0
    62
	Mem::FillZ(&iK2, sizeof(iK2));
sl@0
    63
	Mem::FillZ(&iK3, sizeof(iK3));
sl@0
    64
	}
sl@0
    65
	
sl@0
    66
void C3DesImpl::ConstructL(const CKey& aKey)
sl@0
    67
	{
sl@0
    68
	CDesImpl::ConstructL(aKey);
sl@0
    69
	SetKeySchedule();
sl@0
    70
	}	
sl@0
    71
	
sl@0
    72
CExtendedCharacteristics* C3DesImpl::CreateExtendedCharacteristicsL()
sl@0
    73
	{
sl@0
    74
	// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
sl@0
    75
	// for exclusive use and are not CERTIFIED to be standards compliant.
sl@0
    76
	return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
sl@0
    77
	}
sl@0
    78
	
sl@0
    79
const CExtendedCharacteristics* C3DesImpl::GetExtendedCharacteristicsL()
sl@0
    80
	{
sl@0
    81
	return C3DesImpl::CreateExtendedCharacteristicsL();
sl@0
    82
	}
sl@0
    83
sl@0
    84
TUid C3DesImpl::ImplementationUid() const
sl@0
    85
	{
sl@0
    86
	return KCryptoPlugin3DesUid;
sl@0
    87
	}
sl@0
    88
	
sl@0
    89
TBool C3DesImpl::IsValidKeyLength(TInt aKeyBytes) const
sl@0
    90
	{
sl@0
    91
	return (aKeyBytes == K3DesKeyBytes);
sl@0
    92
	}
sl@0
    93
	
sl@0
    94
TInt C3DesImpl::GetKeyStrength() const
sl@0
    95
	{
sl@0
    96
	// Exclude parity bits from each subkey
sl@0
    97
	return BytesToBits(K3DesKeyBytes - (3 * 8));
sl@0
    98
	}	
sl@0
    99
	
sl@0
   100
void C3DesImpl::TransformEncrypt(
sl@0
   101
	TUint8* aBuffer,
sl@0
   102
	TUint aNumBlocks)
sl@0
   103
	{			
sl@0
   104
	for (TInt i = 0; i < aNumBlocks; ++i)
sl@0
   105
		{		
sl@0
   106
		ModeEncryptStart(aBuffer);
sl@0
   107
sl@0
   108
		TUint32 l, r;
sl@0
   109
		// Split the block into 2 word-sized big endian portions
sl@0
   110
		GetBlockBigEndian(aBuffer, l, r);
sl@0
   111
sl@0
   112
		IPerm(l,r);
sl@0
   113
		// The mode is applied to the entire operation and NOT 
sl@0
   114
		// for each DES transform
sl@0
   115
		TUid opMode = iOperationMode;
sl@0
   116
		iOperationMode = KOperationModeECBUid;
sl@0
   117
		DoTransform(l, r, iK1);
sl@0
   118
		DoTransform(r, l, iK2);
sl@0
   119
		DoTransform(l, r, iK3);
sl@0
   120
		iOperationMode = opMode;
sl@0
   121
		FPerm(l,r);
sl@0
   122
sl@0
   123
		// Put the portions back into the block as little endian
sl@0
   124
		PutBlockBigEndian(aBuffer, r, l);
sl@0
   125
		ModeEncryptEnd(aBuffer);
sl@0
   126
		aBuffer += KDesBlockBytes;
sl@0
   127
		}
sl@0
   128
	}
sl@0
   129
	
sl@0
   130
void C3DesImpl::TransformDecrypt(
sl@0
   131
	TUint8* aBuffer,
sl@0
   132
	const TUint aNumBlocks)
sl@0
   133
	{	
sl@0
   134
	for (TInt i = 0; i < aNumBlocks; ++i)
sl@0
   135
		{		
sl@0
   136
		ModeDecryptStart(aBuffer);
sl@0
   137
			
sl@0
   138
		TUint32 l, r;
sl@0
   139
		// Split the block into 2 word-sized big endian portions
sl@0
   140
		GetBlockBigEndian(aBuffer, l, r);
sl@0
   141
sl@0
   142
		IPerm(l,r);
sl@0
   143
		
sl@0
   144
		// The mode is applied to the entire operation and NOT 
sl@0
   145
		// for each DES transform
sl@0
   146
		TUid opMode = iOperationMode;
sl@0
   147
		iOperationMode = KOperationModeECBUid;
sl@0
   148
		DoTransform(l, r, iK1);
sl@0
   149
		DoTransform(r, l, iK2);
sl@0
   150
		DoTransform(l, r, iK3);
sl@0
   151
		iOperationMode = opMode;
sl@0
   152
		FPerm(l,r);
sl@0
   153
sl@0
   154
		// Put the portions back into the block as little endian
sl@0
   155
		PutBlockBigEndian(aBuffer, r, l);	
sl@0
   156
		ModeDecryptEnd(aBuffer);
sl@0
   157
		aBuffer += K3DesBlockBytes;
sl@0
   158
		}
sl@0
   159
	}	
sl@0
   160
sl@0
   161
void C3DesImpl::SetKeySchedule()
sl@0
   162
	{
sl@0
   163
	if (iCryptoMode.iUid == KCryptoModeEncrypt)
sl@0
   164
		{
sl@0
   165
		// Encrypt -> Decrypt -> Encrypt
sl@0
   166
		// Encryptor key	
sl@0
   167
		SetEncryptKeySchedule(iKey->Mid(0, KDesKeyBytes), iK1);
sl@0
   168
		
sl@0
   169
		// Decryptor key
sl@0
   170
		SetDecryptKeySchedule(iKey->Mid(KDesKeyBytes, 2 * KDesKeyBytes), iK2);
sl@0
   171
		
sl@0
   172
		// Encryptor key
sl@0
   173
		SetEncryptKeySchedule(iKey->Mid(2 * KDesKeyBytes), iK3);
sl@0
   174
		}
sl@0
   175
	else 
sl@0
   176
		{
sl@0
   177
		// Decrypt -> Encrypt -> Decrypt
sl@0
   178
		// Key order is reversed !
sl@0
   179
		
sl@0
   180
		ASSERT(iCryptoMode.iUid == KCryptoModeDecrypt);
sl@0
   181
		// Decryptor key	
sl@0
   182
		SetDecryptKeySchedule(iKey->Mid(0, KDesKeyBytes), iK3);
sl@0
   183
		
sl@0
   184
		// Encryptor key
sl@0
   185
		SetEncryptKeySchedule(iKey->Mid(KDesKeyBytes, 2 * KDesKeyBytes), iK2);
sl@0
   186
		
sl@0
   187
		// Decryptor key
sl@0
   188
		SetDecryptKeySchedule(iKey->Mid(2 * KDesKeyBytes), iK1);
sl@0
   189
		}	
sl@0
   190
	}