Skip to content

Commit

Permalink
drivers: entropy: ease runtime requirements on BT HCI
Browse files Browse the repository at this point in the history
On platforms like nrf5340 there are 2 CPUs:
- one is the cpu_net which takes care of the radio stuff and
  owns the HW random generator
- one is the cpu_app which holds application data and polls
  cpu_net through HCI commands when it needs some random data.

The PSA core implemented in Mbed TLS needs random data at initialization
time, which happens early in the boot process. If we wait for BT to
be ready before issuing the HCI command, then PSA core intialization
will fail. In facts there is no need for the BT to be completely
initialized just to ask for some random data from the cpu_app to
the cpu_net since the HW random generator will likely be already
functional in the cpu_net.
So let's just try the HCI command and, if something is not right,
it will fail anyway. There's no need to anticipate the failure.

Signed-off-by: Valerio Setti <[email protected]>
  • Loading branch information
valeriosetti committed Nov 11, 2024
1 parent ede521c commit 35b67a7
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions drivers/entropy/entropy_bt_hci.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ static int entropy_bt_init(const struct device *dev)
static int entropy_bt_get_entropy(const struct device *dev,
uint8_t *buffer, uint16_t length)
{
if (!bt_is_ready()) {
return -EAGAIN;
}
/* Do not wait for BT to be ready (i.e. bt_is_ready()) before issueing
* the command. The reason is that when crypto is enabled and the PSA
* Crypto API support is provided through Mbed TLS, random number generator
* needs to be available since the very first call to psa_crypto_init()
* which is usually done before BT is completely intialized.

Check warning on line 27 in drivers/entropy/entropy_bt_hci.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

TYPO_SPELLING

drivers/entropy/entropy_bt_hci.c:27 'intialized' may be misspelled - perhaps 'initialized'?
* On the other hand, in devices like the nrf5340, the crytographically
* secure RNG is owned by the cpu_net, so the cpu_app needs to poll it
* to get random data. Again, there is no need to wait for BT to be
* completely initalized for this kind of support. Just try to send the

Check warning on line 31 in drivers/entropy/entropy_bt_hci.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

TYPO_SPELLING

drivers/entropy/entropy_bt_hci.c:31 'initalized' may be misspelled - perhaps 'initialized'?
* request through HCI. If the command fails for any reason, then
* we return failure anyway. */

Check warning on line 33 in drivers/entropy/entropy_bt_hci.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

BLOCK_COMMENT_STYLE

drivers/entropy/entropy_bt_hci.c:33 Block comments use a trailing */ on a separate line

return bt_hci_le_rand(buffer, length);
}
Expand Down

0 comments on commit 35b67a7

Please sign in to comment.