esp_random() returns non-random numbers before wifi initialization after esp_restart()

MalteJ
Posts: 62
Joined: Wed Sep 21, 2016 10:26 pm

esp_random() returns non-random numbers before wifi initialization after esp_restart()

Postby MalteJ » Sat Feb 25, 2017 1:30 pm

Hi,

it looks like esp_random() still tries to get random numbers from the WiFi subsystem after doing an esp_restart() but before initializing the WiFi again.

I suspect the RNG configuration is not set to weak random number generation while/after calling esp_restart().

Here is a test code:

Code: Select all

#include "freertos/FreeRTOS.h"
#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "os.h"

#define TMP_LEN 8

esp_err_t event_handler(void *ctx, system_event_t *event)
{
    return ESP_OK;
}

void app_reset(void *ctx) {
    ESP_LOGI("RAND", "Scheduling esp_restart()...");
    vTaskDelay(5000 / portTICK_RATE_MS);
    ESP_LOGI("RAND", "ESP RESTART...");
    esp_restart();
}

void app_main(void)
{
    nvs_flash_init();
    tcpip_adapter_init();

    ESP_LOGI("RAND", "Random Number Generator Test");
    vTaskDelay(1000 / portTICK_RATE_MS);
    xTaskCreate( app_reset, "app_reset", 2048, 0, 9, NULL );

    ESP_LOGI("RAND", "Generating random numbers...");
    uint32_t tmp[TMP_LEN];
    ESP_LOGI("rand", "looping...");
    for(int i=0; i<TMP_LEN; i++)
        tmp[i] = esp_random();

    vTaskDelay(50 / portTICK_RATE_MS);

    for (int i=0; i<TMP_LEN; i++)
        ESP_LOGI("RAND", "tmp[%.2d] = %.8x", i, tmp[i]);

    vTaskDelay(2000 / portTICK_RATE_MS);


    ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
    ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
    ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
    ESP_ERROR_CHECK( esp_wifi_start() );

    wifi_config_t sta_config = {
        .sta = {
            .ssid = "access_point_name",
            .password = "password",
            .bssid_set = false
        }
    };
    ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &sta_config) );
    ESP_ERROR_CHECK( esp_wifi_start() );
    ESP_ERROR_CHECK( esp_wifi_connect() );
}
After a few restarts it looks like the random buf is empty and you'll always get 0x00.

Best,
Malte

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: esp_random() returns non-random numbers before wifi initialization after esp_restart()

Postby ESP_Angus » Mon Feb 27, 2017 4:08 am

Hi Malte,

Thanks for posting this. I can confirm this bug, using a slightly modified version of your program (had to move the esp_wifi_init() call a few lines higher.)

At the moment, reproducing the bug requires two things:
1) Wifi is initialised and esp_wifi_start() is called before esp_restart().
2) esp_wifi_stop() is called before the restart occurs (this happens by default inside esp_restart().)

For this reason, the bug doesn't occur on resets due to assertion failures, exceptions, etc.

We're working on a permanent fix and I'll let you know once we have one, but in the meantime a workaround is to comment the call to esp_wifi_stop() inside esp_restart() in system_api.c.

Angus

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: esp_random() returns non-random numbers before wifi initialization after esp_restart()

Postby ESP_Angus » Thu Mar 02, 2017 10:35 pm

This is fixed in the IDF master branch and forthcoming V2.0 release.

What happened is that esp_wifi_stop() accidentally disabled the hardware RNG clock, and it wasn't re-enabled following a soft reset.

This has been fixed so esp_wifi_stop() does not disable this clock, and as an extra "belts and braces" style check this clock is also enabled during the bootloader process as part of bootloader_random_enable().

Angus

MalteJ
Posts: 62
Joined: Wed Sep 21, 2016 10:26 pm

Re: esp_random() returns non-random numbers before wifi initialization after esp_restart()

Postby MalteJ » Fri Mar 03, 2017 6:11 pm

Great! Thank you!

Malte

Who is online

Users browsing this forum: No registered users and 150 guests