Bluetooth: controller: Use NRF RNG entropy device
Use the NRF RNG entropy device as the entropy device for bt_rand and controller internal functions when LLL is Nordic. Using an entropy source with a significant increase in stack usage will invalidate all stack size configurations in the system and lead to stack overflow issues. Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
This commit is contained in:
parent
406a33682a
commit
f756b79124
15 changed files with 65 additions and 57 deletions
|
@ -31,7 +31,7 @@ static int internal_rand(void *buf, size_t len)
|
|||
{
|
||||
/* Force using controller rand function. */
|
||||
#if defined(CONFIG_BT_CTLR) && defined(CONFIG_BT_HOST_CRYPTO)
|
||||
return util_rand(buf, len);
|
||||
return lll_csrand_get(buf, len);
|
||||
#else
|
||||
return bt_rand(buf, len);
|
||||
#endif
|
||||
|
|
|
@ -7,13 +7,15 @@
|
|||
#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_HCI_DRIVER)
|
||||
#define LOG_MODULE_NAME bt_ctlr_crypto
|
||||
#include "common/log.h"
|
||||
#include "../util/util.h"
|
||||
|
||||
#include "util/memq.h"
|
||||
|
||||
#include "hal/ecb.h"
|
||||
#include "lll.h"
|
||||
|
||||
int bt_rand(void *buf, size_t len)
|
||||
{
|
||||
return util_rand(buf, len);
|
||||
return lll_csrand_get(buf, len);
|
||||
}
|
||||
|
||||
int bt_encrypt_le(const uint8_t key[16], const uint8_t plaintext[16],
|
||||
|
|
|
@ -886,7 +886,7 @@ static void le_rand(struct net_buf *buf, struct net_buf **evt)
|
|||
rp = hci_cmd_complete(evt, sizeof(*rp));
|
||||
rp->status = 0x00;
|
||||
|
||||
util_rand(rp->rand, count);
|
||||
lll_csrand_get(rp->rand, count);
|
||||
}
|
||||
|
||||
static void le_read_supp_states(struct net_buf *buf, struct net_buf **evt)
|
||||
|
|
|
@ -461,7 +461,7 @@ static int hci_driver_open(void)
|
|||
|
||||
err = ll_init(&sem_prio_recv);
|
||||
if (err) {
|
||||
BT_ERR("LL initialization failed: %u", err);
|
||||
BT_ERR("LL initialization failed: %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#if defined(CONFIG_BT_CTLR_RX_PDU_META)
|
||||
#include "lll_meta.h"
|
||||
#endif /* CONFIG_BT_CTLR_RX_PDU_META */
|
||||
|
@ -335,6 +337,11 @@ int8_t lll_radio_tx_pwr_min_get(void);
|
|||
int8_t lll_radio_tx_pwr_max_get(void);
|
||||
int8_t lll_radio_tx_pwr_floor(int8_t tx_pwr_lvl);
|
||||
|
||||
int lll_csrand_get(void *buf, size_t len);
|
||||
int lll_csrand_isr_get(void *buf, size_t len);
|
||||
int lll_rand_get(void *buf, size_t len);
|
||||
int lll_rand_isr_get(void *buf, size_t len);
|
||||
|
||||
int ull_prepare_enqueue(lll_is_abort_cb_t is_abort_cb,
|
||||
lll_abort_cb_t abort_cb,
|
||||
struct lll_prepare_param *prepare_param,
|
||||
|
|
|
@ -131,8 +131,7 @@ int lll_init(void)
|
|||
int err;
|
||||
|
||||
/* Get reference to entropy device */
|
||||
dev_entropy =
|
||||
device_get_binding(DT_LABEL(DT_INST(0, nordic_nrf_rng)));
|
||||
dev_entropy = device_get_binding(DT_LABEL(DT_NODELABEL(rng)));
|
||||
if (!dev_entropy) {
|
||||
return -ENODEV;
|
||||
}
|
||||
|
@ -179,9 +178,24 @@ int lll_init(void)
|
|||
return 0;
|
||||
}
|
||||
|
||||
uint8_t lll_entropy_get(uint8_t len, void *rand)
|
||||
int lll_csrand_get(void *buf, size_t len)
|
||||
{
|
||||
return entropy_get_entropy_isr(dev_entropy, rand, len, 0);
|
||||
return entropy_get_entropy(dev_entropy, buf, len);
|
||||
}
|
||||
|
||||
int lll_csrand_isr_get(void *buf, size_t len)
|
||||
{
|
||||
return entropy_get_entropy_isr(dev_entropy, buf, len, 0);
|
||||
}
|
||||
|
||||
int lll_rand_get(void *buf, size_t len)
|
||||
{
|
||||
return entropy_get_entropy(dev_entropy, buf, len);
|
||||
}
|
||||
|
||||
int lll_rand_isr_get(void *buf, size_t len)
|
||||
{
|
||||
return entropy_get_entropy_isr(dev_entropy, buf, len, 0);
|
||||
}
|
||||
|
||||
int lll_reset(void)
|
||||
|
|
|
@ -23,7 +23,6 @@ uint32_t lll_evt_offset_get(struct evt_hdr *evt);
|
|||
uint32_t lll_preempt_calc(struct evt_hdr *evt, uint8_t ticker_id,
|
||||
uint32_t ticks_at_event);
|
||||
void lll_chan_set(uint32_t chan);
|
||||
uint8_t lll_entropy_get(uint8_t len, void *rand);
|
||||
void lll_isr_tx_status_reset(void);
|
||||
void lll_isr_rx_status_reset(void);
|
||||
void lll_isr_status_reset(void);
|
||||
|
|
|
@ -116,11 +116,9 @@ int lll_init(void)
|
|||
ARG_UNUSED(clk_k32);
|
||||
|
||||
/* Get reference to entropy device */
|
||||
dev_entropy =
|
||||
device_get_binding(DT_LABEL(DT_INST(0, openisa_rv32m1_trng)));
|
||||
dev_entropy = device_get_binding(DT_CHOSEN_ZEPHYR_ENTROPY_LABEL);
|
||||
if (!dev_entropy) {
|
||||
dev_entropy = NULL;
|
||||
/* return -ENODEV; */
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
/* Initialise LLL internals */
|
||||
|
@ -163,9 +161,23 @@ int lll_init(void)
|
|||
return 0;
|
||||
}
|
||||
|
||||
uint8_t lll_entropy_get(uint8_t len, void *rand)
|
||||
int lll_csrand_get(void *buf, size_t len)
|
||||
{
|
||||
return entropy_get_entropy(dev_entropy, buf, len);
|
||||
}
|
||||
|
||||
int lll_csrand_isr_get(void *buf, size_t len)
|
||||
{
|
||||
return entropy_get_entropy_isr(dev_entropy, buf, len, 0);
|
||||
}
|
||||
|
||||
int lll_rand_get(void *buf, size_t len)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lll_rand_isr_get(void *buf, size_t len)
|
||||
{
|
||||
/* entropy_get_entropy_isr(dev_entropy, rand, len, 0); */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -27,4 +27,3 @@ uint32_t lll_evt_offset_get(struct evt_hdr *evt);
|
|||
uint32_t lll_preempt_calc(struct evt_hdr *evt, uint8_t ticker_id,
|
||||
uint32_t ticks_at_event);
|
||||
void lll_chan_set(uint32_t chan);
|
||||
uint8_t lll_entropy_get(uint8_t len, void *rand);
|
||||
|
|
|
@ -1390,7 +1390,7 @@ static void ticker_cb(uint32_t ticks_at_expire, uint32_t remainder, uint16_t laz
|
|||
uint32_t random_delay;
|
||||
uint32_t ret;
|
||||
|
||||
lll_entropy_get(sizeof(random_delay), &random_delay);
|
||||
lll_rand_isr_get(&random_delay, sizeof(random_delay));
|
||||
random_delay %= ULL_ADV_RANDOM_DELAY;
|
||||
random_delay += 1;
|
||||
|
||||
|
|
|
@ -103,7 +103,7 @@ uint8_t ll_adv_sync_param_set(uint8_t handle, uint16_t interval, uint16_t flags)
|
|||
err = util_aa_le32(lll_sync->access_addr);
|
||||
LL_ASSERT(!err);
|
||||
|
||||
util_rand(lll_sync->crc_init, sizeof(lll_sync->crc_init));
|
||||
lll_csrand_get(lll_sync->crc_init, sizeof(lll_sync->crc_init));
|
||||
|
||||
lll_sync->latency_prepare = 0;
|
||||
lll_sync->latency_event = 0;
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
#include <stddef.h>
|
||||
#include <zephyr.h>
|
||||
#include <device.h>
|
||||
#include <drivers/entropy.h>
|
||||
#include <bluetooth/bluetooth.h>
|
||||
#include <sys/byteorder.h>
|
||||
|
||||
|
@ -169,8 +168,6 @@ static struct ll_conn conn_pool[CONFIG_BT_MAX_CONN];
|
|||
static struct ll_conn *conn_upd_curr;
|
||||
static void *conn_free;
|
||||
|
||||
static struct device *entropy;
|
||||
|
||||
struct ll_conn *ll_conn_acquire(void)
|
||||
{
|
||||
return mem_acquire(&conn_free);
|
||||
|
@ -607,11 +604,6 @@ int ull_conn_init(void)
|
|||
{
|
||||
int err;
|
||||
|
||||
entropy = device_get_binding(DT_CHOSEN_ZEPHYR_ENTROPY_LABEL);
|
||||
if (!entropy) {
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
err = init_reset();
|
||||
if (err) {
|
||||
return err;
|
||||
|
@ -3850,9 +3842,9 @@ static void enc_req_reused_send(struct ll_conn *conn, struct node_tx **tx)
|
|||
sizeof(pdu_ctrl_tx->llctrl.enc_req.skdm)));
|
||||
|
||||
/* NOTE: if not sufficient random numbers, ignore waiting */
|
||||
entropy_get_entropy_isr(entropy, pdu_ctrl_tx->llctrl.enc_req.skdm,
|
||||
sizeof(pdu_ctrl_tx->llctrl.enc_req.skdm) +
|
||||
sizeof(pdu_ctrl_tx->llctrl.enc_req.ivm), 0);
|
||||
lll_csrand_isr_get(pdu_ctrl_tx->llctrl.enc_req.skdm,
|
||||
sizeof(pdu_ctrl_tx->llctrl.enc_req.skdm) +
|
||||
sizeof(pdu_ctrl_tx->llctrl.enc_req.ivm));
|
||||
|
||||
ctrl_tx_enqueue(conn, *tx);
|
||||
|
||||
|
@ -3887,9 +3879,9 @@ static int enc_rsp_send(struct ll_conn *conn)
|
|||
sizeof(pdu_ctrl_tx->llctrl.enc_rsp.skds)));
|
||||
|
||||
/* NOTE: if not sufficient random numbers, ignore waiting */
|
||||
entropy_get_entropy_isr(entropy, pdu_ctrl_tx->llctrl.enc_rsp.skds,
|
||||
sizeof(pdu_ctrl_tx->llctrl.enc_rsp.skds) +
|
||||
sizeof(pdu_ctrl_tx->llctrl.enc_rsp.ivs), 0);
|
||||
lll_csrand_isr_get(pdu_ctrl_tx->llctrl.enc_rsp.skds,
|
||||
sizeof(pdu_ctrl_tx->llctrl.enc_rsp.skds) +
|
||||
sizeof(pdu_ctrl_tx->llctrl.enc_rsp.ivs));
|
||||
|
||||
/* things from slave stored for session key calculation */
|
||||
memcpy(&conn->llcp.encryption.skd[8],
|
||||
|
|
|
@ -149,7 +149,7 @@ uint8_t ll_create_connection(uint16_t scan_interval, uint16_t scan_window,
|
|||
err = util_aa_le32(conn_lll->access_addr);
|
||||
LL_ASSERT(!err);
|
||||
|
||||
util_rand(conn_lll->crc_init, sizeof(conn_lll->crc_init));
|
||||
lll_csrand_get(conn_lll->crc_init, sizeof(conn_lll->crc_init));
|
||||
|
||||
conn_lll->handle = 0xFFFF;
|
||||
conn_lll->interval = interval;
|
||||
|
@ -205,7 +205,7 @@ uint8_t ll_create_connection(uint16_t scan_interval, uint16_t scan_window,
|
|||
conn_lll->event_counter = 0;
|
||||
|
||||
conn_lll->data_chan_count = ull_chan_map_get(conn_lll->data_chan_map);
|
||||
util_rand(&hop, sizeof(uint8_t));
|
||||
lll_csrand_get(&hop, sizeof(uint8_t));
|
||||
conn_lll->data_chan_hop = 5 + (hop % 12);
|
||||
conn_lll->data_chan_sel = 0;
|
||||
conn_lll->data_chan_use = 0;
|
||||
|
@ -492,8 +492,8 @@ uint8_t ll_enc_req_send(uint16_t handle, uint8_t const *const rand,
|
|||
memcpy(enc_req->rand, rand, sizeof(enc_req->rand));
|
||||
enc_req->ediv[0] = ediv[0];
|
||||
enc_req->ediv[1] = ediv[1];
|
||||
util_rand(enc_req->skdm, sizeof(enc_req->skdm));
|
||||
util_rand(enc_req->ivm, sizeof(enc_req->ivm));
|
||||
lll_csrand_get(enc_req->skdm, sizeof(enc_req->skdm));
|
||||
lll_csrand_get(enc_req->ivm, sizeof(enc_req->ivm));
|
||||
} else if (conn->lll.enc_rx && conn->lll.enc_tx) {
|
||||
memcpy(&conn->llcp_enc.rand[0], rand,
|
||||
sizeof(conn->llcp_enc.rand));
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
#include <drivers/entropy.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "util/memq.h"
|
||||
#include "lll.h"
|
||||
|
||||
#include "pdu.h"
|
||||
|
||||
|
@ -42,24 +44,6 @@ uint8_t util_ones_count_get(uint8_t *octets, uint8_t octets_len)
|
|||
return one_count;
|
||||
}
|
||||
|
||||
int util_rand(void *buf, size_t len)
|
||||
{
|
||||
static struct device *dev;
|
||||
|
||||
if (unlikely(!dev)) {
|
||||
/* Only one entropy device exists, so this is safe even
|
||||
* if the whole operation isn't atomic.
|
||||
*/
|
||||
dev = device_get_binding(DT_CHOSEN_ZEPHYR_ENTROPY_LABEL);
|
||||
__ASSERT((dev != NULL),
|
||||
"Device driver for %s (DT_CHOSEN_ZEPHYR_ENTROPY_LABEL) not found. "
|
||||
"Check your build configuration!",
|
||||
DT_CHOSEN_ZEPHYR_ENTROPY_LABEL);
|
||||
}
|
||||
|
||||
return entropy_get_entropy(dev, (uint8_t *)buf, len);
|
||||
}
|
||||
|
||||
/** @brief Prepare access address as per BT Spec.
|
||||
*
|
||||
* - It shall have no more than six consecutive zeros or ones.
|
||||
|
@ -97,7 +81,7 @@ again:
|
|||
}
|
||||
retry--;
|
||||
|
||||
util_rand(dst, sizeof(uint32_t));
|
||||
lll_csrand_get(dst, sizeof(uint32_t));
|
||||
aa = sys_get_le32(dst);
|
||||
|
||||
bit_idx = 31U;
|
||||
|
|
|
@ -16,5 +16,4 @@
|
|||
#endif
|
||||
|
||||
uint8_t util_ones_count_get(uint8_t *octets, uint8_t octets_len);
|
||||
int util_rand(void *buf, size_t len);
|
||||
int util_aa_le32(uint8_t *dst);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue