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