2021-05-09 22:35:07 -04:00
|
|
|
/*
|
2022-01-12 01:53:47 -05:00
|
|
|
* SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
|
2021-05-09 22:35:07 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2019-12-26 02:25:24 -05:00
|
|
|
#include "sdkconfig.h"
|
2016-11-11 01:00:34 -05:00
|
|
|
#include "bootloader_random.h"
|
2022-07-21 07:24:42 -04:00
|
|
|
#include "esp_cpu.h"
|
2016-11-11 01:00:34 -05:00
|
|
|
#include "soc/wdev_reg.h"
|
|
|
|
|
|
|
|
#ifndef BOOTLOADER_BUILD
|
2022-01-12 01:53:47 -05:00
|
|
|
#include "esp_random.h"
|
2021-10-25 05:13:46 -04:00
|
|
|
#include "esp_private/periph_ctrl.h"
|
2016-11-11 01:00:34 -05:00
|
|
|
|
2020-07-29 10:03:46 -04:00
|
|
|
__attribute__((weak)) void bootloader_fill_random(void *buffer, size_t length)
|
2018-08-15 04:20:16 -04:00
|
|
|
{
|
|
|
|
return esp_fill_random(buffer, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2021-10-25 07:13:42 -04:00
|
|
|
|
|
|
|
#if !defined CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
#define RNG_CPU_WAIT_CYCLE_NUM (80 * 32 * 2) /* extra factor of 2 is precautionary */
|
|
|
|
#else
|
|
|
|
#define RNG_CPU_WAIT_CYCLE_NUM (80 * 23) /* 45 KHz reading frequency is the maximum we have tested so far on S3 */
|
|
|
|
#endif
|
|
|
|
|
2020-07-29 10:03:46 -04:00
|
|
|
__attribute__((weak)) void bootloader_fill_random(void *buffer, size_t length)
|
2016-11-11 01:00:34 -05:00
|
|
|
{
|
|
|
|
uint8_t *buffer_bytes = (uint8_t *)buffer;
|
|
|
|
uint32_t random;
|
2017-01-03 23:36:04 -05:00
|
|
|
uint32_t start, now;
|
2018-08-15 04:20:16 -04:00
|
|
|
|
|
|
|
assert(buffer != NULL);
|
2016-11-11 01:00:34 -05:00
|
|
|
|
2020-11-16 23:48:35 -05:00
|
|
|
for (size_t i = 0; i < length; i++) {
|
2016-11-11 01:00:34 -05:00
|
|
|
if (i == 0 || i % 4 == 0) { /* redundant check is for a compiler warning */
|
2017-01-03 23:36:04 -05:00
|
|
|
/* in bootloader with ADC feeding HWRNG, we accumulate 1
|
|
|
|
bit of entropy per 40 APB cycles (==80 CPU cycles.)
|
2016-11-11 01:00:34 -05:00
|
|
|
|
2017-01-03 23:36:04 -05:00
|
|
|
To avoid reading the entire RNG hardware state out
|
|
|
|
as-is, we repeatedly read the RNG register and XOR all
|
|
|
|
values.
|
2016-11-11 01:00:34 -05:00
|
|
|
*/
|
|
|
|
random = REG_READ(WDEV_RND_REG);
|
2022-07-21 07:24:42 -04:00
|
|
|
start = esp_cpu_get_cycle_count();
|
2017-01-03 23:36:04 -05:00
|
|
|
do {
|
|
|
|
random ^= REG_READ(WDEV_RND_REG);
|
2022-07-21 07:24:42 -04:00
|
|
|
now = esp_cpu_get_cycle_count();
|
2021-10-25 07:13:42 -04:00
|
|
|
} while (now - start < RNG_CPU_WAIT_CYCLE_NUM);
|
2016-11-11 01:00:34 -05:00
|
|
|
}
|
|
|
|
buffer_bytes[i] = random >> ((i % 4) * 8);
|
|
|
|
}
|
|
|
|
}
|
2020-11-05 23:00:07 -05:00
|
|
|
|
2020-03-30 01:03:26 -04:00
|
|
|
#ifndef CONFIG_IDF_ENV_FPGA
|
|
|
|
|
|
|
|
#else // CONFIG_IDF_ENV_FPGA
|
|
|
|
#include "esp_log.h"
|
|
|
|
|
|
|
|
static void s_non_functional(const char *func)
|
|
|
|
{
|
|
|
|
ESP_EARLY_LOGW("rand", "%s non-functional for FPGA builds", func);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bootloader_random_enable()
|
|
|
|
{
|
|
|
|
s_non_functional(__func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bootloader_random_disable()
|
|
|
|
{
|
|
|
|
s_non_functional(__func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CONFIG_IDF_ENV_FPGA
|
|
|
|
|
2018-08-15 04:20:16 -04:00
|
|
|
#endif // BOOTLOADER_BUILD
|