os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/desimpl.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 "desimpl.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
sl@0
    29
//	bit 0 is left-most in byte
sl@0
    30
static const TInt bytebit[] = {0200,0100,040,020,010,04,02,01};
sl@0
    31
sl@0
    32
using namespace SoftwareCrypto;
sl@0
    33
sl@0
    34
/* CDesImpl */
sl@0
    35
CDesImpl::CDesImpl(
sl@0
    36
	TUint8 aBlockBytes,
sl@0
    37
	TUid aCryptoMode,
sl@0
    38
	TUid aOperationMode,
sl@0
    39
	TUid aPadding) :
sl@0
    40
	CSymmetricBlockCipherImpl(aBlockBytes, aCryptoMode, aOperationMode, aPadding)
sl@0
    41
	{
sl@0
    42
	}
sl@0
    43
sl@0
    44
CDesImpl* CDesImpl::NewL(const CKey& aKey, TUid aCryptoMode, TUid aOperationMode, TUid aPadding)
sl@0
    45
	{
sl@0
    46
	CDesImpl* self = CDesImpl::NewLC(aKey, aCryptoMode, aOperationMode, aPadding);
sl@0
    47
	CleanupStack::Pop(self);
sl@0
    48
	return self;
sl@0
    49
	}
sl@0
    50
	
sl@0
    51
CDesImpl* CDesImpl::NewLC(const CKey& aKey, TUid aCryptoMode, TUid aOperationMode, TUid aPadding)
sl@0
    52
	{
sl@0
    53
	CDesImpl* self = new(ELeave) CDesImpl(KDesBlockBytes, aCryptoMode, aOperationMode, aPadding);
sl@0
    54
	CleanupStack::PushL(self);
sl@0
    55
	self->ConstructL(aKey);
sl@0
    56
	
sl@0
    57
	const TDesC8& keyContent = aKey.GetTDesC8L(KSymmetricKeyParameterUid);
sl@0
    58
	TCrypto::IsSymmetricWeakEnoughL(BytesToBits(keyContent.Size()) - keyContent.Size());
sl@0
    59
	return self;
sl@0
    60
	}
sl@0
    61
		
sl@0
    62
CDesImpl::~CDesImpl()
sl@0
    63
	{
sl@0
    64
	// make sure key information isn't visible to other processes if the
sl@0
    65
	// page is reused.
sl@0
    66
	Mem::FillZ(&iK, sizeof(iK));
sl@0
    67
	}
sl@0
    68
	
sl@0
    69
void CDesImpl::ConstructL(const CKey& aKey)
sl@0
    70
	{
sl@0
    71
	CSymmetricBlockCipherImpl::ConstructL(aKey);
sl@0
    72
	SetKeySchedule();
sl@0
    73
	}		
sl@0
    74
	
sl@0
    75
CExtendedCharacteristics* CDesImpl::CreateExtendedCharacteristicsL()
sl@0
    76
	{
sl@0
    77
	// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
sl@0
    78
	// for exclusive use and are not CERTIFIED to be standards compliant.
sl@0
    79
	return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
sl@0
    80
	}
sl@0
    81
	
sl@0
    82
const CExtendedCharacteristics* CDesImpl::GetExtendedCharacteristicsL()
sl@0
    83
	{
sl@0
    84
	return CDesImpl::CreateExtendedCharacteristicsL();
sl@0
    85
	}
sl@0
    86
sl@0
    87
TUid CDesImpl::ImplementationUid() const
sl@0
    88
	{
sl@0
    89
	return KCryptoPluginDesUid;
sl@0
    90
	}
sl@0
    91
	
sl@0
    92
TBool CDesImpl::IsValidKeyLength(TInt aKeyBytes) const
sl@0
    93
	{
sl@0
    94
	return (aKeyBytes == KDesKeyBytes);
sl@0
    95
	}
sl@0
    96
	
sl@0
    97
TInt CDesImpl::GetKeyStrength() const
sl@0
    98
	{
sl@0
    99
	// parity bits are excluded
sl@0
   100
	return BytesToBits(KDesKeyBytes - 8);
sl@0
   101
	}	
sl@0
   102
	
sl@0
   103
void CDesImpl::TransformEncrypt(
sl@0
   104
	TUint8* aBuffer,
sl@0
   105
	TUint aNumBlocks)
sl@0
   106
	{
sl@0
   107
	for (TInt i = 0; i < aNumBlocks; ++i)
sl@0
   108
		{		
sl@0
   109
		ModeEncryptStart(aBuffer);
sl@0
   110
		TUint32 l, r;
sl@0
   111
		// Split the block into 2 word-sized big endian portions
sl@0
   112
		GetBlockBigEndian(aBuffer, l, r);
sl@0
   113
		IPerm(l,r);
sl@0
   114
		DoTransform(l, r, iK);		
sl@0
   115
		FPerm(l,r);
sl@0
   116
sl@0
   117
		// Put the portions back into the block as little endian
sl@0
   118
		PutBlockBigEndian(aBuffer, r, l);
sl@0
   119
sl@0
   120
		ModeEncryptEnd(aBuffer);
sl@0
   121
		aBuffer += KDesBlockBytes;
sl@0
   122
		}
sl@0
   123
	}	
sl@0
   124
	
sl@0
   125
void CDesImpl::TransformDecrypt(
sl@0
   126
	TUint8* aBuffer,
sl@0
   127
	TUint aNumBlocks)
sl@0
   128
	{
sl@0
   129
	for (TInt i = 0; i < aNumBlocks; ++i)
sl@0
   130
		{		
sl@0
   131
		ModeDecryptStart(aBuffer);
sl@0
   132
sl@0
   133
		TUint32 l, r;
sl@0
   134
		// Split the block into 2 word-sized big endian portions
sl@0
   135
		GetBlockBigEndian(aBuffer, l, r);
sl@0
   136
sl@0
   137
		IPerm(l,r);
sl@0
   138
		DoTransform(l, r, iK);		
sl@0
   139
		FPerm(l,r);
sl@0
   140
sl@0
   141
		// Put the portions back into the block as little endian
sl@0
   142
		PutBlockBigEndian(aBuffer, r, l);
sl@0
   143
sl@0
   144
		ModeDecryptEnd(aBuffer);
sl@0
   145
		aBuffer += KDesBlockBytes;
sl@0
   146
		}
sl@0
   147
	}
sl@0
   148
sl@0
   149
void CDesImpl::SetKeySchedule()
sl@0
   150
	{
sl@0
   151
	if (iCryptoMode.iUid == KCryptoModeEncrypt)
sl@0
   152
		{
sl@0
   153
		SetEncryptKeySchedule(*iKey, iK);
sl@0
   154
		}
sl@0
   155
	else 
sl@0
   156
		{
sl@0
   157
		ASSERT(iCryptoMode.iUid == KCryptoModeDecrypt);
sl@0
   158
		SetDecryptKeySchedule(*iKey, iK);
sl@0
   159
		}	
sl@0
   160
	}		
sl@0
   161
sl@0
   162
void CDesImpl::DoTransform(TUint32& l, TUint32& r, const TUint32* aKeySchedule)
sl@0
   163
	{
sl@0
   164
	TInt i = 0;
sl@0
   165
	for (; i<8; i++)
sl@0
   166
		{
sl@0
   167
		TUint32 work = rotrFixed(r, 4U) ^ aKeySchedule[4*i+0];
sl@0
   168
		l ^= DES_TABLE::sbox[6][(work) & 0x3f]
sl@0
   169
		  ^  DES_TABLE::sbox[4][(work >> 8) & 0x3f]
sl@0
   170
		  ^  DES_TABLE::sbox[2][(work >> 16) & 0x3f]
sl@0
   171
		  ^  DES_TABLE::sbox[0][(work >> 24) & 0x3f];
sl@0
   172
		work = r ^ aKeySchedule[4*i+1];
sl@0
   173
		l ^= DES_TABLE::sbox[7][(work) & 0x3f]
sl@0
   174
		  ^  DES_TABLE::sbox[5][(work >> 8) & 0x3f]
sl@0
   175
		  ^  DES_TABLE::sbox[3][(work >> 16) & 0x3f]
sl@0
   176
		  ^  DES_TABLE::sbox[1][(work >> 24) & 0x3f];
sl@0
   177
sl@0
   178
		work = rotrFixed(l, 4U) ^ aKeySchedule[4*i+2];
sl@0
   179
		r ^= DES_TABLE::sbox[6][(work) & 0x3f]
sl@0
   180
		  ^  DES_TABLE::sbox[4][(work >> 8) & 0x3f]
sl@0
   181
		  ^  DES_TABLE::sbox[2][(work >> 16) & 0x3f]
sl@0
   182
		  ^  DES_TABLE::sbox[0][(work >> 24) & 0x3f];
sl@0
   183
		work = l ^ aKeySchedule[4*i+3];
sl@0
   184
		r ^= DES_TABLE::sbox[7][(work) & 0x3f]
sl@0
   185
		  ^  DES_TABLE::sbox[5][(work >> 8) & 0x3f]
sl@0
   186
		  ^  DES_TABLE::sbox[3][(work >> 16) & 0x3f]
sl@0
   187
		  ^  DES_TABLE::sbox[1][(work >> 24) & 0x3f];
sl@0
   188
		}
sl@0
   189
	}	
sl@0
   190
sl@0
   191
void CDesImpl::SetEncryptKeySchedule(const TDesC8& aKey, TUint32* aKeySchedule)
sl@0
   192
	{
sl@0
   193
	TInt i=0, j=0, l=0, m=0;
sl@0
   194
sl@0
   195
//	Form a byte array from aKey, taking endianess into account (little->big)	
sl@0
   196
	TUint8 key[8];								//	For big endian byte array	
sl@0
   197
	Mem::Copy(&key, &aKey[0], 8);
sl@0
   198
sl@0
   199
	TUint8 buffer[56+56+8];
sl@0
   200
	TUint8* const pc1m = &buffer[0];			/* place to modify pc1 into */
sl@0
   201
	TUint8* const pcr = pc1m + 56;				/* place to rotate pc1 into */
sl@0
   202
	TUint8* const ks = pcr + 56;
sl@0
   203
sl@0
   204
	for (j=0; j<56; j++) 
sl@0
   205
		{/* convert pc1 to bits of key */
sl@0
   206
		l = DES_TABLE::pc1[j]-1;				/* integer bit location  */
sl@0
   207
		m = l & 07;								/* find bit              */
sl@0
   208
		pc1m[j]=(key[l>>3] &					/* find which key byte l is in */
sl@0
   209
			bytebit[m])							/* and which bit of that byte */
sl@0
   210
			? (TUint8)1 : (TUint8)0;			/* and store 1-bit result */
sl@0
   211
		}
sl@0
   212
sl@0
   213
	for (i=0; i<16; i++) 
sl@0
   214
		{/* key chunk for each iteration */
sl@0
   215
		Mem::FillZ(ks,8);							/* Clear key schedule */
sl@0
   216
		for (j=0; j<56; j++)
sl@0
   217
		/*	rotate pc1 the right amount */
sl@0
   218
			pcr[j] = pc1m[(l=j+DES_TABLE::totrot[i])<(j<28? 28 : 56) ? l: l-28];
sl@0
   219
		
sl@0
   220
		/* rotate left and right halves independently */
sl@0
   221
		
sl@0
   222
		for (j=0; j<48; j++)
sl@0
   223
			{/* select bits individually */
sl@0
   224
			/* check bit that goes to ks[j] */
sl@0
   225
			if (pcr[DES_TABLE::pc2[j]-1])
sl@0
   226
				{/* mask it in if it's there */
sl@0
   227
				l= j % 6;
sl@0
   228
				ks[j/6] |= bytebit[l] >> 2;
sl@0
   229
				}
sl@0
   230
			}
sl@0
   231
sl@0
   232
		/* Now convert to odd/even interleaved form for use in F */
sl@0
   233
		(*(aKeySchedule+(2*i))) = ((TUint32)ks[0] << 24)
sl@0
   234
			| ((TUint32)ks[2] << 16)
sl@0
   235
			| ((TUint32)ks[4] << 8)
sl@0
   236
			| ((TUint32)ks[6]);
sl@0
   237
		
sl@0
   238
		(*(aKeySchedule+(2*i+1))) = ((TUint32)ks[1] << 24)
sl@0
   239
			| ((TUint32)ks[3] << 16)
sl@0
   240
			| ((TUint32)ks[5] << 8)
sl@0
   241
			| ((TUint32)ks[7]);
sl@0
   242
		}		
sl@0
   243
	}
sl@0
   244
sl@0
   245
void CDesImpl::SetDecryptKeySchedule(const TDesC8& aKey, TUint32* aKeySchedule)
sl@0
   246
	{
sl@0
   247
	SetEncryptKeySchedule(aKey, aKeySchedule);
sl@0
   248
	ReverseKeySchedule(aKeySchedule);
sl@0
   249
	}