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