1 /*
2 * Copyright (c) 2022, Commonwealth Scientific and Industrial Research
3 * Organisation (CSIRO) ABN 41 687 119 230.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #define DT_DRV_COMPAT zephyr_bt_hci_entropy
9
10 #include <zephyr/drivers/entropy.h>
11 #include <zephyr/bluetooth/hci.h>
12 #include <string.h>
13
entropy_bt_init(const struct device * dev)14 static int entropy_bt_init(const struct device *dev)
15 {
16 /* Nothing to do */
17 return 0;
18 }
19
entropy_bt_get_entropy(const struct device * dev,uint8_t * buffer,uint16_t length)20 static int entropy_bt_get_entropy(const struct device *dev,
21 uint8_t *buffer, uint16_t length)
22 {
23 /* Do not wait for BT to be ready (i.e. bt_is_ready()) before issueing
24 * the command. The reason is that when crypto is enabled and the PSA
25 * Crypto API support is provided through Mbed TLS, random number generator
26 * needs to be available since the very first call to psa_crypto_init()
27 * which is usually done before BT is completely initialized.
28 * On the other hand, in devices like the nrf5340, the crytographically
29 * secure RNG is owned by the cpu_net, so the cpu_app needs to poll it
30 * to get random data. Again, there is no need to wait for BT to be
31 * completely initialized for this kind of support. Just try to send the
32 * request through HCI. If the command fails for any reason, then
33 * we return failure anyway.
34 */
35
36 return bt_hci_le_rand(buffer, length);
37 }
38
39 /* HCI commands cannot be run from an interrupt context */
40 static DEVICE_API(entropy, entropy_bt_api) = {
41 .get_entropy = entropy_bt_get_entropy,
42 .get_entropy_isr = NULL
43 };
44
45 #define ENTROPY_BT_HCI_INIT(inst) \
46 DEVICE_DT_INST_DEFINE(inst, entropy_bt_init, \
47 NULL, NULL, NULL, \
48 PRE_KERNEL_1, \
49 CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
50 &entropy_bt_api);
51
52 DT_INST_FOREACH_STATUS_OKAY(ENTROPY_BT_HCI_INIT)
53