source: mainline/uspace/lib/ieee80211/src/ieee80211_impl.c@ db51a6a6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since db51a6a6 was 8a64320e, checked in by Martin Decky <martin@…>, 10 years ago

pre-merge coding style cleanup and code review

  • Property mode set to 100644
File size: 10.1 KB
RevLine 
[59fa7ab]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
[8a64320e]34 *
[59fa7ab]35 * IEEE 802.11 default device functions implementation.
36 */
37
[1dcc0b9]38#include <stdio.h>
39#include <crypto.h>
40#include <stdlib.h>
[59fa7ab]41#include <errno.h>
42#include <ieee80211_impl.h>
43
[8a64320e]44/** Default implementation of IEEE802.11 start function.
45 *
[59fa7ab]46 * @param ieee80211_dev Structure of IEEE802.11 device.
[8a64320e]47 *
48 * @return EOK.
49 *
[59fa7ab]50 */
51int ieee80211_start_impl(ieee80211_dev_t *ieee80211_dev)
52{
53 return EOK;
54}
55
[8a64320e]56/** Default implementation of IEEE802.11 TX handler function.
57 *
[59fa7ab]58 * @param ieee80211_dev Structure of IEEE802.11 device.
[8a64320e]59 * @param buffer Buffer with data to send.
60 * @param buffer_size Size of buffer.
61 *
62 * @return EOK.
63 *
[59fa7ab]64 */
[8a64320e]65int ieee80211_tx_handler_impl(ieee80211_dev_t *ieee80211_dev, void *buffer,
66 size_t buffer_size)
[59fa7ab]67{
68 return EOK;
69}
70
[8a64320e]71/** Default implementation of IEEE802.11 set frequency function.
72 *
[59fa7ab]73 * @param ieee80211_dev Structure of IEEE802.11 device.
[8a64320e]74 * @param freq Value of frequency to be switched on.
75 *
76 * @return EOK.
77 *
[59fa7ab]78 */
79int ieee80211_set_freq_impl(ieee80211_dev_t *ieee80211_dev, uint16_t freq)
80{
81 return EOK;
82}
83
[8a64320e]84/** Default implementation of IEEE802.11 BSSID change function.
85 *
[1dcc0b9]86 * @param ieee80211_dev Structure of IEEE802.11 device.
[8a64320e]87 *
88 * @return EOK.
89 *
[1dcc0b9]90 */
[8a64320e]91int ieee80211_bssid_change_impl(ieee80211_dev_t *ieee80211_dev,
92 bool connected)
[1dcc0b9]93{
94 return EOK;
95}
96
[8a64320e]97/** Default implementation of IEEE802.11 key config function.
98 *
[1dcc0b9]99 * @param ieee80211_dev Structure of IEEE802.11 device.
[8a64320e]100 *
101 * @return EOK.
102 *
[1dcc0b9]103 */
104int ieee80211_key_config_impl(ieee80211_dev_t *ieee80211_dev,
[8a64320e]105 ieee80211_key_config_t *key_conf, bool insert)
[1dcc0b9]106{
107 return EOK;
108}
109
[8a64320e]110/** Default implementation of IEEE802.11 scan function.
111 *
[59fa7ab]112 * @param ieee80211_dev Structure of IEEE802.11 device.
[8a64320e]113 * @param clear Whether to clear current scan results.
114 *
115 * @return EOK if succeed, negative error code otherwise.
116 *
[59fa7ab]117 */
118int ieee80211_scan_impl(ieee80211_dev_t *ieee80211_dev)
119{
[053fc2b]120 fibril_mutex_lock(&ieee80211_dev->scan_mutex);
[1dcc0b9]121
[8a64320e]122 if (ieee80211_get_auth_phase(ieee80211_dev) ==
123 IEEE80211_AUTH_DISCONNECTED) {
[053fc2b]124 fibril_mutex_lock(&ieee80211_dev->ap_list.results_mutex);
[8a64320e]125
[053fc2b]126 /* Remove old entries we don't receive beacons from. */
[8a64320e]127 ieee80211_scan_result_list_t *result_list =
128 &ieee80211_dev->ap_list;
129
[053fc2b]130 list_foreach_safe(result_list->list, cur_link, next_link) {
[8a64320e]131 ieee80211_scan_result_link_t *cur_result =
132 list_get_instance(cur_link,
133 ieee80211_scan_result_link_t, link);
134
135 if ((time(NULL) - cur_result->last_beacon) >
136 MAX_KEEP_SCAN_SPAN_SEC)
137 ieee80211_scan_result_list_remove(result_list,
138 cur_result);
[1dcc0b9]139 }
[8a64320e]140
[053fc2b]141 fibril_mutex_unlock(&ieee80211_dev->ap_list.results_mutex);
[8a64320e]142
[053fc2b]143 uint16_t orig_freq = ieee80211_dev->current_freq;
[8a64320e]144
145 for (uint16_t freq = IEEE80211_FIRST_FREQ;
146 freq <= IEEE80211_MAX_FREQ; freq += IEEE80211_CHANNEL_GAP) {
147 if (ieee80211_pending_connect_request(ieee80211_dev))
[053fc2b]148 break;
149
150 ieee80211_dev->ops->set_freq(ieee80211_dev, freq);
151 ieee80211_probe_request(ieee80211_dev, NULL);
[8a64320e]152
[053fc2b]153 /* Wait for probe responses. */
154 async_usleep(SCAN_CHANNEL_WAIT_USEC);
155 }
[8a64320e]156
[053fc2b]157 ieee80211_dev->ops->set_freq(ieee80211_dev, orig_freq);
[1dcc0b9]158 }
159
[053fc2b]160 fibril_mutex_unlock(&ieee80211_dev->scan_mutex);
[1dcc0b9]161
162 return EOK;
163}
164
[8a64320e]165/** Pseudorandom function used for IEEE 802.11 pairwise key computation.
166 *
167 * Using SHA1 hash algorithm.
168 *
169 * @param key Key with PBKDF2 encrypted passphrase.
170 * @param data Concatenated sequence of both MAC
171 * addresses and nonces.
172 * @param hash Output parameter for result hash.
[a931b7b]173 * @param output_size Length of output sequence to be generated.
[8a64320e]174 *
175 * @return EINVAL when key or data not specified,
176 * ENOMEM when pointer for output hash result
177 * is not allocated, otherwise EOK.
178 *
[1dcc0b9]179 */
[8a64320e]180int ieee80211_prf(uint8_t *key, uint8_t *data, uint8_t *hash,
181 size_t output_size)
[1dcc0b9]182{
[8a64320e]183 if ((!key) || (!data))
[1dcc0b9]184 return EINVAL;
185
[8a64320e]186 if (!hash)
[1dcc0b9]187 return ENOMEM;
188
[a931b7b]189 size_t iters = ((output_size * 8) + 159) / 160;
[1dcc0b9]190
191 const char *a = "Pairwise key expansion";
[8a64320e]192 uint8_t result[HASH_SHA1 * iters];
[a931b7b]193 uint8_t temp[HASH_SHA1];
[8a64320e]194
[1dcc0b9]195 size_t data_size = PRF_CRYPT_DATA_LENGTH + str_size(a) + 2;
196 uint8_t work_arr[data_size];
197 memset(work_arr, 0, data_size);
198
199 memcpy(work_arr, a, str_size(a));
200 memcpy(work_arr + str_size(a) + 1, data, PRF_CRYPT_DATA_LENGTH);
[8a64320e]201
202 for (uint8_t i = 0; i < iters; i++) {
[1dcc0b9]203 memcpy(work_arr + data_size - 1, &i, 1);
[8a64320e]204 hmac(key, PBKDF2_KEY_LENGTH, work_arr, data_size, temp,
205 HASH_SHA1);
[a931b7b]206 memcpy(result + i*HASH_SHA1, temp, HASH_SHA1);
[1dcc0b9]207 }
208
[a931b7b]209 memcpy(hash, result, output_size);
[1dcc0b9]210
211 return EOK;
212}
213
[a931b7b]214int ieee80211_rc4_key_unwrap(uint8_t *key, uint8_t *data, size_t data_size,
[8a64320e]215 uint8_t *output)
[a931b7b]216{
217 return rc4(key, 32, data, data_size, 256, output);
218}
219
[1dcc0b9]220int ieee80211_aes_key_unwrap(uint8_t *kek, uint8_t *data, size_t data_size,
[8a64320e]221 uint8_t *output)
[1dcc0b9]222{
[8a64320e]223 if ((!kek) || (!data))
[1dcc0b9]224 return EINVAL;
225
[8a64320e]226 if (!output)
[1dcc0b9]227 return ENOMEM;
[8a64320e]228
229 uint32_t n = data_size / 8 - 1;
230 uint8_t work_data[n * 8];
[1dcc0b9]231 uint8_t work_input[AES_CIPHER_LENGTH];
232 uint8_t work_output[AES_CIPHER_LENGTH];
233 uint8_t *work_block;
234 uint8_t a[8];
[8a64320e]235
[1dcc0b9]236 memcpy(a, data, 8);
[8a64320e]237
238 uint64_t mask = 0xff;
[1dcc0b9]239 uint8_t shift, shb;
240
[8a64320e]241 memcpy(work_data, data + 8, n * 8);
242 for (int j = 5; j >= 0; j--) {
243 for (int i = n; i > 0; i--) {
244 for (size_t k = 0; k < 8; k++) {
245 shift = 56 - 8 * k;
246 shb = ((n * j + i) & (mask << shift)) >> shift;
[1dcc0b9]247 a[k] ^= shb;
248 }
[8a64320e]249
250 work_block = work_data + (i - 1) * 8;
[1dcc0b9]251 memcpy(work_input, a, 8);
252 memcpy(work_input + 8, work_block, 8);
253 aes_decrypt(kek, work_input, work_output);
254 memcpy(a, work_output, 8);
[8a64320e]255 memcpy(work_data + (i - 1) * 8, work_output + 8, 8);
[1dcc0b9]256 }
257 }
258
259 size_t it;
[8a64320e]260 for (it = 0; it < 8; it++) {
261 if (a[it] != 0xa6)
[1dcc0b9]262 break;
263 }
264
[8a64320e]265 if (it == 8) {
266 memcpy(output, work_data, n * 8);
[1dcc0b9]267 return EOK;
268 }
[8a64320e]269
270 return EINVAL;
[1dcc0b9]271}
272
[8a64320e]273static void ieee80211_michael_mic_block(uint32_t *l, uint32_t *r,
274 uint32_t value)
[cc575ef9]275{
276 *l ^= value;
277 *r ^= rotl_uint32(*l, 17);
278 *l += *r;
[8a64320e]279 *r ^= ((*l & 0x00ff00ff) << 8) | ((*l & 0xff00ff00) >> 8);
[cc575ef9]280 *l += *r;
281 *r ^= rotl_uint32(*l, 3);
282 *l += *r;
283 *r ^= rotr_uint32(*l, 2);
284 *l += *r;
285}
286
[8a64320e]287int ieee80211_michael_mic(uint8_t *key, uint8_t *buffer, size_t size,
288 uint8_t *mic)
[cc575ef9]289{
[8a64320e]290 if ((!key) || (!buffer))
[cc575ef9]291 return EINVAL;
292
[8a64320e]293 if (!mic)
[cc575ef9]294 return ENOMEM;
295
296 uint32_t l = uint32le_from_seq(key);
297 uint32_t r = uint32le_from_seq(key + 4);
298
299 ieee80211_data_header_t *data_header =
[8a64320e]300 (ieee80211_data_header_t *) buffer;
[cc575ef9]301
[8a64320e]302 uint8_t *data = buffer + sizeof(ieee80211_data_header_t) +
303 IEEE80211_TKIP_HEADER_LENGTH;
[cc575ef9]304 size_t data_size = size - sizeof(ieee80211_data_header_t) -
[8a64320e]305 IEEE80211_TKIP_HEADER_LENGTH;
[cc575ef9]306
307 /* Process header. */
[8a64320e]308 uint8_t *src_addr =
309 ieee80211_is_fromds_frame(data_header->frame_ctrl) ?
310 data_header->address3 : data_header->address2;
311 uint8_t *dest_addr =
312 ieee80211_is_tods_frame(data_header->frame_ctrl) ?
313 data_header->address3 : data_header->address1;
[cc575ef9]314
315 ieee80211_michael_mic_block(&l, &r, uint32le_from_seq(dest_addr));
[8a64320e]316 ieee80211_michael_mic_block(&l, &r,
317 uint16le_from_seq(dest_addr + 4) |
318 (uint16le_from_seq(src_addr) << 16));
[cc575ef9]319 ieee80211_michael_mic_block(&l, &r, uint32le_from_seq(src_addr + 2));
320 ieee80211_michael_mic_block(&l, &r, 0);
321
322 /* Process data. */
323 size_t blocks = data_size / 4;
324 size_t pad = data_size % 4;
325
[8a64320e]326 for (size_t k = 0; k < blocks; k++) {
327 ieee80211_michael_mic_block(&l, &r,
328 uint32le_from_seq(&data[k * 4]));
[cc575ef9]329 }
330
331 /* Add padding. */
[8a64320e]332 uint32_t value = 0x5a;
333 for (size_t i = pad; i > 0; i--) {
[cc575ef9]334 value <<= 8;
[8a64320e]335 value |= data[blocks * 4 + (i - 1)];
[cc575ef9]336 }
337
338 ieee80211_michael_mic_block(&l, &r, value);
339 ieee80211_michael_mic_block(&l, &r, 0);
340
341 l = host2uint32_t_le(l);
342 r = host2uint32_t_le(r);
343
344 memcpy(mic, &l, 4);
345 memcpy(mic + 4, &r, 4);
346
347 return EOK;
348}
349
350uint16_t uint16le_from_seq(void *seq)
351{
352 uint16_t *u16 = (uint16_t *) seq;
[8a64320e]353 return uint16_t_le2host(*u16);
[cc575ef9]354}
355
356uint32_t uint32le_from_seq(void *seq)
357{
358 uint32_t *u32 = (uint32_t *) seq;
[8a64320e]359 return uint32_t_le2host(*u32);
[cc575ef9]360}
361
362uint16_t uint16be_from_seq(void *seq)
363{
364 uint16_t *u16 = (uint16_t *) seq;
[8a64320e]365 return uint16_t_be2host(*u16);
[cc575ef9]366}
367
368uint32_t uint32be_from_seq(void *seq)
369{
370 uint32_t *u32 = (uint32_t *) seq;
[8a64320e]371 return uint32_t_be2host(*u32);
[cc575ef9]372}
373
[1dcc0b9]374int rnd_sequence(uint8_t *sequence, size_t length)
375{
[8a64320e]376 if (!sequence)
[1dcc0b9]377 return ENOMEM;
378
[8a64320e]379 for (size_t i = 0; i < length; i++)
[1dcc0b9]380 sequence[i] = (uint8_t) rand();
381
[59fa7ab]382 return EOK;
383}
384
[1dcc0b9]385uint8_t *min_sequence(uint8_t *seq1, uint8_t *seq2, size_t size)
386{
[8a64320e]387 if ((!seq1) || (!seq2))
[1dcc0b9]388 return NULL;
389
[8a64320e]390 for (size_t i = 0; i < size; i++) {
391 if (seq1[i] < seq2[i])
[1dcc0b9]392 return seq1;
[8a64320e]393 else if (seq1[i] > seq2[i])
[1dcc0b9]394 return seq2;
395 }
396
397 return seq1;
398}
399
400uint8_t *max_sequence(uint8_t *seq1, uint8_t *seq2, size_t size)
401{
402 uint8_t *min = min_sequence(seq1, seq2, size);
[8a64320e]403 if (min == seq1)
[1dcc0b9]404 return seq2;
[8a64320e]405
406 return seq1;
[1dcc0b9]407}
408
[59fa7ab]409/** @}
[8a64320e]410 */
Note: See TracBrowser for help on using the repository browser.