-
Notifications
You must be signed in to change notification settings - Fork 3k
[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
[NRF52840]: TRNG support #4056
Conversation
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)) | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
codestyle nit ,
if (condition) {
}
How was this tested ? share test results pls if possible |
/morph test |
Result: SUCCESSYour command has finished executing! Here's what you wrote!
OutputAll builds and test passed! |
@0xc0170 Output from mbed TLS Authenticated Encryption example: DONE plaintext message: 536f6d65207468696e67732061726520626574746572206c65667420756e7265616400 DONE |
|
||
nrf_drv_rng_bytes_available(&bytes_available); | ||
|
||
if ((bytes_available < length) || (nrf_drv_rng_rand(output, length) == NRF_ERROR_NOT_FOUND)) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@yanesca Pleas review again |
/morph test |
@@ -42,29 +42,47 @@ | |||
|
|||
void trng_init(trng_t *obj) | |||
{ | |||
(void) obj; | |||
|
|||
(void)nrf_drv_rng_init(NULL); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Result: SUCCESSYour command has finished executing! Here's what you wrote!
OutputAll builds and test passed! |
Happy with the changes? |
@nvlsianpu Can you resolve the conflict? |
aeeb02e
to
9955701
Compare
I rebased it onto mbed-os-workshop-17q2. |
There was a problem hiding this 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; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, thanks
bump @RonEld Are you happy with this now? |
@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 |
There was a problem hiding this 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); |
There was a problem hiding this comment.
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....
HAL is not thread safe, thus a consumer needs to provide it |
@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 |
Well, as it says here:
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. |
There was a problem hiding this 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
Looks good to me |
Description
TRNG hal support for nRF52840 using nordic's TRNG peripheral.