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