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.
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
inline TUint BitsToBytes(TUint bitCount)
sl@0
    73
	{
sl@0
    74
	return ((bitCount+7)/(BYTE_BITS));
sl@0
    75
	}
sl@0
    76
sl@0
    77
inline TUint BytesToWords(TUint byteCount)
sl@0
    78
	{
sl@0
    79
	return ((byteCount+WORD_SIZE-1)/WORD_SIZE);
sl@0
    80
	}
sl@0
    81
sl@0
    82
inline TUint BitsToWords(TUint bitCount)
sl@0
    83
	{
sl@0
    84
	return ((bitCount+WORD_BITS-1)/(WORD_BITS));
sl@0
    85
	}
sl@0
    86
sl@0
    87
inline TUint WordsToBits(TUint wordCount)
sl@0
    88
	{
sl@0
    89
	return wordCount * WORD_BITS;
sl@0
    90
	}
sl@0
    91
sl@0
    92
inline TUint BytesToBits(TUint byteCount)
sl@0
    93
	{
sl@0
    94
	return byteCount * BYTE_BITS;
sl@0
    95
	}
sl@0
    96
sl@0
    97
inline TUint WordsToBytes(TUint wordCount)
sl@0
    98
	{
sl@0
    99
	return wordCount * WORD_BYTES;
sl@0
   100
	}
sl@0
   101
sl@0
   102
inline void XorWords(TUint* r, const TUint* a, TUint n)
sl@0
   103
	{
sl@0
   104
	assert(((TUint32)r & 3) == 0); // Catch alignment problems
sl@0
   105
	
sl@0
   106
	for (TUint i=0; i<n; i++)
sl@0
   107
		r[i] ^= a[i];
sl@0
   108
	}
sl@0
   109
sl@0
   110
inline void XorBuf(TUint8* buf, const TUint8* mask, TUint count)
sl@0
   111
	{
sl@0
   112
	if (((TUint)buf | (TUint)mask | count) % WORD_SIZE == 0) 
sl@0
   113
		{
sl@0
   114
		XorWords((TUint*)buf, (const TUint*)mask, count/WORD_SIZE); 
sl@0
   115
		}
sl@0
   116
	else
sl@0
   117
		{
sl@0
   118
		for (TUint i=0; i<count; i++)
sl@0
   119
			buf[i] ^= mask[i];
sl@0
   120
		}
sl@0
   121
	}
sl@0
   122
sl@0
   123
// ************** rotate functions ***************
sl@0
   124
template <class T> inline T rotlFixed(T x, TUint y)
sl@0
   125
	{
sl@0
   126
	assert(y < sizeof(T)*8);
sl@0
   127
	return ( (T)((x<<y) | (x>>(sizeof(T)*8-y))) );
sl@0
   128
	}
sl@0
   129
sl@0
   130
template <class T> inline T rotrFixed(T x, TUint y)
sl@0
   131
	{
sl@0
   132
	assert(y < sizeof(T)*8);
sl@0
   133
	return ((T)((x>>y) | (x<<(sizeof(T)*8-y))));
sl@0
   134
	}
sl@0
   135
sl@0
   136
inline TUint32 byteReverse(TUint32 value)
sl@0
   137
	{
sl@0
   138
	value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
sl@0
   139
	return rotlFixed(value, 16U);
sl@0
   140
	}
sl@0
   141
sl@0
   142
template <class T>
sl@0
   143
void byteReverse(T* out, const T* in, TUint32 byteCount)
sl@0
   144
	{
sl@0
   145
	TUint count = (byteCount+sizeof(T)-1)/sizeof(T);
sl@0
   146
	for (TUint i=0; i<count; i++)
sl@0
   147
		out[i] = byteReverse(in[i]);
sl@0
   148
	}
sl@0
   149
sl@0
   150
template <class T>
sl@0
   151
inline void GetUserKeyLittleEndian(T *out, TUint32 outlen, const TUint8* in, TUint32 inlen)
sl@0
   152
	{
sl@0
   153
	const TUint U = sizeof(T);
sl@0
   154
	assert(inlen <= outlen*U);
sl@0
   155
	Mem::Copy(out, in, inlen);
sl@0
   156
	Mem::FillZ((TUint8*)out+inlen, outlen*U-inlen);
sl@0
   157
	}
sl@0
   158
sl@0
   159
template <class T>
sl@0
   160
inline void GetUserKeyBigEndian(T *out, TUint32 outlen, const TUint8* in, TUint32 inlen)
sl@0
   161
	{
sl@0
   162
	const TUint U = sizeof(T);
sl@0
   163
	assert(inlen <= outlen*U);
sl@0
   164
	Mem::Copy(out, in, inlen);
sl@0
   165
	Mem::FillZ((TUint8*)out+inlen, outlen*U-inlen);
sl@0
   166
	byteReverse(out, out, inlen);
sl@0
   167
	}
sl@0
   168
sl@0
   169
// The following methods have be changed to use byte rather than word accesses,
sl@0
   170
// as if the input pointer is not be word aligned a fault occurs on arm
sl@0
   171
// hardware.  This isn't optimal from a performance point of view, but it is
sl@0
   172
// neccessary because the crypto interfaces (CSymmetricCipher,
sl@0
   173
// CBlockTransformation) allow clients to pass non-aligned data.
sl@0
   174
sl@0
   175
// Fetch 4 words from user's buffer into "a", "b", "c", "d" in LITTLE-endian order
sl@0
   176
inline void GetBlockLittleEndian(const TUint8* block, TUint16 &a, TUint16 &b, TUint16 &c, TUint16 &d)
sl@0
   177
	{
sl@0
   178
	a = (TUint16)(block[0] | block[1] << 8);
sl@0
   179
	b = (TUint16)(block[2] | block[3] << 8);
sl@0
   180
	c = (TUint16)(block[4] | block[5] << 8);
sl@0
   181
	d = (TUint16)(block[6] | block[7] << 8);
sl@0
   182
	}
sl@0
   183
sl@0
   184
// Put 4 words back into user's buffer in LITTLE-endian order
sl@0
   185
inline void PutBlockLittleEndian(TUint8* block, TUint16 a, TUint16 b, TUint16 c, TUint16 d)
sl@0
   186
	{
sl@0
   187
	block[0] = (TUint8)(a & 0xff);
sl@0
   188
	block[1] = (TUint8)(a >> 8);
sl@0
   189
	block[2] = (TUint8)(b & 0xff);
sl@0
   190
	block[3] = (TUint8)(b >> 8);
sl@0
   191
	block[4] = (TUint8)(c & 0xff);
sl@0
   192
	block[5] = (TUint8)(c >> 8);
sl@0
   193
	block[6] = (TUint8)(d & 0xff);
sl@0
   194
	block[7] = (TUint8)(d >> 8);
sl@0
   195
	}
sl@0
   196
sl@0
   197
// Fetch 1 word from user's buffer in BIG-endian order
sl@0
   198
inline void GetWordBigEndian(const TUint8* block, TUint32 &a)
sl@0
   199
	{
sl@0
   200
	a = block[0] << 24 | block[1] << 16 | block[2] << 8 | block[3];
sl@0
   201
	}
sl@0
   202
sl@0
   203
// Put 1 word back into user's buffer in BIG-endian order
sl@0
   204
inline void PutWordBigEndian(TUint8* block, TUint32 a)
sl@0
   205
	{
sl@0
   206
	block[0] = (TUint8)(a >> 24);
sl@0
   207
	block[1] = (TUint8)((a >> 16) & 0xff);
sl@0
   208
	block[2] = (TUint8)((a >> 8) & 0xff);
sl@0
   209
	block[3] = (TUint8)(a & 0xff);
sl@0
   210
	}
sl@0
   211
sl@0
   212
// Fetch 2 words from user's buffer into "a", "b" in BIG-endian order
sl@0
   213
inline void GetBlockBigEndian(const TUint8* block, TUint32 &a, TUint32& b)
sl@0
   214
	{
sl@0
   215
	GetWordBigEndian(block, a);
sl@0
   216
	GetWordBigEndian(block + 4, b);
sl@0
   217
	}
sl@0
   218
sl@0
   219
// Put 2 words back into user's buffer in BIG-endian order
sl@0
   220
inline void PutBlockBigEndian(TUint8* block, TUint32 a, TUint32 b)
sl@0
   221
	{
sl@0
   222
	PutWordBigEndian(block, a);
sl@0
   223
	PutWordBigEndian(block + 4, b);
sl@0
   224
	}
sl@0
   225
sl@0
   226
// Fetch 4 words from user's buffer into "a", "b", "c", "d" in BIG-endian order
sl@0
   227
inline void GetBlockBigEndian(const TUint8* block, TUint32& a, TUint32& b, TUint32& c, TUint32& d)
sl@0
   228
	{
sl@0
   229
	GetWordBigEndian(block, a);
sl@0
   230
	GetWordBigEndian(block + 4, b);
sl@0
   231
	GetWordBigEndian(block + 8, c);
sl@0
   232
	GetWordBigEndian(block + 12, d);
sl@0
   233
	}
sl@0
   234
sl@0
   235
// Put 4 words back into user's buffer in BIG-endian order
sl@0
   236
inline void PutBlockBigEndian(TUint8* block, TUint32 a, TUint32 b, TUint32 c, TUint32 d)
sl@0
   237
	{
sl@0
   238
	PutWordBigEndian(block, a);
sl@0
   239
	PutWordBigEndian(block + 4, b);
sl@0
   240
	PutWordBigEndian(block + 8, c);
sl@0
   241
	PutWordBigEndian(block + 12, d);
sl@0
   242
	}
sl@0
   243
sl@0
   244
#endif // __INLINES_H__