hw_random.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include <stddef.h>
  8. #include <string.h>
  9. #include <sys/param.h>
  10. #include "esp_attr.h"
  11. #include "esp_cpu.h"
  12. #include "soc/wdev_reg.h"
  13. #include "esp_private/esp_clk.h"
  14. #if SOC_LP_TIMER_SUPPORTED
  15. #include "hal/lp_timer_hal.h"
  16. #endif
  17. #if defined CONFIG_IDF_TARGET_ESP32S3
  18. #define APB_CYCLE_WAIT_NUM (1778) /* If APB clock is 80 MHz, the maximum sampling frequency is around 45 KHz*/
  19. /* 45 KHz reading frequency is the maximum we have tested so far on S3 */
  20. #elif defined CONFIG_IDF_TARGET_ESP32C6
  21. #define APB_CYCLE_WAIT_NUM (160 * 16) /* On ESP32C6, we only read one byte at a time, then XOR the value with
  22. * an asynchronous timer (see code below).
  23. * The current value translates to a sampling frequency of around 62.5 KHz
  24. * for reading 8 bit samples, which is the rate at which the RNG was tested,
  25. * plus additional overhead for the calculation, making it slower. */
  26. #elif defined CONFIG_IDF_TARGET_ESP32H2
  27. #define APB_CYCLE_WAIT_NUM (96 * 16) /* Same reasoning as for ESP32C6, but the CPU frequency on ESP32H2 is
  28. * 96MHz instead of 160 MHz */
  29. #else
  30. #define APB_CYCLE_WAIT_NUM (16)
  31. #endif
  32. #if CONFIG_IDF_TARGET_ESP32P4
  33. #include "esp_log.h"
  34. static const char *TAG = "hw_random";
  35. uint32_t IRAM_ATTR esp_random(void)
  36. {
  37. // TODO: IDF-6522
  38. ESP_EARLY_LOGW(TAG, "esp_random() has not been implemented yet");
  39. return 0xDEADBEEF;
  40. }
  41. #else // !CONFIG_IDF_TARGET_ESP32P4
  42. uint32_t IRAM_ATTR esp_random(void)
  43. {
  44. /* The PRNG which implements WDEV_RANDOM register gets 2 bits
  45. * of extra entropy from a hardware randomness source every APB clock cycle
  46. * (provided WiFi or BT are enabled). To make sure entropy is not drained
  47. * faster than it is added, this function needs to wait for at least 16 APB
  48. * clock cycles after reading previous word. This implementation may actually
  49. * wait a bit longer due to extra time spent in arithmetic and branch statements.
  50. *
  51. * As a (probably unncessary) precaution to avoid returning the
  52. * RNG state as-is, the result is XORed with additional
  53. * WDEV_RND_REG reads while waiting.
  54. */
  55. /* This code does not run in a critical section, so CPU frequency switch may
  56. * happens while this code runs (this will not happen in the current
  57. * implementation, but possible in the future). However if that happens,
  58. * the number of cycles spent on frequency switching will certainly be more
  59. * than the number of cycles we need to wait here.
  60. */
  61. uint32_t cpu_to_apb_freq_ratio = esp_clk_cpu_freq() / esp_clk_apb_freq();
  62. static uint32_t last_ccount = 0;
  63. uint32_t ccount;
  64. uint32_t result = 0;
  65. #if SOC_LP_TIMER_SUPPORTED
  66. for (size_t i = 0; i < sizeof(result); i++) {
  67. do {
  68. ccount = esp_cpu_get_cycle_count();
  69. result ^= REG_READ(WDEV_RND_REG);
  70. } while (ccount - last_ccount < cpu_to_apb_freq_ratio * APB_CYCLE_WAIT_NUM);
  71. uint32_t current_rtc_timer_counter = (lp_timer_hal_get_cycle_count() & 0xFF);
  72. result ^= ((result ^ current_rtc_timer_counter) & 0xFF) << (i * 8);
  73. }
  74. #else
  75. do {
  76. ccount = esp_cpu_get_cycle_count();
  77. result ^= REG_READ(WDEV_RND_REG);
  78. } while (ccount - last_ccount < cpu_to_apb_freq_ratio * APB_CYCLE_WAIT_NUM);
  79. #endif
  80. last_ccount = ccount;
  81. return result ^ REG_READ(WDEV_RND_REG);
  82. }
  83. #endif //!CONFIG_IDF_TARGET_ESP32P4
  84. void esp_fill_random(void *buf, size_t len)
  85. {
  86. assert(buf != NULL);
  87. uint8_t *buf_bytes = (uint8_t *)buf;
  88. while (len > 0) {
  89. uint32_t word = esp_random();
  90. uint32_t to_copy = MIN(sizeof(word), len);
  91. memcpy(buf_bytes, &word, to_copy);
  92. buf_bytes += to_copy;
  93. len -= to_copy;
  94. }
  95. }