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