Skip to content

Random Number Generator support #3045

@nuket

Description

@nuket

Description

  • Type: Enhancement
  • Priority: Major

Target
Nordic nRF5x and any other chip with hardware RNG support


Enhancement

Are there any plans to design / implement an mbed OS API to access the Random Number Generator (RNG) available on Nordic and other chips? I'm trying to seed the mbed TLS CTR-DRBG implementation, but have no access to a good source of entropy. I've tried about all I can to use the hardware RNG unit, but nothing has worked, maybe someone from Nordic can jump in on this, maybe I'm forgetting to power up or enable the particular hardware unit.

Any attempt to perform write operations on the NRF_RNG register, as in the following code, causes the chip to infinitely reset immediately at start (as I try to use the RNG during the static initialization of a cryptographic Salt class):

// Use Nordic Random Number Generator hardware unit.
//
// FIXME: This should be run through mbed TLS CTR-DRBG,
//        there is no guarantee that the Nordic Hardware RNG is 
//        cryptographically strong.
void generate_random_data(uint8_t * output, uint8_t length)
{
    static bool                     initialized = false;
    static mbedtls_ctr_drbg_context context = {0};

    if (!initialized)
    {
        // POWER register is no longer present on nrF52, 
        // use START and STOP tasks.
        // NRF_RNG->POWER    = 1;

        // Disable interrupt on VALRDY.
        NRF_RNG->INTENCLR = 1;

        // mbedtls_ctr_drbg_init(&context);
        // mbedtls_ctr_drbg_seed(&context, nullptr, nullptr, nullptr 0);

        initialized = true;
    }

    // NRF_RNG->TASKS_START   = 1;  // Start RNG
    // NRF_RNG->CONFIG        = 1;  // Enable bias correction.

    for (uint8_t i = 0; i < length; i++)
    {
        // Trying to write this value on an nRF52 causes immediate reset.
        // NRF_RNG->EVENTS_VALRDY = 0;

        // Wait until the value ready event is generated.
        // Roughly 120us, when bias correction is enabled.
        // while ( !NRF_RNG->EVENTS_VALRDY );

        nrf_delay_us(250);

        output[i] = (uint8_t) NRF_RNG->VALUE;
    }

    // NRF_RNG->TASKS_STOP = 1;
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions