|
1 /* |
|
2 * Copyright (c) 2001-2006 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: EAP and WLAN authentication protocols. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // This is enumeration of EAPOL source code. |
|
20 #if defined(USE_EAP_MINIMUM_RELEASE_TRACES) |
|
21 #undef EAP_FILE_NUMBER_ENUM |
|
22 #define EAP_FILE_NUMBER_ENUM 41 |
|
23 #undef EAP_FILE_NUMBER_DATE |
|
24 #define EAP_FILE_NUMBER_DATE 1127594498 |
|
25 #endif //#if defined(USE_EAP_MINIMUM_RELEASE_TRACES) |
|
26 |
|
27 |
|
28 |
|
29 /** @file */ |
|
30 |
|
31 #include "eap_tools.h" |
|
32 |
|
33 //------------------------------------------------------------------- |
|
34 |
|
35 EAP_C_FUNC_EXPORT u16_t eap_htons(const u16_t value) |
|
36 { |
|
37 #if defined(EAP_LITTLE_ENDIAN) /// byte 0 is least significant (i386) |
|
38 return(static_cast<u16_t>((value >> (sizeof(u8_t)*8u)) |
|
39 | (value << (sizeof(u8_t)*8u)))); |
|
40 #elif defined(EAP_BIG_ENDIAN) /// byte 0 is most significant (mc68k) |
|
41 return value; |
|
42 #else |
|
43 #error ERROR: define EAP_LITTLE_ENDIAN (byte 0 is least significant (i386)) \ |
|
44 or EAP_BIG_ENDIAN (byte 0 is most significant (mc68k)). |
|
45 #endif |
|
46 } |
|
47 |
|
48 EAP_C_FUNC_EXPORT u32_t eap_htonl(const u32_t value) |
|
49 { |
|
50 #if defined(EAP_LITTLE_ENDIAN) /// byte 0 is least significant (i386) |
|
51 return(static_cast<u32_t>((value & 0xFF) << 24) |
|
52 | ((value & 0xFF00) << 8) |
|
53 | ((value & 0xFF0000) >> 8) |
|
54 | ((value & 0xFF000000) >> 24)); |
|
55 #elif defined(EAP_BIG_ENDIAN) /// byte 0 is most significant (mc68k) |
|
56 return value; |
|
57 #else |
|
58 #error ERROR: define EAP_LITTLE_ENDIAN (byte 0 is least significant (i386)) \ |
|
59 or EAP_BIG_ENDIAN (byte 0 is most significant (mc68k)). |
|
60 #endif |
|
61 } |
|
62 |
|
63 EAP_C_FUNC_EXPORT u64_t eap_htonll(const u64_t value) |
|
64 { |
|
65 |
|
66 #if defined(EAP_LITTLE_ENDIAN) /// byte 0 is least significant (i386) |
|
67 #if defined(__SYMBIAN32__) |
|
68 u32_t *val = reinterpret_cast<u32_t *>(const_cast<u64_t *>(&value)); |
|
69 u64_t out_buf; |
|
70 u32_t *out = reinterpret_cast<u32_t *>(&out_buf); |
|
71 |
|
72 out[0] = eap_htonl(val[1]); |
|
73 out[1] = eap_htonl(val[0]); |
|
74 |
|
75 return out_buf; |
|
76 #else |
|
77 return(static_cast<u64_t>((value & 0xFF) << 56) |
|
78 | ((value & 0xFF00) << 40) |
|
79 | ((value & 0xFF0000) << 24) |
|
80 | ((value & 0xFF000000) << 8) |
|
81 | (((value >> 32) & 0xFF) << 24) |
|
82 | (((value >> 32) & 0xFF00) << 8) |
|
83 | (((value >> 32) & 0xFF0000) >> 8) |
|
84 | (((value >> 32) & 0xFF000000) >> 24)); |
|
85 #endif |
|
86 #elif defined(EAP_BIG_ENDIAN) /// byte 0 is most significant (mc68k) |
|
87 return value; |
|
88 #else |
|
89 #error ERROR: define EAP_LITTLE_ENDIAN (byte 0 is least significant (i386)) \ |
|
90 or EAP_BIG_ENDIAN (byte 0 is most significant (mc68k)). |
|
91 #endif |
|
92 } |
|
93 |
|
94 EAP_C_FUNC_EXPORT u16_t eap_host_to_little_endian_short(const u16_t value) |
|
95 { |
|
96 #if defined(EAP_LITTLE_ENDIAN) /// byte 0 is least significant (i386) |
|
97 return value; |
|
98 #elif defined(EAP_BIG_ENDIAN) /// byte 0 is most significant (mc68k) |
|
99 return(static_cast<u16_t>((value & 0xFF) << 8) |
|
100 | ((value & 0xFF00) >> 8)); |
|
101 #else |
|
102 #error ERROR: define EAP_LITTLE_ENDIAN (byte 0 is least significant (i386)) \ |
|
103 or EAP_BIG_ENDIAN (byte 0 is most significant (mc68k)). |
|
104 #endif |
|
105 } |
|
106 |
|
107 EAP_C_FUNC_EXPORT u32_t eap_host_to_little_endian_long(const u32_t value) |
|
108 { |
|
109 #if defined(EAP_LITTLE_ENDIAN) /// byte 0 is least significant (i386) |
|
110 return value; |
|
111 #elif defined(EAP_BIG_ENDIAN) /// byte 0 is most significant (mc68k) |
|
112 return(static_cast<u32_t>((value & 0xFF) << 24) |
|
113 | ((value & 0xFF00) << 8) |
|
114 | ((value & 0xFF0000) >> 8) |
|
115 | ((value & 0xFF000000) >> 24)); |
|
116 #else |
|
117 #error ERROR: define EAP_LITTLE_ENDIAN (byte 0 is least significant (i386)) \ |
|
118 or EAP_BIG_ENDIAN (byte 0 is most significant (mc68k)). |
|
119 #endif |
|
120 } |
|
121 |
|
122 EAP_C_FUNC_EXPORT u64_t eap_host_to_little_endian_long_long(const u64_t value) |
|
123 { |
|
124 #if defined(EAP_LITTLE_ENDIAN) /// byte 0 is least significant (i386) |
|
125 return value; |
|
126 #elif defined(EAP_BIG_ENDIAN) /// byte 0 is most significant (mc68k) |
|
127 #if defined(__SYMBIAN32__) |
|
128 u32_t *val = reinterpret_cast<u32_t *>(const_cast<u64_t *>(&value)); |
|
129 u64_t out_buf; |
|
130 u32_t *out = reinterpret_cast<u32_t *>(&out_buf); |
|
131 |
|
132 out[0] = eap_host_to_little_endian_long(val[1]); |
|
133 out[1] = eap_host_to_little_endian_long(val[0]); |
|
134 |
|
135 return out_buf; |
|
136 #else |
|
137 return(static_cast<u64_t>((value & 0xFF) << 56) |
|
138 | ((value & 0xFF00) << 40) |
|
139 | ((value & 0xFF0000) << 24) |
|
140 | ((value & 0xFF000000) << 8) |
|
141 | (((value >> 32) & 0xFF) << 24) |
|
142 | (((value >> 32) & 0xFF00) << 8) |
|
143 | (((value >> 32) & 0xFF0000) >> 8) |
|
144 | (((value >> 32) & 0xFF000000) >> 24)); |
|
145 #endif |
|
146 #else |
|
147 #error ERROR: define EAP_LITTLE_ENDIAN (byte 0 is least significant (i386)) \ |
|
148 or EAP_BIG_ENDIAN (byte 0 is most significant (mc68k)). |
|
149 #endif |
|
150 } |
|
151 |
|
152 //------------------------------------------------------------------- |
|
153 |
|
154 EAP_C_FUNC_EXPORT eap_status_e eap_write_u16_t_little_endian_order( |
|
155 void * const p_data, |
|
156 const u32_t data_length, |
|
157 const u16_t value) |
|
158 { |
|
159 if (data_length < sizeof(u16_t) |
|
160 || p_data == 0) |
|
161 { |
|
162 return eap_status_allocation_error; |
|
163 } |
|
164 |
|
165 u8_t * const data = static_cast<u8_t *>(p_data); |
|
166 |
|
167 data[0] = static_cast<u8_t>((value) & 0xff); |
|
168 data[1] = static_cast<u8_t>((value >> 8) & 0xff); |
|
169 |
|
170 return eap_status_ok; |
|
171 } |
|
172 |
|
173 EAP_C_FUNC_EXPORT eap_status_e eap_write_u32_t_little_endian_order( |
|
174 void * const p_data, |
|
175 const u32_t data_length, |
|
176 const u32_t value) |
|
177 { |
|
178 if (data_length < sizeof(u32_t) |
|
179 || p_data == 0) |
|
180 { |
|
181 return eap_status_allocation_error; |
|
182 } |
|
183 |
|
184 u8_t * const data = static_cast<u8_t *>(p_data); |
|
185 |
|
186 data[0] = static_cast<u8_t>((value) & 0xff); |
|
187 data[1] = static_cast<u8_t>((value >> 8) & 0xff); |
|
188 data[2] = static_cast<u8_t>((value >> 16) & 0xff); |
|
189 data[3] = static_cast<u8_t>((value >> 24) & 0xff); |
|
190 |
|
191 return eap_status_ok; |
|
192 } |
|
193 |
|
194 |
|
195 EAP_C_FUNC_EXPORT eap_status_e eap_write_u64_t_little_endian_order( |
|
196 void * const p_data, |
|
197 const u32_t data_length, |
|
198 const u64_t value) |
|
199 { |
|
200 if (data_length < sizeof(u64_t) |
|
201 || p_data == 0) |
|
202 { |
|
203 return eap_status_allocation_error; |
|
204 } |
|
205 |
|
206 // This is used because Symbian does not have 64 bit shift operation. |
|
207 u8_t * const data = static_cast<u8_t *>(p_data); |
|
208 |
|
209 const u32_t * const outvalue = reinterpret_cast<const u32_t *>(&value); |
|
210 |
|
211 #if defined(EAP_LITTLE_ENDIAN) /// byte 0 is least significant (i386) |
|
212 const u32_t least_significant_u32_t_index = 0; |
|
213 const u32_t most_significant_u32_t_index = 1; |
|
214 #elif defined(EAP_BIG_ENDIAN) /// byte 0 is most significant (mc68k) |
|
215 const u32_t most_significant_u32_t_index = 0; |
|
216 const u32_t least_significant_u32_t_index = 1; |
|
217 #else |
|
218 #error ERROR: define EAP_LITTLE_ENDIAN (byte 0 is least significant (i386)) \ |
|
219 or EAP_BIG_ENDIAN (byte 0 is most significant (mc68k)). |
|
220 #endif |
|
221 |
|
222 eap_status_e status = eap_write_u32_t_little_endian_order( |
|
223 data, |
|
224 sizeof(u32_t), |
|
225 outvalue[least_significant_u32_t_index]); |
|
226 if (status != eap_status_ok) |
|
227 { |
|
228 return status; |
|
229 } |
|
230 |
|
231 status = eap_write_u32_t_little_endian_order( |
|
232 data+sizeof(u32_t), |
|
233 sizeof(u32_t), |
|
234 outvalue[most_significant_u32_t_index]); |
|
235 if (status != eap_status_ok) |
|
236 { |
|
237 return status; |
|
238 } |
|
239 |
|
240 return eap_status_ok; |
|
241 } |
|
242 |
|
243 |
|
244 EAP_C_FUNC_EXPORT u16_t eap_read_u16_t_little_endian_order( |
|
245 const void * const p_data, |
|
246 const u32_t data_length) |
|
247 { |
|
248 if (data_length < sizeof(u16_t) |
|
249 || p_data == 0) |
|
250 { |
|
251 return 0ul; |
|
252 } |
|
253 |
|
254 const u8_t * const data = static_cast<const u8_t *>(p_data); |
|
255 |
|
256 return(static_cast<u16_t>( |
|
257 (static_cast<u16_t>(data[0]) |
|
258 | static_cast<u16_t>(data[1]) << 8))); |
|
259 } |
|
260 |
|
261 EAP_C_FUNC_EXPORT u32_t eap_read_u32_t_little_endian_order( |
|
262 const void * const p_data, |
|
263 const u32_t data_length) |
|
264 { |
|
265 if (data_length < sizeof(u32_t) |
|
266 || p_data == 0) |
|
267 { |
|
268 return 0ul; |
|
269 } |
|
270 |
|
271 const u8_t * const data = static_cast<const u8_t *>(p_data); |
|
272 |
|
273 return(static_cast<u32_t>( |
|
274 static_cast<u32_t>(data[0]) |
|
275 | (static_cast<u32_t>(data[1]) << 8) |
|
276 | (static_cast<u32_t>(data[2]) << 16) |
|
277 | (static_cast<u32_t>(data[3]) << 24))); |
|
278 } |
|
279 |
|
280 EAP_C_FUNC_EXPORT u64_t eap_read_u64_t_little_endian_order( |
|
281 const void * const p_data, |
|
282 const u32_t data_length) |
|
283 { |
|
284 if (data_length < sizeof(u64_t) |
|
285 || p_data == 0) |
|
286 { |
|
287 return 0ul; |
|
288 } |
|
289 |
|
290 union |
|
291 { |
|
292 u64_t q; |
|
293 u64_struct w; |
|
294 } v; |
|
295 |
|
296 const u8_t *data = static_cast<const u8_t *>(p_data); |
|
297 |
|
298 v.w.low = eap_read_u32_t_little_endian_order(data,sizeof(u32_t)); |
|
299 data += sizeof(u32_t); |
|
300 v.w.high = eap_read_u32_t_little_endian_order(data,sizeof(u32_t)); |
|
301 |
|
302 return v.q; |
|
303 } |
|
304 |
|
305 |
|
306 EAP_C_FUNC_EXPORT u16_t eap_read_u16_t_network_order( |
|
307 const void * const p_data, |
|
308 const u32_t data_length) |
|
309 { |
|
310 if (data_length < sizeof(u16_t) |
|
311 || p_data == 0) |
|
312 { |
|
313 return 0ul; |
|
314 } |
|
315 |
|
316 const u8_t * const data = static_cast<const u8_t *>(p_data); |
|
317 |
|
318 return(static_cast<u16_t>( |
|
319 (static_cast<u16_t>(data[0]) << 8) |
|
320 | static_cast<u16_t>(data[1]))); |
|
321 } |
|
322 |
|
323 EAP_C_FUNC_EXPORT u32_t eap_read_u24_t_network_order( |
|
324 const void * const p_data, |
|
325 const u32_t data_length) |
|
326 { |
|
327 if (data_length < sizeof(u8_t)*3ul |
|
328 || p_data == 0) |
|
329 { |
|
330 return 0ul; |
|
331 } |
|
332 |
|
333 const u8_t * const data = static_cast<const u8_t *>(p_data); |
|
334 |
|
335 return(static_cast<u32_t>( |
|
336 (static_cast<u32_t>(data[0]) << 16) |
|
337 | (static_cast<u32_t>(data[1]) << 8) |
|
338 | static_cast<u32_t>(data[2]))); |
|
339 } |
|
340 |
|
341 EAP_C_FUNC_EXPORT u32_t eap_read_u32_t_network_order( |
|
342 const void * const p_data, |
|
343 const u32_t data_length) |
|
344 { |
|
345 if (data_length < sizeof(u32_t) |
|
346 || p_data == 0) |
|
347 { |
|
348 return 0ul; |
|
349 } |
|
350 |
|
351 const u8_t * const data = static_cast<const u8_t *>(p_data); |
|
352 |
|
353 return(static_cast<u32_t>( |
|
354 (static_cast<u32_t>(data[0]) << 24) |
|
355 | (static_cast<u32_t>(data[1]) << 16) |
|
356 | (static_cast<u32_t>(data[2]) << 8) |
|
357 | static_cast<u32_t>(data[3]))); |
|
358 } |
|
359 |
|
360 |
|
361 /* The following function is problematic for ARM Linux. |
|
362 There is now linux_gnu code which _should_ work also |
|
363 with other architectures. It does not use 64 bit shift. |
|
364 */ |
|
365 EAP_C_FUNC_EXPORT u64_t eap_read_u64_t_network_order( |
|
366 const void * const p_data, |
|
367 const u32_t data_length) |
|
368 { |
|
369 if (data_length < sizeof(u64_t) |
|
370 || p_data == 0) |
|
371 { |
|
372 return 0ul; |
|
373 } |
|
374 |
|
375 union |
|
376 { |
|
377 u64_t q; |
|
378 u64_struct w; |
|
379 } v; |
|
380 |
|
381 const u8_t *data = static_cast<const u8_t *>(p_data); |
|
382 |
|
383 v.w.high = eap_read_u32_t_network_order(data,sizeof(u32_t)); |
|
384 data += sizeof(u32_t); |
|
385 v.w.low = eap_read_u32_t_network_order(data,sizeof(u32_t)); |
|
386 |
|
387 return v.q; |
|
388 } |
|
389 |
|
390 //------------------------------------------------------------------- |
|
391 |
|
392 EAP_C_FUNC_EXPORT eap_status_e eap_write_u16_t_network_order( |
|
393 void * const p_data, |
|
394 const u32_t data_length, |
|
395 const u16_t value) |
|
396 { |
|
397 if (data_length < sizeof(u16_t) |
|
398 || p_data == 0) |
|
399 { |
|
400 return eap_status_allocation_error; |
|
401 } |
|
402 |
|
403 u8_t * const data = static_cast<u8_t *>(p_data); |
|
404 |
|
405 data[0] = static_cast<u8_t>((value >> 8) & 0xff); |
|
406 data[1] = static_cast<u8_t>((value) & 0xff); |
|
407 |
|
408 return eap_status_ok; |
|
409 } |
|
410 |
|
411 EAP_C_FUNC_EXPORT eap_status_e eap_write_u24_t_network_order( |
|
412 void * const p_data, |
|
413 const u32_t data_length, |
|
414 const u32_t value) |
|
415 { |
|
416 if (data_length < 3ul*sizeof(u8_t) |
|
417 || p_data == 0) |
|
418 { |
|
419 return eap_status_allocation_error; |
|
420 } |
|
421 |
|
422 u8_t * const data = static_cast<u8_t *>(p_data); |
|
423 |
|
424 data[0] = static_cast<u8_t>((value >> 16) & 0xff); |
|
425 data[1] = static_cast<u8_t>((value >> 8) & 0xff); |
|
426 data[2] = static_cast<u8_t>((value) & 0xff); |
|
427 |
|
428 return eap_status_ok; |
|
429 } |
|
430 |
|
431 EAP_C_FUNC_EXPORT eap_status_e eap_write_u32_t_network_order( |
|
432 void * const p_data, |
|
433 const u32_t data_length, |
|
434 const u32_t value) |
|
435 { |
|
436 if (data_length < sizeof(u32_t) |
|
437 || p_data == 0) |
|
438 { |
|
439 return eap_status_allocation_error; |
|
440 } |
|
441 |
|
442 u8_t * const data = static_cast<u8_t *>(p_data); |
|
443 |
|
444 data[0] = static_cast<u8_t>((value >> 24) & 0xff); |
|
445 data[1] = static_cast<u8_t>((value >> 16) & 0xff); |
|
446 data[2] = static_cast<u8_t>((value >> 8) & 0xff); |
|
447 data[3] = static_cast<u8_t>((value) & 0xff); |
|
448 |
|
449 return eap_status_ok; |
|
450 } |
|
451 |
|
452 EAP_C_FUNC_EXPORT eap_status_e eap_write_u64_t_network_order( |
|
453 void * const p_data, |
|
454 const u32_t data_length, |
|
455 const u64_t value) |
|
456 { |
|
457 if (data_length < sizeof(u64_t) |
|
458 || p_data == 0) |
|
459 { |
|
460 return eap_status_allocation_error; |
|
461 } |
|
462 |
|
463 // This is used because Symbian does not have 64 bit shift operation. |
|
464 u8_t * const data = static_cast<u8_t *>(p_data); |
|
465 |
|
466 const u32_t * const outvalue = reinterpret_cast<const u32_t *>(&value); |
|
467 |
|
468 #if defined(EAP_LITTLE_ENDIAN) /// byte 0 is least significant (i386) |
|
469 const u32_t least_significant_u32_t_index = 0; |
|
470 const u32_t most_significant_u32_t_index = 1; |
|
471 #elif defined(EAP_BIG_ENDIAN) /// byte 0 is most significant (mc68k) |
|
472 const u32_t most_significant_u32_t_index = 0; |
|
473 const u32_t least_significant_u32_t_index = 1; |
|
474 #else |
|
475 #error ERROR: define EAP_LITTLE_ENDIAN (byte 0 is least significant (i386)) \ |
|
476 or EAP_BIG_ENDIAN (byte 0 is most significant (mc68k)). |
|
477 #endif |
|
478 |
|
479 eap_status_e status = eap_write_u32_t_network_order( |
|
480 data, |
|
481 sizeof(u32_t), |
|
482 outvalue[most_significant_u32_t_index]); |
|
483 if (status != eap_status_ok) |
|
484 { |
|
485 return status; |
|
486 } |
|
487 |
|
488 status = eap_write_u32_t_network_order( |
|
489 data+sizeof(u32_t), |
|
490 sizeof(u32_t), |
|
491 outvalue[least_significant_u32_t_index]); |
|
492 if (status != eap_status_ok) |
|
493 { |
|
494 return status; |
|
495 } |
|
496 |
|
497 return eap_status_ok; |
|
498 } |
|
499 |
|
500 //------------------------------------------------------------------- |
|
501 |
|
502 EAP_C_FUNC_EXPORT u64_t eap_shift_left_64_bit(u64_t value, u32_t shift) |
|
503 { |
|
504 if (shift > 63u) |
|
505 { |
|
506 return 0ul; |
|
507 } |
|
508 else if (shift == 0) |
|
509 { |
|
510 return value; |
|
511 } |
|
512 |
|
513 u32_t *val = reinterpret_cast<u32_t *>(&value); |
|
514 u64_t out_buf; |
|
515 u32_t *out = reinterpret_cast<u32_t *>(&out_buf); |
|
516 |
|
517 if (shift < 32ul) |
|
518 { |
|
519 out[0] = val[0] << shift; |
|
520 out[1] = (val[1] << shift) | val[0] >> (32ul - shift); |
|
521 } |
|
522 else |
|
523 { |
|
524 out[0] = 0ul; |
|
525 out[1] = val[0] << (shift - 32ul); |
|
526 } |
|
527 |
|
528 return out_buf; |
|
529 } |
|
530 |
|
531 EAP_C_FUNC_EXPORT u64_t eap_shift_right_64_bit(u64_t value, u32_t shift) |
|
532 { |
|
533 if (shift > 63u) |
|
534 { |
|
535 return 0ul; |
|
536 } |
|
537 else if (shift == 0) |
|
538 { |
|
539 return value; |
|
540 } |
|
541 |
|
542 u32_t *val = reinterpret_cast<u32_t *>(&value); |
|
543 u64_t out_buf; |
|
544 u32_t *out = reinterpret_cast<u32_t *>(&out_buf); |
|
545 |
|
546 if (shift < 32ul) |
|
547 { |
|
548 out[0] = (val[0] >> shift) | val[1] << (32ul - shift); |
|
549 out[1] = val[1] >> shift; |
|
550 } |
|
551 else |
|
552 { |
|
553 out[0] = val[1] >> (shift - 32ul); |
|
554 out[1] = 0ul; |
|
555 } |
|
556 |
|
557 return out_buf; |
|
558 } |
|
559 |
|
560 //------------------------------------------------------------------------------ |
|
561 |
|
562 |
|
563 |
|
564 // End. |