os/security/crypto/weakcryptospi/source/symmetric/rijndael.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2002-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 "rijndael.h"
    20 #include "rijndaelshim.h"
    21 #include "rijndaeltables.h"
    22 #include "../common/inlines.h"
    23 #include <cryptostrength.h>
    24 
    25 const TUint KAESKeyBytes128 = 16;
    26 const TUint KAESKeyBytes192 = 24;
    27 const TUint KAESKeyBytes256 = 32;
    28 const TUint KAESBlockBytes = 16;
    29 
    30 /* CRijndael */
    31 EXPORT_C CRijndael::CRijndael(void)
    32 	{
    33 	}
    34 
    35 void CRijndael::Reset()
    36 	{
    37 	// CRijndael is externally derivable. Don't delete this code
    38 	SetKey(*iKey);
    39 	}
    40 
    41 TInt CRijndael::KeySize() const
    42 	{
    43 	// CRijndael is externally derivable. Don't delete this code
    44 	return (4*(iRounds+1));
    45 	}
    46 
    47 EXPORT_C CRijndael::~CRijndael()
    48 	{
    49 	// CRijndael is externally derivable. Don't delete this code
    50 	delete iKey;
    51 	}
    52 
    53 void CRijndael::ConstructL(const TDesC8& aKey)
    54 	{
    55 	// CRijndael is externally derivable. Don't delete this code
    56 	TUint keySize = aKey.Size();
    57 	assert((keySize==KAESKeyBytes128)||(keySize==KAESKeyBytes192)||(keySize==KAESKeyBytes256));
    58 	iKey = aKey.AllocL();
    59 	iRounds = keySize/4 + 6;
    60 	SetKey(aKey);
    61 	}
    62 
    63 void CRijndael::SetKey(const TDesC8& aKey)
    64 	{
    65 	// CRijndael is externally derivable. Don't delete this code
    66 	TUint keySize = aKey.Size();
    67 	TUint32 temp; 
    68 	TUint32* rk = &iK[0];
    69 
    70 	TUint i = 0;
    71 
    72 	GetUserKeyBigEndian(rk, keySize/4, &aKey[0], keySize);
    73 
    74 	switch(keySize)
    75 		{
    76 		case (KAESKeyBytes128):
    77 			{
    78 			FOREVER
    79 				{
    80 				temp  = rk[3];
    81 				rk[4] = rk[0] ^
    82 					(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 2)] & 0xff000000) ^
    83 					(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 1)] & 0x00ff0000) ^
    84 					(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 0)] & 0x0000ff00) ^
    85 					(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 3)] & 0x000000ff) ^
    86 					RIJNDAEL_TABLE::rcon[i];
    87 				rk[5] = rk[1] ^ rk[4];
    88 				rk[6] = rk[2] ^ rk[5];
    89 				rk[7] = rk[3] ^ rk[6];
    90 				if (++i == 10)
    91 					break;
    92 				rk += 4;
    93 				}
    94 			}
    95 		break;
    96 
    97 		case (KAESKeyBytes192):
    98 			{
    99 			FOREVER
   100 				{
   101 				temp = rk[ 5];
   102 				rk[ 6] = rk[ 0] ^
   103 					(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 2)] & 0xff000000) ^
   104 					(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 1)] & 0x00ff0000) ^
   105 					(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 0)] & 0x0000ff00) ^
   106 					(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 3)] & 0x000000ff) ^
   107 					RIJNDAEL_TABLE::rcon[i];
   108 				rk[ 7] = rk[ 1] ^ rk[ 6];
   109 				rk[ 8] = rk[ 2] ^ rk[ 7];
   110 				rk[ 9] = rk[ 3] ^ rk[ 8];
   111 				if (++i == 8)
   112 					break;
   113 				rk[10] = rk[ 4] ^ rk[ 9];
   114 				rk[11] = rk[ 5] ^ rk[10];
   115 				rk += 6;
   116 				}
   117 			}
   118 		break;
   119 
   120 		case (KAESKeyBytes256):
   121 			{
   122 			FOREVER
   123 				{
   124         		temp = rk[ 7];
   125         		rk[ 8] = rk[ 0] ^
   126         			(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 2)] & 0xff000000) ^
   127         			(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 1)] & 0x00ff0000) ^
   128         			(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 0)] & 0x0000ff00) ^
   129         			(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 3)] & 0x000000ff) ^
   130         			RIJNDAEL_TABLE::rcon[i];
   131         		rk[ 9] = rk[ 1] ^ rk[ 8];
   132         		rk[10] = rk[ 2] ^ rk[ 9];
   133         		rk[11] = rk[ 3] ^ rk[10];
   134 				if (++i == 7)
   135 					break;
   136         		temp = rk[11];
   137         		rk[12] = rk[ 4] ^
   138         			(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 3)] & 0xff000000) ^
   139         			(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 2)] & 0x00ff0000) ^
   140         			(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 1)] & 0x0000ff00) ^
   141         			(RIJNDAEL_TABLE::Te4[GETBYTE(temp, 0)] & 0x000000ff);
   142         		rk[13] = rk[ 5] ^ rk[12];
   143         		rk[14] = rk[ 6] ^ rk[13];
   144         		rk[15] = rk[ 7] ^ rk[14];
   145 
   146 				rk += 8;
   147 				}
   148 			}
   149 		break;
   150 
   151 		default:
   152 			assert(0);	//	Shouldn't get here, keeps compiler happy
   153 		}
   154 	}
   155 
   156 
   157 /* CAESEncryptor */
   158 EXPORT_C CAESEncryptor* CAESEncryptor::NewL(const TDesC8& aKey)
   159 	{
   160 	return CAESEncryptorShim::NewL(aKey);
   161 	}
   162 
   163 EXPORT_C CAESEncryptor* CAESEncryptor::NewLC(const TDesC8& aKey)
   164 	{
   165 	return CAESEncryptorShim::NewLC(aKey);
   166 	}
   167 
   168 CAESEncryptor::CAESEncryptor()
   169 	{
   170 	}
   171 
   172 /* CAESDecryptor */
   173 EXPORT_C CAESDecryptor* CAESDecryptor::NewL(const TDesC8& aKey)
   174 	{
   175 	return CAESDecryptorShim::NewL(aKey);
   176 	}
   177 
   178 EXPORT_C CAESDecryptor* CAESDecryptor::NewLC(const TDesC8& aKey)
   179 	{
   180 	return CAESDecryptorShim::NewLC(aKey);
   181 	}
   182 
   183 CAESDecryptor::CAESDecryptor()
   184 	{	
   185 	}
   186 
   187 // All these methods have been replaced by the shim
   188 #ifdef _BullseyeCoverage
   189 #pragma suppress_warnings on
   190 #pragma BullseyeCoverage off
   191 #pragma suppress_warnings off
   192 #endif
   193 
   194 TInt CAESDecryptor::BlockSize() const
   195 	{
   196 	// Method replaced by shim
   197 	ASSERT(EFalse);
   198 	return 0;
   199 	}
   200 
   201 void CAESDecryptor::Transform(TDes8& /*aBlock*/)
   202 	{
   203 	// Method replaced by shim
   204 	ASSERT(EFalse);	
   205 	}
   206 
   207 void CAESDecryptor::SetKey(const TDesC8& /*aKey*/)
   208 	{
   209 	// Method replaced by shim
   210 	ASSERT(EFalse);
   211 	}
   212 
   213 TInt CAESEncryptor::BlockSize() const
   214 	{
   215 	// Method replaced by shim
   216 	ASSERT(EFalse);
   217 	return KAESBlockBytes;
   218 	}
   219 
   220 void CAESEncryptor::Transform(TDes8& /*aBlock*/)
   221 	{
   222 	// Method replaced by shim
   223 	ASSERT(EFalse);		
   224 	}
   225