This is an automated email from the ASF dual-hosted git repository.
andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git
The following commit(s) were added to refs/heads/master by this push:
new e7006a8e nimble/ll: Add API to read random data from controller
e7006a8e is described below
commit e7006a8e780fa8aed8ba5a2cfe3bd824ea87e8fd
Author: Andrzej Kaczmarek <[email protected]>
AuthorDate: Tue Oct 11 14:22:52 2022 +0200
nimble/ll: Add API to read random data from controller
This may be useful to get some random if MCU does not support trng, e.g.
as on nRF5340 app core.
---
apps/bttester/src/gap.c | 2 +-
nimble/host/include/host/ble_hs_hci.h | 15 +++++++++++++++
nimble/host/mesh/src/glue.c | 2 +-
nimble/host/src/ble_hs_hci_priv.h | 1 -
nimble/host/src/ble_hs_hci_util.c | 2 +-
nimble/host/src/ble_hs_id.c | 2 +-
nimble/host/src/ble_sm.c | 10 +++++-----
nimble/host/src/ble_sm_alg.c | 2 +-
nimble/host/src/ble_sm_sc.c | 2 +-
9 files changed, 26 insertions(+), 12 deletions(-)
diff --git a/apps/bttester/src/gap.c b/apps/bttester/src/gap.c
index 24ef4ab3..aee9591c 100644
--- a/apps/bttester/src/gap.c
+++ b/apps/bttester/src/gap.c
@@ -837,7 +837,7 @@ static void auth_passkey_display(uint16_t conn_handle,
unsigned int passkey)
return;
}
- rc = ble_hs_hci_util_rand(&pk.passkey, sizeof(pk.passkey));
+ rc = ble_hs_hci_rand(&pk.passkey, sizeof(pk.passkey));
assert(rc == 0);
/* Max value is 999999 */
pk.passkey %= 1000000;
diff --git a/nimble/host/include/host/ble_hs_hci.h
b/nimble/host/include/host/ble_hs_hci.h
index e10b8e62..739eea92 100644
--- a/nimble/host/include/host/ble_hs_hci.h
+++ b/nimble/host/include/host/ble_hs_hci.h
@@ -87,6 +87,21 @@ int ble_hs_hci_read_chan_map(uint16_t conn_handle, uint8_t
*out_chan_map);
*/
int ble_hs_hci_set_chan_class(const uint8_t *chan_map);
+/**
+ * Reads random data into buffer from controller.
+ * This allows to use BLE controller as a source of true random data.
+ *
+ * @param dst Destination buffer.
+ * @param len Destination buffer length.
+ *
+ * @return 0 on success;
+ * A BLE host HCI return code if the controller
+ * rejected the request;
+ * A BLE host core return code on unexpected
+ * error.
+ */
+int ble_hs_hci_rand(void *dst, int len);
+
#ifdef __cplusplus
}
#endif
diff --git a/nimble/host/mesh/src/glue.c b/nimble/host/mesh/src/glue.c
index c00af947..f6f57f05 100644
--- a/nimble/host/mesh/src/glue.c
+++ b/nimble/host/mesh/src/glue.c
@@ -588,7 +588,7 @@ int
bt_rand(void *buf, size_t len)
{
int rc;
- rc = ble_hs_hci_util_rand(buf, len);
+ rc = ble_hs_hci_rand(buf, len);
if (rc != 0) {
return -1;
}
diff --git a/nimble/host/src/ble_hs_hci_priv.h
b/nimble/host/src/ble_hs_hci_priv.h
index 9ff4c6f0..356f3a53 100644
--- a/nimble/host/src/ble_hs_hci_priv.h
+++ b/nimble/host/src/ble_hs_hci_priv.h
@@ -98,7 +98,6 @@ void ble_hs_hci_set_phony_ack_cb(ble_hs_hci_phony_ack_fn *cb);
#endif
int ble_hs_hci_util_read_adv_tx_pwr(int8_t *out_pwr);
-int ble_hs_hci_util_rand(void *dst, int len);
int ble_hs_hci_util_read_rssi(uint16_t conn_handle, int8_t *out_rssi);
int ble_hs_hci_util_set_random_addr(const uint8_t *addr);
int ble_hs_hci_util_set_data_len(uint16_t conn_handle, uint16_t tx_octets,
diff --git a/nimble/host/src/ble_hs_hci_util.c
b/nimble/host/src/ble_hs_hci_util.c
index 14e55d52..11d0a77e 100644
--- a/nimble/host/src/ble_hs_hci_util.c
+++ b/nimble/host/src/ble_hs_hci_util.c
@@ -58,7 +58,7 @@ ble_hs_hci_util_read_adv_tx_pwr(int8_t *out_tx_pwr)
}
int
-ble_hs_hci_util_rand(void *dst, int len)
+ble_hs_hci_rand(void *dst, int len)
{
struct ble_hci_le_rand_rp rsp;
uint8_t *u8ptr;
diff --git a/nimble/host/src/ble_hs_id.c b/nimble/host/src/ble_hs_id.c
index bd50e201..2dd21aa7 100644
--- a/nimble/host/src/ble_hs_id.c
+++ b/nimble/host/src/ble_hs_id.c
@@ -41,7 +41,7 @@ ble_hs_id_gen_rnd(int nrpa, ble_addr_t *out_addr)
out_addr->type = BLE_ADDR_RANDOM;
- rc = ble_hs_hci_util_rand(out_addr->val, 6);
+ rc = ble_hs_hci_rand(out_addr->val, 6);
if (rc != 0) {
return rc;
}
diff --git a/nimble/host/src/ble_sm.c b/nimble/host/src/ble_sm.c
index 5188effe..6d781bd5 100644
--- a/nimble/host/src/ble_sm.c
+++ b/nimble/host/src/ble_sm.c
@@ -261,7 +261,7 @@ ble_sm_gen_pair_rand(uint8_t *pair_rand)
}
#endif
- rc = ble_hs_hci_util_rand(pair_rand, 16);
+ rc = ble_hs_hci_rand(pair_rand, 16);
if (rc != 0) {
return rc;
}
@@ -282,7 +282,7 @@ ble_sm_gen_ediv(struct ble_sm_master_id *master_id)
}
#endif
- rc = ble_hs_hci_util_rand(&master_id->ediv, sizeof master_id->ediv);
+ rc = ble_hs_hci_rand(&master_id->ediv, sizeof master_id->ediv);
if (rc != 0) {
return rc;
}
@@ -303,7 +303,7 @@ ble_sm_gen_master_id_rand(struct ble_sm_master_id
*master_id)
}
#endif
- rc = ble_hs_hci_util_rand(&master_id->rand_val, sizeof
master_id->rand_val);
+ rc = ble_hs_hci_rand(&master_id->rand_val, sizeof master_id->rand_val);
if (rc != 0) {
return rc;
}
@@ -325,7 +325,7 @@ ble_sm_gen_ltk(struct ble_sm_proc *proc, uint8_t *ltk)
}
#endif
- rc = ble_hs_hci_util_rand(ltk, proc->key_size);
+ rc = ble_hs_hci_rand(ltk, proc->key_size);
if (rc != 0) {
return rc;
}
@@ -350,7 +350,7 @@ ble_sm_gen_csrk(struct ble_sm_proc *proc, uint8_t *csrk)
}
#endif
- rc = ble_hs_hci_util_rand(csrk, 16);
+ rc = ble_hs_hci_rand(csrk, 16);
if (rc != 0) {
return rc;
}
diff --git a/nimble/host/src/ble_sm_alg.c b/nimble/host/src/ble_sm_alg.c
index 8b3326de..282a2b13 100644
--- a/nimble/host/src/ble_sm_alg.c
+++ b/nimble/host/src/ble_sm_alg.c
@@ -517,7 +517,7 @@ ble_sm_alg_rand(uint8_t *dst, unsigned int size)
size -= num;
}
#else
- if (ble_hs_hci_util_rand(dst, size)) {
+ if (ble_hs_hci_rand(dst, size)) {
return 0;
}
#endif
diff --git a/nimble/host/src/ble_sm_sc.c b/nimble/host/src/ble_sm_sc.c
index 162a4a2b..0cd2b2ee 100644
--- a/nimble/host/src/ble_sm_sc.c
+++ b/nimble/host/src/ble_sm_sc.c
@@ -893,7 +893,7 @@ ble_sm_sc_oob_generate_data(struct ble_sm_sc_oob_data
*oob_data)
return rc;
}
- rc = ble_hs_hci_util_rand(oob_data->r, 16);
+ rc = ble_hs_hci_rand(oob_data->r, 16);
if (rc) {
return rc;
}