Implement different random for rp2040 (#3939)

This commit is contained in:
Jesse Hills 2022-10-25 17:59:10 +13:00 committed by GitHub
parent c3fba97b4c
commit 60717b074e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -23,6 +23,7 @@
#include <freertos/portmacro.h> #include <freertos/portmacro.h>
#elif defined(USE_RP2040) && defined(USE_WIFI) #elif defined(USE_RP2040) && defined(USE_WIFI)
#include <WiFi.h> #include <WiFi.h>
#include <hardware/structs/rosc.h>
#endif #endif
#ifdef USE_ESP32_IGNORE_EFUSE_MAC_CRC #ifdef USE_ESP32_IGNORE_EFUSE_MAC_CRC
@ -94,7 +95,12 @@ uint32_t random_uint32() {
#elif defined(USE_ESP8266) #elif defined(USE_ESP8266)
return os_random(); return os_random();
#elif defined(USE_RP2040) #elif defined(USE_RP2040)
return ((uint32_t) rand()) << 16 + ((uint32_t) rand()); uint32_t result = 0;
for (uint8_t i = 0; i < 32; i++) {
result <<= 1;
result |= rosc_hw->randombit;
}
return result;
#else #else
#error "No random source available for this configuration." #error "No random source available for this configuration."
#endif #endif
@ -107,7 +113,15 @@ bool random_bytes(uint8_t *data, size_t len) {
#elif defined(USE_ESP8266) #elif defined(USE_ESP8266)
return os_get_random(data, len) == 0; return os_get_random(data, len) == 0;
#elif defined(USE_RP2040) #elif defined(USE_RP2040)
return false; while (len-- != 0) {
uint8_t result = 0;
for (uint8_t i = 0; i < 8; i++) {
result <<= 1;
result |= rosc_hw->randombit;
}
*data++ = result;
}
return true;
#else #else
#error "No random source available for this configuration." #error "No random source available for this configuration."
#endif #endif