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