os/security/crypto/weakcrypto/source/common/inlines.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2003-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 /**
    20  @file 
    21  @internalTechnology
    22 */
    23  
    24 #ifndef __INLINES_H__
    25 #define __INLINES_H__
    26 
    27 #include <e32base.h>
    28 
    29 #define assert(x) __ASSERT_DEBUG((x), User::Panic(_L("crypto.dll"), 1))
    30 
    31 #if defined(__GCC32__)
    32 typedef long long Int64;
    33 typedef unsigned long long Uint64;
    34 #elif defined(__VC32__)
    35 typedef __int64 Int64;
    36 typedef unsigned __int64 Uint64;
    37 #elif defined(__CW32__)
    38 #pragma longlong on
    39 typedef long long Int64;
    40 typedef unsigned long long Uint64;
    41 #endif
    42 
    43 typedef Uint64 dword;
    44 typedef TUint word;
    45 typedef TUint32 word32;
    46 
    47 const TUint WORD_SIZE = sizeof(TUint); 
    48 const TUint WORD_BYTES = WORD_SIZE;
    49 const TUint BYTE_BITS = 8;
    50 const TUint WORD_BITS = WORD_SIZE*BYTE_BITS;
    51 
    52 //These next two versions of GETBYTE compile to LDR's of words and then shifts
    53 //and ands to get it down to a byte.
    54 //#define GETBYTE(x, y) (TUint)(((x)>>(8*(y)))&255)
    55 //#define GETBYTE(x, y) (TUint)TUint8((x)>>(8*(y)))
    56 
    57 //This next version gets the best assembler on gcc and armv4 (it uses LDRB
    58 //rather than shifts and ands
    59 #define GETBYTE(x, y) (((TUint8 *)&(x))[y])
    60 
    61 #define MAKE_DWORD(lowWord, highWord) ((dword(highWord)<<WORD_BITS) | (lowWord))
    62 #define LOW_WORD(x) (TUint32)(x)
    63 #define HIGH_WORD(x) (TUint32)((x)>>WORD_BITS)
    64 
    65 template <class T> inline void TClassSwap(T& a, T& b)
    66 	{
    67 	T temp(a);
    68 	a = b;
    69 	b = temp;
    70 	}
    71 	
    72 inline TUint BitsToBytes(TUint bitCount)
    73 	{
    74 	return ((bitCount+7)/(BYTE_BITS));
    75 	}
    76 
    77 inline TUint BytesToWords(TUint byteCount)
    78 	{
    79 	return ((byteCount+WORD_SIZE-1)/WORD_SIZE);
    80 	}
    81 
    82 inline TUint BitsToWords(TUint bitCount)
    83 	{
    84 	return ((bitCount+WORD_BITS-1)/(WORD_BITS));
    85 	}
    86 
    87 inline TUint WordsToBits(TUint wordCount)
    88 	{
    89 	return wordCount * WORD_BITS;
    90 	}
    91 
    92 inline TUint BytesToBits(TUint byteCount)
    93 	{
    94 	return byteCount * BYTE_BITS;
    95 	}
    96 
    97 inline TUint WordsToBytes(TUint wordCount)
    98 	{
    99 	return wordCount * WORD_BYTES;
   100 	}
   101 
   102 inline void XorWords(TUint* r, const TUint* a, TUint n)
   103 	{
   104 	assert(((TUint32)r & 3) == 0); // Catch alignment problems
   105 	
   106 	for (TUint i=0; i<n; i++)
   107 		r[i] ^= a[i];
   108 	}
   109 
   110 inline void XorBuf(TUint8* buf, const TUint8* mask, TUint count)
   111 	{
   112 	if (((TUint)buf | (TUint)mask | count) % WORD_SIZE == 0) 
   113 		{
   114 		XorWords((TUint*)buf, (const TUint*)mask, count/WORD_SIZE); 
   115 		}
   116 	else
   117 		{
   118 		for (TUint i=0; i<count; i++)
   119 			buf[i] ^= mask[i];
   120 		}
   121 	}
   122 
   123 // ************** rotate functions ***************
   124 template <class T> inline T rotlFixed(T x, TUint y)
   125 	{
   126 	assert(y < sizeof(T)*8);
   127 	return ( (T)((x<<y) | (x>>(sizeof(T)*8-y))) );
   128 	}
   129 
   130 template <class T> inline T rotrFixed(T x, TUint y)
   131 	{
   132 	assert(y < sizeof(T)*8);
   133 	return ((T)((x>>y) | (x<<(sizeof(T)*8-y))));
   134 	}
   135 
   136 inline TUint32 byteReverse(TUint32 value)
   137 	{
   138 	value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
   139 	return rotlFixed(value, 16U);
   140 	}
   141 
   142 template <class T>
   143 void byteReverse(T* out, const T* in, TUint32 byteCount)
   144 	{
   145 	TUint count = (byteCount+sizeof(T)-1)/sizeof(T);
   146 	for (TUint i=0; i<count; i++)
   147 		out[i] = byteReverse(in[i]);
   148 	}
   149 
   150 template <class T>
   151 inline void GetUserKeyLittleEndian(T *out, TUint32 outlen, const TUint8* in, TUint32 inlen)
   152 	{
   153 	const TUint U = sizeof(T);
   154 	assert(inlen <= outlen*U);
   155 	Mem::Copy(out, in, inlen);
   156 	Mem::FillZ((TUint8*)out+inlen, outlen*U-inlen);
   157 	}
   158 
   159 template <class T>
   160 inline void GetUserKeyBigEndian(T *out, TUint32 outlen, const TUint8* in, TUint32 inlen)
   161 	{
   162 	const TUint U = sizeof(T);
   163 	assert(inlen <= outlen*U);
   164 	Mem::Copy(out, in, inlen);
   165 	Mem::FillZ((TUint8*)out+inlen, outlen*U-inlen);
   166 	byteReverse(out, out, inlen);
   167 	}
   168 
   169 // The following methods have be changed to use byte rather than word accesses,
   170 // as if the input pointer is not be word aligned a fault occurs on arm
   171 // hardware.  This isn't optimal from a performance point of view, but it is
   172 // neccessary because the crypto interfaces (CSymmetricCipher,
   173 // CBlockTransformation) allow clients to pass non-aligned data.
   174 
   175 // Fetch 4 words from user's buffer into "a", "b", "c", "d" in LITTLE-endian order
   176 inline void GetBlockLittleEndian(const TUint8* block, TUint16 &a, TUint16 &b, TUint16 &c, TUint16 &d)
   177 	{
   178 	a = (TUint16)(block[0] | block[1] << 8);
   179 	b = (TUint16)(block[2] | block[3] << 8);
   180 	c = (TUint16)(block[4] | block[5] << 8);
   181 	d = (TUint16)(block[6] | block[7] << 8);
   182 	}
   183 
   184 // Put 4 words back into user's buffer in LITTLE-endian order
   185 inline void PutBlockLittleEndian(TUint8* block, TUint16 a, TUint16 b, TUint16 c, TUint16 d)
   186 	{
   187 	block[0] = (TUint8)(a & 0xff);
   188 	block[1] = (TUint8)(a >> 8);
   189 	block[2] = (TUint8)(b & 0xff);
   190 	block[3] = (TUint8)(b >> 8);
   191 	block[4] = (TUint8)(c & 0xff);
   192 	block[5] = (TUint8)(c >> 8);
   193 	block[6] = (TUint8)(d & 0xff);
   194 	block[7] = (TUint8)(d >> 8);
   195 	}
   196 
   197 // Fetch 1 word from user's buffer in BIG-endian order
   198 inline void GetWordBigEndian(const TUint8* block, TUint32 &a)
   199 	{
   200 	a = block[0] << 24 | block[1] << 16 | block[2] << 8 | block[3];
   201 	}
   202 
   203 // Put 1 word back into user's buffer in BIG-endian order
   204 inline void PutWordBigEndian(TUint8* block, TUint32 a)
   205 	{
   206 	block[0] = (TUint8)(a >> 24);
   207 	block[1] = (TUint8)((a >> 16) & 0xff);
   208 	block[2] = (TUint8)((a >> 8) & 0xff);
   209 	block[3] = (TUint8)(a & 0xff);
   210 	}
   211 
   212 // Fetch 2 words from user's buffer into "a", "b" in BIG-endian order
   213 inline void GetBlockBigEndian(const TUint8* block, TUint32 &a, TUint32& b)
   214 	{
   215 	GetWordBigEndian(block, a);
   216 	GetWordBigEndian(block + 4, b);
   217 	}
   218 
   219 // Put 2 words back into user's buffer in BIG-endian order
   220 inline void PutBlockBigEndian(TUint8* block, TUint32 a, TUint32 b)
   221 	{
   222 	PutWordBigEndian(block, a);
   223 	PutWordBigEndian(block + 4, b);
   224 	}
   225 
   226 // Fetch 4 words from user's buffer into "a", "b", "c", "d" in BIG-endian order
   227 inline void GetBlockBigEndian(const TUint8* block, TUint32& a, TUint32& b, TUint32& c, TUint32& d)
   228 	{
   229 	GetWordBigEndian(block, a);
   230 	GetWordBigEndian(block + 4, b);
   231 	GetWordBigEndian(block + 8, c);
   232 	GetWordBigEndian(block + 12, d);
   233 	}
   234 
   235 // Put 4 words back into user's buffer in BIG-endian order
   236 inline void PutBlockBigEndian(TUint8* block, TUint32 a, TUint32 b, TUint32 c, TUint32 d)
   237 	{
   238 	PutWordBigEndian(block, a);
   239 	PutWordBigEndian(block + 4, b);
   240 	PutWordBigEndian(block + 8, c);
   241 	PutWordBigEndian(block + 12, d);
   242 	}
   243 
   244 #endif // __INLINES_H__