Skip to content

[NRF52840]: TRNG support #4056

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

Conversation

nvlsianpu
Copy link
Contributor

Description

TRNG hal support for nRF52840 using nordic's TRNG peripheral.

uint8_t bytes_available;
nrf_drv_rng_bytes_available(&bytes_available);
if ((bytes_available < length) || (nrf_drv_rng_rand(output, length) == NRF_ERROR_NOT_FOUND))
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

codestyle nit ,

if (condition) {

}

@0xc0170
Copy link
Contributor

0xc0170 commented Mar 28, 2017

How was this tested ? share test results pls if possible

@bridadan
Copy link
Contributor

/morph test

@mbed-bot
Copy link

Result: SUCCESS

Your command has finished executing! Here's what you wrote!

/morph test

Output

mbed Build Number: 1791

All builds and test passed!

@nvlsianpu
Copy link
Contributor Author

@0xc0170 Output from mbed TLS Authenticated Encryption example:
plaintext message: 536f6d65207468696e67732061726520626574746572206c65667420756e7265616400
ciphertext: 35f5665e10490f8d2c0f876a19dd521a7e40002ed8082cb05c755990e04354fd705ad86246f37b8dfcb5b64a99cba6576dcd724717279a98ac518d
decrypted: 536f6d65207468696e67732061726520626574746572206c65667420756e7265616400

DONE

plaintext message: 536f6d65207468696e67732061726520626574746572206c65667420756e7265616400
ciphertext: 2b6a4fff10260a627969b2c5de906c296f6e61661f15eb1690b89b77df3d1c1147805894f16686ed4370f5c12ecf93d34b75d5602e8176bdff2650
decrypted: 536f6d65207468696e67732061726520626574746572206c65667420756e7265616400

DONE


nrf_drv_rng_bytes_available(&bytes_available);

if ((bytes_available < length) || (nrf_drv_rng_rand(output, length) == NRF_ERROR_NOT_FOUND)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not necessary to return length bytes, it is perfectly fine to return with less. The length input variable only indicate the length of the output buffer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree. There is the indirect limit for the time of providing all of requested bytes for entropy module in mbedtls -> entropy.c, entropy.h. Nordic TRNG module is too slow and therefore it return 0 bytes to many times (this will get worse if nordic BLE stack is runing) for this condition.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I am not sure if I understand. I am just saying that if this function returns with some nonzero but less than length bytes, then it might be enough, mbed TLS won't poll until it gets length bytes of random data. The amount of bytes it needs is usually less.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was my more generic remark for mbed-os/mbedtls entropy implementation. The code in this PR didn't present this case.

@0xc0170
Copy link
Contributor

0xc0170 commented Apr 5, 2017

@yanesca Pleas review again

@0xc0170
Copy link
Contributor

0xc0170 commented Apr 5, 2017

/morph test

@@ -42,29 +42,47 @@

void trng_init(trng_t *obj)
{
(void) obj;

(void)nrf_drv_rng_init(NULL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NULL value passed in config pointer causes that rng peripheral will be configured without bias correction. Maybe we should consider to enable it to be sure about statistically uniform distribution of the random values? Of course enabling this function causes slow down generation of random numbers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice hint. Discussed out-of -band with @kl-cruz . We will keep this as is for this PR.

@mbed-bot
Copy link

mbed-bot commented Apr 5, 2017

Result: SUCCESS

Your command has finished executing! Here's what you wrote!

/morph test

Output

mbed Build Number: 1835

All builds and test passed!

@0xc0170
Copy link
Contributor

0xc0170 commented Apr 5, 2017

@yanesca Pleas review again

Happy with the changes?

@0xc0170
Copy link
Contributor

0xc0170 commented Apr 10, 2017

@yanesca @RonEld bump

@sg-
Copy link
Contributor

sg- commented Apr 10, 2017

@nvlsianpu Can you resolve the conflict?

@nvlsianpu nvlsianpu force-pushed the mbed-os-workshop-17q2-nordic-trng branch from aeeb02e to 9955701 Compare April 11, 2017 06:07
@nvlsianpu
Copy link
Contributor Author

I rebased it onto mbed-os-workshop-17q2.

Copy link
Contributor

@RonEld RonEld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added one small comment to verify with experts.
Assuming nrf_drv_rng_rand results in enough entropy, I approve this, but I would like @yanesca to review as well


if (bytes_available == 0) {
nrf_drv_rng_block_rand(output, 1);
*output_length = 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correct, this means gathering total one byte. This is not enough entropy.
the mbedtls_entropy_func will try to gather more later, so I am less worried about the threshold ( on expense of performance), I am more concerned about whether collecting one byte every time will result in enough entropy, considering that there is a trng_init and trng_free between every collection. Please verify with your experts

Copy link
Contributor Author

@nvlsianpu nvlsianpu Apr 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RonEld
I consulted this doubt. There ain't obstacles to using Nordic TRNG According to mentioned algorithm:

The RNG module produces 1 byte of random data per event. It is not correlated with previous data, so the entropy is not affected by the number of bytes gathered. However, it will obviously take more time to gather more bytes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, thanks

@theotherjimmy
Copy link
Contributor

bump @RonEld Are you happy with this now?

@RonEld
Copy link
Contributor

RonEld commented Apr 25, 2017

@theotherjimmy I have already approved this from my side. I only wanted my question answered, for verification, and I am happy with the answer. Keep in mind that I am not the mbed TLS gate keeper though

@0xc0170 0xc0170 merged commit 870aa34 into ARMmbed:mbed-os-workshop-17q2 Apr 25, 2017
Copy link

@andresag01 andresag01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TRNG implementation seems to be OK. I have the following questions:

  • What are the requirements of this implementation regarding thread safety?
  • What is the amount of entropy returned?
  • If we want to actually initialise the device in the trng_init() function, I think we need to add a return code to that function as it is currently not easy to check whether the initialisation call was successful or not.

{
(void) obj;

(void)nrf_drv_rng_init(NULL);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to check the return code for this function. The function does not have a return code though....

@0xc0170
Copy link
Contributor

0xc0170 commented May 24, 2017

What are the requirements of this implementation regarding thread safety?

HAL is not thread safe, thus a consumer needs to provide it

@andresag01
Copy link

@0xc0170: Thanks for the information, I think that slightly conflicts with the mbed TLS policy.

@0xc0170
Copy link
Contributor

0xc0170 commented May 24, 2017

@0xc0170: Thanks for the information, I think that slightly conflicts with the mbed TLS policy.

please verify that. we got mbed wrapper in mbedtls that should take care of it ! If not, we shall fix it

@andresag01
Copy link

Well, as it says here:

most functions use an explicit context. Most of the time, as long as this context is not shared between threads, you're safe. However, be aware that sometimes a context can be share indirectly, for example an SSL context can point to an RSA context (our private key).

Also, the mbed TLS wrapper around the TRNG API is here and its usage here is not guarded. However, this is for mbed TLS, I am not sure what the policy is when integrating with mbed OS.

Copy link

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me

@yanesca
Copy link
Contributor

yanesca commented Jun 1, 2017

Looks good to me

@c1728p9 c1728p9 mentioned this pull request Jun 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.