Skip to content

Commit c45cb01

Browse files
author
Gang He
committed
[int][new][zbt] Add SMP callback
Project: Bluetooth redmine: #5146, REDMINE-id ext-redmine: bug|feat#id When BLE paired, it will generate BT linkkey and give callback to upper layer. This callback is used when Zephyr BT stack is not used with BLE. Callback will report BT link key for CTKD. Affected branch: [master] Change-Id: Ib867c59e0d9f3a2373a8ed935c80ca60226ee287
1 parent f13bd17 commit c45cb01

File tree

2 files changed

+59
-28
lines changed

2 files changed

+59
-28
lines changed

zephyr_bt/host/smp.c

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -821,11 +821,46 @@ static void smp_sign_info_sent(struct bt_conn *conn, void *user_data, int err)
821821
}
822822
#endif /* CONFIG_BT_SIGNING */
823823

824+
825+
/* Generate BR key from BLE key
826+
* return 0: Success, >0 has error, but continue, < 0 has error, abort.
827+
*/
828+
int ble_key_2_bt_key(bool use_h7, uint8_t *le_key, uint8_t *lebr_key)
829+
{
830+
/* constants as specified in Core Spec Vol.3 Part H 2.4.2.4 */
831+
static const uint8_t lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
832+
uint8_t ilk[16];
833+
int r = 0;
834+
835+
if (use_h7)
836+
{
837+
/* constants as specified in Core Spec Vol.3 Part H 2.4.2.4 */
838+
static const uint8_t salt[16] = { 0x31, 0x70, 0x6d, 0x74,
839+
0x00, 0x00, 0x00, 0x00,
840+
0x00, 0x00, 0x00, 0x00,
841+
0x00, 0x00, 0x00, 0x00
842+
};
843+
r = bt_crypto_h7(salt, le_key, ilk);
844+
}
845+
else
846+
{
847+
/* constants as specified in Core Spec Vol.3 Part H 2.4.2.4 */
848+
static const uint8_t tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
849+
r = bt_crypto_h6(le_key, tmp1, ilk);
850+
}
851+
if (r == 0)
852+
{
853+
r = bt_crypto_h6(ilk, lebr, lebr_key);
854+
if (r < 0)
855+
r = -r;
856+
}
857+
return r;
858+
}
859+
824860
#if defined(CONFIG_BT_CLASSIC)
825861
static void sc_derive_link_key(struct bt_smp *smp)
826862
{
827863
/* constants as specified in Core Spec Vol.3 Part H 2.4.2.4 */
828-
static const uint8_t lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
829864
struct bt_conn *conn = smp->chan.chan.conn;
830865
struct bt_keys_link_key *link_key;
831866
uint8_t ilk[16];
@@ -844,36 +879,13 @@ static void sc_derive_link_key(struct bt_smp *smp)
844879
return;
845880
}
846881

847-
if (atomic_test_bit(smp->flags, SMP_FLAG_CT2))
848-
{
849-
/* constants as specified in Core Spec Vol.3 Part H 2.4.2.4 */
850-
static const uint8_t salt[16] = { 0x31, 0x70, 0x6d, 0x74,
851-
0x00, 0x00, 0x00, 0x00,
852-
0x00, 0x00, 0x00, 0x00,
853-
0x00, 0x00, 0x00, 0x00
854-
};
855-
856-
if (bt_crypto_h7(salt, conn->le.keys->ltk.val, ilk))
857-
{
858-
bt_keys_link_key_clear(link_key);
859-
return;
860-
}
861-
}
862-
else
863-
{
864-
/* constants as specified in Core Spec Vol.3 Part H 2.4.2.4 */
865-
static const uint8_t tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
882+
int r = ble_key_2_bt_key(atomic_test_bit(smp->flags, SMP_FLAG_CT2), conn->le.keys->ltk.val, link_key->val);
866883

867-
if (bt_crypto_h6(conn->le.keys->ltk.val, tmp1, ilk))
868-
{
869-
bt_keys_link_key_clear(link_key);
870-
return;
871-
}
872-
}
873-
874-
if (bt_crypto_h6(ilk, lebr, link_key->val))
884+
if (r)
875885
{
876886
bt_keys_link_key_clear(link_key);
887+
if (r < 0)
888+
return;
877889
}
878890

879891
link_key->flags |= BT_LINK_KEY_SC;
@@ -1940,6 +1952,15 @@ static void smp_pairing_complete(struct bt_smp *smp, uint8_t status)
19401952
{
19411953
sc_derive_link_key(smp);
19421954
}
1955+
#else
1956+
uint8_t lebr_key[16];
1957+
const struct bt_conn_auth_cb *smp_auth_cb = latch_auth_cb(smp);
1958+
1959+
if (ble_key_2_bt_key(atomic_test_bit(smp->flags, SMP_FLAG_CT2), conn->le.keys->ltk.val, lebr_key) == 0)
1960+
{
1961+
if (smp_auth_cb->le_br_key)
1962+
smp_auth_cb->le_br_key(conn, lebr_key);
1963+
}
19431964
#endif /* CONFIG_BT_CLASSIC */
19441965
bool bond_flag = atomic_test_bit(smp->flags, SMP_FLAG_BOND);
19451966
struct bt_conn_auth_info_cb *listener, *next;

zephyr_bt/include/zephyr/bluetooth/conn.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,6 +2302,16 @@ struct bt_conn_auth_cb
23022302
*/
23032303
void (*pairing_confirm)(struct bt_conn *conn);
23042304

2305+
/** @brief Request the user to enter a passkey.
2306+
*
2307+
* This callback will be called for a BR/EDR (Bluetooth Classic)
2308+
* key derived from BLE LTK.
2309+
*
2310+
* @param conn Connection where pairing is currently active.
2311+
* @param lebr_key BR key derived from LE connection LTK.
2312+
*/
2313+
void (*le_br_key)(struct bt_conn *conn, uint8_t *lebr_key);
2314+
23052315
#if defined(CONFIG_BT_CLASSIC)
23062316
/** @brief Request the user to enter a passkey.
23072317
*

0 commit comments

Comments
 (0)