Skip to content

Commit 154f5c0

Browse files
LingaoMcarlescufi
authored andcommitted
Bluetooth: host: crypto: Add a config to select how rand is generated
Considering that in most scenarios, bt_rand will not be called frequently, but the current implementation of tinycrypt will occupy more than 300 bytes of RAM space. Its existence is to optimize the frequent call of bt_rand. Therefore, it is considered to put it into a config (`BT_HOST_CRYPTO_RANDOM`), when this config has been selected, will use tinycrypt library for random. Otherwise will call bt random hci command. Signed-off-by: Lingao Meng <[email protected]>
1 parent 989b71b commit 154f5c0

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

subsys/bluetooth/host/Kconfig

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,29 @@ rsource "../mesh/Kconfig"
114114
rsource "../audio/Kconfig"
115115

116116
config BT_HOST_CRYPTO
117-
# Hidden option that compiles in random number generation and AES
118-
# encryption support using TinyCrypt library if this is not provided
119-
# by the controller implementation.
117+
# Hidden option that compiles in AES encryption support using TinyCrypt
118+
# library if this is not provided by the controller implementation.
120119
bool
121120
default y if !BT_CTLR_CRYPTO
122121
select TINYCRYPT
123122
select TINYCRYPT_AES
123+
124+
config BT_HOST_CRYPTO_PRNG
125+
bool "Use Tinycrypt library for random number generation"
126+
default y
124127
select TINYCRYPT_SHA256
125128
select TINYCRYPT_SHA256_HMAC
126129
select TINYCRYPT_SHA256_HMAC_PRNG
130+
depends on BT_HOST_CRYPTO
131+
help
132+
When selected, will use tinycrypt library for random number generation.
133+
This will consume additional ram, but may speed up the generation of random
134+
numbers.
135+
136+
Otherwise, random numbers will be generated through multiple HCI calls,
137+
which will not consume additional resources, but may take a long time,
138+
depending on the length of the random data.
139+
This method is generally recommended within 16 bytes.
127140

128141
config BT_SETTINGS
129142
bool "Store Bluetooth state and configuration persistently"

subsys/bluetooth/host/crypto.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ int prng_init(void)
9494
return prng_reseed(&prng);
9595
}
9696

97+
#if defined(CONFIG_BT_HOST_CRYPTO_PRNG)
9798
int bt_rand(void *buf, size_t len)
9899
{
99100
int ret;
@@ -114,6 +115,44 @@ int bt_rand(void *buf, size_t len)
114115

115116
return -EIO;
116117
}
118+
#else /* !CONFIG_BT_HOST_CRYPTO_PRNG */
119+
int bt_rand(void *buf, size_t len)
120+
{
121+
int ret, size;
122+
size_t i = 0;
123+
124+
/* Check first that HCI_LE_Rand is supported */
125+
if (!BT_CMD_TEST(bt_dev.supported_commands, 27, 7)) {
126+
return -ENOTSUP;
127+
}
128+
129+
while (len) {
130+
struct bt_hci_rp_le_rand *rp;
131+
struct net_buf *rsp;
132+
133+
ret = bt_hci_cmd_send_sync(BT_HCI_OP_LE_RAND, NULL, &rsp);
134+
if (ret) {
135+
return ret;
136+
}
137+
138+
rp = (void *)rsp->data;
139+
if (rp->status) {
140+
return -EIO;
141+
}
142+
143+
size = MIN(len, sizeof(rp->rand));
144+
145+
(void)memcpy((uint8_t *)buf + i, rp->rand, size);
146+
147+
net_buf_unref(rsp);
148+
149+
i += size;
150+
len -= size;
151+
}
152+
153+
return 0;
154+
}
155+
#endif /* CONFIG_BT_HOST_CRYPTO_PRNG */
117156

118157
int bt_encrypt_le(const uint8_t key[16], const uint8_t plaintext[16],
119158
uint8_t enc_data[16])

subsys/bluetooth/host/hci_core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2655,7 +2655,7 @@ static int common_init(void)
26552655
read_supported_commands_complete(rsp);
26562656
net_buf_unref(rsp);
26572657

2658-
if (IS_ENABLED(CONFIG_BT_HOST_CRYPTO)) {
2658+
if (IS_ENABLED(CONFIG_BT_HOST_CRYPTO_PRNG)) {
26592659
/* Initialize the PRNG so that it is safe to use it later
26602660
* on in the initialization process.
26612661
*/

0 commit comments

Comments
 (0)