sl@0: /* sl@0: * Copyright (c) 2003-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: /** sl@0: @file sl@0: @internalTechnology sl@0: */ sl@0: sl@0: #ifndef __INLINES_H__ sl@0: #define __INLINES_H__ sl@0: sl@0: #include sl@0: sl@0: #define assert(x) __ASSERT_DEBUG((x), User::Panic(_L("crypto.dll"), 1)) sl@0: sl@0: #if defined(__GCC32__) sl@0: typedef long long Int64; sl@0: typedef unsigned long long Uint64; sl@0: #elif defined(__VC32__) sl@0: typedef __int64 Int64; sl@0: typedef unsigned __int64 Uint64; sl@0: #elif defined(__CW32__) sl@0: #pragma longlong on sl@0: typedef long long Int64; sl@0: typedef unsigned long long Uint64; sl@0: #endif sl@0: sl@0: typedef Uint64 dword; sl@0: typedef TUint word; sl@0: typedef TUint32 word32; sl@0: sl@0: const TUint WORD_SIZE = sizeof(TUint); sl@0: const TUint WORD_BYTES = WORD_SIZE; sl@0: const TUint BYTE_BITS = 8; sl@0: const TUint WORD_BITS = WORD_SIZE*BYTE_BITS; sl@0: sl@0: //These next two versions of GETBYTE compile to LDR's of words and then shifts sl@0: //and ands to get it down to a byte. sl@0: //#define GETBYTE(x, y) (TUint)(((x)>>(8*(y)))&255) sl@0: //#define GETBYTE(x, y) (TUint)TUint8((x)>>(8*(y))) sl@0: sl@0: //This next version gets the best assembler on gcc and armv4 (it uses LDRB sl@0: //rather than shifts and ands sl@0: #define GETBYTE(x, y) (((TUint8 *)&(x))[y]) sl@0: sl@0: #define MAKE_DWORD(lowWord, highWord) ((dword(highWord)<>WORD_BITS) sl@0: sl@0: template inline void TClassSwap(T& a, T& b) sl@0: { sl@0: T temp(a); sl@0: a = b; sl@0: b = temp; sl@0: } sl@0: sl@0: inline TUint BitsToBytes(TUint bitCount) sl@0: { sl@0: return ((bitCount+7)/(BYTE_BITS)); sl@0: } sl@0: sl@0: inline TUint BytesToWords(TUint byteCount) sl@0: { sl@0: return ((byteCount+WORD_SIZE-1)/WORD_SIZE); sl@0: } sl@0: sl@0: inline TUint BitsToWords(TUint bitCount) sl@0: { sl@0: return ((bitCount+WORD_BITS-1)/(WORD_BITS)); sl@0: } sl@0: sl@0: inline TUint WordsToBits(TUint wordCount) sl@0: { sl@0: return wordCount * WORD_BITS; sl@0: } sl@0: sl@0: inline TUint BytesToBits(TUint byteCount) sl@0: { sl@0: return byteCount * BYTE_BITS; sl@0: } sl@0: sl@0: inline TUint WordsToBytes(TUint wordCount) sl@0: { sl@0: return wordCount * WORD_BYTES; sl@0: } sl@0: sl@0: inline void XorWords(TUint* r, const TUint* a, TUint n) sl@0: { sl@0: assert(((TUint32)r & 3) == 0); // Catch alignment problems sl@0: sl@0: for (TUint i=0; i inline T rotlFixed(T x, TUint y) sl@0: { sl@0: assert(y < sizeof(T)*8); sl@0: return ( (T)((x<>(sizeof(T)*8-y))) ); sl@0: } sl@0: sl@0: template inline T rotrFixed(T x, TUint y) sl@0: { sl@0: assert(y < sizeof(T)*8); sl@0: return ((T)((x>>y) | (x<<(sizeof(T)*8-y)))); sl@0: } sl@0: sl@0: inline TUint32 byteReverse(TUint32 value) sl@0: { sl@0: value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8); sl@0: return rotlFixed(value, 16U); sl@0: } sl@0: sl@0: template sl@0: void byteReverse(T* out, const T* in, TUint32 byteCount) sl@0: { sl@0: TUint count = (byteCount+sizeof(T)-1)/sizeof(T); sl@0: for (TUint i=0; i sl@0: inline void GetUserKeyLittleEndian(T *out, TUint32 outlen, const TUint8* in, TUint32 inlen) sl@0: { sl@0: const TUint U = sizeof(T); sl@0: assert(inlen <= outlen*U); sl@0: Mem::Copy(out, in, inlen); sl@0: Mem::FillZ((TUint8*)out+inlen, outlen*U-inlen); sl@0: } sl@0: sl@0: template sl@0: inline void GetUserKeyBigEndian(T *out, TUint32 outlen, const TUint8* in, TUint32 inlen) sl@0: { sl@0: const TUint U = sizeof(T); sl@0: assert(inlen <= outlen*U); sl@0: Mem::Copy(out, in, inlen); sl@0: Mem::FillZ((TUint8*)out+inlen, outlen*U-inlen); sl@0: byteReverse(out, out, inlen); sl@0: } sl@0: sl@0: // The following methods have be changed to use byte rather than word accesses, sl@0: // as if the input pointer is not be word aligned a fault occurs on arm sl@0: // hardware. This isn't optimal from a performance point of view, but it is sl@0: // neccessary because the crypto interfaces (CSymmetricCipher, sl@0: // CBlockTransformation) allow clients to pass non-aligned data. sl@0: sl@0: // Fetch 4 words from user's buffer into "a", "b", "c", "d" in LITTLE-endian order sl@0: inline void GetBlockLittleEndian(const TUint8* block, TUint16 &a, TUint16 &b, TUint16 &c, TUint16 &d) sl@0: { sl@0: a = (TUint16)(block[0] | block[1] << 8); sl@0: b = (TUint16)(block[2] | block[3] << 8); sl@0: c = (TUint16)(block[4] | block[5] << 8); sl@0: d = (TUint16)(block[6] | block[7] << 8); sl@0: } sl@0: sl@0: // Put 4 words back into user's buffer in LITTLE-endian order sl@0: inline void PutBlockLittleEndian(TUint8* block, TUint16 a, TUint16 b, TUint16 c, TUint16 d) sl@0: { sl@0: block[0] = (TUint8)(a & 0xff); sl@0: block[1] = (TUint8)(a >> 8); sl@0: block[2] = (TUint8)(b & 0xff); sl@0: block[3] = (TUint8)(b >> 8); sl@0: block[4] = (TUint8)(c & 0xff); sl@0: block[5] = (TUint8)(c >> 8); sl@0: block[6] = (TUint8)(d & 0xff); sl@0: block[7] = (TUint8)(d >> 8); sl@0: } sl@0: sl@0: // Fetch 1 word from user's buffer in BIG-endian order sl@0: inline void GetWordBigEndian(const TUint8* block, TUint32 &a) sl@0: { sl@0: a = block[0] << 24 | block[1] << 16 | block[2] << 8 | block[3]; sl@0: } sl@0: sl@0: // Put 1 word back into user's buffer in BIG-endian order sl@0: inline void PutWordBigEndian(TUint8* block, TUint32 a) sl@0: { sl@0: block[0] = (TUint8)(a >> 24); sl@0: block[1] = (TUint8)((a >> 16) & 0xff); sl@0: block[2] = (TUint8)((a >> 8) & 0xff); sl@0: block[3] = (TUint8)(a & 0xff); sl@0: } sl@0: sl@0: // Fetch 2 words from user's buffer into "a", "b" in BIG-endian order sl@0: inline void GetBlockBigEndian(const TUint8* block, TUint32 &a, TUint32& b) sl@0: { sl@0: GetWordBigEndian(block, a); sl@0: GetWordBigEndian(block + 4, b); sl@0: } sl@0: sl@0: // Put 2 words back into user's buffer in BIG-endian order sl@0: inline void PutBlockBigEndian(TUint8* block, TUint32 a, TUint32 b) sl@0: { sl@0: PutWordBigEndian(block, a); sl@0: PutWordBigEndian(block + 4, b); sl@0: } sl@0: sl@0: // Fetch 4 words from user's buffer into "a", "b", "c", "d" in BIG-endian order sl@0: inline void GetBlockBigEndian(const TUint8* block, TUint32& a, TUint32& b, TUint32& c, TUint32& d) sl@0: { sl@0: GetWordBigEndian(block, a); sl@0: GetWordBigEndian(block + 4, b); sl@0: GetWordBigEndian(block + 8, c); sl@0: GetWordBigEndian(block + 12, d); sl@0: } sl@0: sl@0: // Put 4 words back into user's buffer in BIG-endian order sl@0: inline void PutBlockBigEndian(TUint8* block, TUint32 a, TUint32 b, TUint32 c, TUint32 d) sl@0: { sl@0: PutWordBigEndian(block, a); sl@0: PutWordBigEndian(block + 4, b); sl@0: PutWordBigEndian(block + 8, c); sl@0: PutWordBigEndian(block + 12, d); sl@0: } sl@0: sl@0: #endif // __INLINES_H__