hw_random.c 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2021 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. uint32_t IRAM_ATTR esp_random(void)
  33. {
  34. /* The PRNG which implements WDEV_RANDOM register gets 2 bits
  35. * of extra entropy from a hardware randomness source every APB clock cycle
  36. * (provided WiFi or BT are enabled). To make sure entropy is not drained
  37. * faster than it is added, this function needs to wait for at least 16 APB
  38. * clock cycles after reading previous word. This implementation may actually
  39. * wait a bit longer due to extra time spent in arithmetic and branch statements.
  40. *
  41. * As a (probably unncessary) precaution to avoid returning the
  42. * RNG state as-is, the result is XORed with additional
  43. * WDEV_RND_REG reads while waiting.
  44. */
  45. /* This code does not run in a critical section, so CPU frequency switch may
  46. * happens while this code runs (this will not happen in the current
  47. * implementation, but possible in the future). However if that happens,
  48. * the number of cycles spent on frequency switching will certainly be more
  49. * than the number of cycles we need to wait here.
  50. */
  51. uint32_t cpu_to_apb_freq_ratio = esp_clk_cpu_freq() / esp_clk_apb_freq();
  52. static uint32_t last_ccount = 0;
  53. uint32_t ccount;
  54. uint32_t result = 0;
  55. #if SOC_LP_TIMER_SUPPORTED
  56. for (size_t i = 0; i < sizeof(result); i++) {
  57. do {
  58. ccount = esp_cpu_get_cycle_count();
  59. result ^= REG_READ(WDEV_RND_REG);
  60. } while (ccount - last_ccount < cpu_to_apb_freq_ratio * APB_CYCLE_WAIT_NUM);
  61. uint32_t current_rtc_timer_counter = (lp_timer_hal_get_cycle_count() & 0xFF);
  62. result ^= ((result ^ current_rtc_timer_counter) & 0xFF) << (i * 8);
  63. }
  64. #else
  65. do {
  66. ccount = esp_cpu_get_cycle_count();
  67. result ^= REG_READ(WDEV_RND_REG);
  68. } while (ccount - last_ccount < cpu_to_apb_freq_ratio * APB_CYCLE_WAIT_NUM);
  69. #endif
  70. last_ccount = ccount;
  71. return result ^ REG_READ(WDEV_RND_REG);
  72. }
  73. void esp_fill_random(void *buf, size_t len)
  74. {
  75. assert(buf != NULL);
  76. uint8_t *buf_bytes = (uint8_t *)buf;
  77. while (len > 0) {
  78. uint32_t word = esp_random();
  79. uint32_t to_copy = MIN(sizeof(word), len);
  80. memcpy(buf_bytes, &word, to_copy);
  81. buf_bytes += to_copy;
  82. len -= to_copy;
  83. }
  84. }