fix(docs): Improved RNG documentation

Updated the RNG documentation to add more details
about the High speed ADC as a noise source
and its limitations.

Closes https://github.com/espressif/esp-idf/issues/14665
This commit is contained in:
Aditya Patwardhan
2024-12-27 18:04:36 +08:00
committed by Mahavir Jain
parent f4c40f7e69
commit d2cd0e72ef
3 changed files with 54 additions and 10 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

View File

@@ -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. {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: The hardware RNG produces true random numbers so long as one or more of the following conditions are met:
.. list:: .. 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. :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`. - 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. - 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. 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. 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 Startup
------- -------
@@ -27,7 +71,7 @@ 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 .. 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 .. only:: SOC_WIFI_SUPPORTED or SOC_IEEE802154_SUPPORTED or SOC_BT_SUPPORTED
@@ -39,9 +83,7 @@ To re-enable the entropy source temporarily during application startup, or for a
.. list:: .. list::
- ADC - ADC
:esp32: - I2S :esp32: - I2S
:SOC_WIFI_SUPPORTED or SOC_IEEE802154_SUPPORTED or SOC_BT_SUPPORTED: - {IDF_TARGET_RF_NAME} :SOC_WIFI_SUPPORTED or SOC_IEEE802154_SUPPORTED or SOC_BT_SUPPORTED: - {IDF_TARGET_RF_NAME}
.. note:: .. note::
@@ -54,6 +96,8 @@ To re-enable the entropy source temporarily during application startup, or for a
.. only:: not esp32 .. only:: not esp32
.. _secondary entropy:
Secondary Entropy Secondary Entropy
----------------- -----------------