sd_rand example

nRF SDK 17.1.0 with SD140 7.2.0

There is no example for how to use the sd_rand library. I've seen scant few posts that mention functions like sd_rand_application_bytes_available_get(), sd_rand_application_vector_get(), and the pool function, but there is no example for how to use this from start (in main) to finish (an array of randomly generated bytes). Could someone please show how this is done.

Parents
  • Hi JGusler,

    A quick search over the entire example folder showed me the following examples do use those functions:
    ANT Shared Channel - SDK nRF5 v17.1.0
    ANT Asynchronous Transmitter - nRF5 SDK v17.1.0
    However I have to admit it doesn't show a lot. It just shows a busy wait until the SoftDevice generate enough random bytes, and then get it. And in the examples' use case, they only need 1 byte each time.

    If your interest is only in getting some RNG, we also have the RNG Frontend Module. Here is the link to the documentation, which include a references to an example: nrf_crypto Frontend Module RNG

    If you must use sd_rand_* functions, could you please give some further requirement about your use cases here?
    Unfortunately I am having some backlog circumstances, so it might take more than 2 work days before I can cook up some proper example codes for you. But I will try when I get there.

    Best regards,

    Hieu

  • Okay I did find something elsewhere, and this worked for me:

    ret_val = sd_rand_application_bytes_available_get(&num_rand_bytes_available);
    APP_ERROR_CHECK(ret_val);

    while (num_rand_bytes_available < ENCRYPTION_IV_SIZE)
    {
        // wait for SD to acquire enough random numbers
        sd_rand_application_bytes_available_get(&num_rand_bytes_available);
    }

    ret_val = sd_rand_application_vector_get(iv, ENCRYPTION_IV_SIZE);
    APP_ERROR_CHECK(ret_val);

    Where num_rand_bytes_available is only an initialized uint8_t with no value prior to these calls and ENCRYPTION_IV_SIZE is just a uint8_t of 16 (for my application) and iv is a uint8_t array of 16 values. I have NO idea why this works, I tried to look under the hood at the bytes_available_get but this is all I can find:

    SVCALL(SD_RAND_APPLICATION_VECTOR_GET, uint32_t, sd_rand_application_vector_get(uint8_t * p_buff, uint8_t length));

    So to me this is still all hand wavey magic but it worked.

Reply
  • Okay I did find something elsewhere, and this worked for me:

    ret_val = sd_rand_application_bytes_available_get(&num_rand_bytes_available);
    APP_ERROR_CHECK(ret_val);

    while (num_rand_bytes_available < ENCRYPTION_IV_SIZE)
    {
        // wait for SD to acquire enough random numbers
        sd_rand_application_bytes_available_get(&num_rand_bytes_available);
    }

    ret_val = sd_rand_application_vector_get(iv, ENCRYPTION_IV_SIZE);
    APP_ERROR_CHECK(ret_val);

    Where num_rand_bytes_available is only an initialized uint8_t with no value prior to these calls and ENCRYPTION_IV_SIZE is just a uint8_t of 16 (for my application) and iv is a uint8_t array of 16 values. I have NO idea why this works, I tried to look under the hood at the bytes_available_get but this is all I can find:

    SVCALL(SD_RAND_APPLICATION_VECTOR_GET, uint32_t, sd_rand_application_vector_get(uint8_t * p_buff, uint8_t length));

    So to me this is still all hand wavey magic but it worked.

Children
  • Hi JGusler,

    Congrats on figuring it out on your own

    JGusler said:
    As I understand, I MUST use them if I am using a SoftDevice correct?

    No, you can use the RNG peripheral I showed you just fine.

    JGusler said:

    I have NO idea why this works, I tried to look under the hood at the bytes_available_get but this is all I can find:

    SVCALL(SD_RAND_APPLICATION_VECTOR_GET, uint32_t, sd_rand_application_vector_get(uint8_t * p_buff, uint8_t length));

    It calls a function on the SoftDevice, which source code you don't (and won't) have access to in your project. From a out-of-the-black-box perspective, I guess calling it hand-wavey magic is fair Joy.

    So, behind the smoke and mirrors, it works by entropy generated by the SoftDevice interaction with the physical world. As the SoftDevice continues to work, it produced a pool of random bytes.

    Edit 2022.Oct.17: So, behind the smoke and mirrors, the SoftDevice also gets random numbers from the RNG peripheral. This peripheral generates true non-deterministic random numbers based on internal thermal noise. As the chip continues to work, it produces a pool of random bytes.
    You can check the available bytes in the pool using sd_rand_application_bytes_available_get(); and you consume the bytes using sd_rand_application_vector_get().

    Now that we know that I just want to add some input about your use of the functions:

    Happy coding!

    Hieu

Related