[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
|
---|
| 34 | *
|
---|
| 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 |
|
---|
| 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 |
|
---|
[1dcc0b9] | 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 | {
|
---|
| 94 | return EOK;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /**
|
---|
| 98 | * Default implementation of IEEE802.11 key config function.
|
---|
| 99 | *
|
---|
| 100 | * @param ieee80211_dev Structure of IEEE802.11 device.
|
---|
| 101 | *
|
---|
| 102 | * @return EOK.
|
---|
| 103 | */
|
---|
| 104 | int ieee80211_key_config_impl(ieee80211_dev_t *ieee80211_dev,
|
---|
| 105 | ieee80211_key_config_t *key_conf, bool insert)
|
---|
| 106 | {
|
---|
| 107 | return EOK;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[59fa7ab] | 110 | /**
|
---|
| 111 | * Default implementation of IEEE802.11 scan function.
|
---|
| 112 | *
|
---|
| 113 | * @param ieee80211_dev Structure of IEEE802.11 device.
|
---|
[1dcc0b9] | 114 | * @param clear Whether to clear current scan results.
|
---|
[59fa7ab] | 115 | *
|
---|
| 116 | * @return EOK if succeed, negative error code otherwise.
|
---|
| 117 | */
|
---|
| 118 | int ieee80211_scan_impl(ieee80211_dev_t *ieee80211_dev)
|
---|
| 119 | {
|
---|
[1dcc0b9] | 120 | if(ieee80211_is_connected(ieee80211_dev))
|
---|
| 121 | return EOK;
|
---|
| 122 |
|
---|
| 123 | fibril_mutex_lock(&ieee80211_dev->ap_list.scan_mutex);
|
---|
| 124 |
|
---|
| 125 | /* Remove old entries we don't receive beacons from. */
|
---|
| 126 | ieee80211_scan_result_list_t *result_list =
|
---|
| 127 | &ieee80211_dev->ap_list;
|
---|
| 128 | list_foreach_safe(result_list->list, cur_link, next_link) {
|
---|
| 129 | ieee80211_scan_result_link_t *cur_result =
|
---|
| 130 | list_get_instance(cur_link,
|
---|
| 131 | ieee80211_scan_result_link_t,
|
---|
| 132 | link);
|
---|
| 133 | if((time(NULL) - cur_result->last_beacon) >
|
---|
| 134 | MAX_KEEP_SCAN_SPAN_SEC) {
|
---|
| 135 | ieee80211_scan_result_list_remove(result_list,
|
---|
| 136 | cur_result);
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | fibril_mutex_unlock(&ieee80211_dev->ap_list.scan_mutex);
|
---|
| 141 |
|
---|
[59fa7ab] | 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 | ieee80211_dev->ops->set_freq(ieee80211_dev, freq);
|
---|
[1dcc0b9] | 148 | ieee80211_probe_request(ieee80211_dev, NULL);
|
---|
[59fa7ab] | 149 |
|
---|
| 150 | /* Wait for probe responses. */
|
---|
| 151 | usleep(100000);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | ieee80211_dev->ops->set_freq(ieee80211_dev, orig_freq);
|
---|
| 155 |
|
---|
[1dcc0b9] | 156 | fibril_mutex_lock(&ieee80211_dev->ap_list.scan_mutex);
|
---|
| 157 | time(&ieee80211_dev->ap_list.last_scan);
|
---|
| 158 | fibril_mutex_unlock(&ieee80211_dev->ap_list.scan_mutex);
|
---|
| 159 |
|
---|
| 160 | return EOK;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /**
|
---|
| 164 | * Pseudorandom function used for IEEE 802.11 pairwise key computation.
|
---|
| 165 | *
|
---|
| 166 | * @param key Key with PBKDF2 encrypted passphrase.
|
---|
| 167 | * @param data Concatenated sequence of both mac addresses and nonces.
|
---|
| 168 | * @param hash Output parameter for result hash (48 byte value).
|
---|
| 169 | * @param hash_sel Hash function selector.
|
---|
| 170 | *
|
---|
| 171 | * @return EINVAL when key or data not specified, ENOMEM when pointer for
|
---|
| 172 | * output hash result is not allocated, otherwise EOK.
|
---|
| 173 | */
|
---|
| 174 | int ieee80211_prf(uint8_t *key, uint8_t *data, uint8_t *hash,
|
---|
| 175 | hash_func_t hash_sel)
|
---|
| 176 | {
|
---|
| 177 | if(!key || !data)
|
---|
| 178 | return EINVAL;
|
---|
| 179 |
|
---|
| 180 | if(!hash)
|
---|
| 181 | return ENOMEM;
|
---|
| 182 |
|
---|
[d7dadcb4] | 183 | size_t result_length = (hash_sel == HASH_MD5) ?
|
---|
| 184 | IEEE80211_PTK_TKIP_LENGTH : IEEE80211_PTK_CCMP_LENGTH;
|
---|
[1dcc0b9] | 185 | size_t iters = ((result_length * 8) + 159) / 160;
|
---|
| 186 |
|
---|
| 187 | const char *a = "Pairwise key expansion";
|
---|
[d7dadcb4] | 188 | uint8_t result[hash_sel*iters];
|
---|
| 189 | uint8_t temp[hash_sel];
|
---|
[1dcc0b9] | 190 | size_t data_size = PRF_CRYPT_DATA_LENGTH + str_size(a) + 2;
|
---|
| 191 | uint8_t work_arr[data_size];
|
---|
| 192 | memset(work_arr, 0, data_size);
|
---|
| 193 |
|
---|
| 194 | memcpy(work_arr, a, str_size(a));
|
---|
| 195 | memcpy(work_arr + str_size(a) + 1, data, PRF_CRYPT_DATA_LENGTH);
|
---|
| 196 |
|
---|
| 197 | for(uint8_t i = 0; i < iters; i++) {
|
---|
| 198 | memcpy(work_arr + data_size - 1, &i, 1);
|
---|
| 199 | hmac(key, PBKDF2_KEY_LENGTH, work_arr, data_size, temp,
|
---|
| 200 | hash_sel);
|
---|
[d7dadcb4] | 201 | memcpy(result + i*hash_sel, temp, hash_sel);
|
---|
[1dcc0b9] | 202 | }
|
---|
| 203 |
|
---|
| 204 | memcpy(hash, result, result_length);
|
---|
| 205 |
|
---|
| 206 | return EOK;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | int ieee80211_aes_key_unwrap(uint8_t *kek, uint8_t *data, size_t data_size,
|
---|
| 210 | uint8_t *output)
|
---|
| 211 | {
|
---|
| 212 | if(!kek || !data)
|
---|
| 213 | return EINVAL;
|
---|
| 214 |
|
---|
| 215 | if(!output)
|
---|
| 216 | return ENOMEM;
|
---|
| 217 |
|
---|
| 218 | uint32_t n = data_size/8 - 1;
|
---|
| 219 | uint8_t work_data[n*8];
|
---|
| 220 | uint8_t work_input[AES_CIPHER_LENGTH];
|
---|
| 221 | uint8_t work_output[AES_CIPHER_LENGTH];
|
---|
| 222 | uint8_t *work_block;
|
---|
| 223 | uint8_t a[8];
|
---|
| 224 | memcpy(a, data, 8);
|
---|
| 225 | uint64_t mask = 0xFF;
|
---|
| 226 | uint8_t shift, shb;
|
---|
| 227 |
|
---|
| 228 | memcpy(work_data, data + 8, n*8);
|
---|
| 229 | for(int j = 5; j >=0; j--) {
|
---|
| 230 | for(int i = n; i > 0; i--) {
|
---|
| 231 | for(size_t k = 0; k < 8; k++) {
|
---|
| 232 | shift = 56 - 8*k;
|
---|
| 233 | shb = ((n*j+i) & (mask << shift)) >> shift;
|
---|
| 234 | a[k] ^= shb;
|
---|
| 235 | }
|
---|
| 236 | work_block = work_data + (i-1)*8;
|
---|
| 237 | memcpy(work_input, a, 8);
|
---|
| 238 | memcpy(work_input + 8, work_block, 8);
|
---|
| 239 | aes_decrypt(kek, work_input, work_output);
|
---|
| 240 | memcpy(a, work_output, 8);
|
---|
| 241 | memcpy(work_data + (i-1)*8, work_output + 8, 8);
|
---|
| 242 | }
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | size_t it;
|
---|
| 246 | for(it = 0; it < 8; it++) {
|
---|
| 247 | if(a[it] != 0xA6)
|
---|
| 248 | break;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | if(it == 8) {
|
---|
| 252 | memcpy(output, work_data, n*8);
|
---|
| 253 | return EOK;
|
---|
| 254 | } else {
|
---|
| 255 | return EINVAL;
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | int rnd_sequence(uint8_t *sequence, size_t length)
|
---|
| 260 | {
|
---|
| 261 | if(!sequence)
|
---|
| 262 | return ENOMEM;
|
---|
| 263 |
|
---|
| 264 | for(size_t i = 0; i < length; i++) {
|
---|
| 265 | sequence[i] = (uint8_t) rand();
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[59fa7ab] | 268 | return EOK;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[1dcc0b9] | 271 | uint8_t *min_sequence(uint8_t *seq1, uint8_t *seq2, size_t size)
|
---|
| 272 | {
|
---|
| 273 | if(!seq1 || !seq2)
|
---|
| 274 | return NULL;
|
---|
| 275 |
|
---|
| 276 | for(size_t i = 0; i < size; i++) {
|
---|
| 277 | if(seq1[i] < seq2[i]) {
|
---|
| 278 | return seq1;
|
---|
| 279 | } else if(seq1[i] > seq2[i]) {
|
---|
| 280 | return seq2;
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | return seq1;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | uint8_t *max_sequence(uint8_t *seq1, uint8_t *seq2, size_t size)
|
---|
| 288 | {
|
---|
| 289 | uint8_t *min = min_sequence(seq1, seq2, size);
|
---|
| 290 | if(min == seq1) {
|
---|
| 291 | return seq2;
|
---|
| 292 | } else {
|
---|
| 293 | return seq1;
|
---|
| 294 | }
|
---|
| 295 | }
|
---|
| 296 |
|
---|
[59fa7ab] | 297 | /** @}
|
---|
| 298 | */ |
---|