1 | /*
|
---|
2 | * Copyright (c) 2015 Jan Kolarik
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libieee80211
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /** @file ieee80211_impl.c
|
---|
34 | *
|
---|
35 | * IEEE 802.11 default device functions implementation.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <stdio.h>
|
---|
39 | #include <crypto.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include <errno.h>
|
---|
42 |
|
---|
43 | #include <ieee80211_impl.h>
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Default implementation of IEEE802.11 start function.
|
---|
47 | *
|
---|
48 | * @param ieee80211_dev Structure of IEEE802.11 device.
|
---|
49 | *
|
---|
50 | * @return EOK.
|
---|
51 | */
|
---|
52 | int ieee80211_start_impl(ieee80211_dev_t *ieee80211_dev)
|
---|
53 | {
|
---|
54 | return EOK;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Default implementation of IEEE802.11 TX handler function.
|
---|
59 | *
|
---|
60 | * @param ieee80211_dev Structure of IEEE802.11 device.
|
---|
61 | * @param buffer Buffer with data to send.
|
---|
62 | * @param buffer_size Size of buffer.
|
---|
63 | *
|
---|
64 | * @return EOK.
|
---|
65 | */
|
---|
66 | int ieee80211_tx_handler_impl(ieee80211_dev_t *ieee80211_dev, void *buffer,
|
---|
67 | size_t buffer_size)
|
---|
68 | {
|
---|
69 | return EOK;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Default implementation of IEEE802.11 set frequency function.
|
---|
74 | *
|
---|
75 | * @param ieee80211_dev Structure of IEEE802.11 device.
|
---|
76 | * @param freq Value of frequency to be switched on.
|
---|
77 | *
|
---|
78 | * @return EOK.
|
---|
79 | */
|
---|
80 | int ieee80211_set_freq_impl(ieee80211_dev_t *ieee80211_dev, uint16_t freq)
|
---|
81 | {
|
---|
82 | return EOK;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Default implementation of IEEE802.11 BSSID change function.
|
---|
87 | *
|
---|
88 | * @param ieee80211_dev Structure of IEEE802.11 device.
|
---|
89 | *
|
---|
90 | * @return EOK.
|
---|
91 | */
|
---|
92 | int ieee80211_bssid_change_impl(ieee80211_dev_t *ieee80211_dev,
|
---|
93 | bool connected)
|
---|
94 | {
|
---|
95 | return EOK;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Default implementation of IEEE802.11 key config function.
|
---|
100 | *
|
---|
101 | * @param ieee80211_dev Structure of IEEE802.11 device.
|
---|
102 | *
|
---|
103 | * @return EOK.
|
---|
104 | */
|
---|
105 | int ieee80211_key_config_impl(ieee80211_dev_t *ieee80211_dev,
|
---|
106 | ieee80211_key_config_t *key_conf, bool insert)
|
---|
107 | {
|
---|
108 | return EOK;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Default implementation of IEEE802.11 scan function.
|
---|
113 | *
|
---|
114 | * @param ieee80211_dev Structure of IEEE802.11 device.
|
---|
115 | * @param clear Whether to clear current scan results.
|
---|
116 | *
|
---|
117 | * @return EOK if succeed, negative error code otherwise.
|
---|
118 | */
|
---|
119 | int ieee80211_scan_impl(ieee80211_dev_t *ieee80211_dev)
|
---|
120 | {
|
---|
121 | fibril_mutex_lock(&ieee80211_dev->scan_mutex);
|
---|
122 |
|
---|
123 | if(ieee80211_get_auth_phase(ieee80211_dev) ==
|
---|
124 | IEEE80211_AUTH_DISCONNECTED) {
|
---|
125 | fibril_mutex_lock(&ieee80211_dev->ap_list.results_mutex);
|
---|
126 | /* Remove old entries we don't receive beacons from. */
|
---|
127 | ieee80211_scan_result_list_t *result_list =
|
---|
128 | &ieee80211_dev->ap_list;
|
---|
129 | list_foreach_safe(result_list->list, cur_link, next_link) {
|
---|
130 | ieee80211_scan_result_link_t *cur_result =
|
---|
131 | list_get_instance(cur_link,
|
---|
132 | ieee80211_scan_result_link_t,
|
---|
133 | link);
|
---|
134 | if((time(NULL) - cur_result->last_beacon) >
|
---|
135 | MAX_KEEP_SCAN_SPAN_SEC) {
|
---|
136 | ieee80211_scan_result_list_remove(result_list,
|
---|
137 | cur_result);
|
---|
138 | }
|
---|
139 | }
|
---|
140 | fibril_mutex_unlock(&ieee80211_dev->ap_list.results_mutex);
|
---|
141 |
|
---|
142 | uint16_t orig_freq = ieee80211_dev->current_freq;
|
---|
143 |
|
---|
144 | for(uint16_t freq = IEEE80211_FIRST_FREQ;
|
---|
145 | freq <= IEEE80211_MAX_FREQ;
|
---|
146 | freq += IEEE80211_CHANNEL_GAP) {
|
---|
147 | if(ieee80211_pending_connect_request(ieee80211_dev)) {
|
---|
148 | break;
|
---|
149 | }
|
---|
150 |
|
---|
151 | ieee80211_dev->ops->set_freq(ieee80211_dev, freq);
|
---|
152 | ieee80211_probe_request(ieee80211_dev, NULL);
|
---|
153 |
|
---|
154 | /* Wait for probe responses. */
|
---|
155 | async_usleep(SCAN_CHANNEL_WAIT_USEC);
|
---|
156 | }
|
---|
157 |
|
---|
158 | ieee80211_dev->ops->set_freq(ieee80211_dev, orig_freq);
|
---|
159 | }
|
---|
160 |
|
---|
161 | fibril_mutex_unlock(&ieee80211_dev->scan_mutex);
|
---|
162 |
|
---|
163 | return EOK;
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Pseudorandom function used for IEEE 802.11 pairwise key computation
|
---|
168 | * using SHA1 hash algorithm.
|
---|
169 | *
|
---|
170 | * @param key Key with PBKDF2 encrypted passphrase.
|
---|
171 | * @param data Concatenated sequence of both mac addresses and nonces.
|
---|
172 | * @param hash Output parameter for result hash.
|
---|
173 | * @param output_size Length of output sequence to be generated.
|
---|
174 | *
|
---|
175 | * @return EINVAL when key or data not specified, ENOMEM when pointer for
|
---|
176 | * output hash result is not allocated, otherwise EOK.
|
---|
177 | */
|
---|
178 | int ieee80211_prf(uint8_t *key, uint8_t *data, uint8_t *hash,
|
---|
179 | size_t output_size)
|
---|
180 | {
|
---|
181 | if(!key || !data)
|
---|
182 | return EINVAL;
|
---|
183 |
|
---|
184 | if(!hash)
|
---|
185 | return ENOMEM;
|
---|
186 |
|
---|
187 | size_t iters = ((output_size * 8) + 159) / 160;
|
---|
188 |
|
---|
189 | const char *a = "Pairwise key expansion";
|
---|
190 | uint8_t result[HASH_SHA1*iters];
|
---|
191 | uint8_t temp[HASH_SHA1];
|
---|
192 | size_t data_size = PRF_CRYPT_DATA_LENGTH + str_size(a) + 2;
|
---|
193 | uint8_t work_arr[data_size];
|
---|
194 | memset(work_arr, 0, data_size);
|
---|
195 |
|
---|
196 | memcpy(work_arr, a, str_size(a));
|
---|
197 | memcpy(work_arr + str_size(a) + 1, data, PRF_CRYPT_DATA_LENGTH);
|
---|
198 |
|
---|
199 | for(uint8_t i = 0; i < iters; i++) {
|
---|
200 | memcpy(work_arr + data_size - 1, &i, 1);
|
---|
201 | hmac(key, PBKDF2_KEY_LENGTH, work_arr, data_size, temp,
|
---|
202 | HASH_SHA1);
|
---|
203 | memcpy(result + i*HASH_SHA1, temp, HASH_SHA1);
|
---|
204 | }
|
---|
205 |
|
---|
206 | memcpy(hash, result, output_size);
|
---|
207 |
|
---|
208 | return EOK;
|
---|
209 | }
|
---|
210 |
|
---|
211 | int ieee80211_rc4_key_unwrap(uint8_t *key, uint8_t *data, size_t data_size,
|
---|
212 | uint8_t *output)
|
---|
213 | {
|
---|
214 | return rc4(key, 32, data, data_size, 256, output);
|
---|
215 | }
|
---|
216 |
|
---|
217 | int ieee80211_aes_key_unwrap(uint8_t *kek, uint8_t *data, size_t data_size,
|
---|
218 | uint8_t *output)
|
---|
219 | {
|
---|
220 | if(!kek || !data)
|
---|
221 | return EINVAL;
|
---|
222 |
|
---|
223 | if(!output)
|
---|
224 | return ENOMEM;
|
---|
225 |
|
---|
226 | uint32_t n = data_size/8 - 1;
|
---|
227 | uint8_t work_data[n*8];
|
---|
228 | uint8_t work_input[AES_CIPHER_LENGTH];
|
---|
229 | uint8_t work_output[AES_CIPHER_LENGTH];
|
---|
230 | uint8_t *work_block;
|
---|
231 | uint8_t a[8];
|
---|
232 | memcpy(a, data, 8);
|
---|
233 | uint64_t mask = 0xFF;
|
---|
234 | uint8_t shift, shb;
|
---|
235 |
|
---|
236 | memcpy(work_data, data + 8, n*8);
|
---|
237 | for(int j = 5; j >= 0; j--) {
|
---|
238 | for(int i = n; i > 0; i--) {
|
---|
239 | for(size_t k = 0; k < 8; k++) {
|
---|
240 | shift = 56 - 8*k;
|
---|
241 | shb = ((n*j+i) & (mask << shift)) >> shift;
|
---|
242 | a[k] ^= shb;
|
---|
243 | }
|
---|
244 | work_block = work_data + (i-1)*8;
|
---|
245 | memcpy(work_input, a, 8);
|
---|
246 | memcpy(work_input + 8, work_block, 8);
|
---|
247 | aes_decrypt(kek, work_input, work_output);
|
---|
248 | memcpy(a, work_output, 8);
|
---|
249 | memcpy(work_data + (i-1)*8, work_output + 8, 8);
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | size_t it;
|
---|
254 | for(it = 0; it < 8; it++) {
|
---|
255 | if(a[it] != 0xA6)
|
---|
256 | break;
|
---|
257 | }
|
---|
258 |
|
---|
259 | if(it == 8) {
|
---|
260 | memcpy(output, work_data, n*8);
|
---|
261 | return EOK;
|
---|
262 | } else {
|
---|
263 | return EINVAL;
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | static void ieee80211_michael_mic_block(uint32_t *l, uint32_t *r,
|
---|
268 | uint32_t value)
|
---|
269 | {
|
---|
270 | *l ^= value;
|
---|
271 | *r ^= rotl_uint32(*l, 17);
|
---|
272 | *l += *r;
|
---|
273 | *r ^= ((*l & 0x00FF00FF) << 8) | ((*l & 0xFF00FF00) >> 8);
|
---|
274 | *l += *r;
|
---|
275 | *r ^= rotl_uint32(*l, 3);
|
---|
276 | *l += *r;
|
---|
277 | *r ^= rotr_uint32(*l, 2);
|
---|
278 | *l += *r;
|
---|
279 | }
|
---|
280 |
|
---|
281 | int ieee80211_michael_mic(uint8_t *key, uint8_t *buffer, size_t size,
|
---|
282 | uint8_t *mic)
|
---|
283 | {
|
---|
284 | if(!key || !buffer)
|
---|
285 | return EINVAL;
|
---|
286 |
|
---|
287 | if(!mic)
|
---|
288 | return ENOMEM;
|
---|
289 |
|
---|
290 | uint32_t l = uint32le_from_seq(key);
|
---|
291 | uint32_t r = uint32le_from_seq(key + 4);
|
---|
292 |
|
---|
293 | ieee80211_data_header_t *data_header =
|
---|
294 | (ieee80211_data_header_t *) buffer;
|
---|
295 |
|
---|
296 | uint8_t *data = buffer + sizeof(ieee80211_data_header_t) +
|
---|
297 | IEEE80211_TKIP_HEADER_LENGTH;
|
---|
298 | size_t data_size = size - sizeof(ieee80211_data_header_t) -
|
---|
299 | IEEE80211_TKIP_HEADER_LENGTH;
|
---|
300 |
|
---|
301 | /* Process header. */
|
---|
302 | uint8_t *src_addr =
|
---|
303 | ieee80211_is_fromds_frame(data_header->frame_ctrl) ?
|
---|
304 | data_header->address3 : data_header->address2;
|
---|
305 | uint8_t *dest_addr =
|
---|
306 | ieee80211_is_tods_frame(data_header->frame_ctrl) ?
|
---|
307 | data_header->address3 : data_header->address1;
|
---|
308 |
|
---|
309 | ieee80211_michael_mic_block(&l, &r, uint32le_from_seq(dest_addr));
|
---|
310 | ieee80211_michael_mic_block(&l, &r,
|
---|
311 | uint16le_from_seq(dest_addr + 4) |
|
---|
312 | (uint16le_from_seq(src_addr) << 16));
|
---|
313 | ieee80211_michael_mic_block(&l, &r, uint32le_from_seq(src_addr + 2));
|
---|
314 | ieee80211_michael_mic_block(&l, &r, 0);
|
---|
315 |
|
---|
316 | /* Process data. */
|
---|
317 | size_t blocks = data_size / 4;
|
---|
318 | size_t pad = data_size % 4;
|
---|
319 |
|
---|
320 | for(size_t k = 0; k < blocks; k++) {
|
---|
321 | ieee80211_michael_mic_block(&l, &r,
|
---|
322 | uint32le_from_seq(&data[k*4]));
|
---|
323 | }
|
---|
324 |
|
---|
325 | /* Add padding. */
|
---|
326 | uint32_t value = 0x5A;
|
---|
327 | for(size_t i = pad; i > 0; i--) {
|
---|
328 | value <<= 8;
|
---|
329 | value |= data[blocks*4 + (i-1)];
|
---|
330 | }
|
---|
331 |
|
---|
332 | ieee80211_michael_mic_block(&l, &r, value);
|
---|
333 | ieee80211_michael_mic_block(&l, &r, 0);
|
---|
334 |
|
---|
335 | l = host2uint32_t_le(l);
|
---|
336 | r = host2uint32_t_le(r);
|
---|
337 |
|
---|
338 | memcpy(mic, &l, 4);
|
---|
339 | memcpy(mic + 4, &r, 4);
|
---|
340 |
|
---|
341 | return EOK;
|
---|
342 | }
|
---|
343 |
|
---|
344 | uint16_t uint16le_from_seq(void *seq)
|
---|
345 | {
|
---|
346 | uint16_t *u16 = (uint16_t *) seq;
|
---|
347 | return uint16_t_le2host(*u16);
|
---|
348 | }
|
---|
349 |
|
---|
350 | uint32_t uint32le_from_seq(void *seq)
|
---|
351 | {
|
---|
352 | uint32_t *u32 = (uint32_t *) seq;
|
---|
353 | return uint32_t_le2host(*u32);
|
---|
354 | }
|
---|
355 |
|
---|
356 | uint16_t uint16be_from_seq(void *seq)
|
---|
357 | {
|
---|
358 | uint16_t *u16 = (uint16_t *) seq;
|
---|
359 | return uint16_t_be2host(*u16);
|
---|
360 | }
|
---|
361 |
|
---|
362 | uint32_t uint32be_from_seq(void *seq)
|
---|
363 | {
|
---|
364 | uint32_t *u32 = (uint32_t *) seq;
|
---|
365 | return uint32_t_be2host(*u32);
|
---|
366 | }
|
---|
367 |
|
---|
368 | int rnd_sequence(uint8_t *sequence, size_t length)
|
---|
369 | {
|
---|
370 | if(!sequence)
|
---|
371 | return ENOMEM;
|
---|
372 |
|
---|
373 | for(size_t i = 0; i < length; i++) {
|
---|
374 | sequence[i] = (uint8_t) rand();
|
---|
375 | }
|
---|
376 |
|
---|
377 | return EOK;
|
---|
378 | }
|
---|
379 |
|
---|
380 | uint8_t *min_sequence(uint8_t *seq1, uint8_t *seq2, size_t size)
|
---|
381 | {
|
---|
382 | if(!seq1 || !seq2)
|
---|
383 | return NULL;
|
---|
384 |
|
---|
385 | for(size_t i = 0; i < size; i++) {
|
---|
386 | if(seq1[i] < seq2[i]) {
|
---|
387 | return seq1;
|
---|
388 | } else if(seq1[i] > seq2[i]) {
|
---|
389 | return seq2;
|
---|
390 | }
|
---|
391 | }
|
---|
392 |
|
---|
393 | return seq1;
|
---|
394 | }
|
---|
395 |
|
---|
396 | uint8_t *max_sequence(uint8_t *seq1, uint8_t *seq2, size_t size)
|
---|
397 | {
|
---|
398 | uint8_t *min = min_sequence(seq1, seq2, size);
|
---|
399 | if(min == seq1) {
|
---|
400 | return seq2;
|
---|
401 | } else {
|
---|
402 | return seq1;
|
---|
403 | }
|
---|
404 | }
|
---|
405 |
|
---|
406 | /** @}
|
---|
407 | */ |
---|