sl@0: /* sl@0: * Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "rijndael.h" sl@0: #include "rijndaelshim.h" sl@0: #include "rijndaeltables.h" sl@0: #include "../common/inlines.h" sl@0: #include sl@0: sl@0: const TUint KAESKeyBytes128 = 16; sl@0: const TUint KAESKeyBytes192 = 24; sl@0: const TUint KAESKeyBytes256 = 32; sl@0: const TUint KAESBlockBytes = 16; sl@0: sl@0: /* CRijndael */ sl@0: EXPORT_C CRijndael::CRijndael(void) sl@0: { sl@0: } sl@0: sl@0: void CRijndael::Reset() sl@0: { sl@0: // CRijndael is externally derivable. Don't delete this code sl@0: SetKey(*iKey); sl@0: } sl@0: sl@0: TInt CRijndael::KeySize() const sl@0: { sl@0: // CRijndael is externally derivable. Don't delete this code sl@0: return (4*(iRounds+1)); sl@0: } sl@0: sl@0: EXPORT_C CRijndael::~CRijndael() sl@0: { sl@0: // CRijndael is externally derivable. Don't delete this code sl@0: delete iKey; sl@0: } sl@0: sl@0: void CRijndael::ConstructL(const TDesC8& aKey) sl@0: { sl@0: // CRijndael is externally derivable. Don't delete this code sl@0: TUint keySize = aKey.Size(); sl@0: assert((keySize==KAESKeyBytes128)||(keySize==KAESKeyBytes192)||(keySize==KAESKeyBytes256)); sl@0: iKey = aKey.AllocL(); sl@0: iRounds = keySize/4 + 6; sl@0: SetKey(aKey); sl@0: } sl@0: sl@0: void CRijndael::SetKey(const TDesC8& aKey) sl@0: { sl@0: // CRijndael is externally derivable. Don't delete this code sl@0: TUint keySize = aKey.Size(); sl@0: TUint32 temp; sl@0: TUint32* rk = &iK[0]; sl@0: sl@0: TUint i = 0; sl@0: sl@0: GetUserKeyBigEndian(rk, keySize/4, &aKey[0], keySize); sl@0: sl@0: switch(keySize) sl@0: { sl@0: case (KAESKeyBytes128): sl@0: { sl@0: FOREVER sl@0: { sl@0: temp = rk[3]; sl@0: rk[4] = rk[0] ^ sl@0: (RIJNDAEL_TABLE::Te4[GETBYTE(temp, 2)] & 0xff000000) ^ sl@0: (RIJNDAEL_TABLE::Te4[GETBYTE(temp, 1)] & 0x00ff0000) ^ sl@0: (RIJNDAEL_TABLE::Te4[GETBYTE(temp, 0)] & 0x0000ff00) ^ sl@0: (RIJNDAEL_TABLE::Te4[GETBYTE(temp, 3)] & 0x000000ff) ^ sl@0: RIJNDAEL_TABLE::rcon[i]; sl@0: rk[5] = rk[1] ^ rk[4]; sl@0: rk[6] = rk[2] ^ rk[5]; sl@0: rk[7] = rk[3] ^ rk[6]; sl@0: if (++i == 10) sl@0: break; sl@0: rk += 4; sl@0: } sl@0: } sl@0: break; sl@0: sl@0: case (KAESKeyBytes192): sl@0: { sl@0: FOREVER sl@0: { sl@0: temp = rk[ 5]; sl@0: rk[ 6] = rk[ 0] ^ sl@0: (RIJNDAEL_TABLE::Te4[GETBYTE(temp, 2)] & 0xff000000) ^ sl@0: (RIJNDAEL_TABLE::Te4[GETBYTE(temp, 1)] & 0x00ff0000) ^ sl@0: (RIJNDAEL_TABLE::Te4[GETBYTE(temp, 0)] & 0x0000ff00) ^ sl@0: (RIJNDAEL_TABLE::Te4[GETBYTE(temp, 3)] & 0x000000ff) ^ sl@0: RIJNDAEL_TABLE::rcon[i]; sl@0: rk[ 7] = rk[ 1] ^ rk[ 6]; sl@0: rk[ 8] = rk[ 2] ^ rk[ 7]; sl@0: rk[ 9] = rk[ 3] ^ rk[ 8]; sl@0: if (++i == 8) sl@0: break; sl@0: rk[10] = rk[ 4] ^ rk[ 9]; sl@0: rk[11] = rk[ 5] ^ rk[10]; sl@0: rk += 6; sl@0: } sl@0: } sl@0: break; sl@0: sl@0: case (KAESKeyBytes256): sl@0: { sl@0: FOREVER sl@0: { sl@0: temp = rk[ 7]; sl@0: rk[ 8] = rk[ 0] ^ sl@0: (RIJNDAEL_TABLE::Te4[GETBYTE(temp, 2)] & 0xff000000) ^ sl@0: (RIJNDAEL_TABLE::Te4[GETBYTE(temp, 1)] & 0x00ff0000) ^ sl@0: (RIJNDAEL_TABLE::Te4[GETBYTE(temp, 0)] & 0x0000ff00) ^ sl@0: (RIJNDAEL_TABLE::Te4[GETBYTE(temp, 3)] & 0x000000ff) ^ sl@0: RIJNDAEL_TABLE::rcon[i]; sl@0: rk[ 9] = rk[ 1] ^ rk[ 8]; sl@0: rk[10] = rk[ 2] ^ rk[ 9]; sl@0: rk[11] = rk[ 3] ^ rk[10]; sl@0: if (++i == 7) sl@0: break; sl@0: temp = rk[11]; sl@0: rk[12] = rk[ 4] ^ sl@0: (RIJNDAEL_TABLE::Te4[GETBYTE(temp, 3)] & 0xff000000) ^ sl@0: (RIJNDAEL_TABLE::Te4[GETBYTE(temp, 2)] & 0x00ff0000) ^ sl@0: (RIJNDAEL_TABLE::Te4[GETBYTE(temp, 1)] & 0x0000ff00) ^ sl@0: (RIJNDAEL_TABLE::Te4[GETBYTE(temp, 0)] & 0x000000ff); sl@0: rk[13] = rk[ 5] ^ rk[12]; sl@0: rk[14] = rk[ 6] ^ rk[13]; sl@0: rk[15] = rk[ 7] ^ rk[14]; sl@0: sl@0: rk += 8; sl@0: } sl@0: } sl@0: break; sl@0: sl@0: default: sl@0: assert(0); // Shouldn't get here, keeps compiler happy sl@0: } sl@0: } sl@0: sl@0: sl@0: /* CAESEncryptor */ sl@0: EXPORT_C CAESEncryptor* CAESEncryptor::NewL(const TDesC8& aKey) sl@0: { sl@0: return CAESEncryptorShim::NewL(aKey); sl@0: } sl@0: sl@0: EXPORT_C CAESEncryptor* CAESEncryptor::NewLC(const TDesC8& aKey) sl@0: { sl@0: return CAESEncryptorShim::NewLC(aKey); sl@0: } sl@0: sl@0: CAESEncryptor::CAESEncryptor() sl@0: { sl@0: } sl@0: sl@0: /* CAESDecryptor */ sl@0: EXPORT_C CAESDecryptor* CAESDecryptor::NewL(const TDesC8& aKey) sl@0: { sl@0: return CAESDecryptorShim::NewL(aKey); sl@0: } sl@0: sl@0: EXPORT_C CAESDecryptor* CAESDecryptor::NewLC(const TDesC8& aKey) sl@0: { sl@0: return CAESDecryptorShim::NewLC(aKey); sl@0: } sl@0: sl@0: CAESDecryptor::CAESDecryptor() sl@0: { sl@0: } sl@0: sl@0: // All these methods have been replaced by the shim sl@0: #ifdef _BullseyeCoverage sl@0: #pragma suppress_warnings on sl@0: #pragma BullseyeCoverage off sl@0: #pragma suppress_warnings off sl@0: #endif sl@0: sl@0: TInt CAESDecryptor::BlockSize() const sl@0: { sl@0: // Method replaced by shim sl@0: ASSERT(EFalse); sl@0: return 0; sl@0: } sl@0: sl@0: void CAESDecryptor::Transform(TDes8& /*aBlock*/) sl@0: { sl@0: // Method replaced by shim sl@0: ASSERT(EFalse); sl@0: } sl@0: sl@0: void CAESDecryptor::SetKey(const TDesC8& /*aKey*/) sl@0: { sl@0: // Method replaced by shim sl@0: ASSERT(EFalse); sl@0: } sl@0: sl@0: TInt CAESEncryptor::BlockSize() const sl@0: { sl@0: // Method replaced by shim sl@0: ASSERT(EFalse); sl@0: return KAESBlockBytes; sl@0: } sl@0: sl@0: void CAESEncryptor::Transform(TDes8& /*aBlock*/) sl@0: { sl@0: // Method replaced by shim sl@0: ASSERT(EFalse); sl@0: } sl@0: