diff --git a/.codespellrc b/.codespellrc index c9cdd4c7cd..4f3b1e3bee 100644 --- a/.codespellrc +++ b/.codespellrc @@ -1,4 +1,4 @@ [codespell] -skip = build,*.yuv,components/fatfs/src/*,alice.txt,*.rgb +skip = build,*.yuv,components/fatfs/src/*,alice.txt,*.rgb,components/wpa_supplicant/*,components/esp_wifi/* ignore-words-list = ser,dout,rsource,fram,inout,shs,ans,aci,unstall,unstalling,hart write-changes = true diff --git a/components/esp_wifi/esp32/esp_adapter.c b/components/esp_wifi/esp32/esp_adapter.c index 02baaee315..4bc3a36706 100644 --- a/components/esp_wifi/esp32/esp_adapter.c +++ b/components/esp_wifi/esp32/esp_adapter.c @@ -71,10 +71,10 @@ static void IRAM_ATTR s_esp_dport_access_stall_other_cpu_end(void) If CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP is enabled. Prefer to allocate a chunk of memory in SPIRAM firstly. If failed, try to allocate it in internal memory then. */ -IRAM_ATTR void *wifi_malloc( size_t size ) +IRAM_ATTR void *wifi_malloc(size_t size) { #if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP - return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL); + return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL); #else return malloc(size); #endif @@ -84,10 +84,10 @@ IRAM_ATTR void *wifi_malloc( size_t size ) If CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP is enabled. Prefer to allocate a chunk of memory in SPIRAM firstly. If failed, try to allocate it in internal memory then. */ -IRAM_ATTR void *wifi_realloc( void *ptr, size_t size ) +IRAM_ATTR void *wifi_realloc(void *ptr, size_t size) { #if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP - return heap_caps_realloc_prefer(ptr, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL); + return heap_caps_realloc_prefer(ptr, size, 2, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL); #else return realloc(ptr, size); #endif @@ -97,10 +97,10 @@ IRAM_ATTR void *wifi_realloc( void *ptr, size_t size ) If CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP is enabled. Prefer to allocate a chunk of memory in SPIRAM firstly. If failed, try to allocate it in internal memory then. */ -IRAM_ATTR void *wifi_calloc( size_t n, size_t size ) +IRAM_ATTR void *wifi_calloc(size_t n, size_t size) { #if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP - return heap_caps_calloc_prefer(n, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL); + return heap_caps_calloc_prefer(n, size, 2, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL); #else return calloc(n, size); #endif @@ -112,11 +112,11 @@ static void * IRAM_ATTR wifi_zalloc_wrapper(size_t size) return ptr; } -wifi_static_queue_t* wifi_create_queue( int queue_len, int item_size) +wifi_static_queue_t* wifi_create_queue(int queue_len, int item_size) { wifi_static_queue_t *queue = NULL; - queue = (wifi_static_queue_t*)heap_caps_malloc(sizeof(wifi_static_queue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); + queue = (wifi_static_queue_t*)heap_caps_malloc(sizeof(wifi_static_queue_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); if (!queue) { return NULL; } @@ -124,12 +124,12 @@ wifi_static_queue_t* wifi_create_queue( int queue_len, int item_size) #if CONFIG_SPIRAM_USE_MALLOC /* Wi-Fi still use internal RAM */ - queue->storage = heap_caps_calloc(1, sizeof(StaticQueue_t) + (queue_len*item_size), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); + queue->storage = heap_caps_calloc(1, sizeof(StaticQueue_t) + (queue_len * item_size), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); if (!queue->storage) { goto _error; } - queue->handle = xQueueCreateStatic( queue_len, item_size, ((uint8_t*)(queue->storage)) + sizeof(StaticQueue_t), (StaticQueue_t*)(queue->storage)); + queue->handle = xQueueCreateStatic(queue_len, item_size, ((uint8_t*)(queue->storage)) + sizeof(StaticQueue_t), (StaticQueue_t*)(queue->storage)); if (!queue->handle) { goto _error; @@ -148,7 +148,7 @@ _error: return NULL; #else - queue->handle = xQueueCreate( queue_len, item_size); + queue->handle = xQueueCreate(queue_len, item_size); return queue; #endif } @@ -405,17 +405,17 @@ static int get_time_wrapper(void *t) static void * IRAM_ATTR realloc_internal_wrapper(void *ptr, size_t size) { - return heap_caps_realloc(ptr, size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL); + return heap_caps_realloc(ptr, size, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); } static void * IRAM_ATTR calloc_internal_wrapper(size_t n, size_t size) { - return heap_caps_calloc(n, size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL); + return heap_caps_calloc(n, size, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); } static void * IRAM_ATTR zalloc_internal_wrapper(size_t size) { - void *ptr = heap_caps_calloc(1, size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL); + void *ptr = heap_caps_calloc(1, size, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); return ptr; } @@ -629,8 +629,8 @@ wifi_osi_funcs_t g_wifi_osi_funcs = { ._queue_msg_waiting = (uint32_t(*)(void *))uxQueueMessagesWaiting, ._event_group_create = (void *(*)(void))xEventGroupCreate, ._event_group_delete = (void(*)(void *))vEventGroupDelete, - ._event_group_set_bits = (uint32_t(*)(void *,uint32_t))xEventGroupSetBits, - ._event_group_clear_bits = (uint32_t(*)(void *,uint32_t))xEventGroupClearBits, + ._event_group_set_bits = (uint32_t(*)(void *, uint32_t))xEventGroupSetBits, + ._event_group_clear_bits = (uint32_t(*)(void *, uint32_t))xEventGroupClearBits, ._event_group_wait_bits = event_group_wait_bits_wrapper, ._task_create_pinned_to_core = task_create_pinned_to_core_wrapper, ._task_create = task_create_wrapper, diff --git a/components/esp_wifi/esp32c2/esp_adapter.c b/components/esp_wifi/esp32c2/esp_adapter.c index dc61711e51..6efcee9610 100644 --- a/components/esp_wifi/esp32c2/esp_adapter.c +++ b/components/esp_wifi/esp32c2/esp_adapter.c @@ -56,17 +56,17 @@ extern void wifi_apb80m_request(void); extern void wifi_apb80m_release(void); #endif -IRAM_ATTR void *wifi_malloc( size_t size ) +IRAM_ATTR void *wifi_malloc(size_t size) { return malloc(size); } -IRAM_ATTR void *wifi_realloc( void *ptr, size_t size ) +IRAM_ATTR void *wifi_realloc(void *ptr, size_t size) { return realloc(ptr, size); } -IRAM_ATTR void *wifi_calloc( size_t n, size_t size ) +IRAM_ATTR void *wifi_calloc(size_t n, size_t size) { return calloc(n, size); } @@ -77,16 +77,16 @@ static void * IRAM_ATTR wifi_zalloc_wrapper(size_t size) return ptr; } -wifi_static_queue_t* wifi_create_queue( int queue_len, int item_size) +wifi_static_queue_t* wifi_create_queue(int queue_len, int item_size) { wifi_static_queue_t *queue = NULL; - queue = (wifi_static_queue_t*)heap_caps_malloc(sizeof(wifi_static_queue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); + queue = (wifi_static_queue_t*)heap_caps_malloc(sizeof(wifi_static_queue_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); if (!queue) { return NULL; } - queue->handle = xQueueCreate( queue_len, item_size); + queue->handle = xQueueCreate(queue_len, item_size); return queue; } @@ -317,31 +317,31 @@ static int get_time_wrapper(void *t) static void * IRAM_ATTR realloc_internal_wrapper(void *ptr, size_t size) { - return heap_caps_realloc(ptr, size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL); + return heap_caps_realloc(ptr, size, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); } static void * IRAM_ATTR calloc_internal_wrapper(size_t n, size_t size) { - return heap_caps_calloc(n, size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL); + return heap_caps_calloc(n, size, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); } static void * IRAM_ATTR zalloc_internal_wrapper(size_t size) { - void *ptr = heap_caps_calloc(1, size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL); + void *ptr = heap_caps_calloc(1, size, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); return ptr; } static esp_err_t nvs_open_wrapper(const char* name, unsigned int open_mode, nvs_handle_t *out_handle) { - return nvs_open(name,(nvs_open_mode_t)open_mode, out_handle); + return nvs_open(name, (nvs_open_mode_t)open_mode, out_handle); } static void esp_log_writev_wrapper(unsigned int level, const char *tag, const char *format, va_list args) { - return esp_log_writev((esp_log_level_t)level,tag,format,args); + return esp_log_writev((esp_log_level_t)level, tag, format, args); } -static void esp_log_write_wrapper(unsigned int level,const char *tag,const char *format, ...) +static void esp_log_write_wrapper(unsigned int level, const char *tag, const char *format, ...) { va_list list; va_start(list, format); @@ -568,8 +568,8 @@ wifi_osi_funcs_t g_wifi_osi_funcs = { ._queue_msg_waiting = (uint32_t(*)(void *))uxQueueMessagesWaiting, ._event_group_create = (void *(*)(void))xEventGroupCreate, ._event_group_delete = (void(*)(void *))vEventGroupDelete, - ._event_group_set_bits = (uint32_t(*)(void *,uint32_t))xEventGroupSetBits, - ._event_group_clear_bits = (uint32_t(*)(void *,uint32_t))xEventGroupClearBits, + ._event_group_set_bits = (uint32_t(*)(void *, uint32_t))xEventGroupSetBits, + ._event_group_clear_bits = (uint32_t(*)(void *, uint32_t))xEventGroupClearBits, ._event_group_wait_bits = event_group_wait_bits_wrapper, ._task_create_pinned_to_core = task_create_pinned_to_core_wrapper, ._task_create = task_create_wrapper, diff --git a/components/esp_wifi/esp32c3/esp_adapter.c b/components/esp_wifi/esp32c3/esp_adapter.c index 4711f43684..923c3fcefd 100644 --- a/components/esp_wifi/esp32c3/esp_adapter.c +++ b/components/esp_wifi/esp32c3/esp_adapter.c @@ -59,17 +59,17 @@ extern void wifi_apb80m_request(void); extern void wifi_apb80m_release(void); #endif -IRAM_ATTR void *wifi_malloc( size_t size ) +IRAM_ATTR void *wifi_malloc(size_t size) { return malloc(size); } -IRAM_ATTR void *wifi_realloc( void *ptr, size_t size ) +IRAM_ATTR void *wifi_realloc(void *ptr, size_t size) { return realloc(ptr, size); } -IRAM_ATTR void *wifi_calloc( size_t n, size_t size ) +IRAM_ATTR void *wifi_calloc(size_t n, size_t size) { return calloc(n, size); } @@ -80,16 +80,16 @@ static void * IRAM_ATTR wifi_zalloc_wrapper(size_t size) return ptr; } -wifi_static_queue_t* wifi_create_queue( int queue_len, int item_size) +wifi_static_queue_t* wifi_create_queue(int queue_len, int item_size) { wifi_static_queue_t *queue = NULL; - queue = (wifi_static_queue_t*)heap_caps_malloc(sizeof(wifi_static_queue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); + queue = (wifi_static_queue_t*)heap_caps_malloc(sizeof(wifi_static_queue_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); if (!queue) { return NULL; } - queue->handle = xQueueCreate( queue_len, item_size); + queue->handle = xQueueCreate(queue_len, item_size); return queue; } @@ -334,31 +334,31 @@ static int get_time_wrapper(void *t) static void * IRAM_ATTR realloc_internal_wrapper(void *ptr, size_t size) { - return heap_caps_realloc(ptr, size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL); + return heap_caps_realloc(ptr, size, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); } static void * IRAM_ATTR calloc_internal_wrapper(size_t n, size_t size) { - return heap_caps_calloc(n, size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL); + return heap_caps_calloc(n, size, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); } static void * IRAM_ATTR zalloc_internal_wrapper(size_t size) { - void *ptr = heap_caps_calloc(1, size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL); + void *ptr = heap_caps_calloc(1, size, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); return ptr; } static esp_err_t nvs_open_wrapper(const char* name, unsigned int open_mode, nvs_handle_t *out_handle) { - return nvs_open(name,(nvs_open_mode_t)open_mode, out_handle); + return nvs_open(name, (nvs_open_mode_t)open_mode, out_handle); } static void esp_log_writev_wrapper(unsigned int level, const char *tag, const char *format, va_list args) { - return esp_log_writev((esp_log_level_t)level,tag,format,args); + return esp_log_writev((esp_log_level_t)level, tag, format, args); } -static void esp_log_write_wrapper(unsigned int level,const char *tag,const char *format, ...) +static void esp_log_write_wrapper(unsigned int level, const char *tag, const char *format, ...) { va_list list; va_start(list, format); @@ -585,8 +585,8 @@ wifi_osi_funcs_t g_wifi_osi_funcs = { ._queue_msg_waiting = (uint32_t(*)(void *))uxQueueMessagesWaiting, ._event_group_create = (void *(*)(void))xEventGroupCreate, ._event_group_delete = (void(*)(void *))vEventGroupDelete, - ._event_group_set_bits = (uint32_t(*)(void *,uint32_t))xEventGroupSetBits, - ._event_group_clear_bits = (uint32_t(*)(void *,uint32_t))xEventGroupClearBits, + ._event_group_set_bits = (uint32_t(*)(void *, uint32_t))xEventGroupSetBits, + ._event_group_clear_bits = (uint32_t(*)(void *, uint32_t))xEventGroupClearBits, ._event_group_wait_bits = event_group_wait_bits_wrapper, ._task_create_pinned_to_core = task_create_pinned_to_core_wrapper, ._task_create = task_create_wrapper, diff --git a/components/esp_wifi/esp32c6/esp_adapter.c b/components/esp_wifi/esp32c6/esp_adapter.c index 4c3824fa3b..b44e986000 100644 --- a/components/esp_wifi/esp32c6/esp_adapter.c +++ b/components/esp_wifi/esp32c6/esp_adapter.c @@ -62,17 +62,17 @@ extern void wifi_apb80m_request(void); extern void wifi_apb80m_release(void); #endif -IRAM_ATTR void *wifi_malloc( size_t size ) +IRAM_ATTR void *wifi_malloc(size_t size) { return malloc(size); } -IRAM_ATTR void *wifi_realloc( void *ptr, size_t size ) +IRAM_ATTR void *wifi_realloc(void *ptr, size_t size) { return realloc(ptr, size); } -IRAM_ATTR void *wifi_calloc( size_t n, size_t size ) +IRAM_ATTR void *wifi_calloc(size_t n, size_t size) { return calloc(n, size); } @@ -83,7 +83,7 @@ static void *IRAM_ATTR wifi_zalloc_wrapper(size_t size) return ptr; } -wifi_static_queue_t *wifi_create_queue( int queue_len, int item_size) +wifi_static_queue_t *wifi_create_queue(int queue_len, int item_size) { wifi_static_queue_t *queue = NULL; @@ -92,7 +92,7 @@ wifi_static_queue_t *wifi_create_queue( int queue_len, int item_size) return NULL; } - queue->handle = xQueueCreate( queue_len, item_size); + queue->handle = xQueueCreate(queue_len, item_size); return queue; } diff --git a/components/esp_wifi/esp32p4/esp_adapter.c b/components/esp_wifi/esp32p4/esp_adapter.c index 69494a94cc..e004f3c0d0 100644 --- a/components/esp_wifi/esp32p4/esp_adapter.c +++ b/components/esp_wifi/esp32p4/esp_adapter.c @@ -53,10 +53,10 @@ extern void wifi_apb80m_request(void); extern void wifi_apb80m_release(void); #endif -IRAM_ATTR void *wifi_malloc( size_t size ) +IRAM_ATTR void *wifi_malloc(size_t size) { #if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP - return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL); + return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL); #else return malloc(size); #endif @@ -66,10 +66,10 @@ IRAM_ATTR void *wifi_malloc( size_t size ) If CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP is enabled. Prefer to allocate a chunk of memory in SPIRAM firstly. If failed, try to allocate it in internal memory then. */ -IRAM_ATTR void *wifi_realloc( void *ptr, size_t size ) +IRAM_ATTR void *wifi_realloc(void *ptr, size_t size) { #if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP - return heap_caps_realloc_prefer(ptr, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL); + return heap_caps_realloc_prefer(ptr, size, 2, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL); #else return realloc(ptr, size); #endif @@ -79,10 +79,10 @@ IRAM_ATTR void *wifi_realloc( void *ptr, size_t size ) If CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP is enabled. Prefer to allocate a chunk of memory in SPIRAM firstly. If failed, try to allocate it in internal memory then. */ -IRAM_ATTR void *wifi_calloc( size_t n, size_t size ) +IRAM_ATTR void *wifi_calloc(size_t n, size_t size) { #if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP - return heap_caps_calloc_prefer(n, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL); + return heap_caps_calloc_prefer(n, size, 2, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL); #else return calloc(n, size); #endif @@ -94,7 +94,7 @@ static void *IRAM_ATTR wifi_zalloc_wrapper(size_t size) return ptr; } -wifi_static_queue_t *wifi_create_queue( int queue_len, int item_size) +wifi_static_queue_t *wifi_create_queue(int queue_len, int item_size) { wifi_static_queue_t *queue = NULL; @@ -105,12 +105,12 @@ wifi_static_queue_t *wifi_create_queue( int queue_len, int item_size) #if CONFIG_SPIRAM_USE_MALLOC - queue->storage = heap_caps_calloc(1, sizeof(StaticQueue_t) + (queue_len*item_size), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); + queue->storage = heap_caps_calloc(1, sizeof(StaticQueue_t) + (queue_len * item_size), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); if (!queue->storage) { goto _error; } - queue->handle = xQueueCreateStatic( queue_len, item_size, ((uint8_t*)(queue->storage)) + sizeof(StaticQueue_t), (StaticQueue_t*)(queue->storage)); + queue->handle = xQueueCreateStatic(queue_len, item_size, ((uint8_t*)(queue->storage)) + sizeof(StaticQueue_t), (StaticQueue_t*)(queue->storage)); if (!queue->handle) { goto _error; @@ -129,7 +129,7 @@ _error: return NULL; #else - queue->handle = xQueueCreate( queue_len, item_size); + queue->handle = xQueueCreate(queue_len, item_size); return queue; #endif } @@ -550,10 +550,10 @@ bool IRAM_ATTR esp_coex_common_env_is_chip_wrapper(void) void * esp_coex_common_spin_lock_create_wrapper(void) { portMUX_TYPE tmp = portMUX_INITIALIZER_UNLOCKED; - void *mux = heap_caps_malloc(sizeof(portMUX_TYPE), MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL); + void *mux = heap_caps_malloc(sizeof(portMUX_TYPE), MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL); if (mux) { - memcpy(mux,&tmp,sizeof(portMUX_TYPE)); + memcpy(mux, &tmp, sizeof(portMUX_TYPE)); return mux; } return NULL; @@ -630,7 +630,7 @@ void IRAM_ATTR esp_coex_common_timer_arm_us_wrapper(void *ptimer, uint32_t us, b void * IRAM_ATTR esp_coex_common_malloc_internal_wrapper(size_t size) { - return heap_caps_malloc(size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL); + return heap_caps_malloc(size, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); } wifi_osi_funcs_t g_wifi_osi_funcs = { diff --git a/components/esp_wifi/esp32s2/esp_adapter.c b/components/esp_wifi/esp32s2/esp_adapter.c index 38b8eb3334..72d9fd4b54 100644 --- a/components/esp_wifi/esp32s2/esp_adapter.c +++ b/components/esp_wifi/esp32s2/esp_adapter.c @@ -62,10 +62,10 @@ extern void wifi_apb80m_release(void); If CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP is enabled. Prefer to allocate a chunk of memory in SPIRAM firstly. If failed, try to allocate it in internal memory then. */ -IRAM_ATTR void *wifi_malloc( size_t size ) +IRAM_ATTR void *wifi_malloc(size_t size) { #if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP - return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL); + return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL); #else return malloc(size); #endif @@ -75,10 +75,10 @@ IRAM_ATTR void *wifi_malloc( size_t size ) If CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP is enabled. Prefer to allocate a chunk of memory in SPIRAM firstly. If failed, try to allocate it in internal memory then. */ -IRAM_ATTR void *wifi_realloc( void *ptr, size_t size ) +IRAM_ATTR void *wifi_realloc(void *ptr, size_t size) { #if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP - return heap_caps_realloc_prefer(ptr, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL); + return heap_caps_realloc_prefer(ptr, size, 2, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL); #else return realloc(ptr, size); #endif @@ -88,10 +88,10 @@ IRAM_ATTR void *wifi_realloc( void *ptr, size_t size ) If CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP is enabled. Prefer to allocate a chunk of memory in SPIRAM firstly. If failed, try to allocate it in internal memory then. */ -IRAM_ATTR void *wifi_calloc( size_t n, size_t size ) +IRAM_ATTR void *wifi_calloc(size_t n, size_t size) { #if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP - return heap_caps_calloc_prefer(n, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL); + return heap_caps_calloc_prefer(n, size, 2, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL); #else return calloc(n, size); #endif @@ -103,11 +103,11 @@ static void * IRAM_ATTR wifi_zalloc_wrapper(size_t size) return ptr; } -wifi_static_queue_t* wifi_create_queue( int queue_len, int item_size) +wifi_static_queue_t* wifi_create_queue(int queue_len, int item_size) { wifi_static_queue_t *queue = NULL; - queue = (wifi_static_queue_t*)heap_caps_malloc(sizeof(wifi_static_queue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); + queue = (wifi_static_queue_t*)heap_caps_malloc(sizeof(wifi_static_queue_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); if (!queue) { return NULL; } @@ -115,12 +115,12 @@ wifi_static_queue_t* wifi_create_queue( int queue_len, int item_size) #if CONFIG_SPIRAM_USE_MALLOC /* Wi-Fi still use internal RAM */ - queue->storage = heap_caps_calloc(1, sizeof(StaticQueue_t) + (queue_len*item_size), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); + queue->storage = heap_caps_calloc(1, sizeof(StaticQueue_t) + (queue_len * item_size), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); if (!queue->storage) { goto _error; } - queue->handle = xQueueCreateStatic( queue_len, item_size, ((uint8_t*)(queue->storage)) + sizeof(StaticQueue_t), (StaticQueue_t*)(queue->storage)); + queue->handle = xQueueCreateStatic(queue_len, item_size, ((uint8_t*)(queue->storage)) + sizeof(StaticQueue_t), (StaticQueue_t*)(queue->storage)); if (!queue->handle) { goto _error; @@ -139,7 +139,7 @@ _error: return NULL; #else - queue->handle = xQueueCreate( queue_len, item_size); + queue->handle = xQueueCreate(queue_len, item_size); return queue; #endif } @@ -396,17 +396,17 @@ static int get_time_wrapper(void *t) static void * IRAM_ATTR realloc_internal_wrapper(void *ptr, size_t size) { - return heap_caps_realloc(ptr, size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL); + return heap_caps_realloc(ptr, size, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); } static void * IRAM_ATTR calloc_internal_wrapper(size_t n, size_t size) { - return heap_caps_calloc(n, size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL); + return heap_caps_calloc(n, size, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); } static void * IRAM_ATTR zalloc_internal_wrapper(size_t size) { - void *ptr = heap_caps_calloc(1, size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL); + void *ptr = heap_caps_calloc(1, size, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); return ptr; } @@ -622,8 +622,8 @@ wifi_osi_funcs_t g_wifi_osi_funcs = { ._queue_msg_waiting = (uint32_t(*)(void *))uxQueueMessagesWaiting, ._event_group_create = (void *(*)(void))xEventGroupCreate, ._event_group_delete = (void(*)(void *))vEventGroupDelete, - ._event_group_set_bits = (uint32_t(*)(void *,uint32_t))xEventGroupSetBits, - ._event_group_clear_bits = (uint32_t(*)(void *,uint32_t))xEventGroupClearBits, + ._event_group_set_bits = (uint32_t(*)(void *, uint32_t))xEventGroupSetBits, + ._event_group_clear_bits = (uint32_t(*)(void *, uint32_t))xEventGroupClearBits, ._event_group_wait_bits = event_group_wait_bits_wrapper, ._task_create_pinned_to_core = task_create_pinned_to_core_wrapper, ._task_create = task_create_wrapper, diff --git a/components/esp_wifi/esp32s3/esp_adapter.c b/components/esp_wifi/esp32s3/esp_adapter.c index b49ef5fada..eaa13b29e9 100644 --- a/components/esp_wifi/esp32s3/esp_adapter.c +++ b/components/esp_wifi/esp32s3/esp_adapter.c @@ -65,10 +65,10 @@ extern void wifi_apb80m_release(void); If CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP is enabled. Prefer to allocate a chunk of memory in SPIRAM firstly. If failed, try to allocate it in internal memory then. */ -IRAM_ATTR void *wifi_malloc( size_t size ) +IRAM_ATTR void *wifi_malloc(size_t size) { #if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP - return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL); + return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL); #else return malloc(size); #endif @@ -78,10 +78,10 @@ IRAM_ATTR void *wifi_malloc( size_t size ) If CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP is enabled. Prefer to allocate a chunk of memory in SPIRAM firstly. If failed, try to allocate it in internal memory then. */ -IRAM_ATTR void *wifi_realloc( void *ptr, size_t size ) +IRAM_ATTR void *wifi_realloc(void *ptr, size_t size) { #if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP - return heap_caps_realloc_prefer(ptr, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL); + return heap_caps_realloc_prefer(ptr, size, 2, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL); #else return realloc(ptr, size); #endif @@ -91,10 +91,10 @@ IRAM_ATTR void *wifi_realloc( void *ptr, size_t size ) If CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP is enabled. Prefer to allocate a chunk of memory in SPIRAM firstly. If failed, try to allocate it in internal memory then. */ -IRAM_ATTR void *wifi_calloc( size_t n, size_t size ) +IRAM_ATTR void *wifi_calloc(size_t n, size_t size) { #if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP - return heap_caps_calloc_prefer(n, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL); + return heap_caps_calloc_prefer(n, size, 2, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL); #else return calloc(n, size); #endif @@ -106,11 +106,11 @@ static void * IRAM_ATTR wifi_zalloc_wrapper(size_t size) return ptr; } -wifi_static_queue_t* wifi_create_queue( int queue_len, int item_size) +wifi_static_queue_t* wifi_create_queue(int queue_len, int item_size) { wifi_static_queue_t *queue = NULL; - queue = (wifi_static_queue_t*)heap_caps_malloc(sizeof(wifi_static_queue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); + queue = (wifi_static_queue_t*)heap_caps_malloc(sizeof(wifi_static_queue_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); if (!queue) { return NULL; } @@ -118,12 +118,12 @@ wifi_static_queue_t* wifi_create_queue( int queue_len, int item_size) #if CONFIG_SPIRAM_USE_MALLOC /* Wi-Fi still use internal RAM */ - queue->storage = heap_caps_calloc(1, sizeof(StaticQueue_t) + (queue_len*item_size), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); + queue->storage = heap_caps_calloc(1, sizeof(StaticQueue_t) + (queue_len * item_size), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); if (!queue->storage) { goto _error; } - queue->handle = xQueueCreateStatic( queue_len, item_size, ((uint8_t*)(queue->storage)) + sizeof(StaticQueue_t), (StaticQueue_t*)(queue->storage)); + queue->handle = xQueueCreateStatic(queue_len, item_size, ((uint8_t*)(queue->storage)) + sizeof(StaticQueue_t), (StaticQueue_t*)(queue->storage)); if (!queue->handle) { goto _error; @@ -142,7 +142,7 @@ _error: return NULL; #else - queue->handle = xQueueCreate( queue_len, item_size); + queue->handle = xQueueCreate(queue_len, item_size); return queue; #endif } @@ -413,17 +413,17 @@ static int get_time_wrapper(void *t) static void * IRAM_ATTR realloc_internal_wrapper(void *ptr, size_t size) { - return heap_caps_realloc(ptr, size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL); + return heap_caps_realloc(ptr, size, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); } static void * IRAM_ATTR calloc_internal_wrapper(size_t n, size_t size) { - return heap_caps_calloc(n, size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL); + return heap_caps_calloc(n, size, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); } static void * IRAM_ATTR zalloc_internal_wrapper(size_t size) { - void *ptr = heap_caps_calloc(1, size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL); + void *ptr = heap_caps_calloc(1, size, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); return ptr; } @@ -595,7 +595,6 @@ static void IRAM_ATTR esp_empty_wrapper(void) } - static void esp_phy_enable_wrapper(void) { esp_phy_enable(PHY_MODEM_WIFI); @@ -642,8 +641,8 @@ wifi_osi_funcs_t g_wifi_osi_funcs = { ._queue_msg_waiting = (uint32_t(*)(void *))uxQueueMessagesWaiting, ._event_group_create = (void *(*)(void))xEventGroupCreate, ._event_group_delete = (void(*)(void *))vEventGroupDelete, - ._event_group_set_bits = (uint32_t(*)(void *,uint32_t))xEventGroupSetBits, - ._event_group_clear_bits = (uint32_t(*)(void *,uint32_t))xEventGroupClearBits, + ._event_group_set_bits = (uint32_t(*)(void *, uint32_t))xEventGroupSetBits, + ._event_group_clear_bits = (uint32_t(*)(void *, uint32_t))xEventGroupClearBits, ._event_group_wait_bits = event_group_wait_bits_wrapper, ._task_create_pinned_to_core = task_create_pinned_to_core_wrapper, ._task_create = task_create_wrapper, diff --git a/components/esp_wifi/include/esp_mesh.h b/components/esp_wifi/include/esp_mesh.h index d5de2eed11..4493653829 100644 --- a/components/esp_wifi/include/esp_mesh.h +++ b/components/esp_wifi/include/esp_mesh.h @@ -144,7 +144,6 @@ extern "C" { #define MESH_ASSOC_FLAG_ROOTS_FOUND (0x20) /**< roots conflict is found, means that thre are at least two roots in the mesh network */ #define MESH_ASSOC_FLAG_ROOT_FIXED (0x40) /**< the root is fixed in the mesh network */ - /** * @brief Mesh PS (Power Save) duty cycle type */ diff --git a/components/esp_wifi/include/esp_now.h b/components/esp_wifi/include/esp_now.h index 0f01054d8c..cf32e0b009 100644 --- a/components/esp_wifi/include/esp_now.h +++ b/components/esp_wifi/include/esp_now.h @@ -268,7 +268,7 @@ esp_err_t esp_now_mod_peer(const esp_now_peer_info_t *peer); * - others: failed */ esp_err_t esp_wifi_config_espnow_rate(wifi_interface_t ifx, wifi_phy_rate_t rate) - __attribute__((deprecated("This API can be only used when rate is non-HE rate, \ +__attribute__((deprecated("This API can be only used when rate is non-HE rate, \ please use esp_now_set_peer_rate_config if you want full support of the rate."))); /** diff --git a/components/esp_wifi/include/esp_private/esp_wifi_he_types_private.h b/components/esp_wifi/include/esp_private/esp_wifi_he_types_private.h index a0f8f03ebe..db5a0dcb4c 100644 --- a/components/esp_wifi/include/esp_private/esp_wifi_he_types_private.h +++ b/components/esp_wifi/include/esp_private/esp_wifi_he_types_private.h @@ -121,33 +121,33 @@ typedef struct { } __attribute__((packed)) esp_wifi_vht_siga1_t; typedef struct { - uint32_t ru_allocation :8; - uint32_t crc :4; - uint32_t tail :6; //18 bits + uint32_t ru_allocation : 8; + uint32_t crc : 4; + uint32_t tail : 6; //18 bits } esp_wifi_mu_sigb_common_t; typedef struct { - uint32_t ru_allocation :16; - uint32_t center_26tone_ru :1; - uint32_t crc :4; - uint32_t tail :6; //not included into the sigb_common_info (21bits) + uint32_t ru_allocation : 16; + uint32_t center_26tone_ru : 1; + uint32_t crc : 4; + uint32_t tail : 6; //not included into the sigb_common_info (21bits) } esp_wifi_mu_sigb_common_80mhz_ppdu_t; typedef struct { - uint32_t sta_id :11; - uint32_t nsts :3; - uint32_t beamformed :1; - uint32_t he_mcs :4; - uint32_t dcm :1; - uint32_t coding :1; + uint32_t sta_id : 11; + uint32_t nsts : 3; + uint32_t beamformed : 1; + uint32_t he_mcs : 4; + uint32_t dcm : 1; + uint32_t coding : 1; } esp_wifi_mu_sigb_user_non_mimo_t; typedef struct { - uint32_t sta_id :11; - uint32_t spatial_config :4; - uint32_t he_mcs :4; - uint32_t rsvd :1; - uint32_t coding :1; + uint32_t sta_id : 11; + uint32_t spatial_config : 4; + uint32_t he_mcs : 4; + uint32_t rsvd : 1; + uint32_t coding : 1; } esp_wifi_mu_sigb_user_mimo_t; #define ESP_TEST_RX_MU_USER_NUM (9) diff --git a/components/esp_wifi/include/esp_private/wifi.h b/components/esp_wifi/include/esp_private/wifi.h index 12c4122f94..880dd83076 100644 --- a/components/esp_wifi/include/esp_private/wifi.h +++ b/components/esp_wifi/include/esp_private/wifi.h @@ -15,7 +15,6 @@ * */ - #ifndef __ESP_WIFI_INTERNAL_H__ #define __ESP_WIFI_INTERNAL_H__ @@ -46,7 +45,7 @@ typedef struct { */ typedef enum { WIFI_LOG_NONE = 0, - WIFI_LOG_ERROR , /*enabled by default*/ + WIFI_LOG_ERROR, /*enabled by default*/ WIFI_LOG_WARNING, /*enabled by default*/ WIFI_LOG_INFO, /*enabled by default*/ WIFI_LOG_DEBUG, /*can be set in menuconfig*/ @@ -74,7 +73,6 @@ typedef enum { #define WIFI_LOG_SUBMODULE_CONN (1<<2) /*logs related to connecting*/ #define WIFI_LOG_SUBMODULE_SCAN (1<<3) /*logs related to scanning*/ - /** * @brief Initialize Wi-Fi Driver * Alloc resource for WiFi driver, such as WiFi control structure, RX/TX buffer, @@ -215,7 +213,6 @@ esp_err_t esp_wifi_internal_wapi_deinit(void); */ esp_err_t esp_wifi_internal_reg_netstack_buf_cb(wifi_netstack_buf_ref_cb_t ref, wifi_netstack_buf_free_cb_t free); - /** * @brief The WiFi RX callback function * @@ -370,7 +367,7 @@ esp_err_t esp_wifi_internal_esp_wifi_he_md5_check(const char *md5); * * @return A pointer to the memory allocated on success, NULL on failure */ -void *wifi_malloc( size_t size ); +void *wifi_malloc(size_t size); /** * @brief Reallocate a chunk of memory for WiFi driver @@ -382,7 +379,7 @@ void *wifi_malloc( size_t size ); * * @return A pointer to the memory allocated on success, NULL on failure */ -void *wifi_realloc( void *ptr, size_t size ); +void *wifi_realloc(void *ptr, size_t size); /** * @brief Callocate memory for WiFi driver @@ -394,7 +391,7 @@ void *wifi_realloc( void *ptr, size_t size ); * * @return A pointer to the memory allocated on success, NULL on failure */ -void *wifi_calloc( size_t n, size_t size ); +void *wifi_calloc(size_t n, size_t size); /** * @brief Update WiFi MAC time @@ -403,7 +400,7 @@ void *wifi_calloc( size_t n, size_t size ); * * @return Always returns ESP_OK */ -typedef esp_err_t (* wifi_mac_time_update_cb_t)( uint32_t time_delta ); +typedef esp_err_t (* wifi_mac_time_update_cb_t)(uint32_t time_delta); /** * @brief Update WiFi MAC time @@ -412,7 +409,7 @@ typedef esp_err_t (* wifi_mac_time_update_cb_t)( uint32_t time_delta ); * * @return Always returns ESP_OK */ -esp_err_t esp_wifi_internal_update_mac_time( uint32_t time_delta ); +esp_err_t esp_wifi_internal_update_mac_time(uint32_t time_delta); /** * @brief Set current WiFi log level @@ -539,7 +536,6 @@ void esp_wifi_power_domain_on(void); */ void esp_wifi_power_domain_off(void); - #if (CONFIG_FREERTOS_USE_TICKLESS_IDLE && SOC_PM_MODEM_RETENTION_BY_REGDMA) /** * @brief Get wifi mac sleep retention hardware context configuration and size diff --git a/components/esp_wifi/include/esp_wifi.h b/components/esp_wifi/include/esp_wifi.h index ed9ce40c60..0b99716522 100644 --- a/components/esp_wifi/include/esp_wifi.h +++ b/components/esp_wifi/include/esp_wifi.h @@ -4,7 +4,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - /* Notes about WiFi Programming * * WiFi programming model can be depicted as following picture: @@ -561,7 +560,6 @@ esp_err_t esp_wifi_scan_get_ap_record(wifi_ap_record_t *ap_record); */ esp_err_t esp_wifi_clear_ap_list(void); - /** * @brief Get information of AP to which the device is associated with * @@ -749,7 +747,6 @@ esp_err_t esp_wifi_set_country(const wifi_country_t *country); */ esp_err_t esp_wifi_get_country(wifi_country_t *country); - /** * @brief Set MAC address of WiFi station, soft-AP or NAN interface. * @@ -983,7 +980,7 @@ esp_err_t esp_wifi_set_storage(wifi_storage_t storage); * @param vnd_ie Pointer to the vendor specific element data received. * @param rssi Received signal strength indication. */ -typedef void (*esp_vendor_ie_cb_t) (void *ctx, wifi_vendor_ie_type_t type, const uint8_t sa[6], const vendor_ie_data_t *vnd_ie, int rssi); +typedef void (*esp_vendor_ie_cb_t)(void *ctx, wifi_vendor_ie_type_t type, const uint8_t sa[6], const vendor_ie_data_t *vnd_ie, int rssi); /** * @brief Set 802.11 Vendor-Specific Information Element @@ -1115,7 +1112,6 @@ esp_err_t esp_wifi_80211_tx(wifi_interface_t ifx, const void *buffer, int len, b */ typedef void (* wifi_csi_cb_t)(void *ctx, wifi_csi_info_t *data); - /** * @brief Register the RX callback function of CSI data. * @@ -1194,7 +1190,6 @@ esp_err_t esp_wifi_set_ant_gpio(const wifi_ant_gpio_config_t *config) __attribut */ esp_err_t esp_wifi_get_ant_gpio(wifi_ant_gpio_config_t *config) __attribute__((deprecated("Please use esp_phy_get_ant_gpio instead"))); - /** * @brief Set antenna configuration * diff --git a/components/esp_wifi/include/esp_wifi_ap_get_sta_list.h b/components/esp_wifi/include/esp_wifi_ap_get_sta_list.h index f3c43a4733..812c4ea04d 100644 --- a/components/esp_wifi/include/esp_wifi_ap_get_sta_list.h +++ b/components/esp_wifi/include/esp_wifi_ap_get_sta_list.h @@ -13,7 +13,6 @@ extern "C" { #endif - #ifndef ESP_WIFI_MAX_CONN_NUM // Number of maximum wifi connection may be undefined if we have no native wifi support on this target // and at the same time there's no native interface injected by the wifi_remote component. diff --git a/components/esp_wifi/include/esp_wifi_crypto_types.h b/components/esp_wifi/include/esp_wifi_crypto_types.h index 1fae0297e5..c8601e718d 100644 --- a/components/esp_wifi/include/esp_wifi_crypto_types.h +++ b/components/esp_wifi/include/esp_wifi_crypto_types.h @@ -1,10 +1,9 @@ /* - * SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ - #ifndef __ESP_WIFI_CRYPTO_TYPES_H__ #define __ESP_WIFI_CRYPTO_TYPES_H__ @@ -33,7 +32,7 @@ typedef enum { ESP_CRYPTO_HASH_ALG_MD5, ESP_CRYPTO_HASH_ALG_SHA1, ESP_CRYPTO_HASH_ALG_HMAC_MD5, ESP_CRYPTO_HASH_ALG_HMAC_SHA1, ESP_CRYPTO_HASH_ALG_SHA256, ESP_CRYPTO_HASH_ALG_HMAC_SHA256 -}esp_crypto_hash_alg_t; +} esp_crypto_hash_alg_t; /* * Enumeration for block cipher operations. @@ -112,7 +111,7 @@ typedef int (*esp_aes_unwrap_t)(const unsigned char *kek, int n, const unsigned * */ typedef int (*esp_hmac_sha256_vector_t)(const unsigned char *key, int key_len, int num_elem, - const unsigned char *addr[], const int *len, unsigned char *mac); + const unsigned char *addr[], const int *len, unsigned char *mac); /** * @brief The SHA256 PRF callback function used by esp_wifi. @@ -127,7 +126,7 @@ typedef int (*esp_hmac_sha256_vector_t)(const unsigned char *key, int key_len, i * */ typedef int (*esp_sha256_prf_t)(const unsigned char *key, int key_len, const char *label, - const unsigned char *data, int data_len, unsigned char *buf, int buf_len); + const unsigned char *data, int data_len, unsigned char *buf, int buf_len); /** * @brief HMAC-MD5 callback function over data buffer (RFC 2104)' @@ -154,7 +153,7 @@ typedef int (*esp_hmac_md5_t)(const unsigned char *key, unsigned int key_len, co * Returns: 0 on success, -1 on failure */ typedef int (*esp_hmac_md5_vector_t)(const unsigned char *key, unsigned int key_len, unsigned int num_elem, - const unsigned char *addr[], const unsigned int *len, unsigned char *mac); + const unsigned char *addr[], const unsigned int *len, unsigned char *mac); /** * @brief HMAC-SHA1 callback function over data buffer (RFC 2104) @@ -167,7 +166,7 @@ typedef int (*esp_hmac_md5_vector_t)(const unsigned char *key, unsigned int key_ * Returns: 0 on success, -1 of failure */ typedef int (*esp_hmac_sha1_t)(const unsigned char *key, unsigned int key_len, const unsigned char *data, - unsigned int data_len, unsigned char *mac); + unsigned int data_len, unsigned char *mac); /** * @brief HMAC-SHA1 callback function over data vector (RFC 2104) @@ -181,7 +180,7 @@ typedef int (*esp_hmac_sha1_t)(const unsigned char *key, unsigned int key_len, c * Returns: 0 on success, -1 on failure */ typedef int (*esp_hmac_sha1_vector_t)(const unsigned char *key, unsigned int key_len, unsigned int num_elem, - const unsigned char *addr[], const unsigned int *len, unsigned char *mac); + const unsigned char *addr[], const unsigned int *len, unsigned char *mac); /** * @brief SHA1-based Pseudo-Random Function (PRF) (IEEE 802.11i, 8.5.1.1) callback function @@ -211,7 +210,7 @@ typedef int (*esp_sha1_prf_t)(const unsigned char *key, unsigned int key_len, co * Returns: 0 on success, -1 on failure */ typedef int (*esp_sha1_vector_t)(unsigned int num_elem, const unsigned char *addr[], const unsigned int *len, - unsigned char *mac); + unsigned char *mac); /** * @brief SHA1-based key derivation function (PBKDF2) callback function for IEEE 802.11i @@ -229,7 +228,7 @@ typedef int (*esp_sha1_vector_t)(unsigned int num_elem, const unsigned char *add * IEEE Std 802.11-2004, Clause H.4. The main construction is from PKCS#5 v2.0. */ typedef int (*esp_pbkdf2_sha1_t)(const char *passphrase, const char *ssid, unsigned int ssid_len, - int iterations, unsigned char *buf, unsigned int buflen); + int iterations, unsigned char *buf, unsigned int buflen); /** * @brief XOR RC4 stream callback function to given data with skip-stream-start @@ -258,7 +257,7 @@ typedef int (*esp_rc4_skip_t)(const unsigned char *key, unsigned int keylen, uns * Returns: 0 on success, -1 on failure */ typedef int (*esp_md5_vector_t)(unsigned int num_elem, const unsigned char *addr[], const unsigned int *len, - unsigned char *mac); + unsigned char *mac); /** * @brief Encrypt one AES block callback function @@ -423,14 +422,14 @@ typedef struct wpa_crypto_funcs_t { esp_aes_gmac_t aes_gmac; /**< One-Key GMAC hash callback function with AES for MIC computation */ esp_sha256_vector_t sha256_vector; /**< SHA256 hash callback function for data vector */ esp_crc32_le_t crc32; /**< CRC32 value callback function in little endian */ -}wpa_crypto_funcs_t; +} wpa_crypto_funcs_t; /** * @brief The crypto callback function structure used in mesh vendor IE encryption. The * structure can be set as software crypto or the crypto optimized by device's * hardware. */ -typedef struct{ +typedef struct { esp_aes_128_encrypt_t aes_128_encrypt; /**< Callback function used in mesh vendor IE encryption */ esp_aes_128_decrypt_t aes_128_decrypt; /**< Callback function used in mesh vendor IE decryption */ } mesh_crypto_funcs_t; diff --git a/components/esp_wifi/include/esp_wifi_he.h b/components/esp_wifi/include/esp_wifi_he.h index bb2c2ca8c4..5bc361c2f8 100644 --- a/components/esp_wifi/include/esp_wifi_he.h +++ b/components/esp_wifi/include/esp_wifi_he.h @@ -15,7 +15,6 @@ extern "C" { #endif - /** * @brief Set up an individual TWT agreement (NegotiationType=0) or change TWT parameters of the existing TWT agreement * - TWT Wake Interval = TWT Wake Interval Mantissa * (2 ^ TWT Wake Interval Exponent), unit: us diff --git a/components/esp_wifi/include/esp_wifi_he_types.h b/components/esp_wifi/include/esp_wifi_he_types.h index ebba898e08..0baf7a66d1 100644 --- a/components/esp_wifi/include/esp_wifi_he_types.h +++ b/components/esp_wifi/include/esp_wifi_he_types.h @@ -114,17 +114,16 @@ typedef enum { /** * @brief TWT setup config */ -typedef struct -{ +typedef struct { wifi_twt_setup_cmds_t setup_cmd; /**< Indicates the type of TWT command */ - uint16_t trigger :1; /**< 1: a trigger-enabled TWT, 0: a non-trigger-enabled TWT */ - uint16_t flow_type :1; /**< 0: an announced TWT, 1: an unannounced TWT */ - uint16_t flow_id :3; /**< When set up an individual TWT agreement, the flow id will be assigned by AP after a successful agreement setup. + uint16_t trigger : 1; /**< 1: a trigger-enabled TWT, 0: a non-trigger-enabled TWT */ + uint16_t flow_type : 1; /**< 0: an announced TWT, 1: an unannounced TWT */ + uint16_t flow_id : 3; /**< When set up an individual TWT agreement, the flow id will be assigned by AP after a successful agreement setup. flow_id could be specified to a value in the range of [0, 7], but it might be changed by AP in the response. When change TWT parameters of the existing TWT agreement, flow_id should be an existing one. The value range is [0, 7]. */ - uint16_t wake_invl_expn :5; /**< TWT Wake Interval Exponent. The value range is [0, 31]. */ - uint16_t wake_duration_unit :1; /**< TWT Wake duration unit, 0: 256us 1: TU (TU = 1024us)*/ - uint16_t reserved :5; /**< bit: 11.15 reserved */ + uint16_t wake_invl_expn : 5; /**< TWT Wake Interval Exponent. The value range is [0, 31]. */ + uint16_t wake_duration_unit : 1; /**< TWT Wake duration unit, 0: 256us 1: TU (TU = 1024us)*/ + uint16_t reserved : 5; /**< bit: 11.15 reserved */ uint8_t min_wake_dura; /**< Nominal Minimum Wake Duration, indicates the minimum amount of time, in unit of 256 us, that the TWT requesting STA expects that it needs to be awake. The value range is [1, 255]. */ uint16_t wake_invl_mant; /**< TWT Wake Interval Mantissa. The value range is [1, 65535]. */ uint16_t twt_id; /**< TWT connection id, the value range is [0, 32767]. */ @@ -161,38 +160,38 @@ typedef enum { */ #if CONFIG_IDF_TARGET_ESP32C5 typedef struct { - signed rssi:8; /**< the RSSI of the reception frame */ - unsigned rate:5; /**< if cur_bb_format is RX_BB_FORMAT_11B, it's the transmission rate. otherwise it's Rate field of L-SIG */ + signed rssi: 8; /**< the RSSI of the reception frame */ + unsigned rate: 5; /**< if cur_bb_format is RX_BB_FORMAT_11B, it's the transmission rate. otherwise it's Rate field of L-SIG */ unsigned : 1; /**< reserved */ unsigned : 2; /**< reserved */ unsigned : 12; /**< reserved */ - unsigned rxmatch0:1; /**< indicate whether the reception frame is from interface 0 */ - unsigned rxmatch1:1; /**< indicate whether the reception frame is from interface 1 */ - unsigned rxmatch2:1; /**< indicate whether the reception frame is from interface 2 */ - unsigned rxmatch3:1; /**< indicate whether the reception frame is from interface 3 */ + unsigned rxmatch0: 1; /**< indicate whether the reception frame is from interface 0 */ + unsigned rxmatch1: 1; /**< indicate whether the reception frame is from interface 1 */ + unsigned rxmatch2: 1; /**< indicate whether the reception frame is from interface 2 */ + unsigned rxmatch3: 1; /**< indicate whether the reception frame is from interface 3 */ uint32_t he_siga1; /**< HE-SIGA1 or HT-SIG or VHT-SIG */ - unsigned rxend_state:8; /**< reception state, 0: successful, others: failure */ + unsigned rxend_state: 8; /**< reception state, 0: successful, others: failure */ uint16_t he_siga2; /**< HE-SIGA2 */ unsigned : 7; /**< reserved */ - unsigned is_group:1; /**< indicate whether the reception is a group addressed frame */ - unsigned timestamp:32; /**< timestamp. The local time when this packet is received. It is precise only if modem sleep or light sleep is not enabled. unit: microsecond */ + unsigned is_group: 1; /**< indicate whether the reception is a group addressed frame */ + unsigned timestamp: 32; /**< timestamp. The local time when this packet is received. It is precise only if modem sleep or light sleep is not enabled. unit: microsecond */ unsigned : 15; /**< reserved */ unsigned : 15; /**< reserved */ unsigned : 2; /**< reserved */ - unsigned noise_floor:8; /**< the noise floor of the reception frame */ + unsigned noise_floor: 8; /**< the noise floor of the reception frame */ signed : 8; /**< reserved */ signed : 8; /**< reserved */ unsigned : 8; /**< reserved */ unsigned : 8; /**< reserved */ unsigned : 8; /**< reserved */ unsigned : 2; /**< reserved */ - unsigned sigb_len:10; /**< the sigb length */ + unsigned sigb_len: 10; /**< the sigb length */ unsigned : 1; /**< reserved */ unsigned : 1; /**< reserved */ unsigned : 1; /**< reserved */ unsigned : 1; /**< reserved */ - unsigned channel:4; /**< the primary channel */ - unsigned second:4; /**< the second channel if in HT40 */ + unsigned channel: 4; /**< the primary channel */ + unsigned second: 4; /**< the second channel if in HT40 */ unsigned : 12; /**< reserved */ unsigned : 4; /**< reserved */ unsigned : 1; /**< reserved */ @@ -204,9 +203,9 @@ typedef struct { unsigned : 1; /**< reserved */ unsigned : 12; /**< reserved */ unsigned : 12; /**< reserved */ - unsigned cur_bb_format:4; /**< the format of the reception frame */ - unsigned rx_channel_estimate_len:10; /**< the length of the channel information */ - unsigned rx_channel_estimate_info_vld:1; /**< indicate the channel information is valid */ + unsigned cur_bb_format: 4; /**< the format of the reception frame */ + unsigned rx_channel_estimate_len: 10; /**< the length of the channel information */ + unsigned rx_channel_estimate_info_vld: 1; /**< indicate the channel information is valid */ unsigned : 5; /**< reserved */ unsigned : 21; /**< reserved */ unsigned : 10; /**< reserved */ @@ -221,11 +220,11 @@ typedef struct { unsigned : 1; /**< reserved */ unsigned : 8; /**< reserved */ unsigned : 16; /**< reserved */ - unsigned sig_len:14; /**< the length of the reception MPDU */ + unsigned sig_len: 14; /**< the length of the reception MPDU */ unsigned : 2; /**< reserved */ - unsigned dump_len:14; /**< the length of the reception MPDU excluding the FCS */ + unsigned dump_len: 14; /**< the length of the reception MPDU excluding the FCS */ unsigned : 2; /**< reserved */ - unsigned rx_state:8; /**< reception state, 0: successful, others: failure */ + unsigned rx_state: 8; /**< reception state, 0: successful, others: failure */ unsigned : 8; /**< reserved */ unsigned : 16; /**< reserved */ } __attribute__((packed)) esp_wifi_rxctrl_t; diff --git a/components/esp_wifi/include/esp_wifi_types_generic.h b/components/esp_wifi/include/esp_wifi_types_generic.h index 1d8abce68c..e09506ec56 100644 --- a/components/esp_wifi/include/esp_wifi_types_generic.h +++ b/components/esp_wifi/include/esp_wifi_types_generic.h @@ -4,7 +4,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - #ifndef __ESP_WIFI_TYPES_H__ #define __ESP_WIFI_TYPES_H__ @@ -209,9 +208,9 @@ typedef enum { /** @brief Description of a WiFi AP HE Info */ typedef struct { - uint8_t bss_color:6; /**< an unsigned integer whose value is the BSS Color of the BSS corresponding to the AP */ - uint8_t partial_bss_color:1; /**< indicate if an AID assignment rule based on the BSS color */ - uint8_t bss_color_disabled:1; /**< indicate if the use of BSS color is disabled */ + uint8_t bss_color: 6; /**< an unsigned integer whose value is the BSS Color of the BSS corresponding to the AP */ + uint8_t partial_bss_color: 1; /**< indicate if an AID assignment rule based on the BSS color */ + uint8_t bss_color_disabled: 1; /**< indicate if the use of BSS color is disabled */ uint8_t bssid_index; /**< in M-BSSID set, identifies the nontransmitted BSSID */ } wifi_he_ap_info_t; @@ -226,17 +225,17 @@ typedef struct { wifi_cipher_type_t pairwise_cipher; /**< pairwise cipher of AP */ wifi_cipher_type_t group_cipher; /**< group cipher of AP */ wifi_ant_t ant; /**< antenna used to receive beacon from AP */ - uint32_t phy_11b:1; /**< bit: 0 flag to identify if 11b mode is enabled or not */ - uint32_t phy_11g:1; /**< bit: 1 flag to identify if 11g mode is enabled or not */ - uint32_t phy_11n:1; /**< bit: 2 flag to identify if 11n mode is enabled or not */ - uint32_t phy_lr:1; /**< bit: 3 flag to identify if low rate is enabled or not */ - uint32_t phy_11a:1; /**< bit: 4 flag to identify if 11ax mode is enabled or not */ - uint32_t phy_11ac:1; /**< bit: 5 flag to identify if 11ax mode is enabled or not */ - uint32_t phy_11ax:1; /**< bit: 6 flag to identify if 11ax mode is enabled or not */ - uint32_t wps:1; /**< bit: 7 flag to identify if WPS is supported or not */ - uint32_t ftm_responder:1; /**< bit: 8 flag to identify if FTM is supported in responder mode */ - uint32_t ftm_initiator:1; /**< bit: 9 flag to identify if FTM is supported in initiator mode */ - uint32_t reserved:22; /**< bit: 10..31 reserved */ + uint32_t phy_11b: 1; /**< bit: 0 flag to identify if 11b mode is enabled or not */ + uint32_t phy_11g: 1; /**< bit: 1 flag to identify if 11g mode is enabled or not */ + uint32_t phy_11n: 1; /**< bit: 2 flag to identify if 11n mode is enabled or not */ + uint32_t phy_lr: 1; /**< bit: 3 flag to identify if low rate is enabled or not */ + uint32_t phy_11a: 1; /**< bit: 4 flag to identify if 11ax mode is enabled or not */ + uint32_t phy_11ac: 1; /**< bit: 5 flag to identify if 11ax mode is enabled or not */ + uint32_t phy_11ax: 1; /**< bit: 6 flag to identify if 11ax mode is enabled or not */ + uint32_t wps: 1; /**< bit: 7 flag to identify if WPS is supported or not */ + uint32_t ftm_responder: 1; /**< bit: 8 flag to identify if FTM is supported in responder mode */ + uint32_t ftm_initiator: 1; /**< bit: 9 flag to identify if FTM is supported in initiator mode */ + uint32_t reserved: 22; /**< bit: 10..31 reserved */ wifi_country_t country; /**< country information of AP */ wifi_he_ap_info_t he_ap; /**< HE AP info */ uint8_t bandwidth; /**< For either 20 MHz or 40 MHz operation, the Channel Width field is set to 0. @@ -252,19 +251,19 @@ typedef struct { typedef enum { WIFI_FAST_SCAN = 0, /**< Do fast scan, scan will end after find SSID match AP */ WIFI_ALL_CHANNEL_SCAN, /**< All channel scan, scan will end after scan all the channel */ -}wifi_scan_method_t; +} wifi_scan_method_t; typedef enum { WIFI_CONNECT_AP_BY_SIGNAL = 0, /**< Sort match AP in scan list by RSSI */ WIFI_CONNECT_AP_BY_SECURITY, /**< Sort match AP in scan list by security mode */ -}wifi_sort_method_t; +} wifi_sort_method_t; /** @brief Structure describing parameters for a WiFi fast scan */ typedef struct { int8_t rssi; /**< The minimum rssi to accept in the fast scan mode */ wifi_auth_mode_t authmode; /**< The weakest authmode to accept in the fast scan mode Note: In case this value is not set and password is set as per WPA2 standards(password len >= 8), it will be defaulted to WPA2 and device won't connect to deprecated WEP/WPA networks. Please set authmode threshold as WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK to connect to WEP/WPA networks */ -}wifi_scan_threshold_t; +} wifi_scan_threshold_t; typedef enum { WIFI_PS_NONE, /**< No power save */ @@ -340,26 +339,26 @@ typedef struct { wifi_sort_method_t sort_method; /**< sort the connect AP in the list by rssi or security mode */ wifi_scan_threshold_t threshold; /**< When scan_threshold is set, only APs which have an auth mode that is more secure than the selected auth mode and a signal stronger than the minimum RSSI will be used. */ wifi_pmf_config_t pmf_cfg; /**< Configuration for Protected Management Frame. Will be advertised in RSN Capabilities in RSN IE. */ - uint32_t rm_enabled:1; /**< Whether Radio Measurements are enabled for the connection */ - uint32_t btm_enabled:1; /**< Whether BSS Transition Management is enabled for the connection. Note that when btm is enabled, the application itself should not set specific bssid (i.e using bssid_set and bssid in this config)or channel to connect to. This defeats the purpose of a BTM supported network, and hence if btm is supported and a specific bssid or channel is set in this config, it will be cleared from the config at the first disconnection or connection so that the device can roam to other BSS. It is recommended not to set BSSID when BTM is enabled. */ - uint32_t mbo_enabled:1; /**< Whether MBO is enabled for the connection. Note that when mbo is enabled, the application itself should not set specific bssid (i.e using bssid_set and bssid in this config)or channel to connect to. This defeats the purpose of a MBO supported network, and hence if btm is supported and a specific bssid or channel is set in this config, it will be cleared from the config at the first disconnection or connection so that the device can roam to other BSS. It is recommended not to set BSSID when MBO is enabled. Enabling mbo here, automatically enables btm and rm above.*/ - uint32_t ft_enabled:1; /**< Whether FT is enabled for the connection */ - uint32_t owe_enabled:1; /**< Whether OWE is enabled for the connection */ - uint32_t transition_disable:1; /**< Whether to enable transition disable feature */ - uint32_t reserved:26; /**< Reserved for future feature set */ + uint32_t rm_enabled: 1; /**< Whether Radio Measurements are enabled for the connection */ + uint32_t btm_enabled: 1; /**< Whether BSS Transition Management is enabled for the connection. Note that when btm is enabled, the application itself should not set specific bssid (i.e using bssid_set and bssid in this config)or channel to connect to. This defeats the purpose of a BTM supported network, and hence if btm is supported and a specific bssid or channel is set in this config, it will be cleared from the config at the first disconnection or connection so that the device can roam to other BSS. It is recommended not to set BSSID when BTM is enabled. */ + uint32_t mbo_enabled: 1; /**< Whether MBO is enabled for the connection. Note that when mbo is enabled, the application itself should not set specific bssid (i.e using bssid_set and bssid in this config)or channel to connect to. This defeats the purpose of a MBO supported network, and hence if btm is supported and a specific bssid or channel is set in this config, it will be cleared from the config at the first disconnection or connection so that the device can roam to other BSS. It is recommended not to set BSSID when MBO is enabled. Enabling mbo here, automatically enables btm and rm above.*/ + uint32_t ft_enabled: 1; /**< Whether FT is enabled for the connection */ + uint32_t owe_enabled: 1; /**< Whether OWE is enabled for the connection */ + uint32_t transition_disable: 1; /**< Whether to enable transition disable feature */ + uint32_t reserved: 26; /**< Reserved for future feature set */ wifi_sae_pwe_method_t sae_pwe_h2e; /**< Configuration for SAE PWE derivation method */ wifi_sae_pk_mode_t sae_pk_mode; /**< Configuration for SAE-PK (Public Key) Authentication method */ uint8_t failure_retry_cnt; /**< Number of connection retries station will do before moving to next AP. scan_method should be set as WIFI_ALL_CHANNEL_SCAN to use this config. Note: Enabling this may cause connection time to increase in case best AP doesn't behave properly. */ - uint32_t he_dcm_set:1; /**< Whether DCM max.constellation for transmission and reception is set. */ - uint32_t he_dcm_max_constellation_tx:2; /**< Indicate the max.constellation for DCM in TB PPDU the STA supported. 0: not supported. 1: BPSK, 2: QPSK, 3: 16-QAM. The default value is 3. */ - uint32_t he_dcm_max_constellation_rx:2; /**< Indicate the max.constellation for DCM in both Data field and HE-SIG-B field the STA supported. 0: not supported. 1: BPSK, 2: QPSK, 3: 16-QAM. The default value is 3. */ - uint32_t he_mcs9_enabled:1; /**< Whether to support HE-MCS 0 to 9. The default value is 0. */ - uint32_t he_su_beamformee_disabled:1; /**< Whether to disable support for operation as an SU beamformee. */ - uint32_t he_trig_su_bmforming_feedback_disabled:1; /**< Whether to disable support the transmission of SU feedback in an HE TB sounding sequence. */ - uint32_t he_trig_mu_bmforming_partial_feedback_disabled:1; /**< Whether to disable support the transmission of partial-bandwidth MU feedback in an HE TB sounding sequence. */ - uint32_t he_trig_cqi_feedback_disabled:1; /**< Whether to disable support the transmission of CQI feedback in an HE TB sounding sequence. */ - uint32_t he_reserved:22; /**< Reserved for future feature set */ + uint32_t he_dcm_set: 1; /**< Whether DCM max.constellation for transmission and reception is set. */ + uint32_t he_dcm_max_constellation_tx: 2; /**< Indicate the max.constellation for DCM in TB PPDU the STA supported. 0: not supported. 1: BPSK, 2: QPSK, 3: 16-QAM. The default value is 3. */ + uint32_t he_dcm_max_constellation_rx: 2; /**< Indicate the max.constellation for DCM in both Data field and HE-SIG-B field the STA supported. 0: not supported. 1: BPSK, 2: QPSK, 3: 16-QAM. The default value is 3. */ + uint32_t he_mcs9_enabled: 1; /**< Whether to support HE-MCS 0 to 9. The default value is 0. */ + uint32_t he_su_beamformee_disabled: 1; /**< Whether to disable support for operation as an SU beamformee. */ + uint32_t he_trig_su_bmforming_feedback_disabled: 1; /**< Whether to disable support the transmission of SU feedback in an HE TB sounding sequence. */ + uint32_t he_trig_mu_bmforming_partial_feedback_disabled: 1; /**< Whether to disable support the transmission of partial-bandwidth MU feedback in an HE TB sounding sequence. */ + uint32_t he_trig_cqi_feedback_disabled: 1; /**< Whether to disable support the transmission of CQI feedback in an HE TB sounding sequence. */ + uint32_t he_reserved: 22; /**< Reserved for future feature set */ uint8_t sae_h2e_identifier[SAE_H2E_IDENTIFIER_LEN];/**< Password identifier for H2E. this needs to be null terminated string */ } wifi_sta_config_t; @@ -390,15 +389,15 @@ typedef union { typedef struct { uint8_t mac[6]; /**< mac address */ int8_t rssi; /**< current average rssi of sta connected */ - uint32_t phy_11b:1; /**< bit: 0 flag to identify if 11b mode is enabled or not */ - uint32_t phy_11g:1; /**< bit: 1 flag to identify if 11g mode is enabled or not */ - uint32_t phy_11n:1; /**< bit: 2 flag to identify if 11n mode is enabled or not */ - uint32_t phy_lr:1; /**< bit: 3 flag to identify if low rate is enabled or not */ - uint32_t phy_11a:1; /**< bit: 4 flag to identify if 11ax mode is enabled or not */ - uint32_t phy_11ac:1; /**< bit: 5 flag to identify if 11ax mode is enabled or not */ - uint32_t phy_11ax:1; /**< bit: 6 flag to identify if 11ax mode is enabled or not */ - uint32_t is_mesh_child:1;/**< bit: 7 flag to identify mesh child */ - uint32_t reserved:24; /**< bit: 8..31 reserved */ + uint32_t phy_11b: 1; /**< bit: 0 flag to identify if 11b mode is enabled or not */ + uint32_t phy_11g: 1; /**< bit: 1 flag to identify if 11g mode is enabled or not */ + uint32_t phy_11n: 1; /**< bit: 2 flag to identify if 11n mode is enabled or not */ + uint32_t phy_lr: 1; /**< bit: 3 flag to identify if low rate is enabled or not */ + uint32_t phy_11a: 1; /**< bit: 4 flag to identify if 11ax mode is enabled or not */ + uint32_t phy_11ac: 1; /**< bit: 5 flag to identify if 11ax mode is enabled or not */ + uint32_t phy_11ax: 1; /**< bit: 6 flag to identify if 11ax mode is enabled or not */ + uint32_t is_mesh_child: 1; /**< bit: 7 flag to identify mesh child */ + uint32_t reserved: 24; /**< bit: 8..31 reserved */ } wifi_sta_info_t; typedef enum { @@ -434,8 +433,7 @@ typedef enum { /** * @brief Operation Phymode */ -typedef enum -{ +typedef enum { WIFI_PHY_MODE_LR, /**< PHY mode for Low Rate */ WIFI_PHY_MODE_11B, /**< PHY mode for 11b */ WIFI_PHY_MODE_11G, /**< PHY mode for 11g */ @@ -472,7 +470,6 @@ typedef enum { WIFI_PKT_MISC, /**< Other type, such as MIMO etc. 'buf' argument is wifi_promiscuous_pkt_t but the payload is zero length. */ } wifi_promiscuous_pkt_type_t; - #define WIFI_PROMIS_FILTER_MASK_ALL (0xFFFFFFFF) /**< filter all packets */ #define WIFI_PROMIS_FILTER_MASK_MGMT (1) /**< filter the packets with type of WIFI_PKT_MGMT */ #define WIFI_PROMIS_FILTER_MASK_CTRL (1<<1) /**< filter the packets with type of WIFI_PKT_CTRL */ @@ -616,9 +613,9 @@ typedef struct { wifi_nan_service_type_t type; /**< Service type */ char matching_filter[ESP_WIFI_MAX_FILTER_LEN]; /**< Comma separated filters for filtering services */ char svc_info[ESP_WIFI_MAX_SVC_INFO_LEN]; /**< Service info shared in Publish frame */ - uint8_t single_replied_event:1; /**< Give single Replied event or every time */ - uint8_t datapath_reqd:1; /**< NAN Datapath required for the service */ - uint8_t reserved:6; /**< Reserved */ + uint8_t single_replied_event: 1; /**< Give single Replied event or every time */ + uint8_t datapath_reqd: 1; /**< NAN Datapath required for the service */ + uint8_t reserved: 6; /**< Reserved */ } wifi_nan_publish_cfg_t; /** @@ -630,8 +627,8 @@ typedef struct { wifi_nan_service_type_t type; /**< Service type */ char matching_filter[ESP_WIFI_MAX_FILTER_LEN]; /**< Comma separated filters for filtering services */ char svc_info[ESP_WIFI_MAX_SVC_INFO_LEN]; /**< Service info shared in Subscribe frame */ - uint8_t single_match_event:1; /**< Give single Match event or every time */ - uint8_t reserved:7; /**< Reserved */ + uint8_t single_match_event: 1; /**< Give single Match event or every time */ + uint8_t reserved: 7; /**< Reserved */ } wifi_nan_subscribe_cfg_t; /** diff --git a/components/esp_wifi/include/local/esp_wifi_types_native.h b/components/esp_wifi/include/local/esp_wifi_types_native.h index 13206cccd5..76a45d25b8 100644 --- a/components/esp_wifi/include/local/esp_wifi_types_native.h +++ b/components/esp_wifi/include/local/esp_wifi_types_native.h @@ -35,54 +35,54 @@ typedef esp_wifi_rxctrl_t wifi_pkt_rx_ctrl_t; #else /** @brief Received packet radio metadata header, this is the common header at the beginning of all promiscuous mode RX callback buffers */ typedef struct { - signed rssi:8; /**< Received Signal Strength Indicator(RSSI) of packet. unit: dBm */ - unsigned rate:5; /**< PHY rate encoding of the packet. Only valid for non HT(11bg) packet */ - unsigned :1; /**< reserved */ - unsigned sig_mode:2; /**< Protocol of the received packet, 0: non HT(11bg) packet; 1: HT(11n) packet; 3: VHT(11ac) packet */ - unsigned :16; /**< reserved */ - unsigned mcs:7; /**< Modulation Coding Scheme. If is HT(11n) packet, shows the modulation, range from 0 to 76(MSC0 ~ MCS76) */ - unsigned cwb:1; /**< Channel Bandwidth of the packet. 0: 20MHz; 1: 40MHz */ - unsigned :16; /**< reserved */ - unsigned smoothing:1; /**< Set to 1 indicates that channel estimate smoothing is recommended. + signed rssi: 8; /**< Received Signal Strength Indicator(RSSI) of packet. unit: dBm */ + unsigned rate: 5; /**< PHY rate encoding of the packet. Only valid for non HT(11bg) packet */ + unsigned : 1; /**< reserved */ + unsigned sig_mode: 2; /**< Protocol of the received packet, 0: non HT(11bg) packet; 1: HT(11n) packet; 3: VHT(11ac) packet */ + unsigned : 16; /**< reserved */ + unsigned mcs: 7; /**< Modulation Coding Scheme. If is HT(11n) packet, shows the modulation, range from 0 to 76(MSC0 ~ MCS76) */ + unsigned cwb: 1; /**< Channel Bandwidth of the packet. 0: 20MHz; 1: 40MHz */ + unsigned : 16; /**< reserved */ + unsigned smoothing: 1; /**< Set to 1 indicates that channel estimate smoothing is recommended. Set to 0 indicates that only per-carrierindependent (unsmoothed) channel estimate is recommended. */ - unsigned not_sounding:1; /**< Set to 0 indicates that PPDU is a sounding PPDU. Set to 1indicates that the PPDU is not a sounding PPDU. + unsigned not_sounding: 1; /**< Set to 0 indicates that PPDU is a sounding PPDU. Set to 1indicates that the PPDU is not a sounding PPDU. sounding PPDU is used for channel estimation by the request receiver */ - unsigned :1; /**< reserved */ - unsigned aggregation:1; /**< Aggregation. 0: MPDU packet; 1: AMPDU packet */ - unsigned stbc:2; /**< Space Time Block Code(STBC). 0: non STBC packet; 1: STBC packet */ - unsigned fec_coding:1; /**< Forward Error Correction(FEC). Flag is set for 11n packets which are LDPC */ - unsigned sgi:1; /**< Short Guide Interval(SGI). 0: Long GI; 1: Short GI */ + unsigned : 1; /**< reserved */ + unsigned aggregation: 1; /**< Aggregation. 0: MPDU packet; 1: AMPDU packet */ + unsigned stbc: 2; /**< Space Time Block Code(STBC). 0: non STBC packet; 1: STBC packet */ + unsigned fec_coding: 1; /**< Forward Error Correction(FEC). Flag is set for 11n packets which are LDPC */ + unsigned sgi: 1; /**< Short Guide Interval(SGI). 0: Long GI; 1: Short GI */ #if CONFIG_IDF_TARGET_ESP32 - signed noise_floor:8; /**< noise floor of Radio Frequency Module(RF). unit: dBm*/ + signed noise_floor: 8; /**< noise floor of Radio Frequency Module(RF). unit: dBm*/ #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C2 - unsigned :8; /**< reserved */ + unsigned : 8; /**< reserved */ #endif - unsigned ampdu_cnt:8; /**< the number of subframes aggregated in AMPDU */ - unsigned channel:4; /**< primary channel on which this packet is received */ - unsigned secondary_channel:4; /**< secondary channel on which this packet is received. 0: none; 1: above; 2: below */ - unsigned :8; /**< reserved */ - unsigned timestamp:32; /**< timestamp. The local time when this packet is received. It is precise only if modem sleep or light sleep is not enabled. unit: microsecond */ - unsigned :32; /**< reserved */ + unsigned ampdu_cnt: 8; /**< the number of subframes aggregated in AMPDU */ + unsigned channel: 4; /**< primary channel on which this packet is received */ + unsigned secondary_channel: 4; /**< secondary channel on which this packet is received. 0: none; 1: above; 2: below */ + unsigned : 8; /**< reserved */ + unsigned timestamp: 32; /**< timestamp. The local time when this packet is received. It is precise only if modem sleep or light sleep is not enabled. unit: microsecond */ + unsigned : 32; /**< reserved */ #if CONFIG_IDF_TARGET_ESP32S2 - unsigned :32; /**< reserved */ + unsigned : 32; /**< reserved */ #elif CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C2 - signed noise_floor:8; /**< noise floor of Radio Frequency Module(RF). unit: dBm*/ - unsigned :24; /**< reserved */ - unsigned :32; /**< reserved */ + signed noise_floor: 8; /**< noise floor of Radio Frequency Module(RF). unit: dBm*/ + unsigned : 24; /**< reserved */ + unsigned : 32; /**< reserved */ #endif - unsigned :31; /**< reserved */ - unsigned ant:1; /**< antenna number from which this packet is received. 0: WiFi antenna 0; 1: WiFi antenna 1 */ + unsigned : 31; /**< reserved */ + unsigned ant: 1; /**< antenna number from which this packet is received. 0: WiFi antenna 0; 1: WiFi antenna 1 */ #if CONFIG_IDF_TARGET_ESP32S2 - signed noise_floor:8; /**< noise floor of Radio Frequency Module(RF). unit: dBm*/ - unsigned :24; /**< reserved */ + signed noise_floor: 8; /**< noise floor of Radio Frequency Module(RF). unit: dBm*/ + unsigned : 24; /**< reserved */ #elif CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C2 - unsigned :32; /**< reserved */ - unsigned :32; /**< reserved */ - unsigned :32; /**< reserved */ + unsigned : 32; /**< reserved */ + unsigned : 32; /**< reserved */ + unsigned : 32; /**< reserved */ #endif - unsigned sig_len:12; /**< length of packet including Frame Check Sequence(FCS) */ - unsigned :12; /**< reserved */ - unsigned rx_state:8; /**< state of the packet. 0: no error; others: error numbers which are not public */ + unsigned sig_len: 12; /**< length of packet including Frame Check Sequence(FCS) */ + unsigned : 12; /**< reserved */ + unsigned rx_state: 8; /**< state of the packet. 0: no error; others: error numbers which are not public */ } wifi_pkt_rx_ctrl_t; #endif @@ -105,7 +105,6 @@ typedef struct { } wifi_csi_config_t; #endif // !CONFIG_SOC_WIFI_HE_SUPPORT - /** @brief Payload passed to 'buf' parameter of promiscuous mode RX callback. */ typedef struct { diff --git a/components/esp_wifi/lib b/components/esp_wifi/lib index 49c8e2d99d..852cfcfa66 160000 --- a/components/esp_wifi/lib +++ b/components/esp_wifi/lib @@ -1 +1 @@ -Subproject commit 49c8e2d99d2dfc26e49b320327fb6df06dfc9722 +Subproject commit 852cfcfa667356b2ae99052d6fbe5fdb587956eb diff --git a/components/esp_wifi/src/smartconfig_ack.c b/components/esp_wifi/src/smartconfig_ack.c index f2d8a416f4..d7eb1313e2 100644 --- a/components/esp_wifi/src/smartconfig_ack.c +++ b/components/esp_wifi/src/smartconfig_ack.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2010-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -86,7 +86,7 @@ static void sc_ack_send_task(void *pvParameters) remote_port = SC_ACK_TOUCH_SERVER_PORT; } else if (ack->type == SC_TYPE_ESPTOUCH_V2) { uint8_t port_bit = ack->ctx.token; - if(port_bit > 3) { + if (port_bit > 3) { port_bit = 0; } remote_port = SC_ACK_TOUCH_V2_SERVER_PORT(port_bit); @@ -194,8 +194,7 @@ static void sc_ack_send_task(void *pvParameters) goto _end; } } - } - else { + } else { vTaskDelay((TickType_t)(100 / portTICK_PERIOD_MS)); } } diff --git a/components/esp_wifi/src/wifi_default.c b/components/esp_wifi/src/wifi_default.c index 4851d6bc7d..6a687084c4 100644 --- a/components/esp_wifi/src/wifi_default.c +++ b/components/esp_wifi/src/wifi_default.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -278,13 +278,13 @@ esp_err_t esp_wifi_set_default_wifi_nan_handlers(void) esp_err_t esp_wifi_clear_default_wifi_driver_and_handlers(void *esp_netif) { int i; - for (i = 0; i< MAX_WIFI_IFS; ++i) { + for (i = 0; i < MAX_WIFI_IFS; ++i) { // clear internal static pointers to netifs if (s_wifi_netifs[i] == esp_netif) { s_wifi_netifs[i] = NULL; } } - for (i = 0; i< MAX_WIFI_IFS; ++i) { + for (i = 0; i < MAX_WIFI_IFS; ++i) { // check if all netifs are cleared to delete default handlers if (s_wifi_netifs[i] != NULL) { break; @@ -298,7 +298,6 @@ esp_err_t esp_wifi_clear_default_wifi_driver_and_handlers(void *esp_netif) return disconnect_and_destroy(esp_netif); } - // // Object manipulation // @@ -329,12 +328,12 @@ static inline esp_err_t esp_netif_attach_wifi(esp_netif_t *esp_netif, wifi_inter { if (esp_netif == NULL || (wifi_if != WIFI_IF_STA #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT - && wifi_if != WIFI_IF_AP + && wifi_if != WIFI_IF_AP #endif #ifdef CONFIG_ESP_WIFI_NAN_ENABLE - && wifi_if != WIFI_IF_NAN + && wifi_if != WIFI_IF_NAN #endif - )) { + )) { return ESP_ERR_INVALID_ARG; } s_wifi_netifs[wifi_if] = esp_netif; @@ -360,7 +359,6 @@ esp_err_t esp_netif_attach_wifi_nan(esp_netif_t *esp_netif) } #endif - // // Default WiFi creation from user code // @@ -459,8 +457,8 @@ esp_err_t esp_netif_create_default_wifi_mesh_netifs(esp_netif_t **p_netif_sta, e memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_AP, sizeof(netif_cfg)); netif_cfg.flags &= ~ESP_NETIF_DHCP_SERVER; esp_netif_config_t cfg_ap = { - .base = &netif_cfg, - .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP, + .base = &netif_cfg, + .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP, }; esp_netif_t *netif_ap = esp_netif_new(&cfg_ap); assert(netif_ap); @@ -474,8 +472,8 @@ esp_err_t esp_netif_create_default_wifi_mesh_netifs(esp_netif_t **p_netif_sta, e memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(netif_cfg)); netif_cfg.flags &= ~ESP_NETIF_DHCP_CLIENT; esp_netif_config_t cfg_sta = { - .base = &netif_cfg, - .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA, + .base = &netif_cfg, + .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA, }; esp_netif_t *netif_sta = esp_netif_new(&cfg_sta); assert(netif_sta); diff --git a/components/esp_wifi/src/wifi_init.c b/components/esp_wifi/src/wifi_init.c index a0c210bd30..c0dc4023e1 100644 --- a/components/esp_wifi/src/wifi_init.c +++ b/components/esp_wifi/src/wifi_init.c @@ -162,7 +162,7 @@ static esp_err_t wifi_deinit_internal(void) } if (esp_wifi_internal_reg_rxcb(WIFI_IF_STA, NULL) != ESP_OK || - esp_wifi_internal_reg_rxcb(WIFI_IF_AP, NULL) != ESP_OK) { + esp_wifi_internal_reg_rxcb(WIFI_IF_AP, NULL) != ESP_OK) { ESP_LOGW(TAG, "Failed to unregister Rx callbacks"); } @@ -349,7 +349,7 @@ esp_err_t esp_wifi_init(const wifi_init_config_t *config) #if CONFIG_MAC_BB_PD if (esp_register_mac_bb_pd_callback(pm_mac_sleep) != ESP_OK - || esp_register_mac_bb_pu_callback(pm_mac_wakeup) != ESP_OK) { + || esp_register_mac_bb_pu_callback(pm_mac_wakeup) != ESP_OK) { esp_unregister_mac_bb_pd_callback(pm_mac_sleep); esp_unregister_mac_bb_pu_callback(pm_mac_wakeup); @@ -418,7 +418,7 @@ esp_err_t esp_wifi_init(const wifi_init_config_t *config) #ifdef CONFIG_PM_ENABLE if (s_wifi_modem_sleep_lock == NULL) { result = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "wifi", - &s_wifi_modem_sleep_lock); + &s_wifi_modem_sleep_lock); if (result != ESP_OK) { ESP_LOGE(TAG, "Failed to create pm lock (0x%x)", result); goto _deinit; diff --git a/components/esp_wifi/src/wifi_netif.c b/components/esp_wifi/src/wifi_netif.c index 4fe30acfce..60559b6fe4 100644 --- a/components/esp_wifi/src/wifi_netif.c +++ b/components/esp_wifi/src/wifi_netif.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -81,10 +81,10 @@ static esp_err_t wifi_driver_start(esp_netif_t * esp_netif, void * args) wifi_netif_driver_t driver = args; driver->base.netif = esp_netif; esp_netif_driver_ifconfig_t driver_ifconfig = { - .handle = driver, - .transmit = wifi_transmit, - .transmit_wrap= wifi_transmit_wrap, - .driver_free_rx_buffer = wifi_free + .handle = driver, + .transmit = wifi_transmit, + .transmit_wrap = wifi_transmit_wrap, + .driver_free_rx_buffer = wifi_free }; return esp_netif_set_driver_config(esp_netif, &driver_ifconfig); @@ -94,7 +94,7 @@ void esp_wifi_destroy_if_driver(wifi_netif_driver_t h) { if (h) { esp_wifi_internal_reg_rxcb(h->wifi_if, NULL); // ignore the potential error - // as the wifi might have been already uninitialized + // as the wifi might have been already uninitialized s_wifi_netifs[h->wifi_if] = NULL; } free(h); @@ -140,8 +140,7 @@ esp_err_t esp_wifi_register_if_rxcb(wifi_netif_driver_t ifx, esp_netif_receive_t wifi_rxcb_t rxcb = NULL; esp_err_t ret; - switch (wifi_interface) - { + switch (wifi_interface) { case WIFI_IF_STA: rxcb = wifi_sta_receive; diff --git a/components/esp_wifi/test_apps/wifi_connect/main/app_main.c b/components/esp_wifi/test_apps/wifi_connect/main/app_main.c index 8806a82ce5..5816593f4a 100644 --- a/components/esp_wifi/test_apps/wifi_connect/main/app_main.c +++ b/components/esp_wifi/test_apps/wifi_connect/main/app_main.c @@ -43,7 +43,6 @@ void tearDown(void) check_leak(before_free_32bit, after_free_32bit, "32BIT"); } - void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); diff --git a/components/esp_wifi/test_apps/wifi_connect/main/test_wifi_conn.c b/components/esp_wifi/test_apps/wifi_connect/main/test_wifi_conn.c index 9608f122cb..00dc105e42 100644 --- a/components/esp_wifi/test_apps/wifi_connect/main/test_wifi_conn.c +++ b/components/esp_wifi/test_apps/wifi_connect/main/test_wifi_conn.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 * @@ -24,7 +24,6 @@ #include "freertos/task.h" #include "freertos/event_groups.h" - #ifndef TEST_SUFFIX_STR #define TEST_SUFFIX_STR "_0000" #endif @@ -34,7 +33,6 @@ #define TEST_DEFAULT_CHANNEL (6) #define CONNECT_TIMEOUT_MS (8000) - #define GOT_IP_EVENT (1) #define WIFI_DISCONNECT_EVENT (1<<1) #define WIFI_STA_CONNECTED (1<<2) @@ -42,7 +40,6 @@ #define EVENT_HANDLER_FLAG_DO_NOT_AUTO_RECONNECT 0x00000001 - static const char* TAG = "test_wifi"; static uint32_t wifi_event_handler_flag; static esp_netif_t* s_ap_netif = NULL; @@ -51,60 +48,59 @@ static esp_netif_t* s_sta_netif = NULL; static EventGroupHandle_t wifi_events; static void wifi_event_handler(void* arg, esp_event_base_t event_base, - int32_t event_id, void* event_data) + int32_t event_id, void* event_data) { ESP_LOGI(TAG, "wifi event handler: %"PRIi32, event_id); - switch(event_id) { - case WIFI_EVENT_STA_START: - ESP_LOGI(TAG, "WIFI_EVENT_STA_START"); - break; - case WIFI_EVENT_AP_STACONNECTED: - ESP_LOGI(TAG, "WIFI_EVENT_AP_STACONNECTED"); - if (wifi_events) { - xEventGroupSetBits(wifi_events, WIFI_AP_STA_CONNECTED); - } - break; - case WIFI_EVENT_STA_CONNECTED: - ESP_LOGI(TAG, "WIFI_EVENT_STA_CONNECTED"); - if (wifi_events) { - xEventGroupSetBits(wifi_events, WIFI_STA_CONNECTED); - } - break; - case WIFI_EVENT_STA_DISCONNECTED: - ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED"); - wifi_event_sta_disconnected_t *event = (wifi_event_sta_disconnected_t *)event_data; - ESP_LOGI(TAG, "disconnect reason: %u", event->reason); - if (! (EVENT_HANDLER_FLAG_DO_NOT_AUTO_RECONNECT & wifi_event_handler_flag) ) { - TEST_ESP_OK(esp_wifi_connect()); - } - if (wifi_events) { - xEventGroupSetBits(wifi_events, WIFI_DISCONNECT_EVENT); - } - break; - default: - break; + switch (event_id) { + case WIFI_EVENT_STA_START: + ESP_LOGI(TAG, "WIFI_EVENT_STA_START"); + break; + case WIFI_EVENT_AP_STACONNECTED: + ESP_LOGI(TAG, "WIFI_EVENT_AP_STACONNECTED"); + if (wifi_events) { + xEventGroupSetBits(wifi_events, WIFI_AP_STA_CONNECTED); + } + break; + case WIFI_EVENT_STA_CONNECTED: + ESP_LOGI(TAG, "WIFI_EVENT_STA_CONNECTED"); + if (wifi_events) { + xEventGroupSetBits(wifi_events, WIFI_STA_CONNECTED); + } + break; + case WIFI_EVENT_STA_DISCONNECTED: + ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED"); + wifi_event_sta_disconnected_t *event = (wifi_event_sta_disconnected_t *)event_data; + ESP_LOGI(TAG, "disconnect reason: %u", event->reason); + if (!(EVENT_HANDLER_FLAG_DO_NOT_AUTO_RECONNECT & wifi_event_handler_flag)) { + TEST_ESP_OK(esp_wifi_connect()); + } + if (wifi_events) { + xEventGroupSetBits(wifi_events, WIFI_DISCONNECT_EVENT); + } + break; + default: + break; } return; } - static void ip_event_handler(void* arg, esp_event_base_t event_base, - int32_t event_id, void* event_data) + int32_t event_id, void* event_data) { ip_event_got_ip_t *event; ESP_LOGI(TAG, "ip event handler"); - switch(event_id) { - case IP_EVENT_STA_GOT_IP: - event = (ip_event_got_ip_t*)event_data; - ESP_LOGI(TAG, "IP_EVENT_STA_GOT_IP"); - ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); - if (wifi_events) { - xEventGroupSetBits(wifi_events, GOT_IP_EVENT); - } - break; - default: - break; + switch (event_id) { + case IP_EVENT_STA_GOT_IP: + event = (ip_event_got_ip_t*)event_data; + ESP_LOGI(TAG, "IP_EVENT_STA_GOT_IP"); + ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); + if (wifi_events) { + xEventGroupSetBits(wifi_events, GOT_IP_EVENT); + } + break; + default: + break; } return; } @@ -131,18 +127,17 @@ static esp_err_t event_deinit(void) #define EMPH_STR(s) "****** "s" ******" - static void start_wifi_as_softap(void) { wifi_config_t w_config = { .ap.ssid = TEST_DEFAULT_SSID, .ap.password = TEST_DEFAULT_PWD, .ap.ssid_len = strlen(TEST_DEFAULT_SSID), - .ap.channel = TEST_DEFAULT_CHANNEL, - .ap.authmode = WIFI_AUTH_WPA2_PSK, - .ap.ssid_hidden = false, - .ap.max_connection = 4, - .ap.beacon_interval = 100, + .ap.channel = TEST_DEFAULT_CHANNEL, + .ap.authmode = WIFI_AUTH_WPA2_PSK, + .ap.ssid_hidden = false, + .ap.max_connection = 4, + .ap.beacon_interval = 100, }; event_init(); @@ -181,7 +176,7 @@ static void stop_wifi(void) vEventGroupDelete(wifi_events); wifi_events = NULL; } - vTaskDelay(500/portTICK_PERIOD_MS); + vTaskDelay(500 / portTICK_PERIOD_MS); } static void receive_ds2ds_packet(void) @@ -189,7 +184,7 @@ static void receive_ds2ds_packet(void) start_wifi_as_softap(); // wait for sender to send packets - vTaskDelay(1000/portTICK_PERIOD_MS); + vTaskDelay(1000 / portTICK_PERIOD_MS); stop_wifi(); } @@ -210,7 +205,7 @@ static void send_ds2ds_packet(void) esp_wifi_80211_tx(WIFI_IF_AP, ds2ds_pdu, sizeof(ds2ds_pdu), true); vTaskDelay(50 / portTICK_PERIOD_MS); } - vTaskDelay(500/portTICK_PERIOD_MS); + vTaskDelay(500 / portTICK_PERIOD_MS); stop_wifi(); } @@ -228,7 +223,7 @@ static void wifi_connect(void) TEST_ESP_OK(esp_wifi_set_config(WIFI_IF_STA, &w_config)); TEST_ESP_OK(esp_wifi_connect()); ESP_LOGI(TAG, "start esp_wifi_connect: %s", TEST_DEFAULT_SSID); - bits = xEventGroupWaitBits(wifi_events, GOT_IP_EVENT, 1, 0, CONNECT_TIMEOUT_MS/portTICK_PERIOD_MS); + bits = xEventGroupWaitBits(wifi_events, GOT_IP_EVENT, 1, 0, CONNECT_TIMEOUT_MS / portTICK_PERIOD_MS); TEST_ASSERT(bits & GOT_IP_EVENT); } @@ -238,7 +233,7 @@ static void test_wifi_connection_sta(void) start_wifi_as_sta(); // make sure softap has started - vTaskDelay(1000/portTICK_PERIOD_MS); + vTaskDelay(1000 / portTICK_PERIOD_MS); wifi_connect(); // do not auto reconnect after connected @@ -258,7 +253,7 @@ static void test_wifi_connection_softap(void) start_wifi_as_softap(); // wait station connected - bits = xEventGroupWaitBits(wifi_events, WIFI_AP_STA_CONNECTED, 1, 0, CONNECT_TIMEOUT_MS/portTICK_PERIOD_MS); + bits = xEventGroupWaitBits(wifi_events, WIFI_AP_STA_CONNECTED, 1, 0, CONNECT_TIMEOUT_MS / portTICK_PERIOD_MS); TEST_ASSERT(bits & WIFI_AP_STA_CONNECTED); // wait 70s (longer than station side) @@ -275,7 +270,7 @@ static void esp_wifi_connect_first_time(void) { start_wifi_as_sta(); // make sure softap has started - vTaskDelay(1000/portTICK_PERIOD_MS); + vTaskDelay(1000 / portTICK_PERIOD_MS); wifi_config_t w_config; memset(&w_config, 0, sizeof(w_config)); @@ -296,7 +291,7 @@ static void test_wifi_connect_at_scan_phase(void) esp_wifi_connect_first_time(); // connect when first connect in scan - vTaskDelay(300/portTICK_PERIOD_MS); + vTaskDelay(300 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "connect when first connect in scan"); TEST_ESP_ERR(ESP_ERR_WIFI_CONN, esp_wifi_connect()); wifi_event_handler_flag |= EVENT_HANDLER_FLAG_DO_NOT_AUTO_RECONNECT; @@ -310,7 +305,7 @@ static void test_wifi_connect_before_connected_phase(void) esp_wifi_connect_first_time(); // connect before connected - vTaskDelay(730/portTICK_PERIOD_MS); + vTaskDelay(730 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "connect when first connect after scan before connected"); TEST_ESP_ERR(ESP_ERR_WIFI_CONN, esp_wifi_connect()); wifi_event_handler_flag |= EVENT_HANDLER_FLAG_DO_NOT_AUTO_RECONNECT; @@ -328,7 +323,7 @@ static void test_wifi_connect_after_connected_phase(void) xEventGroupClearBits(wifi_events, WIFI_STA_CONNECTED | WIFI_DISCONNECT_EVENT); ESP_LOGI(TAG, "connect after connected"); TEST_ESP_OK(esp_wifi_connect()); - bits = xEventGroupWaitBits(wifi_events, WIFI_STA_CONNECTED | WIFI_DISCONNECT_EVENT, pdTRUE, pdFALSE, CONNECT_TIMEOUT_MS/portTICK_PERIOD_MS); + bits = xEventGroupWaitBits(wifi_events, WIFI_STA_CONNECTED | WIFI_DISCONNECT_EVENT, pdTRUE, pdFALSE, CONNECT_TIMEOUT_MS / portTICK_PERIOD_MS); // shouldn't reconnect TEST_ASSERT((bits & WIFI_AP_STA_CONNECTED) == 0); // shouldn't disconnect @@ -344,7 +339,7 @@ static void set_wifi_softap(void) start_wifi_as_softap(); // wait for sta connect - vTaskDelay(20000/portTICK_PERIOD_MS); + vTaskDelay(20000 / portTICK_PERIOD_MS); stop_wifi(); } diff --git a/components/esp_wifi/test_apps/wifi_function/main/app_main.c b/components/esp_wifi/test_apps/wifi_function/main/app_main.c index f99864da55..69fde673cf 100644 --- a/components/esp_wifi/test_apps/wifi_function/main/app_main.c +++ b/components/esp_wifi/test_apps/wifi_function/main/app_main.c @@ -38,7 +38,6 @@ void tearDown(void) check_leak(before_free_32bit, after_free_32bit, "32BIT"); } - void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); diff --git a/components/esp_wifi/test_apps/wifi_function/main/test_wifi_country.c b/components/esp_wifi/test_apps/wifi_function/main/test_wifi_country.c index 2c1e4c687d..82bfc347e5 100644 --- a/components/esp_wifi/test_apps/wifi_function/main/test_wifi_country.c +++ b/components/esp_wifi/test_apps/wifi_function/main/test_wifi_country.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -20,8 +20,8 @@ TEST_CASE("wifi set country code", "[wifi_init]") TEST_ESP_OK(esp_wifi_init(&cfg)); wifi_country_t country; - wifi_country_t country_01 = {.cc="01", .schan=1, .nchan=11, .policy=WIFI_COUNTRY_POLICY_MANUAL}; - wifi_country_t country_CN = {.cc="CN", .schan=1, .nchan=13, .policy=WIFI_COUNTRY_POLICY_MANUAL}; + wifi_country_t country_01 = {.cc = "01", .schan = 1, .nchan = 11, .policy = WIFI_COUNTRY_POLICY_MANUAL}; + wifi_country_t country_CN = {.cc = "CN", .schan = 1, .nchan = 13, .policy = WIFI_COUNTRY_POLICY_MANUAL}; ESP_LOGI(TAG, EMPH_STR("esp_wifi_get_country (default)")); TEST_ESP_OK(esp_wifi_get_country(&country)); @@ -34,7 +34,6 @@ TEST_CASE("wifi set country code", "[wifi_init]") TEST_ESP_OK(esp_wifi_get_country(&country)); TEST_ASSERT(country.cc[0] == country_CN.cc[0] && country.cc[1] == country_CN.cc[1]); - ESP_LOGI(TAG, EMPH_STR("esp_wifi_deinit")); TEST_ESP_OK(esp_wifi_deinit()); @@ -55,7 +54,6 @@ TEST_CASE("wifi set country code", "[wifi_init]") TEST_ESP_OK(esp_wifi_get_country_code(&country_code_string[0])); TEST_ASSERT(country_code_string[0] == country_code_string_CN[0] && country_code_string[1] == country_code_string_CN[1]); - ESP_LOGI(TAG, EMPH_STR("esp_wifi_deinit")); TEST_ESP_OK(esp_wifi_deinit()); diff --git a/components/esp_wifi/test_apps/wifi_function/main/test_wifi_init.c b/components/esp_wifi/test_apps/wifi_function/main/test_wifi_init.c index 9b85dfbbd6..f941cda44c 100644 --- a/components/esp_wifi/test_apps/wifi_function/main/test_wifi_init.c +++ b/components/esp_wifi/test_apps/wifi_function/main/test_wifi_init.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -15,24 +15,23 @@ #define EMPH_STR(s) "****** "s" ******" - static const char* TAG = "test_wifi_init"; static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { printf("wifi event handle called.\n"); - switch(event_id) { - case WIFI_EVENT_AP_START: - ESP_LOGI(TAG, "WIFI_EVENT_AP_START"); - break; - case WIFI_EVENT_STA_START: - ESP_LOGI(TAG, "WIFI_EVENT_STA_START"); - break; - case WIFI_EVENT_STA_DISCONNECTED: - ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED"); - break; - default: - break; + switch (event_id) { + case WIFI_EVENT_AP_START: + ESP_LOGI(TAG, "WIFI_EVENT_AP_START"); + break; + case WIFI_EVENT_STA_START: + ESP_LOGI(TAG, "WIFI_EVENT_STA_START"); + break; + case WIFI_EVENT_STA_DISCONNECTED: + ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED"); + break; + default: + break; } return; } @@ -46,7 +45,7 @@ static esp_err_t event_init(void) static esp_err_t event_deinit(void) { - ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT,ESP_EVENT_ANY_ID,&wifi_event_handler)); + ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler)); ESP_ERROR_CHECK(esp_event_loop_delete_default()); return ESP_OK; } @@ -75,9 +74,9 @@ TEST_CASE("wifi driver can start on APP CPU", "[wifi_init]") TEST_ASSERT_NOT_NULL(sema); printf("Creating tasks\n"); #ifndef CONFIG_FREERTOS_UNICORE - xTaskCreatePinnedToCore(wifi_driver_can_start_on_APP_CPU_task, "wifi_driver_can_start_on_APP_CPU_task", 2048*2, &sema, 3, &th, 1); + xTaskCreatePinnedToCore(wifi_driver_can_start_on_APP_CPU_task, "wifi_driver_can_start_on_APP_CPU_task", 2048 * 2, &sema, 3, &th, 1); #else - xTaskCreate(wifi_driver_can_start_on_APP_CPU_task, "wifi_driver_can_start_on_APP_CPU_task", 2048*2, &sema, 3, &th); + xTaskCreate(wifi_driver_can_start_on_APP_CPU_task, "wifi_driver_can_start_on_APP_CPU_task", 2048 * 2, &sema, 3, &th); #endif TEST_ASSERT_NOT_NULL(th); xSemaphoreTake(sema, portMAX_DELAY); diff --git a/components/esp_wifi/wifi_apps/nan_app/src/nan_app.c b/components/esp_wifi/wifi_apps/nan_app/src/nan_app.c index 240420e036..64b2c5bbf0 100644 --- a/components/esp_wifi/wifi_apps/nan_app/src/nan_app.c +++ b/components/esp_wifi/wifi_apps/nan_app/src/nan_app.c @@ -83,8 +83,9 @@ static nan_ctx_t s_nan_ctx; void esp_wifi_nan_get_ipv6_linklocal_from_mac(ip6_addr_t *ip6, uint8_t *mac_addr) { - if (ip6 == NULL || mac_addr == NULL) + if (ip6 == NULL || mac_addr == NULL) { return; + } /* Link-local prefix. */ ip6->addr[0] = htonl(0xfe800000ul); ip6->addr[1] = 0; diff --git a/components/wpa_supplicant/esp_supplicant/include/esp_eap_client.h b/components/wpa_supplicant/esp_supplicant/include/esp_eap_client.h index 1cbfac781e..b18290186d 100644 --- a/components/wpa_supplicant/esp_supplicant/include/esp_eap_client.h +++ b/components/wpa_supplicant/esp_supplicant/include/esp_eap_client.h @@ -42,7 +42,6 @@ typedef struct { bool fast_pac_format_binary; /**< Set to true for binary format PAC, false for ASCII format PAC */ } esp_eap_fast_config; - /** * @brief Enable EAP authentication(WiFi Enterprise) for the station mode. * @@ -61,7 +60,6 @@ typedef struct { */ esp_err_t esp_wifi_sta_enterprise_enable(void); - /** * @brief Disable EAP authentication(WiFi Enterprise) for the station mode. * diff --git a/components/wpa_supplicant/esp_supplicant/include/esp_rrm.h b/components/wpa_supplicant/esp_supplicant/include/esp_rrm.h index bd2fabfc8c..7051a3efbd 100644 --- a/components/wpa_supplicant/esp_supplicant/include/esp_rrm.h +++ b/components/wpa_supplicant/esp_supplicant/include/esp_rrm.h @@ -40,7 +40,7 @@ typedef void (*neighbor_rep_request_cb)(void *ctx, const uint8_t *report, size_t __attribute__((deprecated("Use 'esp_rrm_send_neighbor_report_request' instead"))) int esp_rrm_send_neighbor_rep_request(neighbor_rep_request_cb cb, - void *cb_ctx); + void *cb_ctx); /** * @brief Send Radio measurement neighbor report request to connected AP * @return diff --git a/components/wpa_supplicant/esp_supplicant/include/esp_wnm.h b/components/wpa_supplicant/esp_supplicant/include/esp_wnm.h index 2ee95bedd7..8a73e3a43d 100644 --- a/components/wpa_supplicant/esp_supplicant/include/esp_wnm.h +++ b/components/wpa_supplicant/esp_supplicant/include/esp_wnm.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -16,16 +16,16 @@ extern "C" { * enum btm_query_reason: Reason code for sending btm query */ enum btm_query_reason { - REASON_UNSPECIFIED = 0, - REASON_FRAME_LOSS = 1, - REASON_DELAY = 2, - REASON_BANDWIDTH = 3, - REASON_LOAD_BALANCE = 4, - REASON_RSSI = 5, - REASON_RETRANSMISSIONS = 6, - REASON_INTERFERENCE = 7, - REASON_GRAY_ZONE = 8, - REASON_PREMIUM_AP = 9, + REASON_UNSPECIFIED = 0, + REASON_FRAME_LOSS = 1, + REASON_DELAY = 2, + REASON_BANDWIDTH = 3, + REASON_LOAD_BALANCE = 4, + REASON_RSSI = 5, + REASON_RETRANSMISSIONS = 6, + REASON_INTERFERENCE = 7, + REASON_GRAY_ZONE = 8, + REASON_PREMIUM_AP = 9, }; /** @@ -41,8 +41,8 @@ enum btm_query_reason { * - -2: station not connected to AP */ int esp_wnm_send_bss_transition_mgmt_query(enum btm_query_reason query_reason, - const char *btm_candidates, - int cand_list); + const char *btm_candidates, + int cand_list); /** * @brief Check bss trasition capability of connected AP diff --git a/components/wpa_supplicant/esp_supplicant/include/esp_wpa2.h b/components/wpa_supplicant/esp_supplicant/include/esp_wpa2.h index d697ce6f4b..8632991750 100644 --- a/components/wpa_supplicant/esp_supplicant/include/esp_wpa2.h +++ b/components/wpa_supplicant/esp_supplicant/include/esp_wpa2.h @@ -201,8 +201,8 @@ void esp_wifi_sta_wpa2_ent_clear_ca_cert(void); */ __attribute__((deprecated("Use 'esp_eap_client_set_certificate_and_key' instead"))) esp_err_t esp_wifi_sta_wpa2_ent_set_cert_key(const unsigned char *client_cert, int client_cert_len, - const unsigned char *private_key, int private_key_len, - const unsigned char *private_key_passwd, int private_key_passwd_len); + const unsigned char *private_key, int private_key_len, + const unsigned char *private_key_passwd, int private_key_passwd_len); /** * @brief Clear client certificate and key. diff --git a/components/wpa_supplicant/esp_supplicant/include/esp_wps.h b/components/wpa_supplicant/esp_supplicant/include/esp_wps.h index 148bc81da7..c907480386 100644 --- a/components/wpa_supplicant/esp_supplicant/include/esp_wps.h +++ b/components/wpa_supplicant/esp_supplicant/include/esp_wps.h @@ -181,7 +181,6 @@ esp_err_t esp_wifi_ap_wps_disable(void); */ esp_err_t esp_wifi_ap_wps_start(const unsigned char *pin); - /** * @} */ diff --git a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-bignum.c b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-bignum.c index 29e12978f0..d912c253af 100644 --- a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-bignum.c +++ b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-bignum.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -33,7 +33,6 @@ struct crypto_bignum *crypto_bignum_init(void) return (struct crypto_bignum *)bn; } - struct crypto_bignum *crypto_bignum_init_set(const u8 *buf, size_t len) { int ret = 0; @@ -50,7 +49,6 @@ cleanup: return NULL; } - struct crypto_bignum * crypto_bignum_init_uint(unsigned int val) { @@ -65,14 +63,12 @@ struct crypto_bignum * crypto_bignum_init_uint(unsigned int val) return (struct crypto_bignum *)bn; } - void crypto_bignum_deinit(struct crypto_bignum *n, int clear) { mbedtls_mpi_free((mbedtls_mpi *)n); os_free((mbedtls_mpi *)n); } - int crypto_bignum_to_bin(const struct crypto_bignum *a, u8 *buf, size_t buflen, size_t padlen) { @@ -102,7 +98,6 @@ cleanup: return ret; } - int crypto_bignum_add(const struct crypto_bignum *a, const struct crypto_bignum *b, struct crypto_bignum *c) @@ -111,7 +106,6 @@ int crypto_bignum_add(const struct crypto_bignum *a, -1 : 0; } - int crypto_bignum_mod(const struct crypto_bignum *a, const struct crypto_bignum *b, struct crypto_bignum *c) @@ -119,7 +113,6 @@ int crypto_bignum_mod(const struct crypto_bignum *a, return mbedtls_mpi_mod_mpi((mbedtls_mpi *) c, (const mbedtls_mpi *) a, (const mbedtls_mpi *) b) ? -1 : 0; } - int crypto_bignum_exptmod(const struct crypto_bignum *a, const struct crypto_bignum *b, const struct crypto_bignum *c, @@ -129,7 +122,6 @@ int crypto_bignum_exptmod(const struct crypto_bignum *a, } - int crypto_bignum_inverse(const struct crypto_bignum *a, const struct crypto_bignum *b, struct crypto_bignum *c) @@ -138,7 +130,6 @@ int crypto_bignum_inverse(const struct crypto_bignum *a, (const mbedtls_mpi *) b) ? -1 : 0; } - int crypto_bignum_sub(const struct crypto_bignum *a, const struct crypto_bignum *b, struct crypto_bignum *c) @@ -147,7 +138,6 @@ int crypto_bignum_sub(const struct crypto_bignum *a, -1 : 0; } - int crypto_bignum_div(const struct crypto_bignum *a, const struct crypto_bignum *b, struct crypto_bignum *c) @@ -156,14 +146,13 @@ int crypto_bignum_div(const struct crypto_bignum *a, -1 : 0; } - int crypto_bignum_mulmod(const struct crypto_bignum *a, const struct crypto_bignum *b, const struct crypto_bignum *c, struct crypto_bignum *d) { return mbedtls_mpi_mul_mpi((mbedtls_mpi *)d, (const mbedtls_mpi *)a, (const mbedtls_mpi *)b) || - mbedtls_mpi_mod_mpi((mbedtls_mpi *)d, (mbedtls_mpi *)d, (const mbedtls_mpi *)c) ? -1 : 0; + mbedtls_mpi_mod_mpi((mbedtls_mpi *)d, (mbedtls_mpi *)d, (const mbedtls_mpi *)c) ? -1 : 0; } int crypto_bignum_sqrmod(const struct crypto_bignum *a, @@ -176,19 +165,18 @@ int crypto_bignum_sqrmod(const struct crypto_bignum *a, return -1; } - res = mbedtls_mpi_copy((mbedtls_mpi *) tmp,(const mbedtls_mpi *) a); - res = crypto_bignum_mulmod(a,tmp,b,c); + res = mbedtls_mpi_copy((mbedtls_mpi *) tmp, (const mbedtls_mpi *) a); + res = crypto_bignum_mulmod(a, tmp, b, c); crypto_bignum_deinit(tmp, 0); return res ? -1 : 0; } - int crypto_bignum_rshift(const struct crypto_bignum *a, int n, struct crypto_bignum *r) { int res; - res = mbedtls_mpi_copy((mbedtls_mpi *) r,(const mbedtls_mpi *) a); + res = mbedtls_mpi_copy((mbedtls_mpi *) r, (const mbedtls_mpi *) a); if (res) { return -1; } @@ -198,26 +186,22 @@ int crypto_bignum_rshift(const struct crypto_bignum *a, int n, } - int crypto_bignum_cmp(const struct crypto_bignum *a, const struct crypto_bignum *b) { return mbedtls_mpi_cmp_mpi((const mbedtls_mpi *) a, (const mbedtls_mpi *) b); } - int crypto_bignum_bits(const struct crypto_bignum *a) { return mbedtls_mpi_bitlen((const mbedtls_mpi *) a); } - int crypto_bignum_is_zero(const struct crypto_bignum *a) { return (mbedtls_mpi_cmp_int((const mbedtls_mpi *) a, 0) == 0); } - int crypto_bignum_is_one(const struct crypto_bignum *a) { return (mbedtls_mpi_cmp_int((const mbedtls_mpi *) a, 1) == 0); @@ -231,7 +215,7 @@ int crypto_bignum_is_odd(const struct crypto_bignum *a) int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m) { return ((mbedtls_mpi_random((mbedtls_mpi *) r, 0, (const mbedtls_mpi *) m, - crypto_rng_wrapper, NULL) != 0) ? -1 : 0); + crypto_rng_wrapper, NULL) != 0) ? -1 : 0); } int crypto_bignum_legendre(const struct crypto_bignum *a, @@ -251,11 +235,11 @@ int crypto_bignum_legendre(const struct crypto_bignum *a, if (mbedtls_mpi_cmp_int(&tmp, 1) == 0) { res = 1; } else if (mbedtls_mpi_cmp_int(&tmp, 0) == 0 - /* The below check is workaround for the case where HW - * does not behave properly for X ^ A mod M when X is - * power of M. Instead of returning value 0, value M is - * returned.*/ - || mbedtls_mpi_cmp_mpi(&tmp, (const mbedtls_mpi *)p) == 0) { + /* The below check is workaround for the case where HW + * does not behave properly for X ^ A mod M when X is + * power of M. Instead of returning value 0, value M is + * returned.*/ + || mbedtls_mpi_cmp_mpi(&tmp, (const mbedtls_mpi *)p) == 0) { res = 0; } else { res = -1; @@ -268,7 +252,7 @@ cleanup: } int crypto_bignum_to_string(const struct crypto_bignum *a, - u8 *buf, size_t buflen, size_t padlen) + u8 *buf, size_t buflen, size_t padlen) { int num_bytes, offset; size_t outlen; @@ -287,24 +271,26 @@ int crypto_bignum_to_string(const struct crypto_bignum *a, os_memset(buf, 0, offset); mbedtls_mpi_write_string((mbedtls_mpi *) a, 16, (char *)(buf + offset), - mbedtls_mpi_size((mbedtls_mpi *)a), &outlen); + mbedtls_mpi_size((mbedtls_mpi *)a), &outlen); return outlen; } int crypto_bignum_addmod(const struct crypto_bignum *a, - const struct crypto_bignum *b, - const struct crypto_bignum *c, - struct crypto_bignum *d) + const struct crypto_bignum *b, + const struct crypto_bignum *c, + struct crypto_bignum *d) { struct crypto_bignum *tmp = crypto_bignum_init(); int ret = -1; - if (mbedtls_mpi_add_mpi((mbedtls_mpi *) tmp, (const mbedtls_mpi *) a, (const mbedtls_mpi *) b) < 0) + if (mbedtls_mpi_add_mpi((mbedtls_mpi *) tmp, (const mbedtls_mpi *) a, (const mbedtls_mpi *) b) < 0) { goto fail; + } - if (mbedtls_mpi_mod_mpi( (mbedtls_mpi *) d, (const mbedtls_mpi *) tmp, (const mbedtls_mpi *) c) < 0) + if (mbedtls_mpi_mod_mpi((mbedtls_mpi *) d, (const mbedtls_mpi *) tmp, (const mbedtls_mpi *) c) < 0) { goto fail; + } ret = 0; fail: diff --git a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-ec.c b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-ec.c index 70a6717ca6..87d445abc1 100644 --- a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-ec.c +++ b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-ec.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -37,884 +37,877 @@ #ifdef CONFIG_ECC struct crypto_ec { - mbedtls_ecp_group group; + mbedtls_ecp_group group; }; static int crypto_rng_wrapper(void *ctx, unsigned char *buf, size_t len) { - return random_get_bytes(buf, len); + return random_get_bytes(buf, len); } struct crypto_ec *crypto_ec_init(int group) { - struct crypto_ec *e; + struct crypto_ec *e; - mbedtls_ecp_group_id grp_id; + mbedtls_ecp_group_id grp_id; - /* IANA registry to mbedtls internal mapping*/ - switch (group) { - case IANA_SECP256R1: - /* For now just support NIST-P256. - * This is of type "short Weierstrass". - */ - grp_id = MBEDTLS_ECP_DP_SECP256R1; - break; - default: - return NULL; + /* IANA registry to mbedtls internal mapping*/ + switch (group) { + case IANA_SECP256R1: + /* For now just support NIST-P256. + * This is of type "short Weierstrass". + */ + grp_id = MBEDTLS_ECP_DP_SECP256R1; + break; + default: + return NULL; - } - e = os_zalloc(sizeof(*e)); - if (e == NULL) { - return NULL; - } + } + e = os_zalloc(sizeof(*e)); + if (e == NULL) { + return NULL; + } - mbedtls_ecp_group_init(&e->group); + mbedtls_ecp_group_init(&e->group); - if (mbedtls_ecp_group_load(&e->group, grp_id)) { - crypto_ec_deinit(e); - e = NULL; - } + if (mbedtls_ecp_group_load(&e->group, grp_id)) { + crypto_ec_deinit(e); + e = NULL; + } - return e; + return e; } - void crypto_ec_deinit(struct crypto_ec *e) { - if (e == NULL) { - return; - } + if (e == NULL) { + return; + } - mbedtls_ecp_group_free(&e->group); - os_free(e); + mbedtls_ecp_group_free(&e->group); + os_free(e); } - struct crypto_ec_point *crypto_ec_point_init(struct crypto_ec *e) { - mbedtls_ecp_point *pt; - if (e == NULL) { - return NULL; - } + mbedtls_ecp_point *pt; + if (e == NULL) { + return NULL; + } - pt = os_zalloc(sizeof(mbedtls_ecp_point)); + pt = os_zalloc(sizeof(mbedtls_ecp_point)); - if( pt == NULL) { - return NULL; - } + if (pt == NULL) { + return NULL; + } - mbedtls_ecp_point_init(pt); + mbedtls_ecp_point_init(pt); - return (struct crypto_ec_point *) pt; + return (struct crypto_ec_point *) pt; } - size_t crypto_ec_prime_len(struct crypto_ec *e) { - return mbedtls_mpi_size(&e->group.P); + return mbedtls_mpi_size(&e->group.P); } size_t crypto_ec_order_len(struct crypto_ec *e) { - return mbedtls_mpi_size(&e->group.N); + return mbedtls_mpi_size(&e->group.N); } - size_t crypto_ec_prime_len_bits(struct crypto_ec *e) { - return mbedtls_mpi_bitlen(&e->group.P); + return mbedtls_mpi_bitlen(&e->group.P); } struct crypto_ec_group *crypto_ec_get_group_byname(const char *name) { - struct crypto_ec *e; - const mbedtls_ecp_curve_info *curve = mbedtls_ecp_curve_info_from_name(name); + struct crypto_ec *e; + const mbedtls_ecp_curve_info *curve = mbedtls_ecp_curve_info_from_name(name); - e = os_zalloc(sizeof(*e)); - if (e == NULL) { - return NULL; - } + e = os_zalloc(sizeof(*e)); + if (e == NULL) { + return NULL; + } - mbedtls_ecp_group_init( &e->group ); + mbedtls_ecp_group_init(&e->group); - if (mbedtls_ecp_group_load(&e->group, curve->grp_id)) { - crypto_ec_deinit(e); - e = NULL; - } + if (mbedtls_ecp_group_load(&e->group, curve->grp_id)) { + crypto_ec_deinit(e); + e = NULL; + } - return (struct crypto_ec_group *) &e->group; + return (struct crypto_ec_group *) &e->group; } const struct crypto_bignum *crypto_ec_get_prime(struct crypto_ec *e) { - return (const struct crypto_bignum *) &e->group.P; + return (const struct crypto_bignum *) &e->group.P; } - const struct crypto_bignum *crypto_ec_get_order(struct crypto_ec *e) { - return (const struct crypto_bignum *) &e->group.N; + return (const struct crypto_bignum *) &e->group.N; } - const struct crypto_bignum * crypto_ec_get_b(struct crypto_ec *e) { - return (const struct crypto_bignum *) &e->group.B; + return (const struct crypto_bignum *) &e->group.B; } - void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear) { - mbedtls_ecp_point_free((mbedtls_ecp_point *) p); - os_free(p); + mbedtls_ecp_point_free((mbedtls_ecp_point *) p); + os_free(p); } int crypto_ec_point_to_bin(struct crypto_ec *e, - const struct crypto_ec_point *point, u8 *x, u8 *y) + const struct crypto_ec_point *point, u8 *x, u8 *y) { - int len = mbedtls_mpi_size(&e->group.P); + int len = mbedtls_mpi_size(&e->group.P); - if (x) { - if(crypto_bignum_to_bin((struct crypto_bignum *) & ((mbedtls_ecp_point *) point)->MBEDTLS_PRIVATE(X), - x, len, len) < 0) { - return -1; - } + if (x) { + if (crypto_bignum_to_bin((struct crypto_bignum *) & ((mbedtls_ecp_point *) point)->MBEDTLS_PRIVATE(X), + x, len, len) < 0) { + return -1; + } - } + } - if (y) { - if(crypto_bignum_to_bin((struct crypto_bignum *) & ((mbedtls_ecp_point *) point)->MBEDTLS_PRIVATE(Y), - y, len, len) < 0) { - return -1; - } - } + if (y) { + if (crypto_bignum_to_bin((struct crypto_bignum *) & ((mbedtls_ecp_point *) point)->MBEDTLS_PRIVATE(Y), + y, len, len) < 0) { + return -1; + } + } - return 0; + return 0; } int crypto_ec_get_affine_coordinates(struct crypto_ec *e, struct crypto_ec_point *pt, - struct crypto_bignum *x, struct crypto_bignum *y) + struct crypto_bignum *x, struct crypto_bignum *y) { - int ret = -1; - mbedtls_ecp_point *point = (mbedtls_ecp_point *)pt; + int ret = -1; + mbedtls_ecp_point *point = (mbedtls_ecp_point *)pt; - if (!mbedtls_ecp_is_zero(point) && (mbedtls_mpi_cmp_int( &point->MBEDTLS_PRIVATE(Z), 1 ) == 0 )) { - // Affine coordinates mean that z should be 1, - wpa_printf(MSG_ERROR, "Z coordinate is neither 0 or 1"); - return -1; - } + if (!mbedtls_ecp_is_zero(point) && (mbedtls_mpi_cmp_int(&point->MBEDTLS_PRIVATE(Z), 1) == 0)) { + // Affine coordinates mean that z should be 1, + wpa_printf(MSG_ERROR, "Z coordinate is neither 0 or 1"); + return -1; + } - if (x) { - MBEDTLS_MPI_CHK(mbedtls_mpi_copy((mbedtls_mpi*) x, &((mbedtls_ecp_point* )point)->MBEDTLS_PRIVATE(X))); - } - if (y) { - MBEDTLS_MPI_CHK(mbedtls_mpi_copy((mbedtls_mpi*) y, &((mbedtls_ecp_point* )point)->MBEDTLS_PRIVATE(Y))); - } - return 0; + if (x) { + MBEDTLS_MPI_CHK(mbedtls_mpi_copy((mbedtls_mpi*) x, &((mbedtls_ecp_point*)point)->MBEDTLS_PRIVATE(X))); + } + if (y) { + MBEDTLS_MPI_CHK(mbedtls_mpi_copy((mbedtls_mpi*) y, &((mbedtls_ecp_point*)point)->MBEDTLS_PRIVATE(Y))); + } + return 0; cleanup: - return ret; + return ret; } struct crypto_ec_point *crypto_ec_point_from_bin(struct crypto_ec *e, - const u8 *val) + const u8 *val) { - mbedtls_ecp_point *pt; - int len, ret; + mbedtls_ecp_point *pt; + int len, ret; - if (e == NULL) { - return NULL; - } + if (e == NULL) { + return NULL; + } - len = mbedtls_mpi_size(&e->group.P); + len = mbedtls_mpi_size(&e->group.P); - pt = os_zalloc(sizeof(mbedtls_ecp_point)); - if (!pt) { - return NULL; - } - mbedtls_ecp_point_init(pt); + pt = os_zalloc(sizeof(mbedtls_ecp_point)); + if (!pt) { + return NULL; + } + mbedtls_ecp_point_init(pt); - MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&pt->MBEDTLS_PRIVATE(X), val, len)); - MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&pt->MBEDTLS_PRIVATE(Y), val + len, len)); - MBEDTLS_MPI_CHK(mbedtls_mpi_lset((&pt->MBEDTLS_PRIVATE(Z)), 1)); + MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&pt->MBEDTLS_PRIVATE(X), val, len)); + MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&pt->MBEDTLS_PRIVATE(Y), val + len, len)); + MBEDTLS_MPI_CHK(mbedtls_mpi_lset((&pt->MBEDTLS_PRIVATE(Z)), 1)); - return (struct crypto_ec_point *) pt; + return (struct crypto_ec_point *) pt; cleanup: - mbedtls_ecp_point_free(pt); - os_free(pt); - return NULL; + mbedtls_ecp_point_free(pt); + os_free(pt); + return NULL; } - int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a, - const struct crypto_ec_point *b, - struct crypto_ec_point *c) + const struct crypto_ec_point *b, + struct crypto_ec_point *c) { - int ret; - mbedtls_mpi one; + int ret; + mbedtls_mpi one; - mbedtls_mpi_init(&one); + mbedtls_mpi_init(&one); - MBEDTLS_MPI_CHK(mbedtls_mpi_lset( &one, 1 )); - MBEDTLS_MPI_CHK(mbedtls_ecp_muladd(&e->group, (mbedtls_ecp_point *) c, &one, (const mbedtls_ecp_point *)a , &one, (const mbedtls_ecp_point *)b)); + MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&one, 1)); + MBEDTLS_MPI_CHK(mbedtls_ecp_muladd(&e->group, (mbedtls_ecp_point *) c, &one, (const mbedtls_ecp_point *)a, &one, (const mbedtls_ecp_point *)b)); cleanup: - mbedtls_mpi_free(&one); - return ret ? -1 : 0; + mbedtls_mpi_free(&one); + return ret ? -1 : 0; } - int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p, - const struct crypto_bignum *b, - struct crypto_ec_point *res) + const struct crypto_bignum *b, + struct crypto_ec_point *res) { - int ret; - mbedtls_entropy_context entropy; - mbedtls_ctr_drbg_context ctr_drbg; + int ret; + mbedtls_entropy_context entropy; + mbedtls_ctr_drbg_context ctr_drbg; - mbedtls_entropy_init(&entropy); - mbedtls_ctr_drbg_init(&ctr_drbg); + mbedtls_entropy_init(&entropy); + mbedtls_ctr_drbg_init(&ctr_drbg); - MBEDTLS_MPI_CHK(mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, - NULL, 0)); + MBEDTLS_MPI_CHK(mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, + NULL, 0)); - MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&e->group, - (mbedtls_ecp_point *) res, - (const mbedtls_mpi *)b, - (const mbedtls_ecp_point *)p, - mbedtls_ctr_drbg_random, - &ctr_drbg)); + MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&e->group, + (mbedtls_ecp_point *) res, + (const mbedtls_mpi *)b, + (const mbedtls_ecp_point *)p, + mbedtls_ctr_drbg_random, + &ctr_drbg)); cleanup: - mbedtls_ctr_drbg_free(&ctr_drbg); - mbedtls_entropy_free(&entropy); - return ret ? -1 : 0; + mbedtls_ctr_drbg_free(&ctr_drbg); + mbedtls_entropy_free(&entropy); + return ret ? -1 : 0; } - /* Currently mbedtls does not have any function for inverse * This function calculates inverse of a point. * Set R = -P */ static int ecp_opp(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R, const mbedtls_ecp_point *P) { - int ret = 0; + int ret = 0; - /* Copy */ - if (R != P) { - MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, P)); - } + /* Copy */ + if (R != P) { + MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, P)); + } - /* In-place opposite */ - if (mbedtls_mpi_cmp_int(&R->MBEDTLS_PRIVATE(Y), 0) != 0) { - MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&R->MBEDTLS_PRIVATE(Y), &grp->P, &R->MBEDTLS_PRIVATE(Y))); - } + /* In-place opposite */ + if (mbedtls_mpi_cmp_int(&R->MBEDTLS_PRIVATE(Y), 0) != 0) { + MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&R->MBEDTLS_PRIVATE(Y), &grp->P, &R->MBEDTLS_PRIVATE(Y))); + } cleanup: - return (ret ); + return (ret); } int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p) { - return ecp_opp(&e->group, (mbedtls_ecp_point *) p, (mbedtls_ecp_point *) p) ? -1 : 0; + return ecp_opp(&e->group, (mbedtls_ecp_point *) p, (mbedtls_ecp_point *) p) ? -1 : 0; } int crypto_ec_point_solve_y_coord(struct crypto_ec *e, - struct crypto_ec_point *p, - const struct crypto_bignum *x, int y_bit) + struct crypto_ec_point *p, + const struct crypto_bignum *x, int y_bit) { - mbedtls_mpi temp; - mbedtls_mpi *y_sqr, *y; - mbedtls_mpi_init(&temp); - int ret = 0; + mbedtls_mpi temp; + mbedtls_mpi *y_sqr, *y; + mbedtls_mpi_init(&temp); + int ret = 0; - y = &((mbedtls_ecp_point *)p)->MBEDTLS_PRIVATE(Y); + y = &((mbedtls_ecp_point *)p)->MBEDTLS_PRIVATE(Y); - /* Faster way to find sqrt - * Works only with curves having prime p - * such that p ≡ 3 (mod 4) - * y_ = (y2 ^ ((p+1)/4)) mod p - * - * if LSB of both x and y are same: y = y_ - * else y = p - y_ - * y_bit is LSB of x - */ - y_bit = (y_bit != 0); + /* Faster way to find sqrt + * Works only with curves having prime p + * such that p ≡ 3 (mod 4) + * y_ = (y2 ^ ((p+1)/4)) mod p + * + * if LSB of both x and y are same: y = y_ + * else y = p - y_ + * y_bit is LSB of x + */ + y_bit = (y_bit != 0); - y_sqr = (mbedtls_mpi *) crypto_ec_point_compute_y_sqr(e, x); + y_sqr = (mbedtls_mpi *) crypto_ec_point_compute_y_sqr(e, x); - if (y_sqr) { + if (y_sqr) { - MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&temp, &e->group.P, 1)); - MBEDTLS_MPI_CHK(mbedtls_mpi_div_int(&temp, NULL, &temp, 4)); - MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(y, y_sqr, &temp, &e->group.P, NULL)); + MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&temp, &e->group.P, 1)); + MBEDTLS_MPI_CHK(mbedtls_mpi_div_int(&temp, NULL, &temp, 4)); + MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(y, y_sqr, &temp, &e->group.P, NULL)); - if (y_bit != mbedtls_mpi_get_bit(y, 0)) - MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(y, &e->group.P, y)); + if (y_bit != mbedtls_mpi_get_bit(y, 0)) { + MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(y, &e->group.P, y)); + } - MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&((mbedtls_ecp_point* )p)->MBEDTLS_PRIVATE(X), (const mbedtls_mpi*) x)); - MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&((mbedtls_ecp_point *)p)->MBEDTLS_PRIVATE(Z), 1)); - } else { - ret = 1; - } + MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&((mbedtls_ecp_point*)p)->MBEDTLS_PRIVATE(X), (const mbedtls_mpi*) x)); + MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&((mbedtls_ecp_point *)p)->MBEDTLS_PRIVATE(Z), 1)); + } else { + ret = 1; + } cleanup: - mbedtls_mpi_free(&temp); - mbedtls_mpi_free(y_sqr); - os_free(y_sqr); - return ret ? -1 : 0; + mbedtls_mpi_free(&temp); + mbedtls_mpi_free(y_sqr); + os_free(y_sqr); + return ret ? -1 : 0; } int crypto_get_order(struct crypto_ec_group *group, struct crypto_bignum *x) { - return mbedtls_mpi_copy((mbedtls_mpi *) x, &((mbedtls_ecp_group *)group)->N); + return mbedtls_mpi_copy((mbedtls_mpi *) x, &((mbedtls_ecp_group *)group)->N); } struct crypto_bignum *crypto_ec_point_compute_y_sqr(struct crypto_ec *e, - const struct crypto_bignum *x) + const struct crypto_bignum *x) { - mbedtls_mpi temp, temp2, num; - int ret = 0; + mbedtls_mpi temp, temp2, num; + int ret = 0; - mbedtls_mpi *y_sqr = os_zalloc(sizeof(mbedtls_mpi)); - if (y_sqr == NULL) { - return NULL; - } + mbedtls_mpi *y_sqr = os_zalloc(sizeof(mbedtls_mpi)); + if (y_sqr == NULL) { + return NULL; + } - mbedtls_mpi_init(&temp); - mbedtls_mpi_init(&temp2); - mbedtls_mpi_init(&num); - mbedtls_mpi_init(y_sqr); + mbedtls_mpi_init(&temp); + mbedtls_mpi_init(&temp2); + mbedtls_mpi_init(&num); + mbedtls_mpi_init(y_sqr); - /* y^2 = x^3 + ax + b mod P */ - /* X*X*X is faster on esp32 whereas X^3 is faster on other chips */ + /* y^2 = x^3 + ax + b mod P */ + /* X*X*X is faster on esp32 whereas X^3 is faster on other chips */ #if CONFIG_IDF_TARGET_ESP32 - /* Calculate x*x*x mod P*/ - MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&temp, (const mbedtls_mpi *) x, (const mbedtls_mpi *) x)); - MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&temp, &temp, (const mbedtls_mpi *) x)); - MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&temp, &temp, &e->group.P)); + /* Calculate x*x*x mod P*/ + MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&temp, (const mbedtls_mpi *) x, (const mbedtls_mpi *) x)); + MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&temp, &temp, (const mbedtls_mpi *) x)); + MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&temp, &temp, &e->group.P)); #else - /* Calculate x^3 mod P*/ - MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&num, 3)); - MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&temp, (const mbedtls_mpi *) x, &num, &e->group.P, NULL)); + /* Calculate x^3 mod P*/ + MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&num, 3)); + MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&temp, (const mbedtls_mpi *) x, &num, &e->group.P, NULL)); #endif - /* Calculate ax mod P*/ - MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&num, -3)); - MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&temp2, (const mbedtls_mpi *) x, &num)); - MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&temp2, &temp2, &e->group.P)); + /* Calculate ax mod P*/ + MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&num, -3)); + MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&temp2, (const mbedtls_mpi *) x, &num)); + MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&temp2, &temp2, &e->group.P)); - /* Calculate ax + b mod P. Note that b is already < P*/ - MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&temp2, &temp2, &e->group.B)); - MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&temp2, &temp2, &e->group.P)); + /* Calculate ax + b mod P. Note that b is already < P*/ + MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&temp2, &temp2, &e->group.B)); + MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&temp2, &temp2, &e->group.P)); - /* Calculate x^3 + ax + b mod P*/ - MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&temp2, &temp2, &temp)); - MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(y_sqr, &temp2, &e->group.P)); + /* Calculate x^3 + ax + b mod P*/ + MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&temp2, &temp2, &temp)); + MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(y_sqr, &temp2, &e->group.P)); cleanup: - mbedtls_mpi_free(&temp); - mbedtls_mpi_free(&temp2); - mbedtls_mpi_free(&num); - if (ret) { - mbedtls_mpi_free(y_sqr); - os_free(y_sqr); - return NULL; - } else { - return (struct crypto_bignum *) y_sqr; - } + mbedtls_mpi_free(&temp); + mbedtls_mpi_free(&temp2); + mbedtls_mpi_free(&num); + if (ret) { + mbedtls_mpi_free(y_sqr); + os_free(y_sqr); + return NULL; + } else { + return (struct crypto_bignum *) y_sqr; + } } int crypto_ec_point_is_at_infinity(struct crypto_ec *e, - const struct crypto_ec_point *p) + const struct crypto_ec_point *p) { - return mbedtls_ecp_is_zero((mbedtls_ecp_point *) p); + return mbedtls_ecp_is_zero((mbedtls_ecp_point *) p); } int crypto_ec_point_is_on_curve(struct crypto_ec *e, - const struct crypto_ec_point *p) + const struct crypto_ec_point *p) { - mbedtls_mpi y_sqr_lhs, *y_sqr_rhs = NULL, two; - int ret = 0, on_curve = 0; + mbedtls_mpi y_sqr_lhs, *y_sqr_rhs = NULL, two; + int ret = 0, on_curve = 0; - mbedtls_mpi_init(&y_sqr_lhs); - mbedtls_mpi_init(&two); + mbedtls_mpi_init(&y_sqr_lhs); + mbedtls_mpi_init(&two); - /* Calculate y^2 mod P*/ - MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&two, 2)); - MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&y_sqr_lhs, &((const mbedtls_ecp_point *)p)->MBEDTLS_PRIVATE(Y) , &two, &e->group.P, NULL)); + /* Calculate y^2 mod P*/ + MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&two, 2)); + MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&y_sqr_lhs, &((const mbedtls_ecp_point *)p)->MBEDTLS_PRIVATE(Y), &two, &e->group.P, NULL)); - y_sqr_rhs = (mbedtls_mpi *) crypto_ec_point_compute_y_sqr(e, (const struct crypto_bignum *) & ((const mbedtls_ecp_point *)p)->MBEDTLS_PRIVATE(X)); + y_sqr_rhs = (mbedtls_mpi *) crypto_ec_point_compute_y_sqr(e, (const struct crypto_bignum *) & ((const mbedtls_ecp_point *)p)->MBEDTLS_PRIVATE(X)); - if (y_sqr_rhs && (mbedtls_mpi_cmp_mpi(y_sqr_rhs, &y_sqr_lhs) == 0)) { - on_curve = 1; - } + if (y_sqr_rhs && (mbedtls_mpi_cmp_mpi(y_sqr_rhs, &y_sqr_lhs) == 0)) { + on_curve = 1; + } cleanup: - mbedtls_mpi_free(&y_sqr_lhs); - mbedtls_mpi_free(&two); - mbedtls_mpi_free(y_sqr_rhs); - os_free(y_sqr_rhs); - return (ret == 0) && (on_curve == 1); + mbedtls_mpi_free(&y_sqr_lhs); + mbedtls_mpi_free(&two); + mbedtls_mpi_free(y_sqr_rhs); + os_free(y_sqr_rhs); + return (ret == 0) && (on_curve == 1); } int crypto_ec_point_cmp(const struct crypto_ec *e, - const struct crypto_ec_point *a, - const struct crypto_ec_point *b) + const struct crypto_ec_point *a, + const struct crypto_ec_point *b) { - return mbedtls_ecp_point_cmp((const mbedtls_ecp_point *) a, - (const mbedtls_ecp_point *) b); + return mbedtls_ecp_point_cmp((const mbedtls_ecp_point *) a, + (const mbedtls_ecp_point *) b); } int crypto_key_compare(struct crypto_key *key1, struct crypto_key *key2) { - int ret = 0; - mbedtls_entropy_context entropy; - mbedtls_ctr_drbg_context ctr_drbg; + int ret = 0; + mbedtls_entropy_context entropy; + mbedtls_ctr_drbg_context ctr_drbg; - mbedtls_entropy_init(&entropy); - mbedtls_ctr_drbg_init(&ctr_drbg); + mbedtls_entropy_init(&entropy); + mbedtls_ctr_drbg_init(&ctr_drbg); - MBEDTLS_MPI_CHK(mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0)); - if (mbedtls_pk_check_pair((mbedtls_pk_context *)key1, (mbedtls_pk_context *)key2, mbedtls_ctr_drbg_random, &ctr_drbg) < 0) { - goto cleanup; - } + MBEDTLS_MPI_CHK(mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0)); + if (mbedtls_pk_check_pair((mbedtls_pk_context *)key1, (mbedtls_pk_context *)key2, mbedtls_ctr_drbg_random, &ctr_drbg) < 0) { + goto cleanup; + } - ret = 1; + ret = 1; cleanup: - mbedtls_ctr_drbg_free(&ctr_drbg); - mbedtls_entropy_free(&entropy); - return ret; + mbedtls_ctr_drbg_free(&ctr_drbg); + mbedtls_entropy_free(&entropy); + return ret; } void crypto_debug_print_point(const char *title, struct crypto_ec *e, - const struct crypto_ec_point *point) + const struct crypto_ec_point *point) { - u8 x[32], y[32]; + u8 x[32], y[32]; - if (crypto_ec_point_to_bin(e, point, x, y) < 0) { - wpa_printf(MSG_ERROR, "error: failed to get corrdinates"); - return; - } + if (crypto_ec_point_to_bin(e, point, x, y) < 0) { + wpa_printf(MSG_ERROR, "error: failed to get corrdinates"); + return; + } - wpa_hexdump(MSG_ERROR, "x:", x, 32); - wpa_hexdump(MSG_ERROR, "y:", y, 32); + wpa_hexdump(MSG_ERROR, "x:", x, 32); + wpa_hexdump(MSG_ERROR, "y:", y, 32); } static struct crypto_key *crypto_alloc_key(void) { - mbedtls_pk_context *key = os_malloc(sizeof(*key)); + mbedtls_pk_context *key = os_malloc(sizeof(*key)); - if (!key) { - wpa_printf(MSG_ERROR, "%s: memory allocation failed", __func__); - return NULL; - } - mbedtls_pk_init(key); + if (!key) { + wpa_printf(MSG_ERROR, "%s: memory allocation failed", __func__); + return NULL; + } + mbedtls_pk_init(key); - return (struct crypto_key *)key; + return (struct crypto_key *)key; } - struct crypto_key * crypto_ec_set_pubkey_point(const struct crypto_ec_group *group, - const u8 *buf, size_t len) + const u8 *buf, size_t len) { - mbedtls_ecp_point *point = NULL; - struct crypto_key *pkey = NULL; - int ret; - mbedtls_pk_context *key = (mbedtls_pk_context *)crypto_alloc_key(); - mbedtls_ecp_group *ecp_grp = (mbedtls_ecp_group *)group; + mbedtls_ecp_point *point = NULL; + struct crypto_key *pkey = NULL; + int ret; + mbedtls_pk_context *key = (mbedtls_pk_context *)crypto_alloc_key(); + mbedtls_ecp_group *ecp_grp = (mbedtls_ecp_group *)group; - if (!key) { - wpa_printf(MSG_ERROR, "%s: memory allocation failed", __func__); - return NULL; - } + if (!key) { + wpa_printf(MSG_ERROR, "%s: memory allocation failed", __func__); + return NULL; + } - point = (mbedtls_ecp_point *)crypto_ec_point_from_bin((struct crypto_ec *)group, buf); - if (!point) { - wpa_printf(MSG_ERROR, "%s: Point initialization failed", __func__); - goto fail; - } - if (crypto_ec_point_is_at_infinity((struct crypto_ec *)group, (struct crypto_ec_point *)point)) { - wpa_printf(MSG_ERROR, "Point is at infinity"); - goto fail; - } - if (!crypto_ec_point_is_on_curve((struct crypto_ec *)group, (struct crypto_ec_point *)point)) { - wpa_printf(MSG_ERROR, "Point not on curve"); - goto fail; - } + point = (mbedtls_ecp_point *)crypto_ec_point_from_bin((struct crypto_ec *)group, buf); + if (!point) { + wpa_printf(MSG_ERROR, "%s: Point initialization failed", __func__); + goto fail; + } + if (crypto_ec_point_is_at_infinity((struct crypto_ec *)group, (struct crypto_ec_point *)point)) { + wpa_printf(MSG_ERROR, "Point is at infinity"); + goto fail; + } + if (!crypto_ec_point_is_on_curve((struct crypto_ec *)group, (struct crypto_ec_point *)point)) { + wpa_printf(MSG_ERROR, "Point not on curve"); + goto fail; + } - if (mbedtls_ecp_check_pubkey(ecp_grp, point) < 0) { - // ideally should have failed in upper condition, duplicate code?? - wpa_printf(MSG_ERROR, "Invalid key"); - goto fail; - } - /* Assign values */ - if( ( ret = mbedtls_pk_setup( key, - mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY) ) ) != 0 ) - goto fail; + if (mbedtls_ecp_check_pubkey(ecp_grp, point) < 0) { + // ideally should have failed in upper condition, duplicate code?? + wpa_printf(MSG_ERROR, "Invalid key"); + goto fail; + } + /* Assign values */ + if ((ret = mbedtls_pk_setup(key, + mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY))) != 0) { + goto fail; + } - mbedtls_ecp_copy(&mbedtls_pk_ec(*key)->MBEDTLS_PRIVATE(Q), point); - mbedtls_ecp_group_load(&mbedtls_pk_ec(*key)->MBEDTLS_PRIVATE(grp), ecp_grp->id); + mbedtls_ecp_copy(&mbedtls_pk_ec(*key)->MBEDTLS_PRIVATE(Q), point); + mbedtls_ecp_group_load(&mbedtls_pk_ec(*key)->MBEDTLS_PRIVATE(grp), ecp_grp->id); - pkey = (struct crypto_key *)key; - crypto_ec_point_deinit((struct crypto_ec_point *)point, 0); - return pkey; + pkey = (struct crypto_key *)key; + crypto_ec_point_deinit((struct crypto_ec_point *)point, 0); + return pkey; fail: - if (point) - crypto_ec_point_deinit((struct crypto_ec_point *)point, 0); - if (key) - mbedtls_pk_free(key); - pkey = NULL; - return pkey; + if (point) { + crypto_ec_point_deinit((struct crypto_ec_point *)point, 0); + } + if (key) { + mbedtls_pk_free(key); + } + pkey = NULL; + return pkey; } - void crypto_ec_free_key(struct crypto_key *key) { - mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; - mbedtls_pk_free(pkey); - os_free(key); + mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; + mbedtls_pk_free(pkey); + os_free(key); } struct crypto_ec_point *crypto_ec_get_public_key(struct crypto_key *key) { - mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; + mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; - return (struct crypto_ec_point *)&mbedtls_pk_ec(*pkey)->MBEDTLS_PRIVATE(Q); + return (struct crypto_ec_point *)&mbedtls_pk_ec(*pkey)->MBEDTLS_PRIVATE(Q); } - int crypto_ec_get_priv_key_der(struct crypto_key *key, unsigned char **key_data, int *key_len) { - mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; - char *der_data = os_malloc(ECP_PRV_DER_MAX_BYTES); + mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; + char *der_data = os_malloc(ECP_PRV_DER_MAX_BYTES); - if (!der_data) { - wpa_printf(MSG_ERROR, "memory allocation failed"); - return -1; - } - *key_len = mbedtls_pk_write_key_der(pkey, (unsigned char *)der_data, ECP_PRV_DER_MAX_BYTES); - if (*key_len <= 0) { - wpa_printf(MSG_ERROR, "Failed to write priv key"); - os_free(der_data); - return -1; - } - *key_data = os_malloc(*key_len); + if (!der_data) { + wpa_printf(MSG_ERROR, "memory allocation failed"); + return -1; + } + *key_len = mbedtls_pk_write_key_der(pkey, (unsigned char *)der_data, ECP_PRV_DER_MAX_BYTES); + if (*key_len <= 0) { + wpa_printf(MSG_ERROR, "Failed to write priv key"); + os_free(der_data); + return -1; + } + *key_data = os_malloc(*key_len); - if (!*key_data) { - wpa_printf(MSG_ERROR, "memory allocation failed"); - os_free(der_data); - return -1; - } - os_memcpy(*key_data, der_data + ECP_PRV_DER_MAX_BYTES - *key_len, *key_len); - os_free(der_data); + if (!*key_data) { + wpa_printf(MSG_ERROR, "memory allocation failed"); + os_free(der_data); + return -1; + } + os_memcpy(*key_data, der_data + ECP_PRV_DER_MAX_BYTES - *key_len, *key_len); + os_free(der_data); - return 0; + return 0; } struct crypto_ec_group *crypto_ec_get_group_from_key(struct crypto_key *key) { - mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; + mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; - return (struct crypto_ec_group *)&(mbedtls_pk_ec(*pkey)->MBEDTLS_PRIVATE(grp)); + return (struct crypto_ec_group *) & (mbedtls_pk_ec(*pkey)->MBEDTLS_PRIVATE(grp)); } int crypto_ec_key_group(struct crypto_ec_key *key) { - mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; + mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; - int iana_group = (int)crypto_ec_get_mbedtls_to_nist_group_id(mbedtls_pk_ec(*pkey)->MBEDTLS_PRIVATE(grp).id); - return iana_group; + int iana_group = (int)crypto_ec_get_mbedtls_to_nist_group_id(mbedtls_pk_ec(*pkey)->MBEDTLS_PRIVATE(grp).id); + return iana_group; } struct crypto_bignum *crypto_ec_get_private_key(struct crypto_key *key) { - mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; + mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; - return ((struct crypto_bignum *)&(mbedtls_pk_ec(*pkey)->MBEDTLS_PRIVATE(d))); + return ((struct crypto_bignum *) & (mbedtls_pk_ec(*pkey)->MBEDTLS_PRIVATE(d))); } int crypto_ec_get_publickey_buf(struct crypto_key *key, u8 *key_buf, int len) { - mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; - unsigned char buf[MBEDTLS_MPI_MAX_SIZE + 10]; /* tag, length + MPI */ - unsigned char *c = buf + sizeof(buf ); - int pk_len = 0; + mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; + unsigned char buf[MBEDTLS_MPI_MAX_SIZE + 10]; /* tag, length + MPI */ + unsigned char *c = buf + sizeof(buf); + int pk_len = 0; - memset(buf, 0, sizeof(buf) ); - pk_len = mbedtls_pk_write_pubkey( &c, buf, pkey); + memset(buf, 0, sizeof(buf)); + pk_len = mbedtls_pk_write_pubkey(&c, buf, pkey); - if (pk_len < 0) - return -1; + if (pk_len < 0) { + return -1; + } - if (len == 0) - return pk_len; + if (len == 0) { + return pk_len; + } - os_memcpy(key_buf, buf + MBEDTLS_MPI_MAX_SIZE + 10 - pk_len, pk_len); + os_memcpy(key_buf, buf + MBEDTLS_MPI_MAX_SIZE + 10 - pk_len, pk_len); - return pk_len; + return pk_len; } int crypto_write_pubkey_der(struct crypto_key *key, unsigned char **key_buf) { - unsigned char *buf = os_malloc(ECP_PUB_DER_MAX_BYTES); + unsigned char *buf = os_malloc(ECP_PUB_DER_MAX_BYTES); - if(!buf) { - wpa_printf(MSG_ERROR, "memory allocation failed"); - return -1; - } - int len = mbedtls_pk_write_pubkey_der((mbedtls_pk_context *)key, buf, ECP_PUB_DER_MAX_BYTES); - if (len <= 0) { - os_free(buf); - return -1; - } + if (!buf) { + wpa_printf(MSG_ERROR, "memory allocation failed"); + return -1; + } + int len = mbedtls_pk_write_pubkey_der((mbedtls_pk_context *)key, buf, ECP_PUB_DER_MAX_BYTES); + if (len <= 0) { + os_free(buf); + return -1; + } - *key_buf = os_malloc(len); - if (!*key_buf) { - os_free(buf); - return -1; - } - os_memcpy(*key_buf, buf + ECP_PUB_DER_MAX_BYTES - len, len); - os_free(buf); + *key_buf = os_malloc(len); + if (!*key_buf) { + os_free(buf); + return -1; + } + os_memcpy(*key_buf, buf + ECP_PUB_DER_MAX_BYTES - len, len); + os_free(buf); - return len; + return len; } struct crypto_key *crypto_ec_get_key(const u8 *privkey, size_t privkey_len) { - int ret; - mbedtls_pk_context *kctx = (mbedtls_pk_context *)crypto_alloc_key(); + int ret; + mbedtls_pk_context *kctx = (mbedtls_pk_context *)crypto_alloc_key(); - if (!kctx) { - wpa_printf(MSG_ERROR, "memory allocation failed"); - return NULL; - } - ret = mbedtls_pk_parse_key(kctx, privkey, privkey_len, NULL, 0, crypto_rng_wrapper, NULL); + if (!kctx) { + wpa_printf(MSG_ERROR, "memory allocation failed"); + return NULL; + } + ret = mbedtls_pk_parse_key(kctx, privkey, privkey_len, NULL, 0, crypto_rng_wrapper, NULL); - if (ret < 0) { - //crypto_print_error_string(ret); - goto fail; - } + if (ret < 0) { + //crypto_print_error_string(ret); + goto fail; + } - return (struct crypto_key *)kctx; + return (struct crypto_key *)kctx; fail: - mbedtls_pk_free(kctx); - os_free(kctx); - return NULL; + mbedtls_pk_free(kctx); + os_free(kctx); + return NULL; } unsigned int crypto_ec_get_mbedtls_to_nist_group_id(int id) { - unsigned int nist_grpid = 0; - switch (id) { - case MBEDTLS_ECP_DP_SECP256R1: - nist_grpid = 19; - break; - case MBEDTLS_ECP_DP_SECP384R1: - nist_grpid = 20; - break; - case MBEDTLS_ECP_DP_SECP521R1: - nist_grpid = 21; - break; - case MBEDTLS_ECP_DP_BP256R1: - nist_grpid = 28; - break; - case MBEDTLS_ECP_DP_BP384R1: - nist_grpid = 29; - break; - case MBEDTLS_ECP_DP_BP512R1: - nist_grpid = 30; - break; - default: - break; - } + unsigned int nist_grpid = 0; + switch (id) { + case MBEDTLS_ECP_DP_SECP256R1: + nist_grpid = 19; + break; + case MBEDTLS_ECP_DP_SECP384R1: + nist_grpid = 20; + break; + case MBEDTLS_ECP_DP_SECP521R1: + nist_grpid = 21; + break; + case MBEDTLS_ECP_DP_BP256R1: + nist_grpid = 28; + break; + case MBEDTLS_ECP_DP_BP384R1: + nist_grpid = 29; + break; + case MBEDTLS_ECP_DP_BP512R1: + nist_grpid = 30; + break; + default: + break; + } - return nist_grpid; + return nist_grpid; } int crypto_ec_get_curve_id(const struct crypto_ec_group *group) { - mbedtls_ecp_group *grp = (mbedtls_ecp_group *)group; - return (crypto_ec_get_mbedtls_to_nist_group_id(grp->id)); + mbedtls_ecp_group *grp = (mbedtls_ecp_group *)group; + return (crypto_ec_get_mbedtls_to_nist_group_id(grp->id)); } int crypto_ecdh(struct crypto_key *key_own, struct crypto_key *key_peer, - u8 *secret, size_t *secret_len) + u8 *secret, size_t *secret_len) { - mbedtls_ecdh_context *ctx = NULL; - mbedtls_pk_context *own = (mbedtls_pk_context *)key_own; - mbedtls_pk_context *peer = (mbedtls_pk_context *)key_peer; - mbedtls_entropy_context entropy; - mbedtls_ctr_drbg_context ctr_drbg; - int ret = -1; + mbedtls_ecdh_context *ctx = NULL; + mbedtls_pk_context *own = (mbedtls_pk_context *)key_own; + mbedtls_pk_context *peer = (mbedtls_pk_context *)key_peer; + mbedtls_entropy_context entropy; + mbedtls_ctr_drbg_context ctr_drbg; + int ret = -1; - mbedtls_entropy_init(&entropy); - mbedtls_ctr_drbg_init(&ctr_drbg); + mbedtls_entropy_init(&entropy); + mbedtls_ctr_drbg_init(&ctr_drbg); - if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) < 0) { - goto fail; - } + if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) < 0) { + goto fail; + } - *secret_len = 0; - ctx = os_malloc(sizeof(*ctx)); - if (!ctx) { - wpa_printf(MSG_ERROR, "DPP: EVP_PKEY_CTX_new failed: %s", - __func__); - goto fail; - } + *secret_len = 0; + ctx = os_malloc(sizeof(*ctx)); + if (!ctx) { + wpa_printf(MSG_ERROR, "DPP: EVP_PKEY_CTX_new failed: %s", + __func__); + goto fail; + } - mbedtls_ecdh_init(ctx); - /* No need to setup, done through mbedtls_ecdh_get_params */ + mbedtls_ecdh_init(ctx); + /* No need to setup, done through mbedtls_ecdh_get_params */ - /* set params from our key */ - if (mbedtls_ecdh_get_params(ctx, mbedtls_pk_ec(*own), MBEDTLS_ECDH_OURS) < 0) { - wpa_printf(MSG_ERROR, "failed to set our ecdh params"); - goto fail; - } + /* set params from our key */ + if (mbedtls_ecdh_get_params(ctx, mbedtls_pk_ec(*own), MBEDTLS_ECDH_OURS) < 0) { + wpa_printf(MSG_ERROR, "failed to set our ecdh params"); + goto fail; + } #ifndef DPP_MAX_SHARED_SECRET_LEN #define DPP_MAX_SHARED_SECRET_LEN 66 #endif - /* set params from peers key */ - if (mbedtls_ecdh_get_params(ctx, mbedtls_pk_ec(*peer), MBEDTLS_ECDH_THEIRS) < 0) { - wpa_printf(MSG_ERROR, "failed to set peer's ecdh params"); - goto fail; - } + /* set params from peers key */ + if (mbedtls_ecdh_get_params(ctx, mbedtls_pk_ec(*peer), MBEDTLS_ECDH_THEIRS) < 0) { + wpa_printf(MSG_ERROR, "failed to set peer's ecdh params"); + goto fail; + } - if (mbedtls_ecdh_calc_secret(ctx, secret_len, secret, DPP_MAX_SHARED_SECRET_LEN, - mbedtls_ctr_drbg_random, &ctr_drbg) < 0) { - wpa_printf(MSG_ERROR, "failed to calculate secret"); - goto fail; - } + if (mbedtls_ecdh_calc_secret(ctx, secret_len, secret, DPP_MAX_SHARED_SECRET_LEN, + mbedtls_ctr_drbg_random, &ctr_drbg) < 0) { + wpa_printf(MSG_ERROR, "failed to calculate secret"); + goto fail; + } - if (*secret_len > DPP_MAX_SHARED_SECRET_LEN) { - wpa_printf(MSG_ERROR, "secret len=%d is too big", *secret_len); - goto fail; - } + if (*secret_len > DPP_MAX_SHARED_SECRET_LEN) { + wpa_printf(MSG_ERROR, "secret len=%d is too big", *secret_len); + goto fail; + } - ret = 0; + ret = 0; fail: - mbedtls_ctr_drbg_free(&ctr_drbg); - mbedtls_entropy_free(&entropy); - if (ctx) { - mbedtls_ecdh_free(ctx); - os_free(ctx); - } - return ret; + mbedtls_ctr_drbg_free(&ctr_drbg); + mbedtls_entropy_free(&entropy); + if (ctx) { + mbedtls_ecdh_free(ctx); + os_free(ctx); + } + return ret; } - int crypto_ecdsa_get_sign(unsigned char *hash, - const struct crypto_bignum *r, const struct crypto_bignum *s, struct crypto_key *csign, int hash_len) + const struct crypto_bignum *r, const struct crypto_bignum *s, struct crypto_key *csign, int hash_len) { - int ret = -1; - mbedtls_pk_context *pkey = (mbedtls_pk_context *)csign; + int ret = -1; + mbedtls_pk_context *pkey = (mbedtls_pk_context *)csign; - mbedtls_ecdsa_context *ctx = os_malloc(sizeof(*ctx)); - if (!ctx) { - wpa_printf(MSG_ERROR,"failed to allcate memory"); - return -1; - } - mbedtls_ecdsa_init(ctx); + mbedtls_ecdsa_context *ctx = os_malloc(sizeof(*ctx)); + if (!ctx) { + wpa_printf(MSG_ERROR, "failed to allcate memory"); + return -1; + } + mbedtls_ecdsa_init(ctx); - if (mbedtls_ecdsa_from_keypair(ctx, mbedtls_pk_ec(*pkey)) < 0) { - goto fail; - } - ret = mbedtls_ecdsa_sign(&ctx->MBEDTLS_PRIVATE(grp), (mbedtls_mpi *)r, (mbedtls_mpi *)s, - &ctx->MBEDTLS_PRIVATE(d), hash, SHA256_MAC_LEN, crypto_rng_wrapper, NULL); + if (mbedtls_ecdsa_from_keypair(ctx, mbedtls_pk_ec(*pkey)) < 0) { + goto fail; + } + ret = mbedtls_ecdsa_sign(&ctx->MBEDTLS_PRIVATE(grp), (mbedtls_mpi *)r, (mbedtls_mpi *)s, + &ctx->MBEDTLS_PRIVATE(d), hash, SHA256_MAC_LEN, crypto_rng_wrapper, NULL); fail: - mbedtls_ecdsa_free(ctx); - os_free(ctx); + mbedtls_ecdsa_free(ctx); + os_free(ctx); - return ret; + return ret; } int crypto_edcsa_sign_verify(const unsigned char *hash, - const struct crypto_bignum *r, const struct crypto_bignum *s, struct crypto_key *csign, int hlen) + const struct crypto_bignum *r, const struct crypto_bignum *s, struct crypto_key *csign, int hlen) { - /* (mbedtls_ecdsa_context *) */ - mbedtls_ecp_keypair *ecp_kp = mbedtls_pk_ec(*(mbedtls_pk_context *)csign); - if (!ecp_kp) { - return -1; - } + /* (mbedtls_ecdsa_context *) */ + mbedtls_ecp_keypair *ecp_kp = mbedtls_pk_ec(*(mbedtls_pk_context *)csign); + if (!ecp_kp) { + return -1; + } - mbedtls_ecp_group *ecp_kp_grp = &ecp_kp->MBEDTLS_PRIVATE(grp); - mbedtls_ecp_point *ecp_kp_q = &ecp_kp->MBEDTLS_PRIVATE(Q); - int ret = mbedtls_ecdsa_verify(ecp_kp_grp, hash, hlen, - ecp_kp_q, (mbedtls_mpi *)r, (mbedtls_mpi *)s); - if (ret != 0) { - wpa_printf(MSG_ERROR, "ecdsa verification failed"); - return ret; - } + mbedtls_ecp_group *ecp_kp_grp = &ecp_kp->MBEDTLS_PRIVATE(grp); + mbedtls_ecp_point *ecp_kp_q = &ecp_kp->MBEDTLS_PRIVATE(Q); + int ret = mbedtls_ecdsa_verify(ecp_kp_grp, hash, hlen, + ecp_kp_q, (mbedtls_mpi *)r, (mbedtls_mpi *)s); + if (ret != 0) { + wpa_printf(MSG_ERROR, "ecdsa verification failed"); + return ret; + } - return ret; + return ret; } void crypto_debug_print_ec_key(const char *title, struct crypto_key *key) { #ifdef 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_ERROR, "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))); + 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_ERROR, "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_ERROR, "prime len is %d", len); - crypto_ec_point_to_bin((struct crypto_ec *)crypto_ec_get_group_from_key(key), crypto_ec_get_public_key(key), x, y); - crypto_bignum_to_bin(crypto_ec_get_private_key(key), - d, len, len); - wpa_hexdump(MSG_ERROR, "Q_x:", x, 32); - wpa_hexdump(MSG_ERROR, "Q_y:", y, 32); - wpa_hexdump(MSG_ERROR, "d: ", d , 32); + wpa_printf(MSG_ERROR, "prime len is %d", len); + crypto_ec_point_to_bin((struct crypto_ec *)crypto_ec_get_group_from_key(key), crypto_ec_get_public_key(key), x, y); + crypto_bignum_to_bin(crypto_ec_get_private_key(key), + d, len, len); + wpa_hexdump(MSG_ERROR, "Q_x:", x, 32); + wpa_hexdump(MSG_ERROR, "Q_y:", y, 32); + wpa_hexdump(MSG_ERROR, "d: ", d, 32); #endif } struct crypto_key *crypto_ec_parse_subpub_key(const unsigned char *p, size_t len) { - int ret; - mbedtls_pk_context *pkey = (mbedtls_pk_context *)crypto_alloc_key(); + int ret; + mbedtls_pk_context *pkey = (mbedtls_pk_context *)crypto_alloc_key(); - if (!pkey) { - return NULL; - } - ret = mbedtls_pk_parse_subpubkey((unsigned char **)&p, p + len, pkey); - if (ret == 0) { - return (struct crypto_key *)pkey; - } + if (!pkey) { + return NULL; + } + ret = mbedtls_pk_parse_subpubkey((unsigned char **)&p, p + len, pkey); + if (ret == 0) { + return (struct crypto_key *)pkey; + } - mbedtls_pk_free(pkey); - os_free(pkey); - return NULL; + mbedtls_pk_free(pkey); + os_free(pkey); + return NULL; } int crypto_is_ec_key(struct crypto_key *key) { - int ret = mbedtls_pk_can_do((mbedtls_pk_context *)key, MBEDTLS_PK_ECKEY); - return ret; + int ret = mbedtls_pk_can_do((mbedtls_pk_context *)key, MBEDTLS_PK_ECKEY); + return ret; } struct crypto_key * crypto_ec_gen_keypair(u16 ike_group) { - mbedtls_pk_context *kctx = (mbedtls_pk_context *)crypto_alloc_key(); + mbedtls_pk_context *kctx = (mbedtls_pk_context *)crypto_alloc_key(); - if (!kctx) { - wpa_printf(MSG_ERROR, "%s: memory allocation failed", __func__); - return NULL; - } + if (!kctx) { + wpa_printf(MSG_ERROR, "%s: memory allocation failed", __func__); + return NULL; + } - if(mbedtls_pk_setup(kctx, - mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY)) != 0 ) - goto fail; + if (mbedtls_pk_setup(kctx, + mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY)) != 0) { + goto fail; + } - mbedtls_ecp_gen_key(MBEDTLS_ECP_DP_SECP256R1, mbedtls_pk_ec(*kctx), //get this from argument - crypto_rng_wrapper, NULL); + mbedtls_ecp_gen_key(MBEDTLS_ECP_DP_SECP256R1, mbedtls_pk_ec(*kctx), //get this from argument + crypto_rng_wrapper, NULL); - return (struct crypto_key *)kctx; + return (struct crypto_key *)kctx; fail: - mbedtls_pk_free(kctx); - os_free(kctx); - return NULL; + mbedtls_pk_free(kctx); + os_free(kctx); + return NULL; } /* @@ -922,390 +915,388 @@ fail: * namedCurve OBJECT IDENTIFIER * } */ -static int pk_write_ec_param( unsigned char **p, unsigned char *start, - mbedtls_ecp_keypair *ec ) +static int pk_write_ec_param(unsigned char **p, unsigned char *start, + mbedtls_ecp_keypair *ec) { - int ret; - size_t len = 0; - const char *oid; - size_t oid_len; + int ret; + size_t len = 0; + const char *oid; + size_t oid_len; - if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->MBEDTLS_PRIVATE(grp).id, &oid, &oid_len ) ) != 0 ) - return( ret ); + if ((ret = mbedtls_oid_get_oid_by_ec_grp(ec->MBEDTLS_PRIVATE(grp).id, &oid, &oid_len)) != 0) { + return (ret); + } - MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) ); + MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len)); - return( (int) len ); + return ((int) len); } -static int pk_write_ec_pubkey_formatted( unsigned char **p, unsigned char *start, - mbedtls_ecp_keypair *ec, int format ) +static int pk_write_ec_pubkey_formatted(unsigned char **p, unsigned char *start, + mbedtls_ecp_keypair *ec, int format) { - int ret; - size_t len = 0; - unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN]; + int ret; + size_t len = 0; + unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN]; - if( ( ret = mbedtls_ecp_point_write_binary( &ec->MBEDTLS_PRIVATE(grp), &ec->MBEDTLS_PRIVATE(Q), - format, - &len, buf, sizeof( buf ) ) ) != 0 ) - { - return( ret ); - } + if ((ret = mbedtls_ecp_point_write_binary(&ec->MBEDTLS_PRIVATE(grp), &ec->MBEDTLS_PRIVATE(Q), + format, + &len, buf, sizeof(buf))) != 0) { + return (ret); + } - if( *p < start || (size_t)( *p - start ) < len ) - return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); + if (*p < start || (size_t)(*p - start) < len) { + return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL); + } - *p -= len; - memcpy( *p, buf, len ); + *p -= len; + memcpy(*p, buf, len); - return( (int) len ); + return ((int) len); } -int mbedtls_pk_write_pubkey_formatted( unsigned char **p, unsigned char *start, - const mbedtls_pk_context *key, int format ) +int mbedtls_pk_write_pubkey_formatted(unsigned char **p, unsigned char *start, + const mbedtls_pk_context *key, int format) { - int ret; - size_t len = 0; + int ret; + size_t len = 0; - if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY ) - MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey_formatted( p, start, mbedtls_pk_ec( *key ), format ) ); - else - return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE ); + if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) { + MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_pubkey_formatted(p, start, mbedtls_pk_ec(*key), format)); + } else { + return (MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE); + } - return( (int) len ); + return ((int) len); } int crypto_pk_write_formatted_pubkey_der(mbedtls_pk_context *key, unsigned char *buf, size_t size, int format) { - int ret; - unsigned char *c; - size_t len = 0, par_len = 0, oid_len; - const char *oid; + int ret; + unsigned char *c; + size_t len = 0, par_len = 0, oid_len; + const char *oid; - if( size == 0 ) - return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); + if (size == 0) { + return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL); + } - c = buf + size; + c = buf + size; - MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey_formatted( &c, buf, key, format) ); + MBEDTLS_ASN1_CHK_ADD(len, mbedtls_pk_write_pubkey_formatted(&c, buf, key, format)); - if( c - buf < 1 ) - return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); + if (c - buf < 1) { + return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL); + } - /* - * SubjectPublicKeyInfo ::= SEQUENCE { - * algorithm AlgorithmIdentifier, - * subjectPublicKey BIT STRING } - */ - *--c = 0; - len += 1; + /* + * SubjectPublicKeyInfo ::= SEQUENCE { + * algorithm AlgorithmIdentifier, + * subjectPublicKey BIT STRING } + */ + *--c = 0; + len += 1; + MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); + MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_BIT_STRING)); - MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); - MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) ); + if ((ret = mbedtls_oid_get_oid_by_pk_alg(mbedtls_pk_get_type(key), + &oid, &oid_len)) != 0) { + return (ret); + } - if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ), - &oid, &oid_len ) ) != 0 ) - { - return( ret ); - } + if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) { + MBEDTLS_ASN1_CHK_ADD(par_len, pk_write_ec_param(&c, buf, mbedtls_pk_ec(*key))); + } - if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY ) - { - MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) ); - } + MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_algorithm_identifier(&c, buf, oid, oid_len, + par_len)); - MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len, - par_len ) ); + MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); + MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED | + MBEDTLS_ASN1_SEQUENCE)); - MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); - MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED | - MBEDTLS_ASN1_SEQUENCE ) ); - - return( (int) len ); + return ((int) len); } int crypto_ec_write_pub_key(struct crypto_key *key, unsigned char **key_buf) { - unsigned char output_buf[1600] = {0}; - int len = crypto_pk_write_formatted_pubkey_der((mbedtls_pk_context *)key, output_buf, 1600, 1); - if (len <= 0) - return 0; + unsigned char output_buf[1600] = {0}; + int len = crypto_pk_write_formatted_pubkey_der((mbedtls_pk_context *)key, output_buf, 1600, 1); + if (len <= 0) { + return 0; + } - *key_buf = os_malloc(len); - if (!*key_buf) { - wpa_printf(MSG_ERROR, "%s: memory allocation failed", __func__); - return 0; - } - os_memcpy(*key_buf, output_buf + 1600 - len, len); + *key_buf = os_malloc(len); + if (!*key_buf) { + wpa_printf(MSG_ERROR, "%s: memory allocation failed", __func__); + return 0; + } + os_memcpy(*key_buf, output_buf + 1600 - len, len); - return len; + return len; } int crypto_mbedtls_get_grp_id(int group) { - switch(group) { - case IANA_SECP256R1: - return MBEDTLS_ECP_DP_SECP256R1; - case IANA_SECP384R1: - return MBEDTLS_ECP_DP_SECP384R1; - case IANA_SECP521R1: - return MBEDTLS_ECP_DP_SECP521R1; - default: - return MBEDTLS_ECP_DP_NONE; - } + switch (group) { + case IANA_SECP256R1: + return MBEDTLS_ECP_DP_SECP256R1; + case IANA_SECP384R1: + return MBEDTLS_ECP_DP_SECP384R1; + case IANA_SECP521R1: + return MBEDTLS_ECP_DP_SECP521R1; + default: + return MBEDTLS_ECP_DP_NONE; + } } void crypto_ecdh_deinit(struct crypto_ecdh *ecdh) { - mbedtls_ecdh_context *ctx = (mbedtls_ecdh_context *)ecdh; - if (!ctx) { - return; - } - mbedtls_ecdh_free(ctx); - os_free(ctx); - ctx = NULL; + mbedtls_ecdh_context *ctx = (mbedtls_ecdh_context *)ecdh; + if (!ctx) { + return; + } + mbedtls_ecdh_free(ctx); + os_free(ctx); + ctx = NULL; } struct crypto_ecdh * crypto_ecdh_init(int group) { - mbedtls_ctr_drbg_context ctr_drbg; - mbedtls_entropy_context entropy; - mbedtls_ecdh_context *ctx; + mbedtls_ctr_drbg_context ctr_drbg; + mbedtls_entropy_context entropy; + mbedtls_ecdh_context *ctx; - ctx = os_zalloc(sizeof(*ctx)); - if (!ctx) { - wpa_printf(MSG_ERROR, "Memory allocation failed for ecdh context"); - goto fail; - } - mbedtls_ecdh_init(ctx); + ctx = os_zalloc(sizeof(*ctx)); + if (!ctx) { + wpa_printf(MSG_ERROR, "Memory allocation failed for ecdh context"); + goto fail; + } + mbedtls_ecdh_init(ctx); #ifndef CONFIG_MBEDTLS_ECDH_LEGACY_CONTEXT - ctx->MBEDTLS_PRIVATE(var) = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0; + ctx->MBEDTLS_PRIVATE(var) = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0; #endif - if ((mbedtls_ecp_group_load(ACCESS_ECDH(&ctx, grp), crypto_mbedtls_get_grp_id(group))) != 0) { - wpa_printf(MSG_ERROR, "Failed to set up ECDH context with group info"); - goto fail; - } + if ((mbedtls_ecp_group_load(ACCESS_ECDH(&ctx, grp), crypto_mbedtls_get_grp_id(group))) != 0) { + wpa_printf(MSG_ERROR, "Failed to set up ECDH context with group info"); + goto fail; + } - /* Initialize CTR_DRBG context */ - mbedtls_ctr_drbg_init(&ctr_drbg); - mbedtls_entropy_init(&entropy); + /* Initialize CTR_DRBG context */ + mbedtls_ctr_drbg_init(&ctr_drbg); + mbedtls_entropy_init(&entropy); - /* Seed and setup CTR_DRBG entropy source for future reseeds */ - if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) != 0) { - wpa_printf(MSG_ERROR, "Seeding entropy source failed"); - goto fail; - } + /* Seed and setup CTR_DRBG entropy source for future reseeds */ + if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) != 0) { + wpa_printf(MSG_ERROR, "Seeding entropy source failed"); + goto fail; + } - /* Generates ECDH keypair on elliptic curve */ - if (mbedtls_ecdh_gen_public(ACCESS_ECDH(&ctx, grp), ACCESS_ECDH(&ctx, d), ACCESS_ECDH(&ctx, Q), mbedtls_ctr_drbg_random, &ctr_drbg)!=0) { - wpa_printf(MSG_ERROR, "ECDH keypair on curve failed"); - goto fail; - } + /* Generates ECDH keypair on elliptic curve */ + if (mbedtls_ecdh_gen_public(ACCESS_ECDH(&ctx, grp), ACCESS_ECDH(&ctx, d), ACCESS_ECDH(&ctx, Q), mbedtls_ctr_drbg_random, &ctr_drbg) != 0) { + wpa_printf(MSG_ERROR, "ECDH keypair on curve failed"); + goto fail; + } - mbedtls_ctr_drbg_free(&ctr_drbg); - mbedtls_entropy_free(&entropy); - return (struct crypto_ecdh *)ctx; + mbedtls_ctr_drbg_free(&ctr_drbg); + mbedtls_entropy_free(&entropy); + return (struct crypto_ecdh *)ctx; fail: - if (ctx) { - mbedtls_ecdh_free(ctx); - os_free(ctx); - ctx = NULL; - } - mbedtls_ctr_drbg_free(&ctr_drbg); - mbedtls_entropy_free(&entropy); - return NULL; + if (ctx) { + mbedtls_ecdh_free(ctx); + os_free(ctx); + ctx = NULL; + } + mbedtls_ctr_drbg_free(&ctr_drbg); + mbedtls_entropy_free(&entropy); + return NULL; } struct wpabuf * crypto_ecdh_get_pubkey(struct crypto_ecdh *ecdh, int y) { - struct wpabuf *public_key = NULL; - uint8_t *buf = NULL; - mbedtls_ecdh_context *ctx = (mbedtls_ecdh_context *)ecdh; - size_t prime_len = ACCESS_ECDH(ctx, grp).pbits/8; + struct wpabuf *public_key = NULL; + uint8_t *buf = NULL; + mbedtls_ecdh_context *ctx = (mbedtls_ecdh_context *)ecdh; + size_t prime_len = ACCESS_ECDH(ctx, grp).pbits / 8; - buf = os_zalloc(y ? prime_len : 2 * prime_len); - if (!buf) { - wpa_printf(MSG_ERROR, "Memory allocation failed"); - return NULL; - } + buf = os_zalloc(y ? prime_len : 2 * prime_len); + if (!buf) { + wpa_printf(MSG_ERROR, "Memory allocation failed"); + return NULL; + } - /* Export an MPI into unsigned big endian binary data of fixed size */ - mbedtls_mpi_write_binary(ACCESS_ECDH(&ctx, Q).MBEDTLS_PRIVATE(X), buf, prime_len); - public_key = wpabuf_alloc_copy(buf, 32); - os_free(buf); - return public_key; + /* Export an MPI into unsigned big endian binary data of fixed size */ + mbedtls_mpi_write_binary(ACCESS_ECDH(&ctx, Q).MBEDTLS_PRIVATE(X), buf, prime_len); + public_key = wpabuf_alloc_copy(buf, 32); + os_free(buf); + return public_key; } struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y, const u8 *key, size_t len) { - uint8_t *secret = 0; - size_t olen = 0, len_prime = 0; - struct crypto_bignum *bn_x = NULL; - struct crypto_ec_point *ec_pt = NULL; - uint8_t *px = NULL, *py = NULL, *buf = NULL; - struct crypto_key *pkey = NULL; - struct wpabuf *sh_secret = NULL; - int secret_key = 0; + uint8_t *secret = 0; + size_t olen = 0, len_prime = 0; + struct crypto_bignum *bn_x = NULL; + struct crypto_ec_point *ec_pt = NULL; + uint8_t *px = NULL, *py = NULL, *buf = NULL; + struct crypto_key *pkey = NULL; + struct wpabuf *sh_secret = NULL; + int secret_key = 0; - mbedtls_ecdh_context *ctx = (mbedtls_ecdh_context *)ecdh; + mbedtls_ecdh_context *ctx = (mbedtls_ecdh_context *)ecdh; - mbedtls_ctr_drbg_context ctr_drbg; - mbedtls_entropy_context entropy; + mbedtls_ctr_drbg_context ctr_drbg; + mbedtls_entropy_context entropy; - /* Initialize CTR_DRBG context */ - mbedtls_ctr_drbg_init(&ctr_drbg); - mbedtls_entropy_init(&entropy); + /* Initialize CTR_DRBG context */ + mbedtls_ctr_drbg_init(&ctr_drbg); + mbedtls_entropy_init(&entropy); - /* Seed and setup CTR_DRBG entropy source for future reseeds */ - if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) != 0) { - wpa_printf(MSG_ERROR, "Seeding entropy source failed"); - goto cleanup; - } - len_prime = ACCESS_ECDH(ctx, grp).pbits/8; - bn_x = crypto_bignum_init_set(key, len); + /* Seed and setup CTR_DRBG entropy source for future reseeds */ + if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) != 0) { + wpa_printf(MSG_ERROR, "Seeding entropy source failed"); + goto cleanup; + } + len_prime = ACCESS_ECDH(ctx, grp).pbits / 8; + bn_x = crypto_bignum_init_set(key, len); - /* Initialize data for EC point */ - ec_pt = crypto_ec_point_init((struct crypto_ec*)ACCESS_ECDH(&ctx, grp)); - if (!ec_pt) { - wpa_printf(MSG_ERROR,"Initializing for EC point failed"); - goto cleanup; - } + /* Initialize data for EC point */ + ec_pt = crypto_ec_point_init((struct crypto_ec*)ACCESS_ECDH(&ctx, grp)); + if (!ec_pt) { + wpa_printf(MSG_ERROR, "Initializing for EC point failed"); + goto cleanup; + } - if (crypto_ec_point_solve_y_coord((struct crypto_ec*)ACCESS_ECDH(&ctx, grp), ec_pt, bn_x, inc_y) != 0) { - wpa_printf(MSG_ERROR,"Failed to solve for y coordinate"); - goto cleanup; - } - px = os_zalloc(len); - py = os_zalloc(len); - buf = os_zalloc(2*len); + if (crypto_ec_point_solve_y_coord((struct crypto_ec *)ACCESS_ECDH(&ctx, grp), ec_pt, bn_x, inc_y) != 0) { + wpa_printf(MSG_ERROR, "Failed to solve for y coordinate"); + goto cleanup; + } + px = os_zalloc(len); + py = os_zalloc(len); + buf = os_zalloc(2 * len); - if (!px || !py || !buf) { - wpa_printf(MSG_ERROR, "Memory allocation failed"); - goto cleanup; - } - if (crypto_ec_point_to_bin((struct crypto_ec*)ACCESS_ECDH(&ctx, grp), ec_pt, px, py) != 0) { - wpa_printf(MSG_ERROR,"Failed to write EC point value as binary data"); - goto cleanup; - } + if (!px || !py || !buf) { + wpa_printf(MSG_ERROR, "Memory allocation failed"); + goto cleanup; + } + if (crypto_ec_point_to_bin((struct crypto_ec *)ACCESS_ECDH(&ctx, grp), ec_pt, px, py) != 0) { + wpa_printf(MSG_ERROR, "Failed to write EC point value as binary data"); + goto cleanup; + } - os_memcpy(buf, px, len); - os_memcpy(buf+len, py, len); + os_memcpy(buf, px, len); + os_memcpy(buf + len, py, len); - pkey = crypto_ec_set_pubkey_point((struct crypto_ec_group*)ACCESS_ECDH(&ctx, grp), buf, len); - if (!pkey) { - wpa_printf(MSG_ERROR, "Failed to set point for peer's public key"); - goto cleanup; - } + pkey = crypto_ec_set_pubkey_point((struct crypto_ec_group*)ACCESS_ECDH(&ctx, grp), buf, len); + if (!pkey) { + wpa_printf(MSG_ERROR, "Failed to set point for peer's public key"); + goto cleanup; + } + mbedtls_pk_context *peer = (mbedtls_pk_context*)pkey; - mbedtls_pk_context *peer = (mbedtls_pk_context*)pkey; - - /* Setup ECDH context from EC key */ -/* Call to mbedtls_ecdh_get_params() will initialize the context when not LEGACY context */ - if (ctx != NULL && peer != NULL) { - mbedtls_ecp_copy( ACCESS_ECDH(&ctx, Qp), &(mbedtls_pk_ec(*peer))->MBEDTLS_PRIVATE(Q) ); + /* Setup ECDH context from EC key */ + /* Call to mbedtls_ecdh_get_params() will initialize the context when not LEGACY context */ + if (ctx != NULL && peer != NULL) { + mbedtls_ecp_copy(ACCESS_ECDH(&ctx, Qp), &(mbedtls_pk_ec(*peer))->MBEDTLS_PRIVATE(Q)); #ifndef CONFIG_MBEDTLS_ECDH_LEGACY_CONTEXT - ctx->MBEDTLS_PRIVATE(var) = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0; + ctx->MBEDTLS_PRIVATE(var) = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0; #endif - } else { - wpa_printf(MSG_ERROR, "Failed to set peer's ECDH context"); - goto cleanup; - } - int len_secret = inc_y ? 2*len : len; - secret = os_zalloc(len_secret); - if (!secret) { - wpa_printf(MSG_ERROR, "Allocation failed for secret"); - goto cleanup; - } + } else { + wpa_printf(MSG_ERROR, "Failed to set peer's ECDH context"); + goto cleanup; + } + int len_secret = inc_y ? 2 * len : len; + secret = os_zalloc(len_secret); + if (!secret) { + wpa_printf(MSG_ERROR, "Allocation failed for secret"); + goto cleanup; + } - /* Calculate secret - z = F(DH(x,Y)) */ - secret_key = mbedtls_ecdh_calc_secret(ctx, &olen, secret, len_prime, mbedtls_ctr_drbg_random, &ctr_drbg); - if (secret_key != 0) { - wpa_printf(MSG_ERROR, "Calculation of secret failed"); - goto cleanup; - } - sh_secret = wpabuf_alloc_copy(secret, len_secret); + /* Calculate secret + z = F(DH(x,Y)) */ + secret_key = mbedtls_ecdh_calc_secret(ctx, &olen, secret, len_prime, mbedtls_ctr_drbg_random, &ctr_drbg); + if (secret_key != 0) { + wpa_printf(MSG_ERROR, "Calculation of secret failed"); + goto cleanup; + } + sh_secret = wpabuf_alloc_copy(secret, len_secret); cleanup: - os_free(px); - os_free(py); - os_free(buf); - os_free(secret); - crypto_ec_free_key(pkey); - crypto_bignum_deinit(bn_x, 1); - crypto_ec_point_deinit(ec_pt, 1); - mbedtls_ctr_drbg_free(&ctr_drbg); - mbedtls_entropy_free(&entropy); - return sh_secret; + os_free(px); + os_free(py); + os_free(buf); + os_free(secret); + crypto_ec_free_key(pkey); + crypto_bignum_deinit(bn_x, 1); + crypto_ec_point_deinit(ec_pt, 1); + mbedtls_ctr_drbg_free(&ctr_drbg); + mbedtls_entropy_free(&entropy); + return sh_secret; } - struct crypto_ec_key *crypto_ec_key_parse_pub(const u8 *der, size_t der_len) { - int ret; - mbedtls_pk_context *pkey = os_zalloc(sizeof(*pkey)); + int ret; + mbedtls_pk_context *pkey = os_zalloc(sizeof(*pkey)); - if (!pkey) { - return NULL; - } + if (!pkey) { + return NULL; + } - mbedtls_pk_init(pkey); - ret = mbedtls_pk_parse_public_key(pkey, der, der_len); + mbedtls_pk_init(pkey); + ret = mbedtls_pk_parse_public_key(pkey, der, der_len); - if (ret < 0) { - wpa_printf(MSG_ERROR, "failed to parse ec public key"); - os_free(pkey); - return NULL; - } - return (struct crypto_ec_key *)pkey; + if (ret < 0) { + wpa_printf(MSG_ERROR, "failed to parse ec public key"); + os_free(pkey); + return NULL; + } + return (struct crypto_ec_key *)pkey; } - void crypto_ec_key_deinit(struct crypto_ec_key *key) { - mbedtls_pk_free((mbedtls_pk_context *)key); - os_free(key); + mbedtls_pk_free((mbedtls_pk_context *)key); + os_free(key); } int crypto_ec_key_verify_signature(struct crypto_ec_key *key, const u8 *data, - size_t len, const u8 *sig, size_t sig_len) + size_t len, const u8 *sig, size_t sig_len) { - int ret = 0; + int ret = 0; - mbedtls_ecdsa_context *ctx_verify = os_malloc(sizeof(mbedtls_ecdsa_context)); - if (ctx_verify == NULL) { - return -1; - } + mbedtls_ecdsa_context *ctx_verify = os_malloc(sizeof(mbedtls_ecdsa_context)); + if (ctx_verify == NULL) { + return -1; + } - mbedtls_ecdsa_init(ctx_verify); + mbedtls_ecdsa_init(ctx_verify); - mbedtls_ecp_keypair *ec_key = mbedtls_pk_ec(*((mbedtls_pk_context *)key)); - mbedtls_ecp_group *grp = &ec_key->MBEDTLS_PRIVATE(grp); + mbedtls_ecp_keypair *ec_key = mbedtls_pk_ec(*((mbedtls_pk_context *)key)); + mbedtls_ecp_group *grp = &ec_key->MBEDTLS_PRIVATE(grp); - if ((ret = mbedtls_ecp_group_copy(&ctx_verify->MBEDTLS_PRIVATE(grp),grp)) != 0) { - goto cleanup; - } + if ((ret = mbedtls_ecp_group_copy(&ctx_verify->MBEDTLS_PRIVATE(grp), grp)) != 0) { + goto cleanup; + } - if ((ret = mbedtls_ecp_copy(&ctx_verify->MBEDTLS_PRIVATE(Q), &ec_key->MBEDTLS_PRIVATE(Q))) != 0) { - goto cleanup; - } + if ((ret = mbedtls_ecp_copy(&ctx_verify->MBEDTLS_PRIVATE(Q), &ec_key->MBEDTLS_PRIVATE(Q))) != 0) { + goto cleanup; + } - if ((ret = mbedtls_ecdsa_read_signature(ctx_verify, - data, len, - sig, sig_len)) != 0) { - goto cleanup; - } - ret = 1; + if ((ret = mbedtls_ecdsa_read_signature(ctx_verify, + data, len, + sig, sig_len)) != 0) { + goto cleanup; + } + ret = 1; cleanup: - mbedtls_ecdsa_free(ctx_verify); - os_free(ctx_verify); - return ret; + mbedtls_ecdsa_free(ctx_verify); + os_free(ctx_verify); + return ret; } - #endif /* CONFIG_ECC */ diff --git a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-rsa.c b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-rsa.c index 21f8f5956b..5eb0220fd0 100644 --- a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-rsa.c +++ b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-rsa.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -29,10 +29,10 @@ struct crypto_private_key; #ifdef DEBUG_PRINT static void crypto_dump_verify_info(u32 flags) { - char dump_buffer[1024]; + char dump_buffer[1024]; - mbedtls_x509_crt_verify_info(dump_buffer, 1024, " ! ", flags ); - wpa_printf(MSG_ERROR, "%s", dump_buffer); + mbedtls_x509_crt_verify_info(dump_buffer, 1024, " ! ", flags); + wpa_printf(MSG_ERROR, "%s", dump_buffer); } #else static void crypto_dump_verify_info(u32 flags) { } @@ -40,365 +40,377 @@ static void crypto_dump_verify_info(u32 flags) { } static int crypto_rng_wrapper(void *ctx, unsigned char *buf, size_t len) { - return os_get_random(buf, len); + return os_get_random(buf, len); } int crypto_verify_cert(const u8 *cert_start, int certlen, const u8 *ca_cert_start, int ca_certlen) { - int ret; - u32 flags = 0; + int ret; + u32 flags = 0; - mbedtls_x509_crt *cert = os_zalloc(sizeof(mbedtls_x509_crt)); - mbedtls_x509_crt *ca_cert = os_zalloc(sizeof(mbedtls_x509_crt)); + mbedtls_x509_crt *cert = os_zalloc(sizeof(mbedtls_x509_crt)); + mbedtls_x509_crt *ca_cert = os_zalloc(sizeof(mbedtls_x509_crt)); - if (!cert || !ca_cert) { - if (cert) - os_free(cert); - if (ca_cert) - os_free(ca_cert); - wpa_printf(MSG_ERROR, "%s: memory allocation failed", __func__); - return -1; - } - mbedtls_x509_crt_init(cert); - mbedtls_x509_crt_init(ca_cert); - ret = mbedtls_x509_crt_parse(cert, cert_start, certlen); - if (ret < 0) { - wpa_printf(MSG_ERROR, "peer cert parsing failed"); - goto cleanup; - } - ret = mbedtls_x509_crt_parse(ca_cert, ca_cert_start, ca_certlen); - if (ret < 0) { - wpa_printf(MSG_ERROR, "CA cert parsing failed"); - goto cleanup; - } + if (!cert || !ca_cert) { + if (cert) { + os_free(cert); + } + if (ca_cert) { + os_free(ca_cert); + } + wpa_printf(MSG_ERROR, "%s: memory allocation failed", __func__); + return -1; + } + mbedtls_x509_crt_init(cert); + mbedtls_x509_crt_init(ca_cert); + ret = mbedtls_x509_crt_parse(cert, cert_start, certlen); + if (ret < 0) { + wpa_printf(MSG_ERROR, "peer cert parsing failed"); + goto cleanup; + } + ret = mbedtls_x509_crt_parse(ca_cert, ca_cert_start, ca_certlen); + if (ret < 0) { + wpa_printf(MSG_ERROR, "CA cert parsing failed"); + goto cleanup; + } - ret = mbedtls_x509_crt_verify(cert, ca_cert, NULL, NULL, &flags, NULL, NULL ); + ret = mbedtls_x509_crt_verify(cert, ca_cert, NULL, NULL, &flags, NULL, NULL); - /* Certification is failed, try to get some more info */ - if (ret != 0) - crypto_dump_verify_info(flags); + /* Certification is failed, try to get some more info */ + if (ret != 0) { + crypto_dump_verify_info(flags); + } cleanup: - mbedtls_x509_crt_free(cert); - mbedtls_x509_crt_free(ca_cert); + mbedtls_x509_crt_free(cert); + mbedtls_x509_crt_free(ca_cert); - os_free(cert); - os_free(ca_cert); + os_free(cert); + os_free(ca_cert); - return ret; + return ret; } struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len) { - int ret; - mbedtls_pk_context *pkey = os_zalloc(sizeof(*pkey)); + int ret; + mbedtls_pk_context *pkey = os_zalloc(sizeof(*pkey)); - if (!pkey) - return NULL; + if (!pkey) { + return NULL; + } - mbedtls_pk_init(pkey); - ret = mbedtls_pk_parse_public_key(pkey, key, len); + mbedtls_pk_init(pkey); + ret = mbedtls_pk_parse_public_key(pkey, key, len); - if (ret < 0) { - wpa_printf(MSG_ERROR, "failed to parse public key"); - os_free(pkey); - return NULL; - } + if (ret < 0) { + wpa_printf(MSG_ERROR, "failed to parse public key"); + os_free(pkey); + return NULL; + } - return (struct crypto_public_key *)pkey; + return (struct crypto_public_key *)pkey; } struct crypto_private_key * crypto_private_key_import(const u8 *key, - size_t len, - const char *passwd) + size_t len, + const char *passwd) { - int ret; - mbedtls_pk_context *pkey = os_zalloc(sizeof(mbedtls_pk_context)); - if (!pkey) - return NULL; + int ret; + mbedtls_pk_context *pkey = os_zalloc(sizeof(mbedtls_pk_context)); + if (!pkey) { + return NULL; + } - mbedtls_pk_init(pkey); + mbedtls_pk_init(pkey); - ret = mbedtls_pk_parse_key(pkey, key, len, (const unsigned char *)passwd, - passwd ? os_strlen(passwd) : 0, crypto_rng_wrapper, NULL); + ret = mbedtls_pk_parse_key(pkey, key, len, (const unsigned char *)passwd, + passwd ? os_strlen(passwd) : 0, crypto_rng_wrapper, NULL); - if (ret < 0) { - wpa_printf(MSG_ERROR, "failed to parse private key"); - os_free(pkey); - pkey = NULL; - } + if (ret < 0) { + wpa_printf(MSG_ERROR, "failed to parse private key"); + os_free(pkey); + pkey = NULL; + } - return (struct crypto_private_key *)pkey; + return (struct crypto_private_key *)pkey; } struct crypto_public_key *crypto_public_key_from_cert(const u8 *buf, - size_t len) + size_t len) { - int ret; - mbedtls_x509_crt *cert; - mbedtls_pk_context *kctx = os_zalloc(sizeof(*kctx)); + int ret; + mbedtls_x509_crt *cert; + mbedtls_pk_context *kctx = os_zalloc(sizeof(*kctx)); - if (!kctx) { - wpa_printf(MSG_ERROR, "failed to allocate memory"); - return NULL; - } + if (!kctx) { + wpa_printf(MSG_ERROR, "failed to allocate memory"); + return NULL; + } - cert = os_zalloc(sizeof(mbedtls_x509_crt)); - if (!cert) { - wpa_printf(MSG_ERROR, "failed to allocate memory"); - goto fail; - } - mbedtls_x509_crt_init(cert); + cert = os_zalloc(sizeof(mbedtls_x509_crt)); + if (!cert) { + wpa_printf(MSG_ERROR, "failed to allocate memory"); + goto fail; + } + mbedtls_x509_crt_init(cert); - ret = mbedtls_x509_crt_parse(cert, buf, len); - if (ret < 0) { - wpa_printf(MSG_ERROR, "cert parsing failed"); - goto fail; - } + ret = mbedtls_x509_crt_parse(cert, buf, len); + if (ret < 0) { + wpa_printf(MSG_ERROR, "cert parsing failed"); + goto fail; + } - mbedtls_pk_init(kctx); + mbedtls_pk_init(kctx); - if(mbedtls_pk_setup(kctx, mbedtls_pk_info_from_type(mbedtls_pk_get_type(&cert->pk))) != 0) { - wpa_printf(MSG_ERROR, "key setup failed"); - goto fail; - } - ret = mbedtls_rsa_copy(mbedtls_pk_rsa(*kctx), mbedtls_pk_rsa(cert->pk)); + if (mbedtls_pk_setup(kctx, mbedtls_pk_info_from_type(mbedtls_pk_get_type(&cert->pk))) != 0) { + wpa_printf(MSG_ERROR, "key setup failed"); + goto fail; + } + ret = mbedtls_rsa_copy(mbedtls_pk_rsa(*kctx), mbedtls_pk_rsa(cert->pk)); - if (ret < 0) { - wpa_printf(MSG_ERROR, "key copy failed"); - goto fail; - } + if (ret < 0) { + wpa_printf(MSG_ERROR, "key copy failed"); + goto fail; + } cleanup: - mbedtls_x509_crt_free(cert); - os_free(cert); - return (struct crypto_public_key *)kctx; + mbedtls_x509_crt_free(cert); + os_free(cert); + return (struct crypto_public_key *)kctx; fail: - os_free(kctx); - kctx = NULL; - goto cleanup; + os_free(kctx); + kctx = NULL; + goto cleanup; } int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key, - const u8 *in, size_t inlen, - u8 *out, size_t *outlen) + const u8 *in, size_t inlen, + u8 *out, size_t *outlen) { - int ret; - mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; - const char *pers = "rsa_encrypt"; - mbedtls_entropy_context *entropy = os_zalloc(sizeof(*entropy)); - mbedtls_ctr_drbg_context *ctr_drbg = os_zalloc(sizeof(*ctr_drbg)); + int ret; + mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; + const char *pers = "rsa_encrypt"; + mbedtls_entropy_context *entropy = os_zalloc(sizeof(*entropy)); + mbedtls_ctr_drbg_context *ctr_drbg = os_zalloc(sizeof(*ctr_drbg)); - if (!pkey || !entropy || !ctr_drbg) { - if (entropy) - os_free(entropy); - if (ctr_drbg) - os_free(ctr_drbg); - wpa_printf(MSG_ERROR, "failed to allocate memory"); - return -1; - } + if (!pkey || !entropy || !ctr_drbg) { + if (entropy) { + os_free(entropy); + } + if (ctr_drbg) { + os_free(ctr_drbg); + } + wpa_printf(MSG_ERROR, "failed to allocate memory"); + return -1; + } - mbedtls_entropy_init( entropy ); - mbedtls_ctr_drbg_init( ctr_drbg ); + mbedtls_entropy_init(entropy); + mbedtls_ctr_drbg_init(ctr_drbg); - ret = mbedtls_ctr_drbg_seed( ctr_drbg, mbedtls_entropy_func, - entropy, (const unsigned char *) pers, - strlen( pers ) ); - if( ret != 0 ) { - wpa_printf(MSG_ERROR, " failed ! mbedtls_ctr_drbg_seed returned %d", - ret ); - goto cleanup; - } + ret = mbedtls_ctr_drbg_seed(ctr_drbg, mbedtls_entropy_func, + entropy, (const unsigned char *) pers, + strlen(pers)); + if (ret != 0) { + wpa_printf(MSG_ERROR, " failed ! mbedtls_ctr_drbg_seed returned %d", + ret); + goto cleanup; + } - ret = mbedtls_rsa_pkcs1_encrypt(mbedtls_pk_rsa(*pkey), mbedtls_ctr_drbg_random, - ctr_drbg, inlen, in, out); + ret = mbedtls_rsa_pkcs1_encrypt(mbedtls_pk_rsa(*pkey), mbedtls_ctr_drbg_random, + ctr_drbg, inlen, in, out); - if(ret != 0) { - wpa_printf(MSG_ERROR, " failed ! mbedtls_rsa_pkcs1_encrypt returned -0x%04x", -ret); - goto cleanup; - } - *outlen = mbedtls_rsa_get_len(mbedtls_pk_rsa(*pkey)); + if (ret != 0) { + wpa_printf(MSG_ERROR, " failed ! mbedtls_rsa_pkcs1_encrypt returned -0x%04x", -ret); + goto cleanup; + } + *outlen = mbedtls_rsa_get_len(mbedtls_pk_rsa(*pkey)); cleanup: - mbedtls_ctr_drbg_free( ctr_drbg ); - mbedtls_entropy_free( entropy ); - os_free(entropy); - os_free(ctr_drbg); + mbedtls_ctr_drbg_free(ctr_drbg); + mbedtls_entropy_free(entropy); + os_free(entropy); + os_free(ctr_drbg); - return ret; + return ret; } - int crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key *key, - const u8 *in, size_t inlen, - u8 *out, size_t *outlen) + const u8 *in, size_t inlen, + u8 *out, size_t *outlen) { - int ret; - size_t i; - mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; - const char *pers = "rsa_decrypt"; - mbedtls_entropy_context *entropy = os_malloc(sizeof(*entropy)); - mbedtls_ctr_drbg_context *ctr_drbg = os_malloc(sizeof(*ctr_drbg)); + int ret; + size_t i; + mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; + const char *pers = "rsa_decrypt"; + mbedtls_entropy_context *entropy = os_malloc(sizeof(*entropy)); + mbedtls_ctr_drbg_context *ctr_drbg = os_malloc(sizeof(*ctr_drbg)); - if (!pkey || !entropy || !ctr_drbg) { - if (entropy) - os_free(entropy); - if (ctr_drbg) - os_free(ctr_drbg); - return -1; - } - mbedtls_ctr_drbg_init( ctr_drbg ); - mbedtls_entropy_init( entropy ); - ret = mbedtls_ctr_drbg_seed(ctr_drbg, mbedtls_entropy_func, - entropy, (const unsigned char *) pers, - strlen(pers)); + if (!pkey || !entropy || !ctr_drbg) { + if (entropy) { + os_free(entropy); + } + if (ctr_drbg) { + os_free(ctr_drbg); + } + return -1; + } + mbedtls_ctr_drbg_init(ctr_drbg); + mbedtls_entropy_init(entropy); + ret = mbedtls_ctr_drbg_seed(ctr_drbg, mbedtls_entropy_func, + entropy, (const unsigned char *) pers, + strlen(pers)); - if (ret < 0) - goto cleanup; + if (ret < 0) { + goto cleanup; + } - i = mbedtls_rsa_get_len(mbedtls_pk_rsa(*pkey)); - ret = mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_pk_rsa(*pkey), mbedtls_ctr_drbg_random, - ctr_drbg, &i, in, out, *outlen); + i = mbedtls_rsa_get_len(mbedtls_pk_rsa(*pkey)); + ret = mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_pk_rsa(*pkey), mbedtls_ctr_drbg_random, + ctr_drbg, &i, in, out, *outlen); - *outlen = i; + *outlen = i; cleanup: - mbedtls_ctr_drbg_free( ctr_drbg ); - mbedtls_entropy_free( entropy ); - os_free(entropy); - os_free(ctr_drbg); + mbedtls_ctr_drbg_free(ctr_drbg); + mbedtls_entropy_free(entropy); + os_free(entropy); + os_free(ctr_drbg); - return ret; + return ret; } - int crypto_private_key_sign_pkcs1(struct crypto_private_key *key, - const u8 *in, size_t inlen, - u8 *out, size_t *outlen) + const u8 *in, size_t inlen, + u8 *out, size_t *outlen) { - int ret; - const char *pers = "rsa_encrypt"; - mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; - mbedtls_entropy_context *entropy = os_malloc(sizeof(*entropy)); - mbedtls_ctr_drbg_context *ctr_drbg = os_malloc(sizeof(*ctr_drbg)); + int ret; + const char *pers = "rsa_encrypt"; + mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; + mbedtls_entropy_context *entropy = os_malloc(sizeof(*entropy)); + mbedtls_ctr_drbg_context *ctr_drbg = os_malloc(sizeof(*ctr_drbg)); - if (!pkey || !entropy || !ctr_drbg) { - if (entropy) - os_free(entropy); - if (ctr_drbg) - os_free(ctr_drbg); - return -1; - } - mbedtls_ctr_drbg_init( ctr_drbg ); - mbedtls_entropy_init( entropy ); - ret = mbedtls_ctr_drbg_seed(ctr_drbg, mbedtls_entropy_func, - entropy, (const unsigned char *) pers, - strlen(pers)); + if (!pkey || !entropy || !ctr_drbg) { + if (entropy) { + os_free(entropy); + } + if (ctr_drbg) { + os_free(ctr_drbg); + } + return -1; + } + mbedtls_ctr_drbg_init(ctr_drbg); + mbedtls_entropy_init(entropy); + ret = mbedtls_ctr_drbg_seed(ctr_drbg, mbedtls_entropy_func, + entropy, (const unsigned char *) pers, + strlen(pers)); - if((ret = mbedtls_rsa_pkcs1_sign(mbedtls_pk_rsa(*pkey), mbedtls_ctr_drbg_random, ctr_drbg, - (mbedtls_pk_rsa(*pkey))->MBEDTLS_PRIVATE(hash_id), - inlen, in, out)) != 0 ) { - wpa_printf(MSG_ERROR, " failed ! mbedtls_rsa_pkcs1_sign returned %d", ret ); - goto cleanup; - } - *outlen = mbedtls_rsa_get_len(mbedtls_pk_rsa(*pkey)); + if ((ret = mbedtls_rsa_pkcs1_sign(mbedtls_pk_rsa(*pkey), mbedtls_ctr_drbg_random, ctr_drbg, + (mbedtls_pk_rsa(*pkey))->MBEDTLS_PRIVATE(hash_id), + inlen, in, out)) != 0) { + wpa_printf(MSG_ERROR, " failed ! mbedtls_rsa_pkcs1_sign returned %d", ret); + goto cleanup; + } + *outlen = mbedtls_rsa_get_len(mbedtls_pk_rsa(*pkey)); cleanup: - mbedtls_ctr_drbg_free( ctr_drbg ); - mbedtls_entropy_free( entropy ); - os_free(entropy); - os_free(ctr_drbg); - return ret; + mbedtls_ctr_drbg_free(ctr_drbg); + mbedtls_entropy_free(entropy); + os_free(entropy); + os_free(ctr_drbg); + return ret; } - void crypto_public_key_free(struct crypto_public_key *key) { - mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; - if (!pkey) - return; + mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; + if (!pkey) { + return; + } - mbedtls_pk_free(pkey); - os_free(pkey); + mbedtls_pk_free(pkey); + os_free(pkey); } - void crypto_private_key_free(struct crypto_private_key *key) { - mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; - if (!pkey) - return; + mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; + if (!pkey) { + return; + } - mbedtls_pk_free(pkey); - os_free(pkey); + mbedtls_pk_free(pkey); + os_free(pkey); } int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key, - const u8 *crypt, size_t crypt_len, - u8 *plain, size_t *plain_len) + const u8 *crypt, size_t crypt_len, + u8 *plain, size_t *plain_len) { - size_t len; - u8 *pos; - mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; - len = mbedtls_pk_rsa(*pkey)->MBEDTLS_PRIVATE(len); - if (len != crypt_len) { - return -1; - } + size_t len; + u8 *pos; + mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; + len = mbedtls_pk_rsa(*pkey)->MBEDTLS_PRIVATE(len); + if (len != crypt_len) { + return -1; + } - if (mbedtls_rsa_public(mbedtls_pk_rsa(*pkey), crypt, plain) < 0) - return -1; + if (mbedtls_rsa_public(mbedtls_pk_rsa(*pkey), crypt, plain) < 0) { + return -1; + } - /* - * PKCS #1 v1.5, 8.1: - * - * EB = 00 || BT || PS || 00 || D - * BT = 00 or 01 - * PS = k-3-||D|| times (00 if BT=00) or (FF if BT=01) - * k = length of modulus in octets - * - * Based on 10.1.3, "The block type shall be 01" for a signature. - */ + /* + * PKCS #1 v1.5, 8.1: + * + * EB = 00 || BT || PS || 00 || D + * BT = 00 or 01 + * PS = k-3-||D|| times (00 if BT=00) or (FF if BT=01) + * k = length of modulus in octets + * + * Based on 10.1.3, "The block type shall be 01" for a signature. + */ - if (len < 3 + 8 + 16 /* min hash len */ || - plain[0] != 0x00 || plain[1] != 0x01) { - wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB " - "structure"); - wpa_hexdump_key(MSG_DEBUG, "Signature EB", plain, len); - return -1; - } + if (len < 3 + 8 + 16 /* min hash len */ || + plain[0] != 0x00 || plain[1] != 0x01) { + wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB " + "structure"); + wpa_hexdump_key(MSG_DEBUG, "Signature EB", plain, len); + return -1; + } - pos = plain + 3; - /* BT = 01 */ - if (plain[2] != 0xff) { - wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature " - "PS (BT=01)"); - wpa_hexdump_key(MSG_DEBUG, "Signature EB", plain, len); - return -1; - } - while (pos < plain + len && *pos == 0xff) - pos++; + pos = plain + 3; + /* BT = 01 */ + if (plain[2] != 0xff) { + wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature " + "PS (BT=01)"); + wpa_hexdump_key(MSG_DEBUG, "Signature EB", plain, len); + return -1; + } + while (pos < plain + len && *pos == 0xff) { + pos++; + } - if (pos - plain - 2 < 8) { - /* PKCS #1 v1.5, 8.1: At least eight octets long PS */ - wpa_printf(MSG_INFO, "LibTomCrypt: Too short signature " - "padding"); - wpa_hexdump_key(MSG_DEBUG, "Signature EB", plain, len); - return -1; - } + if (pos - plain - 2 < 8) { + /* PKCS #1 v1.5, 8.1: At least eight octets long PS */ + wpa_printf(MSG_INFO, "LibTomCrypt: Too short signature " + "padding"); + wpa_hexdump_key(MSG_DEBUG, "Signature EB", plain, len); + return -1; + } - if (pos + 16 /* min hash len */ >= plain + len || *pos != 0x00) { - wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB " - "structure (2)"); - wpa_hexdump_key(MSG_DEBUG, "Signature EB", plain, len); - return -1; - } - pos++; - len -= pos - plain; + if (pos + 16 /* min hash len */ >= plain + len || *pos != 0x00) { + wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB " + "structure (2)"); + wpa_hexdump_key(MSG_DEBUG, "Signature EB", plain, len); + return -1; + } + pos++; + len -= pos - plain; - /* Strip PKCS #1 header */ - os_memmove(plain, pos, len); - *plain_len = len; + /* Strip PKCS #1 header */ + os_memmove(plain, pos, len); + *plain_len = len; - return 0; + return 0; } #endif diff --git a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls.c b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls.c index 97b0df675f..b2679a04ea 100644 --- a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls.c +++ b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -41,919 +41,917 @@ #endif static int digest_vector(mbedtls_md_type_t md_type, size_t num_elem, - const u8 *addr[], const size_t *len, u8 *mac) + const u8 *addr[], const size_t *len, u8 *mac) { - size_t i; - const mbedtls_md_info_t *md_info; - mbedtls_md_context_t md_ctx; - int ret; + size_t i; + const mbedtls_md_info_t *md_info; + mbedtls_md_context_t md_ctx; + int ret; - mbedtls_md_init(&md_ctx); + mbedtls_md_init(&md_ctx); - md_info = mbedtls_md_info_from_type(md_type); - if (!md_info) { - wpa_printf(MSG_ERROR, "mbedtls_md_info_from_type() failed"); - return -1; - } + md_info = mbedtls_md_info_from_type(md_type); + if (!md_info) { + wpa_printf(MSG_ERROR, "mbedtls_md_info_from_type() failed"); + return -1; + } - ret = mbedtls_md_setup(&md_ctx, md_info, 0); - if (ret != 0) { - wpa_printf(MSG_ERROR, "mbedtls_md_setup() returned error"); - goto cleanup; - } + ret = mbedtls_md_setup(&md_ctx, md_info, 0); + if (ret != 0) { + wpa_printf(MSG_ERROR, "mbedtls_md_setup() returned error"); + goto cleanup; + } - ret = mbedtls_md_starts(&md_ctx); - if (ret != 0) { - wpa_printf(MSG_ERROR, "mbedtls_md_starts returned error"); - goto cleanup; - } + ret = mbedtls_md_starts(&md_ctx); + if (ret != 0) { + wpa_printf(MSG_ERROR, "mbedtls_md_starts returned error"); + goto cleanup; + } - for (i = 0; i < num_elem; i++) { - ret = mbedtls_md_update(&md_ctx, addr[i], len[i]); - if (ret != 0) { - wpa_printf(MSG_ERROR, "mbedtls_md_update ret=%d", ret); - goto cleanup; - } - } + for (i = 0; i < num_elem; i++) { + ret = mbedtls_md_update(&md_ctx, addr[i], len[i]); + if (ret != 0) { + wpa_printf(MSG_ERROR, "mbedtls_md_update ret=%d", ret); + goto cleanup; + } + } - ret = mbedtls_md_finish(&md_ctx, mac); + ret = mbedtls_md_finish(&md_ctx, mac); cleanup: - mbedtls_md_free(&md_ctx); + mbedtls_md_free(&md_ctx); - return ret; + return ret; } int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len, - u8 *mac) + u8 *mac) { - return digest_vector(MBEDTLS_MD_SHA256, num_elem, addr, len, mac); + return digest_vector(MBEDTLS_MD_SHA256, num_elem, addr, len, mac); } int sha384_vector(size_t num_elem, const u8 *addr[], const size_t *len, - u8 *mac) + u8 *mac) { - return digest_vector(MBEDTLS_MD_SHA384, num_elem, addr, len, mac); + return digest_vector(MBEDTLS_MD_SHA384, num_elem, addr, len, mac); } int sha512_vector(size_t num_elem, const u8 *addr[], const size_t *len, - u8 *mac) + u8 *mac) { - return digest_vector(MBEDTLS_MD_SHA512, num_elem, addr, len, mac); + return digest_vector(MBEDTLS_MD_SHA512, num_elem, addr, len, mac); } int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) { - return digest_vector(MBEDTLS_MD_SHA1, num_elem, addr, len, mac); + return digest_vector(MBEDTLS_MD_SHA1, num_elem, addr, len, mac); } int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) { - return digest_vector(MBEDTLS_MD_MD5, num_elem, addr, len, mac); + return digest_vector(MBEDTLS_MD_MD5, num_elem, addr, len, mac); } #ifdef MBEDTLS_MD4_C int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) { - return digest_vector(MBEDTLS_MD_MD4, num_elem, addr, len, mac); + return digest_vector(MBEDTLS_MD_MD4, num_elem, addr, len, mac); } #endif struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key, - size_t key_len) + size_t key_len) { - mbedtls_md_context_t *ctx = NULL; - mbedtls_md_type_t md_type; - const mbedtls_md_info_t *md_info; - int ret; - int is_hmac = 0; + mbedtls_md_context_t *ctx = NULL; + mbedtls_md_type_t md_type; + const mbedtls_md_info_t *md_info; + int ret; + int is_hmac = 0; - switch (alg) { - case CRYPTO_HASH_ALG_MD5: - case CRYPTO_HASH_ALG_HMAC_MD5: - md_type = MBEDTLS_MD_MD5; - break; - case CRYPTO_HASH_ALG_SHA1: - case CRYPTO_HASH_ALG_HMAC_SHA1: - md_type = MBEDTLS_MD_SHA1; - break; - case CRYPTO_HASH_ALG_SHA256: - case CRYPTO_HASH_ALG_HMAC_SHA256: - md_type = MBEDTLS_MD_SHA256; - break; - case CRYPTO_HASH_ALG_SHA384: - md_type = MBEDTLS_MD_SHA384; - break; - case CRYPTO_HASH_ALG_SHA512: - md_type = MBEDTLS_MD_SHA512; - break; - default: - return NULL; - } + switch (alg) { + case CRYPTO_HASH_ALG_MD5: + case CRYPTO_HASH_ALG_HMAC_MD5: + md_type = MBEDTLS_MD_MD5; + break; + case CRYPTO_HASH_ALG_SHA1: + case CRYPTO_HASH_ALG_HMAC_SHA1: + md_type = MBEDTLS_MD_SHA1; + break; + case CRYPTO_HASH_ALG_SHA256: + case CRYPTO_HASH_ALG_HMAC_SHA256: + md_type = MBEDTLS_MD_SHA256; + break; + case CRYPTO_HASH_ALG_SHA384: + md_type = MBEDTLS_MD_SHA384; + break; + case CRYPTO_HASH_ALG_SHA512: + md_type = MBEDTLS_MD_SHA512; + break; + default: + return NULL; + } - switch (alg) { - case CRYPTO_HASH_ALG_HMAC_MD5: - case CRYPTO_HASH_ALG_HMAC_SHA1: - case CRYPTO_HASH_ALG_HMAC_SHA256: - is_hmac = 1; - break; - default: - break; - } - ctx = os_zalloc(sizeof(*ctx)); - if (ctx == NULL) { - return NULL; - } + switch (alg) { + case CRYPTO_HASH_ALG_HMAC_MD5: + case CRYPTO_HASH_ALG_HMAC_SHA1: + case CRYPTO_HASH_ALG_HMAC_SHA256: + is_hmac = 1; + break; + default: + break; + } + ctx = os_zalloc(sizeof(*ctx)); + if (ctx == NULL) { + return NULL; + } - mbedtls_md_init(ctx); - md_info = mbedtls_md_info_from_type(md_type); - if (!md_info) { - goto cleanup; - } - if (mbedtls_md_setup(ctx, md_info, is_hmac) != 0) { - goto cleanup; - } - if (is_hmac) { - ret = mbedtls_md_hmac_starts(ctx, key, key_len); - } else { - ret = mbedtls_md_starts(ctx); - } - if (ret < 0) { - goto cleanup; - } + mbedtls_md_init(ctx); + md_info = mbedtls_md_info_from_type(md_type); + if (!md_info) { + goto cleanup; + } + if (mbedtls_md_setup(ctx, md_info, is_hmac) != 0) { + goto cleanup; + } + if (is_hmac) { + ret = mbedtls_md_hmac_starts(ctx, key, key_len); + } else { + ret = mbedtls_md_starts(ctx); + } + if (ret < 0) { + goto cleanup; + } - return (struct crypto_hash *)ctx; + return (struct crypto_hash *)ctx; cleanup: - mbedtls_md_free(ctx); - os_free(ctx); - return NULL; + mbedtls_md_free(ctx); + os_free(ctx); + return NULL; } void crypto_hash_update(struct crypto_hash *crypto_ctx, const u8 *data, size_t len) { - int ret; - mbedtls_md_context_t *ctx = (mbedtls_md_context_t *)crypto_ctx; + int ret; + mbedtls_md_context_t *ctx = (mbedtls_md_context_t *)crypto_ctx; - if (ctx == NULL) { - return; - } - if (ctx->MBEDTLS_PRIVATE(hmac_ctx)) { - ret = mbedtls_md_hmac_update(ctx, data, len); - } else { - ret = mbedtls_md_update(ctx, data, len); - } - if (ret != 0) { - wpa_printf(MSG_ERROR, "%s: mbedtls_md_hmac_update failed", __func__); - } + if (ctx == NULL) { + return; + } + if (ctx->MBEDTLS_PRIVATE(hmac_ctx)) { + ret = mbedtls_md_hmac_update(ctx, data, len); + } else { + ret = mbedtls_md_update(ctx, data, len); + } + if (ret != 0) { + wpa_printf(MSG_ERROR, "%s: mbedtls_md_hmac_update failed", __func__); + } } int crypto_hash_finish(struct crypto_hash *crypto_ctx, u8 *mac, size_t *len) { - int ret = 0; - mbedtls_md_type_t md_type; - mbedtls_md_context_t *ctx = (mbedtls_md_context_t *)crypto_ctx; + int ret = 0; + mbedtls_md_type_t md_type; + mbedtls_md_context_t *ctx = (mbedtls_md_context_t *)crypto_ctx; - if (ctx == NULL) { - return -2; - } + if (ctx == NULL) { + return -2; + } - if (mac == NULL || len == NULL) { - goto err; - } + if (mac == NULL || len == NULL) { + goto err; + } - md_type = mbedtls_md_get_type(mbedtls_md_info_from_ctx(ctx)); - switch(md_type) { - case MBEDTLS_MD_MD5: - if (*len < MD5_MAC_LEN) { - *len = MD5_MAC_LEN; - ret = -1; - goto err; - } - *len = MD5_MAC_LEN; - break; - case MBEDTLS_MD_SHA1: - if (*len < SHA1_MAC_LEN) { - *len = SHA1_MAC_LEN; - ret = -1; - goto err; - } - *len = SHA1_MAC_LEN; - break; - case MBEDTLS_MD_SHA256: - if (*len < SHA256_MAC_LEN) { - *len = SHA256_MAC_LEN; - ret = -1; - goto err; - } - *len = SHA256_MAC_LEN; - break; - case MBEDTLS_MD_SHA384: - if (*len < SHA384_MAC_LEN) { - *len = SHA384_MAC_LEN; - ret = -1; - goto err; - } - *len = SHA384_MAC_LEN; - break; - case MBEDTLS_MD_SHA512: - if (*len < SHA512_MAC_LEN) { - *len = SHA512_MAC_LEN; - ret = -1; - goto err; - } - *len = SHA512_MAC_LEN; - break; - default: - *len = 0; - ret = -1; - goto err; - } - if (ctx->MBEDTLS_PRIVATE(hmac_ctx)) { - ret = mbedtls_md_hmac_finish(ctx, mac); - } else { - ret = mbedtls_md_finish(ctx, mac); - } + md_type = mbedtls_md_get_type(mbedtls_md_info_from_ctx(ctx)); + switch (md_type) { + case MBEDTLS_MD_MD5: + if (*len < MD5_MAC_LEN) { + *len = MD5_MAC_LEN; + ret = -1; + goto err; + } + *len = MD5_MAC_LEN; + break; + case MBEDTLS_MD_SHA1: + if (*len < SHA1_MAC_LEN) { + *len = SHA1_MAC_LEN; + ret = -1; + goto err; + } + *len = SHA1_MAC_LEN; + break; + case MBEDTLS_MD_SHA256: + if (*len < SHA256_MAC_LEN) { + *len = SHA256_MAC_LEN; + ret = -1; + goto err; + } + *len = SHA256_MAC_LEN; + break; + case MBEDTLS_MD_SHA384: + if (*len < SHA384_MAC_LEN) { + *len = SHA384_MAC_LEN; + ret = -1; + goto err; + } + *len = SHA384_MAC_LEN; + break; + case MBEDTLS_MD_SHA512: + if (*len < SHA512_MAC_LEN) { + *len = SHA512_MAC_LEN; + ret = -1; + goto err; + } + *len = SHA512_MAC_LEN; + break; + default: + *len = 0; + ret = -1; + goto err; + } + if (ctx->MBEDTLS_PRIVATE(hmac_ctx)) { + ret = mbedtls_md_hmac_finish(ctx, mac); + } else { + ret = mbedtls_md_finish(ctx, mac); + } err: - mbedtls_md_free(ctx); - bin_clear_free(ctx, sizeof(*ctx)); + mbedtls_md_free(ctx); + bin_clear_free(ctx, sizeof(*ctx)); - return ret; + return ret; } static int hmac_vector(mbedtls_md_type_t md_type, - const u8 *key, size_t key_len, - size_t num_elem, const u8 *addr[], - const size_t *len, u8 *mac) + const u8 *key, size_t key_len, + size_t num_elem, const u8 *addr[], + const size_t *len, u8 *mac) { - size_t i; - const mbedtls_md_info_t *md_info; - mbedtls_md_context_t md_ctx; - int ret; + size_t i; + const mbedtls_md_info_t *md_info; + mbedtls_md_context_t md_ctx; + int ret; - mbedtls_md_init(&md_ctx); + mbedtls_md_init(&md_ctx); - md_info = mbedtls_md_info_from_type(md_type); - if (!md_info) { - return -1; - } + md_info = mbedtls_md_info_from_type(md_type); + if (!md_info) { + return -1; + } - ret = mbedtls_md_setup(&md_ctx, md_info, 1); - if (ret != 0) { - return(ret); - } + ret = mbedtls_md_setup(&md_ctx, md_info, 1); + if (ret != 0) { + return (ret); + } - ret = mbedtls_md_hmac_starts(&md_ctx, key, key_len); - if (ret != 0) { - return(ret); - } + ret = mbedtls_md_hmac_starts(&md_ctx, key, key_len); + if (ret != 0) { + return (ret); + } - for (i = 0; i < num_elem; i++) { - ret = mbedtls_md_hmac_update(&md_ctx, addr[i], len[i]); - if (ret != 0) { - return(ret); - } + for (i = 0; i < num_elem; i++) { + ret = mbedtls_md_hmac_update(&md_ctx, addr[i], len[i]); + if (ret != 0) { + return (ret); + } - } + } - ret = mbedtls_md_hmac_finish(&md_ctx, mac); + ret = mbedtls_md_hmac_finish(&md_ctx, mac); - mbedtls_md_free(&md_ctx); + mbedtls_md_free(&md_ctx); - return ret; + return ret; } int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem, - const u8 *addr[], const size_t *len, u8 *mac) + const u8 *addr[], const size_t *len, u8 *mac) { - return hmac_vector(MBEDTLS_MD_SHA384, key, key_len, num_elem, addr, - len, mac); + return hmac_vector(MBEDTLS_MD_SHA384, key, key_len, num_elem, addr, + len, mac); } - int hmac_sha384(const u8 *key, size_t key_len, const u8 *data, - size_t data_len, u8 *mac) + size_t data_len, u8 *mac) { - return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac); + return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac); } int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem, - const u8 *addr[], const size_t *len, u8 *mac) + const u8 *addr[], const size_t *len, u8 *mac) { - return hmac_vector(MBEDTLS_MD_SHA256, key, key_len, num_elem, addr, - len, mac); + return hmac_vector(MBEDTLS_MD_SHA256, key, key_len, num_elem, addr, + len, mac); } int hmac_sha256(const u8 *key, size_t key_len, const u8 *data, - size_t data_len, u8 *mac) + size_t data_len, u8 *mac) { - return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac); + return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac); } int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem, - const u8 *addr[], const size_t *len, u8 *mac) + const u8 *addr[], const size_t *len, u8 *mac) { - return hmac_vector(MBEDTLS_MD_MD5, key, key_len, - num_elem, addr, len, mac); + return hmac_vector(MBEDTLS_MD_MD5, key, key_len, + num_elem, addr, len, mac); } int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len, - u8 *mac) + u8 *mac) { - return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac); + return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac); } int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem, - const u8 *addr[], const size_t *len, u8 *mac) + const u8 *addr[], const size_t *len, u8 *mac) { - return hmac_vector(MBEDTLS_MD_SHA1, key, key_len, num_elem, addr, - len, mac); + return hmac_vector(MBEDTLS_MD_SHA1, key, key_len, num_elem, addr, + len, mac); } int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len, - u8 *mac) + u8 *mac) { - return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac); + return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac); } static void *aes_crypt_init(int mode, const u8 *key, size_t len) { - int ret = -1; - mbedtls_aes_context *aes = os_malloc(sizeof(*aes)); - if (!aes) { - return NULL; - } - mbedtls_aes_init(aes); + int ret = -1; + mbedtls_aes_context *aes = os_malloc(sizeof(*aes)); + if (!aes) { + return NULL; + } + mbedtls_aes_init(aes); - if (mode == MBEDTLS_AES_ENCRYPT) { - ret = mbedtls_aes_setkey_enc(aes, key, len * 8); - } else if (mode == MBEDTLS_AES_DECRYPT){ - ret = mbedtls_aes_setkey_dec(aes, key, len * 8); - } - if (ret < 0) { - mbedtls_aes_free(aes); - os_free(aes); - wpa_printf(MSG_ERROR, "%s: mbedtls_aes_setkey_enc/mbedtls_aes_setkey_dec failed", __func__); - return NULL; - } + if (mode == MBEDTLS_AES_ENCRYPT) { + ret = mbedtls_aes_setkey_enc(aes, key, len * 8); + } else if (mode == MBEDTLS_AES_DECRYPT) { + ret = mbedtls_aes_setkey_dec(aes, key, len * 8); + } + if (ret < 0) { + mbedtls_aes_free(aes); + os_free(aes); + wpa_printf(MSG_ERROR, "%s: mbedtls_aes_setkey_enc/mbedtls_aes_setkey_dec failed", __func__); + return NULL; + } - return (void *) aes; + return (void *) aes; } static int aes_crypt(void *ctx, int mode, const u8 *in, u8 *out) { - return mbedtls_aes_crypt_ecb((mbedtls_aes_context *)ctx, - mode, in, out); + return mbedtls_aes_crypt_ecb((mbedtls_aes_context *)ctx, + mode, in, out); } static void aes_crypt_deinit(void *ctx) { - mbedtls_aes_free((mbedtls_aes_context *)ctx); - os_free(ctx); + mbedtls_aes_free((mbedtls_aes_context *)ctx); + os_free(ctx); } void *aes_encrypt_init(const u8 *key, size_t len) { - return aes_crypt_init(MBEDTLS_AES_ENCRYPT, key, len); + return aes_crypt_init(MBEDTLS_AES_ENCRYPT, key, len); } int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt) { - return aes_crypt(ctx, MBEDTLS_AES_ENCRYPT, plain, crypt); + return aes_crypt(ctx, MBEDTLS_AES_ENCRYPT, plain, crypt); } void aes_encrypt_deinit(void *ctx) { - return aes_crypt_deinit(ctx); + return aes_crypt_deinit(ctx); } void * aes_decrypt_init(const u8 *key, size_t len) { - return aes_crypt_init(MBEDTLS_AES_DECRYPT, key, len); + return aes_crypt_init(MBEDTLS_AES_DECRYPT, key, len); } int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain) { - return aes_crypt(ctx, MBEDTLS_AES_DECRYPT, crypt, plain); + return aes_crypt(ctx, MBEDTLS_AES_DECRYPT, crypt, plain); } void aes_decrypt_deinit(void *ctx) { - return aes_crypt_deinit(ctx); + return aes_crypt_deinit(ctx); } int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len) { - int ret = 0; - mbedtls_aes_context ctx; - u8 cbc[MBEDTLS_AES_BLOCK_SIZE]; + int ret = 0; + mbedtls_aes_context ctx; + u8 cbc[MBEDTLS_AES_BLOCK_SIZE]; - mbedtls_aes_init(&ctx); + mbedtls_aes_init(&ctx); - ret = mbedtls_aes_setkey_enc(&ctx, key, 128); - if (ret < 0) { - mbedtls_aes_free(&ctx); - return ret; - } + ret = mbedtls_aes_setkey_enc(&ctx, key, 128); + if (ret < 0) { + mbedtls_aes_free(&ctx); + return ret; + } - os_memcpy(cbc, iv, MBEDTLS_AES_BLOCK_SIZE); - ret = mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT, - data_len, cbc, data, data); - mbedtls_aes_free(&ctx); + os_memcpy(cbc, iv, MBEDTLS_AES_BLOCK_SIZE); + ret = mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT, + data_len, cbc, data, data); + mbedtls_aes_free(&ctx); - return ret; + return ret; } int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len) { - int ret = 0; - mbedtls_aes_context ctx; - u8 cbc[MBEDTLS_AES_BLOCK_SIZE]; + int ret = 0; + mbedtls_aes_context ctx; + u8 cbc[MBEDTLS_AES_BLOCK_SIZE]; - mbedtls_aes_init(&ctx); + mbedtls_aes_init(&ctx); - ret = mbedtls_aes_setkey_dec(&ctx, key, 128); - if (ret < 0) { - mbedtls_aes_free(&ctx); - return ret; - } + ret = mbedtls_aes_setkey_dec(&ctx, key, 128); + if (ret < 0) { + mbedtls_aes_free(&ctx); + return ret; + } - os_memcpy(cbc, iv, MBEDTLS_AES_BLOCK_SIZE); - ret = mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_DECRYPT, - data_len, cbc, data, data); - mbedtls_aes_free(&ctx); + os_memcpy(cbc, iv, MBEDTLS_AES_BLOCK_SIZE); + ret = mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_DECRYPT, + data_len, cbc, data, data); + mbedtls_aes_free(&ctx); - return ret; + return ret; } #ifdef CONFIG_TLS_INTERNAL_CLIENT struct crypto_cipher { - mbedtls_cipher_context_t ctx_enc; - mbedtls_cipher_context_t ctx_dec; + mbedtls_cipher_context_t ctx_enc; + mbedtls_cipher_context_t ctx_dec; }; static int crypto_init_cipher_ctx(mbedtls_cipher_context_t *ctx, - const mbedtls_cipher_info_t *cipher_info, - const u8 *iv, const u8 *key, size_t key_len, - mbedtls_operation_t operation) + const mbedtls_cipher_info_t *cipher_info, + const u8 *iv, const u8 *key, size_t key_len, + mbedtls_operation_t operation) { - mbedtls_cipher_init(ctx); - int ret; + mbedtls_cipher_init(ctx); + int ret; - ret = mbedtls_cipher_setup(ctx, cipher_info); - if (ret != 0) { - return -1; - } + ret = mbedtls_cipher_setup(ctx, cipher_info); + if (ret != 0) { + return -1; + } - ret = mbedtls_cipher_setkey(ctx, key, key_len * 8, operation); - if (ret != 0) { - wpa_printf(MSG_ERROR, "mbedtls_cipher_setkey returned error=%d", ret); - return -1; - } - ret = mbedtls_cipher_set_iv(ctx, iv, cipher_info->MBEDTLS_PRIVATE(iv_size) << MBEDTLS_IV_SIZE_SHIFT); - if (ret != 0) { - wpa_printf(MSG_ERROR, "mbedtls_cipher_set_iv returned error=%d", ret); - return -1; - } - ret = mbedtls_cipher_reset(ctx); - if (ret != 0) { - wpa_printf(MSG_ERROR, "mbedtls_cipher_reset() returned error=%d", ret); - return -1; - } + ret = mbedtls_cipher_setkey(ctx, key, key_len * 8, operation); + if (ret != 0) { + wpa_printf(MSG_ERROR, "mbedtls_cipher_setkey returned error=%d", ret); + return -1; + } + ret = mbedtls_cipher_set_iv(ctx, iv, cipher_info->MBEDTLS_PRIVATE(iv_size) << MBEDTLS_IV_SIZE_SHIFT); + if (ret != 0) { + wpa_printf(MSG_ERROR, "mbedtls_cipher_set_iv returned error=%d", ret); + return -1; + } + ret = mbedtls_cipher_reset(ctx); + if (ret != 0) { + wpa_printf(MSG_ERROR, "mbedtls_cipher_reset() returned error=%d", ret); + return -1; + } - return 0; + return 0; } static mbedtls_cipher_type_t alg_to_mbedtls_cipher(enum crypto_cipher_alg alg, - size_t key_len) + size_t key_len) { - switch (alg) { - case CRYPTO_CIPHER_ALG_AES: - if (key_len == 16) { - return MBEDTLS_CIPHER_AES_128_CBC; - } - if (key_len == 24) { - return MBEDTLS_CIPHER_AES_192_CBC; - } - if (key_len == 32) { - return MBEDTLS_CIPHER_AES_256_CBC; - } - break; + switch (alg) { + case CRYPTO_CIPHER_ALG_AES: + if (key_len == 16) { + return MBEDTLS_CIPHER_AES_128_CBC; + } + if (key_len == 24) { + return MBEDTLS_CIPHER_AES_192_CBC; + } + if (key_len == 32) { + return MBEDTLS_CIPHER_AES_256_CBC; + } + break; #ifdef MBEDTLS_DES_C - case CRYPTO_CIPHER_ALG_3DES: - return MBEDTLS_CIPHER_DES_EDE3_CBC; - case CRYPTO_CIPHER_ALG_DES: - return MBEDTLS_CIPHER_DES_CBC; + case CRYPTO_CIPHER_ALG_3DES: + return MBEDTLS_CIPHER_DES_EDE3_CBC; + case CRYPTO_CIPHER_ALG_DES: + return MBEDTLS_CIPHER_DES_CBC; #endif - default: - break; - } + default: + break; + } - return MBEDTLS_CIPHER_NONE; + return MBEDTLS_CIPHER_NONE; } struct crypto_cipher *crypto_cipher_init(enum crypto_cipher_alg alg, - const u8 *iv, const u8 *key, - size_t key_len) + const u8 *iv, const u8 *key, + size_t key_len) { - struct crypto_cipher *ctx; - mbedtls_cipher_type_t cipher_type; - const mbedtls_cipher_info_t *cipher_info; + struct crypto_cipher *ctx; + mbedtls_cipher_type_t cipher_type; + const mbedtls_cipher_info_t *cipher_info; - ctx = (struct crypto_cipher *)os_zalloc(sizeof(*ctx)); - if (!ctx) { - return NULL; - } + ctx = (struct crypto_cipher *)os_zalloc(sizeof(*ctx)); + if (!ctx) { + return NULL; + } - cipher_type = alg_to_mbedtls_cipher(alg, key_len); - if (cipher_type == MBEDTLS_CIPHER_NONE) { - goto cleanup; - } + cipher_type = alg_to_mbedtls_cipher(alg, key_len); + if (cipher_type == MBEDTLS_CIPHER_NONE) { + goto cleanup; + } - cipher_info = mbedtls_cipher_info_from_type(cipher_type); - if (cipher_info == NULL) { - goto cleanup; - } + cipher_info = mbedtls_cipher_info_from_type(cipher_type); + if (cipher_info == NULL) { + goto cleanup; + } - /* Init both ctx encryption/decryption */ - if (crypto_init_cipher_ctx(&ctx->ctx_enc, cipher_info, iv, key, - key_len, MBEDTLS_ENCRYPT) < 0) { - goto cleanup; - } + /* Init both ctx encryption/decryption */ + if (crypto_init_cipher_ctx(&ctx->ctx_enc, cipher_info, iv, key, + key_len, MBEDTLS_ENCRYPT) < 0) { + goto cleanup; + } - if (crypto_init_cipher_ctx(&ctx->ctx_dec, cipher_info, iv, key, - key_len, MBEDTLS_DECRYPT) < 0) { - goto cleanup; - } + if (crypto_init_cipher_ctx(&ctx->ctx_dec, cipher_info, iv, key, + key_len, MBEDTLS_DECRYPT) < 0) { + goto cleanup; + } - if (mbedtls_cipher_set_padding_mode(&ctx->ctx_enc, MBEDTLS_PADDING_NONE) < 0) { - goto cleanup; - } - if (mbedtls_cipher_set_padding_mode(&ctx->ctx_dec, MBEDTLS_PADDING_NONE) < 0) { - goto cleanup; - } - return ctx; + if (mbedtls_cipher_set_padding_mode(&ctx->ctx_enc, MBEDTLS_PADDING_NONE) < 0) { + goto cleanup; + } + if (mbedtls_cipher_set_padding_mode(&ctx->ctx_dec, MBEDTLS_PADDING_NONE) < 0) { + goto cleanup; + } + return ctx; cleanup: - os_free(ctx); - return NULL; + os_free(ctx); + return NULL; } int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain, - u8 *crypt, size_t len) + u8 *crypt, size_t len) { - int ret; - size_t olen = 0; + int ret; + size_t olen = 0; - ret = mbedtls_cipher_update(&ctx->ctx_enc, plain, len, crypt, &olen); - if (ret != 0) { - return -1; - } + ret = mbedtls_cipher_update(&ctx->ctx_enc, plain, len, crypt, &olen); + if (ret != 0) { + return -1; + } - ret = mbedtls_cipher_finish(&ctx->ctx_enc, crypt + olen, &olen); - if (ret != 0) { - return -1; - } + ret = mbedtls_cipher_finish(&ctx->ctx_enc, crypt + olen, &olen); + if (ret != 0) { + return -1; + } - return 0; + return 0; } int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt, - u8 *plain, size_t len) + u8 *plain, size_t len) { - int ret; - size_t olen = 0; + int ret; + size_t olen = 0; - ret = mbedtls_cipher_update(&ctx->ctx_dec, crypt, len, plain, &olen); - if (ret != 0) { - return -1; - } + ret = mbedtls_cipher_update(&ctx->ctx_dec, crypt, len, plain, &olen); + if (ret != 0) { + return -1; + } - ret = mbedtls_cipher_finish(&ctx->ctx_dec, plain + olen, &olen); - if (ret != 0) { - return -1; - } + ret = mbedtls_cipher_finish(&ctx->ctx_dec, plain + olen, &olen); + if (ret != 0) { + return -1; + } - return 0; + return 0; } void crypto_cipher_deinit(struct crypto_cipher *ctx) { - mbedtls_cipher_free(&ctx->ctx_enc); - mbedtls_cipher_free(&ctx->ctx_dec); - os_free(ctx); + mbedtls_cipher_free(&ctx->ctx_enc); + mbedtls_cipher_free(&ctx->ctx_dec); + os_free(ctx); } #endif int aes_ctr_encrypt(const u8 *key, size_t key_len, const u8 *nonce, - u8 *data, size_t data_len) + u8 *data, size_t data_len) { - int ret; - mbedtls_aes_context ctx; - uint8_t stream_block[MBEDTLS_AES_BLOCK_SIZE]; - size_t offset = 0; + int ret; + mbedtls_aes_context ctx; + uint8_t stream_block[MBEDTLS_AES_BLOCK_SIZE]; + size_t offset = 0; - mbedtls_aes_init(&ctx); - ret = mbedtls_aes_setkey_enc(&ctx, key, key_len * 8); - if (ret < 0) { - goto cleanup; - } - ret = mbedtls_aes_crypt_ctr(&ctx, data_len, &offset, (u8 *)nonce, - stream_block, data, data); + mbedtls_aes_init(&ctx); + ret = mbedtls_aes_setkey_enc(&ctx, key, key_len * 8); + if (ret < 0) { + goto cleanup; + } + ret = mbedtls_aes_crypt_ctr(&ctx, data_len, &offset, (u8 *)nonce, + stream_block, data, data); cleanup: - mbedtls_aes_free(&ctx); - return ret; + mbedtls_aes_free(&ctx); + return ret; } int aes_128_ctr_encrypt(const u8 *key, const u8 *nonce, - u8 *data, size_t data_len) + u8 *data, size_t data_len) { - return aes_ctr_encrypt(key, 16, nonce, data, data_len); + return aes_ctr_encrypt(key, 16, nonce, data, data_len); } - #ifdef MBEDTLS_NIST_KW_C int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher) { - mbedtls_nist_kw_context ctx; - size_t olen; - int ret = 0; - mbedtls_nist_kw_init(&ctx); + mbedtls_nist_kw_context ctx; + size_t olen; + int ret = 0; + mbedtls_nist_kw_init(&ctx); - ret = mbedtls_nist_kw_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, - kek, kek_len * 8, 1); - if (ret != 0) { - return ret; - } + ret = mbedtls_nist_kw_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, + kek, kek_len * 8, 1); + if (ret != 0) { + return ret; + } - ret = mbedtls_nist_kw_wrap(&ctx, MBEDTLS_KW_MODE_KW, plain, - n * 8, cipher, &olen, (n + 1) * 8); + ret = mbedtls_nist_kw_wrap(&ctx, MBEDTLS_KW_MODE_KW, plain, + n * 8, cipher, &olen, (n + 1) * 8); - mbedtls_nist_kw_free(&ctx); - return ret; + mbedtls_nist_kw_free(&ctx); + return ret; } int aes_unwrap(const u8 *kek, size_t kek_len, int n, const u8 *cipher, - u8 *plain) + u8 *plain) { - mbedtls_nist_kw_context ctx; - size_t olen; - int ret = 0; - mbedtls_nist_kw_init(&ctx); + mbedtls_nist_kw_context ctx; + size_t olen; + int ret = 0; + mbedtls_nist_kw_init(&ctx); - ret = mbedtls_nist_kw_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, - kek, kek_len * 8, 0); - if (ret != 0) { - return ret; - } + ret = mbedtls_nist_kw_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, + kek, kek_len * 8, 0); + if (ret != 0) { + return ret; + } - ret = mbedtls_nist_kw_unwrap(&ctx, MBEDTLS_KW_MODE_KW, cipher, - (n + 1) * 8, plain, &olen, (n * 8)); + ret = mbedtls_nist_kw_unwrap(&ctx, MBEDTLS_KW_MODE_KW, cipher, + (n + 1) * 8, plain, &olen, (n * 8)); - mbedtls_nist_kw_free(&ctx); - return ret; + mbedtls_nist_kw_free(&ctx); + return ret; } #endif int crypto_mod_exp(const uint8_t *base, size_t base_len, - const uint8_t *power, size_t power_len, - const uint8_t *modulus, size_t modulus_len, - uint8_t *result, size_t *result_len) + const uint8_t *power, size_t power_len, + const uint8_t *modulus, size_t modulus_len, + uint8_t *result, size_t *result_len) { - mbedtls_mpi bn_base, bn_exp, bn_modulus, bn_result, bn_rinv; - int ret = 0; + mbedtls_mpi bn_base, bn_exp, bn_modulus, bn_result, bn_rinv; + int ret = 0; - mbedtls_mpi_init(&bn_base); - mbedtls_mpi_init(&bn_exp); - mbedtls_mpi_init(&bn_modulus); - mbedtls_mpi_init(&bn_result); - mbedtls_mpi_init(&bn_rinv); + mbedtls_mpi_init(&bn_base); + mbedtls_mpi_init(&bn_exp); + mbedtls_mpi_init(&bn_modulus); + mbedtls_mpi_init(&bn_result); + mbedtls_mpi_init(&bn_rinv); - MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&bn_base, base, base_len)); - MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&bn_exp, power, power_len)); - MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&bn_modulus, modulus, modulus_len)); + MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&bn_base, base, base_len)); + MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&bn_exp, power, power_len)); + MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&bn_modulus, modulus, modulus_len)); - MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&bn_result, &bn_base, &bn_exp, &bn_modulus, - &bn_rinv)); + MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&bn_result, &bn_base, &bn_exp, &bn_modulus, + &bn_rinv)); - ret = mbedtls_mpi_write_binary(&bn_result, result, *result_len); + ret = mbedtls_mpi_write_binary(&bn_result, result, *result_len); cleanup: - mbedtls_mpi_free(&bn_base); - mbedtls_mpi_free(&bn_exp); - mbedtls_mpi_free(&bn_modulus); - mbedtls_mpi_free(&bn_result); - mbedtls_mpi_free(&bn_rinv); + mbedtls_mpi_free(&bn_base); + mbedtls_mpi_free(&bn_exp); + mbedtls_mpi_free(&bn_modulus); + mbedtls_mpi_free(&bn_result); + mbedtls_mpi_free(&bn_rinv); - return ret; + return ret; } int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len, - int iterations, u8 *buf, size_t buflen) + int iterations, u8 *buf, size_t buflen) { #ifdef CONFIG_FAST_PBKDF2 - fastpbkdf2_hmac_sha1((const u8 *) passphrase, os_strlen(passphrase), - ssid, ssid_len, iterations, buf, buflen); - return 0; + fastpbkdf2_hmac_sha1((const u8 *) passphrase, os_strlen(passphrase), + ssid, ssid_len, iterations, buf, buflen); + return 0; #else - int ret = mbedtls_pkcs5_pbkdf2_hmac_ext(MBEDTLS_MD_SHA1, (const u8 *) passphrase, - os_strlen(passphrase) , ssid, - ssid_len, iterations, buflen, buf); - if (ret != 0) { - ret = -1; - goto cleanup; - } + int ret = mbedtls_pkcs5_pbkdf2_hmac_ext(MBEDTLS_MD_SHA1, (const u8 *) passphrase, + os_strlen(passphrase), ssid, + ssid_len, iterations, buflen, buf); + if (ret != 0) { + ret = -1; + goto cleanup; + } cleanup: - return ret; + return ret; #endif } #ifdef MBEDTLS_DES_C int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher) { - int ret; - mbedtls_des_context des; - u8 pkey[8], next, tmp; - int i; + int ret; + mbedtls_des_context des; + u8 pkey[8], next, tmp; + int i; - /* Add parity bits to the key */ - next = 0; - for (i = 0; i < 7; i++) { - tmp = key[i]; - pkey[i] = (tmp >> i) | next | 1; - next = tmp << (7 - i); - } - pkey[i] = next | 1; + /* Add parity bits to the key */ + next = 0; + for (i = 0; i < 7; i++) { + tmp = key[i]; + pkey[i] = (tmp >> i) | next | 1; + next = tmp << (7 - i); + } + pkey[i] = next | 1; - mbedtls_des_init(&des); - ret = mbedtls_des_setkey_enc(&des, pkey); - if (ret < 0) { - return ret; - } - ret = mbedtls_des_crypt_ecb(&des, clear, cypher); - mbedtls_des_free(&des); + mbedtls_des_init(&des); + ret = mbedtls_des_setkey_enc(&des, pkey); + if (ret < 0) { + return ret; + } + ret = mbedtls_des_crypt_ecb(&des, clear, cypher); + mbedtls_des_free(&des); - return ret; + return ret; } #endif /* Only enable this if all other ciphers are using MbedTLS implementation */ #if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_CMAC_C) && defined(MBEDTLS_NIST_KW_C) int aes_ccm_ae(const u8 *key, size_t key_len, const u8 *nonce, - size_t M, const u8 *plain, size_t plain_len, - const u8 *aad, size_t aad_len, u8 *crypt, u8 *auth) + size_t M, const u8 *plain, size_t plain_len, + const u8 *aad, size_t aad_len, u8 *crypt, u8 *auth) { - int ret; - mbedtls_ccm_context ccm; + int ret; + mbedtls_ccm_context ccm; - mbedtls_ccm_init(&ccm); + mbedtls_ccm_init(&ccm); - ret = mbedtls_ccm_setkey(&ccm, MBEDTLS_CIPHER_ID_AES, - key, key_len * 8); - if (ret < 0) { - wpa_printf(MSG_ERROR, "mbedtls_ccm_setkey failed"); - goto cleanup; - } + ret = mbedtls_ccm_setkey(&ccm, MBEDTLS_CIPHER_ID_AES, + key, key_len * 8); + if (ret < 0) { + wpa_printf(MSG_ERROR, "mbedtls_ccm_setkey failed"); + goto cleanup; + } - ret = mbedtls_ccm_encrypt_and_tag(&ccm, plain_len, nonce, 13, aad, - aad_len, plain, crypt, auth, M); + ret = mbedtls_ccm_encrypt_and_tag(&ccm, plain_len, nonce, 13, aad, + aad_len, plain, crypt, auth, M); cleanup: - mbedtls_ccm_free(&ccm); + mbedtls_ccm_free(&ccm); - return ret; + return ret; } int aes_ccm_ad(const u8 *key, size_t key_len, const u8 *nonce, - size_t M, const u8 *crypt, size_t crypt_len, - const u8 *aad, size_t aad_len, const u8 *auth, - u8 *plain) + size_t M, const u8 *crypt, size_t crypt_len, + const u8 *aad, size_t aad_len, const u8 *auth, + u8 *plain) { - int ret; - mbedtls_ccm_context ccm; + int ret; + mbedtls_ccm_context ccm; - mbedtls_ccm_init(&ccm); + mbedtls_ccm_init(&ccm); - ret = mbedtls_ccm_setkey(&ccm, MBEDTLS_CIPHER_ID_AES, - key, key_len * 8); - if (ret < 0) { - goto cleanup;; - } + ret = mbedtls_ccm_setkey(&ccm, MBEDTLS_CIPHER_ID_AES, + key, key_len * 8); + if (ret < 0) { + goto cleanup;; + } - ret = mbedtls_ccm_star_auth_decrypt(&ccm, crypt_len, - nonce, 13, aad, aad_len, - crypt, plain, auth, M); + ret = mbedtls_ccm_star_auth_decrypt(&ccm, crypt_len, + nonce, 13, aad, aad_len, + crypt, plain, auth, M); cleanup: - mbedtls_ccm_free(&ccm); + mbedtls_ccm_free(&ccm); - return ret; + return ret; } #endif #ifdef MBEDTLS_CMAC_C int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem, - const u8 *addr[], const size_t *len, u8 *mac) + const u8 *addr[], const size_t *len, u8 *mac) { - const mbedtls_cipher_info_t *cipher_info; - int i, ret = 0; - mbedtls_cipher_type_t cipher_type; - mbedtls_cipher_context_t ctx; + const mbedtls_cipher_info_t *cipher_info; + int i, ret = 0; + mbedtls_cipher_type_t cipher_type; + mbedtls_cipher_context_t ctx; - switch (key_len) { - case 16: - cipher_type = MBEDTLS_CIPHER_AES_128_ECB; - break; - case 24: - cipher_type = MBEDTLS_CIPHER_AES_192_ECB; - break; - case 32: - cipher_type = MBEDTLS_CIPHER_AES_256_ECB; - break; - default: - cipher_type = MBEDTLS_CIPHER_NONE; - break; - } - cipher_info = mbedtls_cipher_info_from_type(cipher_type); - if (cipher_info == NULL) { - /* Failing at this point must be due to a build issue */ - ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; - goto cleanup; - } + switch (key_len) { + case 16: + cipher_type = MBEDTLS_CIPHER_AES_128_ECB; + break; + case 24: + cipher_type = MBEDTLS_CIPHER_AES_192_ECB; + break; + case 32: + cipher_type = MBEDTLS_CIPHER_AES_256_ECB; + break; + default: + cipher_type = MBEDTLS_CIPHER_NONE; + break; + } + cipher_info = mbedtls_cipher_info_from_type(cipher_type); + if (cipher_info == NULL) { + /* Failing at this point must be due to a build issue */ + ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; + goto cleanup; + } - if (key == NULL || mac == NULL) { - return -1; - } + if (key == NULL || mac == NULL) { + return -1; + } - mbedtls_cipher_init(&ctx); + mbedtls_cipher_init(&ctx); - ret = mbedtls_cipher_setup(&ctx, cipher_info); - if (ret != 0) { - goto cleanup; - } + ret = mbedtls_cipher_setup(&ctx, cipher_info); + if (ret != 0) { + goto cleanup; + } - ret = mbedtls_cipher_cmac_starts(&ctx, key, key_len * 8); - if (ret != 0) { - goto cleanup; - } + ret = mbedtls_cipher_cmac_starts(&ctx, key, key_len * 8); + if (ret != 0) { + goto cleanup; + } - for (i = 0 ; i < num_elem; i++) { - ret = mbedtls_cipher_cmac_update(&ctx, addr[i], len[i]); - if (ret != 0) { - goto cleanup; - } - } + for (i = 0 ; i < num_elem; i++) { + ret = mbedtls_cipher_cmac_update(&ctx, addr[i], len[i]); + if (ret != 0) { + goto cleanup; + } + } - ret = mbedtls_cipher_cmac_finish(&ctx, mac); + ret = mbedtls_cipher_cmac_finish(&ctx, mac); cleanup: - mbedtls_cipher_free(&ctx); - return(ret); + mbedtls_cipher_free(&ctx); + return (ret); } int omac1_aes_128_vector(const u8 *key, size_t num_elem, - const u8 *addr[], const size_t *len, u8 *mac) + const u8 *addr[], const size_t *len, u8 *mac) { - return omac1_aes_vector(key, 16, num_elem, addr, len, mac); + return omac1_aes_vector(key, 16, num_elem, addr, len, mac); } int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac) { - return omac1_aes_128_vector(key, 1, &data, &data_len, mac); + return omac1_aes_128_vector(key, 1, &data, &data_len, mac); } #endif int crypto_dh_init(u8 generator, const u8 *prime, size_t prime_len, u8 *privkey, - u8 *pubkey) + u8 *pubkey) { - size_t pubkey_len, pad; + size_t pubkey_len, pad; - if (os_get_random(privkey, prime_len) < 0) { - return -1; - } - if (os_memcmp(privkey, prime, prime_len) > 0) { - /* Make sure private value is smaller than prime */ - privkey[0] = 0; - } + if (os_get_random(privkey, prime_len) < 0) { + return -1; + } + if (os_memcmp(privkey, prime, prime_len) > 0) { + /* Make sure private value is smaller than prime */ + privkey[0] = 0; + } - pubkey_len = prime_len; - if (crypto_mod_exp(&generator, 1, privkey, prime_len, prime, prime_len, - pubkey, &pubkey_len) < 0) { - return -1; - } - if (pubkey_len < prime_len) { - pad = prime_len - pubkey_len; - os_memmove(pubkey + pad, pubkey, pubkey_len); - os_memset(pubkey, 0, pad); - } + pubkey_len = prime_len; + if (crypto_mod_exp(&generator, 1, privkey, prime_len, prime, prime_len, + pubkey, &pubkey_len) < 0) { + return -1; + } + if (pubkey_len < prime_len) { + pad = prime_len - pubkey_len; + os_memmove(pubkey + pad, pubkey, pubkey_len); + os_memset(pubkey, 0, pad); + } - return 0; + return 0; } int crypto_global_init(void) { - return 0; + return 0; } void crypto_global_deinit(void) diff --git a/components/wpa_supplicant/esp_supplicant/src/crypto/fastpbkdf2.c b/components/wpa_supplicant/esp_supplicant/src/crypto/fastpbkdf2.c index 24fc706c63..f2bd0cfd3e 100644 --- a/components/wpa_supplicant/esp_supplicant/src/crypto/fastpbkdf2.c +++ b/components/wpa_supplicant/esp_supplicant/src/crypto/fastpbkdf2.c @@ -44,12 +44,12 @@ static inline void write32_be(uint32_t n, uint8_t out[4]) { #if defined(__GNUC__) && __GNUC__ >= 4 && __BYTE_ORDER == __LITTLE_ENDIAN - *(uint32_t *)(out) = __builtin_bswap32(n); + *(uint32_t *)(out) = __builtin_bswap32(n); #else - out[0] = (n >> 24) & 0xff; - out[1] = (n >> 16) & 0xff; - out[2] = (n >> 8) & 0xff; - out[3] = n & 0xff; + out[0] = (n >> 24) & 0xff; + out[1] = (n >> 16) & 0xff; + out[2] = (n >> 8) & 0xff; + out[3] = n & 0xff; #endif } @@ -59,10 +59,10 @@ static inline void write32_be(uint32_t n, uint8_t out[4]) * Message length is expressed in 32 bits (so suitable for sha1, sha256, sha512). */ static inline void md_pad(uint8_t *block, size_t blocksz, size_t used, size_t msg) { - memset(block + used, 0, blocksz - used - 4); - block[used] = 0x80; - block += blocksz - 4; - write32_be((uint32_t) (msg * 8), block); + memset(block + used, 0, blocksz - used - 4); + block[used] = 0x80; + block += blocksz - 4; + write32_be((uint32_t)(msg * 8), block); } /* Internal function/type names for hash-specific things. */ @@ -237,76 +237,76 @@ static inline void sha1_extract(mbedtls_sha1_context *restrict ctx, uint8_t *res { #if defined(MBEDTLS_SHA1_ALT) #if CONFIG_IDF_TARGET_ESP32 - /* ESP32 stores internal SHA state in BE format similar to software */ - write32_be(ctx->state[0], out); - write32_be(ctx->state[1], out + 4); - write32_be(ctx->state[2], out + 8); - write32_be(ctx->state[3], out + 12); - write32_be(ctx->state[4], out + 16); + /* ESP32 stores internal SHA state in BE format similar to software */ + write32_be(ctx->state[0], out); + write32_be(ctx->state[1], out + 4); + write32_be(ctx->state[2], out + 8); + write32_be(ctx->state[3], out + 12); + write32_be(ctx->state[4], out + 16); #else - *(uint32_t *)(out) = ctx->state[0]; - *(uint32_t *)(out + 4) = ctx->state[1]; - *(uint32_t *)(out + 8) = ctx->state[2]; - *(uint32_t *)(out + 12) = ctx->state[3]; - *(uint32_t *)(out + 16) = ctx->state[4]; + *(uint32_t *)(out) = ctx->state[0]; + *(uint32_t *)(out + 4) = ctx->state[1]; + *(uint32_t *)(out + 8) = ctx->state[2]; + *(uint32_t *)(out + 12) = ctx->state[3]; + *(uint32_t *)(out + 16) = ctx->state[4]; #endif #else - write32_be(ctx->MBEDTLS_PRIVATE(state)[0], out); - write32_be(ctx->MBEDTLS_PRIVATE(state)[1], out + 4); - write32_be(ctx->MBEDTLS_PRIVATE(state)[2], out + 8); - write32_be(ctx->MBEDTLS_PRIVATE(state)[3], out + 12); - write32_be(ctx->MBEDTLS_PRIVATE(state)[4], out + 16); + write32_be(ctx->MBEDTLS_PRIVATE(state)[0], out); + write32_be(ctx->MBEDTLS_PRIVATE(state)[1], out + 4); + write32_be(ctx->MBEDTLS_PRIVATE(state)[2], out + 8); + write32_be(ctx->MBEDTLS_PRIVATE(state)[3], out + 12); + write32_be(ctx->MBEDTLS_PRIVATE(state)[4], out + 16); #endif } static inline void sha1_cpy(mbedtls_sha1_context *restrict out, const mbedtls_sha1_context *restrict in) { #if defined(MBEDTLS_SHA1_ALT) - out->state[0] = in->state[0]; - out->state[1] = in->state[1]; - out->state[2] = in->state[2]; - out->state[3] = in->state[3]; - out->state[4] = in->state[4]; + out->state[0] = in->state[0]; + out->state[1] = in->state[1]; + out->state[2] = in->state[2]; + out->state[3] = in->state[3]; + out->state[4] = in->state[4]; #else - out->MBEDTLS_PRIVATE(state)[0] = in->MBEDTLS_PRIVATE(state)[0]; - out->MBEDTLS_PRIVATE(state)[1] = in->MBEDTLS_PRIVATE(state)[1]; - out->MBEDTLS_PRIVATE(state)[2] = in->MBEDTLS_PRIVATE(state)[2]; - out->MBEDTLS_PRIVATE(state)[3] = in->MBEDTLS_PRIVATE(state)[3]; - out->MBEDTLS_PRIVATE(state)[4] = in->MBEDTLS_PRIVATE(state)[4]; + out->MBEDTLS_PRIVATE(state)[0] = in->MBEDTLS_PRIVATE(state)[0]; + out->MBEDTLS_PRIVATE(state)[1] = in->MBEDTLS_PRIVATE(state)[1]; + out->MBEDTLS_PRIVATE(state)[2] = in->MBEDTLS_PRIVATE(state)[2]; + out->MBEDTLS_PRIVATE(state)[3] = in->MBEDTLS_PRIVATE(state)[3]; + out->MBEDTLS_PRIVATE(state)[4] = in->MBEDTLS_PRIVATE(state)[4]; #endif } static inline void sha1_xor(mbedtls_sha1_context *restrict out, const mbedtls_sha1_context *restrict in) { #if defined(MBEDTLS_SHA1_ALT) - out->state[0] ^= in->state[0]; - out->state[1] ^= in->state[1]; - out->state[2] ^= in->state[2]; - out->state[3] ^= in->state[3]; - out->state[4] ^= in->state[4]; + out->state[0] ^= in->state[0]; + out->state[1] ^= in->state[1]; + out->state[2] ^= in->state[2]; + out->state[3] ^= in->state[3]; + out->state[4] ^= in->state[4]; #else - out->MBEDTLS_PRIVATE(state)[0] ^= in->MBEDTLS_PRIVATE(state)[0]; - out->MBEDTLS_PRIVATE(state)[1] ^= in->MBEDTLS_PRIVATE(state)[1]; - out->MBEDTLS_PRIVATE(state)[2] ^= in->MBEDTLS_PRIVATE(state)[2]; - out->MBEDTLS_PRIVATE(state)[3] ^= in->MBEDTLS_PRIVATE(state)[3]; - out->MBEDTLS_PRIVATE(state)[4] ^= in->MBEDTLS_PRIVATE(state)[4]; + out->MBEDTLS_PRIVATE(state)[0] ^= in->MBEDTLS_PRIVATE(state)[0]; + out->MBEDTLS_PRIVATE(state)[1] ^= in->MBEDTLS_PRIVATE(state)[1]; + out->MBEDTLS_PRIVATE(state)[2] ^= in->MBEDTLS_PRIVATE(state)[2]; + out->MBEDTLS_PRIVATE(state)[3] ^= in->MBEDTLS_PRIVATE(state)[3]; + out->MBEDTLS_PRIVATE(state)[4] ^= in->MBEDTLS_PRIVATE(state)[4]; #endif } static int mbedtls_sha1_init_start(mbedtls_sha1_context *ctx) { - mbedtls_sha1_init(ctx); - mbedtls_sha1_starts(ctx); + mbedtls_sha1_init(ctx); + mbedtls_sha1_starts(ctx); #if defined(CONFIG_IDF_TARGET_ESP32) && defined(MBEDTLS_SHA1_ALT) - /* Use software mode for esp32 since hardware can't give output more than 20 */ - esp_mbedtls_set_sha1_mode(ctx, ESP_MBEDTLS_SHA1_SOFTWARE); + /* Use software mode for esp32 since hardware can't give output more than 20 */ + esp_mbedtls_set_sha1_mode(ctx, ESP_MBEDTLS_SHA1_SOFTWARE); #endif - return 0; + return 0; } #ifndef MBEDTLS_SHA1_ALT static int sha1_finish(mbedtls_sha1_context *ctx, - unsigned char output[20]) + unsigned char output[20]) { int ret = -1; uint32_t used; @@ -384,5 +384,5 @@ void fastpbkdf2_hmac_sha1(const uint8_t *pw, size_t npw, uint32_t iterations, uint8_t *out, size_t nout) { - PBKDF2(sha1)(pw, npw, salt, nsalt, iterations, out, nout); + PBKDF2(sha1)(pw, npw, salt, nsalt, iterations, out, nout); } diff --git a/components/wpa_supplicant/esp_supplicant/src/crypto/tls_mbedtls.c b/components/wpa_supplicant/esp_supplicant/src/crypto/tls_mbedtls.c index b51e016f98..deb0415ad6 100644 --- a/components/wpa_supplicant/esp_supplicant/src/crypto/tls_mbedtls.c +++ b/components/wpa_supplicant/esp_supplicant/src/crypto/tls_mbedtls.c @@ -46,15 +46,12 @@ psa_generic_status_to_mbedtls) #endif /* CONFIG_TLSV13 */ - #define TLS_RANDOM_LEN 32 #define TLS_HASH_MAX_SIZE 64 #define TLS_MASTER_SECRET_LEN 48 #define MAX_CIPHERSUITE 32 #define MAX_EXPORTER_CONTEXT_LEN 65535 - - /* Throw a compilation error if basic requirements in mbedtls are not enabled */ #if !defined(MBEDTLS_SSL_TLS_C) #error "TLS not enabled in mbedtls config" @@ -70,146 +67,147 @@ uint32_t tls_instance_count; struct tls_data { - /* Data for mbedlts */ - struct wpabuf *in_data; - /* Data from mbedtls */ - struct wpabuf *out_data; + /* Data for mbedlts */ + struct wpabuf *in_data; + /* Data from mbedtls */ + struct wpabuf *out_data; }; typedef struct tls_context { - mbedtls_ssl_context ssl; /*!< TLS/SSL context */ - mbedtls_entropy_context entropy; /*!< mbedTLS entropy context structure */ - mbedtls_ctr_drbg_context ctr_drbg; /*!< mbedTLS ctr drbg context structure */ - mbedtls_ssl_config conf; /*!< TLS/SSL config to be shared structures */ - mbedtls_x509_crt cacert; /*!< Container for X.509 CA certificate */ - mbedtls_x509_crt *cacert_ptr; /*!< Pointer to the cacert being used. */ - mbedtls_x509_crt clientcert; /*!< Container for X.509 client certificate */ - mbedtls_pk_context clientkey; /*!< Private key of client certificate */ - int ciphersuite[MAX_CIPHERSUITE]; + mbedtls_ssl_context ssl; /*!< TLS/SSL context */ + mbedtls_entropy_context entropy; /*!< mbedTLS entropy context structure */ + mbedtls_ctr_drbg_context ctr_drbg; /*!< mbedTLS ctr drbg context structure */ + mbedtls_ssl_config conf; /*!< TLS/SSL config to be shared structures */ + mbedtls_x509_crt cacert; /*!< Container for X.509 CA certificate */ + mbedtls_x509_crt *cacert_ptr; /*!< Pointer to the cacert being used. */ + mbedtls_x509_crt clientcert; /*!< Container for X.509 client certificate */ + mbedtls_pk_context clientkey; /*!< Private key of client certificate */ + int ciphersuite[MAX_CIPHERSUITE]; } tls_context_t; struct tls_connection { - tls_context_t *tls; - struct tls_data tls_io_data; - unsigned char master_secret[TLS_MASTER_SECRET_LEN]; - unsigned char randbytes[2 * TLS_RANDOM_LEN]; - mbedtls_tls_prf_types tls_prf_type; + tls_context_t *tls; + struct tls_data tls_io_data; + unsigned char master_secret[TLS_MASTER_SECRET_LEN]; + unsigned char randbytes[2 * TLS_RANDOM_LEN]; + mbedtls_tls_prf_types tls_prf_type; #ifdef CONFIG_TLSV13 - unsigned char exporter_master_secret[TLS_HASH_MAX_SIZE]; + unsigned char exporter_master_secret[TLS_HASH_MAX_SIZE]; #endif /* CONFIG_TLSV13 */ }; static void tls_mbedtls_cleanup(tls_context_t *tls) { - if (!tls) { - return; - } - tls->cacert_ptr = NULL; - mbedtls_x509_crt_free(&tls->cacert); - mbedtls_x509_crt_free(&tls->clientcert); - mbedtls_pk_free(&tls->clientkey); - mbedtls_entropy_free(&tls->entropy); - mbedtls_ssl_config_free(&tls->conf); - mbedtls_ctr_drbg_free(&tls->ctr_drbg); - mbedtls_ssl_free(&tls->ssl); + if (!tls) { + return; + } + tls->cacert_ptr = NULL; + mbedtls_x509_crt_free(&tls->cacert); + mbedtls_x509_crt_free(&tls->clientcert); + mbedtls_pk_free(&tls->clientkey); + mbedtls_entropy_free(&tls->entropy); + mbedtls_ssl_config_free(&tls->conf); + mbedtls_ctr_drbg_free(&tls->ctr_drbg); + mbedtls_ssl_free(&tls->ssl); } static void tls_mbedtls_conn_delete(tls_context_t *tls) { - if (tls != NULL) { - tls_mbedtls_cleanup(tls); - } + if (tls != NULL) { + tls_mbedtls_cleanup(tls); + } } static int tls_mbedtls_write(void *ctx, const unsigned char *buf, size_t len) { - struct tls_connection *conn = (struct tls_connection *)ctx; - struct tls_data *data = &conn->tls_io_data; + struct tls_connection *conn = (struct tls_connection *)ctx; + struct tls_data *data = &conn->tls_io_data; - if (wpabuf_resize(&data->out_data, len) < 0) - return 0; + if (wpabuf_resize(&data->out_data, len) < 0) { + return 0; + } - wpabuf_put_data(data->out_data, buf, len); + wpabuf_put_data(data->out_data, buf, len); - return len; + return len; } static int tls_mbedtls_read(void *ctx, unsigned char *buf, size_t len) { - struct tls_connection *conn = (struct tls_connection *)ctx; - struct tls_data *data = &conn->tls_io_data; - struct wpabuf *local_buf; + struct tls_connection *conn = (struct tls_connection *)ctx; + struct tls_data *data = &conn->tls_io_data; + struct wpabuf *local_buf; - if (data->in_data == NULL || len > wpabuf_len(data->in_data)) { - /* We don't have suffient buffer available for read */ - wpa_printf(MSG_INFO, "len=%zu not available in input", len); - return MBEDTLS_ERR_SSL_WANT_READ; - } + if (data->in_data == NULL || len > wpabuf_len(data->in_data)) { + /* We don't have suffient buffer available for read */ + wpa_printf(MSG_INFO, "len=%zu not available in input", len); + return MBEDTLS_ERR_SSL_WANT_READ; + } - os_memcpy(buf, wpabuf_head(data->in_data), len); - /* adjust buffer */ - if (len < wpabuf_len(data->in_data)) { - /* TODO optimize this operation */ - local_buf = wpabuf_alloc_copy(wpabuf_mhead_u8(data->in_data) + len, - wpabuf_len(data->in_data) - len); - wpabuf_free(data->in_data); - data->in_data = local_buf; - } else { - wpabuf_free(data->in_data); - data->in_data = NULL; - } + os_memcpy(buf, wpabuf_head(data->in_data), len); + /* adjust buffer */ + if (len < wpabuf_len(data->in_data)) { + /* TODO optimize this operation */ + local_buf = wpabuf_alloc_copy(wpabuf_mhead_u8(data->in_data) + len, + wpabuf_len(data->in_data) - len); + wpabuf_free(data->in_data); + data->in_data = local_buf; + } else { + wpabuf_free(data->in_data); + data->in_data = NULL; + } - return len; + return len; } static int set_pki_context(tls_context_t *tls, const struct tls_connection_params *cfg) { - int ret; + int ret; - if (cfg->client_cert_blob == NULL || cfg->private_key_blob == NULL) { - wpa_printf(MSG_ERROR, "%s: config not correct", __func__); - return -1; - } + if (cfg->client_cert_blob == NULL || cfg->private_key_blob == NULL) { + wpa_printf(MSG_ERROR, "%s: config not correct", __func__); + return -1; + } - mbedtls_x509_crt_init(&tls->clientcert); - mbedtls_pk_init(&tls->clientkey); + mbedtls_x509_crt_init(&tls->clientcert); + mbedtls_pk_init(&tls->clientkey); - ret = mbedtls_x509_crt_parse(&tls->clientcert, - cfg->client_cert_blob, cfg->client_cert_blob_len); - if (ret < 0) { - wpa_printf(MSG_ERROR, "mbedtls_x509_crt_parse returned -0x%x", -ret); - return ret; - } + ret = mbedtls_x509_crt_parse(&tls->clientcert, + cfg->client_cert_blob, cfg->client_cert_blob_len); + if (ret < 0) { + wpa_printf(MSG_ERROR, "mbedtls_x509_crt_parse returned -0x%x", -ret); + return ret; + } - ret = mbedtls_pk_parse_key(&tls->clientkey, cfg->private_key_blob, cfg->private_key_blob_len, - (const unsigned char *)cfg->private_key_passwd, - cfg->private_key_passwd ? os_strlen(cfg->private_key_passwd) : 0, mbedtls_ctr_drbg_random, &tls->ctr_drbg); - if (ret < 0) { - wpa_printf(MSG_ERROR, "mbedtls_pk_parse_keyfile returned -0x%x", -ret); - return ret; - } + ret = mbedtls_pk_parse_key(&tls->clientkey, cfg->private_key_blob, cfg->private_key_blob_len, + (const unsigned char *)cfg->private_key_passwd, + cfg->private_key_passwd ? os_strlen(cfg->private_key_passwd) : 0, mbedtls_ctr_drbg_random, &tls->ctr_drbg); + if (ret < 0) { + wpa_printf(MSG_ERROR, "mbedtls_pk_parse_keyfile returned -0x%x", -ret); + return ret; + } - ret = mbedtls_ssl_conf_own_cert(&tls->conf, &tls->clientcert, &tls->clientkey); - if (ret < 0) { - wpa_printf(MSG_ERROR, "mbedtls_ssl_conf_own_cert returned -0x%x", -ret); - return ret; - } + ret = mbedtls_ssl_conf_own_cert(&tls->conf, &tls->clientcert, &tls->clientkey); + if (ret < 0) { + wpa_printf(MSG_ERROR, "mbedtls_ssl_conf_own_cert returned -0x%x", -ret); + return ret; + } - return 0; + return 0; } static int set_ca_cert(tls_context_t *tls, const unsigned char *cacert, size_t cacert_len) { - tls->cacert_ptr = &tls->cacert; - mbedtls_x509_crt_init(tls->cacert_ptr); - int ret = mbedtls_x509_crt_parse(tls->cacert_ptr, cacert, cacert_len); - if (ret < 0) { - wpa_printf(MSG_ERROR, "mbedtls_x509_crt_parse returned -0x%x", -ret); - return ret; - } - mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_REQUIRED); - mbedtls_ssl_conf_ca_chain(&tls->conf, tls->cacert_ptr, NULL); - return 0; + tls->cacert_ptr = &tls->cacert; + mbedtls_x509_crt_init(tls->cacert_ptr); + int ret = mbedtls_x509_crt_parse(tls->cacert_ptr, cacert, cacert_len); + if (ret < 0) { + wpa_printf(MSG_ERROR, "mbedtls_x509_crt_parse returned -0x%x", -ret); + return ret; + } + mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_REQUIRED); + mbedtls_ssl_conf_ca_chain(&tls->conf, tls->cacert_ptr, NULL); + return 0; } #ifdef CONFIG_SUITEB192 @@ -253,34 +251,33 @@ static uint16_t tls_sig_algs_for_suiteb[] = { #if defined(MBEDTLS_SHA512_C) #if defined(MBEDTLS_ECDSA_C) - MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ), - MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ), + MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512), + MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384), #endif #if defined(MBEDTLS_RSA_C) - MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ), - MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), + MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512), + MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384), #endif #endif /* MBEDTLS_SHA512_C */ MBEDTLS_TLS_SIG_NONE }; -const mbedtls_x509_crt_profile suiteb_mbedtls_x509_crt_profile = -{ +const mbedtls_x509_crt_profile suiteb_mbedtls_x509_crt_profile = { #if defined(MBEDTLS_SHA512_C) - MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) | - MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ) | + MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) | + MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512) | #endif - 0, - 0xFFFFFFF, /* Any PK alg */ - MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1), - 1024, + 0, + 0xFFFFFFF, /* Any PK alg */ + MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1), + 1024, }; static void tls_set_suiteb_config(tls_context_t *tls) { - const mbedtls_x509_crt_profile *crt_profile = &suiteb_mbedtls_x509_crt_profile; - mbedtls_ssl_conf_cert_profile(&tls->conf, crt_profile); - mbedtls_ssl_conf_sig_algs(&tls->conf, tls_sig_algs_for_suiteb); + const mbedtls_x509_crt_profile *crt_profile = &suiteb_mbedtls_x509_crt_profile; + mbedtls_ssl_conf_cert_profile(&tls->conf, crt_profile); + mbedtls_ssl_conf_sig_algs(&tls->conf, tls_sig_algs_for_suiteb); } #endif @@ -341,885 +338,877 @@ static uint16_t tls_sig_algs_for_eap[] = { #if defined(MBEDTLS_SHA512_C) #if defined(MBEDTLS_ECDSA_C) - MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ), - MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ), + MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512), + MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384), #endif #if defined(MBEDTLS_RSA_C) - MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ), - MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), + MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512), + MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384), #endif #endif /* MBEDTLS_SHA512_C */ #if defined(MBEDTLS_SHA256_C) #if defined(MBEDTLS_ECDSA_C) - MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ), - MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA224 ), + MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256), + MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA224), #endif #if defined(MBEDTLS_RSA_C) - MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), - MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA224 ), + MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256), + MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA224), #endif #endif /* MBEDTLS_SHA256_C */ #if defined(MBEDTLS_SHA1_C) #if defined(MBEDTLS_ECDSA_C) - MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA1 ), + MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA1), #endif #if defined(MBEDTLS_RSA_C) - MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA1 ), + MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA1), #endif #endif /* MBEDTLS_SHA1_C */ MBEDTLS_TLS_SIG_NONE }; -const mbedtls_x509_crt_profile eap_mbedtls_x509_crt_profile = -{ +const mbedtls_x509_crt_profile eap_mbedtls_x509_crt_profile = { #if defined(MBEDTLS_SHA1_C) - MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) | + MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) | #endif #if defined(MBEDTLS_SHA256_C) - MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) | - MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) | + MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) | + MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) | #endif #if defined(MBEDTLS_SHA512_C) - MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) | - MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ) | + MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) | + MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512) | #endif - 0, - 0xFFFFFFF, /* Any PK alg */ - 0xFFFFFFF, /* Any curve */ - 1024, + 0, + 0xFFFFFFF, /* Any PK alg */ + 0xFFFFFFF, /* Any curve */ + 1024, }; static void tls_enable_sha1_config(tls_context_t *tls) { - const mbedtls_x509_crt_profile *crt_profile = &eap_mbedtls_x509_crt_profile; - mbedtls_ssl_conf_cert_profile(&tls->conf, crt_profile); - mbedtls_ssl_conf_sig_algs(&tls->conf, tls_sig_algs_for_eap); + const mbedtls_x509_crt_profile *crt_profile = &eap_mbedtls_x509_crt_profile; + mbedtls_ssl_conf_cert_profile(&tls->conf, crt_profile); + mbedtls_ssl_conf_sig_algs(&tls->conf, tls_sig_algs_for_eap); } #ifdef CONFIG_ESP_WIFI_DISABLE_KEY_USAGE_CHECK static int tls_disable_key_usages(void *data, mbedtls_x509_crt *cert, int depth, uint32_t *flags) { - cert->MBEDTLS_PRIVATE(ext_types) &= ~MBEDTLS_X509_EXT_KEY_USAGE; - cert->MBEDTLS_PRIVATE(ext_types) &= ~MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE; - return 0; + cert->MBEDTLS_PRIVATE(ext_types) &= ~MBEDTLS_X509_EXT_KEY_USAGE; + cert->MBEDTLS_PRIVATE(ext_types) &= ~MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE; + return 0; } #endif /*CONFIG_ESP_WIFI_DISABLE_KEY_USAGE_CHECK*/ #ifdef CONFIG_SUITEB192 -static const int suiteb_rsa_ciphersuite_preference[] = -{ +static const int suiteb_rsa_ciphersuite_preference[] = { #if defined(CONFIG_ESP_WIFI_EAP_TLS1_3) - MBEDTLS_TLS1_3_AES_256_GCM_SHA384, + MBEDTLS_TLS1_3_AES_256_GCM_SHA384, #endif /* CONFIG_ESP_WIFI_EAP_TLS1_3 */ #if defined(MBEDTLS_GCM_C) #if defined(MBEDTLS_SHA512_C) - MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, - MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, + MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, + MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, #endif #endif - 0 + 0 }; -static const int suiteb_ecc_ciphersuite_preference[] = -{ +static const int suiteb_ecc_ciphersuite_preference[] = { #if defined(CONFIG_ESP_WIFI_EAP_TLS1_3) - MBEDTLS_TLS1_3_AES_256_GCM_SHA384, + MBEDTLS_TLS1_3_AES_256_GCM_SHA384, #endif /* CONFIG_ESP_WIFI_EAP_TLS1_3 */ #if defined(MBEDTLS_GCM_C) #if defined(MBEDTLS_SHA512_C) - MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, + MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, #endif #endif - 0 + 0 }; -static const int suiteb_ciphersuite_preference[] = -{ +static const int suiteb_ciphersuite_preference[] = { #if defined(CONFIG_ESP_WIFI_EAP_TLS1_3) - MBEDTLS_TLS1_3_AES_256_GCM_SHA384, + MBEDTLS_TLS1_3_AES_256_GCM_SHA384, #endif /* CONFIG_ESP_WIFI_EAP_TLS1_3 */ #if defined(MBEDTLS_GCM_C) #if defined(MBEDTLS_SHA512_C) - MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, - MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, - MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, + MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, + MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, + MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, #endif #endif - 0 + 0 }; #endif static void tls_set_ciphersuite(const struct tls_connection_params *cfg, tls_context_t *tls) { - /* Only set ciphersuite if cert's key length is high or ciphersuites are set by user */ + /* Only set ciphersuite if cert's key length is high or ciphersuites are set by user */ #ifdef CONFIG_SUITEB192 - if (cfg->flags & TLS_CONN_SUITEB) { - /* cipher suites will be set based on certificate */ - mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(&tls->clientkey); - if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) { - mbedtls_ssl_conf_ciphersuites(&tls->conf, - suiteb_rsa_ciphersuite_preference); - } else if (pk_alg == MBEDTLS_PK_ECDSA || - pk_alg == MBEDTLS_PK_ECKEY || - pk_alg == MBEDTLS_PK_ECKEY_DH) { - mbedtls_ssl_conf_ciphersuites(&tls->conf, - suiteb_ecc_ciphersuite_preference); - } else { - mbedtls_ssl_conf_ciphersuites(&tls->conf, - suiteb_ciphersuite_preference); - } - } else + if (cfg->flags & TLS_CONN_SUITEB) { + /* cipher suites will be set based on certificate */ + mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(&tls->clientkey); + if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) { + mbedtls_ssl_conf_ciphersuites(&tls->conf, + suiteb_rsa_ciphersuite_preference); + } else if (pk_alg == MBEDTLS_PK_ECDSA || + pk_alg == MBEDTLS_PK_ECKEY || + pk_alg == MBEDTLS_PK_ECKEY_DH) { + mbedtls_ssl_conf_ciphersuites(&tls->conf, + suiteb_ecc_ciphersuite_preference); + } else { + mbedtls_ssl_conf_ciphersuites(&tls->conf, + suiteb_ciphersuite_preference); + } + } else #endif - /* Set cipher suites if User has explicitly set those - * TODO: public API to set EAP ciphers */ - if (tls->ciphersuite[0]) { - mbedtls_ssl_conf_ciphersuites(&tls->conf, tls->ciphersuite); - } + /* Set cipher suites if User has explicitly set those + * TODO: public API to set EAP ciphers */ + if (tls->ciphersuite[0]) { + mbedtls_ssl_conf_ciphersuites(&tls->conf, tls->ciphersuite); + } } static int set_client_config(const struct tls_connection_params *cfg, tls_context_t *tls) { - int ret; - int preset = MBEDTLS_SSL_PRESET_DEFAULT; - assert(cfg != NULL); - assert(tls != NULL); + int ret; + int preset = MBEDTLS_SSL_PRESET_DEFAULT; + assert(cfg != NULL); + assert(tls != NULL); #ifdef CONFIG_SUITEB192 - if (cfg->flags & TLS_CONN_SUITEB) - preset = MBEDTLS_SSL_PRESET_SUITEB; + if (cfg->flags & TLS_CONN_SUITEB) { + preset = MBEDTLS_SSL_PRESET_SUITEB; + } #endif - ret = mbedtls_ssl_config_defaults(&tls->conf, - MBEDTLS_SSL_IS_CLIENT, - MBEDTLS_SSL_TRANSPORT_STREAM, - preset); - if (ret != 0) { - wpa_printf(MSG_ERROR, "mbedtls_ssl_config_defaults returned -0x%x", -ret); - return ret; - } + ret = mbedtls_ssl_config_defaults(&tls->conf, + MBEDTLS_SSL_IS_CLIENT, + MBEDTLS_SSL_TRANSPORT_STREAM, + preset); + if (ret != 0) { + wpa_printf(MSG_ERROR, "mbedtls_ssl_config_defaults returned -0x%x", -ret); + return ret; + } - if (preset != MBEDTLS_SSL_PRESET_SUITEB) { - /* Enable SHA1 support since it's not enabled by default in mbedtls */ - tls_enable_sha1_config(tls); + if (preset != MBEDTLS_SSL_PRESET_SUITEB) { + /* Enable SHA1 support since it's not enabled by default in mbedtls */ + tls_enable_sha1_config(tls); #ifdef CONFIG_SUITEB192 - } else { - tls_set_suiteb_config(tls); + } else { + tls_set_suiteb_config(tls); #endif - } + } - if (cfg->ca_cert_blob != NULL) { - ret = set_ca_cert(tls, cfg->ca_cert_blob, cfg->ca_cert_blob_len); - if (ret != 0) { - return ret; - } - } else { - mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_NONE); - } + if (cfg->ca_cert_blob != NULL) { + ret = set_ca_cert(tls, cfg->ca_cert_blob, cfg->ca_cert_blob_len); + if (ret != 0) { + return ret; + } + } else { + mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_NONE); + } - if (cfg->client_cert_blob != NULL && cfg->private_key_blob != NULL) { - ret = set_pki_context(tls, cfg); - if (ret != 0) { - wpa_printf(MSG_ERROR, "Failed to set client pki context"); - return ret; - } - } + if (cfg->client_cert_blob != NULL && cfg->private_key_blob != NULL) { + ret = set_pki_context(tls, cfg); + if (ret != 0) { + wpa_printf(MSG_ERROR, "Failed to set client pki context"); + return ret; + } + } - /* Usages of default ciphersuites can take a lot of time on low end device - * and can cause watchdog. Enabling the ciphers which are secured enough - * but doesn't take that much processing power */ - tls_set_ciphersuite(cfg, tls); + /* Usages of default ciphersuites can take a lot of time on low end device + * and can cause watchdog. Enabling the ciphers which are secured enough + * but doesn't take that much processing power */ + tls_set_ciphersuite(cfg, tls); #ifdef CONFIG_ESP_WIFI_DISABLE_KEY_USAGE_CHECK - mbedtls_ssl_set_verify( &tls->ssl, tls_disable_key_usages, NULL ); + mbedtls_ssl_set_verify(&tls->ssl, tls_disable_key_usages, NULL); #endif /*CONFIG_ESP_WIFI_DISABLE_KEY_USAGE_CHECK*/ #ifdef CONFIG_MBEDTLS_CERTIFICATE_BUNDLE - if (cfg->flags & TLS_CONN_USE_DEFAULT_CERT_BUNDLE) { - wpa_printf(MSG_INFO, "Using default cert bundle"); - if (esp_crt_bundle_attach_fn) { - ret = (*esp_crt_bundle_attach_fn)(&tls->conf); - } - if (ret != 0) { - wpa_printf(MSG_ERROR, "Failed to set default cert bundle"); - return ret; - } - } + if (cfg->flags & TLS_CONN_USE_DEFAULT_CERT_BUNDLE) { + wpa_printf(MSG_INFO, "Using default cert bundle"); + if (esp_crt_bundle_attach_fn) { + ret = (*esp_crt_bundle_attach_fn)(&tls->conf); + } + if (ret != 0) { + wpa_printf(MSG_ERROR, "Failed to set default cert bundle"); + return ret; + } + } #endif - return 0; + return 0; } #ifdef CONFIG_TLSV13 static void tls13_extract_exporter_master_secret(struct tls_connection *conn) { - mbedtls_ssl_context *ssl = &conn->tls->ssl; - const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info; - psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(ciphersuite_info->mac); + mbedtls_ssl_context *ssl = &conn->tls->ssl; + const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info; + psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(ciphersuite_info->mac); - size_t hash_len = PSA_HASH_LENGTH(hash_alg); - assert(hash_len != 0); + size_t hash_len = PSA_HASH_LENGTH(hash_alg); + assert(hash_len != 0); - mbedtls_ssl_tls13_application_secrets *app_secrets = - &ssl->session_negotiate->app_secrets; - os_memcpy(conn->exporter_master_secret, app_secrets->exporter_master_secret, hash_len); + mbedtls_ssl_tls13_application_secrets *app_secrets = + &ssl->session_negotiate->app_secrets; + os_memcpy(conn->exporter_master_secret, app_secrets->exporter_master_secret, hash_len); } #endif /* CONFIG_TLSV13 */ static void tls_key_derivation(void *ctx, - mbedtls_ssl_key_export_type secret_type, - const unsigned char *secret, - size_t secret_len, - const unsigned char client_random[TLS_RANDOM_LEN], - const unsigned char server_random[TLS_RANDOM_LEN], - mbedtls_tls_prf_types tls_prf_type) + mbedtls_ssl_key_export_type secret_type, + const unsigned char *secret, + size_t secret_len, + const unsigned char client_random[TLS_RANDOM_LEN], + const unsigned char server_random[TLS_RANDOM_LEN], + mbedtls_tls_prf_types tls_prf_type) { - struct tls_connection *conn = (struct tls_connection *)ctx; + struct tls_connection *conn = (struct tls_connection *)ctx; - os_memcpy(conn->master_secret, secret, sizeof(conn->master_secret)); - os_memcpy(conn->randbytes, client_random, TLS_RANDOM_LEN); - os_memcpy(conn->randbytes + 32, server_random, TLS_RANDOM_LEN); - conn->tls_prf_type = tls_prf_type; + os_memcpy(conn->master_secret, secret, sizeof(conn->master_secret)); + os_memcpy(conn->randbytes, client_random, TLS_RANDOM_LEN); + os_memcpy(conn->randbytes + 32, server_random, TLS_RANDOM_LEN); + conn->tls_prf_type = tls_prf_type; #ifdef CONFIG_TLSV13 - if (secret_type == MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET) { - tls13_extract_exporter_master_secret(conn); - } + if (secret_type == MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET) { + tls13_extract_exporter_master_secret(conn); + } #endif /* CONFIG_TLSV13 */ } static int tls_create_mbedtls_handle(struct tls_connection *conn, - const struct tls_connection_params *params, - tls_context_t *tls) + const struct tls_connection_params *params, + tls_context_t *tls) { - int ret; + int ret; - assert(params != NULL); - assert(tls != NULL); + assert(params != NULL); + assert(tls != NULL); - mbedtls_ssl_init(&tls->ssl); - mbedtls_ctr_drbg_init(&tls->ctr_drbg); - mbedtls_ssl_config_init(&tls->conf); - mbedtls_entropy_init(&tls->entropy); + mbedtls_ssl_init(&tls->ssl); + mbedtls_ctr_drbg_init(&tls->ctr_drbg); + mbedtls_ssl_config_init(&tls->conf); + mbedtls_entropy_init(&tls->entropy); - ret = set_client_config(params, tls); - if (ret != 0) { - wpa_printf(MSG_ERROR, "Failed to set client configurations"); - goto exit; - } + ret = set_client_config(params, tls); + if (ret != 0) { + wpa_printf(MSG_ERROR, "Failed to set client configurations"); + goto exit; + } - ret = mbedtls_ctr_drbg_seed(&tls->ctr_drbg, mbedtls_entropy_func, - &tls->entropy, NULL, 0); - if (ret != 0) { - wpa_printf(MSG_ERROR, "mbedtls_ctr_drbg_seed returned -0x%x", -ret); - goto exit; - } + ret = mbedtls_ctr_drbg_seed(&tls->ctr_drbg, mbedtls_entropy_func, + &tls->entropy, NULL, 0); + if (ret != 0) { + wpa_printf(MSG_ERROR, "mbedtls_ctr_drbg_seed returned -0x%x", -ret); + goto exit; + } - mbedtls_ssl_conf_rng(&tls->conf, mbedtls_ctr_drbg_random, &tls->ctr_drbg); + mbedtls_ssl_conf_rng(&tls->conf, mbedtls_ctr_drbg_random, &tls->ctr_drbg); #if defined(CONFIG_MBEDTLS_SSL_PROTO_TLS1_3) && !defined(CONFIG_TLSV13) - /* Disable TLSv1.3 even when enabled in MbedTLS and not enabled in WiFi config. - * TODO: Remove Kconfig option for TLSv1.3 when it is matured enough */ - mbedtls_ssl_conf_max_tls_version(&tls->conf, MBEDTLS_SSL_VERSION_TLS1_2); + /* Disable TLSv1.3 even when enabled in MbedTLS and not enabled in WiFi config. + * TODO: Remove Kconfig option for TLSv1.3 when it is matured enough */ + mbedtls_ssl_conf_max_tls_version(&tls->conf, MBEDTLS_SSL_VERSION_TLS1_2); #endif /* CONFIG_MBEDTLS_SSL_PROTO_TLS1_3 && !CONFIG_TLSV13 */ - ret = mbedtls_ssl_setup(&tls->ssl, &tls->conf); - if (ret != 0) { - wpa_printf(MSG_ERROR, "mbedtls_ssl_setup returned -0x%x", -ret); - goto exit; - } - mbedtls_ssl_set_export_keys_cb(&tls->ssl, tls_key_derivation, conn); + ret = mbedtls_ssl_setup(&tls->ssl, &tls->conf); + if (ret != 0) { + wpa_printf(MSG_ERROR, "mbedtls_ssl_setup returned -0x%x", -ret); + goto exit; + } + mbedtls_ssl_set_export_keys_cb(&tls->ssl, tls_key_derivation, conn); #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) - /* Disable BEAST attack countermeasures for Windows 2008 interoperability */ - mbedtls_ssl_conf_cbc_record_splitting(&tls->conf, MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED); + /* Disable BEAST attack countermeasures for Windows 2008 interoperability */ + mbedtls_ssl_conf_cbc_record_splitting(&tls->conf, MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED); #endif - /* Enable debug prints in case supplicant's prints are enabled */ + /* Enable debug prints in case supplicant's prints are enabled */ #if defined(CONFIG_MBEDTLS_DEBUG) && defined(ESPRESSIF_USE) - mbedtls_esp_enable_debug_log(&tls->conf, 2); + mbedtls_esp_enable_debug_log(&tls->conf, 2); #endif - return 0; + return 0; exit: - tls_mbedtls_cleanup(tls); - return ret; + tls_mbedtls_cleanup(tls); + return ret; } void *tls_init(const struct tls_config *conf) { - tls_instance_count++; - return &tls_instance_count; + tls_instance_count++; + return &tls_instance_count; } void tls_deinit(void *tls_ctx) { - tls_instance_count--; + tls_instance_count--; } struct tls_connection * tls_connection_init(void *tls_ctx) { - struct tls_connection *conn = os_zalloc(sizeof(*conn)); - if (!conn) { - wpa_printf(MSG_ERROR, "TLS: Failed to allocate connection memory"); - return NULL; - } + struct tls_connection *conn = os_zalloc(sizeof(*conn)); + if (!conn) { + wpa_printf(MSG_ERROR, "TLS: Failed to allocate connection memory"); + return NULL; + } #ifdef CONFIG_TLSV13 - psa_status_t status = psa_crypto_init(); - if (status != PSA_SUCCESS) { - wpa_printf(MSG_ERROR, "Failed to initialize PSA crypto, returned %d", (int) status); - return NULL; - } + psa_status_t status = psa_crypto_init(); + if (status != PSA_SUCCESS) { + wpa_printf(MSG_ERROR, "Failed to initialize PSA crypto, returned %d", (int) status); + return NULL; + } #endif /* CONFIG_TLSV13 */ - return conn; + return conn; } - void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn) { - /* case: tls init failed */ - if (!conn) { - return; - } - /* Free ssl ctx and data */ - tls_mbedtls_conn_delete((tls_context_t *) conn->tls); - os_free(conn->tls); - conn->tls = NULL; - /* Data in in ssl ctx, free connection */ - os_free(conn); + /* case: tls init failed */ + if (!conn) { + return; + } + /* Free ssl ctx and data */ + tls_mbedtls_conn_delete((tls_context_t *) conn->tls); + os_free(conn->tls); + conn->tls = NULL; + /* Data in in ssl ctx, free connection */ + os_free(conn); } int tls_get_errors(void *tls_ctx) { - return 0; + return 0; } int tls_connection_established(void *tls_ctx, struct tls_connection *conn) { - mbedtls_ssl_context *ssl = &conn->tls->ssl; + mbedtls_ssl_context *ssl = &conn->tls->ssl; - return mbedtls_ssl_is_handshake_over(ssl); + return mbedtls_ssl_is_handshake_over(ssl); } int tls_global_set_verify(void *tls_ctx, int check_crl, int strict) { - wpa_printf(MSG_INFO, "TLS: global settings are not supported"); - return -1; + wpa_printf(MSG_INFO, "TLS: global settings are not supported"); + return -1; } int tls_connection_set_verify(void *tls_ctx, struct tls_connection *conn, - int verify_peer, unsigned int flags, - const u8 *session_ctx, size_t session_ctx_len) + int verify_peer, unsigned int flags, + const u8 *session_ctx, size_t session_ctx_len) { - wpa_printf(MSG_INFO, "TLS: tls_connection_set_verify not supported"); - return -1; + wpa_printf(MSG_INFO, "TLS: tls_connection_set_verify not supported"); + return -1; } #ifdef CONFIG_ESP_WIFI_ENT_FREE_DYNAMIC_BUFFER static void esp_mbedtls_free_dhm(mbedtls_ssl_context *ssl) { #ifdef CONFIG_MBEDTLS_DHM_C - const mbedtls_ssl_config *conf = mbedtls_ssl_context_get_config(ssl); - mbedtls_mpi_free((mbedtls_mpi *)&conf->MBEDTLS_PRIVATE(dhm_P)); - mbedtls_mpi_free((mbedtls_mpi *)&conf->MBEDTLS_PRIVATE(dhm_G)); + const mbedtls_ssl_config *conf = mbedtls_ssl_context_get_config(ssl); + mbedtls_mpi_free((mbedtls_mpi *)&conf->MBEDTLS_PRIVATE(dhm_P)); + mbedtls_mpi_free((mbedtls_mpi *)&conf->MBEDTLS_PRIVATE(dhm_G)); #endif /* CONFIG_MBEDTLS_DHM_C */ } static void esp_mbedtls_free_keycert(mbedtls_ssl_context *ssl) { - mbedtls_ssl_config *conf = (mbedtls_ssl_config * )mbedtls_ssl_context_get_config(ssl); - mbedtls_ssl_key_cert *keycert = conf->MBEDTLS_PRIVATE(key_cert), *next; + mbedtls_ssl_config *conf = (mbedtls_ssl_config *)mbedtls_ssl_context_get_config(ssl); + mbedtls_ssl_key_cert *keycert = conf->MBEDTLS_PRIVATE(key_cert), *next; - while (keycert) { - next = keycert->next; + while (keycert) { + next = keycert->next; - if (keycert) { - mbedtls_free(keycert); - } + if (keycert) { + mbedtls_free(keycert); + } - keycert = next; - } + keycert = next; + } - conf->MBEDTLS_PRIVATE(key_cert) = NULL; + conf->MBEDTLS_PRIVATE(key_cert) = NULL; } static void esp_mbedtls_free_keycert_key(mbedtls_ssl_context *ssl) { - const mbedtls_ssl_config *conf = mbedtls_ssl_context_get_config(ssl); - mbedtls_ssl_key_cert *keycert = conf->MBEDTLS_PRIVATE(key_cert); + const mbedtls_ssl_config *conf = mbedtls_ssl_context_get_config(ssl); + mbedtls_ssl_key_cert *keycert = conf->MBEDTLS_PRIVATE(key_cert); - while (keycert) { - if (keycert->key) { - mbedtls_pk_free(keycert->key); - keycert->key = NULL; - } - keycert = keycert->next; - } + while (keycert) { + if (keycert->key) { + mbedtls_pk_free(keycert->key); + keycert->key = NULL; + } + keycert = keycert->next; + } } static void esp_mbedtls_free_cacert(mbedtls_ssl_context *ssl) { - if (ssl->MBEDTLS_PRIVATE(conf)->MBEDTLS_PRIVATE(ca_chain)) { - mbedtls_ssl_config *conf = (mbedtls_ssl_config * )mbedtls_ssl_context_get_config(ssl); + if (ssl->MBEDTLS_PRIVATE(conf)->MBEDTLS_PRIVATE(ca_chain)) { + mbedtls_ssl_config *conf = (mbedtls_ssl_config *)mbedtls_ssl_context_get_config(ssl); - mbedtls_x509_crt_free(conf->MBEDTLS_PRIVATE(ca_chain)); - conf->MBEDTLS_PRIVATE(ca_chain) = NULL; - } + mbedtls_x509_crt_free(conf->MBEDTLS_PRIVATE(ca_chain)); + conf->MBEDTLS_PRIVATE(ca_chain) = NULL; + } } #endif struct wpabuf * tls_connection_handshake(void *tls_ctx, - struct tls_connection *conn, - const struct wpabuf *in_data, - struct wpabuf **appl_data) + struct tls_connection *conn, + const struct wpabuf *in_data, + struct wpabuf **appl_data) { - tls_context_t *tls = conn->tls; - int ret = 0; - struct wpabuf *resp; + tls_context_t *tls = conn->tls; + int ret = 0; + struct wpabuf *resp; - /* data freed by sender */ - conn->tls_io_data.out_data = NULL; - if (wpabuf_len(in_data)) { - conn->tls_io_data.in_data = wpabuf_dup(in_data); - } + /* data freed by sender */ + conn->tls_io_data.out_data = NULL; + if (wpabuf_len(in_data)) { + conn->tls_io_data.in_data = wpabuf_dup(in_data); + } - /* Multiple reads */ - while (!mbedtls_ssl_is_handshake_over(&tls->ssl)) { + /* Multiple reads */ + while (!mbedtls_ssl_is_handshake_over(&tls->ssl)) { #ifdef CONFIG_ESP_WIFI_ENT_FREE_DYNAMIC_BUFFER - int cli_state = tls->ssl.MBEDTLS_PRIVATE(state); + int cli_state = tls->ssl.MBEDTLS_PRIVATE(state); #endif /* CONFIG_ESP_WIFI_ENT_FREE_DYNAMIC_BUFFER */ - ret = mbedtls_ssl_handshake_step(&tls->ssl); - if (ret < 0) { - break; - } + ret = mbedtls_ssl_handshake_step(&tls->ssl); + if (ret < 0) { + break; + } #ifdef CONFIG_ESP_WIFI_ENT_FREE_DYNAMIC_BUFFER - if (mbedtls_ssl_get_version_number(&tls->ssl) == MBEDTLS_SSL_VERSION_TLS1_2) { - if (cli_state == MBEDTLS_SSL_SERVER_CERTIFICATE) { - esp_mbedtls_free_cacert(&tls->ssl); - } else if (cli_state == MBEDTLS_SSL_CERTIFICATE_VERIFY) { - esp_mbedtls_free_dhm(&tls->ssl); - esp_mbedtls_free_keycert_key(&tls->ssl); - esp_mbedtls_free_keycert(&tls->ssl); - } - } + if (mbedtls_ssl_get_version_number(&tls->ssl) == MBEDTLS_SSL_VERSION_TLS1_2) { + if (cli_state == MBEDTLS_SSL_SERVER_CERTIFICATE) { + esp_mbedtls_free_cacert(&tls->ssl); + } else if (cli_state == MBEDTLS_SSL_CERTIFICATE_VERIFY) { + esp_mbedtls_free_dhm(&tls->ssl); + esp_mbedtls_free_keycert_key(&tls->ssl); + esp_mbedtls_free_keycert(&tls->ssl); + } + } #endif - } - if (ret < 0 && ret != MBEDTLS_ERR_SSL_WANT_READ) { - wpa_printf(MSG_INFO, "%s: ret is %d line:%d", __func__, ret, __LINE__); - goto end; - } + } + if (ret < 0 && ret != MBEDTLS_ERR_SSL_WANT_READ) { + wpa_printf(MSG_INFO, "%s: ret is %d line:%d", __func__, ret, __LINE__); + goto end; + } - if (!conn->tls_io_data.out_data) { - wpa_printf(MSG_INFO, "application data is null, adding one byte for ack"); - u8 *dummy = os_zalloc(1); - if (dummy == NULL) { - wpa_printf(MSG_INFO, "%s: memory allocation failure. line:%d", __func__, __LINE__); - goto end; - } + if (!conn->tls_io_data.out_data) { + wpa_printf(MSG_INFO, "application data is null, adding one byte for ack"); + u8 *dummy = os_zalloc(1); + if (dummy == NULL) { + wpa_printf(MSG_INFO, "%s: memory allocation failure. line:%d", __func__, __LINE__); + goto end; + } #ifdef CONFIG_TLSV13 - if (mbedtls_ssl_get_version_number(&conn->tls->ssl) == MBEDTLS_SSL_VERSION_TLS1_3) { - *appl_data = wpabuf_alloc_ext_data(dummy, 1); - if (appl_data == NULL) { - wpa_printf(MSG_INFO, "%s: memory allocation failure. line:%d", __func__, __LINE__); - os_free(dummy); - goto end; - } - return NULL; - } + if (mbedtls_ssl_get_version_number(&conn->tls->ssl) == MBEDTLS_SSL_VERSION_TLS1_3) { + *appl_data = wpabuf_alloc_ext_data(dummy, 1); + if (appl_data == NULL) { + wpa_printf(MSG_INFO, "%s: memory allocation failure. line:%d", __func__, __LINE__); + os_free(dummy); + goto end; + } + return NULL; + } #endif /* CONFIG_TLSV13 */ - conn->tls_io_data.out_data = wpabuf_alloc_ext_data(dummy, 0); - if (conn->tls_io_data.out_data == NULL) { - wpa_printf(MSG_INFO, "%s: memory allocation failure. line:%d", __func__, __LINE__); - os_free(dummy); - goto end; - } - } + conn->tls_io_data.out_data = wpabuf_alloc_ext_data(dummy, 0); + if (conn->tls_io_data.out_data == NULL) { + wpa_printf(MSG_INFO, "%s: memory allocation failure. line:%d", __func__, __LINE__); + os_free(dummy); + goto end; + } + } end: - resp = conn->tls_io_data.out_data; - conn->tls_io_data.out_data = NULL; - return resp; + resp = conn->tls_io_data.out_data; + conn->tls_io_data.out_data = NULL; + return resp; } struct wpabuf * tls_connection_server_handshake(void *tls_ctx, - struct tls_connection *conn, - const struct wpabuf *in_data, - struct wpabuf **appl_data) + struct tls_connection *conn, + const struct wpabuf *in_data, + struct wpabuf **appl_data) { - wpa_printf(MSG_ERROR, "%s: not supported %d", __func__, __LINE__); - return NULL; + wpa_printf(MSG_ERROR, "%s: not supported %d", __func__, __LINE__); + return NULL; } - struct wpabuf * tls_connection_encrypt(void *tls_ctx, - struct tls_connection *conn, - const struct wpabuf *in_data) + struct tls_connection *conn, + const struct wpabuf *in_data) { - struct wpabuf *resp; - size_t ret; + struct wpabuf *resp; + size_t ret; - /* Reset dangling pointer */ - conn->tls_io_data.out_data = NULL; - ret = mbedtls_ssl_write(&conn->tls->ssl, - (unsigned char*) wpabuf_head(in_data), wpabuf_len(in_data)); + /* Reset dangling pointer */ + conn->tls_io_data.out_data = NULL; + ret = mbedtls_ssl_write(&conn->tls->ssl, + (unsigned char*) wpabuf_head(in_data), wpabuf_len(in_data)); - if (ret < wpabuf_len(in_data)) { - wpa_printf(MSG_ERROR, "%s:%d, not able to write whole data", - __func__, __LINE__); - } + if (ret < wpabuf_len(in_data)) { + wpa_printf(MSG_ERROR, "%s:%d, not able to write whole data", + __func__, __LINE__); + } - resp = conn->tls_io_data.out_data; - conn->tls_io_data.out_data = NULL; - return resp; + resp = conn->tls_io_data.out_data; + conn->tls_io_data.out_data = NULL; + return resp; } - struct wpabuf *tls_connection_decrypt(void *tls_ctx, - struct tls_connection *conn, - const struct wpabuf *in_data) + struct tls_connection *conn, + const struct wpabuf *in_data) { #define MAX_PHASE2_BUFFER 1536 - struct wpabuf *out = NULL; - int ret; - unsigned char *buf = os_malloc(MAX_PHASE2_BUFFER); + struct wpabuf *out = NULL; + int ret; + unsigned char *buf = os_malloc(MAX_PHASE2_BUFFER); - if (!buf) { - return NULL; - } - /* Reset dangling output buffer before setting data, data was freed by caller */ - conn->tls_io_data.out_data = NULL; + if (!buf) { + return NULL; + } + /* Reset dangling output buffer before setting data, data was freed by caller */ + conn->tls_io_data.out_data = NULL; - conn->tls_io_data.in_data = wpabuf_dup(in_data); + conn->tls_io_data.in_data = wpabuf_dup(in_data); - if (!conn->tls_io_data.in_data) { - goto cleanup; - } - ret = mbedtls_ssl_read(&conn->tls->ssl, buf, MAX_PHASE2_BUFFER); - if (ret < 0) { - wpa_printf(MSG_ERROR, "%s:%d, not able to read data", - __func__, __LINE__); - goto cleanup; - } - out = wpabuf_alloc_copy(buf, ret); + if (!conn->tls_io_data.in_data) { + goto cleanup; + } + ret = mbedtls_ssl_read(&conn->tls->ssl, buf, MAX_PHASE2_BUFFER); + if (ret < 0) { + wpa_printf(MSG_ERROR, "%s:%d, not able to read data", + __func__, __LINE__); + goto cleanup; + } + out = wpabuf_alloc_copy(buf, ret); cleanup: - /* there may be some error written in output buffer */ - if (conn->tls_io_data.out_data) { - os_free(conn->tls_io_data.out_data); - conn->tls_io_data.out_data = NULL; - } + /* there may be some error written in output buffer */ + if (conn->tls_io_data.out_data) { + os_free(conn->tls_io_data.out_data); + conn->tls_io_data.out_data = NULL; + } - os_free(buf); + os_free(buf); - return out; + return out; #undef MAX_PHASE2_BUFFER } - int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn) { - if (conn && conn->tls && conn->tls->ssl.MBEDTLS_PRIVATE(handshake)) { - return conn->tls->ssl.MBEDTLS_PRIVATE(handshake)->resume; - } + if (conn && conn->tls && conn->tls->ssl.MBEDTLS_PRIVATE(handshake)) { + return conn->tls->ssl.MBEDTLS_PRIVATE(handshake)->resume; + } - return 0; + return 0; } /* cipher array should contain cipher number in mbedtls num as per IANA * Please see cipherlist is u8, therefore only initial ones are supported */ int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn, - u8 *ciphers) + u8 *ciphers) { - int i = 0; + int i = 0; - while (*ciphers != 0 && i < MAX_CIPHERSUITE) { - conn->tls->ciphersuite[i] = ciphers[i]; - i++; - } - return 0; + while (*ciphers != 0 && i < MAX_CIPHERSUITE) { + conn->tls->ciphersuite[i] = ciphers[i]; + i++; + } + return 0; } int tls_get_version(void *tls_ctx, struct tls_connection *conn, - char *buf, size_t buflen) + char *buf, size_t buflen) { - const char *name; + const char *name; - if (conn == NULL) { - return -1; - } + if (conn == NULL) { + return -1; + } - name = mbedtls_ssl_get_version(&conn->tls->ssl); - if (name == NULL) { - return -1; - } + name = mbedtls_ssl_get_version(&conn->tls->ssl); + if (name == NULL) { + return -1; + } - os_strlcpy(buf, name, buflen); + os_strlcpy(buf, name, buflen); - return 0; + return 0; } int tls_get_cipher(void *tls_ctx, struct tls_connection *conn, - char *buf, size_t buflen) + char *buf, size_t buflen) { - const char *name; - if (conn == NULL) { - return -1; - } + const char *name; + if (conn == NULL) { + return -1; + } - name = mbedtls_ssl_get_ciphersuite(&conn->tls->ssl); - if (name == NULL) { - return -1; - } + name = mbedtls_ssl_get_ciphersuite(&conn->tls->ssl); + if (name == NULL) { + return -1; + } - os_strlcpy(buf, name, buflen); + os_strlcpy(buf, name, buflen); - return 0; + return 0; } - int tls_connection_enable_workaround(void *tls_ctx, - struct tls_connection *conn) + struct tls_connection *conn) { - wpa_printf(MSG_ERROR, "%s: not supported %d", __func__, __LINE__); - return -1; + wpa_printf(MSG_ERROR, "%s: not supported %d", __func__, __LINE__); + return -1; } int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn) { - return 0; + return 0; } int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn) { - wpa_printf(MSG_ERROR, "%s: not supported %d", __func__, __LINE__); - return 0; + wpa_printf(MSG_ERROR, "%s: not supported %d", __func__, __LINE__); + return 0; } int tls_connection_get_write_alerts(void *tls_ctx, struct tls_connection *conn) { - wpa_printf(MSG_ERROR, "%s: not supported %d", __func__, __LINE__); - return 0; + wpa_printf(MSG_ERROR, "%s: not supported %d", __func__, __LINE__); + return 0; } int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn, - const struct tls_connection_params *params) + const struct tls_connection_params *params) { - int ret = 0; - tls_context_t *tls = (tls_context_t *)os_zalloc(sizeof(tls_context_t)); + int ret = 0; + tls_context_t *tls = (tls_context_t *)os_zalloc(sizeof(tls_context_t)); - if (!tls) { - wpa_printf(MSG_ERROR, "failed to allocate tls context"); - return -1; - } - if (!params) { - wpa_printf(MSG_ERROR, "configuration is null"); - ret = -1; - goto err; - } + if (!tls) { + wpa_printf(MSG_ERROR, "failed to allocate tls context"); + return -1; + } + if (!params) { + wpa_printf(MSG_ERROR, "configuration is null"); + ret = -1; + goto err; + } - ret = tls_create_mbedtls_handle(conn, params, tls); - if (ret < 0) { - wpa_printf(MSG_ERROR, "failed to create ssl handle"); - goto err; - } - mbedtls_ssl_set_bio(&tls->ssl, conn, tls_mbedtls_write, tls_mbedtls_read, NULL); - conn->tls = (tls_context_t *)tls; + ret = tls_create_mbedtls_handle(conn, params, tls); + if (ret < 0) { + wpa_printf(MSG_ERROR, "failed to create ssl handle"); + goto err; + } + mbedtls_ssl_set_bio(&tls->ssl, conn, tls_mbedtls_write, tls_mbedtls_read, NULL); + conn->tls = (tls_context_t *)tls; - return ret; + return ret; err: - os_free(tls); - return ret; + os_free(tls); + return ret; } int tls_global_set_params(void *tls_ctx, - const struct tls_connection_params *params) + const struct tls_connection_params *params) { - wpa_printf(MSG_INFO, "TLS: Global parameters not supported"); - return -1; + wpa_printf(MSG_INFO, "TLS: Global parameters not supported"); + return -1; } int tls_connection_set_session_ticket_cb(void *tls_ctx, - struct tls_connection *conn, - tls_session_ticket_cb cb, - void *ctx) + struct tls_connection *conn, + tls_session_ticket_cb cb, + void *ctx) { - wpa_printf(MSG_ERROR, "TLS: %s not supported", __func__); - return -1; + wpa_printf(MSG_ERROR, "TLS: %s not supported", __func__); + return -1; } static int tls_connection_prf(void *tls_ctx, struct tls_connection *conn, - const char *label, const u8 *context, - size_t context_len, int server_random_first, - u8 *out, size_t out_len) + const char *label, const u8 *context, + size_t context_len, int server_random_first, + u8 *out, size_t out_len) { - int ret; - u8 *seed, *pos; - size_t seed_len = 2 * TLS_RANDOM_LEN; - mbedtls_ssl_context *ssl = &conn->tls->ssl; + int ret; + u8 *seed, *pos; + size_t seed_len = 2 * TLS_RANDOM_LEN; + mbedtls_ssl_context *ssl = &conn->tls->ssl; - if (context_len > MAX_EXPORTER_CONTEXT_LEN) { - return -1; - } + if (context_len > MAX_EXPORTER_CONTEXT_LEN) { + return -1; + } - if (context) { - // The magic value 2 represents the memory required to store the context length. - seed_len += 2 + context_len; - } + if (context) { + // The magic value 2 represents the memory required to store the context length. + seed_len += 2 + context_len; + } - if (!ssl) { - wpa_printf(MSG_ERROR, "TLS: %s, session ingo is null", __func__); - return -1; - } - if (!mbedtls_ssl_is_handshake_over(ssl)) { - wpa_printf(MSG_ERROR, "TLS: %s, incorrect tls state=%d", __func__, ssl->MBEDTLS_PRIVATE(state)); - return -1; - } + if (!ssl) { + wpa_printf(MSG_ERROR, "TLS: %s, session ingo is null", __func__); + return -1; + } + if (!mbedtls_ssl_is_handshake_over(ssl)) { + wpa_printf(MSG_ERROR, "TLS: %s, incorrect tls state=%d", __func__, ssl->MBEDTLS_PRIVATE(state)); + return -1; + } - seed = os_malloc(seed_len); - if (!seed) { - return -1; - } + seed = os_malloc(seed_len); + if (!seed) { + return -1; + } - if (server_random_first) { - os_memcpy(seed, conn->randbytes + TLS_RANDOM_LEN, TLS_RANDOM_LEN); - os_memcpy(seed + TLS_RANDOM_LEN, conn->randbytes, TLS_RANDOM_LEN); - } else { - os_memcpy(seed, conn->randbytes, 2 * TLS_RANDOM_LEN); - } + if (server_random_first) { + os_memcpy(seed, conn->randbytes + TLS_RANDOM_LEN, TLS_RANDOM_LEN); + os_memcpy(seed + TLS_RANDOM_LEN, conn->randbytes, TLS_RANDOM_LEN); + } else { + os_memcpy(seed, conn->randbytes, 2 * TLS_RANDOM_LEN); + } - if (context) { - pos = seed + 2 * TLS_RANDOM_LEN; - WPA_PUT_BE16(pos, context_len); - pos += 2; - os_memcpy(pos, context, context_len); - } + if (context) { + pos = seed + 2 * TLS_RANDOM_LEN; + WPA_PUT_BE16(pos, context_len); + pos += 2; + os_memcpy(pos, context, context_len); + } - wpa_hexdump_key(MSG_MSGDUMP, "random", seed, 2 * TLS_RANDOM_LEN); - wpa_hexdump_key(MSG_MSGDUMP, "master", ssl->MBEDTLS_PRIVATE(session)->MBEDTLS_PRIVATE(master), TLS_MASTER_SECRET_LEN); + wpa_hexdump_key(MSG_MSGDUMP, "random", seed, 2 * TLS_RANDOM_LEN); + wpa_hexdump_key(MSG_MSGDUMP, "master", ssl->MBEDTLS_PRIVATE(session)->MBEDTLS_PRIVATE(master), TLS_MASTER_SECRET_LEN); - ret = mbedtls_ssl_tls_prf(conn->tls_prf_type, conn->master_secret, TLS_MASTER_SECRET_LEN, - label, seed, seed_len, out, out_len); - os_free(seed); + ret = mbedtls_ssl_tls_prf(conn->tls_prf_type, conn->master_secret, TLS_MASTER_SECRET_LEN, + label, seed, seed_len, out, out_len); + os_free(seed); - if (ret < 0) { - wpa_printf(MSG_ERROR, "prf failed, ret=%d", ret); - return -1; - } - wpa_hexdump_key(MSG_MSGDUMP, "key", out, out_len); + if (ret < 0) { + wpa_printf(MSG_ERROR, "prf failed, ret=%d", ret); + return -1; + } + wpa_hexdump_key(MSG_MSGDUMP, "key", out, out_len); - return ret; + return ret; } #ifdef CONFIG_TLSV13 /* RFC 8446 Section 7.5 */ static int tls13_connection_export_key(void *tls_ctx, struct tls_connection *conn, - const char *label, const u8 *context, - size_t context_len, u8 *out, size_t out_len) + const char *label, const u8 *context, + size_t context_len, u8 *out, size_t out_len) { - int ret; - mbedtls_ssl_context *ssl = &conn->tls->ssl; - psa_algorithm_t hash_alg; - size_t hash_len; - unsigned char tmp_secret[PSA_MAC_MAX_SIZE] = { 0 }; - unsigned char hashed_context[PSA_HASH_MAX_SIZE] = { 0 }; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; + int ret; + mbedtls_ssl_context *ssl = &conn->tls->ssl; + psa_algorithm_t hash_alg; + size_t hash_len; + unsigned char tmp_secret[PSA_MAC_MAX_SIZE] = { 0 }; + unsigned char hashed_context[PSA_HASH_MAX_SIZE] = { 0 }; + psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info; + const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info; - hash_alg = mbedtls_md_psa_alg_from_type(ciphersuite_info->mac); - hash_len = PSA_HASH_LENGTH(hash_alg); + hash_alg = mbedtls_md_psa_alg_from_type(ciphersuite_info->mac); + hash_len = PSA_HASH_LENGTH(hash_alg); - ret = mbedtls_ssl_tls13_derive_secret(hash_alg, - conn->exporter_master_secret, hash_len, - (unsigned char const *) label, (size_t) strlen(label), - NULL, 0, - MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, - tmp_secret, hash_len); + ret = mbedtls_ssl_tls13_derive_secret(hash_alg, + conn->exporter_master_secret, hash_len, + (unsigned char const *) label, (size_t) strlen(label), + NULL, 0, + MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, + tmp_secret, hash_len); - if (ret != 0) { - wpa_printf(MSG_ERROR, "%s(): mbedtls_ssl_tls13_derive_secret() failed", - __func__); - return ret; - } + if (ret != 0) { + wpa_printf(MSG_ERROR, "%s(): mbedtls_ssl_tls13_derive_secret() failed", + __func__); + return ret; + } - status = psa_hash_compute(hash_alg, context, context_len, hashed_context, - PSA_HASH_LENGTH(hash_alg), &context_len); - if (status != PSA_SUCCESS) { - wpa_printf(MSG_ERROR, "%s(): psa_hash_compute() failed", - __func__); - ret = PSA_TO_MBEDTLS_ERR(status); - return ret; - } + status = psa_hash_compute(hash_alg, context, context_len, hashed_context, + PSA_HASH_LENGTH(hash_alg), &context_len); + if (status != PSA_SUCCESS) { + wpa_printf(MSG_ERROR, "%s(): psa_hash_compute() failed", + __func__); + ret = PSA_TO_MBEDTLS_ERR(status); + return ret; + } - ret = mbedtls_ssl_tls13_hkdf_expand_label(hash_alg, - tmp_secret, hash_len, - MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exporter), - hashed_context, context_len, - out, out_len); + ret = mbedtls_ssl_tls13_hkdf_expand_label(hash_alg, + tmp_secret, hash_len, + MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exporter), + hashed_context, context_len, + out, out_len); - if (ret != 0) { - wpa_printf(MSG_ERROR, "%s(): psa_hash_compute() failed", - __func__); - return ret; - } - return 0; + if (ret != 0) { + wpa_printf(MSG_ERROR, "%s(): psa_hash_compute() failed", + __func__); + return ret; + } + return 0; } #endif /* CONFIG_TLSV13 */ int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn, - const char *label, const u8 *context, - size_t context_len, u8 *out, size_t out_len) + const char *label, const u8 *context, + size_t context_len, u8 *out, size_t out_len) { #ifdef CONFIG_TLSV13 - if (mbedtls_ssl_get_version_number(&conn->tls->ssl) == MBEDTLS_SSL_VERSION_TLS1_3) - return tls13_connection_export_key(tls_ctx, conn, label, context, context_len, - out, out_len); + if (mbedtls_ssl_get_version_number(&conn->tls->ssl) == MBEDTLS_SSL_VERSION_TLS1_3) + return tls13_connection_export_key(tls_ctx, conn, label, context, context_len, + out, out_len); #endif /* CONFIG_TLSV13 */ - return tls_connection_prf(tls_ctx, conn, label, context, context_len, - 0, out, out_len); + return tls_connection_prf(tls_ctx, conn, label, context, context_len, + 0, out, out_len); } int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn, - u8 *out, size_t out_len) + u8 *out, size_t out_len) { - wpa_printf(MSG_INFO, "TLS: tls_connection_get_eap_fast_key not supported, please unset mbedtls crypto and try again"); - return -1; + wpa_printf(MSG_INFO, "TLS: tls_connection_get_eap_fast_key not supported, please unset mbedtls crypto and try again"); + return -1; } int tls_connection_client_hello_ext(void *tls_ctx, struct tls_connection *conn, - int ext_type, const u8 *data, - size_t data_len) + int ext_type, const u8 *data, + size_t data_len) { - wpa_printf(MSG_INFO, "TLS: tls_connection_client_hello_ext not supported, please unset mbedtls crypto and try again"); - return -1; + wpa_printf(MSG_INFO, "TLS: tls_connection_client_hello_ext not supported, please unset mbedtls crypto and try again"); + return -1; } int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn) { - if (conn->tls_io_data.in_data) { - wpabuf_free(conn->tls_io_data.in_data); - } - conn->tls_io_data.in_data = NULL; + if (conn->tls_io_data.in_data) { + wpabuf_free(conn->tls_io_data.in_data); + } + conn->tls_io_data.in_data = NULL; - /* outdata may have dangling pointer */ - conn->tls_io_data.out_data = NULL; + /* outdata may have dangling pointer */ + conn->tls_io_data.out_data = NULL; - return mbedtls_ssl_session_reset(&conn->tls->ssl); + return mbedtls_ssl_session_reset(&conn->tls->ssl); } int tls_connection_get_random(void *tls_ctx, struct tls_connection *conn, - struct tls_random *data) + struct tls_random *data) { - mbedtls_ssl_context *ssl = &conn->tls->ssl; + mbedtls_ssl_context *ssl = &conn->tls->ssl; - os_memset(data, 0, sizeof(*data)); - if (ssl->MBEDTLS_PRIVATE(state) == MBEDTLS_SSL_CLIENT_HELLO) { - return -1; - } + os_memset(data, 0, sizeof(*data)); + if (ssl->MBEDTLS_PRIVATE(state) == MBEDTLS_SSL_CLIENT_HELLO) { + return -1; + } - data->client_random = conn->randbytes; - data->client_random_len = TLS_RANDOM_LEN; + data->client_random = conn->randbytes; + data->client_random_len = TLS_RANDOM_LEN; - if (ssl->MBEDTLS_PRIVATE(state) != MBEDTLS_SSL_SERVER_HELLO) { - data->server_random = conn->randbytes + TLS_RANDOM_LEN; - data->server_random_len = TLS_RANDOM_LEN; - } + if (ssl->MBEDTLS_PRIVATE(state) != MBEDTLS_SSL_SERVER_HELLO) { + data->server_random = conn->randbytes + TLS_RANDOM_LEN; + data->server_random_len = TLS_RANDOM_LEN; + } - return 0; + return 0; } diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_common.c b/components/wpa_supplicant/esp_supplicant/src/esp_common.c index 97f41ba758..db249a8763 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_common.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_common.c @@ -50,278 +50,283 @@ static bool s_supplicant_task_init_done; #define SUPPLICANT_TASK_STACK_SIZE (6144 + TASK_STACK_SIZE_ADD) static int handle_action_frm(u8 *frame, size_t len, - u8 *sender, int8_t rssi, u8 channel) + u8 *sender, int8_t rssi, u8 channel) { - struct ieee_mgmt_frame *frm = os_malloc(sizeof(struct ieee_mgmt_frame) + len); + struct ieee_mgmt_frame *frm = os_malloc(sizeof(struct ieee_mgmt_frame) + len); - if (!frm) { - wpa_printf(MSG_ERROR, "memory allocation failed"); - return -1; - } + if (!frm) { + wpa_printf(MSG_ERROR, "memory allocation failed"); + return -1; + } - os_memcpy(frm->sender, sender, ETH_ALEN); - frm->len = len; - frm->channel = channel; - frm->rssi = rssi; + os_memcpy(frm->sender, sender, ETH_ALEN); + frm->len = len; + frm->channel = channel; + frm->rssi = rssi; - os_memcpy(frm->payload, frame, len); - if (esp_supplicant_post_evt(SIG_SUPPLICANT_RX_ACTION, (u32)frm) != 0) { - os_free(frm); - return -1; - } + os_memcpy(frm->payload, frame, len); + if (esp_supplicant_post_evt(SIG_SUPPLICANT_RX_ACTION, (u32)frm) != 0) { + os_free(frm); + return -1; + } - return 0; + return 0; } #endif /* CONFIG_SUPPLICANT_TASK */ #if defined(CONFIG_IEEE80211KV) static void handle_rrm_frame(struct wpa_supplicant *wpa_s, u8 *sender, - u8 *payload, size_t len, int8_t rssi) + u8 *payload, size_t len, int8_t rssi) { - if (payload[0] == WLAN_RRM_NEIGHBOR_REPORT_RESPONSE) { - /* neighbor report parsing */ - wpas_rrm_process_neighbor_rep(wpa_s, payload + 1, len - 1); - } else if (payload[0] == WLAN_RRM_RADIO_MEASUREMENT_REQUEST) { - /* Beacon measurement */ - wpas_rrm_handle_radio_measurement_request(wpa_s, NULL, - sender, payload + 1, len - 1); - } else if (payload[0] == WLAN_RRM_LINK_MEASUREMENT_REQUEST) { - /* Link measurement */ - wpas_rrm_handle_link_measurement_request(wpa_s, NULL, - payload + 1, len - 1, rssi); - } + if (payload[0] == WLAN_RRM_NEIGHBOR_REPORT_RESPONSE) { + /* neighbor report parsing */ + wpas_rrm_process_neighbor_rep(wpa_s, payload + 1, len - 1); + } else if (payload[0] == WLAN_RRM_RADIO_MEASUREMENT_REQUEST) { + /* Beacon measurement */ + wpas_rrm_handle_radio_measurement_request(wpa_s, NULL, + sender, payload + 1, len - 1); + } else if (payload[0] == WLAN_RRM_LINK_MEASUREMENT_REQUEST) { + /* Link measurement */ + wpas_rrm_handle_link_measurement_request(wpa_s, NULL, + payload + 1, len - 1, rssi); + } } static int mgmt_rx_action(u8 *frame, size_t len, u8 *sender, int8_t rssi, u8 channel) { - u8 category; - u8 bssid[ETH_ALEN]; - struct wpa_supplicant *wpa_s = &g_wpa_supp; - int ret = esp_wifi_get_assoc_bssid_internal(bssid); + u8 category; + u8 bssid[ETH_ALEN]; + struct wpa_supplicant *wpa_s = &g_wpa_supp; + int ret = esp_wifi_get_assoc_bssid_internal(bssid); - if (ret < 0) { - wpa_printf(MSG_INFO, "STA not associated"); - return -1; - } + if (ret < 0) { + wpa_printf(MSG_INFO, "STA not associated"); + return -1; + } - category = *frame++; - len--; - if (category == WLAN_ACTION_WNM) { - ieee802_11_rx_wnm_action(wpa_s, sender, frame, len); - } else if (category == WLAN_ACTION_RADIO_MEASUREMENT) { - handle_rrm_frame(wpa_s, sender, frame, len, rssi); - } + category = *frame++; + len--; + if (category == WLAN_ACTION_WNM) { + ieee802_11_rx_wnm_action(wpa_s, sender, frame, len); + } else if (category == WLAN_ACTION_RADIO_MEASUREMENT) { + handle_rrm_frame(wpa_s, sender, frame, len, rssi); + } - return 0; + return 0; } #endif /* defined(CONFIG_IEEE80211KV) */ #ifdef CONFIG_SUPPLICANT_TASK static void btm_rrm_task(void *pvParameters) { - supplicant_event_t *evt; - bool task_del = false; + supplicant_event_t *evt; + bool task_del = false; - while(1) { - if (os_queue_recv(s_supplicant_evt_queue, &evt, OS_BLOCK) != TRUE) - continue; + while (1) { + if (os_queue_recv(s_supplicant_evt_queue, &evt, OS_BLOCK) != TRUE) { + continue; + } - /* event validation failed */ - if (evt->id >= SIG_SUPPLICANT_MAX) { - os_free(evt); - continue; - } + /* event validation failed */ + if (evt->id >= SIG_SUPPLICANT_MAX) { + os_free(evt); + continue; + } - switch (evt->id) { - case SIG_SUPPLICANT_RX_ACTION: - { - struct ieee_mgmt_frame *frm = (struct ieee_mgmt_frame *)evt->data; - mgmt_rx_action(frm->payload, frm->len, frm->sender, frm->rssi, frm->channel); - os_free(frm); - break; - } + switch (evt->id) { + case SIG_SUPPLICANT_RX_ACTION: { + struct ieee_mgmt_frame *frm = (struct ieee_mgmt_frame *)evt->data; + mgmt_rx_action(frm->payload, frm->len, frm->sender, frm->rssi, frm->channel); + os_free(frm); + break; + } - case SIG_SUPPLICANT_SCAN_DONE: - esp_supplicant_handle_scan_done_evt(); - break; - case SIG_SUPPLICANT_DEL_TASK: - task_del = true; - break; - default: - break; - } + case SIG_SUPPLICANT_SCAN_DONE: + esp_supplicant_handle_scan_done_evt(); + break; + case SIG_SUPPLICANT_DEL_TASK: + task_del = true; + break; + default: + break; + } - os_free(evt); + os_free(evt); - if (task_del) - break; - } + if (task_del) { + break; + } + } - os_queue_delete(s_supplicant_evt_queue); - s_supplicant_evt_queue = NULL; + os_queue_delete(s_supplicant_evt_queue); + s_supplicant_evt_queue = NULL; - /* At this point, we completed */ - os_task_delete(NULL); + /* At this point, we completed */ + os_task_delete(NULL); } #endif /* CONFIG_SUPPLICANT_TASK */ static void clear_bssid_flag_and_channel(struct wpa_supplicant *wpa_s) { - wifi_config_t *config; + wifi_config_t *config; - /* Reset only if btm is enabled */ - if (esp_wifi_is_btm_enabled_internal(WIFI_IF_STA) == false) - return; + /* Reset only if btm is enabled */ + if (esp_wifi_is_btm_enabled_internal(WIFI_IF_STA) == false) { + return; + } - config = os_zalloc(sizeof(wifi_config_t)); + config = os_zalloc(sizeof(wifi_config_t)); - if (!config) { - wpa_printf(MSG_ERROR, "failed to allocate memory"); - return; - } + if (!config) { + wpa_printf(MSG_ERROR, "failed to allocate memory"); + return; + } - esp_wifi_get_config(WIFI_IF_STA, config); - if (config->sta.bssid_set || config->sta.channel) { - config->sta.channel = 0; - config->sta.bssid_set = 0; - esp_wifi_set_config(WIFI_IF_STA, config); - } - os_free(config); - wpa_printf(MSG_DEBUG, "cleared bssid flag"); + esp_wifi_get_config(WIFI_IF_STA, config); + if (config->sta.bssid_set || config->sta.channel) { + config->sta.channel = 0; + config->sta.bssid_set = 0; + esp_wifi_set_config(WIFI_IF_STA, config); + } + os_free(config); + wpa_printf(MSG_DEBUG, "cleared bssid flag"); } static void register_mgmt_frames(struct wpa_supplicant *wpa_s) { - wpa_s->type &= ~(1 << WLAN_FC_STYPE_ACTION); - /* subtype is defined only for action frame */ - wpa_s->subtype = 0; + wpa_s->type &= ~(1 << WLAN_FC_STYPE_ACTION); + /* subtype is defined only for action frame */ + wpa_s->subtype = 0; #ifdef CONFIG_IEEE80211KV - /* current supported features in supplicant: rrm and btm */ - if (esp_wifi_is_rm_enabled_internal(WIFI_IF_STA)) - wpa_s->subtype = 1 << WLAN_ACTION_RADIO_MEASUREMENT; - if (esp_wifi_is_btm_enabled_internal(WIFI_IF_STA)) - wpa_s->subtype |= 1 << WLAN_ACTION_WNM; + /* current supported features in supplicant: rrm and btm */ + if (esp_wifi_is_rm_enabled_internal(WIFI_IF_STA)) { + wpa_s->subtype = 1 << WLAN_ACTION_RADIO_MEASUREMENT; + } + if (esp_wifi_is_btm_enabled_internal(WIFI_IF_STA)) { + wpa_s->subtype |= 1 << WLAN_ACTION_WNM; + } - if (wpa_s->subtype) - wpa_s->type |= 1 << WLAN_FC_STYPE_ACTION; + if (wpa_s->subtype) { + wpa_s->type |= 1 << WLAN_FC_STYPE_ACTION; + } #endif /* CONFIG_IEEE80211KV */ #ifdef CONFIG_IEEE80211R - /* register auth/assoc frames if FT is enabled */ - if (esp_wifi_is_ft_enabled_internal(ESP_IF_WIFI_STA)) - wpa_s->type |= (1 << WLAN_FC_STYPE_AUTH) | - (1 << WLAN_FC_STYPE_ASSOC_RESP) | - (1 << WLAN_FC_STYPE_REASSOC_RESP); + /* register auth/assoc frames if FT is enabled */ + if (esp_wifi_is_ft_enabled_internal(ESP_IF_WIFI_STA)) + wpa_s->type |= (1 << WLAN_FC_STYPE_AUTH) | + (1 << WLAN_FC_STYPE_ASSOC_RESP) | + (1 << WLAN_FC_STYPE_REASSOC_RESP); #endif /* CONFIG_IEEE80211R */ - esp_wifi_register_mgmt_frame_internal(wpa_s->type, wpa_s->subtype); + esp_wifi_register_mgmt_frame_internal(wpa_s->type, wpa_s->subtype); } #ifdef CONFIG_IEEE80211R static int handle_auth_frame(u8 *frame, size_t len, - u8 *sender, int8_t rssi, u8 channel) + u8 *sender, int8_t rssi, u8 channel) { - if (gWpaSm.key_mgmt == WPA_KEY_MGMT_FT_PSK) { - if (gWpaSm.ft_protocol) { - if (wpa_ft_process_response(&gWpaSm, frame + 6, - len - 6, 0, sender, NULL, 0) < 0) { - wpa_sm_set_ft_params(&gWpaSm, NULL, 0); - return -1; - } - } - } - return 0; + if (gWpaSm.key_mgmt == WPA_KEY_MGMT_FT_PSK) { + if (gWpaSm.ft_protocol) { + if (wpa_ft_process_response(&gWpaSm, frame + 6, + len - 6, 0, sender, NULL, 0) < 0) { + wpa_sm_set_ft_params(&gWpaSm, NULL, 0); + return -1; + } + } + } + return 0; } static int handle_assoc_frame(u8 *frame, size_t len, - u8 *sender, int8_t rssi, u8 channel) + u8 *sender, int8_t rssi, u8 channel) { - if (gWpaSm.key_mgmt == WPA_KEY_MGMT_FT_PSK) { - if (gWpaSm.ft_protocol) { - if (wpa_ft_validate_reassoc_resp(&gWpaSm, frame + 6, len - 6, sender)) { - wpa_sm_set_ft_params(&gWpaSm, NULL, 0); - return -1; - } - } - wpa_sm_set_ft_params(&gWpaSm, frame + 6, len - 6); - } - return 0; + if (gWpaSm.key_mgmt == WPA_KEY_MGMT_FT_PSK) { + if (gWpaSm.ft_protocol) { + if (wpa_ft_validate_reassoc_resp(&gWpaSm, frame + 6, len - 6, sender)) { + wpa_sm_set_ft_params(&gWpaSm, NULL, 0); + return -1; + } + } + wpa_sm_set_ft_params(&gWpaSm, frame + 6, len - 6); + } + return 0; } #endif /* CONFIG_IEEE80211R */ #endif /* defined(CONFIG_IEEE80211KV) || defined(CONFIG_IEEE80211R) */ void esp_supplicant_unset_all_appie(void) { - uint8_t appie; - for (appie = WIFI_APPIE_PROBEREQ; appie < WIFI_APPIE_RAM_MAX; appie++) { + uint8_t appie; + for (appie = WIFI_APPIE_PROBEREQ; appie < WIFI_APPIE_RAM_MAX; appie++) { esp_wifi_unset_appie_internal(appie); - } + } } static int ieee80211_handle_rx_frm(u8 type, u8 *frame, size_t len, u8 *sender, - int8_t rssi, u8 channel, u64 current_tsf) + int8_t rssi, u8 channel, u64 current_tsf) { - int ret = 0; + int ret = 0; - switch (type) { + switch (type) { #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211KV) - case WLAN_FC_STYPE_BEACON: - case WLAN_FC_STYPE_PROBE_RESP: - ret = esp_handle_beacon_probe(type, frame, len, sender, rssi, channel, current_tsf); - break; + case WLAN_FC_STYPE_BEACON: + case WLAN_FC_STYPE_PROBE_RESP: + ret = esp_handle_beacon_probe(type, frame, len, sender, rssi, channel, current_tsf); + break; #endif /* defined(CONFIG_IEEE80211KV) || defined(CONFIG_IEEE80211R) */ #ifdef CONFIG_IEEE80211R - case WLAN_FC_STYPE_AUTH: - ret = handle_auth_frame(frame, len, sender, rssi, channel); - break; + case WLAN_FC_STYPE_AUTH: + ret = handle_auth_frame(frame, len, sender, rssi, channel); + break; #endif /* CONFIG_IEEE80211R */ - case WLAN_FC_STYPE_ASSOC_RESP: - case WLAN_FC_STYPE_REASSOC_RESP: - wpa_sm_notify_assoc(&gWpaSm, sender); + case WLAN_FC_STYPE_ASSOC_RESP: + case WLAN_FC_STYPE_REASSOC_RESP: + wpa_sm_notify_assoc(&gWpaSm, sender); #ifdef CONFIG_IEEE80211R - ret = handle_assoc_frame(frame, len, sender, rssi, channel); + ret = handle_assoc_frame(frame, len, sender, rssi, channel); #endif /* CONFIG_IEEE80211R */ - break; + break; #if defined(CONFIG_IEEE80211KV) - case WLAN_FC_STYPE_ACTION: + case WLAN_FC_STYPE_ACTION: #ifdef CONFIG_SUPPLICANT_TASK - ret = handle_action_frm(frame, len, sender, rssi, channel); + ret = handle_action_frm(frame, len, sender, rssi, channel); #else /* CONFIG_SUPPLICANT_TASK */ - ret = mgmt_rx_action(frame, len, sender, rssi, channel); + ret = mgmt_rx_action(frame, len, sender, rssi, channel); #endif /* CONFIG_SUPPLICANT_TASK */ - break; + break; #endif /* defined(CONFIG_IEEE80211KV) */ - default: - ret = -1; - break; - } + default: + ret = -1; + break; + } - return ret; + return ret; } #if defined(CONFIG_IEEE80211KV) || defined(CONFIG_IEEE80211R) #ifdef CONFIG_MBO bool mbo_bss_profile_match(u8 *bssid) { - /* In case supplicant wants drivers to skip this BSS, return false */ - struct wpa_bss *bss = wpa_bss_get_bssid(&g_wpa_supp, bssid); - if (!bss) { - return true; - } - const u8 *assoc_disallow = wpas_mbo_get_bss_attr(bss, MBO_ATTR_ID_ASSOC_DISALLOW); - if (assoc_disallow && assoc_disallow[1] >= 1) { - wpa_printf(MSG_DEBUG, - "skip - MBO association disallowed (reason %u)", assoc_disallow[2]); - return false; - } + /* In case supplicant wants drivers to skip this BSS, return false */ + struct wpa_bss *bss = wpa_bss_get_bssid(&g_wpa_supp, bssid); + if (!bss) { + return true; + } + const u8 *assoc_disallow = wpas_mbo_get_bss_attr(bss, MBO_ATTR_ID_ASSOC_DISALLOW); + if (assoc_disallow && assoc_disallow[1] >= 1) { + wpa_printf(MSG_DEBUG, + "skip - MBO association disallowed (reason %u)", assoc_disallow[2]); + return false; + } - if (wpa_is_bss_tmp_disallowed(&g_wpa_supp, bss)) { - wpa_printf(MSG_DEBUG, - "skip - BSS is temporary disallowed"); - return false; - } + if (wpa_is_bss_tmp_disallowed(&g_wpa_supp, bss)) { + wpa_printf(MSG_DEBUG, + "skip - BSS is temporary disallowed"); + return false; + } - return true; + return true; } #endif /* CONFIG_MBO */ @@ -329,98 +334,98 @@ bool mbo_bss_profile_match(u8 *bssid) int esp_supplicant_common_init(struct wpa_funcs *wpa_cb) { - struct wpa_supplicant *wpa_s = &g_wpa_supp; - int ret = 0; + struct wpa_supplicant *wpa_s = &g_wpa_supp; + int ret = 0; #if defined(CONFIG_IEEE80211KV) || defined(CONFIG_IEEE80211R) #ifdef CONFIG_SUPPLICANT_TASK - if (!s_supplicant_api_lock) { - s_supplicant_api_lock = os_recursive_mutex_create(); - } + if (!s_supplicant_api_lock) { + s_supplicant_api_lock = os_recursive_mutex_create(); + } - if (!s_supplicant_api_lock) { - wpa_printf(MSG_ERROR, "%s: failed to create Supplicant API lock", __func__); - ret = -1; - goto err; - } + if (!s_supplicant_api_lock) { + wpa_printf(MSG_ERROR, "%s: failed to create Supplicant API lock", __func__); + ret = -1; + goto err; + } - s_supplicant_evt_queue = os_queue_create(3, sizeof(supplicant_event_t)); + s_supplicant_evt_queue = os_queue_create(3, sizeof(supplicant_event_t)); - if (!s_supplicant_evt_queue) { - wpa_printf(MSG_ERROR, "%s: failed to create Supplicant event queue", __func__); - ret = -1; - goto err; - } - ret = os_task_create(btm_rrm_task, "btm_rrm_t", SUPPLICANT_TASK_STACK_SIZE, NULL, 2, &s_supplicant_task_hdl); - if (ret != TRUE) { - wpa_printf(MSG_ERROR, "btm: failed to create task"); - ret = -1; - goto err; - } - s_supplicant_task_init_done = true; + if (!s_supplicant_evt_queue) { + wpa_printf(MSG_ERROR, "%s: failed to create Supplicant event queue", __func__); + ret = -1; + goto err; + } + ret = os_task_create(btm_rrm_task, "btm_rrm_t", SUPPLICANT_TASK_STACK_SIZE, NULL, 2, &s_supplicant_task_hdl); + if (ret != TRUE) { + wpa_printf(MSG_ERROR, "btm: failed to create task"); + ret = -1; + goto err; + } + s_supplicant_task_init_done = true; #endif /* CONFIG_SUPPLICANT_TASK */ #ifdef CONFIG_IEEE80211KV - wpas_rrm_reset(wpa_s); - wpas_clear_beacon_rep_data(wpa_s); + wpas_rrm_reset(wpa_s); + wpas_clear_beacon_rep_data(wpa_s); #endif /* CONFIG_IEEE80211KV */ - esp_scan_init(wpa_s); + esp_scan_init(wpa_s); #endif /* defined(CONFIG_IEEE80211KV) || defined(CONFIG_IEEE80211R) */ - wpa_s->type = 0; - wpa_s->subtype = 0; - wpa_s->type |= (1 << WLAN_FC_STYPE_ASSOC_RESP) | (1 << WLAN_FC_STYPE_REASSOC_RESP) | (1 << WLAN_FC_STYPE_AUTH); - if (esp_wifi_register_mgmt_frame_internal(wpa_s->type, wpa_s->subtype) != ESP_OK) { - ret = -1; - goto err; - } - wpa_cb->wpa_sta_rx_mgmt = ieee80211_handle_rx_frm; + wpa_s->type = 0; + wpa_s->subtype = 0; + wpa_s->type |= (1 << WLAN_FC_STYPE_ASSOC_RESP) | (1 << WLAN_FC_STYPE_REASSOC_RESP) | (1 << WLAN_FC_STYPE_AUTH); + if (esp_wifi_register_mgmt_frame_internal(wpa_s->type, wpa_s->subtype) != ESP_OK) { + ret = -1; + goto err; + } + wpa_cb->wpa_sta_rx_mgmt = ieee80211_handle_rx_frm; #if defined(CONFIG_IEEE80211KV) || defined(CONFIG_IEEE80211R) #ifdef CONFIG_MBO - dl_list_init(&wpa_s->bss_tmp_disallowed); + dl_list_init(&wpa_s->bss_tmp_disallowed); #endif /* CONFIG_MBO */ #endif /* defined(CONFIG_IEEE80211KV) || defined(CONFIG_IEEE80211R) */ - return 0; + return 0; err: - esp_supplicant_common_deinit(); - return ret; + esp_supplicant_common_deinit(); + return ret; } void esp_supplicant_common_deinit(void) { - struct wpa_supplicant *wpa_s = &g_wpa_supp; + struct wpa_supplicant *wpa_s = &g_wpa_supp; #if defined(CONFIG_IEEE80211KV) || defined(CONFIG_IEEE80211R) - esp_scan_deinit(wpa_s); + esp_scan_deinit(wpa_s); #ifdef CONFIG_IEEE80211KV - wpas_rrm_reset(wpa_s); - wpas_clear_beacon_rep_data(wpa_s); + wpas_rrm_reset(wpa_s); + wpas_clear_beacon_rep_data(wpa_s); #endif /* CONFIG_IEEE80211KV */ #endif /* defined(CONFIG_IEEE80211KV) || defined(CONFIG_IEEE80211R) */ - if (wpa_s->type) { - wpa_s->type = 0; - esp_wifi_register_mgmt_frame_internal(wpa_s->type, wpa_s->subtype); - } + if (wpa_s->type) { + wpa_s->type = 0; + esp_wifi_register_mgmt_frame_internal(wpa_s->type, wpa_s->subtype); + } #if defined(CONFIG_IEEE80211KV) || defined(CONFIG_IEEE80211R) #ifdef CONFIG_SUPPLICANT_TASK - /* We have failed to create task, delete queue and exit */ - if (!s_supplicant_task_hdl) { - if (s_supplicant_evt_queue) { - os_queue_delete(s_supplicant_evt_queue); - s_supplicant_evt_queue = NULL; - } - if (s_supplicant_api_lock) { - os_semphr_delete(s_supplicant_api_lock); - s_supplicant_api_lock = NULL; - } - } else if (esp_supplicant_post_evt(SIG_SUPPLICANT_DEL_TASK, 0) != 0) { - /* failed to post delete event, just delete event queue and exit */ - if (s_supplicant_evt_queue) { - os_queue_delete(s_supplicant_evt_queue); - s_supplicant_evt_queue = NULL; - } - } - s_supplicant_task_init_done = false; + /* We have failed to create task, delete queue and exit */ + if (!s_supplicant_task_hdl) { + if (s_supplicant_evt_queue) { + os_queue_delete(s_supplicant_evt_queue); + s_supplicant_evt_queue = NULL; + } + if (s_supplicant_api_lock) { + os_semphr_delete(s_supplicant_api_lock); + s_supplicant_api_lock = NULL; + } + } else if (esp_supplicant_post_evt(SIG_SUPPLICANT_DEL_TASK, 0) != 0) { + /* failed to post delete event, just delete event queue and exit */ + if (s_supplicant_evt_queue) { + os_queue_delete(s_supplicant_evt_queue); + s_supplicant_evt_queue = NULL; + } + } + s_supplicant_task_init_done = false; #endif /* CONFIG_SUPPLICANT_TASK */ #if CONFIG_ESP_WIFI_ENABLE_ROAMING_APP deinit_roaming_app(); @@ -431,43 +436,43 @@ void esp_supplicant_common_deinit(void) void supplicant_sta_conn_handler(uint8_t *bssid) { #if defined(CONFIG_IEEE80211KV) || defined(CONFIG_IEEE80211R) - u8 *ie; - struct wpa_supplicant *wpa_s = &g_wpa_supp; - struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, bssid); - if (!bss) { - wpa_printf(MSG_INFO, "connected bss entry not present in scan cache"); - return; - } - wpa_s->current_bss = bss; - ie = (u8 *)bss; - ie += sizeof(struct wpa_bss); - ieee802_11_parse_elems(wpa_s, ie, bss->ie_len); - wpa_bss_flush(wpa_s); - /* Register for mgmt frames */ - register_mgmt_frames(wpa_s); - /* clear set bssid flag */ - clear_bssid_flag_and_channel(wpa_s); + u8 *ie; + struct wpa_supplicant *wpa_s = &g_wpa_supp; + struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, bssid); + if (!bss) { + wpa_printf(MSG_INFO, "connected bss entry not present in scan cache"); + return; + } + wpa_s->current_bss = bss; + ie = (u8 *)bss; + ie += sizeof(struct wpa_bss); + ieee802_11_parse_elems(wpa_s, ie, bss->ie_len); + wpa_bss_flush(wpa_s); + /* Register for mgmt frames */ + register_mgmt_frames(wpa_s); + /* clear set bssid flag */ + clear_bssid_flag_and_channel(wpa_s); #endif /* defined(CONFIG_IEEE80211KV) || defined(CONFIG_IEEE80211R) */ } void supplicant_sta_disconn_handler(uint8_t reason_code) { #if defined(CONFIG_IEEE80211KV) || defined(CONFIG_IEEE80211R) - struct wpa_supplicant *wpa_s = &g_wpa_supp; + struct wpa_supplicant *wpa_s = &g_wpa_supp; #ifdef CONFIG_IEEE80211KV - wpas_rrm_reset(wpa_s); - wpas_clear_beacon_rep_data(wpa_s); - /* Not clearing in case of roaming disconnect as BTM induced connection - * itself sets a specific bssid and channel to connect to before disconnection. - * Subsequent connections or disconnections will clear this flag */ - if (reason_code != WIFI_REASON_ROAMING) { - clear_bssid_flag_and_channel(wpa_s); - } + wpas_rrm_reset(wpa_s); + wpas_clear_beacon_rep_data(wpa_s); + /* Not clearing in case of roaming disconnect as BTM induced connection + * itself sets a specific bssid and channel to connect to before disconnection. + * Subsequent connections or disconnections will clear this flag */ + if (reason_code != WIFI_REASON_ROAMING) { + clear_bssid_flag_and_channel(wpa_s); + } #endif /* CONFIG_IEEE80211KV */ - if (wpa_s->current_bss) { - wpa_s->current_bss = NULL; - } + if (wpa_s->current_bss) { + wpa_s->current_bss = NULL; + } #endif /* defined(CONFIG_IEEE80211KV) || defined(CONFIG_IEEE80211R) */ } @@ -475,55 +480,55 @@ void supplicant_sta_disconn_handler(uint8_t reason_code) #ifdef CONFIG_IEEE80211KV bool esp_rrm_is_rrm_supported_connection(void) { - struct wpa_supplicant *wpa_s = &g_wpa_supp; + struct wpa_supplicant *wpa_s = &g_wpa_supp; - if (!wpa_s->current_bss) { - wpa_printf(MSG_DEBUG, "STA not associated, return"); - return false; - } + if (!wpa_s->current_bss) { + wpa_printf(MSG_DEBUG, "STA not associated, return"); + return false; + } - if (!(wpa_s->rrm_ie[0] & WLAN_RRM_CAPS_NEIGHBOR_REPORT)) { - wpa_printf(MSG_DEBUG, - "RRM: No network support for Neighbor Report."); - return false; - } + if (!(wpa_s->rrm_ie[0] & WLAN_RRM_CAPS_NEIGHBOR_REPORT)) { + wpa_printf(MSG_DEBUG, + "RRM: No network support for Neighbor Report."); + return false; + } - return true; + return true; } /*This function has been deprecated in favour of esp_rrm_send_neighbor_report_request*/ int esp_rrm_send_neighbor_rep_request(neighbor_rep_request_cb cb, - void *cb_ctx) + void *cb_ctx) { - struct wpa_supplicant *wpa_s = &g_wpa_supp; - struct wpa_ssid_value wpa_ssid = {0}; - struct wifi_ssid *ssid; + struct wpa_supplicant *wpa_s = &g_wpa_supp; + struct wpa_ssid_value wpa_ssid = {0}; + struct wifi_ssid *ssid; - if (!wpa_s->current_bss) { - wpa_printf(MSG_ERROR, "STA not associated, return"); - return -2; - } + if (!wpa_s->current_bss) { + wpa_printf(MSG_ERROR, "STA not associated, return"); + return -2; + } - if (!(wpa_s->rrm_ie[0] & WLAN_RRM_CAPS_NEIGHBOR_REPORT)) { - wpa_printf(MSG_ERROR, - "RRM: No network support for Neighbor Report."); - return -1; - } + if (!(wpa_s->rrm_ie[0] & WLAN_RRM_CAPS_NEIGHBOR_REPORT)) { + wpa_printf(MSG_ERROR, + "RRM: No network support for Neighbor Report."); + return -1; + } - ssid = esp_wifi_sta_get_prof_ssid_internal(); + ssid = esp_wifi_sta_get_prof_ssid_internal(); - os_memcpy(wpa_ssid.ssid, ssid->ssid, ssid->len); - wpa_ssid.ssid_len = ssid->len; + os_memcpy(wpa_ssid.ssid, ssid->ssid, ssid->len); + wpa_ssid.ssid_len = ssid->len; - return wpas_rrm_send_neighbor_rep_request(wpa_s, &wpa_ssid, 0, 0, cb, cb_ctx); + return wpas_rrm_send_neighbor_rep_request(wpa_s, &wpa_ssid, 0, 0, cb, cb_ctx); } - -void neighbor_report_recvd_cb(void *ctx, const uint8_t *report, size_t report_len) { - if (report == NULL) { - wpa_printf(MSG_DEBUG, "RRM: Notifying neighbor report - NONE"); - esp_event_post(WIFI_EVENT, WIFI_EVENT_STA_NEIGHBOR_REP, NULL, 0, 0); - return; - } +void neighbor_report_recvd_cb(void *ctx, const uint8_t *report, size_t report_len) +{ + if (report == NULL) { + wpa_printf(MSG_DEBUG, "RRM: Notifying neighbor report - NONE"); + esp_event_post(WIFI_EVENT, WIFI_EVENT_STA_NEIGHBOR_REP, NULL, 0, 0); + return; + } if (report_len > ESP_WIFI_MAX_NEIGHBOR_REP_LEN) { wpa_printf(MSG_ERROR, "RRM: Neighbor report too large (>%d bytes), hence not reporting", ESP_WIFI_MAX_NEIGHBOR_REP_LEN); return; @@ -537,216 +542,216 @@ void neighbor_report_recvd_cb(void *ctx, const uint8_t *report, size_t report_le int esp_rrm_send_neighbor_report_request(void) { - struct wpa_supplicant *wpa_s = &g_wpa_supp; - struct wpa_ssid_value wpa_ssid = {0}; - struct wifi_ssid *ssid; + struct wpa_supplicant *wpa_s = &g_wpa_supp; + struct wpa_ssid_value wpa_ssid = {0}; + struct wifi_ssid *ssid; - if (!wpa_s->current_bss) { - wpa_printf(MSG_ERROR, "STA not associated, return"); - return -2; - } + if (!wpa_s->current_bss) { + wpa_printf(MSG_ERROR, "STA not associated, return"); + return -2; + } - if (!(wpa_s->rrm_ie[0] & WLAN_RRM_CAPS_NEIGHBOR_REPORT)) { - wpa_printf(MSG_ERROR, - "RRM: No network support for Neighbor Report."); - return -1; - } + if (!(wpa_s->rrm_ie[0] & WLAN_RRM_CAPS_NEIGHBOR_REPORT)) { + wpa_printf(MSG_ERROR, + "RRM: No network support for Neighbor Report."); + return -1; + } - ssid = esp_wifi_sta_get_prof_ssid_internal(); + ssid = esp_wifi_sta_get_prof_ssid_internal(); - os_memcpy(wpa_ssid.ssid, ssid->ssid, ssid->len); - wpa_ssid.ssid_len = ssid->len; + os_memcpy(wpa_ssid.ssid, ssid->ssid, ssid->len); + wpa_ssid.ssid_len = ssid->len; - return wpas_rrm_send_neighbor_rep_request(wpa_s, &wpa_ssid, 0, 0, neighbor_report_recvd_cb, NULL); + return wpas_rrm_send_neighbor_rep_request(wpa_s, &wpa_ssid, 0, 0, neighbor_report_recvd_cb, NULL); } bool esp_wnm_is_btm_supported_connection(void) { - struct wpa_supplicant *wpa_s = &g_wpa_supp; + struct wpa_supplicant *wpa_s = &g_wpa_supp; - if (!wpa_s->current_bss) { - wpa_printf(MSG_DEBUG, "STA not associated, return"); - return false; - } + if (!wpa_s->current_bss) { + wpa_printf(MSG_DEBUG, "STA not associated, return"); + return false; + } - if (!wpa_bss_ext_capab(wpa_s->current_bss, WLAN_EXT_CAPAB_BSS_TRANSITION)) { - wpa_printf(MSG_DEBUG, "AP doesn't support BTM, return"); - return false; - } + if (!wpa_bss_ext_capab(wpa_s->current_bss, WLAN_EXT_CAPAB_BSS_TRANSITION)) { + wpa_printf(MSG_DEBUG, "AP doesn't support BTM, return"); + return false; + } - return true; + return true; } int esp_wnm_send_bss_transition_mgmt_query(enum btm_query_reason query_reason, - const char *btm_candidates, - int cand_list) + const char *btm_candidates, + int cand_list) { - struct wpa_supplicant *wpa_s = &g_wpa_supp; + struct wpa_supplicant *wpa_s = &g_wpa_supp; - if (!wpa_s->current_bss) { - wpa_printf(MSG_ERROR, "STA not associated, return"); - return -2; - } + if (!wpa_s->current_bss) { + wpa_printf(MSG_ERROR, "STA not associated, return"); + return -2; + } - if (!wpa_bss_ext_capab(wpa_s->current_bss, WLAN_EXT_CAPAB_BSS_TRANSITION)) { - wpa_printf(MSG_ERROR, "AP doesn't support BTM, return"); - return -1; - } - return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason, btm_candidates, cand_list); + if (!wpa_bss_ext_capab(wpa_s->current_bss, WLAN_EXT_CAPAB_BSS_TRANSITION)) { + wpa_printf(MSG_ERROR, "AP doesn't support BTM, return"); + return -1; + } + return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason, btm_candidates, cand_list); } #ifdef CONFIG_MBO int esp_mbo_update_non_pref_chan(struct non_pref_chan_s *non_pref_chan) { - int ret = wpas_mbo_update_non_pref_chan(&g_wpa_supp, non_pref_chan); + int ret = wpas_mbo_update_non_pref_chan(&g_wpa_supp, non_pref_chan); - return ret; + return ret; } #endif /* CONFIG_MBO */ void wpa_supplicant_connect(struct wpa_supplicant *wpa_s, - struct wpa_bss *bss, char *ssid) + struct wpa_bss *bss, char *ssid) { - wifi_config_t *config = os_zalloc(sizeof(wifi_config_t)); + wifi_config_t *config = os_zalloc(sizeof(wifi_config_t)); - if (!config) { - wpa_printf(MSG_ERROR, "failed to allocate memory"); - return; - } + if (!config) { + wpa_printf(MSG_ERROR, "failed to allocate memory"); + return; + } - esp_wifi_get_config(WIFI_IF_STA, config); - /* We only support roaming in same ESS, therefore only bssid setting is needed */ - os_memcpy(config->sta.bssid, bss->bssid, ETH_ALEN); - config->sta.bssid_set = 1; - config->sta.channel = bss->channel; - /* supplicant connect will only be called in case of bss transition(roaming) */ - esp_wifi_internal_issue_disconnect(WIFI_REASON_BSS_TRANSITION_DISASSOC); - esp_wifi_set_config(WIFI_IF_STA, config); - os_free(config); - esp_wifi_connect(); + esp_wifi_get_config(WIFI_IF_STA, config); + /* We only support roaming in same ESS, therefore only bssid setting is needed */ + os_memcpy(config->sta.bssid, bss->bssid, ETH_ALEN); + config->sta.bssid_set = 1; + config->sta.channel = bss->channel; + /* supplicant connect will only be called in case of bss transition(roaming) */ + esp_wifi_internal_issue_disconnect(WIFI_REASON_BSS_TRANSITION_DISASSOC); + esp_wifi_set_config(WIFI_IF_STA, config); + os_free(config); + esp_wifi_connect(); } static size_t get_rm_enabled_ie(uint8_t *ie, size_t len) { - uint8_t rrm_ie[7] = {0}; - uint8_t rrm_ie_len = 5; - uint8_t *pos = rrm_ie; + uint8_t rrm_ie[7] = {0}; + uint8_t rrm_ie_len = 5; + uint8_t *pos = rrm_ie; - if (!esp_wifi_is_rm_enabled_internal(WIFI_IF_STA)) { - return 0; - } + if (!esp_wifi_is_rm_enabled_internal(WIFI_IF_STA)) { + return 0; + } - *pos++ = WLAN_EID_RRM_ENABLED_CAPABILITIES; - *pos++ = rrm_ie_len; - *pos |= WLAN_RRM_CAPS_LINK_MEASUREMENT; + *pos++ = WLAN_EID_RRM_ENABLED_CAPABILITIES; + *pos++ = rrm_ie_len; + *pos |= WLAN_RRM_CAPS_LINK_MEASUREMENT; - *pos |= WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE | + *pos |= WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE | #ifdef SCAN_CACHE_SUPPORTED - WLAN_RRM_CAPS_BEACON_REPORT_TABLE | + WLAN_RRM_CAPS_BEACON_REPORT_TABLE | #endif /* SCAN_CACHE_SUPPORTED */ - WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE; + WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE; - os_memcpy(ie, rrm_ie, sizeof(rrm_ie)); + os_memcpy(ie, rrm_ie, sizeof(rrm_ie)); - return rrm_ie_len + 2; + return rrm_ie_len + 2; } #ifdef CONFIG_MBO static size_t get_mbo_oce_scan_ie(uint8_t *ie, size_t len) { - uint8_t mbo_ie[32] = {0}; - uint8_t mbo_ie_len = 32; + uint8_t mbo_ie[32] = {0}; + uint8_t mbo_ie_len = 32; - /* Return if MBO IE is not enabled in driver */ - if (!esp_wifi_is_mbo_enabled_internal(WIFI_IF_STA)) { - return 0; - } + /* Return if MBO IE is not enabled in driver */ + if (!esp_wifi_is_mbo_enabled_internal(WIFI_IF_STA)) { + return 0; + } - struct wpabuf *default_ies = NULL; - if (wpabuf_resize(&default_ies, 18) == 0) { - wpas_mbo_scan_ie(&g_wpa_supp, default_ies); - os_memcpy(mbo_ie, wpabuf_head_u8(default_ies), wpabuf_len(default_ies)); - mbo_ie_len = wpabuf_len(default_ies); - wpabuf_free(default_ies); - } - os_memcpy(ie, mbo_ie, mbo_ie_len); - return mbo_ie_len; + struct wpabuf *default_ies = NULL; + if (wpabuf_resize(&default_ies, 18) == 0) { + wpas_mbo_scan_ie(&g_wpa_supp, default_ies); + os_memcpy(mbo_ie, wpabuf_head_u8(default_ies), wpabuf_len(default_ies)); + mbo_ie_len = wpabuf_len(default_ies); + wpabuf_free(default_ies); + } + os_memcpy(ie, mbo_ie, mbo_ie_len); + return mbo_ie_len; } static size_t get_mbo_oce_assoc_ie(uint8_t *ie, size_t len) { - uint8_t mbo_ie[32] = {0}; - uint8_t mbo_ie_len = 32; + uint8_t mbo_ie[32] = {0}; + uint8_t mbo_ie_len = 32; - /* Return if MBO IE is not enabled in driver */ - if (!esp_wifi_is_mbo_enabled_internal(WIFI_IF_STA)) { - return 0; - } + /* Return if MBO IE is not enabled in driver */ + if (!esp_wifi_is_mbo_enabled_internal(WIFI_IF_STA)) { + return 0; + } - mbo_ie_len = wpas_mbo_ie(&g_wpa_supp, mbo_ie, mbo_ie_len, 0); - os_memcpy(ie, mbo_ie, mbo_ie_len); + mbo_ie_len = wpas_mbo_ie(&g_wpa_supp, mbo_ie, mbo_ie_len, 0); + os_memcpy(ie, mbo_ie, mbo_ie_len); - return mbo_ie_len; + return mbo_ie_len; } static uint8_t get_operating_class_ie(uint8_t *ie, size_t len) { - uint8_t op_class_ie[4] = {0}; - uint8_t op_class_ie_len = 2; - uint8_t *pos = op_class_ie; + uint8_t op_class_ie[4] = {0}; + uint8_t op_class_ie_len = 2; + uint8_t *pos = op_class_ie; - *pos++ = WLAN_EID_SUPPORTED_OPERATING_CLASSES; - *pos++ = op_class_ie_len; + *pos++ = WLAN_EID_SUPPORTED_OPERATING_CLASSES; + *pos++ = op_class_ie_len; #define OPER_CLASS 0x51 - /* Current Operating Class */ - *pos++ = OPER_CLASS; + /* Current Operating Class */ + *pos++ = OPER_CLASS; #undef OPER_CLASS - *pos = 0; - os_memcpy(ie, op_class_ie, sizeof(op_class_ie)); + *pos = 0; + os_memcpy(ie, op_class_ie, sizeof(op_class_ie)); - return op_class_ie_len + 2; + return op_class_ie_len + 2; } #endif /* CONFIG_MBO */ static uint8_t get_extended_caps_ie(uint8_t *ie, size_t len) { - uint8_t ext_caps_ie[5] = {0}; - uint8_t ext_caps_ie_len = 3; - uint8_t *pos = ext_caps_ie; - wifi_ioctl_config_t cfg = {0}; - esp_err_t err = 0; + uint8_t ext_caps_ie[5] = {0}; + uint8_t ext_caps_ie_len = 3; + uint8_t *pos = ext_caps_ie; + wifi_ioctl_config_t cfg = {0}; + esp_err_t err = 0; - if (!esp_wifi_is_btm_enabled_internal(WIFI_IF_STA)) { - return 0; - } + if (!esp_wifi_is_btm_enabled_internal(WIFI_IF_STA)) { + return 0; + } - *pos++ = WLAN_EID_EXT_CAPAB; - *pos++ = ext_caps_ie_len; + *pos++ = WLAN_EID_EXT_CAPAB; + *pos++ = ext_caps_ie_len; - err = esp_wifi_internal_ioctl(WIFI_IOCTL_GET_STA_HT2040_COEX, &cfg); - if (err == ESP_OK && cfg.data.ht2040_coex.enable) { - *pos++ |= BIT(WLAN_EXT_CAPAB_20_40_COEX); - } else { - *pos++ = 0; - } - *pos ++ = 0; + err = esp_wifi_internal_ioctl(WIFI_IOCTL_GET_STA_HT2040_COEX, &cfg); + if (err == ESP_OK && cfg.data.ht2040_coex.enable) { + *pos++ |= BIT(WLAN_EXT_CAPAB_20_40_COEX); + } else { + *pos++ = 0; + } + *pos ++ = 0; #define CAPAB_BSS_TRANSITION BIT(3) - *pos |= CAPAB_BSS_TRANSITION; + *pos |= CAPAB_BSS_TRANSITION; #undef CAPAB_BSS_TRANSITION - os_memcpy(ie, ext_caps_ie, sizeof(ext_caps_ie)); + os_memcpy(ie, ext_caps_ie, sizeof(ext_caps_ie)); - return ext_caps_ie_len + 2; + return ext_caps_ie_len + 2; } #else /* CONFIG_IEEE80211KV */ bool esp_rrm_is_rrm_supported_connection(void) { - return false; + return false; } bool esp_wnm_is_btm_supported_connection(void) { - return false; + return false; } #endif /* CONFIG_IEEE80211KV */ @@ -754,26 +759,26 @@ void esp_set_scan_ie(void) { #ifdef CONFIG_IEEE80211KV #define SCAN_IE_LEN 64 - uint8_t *ie, *pos; - size_t len = SCAN_IE_LEN, ie_len; + uint8_t *ie, *pos; + size_t len = SCAN_IE_LEN, ie_len; - ie = os_malloc(SCAN_IE_LEN); - if (!ie) { - wpa_printf(MSG_ERROR, "failed to allocate ie"); - return; - } - pos = ie; - ie_len = get_extended_caps_ie(pos, len); - pos += ie_len; - len -= ie_len; + ie = os_malloc(SCAN_IE_LEN); + if (!ie) { + wpa_printf(MSG_ERROR, "failed to allocate ie"); + return; + } + pos = ie; + ie_len = get_extended_caps_ie(pos, len); + pos += ie_len; + len -= ie_len; #ifdef CONFIG_MBO - ie_len = get_mbo_oce_scan_ie(pos, len); - pos += ie_len; - len -= ie_len; + ie_len = get_mbo_oce_scan_ie(pos, len); + pos += ie_len; + len -= ie_len; #endif /* CONFIG_MBO */ - esp_wifi_unset_appie_internal(WIFI_APPIE_PROBEREQ); - esp_wifi_set_appie_internal(WIFI_APPIE_PROBEREQ, ie, SCAN_IE_LEN - len, 0); - os_free(ie); + esp_wifi_unset_appie_internal(WIFI_APPIE_PROBEREQ); + esp_wifi_set_appie_internal(WIFI_APPIE_PROBEREQ, ie, SCAN_IE_LEN - len, 0); + os_free(ie); #undef SCAN_IE_LEN #endif /* CONFIG_IEEE80211KV */ } @@ -781,155 +786,155 @@ void esp_set_scan_ie(void) #ifdef CONFIG_IEEE80211R static size_t add_mdie(uint8_t *bssid, uint8_t *ie, size_t len) { - size_t mdie_len = 0; - struct wpa_sm *sm = &gWpaSm; + size_t mdie_len = 0; + struct wpa_sm *sm = &gWpaSm; - /* Return if MBO IE is not enabled in driver */ - if (!esp_wifi_is_ft_enabled_internal(WIFI_IF_STA)) { - return 0; - } + /* Return if MBO IE is not enabled in driver */ + if (!esp_wifi_is_ft_enabled_internal(WIFI_IF_STA)) { + return 0; + } - struct wpa_bss *bss = wpa_bss_get_bssid(&g_wpa_supp, bssid); - if (bss && wpa_key_mgmt_ft(sm->key_mgmt)) { - const u8 *mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN); + struct wpa_bss *bss = wpa_bss_get_bssid(&g_wpa_supp, bssid); + if (bss && wpa_key_mgmt_ft(sm->key_mgmt)) { + const u8 *mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN); - if (mdie && mdie[1] >= MOBILITY_DOMAIN_ID_LEN) { - const u8 *md = mdie + 2; - const u8 *wpa_md = wpa_sm_get_ft_md(sm); + if (mdie && mdie[1] >= MOBILITY_DOMAIN_ID_LEN) { + const u8 *md = mdie + 2; + const u8 *wpa_md = wpa_sm_get_ft_md(sm); - if (os_memcmp(md, wpa_md, - MOBILITY_DOMAIN_ID_LEN) == 0) { - /* Add mobility domain IE */ - mdie_len = wpa_ft_add_mdie( - sm, ie, - len, mdie); - } - } - } + if (os_memcmp(md, wpa_md, + MOBILITY_DOMAIN_ID_LEN) == 0) { + /* Add mobility domain IE */ + mdie_len = wpa_ft_add_mdie( + sm, ie, + len, mdie); + } + } + } - return mdie_len; + return mdie_len; } #endif /* CONFIG_IEEE80211R */ - #ifdef CONFIG_IEEE80211R int wpa_sm_update_ft_ies(struct wpa_sm *sm, const u8 *md, - const u8 *ies, size_t ies_len, bool auth_ie) + const u8 *ies, size_t ies_len, bool auth_ie) { - wpa_printf(MSG_INFO, "Updating FT IEs (len=%d)", ies_len); - if (os_memcmp(sm->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN) != 0) { - return 0; - } - /* Update auth IEs to be used in FT association */ - if (auth_ie) { - esp_wifi_set_appie_internal(WIFI_APPIE_RAM_STA_AUTH, (u8 *)ies, ies_len, 0); - } else { - esp_set_assoc_ie(sm->bssid, ies, ies_len, false); - } - wpa_printf(MSG_INFO, "Updated FT IEs (len=%d) auth_ie=%d", ies_len, auth_ie); + wpa_printf(MSG_INFO, "Updating FT IEs (len=%d)", ies_len); + if (os_memcmp(sm->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN) != 0) { + return 0; + } + /* Update auth IEs to be used in FT association */ + if (auth_ie) { + esp_wifi_set_appie_internal(WIFI_APPIE_RAM_STA_AUTH, (u8 *)ies, ies_len, 0); + } else { + esp_set_assoc_ie(sm->bssid, ies, ies_len, false); + } + wpa_printf(MSG_INFO, "Updated FT IEs (len=%d) auth_ie=%d", ies_len, auth_ie); - return 0; + return 0; } #endif /* CONFIG_IEEE80211R */ void esp_get_tx_power(uint8_t *tx_power) { #define DEFAULT_MAX_TX_POWER 19 /* max tx power is 19.5 dbm */ - s8 power; - /* esp sends management frames at max tx power configured */ - int ret = esp_wifi_get_max_tx_power(&power); - if (ret != 0) { - wpa_printf(MSG_ERROR, "failed to get tx power"); - *tx_power = DEFAULT_MAX_TX_POWER; - return; - } - *tx_power = power/4; + s8 power; + /* esp sends management frames at max tx power configured */ + int ret = esp_wifi_get_max_tx_power(&power); + if (ret != 0) { + wpa_printf(MSG_ERROR, "failed to get tx power"); + *tx_power = DEFAULT_MAX_TX_POWER; + return; + } + *tx_power = power / 4; #undef DEFAULT_MAX_TX_POWER } int wpa_drv_send_action(struct wpa_supplicant *wpa_s, - unsigned int channel, - unsigned int wait, - const u8 *data, size_t data_len, - int no_cck) + unsigned int channel, + unsigned int wait, + const u8 *data, size_t data_len, + int no_cck) { - int ret = 0; - wifi_mgmt_frm_req_t *req = os_zalloc(sizeof(*req) + data_len);; - if (!req) - return -1; + int ret = 0; + wifi_mgmt_frm_req_t *req = os_zalloc(sizeof(*req) + data_len);; + if (!req) { + return -1; + } - if (!wpa_s->current_bss) { - wpa_printf(MSG_ERROR, "STA not associated, return"); - ret = -1; - goto cleanup; - } + if (!wpa_s->current_bss) { + wpa_printf(MSG_ERROR, "STA not associated, return"); + ret = -1; + goto cleanup; + } - req->ifx = WIFI_IF_STA; - req->subtype = WLAN_FC_STYPE_ACTION; - req->data_len = data_len; - os_memcpy(req->data, data, req->data_len); + req->ifx = WIFI_IF_STA; + req->subtype = WLAN_FC_STYPE_ACTION; + req->data_len = data_len; + os_memcpy(req->data, data, req->data_len); - if (esp_wifi_send_mgmt_frm_internal(req) != 0) { - wpa_printf(MSG_ERROR, "action frame sending failed"); - ret = -1; - goto cleanup; - } - wpa_printf(MSG_INFO, "action frame sent"); + if (esp_wifi_send_mgmt_frm_internal(req) != 0) { + wpa_printf(MSG_ERROR, "action frame sending failed"); + ret = -1; + goto cleanup; + } + wpa_printf(MSG_INFO, "action frame sent"); cleanup: - os_free(req); - return ret; + os_free(req); + return ret; } #ifdef CONFIG_SUPPLICANT_TASK int esp_supplicant_post_evt(uint32_t evt_id, uint32_t data) { - supplicant_event_t *evt = os_zalloc(sizeof(supplicant_event_t)); - if (!evt) { - wpa_printf(MSG_ERROR, "Failed to allocated memory"); - return -1; - } - evt->id = evt_id; - evt->data = data; + supplicant_event_t *evt = os_zalloc(sizeof(supplicant_event_t)); + if (!evt) { + wpa_printf(MSG_ERROR, "Failed to allocated memory"); + return -1; + } + evt->id = evt_id; + evt->data = data; - /* Make sure lock exists before taking it */ - SUPPLICANT_API_LOCK(); + /* Make sure lock exists before taking it */ + SUPPLICANT_API_LOCK(); - /* Make sure no event can be sent when deletion event is sent or task not initialized */ - if (!s_supplicant_task_init_done) { - SUPPLICANT_API_UNLOCK(); - os_free(evt); - return -1; - } + /* Make sure no event can be sent when deletion event is sent or task not initialized */ + if (!s_supplicant_task_init_done) { + SUPPLICANT_API_UNLOCK(); + os_free(evt); + return -1; + } - if (os_queue_send(s_supplicant_evt_queue, &evt, os_task_ms_to_tick(10)) != TRUE) { - SUPPLICANT_API_UNLOCK(); - os_free(evt); - return -1; - } - if (evt_id == SIG_SUPPLICANT_DEL_TASK) { - s_supplicant_task_init_done = false; - } - SUPPLICANT_API_UNLOCK(); - return 0; + if (os_queue_send(s_supplicant_evt_queue, &evt, os_task_ms_to_tick(10)) != TRUE) { + SUPPLICANT_API_UNLOCK(); + os_free(evt); + return -1; + } + if (evt_id == SIG_SUPPLICANT_DEL_TASK) { + s_supplicant_task_init_done = false; + } + SUPPLICANT_API_UNLOCK(); + return 0; } #endif /* CONFIG_SUPPLICANT_TASK */ #else /* defined(CONFIG_IEEE80211KV) || defined(CONFIG_IEEE80211R) */ int esp_rrm_send_neighbor_report_request(void) { - return -1; + return -1; } int esp_rrm_send_neighbor_rep_request(neighbor_rep_request_cb cb, - void *cb_ctx) + void *cb_ctx) { - return -1; + return -1; } int esp_wnm_send_bss_transition_mgmt_query(enum btm_query_reason query_reason, - const char *btm_candidates, - int cand_list) + const char *btm_candidates, + int cand_list) { - return -1; + return -1; } void esp_set_scan_ie(void) { } @@ -939,44 +944,44 @@ void esp_set_scan_ie(void) { } void esp_set_assoc_ie(uint8_t *bssid, const u8 *ies, size_t ies_len, bool mdie) { #define ASSOC_IE_LEN 128 - uint8_t *ie, *pos; - size_t len = ASSOC_IE_LEN; + uint8_t *ie, *pos; + size_t len = ASSOC_IE_LEN; #if defined(CONFIG_IEEE80211KV) || defined(CONFIG_IEEE80211R) size_t ie_len; #endif /* defined(CONFIG_IEEE80211KV) || defined(CONFIG_IEEE80211R) */ - ie = os_malloc(ASSOC_IE_LEN + ies_len); - if (!ie) { - wpa_printf(MSG_ERROR, "failed to allocate ie"); - return; - } - pos = ie; + ie = os_malloc(ASSOC_IE_LEN + ies_len); + if (!ie) { + wpa_printf(MSG_ERROR, "failed to allocate ie"); + return; + } + pos = ie; #ifdef CONFIG_IEEE80211KV - ie_len = get_rm_enabled_ie(pos, len); - pos += ie_len; - len -= ie_len; + ie_len = get_rm_enabled_ie(pos, len); + pos += ie_len; + len -= ie_len; #ifdef CONFIG_MBO - ie_len = get_operating_class_ie(pos, len); - pos += ie_len; - len -= ie_len; - ie_len = get_mbo_oce_assoc_ie(pos, len); - pos += ie_len; - len -= ie_len; + ie_len = get_operating_class_ie(pos, len); + pos += ie_len; + len -= ie_len; + ie_len = get_mbo_oce_assoc_ie(pos, len); + pos += ie_len; + len -= ie_len; #endif /* CONFIG_MBO */ #endif /* CONFIG_IEEE80211KV */ #ifdef CONFIG_IEEE80211R - if (mdie) { - ie_len = add_mdie(bssid, pos, len); - pos += ie_len; - len -= ie_len; - } + if (mdie) { + ie_len = add_mdie(bssid, pos, len); + pos += ie_len; + len -= ie_len; + } #endif /* CONFIG_IEEE80211R */ - if (ies_len) { - os_memcpy(pos, ies, ies_len); - pos += ies_len; - len -= ies_len; - } - esp_wifi_set_appie_internal(WIFI_APPIE_ASSOC_REQ, ie, ASSOC_IE_LEN - len, 0); - os_free(ie); + if (ies_len) { + os_memcpy(pos, ies, ies_len); + pos += ies_len; + len -= ies_len; + } + esp_wifi_set_appie_internal(WIFI_APPIE_ASSOC_REQ, ie, ASSOC_IE_LEN - len, 0); + os_free(ie); #undef ASSOC_IE_LEN } #else diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_common_i.h b/components/wpa_supplicant/esp_supplicant/src/esp_common_i.h index 3f4a439bbb..8a4857b139 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_common_i.h +++ b/components/wpa_supplicant/esp_supplicant/src/esp_common_i.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -14,11 +14,11 @@ extern struct wpa_supplicant g_wpa_supp; #ifdef CONFIG_IEEE80211KV struct ieee_mgmt_frame { - u8 sender[ETH_ALEN]; - u8 channel; - int8_t rssi; - size_t len; - u8 payload[0]; + u8 sender[ETH_ALEN]; + u8 channel; + int8_t rssi; + size_t len; + u8 payload[0]; }; int esp_supplicant_post_evt(uint32_t evt_id, uint32_t data); @@ -29,10 +29,10 @@ typedef struct { } supplicant_event_t; enum SIG_SUPPLICANT { - SIG_SUPPLICANT_RX_ACTION, - SIG_SUPPLICANT_SCAN_DONE, - SIG_SUPPLICANT_DEL_TASK, - SIG_SUPPLICANT_MAX, + SIG_SUPPLICANT_RX_ACTION, + SIG_SUPPLICANT_SCAN_DONE, + SIG_SUPPLICANT_DEL_TASK, + SIG_SUPPLICANT_MAX, }; void esp_get_tx_power(uint8_t *tx_power); diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_dpp.c b/components/wpa_supplicant/esp_supplicant/src/esp_dpp.c index 426558611b..9279f340dc 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_dpp.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_dpp.c @@ -64,14 +64,14 @@ esp_err_t esp_dpp_post_evt(uint32_t evt_id, uint32_t data) if (evt_id != SIG_DPP_DEL_TASK) { DPP_API_UNLOCK(); } - wpa_printf(MSG_DEBUG,"DPP: Sent event %d to DPP task", evt_id); + wpa_printf(MSG_DEBUG, "DPP: Sent event %d to DPP task", evt_id); return ret; end: if (evt) { os_free(evt); } - wpa_printf(MSG_ERROR,"DPP: Failed to send event %d to DPP task", evt_id); + wpa_printf(MSG_ERROR, "DPP: Failed to send event %d to DPP task", evt_id); return ret; } @@ -79,15 +79,15 @@ static uint8_t esp_dpp_deinit_auth(void) { esp_err_t ret = esp_dpp_post_evt(SIG_DPP_DEINIT_AUTH, 0); if (ESP_OK != ret) { - wpa_printf(MSG_ERROR, "Failed to post DPP auth deinit to DPP Task(status=%d)", ret); - return ret; - } - return ESP_OK; + wpa_printf(MSG_ERROR, "Failed to post DPP auth deinit to DPP Task(status=%d)", ret); + return ret; + } + return ESP_OK; } static void esp_dpp_call_cb(esp_supp_dpp_event_t evt, void *data) { - if ( evt == ESP_SUPP_DPP_FAIL && s_dpp_ctx.dpp_auth) { + if (evt == ESP_SUPP_DPP_FAIL && s_dpp_ctx.dpp_auth) { esp_dpp_deinit_auth(); } s_dpp_ctx.dpp_event_cb(evt, data); @@ -95,16 +95,17 @@ static void esp_dpp_call_cb(esp_supp_dpp_event_t evt, void *data) static void esp_dpp_auth_conf_wait_timeout(void *eloop_ctx, void *timeout_ctx) { - if (!s_dpp_ctx.dpp_auth || !s_dpp_ctx.dpp_auth->waiting_auth_conf) + if (!s_dpp_ctx.dpp_auth || !s_dpp_ctx.dpp_auth->waiting_auth_conf) { return; + } wpa_printf(MSG_DEBUG, - "DPP: Terminate authentication exchange due to Auth Confirm timeout"); + "DPP: Terminate authentication exchange due to Auth Confirm timeout"); esp_dpp_call_cb(ESP_SUPP_DPP_FAIL, (void *)ESP_ERR_DPP_AUTH_TIMEOUT); } esp_err_t esp_dpp_send_action_frame(uint8_t *dest_mac, const uint8_t *buf, uint32_t len, - uint8_t channel, uint32_t wait_time_ms) + uint8_t channel, uint32_t wait_time_ms) { wifi_action_tx_req_t *req = os_zalloc(sizeof(*req) + len);; if (!req) { @@ -179,10 +180,10 @@ static void esp_dpp_rx_auth_req(struct action_rx_param *rx_param, uint8_t *dpp_d os_memcpy(s_dpp_ctx.dpp_auth->peer_mac_addr, rx_param->sa, ETH_ALEN); esp_dpp_send_action_frame(rx_param->sa, wpabuf_head(s_dpp_ctx.dpp_auth->resp_msg), - wpabuf_len(s_dpp_ctx.dpp_auth->resp_msg), - rx_param->channel, OFFCHAN_TX_WAIT_TIME); - eloop_cancel_timeout(esp_dpp_auth_conf_wait_timeout, NULL,NULL); - eloop_register_timeout(ESP_DPP_AUTH_TIMEOUT_SECS, 0, esp_dpp_auth_conf_wait_timeout,NULL, NULL); + wpabuf_len(s_dpp_ctx.dpp_auth->resp_msg), + rx_param->channel, OFFCHAN_TX_WAIT_TIME); + eloop_cancel_timeout(esp_dpp_auth_conf_wait_timeout, NULL, NULL); + eloop_register_timeout(ESP_DPP_AUTH_TIMEOUT_SECS, 0, esp_dpp_auth_conf_wait_timeout, NULL, NULL); return; fail: @@ -206,7 +207,7 @@ static void gas_query_req_tx(struct dpp_authentication *auth) MAC2STR(auth->peer_mac_addr), auth->curr_chan); esp_dpp_send_action_frame(auth->peer_mac_addr, wpabuf_head(buf), wpabuf_len(buf), - auth->curr_chan, OFFCHAN_TX_WAIT_TIME); + auth->curr_chan, OFFCHAN_TX_WAIT_TIME); } static int esp_dpp_handle_config_obj(struct dpp_authentication *auth, @@ -355,16 +356,17 @@ static esp_err_t esp_dpp_rx_peer_disc_resp(struct action_rx_param *rx_param) } res = dpp_peer_intro(&intro, auth->conf_obj[i].connector, - wpabuf_head(auth->net_access_key), - wpabuf_len(auth->net_access_key), - wpabuf_head(auth->conf_obj[i].c_sign_key), - wpabuf_len(auth->conf_obj[i].c_sign_key), - connector, connector_len, &expiry); + wpabuf_head(auth->net_access_key), + wpabuf_len(auth->net_access_key), + wpabuf_head(auth->conf_obj[i].c_sign_key), + wpabuf_len(auth->conf_obj[i].c_sign_key), + connector, connector_len, &expiry); if (res == DPP_STATUS_OK) { - entry = os_zalloc(sizeof(*entry)); - if (!entry) + entry = os_zalloc(sizeof(*entry)); + if (!entry) { goto fail; + } os_memcpy(entry->aa, rx_param->sa, ETH_ALEN); os_memcpy(entry->pmkid, intro.pmkid, PMKID_LEN); os_memcpy(entry->pmk, intro.pmk, intro.pmk_len); @@ -385,8 +387,8 @@ static esp_err_t esp_dpp_rx_peer_disc_resp(struct action_rx_param *rx_param) pmksa_cache_add_entry(sm->pmksa, entry); wpa_printf(MSG_INFO, "peer=" MACSTR " status=%u", MAC2STR(rx_param->sa), status[0]); - break; - } + break; + } } if (res != DPP_STATUS_OK) { @@ -395,13 +397,13 @@ static esp_err_t esp_dpp_rx_peer_disc_resp(struct action_rx_param *rx_param) } wpa_printf(MSG_DEBUG, - "DPP: Try connection after successful network introduction"); + "DPP: Try connection after successful network introduction"); dpp_connect(rx_param->sa, true); return ESP_OK; fail: os_memset(&intro, 0, sizeof(intro)); if (entry != NULL) { - os_free(entry); + os_free(entry); } return ESP_FAIL; } @@ -469,8 +471,9 @@ static esp_err_t esp_dpp_rx_action(struct action_rx_param *rx_param) int ret = ESP_OK; - if (!rx_param) + if (!rx_param) { return ESP_ERR_INVALID_ARG; + } if (rx_param->action_frm->category == WLAN_ACTION_PUBLIC) { struct ieee80211_public_action *public_action = @@ -511,7 +514,7 @@ static esp_err_t esp_dpp_rx_action(struct action_rx_param *rx_param) return ret; } -static void esp_dpp_task(void *pvParameters ) +static void esp_dpp_task(void *pvParameters) { dpp_event_t *evt; bool task_del = false; @@ -572,7 +575,7 @@ static void esp_dpp_task(void *pvParameters ) } channel = p->chan_list[counter++ % p->num_chan]; ret = esp_wifi_remain_on_channel(WIFI_IF_STA, WIFI_ROC_REQ, channel, - BOOTSTRAP_ROC_WAIT_TIME, s_action_rx_cb); + BOOTSTRAP_ROC_WAIT_TIME, s_action_rx_cb); if (ret != ESP_OK) { wpa_printf(MSG_ERROR, "Failed ROC. error : 0x%x", ret); break; @@ -773,10 +776,10 @@ esp_supp_dpp_bootstrap_gen(const char *chan_list, enum dpp_bootstrap_type type, } os_snprintf(command, 1200, "type=qrcode mac=" MACSTR "%s%s%s%s%s", - MAC2STR(params->mac), uri_chan_list, - key ? "key=" : "", key ? key : "", - params->info_len ? " info=" : "", - params->info_len ? params->info : ""); + MAC2STR(params->mac), uri_chan_list, + key ? "key=" : "", key ? key : "", + params->info_len ? " info=" : "", + params->info_len ? params->info : ""); ret = esp_dpp_post_evt(SIG_DPP_BOOTSTRAP_GEN, (u32)command); if (ret != ESP_OK) { @@ -910,17 +913,17 @@ esp_err_t esp_dpp_start_net_intro_protocol(uint8_t *bssid) struct dpp_authentication *auth = s_dpp_ctx.dpp_auth; struct wpabuf *buf; for (int i = 0; i < auth->num_conf_obj; i++) { - os_memcpy(auth->peer_mac_addr, bssid, ETH_ALEN); - buf = dpp_build_peer_disc_req(auth, &auth->conf_obj[i]); + os_memcpy(auth->peer_mac_addr, bssid, ETH_ALEN); + buf = dpp_build_peer_disc_req(auth, &auth->conf_obj[i]); - if (buf) { + if (buf) { if (esp_dpp_send_action_frame(bssid, wpabuf_head(buf), wpabuf_len(buf), auth->curr_chan, OFFCHAN_TX_WAIT_TIME) != ESP_OK) { wpabuf_free(buf); return ESP_FAIL; - } - } else { - return ESP_ERR_NO_MEM; - } + } + } else { + return ESP_ERR_NO_MEM; + } } return ESP_OK; } @@ -929,9 +932,9 @@ esp_err_t esp_supp_dpp_deinit(void) { esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_ACTION_TX_STATUS, - &offchan_event_handler); + &offchan_event_handler); esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_ROC_DONE, - &offchan_event_handler); + &offchan_event_handler); if (s_dpp_ctx.dpp_global) { if (esp_dpp_post_evt(SIG_DPP_DEL_TASK, 0)) { wpa_printf(MSG_ERROR, "DPP Deinit Failed"); diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_eap_client.c b/components/wpa_supplicant/esp_supplicant/src/esp_eap_client.c index ba30370e7c..269622544f 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_eap_client.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_eap_client.c @@ -165,7 +165,7 @@ static void wpa2_rxq_init(void) static void wpa2_rxq_enqueue(struct wpa2_rx_param *param) { DATA_MUTEX_TAKE(); - STAILQ_INSERT_TAIL(&s_wpa2_rxq,param, bqentry); + STAILQ_INSERT_TAIL(&s_wpa2_rxq, param, bqentry); DATA_MUTEX_GIVE(); } @@ -175,7 +175,7 @@ static struct wpa2_rx_param * wpa2_rxq_dequeue(void) DATA_MUTEX_TAKE(); if ((param = STAILQ_FIRST(&s_wpa2_rxq)) != NULL) { STAILQ_REMOVE_HEAD(&s_wpa2_rxq, bqentry); - STAILQ_NEXT(param,bqentry) = NULL; + STAILQ_NEXT(param, bqentry) = NULL; } DATA_MUTEX_GIVE(); return param; @@ -187,14 +187,14 @@ static void wpa2_rxq_deinit(void) DATA_MUTEX_TAKE(); while ((param = STAILQ_FIRST(&s_wpa2_rxq)) != NULL) { STAILQ_REMOVE_HEAD(&s_wpa2_rxq, bqentry); - STAILQ_NEXT(param,bqentry) = NULL; + STAILQ_NEXT(param, bqentry) = NULL; os_free(param->buf); os_free(param); } DATA_MUTEX_GIVE(); } -void wpa2_task(void *pvParameters ) +void wpa2_task(void *pvParameters) { ETSEvent *e; struct eap_sm *sm = gEapSm; @@ -205,10 +205,10 @@ void wpa2_task(void *pvParameters ) } for (;;) { - if ( TRUE == os_queue_recv(s_wpa2_queue, &e, OS_BLOCK) ) { + if (TRUE == os_queue_recv(s_wpa2_queue, &e, OS_BLOCK)) { if (e->sig < SIG_WPA2_MAX) { DATA_MUTEX_TAKE(); - if(sm->wpa2_sig_cnt[e->sig]) { + if (sm->wpa2_sig_cnt[e->sig]) { sm->wpa2_sig_cnt[e->sig]--; } else { wpa_printf(MSG_ERROR, "wpa2_task: invalid sig cnt, sig=%" PRId32 " cnt=%d", e->sig, sm->wpa2_sig_cnt[e->sig]); @@ -225,7 +225,7 @@ void wpa2_task(void *pvParameters ) case SIG_WPA2_RX: { struct wpa2_rx_param *param = NULL; - while ((param = wpa2_rxq_dequeue()) != NULL){ + while ((param = wpa2_rxq_dequeue()) != NULL) { eap_sm_rx_eapol_internal(param->sa, param->buf, param->len, param->bssid); os_free(param->buf); os_free(param); @@ -320,8 +320,8 @@ int eap_sm_send_eapol(struct eap_sm *sm, struct wpabuf *resp) } outbuf = wpa_alloc_eapol(sm, IEEE802_1X_TYPE_EAP_PACKET, - wpabuf_head_u8(resp), wpabuf_len(resp), - &outlen, NULL); + wpabuf_head_u8(resp), wpabuf_len(resp), + &outlen, NULL); if (!outbuf) { return ESP_ERR_NO_MEM; } @@ -357,7 +357,7 @@ int eap_sm_process_request(struct eap_sm *sm, struct wpabuf *reqData) } if (ehdr->identifier == sm->current_identifier && - sm->lastRespData != NULL) { + sm->lastRespData != NULL) { /*Retransmit*/ resp = sm->lastRespData; goto send_resp; @@ -398,7 +398,7 @@ int eap_sm_process_request(struct eap_sm *sm, struct wpabuf *reqData) if (!eap_sm_allowMethod(sm, reqVendor, reqVendorMethod)) { wpa_printf(MSG_DEBUG, "EAP: vendor %" PRIu32 " method %" PRIu32 " not allowed", - reqVendor, reqVendorMethod); + reqVendor, reqVendorMethod); wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD "vendor=%" PRIu32 " method=%" PRIu32 " -> NAK", reqVendor, reqVendorMethod); @@ -502,20 +502,20 @@ static int wpa2_ent_rx_eapol(u8 *src_addr, u8 *buf, u32 len, uint8_t *bssid) hdr = (struct ieee802_1x_hdr *) buf; switch (hdr->type) { - case IEEE802_1X_TYPE_EAPOL_START: - case IEEE802_1X_TYPE_EAP_PACKET: - case IEEE802_1X_TYPE_EAPOL_LOGOFF: - ret = eap_sm_rx_eapol(src_addr, buf, len, bssid); - break; - case IEEE802_1X_TYPE_EAPOL_KEY: - ret = wpa_sm_rx_eapol(src_addr, buf, len); - break; - default: - wpa_printf(MSG_ERROR, "Unknown EAPOL packet type - %d", hdr->type); - break; + case IEEE802_1X_TYPE_EAPOL_START: + case IEEE802_1X_TYPE_EAP_PACKET: + case IEEE802_1X_TYPE_EAPOL_LOGOFF: + ret = eap_sm_rx_eapol(src_addr, buf, len, bssid); + break; + case IEEE802_1X_TYPE_EAPOL_KEY: + ret = wpa_sm_rx_eapol(src_addr, buf, len); + break; + default: + wpa_printf(MSG_ERROR, "Unknown EAPOL packet type - %d", hdr->type); + break; } - return ret; + return ret; } static int eap_sm_rx_eapol_internal(u8 *src_addr, u8 *buf, u32 len, uint8_t *bssid) @@ -543,7 +543,7 @@ static int eap_sm_rx_eapol_internal(u8 *src_addr, u8 *buf, u32 len, uint8_t *bss tmp = buf; hdr = (struct ieee802_1x_hdr *) tmp; - ehdr = (struct eap_hdr *) (hdr + 1); + ehdr = (struct eap_hdr *)(hdr + 1); plen = be_to_host16(hdr->length); data_len = plen + sizeof(*hdr); @@ -578,9 +578,9 @@ static int eap_sm_rx_eapol_internal(u8 *src_addr, u8 *buf, u32 len, uint8_t *bss case EAP_CODE_REQUEST: /* Handle EAP-reauthentication case */ if (sm->finish_state == WPA2_ENT_EAP_STATE_SUCCESS) { - wpa_printf(MSG_INFO, "EAP Re-authentication in progress"); - wpa2_set_eap_state(WPA2_ENT_EAP_STATE_IN_PROGRESS); - } + wpa_printf(MSG_INFO, "EAP Re-authentication in progress"); + wpa2_set_eap_state(WPA2_ENT_EAP_STATE_IN_PROGRESS); + } req = wpabuf_alloc_copy((u8 *)ehdr, len - sizeof(*hdr)); ret = eap_sm_process_request(sm, req); @@ -596,7 +596,7 @@ static int eap_sm_rx_eapol_internal(u8 *src_addr, u8 *buf, u32 len, uint8_t *bss wpa_printf(MSG_INFO, ">>>>>EAP FINISH"); ret = WPA2_ENT_EAP_STATE_SUCCESS; wpa2_set_eap_state(WPA2_ENT_EAP_STATE_SUCCESS); - eap_deinit_prev_method(sm, "EAP Success"); + eap_deinit_prev_method(sm, "EAP Success"); } else { wpa_printf(MSG_INFO, ">>>>>EAP FAILED, receive EAP_SUCCESS but pmk is empty, potential attack!"); ret = WPA2_ENT_EAP_STATE_FAIL; @@ -638,7 +638,7 @@ static int wpa2_start_eapol_internal(void) if (wpa_sta_cur_pmksa_matches_akm()) { wpa_printf(MSG_DEBUG, - "RSN: PMKSA caching - do not send EAPOL-Start"); + "RSN: PMKSA caching - do not send EAPOL-Start"); return ESP_FAIL; } @@ -902,8 +902,8 @@ esp_err_t esp_wifi_sta_enterprise_disable(void) } esp_err_t esp_eap_client_set_certificate_and_key(const unsigned char *client_cert, int client_cert_len, - const unsigned char *private_key, int private_key_len, - const unsigned char *private_key_passwd, int private_key_passwd_len) + const unsigned char *private_key, int private_key_len, + const unsigned char *private_key_passwd, int private_key_passwd_len) { if (client_cert && client_cert_len > 0) { g_wpa_client_cert = client_cert; @@ -1098,24 +1098,24 @@ esp_err_t esp_eap_client_get_disable_time_check(bool *disable) esp_err_t esp_eap_client_set_ttls_phase2_method(esp_eap_ttls_phase2_types type) { switch (type) { - case ESP_EAP_TTLS_PHASE2_EAP: - g_wpa_ttls_phase2_type = "auth=EAP"; - break; - case ESP_EAP_TTLS_PHASE2_MSCHAPV2: - g_wpa_ttls_phase2_type = "auth=MSCHAPV2"; - break; - case ESP_EAP_TTLS_PHASE2_MSCHAP: - g_wpa_ttls_phase2_type = "auth=MSCHAP"; - break; - case ESP_EAP_TTLS_PHASE2_PAP: - g_wpa_ttls_phase2_type = "auth=PAP"; - break; - case ESP_EAP_TTLS_PHASE2_CHAP: - g_wpa_ttls_phase2_type = "auth=CHAP"; - break; - default: - g_wpa_ttls_phase2_type = "auth=MSCHAPV2"; - break; + case ESP_EAP_TTLS_PHASE2_EAP: + g_wpa_ttls_phase2_type = "auth=EAP"; + break; + case ESP_EAP_TTLS_PHASE2_MSCHAPV2: + g_wpa_ttls_phase2_type = "auth=MSCHAPV2"; + break; + case ESP_EAP_TTLS_PHASE2_MSCHAP: + g_wpa_ttls_phase2_type = "auth=MSCHAP"; + break; + case ESP_EAP_TTLS_PHASE2_PAP: + g_wpa_ttls_phase2_type = "auth=PAP"; + break; + case ESP_EAP_TTLS_PHASE2_CHAP: + g_wpa_ttls_phase2_type = "auth=CHAP"; + break; + default: + g_wpa_ttls_phase2_type = "auth=MSCHAPV2"; + break; } return ESP_OK; } @@ -1164,8 +1164,8 @@ esp_err_t esp_eap_client_set_fast_params(esp_eap_fast_config config) } if (config.fast_max_pac_list_len && config.fast_max_pac_list_len < 100) { os_snprintf((char *) &config_for_supplicant + strlen(config_for_supplicant), - PHASE1_PARAM_STRING_LEN - strlen(config_for_supplicant), - "fast_max_pac_list_len=%d ", config.fast_max_pac_list_len); + PHASE1_PARAM_STRING_LEN - strlen(config_for_supplicant), + "fast_max_pac_list_len=%d ", config.fast_max_pac_list_len); } else if (config.fast_max_pac_list_len >= 100) { return ESP_ERR_INVALID_ARG; } diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_hostap.c b/components/wpa_supplicant/esp_supplicant/src/esp_hostap.c index 3beb7590da..4469e6d8e4 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_hostap.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_hostap.c @@ -82,8 +82,7 @@ void *hostap_init(void) pairwise_cipher = esp_wifi_ap_get_prof_pairwise_cipher_internal(); #ifdef CONFIG_IEEE80211W - if((auth_conf->wpa & WPA_PROTO_RSN) == WPA_PROTO_RSN) - { + if ((auth_conf->wpa & WPA_PROTO_RSN) == WPA_PROTO_RSN) { esp_wifi_get_pmf_config_internal(&pmf_cfg, WIFI_IF_AP); if (pmf_cfg.required) { pairwise_cipher = WIFI_CIPHER_TYPE_CCMP; @@ -147,7 +146,7 @@ void *hostap_init(void) #ifdef CONFIG_SAE if (authmode == WIFI_AUTH_WPA3_PSK || - authmode == WIFI_AUTH_WPA2_WPA3_PSK) { + authmode == WIFI_AUTH_WPA2_WPA3_PSK) { if (wpa3_hostap_auth_init(hapd) != 0) { goto fail; } @@ -191,7 +190,7 @@ void hostapd_cleanup(struct hostapd_data *hapd) if (hapd == NULL) { return; } - if(hapd->wpa_auth) { + if (hapd->wpa_auth) { wpa_deinit(hapd->wpa_auth); hapd->wpa_auth = NULL; } @@ -209,7 +208,7 @@ void hostapd_cleanup(struct hostapd_data *hapd) if (dl_list_empty(&hapd->sae_commit_queue)) { dl_list_for_each_safe(q, tmp, &hapd->sae_commit_queue, - struct hostapd_sae_commit_queue, list) { + struct hostapd_sae_commit_queue, list) { dl_list_del(&q->list); os_free(q); } @@ -217,8 +216,8 @@ void hostapd_cleanup(struct hostapd_data *hapd) #endif /* CONFIG_SAE */ #ifdef CONFIG_WPS_REGISTRAR - if (esp_wifi_get_wps_type_internal () != WPS_TYPE_DISABLE || - esp_wifi_get_wps_status_internal() != WPS_STATUS_DISABLE) { + if (esp_wifi_get_wps_type_internal() != WPS_TYPE_DISABLE || + esp_wifi_get_wps_status_internal() != WPS_STATUS_DISABLE) { esp_wifi_ap_wps_disable(); } #endif /* CONFIG_WPS_REGISTRAR */ @@ -227,7 +226,6 @@ void hostapd_cleanup(struct hostapd_data *hapd) } - bool hostap_deinit(void *data) { struct hostapd_data *hapd = (struct hostapd_data *)data; @@ -242,7 +240,7 @@ bool hostap_deinit(void *data) wpa3_hostap_auth_deinit(); /* Wait till lock is released by wpa3 task */ if (g_wpa3_hostap_auth_api_lock && - WPA3_HOSTAP_AUTH_API_LOCK() == pdTRUE) { + WPA3_HOSTAP_AUTH_API_LOCK() == pdTRUE) { WPA3_HOSTAP_AUTH_API_UNLOCK(); } #endif /* CONFIG_SAE */ @@ -263,8 +261,8 @@ int esp_wifi_build_rsnxe(struct hostapd_data *hapd, u8 *eid, size_t len) } if (wpa_key_mgmt_sae(hapd->wpa_auth->conf.wpa_key_mgmt) && - (hapd->conf->sae_pwe == SAE_PWE_HASH_TO_ELEMENT - || hapd->conf->sae_pwe == SAE_PWE_BOTH)) { + (hapd->conf->sae_pwe == SAE_PWE_HASH_TO_ELEMENT + || hapd->conf->sae_pwe == SAE_PWE_BOTH)) { capab |= BIT(WLAN_RSNX_CAPAB_SAE_H2E); } @@ -282,7 +280,7 @@ int esp_wifi_build_rsnxe(struct hostapd_data *hapd, u8 *eid, size_t len) } u16 esp_send_assoc_resp(struct hostapd_data *hapd, const u8 *addr, - u16 status_code, bool omit_rsnxe, int subtype) + u16 status_code, bool omit_rsnxe, int subtype) { #define ASSOC_RESP_LENGTH 20 u8 buf[ASSOC_RESP_LENGTH]; diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_hostap.h b/components/wpa_supplicant/esp_supplicant/src/esp_hostap.h index 7e5d409997..9f193b3144 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_hostap.h +++ b/components/wpa_supplicant/esp_supplicant/src/esp_hostap.h @@ -16,11 +16,11 @@ extern "C" { void *hostap_init(void); bool hostap_deinit(void *data); u16 esp_send_assoc_resp(struct hostapd_data *data, const u8 *addr, - u16 status_code, bool omit_rsnxe, int subtype); + u16 status_code, bool omit_rsnxe, int subtype); int esp_send_sae_auth_reply(struct hostapd_data *hapd, - const u8 *dst, const u8 *bssid, - u16 auth_alg, u16 auth_transaction, u16 resp, - const u8 *ies, size_t ies_len); + const u8 *dst, const u8 *bssid, + u16 auth_alg, u16 auth_transaction, u16 resp, + const u8 *ies, size_t ies_len); #endif #ifdef __cplusplus diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_hostpad_wps.c b/components/wpa_supplicant/esp_supplicant/src/esp_hostpad_wps.c index 869deccc6e..8ee6678922 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_hostpad_wps.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_hostpad_wps.c @@ -186,7 +186,6 @@ static int wifi_ap_wps_enable_internal(const esp_wps_config_t *config) return ESP_FAIL; } - if (wps_set_type(config->wps_type) != ESP_OK) { goto _err; } @@ -247,7 +246,6 @@ static int wifi_ap_wps_disable_internal(void) goto _err; } - s_wps_enabled = false; return ESP_OK; @@ -275,7 +273,6 @@ static int wifi_ap_wps_start_internal(const unsigned char *pin) return ESP_ERR_WIFI_MODE; } - if (!s_wps_enabled) { wpa_printf(MSG_ERROR, "wps start: wps not enabled"); API_MUTEX_GIVE(); @@ -286,13 +283,13 @@ static int wifi_ap_wps_start_internal(const unsigned char *pin) (wps_get_status() != WPS_STATUS_DISABLE && wps_get_status() != WPS_STATUS_SCANNING)) { wpa_printf(MSG_ERROR, "wps start: wps_get_type=%d wps_get_status=%d", - wps_get_type(), wps_get_status()); + wps_get_type(), wps_get_status()); return ESP_ERR_WIFI_WPS_TYPE; } if (esp_wifi_get_user_init_flag_internal() == 0) { wpa_printf(MSG_ERROR, "wps start: esp_wifi_get_user_init_flag_internal=%d", - esp_wifi_get_user_init_flag_internal()); + esp_wifi_get_user_init_flag_internal()); return ESP_ERR_WIFI_STATE; } @@ -332,7 +329,7 @@ static void wps_reg_eloop_handler(void *eloop_ctx, void *user_ctx) enum wps_reg_sig_type *sig = (enum wps_reg_sig_type *) eloop_ctx; wps_ioctl_param_t *param = (wps_ioctl_param_t *) user_ctx; - switch(*sig) { + switch (*sig) { case SIG_WPS_REG_ENABLE: esp_wps_config_t *config = (esp_wps_config_t *)param->arg; ret = wifi_ap_wps_enable_internal(config); diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_scan.c b/components/wpa_supplicant/esp_supplicant/src/esp_scan.c index 92b9c900ad..df04c6f1a6 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_scan.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_scan.c @@ -27,148 +27,149 @@ extern struct wpa_supplicant g_wpa_supp; static void scan_done_event_handler(void *arg, ETS_STATUS status) { - struct wpa_supplicant *wpa_s = &g_wpa_supp; + struct wpa_supplicant *wpa_s = &g_wpa_supp; - /* update last scan time */ - wpa_s->scan_start_tsf = esp_wifi_get_tsf_time(WIFI_IF_STA); - if (wpa_s->scanning) { - wpa_s->type &= ~(1 << WLAN_FC_STYPE_BEACON) & ~(1 << WLAN_FC_STYPE_PROBE_RESP); - esp_wifi_register_mgmt_frame_internal(wpa_s->type, wpa_s->subtype); - } + /* update last scan time */ + wpa_s->scan_start_tsf = esp_wifi_get_tsf_time(WIFI_IF_STA); + if (wpa_s->scanning) { + wpa_s->type &= ~(1 << WLAN_FC_STYPE_BEACON) & ~(1 << WLAN_FC_STYPE_PROBE_RESP); + esp_wifi_register_mgmt_frame_internal(wpa_s->type, wpa_s->subtype); + } #ifdef CONFIG_SUPPLICANT_TASK - if (esp_supplicant_post_evt(SIG_SUPPLICANT_SCAN_DONE, 0) != 0) { - wpa_printf(MSG_ERROR, "Posting of scan done failed!"); - } + if (esp_supplicant_post_evt(SIG_SUPPLICANT_SCAN_DONE, 0) != 0) { + wpa_printf(MSG_ERROR, "Posting of scan done failed!"); + } #else - esp_supplicant_handle_scan_done_evt(); + esp_supplicant_handle_scan_done_evt(); #endif /*CONFIG_SUPPLICANT_TASK*/ } #if defined(CONFIG_IEEE80211KV) static void handle_wnm_scan_done(struct wpa_supplicant *wpa_s) { - struct wpa_bss *bss = wpa_bss_get_next_bss(wpa_s, wpa_s->current_bss); + struct wpa_bss *bss = wpa_bss_get_next_bss(wpa_s, wpa_s->current_bss); - if (wpa_s->wnm_neighbor_report_elements) { - wnm_scan_process(wpa_s, 1); - } else if (wpa_s->wnm_dissoc_timer) { - if (wpa_s->num_bss == 1) { - wpa_printf(MSG_INFO, "not able to find another candidate, do nothing"); - return; - } - /* this is a already matched bss */ - if (bss) { - wnm_bss_tm_connect(wpa_s, bss, NULL, 1); - } - } + if (wpa_s->wnm_neighbor_report_elements) { + wnm_scan_process(wpa_s, 1); + } else if (wpa_s->wnm_dissoc_timer) { + if (wpa_s->num_bss == 1) { + wpa_printf(MSG_INFO, "not able to find another candidate, do nothing"); + return; + } + /* this is a already matched bss */ + if (bss) { + wnm_bss_tm_connect(wpa_s, bss, NULL, 1); + } + } } #endif static void scan_done_cleanup(struct wpa_supplicant *wpa_s) { - wpa_s->scanning = 0; - wpa_s->scan_reason = 0; - /* clean scan list from net80211 */ - esp_wifi_clear_ap_list(); + wpa_s->scanning = 0; + wpa_s->scan_reason = 0; + /* clean scan list from net80211 */ + esp_wifi_clear_ap_list(); } void esp_supplicant_handle_scan_done_evt(void) { - struct wpa_supplicant *wpa_s = &g_wpa_supp; + struct wpa_supplicant *wpa_s = &g_wpa_supp; - wpa_printf(MSG_INFO, "scan done received"); + wpa_printf(MSG_INFO, "scan done received"); #if defined(CONFIG_IEEE80211KV) - /* Check which module started this, call the respective function */ - if (wpa_s->scan_reason == REASON_RRM_BEACON_REPORT) { - wpas_beacon_rep_scan_process(wpa_s, wpa_s->scan_start_tsf); - } else if (wpa_s->scan_reason == REASON_WNM_BSS_TRANS_REQ) { - handle_wnm_scan_done(wpa_s); - } + /* Check which module started this, call the respective function */ + if (wpa_s->scan_reason == REASON_RRM_BEACON_REPORT) { + wpas_beacon_rep_scan_process(wpa_s, wpa_s->scan_start_tsf); + } else if (wpa_s->scan_reason == REASON_WNM_BSS_TRANS_REQ) { + handle_wnm_scan_done(wpa_s); + } #endif - if (wpa_s->scanning) { - scan_done_cleanup(wpa_s); - } - wpa_bss_update_end(wpa_s); + if (wpa_s->scanning) { + scan_done_cleanup(wpa_s); + } + wpa_bss_update_end(wpa_s); #ifndef SCAN_CACHE_SUPPORTED - wpa_bss_flush(wpa_s); + wpa_bss_flush(wpa_s); #endif } void esp_scan_init(struct wpa_supplicant *wpa_s) { - wpa_s->scanning = 0; - wpa_bss_init(wpa_s); - wpa_s->last_scan_res = NULL; - wpa_s->last_scan_res_used = 0; + wpa_s->scanning = 0; + wpa_bss_init(wpa_s); + wpa_s->last_scan_res = NULL; + wpa_s->last_scan_res_used = 0; } void esp_scan_deinit(struct wpa_supplicant *wpa_s) { - wpa_bss_deinit(wpa_s); - os_free(wpa_s->last_scan_res); - wpa_s->last_scan_res = NULL; - wpa_s->last_scan_res_used = 0; + wpa_bss_deinit(wpa_s); + os_free(wpa_s->last_scan_res); + wpa_s->last_scan_res = NULL; + wpa_s->last_scan_res_used = 0; } int esp_handle_beacon_probe(u8 type, u8 *frame, size_t len, u8 *sender, - int8_t rssi, u8 channel, u64 current_tsf) + int8_t rssi, u8 channel, u64 current_tsf) { - struct wpa_supplicant *wpa_s = &g_wpa_supp; - struct os_reltime now; - struct wpa_scan_res *res; - u8 *ptr; + struct wpa_supplicant *wpa_s = &g_wpa_supp; + struct os_reltime now; + struct wpa_scan_res *res; + u8 *ptr; - if (len < 12) { - wpa_printf(MSG_ERROR, "beacon/probe is having short len=%d", len); - return -1; - } + if (len < 12) { + wpa_printf(MSG_ERROR, "beacon/probe is having short len=%d", len); + return -1; + } - res = os_zalloc(sizeof(struct wpa_scan_res) + len - 12); - if (!res) { - wpa_printf(MSG_ERROR, "failed to allocate memory"); - return -1; - } + res = os_zalloc(sizeof(struct wpa_scan_res) + len - 12); + if (!res) { + wpa_printf(MSG_ERROR, "failed to allocate memory"); + return -1; + } - ptr = (u8 *)res; - os_get_time(&now); - os_memcpy(res->bssid, sender, ETH_ALEN); - res->tsf = WPA_GET_LE64(frame); - frame += 8; - len -= 8; + ptr = (u8 *)res; + os_get_time(&now); + os_memcpy(res->bssid, sender, ETH_ALEN); + res->tsf = WPA_GET_LE64(frame); + frame += 8; + len -= 8; - if ((wpa_s->scan_start_tsf == 0) && - wpa_s->current_bss && - (os_memcmp(wpa_s->current_bss, sender, ETH_ALEN) == 0)) { - wpa_s->scan_start_tsf = res->tsf; - os_memcpy(wpa_s->tsf_bssid, sender, ETH_ALEN); - } - res->beacon_int = WPA_GET_LE16(frame); + if ((wpa_s->scan_start_tsf == 0) && + wpa_s->current_bss && + (os_memcmp(wpa_s->current_bss, sender, ETH_ALEN) == 0)) { + wpa_s->scan_start_tsf = res->tsf; + os_memcpy(wpa_s->tsf_bssid, sender, ETH_ALEN); + } + res->beacon_int = WPA_GET_LE16(frame); - frame += 2; - len -= 2; - res->caps = WPA_GET_LE16(frame); - frame += 2; - len -= 2; + frame += 2; + len -= 2; + res->caps = WPA_GET_LE16(frame); + frame += 2; + len -= 2; - res->chan = channel; - res->noise = 0; - res->level = rssi; - os_memcpy(res->tsf_bssid, wpa_s->tsf_bssid, ETH_ALEN); - res->parent_tsf = current_tsf - wpa_s->scan_start_tsf; - res->ie_len = len; + res->chan = channel; + res->noise = 0; + res->level = rssi; + os_memcpy(res->tsf_bssid, wpa_s->tsf_bssid, ETH_ALEN); + res->parent_tsf = current_tsf - wpa_s->scan_start_tsf; + res->ie_len = len; - ptr += sizeof(struct wpa_scan_res); + ptr += sizeof(struct wpa_scan_res); - /* update rest of the frame */ - os_memcpy(ptr, frame, len); - wpa_bss_update_scan_res(wpa_s, res, &now); - os_get_reltime(&wpa_s->last_scan); - os_free(res); + /* update rest of the frame */ + os_memcpy(ptr, frame, len); + wpa_bss_update_scan_res(wpa_s, res, &now); + os_get_reltime(&wpa_s->last_scan); + os_free(res); - return 0; + return 0; } #ifdef CONFIG_WNM -void get_scan_channel_bitmap(struct wpa_supplicant *wpa_s, wifi_scan_config_t *params) { +void get_scan_channel_bitmap(struct wpa_supplicant *wpa_s, wifi_scan_config_t *params) +{ if (!wpa_s->wnm_num_neighbor_report) { wpa_printf(MSG_DEBUG, "No Neighbor Report to gather scan channel list"); return; @@ -183,96 +184,98 @@ void get_scan_channel_bitmap(struct wpa_supplicant *wpa_s, wifi_scan_config_t *p } #endif /*CONFIG_WNM*/ static int issue_scan(struct wpa_supplicant *wpa_s, - struct wpa_driver_scan_params *scan_params) + struct wpa_driver_scan_params *scan_params) { - wifi_scan_config_t *params = NULL; - int ret = 0; - u64 scan_start_tsf = esp_wifi_get_tsf_time(WIFI_IF_STA); + wifi_scan_config_t *params = NULL; + int ret = 0; + u64 scan_start_tsf = esp_wifi_get_tsf_time(WIFI_IF_STA); - /* TODO: Directly try to connect if scan results are recent */ - if ((scan_start_tsf - wpa_s->scan_start_tsf) > 100000) { - wpa_printf(MSG_DEBUG, "flushing old scan cache %llu", - (scan_start_tsf - wpa_s->scan_start_tsf)); - wpa_bss_flush(wpa_s); - } + /* TODO: Directly try to connect if scan results are recent */ + if ((scan_start_tsf - wpa_s->scan_start_tsf) > 100000) { + wpa_printf(MSG_DEBUG, "flushing old scan cache %llu", + (scan_start_tsf - wpa_s->scan_start_tsf)); + wpa_bss_flush(wpa_s); + } - esp_wifi_get_macaddr_internal(WIFI_IF_STA, wpa_s->tsf_bssid); + esp_wifi_get_macaddr_internal(WIFI_IF_STA, wpa_s->tsf_bssid); - if (scan_params) { - params = os_zalloc(sizeof(wifi_scan_config_t)); - if (!params) { - wpa_printf(MSG_ERROR, "failed to allocate memory"); - return -1; - } - if (scan_params->num_ssids) { - params->ssid = os_zalloc(scan_params->ssids[0].ssid_len + 1); - if (!params->ssid) { - wpa_printf(MSG_ERROR, "failed to allocate memory"); - ret = -1; - goto cleanup; - } - os_memcpy(params->ssid, scan_params->ssids[0].ssid, scan_params->ssids[0].ssid_len); - } else + if (scan_params) { + params = os_zalloc(sizeof(wifi_scan_config_t)); + if (!params) { + wpa_printf(MSG_ERROR, "failed to allocate memory"); + return -1; + } + if (scan_params->num_ssids) { + params->ssid = os_zalloc(scan_params->ssids[0].ssid_len + 1); + if (!params->ssid) { + wpa_printf(MSG_ERROR, "failed to allocate memory"); + ret = -1; + goto cleanup; + } + os_memcpy(params->ssid, scan_params->ssids[0].ssid, scan_params->ssids[0].ssid_len); + } else - if (scan_params->mode == BEACON_REPORT_MODE_PASSIVE) { - params->scan_type = WIFI_SCAN_TYPE_PASSIVE; - } else { - params->scan_type = WIFI_SCAN_TYPE_ACTIVE; - } + if (scan_params->mode == BEACON_REPORT_MODE_PASSIVE) { + params->scan_type = WIFI_SCAN_TYPE_PASSIVE; + } else { + params->scan_type = WIFI_SCAN_TYPE_ACTIVE; + } - if (scan_params->bssid) { - params->bssid = os_zalloc(ETH_ALEN); - if (!params->bssid) { - wpa_printf(MSG_ERROR, "failed to allocate memory"); - ret = -1; - goto cleanup; - } - os_memcpy(params->bssid, scan_params->bssid, ETH_ALEN); - } - if (scan_params->channel) { - params->channel = scan_params->channel; - } + if (scan_params->bssid) { + params->bssid = os_zalloc(ETH_ALEN); + if (!params->bssid) { + wpa_printf(MSG_ERROR, "failed to allocate memory"); + ret = -1; + goto cleanup; + } + os_memcpy(params->bssid, scan_params->bssid, ETH_ALEN); + } + if (scan_params->channel) { + params->channel = scan_params->channel; + } #ifdef CONFIG_WNM - else { - get_scan_channel_bitmap(wpa_s, params); - } + else { + get_scan_channel_bitmap(wpa_s, params); + } #endif /*CONFIG_WNM*/ - if (scan_params->duration) { - params->scan_time.passive = scan_params->duration; - params->scan_time.active.min = scan_params->duration; - params->scan_time.active.max = scan_params->duration; - } else { - params->scan_time.active.min = SUPPLICANT_SCAN_ACTIVE_SCAN_MIN_DURATION; - params->scan_time.active.max = SUPPLICANT_SCAN_ACTIVE_SCAN_MAX_DURATION; - } - } + if (scan_params->duration) { + params->scan_time.passive = scan_params->duration; + params->scan_time.active.min = scan_params->duration; + params->scan_time.active.max = scan_params->duration; + } else { + params->scan_time.active.min = SUPPLICANT_SCAN_ACTIVE_SCAN_MIN_DURATION; + params->scan_time.active.max = SUPPLICANT_SCAN_ACTIVE_SCAN_MAX_DURATION; + } + } - wpa_s->scan_start_tsf = scan_start_tsf; - /* Register frames to come to supplicant when we park on channel */ - wpa_s->type |= (1 << WLAN_FC_STYPE_BEACON) | (1 << WLAN_FC_STYPE_PROBE_RESP); - esp_wifi_register_mgmt_frame_internal(wpa_s->type, wpa_s->subtype); + wpa_s->scan_start_tsf = scan_start_tsf; + /* Register frames to come to supplicant when we park on channel */ + wpa_s->type |= (1 << WLAN_FC_STYPE_BEACON) | (1 << WLAN_FC_STYPE_PROBE_RESP); + esp_wifi_register_mgmt_frame_internal(wpa_s->type, wpa_s->subtype); - typedef void (* scan_done_cb_t)(void *arg, ETS_STATUS status); - extern int esp_wifi_promiscuous_scan_start(wifi_scan_config_t *config, scan_done_cb_t cb); - /* issue scan */ - if (esp_wifi_promiscuous_scan_start(params, scan_done_event_handler) < 0) { - ret = -1; - goto cleanup; - } - wpa_s->scanning = 1; - wpa_bss_update_start(wpa_s); - wpa_printf(MSG_INFO, "scan issued at time=%llu", wpa_s->scan_start_tsf); + typedef void (* scan_done_cb_t)(void *arg, ETS_STATUS status); + extern int esp_wifi_promiscuous_scan_start(wifi_scan_config_t *config, scan_done_cb_t cb); + /* issue scan */ + if (esp_wifi_promiscuous_scan_start(params, scan_done_event_handler) < 0) { + ret = -1; + goto cleanup; + } + wpa_s->scanning = 1; + wpa_bss_update_start(wpa_s); + wpa_printf(MSG_INFO, "scan issued at time=%llu", wpa_s->scan_start_tsf); cleanup: if (params) { - if (params->ssid) + if (params->ssid) { os_free(params->ssid); - if (params->bssid) + } + if (params->bssid) { os_free(params->bssid); + } os_free(params); } - return ret; + return ret; } /** @@ -282,20 +285,22 @@ cleanup: * Returns: 0 on success, -1 on failure */ int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s, - struct wpa_driver_scan_params *params) + struct wpa_driver_scan_params *params) { - return issue_scan(wpa_s, params); + return issue_scan(wpa_s, params); } void wpa_scan_results_free(struct wpa_scan_results *res) { - size_t i; + size_t i; - if (res == NULL) - return; + if (res == NULL) { + return; + } - for (i = 0; i < res->num; i++) - os_free(res->res[i]); - os_free(res->res); - os_free(res); + for (i = 0; i < res->num; i++) { + os_free(res->res[i]); + } + os_free(res->res); + os_free(res); } diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_scan_i.h b/components/wpa_supplicant/esp_supplicant/src/esp_scan_i.h index a4b6da0e82..bacdc41bc3 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_scan_i.h +++ b/components/wpa_supplicant/esp_supplicant/src/esp_scan_i.h @@ -13,7 +13,7 @@ void esp_scan_init(struct wpa_supplicant *wpa_s); void esp_scan_deinit(struct wpa_supplicant *wpa_s); int esp_handle_beacon_probe(u8 type, u8 *frame, size_t len, u8 *sender, - int8_t rssi, u8 channel, u64 current_tsf); + int8_t rssi, u8 channel, u64 current_tsf); void esp_supplicant_handle_scan_done_evt(void); #endif diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_wifi_driver.h b/components/wpa_supplicant/esp_supplicant/src/esp_wifi_driver.h index d5801593e5..9536c8785b 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_wifi_driver.h +++ b/components/wpa_supplicant/esp_supplicant/src/esp_wifi_driver.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -19,7 +19,7 @@ #define WPA2_TASK_STACK_SIZE (6144 + TASK_STACK_SIZE_ADD) #define WPS_TASK_STACK_SIZE (12288 + TASK_STACK_SIZE_ADD) -enum wpa_alg{ +enum wpa_alg { WIFI_WPA_ALG_NONE = 0, WIFI_WPA_ALG_WEP40 = 1, WIFI_WPA_ALG_TKIP = 2, @@ -64,9 +64,9 @@ enum { WPA2_AUTH_PSK = 0x05, WPA_AUTH_CCKM = 0x06, WPA2_AUTH_CCKM = 0x07, - WPA2_AUTH_PSK_SHA256= 0x08, + WPA2_AUTH_PSK_SHA256 = 0x08, WPA3_AUTH_PSK = 0x09, - WPA2_AUTH_ENT_SHA256= 0x0a, + WPA2_AUTH_ENT_SHA256 = 0x0a, WAPI_AUTH_PSK = 0x0b, WAPI_AUTH_CERT = 0x0c, WPA2_AUTH_ENT_SHA384_SUITE_B = 0x0d, @@ -146,18 +146,18 @@ struct wpa_funcs { }; struct wpa2_funcs { - int (*wpa2_sm_rx_eapol)(u8 *src_addr, u8 *buf, u32 len, u8 *bssid); - int (*wpa2_start)(void); - u8 (*wpa2_get_state)(void); - int (*wpa2_init)(void); + int (*wpa2_sm_rx_eapol)(u8 *src_addr, u8 *buf, u32 len, u8 *bssid); + int (*wpa2_start)(void); + u8(*wpa2_get_state)(void); + int (*wpa2_init)(void); void (*wpa2_deinit)(void); }; struct wps_funcs { bool (*wps_parse_scan_result)(struct wps_scan_ie *scan); - int (*wifi_station_wps_start)(void); - int (*wps_sm_rx_eapol)(u8 *src_addr, u8 *buf, u32 len); - int (*wps_start_pending)(void); + int (*wifi_station_wps_start)(void); + int (*wps_sm_rx_eapol)(u8 *src_addr, u8 *buf, u32 len); + int (*wps_start_pending)(void); }; typedef esp_err_t (*wifi_wpa2_fn_t)(void *); diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_wpa2_api_port.c b/components/wpa_supplicant/esp_supplicant/src/esp_wpa2_api_port.c index a61c3ca8eb..cdf996f487 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_wpa2_api_port.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_wpa2_api_port.c @@ -71,7 +71,7 @@ esp_err_t esp_wifi_sta_wpa2_ent_set_cert_key(const unsigned char *client_cert, i const unsigned char *private_key_passwd, int private_key_passwd_len) { return esp_eap_client_set_certificate_and_key(client_cert, client_cert_len, - private_key, private_key_len, private_key_passwd, private_key_passwd_len); + private_key, private_key_len, private_key_passwd, private_key_passwd_len); } void esp_wifi_sta_wpa2_ent_clear_cert_key(void) diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_wpa3.c b/components/wpa_supplicant/esp_supplicant/src/esp_wpa3.c index a0e7ba4d16..3da372ebb9 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_wpa3.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_wpa3.c @@ -17,7 +17,6 @@ #include "esp_hostap.h" #include - static struct sae_pt *g_sae_pt; static struct sae_data g_sae_data; static struct wpabuf *g_sae_token = NULL; @@ -34,7 +33,7 @@ static esp_err_t wpa3_build_sae_commit(u8 *bssid, size_t *sae_msg_len) const u8 *pw = (const u8 *)esp_wifi_sta_get_prof_password_internal(); struct wifi_ssid *ssid = esp_wifi_sta_get_prof_ssid_internal(); uint8_t sae_pwe = esp_wifi_get_config_sae_pwe_h2e_internal(WIFI_IF_STA); - char sae_pwd_id[SAE_H2E_IDENTIFIER_LEN+1] = {0}; + char sae_pwd_id[SAE_H2E_IDENTIFIER_LEN + 1] = {0}; bool valid_pwd_id = false; const u8 *rsnxe; u8 rsnxe_capa = 0; @@ -43,39 +42,39 @@ static esp_err_t wpa3_build_sae_commit(u8 *bssid, size_t *sae_msg_len) use_pt = 1; } - rsnxe = esp_wifi_sta_get_rsnxe(bssid); - if (rsnxe && rsnxe[1] >= 1) { - rsnxe_capa = rsnxe[2]; - } + rsnxe = esp_wifi_sta_get_rsnxe(bssid); + if (rsnxe && rsnxe[1] >= 1) { + rsnxe_capa = rsnxe[2]; + } #ifdef CONFIG_SAE_PK bool use_pk = false; uint8_t sae_pk_mode = esp_wifi_sta_get_config_sae_pk_internal(); if ((rsnxe_capa & BIT(WLAN_RSNX_CAPAB_SAE_PK)) && - sae_pk_mode != WPA3_SAE_PK_MODE_DISABLED && - ((pw && sae_pk_valid_password((const char*)pw)))) { + sae_pk_mode != WPA3_SAE_PK_MODE_DISABLED && + ((pw && sae_pk_valid_password((const char *)pw)))) { use_pt = 1; use_pk = true; } if (sae_pk_mode == WPA3_SAE_PK_MODE_ONLY && !use_pk) { wpa_printf(MSG_DEBUG, - "SAE: Cannot use PK with the selected AP"); + "SAE: Cannot use PK with the selected AP"); return ESP_FAIL; } #endif /* CONFIG_SAE_PK */ if (use_pt || sae_pwe == SAE_PWE_HASH_TO_ELEMENT || - sae_pwe == SAE_PWE_BOTH) { + sae_pwe == SAE_PWE_BOTH) { use_pt = !!(rsnxe_capa & BIT(WLAN_RSNX_CAPAB_SAE_H2E)); if ((sae_pwe == SAE_PWE_HASH_TO_ELEMENT || - wpa_key_mgmt_sae_ext_key(gWpaSm.key_mgmt) + wpa_key_mgmt_sae_ext_key(gWpaSm.key_mgmt) #ifdef CONFIG_SAE_PK - || (use_pk && sae_pk_mode == WPA3_SAE_PK_MODE_ONLY) + || (use_pk && sae_pk_mode == WPA3_SAE_PK_MODE_ONLY) #endif /* CONFIG_SAE_PK */ ) && !use_pt) { wpa_printf(MSG_DEBUG, - "SAE: Cannot use H2E with the selected AP"); + "SAE: Cannot use H2E with the selected AP"); return ESP_FAIL; } } @@ -123,14 +122,14 @@ static esp_err_t wpa3_build_sae_commit(u8 *bssid, size_t *sae_msg_len) if (use_pt && sae_prepare_commit_pt(&g_sae_data, g_sae_pt, - own_addr, bssid, NULL, NULL) < 0) { + own_addr, bssid, NULL, NULL) < 0) { wpa_printf(MSG_ERROR, "wpa3: failed to prepare SAE commit!"); return ESP_FAIL; } if (!use_pt && sae_prepare_commit(own_addr, bssid, pw, - strlen((const char *)pw), - &g_sae_data) < 0) { + strlen((const char *)pw), + &g_sae_data) < 0) { wpa_printf(MSG_ERROR, "wpa3: failed to prepare SAE commit!"); return ESP_FAIL; } @@ -138,9 +137,9 @@ static esp_err_t wpa3_build_sae_commit(u8 *bssid, size_t *sae_msg_len) #ifdef CONFIG_SAE_PK if (g_sae_data.tmp && use_pt && use_pk) { g_sae_data.pk = 1; - os_memcpy(g_sae_data.tmp->own_addr,own_addr, ETH_ALEN ); + os_memcpy(g_sae_data.tmp->own_addr, own_addr, ETH_ALEN); os_memcpy(g_sae_data.tmp->peer_addr, bssid, ETH_ALEN); - sae_pk_set_password(&g_sae_data,(const char*) pw); + sae_pk_set_password(&g_sae_data, (const char*) pw); } #endif @@ -170,8 +169,9 @@ reuse_data: static esp_err_t wpa3_build_sae_confirm(void) { - if (g_sae_data.state != SAE_COMMITTED) + if (g_sae_data.state != SAE_COMMITTED) { return ESP_FAIL; + } if (g_sae_confirm) { wpabuf_free(g_sae_confirm); @@ -218,25 +218,27 @@ static u8 *wpa3_build_sae_msg(u8 *bssid, u32 sae_msg_type, size_t *sae_msg_len) u8 *buf = NULL; switch (sae_msg_type) { - case SAE_MSG_COMMIT: - /* Do not go for SAE when WPS is ongoing */ - if (esp_wifi_get_wps_status_internal() != WPS_STATUS_DISABLE) { - *sae_msg_len = 0; - return NULL; - } - if (ESP_OK != wpa3_build_sae_commit(bssid, sae_msg_len)) - return NULL; - *sae_msg_len = wpabuf_len(g_sae_commit); - buf = wpabuf_mhead_u8(g_sae_commit); - break; - case SAE_MSG_CONFIRM: - if (ESP_OK != wpa3_build_sae_confirm()) - return NULL; - *sae_msg_len = wpabuf_len(g_sae_confirm); - buf = wpabuf_mhead_u8(g_sae_confirm); - break; - default: - break; + case SAE_MSG_COMMIT: + /* Do not go for SAE when WPS is ongoing */ + if (esp_wifi_get_wps_status_internal() != WPS_STATUS_DISABLE) { + *sae_msg_len = 0; + return NULL; + } + if (ESP_OK != wpa3_build_sae_commit(bssid, sae_msg_len)) { + return NULL; + } + *sae_msg_len = wpabuf_len(g_sae_commit); + buf = wpabuf_mhead_u8(g_sae_commit); + break; + case SAE_MSG_CONFIRM: + if (ESP_OK != wpa3_build_sae_confirm()) { + return NULL; + } + *sae_msg_len = wpabuf_len(g_sae_confirm); + buf = wpabuf_mhead_u8(g_sae_confirm); + break; + default: + break; } return buf; @@ -252,13 +254,14 @@ static int wpa3_parse_sae_commit(u8 *buf, u32 len, u16 status) } if (status == WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ) { - if (g_sae_token) + if (g_sae_token) { wpabuf_free(g_sae_token); + } if (g_sae_data.h2e) { if ((buf[2] != WLAN_EID_EXTENSION) || - (buf[3] == 0) || - (buf[3] > len - 4) || - (buf[4] != WLAN_EID_EXT_ANTI_CLOGGING_TOKEN)) { + (buf[3] == 0) || + (buf[3] > len - 4) || + (buf[4] != WLAN_EID_EXT_ANTI_CLOGGING_TOKEN)) { wpa_printf(MSG_ERROR, "Invalid SAE anti-clogging token container header"); return ESP_FAIL; } @@ -312,17 +315,17 @@ static int wpa3_parse_sae_msg(u8 *buf, size_t len, u32 sae_msg_type, u16 status) int ret = ESP_OK; switch (sae_msg_type) { - case SAE_MSG_COMMIT: - ret = wpa3_parse_sae_commit(buf, len, status); - break; - case SAE_MSG_CONFIRM: - ret = wpa3_parse_sae_confirm(buf, len); - esp_wpa3_free_sae_data(); - break; - default: - wpa_printf(MSG_ERROR, "wpa3: Invalid SAE msg type(%" PRId32 ")!", sae_msg_type); - ret = ESP_FAIL; - break; + case SAE_MSG_COMMIT: + ret = wpa3_parse_sae_commit(buf, len, status); + break; + case SAE_MSG_CONFIRM: + ret = wpa3_parse_sae_confirm(buf, len); + esp_wpa3_free_sae_data(); + break; + default: + wpa_printf(MSG_ERROR, "wpa3: Invalid SAE msg type(%" PRId32 ")!", sae_msg_type); + ret = ESP_FAIL; + break; } return ret; @@ -433,7 +436,7 @@ static void wpa3_process_rx_commit(wpa3_hostap_auth_event_t *evt) os_semphr_give(sta->lock); uint16_t aid = 0; if (ret != WLAN_STATUS_SUCCESS && - ret != WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ) { + ret != WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ) { esp_wifi_ap_get_sta_aid(frm->bssid, &aid); if (aid == 0) { esp_wifi_ap_deauth_internal(frm->bssid, ret); @@ -516,7 +519,7 @@ static void esp_wpa3_hostap_task(void *pvParameters) } } uint32_t items_in_queue = os_queue_msg_waiting(g_wpa3_hostap_evt_queue); - while(items_in_queue--) { + while (items_in_queue--) { /* Free events posted to queue */ os_queue_recv(g_wpa3_hostap_evt_queue, &evt, portMAX_DELAY); if (evt->id == SIG_WPA3_RX_CONFIRM) { @@ -556,9 +559,9 @@ int wpa3_hostap_auth_init(void *data) } if (os_task_create(esp_wpa3_hostap_task, "esp_wpa3_hostap_task", - WPA3_HOSTAP_HANDLE_AUTH_TASK_STACK_SIZE, NULL, - WPA3_HOSTAP_HANDLE_AUTH_TASK_PRIORITY, - &g_wpa3_hostap_task_hdl) != pdPASS) { + WPA3_HOSTAP_HANDLE_AUTH_TASK_STACK_SIZE, NULL, + WPA3_HOSTAP_HANDLE_AUTH_TASK_PRIORITY, + &g_wpa3_hostap_task_hdl) != pdPASS) { wpa_printf(MSG_ERROR, "wpa3_hostap_auth_init: failed to create task"); os_queue_delete(g_wpa3_hostap_evt_queue); return ESP_FAIL; diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_wpa3_i.h b/components/wpa_supplicant/esp_supplicant/src/esp_wpa3_i.h index 198a52d8a4..9f5e49018d 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_wpa3_i.h +++ b/components/wpa_supplicant/esp_supplicant/src/esp_wpa3_i.h @@ -62,7 +62,7 @@ bool wpa3_hostap_auth_deinit(void); static inline void esp_wifi_register_wpa3_ap_cb(struct wpa_funcs *wpa_cb) { - wpa_cb->wpa3_hostap_handle_auth = NULL; + wpa_cb->wpa3_hostap_handle_auth = NULL; } #endif /* CONFIG_SAE */ diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_wpa_main.c b/components/wpa_supplicant/esp_supplicant/src/esp_wpa_main.c index d0a742467e..117e799732 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_wpa_main.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_wpa_main.c @@ -114,7 +114,7 @@ int wpa_config_bss(uint8_t *bssid) esp_wifi_get_macaddr_internal(0, mac); ret = wpa_set_bss((char *)mac, (char *)bssid, esp_wifi_sta_get_pairwise_cipher_internal(), esp_wifi_sta_get_group_cipher_internal(), - (char *)esp_wifi_sta_get_prof_password_internal(), ssid->ssid, ssid->len); + (char *)esp_wifi_sta_get_prof_password_internal(), ssid->ssid, ssid->len); return ret; } @@ -136,7 +136,7 @@ bool wpa_attach(void) { bool ret = true; ret = wpa_sm_init(); - if(ret) { + if (ret) { ret = (esp_wifi_register_eapol_txdonecb_internal(eapol_txcb) == ESP_OK); } esp_set_scan_ie(); @@ -167,8 +167,8 @@ bool wpa_ap_rx_eapol(void *hapd_data, void *sm_data, u8 *data, size_t data_len) int wps_type = esp_wifi_get_wps_type_internal(); if ((wps_type == WPS_TYPE_PBC) || - (wps_type == WPS_TYPE_PIN)) { - ieee802_1x_receive(hapd, sta->addr, data, data_len); + (wps_type == WPS_TYPE_PIN)) { + ieee802_1x_receive(hapd, sta->addr, data, data_len); return true; } #endif @@ -285,24 +285,24 @@ static void wpa_sta_connected_cb(uint8_t *bssid) static void wpa_sta_disconnected_cb(uint8_t reason_code) { switch (reason_code) { - case WIFI_REASON_AUTH_EXPIRE: - case WIFI_REASON_NOT_AUTHED: - case WIFI_REASON_NOT_ASSOCED: - case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT: - case WIFI_REASON_INVALID_PMKID: - case WIFI_REASON_AUTH_FAIL: - case WIFI_REASON_ASSOC_FAIL: - case WIFI_REASON_CONNECTION_FAIL: - case WIFI_REASON_HANDSHAKE_TIMEOUT: - esp_wpa3_free_sae_data(); + case WIFI_REASON_AUTH_EXPIRE: + case WIFI_REASON_NOT_AUTHED: + case WIFI_REASON_NOT_ASSOCED: + case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT: + case WIFI_REASON_INVALID_PMKID: + case WIFI_REASON_AUTH_FAIL: + case WIFI_REASON_ASSOC_FAIL: + case WIFI_REASON_CONNECTION_FAIL: + case WIFI_REASON_HANDSHAKE_TIMEOUT: + esp_wpa3_free_sae_data(); + wpa_sta_clear_curr_pmksa(); + wpa_sm_notify_disassoc(&gWpaSm); + break; + default: + if (g_wpa_pmk_caching_disabled) { wpa_sta_clear_curr_pmksa(); - wpa_sm_notify_disassoc(&gWpaSm); - break; - default: - if (g_wpa_pmk_caching_disabled) { - wpa_sta_clear_curr_pmksa(); - } - break; + } + break; } #ifdef CONFIG_OWE_STA owe_deinit(); @@ -346,7 +346,7 @@ static int check_n_add_wps_sta(struct hostapd_data *hapd, struct sta_info *sta_i } #endif -static bool hostap_sta_join(void **sta, u8 *bssid, u8 *wpa_ie, u8 wpa_ie_len,u8 *rsnxe, u8 rsnxe_len, bool *pmf_enable, int subtype) +static bool hostap_sta_join(void **sta, u8 *bssid, u8 *wpa_ie, u8 wpa_ie_len, u8 *rsnxe, u8 rsnxe_len, bool *pmf_enable, int subtype) { struct sta_info *sta_info = NULL; struct hostapd_data *hapd = hostapd_get_hapd_data(); @@ -370,7 +370,7 @@ static bool hostap_sta_join(void **sta, u8 *bssid, u8 *wpa_ie, u8 wpa_ie_len,u8 ap_free_sta(hapd, old_sta); } #ifdef CONFIG_SAE - else if (old_sta && old_sta->lock) { + else if (old_sta && old_sta->lock) { sta_info = old_sta; goto process_old_sta; } @@ -379,7 +379,7 @@ static bool hostap_sta_join(void **sta, u8 *bssid, u8 *wpa_ie, u8 wpa_ie_len,u8 sta_info = ap_get_sta(hapd, bssid); if (!sta_info) { - sta_info = ap_sta_add(hapd,bssid); + sta_info = ap_sta_add(hapd, bssid); if (!sta_info) { wpa_printf(MSG_ERROR, "failed to add station " MACSTR, MAC2STR(bssid)); goto fail; @@ -397,7 +397,6 @@ static bool hostap_sta_join(void **sta, u8 *bssid, u8 *wpa_ie, u8 wpa_ie_len,u8 process_old_sta: #endif /* CONFIG_SAE */ - #ifdef CONFIG_WPS_REGISTRAR if (check_n_add_wps_sta(hapd, sta_info, wpa_ie, wpa_ie_len, pmf_enable, subtype) == 0) { if (sta_info->eapol_sm) { diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_wpas_glue.c b/components/wpa_supplicant/esp_supplicant/src/esp_wpas_glue.c index 23608c7299..e4f52c4321 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_wpas_glue.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_wpas_glue.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -72,13 +72,13 @@ int wpa_ether_send(void *ctx, const u8 *dest, u16 proto, } int hostapd_send_eapol(const u8 *source, const u8 *sta_addr, - const u8 *data, size_t data_len) + const u8 *data, size_t data_len) { void *buffer = os_malloc(data_len + sizeof(struct l2_ethhdr)); struct l2_ethhdr *eth = buffer; - if (!buffer){ - wpa_printf( MSG_DEBUG, "send_eapol, buffer=%p", buffer); + if (!buffer) { + wpa_printf(MSG_DEBUG, "send_eapol, buffer=%p", buffer); return -1; } @@ -102,26 +102,26 @@ void wpa_supplicant_transition_disable(struct wpa_sm *sm, u8 bitmap) { wpa_printf(MSG_DEBUG, "TRANSITION_DISABLE %02x", bitmap); - if ((bitmap & TRANSITION_DISABLE_WPA3_PERSONAL) && - wpa_key_mgmt_sae(sm->key_mgmt)) { + if ((bitmap & TRANSITION_DISABLE_WPA3_PERSONAL) && + wpa_key_mgmt_sae(sm->key_mgmt)) { disable_wpa_wpa2(); } if ((bitmap & TRANSITION_DISABLE_SAE_PK) && - wpa_key_mgmt_sae(sm->key_mgmt)) { + wpa_key_mgmt_sae(sm->key_mgmt)) { wpa_printf(MSG_INFO, - "SAE-PK: SAE authentication without PK disabled based on AP notification"); + "SAE-PK: SAE authentication without PK disabled based on AP notification"); disable_wpa_wpa2(); esp_wifi_enable_sae_pk_only_mode_internal(); } if ((bitmap & TRANSITION_DISABLE_WPA3_ENTERPRISE) && - wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) { + wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) { disable_wpa_wpa2(); } if ((bitmap & TRANSITION_DISABLE_ENHANCED_OPEN) && - wpa_key_mgmt_owe(sm->key_mgmt)) { + wpa_key_mgmt_owe(sm->key_mgmt)) { esp_wifi_sta_disable_owe_trans_internal(); } } diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_wpas_glue.h b/components/wpa_supplicant/esp_supplicant/src/esp_wpas_glue.h index e18ad28cdd..48ff0a6379 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_wpas_glue.h +++ b/components/wpa_supplicant/esp_supplicant/src/esp_wpas_glue.h @@ -34,5 +34,5 @@ int wpa_ether_send(void *ctx, const u8 *dest, u16 proto, void wpa_supplicant_transition_disable(struct wpa_sm *sm, u8 bitmap); int hostapd_send_eapol(const u8 *source, const u8 *sta_addr, - const u8 *data, size_t data_len); + const u8 *data, size_t data_len); #endif /* WPAS_GLUE_H */ diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_wps.c b/components/wpa_supplicant/esp_supplicant/src/esp_wps.c index 05680bd115..54a01e741b 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_wps.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_wps.c @@ -43,7 +43,7 @@ struct wps_rx_param { int len; STAILQ_ENTRY(wps_rx_param) bqentry; }; -static STAILQ_HEAD(,wps_rx_param) s_wps_rxq; +static STAILQ_HEAD(, wps_rx_param) s_wps_rxq; static void *s_wps_task_hdl = NULL; static void *s_wps_queue = NULL; @@ -85,7 +85,7 @@ static void wps_rxq_init(void) static void wps_rxq_enqueue(struct wps_rx_param *param) { DATA_MUTEX_TAKE(); - STAILQ_INSERT_TAIL(&s_wps_rxq,param, bqentry); + STAILQ_INSERT_TAIL(&s_wps_rxq, param, bqentry); DATA_MUTEX_GIVE(); } @@ -95,7 +95,7 @@ static struct wps_rx_param * wps_rxq_dequeue(void) DATA_MUTEX_TAKE(); if ((param = STAILQ_FIRST(&s_wps_rxq)) != NULL) { STAILQ_REMOVE_HEAD(&s_wps_rxq, bqentry); - STAILQ_NEXT(param,bqentry) = NULL; + STAILQ_NEXT(param, bqentry) = NULL; } DATA_MUTEX_GIVE(); return param; @@ -107,7 +107,7 @@ static void wps_rxq_deinit(void) DATA_MUTEX_TAKE(); while ((param = STAILQ_FIRST(&s_wps_rxq)) != NULL) { STAILQ_REMOVE_HEAD(&s_wps_rxq, bqentry); - STAILQ_NEXT(param,bqentry) = NULL; + STAILQ_NEXT(param, bqentry) = NULL; os_free(param->buf); os_free(param); } @@ -115,7 +115,7 @@ static void wps_rxq_deinit(void) } #ifdef USE_WPS_TASK -void wps_task(void *pvParameters ) +void wps_task(void *pvParameters) { ETSEvent *e; wps_ioctl_param_t *param; @@ -125,9 +125,9 @@ void wps_task(void *pvParameters ) wpa_printf(MSG_DEBUG, "wps_Task enter"); for (;;) { - if ( TRUE == os_queue_recv(s_wps_queue, &e, OS_BLOCK) ) { + if (TRUE == os_queue_recv(s_wps_queue, &e, OS_BLOCK)) { - if ( (e->sig >= SIG_WPS_ENABLE) && (e->sig < SIG_WPS_NUM) ) { + if ((e->sig >= SIG_WPS_ENABLE) && (e->sig < SIG_WPS_NUM)) { DATA_MUTEX_TAKE(); if (s_wps_sig_cnt[e->sig]) { s_wps_sig_cnt[e->sig]--; @@ -276,7 +276,6 @@ static inline int wps_sm_ether_send(struct wps_sm *sm, u16 proto, return wpa_ether_send(sm, bssid, proto, data, data_len); } - u8 *wps_sm_alloc_eapol(struct wps_sm *sm, u8 type, const void *data, u16 data_len, size_t *msg_len, void **data_pos) @@ -284,7 +283,6 @@ u8 *wps_sm_alloc_eapol(struct wps_sm *sm, u8 type, return wpa_alloc_eapol(sm, type, data, data_len, msg_len, data_pos); } - void wps_sm_free_eapol(u8 *buffer) { return wpa_free_eapol(buffer); @@ -374,7 +372,7 @@ wps_parse_scan_result(struct wps_scan_ie *scan) esp_wifi_get_mode(&op_mode); if ((op_mode != WIFI_MODE_STA) #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT - && (op_mode != WIFI_MODE_APSTA) + && (op_mode != WIFI_MODE_APSTA) #endif ) { return false; @@ -396,7 +394,7 @@ wps_parse_scan_result(struct wps_scan_ie *scan) int count; if ((wps_get_type() == WPS_TYPE_PBC && wps_is_selected_pbc_registrar(buf)) || - (wps_get_type() == WPS_TYPE_PIN && wps_is_addr_authorized(buf, sm->ownaddr, 1))) { + (wps_get_type() == WPS_TYPE_PIN && wps_is_addr_authorized(buf, sm->ownaddr, 1))) { /* Found one AP with selected registrar true */ sm->ignore_sel_reg = false; sm->discard_ap_cnt = 0; @@ -417,7 +415,7 @@ wps_parse_scan_result(struct wps_scan_ie *scan) wpabuf_free(buf); if (scan->ssid[1] > SSID_MAX_LEN) { return false; - } + } esp_wifi_enable_sta_privacy_internal(); os_memset(sm->ssid[0], 0, SSID_MAX_LEN); os_memcpy(sm->ssid[0], (char *)&scan->ssid[2], (int)scan->ssid[1]); @@ -463,7 +461,6 @@ int wps_send_eap_identity_rsp(u8 id) goto _err; } - wpabuf_put_data(eap_buf, sm->identity, sm->identity_len); buf = wps_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAP_PACKET, wpabuf_head_u8(eap_buf), wpabuf_len(eap_buf), (size_t *)&len, NULL); @@ -590,14 +587,14 @@ int wps_process_wps_mX_req(u8 *ubuf, int len, enum wps_process_res *res) if (sm->state == WAIT_START) { if (expd->opcode != WSC_Start) { wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d " - "in WAIT_START state", expd->opcode); + "in WAIT_START state", expd->opcode); return ESP_FAIL; } wpa_printf(MSG_DEBUG, "EAP-WSC: Received start"); sm->state = WPA_MESG; - } else if (expd->opcode == WSC_Start){ + } else if (expd->opcode == WSC_Start) { wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d", - expd->opcode); + expd->opcode); return ESP_FAIL; } @@ -617,7 +614,7 @@ int wps_process_wps_mX_req(u8 *ubuf, int len, enum wps_process_res *res) if (tlen > 50000) { wpa_printf(MSG_ERROR, "EAP-WSC: Invalid Message Length"); return ESP_FAIL; - } + } wpa_printf(MSG_DEBUG, "rx frag msg id:%d, flag:%d, frag_len: %d, tot_len: %d, be_tot_len:%d", sm->current_identifier, flag, frag_len, tlen, be_tot_len); if (ESP_OK != wps_enrollee_process_msg_frag(&wps_buf, tlen, tbuf, frag_len, flag)) { if (wps_buf) { @@ -710,8 +707,6 @@ _err: return ret; } - - int wps_tx_start(void) { struct wps_sm *sm = gWpsSm; @@ -825,7 +820,7 @@ int wps_finish(void) os_free(config); } eloop_cancel_timeout(wifi_station_wps_success, NULL, NULL); - eloop_register_timeout(1, 0, wifi_station_wps_success, NULL, NULL); + eloop_register_timeout(1, 0, wifi_station_wps_success, NULL, NULL); ret = 0; } else { @@ -1021,7 +1016,7 @@ int wps_sm_rx_eapol_internal(u8 *src_addr, u8 *buf, u32 len) wpa_printf(MSG_DEBUG, "error: receive eapol response frame!"); ret = 0; break; - case EAP_CODE_REQUEST: { + case EAP_CODE_REQUEST: eap_type = ((u8 *)ehdr)[sizeof(*ehdr)]; switch (eap_type) { case EAP_TYPE_IDENTITY: @@ -1058,7 +1053,6 @@ int wps_sm_rx_eapol_internal(u8 *src_addr, u8 *buf, u32 len) break; } break; - } default: break; } @@ -1658,7 +1652,7 @@ int wifi_station_wps_start(void) eloop_register_timeout(120, 0, wifi_station_wps_timeout, NULL, NULL); switch (wps_get_status()) { - case WPS_STATUS_DISABLE: { + case WPS_STATUS_DISABLE: sm->is_wps_scan = true; wps_build_public_key(sm->wps, NULL); @@ -1672,7 +1666,6 @@ int wifi_station_wps_start(void) sm->wps->dh_privkey = NULL; wifi_wps_scan(NULL, NULL); break; - } case WPS_STATUS_SCANNING: sm->scan_cnt = 0; eloop_cancel_timeout(wifi_station_wps_timeout, NULL, NULL); diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_wps_i.h b/components/wpa_supplicant/esp_supplicant/src/esp_wps_i.h index ca527ff24e..a59dc897f9 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_wps_i.h +++ b/components/wpa_supplicant/esp_supplicant/src/esp_wps_i.h @@ -41,20 +41,20 @@ typedef struct { } wps_ioctl_param_t; #ifdef ESP_SUPPLICANT -enum wps_sm_state{ - WAIT_START, - WPA_MESG, - WPA_FAIL +enum wps_sm_state { + WAIT_START, + WPA_MESG, + WPA_FAIL }; #endif /* ESP_SUPPLICANT */ -#define WPS_IGNORE_SEL_REG_MAX_CNT 4 +#define WPS_IGNORE_SEL_REG_MAX_CNT 4 -#define WPS_MAX_DIS_AP_NUM 10 +#define WPS_MAX_DIS_AP_NUM 10 /* Bssid of the discard AP which is discarded for not select reg or other reason */ -struct discard_ap_list_t{ - u8 bssid[6]; +struct discard_ap_list_t { + u8 bssid[6]; }; #ifndef MAX_PASSPHRASE_LEN diff --git a/tools/ci/astyle-rules.yml b/tools/ci/astyle-rules.yml index c636bbf1cb..825c18801a 100644 --- a/tools/ci/astyle-rules.yml +++ b/tools/ci/astyle-rules.yml @@ -65,7 +65,6 @@ components_not_formatted_temporary: - "/components/esp_phy/" - "/components/esp_pm/" - "/components/esp_rom/" - - "/components/esp_wifi/" - "/components/esp-tls/" - "/components/esptool_py/" - "/components/fatfs/" @@ -95,7 +94,6 @@ components_not_formatted_temporary: - "/components/vfs/" - "/components/wear_levelling/" - "/components/wifi_provisioning/" - - "/components/wpa_supplicant/" - "/components/xtensa/" - "/examples/bluetooth/" - "/examples/build_system/" @@ -133,6 +131,8 @@ components_not_formatted_permanent: - "/components/xtensa/include/xtensa/" # FAT FS (upstream source code) - "/components/fatfs/src/" + # wpa_supplicant upstream code + - "/components/wpa_supplicant/src/" # Nginx HTTP parser (upstream source code) - "/components/http_parser/" # Argtable (upstream source code)