Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIHugging Face LMArena Human Preference 140K

How do I get a cryptographically secure pseudorandom number generator on an ESP32? Or if…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with chatgpt-4o-latest-20250326’s answer.
  1. User

    How do I get a cryptographically secure pseudorandom number generator on an ESP32? Or if available, a true hardware RNG? I'm willing to build my own circuits to improve quality if it's just a few parts.

  2. Assistant

    The ESP32 actually has a built-in hardware random number generator (HRNG) that provides true random numbers based on noise sources inside the chip. This is generally considered cryptographically secure and is the best starting point for generating secure random numbers on the ESP32.

    Using the ESP32's Built-in Hardware RNG

    The ESP-IDF (Espressif IoT Development Framework) and Arduino core for ESP32 both provide APIs to access this hardware RNG.

    In ESP-IDF:

    You can use the function:

    uint32_t esp_random(void);
    

    This returns a 32-bit random number generated by the hardware RNG.

    Example:

    #include "esp_system.h"
    
    void app_main() {
        uint32_t rand_num = esp_random();
        printf("Random number: %u\n", rand_num);
    }
    

    In Arduino Core for ESP32:

    You can call:

    uint32_t rand_num = esp_random();
    

    This is a true hardware RNG, not just a PRNG seeded from a fixed source.


    Cryptographically Secure PRNG (CSPRNG)

    If you want a cryptographically secure PRNG seeded from the hardware RNG, you can use the hardware RNG output as entropy input to a CSPRNG like AES-CTR DRBG or HMAC-DRBG.

    • The ESP-IDF provides mbedTLS, which includes CSPRNG implementations.
    • You can seed mbedTLS’s DRBG with esp_random() output.

    Example (ESP-IDF):

    #include "mbedtls/ctr_drbg.h"
    #include "mbedtls/entropy.h"
    #include "esp_system.h"
    
    mbedtls_ctr_drbg_context ctr_drbg;
    mbedtls_entropy_context entropy;
    
    void init_rng() {
        mbedtls_entropy_init(&entropy);
        mbedtls_ctr_drbg_init(&ctr_drbg);
    
        // Seed the DRBG with entropy from esp_random()
        unsigned char seed[32];
        for (int i = 0; i < 8; i++) {
            uint32_t r = esp_random();
            memcpy(seed + i*4, &r, 4);
        }
    
        mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, seed, sizeof(seed));
    }
    
    uint32_t get_secure_random() {
        uint32_t output;
        mbedtls_ctr_drbg_random(&ctr_drbg, (unsigned char*)&output, sizeof(output));
        return output;
    }
    

    Building Your Own Hardware RNG Circuit

    If you want to improve or supplement the ESP32’s RNG with your own hardware RNG, here are some common approaches:

    • Noise diode or Zener diode noise source: Amplify the noise from a reverse-biased diode, then sample it with an ADC or comparator.
    • Reverse-biased transistor junction noise: Similar to diode noise.
    • Avalanche noise from a transistor or diode: Generates high-quality noise.
    • Ring oscillator jitter: Use multiple ring oscillators and sample their jitter.

    You would then feed this raw noise into the ESP32 ADC or digital input, collect entropy, and run it through a whitening function or a CSPRNG to get uniform random bits.

    Example simple circuit:

    • Reverse-biased Zener diode (e.g., 5.1V) connected to a high-gain amplifier.
    • Output fed into an ADC input pin on the ESP32.
    • Sample ADC values at high rate, extract least significant bits.
    • Use a hash or a CSPRNG to distill entropy.

    Summary

    • Best and easiest: Use esp_random() — the ESP32’s built-in hardware RNG.
    • For cryptographic applications, seed a CSPRNG (like mbedTLS CTR-DRBG) with esp_random().
    • If you want to build your own hardware RNG, use a noise diode or transistor noise source, amplify it, sample with ADC, and post-process.
    • Always run raw noise through a cryptographic hash or CSPRNG to ensure uniformity and unpredictability.

    If you want, I can help you with example code for Arduino or ESP-IDF, or a schematic for a simple noise source circuit!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.