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