hw_random.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 "hal/cpu_hal.h"
  12. #include "soc/wdev_reg.h"
  13. #if CONFIG_IDF_TARGET_ESP32
  14. #include "esp32/clk.h"
  15. #elif CONFIG_IDF_TARGET_ESP32S2
  16. #include "esp32s2/clk.h"
  17. #elif CONFIG_IDF_TARGET_ESP32S3
  18. #include "esp32s3/clk.h"
  19. #elif CONFIG_IDF_TARGET_ESP32C3
  20. #include "esp32c3/clk.h"
  21. #elif CONFIG_IDF_TARGET_ESP32H2
  22. #include "esp32h2/clk.h"
  23. #endif
  24. #if defined CONFIG_IDF_TARGET_ESP32S3
  25. #define APB_CYCLE_WAIT_NUM (1778) /* If APB clock is 80 MHz, maximum sampling frequency is around 45 KHz*/
  26. /* 45 KHz reading frequency is the maximum we have tested so far on S3 */
  27. #else
  28. #define APB_CYCLE_WAIT_NUM (16)
  29. #endif
  30. uint32_t IRAM_ATTR esp_random(void)
  31. {
  32. /* The PRNG which implements WDEV_RANDOM register gets 2 bits
  33. * of extra entropy from a hardware randomness source every APB clock cycle
  34. * (provided WiFi or BT are enabled). To make sure entropy is not drained
  35. * faster than it is added, this function needs to wait for at least 16 APB
  36. * clock cycles after reading previous word. This implementation may actually
  37. * wait a bit longer due to extra time spent in arithmetic and branch statements.
  38. *
  39. * As a (probably unncessary) precaution to avoid returning the
  40. * RNG state as-is, the result is XORed with additional
  41. * WDEV_RND_REG reads while waiting.
  42. */
  43. /* This code does not run in a critical section, so CPU frequency switch may
  44. * happens while this code runs (this will not happen in the current
  45. * implementation, but possible in the future). However if that happens,
  46. * the number of cycles spent on frequency switching will certainly be more
  47. * than the number of cycles we need to wait here.
  48. */
  49. uint32_t cpu_to_apb_freq_ratio = esp_clk_cpu_freq() / esp_clk_apb_freq();
  50. static uint32_t last_ccount = 0;
  51. uint32_t ccount;
  52. uint32_t result = 0;
  53. do {
  54. ccount = cpu_hal_get_cycle_count();
  55. result ^= REG_READ(WDEV_RND_REG);
  56. } while (ccount - last_ccount < cpu_to_apb_freq_ratio * APB_CYCLE_WAIT_NUM);
  57. last_ccount = ccount;
  58. return result ^ REG_READ(WDEV_RND_REG);
  59. }
  60. void esp_fill_random(void *buf, size_t len)
  61. {
  62. assert(buf != NULL);
  63. uint8_t *buf_bytes = (uint8_t *)buf;
  64. while (len > 0) {
  65. uint32_t word = esp_random();
  66. uint32_t to_copy = MIN(sizeof(word), len);
  67. memcpy(buf_bytes, &word, to_copy);
  68. buf_bytes += to_copy;
  69. len -= to_copy;
  70. }
  71. }