Merge branch 'bugfix/dpp_optimizations' into 'master'

WiFi: DPP code optimizations

Closes IDFGH-9228

See merge request espressif/esp-idf!34014
This commit is contained in:
Jiang Jiang Jian
2025-04-16 10:20:30 +08:00
11 changed files with 637 additions and 412 deletions

View File

@@ -474,6 +474,9 @@ static const esp_err_msg_t esp_err_msg_table[] = {
ERR_TBL_IT(ESP_ERR_DPP_INVALID_LIST), /* 12443 0x309b Channel list given in
esp_supp_dpp_bootstrap_gen() is not
valid or too big */
# endif
# ifdef ESP_ERR_DPP_CONF_TIMEOUT
ERR_TBL_IT(ESP_ERR_DPP_CONF_TIMEOUT), /* 12444 0x309c DPP Configuration was not received in time */
# endif
// components/esp_common/include/esp_err.h
# ifdef ESP_ERR_MESH_BASE

View File

@@ -16,7 +16,6 @@
extern "C" {
#endif
#define ESP_DPP_AUTH_TIMEOUT_SECS 1
#define ESP_DPP_MAX_CHAN_COUNT 5
#define ESP_ERR_DPP_FAILURE (ESP_ERR_WIFI_BASE + 151) /*!< Generic failure during DPP Operation */
@@ -24,6 +23,7 @@ extern "C" {
#define ESP_ERR_DPP_INVALID_ATTR (ESP_ERR_WIFI_BASE + 153) /*!< Encountered invalid DPP Attribute */
#define ESP_ERR_DPP_AUTH_TIMEOUT (ESP_ERR_WIFI_BASE + 154) /*!< DPP Auth response was not received in time */
#define ESP_ERR_DPP_INVALID_LIST (ESP_ERR_WIFI_BASE + 155) /*!< Channel list given in esp_supp_dpp_bootstrap_gen() is not valid or too big */
#define ESP_ERR_DPP_CONF_TIMEOUT (ESP_ERR_WIFI_BASE + 156) /*!< DPP Configuration was not received in time */
/** @brief Types of Bootstrap Methods for DPP. */
typedef enum dpp_bootstrap_type {

View File

@@ -880,21 +880,23 @@ int crypto_ec_key_verify_signature_r_s(struct crypto_ec_key *csign,
void crypto_ec_key_debug_print(struct crypto_ec_key *key, const char *title)
{
#ifdef DEBUG_PRINT
#if defined(CONFIG_LOG_DEFAULT_LEVEL_DEBUG) || defined(CONFIG_LOG_DEFAULT_LEVEL_VERBOSE)
#if defined(DEBUG_PRINT)
mbedtls_pk_context *pkey = (mbedtls_pk_context *)key;
mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(*pkey);
u8 x[32], y[32], d[32];
wpa_printf(MSG_INFO, "curve: %s",
wpa_printf(MSG_EXCESSIVE, "curve: %s",
mbedtls_ecp_curve_info_from_grp_id(ecp->MBEDTLS_PRIVATE(grp).id)->name);
int len = mbedtls_mpi_size((mbedtls_mpi *)crypto_ec_get_prime((struct crypto_ec *)crypto_ec_get_group_from_key(key)));
wpa_printf(MSG_INFO, "prime len is %d", len);
wpa_printf(MSG_EXCESSIVE, "prime len is %d", len);
crypto_ec_point_to_bin((struct crypto_ec *)crypto_ec_get_group_from_key(key), crypto_ec_key_get_public_key(key), x, y);
crypto_bignum_to_bin(crypto_ec_key_get_private_key(key),
d, len, len);
wpa_hexdump(MSG_INFO, "Q_x:", x, 32);
wpa_hexdump(MSG_INFO, "Q_y:", y, 32);
wpa_hexdump(MSG_INFO, "d: ", d, 32);
wpa_hexdump(MSG_EXCESSIVE, "Q_x:", x, 32);
wpa_hexdump(MSG_EXCESSIVE, "Q_y:", y, 32);
wpa_hexdump(MSG_EXCESSIVE, "d: ", d, 32);
#endif
#endif
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2020-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -14,27 +14,12 @@
#include "esp_dpp.h"
#include "esp_wifi_driver.h"
#define DPP_TASK_STACK_SIZE (6144 + TASK_STACK_SIZE_ADD)
#define ESP_DPP_AUTH_TIMEOUT_SECS 2
#define ESP_GAS_TIMEOUT_SECS 2
#define ESP_DPP_PMK_CACHE_DEFAULT_TIMEOUT 86400 * 7 /*!< 7 days */
enum SIG_DPP {
SIG_DPP_RESET = 0,
SIG_DPP_BOOTSTRAP_GEN,
SIG_DPP_RX_ACTION,
SIG_DPP_LISTEN_NEXT_CHANNEL,
SIG_DPP_DEL_TASK,
SIG_DPP_START_NET_INTRO,
SIG_DPP_DEINIT_AUTH,
SIG_DPP_MAX,
};
typedef struct {
uint32_t id;
uint32_t data;
} dpp_event_t;
#define BOOTSTRAP_ROC_WAIT_TIME 500
#define OFFCHAN_TX_WAIT_TIME 500
#define OFFCHAN_TX_WAIT_TIME 600
struct dpp_bootstrap_params_t {
enum dpp_bootstrap_type type;
@@ -53,11 +38,10 @@ struct esp_dpp_context_t {
struct dpp_global *dpp_global;
wifi_config_t wifi_cfg;
int id;
bool dpp_init_done;
bool bootstrap_done;
};
int esp_supp_rx_action(uint8_t *hdr, uint8_t *payload, size_t len, uint8_t channel);
esp_err_t esp_dpp_post_evt(uint32_t evt_id, uint32_t data);
#ifdef CONFIG_TESTING_OPTIONS
int dpp_test_gen_invalid_key(struct wpabuf *msg, const struct dpp_curve_params *curve);
char * dpp_corrupt_connector_signature(const char *connector);

View File

@@ -213,7 +213,7 @@ int dpp_connect(uint8_t *bssid, bool pdr_done)
int res = 0;
if (!pdr_done) {
if (esp_wifi_sta_get_prof_authmode_internal() == WPA3_AUTH_DPP) {
esp_dpp_post_evt(SIG_DPP_START_NET_INTRO, (u32)bssid);
esp_dpp_start_net_intro_protocol(bssid);
}
} else {
res = wpa_config_bss(bssid);

View File

@@ -6,7 +6,7 @@
* See README for more details.
*/
/*
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -18,6 +18,9 @@
#include "eloop.h"
#include "esp_wifi_driver.h"
#include "rom/ets_sys.h"
#include <stdatomic.h>
bool current_task_is_wifi_task(void);
struct eloop_timeout {
struct dl_list list;
@@ -29,12 +32,17 @@ struct eloop_timeout {
char func_name[100];
int line;
#endif
void *sync_semph;
int ret;
eloop_blocking_timeout_handler blocking_handler;
};
struct eloop_data {
struct dl_list timeout;
ETSTimer eloop_timer;
bool eloop_started;
atomic_bool eloop_started;
atomic_bool timeout_running;
void *eloop_semph;
};
#define ELOOP_LOCK() os_mutex_lock(eloop_data_lock)
@@ -74,7 +82,8 @@ int eloop_init(void)
wpa_printf(MSG_ERROR, "failed to create eloop data loop");
return -1;
}
eloop.eloop_started = true;
atomic_store(&eloop.eloop_started, true);
atomic_store(&eloop.timeout_running, false);
return 0;
}
@@ -95,6 +104,9 @@ int eloop_register_timeout(unsigned int secs, unsigned int usecs,
int count = 0;
#endif
if (!atomic_load(&eloop.eloop_started)) {
return -1;
}
timeout = os_zalloc(sizeof(*timeout));
if (timeout == NULL) {
return -1;
@@ -164,6 +176,90 @@ overflow:
return 0;
}
#ifdef ELOOP_DEBUG
int eloop_register_timeout_blocking_debug(eloop_blocking_timeout_handler handler, void *eloop_data,
void *user_data, const char *func, int line)
#else
int eloop_register_timeout_blocking(eloop_blocking_timeout_handler handler,
void *eloop_data, void *user_data)
#endif
{
struct eloop_timeout *timeout, *tmp;
#ifdef ELOOP_DEBUG
int count = 0;
#endif
int ret;
if (current_task_is_wifi_task()) {
assert(false);
return -1;
}
if (!atomic_load(&eloop.eloop_started)) {
return -1;
}
timeout = os_zalloc(sizeof(*timeout));
if (timeout == NULL) {
return -1;
}
if (os_get_reltime(&timeout->time) < 0) {
os_free(timeout);
return -1;
}
timeout->eloop_data = eloop_data;
timeout->user_data = user_data;
timeout->blocking_handler = handler;
#ifdef ELOOP_DEBUG
os_strlcpy(timeout->func_name, func, 100);
timeout->line = line;
#endif
ELOOP_LOCK();
if (!eloop.eloop_semph) {
eloop.eloop_semph = os_semphr_create(1, 0);
}
ELOOP_UNLOCK();
if (!eloop.eloop_semph) {
wpa_printf(MSG_INFO, "ELOOP: sync semphr not available");
os_free(timeout);
return -1;
}
timeout->sync_semph = eloop.eloop_semph;
/* Maintain timeouts in order of increasing time */
dl_list_for_each(tmp, &eloop.timeout, struct eloop_timeout, list) {
if (os_reltime_before(&timeout->time, &tmp->time)) {
ELOOP_LOCK();
dl_list_add(tmp->list.prev, &timeout->list);
ELOOP_UNLOCK();
goto run;
}
#ifdef ELOOP_DEBUG
count++;
#endif
}
ELOOP_LOCK();
dl_list_add_tail(&eloop.timeout, &timeout->list);
ELOOP_UNLOCK();
run:
#ifdef ELOOP_DEBUG
wpa_printf(MSG_DEBUG, "ELOOP: Added one blocking timer from %s:%d to call %p, current order=%d",
timeout->func_name, line, timeout->handler, count);
#endif
ELOOP_LOCK();
os_timer_disarm(&eloop.eloop_timer);
os_timer_arm(&eloop.eloop_timer, 0, 0);
ELOOP_UNLOCK();
wpa_printf(MSG_DEBUG, "ELOOP: waiting for sync semphr");
os_semphr_take(eloop.eloop_semph, OS_BLOCK);
ret = timeout->ret;
os_free(timeout);
return ret;
}
static bool timeout_exists(struct eloop_timeout *old)
{
struct eloop_timeout *timeout, *prev;
@@ -177,6 +273,18 @@ static bool timeout_exists(struct eloop_timeout *old)
return false;
}
static void eloop_remove_blocking_timeout(struct eloop_timeout *timeout)
{
bool timeout_present = false;
ELOOP_LOCK();
/* Make sure timeout still exists(Another context may have deleted this) */
timeout_present = timeout_exists(timeout);
if (timeout_present) {
dl_list_del(&timeout->list);
}
ELOOP_UNLOCK();
}
static void eloop_remove_timeout(struct eloop_timeout *timeout)
{
bool timeout_present = false;
@@ -331,6 +439,11 @@ void eloop_run(void)
{
struct os_reltime tv, now;
if (!atomic_load(&eloop.eloop_started)) {
return;
}
atomic_store(&eloop.timeout_running, true);
while (!dl_list_empty(&eloop.timeout)) {
struct eloop_timeout *timeout;
@@ -348,17 +461,10 @@ void eloop_run(void)
os_timer_arm(&eloop.eloop_timer, ms, 0);
ELOOP_UNLOCK();
goto out;
}
}
/* check if some registered timeouts have occurred */
timeout = dl_list_first(&eloop.timeout, struct eloop_timeout,
list);
if (timeout) {
os_get_reltime(&now);
if (!os_reltime_before(&now, &timeout->time)) {
} else {
void *eloop_data = timeout->eloop_data;
void *user_data = timeout->user_data;
void *sync_semaphr = timeout->sync_semph;
eloop_timeout_handler handler =
timeout->handler;
#ifdef ELOOP_DEBUG
@@ -366,30 +472,54 @@ void eloop_run(void)
int line = timeout->line;
os_strlcpy(fn_name, timeout->func_name, 100);
#endif
eloop_remove_timeout(timeout);
/* will be freed in caller context in blocking call */
if (!sync_semaphr) {
eloop_remove_timeout(timeout);
#ifdef ELOOP_DEBUG
wpa_printf(MSG_DEBUG, "ELOOP: Running timer fn:%p scheduled by %s:%d ",
handler, fn_name, line);
wpa_printf(MSG_DEBUG, "ELOOP: Running timer fn:%p scheduled by %s:%d ",
handler, fn_name, line);
#endif
handler(eloop_data, user_data);
handler(eloop_data, user_data);
} else {
eloop_remove_blocking_timeout(timeout);
eloop_blocking_timeout_handler handler2 =
timeout->blocking_handler;
#ifdef ELOOP_DEBUG
wpa_printf(MSG_DEBUG, "ELOOP: Running blocking timer fn:%p scheduled by %s:%d ",
handler2, fn_name, line);
#endif
timeout->ret = handler2(eloop_data, user_data);
#ifdef ELOOP_DEBUG
wpa_printf(MSG_DEBUG, "ELOOP: releasing sync semaphor");
#endif
os_semphr_give(sync_semaphr);
}
}
}
}
out:
atomic_store(&eloop.timeout_running, false);
return;
}
void eloop_destroy(void)
{
struct eloop_timeout *timeout, *prev;
struct os_reltime now;
if (!eloop.eloop_started) {
if (!atomic_load(&eloop.eloop_started)) {
return;
}
os_get_reltime(&now);
atomic_store(&eloop.eloop_started, false);
while (atomic_load(&eloop.timeout_running)) {
vTaskDelay(100 / portTICK_PERIOD_MS); // Yield CPU
}
dl_list_for_each_safe(timeout, prev, &eloop.timeout,
struct eloop_timeout, list) {
#ifdef ELOOP_DEBUG
struct os_reltime now;
os_get_reltime(&now);
int sec, usec;
sec = timeout->time.sec - now.sec;
usec = timeout->time.usec - now.usec;
@@ -401,12 +531,17 @@ void eloop_destroy(void)
"eloop_data=%p user_data=%p handler=%p",
sec, usec, timeout->eloop_data, timeout->user_data,
timeout->handler);
#endif
eloop_remove_timeout(timeout);
}
if (eloop_data_lock) {
os_mutex_delete(eloop_data_lock);
eloop_data_lock = NULL;
}
if (eloop.eloop_semph) {
os_semphr_delete(eloop.eloop_semph);
eloop.eloop_semph = NULL;
}
os_timer_disarm(&eloop.eloop_timer);
os_timer_done(&eloop.eloop_timer);
os_memset(&eloop, 0, sizeof(eloop));

View File

@@ -84,7 +84,7 @@ void dpp_debug_print_point(const char *title, struct crypto_ec *e,
static void dpp_auth_fail(struct dpp_authentication *auth, const char *txt)
{
wpa_msg(auth->msg_ctx, MSG_INFO, DPP_EVENT_FAIL "%s", txt);
wpa_printf(MSG_INFO, "%s", txt);
}
struct wpabuf * dpp_alloc_msg(enum dpp_public_action_frame_type type,

View File

@@ -261,7 +261,7 @@ struct dpp_authentication {
* Authentication exchange */
unsigned int freq[DPP_BOOTSTRAP_MAX_FREQ];
unsigned int num_freq, freq_idx;
unsigned int curr_chan;
unsigned int curr_chan;
unsigned int curr_freq;
unsigned int neg_freq;
unsigned int num_freq_iters;

View File

@@ -56,6 +56,9 @@ typedef void (*eloop_event_handler)(void *eloop_ctx, void *user_ctx);
*/
typedef void (*eloop_timeout_handler)(void *eloop_ctx, void *user_ctx);
typedef int (*eloop_blocking_timeout_handler)(void *eloop_ctx, void *user_ctx);
/**
* eloop_signal_handler - eloop signal event callback type
* @sig: Signal number
@@ -190,6 +193,19 @@ int eloop_register_timeout(unsigned int secs, unsigned int usecs,
void *eloop_data, void *user_data);
#endif
#ifdef ELOOP_DEBUG
int eloop_register_timeout_blocking_debug(eloop_blocking_timeout_handler handler, void *eloop_data,
void *user_data, const char *func, int line);
#define eloop_register_timeout_blocking(handler, eloop_data, user_data) \
eloop_register_timeout_blocking_debug(handler, eloop_data, user_data, __func__, __LINE__)
#else
int eloop_register_timeout_blocking(eloop_blocking_timeout_handler handler,
void *eloop_data, void *user_data);
#endif
/**
* eloop_cancel_timeout - Cancel timeouts
* @handler: Matching callback function

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
@@ -25,7 +25,6 @@ static int executed_order[6];
static int t;
static struct os_reltime ts;
/* there is only single instance of esp_timer so no need of protection */
static void callback(void *a, void *b)
{
@@ -80,6 +79,15 @@ TEST_CASE("Test eloop timers run", "[eloop]")
os_sleep(20, 0);
/* check the execution order, this will also check whether they were fired at correct time */
TEST_ASSERT(memcmp(execution_order, executed_order, 6 * sizeof(int)) == 0);
/* Add timers to check deinit happens gracefully */
for (int i = 0; i < 6; i++) {
eloop_register_timeout(timeouts_sec[i], timeouts_usec[i],
callback, &index[i], NULL);
}
/* Stop wifi before all the timers have run */
os_sleep(2, 0);
TEST_ESP_OK(esp_wifi_stop());
TEST_ESP_OK(esp_wifi_deinit());
os_sleep(3, 0);