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