os/security/crypto/weakcryptospi/source/symmetric/rijndael.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcryptospi/source/symmetric/rijndael.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,225 @@
     1.4 +/*
     1.5 +* Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include "rijndael.h"
    1.23 +#include "rijndaelshim.h"
    1.24 +#include "rijndaeltables.h"
    1.25 +#include "../common/inlines.h"
    1.26 +#include <cryptostrength.h>
    1.27 +
    1.28 +const TUint KAESKeyBytes128 = 16;
    1.29 +const TUint KAESKeyBytes192 = 24;
    1.30 +const TUint KAESKeyBytes256 = 32;
    1.31 +const TUint KAESBlockBytes = 16;
    1.32 +
    1.33 +/* CRijndael */
    1.34 +EXPORT_C CRijndael::CRijndael(void)
    1.35 +	{
    1.36 +	}
    1.37 +
    1.38 +void CRijndael::Reset()
    1.39 +	{
    1.40 +	// CRijndael is externally derivable. Don't delete this code
    1.41 +	SetKey(*iKey);
    1.42 +	}
    1.43 +
    1.44 +TInt CRijndael::KeySize() const
    1.45 +	{
    1.46 +	// CRijndael is externally derivable. Don't delete this code
    1.47 +	return (4*(iRounds+1));
    1.48 +	}
    1.49 +
    1.50 +EXPORT_C CRijndael::~CRijndael()
    1.51 +	{
    1.52 +	// CRijndael is externally derivable. Don't delete this code
    1.53 +	delete iKey;
    1.54 +	}
    1.55 +
    1.56 +void CRijndael::ConstructL(const TDesC8& aKey)
    1.57 +	{
    1.58 +	// CRijndael is externally derivable. Don't delete this code
    1.59 +	TUint keySize = aKey.Size();
    1.60 +	assert((keySize==KAESKeyBytes128)||(keySize==KAESKeyBytes192)||(keySize==KAESKeyBytes256));
    1.61 +	iKey = aKey.AllocL();
    1.62 +	iRounds = keySize/4 + 6;
    1.63 +	SetKey(aKey);
    1.64 +	}
    1.65 +
    1.66 +void CRijndael::SetKey(const TDesC8& aKey)
    1.67 +	{
    1.68 +	// CRijndael is externally derivable. Don't delete this code
    1.69 +	TUint keySize = aKey.Size();
    1.70 +	TUint32 temp; 
    1.71 +	TUint32* rk = &iK[0];
    1.72 +
    1.73 +	TUint i = 0;
    1.74 +
    1.75 +	GetUserKeyBigEndian(rk, keySize/4, &aKey[0], keySize);
    1.76 +
    1.77 +	switch(keySize)
    1.78 +		{
    1.79 +		case (KAESKeyBytes128):
    1.80 +			{
    1.81 +			FOREVER
    1.82 +				{
    1.83 +				temp  = rk[3];
    1.84 +				rk[4] = rk[0] ^
    1.85 +					(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 2)] & 0xff000000) ^
    1.86 +					(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 1)] & 0x00ff0000) ^
    1.87 +					(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 0)] & 0x0000ff00) ^
    1.88 +					(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 3)] & 0x000000ff) ^
    1.89 +					RIJNDAEL_TABLE::rcon[i];
    1.90 +				rk[5] = rk[1] ^ rk[4];
    1.91 +				rk[6] = rk[2] ^ rk[5];
    1.92 +				rk[7] = rk[3] ^ rk[6];
    1.93 +				if (++i == 10)
    1.94 +					break;
    1.95 +				rk += 4;
    1.96 +				}
    1.97 +			}
    1.98 +		break;
    1.99 +
   1.100 +		case (KAESKeyBytes192):
   1.101 +			{
   1.102 +			FOREVER
   1.103 +				{
   1.104 +				temp = rk[ 5];
   1.105 +				rk[ 6] = rk[ 0] ^
   1.106 +					(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 2)] & 0xff000000) ^
   1.107 +					(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 1)] & 0x00ff0000) ^
   1.108 +					(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 0)] & 0x0000ff00) ^
   1.109 +					(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 3)] & 0x000000ff) ^
   1.110 +					RIJNDAEL_TABLE::rcon[i];
   1.111 +				rk[ 7] = rk[ 1] ^ rk[ 6];
   1.112 +				rk[ 8] = rk[ 2] ^ rk[ 7];
   1.113 +				rk[ 9] = rk[ 3] ^ rk[ 8];
   1.114 +				if (++i == 8)
   1.115 +					break;
   1.116 +				rk[10] = rk[ 4] ^ rk[ 9];
   1.117 +				rk[11] = rk[ 5] ^ rk[10];
   1.118 +				rk += 6;
   1.119 +				}
   1.120 +			}
   1.121 +		break;
   1.122 +
   1.123 +		case (KAESKeyBytes256):
   1.124 +			{
   1.125 +			FOREVER
   1.126 +				{
   1.127 +        		temp = rk[ 7];
   1.128 +        		rk[ 8] = rk[ 0] ^
   1.129 +        			(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 2)] & 0xff000000) ^
   1.130 +        			(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 1)] & 0x00ff0000) ^
   1.131 +        			(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 0)] & 0x0000ff00) ^
   1.132 +        			(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 3)] & 0x000000ff) ^
   1.133 +        			RIJNDAEL_TABLE::rcon[i];
   1.134 +        		rk[ 9] = rk[ 1] ^ rk[ 8];
   1.135 +        		rk[10] = rk[ 2] ^ rk[ 9];
   1.136 +        		rk[11] = rk[ 3] ^ rk[10];
   1.137 +				if (++i == 7)
   1.138 +					break;
   1.139 +        		temp = rk[11];
   1.140 +        		rk[12] = rk[ 4] ^
   1.141 +        			(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 3)] & 0xff000000) ^
   1.142 +        			(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 2)] & 0x00ff0000) ^
   1.143 +        			(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 1)] & 0x0000ff00) ^
   1.144 +        			(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 0)] & 0x000000ff);
   1.145 +        		rk[13] = rk[ 5] ^ rk[12];
   1.146 +        		rk[14] = rk[ 6] ^ rk[13];
   1.147 +        		rk[15] = rk[ 7] ^ rk[14];
   1.148 +
   1.149 +				rk += 8;
   1.150 +				}
   1.151 +			}
   1.152 +		break;
   1.153 +
   1.154 +		default:
   1.155 +			assert(0);	//	Shouldn't get here, keeps compiler happy
   1.156 +		}
   1.157 +	}
   1.158 +
   1.159 +
   1.160 +/* CAESEncryptor */
   1.161 +EXPORT_C CAESEncryptor* CAESEncryptor::NewL(const TDesC8& aKey)
   1.162 +	{
   1.163 +	return CAESEncryptorShim::NewL(aKey);
   1.164 +	}
   1.165 +
   1.166 +EXPORT_C CAESEncryptor* CAESEncryptor::NewLC(const TDesC8& aKey)
   1.167 +	{
   1.168 +	return CAESEncryptorShim::NewLC(aKey);
   1.169 +	}
   1.170 +
   1.171 +CAESEncryptor::CAESEncryptor()
   1.172 +	{
   1.173 +	}
   1.174 +
   1.175 +/* CAESDecryptor */
   1.176 +EXPORT_C CAESDecryptor* CAESDecryptor::NewL(const TDesC8& aKey)
   1.177 +	{
   1.178 +	return CAESDecryptorShim::NewL(aKey);
   1.179 +	}
   1.180 +
   1.181 +EXPORT_C CAESDecryptor* CAESDecryptor::NewLC(const TDesC8& aKey)
   1.182 +	{
   1.183 +	return CAESDecryptorShim::NewLC(aKey);
   1.184 +	}
   1.185 +
   1.186 +CAESDecryptor::CAESDecryptor()
   1.187 +	{	
   1.188 +	}
   1.189 +
   1.190 +// All these methods have been replaced by the shim
   1.191 +#ifdef _BullseyeCoverage
   1.192 +#pragma suppress_warnings on
   1.193 +#pragma BullseyeCoverage off
   1.194 +#pragma suppress_warnings off
   1.195 +#endif
   1.196 +
   1.197 +TInt CAESDecryptor::BlockSize() const
   1.198 +	{
   1.199 +	// Method replaced by shim
   1.200 +	ASSERT(EFalse);
   1.201 +	return 0;
   1.202 +	}
   1.203 +
   1.204 +void CAESDecryptor::Transform(TDes8& /*aBlock*/)
   1.205 +	{
   1.206 +	// Method replaced by shim
   1.207 +	ASSERT(EFalse);	
   1.208 +	}
   1.209 +
   1.210 +void CAESDecryptor::SetKey(const TDesC8& /*aKey*/)
   1.211 +	{
   1.212 +	// Method replaced by shim
   1.213 +	ASSERT(EFalse);
   1.214 +	}
   1.215 +
   1.216 +TInt CAESEncryptor::BlockSize() const
   1.217 +	{
   1.218 +	// Method replaced by shim
   1.219 +	ASSERT(EFalse);
   1.220 +	return KAESBlockBytes;
   1.221 +	}
   1.222 +
   1.223 +void CAESEncryptor::Transform(TDes8& /*aBlock*/)
   1.224 +	{
   1.225 +	// Method replaced by shim
   1.226 +	ASSERT(EFalse);		
   1.227 +	}
   1.228 +