Skip to content

Commit 9931065

Browse files
zhongzhijie1hyson710
authored andcommitted
fix multi-controller ecc smp buiild_warning, use syswork during key generate, and adapt to external/app PSA crypto lib.
bug: v/76826 release note: Now all SMP public keys are generated by the host using the PSA crypto backend, not by the controller. This allows controllers without P-256 hardware support to use LE Secure Connections. Signed-off-by: zhongzhijie1 <[email protected]>
1 parent dc2e2c1 commit 9931065

File tree

4 files changed

+36
-33
lines changed

4 files changed

+36
-33
lines changed

subsys/bluetooth/host/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ endif # BT_DF
994994

995995
config BT_ECC
996996
bool
997-
select MBEDTLS if !BUILD_WITH_TFM
997+
select CRYPTO_MBEDTLS if !BUILD_WITH_TFM
998998
select MBEDTLS_PSA_CRYPTO_C if !BUILD_WITH_TFM
999999
select PSA_WANT_ALG_ECDH
10001000
select PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE

subsys/bluetooth/host/conn.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4523,6 +4523,8 @@ int bt_conn_init(struct bt_dev *hdev)
45234523

45244524
bt_att_init(hdev);
45254525

4526+
bt_ecc_init(hdev);
4527+
45264528
err = bt_smp_init(hdev);
45274529
if (err) {
45284530
return err;

subsys/bluetooth/host/ecc.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ static void generate_pub_key(struct k_work *work)
151151

152152
SYS_SLIST_FOR_EACH_CONTAINER(&hdev->pub_key_cb_slist, cb, node) {
153153
if (cb->func) {
154-
cb->func(err ? NULL : pub_key);
154+
cb->func(hdev, err ? NULL : hdev->pub_key);
155155
}
156156
}
157157

@@ -217,12 +217,12 @@ static void generate_dh_key(struct k_work *work)
217217
atomic_clear_bit(ecc->flags, PENDING_DHKEY);
218218

219219
if (err) {
220-
cb(NULL);
220+
cb(hdev, NULL);
221221
} else {
222222
uint8_t dhkey[BT_DH_KEY_LEN];
223223

224224
sys_memcpy_swap(dhkey, ecc->dhkey_be, sizeof(ecc->dhkey_be));
225-
cb(dhkey);
225+
cb(hdev, dhkey);
226226
}
227227
}
228228

@@ -236,7 +236,7 @@ int bt_pub_key_gen(struct bt_dev *hdev, struct bt_pub_key_cb *new_cb)
236236
if (IS_ENABLED(CONFIG_BT_USE_DEBUG_KEYS)) {
237237
atomic_set_bit(hdev->flags, BT_DEV_HAS_PUB_KEY);
238238
__ASSERT_NO_MSG(new_cb->func != NULL);
239-
new_cb->func(debug_public_key);
239+
new_cb->func(hdev, debug_public_key);
240240
return 0;
241241
}
242242

@@ -264,11 +264,7 @@ int bt_pub_key_gen(struct bt_dev *hdev, struct bt_pub_key_cb *new_cb)
264264

265265
atomic_clear_bit(hdev->flags, BT_DEV_HAS_PUB_KEY);
266266

267-
if (IS_ENABLED(CONFIG_BT_LONG_WQ)) {
268-
bt_long_wq_submit(&hdev->ecc.pub_key_work);
269-
} else {
270-
k_work_submit(&hdev->ecc.pub_key_work);
271-
}
267+
k_work_submit(&hdev->ecc.pub_key_work);
272268

273269
return 0;
274270
}
@@ -326,22 +322,27 @@ int bt_dh_key_gen(struct bt_dev *hdev, const uint8_t remote_pk[BT_PUB_KEY_LEN],
326322
sys_memcpy_swap(&hdev->ecc.public_key_be[BT_PUB_KEY_COORD_LEN],
327323
&remote_pk[BT_PUB_KEY_COORD_LEN], BT_PUB_KEY_COORD_LEN);
328324

329-
if (IS_ENABLED(CONFIG_BT_LONG_WQ)) {
330-
bt_long_wq_submit(&hdev->ecc.dh_key_work);
331-
} else {
332-
k_work_submit(&hdev->ecc.dh_key_work);
333-
}
325+
k_work_submit(&hdev->ecc.dh_key_work);
334326

335327
return 0;
336328
}
337329

338330
void bt_ecc_init(struct bt_dev *hdev)
339331
{
332+
psa_status_t status;
333+
334+
status = psa_crypto_init();
335+
336+
if (status != PSA_SUCCESS) {
337+
LOG_ERR("PSA Crypto init failed: %d", status);
338+
return;
339+
}
340+
340341
memset(&hdev->ecc, 0, sizeof(hdev->ecc));
341342

342343
/* Initialize the ECC work queue */
343-
k_work_init(hdev->ecc.pub_key_work, generate_pub_key);
344-
k_work_init(hdev->ecc.dh_key_work, generate_dh_key);
344+
k_work_init(&hdev->ecc.pub_key_work, generate_pub_key);
345+
k_work_init(&hdev->ecc.dh_key_work, generate_dh_key);
345346
}
346347

347348
#ifdef ZTEST_UNITTEST

subsys/bluetooth/host/smp.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ static bool ltk_derive_link_key_allowed(struct bt_smp *smp)
768768
}
769769

770770
/* Check whether it is has been bonded */
771-
link_key = bt_keys_find_link_key(&conn->le.dst.a);
771+
link_key = bt_keys_find_link_key(conn->hdev, &conn->le.dst.a);
772772
if (link_key == NULL) {
773773
return true;
774774
}
@@ -810,9 +810,9 @@ static void sc_derive_link_key(struct bt_smp *smp)
810810
}
811811

812812
/* Remove the bonding information */
813-
link_key = bt_keys_find_link_key(&conn->le.dst.a);
813+
link_key = bt_keys_find_link_key(conn->hdev, &conn->le.dst.a);
814814
if (link_key != NULL) {
815-
bt_keys_link_key_clear(link_key);
815+
bt_keys_link_key_clear(conn->hdev, link_key);
816816
}
817817

818818
/*
@@ -863,7 +863,7 @@ static void sc_derive_link_key(struct bt_smp *smp)
863863

864864
if (atomic_test_bit(smp->flags, SMP_FLAG_BOND)) {
865865
/* Store the link key */
866-
bt_keys_link_key_store(link_key);
866+
bt_keys_link_key_store(conn->hdev, link_key);
867867
}
868868
}
869869

@@ -892,16 +892,16 @@ static void smp_br_reset(struct bt_smp_br *smp)
892892
atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_REQ);
893893
}
894894

895-
static void smp_br_id_add_replace(struct bt_keys *keys)
895+
static void smp_br_id_add_replace(struct bt_dev *hdev, struct bt_keys *keys)
896896
{
897897
struct bt_keys *conflict;
898898

899899
/* Check whether key has been added to resolving list. */
900900
if (keys->state & BT_KEYS_ID_ADDED) {
901-
bt_id_del(keys);
901+
bt_id_del(hdev, keys);
902902
}
903903

904-
conflict = bt_id_find_conflict(keys);
904+
conflict = bt_id_find_conflict(hdev, keys);
905905
if (conflict != NULL) {
906906
int err;
907907

@@ -911,8 +911,8 @@ static void smp_br_id_add_replace(struct bt_keys *keys)
911911
__ASSERT_NO_MSG(!err);
912912
}
913913

914-
__ASSERT_NO_MSG(!bt_id_find_conflict(keys));
915-
bt_id_add(keys);
914+
__ASSERT_NO_MSG(!bt_id_find_conflict(hdev, keys));
915+
bt_id_add(hdev, keys);
916916
}
917917

918918
static void smp_pairing_br_complete(struct bt_smp_br *smp, uint8_t status)
@@ -949,7 +949,7 @@ static void smp_pairing_br_complete(struct bt_smp_br *smp, uint8_t status)
949949
struct bt_conn_auth_info_cb *listener, *next;
950950

951951
if (keys) {
952-
smp_br_id_add_replace(keys);
952+
smp_br_id_add_replace(conn->hdev, keys);
953953
}
954954

955955
if (bond_flag && keys) {
@@ -1074,10 +1074,10 @@ static void smp_br_derive_ltk(struct bt_smp_br *smp)
10741074
bt_addr_copy(&addr.a, &conn->br.dst);
10751075
addr.type = BT_ADDR_LE_PUBLIC;
10761076

1077-
keys = bt_keys_find_addr(conn->id, &addr);
1077+
keys = bt_keys_find_addr(conn->hdev, conn->id, &addr);
10781078
if (keys != NULL) {
10791079
LOG_DBG("Clear the current keys for %s", bt_addr_le_str(&addr));
1080-
bt_keys_clear(keys);
1080+
bt_keys_clear(conn->hdev, keys);
10811081
}
10821082

10831083
keys = bt_keys_get_type(conn->hdev, BT_KEYS_LTK_P256, conn->id, &addr);
@@ -1266,9 +1266,9 @@ static bool smp_br_pairing_allowed(struct bt_smp_br *smp)
12661266

12671267
addr.type = BT_ADDR_LE_PUBLIC;
12681268
bt_addr_copy(&addr.a, &conn->br.dst);
1269-
le_keys = bt_keys_find_addr(BT_ID_DEFAULT, &addr);
1269+
le_keys = bt_keys_find_addr(conn->hdev, BT_ID_DEFAULT, &addr);
12701270

1271-
key = bt_keys_find_link_key(&conn->br.dst);
1271+
key = bt_keys_find_link_key(conn->hdev, &conn->br.dst);
12721272
if (!key) {
12731273
return false;
12741274
}
@@ -1524,7 +1524,7 @@ static void convert_to_id_on_irk_match(struct bt_conn *conn, void *data)
15241524

15251525
if (bt_rpa_irk_matches(keys->irk.val, &conn->le.dst.a)) {
15261526
if (conn->le.keys != NULL && conn->le.keys != keys) {
1527-
bt_keys_clear(conn->le.keys);
1527+
bt_keys_clear(conn->hdev, conn->le.keys);
15281528
}
15291529

15301530
conn->le.keys = keys;
@@ -1564,7 +1564,7 @@ static uint8_t smp_br_ident_addr_info(struct bt_smp_br *smp,
15641564
}
15651565

15661566
/* Check the BLE connections that has RPA matched with this IRK */
1567-
keys = bt_keys_get_type(BT_KEYS_IRK, conn->id, &addr);
1567+
keys = bt_keys_get_type(conn->hdev, BT_KEYS_IRK, conn->id, &addr);
15681568
if (keys) {
15691569
bt_conn_foreach(BT_CONN_TYPE_LE, convert_to_id_on_irk_match, keys);
15701570
} else {

0 commit comments

Comments
 (0)