Skip to content

TRNG support for nRF5x devices #4588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
HITMAnsOFT opened this issue Jun 20, 2017 · 10 comments
Closed

TRNG support for nRF5x devices #4588

HITMAnsOFT opened this issue Jun 20, 2017 · 10 comments

Comments

@HITMAnsOFT
Copy link

  • Type: Enhancement

I am working on a BLE-based car alarm using nRF52832, and I need secure random key generation for a custom communication protocol. The nRF51x and nRF52x devices all contain a hardware RNG core, and the HAL and drivers are available in the Nordic SDK. However, the mbed OS doesn't seem to support trng_api for Nordic targets. I tried to directly call the Nordic HAL methods, but I found out that the corresponding files are missing from the TARGET_SDK11 folder. Those files (drivers_nrf/hal/nrf_rng.h, drivers_nrf/rng/nrf_drv_rng.h, drivers_nrf/rng/nrf_drv_rng.c) do exist in the TARGET_SDK13 folder, but for some reason it is not used with the nRF52832 device.

It would be nice to support TRNG HAL for nRF51 and nRF52 devices.

@pan-
Copy link
Member

pan- commented Jun 20, 2017

@nvlsianpu

@HITMAnsOFT
Copy link
Author

Update: I downloaded the Nordic SDK version 11 and copied the HAL and driver files mentioned above into my project source dir and was able to use the RNG module directly.

@nvlsianpu
Copy link
Contributor

@nvlsianpu have You a POC for look at?

@HITMAnsOFT
Copy link
Author

HITMAnsOFT commented Jun 27, 2017

@nvlsianpu I have this small C++ wrapper. It may not be robust and may cause an infinite loop, but it's working for me on nRF52832. The buffer depth of the RNG seems to be 32 bytes:

#include "nrf_drv_rng.h"

class Random {
public:
    Random ()
    {
        nrf_drv_rng_init(nullptr);
    }

    template <class T, size_t size> void getArray(std::array<T, size>& out)
    {
        for (T& a : out) {
            uint8_t available = 0;
            do {
                nrf_drv_rng_bytes_available(&available);
            } while (available < sizeof(T));
            nrf_drv_rng_rand(&a, sizeof(T)); 
        }
    }
};

@nvlsianpu
Copy link
Contributor

Thanks for the snipped. I wanted to know whether rng driver work out-off-the-box for nRF52. So answer is yes.

@HITMAnsOFT
Copy link
Author

Yes, it works. Chip code is QFAA B0. Unfortunately I was not able to run my applicaiton on nRF51. Do you happen to know if mbed works on Rev. 2 nRF51822 chips?

@nvlsianpu
Copy link
Contributor

mbed-os apps are supposed to work since rev. 3 of nRF51822. I never tested it with rev. 2

@HITMAnsOFT
Copy link
Author

I've ordered a couple of what seems to be Rev 3 nRF51822 modules. Will test this code on them when I get them.

@dlfryar-zz
Copy link
Contributor

Mbed OS SDK updates happening here

@marcuschangarm
Copy link
Contributor

New TRNG driver has been merged to master: #6116

@0xc0170 0xc0170 closed this as completed May 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants