void wpa_pmk_to_ptk(u8 * pmk, u8 * addr1, u8 * addr2, u8 * nonce1, u8 * nonce2, u8 * ptk, size_t ptk_len) { u8 data[2 * ETH_ALEN + 2 * 32]; memset(&data, 0, sizeof(data)); /* PTK = PRF-X(PMK, "Pairwise key expansion", * Min(AA, SA) || Max(AA, SA) || * Min(ANonce, SNonce) || Max(ANonce, SNonce)) */ if (memcmp(addr1, addr2, ETH_ALEN) < 0) { memcpy(data, addr1, ETH_ALEN); memcpy(data + ETH_ALEN, addr2, ETH_ALEN); } else { memcpy(data, addr2, ETH_ALEN); memcpy(data + ETH_ALEN, addr1, ETH_ALEN); } if (memcmp(nonce1, nonce2, 32) < 0) { memcpy(data + 2 * ETH_ALEN, nonce1, 32); memcpy(data + 2 * ETH_ALEN + 32, nonce2, 32); } else { memcpy(data + 2 * ETH_ALEN, nonce2, 32); memcpy(data + 2 * ETH_ALEN + 32, nonce1, 32); } sha1_prf(pmk, 32, "Pairwise key expansion", data, sizeof(data), ptk, ptk_len); }
static void * wpa_driver_test_init(void *ctx, const char *ifname) { struct wpa_driver_test_data *drv; drv = malloc(sizeof(*drv)); if (drv == NULL) return NULL; memset(drv, 0, sizeof(*drv)); drv->ctx = ctx; drv->test_socket = -1; /* Set dummy BSSID and SSID for testing. */ drv->bssid[0] = 0x02; drv->bssid[1] = 0x00; drv->bssid[2] = 0x00; drv->bssid[3] = 0x00; drv->bssid[4] = 0x00; drv->bssid[5] = 0x01; memcpy(drv->ssid, "test", 5); drv->ssid_len = 4; /* Generate a MAC address to help testing with multiple STAs */ drv->own_addr[0] = 0x02; /* locally administered */ sha1_prf(ifname, strlen(ifname), "wpa_supplicant test mac addr generation", NULL, 0, drv->own_addr + 1, ETH_ALEN - 1); return drv; }
/** * wpa_pmk_to_ptk - Calculate PTK from PMK, addresses, and nonces * @pmk: Pairwise master key * @pmk_len: Length of PMK * @label: Label to use in derivation * @addr1: AA or SA * @addr2: SA or AA * @nonce1: ANonce or SNonce * @nonce2: SNonce or ANonce * @ptk: Buffer for pairwise transient key * @ptk_len: Length of PTK * * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy * PTK = PRF-X(PMK, "Pairwise key expansion", * Min(AA, SA) || Max(AA, SA) || * Min(ANonce, SNonce) || Max(ANonce, SNonce)) * * STK = PRF-X(SMK, "Peer key expansion", * Min(MAC_I, MAC_P) || Max(MAC_I, MAC_P) || * Min(INonce, PNonce) || Max(INonce, PNonce)) */ void wpa_pmk_to_ptk(const u8 *pmk, size_t pmk_len, const u8 *addr1, const u8 *addr2, const u8 *nonce1, const u8 *nonce2, u8 *ptk, size_t ptk_len) { u8 data[2 * ETH_ALEN + 2 * 32]; if (os_memcmp(addr1, addr2, ETH_ALEN) < 0) { os_memcpy(data, addr1, ETH_ALEN); os_memcpy(data + ETH_ALEN, addr2, ETH_ALEN); } else { os_memcpy(data, addr2, ETH_ALEN); os_memcpy(data + ETH_ALEN, addr1, ETH_ALEN); } if (os_memcmp(nonce1, nonce2, 32) < 0) { os_memcpy(data + 2 * ETH_ALEN, nonce1, 32); os_memcpy(data + 2 * ETH_ALEN + 32, nonce2, 32); } else { os_memcpy(data + 2 * ETH_ALEN, nonce2, 32); os_memcpy(data + 2 * ETH_ALEN + 32, nonce1, 32); } sha1_prf(pmk, pmk_len, "Pairwise key expansion", data, sizeof(data), ptk, ptk_len); wpa_hexdump_key(MSG_DEBUG, "WPA: PMK", pmk, pmk_len); wpa_hexdump_key(MSG_DEBUG, "WPA: PTK", ptk, ptk_len); }
/** * wpa_pmk_to_ptk - Calculate PTK from PMK, addresses, and nonces * @pmk: Pairwise master key * @pmk_len: Length of PMK * @label: Label to use in derivation * @addr1: AA or SA * @addr2: SA or AA * @nonce1: ANonce or SNonce * @nonce2: SNonce or ANonce * @ptk: Buffer for pairwise transient key * @ptk_len: Length of PTK * * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy * PTK = PRF-X(PMK, "Pairwise key expansion", * Min(AA, SA) || Max(AA, SA) || * Min(ANonce, SNonce) || Max(ANonce, SNonce)) * * STK = PRF-X(SMK, "Peer key expansion", * Min(MAC_I, MAC_P) || Max(MAC_I, MAC_P) || * Min(INonce, PNonce) || Max(INonce, PNonce)) */ void wpa_pmk_to_ptk(const u8 *pmk, size_t pmk_len, const char *label, const u8 *addr1, const u8 *addr2, const u8 *nonce1, const u8 *nonce2, u8 *ptk, size_t ptk_len) { u8 data[2 * ETH_ALEN + 2 * WPA_NONCE_LEN]; if (os_memcmp(addr1, addr2, ETH_ALEN) < 0) { os_memcpy(data, addr1, ETH_ALEN); os_memcpy(data + ETH_ALEN, addr2, ETH_ALEN); } else { os_memcpy(data, addr2, ETH_ALEN); os_memcpy(data + ETH_ALEN, addr1, ETH_ALEN); } if (os_memcmp(nonce1, nonce2, WPA_NONCE_LEN) < 0) { os_memcpy(data + 2 * ETH_ALEN, nonce1, WPA_NONCE_LEN); os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce2, WPA_NONCE_LEN); } else { os_memcpy(data + 2 * ETH_ALEN, nonce2, WPA_NONCE_LEN); os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce1, WPA_NONCE_LEN); } sha1_prf(pmk, pmk_len, label, data, sizeof(data), ptk, ptk_len); wpa_printf(MSG_DEBUG, "WPA: PTK derivation - A1=" MACSTR " A2=" MACSTR, MAC2STR(addr1), MAC2STR(addr2)); wpa_hexdump_key(MSG_DEBUG, "WPA: PMK", pmk, pmk_len); wpa_hexdump_key(MSG_DEBUG, "WPA: PTK", ptk, ptk_len); }
/** * wpa_pmk_to_ptk - Calculate PTK from PMK, addresses, and nonces * @pmk: Pairwise master key * @pmk_len: Length of PMK * @label: Label to use in derivation * @addr1: AA or SA * @addr2: SA or AA * @nonce1: ANonce or SNonce * @nonce2: SNonce or ANonce * @ptk: Buffer for pairwise transient key * @akmp: Negotiated AKM * @cipher: Negotiated pairwise cipher * Returns: 0 on success, -1 on failure * * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy * PTK = PRF-X(PMK, "Pairwise key expansion", * Min(AA, SA) || Max(AA, SA) || * Min(ANonce, SNonce) || Max(ANonce, SNonce)) * * STK = PRF-X(SMK, "Peer key expansion", * Min(MAC_I, MAC_P) || Max(MAC_I, MAC_P) || * Min(INonce, PNonce) || Max(INonce, PNonce)) */ int wpa_pmk_to_ptk(const u8 *pmk, size_t pmk_len, const char *label, const u8 *addr1, const u8 *addr2, const u8 *nonce1, const u8 *nonce2, struct wpa_ptk *ptk, int akmp, int cipher) { u8 data[2 * ETH_ALEN + 2 * WPA_NONCE_LEN]; u8 tmp[WPA_KCK_MAX_LEN + WPA_KEK_MAX_LEN + WPA_TK_MAX_LEN]; size_t ptk_len; if (os_memcmp(addr1, addr2, ETH_ALEN) < 0) { os_memcpy(data, addr1, ETH_ALEN); os_memcpy(data + ETH_ALEN, addr2, ETH_ALEN); } else { os_memcpy(data, addr2, ETH_ALEN); os_memcpy(data + ETH_ALEN, addr1, ETH_ALEN); } if (os_memcmp(nonce1, nonce2, WPA_NONCE_LEN) < 0) { os_memcpy(data + 2 * ETH_ALEN, nonce1, WPA_NONCE_LEN); os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce2, WPA_NONCE_LEN); } else { os_memcpy(data + 2 * ETH_ALEN, nonce2, WPA_NONCE_LEN); os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce1, WPA_NONCE_LEN); } ptk->kck_len = wpa_kck_len(akmp); ptk->kek_len = wpa_kek_len(akmp); ptk->tk_len = wpa_cipher_key_len(cipher); ptk_len = ptk->kck_len + ptk->kek_len + ptk->tk_len; #ifdef CONFIG_IEEE80211W if (wpa_key_mgmt_sha256(akmp)) sha256_prf(pmk, pmk_len, label, data, sizeof(data), tmp, ptk_len); else #endif /* CONFIG_IEEE80211W */ sha1_prf(pmk, pmk_len, label, data, sizeof(data), tmp, ptk_len); wpa_printf(MSG_DEBUG, "WPA: PTK derivation - A1=" MACSTR " A2=" MACSTR, MAC2STR(addr1), MAC2STR(addr2)); wpa_hexdump(MSG_DEBUG, "WPA: Nonce1", nonce1, WPA_NONCE_LEN); wpa_hexdump(MSG_DEBUG, "WPA: Nonce2", nonce2, WPA_NONCE_LEN); wpa_hexdump_key(MSG_DEBUG, "WPA: PMK", pmk, pmk_len); wpa_hexdump_key(MSG_DEBUG, "WPA: PTK", tmp, ptk_len); os_memcpy(ptk->kck, tmp, ptk->kck_len); wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", ptk->kck, ptk->kck_len); os_memcpy(ptk->kek, tmp + ptk->kck_len, ptk->kek_len); wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len); os_memcpy(ptk->tk, tmp + ptk->kck_len + ptk->kek_len, ptk->tk_len); wpa_hexdump_key(MSG_DEBUG, "WPA: TK", ptk->tk, ptk->tk_len); os_memset(tmp, 0, sizeof(tmp)); return 0; }
/** * wpa_btk_to_ptk - Calculate PTK from BTK, address, and Replay Counter * @btk: Base Transient Key * @btk_len: Length of BTK * @bssid: BSSID / AP ID * @ptk: Buffer for Pairwise Transient Key * @ptk_len: Length of PTK * * PTK = PRF-512(BTK, "", RN || AP_ID) */ void wpa_btk_to_ptk(const u8 *btk, size_t btk_len, const u8 *bssid, u32 cckm_rn, u8 *ptk, size_t ptk_len) { u8 data[ETH_ALEN + 4]; WPA_PUT_LE32(data, cckm_rn); os_memcpy(data + 4, bssid, ETH_ALEN); sha1_prf(btk, btk_len, NULL, data, sizeof(data), ptk, ptk_len); wpa_hexdump_key(MSG_DEBUG, "WPA: BTK-PTK", ptk, ptk_len); }
void wpa_gmk_to_gtk(u8 *gmk, u8 *addr, u8 *gnonce, u8 *gtk, size_t gtk_len) { u8 data[ETH_ALEN + WPA_NONCE_LEN]; /* GTK = PRF-X(GMK, "Group key expansion", AA || GNonce) */ memcpy(data, addr, ETH_ALEN); memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN); sha1_prf(gmk, WPA_GMK_LEN, "Group key expansion", data, sizeof(data), gtk, gtk_len); //wpa_hexdump_key(MSG_DEBUG, "WPA: GMK", gmk, gmk_len); //wpa_hexdump_key(MSG_DEBUG, "WPA: GTK", gtk, gtk_len); }
/** * wpa_nsk_to_cgk - Calculate CCMP Keys from NSK, addresses, and nonces * @nsk: Network Session Key * @nsk_len: Length of NSK * @addr1: AA * @addr2: SA * @nonce1: SNonce * @nonce2: ANonce * @cgk: Buffer for CCKM Common Keys * @cgk_len: Length of CGK * */ void wpa_nsk_to_cgk(const u8 *nsk, size_t nsk_len, const u8 *addr1, const u8 *addr2, const u8 *nonce1, const u8 *nonce2, u8 *cgk, size_t cgk_len) { u8 data[2 * ETH_ALEN + 2 * WPA_NONCE_LEN]; const char *label = "Fast-Roam Generate Base Key"; os_memcpy(data, addr1, ETH_ALEN); os_memcpy(data + ETH_ALEN, addr2, ETH_ALEN); os_memcpy(data + 2 * ETH_ALEN, nonce1, WPA_NONCE_LEN); os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce2, WPA_NONCE_LEN); sha1_prf(nsk, nsk_len, label, data, sizeof(data), cgk, cgk_len); wpa_hexdump_key(MSG_DEBUG, "WPA: NSK", nsk, nsk_len); wpa_hexdump_key(MSG_DEBUG, "WPA: CGK", cgk, cgk_len); }
/** * wpa_pmk_to_ptk - Calculate PTK from PMK, addresses, and nonces * @pmk: Pairwise master key * @pmk_len: Length of PMK * @label: Label to use in derivation * @addr1: AA or SA * @addr2: SA or AA * @nonce1: ANonce or SNonce * @nonce2: SNonce or ANonce * @ptk: Buffer for pairwise transient key * @ptk_len: Length of PTK * @use_sha256: Whether to use SHA256-based KDF * * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy * PTK = PRF-X(PMK, "Pairwise key expansion", * Min(AA, SA) || Max(AA, SA) || * Min(ANonce, SNonce) || Max(ANonce, SNonce)) * * STK = PRF-X(SMK, "Peer key expansion", * Min(MAC_I, MAC_P) || Max(MAC_I, MAC_P) || * Min(INonce, PNonce) || Max(INonce, PNonce)) */ void wpa_pmk_to_ptk(const u8 *pmk, size_t pmk_len, const char *label, const u8 *addr1, const u8 *addr2, const u8 *nonce1, const u8 *nonce2, u8 *ptk, size_t ptk_len, int use_sha256) { u8 data[2 * ETH_ALEN + 2 * WPA_NONCE_LEN]; if (os_memcmp(addr1, addr2, ETH_ALEN) < 0) { os_memcpy(data, addr1, ETH_ALEN); os_memcpy(data + ETH_ALEN, addr2, ETH_ALEN); } else { os_memcpy(data, addr2, ETH_ALEN); os_memcpy(data + ETH_ALEN, addr1, ETH_ALEN); } if (os_memcmp(nonce1, nonce2, WPA_NONCE_LEN) < 0) { os_memcpy(data + 2 * ETH_ALEN, nonce1, WPA_NONCE_LEN); os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce2, WPA_NONCE_LEN); } else { os_memcpy(data + 2 * ETH_ALEN, nonce2, WPA_NONCE_LEN); os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce1, WPA_NONCE_LEN); } #ifdef CONFIG_IEEE80211W if (use_sha256) sha256_prf(pmk, pmk_len, label, data, sizeof(data), ptk, ptk_len); else #endif /* CONFIG_IEEE80211W */ sha1_prf(pmk, pmk_len, label, data, sizeof(data), ptk, ptk_len); wpa_printf(MSG_DEBUG, "WPA: PTK derivation - A1=" MACSTR " A2=" MACSTR, MAC2STR(addr1), MAC2STR(addr2)); wpa_hexdump(MSG_DEBUG, "WPA: Nonce1", nonce1, WPA_NONCE_LEN); wpa_hexdump(MSG_DEBUG, "WPA: Nonce2", nonce2, WPA_NONCE_LEN); wpa_hexdump_key(MSG_DEBUG, "WPA: PMK", pmk, pmk_len); wpa_hexdump_key(MSG_DEBUG, "WPA: PTK", ptk, ptk_len); }
void wpa_smk_m3(struct wpa_authenticator *wpa_auth, struct wpa_state_machine *sm, struct wpa_eapol_key *key) { struct wpa_eapol_ie_parse kde; struct wpa_stsl_search search; u8 smk[32], buf[ETH_ALEN + 8 + 2 * WPA_NONCE_LEN], *pos; if (wpa_parse_kde_ies((const u8 *) (key + 1), WPA_GET_BE16(key->key_data_length), &kde) < 0) { wpa_printf(MSG_INFO, "RSN: Failed to parse KDEs in SMK M3"); return; } if (kde.rsn_ie == NULL || kde.mac_addr == NULL || kde.mac_addr_len < ETH_ALEN || kde.nonce == NULL || kde.nonce_len < WPA_NONCE_LEN) { wpa_printf(MSG_INFO, "RSN: No RSN IE, MAC address KDE, or " "Nonce KDE in SMK M3"); return; } /* Peer = sm->addr; Initiator = kde.mac_addr; * Peer Nonce = key->key_nonce; Initiator Nonce = kde.nonce */ search.addr = kde.mac_addr; search.sm = NULL; if (wpa_auth_for_each_sta(wpa_auth, wpa_stsl_select_sta, &search) == 0 || search.sm == NULL) { wpa_printf(MSG_DEBUG, "RSN: SMK handshake with " MACSTR " aborted - STA not associated anymore", MAC2STR(kde.mac_addr)); wpa_smk_send_error(wpa_auth, sm, kde.mac_addr, STK_MUI_SMK, STK_ERR_STA_NR); /* FIX: wpa_stsl_remove(wpa_auth, neg); */ return; } if (os_get_random(smk, PMK_LEN)) { wpa_printf(MSG_DEBUG, "RSN: Failed to generate SMK"); return; } /* SMK = PRF-256(Random number, "SMK Derivation", * AA || Time || INonce || PNonce) */ os_memcpy(buf, wpa_auth->addr, ETH_ALEN); pos = buf + ETH_ALEN; wpa_get_ntp_timestamp(pos); pos += 8; os_memcpy(pos, kde.nonce, WPA_NONCE_LEN); pos += WPA_NONCE_LEN; os_memcpy(pos, key->key_nonce, WPA_NONCE_LEN); sha1_prf(smk, PMK_LEN, "SMK Derivation", buf, sizeof(buf), smk, PMK_LEN); wpa_hexdump_key(MSG_DEBUG, "RSN: SMK", smk, PMK_LEN); wpa_send_smk_m4(wpa_auth, sm, key, &kde, smk); wpa_send_smk_m5(wpa_auth, search.sm, key, &kde, smk, sm->addr); /* Authenticator does not need SMK anymore and it is required to forget * it. */ os_memset(smk, 0, sizeof(*smk)); }
int main(int argc, char *argv[]) { u8 res[512]; int ret = 0; unsigned int i; printf("PRF-SHA1 test cases:\n"); sha1_prf(key0, sizeof(key0), "prefix", data0, sizeof(data0) - 1, res, sizeof(prf0)); if (memcmp(res, prf0, sizeof(prf0)) == 0) printf("Test case 0 - OK\n"); else { printf("Test case 0 - FAILED!\n"); ret++; } sha1_prf(key1, sizeof(key1) - 1, "prefix", data1, sizeof(data1) - 1, res, sizeof(prf1)); if (memcmp(res, prf1, sizeof(prf1)) == 0) printf("Test case 1 - OK\n"); else { printf("Test case 1 - FAILED!\n"); ret++; } sha1_prf(key2, sizeof(key2), "prefix", data2, sizeof(data2), res, sizeof(prf2)); if (memcmp(res, prf2, sizeof(prf2)) == 0) printf("Test case 2 - OK\n"); else { printf("Test case 2 - FAILED!\n"); ret++; } ret += test_eap_fast(); printf("PBKDF2-SHA1 Passphrase test cases:\n"); for (i = 0; i < NUM_PASSPHRASE_TESTS; i++) { u8 psk[32]; struct passphrase_test *test = &passphrase_tests[i]; pbkdf2_sha1(test->passphrase, test->ssid, strlen(test->ssid), 4096, psk, 32); if (memcmp(psk, test->psk, 32) == 0) printf("Test case %d - OK\n", i); else { printf("Test case %d - FAILED!\n", i); ret++; } } printf("PBKDF2-SHA1 test cases (RFC 6070):\n"); for (i = 0; i < NUM_RFC6070_TESTS; i++) { u8 dk[25]; struct rfc6070_test *test = &rfc6070_tests[i]; pbkdf2_sha1(test->p, test->s, strlen(test->s), test->c, dk, test->dk_len); if (memcmp(dk, test->dk, test->dk_len) == 0) printf("Test case %d - OK\n", i); else { printf("Test case %d - FAILED!\n", i); ret++; } } return ret; }
static void * test_driver_init(struct hostapd_data *hapd) { struct test_driver_data *drv; struct sockaddr_un addr_un; struct sockaddr_in addr_in; struct sockaddr *addr; socklen_t alen; drv = os_zalloc(sizeof(struct test_driver_data)); if (drv == NULL) { printf("Could not allocate memory for test driver data\n"); return NULL; } drv->bss = os_zalloc(sizeof(*drv->bss)); if (drv->bss == NULL) { printf("Could not allocate memory for test driver BSS data\n"); free(drv); return NULL; } drv->hapd = hapd; /* Generate a MAC address to help testing with multiple APs */ hapd->own_addr[0] = 0x02; /* locally administered */ sha1_prf((const u8 *) hapd->conf->iface, strlen(hapd->conf->iface), "hostapd test bssid generation", (const u8 *) hapd->conf->ssid.ssid, hapd->conf->ssid.ssid_len, hapd->own_addr + 1, ETH_ALEN - 1); os_strlcpy(drv->bss->ifname, hapd->conf->iface, IFNAMSIZ); memcpy(drv->bss->bssid, hapd->own_addr, ETH_ALEN); if (hapd->conf->test_socket) { if (strlen(hapd->conf->test_socket) >= sizeof(addr_un.sun_path)) { printf("Too long test_socket path\n"); test_driver_free_priv(drv); return NULL; } if (strncmp(hapd->conf->test_socket, "DIR:", 4) == 0) { size_t len = strlen(hapd->conf->test_socket) + 30; drv->socket_dir = strdup(hapd->conf->test_socket + 4); drv->own_socket_path = malloc(len); if (drv->own_socket_path) { snprintf(drv->own_socket_path, len, "%s/AP-" MACSTR, hapd->conf->test_socket + 4, MAC2STR(hapd->own_addr)); } } else if (strncmp(hapd->conf->test_socket, "UDP:", 4) == 0) { drv->udp_port = atoi(hapd->conf->test_socket + 4); } else { drv->own_socket_path = strdup(hapd->conf->test_socket); } if (drv->own_socket_path == NULL && drv->udp_port == 0) { test_driver_free_priv(drv); return NULL; } drv->test_socket = socket(drv->udp_port ? PF_INET : PF_UNIX, SOCK_DGRAM, 0); if (drv->test_socket < 0) { perror("socket"); test_driver_free_priv(drv); return NULL; } if (drv->udp_port) { os_memset(&addr_in, 0, sizeof(addr_in)); addr_in.sin_family = AF_INET; addr_in.sin_port = htons(drv->udp_port); addr = (struct sockaddr *) &addr_in; alen = sizeof(addr_in); } else { os_memset(&addr_un, 0, sizeof(addr_un)); addr_un.sun_family = AF_UNIX; os_strlcpy(addr_un.sun_path, drv->own_socket_path, sizeof(addr_un.sun_path)); addr = (struct sockaddr *) &addr_un; alen = sizeof(addr_un); } if (bind(drv->test_socket, addr, alen) < 0) { perror("bind(PF_UNIX)"); close(drv->test_socket); if (drv->own_socket_path) unlink(drv->own_socket_path); test_driver_free_priv(drv); return NULL; } eloop_register_read_sock(drv->test_socket, test_driver_receive_unix, drv, NULL); } else drv->test_socket = -1; return drv; }
int mrvl_sha1_prf(const u8 *key, size_t key_len, const char *label, const u8 *data, size_t data_len, u8 *buf, size_t buf_len) { sha1_prf(key, key_len, label, data, data_len, buf, buf_len); return 0; }
static int test_driver_init(struct hostapd_data *hapd) { struct test_driver_data *drv; struct sockaddr_un addr; drv = wpa_zalloc(sizeof(struct test_driver_data)); if (drv == NULL) { printf("Could not allocate memory for test driver data\n"); return -1; } drv->bss = wpa_zalloc(sizeof(*drv->bss)); if (drv->bss == NULL) { printf("Could not allocate memory for test driver BSS data\n"); free(drv); return -1; } drv->ops = test_driver_ops; drv->hapd = hapd; /* Generate a MAC address to help testing with multiple APs */ hapd->own_addr[0] = 0x02; /* locally administered */ sha1_prf((const u8 *) hapd->conf->iface, strlen(hapd->conf->iface), "hostapd test bssid generation", (const u8 *) hapd->conf->ssid.ssid, hapd->conf->ssid.ssid_len, hapd->own_addr + 1, ETH_ALEN - 1); strncpy(drv->bss->ifname, hapd->conf->iface, IFNAMSIZ); memcpy(drv->bss->bssid, hapd->own_addr, ETH_ALEN); if (hapd->conf->test_socket) { if (strlen(hapd->conf->test_socket) >= sizeof(addr.sun_path)) { printf("Too long test_socket path\n"); test_driver_free_priv(drv); return -1; } if (strncmp(hapd->conf->test_socket, "DIR:", 4) == 0) { size_t len = strlen(hapd->conf->test_socket) + 30; drv->socket_dir = strdup(hapd->conf->test_socket + 4); drv->own_socket_path = malloc(len); if (drv->own_socket_path) { snprintf(drv->own_socket_path, len, "%s/AP-" MACSTR, hapd->conf->test_socket + 4, MAC2STR(hapd->own_addr)); } } else { drv->own_socket_path = strdup(hapd->conf->test_socket); } if (drv->own_socket_path == NULL) { test_driver_free_priv(drv); return -1; } drv->test_socket = socket(PF_UNIX, SOCK_DGRAM, 0); if (drv->test_socket < 0) { perror("socket(PF_UNIX)"); test_driver_free_priv(drv); return -1; } memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX; strncpy(addr.sun_path, drv->own_socket_path, sizeof(addr.sun_path)); if (bind(drv->test_socket, (struct sockaddr *) &addr, sizeof(addr)) < 0) { perror("bind(PF_UNIX)"); close(drv->test_socket); unlink(drv->own_socket_path); test_driver_free_priv(drv); return -1; } eloop_register_read_sock(drv->test_socket, test_driver_receive_unix, drv, NULL); } else drv->test_socket = -1; hapd->driver = &drv->ops; return 0; }