entropy: bt_hci: driver added

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 #37186

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
This commit is contained in:
Jordan Yates 2021-12-23 09:27:19 +10:00 committed by Carles Cufí
parent 01402429cc
commit 80e7ad7a23
6 changed files with 90 additions and 0 deletions

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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