HelenOS sources
This source file includes following definitions.
- ieee80211_start_impl
- ieee80211_tx_handler_impl
- ieee80211_set_freq_impl
- ieee80211_bssid_change_impl
- ieee80211_key_config_impl
- ieee80211_scan_impl
- ieee80211_prf
- ieee80211_rc4_key_unwrap
- ieee80211_aes_key_unwrap
- ieee80211_michael_mic_block
- ieee80211_michael_mic
- uint16le_from_seq
- uint32le_from_seq
- uint16be_from_seq
- uint32be_from_seq
- rnd_sequence
- min_sequence
- max_sequence
#include <stdio.h>
#include <crypto.h>
#include <stdlib.h>
#include <errno.h>
#include <ieee80211_impl.h>
errno_t ieee80211_start_impl(ieee80211_dev_t *ieee80211_dev)
{
return EOK;
}
errno_t ieee80211_tx_handler_impl(ieee80211_dev_t *ieee80211_dev, void *buffer,
size_t buffer_size)
{
return EOK;
}
errno_t ieee80211_set_freq_impl(ieee80211_dev_t *ieee80211_dev, uint16_t freq)
{
return EOK;
}
errno_t ieee80211_bssid_change_impl(ieee80211_dev_t *ieee80211_dev,
bool connected)
{
return EOK;
}
errno_t ieee80211_key_config_impl(ieee80211_dev_t *ieee80211_dev,
ieee80211_key_config_t *key_conf, bool insert)
{
return EOK;
}
errno_t ieee80211_scan_impl(ieee80211_dev_t *ieee80211_dev)
{
fibril_mutex_lock(&ieee80211_dev->scan_mutex);
if (ieee80211_get_auth_phase(ieee80211_dev) ==
IEEE80211_AUTH_DISCONNECTED) {
fibril_mutex_lock(&ieee80211_dev->ap_list.results_mutex);
ieee80211_scan_result_list_t *result_list =
&ieee80211_dev->ap_list;
list_foreach_safe(result_list->list, cur_link, next_link) {
ieee80211_scan_result_link_t *cur_result =
list_get_instance(cur_link,
ieee80211_scan_result_link_t, link);
if ((time(NULL) - cur_result->last_beacon) >
MAX_KEEP_SCAN_SPAN_SEC)
ieee80211_scan_result_list_remove(result_list,
cur_result);
}
fibril_mutex_unlock(&ieee80211_dev->ap_list.results_mutex);
uint16_t orig_freq = ieee80211_dev->current_freq;
for (uint16_t freq = IEEE80211_FIRST_FREQ;
freq <= IEEE80211_MAX_FREQ; freq += IEEE80211_CHANNEL_GAP) {
if (ieee80211_pending_connect_request(ieee80211_dev))
break;
ieee80211_dev->ops->set_freq(ieee80211_dev, freq);
ieee80211_probe_request(ieee80211_dev, NULL);
fibril_usleep(SCAN_CHANNEL_WAIT_USEC);
}
ieee80211_dev->ops->set_freq(ieee80211_dev, orig_freq);
}
fibril_mutex_unlock(&ieee80211_dev->scan_mutex);
return EOK;
}
errno_t ieee80211_prf(uint8_t *key, uint8_t *data, uint8_t *hash,
size_t output_size)
{
if ((!key) || (!data))
return EINVAL;
if (!hash)
return ENOMEM;
size_t iters = ((output_size * 8) + 159) / 160;
const char *a = "Pairwise key expansion";
uint8_t result[HASH_SHA1 * iters];
uint8_t temp[HASH_SHA1];
size_t data_size = PRF_CRYPT_DATA_LENGTH + str_size(a) + 2;
uint8_t work_arr[data_size];
memset(work_arr, 0, data_size);
memcpy(work_arr, a, str_size(a));
memcpy(work_arr + str_size(a) + 1, data, PRF_CRYPT_DATA_LENGTH);
for (uint8_t i = 0; i < iters; i++) {
memcpy(work_arr + data_size - 1, &i, 1);
hmac(key, PBKDF2_KEY_LENGTH, work_arr, data_size, temp,
HASH_SHA1);
memcpy(result + i * HASH_SHA1, temp, HASH_SHA1);
}
memcpy(hash, result, output_size);
return EOK;
}
errno_t ieee80211_rc4_key_unwrap(uint8_t *key, uint8_t *data, size_t data_size,
uint8_t *output)
{
return rc4(key, 32, data, data_size, 256, output);
}
errno_t ieee80211_aes_key_unwrap(uint8_t *kek, uint8_t *data, size_t data_size,
uint8_t *output)
{
if ((!kek) || (!data))
return EINVAL;
if (!output)
return ENOMEM;
uint32_t n = data_size / 8 - 1;
uint8_t work_data[n * 8];
uint8_t work_input[AES_CIPHER_LENGTH];
uint8_t work_output[AES_CIPHER_LENGTH];
uint8_t *work_block;
uint8_t a[8];
memcpy(a, data, 8);
uint64_t mask = 0xff;
uint8_t shift, shb;
memcpy(work_data, data + 8, n * 8);
for (int j = 5; j >= 0; j--) {
for (int i = n; i > 0; i--) {
for (size_t k = 0; k < 8; k++) {
shift = 56 - 8 * k;
shb = ((n * j + i) & (mask << shift)) >> shift;
a[k] ^= shb;
}
work_block = work_data + (i - 1) * 8;
memcpy(work_input, a, 8);
memcpy(work_input + 8, work_block, 8);
aes_decrypt(kek, work_input, work_output);
memcpy(a, work_output, 8);
memcpy(work_data + (i - 1) * 8, work_output + 8, 8);
}
}
size_t it;
for (it = 0; it < 8; it++) {
if (a[it] != 0xa6)
break;
}
if (it == 8) {
memcpy(output, work_data, n * 8);
return EOK;
}
return EINVAL;
}
static void ieee80211_michael_mic_block(uint32_t *l, uint32_t *r,
uint32_t value)
{
*l ^= value;
*r ^= rotl_uint32(*l, 17);
*l += *r;
*r ^= ((*l & 0x00ff00ff) << 8) | ((*l & 0xff00ff00) >> 8);
*l += *r;
*r ^= rotl_uint32(*l, 3);
*l += *r;
*r ^= rotr_uint32(*l, 2);
*l += *r;
}
errno_t ieee80211_michael_mic(uint8_t *key, uint8_t *buffer, size_t size,
uint8_t *mic)
{
if ((!key) || (!buffer))
return EINVAL;
if (!mic)
return ENOMEM;
uint32_t l = uint32le_from_seq(key);
uint32_t r = uint32le_from_seq(key + 4);
ieee80211_data_header_t *data_header =
(ieee80211_data_header_t *) buffer;
uint8_t *data = buffer + sizeof(ieee80211_data_header_t) +
IEEE80211_TKIP_HEADER_LENGTH;
size_t data_size = size - sizeof(ieee80211_data_header_t) -
IEEE80211_TKIP_HEADER_LENGTH;
uint8_t *src_addr =
ieee80211_is_fromds_frame(data_header->frame_ctrl) ?
data_header->address3 : data_header->address2;
uint8_t *dest_addr =
ieee80211_is_tods_frame(data_header->frame_ctrl) ?
data_header->address3 : data_header->address1;
ieee80211_michael_mic_block(&l, &r, uint32le_from_seq(dest_addr));
ieee80211_michael_mic_block(&l, &r,
uint16le_from_seq(dest_addr + 4) |
(uint16le_from_seq(src_addr) << 16));
ieee80211_michael_mic_block(&l, &r, uint32le_from_seq(src_addr + 2));
ieee80211_michael_mic_block(&l, &r, 0);
size_t blocks = data_size / 4;
size_t pad = data_size % 4;
for (size_t k = 0; k < blocks; k++) {
ieee80211_michael_mic_block(&l, &r,
uint32le_from_seq(&data[k * 4]));
}
uint32_t value = 0x5a;
for (size_t i = pad; i > 0; i--) {
value <<= 8;
value |= data[blocks * 4 + (i - 1)];
}
ieee80211_michael_mic_block(&l, &r, value);
ieee80211_michael_mic_block(&l, &r, 0);
l = host2uint32_t_le(l);
r = host2uint32_t_le(r);
memcpy(mic, &l, 4);
memcpy(mic + 4, &r, 4);
return EOK;
}
uint16_t uint16le_from_seq(void *seq)
{
uint16_t *u16 = (uint16_t *) seq;
return uint16_t_le2host(*u16);
}
uint32_t uint32le_from_seq(void *seq)
{
uint32_t *u32 = (uint32_t *) seq;
return uint32_t_le2host(*u32);
}
uint16_t uint16be_from_seq(void *seq)
{
uint16_t *u16 = (uint16_t *) seq;
return uint16_t_be2host(*u16);
}
uint32_t uint32be_from_seq(void *seq)
{
uint32_t *u32 = (uint32_t *) seq;
return uint32_t_be2host(*u32);
}
errno_t rnd_sequence(uint8_t *sequence, size_t length)
{
if (!sequence)
return ENOMEM;
for (size_t i = 0; i < length; i++)
sequence[i] = (uint8_t) rand();
return EOK;
}
uint8_t *min_sequence(uint8_t *seq1, uint8_t *seq2, size_t size)
{
if ((!seq1) || (!seq2))
return NULL;
for (size_t i = 0; i < size; i++) {
if (seq1[i] < seq2[i])
return seq1;
else if (seq1[i] > seq2[i])
return seq2;
}
return seq1;
}
uint8_t *max_sequence(uint8_t *seq1, uint8_t *seq2, size_t size)
{
uint8_t *min = min_sequence(seq1, seq2, size);
if (min == seq1)
return seq2;
return seq1;
}
HelenOS homepage, sources at GitHub