Skip to content

Commit

Permalink
entropy: bt_hci: driver added
Browse files Browse the repository at this point in the history
Adds an entropy driver that uses Bluetooth HCI commands as its source
of randomness. As this method is blocking, the ISR API is not supported.

As this method will range from relatively slow (same core Bluetooth HCI
controller) to extremely slow (UART HCI Bluetooth controller), use the
xoshiro PRNG by default for RNG generation.

Implements zephyrproject-rtos#37186

Signed-off-by: Jordan Yates <[email protected]>
  • Loading branch information
Jordan Yates authored and carlescufi committed Mar 23, 2022
1 parent 0140242 commit 80e7ad7
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@
/drivers/eeprom/ @henrikbrixandersen
/drivers/eeprom/eeprom_stm32.c @KwonTae-young
/drivers/entropy/*b91* @yurvyn
/drivers/entropy/*bt_hci* @JordanYates
/drivers/entropy/*rv32m1* @dleach02
/drivers/entropy/*gecko* @chrta
/drivers/entropy/*litex* @mateusz-holenko @kgugala @pgielda
Expand Down
1 change: 1 addition & 0 deletions drivers/entropy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ zephyr_library_sources_ifdef(CONFIG_USERSPACE entropy_handlers.
zephyr_library_sources_ifdef(CONFIG_ENTROPY_RV32M1_TRNG entropy_rv32m1_trng.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_GECKO_TRNG entropy_gecko_trng.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_NEORV32_TRNG entropy_neorv32_trng.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_BT_HCI entropy_bt_hci.c)
1 change: 1 addition & 0 deletions drivers/entropy/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ source "drivers/entropy/Kconfig.rv32m1"
source "drivers/entropy/Kconfig.litex"
source "drivers/entropy/Kconfig.gecko"
source "drivers/entropy/Kconfig.neorv32"
source "drivers/entropy/Kconfig.bt_hci"

config ENTROPY_HAS_DRIVER
bool
Expand Down
16 changes: 16 additions & 0 deletions drivers/entropy/Kconfig.bt_hci
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2022, Commonwealth Scientific and Industrial Research
# Organisation (CSIRO) ABN 41 687 119 230.
# SPDX-License-Identifier: Apache-2.0

config ENTROPY_BT_HCI
bool "Bluetooth HCI RNG driver"
depends on BT_HCI
select ENTROPY_HAS_DRIVER
help
Enable Random Number Generator from a Bluetooth HCI device.

# Don't use use Bluetooth HCI as a random source since it will be slow.
# Instead, use the software implemented xoshiro RNG.
choice RNG_GENERATOR_CHOICE
default XOSHIRO_RANDOM_GENERATOR if ENTROPY_BT_HCI
endchoice
61 changes: 61 additions & 0 deletions drivers/entropy/entropy_bt_hci.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2022, Commonwealth Scientific and Industrial Research
* Organisation (CSIRO) ABN 41 687 119 230.
*
* SPDX-License-Identifier: Apache-2.0
*/

#define DT_DRV_COMPAT zephyr_bt_hci_entropy

#include <drivers/entropy.h>
#include <bluetooth/hci.h>
#include <string.h>

static int entropy_bt_init(const struct device *dev)
{
/* Nothing to do */
return 0;
}

static int entropy_bt_get_entropy(const struct device *dev,
uint8_t *buffer, uint16_t length)
{
struct bt_hci_rp_le_rand *rp;
struct net_buf *rsp;
int req, ret;

if (!bt_is_ready()) {
return -EAGAIN;
}

while (length > 0) {
/* Number of bytes to fill on this iteration */
req = MIN(length, sizeof(rp->rand));
/* Request the next 8 bytes over HCI */
ret = bt_hci_cmd_send_sync(BT_HCI_OP_LE_RAND, NULL, &rsp);
if (ret) {
return ret;
}
/* Copy random data into buffer */
rp = (void *)rsp->data;
memcpy(buffer, rp->rand, req);
buffer += req;
length -= req;
}
return 0;
}

/* HCI commands cannot be run from an interrupt context */
static const struct entropy_driver_api entropy_bt_api = {
.get_entropy = entropy_bt_get_entropy,
.get_entropy_isr = NULL
};

#define ENTROPY_BT_HCI_INIT(inst) \
DEVICE_DT_INST_DEFINE(inst, entropy_bt_init, \
NULL, NULL, NULL, \
PRE_KERNEL_1, \
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
&entropy_bt_api);

DT_INST_FOREACH_STATUS_OKAY(ENTROPY_BT_HCI_INIT)
10 changes: 10 additions & 0 deletions dts/bindings/bluetooth/zephyr,bt-hci-entropy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2018, I-SENSE group of ICCS
# SPDX-License-Identifier: Apache-2.0

description: |
Bluetooth module that uses Zephyr's Bluetooth Host Controller Interface as
an entropy source
compatible: "zephyr,bt-hci-entropy"

include: base.yaml

0 comments on commit 80e7ad7

Please sign in to comment.