diff --git a/docs/_static/esp_rng_noise_source_rf_available.png b/docs/_static/esp_rng_noise_source_rf_available.png new file mode 100644 index 0000000000..7af9335ec1 Binary files /dev/null and b/docs/_static/esp_rng_noise_source_rf_available.png differ diff --git a/docs/_static/esp_rng_noise_source_rf_unavailable.png b/docs/_static/esp_rng_noise_source_rf_unavailable.png new file mode 100644 index 0000000000..0973f66187 Binary files /dev/null and b/docs/_static/esp_rng_noise_source_rf_unavailable.png differ diff --git a/docs/en/api-reference/system/random.rst b/docs/en/api-reference/system/random.rst index ca0a13a1d7..aeb370a464 100644 --- a/docs/en/api-reference/system/random.rst +++ b/docs/en/api-reference/system/random.rst @@ -8,18 +8,62 @@ Random Number Generation {IDF_TARGET_NAME} contains a hardware random number generator (RNG). You can use the APIs :cpp:func:`esp_random` and :cpp:func:`esp_fill_random` to obtained random values from it. +Every 32-bit value that the system reads from the RNG_DATA_REG register of the random number generator is a true random number. These true random numbers are generated based on the thermal noise in the system and the asynchronous clock mismatch. + +.. only:: SOC_WIFI_SUPPORTED or SOC_IEEE802154_SUPPORTED or SOC_BT_SUPPORTED + + - Thermal noise comes from the high-speed ADC or SAR ADC or both. Whenever the high-speed ADC or SAR ADC is enabled, bit streams will be generated and fed into the random number generator through an XOR logic gate as random seeds. + +.. only:: not SOC_WIFI_SUPPORTED and not SOC_IEEE802154_SUPPORTED and not SOC_BT_SUPPORTED + + - Thermal noise comes from the SAR ADC. Whenever the SAR ADC is enabled, bit streams will be generated and fed into the random number generator through an XOR logic gate as random seeds. + +.. only:: not esp32 + + - RC_FAST_CLK is an asynchronous clock source and it increases the RNG entropy by introducing circuit metastability. See the :ref:`secondary entropy` section for more details. + +The following digram shows the noise sources for the RNG on the {IDF_TARGET_NAME}: + +.. only:: SOC_WIFI_SUPPORTED or SOC_IEEE802154_SUPPORTED or SOC_BT_SUPPORTED + + .. image:: /../_static/esp_rng_noise_source_rf_available.png + :scale: 80 % + :align: center + +.. only:: not SOC_WIFI_SUPPORTED and not SOC_IEEE802154_SUPPORTED and not SOC_BT_SUPPORTED + + .. image:: /../_static/esp_rng_noise_source_rf_unavailable.png + :scale: 80 % + :align: center + The hardware RNG produces true random numbers so long as one or more of the following conditions are met: .. list:: - :SOC_WIFI_SUPPORTED or SOC_IEEE802154_SUPPORTED or SOC_BT_SUPPORTED: - RF subsystem is enabled. i.e., {IDF_TARGET_RF_NAME} {IDF_TARGET_RF_IS} enabled. - - The internal entropy source (SAR ADC) has been enabled by calling :cpp:func:`bootloader_random_enable` and not yet disabled by calling :cpp:func:`bootloader_random_disable`. + :SOC_WIFI_SUPPORTED or SOC_IEEE802154_SUPPORTED or SOC_BT_SUPPORTED: - RF subsystem is enabled. i.e., {IDF_TARGET_RF_NAME} {IDF_TARGET_RF_IS} enabled. When enabled, the RF subsystem internally enables the High Speed ADC that can be used as the entropy source. The High Speed ADC may only be available when the respective RF subsystem is active (e.g., not in sleep mode). See the :ref:`enabling RF subsystem` section for more details. + - The internal entropy source SAR ADC has been enabled by calling :cpp:func:`bootloader_random_enable` and not yet disabled by calling :cpp:func:`bootloader_random_disable`. - While the ESP-IDF :ref:`second-stage-bootloader` is running. This is because the default ESP-IDF bootloader implementation calls :cpp:func:`bootloader_random_enable` when the bootloader starts, and :cpp:func:`bootloader_random_disable` before executing the application. When any of these conditions are true, samples of physical noise are continuously mixed into the internal hardware RNG state to provide entropy. Consult the **{IDF_TARGET_NAME} Technical Reference Manual** > **Random Number Generator (RNG)** [`PDF <{IDF_TARGET_TRM_EN_URL}#rng>`__] chapter for more details. If none of the above conditions are true, the output of the RNG should be considered as pseudo-random only. +.. only:: SOC_WIFI_SUPPORTED or SOC_IEEE802154_SUPPORTED or SOC_BT_SUPPORTED + + .. _enabling RF subsystem: + + Enabling RF subsystem + --------------------- + + The RF subsystem can be enabled with help of the following APIs: + + .. list:: + + :SOC_WIFI_SUPPORTED: - Wi-Fi: :cpp:func:`esp_wifi_start` + :SOC_BT_SUPPORTED: - Bluetooth (NimBLE): :cpp:func:`nimble_port_init()` which internally calls :cpp:func:`esp_bt_controller_enable()` + :SOC_BT_SUPPORTED: - Bluetooth (Bluedroid): :cpp:func:`esp_bt_controller_enable()` + :SOC_IEEE802154_SUPPORTED: - Thread/Zigbee: :cpp:func:`esp_openthread_init` + Startup ------- @@ -27,22 +71,20 @@ During startup, the ESP-IDF bootloader temporarily enables the non-RF internal e .. only:: not SOC_WIFI_SUPPORTED and not SOC_IEEE802154_SUPPORTED and not SOC_BT_SUPPORTED - However, after the application starts executing, then normally only pseudo-random numbers are available until the internal entropy source has been enabled again. + For {IDF_TARGET_NAME}, the High Speed ADC is not available. Hence the non-RF internal entropy source (SAR ADC) is kept enabled by default at the time of application startup. Please note that if the application wants to use ADC for other purposes, it should call :cpp:func:`bootloader_random_disable` before using the ADC and :cpp:func:`bootloader_random_enable` to re-enable the entropy source when the Random Number Generator is needed again. .. only:: SOC_WIFI_SUPPORTED or SOC_IEEE802154_SUPPORTED or SOC_BT_SUPPORTED However, after the application starts executing, then normally only pseudo-random numbers are available until {IDF_TARGET_RF_NAME} {IDF_TARGET_RF_IS} initialized or until the internal entropy source has been enabled again. -To re-enable the entropy source temporarily during application startup, or for an application that does not use {IDF_TARGET_RF_NAME}, call the function :cpp:func:`bootloader_random_enable` to re-enable the internal entropy source. The function :cpp:func:`bootloader_random_disable` must be called to disable the entropy source again before using any of the following features: + To re-enable the entropy source temporarily during application startup, or for an application that does not use {IDF_TARGET_RF_NAME}, call the function :cpp:func:`bootloader_random_enable` to re-enable the internal entropy source. The function :cpp:func:`bootloader_random_disable` must be called to disable the entropy source again before using any of the following features: -.. list:: + .. list:: - - ADC - - :esp32: - I2S - - :SOC_WIFI_SUPPORTED or SOC_IEEE802154_SUPPORTED or SOC_BT_SUPPORTED: - {IDF_TARGET_RF_NAME} + - ADC + :esp32: - I2S + :SOC_WIFI_SUPPORTED or SOC_IEEE802154_SUPPORTED or SOC_BT_SUPPORTED: - {IDF_TARGET_RF_NAME} .. note:: @@ -54,6 +96,8 @@ To re-enable the entropy source temporarily during application startup, or for a .. only:: not esp32 + .. _secondary entropy: + Secondary Entropy -----------------