The ESP32 microcontroller includes a built-in true hardware random number generator (RNG), which is cryptographically secure and suitable for most purposes involving encryption, secure token generation, etc.
✅ Summary: Use the Built-in Hardware RNG
- Native support for RNG via a true hardware random number generator.
- Accessible via the
esp_random() function in the ESP-IDF (Espressif IoT Development Framework).
- Returns high-entropy 32-bit random numbers.
- Cryptographically secure (based on the ESP32’s internal TRNG).
✅ Option 1: Using ESP32’s Built-in RNG (Recommended)
The simplest, high-quality source of cryptographically secure randomness on the ESP32 is the native TRNG.
In ESP-IDF (C/C++):
You can use the ESP-IDF's esp_random() function:
#include "esp_system.h"
uint32_t rand_num = esp_random();
Notes:
- This function interfaces with the hardware RNG.
- It’s fast and typically returns unbiased, high-entropy values.
✅ Option 2: In Arduino (Using the Espressif Core)
If you're using Arduino with ESP32 (Espressif's board support package), there's also support for the hardware RNG.
uint32_t randNumber = esp_random();
You can also use:
uint32_t randNumber = (uint32_t)random(0xFFFFFFFF);
…but this may not be cryptographically secure depending on how your Arduino core is configured. Prefer esp_random().
🛡 Entropy Source Details
The ESP32's RNG is documented to:
- Use background RF noise + internal jitter sources.
- Be designed with cryptographic applications in mind.
- Seed the internal PRNG used in secure communications like WiFi encryption.
Refer to Espressif’s official TRM (Technical Reference Manual) for deeper implementation info, specifically the Digital Random Number Generator (DRNG) module.
💡 Optional: Adding an External Hardware RNG (e.g., TRNG via Circuit)
If you're extremely entropy-conscious or wish to supplement the ESP32 RNG for ultra-secure applications (or distrust the manufacturer-provided RNG), you can use an external hardware entropy source, such as:
▶️ Option: Use the Avalanche Noise Source Circuit
You can build a simple true-RNG circuit using an avalanche diode or reverse-biased transistor, and sample its noise via the ESP32's ADC.
Example Components:
- 2N3904 in reverse-bias avalanche mode
- High-value resistor (~10k–100kΩ)
- Low-pass filter
- Optional: Zener diode for stability
- Feed this into an ESP32 ADC pin
ESP32 ADC Caveat:
ESP32’s ADC is somewhat noisy and nonlinear, so you may want to do:
- Oversampling
- Bias correction
- Von Neumann debiasing
- SHA256 whitening
But for many DIY security projects, this will provide extra entropy.
🔐 Extra Tips (Whitening and Pooling):
If you're generating secure keys or seeds:
- Use SHA-256 or a CSPRNG like CTR_DRBG or HMAC_DRBG seeded from
esp_random().
- Libraries like mbedTLS (included with ESP-IDF) give access to secure PRNG interfaces.
Example:
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/entropy.h"
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_entropy_init(&entropy);
mbedtls_ctr_drbg_init(&ctr_drbg);
mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0);
uint8_t buf[32];
mbedtls_ctr_drbg_random(&ctr_drbg, buf, 32); // 32 bytes CSPRNG
This method creates a cryptographic stream with well-defined security properties.
✅ Conclusion
- The ESP32 already has a true hardware RNG, accessible via
esp_random(). This is cryptographically secure, and fine for most applications.
- If you’re paranoid or need extra entropy, consider a reverse-biased transistor noise source and use the ADC.
- Always whiten/condition raw noise sources before using them in secure applications.
- For cryptographic use, combine entropy with a DRBG (e.g., via mbedTLS), seeded from
esp_random() or your own entropy source.
🔗 Resources
Let me know if you want help building an external entropy circuit or need to verify quality of randomness via testing tools like NIST STS or Dieharder.