forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
0140242
commit 80e7ad7
Showing
6 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |